Search in sources :

Example 1 with CompletedCallback

use of io.pravega.client.connection.impl.ClientConnection.CompletedCallback in project pravega by pravega.

the class SegmentOutputStreamTest method testFailOnClose.

/**
 * Verifies that if a exception is encountered while flushing data inside of close, the
 * connection is reestablished and the data is retransmitted before close returns.
 */
@Test
public void testFailOnClose() throws ConnectionFailedException {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    cf.setExecutor(executorService());
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf, true);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
    output.reconnect();
    InOrder inOrder = Mockito.inOrder(connection);
    inOrder.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
    cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
    ByteBuffer data = getBuffer("test");
    // Prep mock: the mockito doAnswers setup below are triggered during the close inside of the testBlocking() call.
    CompletableFuture<Void> acked = new CompletableFuture<>();
    Append append = new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            cf.getProcessor(uri).connectionDropped();
            throw new ConnectionFailedException();
        }
    }).doNothing().when(connection).send(new WireCommands.KeepAlive());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CompletedCallback callback = (CompletedCallback) invocation.getArgument(1);
            callback.complete(null);
            return null;
        }
    }).when(connection).sendAsync(Mockito.eq(Collections.singletonList(append)), Mockito.any());
    // Queue up event.
    output.write(PendingEvent.withoutHeader(null, data, acked));
    inOrder.verify(connection).send(append);
    // Verify behavior
    AssertExtensions.assertBlocks(() -> {
        output.close();
    }, () -> {
        // close is unblocked once the connection is setup and data is appended on Segment store.
        cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
        cf.getProcessor(uri).dataAppended(new WireCommands.DataAppended(output.getRequestId(), cid, 1, 0, -1));
    });
    // Verify the order of WireCommands sent.
    inOrder.verify(connection).send(new WireCommands.KeepAlive());
    // Two SetupAppend WireCommands are sent since the connection is dropped right after the first KeepAlive WireCommand is sent.
    // The second SetupAppend WireCommand is sent while trying to re-establish connection.
    inOrder.verify(connection, times(2)).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
    // Ensure the pending append is sent over the connection. The exact verification of the append data is performed while setting up
    // the when clause of setting up append.
    inOrder.verify(connection).sendAsync(Mockito.anyList(), Mockito.any());
    inOrder.verify(connection).close();
    assertEquals(true, acked.isDone());
    inOrder.verifyNoMoreInteractions();
}
Also used : CompletedCallback(io.pravega.client.connection.impl.ClientConnection.CompletedCallback) InOrder(org.mockito.InOrder) ByteBuffer(java.nio.ByteBuffer) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) 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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) 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.connection.impl.ClientConnection) UUID(java.util.UUID) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Test(org.junit.Test)

Example 2 with CompletedCallback

use of io.pravega.client.connection.impl.ClientConnection.CompletedCallback in project pravega by pravega.

the class SegmentOutputStreamTest method testFailureWhileRetransmittingInflight.

