public void doWork() throws InterruptedException { if (!key.isValid()) { selectorThread.cleanupSelectionKey(key); return; } if (key.isReadable() || key.isWritable()) { cnxn.doIO(key); // Check if we shutdown or doIO() closed this connection if (stopped) { cnxn.close(); return; } if (!key.isValid()) { selectorThread.cleanupSelectionKey(key); return; } touchCnxn(cnxn); } // Mark this connection as once again ready for selection cnxn.enableSelectable(); // Push an update request on the queue to resume selecting // on the current set of interest ops, which may have changed // as a result of the I/O operations we just performed. if (!selectorThread.addInterestOpsUpdateRequest(key)) { cnxn.close(); } }
protected NIOServerCnxn createConnection(SocketChannel sock, SelectionKey sk, SelectorThread selectorThread) throws IOException { return new NIOServerCnxn(zkServer, sock, sk, this, selectorThread); }
/** Reads the first 4 bytes of lenBuffer, which could be true length or * four letter word. * * @param k selection key * @return true if length read, otw false (wasn't really the length) * @throws IOException if buffer size exceeds maxBuffer size */ private boolean readLength(SelectionKey k) throws IOException { // Read the length, now get the buffer int len = lenBuffer.getInt(); if (!initialized && checkFourLetterWord(sk, len)) { return false; } if (len < 0 || len > BinaryInputArchive.maxBuffer) { throw new IOException("Len error " + len); } if (!isZKServerRunning()) { throw new IOException("ZooKeeperServer not running"); } incomingBuffer = ByteBuffer.allocate(len); return true; }
/** Read the request payload (everything following the length prefix) */ private void readPayload() throws IOException, InterruptedException, ClientCnxnLimitException { if (incomingBuffer.remaining() != 0) { // have we read length bytes? int rc = sock.read(incomingBuffer); // sock is non-blocking, so ok if (rc < 0) { throw new EndOfStreamException( "Unable to read additional data from client sessionid 0x" + Long.toHexString(sessionId) + ", likely client has closed socket"); } } if (incomingBuffer.remaining() == 0) { // have we read length bytes? incomingBuffer.flip(); packetReceived(4 + incomingBuffer.remaining()); if (!initialized) { readConnectRequest(); } else { readRequest(); } lenBuffer.clear(); incomingBuffer = lenBuffer; } }
/** * Close the cnxn and remove it from the factory cnxns list. */ @Override public void close() { setStale(); if (!factory.removeCnxn(this)) { return; } if (zkServer != null) { zkServer.removeCnxn(this); } if (sk != null) { try { // need to cancel this selection key from the selector sk.cancel(); } catch (Exception e) { if (LOG.isDebugEnabled()) { LOG.debug("ignoring exception during selectionkey cancel", e); } } } closeSock(); }
if (isSocketOpen() == false) { LOG.warn("trying to do i/o on a null socket for session:0x" + Long.toHexString(sessionId)); if (incomingBuffer == lenBuffer) { // start of next request incomingBuffer.flip(); isPayload = readLength(k); incomingBuffer.clear(); } else { readPayload(); break; packetSent(); close(); } catch (CloseRequestException e) { close(); } catch (EndOfStreamException e) { LOG.warn(e.getMessage()); close(); } catch (IOException e) { LOG.warn("Exception causing close of session 0x" close();
if (isSocketOpen() == false) { LOG.warn("trying to do i/o on a null socket for session:0x" + Long.toHexString(sessionId)); if (incomingBuffer == lenBuffer) { // start of next request incomingBuffer.flip(); isPayload = readLength(k); incomingBuffer.clear(); } else { readPayload(); handleWrite(k); if (!initialized && !getReadInterest() && !getWriteInterest()) { throw new CloseRequestException("responded to info probe"); close(); } catch (CloseRequestException e) { close(); } catch (EndOfStreamException e) { LOG.warn(e.getMessage()); close(); } catch (ClientCnxnLimitException e) { close(); } catch (IOException e) {
ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); bb.putInt(bb.remaining() - 4).rewind(); sendBuffer(bb); + sock.socket().getRemoteSocketAddress() + ", probably expired"); sendCloseSession(); } else { LOG.info("Established session 0x" enableRecv(); close();
@Override public void cleanup() { cnxn.close(); } }
/** * send buffer without using the asynchronous * calls to selector and then close the socket * @param bb */ void sendBufferSync(ByteBuffer bb) { try { /* configure socket to be blocking * so that we dont have to do write in * a tight while loop */ if (bb != ServerCnxnFactory.closeConn) { if (sock.isOpen()) { sock.configureBlocking(true); sock.write(bb); } packetSent(); } } catch (IOException ie) { LOG.error("Error sending data synchronously ", ie); } }
/** * Handles read/write IO on connection. */ public void doIO(SelectionKey k) throws InterruptedException { super.doIO(k); }
protected void fastCloseSock(SocketChannel sc) { if (sc != null) { try { // Hard close immediately, discarding buffers sc.socket().setSoLinger(true, 0); } catch (SocketException e) { LOG.warn("Unable to set socket linger to 0, socket close" + " may stall in CLOSE_WAIT", e); } NIOServerCnxn.closeSock(sc); } } }
if (cnxn.isSelectable()) { cnxn.close();
/** Reads the first 4 bytes of lenBuffer, which could be true length or * four letter word. * * @param k selection key * @return true if length read, otw false (wasn't really the length) * @throws IOException if buffer size exceeds maxBuffer size */ private boolean readLength(SelectionKey k) throws IOException { // Read the length, now get the buffer int len = lenBuffer.getInt(); if (!initialized && checkFourLetterWord(k, len)) { return false; } if (len < 0 || len > BinaryInputArchive.maxBuffer) { throw new IOException("Len error " + len); } if (zk == null) { throw new IOException("ZooKeeperServer not running"); } incomingBuffer = ByteBuffer.allocate(len); return true; }
if (incomingBuffer == lenBuffer) { // start of next request incomingBuffer.flip(); isPayload = readLength(k); incomingBuffer.clear(); } else { readPayload(); break; packetSent(); LOG.debug("CancelledKeyException stack trace", e); close(); } catch (CloseRequestException e) { close(); } catch (EndOfStreamException e) { close(); } catch (IOException e) { LOG.warn("Exception causing close of session 0x" LOG.debug("IOException stack trace", e); close();
/** Read the request payload (everything following the length prefix) */ private void readPayload() throws IOException, InterruptedException { if (incomingBuffer.remaining() != 0) { // have we read length bytes? int rc = sock.read(incomingBuffer); // sock is non-blocking, so ok if (rc < 0) { throw new EndOfStreamException( "Unable to read additional data from client sessionid 0x" + Long.toHexString(sessionId) + ", likely client has closed socket"); } } if (incomingBuffer.remaining() == 0) { // have we read length bytes? packetReceived(); incomingBuffer.flip(); if (!initialized) { readConnectRequest(); } else { readRequest(); } lenBuffer.clear(); incomingBuffer = lenBuffer; } }
@SuppressWarnings("unchecked") private void closeSessionWithoutWakeup(long sessionId) { NIOServerCnxn cnxn = (NIOServerCnxn) sessionMap.remove(sessionId); if (cnxn != null) { try { cnxn.close(); } catch (Exception e) { LOG.warn("exception during session close", e); } } }
/** * send buffer without using the asynchronous * calls to selector and then close the socket * @param bb */ void sendBufferSync(ByteBuffer bb) { try { /* configure socket to be blocking * so that we dont have to do write in * a tight while loop */ sock.configureBlocking(true); if (bb != ServerCnxnFactory.closeConn) { if (sock.isOpen()) { sock.write(bb); } packetSent(); } } catch (IOException ie) { LOG.error("Error sending data synchronously ", ie); } }
c.doIO(k); } else { if (LOG.isDebugEnabled()) {