private void doConnect(SelectionKey key) { SocketChannel sc = (SocketChannel) key.channel(); TCConnectionImpl conn = (TCConnectionImpl) key.attachment(); try { if (sc.finishConnect()) { sc.register(selector, SelectionKey.OP_READ, conn); conn.finishConnect(); } else { String errMsg = "finishConnect() returned false, but no exception thrown"; if (logger.isInfoEnabled()) { logger.info(errMsg); } conn.fireErrorEvent(new Exception(errMsg), null); } } catch (IOException ioe) { if (logger.isInfoEnabled()) { logger.info("IOException attempting to finish socket connection", ioe); } conn.fireErrorEvent(ioe, null); } }
private void addConnection(TCConnectionImpl connection, int initialWeight) { synchronized (managedConnectionsMap) { Assert.eval(!managedConnectionsMap.containsKey(connection)); managedConnectionsMap.put(connection, initialWeight); this.clientWeights += initialWeight; connection.addListener(this); } }
/** * Change thread ownership of a connection or upgrade weight. * * @param connection : connection which has to be transfered from the main selector thread to a * new worker comm thread that has the least weight. If the connection is already managed by a * comm thread, then just update connection's weight. * @param addWeightBy : upgrade weight of connection * @param channel : SocketChannel for the passed in connection */ public void addWeight( final TCConnectionImpl connection, final int addWeightBy, final SocketChannel channel) { synchronized (managedConnectionsMap) { // this connection is already handled by a WorkerComm if (this.managedConnectionsMap.containsKey(connection)) { this.clientWeights += addWeightBy; this.managedConnectionsMap.put( connection, this.managedConnectionsMap.get(connection) + addWeightBy); return; } } // MainComm Thread if (workerCommMgr == null) { return; } readerComm.unregister(channel); final CoreNIOServices workerComm = workerCommMgr.getNextWorkerComm(); connection.setCommWorker(workerComm); workerComm.addConnection(connection, addWeightBy); workerComm.requestReadWriteInterest(connection, channel); }
private void selectLoop() throws IOException { Assert.eval(Thread.currentThread() == this); Selector localSelector = this.selector; LinkedBlockingQueue localSelectorTasks = this.selectorTasks; while (true) { final int numKeys; try { numKeys = localSelector.select(); } catch (IOException ioe) { if (NIOWorkarounds.linuxSelectWorkaround(ioe)) { logger.warn("working around Sun bug 4504001"); continue; } if (NIOWorkaroundsTemp.solarisSelectWorkaround(ioe)) { logger.warn("working around Solaris select IOException"); continue; } throw ioe; } catch (CancelledKeyException cke) { logger.warn("Cencelled Key " + cke); continue; } if (isStopRequested()) { if (logger.isDebugEnabled()) { logger.debug("Select loop terminating"); } return; } boolean isInterrupted = false; // run any pending selector tasks while (true) { Runnable task; while (true) { try { task = (Runnable) localSelectorTasks.poll(0, TimeUnit.MILLISECONDS); break; } catch (InterruptedException ie) { logger.error("Error getting task from task queue", ie); isInterrupted = true; } } if (null == task) { break; } try { task.run(); } catch (Exception e) { logger.error("error running selector task", e); } } Util.selfInterruptIfNeeded(isInterrupted); final Set selectedKeys = localSelector.selectedKeys(); if ((0 == numKeys) && (0 == selectedKeys.size())) { continue; } for (Iterator iter = selectedKeys.iterator(); iter.hasNext(); ) { SelectionKey key = (SelectionKey) iter.next(); iter.remove(); if (null == key) { logger.error("Selection key is null"); continue; } try { if (key.isAcceptable()) { doAccept(key); continue; } if (key.isConnectable()) { doConnect(key); continue; } if (isReader() && key.isValid() && key.isReadable()) { int read; TCChannelReader reader = (TCChannelReader) key.attachment(); do { read = reader.doRead(); this.bytesRead.addAndGet(read); } while ((read != 0) && key.isReadable()); } if (key.isValid() && !isReader() && key.isWritable()) { int written = ((TCChannelWriter) key.attachment()).doWrite(); this.bytesWritten.addAndGet(written); } TCConnection conn = (TCConnection) key.attachment(); if (conn != null && conn.isClosePending()) { conn.asynchClose(); } } catch (CancelledKeyException cke) { logger.info("selection key cancelled key@" + key.hashCode()); } catch (Exception e) { // DEV-9369. Do not reconnect on fatal errors. logger.info("Unhandled exception occured on connection layer", e); TCConnectionImpl conn = (TCConnectionImpl) key.attachment(); // TCConnectionManager will take care of closing and cleaning up resources conn.fireErrorEvent(new RuntimeException(e), null); } } // for } // while (true) }