Search in sources :

Example 6 with AppendSetup

use of io.pravega.shared.protocol.netty.WireCommands.AppendSetup 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 7 with AppendSetup

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

the class SegmentOutputStreamTest method testSealedBeforeFlush.

@Test(timeout = 10000)
public void testSealedBeforeFlush() 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);
    InOrder order = Mockito.inOrder(connection);
    @SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
    output.reconnect();
    order.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> ack = new CompletableFuture<>();
    output.write(PendingEvent.withoutHeader(null, data, ack));
    order.verify(connection).send(new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId()));
    assertEquals(false, ack.isDone());
    cf.getProcessor(uri).segmentIsSealed(new WireCommands.SegmentIsSealed(output.getRequestId(), SEGMENT, "SomeException", 1));
    // this is invoked by the segmentSealedCallback.
    output.getUnackedEventsOnSeal();
    AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush());
}
Also used : 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) 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) Test(org.junit.Test)

Example 8 with AppendSetup

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

the class SegmentOutputStreamTest method testConnectAndSendWithoutConnectionPooling.

@Test(timeout = 10000)
public void testConnectAndSendWithoutConnectionPooling() 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, false, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
    output.reconnect();
    verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
    cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
    sendAndVerifyEvent(cid, connection, output, getBuffer("test"), 1);
    verifyNoMoreInteractions(connection);
}
Also used : 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.connection.impl.ClientConnection) UUID(java.util.UUID) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) Test(org.junit.Test)

Example 9 with AppendSetup

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

the class SegmentOutputStreamTest method testConditionalSend.

@Test(timeout = 10000)
public void testConditionalSend() 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();
    verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
    cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
    sendAndVerifyEvent(cid, connection, output, getBuffer("test"), 1);
    verifyNoMoreInteractions(connection);
}
Also used : 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.connection.impl.ClientConnection) UUID(java.util.UUID) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) Test(org.junit.Test)

Example 10 with AppendSetup

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

the class SegmentOutputStreamTest method testFlushDuringTransactionAbort.

@Test(timeout = 10000)
public void testFlushDuringTransactionAbort() 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);
    InOrder order = Mockito.inOrder(connection);
    @SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(TXN_SEGMENT, true, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
    output.reconnect();
    order.verify(connection).send(new SetupAppend(output.getRequestId(), cid, TXN_SEGMENT, ""));
    cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), TXN_SEGMENT, cid, 0));
    ByteBuffer data = getBuffer("test");
    // Write an Event.
    CompletableFuture<Void> ack = new CompletableFuture<>();
    output.write(PendingEvent.withoutHeader(null, data, ack));
    order.verify(connection).send(new Append(TXN_SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId()));
    // writer is not complete until a response from Segment Store is received.
    assertFalse(ack.isDone());
    // Validate that flush() is blocking until there is a response from Segment Store.
    AssertExtensions.assertBlocks(() -> {
        // A flush() should throw a SegmentSealedException.
        AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush());
    }, () -> {
        // Simulate a NoSuchSegment response from SegmentStore due to a Transaction abort.
        cf.getProcessor(uri).noSuchSegment(new WireCommands.NoSuchSegment(output.getRequestId(), TXN_SEGMENT, "SomeException", -1L));
    });
    AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush());
}
Also used : 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) 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) Test(org.junit.Test)

Aggregations

AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)54 Test (org.junit.Test)51 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)49 UUID (java.util.UUID)46 Append (io.pravega.shared.protocol.netty.Append)40 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)33 MockController (io.pravega.client.stream.mock.MockController)33 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)33 ClientConnection (io.pravega.client.connection.impl.ClientConnection)32 CompletableFuture (java.util.concurrent.CompletableFuture)26 Cleanup (lombok.Cleanup)26 ByteBuffer (java.nio.ByteBuffer)24 InOrder (org.mockito.InOrder)22 WireCommands (io.pravega.shared.protocol.netty.WireCommands)20 DataAppended (io.pravega.shared.protocol.netty.WireCommands.DataAppended)20 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)19 lombok.val (lombok.val)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)15 InvocationOnMock (org.mockito.invocation.InvocationOnMock)13 SegmentCreated (io.pravega.shared.protocol.netty.WireCommands.SegmentCreated)12