Search in sources :

Example 1 with Preconditions.checkState

use of com.google.common.base.Preconditions.checkState in project pravega by pravega.

the class SegmentOutputStreamImpl method reconnect.

@VisibleForTesting
void reconnect() {
    if (state.isClosed()) {
        return;
    }
    log.debug("(Re)connect invoked, Segment: {}, writerID: {}", segmentName, writerId);
    state.setupConnection.registerAndRunReleaser(() -> {
        // retry on all exceptions.
        retrySchedule.retryWhen(t -> t instanceof Exception).runAsync(() -> {
            log.debug("Running reconnect for segment {} writer {}", segmentName, writerId);
            if (state.isClosed() || state.needSuccessors.get()) {
                // stop reconnect when writer is closed or resend inflight to successors has been triggered.
                return CompletableFuture.completedFuture(null);
            }
            Preconditions.checkState(state.getConnection() == null);
            log.info("Fetching endpoint for segment {}, writer {}", segmentName, writerId);
            return controller.getEndpointForSegment(segmentName).thenComposeAsync((PravegaNodeUri uri) -> {
                log.info("Establishing connection to {} for {}, writerID: {}", uri, segmentName, writerId);
                return establishConnection(uri);
            }, connectionPool.getInternalExecutor()).thenCombineAsync(tokenProvider.retrieveToken(), AbstractMap.SimpleEntry<ClientConnection, String>::new, connectionPool.getInternalExecutor()).thenComposeAsync(pair -> {
                ClientConnection connection = pair.getKey();
                String token = pair.getValue();
                CompletableFuture<Void> connectionSetupFuture = state.newConnection(connection);
                SetupAppend cmd = new SetupAppend(requestId, writerId, segmentName, token);
                try {
                    connection.send(cmd);
                } catch (ConnectionFailedException e1) {
                    // This needs to be invoked here because call to failConnection from netty may occur before state.newConnection above.
                    state.failConnection(e1);
                    throw Exceptions.sneakyThrow(e1);
                }
                return connectionSetupFuture.exceptionally(t1 -> {
                    Throwable exception = Exceptions.unwrap(t1);
                    if (exception instanceof InvalidTokenException) {
                        log.info("Ending reconnect attempts on writer {} to {} because token verification failed due to invalid token", writerId, segmentName);
                        return null;
                    }
                    if (exception instanceof SegmentSealedException) {
                        log.info("Ending reconnect attempts on writer {} to {} because segment is sealed", writerId, segmentName);
                        return null;
                    }
                    if (exception instanceof NoSuchSegmentException) {
                        log.info("Ending reconnect attempts on writer {} to {} because segment is truncated", writerId, segmentName);
                        return null;
                    }
                    throw Exceptions.sneakyThrow(t1);
                });
            }, connectionPool.getInternalExecutor());
        }, connectionPool.getInternalExecutor()).exceptionally(t -> {
            log.error("Error while attempting to establish connection for writer {}", writerId, t);
            failAndRemoveUnackedEvents(t);
            return null;
        });
    }, new CompletableFuture<ClientConnection>());
}
Also used : TokenExpiredException(io.pravega.auth.TokenExpiredException) Retry(io.pravega.common.util.Retry) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ClientConnection(io.pravega.client.connection.impl.ClientConnection) FailingReplyProcessor(io.pravega.shared.protocol.netty.FailingReplyProcessor) Map(java.util.Map) ToString(lombok.ToString) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Flow(io.pravega.client.connection.impl.Flow) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) UUID(java.util.UUID) ReusableFutureLatch(io.pravega.common.util.ReusableFutureLatch) GuardedBy(javax.annotation.concurrent.GuardedBy) Collectors(java.util.stream.Collectors) RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Entry(java.util.Map.Entry) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) SegmentIsSealed(io.pravega.shared.protocol.netty.WireCommands.SegmentIsSealed) Controller(io.pravega.client.control.impl.Controller) Futures(io.pravega.common.concurrent.Futures) Getter(lombok.Getter) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) Exceptions(io.pravega.common.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) PendingEvent(io.pravega.client.stream.impl.PendingEvent) Append(io.pravega.shared.protocol.netty.Append) ArrayList(java.util.ArrayList) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) KeepAlive(io.pravega.shared.protocol.netty.WireCommands.KeepAlive) ReusableLatch(io.pravega.common.util.ReusableLatch) NameUtils(io.pravega.shared.NameUtils) RetryWithBackoff(io.pravega.common.util.Retry.RetryWithBackoff) ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) WireCommands(io.pravega.shared.protocol.netty.WireCommands) WrongHost(io.pravega.shared.protocol.netty.WireCommands.WrongHost) DelegationTokenProvider(io.pravega.client.security.auth.DelegationTokenProvider) Consumer(java.util.function.Consumer) WireCommand(io.pravega.shared.protocol.netty.WireCommand) AbstractMap(java.util.AbstractMap) InvalidTokenException(io.pravega.auth.InvalidTokenException) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) InvalidTokenException(io.pravega.auth.InvalidTokenException) ToString(lombok.ToString) TokenExpiredException(io.pravega.auth.TokenExpiredException) RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) InvalidTokenException(io.pravega.auth.InvalidTokenException) CompletableFuture(java.util.concurrent.CompletableFuture) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) ClientConnection(io.pravega.client.connection.impl.ClientConnection) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions (com.google.common.base.Preconditions)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 InvalidTokenException (io.pravega.auth.InvalidTokenException)1 TokenExpiredException (io.pravega.auth.TokenExpiredException)1 ClientConnection (io.pravega.client.connection.impl.ClientConnection)1 ConnectionPool (io.pravega.client.connection.impl.ConnectionPool)1 Flow (io.pravega.client.connection.impl.Flow)1 Controller (io.pravega.client.control.impl.Controller)1 DelegationTokenProvider (io.pravega.client.security.auth.DelegationTokenProvider)1 PendingEvent (io.pravega.client.stream.impl.PendingEvent)1 Exceptions (io.pravega.common.Exceptions)1 Futures (io.pravega.common.concurrent.Futures)1 RetriesExhaustedException (io.pravega.common.util.RetriesExhaustedException)1 Retry (io.pravega.common.util.Retry)1 RetryWithBackoff (io.pravega.common.util.Retry.RetryWithBackoff)1 ReusableFutureLatch (io.pravega.common.util.ReusableFutureLatch)1 ReusableLatch (io.pravega.common.util.ReusableLatch)1 NameUtils (io.pravega.shared.NameUtils)1 Append (io.pravega.shared.protocol.netty.Append)1