@Test(timeout = 10000)
public void testFailureWhileRetransmittingInflight() throws ConnectionFailedException {
    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, true);
    ClientConnection connection = mock(ClientConnection.class);
    InOrder inOrder = inOrder(connection);
    cf.provideConnection(uri, connection);
    @SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
    output.reconnect();
    cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
    output.write(PendingEvent.withoutHeader(null, getBuffer("test1"), new CompletableFuture<>()));
    output.write(PendingEvent.withoutHeader(null, getBuffer("test2"), new CompletableFuture<>()));
    doAnswer(new Answer<Void>() {

        boolean failed = false;

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CompletedCallback callback = (CompletedCallback) invocation.getArgument(1);
            if (failed) {
                callback.complete(null);
            } else {
                failed = true;
                callback.complete(new ConnectionFailedException("Injected"));
            }
            return null;
        }
    }).when(connection).sendAsync(ArgumentMatchers.<List<Append>>any(), Mockito.any(CompletedCallback.class));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
            return null;
        }
    }).doNothing().when(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
    cf.getProcessor(uri).connectionDropped();
    AssertExtensions.assertBlocks(() -> output.write(PendingEvent.withoutHeader(null, getBuffer("test3"), new CompletableFuture<>())), () -> {
        // the write should be blocked until the appendSetup is returned by the Segment stores.
        cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
    });
    output.write(PendingEvent.withoutHeader(null, getBuffer("test4"), new CompletableFuture<>()));
    Append append1 = new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(getBuffer("test1")), null, output.getRequestId());
    Append append2 = new Append(SEGMENT, cid, 2, 1, Unpooled.wrappedBuffer(getBuffer("test2")), null, output.getRequestId());
    Append append3 = new Append(SEGMENT, cid, 3, 1, Unpooled.wrappedBuffer(getBuffer("test3")), null, output.getRequestId());
    Append append4 = new Append(SEGMENT, cid, 4, 1, Unpooled.wrappedBuffer(getBuffer("test4")), null, output.getRequestId());
    inOrder.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
    inOrder.verify(connection).send(append1);
    inOrder.verify(connection).send(append2);
    inOrder.verify(connection).close();
    inOrder.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
    inOrder.verify(connection).sendAsync(eq(ImmutableList.of(append1, append2)), any());
    inOrder.verify(connection).close();
    inOrder.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
    inOrder.verify(connection).sendAsync(eq(ImmutableList.of(append1, append2)), any());
    inOrder.verify(connection).send(append3);
    inOrder.verify(connection).send(append4);
    verifyNoMoreInteractions(connection);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CompletedCallback(io.pravega.client.connection.impl.ClientConnection.CompletedCallback) InOrder(org.mockito.InOrder) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) 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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) 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.connection.impl.ClientConnection) UUID(java.util.UUID) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Test(org.junit.Test)

Example 3 with CompletedCallback

use of io.pravega.client.connection.impl.ClientConnection.CompletedCallback in project pravega by pravega.

the class SegmentOutputStreamTest method testConnectionFailure.

@Test(timeout = 10000)
public void testConnectionFailure() throws Exception {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    cf.setExecutor(executorService());
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf, true);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    @SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
    output.reconnect();
    InOrder inOrder = Mockito.inOrder(connection);
    inOrder.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
    cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
    ByteBuffer data = getBuffer("test");
    CompletableFuture<Void> acked = new CompletableFuture<>();
    Append append = new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId());
    CompletableFuture<Void> acked2 = new CompletableFuture<>();
    Append append2 = new Append(SEGMENT, cid, 2, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            cf.getProcessor(uri).connectionDropped();
            throw new ConnectionFailedException();
        }
    }).when(connection).send(append);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CompletedCallback callback = (CompletedCallback) invocation.getArgument(1);
            callback.complete(null);
            return null;
        }
    }).when(connection).sendAsync(Mockito.eq(Collections.singletonList(append)), Mockito.any());
    AssertExtensions.assertBlocks(() -> {
        output.write(PendingEvent.withoutHeader(null, data, acked));
        output.write(PendingEvent.withoutHeader(null, data, acked2));
    }, () -> {
        cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
    });
    inOrder.verify(connection).send(append);
    inOrder.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
    inOrder.verify(connection).sendAsync(Mockito.eq(Collections.singletonList(append)), Mockito.any());
    inOrder.verify(connection).send(append2);
    assertEquals(false, acked.isDone());
    assertEquals(false, acked2.isDone());
    inOrder.verifyNoMoreInteractions();
}
Also used : CompletedCallback(io.pravega.client.connection.impl.ClientConnection.CompletedCallback) 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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) 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.connection.impl.ClientConnection) UUID(java.util.UUID) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Test(org.junit.Test)

Aggregations

ClientConnection (io.pravega.client.connection.impl.ClientConnection)3 CompletedCallback (io.pravega.client.connection.impl.ClientConnection.CompletedCallback)3 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)3 MockController (io.pravega.client.stream.mock.MockController)3 Append (io.pravega.shared.protocol.netty.Append)3 ConnectionFailedException (io.pravega.shared.protocol.netty.ConnectionFailedException)3 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)3 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)3 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)3 UUID (java.util.UUID)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Test (org.junit.Test)3 InOrder (org.mockito.InOrder)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 ByteBuffer (java.nio.ByteBuffer)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 Answer (org.mockito.stubbing.Answer)2 WireCommands (io.pravega.shared.protocol.netty.WireCommands)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1