Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to schedule tasks up to Long.MAX_VALUE #7972

Merged
merged 1 commit into from May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -150,7 +150,7 @@ public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
if (delay < 0) {
delay = 0;
}
validateScheduled(delay, unit);
validateScheduled0(delay, unit);

return schedule(new ScheduledFutureTask<Void>(
this, command, null, ScheduledFutureTask.deadlineNanos(unit.toNanos(delay))));
Expand All @@ -163,7 +163,7 @@ public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUni
if (delay < 0) {
delay = 0;
}
validateScheduled(delay, unit);
validateScheduled0(delay, unit);

return schedule(new ScheduledFutureTask<V>(
this, callable, ScheduledFutureTask.deadlineNanos(unit.toNanos(delay))));
Expand All @@ -181,8 +181,8 @@ public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDela
throw new IllegalArgumentException(
String.format("period: %d (expected: > 0)", period));
}
validateScheduled(initialDelay, unit);
validateScheduled(period, unit);
validateScheduled0(initialDelay, unit);
validateScheduled0(period, unit);

return schedule(new ScheduledFutureTask<Void>(
this, Executors.<Void>callable(command, null),
Expand All @@ -202,17 +202,25 @@ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialD
String.format("delay: %d (expected: > 0)", delay));
}

validateScheduled(initialDelay, unit);
validateScheduled(delay, unit);
validateScheduled0(initialDelay, unit);
validateScheduled0(delay, unit);

return schedule(new ScheduledFutureTask<Void>(
this, Executors.<Void>callable(command, null),
ScheduledFutureTask.deadlineNanos(unit.toNanos(initialDelay)), -unit.toNanos(delay)));
}

@SuppressWarnings("deprecation")
private void validateScheduled0(long amount, TimeUnit unit) {
validateScheduled(amount, unit);
}

/**
* Sub-classes may override this to restrict the maximal amount of time someone can use to schedule a task.
*
* @deprecated will be removed in the future.
*/
@Deprecated
protected void validateScheduled(long amount, TimeUnit unit) {
// NOOP
}
Expand Down
Expand Up @@ -35,7 +35,9 @@ static long nanoTime() {
}

static long deadlineNanos(long delay) {
return nanoTime() + delay;
long deadlineNanos = nanoTime() + delay;
// Guard against overflow
return deadlineNanos < 0 ? Long.MAX_VALUE : deadlineNanos;
}

