public byte[] read(int readSize) throws IOException { InputStream input = this.input; byte[] data = new byte[readSize]; int remain = readSize; if (input == null) { throw new SocketException("Socket already closed."); } while (remain > 0) { try { int read = input.read(data, readSize - remain, remain); if (read > -1) { remain -= read; } else { throw new IOException("EOF encountered."); } } catch (SocketTimeoutException te) { if (Thread.interrupted()) { throw new ClosedByInterruptException(); } } } return data; }
throw new RuntimeException(e.getMessage()); } catch (IOException e) { logger.error("error while calling spill() on " + c, e); throw new RuntimeException(e.getMessage()); } catch (IOException e) { logger.error("error while calling spill() on " + consumer, e);
Thread.currentThread().interrupt(); if ( !isOpen() ) { ClosedByInterruptException cie = new ClosedByInterruptException(); cie.setStackTrace(e.getStackTrace()); throw cie; } else {
public byte[] read(int readSize, int timeout) throws IOException { InputStream input = this.input; byte[] data = new byte[readSize]; int remain = readSize; int accTimeout = 0; if (input == null) { throw new SocketException("Socket already closed."); } while (remain > 0 && accTimeout < timeout) { try { int read = input.read(data, readSize - remain, remain); if (read > -1) { remain -= read; } else { throw new IOException("EOF encountered."); } } catch (SocketTimeoutException te) { if (Thread.interrupted()) { throw new ClosedByInterruptException(); } accTimeout += SO_TIMEOUT; } } if (remain > 0 && accTimeout >= timeout) { throw new SocketTimeoutException("Timeout occurred, failed to read total " + readSize + " bytes in " + timeout + " milliseconds, actual read only " + (readSize - remain) + " bytes"); } return data; }
throw new RuntimeException(e.getMessage()); } catch (IOException e) { logger.error("error while calling spill() on " + c, e); throw new RuntimeException(e.getMessage()); } catch (IOException e) { logger.error("error while calling spill() on " + consumer, e);
@Override public void read(byte[] data, int off, int len, int timeout) throws IOException { InputStream input = this.input; int accTimeout = 0; if (input == null) { throw new SocketException("Socket already closed."); } int n = 0; while (n < len && accTimeout < timeout) { try { int read = input.read(data, off + n, len - n); if (read > -1) { n += read; } else { throw new IOException("EOF encountered."); } } catch (SocketTimeoutException te) { if (Thread.interrupted()) { throw new ClosedByInterruptException(); } accTimeout += SO_TIMEOUT; } } if (n < len && accTimeout >= timeout) { throw new SocketTimeoutException("Timeout occurred, failed to read total " + len + " bytes in " + timeout + " milliseconds, actual read only " + n + " bytes"); } }
throw new RuntimeException(e.getMessage()); } catch (IOException e) { logger.error("error while calling spill() on " + c, e); throw new RuntimeException(e.getMessage()); } catch (IOException e) { logger.error("error while calling spill() on " + consumer, e);
@Override public void write(final byte[] b, final int off, final int len) throws IOException { final ByteBuffer buffer = ByteBuffer.wrap(b, off, len); final int timeoutMillis = this.timeout; long maxTime = System.currentTimeMillis() + timeoutMillis; int bytesWritten; long sleepNanos = 1L; while (buffer.hasRemaining()) { bytesWritten = channel.write(buffer); if (bytesWritten == 0) { if (System.currentTimeMillis() > maxTime) { throw new SocketTimeoutException("Timed out writing to socket"); } try { TimeUnit.NANOSECONDS.sleep(sleepNanos); } catch (InterruptedException e) { close(); Thread.currentThread().interrupt(); // set the interrupt status throw new ClosedByInterruptException(); // simulate an interrupted blocked write operation } sleepNanos = Math.min(sleepNanos * 2, CHANNEL_FULL_WAIT_NANOS); } else { maxTime = System.currentTimeMillis() + timeoutMillis; } } }
} catch (ClosedByInterruptException ie1) { LOGGER.info("Retrying...."); LOGGER.error("Getting Error: " + ie1.getMessage(), ie1); return true; } finally {
@Override public void write(final int b) throws IOException { oneByteBuffer.flip(); oneByteBuffer.clear(); oneByteBuffer.put((byte) b); oneByteBuffer.flip(); final int timeoutMillis = this.timeout; long maxTime = System.currentTimeMillis() + timeoutMillis; int bytesWritten; long sleepNanos = 1L; while (oneByteBuffer.hasRemaining()) { bytesWritten = channel.write(oneByteBuffer); if (bytesWritten == 0) { if (System.currentTimeMillis() > maxTime) { throw new SocketTimeoutException("Timed out writing to socket"); } try { TimeUnit.NANOSECONDS.sleep(sleepNanos); } catch (InterruptedException e) { close(); Thread.currentThread().interrupt(); // set the interrupt status throw new ClosedByInterruptException(); // simulate an interrupted blocked write operation } sleepNanos = Math.min(sleepNanos * 2, CHANNEL_FULL_WAIT_NANOS); } else { return; } } }
public void enqueue(IndexOperation indexOp) throws AnalyticsException { try { this.primaryQueue.enqueue(indexOp.getBytes()); } catch (ClosedByInterruptException e) { throw new AnalyticsInterruptException("Error in index data enqueue (Interrupted..): " + e.getMessage(), e); } catch (IOException e) { throw new AnalyticsException("Error in index data enqueue: " + e.getMessage(), e); } }
private void writeFully(final ByteBuffer src) throws IOException { long lastByteWrittenTime = System.currentTimeMillis(); int bytesWritten = 0; while (src.hasRemaining()) { if (interrupted) { throw new TransmissionDisabledException(); } final int written = channel.write(src); bytesWritten += written; final long now = System.currentTimeMillis(); long sleepNanos = 1L; if (written > 0) { lastByteWrittenTime = now; } else { if (now > lastByteWrittenTime + timeoutMillis) { throw new SocketTimeoutException("Timed out writing to socket connected to " + hostname + ":" + port); } try { TimeUnit.NANOSECONDS.sleep(sleepNanos); } catch (final InterruptedException e) { close(); Thread.currentThread().interrupt(); // set the interrupt status throw new ClosedByInterruptException(); } sleepNanos = Math.min(sleepNanos * 2, BUFFER_FULL_EMPTY_WAIT_NANOS); } } logger.trace("{} Wrote {} bytes", this, bytesWritten); }
@Override public void run() { while (!serverExecutorService.isShutdown()) { try { LOG.debug("ServerTask: accepting... this={}, local.port={}", this, getLocalPort()); Socket acceptSocket = serverSocket.accept(); LOG.debug("ServerTask: accepted. this={}, local.port={}, remote.port={}", this, getLocalPort(), acceptSocket.getPort()); serverExecutorService.execute( new AcceptTask(serverExecutorService, lastEventTimeStampMilli, eventHandler, acceptSocket)); } catch (RejectedExecutionException e) { LOG.debug("ServerTask: ServerSocket.accept() failed[{}]: this={}", e.getMessage(), this); } catch (ClosedByInterruptException e) { LOG.debug("ServerTask: ServerSocket.accept() failed[{}]: this={}", e.getMessage(), this); } catch (IOException e) { LOG.warn("ServerTask: ServerSocket.accept() failed[{}]: this={}", e.getMessage(), this); } } try { serverSocket.close(); } catch (IOException e) { LOG.warn("ServerTask: ServerSocketChannel.close() failed", e); } LOG.info("ServerTask: Finishing ServerTask...: this={}", this); }
private int readData(final ByteBuffer dest) throws IOException { final long startTime = System.currentTimeMillis(); while (true) { if (interrupted) { throw new TransmissionDisabledException(); } if (dest.remaining() == 0) { return 0; } final int readCount = channel.read(dest); long sleepNanos = 1L; if (readCount == 0) { if (System.currentTimeMillis() > startTime + timeoutMillis) { throw new SocketTimeoutException("Timed out reading from socket connected to " + hostname + ":" + port); } try { TimeUnit.NANOSECONDS.sleep(sleepNanos); } catch (InterruptedException e) { close(); Thread.currentThread().interrupt(); // set the interrupt status throw new ClosedByInterruptException(); } sleepNanos = Math.min(sleepNanos * 2, BUFFER_FULL_EMPTY_WAIT_NANOS); continue; } logger.trace("{} Read {} bytes", this, readCount); return readCount; } }
"Replica loop unexpected interrupt."); throw new InterruptedException (closedByInterruptException.getMessage()); } catch (IOException e) {
/** * Indicates the end of a code section that has been started with * {@code begin()} and that includes a potentially blocking I/O operation. * * @param success * pass {@code true} if the blocking operation has succeeded and * has had a noticeable effect; {@code false} otherwise. * @throws AsynchronousCloseException * if this channel is closed by another thread while this method * is executing. * @throws ClosedByInterruptException * if another thread interrupts the calling thread while this * method is executing. */ protected final void end(boolean success) throws AsynchronousCloseException { Thread.currentThread().popInterruptAction$(interruptAndCloseRunnable); if (interrupted) { interrupted = false; throw new ClosedByInterruptException(); } if (!success && closed) { throw new AsynchronousCloseException(); } }
"Arbiter loop unexpected interrupt."); throw new InterruptedException (closedByInterruptException.getMessage()); } catch (IOException | UnknownMasterException e) {
private void checkIfClosedOrInterrupted() throws IOException { if ( !isOpen() ) { throw new ClosedChannelException(); } if ( Thread.currentThread().isInterrupted() ) { close(); throw new ClosedByInterruptException(); } }
throw new InterruptedException(e.getMessage()); } catch (IOException e) { log.error(e.getMessage(), e);
@Test public void shouldNotReportClosedByInterruptException() throws Exception { when(mockAgent.doWork()).thenThrow(new ClosedByInterruptException()); assertExceptionNotReported(); }