@Override public SegmentInputStream createInputStreamForSegment(Segment segment, String delegationToken) { AsyncSegmentInputStreamImpl async = new AsyncSegmentInputStreamImpl(controller, cf, segment, delegationToken); async.getConnection(); return new SegmentInputStreamImpl(async, 0); } }
@Override public CompletableFuture<SegmentRead> read(long offset, int length) { Exceptions.checkNotClosed(closed.get(), this); WireCommands.ReadSegment request = new WireCommands.ReadSegment(segmentId.getScopedName(), offset, length, this.delegationToken); return backoffSchedule.retryWhen(t -> { Throwable ex = Exceptions.unwrap(t); if (closed.get()) { log.debug("Exception while reading from Segment : {}", segmentId, ex); } else { log.warn("Exception while reading from Segment : {}", segmentId, ex); } return ex instanceof Exception && !(ex instanceof ConnectionClosedException) && !(ex instanceof SegmentTruncatedException); }).runAsync(() -> { return getConnection() .whenComplete((connection, ex) -> { if (ex != null) { log.warn("Exception while establishing connection with Pravega " + "node", ex); closeConnection(new ConnectionFailedException(ex)); } }).thenCompose(c -> sendRequestOverConnection(request, c)); }, connectionFactory.getInternalExecutor()); }
private EventSegmentReader getEventSegmentReader(Segment segment, long endOffset, int bufferSize) { String delegationToken = Futures.getAndHandleExceptions(controller.getOrRefreshDelegationTokenFor(segment.getScope(), segment.getStream() .getStreamName()), RuntimeException::new); AsyncSegmentInputStreamImpl async = new AsyncSegmentInputStreamImpl(controller, cf, segment, delegationToken); async.getConnection(); return getEventSegmentReader(async, 0, endOffset, bufferSize); }
@Test(timeout = 10000) public void testCloseAbortsRead() throws InterruptedException, ExecutionException { Segment segment = new Segment("scope", "testRetry", 4); PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT); MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl(); MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory); @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, ""); ClientConnection c = mock(ClientConnection.class); connectionFactory.provideConnection(endpoint, c); in.getConnection().get(); // Make sure connection is established. CompletableFuture<SegmentRead> read = in.read(1234, 5678); assertFalse(read.isDone()); in.close(); assertThrows(ConnectionClosedException.class, () -> Futures.getThrowingException(read)); verify(c).close(); }