private void restoreTasks(NioThread owner, TaskNode node, boolean wakeup) { if (node == null) { return; } // we restore in the opposite order so that we get fifo. restoreTasks(owner, node.next, false); if (wakeup) { owner.addTaskAndWakeup(node.task); } else { owner.addTask(node.task); } }
@Override public String toString() { return getName(); } }
totalBytesReceived += thread.bytesTransceived(); totalFramesReceived += thread.framesTransceived(); totalPriorityFramesReceived += thread.priorityFramesTransceived(); totalEvents += thread.eventCount(); totalTaskCount += thread.completedTaskCount(); totalHandleCount += thread.handleCount(); writer.startSection(thread.getName()); writer.writeKeyValueEntry("frames-percentage", toPercentage(thread.framesTransceived(), totalFramesReceived)); writer.writeKeyValueEntry("frames", thread.framesTransceived()); writer.writeKeyValueEntry("priority-frames-percentage", toPercentage(thread.priorityFramesTransceived(), totalPriorityFramesReceived)); writer.writeKeyValueEntry("priority-frames", thread.priorityFramesTransceived()); writer.writeKeyValueEntry("bytes-percentage", toPercentage(thread.bytesTransceived(), totalBytesReceived)); writer.writeKeyValueEntry("bytes", thread.bytesTransceived()); writer.writeKeyValueEntry("events-percentage", toPercentage(thread.eventCount(), totalEvents)); writer.writeKeyValueEntry("events", thread.eventCount()); writer.writeKeyValueEntry("handle-count-percentage", toPercentage(thread.handleCount(), totalHandleCount)); writer.writeKeyValueEntry("handle-count", thread.handleCount()); writer.writeKeyValueEntry("tasks-percentage", toPercentage(thread.completedTaskCount(), totalTaskCount)); writer.writeKeyValueEntry("tasks", thread.completedTaskCount()); writer.endSection();
switch (selectMode) { case SELECT_WITH_FIX: selectLoopWithFix(); break; case SELECT_NOW: selectNowLoop(); break; case SELECT: selectLoop(); break; default: } catch (IOException nonFatalException) { selectorIOExceptionCount.inc(); logger.warning(getName() + " " + nonFatalException.toString(), nonFatalException); coolDown(); errorHandler.onError(null, e); } finally { closeSelector(); logger.finest(getName() + " finished");
NioThread thread = new NioThread( createThreadPoolName(threadNamePrefix, "IO") + "in-" + i, loggingService.getLogger(NioThread.class), idleStrategy); thread.id = i; thread.setSelectorWorkaroundTest(selectorWorkaroundTest); inputThreads[i] = thread; metricsRegistry.scanAndRegister(thread, "tcp.inputThread[" + thread.getName() + "]"); thread.start(); NioThread thread = new NioThread( createThreadPoolName(threadNamePrefix, "IO") + "out-" + i, loggingService.getLogger(NioThread.class), idleStrategy); thread.id = i; thread.setSelectorWorkaroundTest(selectorWorkaroundTest); outputThreads[i] = thread; metricsRegistry.scanAndRegister(thread, "tcp.outputThread[" + thread.getName() + "]"); thread.start();
/** * Migrates this pipeline to a different owner. * The migration logic is rather simple: * <p><ul> * <li>Submit a de-registration task to a current NioThread</li> * <li>The de-registration task submits a registration task to the new NioThread</li> * </ul></p> * * @param newOwner target NioThread this handler migrates to */ @Override public final void requestMigration(NioThread newOwner) { // todo: what happens when owner null. owner.addTaskAndWakeup(new StartMigrationTask(newOwner)); }
private void rebuildSelector() { selectorRebuildCount.inc(); Selector newSelector = newSelector(logger); Selector oldSelector = this.selector; // reset each pipeline's selectionKey, cancel the old keys for (SelectionKey key : oldSelector.keys()) { NioPipeline pipeline = (NioPipeline) key.attachment(); SelectableChannel channel = key.channel(); try { int ops = key.interestOps(); SelectionKey newSelectionKey = channel.register(newSelector, ops, pipeline); pipeline.setSelectionKey(newSelectionKey); } catch (ClosedChannelException e) { logger.info("Channel was closed while trying to register with new selector."); } catch (CancelledKeyException e) { // a CancelledKeyException may be thrown in key.interestOps // in this case, since the key is already cancelled, just do nothing ignore(e); } key.cancel(); } // close the old selector and substitute with new one closeSelector(); this.selector = newSelector; logger.warning("Recreated Selector because of possible java/network stack bug."); }
owner().addTask(this);
totalBytesReceived += thread.bytesTransceived(); totalFramesReceived += thread.framesTransceived(); totalPriorityFramesReceived += thread.priorityFramesTransceived(); totalEvents += thread.eventCount(); totalTaskCount += thread.completedTaskCount(); totalHandleCount += thread.handleCount(); writer.startSection(thread.getName()); writer.writeKeyValueEntry("frames-percentage", toPercentage(thread.framesTransceived(), totalFramesReceived)); writer.writeKeyValueEntry("frames", thread.framesTransceived()); writer.writeKeyValueEntry("priority-frames-percentage", toPercentage(thread.priorityFramesTransceived(), totalPriorityFramesReceived)); writer.writeKeyValueEntry("priority-frames", thread.priorityFramesTransceived()); writer.writeKeyValueEntry("bytes-percentage", toPercentage(thread.bytesTransceived(), totalBytesReceived)); writer.writeKeyValueEntry("bytes", thread.bytesTransceived()); writer.writeKeyValueEntry("events-percentage", toPercentage(thread.eventCount(), totalEvents)); writer.writeKeyValueEntry("events", thread.eventCount()); writer.writeKeyValueEntry("handle-count-percentage", toPercentage(thread.handleCount(), totalHandleCount)); writer.writeKeyValueEntry("handle-count", thread.handleCount()); writer.writeKeyValueEntry("tasks-percentage", toPercentage(thread.completedTaskCount(), totalTaskCount)); writer.writeKeyValueEntry("tasks", thread.completedTaskCount()); writer.endSection();
switch (selectMode) { case SELECT_WITH_FIX: selectLoopWithFix(); break; case SELECT_NOW: selectNowLoop(); break; case SELECT: selectLoop(); break; default: } catch (IOException nonFatalException) { selectorIOExceptionCount.inc(); logger.warning(getName() + " " + nonFatalException.toString(), nonFatalException); coolDown(); errorHandler.onError(null, e); } finally { closeSelector(); logger.finest(getName() + " finished");
NioThread thread = new NioThread( createThreadPoolName(threadNamePrefix, "IO") + "in-" + i, loggingService.getLogger(NioThread.class), idleStrategy); thread.id = i; thread.setSelectorWorkaroundTest(selectorWorkaroundTest); inputThreads[i] = thread; metricsRegistry.scanAndRegister(thread, "tcp.inputThread[" + thread.getName() + "]"); thread.start(); NioThread thread = new NioThread( createThreadPoolName(threadNamePrefix, "IO") + "out-" + i, loggingService.getLogger(NioThread.class), idleStrategy); thread.id = i; thread.setSelectorWorkaroundTest(selectorWorkaroundTest); outputThreads[i] = thread; metricsRegistry.scanAndRegister(thread, "tcp.outputThread[" + thread.getName() + "]"); thread.start();
/** * Migrates this pipeline to a different owner. * The migration logic is rather simple: * <p><ul> * <li>Submit a de-registration task to a current NioThread</li> * <li>The de-registration task submits a registration task to the new NioThread</li> * </ul></p> * * @param newOwner target NioThread this handler migrates to */ @Override public final void requestMigration(NioThread newOwner) { // todo: what happens when owner null. owner.addTaskAndWakeup(new StartMigrationTask(newOwner)); }
private void rebuildSelector() { selectorRebuildCount.inc(); Selector newSelector = newSelector(logger); Selector oldSelector = this.selector; // reset each pipeline's selectionKey, cancel the old keys for (SelectionKey key : oldSelector.keys()) { NioPipeline pipeline = (NioPipeline) key.attachment(); SelectableChannel channel = key.channel(); try { int ops = key.interestOps(); SelectionKey newSelectionKey = channel.register(newSelector, ops, pipeline); pipeline.setSelectionKey(newSelectionKey); } catch (ClosedChannelException e) { logger.info("Channel was closed while trying to register with new selector."); } catch (CancelledKeyException e) { // a CancelledKeyException may be thrown in key.interestOps // in this case, since the key is already cancelled, just do nothing ignore(e); } key.cancel(); } // close the old selector and substitute with new one closeSelector(); this.selector = newSelector; logger.warning("Recreated Selector because of possible java/network stack bug."); }
owner().addTask(this);
private void restoreTasks(NioThread owner, TaskNode node, boolean wakeup) { if (node == null) { return; } // we restore in the opposite order so that we get fifo. restoreTasks(owner, node.next, false); if (wakeup) { owner.addTaskAndWakeup(node.task); } else { owner.addTask(node.task); } }
@Override public void run() { for (NioChannel channel : channels) { final NioInboundPipeline inboundPipeline = channel.inboundPipeline; NioThread inputThread = inboundPipeline.owner(); if (inputThread != null) { inputThread.addTaskAndWakeup(new Runnable() { @Override public void run() { inboundPipeline.publishMetrics(); } }); } final NioOutboundPipeline outboundPipeline = channel.outboundPipeline; NioThread outputThread = outboundPipeline.owner(); if (outputThread != null) { outputThread.addTaskAndWakeup(new Runnable() { @Override public void run() { outboundPipeline.publishMetrics(); } }); } } } }
@Override public String toString() { return getName(); } }
@Override public void run() { for (NioChannel channel : channels) { final NioInboundPipeline inboundPipeline = channel.inboundPipeline; NioThread inputThread = inboundPipeline.owner(); if (inputThread != null) { inputThread.addTaskAndWakeup(new Runnable() { @Override public void run() { inboundPipeline.publishMetrics(); } }); } final NioOutboundPipeline outboundPipeline = channel.outboundPipeline; NioThread outputThread = outboundPipeline.owner(); if (outputThread != null) { outputThread.addTaskAndWakeup(new Runnable() { @Override public void run() { outboundPipeline.publishMetrics(); } }); } } } }
private void closeSelector() { if (logger.isFinestEnabled()) { logger.finest("Closing selector for:" + getName()); } try { selector.close(); } catch (Exception e) { logger.finest("Failed to close selector", e); } }
private void startMigration(final NioThread newOwner) throws IOException { assert owner == currentThread() : "startMigration can only run on the owning NioThread"; assert owner != newOwner : "newOwner can't be the same as the existing owner"; if (!socketChannel.isOpen()) { // if the channel is closed, we are done. return; } startedMigrations.inc(); unregisterOp(initialOps); selectionKey.cancel(); selectionKey = null; owner = null; ownerId = -1; newOwner.addTaskAndWakeup(new CompleteMigrationTask(newOwner)); } }