use of io.pravega.shared.protocol.netty.WireCommands.SegmentRead in project pravega by pravega.
the class AsyncSegmentInputStreamImpl method read.
@Override
public CompletableFuture<SegmentRead> read(long offset, int length) {
Exceptions.checkNotClosed(closed.get(), this);
return backoffSchedule.retryWhen(t -> {
Throwable ex = Exceptions.unwrap(t);
if (closed.get()) {
log.debug("Exception: {} while reading from Segment : {}", ex.toString(), segmentId);
} else {
log.warn("Exception while reading from Segment {} at offset {} :", segmentId, offset, ex);
}
return ex instanceof Exception && !(ex instanceof ConnectionClosedException) && !(ex instanceof SegmentTruncatedException) && !(ex instanceof AuthenticationException);
}).runAsync(() -> this.tokenProvider.retrieveToken().thenComposeAsync(token -> {
final WireCommands.ReadSegment request = new WireCommands.ReadSegment(segmentId.getScopedName(), offset, length, token, requestId);
return getConnection().whenComplete((connection1, ex) -> {
if (ex != null) {
log.warn("Exception while establishing connection with Pravega node {}: ", connection1, ex);
closeConnection(new ConnectionFailedException(ex));
}
}).thenCompose(c -> sendRequestOverConnection(request, c).whenComplete((reply, ex) -> {
if (ex instanceof ConnectionFailedException) {
log.debug("ConnectionFailedException observed when sending request {}", request, ex);
closeConnection((ConnectionFailedException) ex);
}
}));
}, connectionPool.getInternalExecutor()), connectionPool.getInternalExecutor());
}
use of io.pravega.shared.protocol.netty.WireCommands.SegmentRead in project pravega by pravega.
the class SegmentInputStreamImpl method handleRequest.
private void handleRequest() throws SegmentTruncatedException {
SegmentRead segmentRead;
try {
segmentRead = outstandingRequest.join();
} catch (Exception e) {
outstandingRequest = null;
if (Exceptions.unwrap(e) instanceof SegmentTruncatedException) {
receivedTruncated = true;
throw new SegmentTruncatedException(e);
}
throw e;
}
verifyIsAtCorrectOffset(segmentRead);
if (segmentRead.getData().readableBytes() > 0) {
int copied = buffer.fill(segmentRead.getData().nioBuffers());
segmentRead.getData().skipBytes(copied);
}
if (segmentRead.isEndOfSegment()) {
receivedEndOfSegment = true;
}
if (segmentRead.getData().readableBytes() == 0) {
segmentRead.release();
outstandingRequest = null;
issueRequestIfNeeded();
}
}
use of io.pravega.shared.protocol.netty.WireCommands.SegmentRead in project pravega by pravega.
the class SegmentInputStreamImpl method cancelOutstandingRequest.
@GuardedBy("$lock")
private void cancelOutstandingRequest() {
// We need to make sure that we release the ByteBuf held on to by WireCommands.SegmentRead.
// We first attempt to cancel the request. If it has not already completed (and will complete successfully at one point),
// it will automatically release the buffer.
outstandingRequest.cancel(true);
// well complete while we're executing this method and we want to ensure no SegmentRead instances are left hanging.
if (outstandingRequest.isDone() && !outstandingRequest.isCompletedExceptionally()) {
SegmentRead request = outstandingRequest.join();
request.release();
}
log.debug("Completed cancelling outstanding read request for segment {}", asyncInput.getSegmentId());
outstandingRequest = null;
}
use of io.pravega.shared.protocol.netty.WireCommands.SegmentRead in project pravega by pravega.
the class SegmentInputStreamImpl method bytesInBuffer.
@Override
@Synchronized
public int bytesInBuffer() {
int result = buffer.dataAvailable();
boolean atEnd = receivedEndOfSegment || receivedTruncated || (outstandingRequest != null && outstandingRequest.isCompletedExceptionally());
if (outstandingRequest != null && Futures.isSuccessful(outstandingRequest)) {
SegmentRead request = outstandingRequest.join();
result += request.getData().readableBytes();
atEnd |= request.isEndOfSegment();
}
if (result <= 0 && atEnd) {
result = -1;
}
log.trace("bytesInBuffer {} on segment {} status is {}", result, getSegmentId(), this);
return result;
}
use of io.pravega.shared.protocol.netty.WireCommands.SegmentRead in project pravega by pravega.
the class AsyncSegmentInputStreamTest method testAuthenticationFailure.
@SneakyThrows
@Test(timeout = 10000)
public void testAuthenticationFailure() {
Segment segment = new Segment("scope", "testRead", 1);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
Semaphore dataAvailable = new Semaphore(0);
@Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, DelegationTokenProviderFactory.createWithEmptyToken(), dataAvailable);
ClientConnection c = mock(ClientConnection.class);
connectionFactory.provideConnection(endpoint, c);
// Non-token expiry auth token check failure response from Segment store.
WireCommands.AuthTokenCheckFailed authTokenCheckFailed = new WireCommands.AuthTokenCheckFailed(in.getRequestId(), "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_CHECK_FAILED);
// Trigger read.
CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
assertEquals(0, dataAvailable.availablePermits());
// verify that a response from Segment store completes the readFuture and the future completes with the specified exception.
AssertExtensions.assertBlocks(() -> assertThrows(AuthenticationException.class, () -> readFuture.get()), () -> {
ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
processor.process(authTokenCheckFailed);
});
verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
// verify read future completedExceptionally
assertTrue(!Futures.isSuccessful(readFuture));
}
Aggregations