Search in sources :

Example 1 with SegmentRead

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());
}
Also used : Getter(lombok.Getter) TokenExpiredException(io.pravega.auth.TokenExpiredException) Retry(io.pravega.common.util.Retry) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Reply(io.pravega.shared.protocol.netty.Reply) Exceptions(io.pravega.common.Exceptions) SegmentIsTruncated(io.pravega.shared.protocol.netty.WireCommands.SegmentIsTruncated) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Unpooled(io.netty.buffer.Unpooled) ArrayList(java.util.ArrayList) ClientConnection(io.pravega.client.connection.impl.ClientConnection) FailingReplyProcessor(io.pravega.shared.protocol.netty.FailingReplyProcessor) SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) Map(java.util.Map) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Flow(io.pravega.client.connection.impl.Flow) RetryWithBackoff(io.pravega.common.util.Retry.RetryWithBackoff) ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) Semaphore(java.util.concurrent.Semaphore) AuthenticationException(io.pravega.auth.AuthenticationException) WireCommands(io.pravega.shared.protocol.netty.WireCommands) GuardedBy(javax.annotation.concurrent.GuardedBy) DelegationTokenProvider(io.pravega.client.security.auth.DelegationTokenProvider) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ConnectionClosedException(io.pravega.client.stream.impl.ConnectionClosedException) Controller(io.pravega.client.control.impl.Controller) Futures(io.pravega.common.concurrent.Futures) AuthenticationException(io.pravega.auth.AuthenticationException) ConnectionClosedException(io.pravega.client.stream.impl.ConnectionClosedException) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) TokenExpiredException(io.pravega.auth.TokenExpiredException) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) AuthenticationException(io.pravega.auth.AuthenticationException) ConnectionClosedException(io.pravega.client.stream.impl.ConnectionClosedException)

Example 2 with SegmentRead

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();
    }
}
Also used : SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead)

Example 3 with SegmentRead

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;
}
Also used : SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 4 with SegmentRead

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;
}
Also used : SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) Synchronized(lombok.Synchronized)

Example 5 with SegmentRead

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));
}
Also used : AuthenticationException(io.pravega.auth.AuthenticationException) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) Semaphore(java.util.concurrent.Semaphore) Cleanup(lombok.Cleanup) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test) SneakyThrows(lombok.SneakyThrows)

Aggregations

SegmentRead (io.pravega.shared.protocol.netty.WireCommands.SegmentRead)17 ReadSegment (io.pravega.shared.protocol.netty.WireCommands.ReadSegment)11 WireCommands (io.pravega.shared.protocol.netty.WireCommands)10 Cleanup (lombok.Cleanup)10 Test (org.junit.Test)10 ClientConnection (io.pravega.client.connection.impl.ClientConnection)9 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)9 Semaphore (java.util.concurrent.Semaphore)9 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)8 MockController (io.pravega.client.stream.mock.MockController)8 ReplyProcessor (io.pravega.shared.protocol.netty.ReplyProcessor)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 Preconditions (com.google.common.base.Preconditions)3 ByteBuf (io.netty.buffer.ByteBuf)3 Unpooled (io.netty.buffer.Unpooled)3 TokenExpiredException (io.pravega.auth.TokenExpiredException)3 Exceptions (io.pravega.common.Exceptions)3 Futures (io.pravega.common.concurrent.Futures)3 ConnectionFailedException (io.pravega.shared.protocol.netty.ConnectionFailedException)3