Search in sources :

Example 1 with SetupAppend

use of io.pravega.shared.protocol.netty.WireCommands.SetupAppend 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.indefinitelyWithExpBackoff(retrySchedule.getInitialMillis(), retrySchedule.getMultiplier(), retrySchedule.getMaxDelay(), t -> log.warn(writerId + " Failed to connect: ", t)).runAsync(() -> {
            log.debug("Running reconnect for segment:{} writerID: {}", segmentName, writerId);
            if (state.isClosed() || state.isAlreadySealed()) {
                return CompletableFuture.completedFuture(null);
            }
            Preconditions.checkState(state.getConnection() == null);
            log.info("Fetching endpoint for segment {}, writerID: {}", segmentName, writerId);
            return controller.getEndpointForSegment(segmentName).thenComposeAsync((PravegaNodeUri uri) -> {
                log.info("Establishing connection to {} for {}, writerID: {}", uri, segmentName, writerId);
                return connectionFactory.establishConnection(uri, responseProcessor);
            }, connectionFactory.getInternalExecutor()).thenComposeAsync(connection -> {
                CompletableFuture<Void> connectionSetupFuture = state.newConnection(connection);
                SetupAppend cmd = new SetupAppend(requestIdGenerator.get(), writerId, segmentName, delegationToken);
                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 Lombok.sneakyThrow(e1);
                }
                return connectionSetupFuture.exceptionally(t -> {
                    if (Exceptions.unwrap(t) instanceof SegmentSealedException) {
                        log.info("Ending reconnect attempts to {} because segment is sealed", segmentName);
                        return null;
                    }
                    throw Lombok.sneakyThrow(t);
                });
            }, connectionFactory.getInternalExecutor());
        }, connectionFactory.getInternalExecutor());
    }, new CompletableFuture<ClientConnection>());
}
Also used : ClientConnection(io.pravega.client.netty.impl.ClientConnection) AuthenticationException(io.pravega.common.auth.AuthenticationException) Getter(lombok.Getter) Retry(io.pravega.common.util.Retry) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) Exceptions(io.pravega.common.Exceptions) ConditionalCheckFailed(io.pravega.shared.protocol.netty.WireCommands.ConditionalCheckFailed) RequiredArgsConstructor(lombok.RequiredArgsConstructor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) PendingEvent(io.pravega.client.stream.impl.PendingEvent) Append(io.pravega.shared.protocol.netty.Append) Supplier(java.util.function.Supplier) Unpooled(io.netty.buffer.Unpooled) ArrayList(java.util.ArrayList) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) FailingReplyProcessor(io.pravega.shared.protocol.netty.FailingReplyProcessor) Map(java.util.Map) ToString(lombok.ToString) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) KeepAlive(io.pravega.shared.protocol.netty.WireCommands.KeepAlive) ReusableLatch(io.pravega.common.util.ReusableLatch) RetryWithBackoff(io.pravega.common.util.Retry.RetryWithBackoff) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) ReusableFutureLatch(io.pravega.common.util.ReusableFutureLatch) UUID(java.util.UUID) WireCommands(io.pravega.shared.protocol.netty.WireCommands) GuardedBy(javax.annotation.concurrent.GuardedBy) Collectors(java.util.stream.Collectors) WrongHost(io.pravega.shared.protocol.netty.WireCommands.WrongHost) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Slf4j(lombok.extern.slf4j.Slf4j) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) SegmentIsSealed(io.pravega.shared.protocol.netty.WireCommands.SegmentIsSealed) Controller(io.pravega.client.stream.impl.Controller) Lombok(lombok.Lombok) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) ClientConnection(io.pravega.client.netty.impl.ClientConnection) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with SetupAppend

use of io.pravega.shared.protocol.netty.WireCommands.SetupAppend in project pravega by pravega.

the class SegmentOutputStreamTest method testFlush.

@Test(timeout = 10000)
public void testFlush() throws ConnectionFailedException, SegmentSealedException {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
    // Ensure task submitted to executor is run inline.
    implementAsDirectExecutor(executor);
    cf.setExecutor(executor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    InOrder order = Mockito.inOrder(connection);
    SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, "");
    output.reconnect();
    order.verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
    cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
    ByteBuffer data = getBuffer("test");
    CompletableFuture<Boolean> acked1 = new CompletableFuture<>();
    output.write(new PendingEvent(null, data, acked1));
    order.verify(connection).send(new Append(SEGMENT, cid, 1, Unpooled.wrappedBuffer(data), null));
    assertEquals(false, acked1.isDone());
    Async.testBlocking(() -> output.flush(), () -> cf.getProcessor(uri).dataAppended(new WireCommands.DataAppended(cid, 1, 0)));
    assertEquals(false, acked1.isCompletedExceptionally());
    assertEquals(true, acked1.isDone());
    order.verify(connection).send(new WireCommands.KeepAlive());
    CompletableFuture<Boolean> acked2 = new CompletableFuture<>();
    output.write(new PendingEvent(null, data, acked2));
    order.verify(connection).send(new Append(SEGMENT, cid, 2, Unpooled.wrappedBuffer(data), null));
    assertEquals(false, acked2.isDone());
    Async.testBlocking(() -> output.flush(), () -> cf.getProcessor(uri).dataAppended(new WireCommands.DataAppended(cid, 2, 1)));
    assertEquals(false, acked2.isCompletedExceptionally());
    assertEquals(true, acked2.isDone());
    order.verify(connection).send(new WireCommands.KeepAlive());
    order.verifyNoMoreInteractions();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InOrder(org.mockito.InOrder) ByteBuffer(java.nio.ByteBuffer) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) CompletableFuture(java.util.concurrent.CompletableFuture) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) PendingEvent(io.pravega.client.stream.impl.PendingEvent) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) UUID(java.util.UUID) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Example 3 with SetupAppend

