Search in sources :

Example 16 with Append

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

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

use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.

the class SegmentOutputStreamTest method sendAndVerifyEvent.

private void sendAndVerifyEvent(UUID cid, ClientConnection connection, SegmentOutputStreamImpl output, ByteBuffer data, int num) throws ConnectionFailedException {
    CompletableFuture<Void> acked = new CompletableFuture<>();
    output.write(PendingEvent.withoutHeader(null, data, acked));
    verify(connection).send(new Append(SEGMENT, cid, num, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId()));
    assertEquals(false, acked.isDone());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend)

Example 19 with Append

use of io.pravega.shared.protocol.netty.Append 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)

Example 20 with Append

use of io.pravega.shared.protocol.netty.Append 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, true);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    InOrder order = Mockito.inOrder(connection);
    @Cleanup 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(1, SEGMENT, cid, 0));
    ByteBuffer data = getBuffer("test");
    CompletableFuture<Void> acked1 = new CompletableFuture<>();
    output.write(PendingEvent.withoutHeader(null, data, acked1));
    order.verify(connection).send(new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId()));
    assertEquals(false, acked1.isDone());
    AssertExtensions.assertBlocks(() -> output.flush(), () -> cf.getProcessor(uri).dataAppended(new WireCommands.DataAppended(output.getRequestId(), cid, 1, 0, -1)));
    assertEquals(false, acked1.isCompletedExceptionally());
    assertEquals(true, acked1.isDone());
    order.verify(connection).send(new WireCommands.KeepAlive());
    CompletableFuture<Void> acked2 = new CompletableFuture<>();
    output.write(PendingEvent.withoutHeader(null, data, acked2));
    order.verify(connection).send(new Append(SEGMENT, cid, 2, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId()));
    assertEquals(false, acked2.isDone());
    AssertExtensions.assertBlocks(() -> output.flush(), () -> cf.getProcessor(uri).dataAppended(new WireCommands.DataAppended(output.getRequestId(), cid, 2, 1, -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) 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) 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

Append (io.pravega.shared.protocol.netty.Append)74 UUID (java.util.UUID)67 Test (org.junit.Test)64 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)52 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)44 WireCommands (io.pravega.shared.protocol.netty.WireCommands)37 Cleanup (lombok.Cleanup)37 CompletableFuture (java.util.concurrent.CompletableFuture)33 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)31 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)27 InOrder (org.mockito.InOrder)22 ClientConnection (io.pravega.client.connection.impl.ClientConnection)21 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)21 MockController (io.pravega.client.stream.mock.MockController)21 Event (io.pravega.shared.protocol.netty.WireCommands.Event)21 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)21 lombok.val (lombok.val)20 DataAppended (io.pravega.shared.protocol.netty.WireCommands.DataAppended)17 ByteBuffer (java.nio.ByteBuffer)17 ByteBuf (io.netty.buffer.ByteBuf)16