2012-06-04 22:31:44 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2012 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.
|
|
|
|
*/
|
2013-02-01 09:02:26 +01:00
|
|
|
package io.netty.channel.nio;
|
2012-05-26 00:51:22 +02:00
|
|
|
|
2012-11-30 07:07:26 +01:00
|
|
|
|
2012-08-10 13:17:18 +02:00
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import io.netty.channel.ChannelException;
|
2012-10-25 02:08:11 +02:00
|
|
|
import io.netty.channel.EventLoopException;
|
2012-09-01 09:56:09 +02:00
|
|
|
import io.netty.channel.SingleThreadEventLoop;
|
2013-02-01 09:02:26 +01:00
|
|
|
import io.netty.channel.nio.AbstractNioChannel.NioUnsafe;
|
2013-03-13 22:49:08 +01:00
|
|
|
import io.netty.util.internal.SystemPropertyUtil;
|
2013-02-26 23:54:25 +01:00
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
2012-05-26 00:51:22 +02:00
|
|
|
|
2012-08-10 13:17:18 +02:00
|
|
|
import java.io.IOException;
|
2012-08-29 08:12:19 +02:00
|
|
|
import java.nio.channels.CancelledKeyException;
|
2012-09-01 09:56:09 +02:00
|
|
|
import java.nio.channels.SelectableChannel;
|
2012-08-29 08:12:19 +02:00
|
|
|
import java.nio.channels.SelectionKey;
|
|
|
|
import java.nio.channels.Selector;
|
2012-05-26 00:51:22 +02:00
|
|
|
import java.nio.channels.spi.SelectorProvider;
|
2012-08-10 13:17:18 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2012-12-14 03:43:04 +01:00
|
|
|
import java.util.ConcurrentModificationException;
|
2012-08-10 13:17:18 +02:00
|
|
|
import java.util.Iterator;
|
2012-08-18 11:48:44 +02:00
|
|
|
import java.util.Queue;
|
2012-08-10 13:17:18 +02:00
|
|
|
import java.util.Set;
|
2012-08-18 11:48:44 +02:00
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
2012-05-26 00:51:22 +02:00
|
|
|
import java.util.concurrent.ThreadFactory;
|
2012-08-10 13:17:18 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2012-05-26 00:51:22 +02:00
|
|
|
|
2012-08-28 02:10:17 +02:00
|
|
|
/**
|
|
|
|
* {@link SingleThreadEventLoop} implementation which register the {@link Channel}'s to a
|
|
|
|
* {@link Selector} and so does the multi-plexing of these in the event loop.
|
|
|
|
*
|
|
|
|
*/
|
2012-10-25 02:08:11 +02:00
|
|
|
public final class NioEventLoop extends SingleThreadEventLoop {
|
2012-05-26 00:51:22 +02:00
|
|
|
|
2012-08-10 13:17:18 +02:00
|
|
|
/**
|
|
|
|
* Internal Netty logger.
|
|
|
|
*/
|
2012-09-01 09:56:09 +02:00
|
|
|
private static final InternalLogger logger =
|
|
|
|
InternalLoggerFactory.getInstance(NioEventLoop.class);
|
2012-08-10 13:17:18 +02:00
|
|
|
|
2013-03-13 22:49:08 +01:00
|
|
|
private static final int CLEANUP_INTERVAL = 256; // XXX Hard-coded value, but won't need customization.
|
|
|
|
|
2013-03-22 04:59:47 +01:00
|
|
|
private static final int MIN_PREMATURE_SELECTOR_RETURNS = 3;
|
2013-03-22 04:37:11 +01:00
|
|
|
private static final int SELECTOR_AUTO_REBUILD_THRESHOLD;
|
2013-03-13 22:49:08 +01:00
|
|
|
|
|
|
|
// Workaround for JDK NIO bug.
|
|
|
|
//
|
|
|
|
// See:
|
|
|
|
// - http://bugs.sun.com/view_bug.do?bug_id=6427854
|
|
|
|
// - https://github.com/netty/netty/issues/203
|
|
|
|
static {
|
|
|
|
String key = "sun.nio.ch.bugLevel";
|
|
|
|
try {
|
|
|
|
String buglevel = System.getProperty(key);
|
|
|
|
if (buglevel == null) {
|
|
|
|
System.setProperty(key, "");
|
|
|
|
}
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
if (logger.isDebugEnabled()) {
|
2013-04-03 04:44:30 +02:00
|
|
|
logger.debug("Unable to get/set System Property: {}", key, e);
|
2013-03-13 22:49:08 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-22 03:48:41 +01:00
|
|
|
|
2013-03-22 06:33:47 +01:00
|
|
|
int selectorAutoRebuildThreshold = SystemPropertyUtil.getInt("io.netty.selectorAutoRebuildThreshold", 512);
|
2013-03-22 04:59:47 +01:00
|
|
|
if (selectorAutoRebuildThreshold < MIN_PREMATURE_SELECTOR_RETURNS) {
|
2013-03-22 04:37:11 +01:00
|
|
|
selectorAutoRebuildThreshold = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SELECTOR_AUTO_REBUILD_THRESHOLD = selectorAutoRebuildThreshold;
|
|
|
|
|
2013-03-13 22:49:08 +01:00
|
|
|
if (logger.isDebugEnabled()) {
|
2013-04-03 04:44:30 +02:00
|
|
|
logger.debug("io.netty.selectorAutoRebuildThreshold: {}", SELECTOR_AUTO_REBUILD_THRESHOLD);
|
2013-03-13 22:49:08 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-13 22:54:57 +01:00
|
|
|
|
2012-08-10 13:17:18 +02:00
|
|
|
/**
|
|
|
|
* The NIO {@link Selector}.
|
|
|
|
*/
|
2012-09-01 09:56:09 +02:00
|
|
|
Selector selector;
|
2012-08-10 13:17:18 +02:00
|
|
|
|
2012-09-01 09:56:09 +02:00
|
|
|
private final SelectorProvider provider;
|
2012-08-29 08:12:19 +02:00
|
|
|
|
2012-08-10 13:17:18 +02:00
|
|
|
/**
|
|
|
|
* Boolean that controls determines if a blocked Selector.select should
|
|
|
|
* break out of its selection process. In our case we use a timeone for
|
|
|
|
* the select method and the select method will block for that time unless
|
|
|
|
* waken up.
|
|
|
|
*/
|
2012-09-01 09:56:09 +02:00
|
|
|
private final AtomicBoolean wakenUp = new AtomicBoolean();
|
2013-03-22 04:37:11 +01:00
|
|
|
private boolean oldWakenUp;
|
2012-08-10 13:17:18 +02:00
|
|
|
|
2013-03-13 22:49:08 +01:00
|
|
|
private volatile int ioRatio = 50;
|
2012-08-10 13:17:18 +02:00
|
|
|
private int cancelledKeys;
|
2013-03-18 07:01:19 +01:00
|
|
|
private boolean needsToSelectAgain;
|
2012-08-10 13:17:18 +02:00
|
|
|
|
2012-08-18 11:40:21 +02:00
|
|
|
NioEventLoop(
|
2013-03-22 01:00:38 +01:00
|
|
|
NioEventLoopGroup parent, ThreadFactory threadFactory, SelectorProvider selectorProvider) {
|
|
|
|
super(parent, threadFactory);
|
2012-08-10 13:17:18 +02:00
|
|
|
if (selectorProvider == null) {
|
|
|
|
throw new NullPointerException("selectorProvider");
|
|
|
|
}
|
2012-08-29 08:03:32 +02:00
|
|
|
provider = selectorProvider;
|
|
|
|
selector = openSelector();
|
2012-06-06 16:02:47 +02:00
|
|
|
}
|
2012-05-26 00:51:22 +02:00
|
|
|
|
2012-08-29 08:03:32 +02:00
|
|
|
private Selector openSelector() {
|
2012-08-10 13:17:18 +02:00
|
|
|
try {
|
|
|
|
return provider.openSelector();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new ChannelException("failed to open a new selector", e);
|
|
|
|
}
|
2012-05-26 00:51:22 +02:00
|
|
|
}
|
|
|
|
|
2012-08-18 11:48:44 +02:00
|
|
|
@Override
|
|
|
|
protected Queue<Runnable> newTaskQueue() {
|
|
|
|
// This event loop never calls takeTask()
|
|
|
|
return new ConcurrentLinkedQueue<Runnable>();
|
|
|
|
}
|
|
|
|
|
2012-10-25 02:08:11 +02:00
|
|
|
/**
|
|
|
|
* Registers an arbitrary {@link SelectableChannel}, not necessarily created by Netty, to the {@link Selector}
|
|
|
|
* of this event loop. Once the specified {@link SelectableChannel} is registered, the specified {@code task} will
|
|
|
|
* be executed by this event loop when the {@link SelectableChannel} is ready.
|
|
|
|
*/
|
|
|
|
public void register(final SelectableChannel ch, final int interestOps, final NioTask<?> task) {
|
|
|
|
if (ch == null) {
|
|
|
|
throw new NullPointerException("ch");
|
|
|
|
}
|
|
|
|
if (interestOps == 0) {
|
|
|
|
throw new IllegalArgumentException("interestOps must be non-zero.");
|
|
|
|
}
|
|
|
|
if ((interestOps & ~ch.validOps()) != 0) {
|
|
|
|
throw new IllegalArgumentException(
|
2012-11-10 00:03:52 +01:00
|
|
|
"invalid interestOps: " + interestOps + "(validOps: " + ch.validOps() + ')');
|
2012-10-25 02:08:11 +02:00
|
|
|
}
|
|
|
|
if (task == null) {
|
|
|
|
throw new NullPointerException("task");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isShutdown()) {
|
|
|
|
throw new IllegalStateException("event loop shut down");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
ch.register(selector, interestOps, task);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new EventLoopException("failed to register a channel", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-14 03:23:30 +01:00
|
|
|
void executeWhenWritable(AbstractNioChannel channel, NioTask<SelectableChannel> task) {
|
2012-10-24 18:27:26 +02:00
|
|
|
if (channel == null) {
|
|
|
|
throw new NullPointerException("channel");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isShutdown()) {
|
|
|
|
throw new IllegalStateException("event loop shut down");
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectionKey key = channel.selectionKey();
|
2012-12-14 03:23:30 +01:00
|
|
|
channel.writableTasks.offer(task);
|
2013-02-11 02:39:50 +01:00
|
|
|
|
|
|
|
int interestOps = key.interestOps();
|
|
|
|
if ((interestOps & SelectionKey.OP_WRITE) == 0) {
|
|
|
|
key.interestOps(interestOps | SelectionKey.OP_WRITE);
|
2013-02-10 20:42:40 +01:00
|
|
|
}
|
2012-10-24 18:27:26 +02:00
|
|
|
}
|
|
|
|
|
2013-03-13 22:49:08 +01:00
|
|
|
/**
|
|
|
|
* Returns the percentage of the desired amount of time spent for I/O in the event loop.
|
|
|
|
*/
|
|
|
|
public int getIoRatio() {
|
|
|
|
return ioRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the percentage of the desired amount of time spent for I/O in the event loop. The default value is
|
|
|
|
* {@code 50}, which means the event loop will try to spend the same amount of time for I/O as for non-I/O tasks.
|
|
|
|
*/
|
|
|
|
public void setIoRatio(int ioRatio) {
|
|
|
|
if (ioRatio <= 0 || ioRatio >= 100) {
|
|
|
|
throw new IllegalArgumentException("ioRatio: " + ioRatio + " (expected: 0 < ioRatio < 100)");
|
|
|
|
}
|
|
|
|
this.ioRatio = ioRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces the current {@link Selector} of this event loop with newly created {@link Selector}s to work
|
|
|
|
* around the infamous epoll 100% CPU bug.
|
|
|
|
*/
|
2012-12-14 03:43:04 +01:00
|
|
|
public void rebuildSelector() {
|
|
|
|
if (!inEventLoop()) {
|
|
|
|
execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
rebuildSelector();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-09 23:03:07 +01:00
|
|
|
final Selector oldSelector = selector;
|
2012-12-14 03:43:04 +01:00
|
|
|
final Selector newSelector;
|
|
|
|
|
|
|
|
if (oldSelector == null) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-29 08:03:32 +02:00
|
|
|
|
2012-10-25 02:08:11 +02:00
|
|
|
try {
|
2013-03-22 03:48:41 +01:00
|
|
|
newSelector = openSelector();
|
2012-10-25 02:08:11 +02:00
|
|
|
} catch (Exception e) {
|
2012-12-14 03:43:04 +01:00
|
|
|
logger.warn("Failed to create a new Selector.", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register all channels to the new Selector.
|
|
|
|
int nChannels = 0;
|
|
|
|
for (;;) {
|
|
|
|
try {
|
|
|
|
for (SelectionKey key: oldSelector.keys()) {
|
|
|
|
Object a = key.attachment();
|
|
|
|
try {
|
|
|
|
if (key.channel().keyFor(newSelector) != null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int interestOps = key.interestOps();
|
|
|
|
key.cancel();
|
|
|
|
key.channel().register(newSelector, interestOps, a);
|
|
|
|
nChannels ++;
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.warn("Failed to re-register a Channel to the new Selector.", e);
|
|
|
|
if (a instanceof AbstractNioChannel) {
|
|
|
|
AbstractNioChannel ch = (AbstractNioChannel) a;
|
|
|
|
ch.unsafe().close(ch.unsafe().voidFuture());
|
|
|
|
} else {
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
|
|
|
|
invokeChannelUnregistered(task, key, e);
|
|
|
|
}
|
|
|
|
}
|
2012-10-25 02:08:11 +02:00
|
|
|
}
|
2012-12-14 03:43:04 +01:00
|
|
|
} catch (ConcurrentModificationException e) {
|
|
|
|
// Probably due to concurrent modification of the key set.
|
|
|
|
continue;
|
2012-10-25 02:08:11 +02:00
|
|
|
}
|
|
|
|
|
2012-12-14 03:43:04 +01:00
|
|
|
break;
|
2012-08-29 08:03:32 +02:00
|
|
|
}
|
2012-10-25 02:08:11 +02:00
|
|
|
|
2012-12-14 03:43:04 +01:00
|
|
|
selector = newSelector;
|
|
|
|
|
2012-08-29 08:03:32 +02:00
|
|
|
try {
|
2012-12-14 03:43:04 +01:00
|
|
|
// time to close the old selector as everything else is registered to the new one
|
|
|
|
oldSelector.close();
|
|
|
|
} catch (Throwable t) {
|
|
|
|
if (logger.isWarnEnabled()) {
|
|
|
|
logger.warn("Failed to close the old Selector.", t);
|
|
|
|
}
|
2012-08-29 08:03:32 +02:00
|
|
|
}
|
2012-10-25 02:08:11 +02:00
|
|
|
|
2012-12-14 03:43:04 +01:00
|
|
|
logger.info("Migrated " + nChannels + " channel(s) to the new Selector.");
|
2012-08-29 08:03:32 +02:00
|
|
|
}
|
2012-08-29 08:12:19 +02:00
|
|
|
|
2012-08-10 13:17:18 +02:00
|
|
|
@Override
|
|
|
|
protected void run() {
|
|
|
|
for (;;) {
|
2013-03-22 04:37:11 +01:00
|
|
|
oldWakenUp = wakenUp.getAndSet(false);
|
2012-08-10 13:17:18 +02:00
|
|
|
try {
|
2013-03-13 22:49:08 +01:00
|
|
|
if (hasTasks()) {
|
|
|
|
selectNow();
|
|
|
|
} else {
|
2013-03-22 03:48:41 +01:00
|
|
|
select();
|
2012-08-10 13:17:18 +02:00
|
|
|
|
2013-03-13 22:49:08 +01:00
|
|
|
// 'wakenUp.compareAndSet(false, true)' is always evaluated
|
|
|
|
// before calling 'selector.wakeup()' to reduce the wake-up
|
|
|
|
// overhead. (Selector.wakeup() is an expensive operation.)
|
|
|
|
//
|
|
|
|
// However, there is a race condition in this approach.
|
|
|
|
// The race condition is triggered when 'wakenUp' is set to
|
|
|
|
// true too early.
|
|
|
|
//
|
|
|
|
// 'wakenUp' is set to true too early if:
|
|
|
|
// 1) Selector is waken up between 'wakenUp.set(false)' and
|
|
|
|
// 'selector.select(...)'. (BAD)
|
|
|
|
// 2) Selector is waken up between 'selector.select(...)' and
|
|
|
|
// 'if (wakenUp.get()) { ... }'. (OK)
|
|
|
|
//
|
|
|
|
// In the first case, 'wakenUp' is set to true and the
|
|
|
|
// following 'selector.select(...)' will wake up immediately.
|
|
|
|
// Until 'wakenUp' is set to false again in the next round,
|
|
|
|
// 'wakenUp.compareAndSet(false, true)' will fail, and therefore
|
|
|
|
// any attempt to wake up the Selector will fail, too, causing
|
|
|
|
// the following 'selector.select(...)' call to block
|
|
|
|
// unnecessarily.
|
|
|
|
//
|
|
|
|
// To fix this problem, we wake up the selector again if wakenUp
|
|
|
|
// is true immediately after selector.select(...).
|
|
|
|
// It is inefficient in that it wakes up the selector for both
|
|
|
|
// the first case (BAD - wake-up required) and the second case
|
|
|
|
// (OK - no wake-up required).
|
|
|
|
|
|
|
|
if (wakenUp.get()) {
|
|
|
|
selector.wakeup();
|
|
|
|
}
|
2012-08-10 13:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cancelledKeys = 0;
|
2012-12-14 03:43:04 +01:00
|
|
|
|
2013-03-13 22:49:08 +01:00
|
|
|
final long ioStartTime = System.nanoTime();
|
2013-02-08 17:05:33 +01:00
|
|
|
processSelectedKeys();
|
2013-03-13 22:49:08 +01:00
|
|
|
final long ioTime = System.nanoTime() - ioStartTime;
|
2012-12-14 03:43:04 +01:00
|
|
|
|
2013-03-13 22:49:08 +01:00
|
|
|
final int ioRatio = this.ioRatio;
|
|
|
|
runAllTasks(ioTime * (100 - ioRatio) / ioRatio);
|
2012-08-10 13:17:18 +02:00
|
|
|
|
|
|
|
if (isShutdown()) {
|
|
|
|
closeAll();
|
Reduce the chance of RejectedExecutionException
When a Netty application shuts down, a user often sees a REE
(RejectedExecutionException).
A REE is raised due to various reasons we don't have control over, such
as:
- A client connects to a server while the server is shutting down.
- An event is triggered for a closed Channel while its event loop is
also shutting down. Some of them are:
- channelDeregistered (triggered after a channel is closed)
- freeIn/OutboundBuffer (triggered after channelDeregistered)
- userEventTriggered (triggered anytime)
To address this issue, a new method called confirmShutdown() has been
added to SingleThreadEventExecutor. After a user calls shutdown(),
confirmShutdown() runs any remaining tasks in the task queue and ensures
no events are triggered for last 2 seconds. If any task are added to
the task queue before 2 seconds passes, confirmShutdown() prevents the
event loop from terminating by returning false.
Now that SingleThreadEventExecutor needs to accept tasks even after
shutdown(), its execute() method only rejects the task after the event
loop is terminated (i.e. isTerminated() returns true.) Except that,
there's no change in semantics.
SingleThreadEventExecutor also checks if its subclass called
confirmShutdown() in its run() implementation, so that Netty developers
can make sure they shut down their event loop impementation correctly.
It also fixes a bug in AioSocketChannel, revealed by delayed shutdown,
where an inboundBufferUpdated() event is triggered on a closed Channel
with deallocated buffers.
Caveats:
Because SingleThreadEventExecutor.takeTask() does not have a notion of
timeout, confirmShutdown() adds a dummy task (WAKEUP_TASK) to wake up
takeTask() immediately and instead sleeps hard-coded 100ms. I'll
address this issue later by modifying takeTask() times out dynamically.
Miscellaneous changes:
SingleThreadEventExecutor.wakeup() now has the default implementation.
Instead of interrupting the current thread, it simply adds a dummy task
(WAKEUP_TASK) to the task queue, which is more elegant and efficient.
NioEventLoop is the only implementation that overrides it. All other
implementations' wakeup()s were removed thanks to this change.
2012-11-22 12:45:49 +01:00
|
|
|
if (confirmShutdown()) {
|
2012-08-10 13:17:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
2013-03-13 22:49:08 +01:00
|
|
|
logger.warn("Unexpected exception in the selector loop.", t);
|
2012-08-10 13:17:18 +02:00
|
|
|
|
|
|
|
// Prevent possible consecutive immediate failures that lead to
|
|
|
|
// excessive CPU consumption.
|
|
|
|
try {
|
|
|
|
Thread.sleep(1000);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
// Ignore.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-26 00:51:22 +02:00
|
|
|
}
|
|
|
|
|
2012-08-10 13:17:18 +02:00
|
|
|
@Override
|
|
|
|
protected void cleanup() {
|
|
|
|
try {
|
|
|
|
selector.close();
|
|
|
|
} catch (IOException e) {
|
2012-10-25 02:09:40 +02:00
|
|
|
logger.warn("Failed to close a selector.", e);
|
2012-08-10 13:17:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cancel(SelectionKey key) {
|
|
|
|
key.cancel();
|
|
|
|
cancelledKeys ++;
|
|
|
|
if (cancelledKeys >= CLEANUP_INTERVAL) {
|
|
|
|
cancelledKeys = 0;
|
2013-03-18 07:01:19 +01:00
|
|
|
needsToSelectAgain = true;
|
2012-08-10 13:17:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-18 07:01:19 +01:00
|
|
|
@Override
|
|
|
|
protected Runnable pollTask() {
|
|
|
|
Runnable task = super.pollTask();
|
|
|
|
if (needsToSelectAgain) {
|
|
|
|
selectAgain();
|
|
|
|
}
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
2012-08-10 13:17:18 +02:00
|
|
|
private void processSelectedKeys() {
|
2013-03-18 07:01:19 +01:00
|
|
|
needsToSelectAgain = false;
|
2012-08-10 13:17:18 +02:00
|
|
|
Set<SelectionKey> selectedKeys = selector.selectedKeys();
|
2012-09-13 10:25:59 +02:00
|
|
|
// check if the set is empty and if so just return to not create garbage by
|
|
|
|
// creating a new Iterator every time even if there is nothing to process.
|
|
|
|
// See https://github.com/netty/netty/issues/597
|
2012-08-10 13:17:18 +02:00
|
|
|
if (selectedKeys.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-18 07:01:19 +01:00
|
|
|
Iterator<SelectionKey> i = selectedKeys.iterator();
|
|
|
|
for (;;) {
|
|
|
|
final SelectionKey k = i.next();
|
|
|
|
final Object a = k.attachment();
|
|
|
|
i.remove();
|
2012-08-10 13:17:18 +02:00
|
|
|
|
2013-03-18 07:01:19 +01:00
|
|
|
if (a instanceof AbstractNioChannel) {
|
|
|
|
processSelectedKey(k, (AbstractNioChannel) a);
|
|
|
|
} else {
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
|
|
|
|
processSelectedKey(k, task);
|
2012-08-10 13:17:18 +02:00
|
|
|
}
|
2013-03-18 07:01:19 +01:00
|
|
|
|
|
|
|
if (!i.hasNext()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needsToSelectAgain) {
|
|
|
|
selectAgain();
|
|
|
|
selectedKeys = selector.selectedKeys();
|
|
|
|
|
|
|
|
// Create the iterator again to avoid ConcurrentModificationException
|
|
|
|
if (selectedKeys.isEmpty()) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
i = selectedKeys.iterator();
|
|
|
|
}
|
2012-08-10 13:17:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-25 02:08:11 +02:00
|
|
|
private static void processSelectedKey(SelectionKey k, AbstractNioChannel ch) {
|
|
|
|
final NioUnsafe unsafe = ch.unsafe();
|
2012-12-29 18:07:02 +01:00
|
|
|
if (!k.isValid()) {
|
|
|
|
// close the channel if the key is not valid anymore
|
|
|
|
unsafe.close(unsafe.voidFuture());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-24 18:27:26 +02:00
|
|
|
int readyOps = -1;
|
2012-10-25 02:08:11 +02:00
|
|
|
try {
|
2012-10-24 18:27:26 +02:00
|
|
|
readyOps = k.readyOps();
|
2012-10-25 02:08:11 +02:00
|
|
|
if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
|
|
|
|
unsafe.read();
|
|
|
|
if (!ch.isOpen()) {
|
|
|
|
// Connection already closed - no need to handle write.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((readyOps & SelectionKey.OP_WRITE) != 0) {
|
2012-10-24 18:27:26 +02:00
|
|
|
processWritable(k, ch);
|
2012-10-25 02:08:11 +02:00
|
|
|
}
|
|
|
|
if ((readyOps & SelectionKey.OP_CONNECT) != 0) {
|
2013-01-11 19:42:21 +01:00
|
|
|
// remove OP_CONNECT as otherwise Selector.select(..) will always return without blocking
|
|
|
|
// See https://github.com/netty/netty/issues/924
|
|
|
|
int ops = k.interestOps();
|
|
|
|
ops &= ~SelectionKey.OP_CONNECT;
|
|
|
|
k.interestOps(ops);
|
|
|
|
|
2012-10-25 02:08:11 +02:00
|
|
|
unsafe.finishConnect();
|
|
|
|
}
|
2012-10-24 18:27:26 +02:00
|
|
|
} catch (CancelledKeyException e) {
|
2012-12-27 22:24:07 +01:00
|
|
|
if (readyOps != -1 && (readyOps & SelectionKey.OP_WRITE) != 0) {
|
2012-10-24 18:27:26 +02:00
|
|
|
unregisterWritableTasks(ch);
|
|
|
|
}
|
2012-10-25 02:08:11 +02:00
|
|
|
unsafe.close(unsafe.voidFuture());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-24 18:27:26 +02:00
|
|
|
private static void processWritable(SelectionKey k, AbstractNioChannel ch) {
|
|
|
|
if (ch.writableTasks.isEmpty()) {
|
|
|
|
ch.unsafe().flushNow();
|
|
|
|
} else {
|
2012-12-14 03:23:30 +01:00
|
|
|
NioTask<SelectableChannel> task;
|
2012-10-24 18:27:26 +02:00
|
|
|
for (;;) {
|
|
|
|
task = ch.writableTasks.poll();
|
|
|
|
if (task == null) { break; }
|
|
|
|
processSelectedKey(ch.selectionKey(), task);
|
|
|
|
}
|
|
|
|
k.interestOps(k.interestOps() | SelectionKey.OP_WRITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void unregisterWritableTasks(AbstractNioChannel ch) {
|
2012-12-14 03:23:30 +01:00
|
|
|
NioTask<SelectableChannel> task;
|
2012-10-24 18:27:26 +02:00
|
|
|
for (;;) {
|
|
|
|
task = ch.writableTasks.poll();
|
|
|
|
if (task == null) {
|
|
|
|
break;
|
|
|
|
} else {
|
2012-12-14 03:17:31 +01:00
|
|
|
invokeChannelUnregistered(task, ch.selectionKey(), null);
|
2012-10-24 18:27:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-25 02:08:11 +02:00
|
|
|
private static void processSelectedKey(SelectionKey k, NioTask<SelectableChannel> task) {
|
2012-12-14 03:17:31 +01:00
|
|
|
int state = 0;
|
2012-10-25 02:08:11 +02:00
|
|
|
try {
|
|
|
|
task.channelReady(k.channel(), k);
|
2012-12-14 03:17:31 +01:00
|
|
|
state = 1;
|
2012-10-25 02:08:11 +02:00
|
|
|
} catch (Exception e) {
|
2012-12-14 03:17:31 +01:00
|
|
|
k.cancel();
|
|
|
|
invokeChannelUnregistered(task, k, e);
|
|
|
|
state = 2;
|
2012-10-25 02:08:11 +02:00
|
|
|
} finally {
|
2012-12-14 03:17:31 +01:00
|
|
|
switch (state) {
|
|
|
|
case 0:
|
2012-10-25 02:08:11 +02:00
|
|
|
k.cancel();
|
2012-12-14 03:17:31 +01:00
|
|
|
invokeChannelUnregistered(task, k, null);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (!k.isValid()) { // Cancelled by channelReady()
|
|
|
|
invokeChannelUnregistered(task, k, null);
|
|
|
|
}
|
|
|
|
break;
|
2012-10-25 02:08:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-10 13:17:18 +02:00
|
|
|
private void closeAll() {
|
2013-03-18 07:01:19 +01:00
|
|
|
selectAgain();
|
2012-08-10 13:17:18 +02:00
|
|
|
Set<SelectionKey> keys = selector.keys();
|
2012-10-24 18:27:26 +02:00
|
|
|
Collection<AbstractNioChannel> channels = new ArrayList<AbstractNioChannel>(keys.size());
|
2012-08-10 13:17:18 +02:00
|
|
|
for (SelectionKey k: keys) {
|
2012-10-25 02:08:11 +02:00
|
|
|
Object a = k.attachment();
|
2012-10-24 18:27:26 +02:00
|
|
|
if (a instanceof AbstractNioChannel) {
|
|
|
|
channels.add((AbstractNioChannel) a);
|
2012-10-25 02:08:11 +02:00
|
|
|
} else {
|
|
|
|
k.cancel();
|
2012-11-30 14:49:51 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
|
2012-12-14 03:17:31 +01:00
|
|
|
invokeChannelUnregistered(task, k, null);
|
2012-10-25 02:08:11 +02:00
|
|
|
}
|
2012-08-10 13:17:18 +02:00
|
|
|
}
|
|
|
|
|
2012-10-24 18:27:26 +02:00
|
|
|
for (AbstractNioChannel ch: channels) {
|
|
|
|
unregisterWritableTasks(ch);
|
2012-08-10 13:17:18 +02:00
|
|
|
ch.unsafe().close(ch.unsafe().voidFuture());
|
|
|
|
}
|
2012-06-02 02:51:19 +02:00
|
|
|
}
|
2012-05-26 00:51:22 +02:00
|
|
|
|
2012-12-14 03:17:31 +01:00
|
|
|
private static void invokeChannelUnregistered(NioTask<SelectableChannel> task, SelectionKey k, Throwable cause) {
|
2012-10-25 02:08:11 +02:00
|
|
|
try {
|
2012-12-14 03:17:31 +01:00
|
|
|
task.channelUnregistered(k.channel(), cause);
|
2012-10-25 02:08:11 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
logger.warn("Unexpected exception while running NioTask.channelUnregistered()", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-02 02:51:19 +02:00
|
|
|
@Override
|
2012-08-10 13:17:18 +02:00
|
|
|
protected void wakeup(boolean inEventLoop) {
|
2013-02-08 17:05:33 +01:00
|
|
|
if (!inEventLoop && wakenUp.compareAndSet(false, true)) {
|
2012-08-10 13:17:18 +02:00
|
|
|
selector.wakeup();
|
|
|
|
}
|
2012-05-26 00:51:22 +02:00
|
|
|
}
|
2013-01-16 12:20:35 +01:00
|
|
|
|
|
|
|
void selectNow() throws IOException {
|
|
|
|
try {
|
2013-03-22 06:33:47 +01:00
|
|
|
selector.selectNow();
|
2013-01-16 12:20:35 +01:00
|
|
|
} finally {
|
|
|
|
// restore wakup state if needed
|
|
|
|
if (wakenUp.get()) {
|
|
|
|
selector.wakeup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-13 22:49:08 +01:00
|
|
|
|
2013-03-22 03:48:41 +01:00
|
|
|
private void select() throws IOException {
|
|
|
|
Selector selector = this.selector;
|
2013-03-13 22:49:08 +01:00
|
|
|
try {
|
2013-03-22 06:33:47 +01:00
|
|
|
int selectCnt = 0;
|
|
|
|
long currentTimeNanos = System.nanoTime();
|
|
|
|
long selectDeadLineNanos = currentTimeNanos + delayNanos(currentTimeNanos);
|
|
|
|
for (;;) {
|
|
|
|
long timeoutMillis = (selectDeadLineNanos - currentTimeNanos + 500000L) / 1000000L;
|
|
|
|
if (timeoutMillis <= 0) {
|
|
|
|
if (selectCnt == 0) {
|
|
|
|
selector.selectNow();
|
|
|
|
selectCnt = 1;
|
2013-03-22 03:48:41 +01:00
|
|
|
}
|
2013-03-22 06:33:47 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-03-22 03:48:41 +01:00
|
|
|
|
2013-03-22 06:33:47 +01:00
|
|
|
int selectedKeys = selector.select(timeoutMillis);
|
|
|
|
selectCnt ++;
|
2013-03-22 03:48:41 +01:00
|
|
|
|
2013-03-22 06:33:47 +01:00
|
|
|
if (selectedKeys != 0 || oldWakenUp || wakenUp.get() || hasTasks()) {
|
|
|
|
// Selected something,
|
|
|
|
// waken up by user, or
|
|
|
|
// the task queue has a pending task.
|
|
|
|
break;
|
2013-03-22 03:48:41 +01:00
|
|
|
}
|
2013-03-22 06:33:47 +01:00
|
|
|
|
|
|
|
if (SELECTOR_AUTO_REBUILD_THRESHOLD > 0 &&
|
|
|
|
selectCnt >= SELECTOR_AUTO_REBUILD_THRESHOLD) {
|
|
|
|
// The selector returned prematurely many times in a row.
|
|
|
|
// Rebuild the selector to work around the problem.
|
|
|
|
logger.warn(
|
|
|
|
"Selector.select() returned prematurely {} times in a row; rebuilding selector.",
|
|
|
|
selectCnt);
|
|
|
|
|
|
|
|
rebuildSelector();
|
|
|
|
|
|
|
|
// Select again to populate selectedKeys.
|
|
|
|
selector.selectNow();
|
|
|
|
selectCnt = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTimeNanos = System.nanoTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selectCnt > MIN_PREMATURE_SELECTOR_RETURNS) {
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
logger.debug("Selector.select() returned prematurely {} times in a row.", selectCnt - 1);
|
2013-03-22 03:48:41 +01:00
|
|
|
}
|
2013-03-22 01:00:38 +01:00
|
|
|
}
|
2013-03-13 22:49:08 +01:00
|
|
|
} catch (CancelledKeyException e) {
|
|
|
|
if (logger.isDebugEnabled()) {
|
2013-03-22 04:37:11 +01:00
|
|
|
logger.debug(CancelledKeyException.class.getSimpleName() + " raised by a Selector - JDK bug?", e);
|
2013-03-13 22:49:08 +01:00
|
|
|
}
|
|
|
|
// Harmless exception - log anyway
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-18 07:01:19 +01:00
|
|
|
private void selectAgain() {
|
|
|
|
needsToSelectAgain = false;
|
2013-03-13 22:49:08 +01:00
|
|
|
try {
|
2013-03-22 06:33:47 +01:00
|
|
|
selector.selectNow();
|
2013-03-13 22:49:08 +01:00
|
|
|
} catch (Throwable t) {
|
|
|
|
logger.warn("Failed to update SelectionKeys.", t);
|
|
|
|
}
|
|
|
|
}
|
2012-05-26 00:51:22 +02:00
|
|
|
}
|