private final long id = nextTaskId.getAndIncrement();
Expand Down
@@ -0,0 +1,27 @@
/*
* Copyright 2018 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.util.concurrent;

import org.junit.Assert;
import org.junit.Test;

public class ScheduledFutureTaskTest {

@Test
public void testDeadlineNanosNotOverflow() {
Assert.assertEquals(Long.MAX_VALUE, ScheduledFutureTask.deadlineNanos(Long.MAX_VALUE));
}
}
Expand Up @@ -37,7 +37,6 @@
import java.util.Queue;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import static java.lang.Math.min;
Expand Down Expand Up @@ -80,7 +79,7 @@ public Integer call() throws Exception {
private volatile int ioRatio = 50;

// See http://man7.org/linux/man-pages/man2/timerfd_create.2.html.
static final long MAX_SCHEDULED_DAYS = TimeUnit.SECONDS.toDays(999999999);
private static final long MAX_SCHEDULED_TIMERFD_NS = 999999999;

EpollEventLoop(EventLoopGroup parent, Executor executor, int maxEvents,
SelectStrategy strategy, RejectedExecutionHandler rejectedExecutionHandler) {
Expand Down Expand Up @@ -237,7 +236,7 @@ private int epollWait(boolean oldWakeup) throws IOException {
long totalDelay = delayNanos(System.nanoTime());
int delaySeconds = (int) min(totalDelay / 1000000000L, Integer.MAX_VALUE);
return Native.epollWait(epollFd, events, timerFd, delaySeconds,
(int) min(totalDelay - delaySeconds * 1000000000L, Integer.MAX_VALUE));
(int) min(MAX_SCHEDULED_TIMERFD_NS, totalDelay - delaySeconds * 1000000000L));
}

private int epollWaitNow() throws IOException {
Expand Down Expand Up @@ -453,12 +452,4 @@ protected void cleanup() {
events.free();
}
}

@Override
protected void validateScheduled(long amount, TimeUnit unit) {
long days = unit.toDays(amount);
if (days > MAX_SCHEDULED_DAYS) {
throw new IllegalArgumentException("days: " + days + " (expected: < " + MAX_SCHEDULED_DAYS + ')');
}
}
}
Expand Up @@ -24,32 +24,11 @@

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class EpollEventLoopTest {

@Test(timeout = 5000L)
public void testScheduleBigDelayOverMax() {
EventLoopGroup group = new EpollEventLoopGroup(1);

final EventLoop el = group.next();
try {
el.schedule(new Runnable() {
@Override
public void run() {
// NOOP
}
}, Integer.MAX_VALUE, TimeUnit.DAYS);
fail();
} catch (IllegalArgumentException expected) {
// expected
}

group.shutdownGracefully();
}

@Test
public void testScheduleBigDelay() {
public void testScheduleBigDelayNotOverflow() {
EventLoopGroup group = new EpollEventLoopGroup(1);

final EventLoop el = group.next();
Expand All @@ -58,7 +37,7 @@ public void testScheduleBigDelay() {
public void run() {
// NOOP
}
}, EpollEventLoop.MAX_SCHEDULED_DAYS, TimeUnit.DAYS);
}, Long.MAX_VALUE, TimeUnit.MILLISECONDS);

assertFalse(future.awaitUninterruptibly(1000));
assertTrue(future.cancel(true));
Expand Down
Expand Up @@ -33,7 +33,6 @@
import java.util.Queue;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import static io.netty.channel.kqueue.KQueueEventArray.deleteGlobalRefs;
Expand Down Expand Up @@ -77,8 +76,6 @@ public Integer call() throws Exception {
private volatile int wakenUp;
private volatile int ioRatio = 50;

static final long MAX_SCHEDULED_DAYS = 365 * 3;

KQueueEventLoop(EventLoopGroup parent, Executor executor, int maxEvents,
SelectStrategy strategy, RejectedExecutionHandler rejectedExecutionHandler) {
super(parent, executor, false, DEFAULT_MAX_PENDING_TASKS, rejectedExecutionHandler);
Expand Down Expand Up @@ -370,12 +367,4 @@ private static void handleLoopException(Throwable t) {
// Ignore.
}
}

@Override
protected void validateScheduled(long amount, TimeUnit unit) {
long days = unit.toDays(amount);
if (days > MAX_SCHEDULED_DAYS) {
throw new IllegalArgumentException("days: " + days + " (expected: < " + MAX_SCHEDULED_DAYS + ')');
}
}
}
Expand Up @@ -24,32 +24,11 @@

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class KQueueEventLoopTest {

@Test(timeout = 5000L)
public void testScheduleBigDelayOverMax() {
EventLoopGroup group = new KQueueEventLoopGroup(1);

final EventLoop el = group.next();
try {
el.schedule(new Runnable() {
@Override
public void run() {
// NOOP
}
}, Integer.MAX_VALUE, TimeUnit.DAYS);
fail();
} catch (IllegalArgumentException expected) {
// expected
}

group.shutdownGracefully();
}

@Test
public void testScheduleBigDelay() {
public void testScheduleBigDelayNotOverflow() {
EventLoopGroup group = new KQueueEventLoopGroup(1);

final EventLoop el = group.next();
Expand All @@ -58,7 +37,7 @@ public void testScheduleBigDelay() {
public void run() {
// NOOP
}
}, KQueueEventLoop.MAX_SCHEDULED_DAYS, TimeUnit.DAYS);
}, Long.MAX_VALUE, TimeUnit.MILLISECONDS);

assertFalse(future.awaitUninterruptibly(1000));
assertTrue(future.cancel(true));
Expand Down
10 changes: 0 additions & 10 deletions transport/src/main/java/io/netty/channel/nio/NioEventLoop.java
Expand Up @@ -113,8 +113,6 @@ public Void run() {
}
}

static final long MAX_SCHEDULED_DAYS = 365 * 3;

/**
* The NIO {@link Selector}.
*/
Expand Down Expand Up @@ -825,12 +823,4 @@ private void selectAgain() {
logger.warn("Failed to update SelectionKeys.", t);
}
}

@Override
protected void validateScheduled(long amount, TimeUnit unit) {
long days = unit.toDays(amount);
if (days > MAX_SCHEDULED_DAYS) {
throw new IllegalArgumentException("days: " + days + " (expected: < " + MAX_SCHEDULED_DAYS + ')');
}
}
}
23 changes: 2 additions & 21 deletions transport/src/test/java/io/netty/channel/nio/NioEventLoopTest.java
Expand Up @@ -73,27 +73,8 @@ public void run() {
}
}

@Test(timeout = 5000L)
public void testScheduleBigDelayOverMax() {
EventLoopGroup group = new NioEventLoopGroup(1);
final EventLoop el = group.next();
try {
el.schedule(new Runnable() {
@Override
public void run() {
// NOOP
}
}, Integer.MAX_VALUE, TimeUnit.DAYS);
fail();
} catch (IllegalArgumentException expected) {
// expected
}

group.shutdownGracefully();
}

@Test
public void testScheduleBigDelay() {
public void testScheduleBigDelayNotOverflow() {
EventLoopGroup group = new NioEventLoopGroup(1);

final EventLoop el = group.next();
Expand All @@ -102,7 +83,7 @@ public void testScheduleBigDelay() {
public void run() {
// NOOP
}
}, NioEventLoop.MAX_SCHEDULED_DAYS, TimeUnit.DAYS);
}, Long.MAX_VALUE, TimeUnit.MILLISECONDS);

assertFalse(future.awaitUninterruptibly(1000));
assertTrue(future.cancel(true));
Expand Down