use of io.pravega.shared.protocol.netty.WireCommands.SetupAppend in project pravega by pravega.

the class SegmentOutputStreamTest method testConnectWithMultipleFailures.

@Test(timeout = 10000)
public void testConnectWithMultipleFailures() throws Exception {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
    // Ensure task submitted to executor is run inline.
    implementAsDirectExecutor(executor);
    cf.setExecutor(executor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, "");
    output.reconnect();
    verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
    // simulate a processing Failure and ensure SetupAppend is executed.
    cf.getProcessor(uri).processingFailure(new IOException());
    verify(connection).send(new SetupAppend(2, cid, SEGMENT, ""));
    cf.getProcessor(uri).connectionDropped();
    verify(connection).send(new SetupAppend(3, cid, SEGMENT, ""));
    cf.getProcessor(uri).wrongHost(new WireCommands.WrongHost(3, SEGMENT, "newHost"));
    verify(connection).send(new SetupAppend(4, cid, SEGMENT, ""));
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) IOException(java.io.IOException) UUID(java.util.UUID) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Example 4 with SetupAppend

use of io.pravega.shared.protocol.netty.WireCommands.SetupAppend in project pravega by pravega.

the class SegmentOutputStreamTest method testClose.

@Test(timeout = 10000)
public void testClose() throws ConnectionFailedException, SegmentSealedException {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    @Cleanup("shutdown") InlineExecutor inlineExecutor = new InlineExecutor();
    cf.setExecutor(inlineExecutor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, "");
    output.reconnect();
    verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
    cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
    ByteBuffer data = getBuffer("test");
    CompletableFuture<Boolean> acked = new CompletableFuture<>();
    output.write(new PendingEvent(null, data, acked));
    verify(connection).send(new Append(SEGMENT, cid, 1, Unpooled.wrappedBuffer(data), null));
    assertEquals(false, acked.isDone());
    Async.testBlocking(() -> output.close(), () -> cf.getProcessor(uri).dataAppended(new WireCommands.DataAppended(cid, 1, 0)));
    assertEquals(false, acked.isCompletedExceptionally());
    assertEquals(true, acked.isDone());
    verify(connection, Mockito.atMost(1)).send(new WireCommands.KeepAlive());
    verify(connection).close();
    verifyNoMoreInteractions(connection);
}
Also used : Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) CompletableFuture(java.util.concurrent.CompletableFuture) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) PendingEvent(io.pravega.client.stream.impl.PendingEvent) InlineExecutor(io.pravega.test.common.InlineExecutor) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) UUID(java.util.UUID) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Example 5 with SetupAppend

use of io.pravega.shared.protocol.netty.WireCommands.SetupAppend in project pravega by pravega.

the class SegmentOutputStreamTest method testConnectAndConnectionDrop.

@Test(timeout = 10000)
public void testConnectAndConnectionDrop() throws Exception {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
    // Ensure task submitted to executor is run inline.
    implementAsDirectExecutor(executor);
    cf.setExecutor(executor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, "");
    output.reconnect();
    verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
    // simulate a connection dropped
    cf.getProcessor(uri).connectionDropped();
    // Ensure setup Append is invoked on the executor.
    verify(connection).send(new SetupAppend(2, cid, SEGMENT, ""));
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)39 Test (org.junit.Test)35 UUID (java.util.UUID)34 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)27 Append (io.pravega.shared.protocol.netty.Append)26 ClientConnection (io.pravega.client.netty.impl.ClientConnection)20 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)20 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)19 MockController (io.pravega.client.stream.mock.MockController)19 CompletableFuture (java.util.concurrent.CompletableFuture)17 Cleanup (lombok.Cleanup)17 PendingEvent (io.pravega.client.stream.impl.PendingEvent)15 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 InOrder (org.mockito.InOrder)13 FailingRequestProcessor (io.pravega.shared.protocol.netty.FailingRequestProcessor)11 WireCommands (io.pravega.shared.protocol.netty.WireCommands)11 ByteBuffer (java.nio.ByteBuffer)11 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)11 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)11