Search in sources :

Example 21 with AppendSetup

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

the class AppendProcessorTest method testConditionalAppendFailureOnUnconditionalAppend.

@Test
public void testConditionalAppendFailureOnUnconditionalAppend() {
    String streamSegmentName = "scope/stream/testConditionalAppendFailureOnUnconditionalAppend";
    UUID clientId = UUID.randomUUID();
    byte[] data = new byte[] { 1, 2, 3, 4, 6, 7, 8, 9 };
    StreamSegmentStore store = mock(StreamSegmentStore.class);
    ServerConnection connection = mock(ServerConnection.class);
    val mockedRecorder = Mockito.mock(SegmentStatsRecorder.class);
    ConnectionTracker tracker = mock(ConnectionTracker.class);
    @Cleanup AppendProcessor processor = AppendProcessor.defaultBuilder().store(store).connection(new TrackedConnection(connection, tracker)).statsRecorder(mockedRecorder).build();
    setupGetAttributes(streamSegmentName, clientId, store);
    val ac1 = interceptAppend(store, streamSegmentName, updateEventNumber(clientId, 1), CompletableFuture.completedFuture((long) data.length));
    processor.setupAppend(new SetupAppend(1, clientId, streamSegmentName, ""));
    processor.append(new Append(streamSegmentName, clientId, 1, 1, Unpooled.wrappedBuffer(data), null, requestId));
    val ac2 = interceptAppend(store, streamSegmentName, updateEventNumber(clientId, 1, 1, 1), Futures.failedFuture(new BadOffsetException(streamSegmentName, data.length, 0)));
    processor.append(new Append(streamSegmentName, clientId, 1, 1, Unpooled.wrappedBuffer(data), null, requestId));
    verify(store).getAttributes(anyString(), eq(Collections.singleton(AttributeId.fromUUID(clientId))), eq(true), eq(AppendProcessor.TIMEOUT));
    verifyStoreAppend(ac1, data);
    verifyStoreAppend(ac2, data);
    verify(connection).send(new AppendSetup(1, streamSegmentName, clientId, 0));
    verify(tracker, times(2)).updateOutstandingBytes(connection, data.length, data.length);
    verify(connection).send(new DataAppended(requestId, clientId, 1, 0, data.length));
    verify(connection).close();
    verify(tracker, times(2)).updateOutstandingBytes(connection, -data.length, 0);
    verifyNoMoreInteractions(connection);
    verifyNoMoreInteractions(store);
}
Also used : lombok.val(lombok.val) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Cleanup(lombok.Cleanup) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException) UUID(java.util.UUID) Test(org.junit.Test)

Example 22 with AppendSetup

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

the class AppendProcessorTest method testSwitchingStream.

@Test
public void testSwitchingStream() {
    String segment1 = "scope/stream/segment1";
    String segment2 = "scope/stream/segment2";
    UUID clientId1 = UUID.randomUUID();
    UUID clientId2 = UUID.randomUUID();
    byte[] data = new byte[] { 1, 2, 3, 4, 6, 7, 8, 9 };
    StreamSegmentStore store = mock(StreamSegmentStore.class);
    ServerConnection connection = mock(ServerConnection.class);
    ConnectionTracker tracker = mock(ConnectionTracker.class);
    @Cleanup AppendProcessor processor = AppendProcessor.defaultBuilder().store(store).connection(new TrackedConnection(connection, tracker)).build();
    setupGetAttributes(segment1, clientId1, store);
    val ac1 = interceptAppend(store, segment1, updateEventNumber(clientId1, data.length), CompletableFuture.completedFuture((long) data.length));
    setupGetAttributes(segment2, clientId2, store);
    val ac2 = interceptAppend(store, segment2, updateEventNumber(clientId2, data.length), CompletableFuture.completedFuture((long) data.length));
    processor.setupAppend(new SetupAppend(1, clientId1, segment1, ""));
    processor.append(new Append(segment1, clientId1, data.length, 1, Unpooled.wrappedBuffer(data), null, 2));
    processor.setupAppend(new SetupAppend(3, clientId2, segment2, ""));
    processor.append(new Append(segment2, clientId2, data.length, 1, Unpooled.wrappedBuffer(data), null, 4));
    verify(store).getAttributes(eq(segment1), eq(Collections.singleton(AttributeId.fromUUID(clientId1))), eq(true), eq(AppendProcessor.TIMEOUT));
    verifyStoreAppend(ac1, data);
    verify(store).getAttributes(eq(segment2), eq(Collections.singleton(AttributeId.fromUUID(clientId2))), eq(true), eq(AppendProcessor.TIMEOUT));
    verifyStoreAppend(ac2, data);
    verify(tracker, times(2)).updateOutstandingBytes(connection, data.length, data.length);
    verify(connection).send(new AppendSetup(1, segment1, clientId1, 0));
    verify(connection).send(new DataAppended(2, clientId1, data.length, 0, data.length));
    verify(connection).send(new AppendSetup(3, segment2, clientId2, 0));
    verify(connection).send(new DataAppended(4, clientId2, data.length, 0, data.length));
    verify(tracker, times(2)).updateOutstandingBytes(connection, -data.length, 0);
    verifyNoMoreInteractions(connection);
    verifyNoMoreInteractions(store);
}
Also used : lombok.val(lombok.val) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Cleanup(lombok.Cleanup) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) UUID(java.util.UUID) Test(org.junit.Test)

Example 23 with AppendSetup

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

the class AppendTest method sendReceivingAppend.

@Test(timeout = 10000)
public void sendReceivingAppend() throws Exception {
    String segment = "sendReceivingAppend";
    ByteBuf data = Unpooled.wrappedBuffer("Hello world\n".getBytes());
    StreamSegmentStore store = SERVICE_BUILDER.createStreamSegmentService();
    @Cleanup EmbeddedChannel channel = createChannel(store);
    SegmentCreated created = (SegmentCreated) sendRequest(channel, new CreateSegment(1, segment, CreateSegment.NO_SCALE, 0, "", 1024L));
    assertEquals(segment, created.getSegment());
    UUID uuid = UUID.randomUUID();
    AppendSetup setup = (AppendSetup) sendRequest(channel, new SetupAppend(2, uuid, segment, ""));
    assertEquals(segment, setup.getSegment());
    assertEquals(uuid, setup.getWriterId());
    DataAppended ack = (DataAppended) sendRequest(channel, new Append(segment, uuid, data.readableBytes(), new Event(data), 1L));
    assertEquals(uuid, ack.getWriterId());
    assertEquals(data.readableBytes(), ack.getEventNumber());
    assertEquals(Long.MIN_VALUE, ack.getPreviousEventNumber());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Cleanup(lombok.Cleanup) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) SegmentCreated(io.pravega.shared.protocol.netty.WireCommands.SegmentCreated) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) Event(io.pravega.shared.protocol.netty.WireCommands.Event) PendingEvent(io.pravega.client.stream.impl.PendingEvent) UUID(java.util.UUID) CreateSegment(io.pravega.shared.protocol.netty.WireCommands.CreateSegment) Test(org.junit.Test)

Example 24 with AppendSetup

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

the class LargeEventWriterTest method testPipelining.

@Test(timeout = 5000)
public void testPipelining() throws NoSuchSegmentException, AuthenticationException, SegmentSealedException, ConnectionFailedException {
    Segment segment = Segment.fromScopedName("foo/bar/1");
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = new MockController("localhost", 0, connectionFactory, false);
    ClientConnection connection = Mockito.mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    connectionFactory.provideConnection(location, connection);
    ArrayList<ByteBuffer> buffers = new ArrayList<>();
    buffers.add(ByteBuffer.allocate(Serializer.MAX_EVENT_SIZE - WireCommands.TYPE_PLUS_LENGTH_SIZE));
    buffers.add(ByteBuffer.allocate(Serializer.MAX_EVENT_SIZE - WireCommands.TYPE_PLUS_LENGTH_SIZE));
    buffers.add(ByteBuffer.allocate(Serializer.MAX_EVENT_SIZE - WireCommands.TYPE_PLUS_LENGTH_SIZE));
    ArrayList<ConditionalBlockEnd> written = new ArrayList<>();
    answerRequest(connectionFactory, connection, location, CreateTransientSegment.class, r -> new SegmentCreated(r.getRequestId(), "transient-segment"));
    answerRequest(connectionFactory, connection, location, SetupAppend.class, r -> new AppendSetup(r.getRequestId(), segment.getScopedName(), r.getWriterId(), WireCommands.NULL_ATTRIBUTE_VALUE));
    // If appends are not pipelined, the call to writeLargeEvents will stall waiting for the first reply.
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ConditionalBlockEnd argument = (ConditionalBlockEnd) invocation.getArgument(0);
            written.add(argument);
            if (written.size() == buffers.size()) {
                for (ConditionalBlockEnd append : written) {
                    connectionFactory.getProcessor(location).process(new DataAppended(append.getRequestId(), writerId, append.getEventNumber(), append.getEventNumber() - 1, append.getExpectedOffset() + append.getData().readableBytes()));
                }
            }
            return null;
        }
    }).when(connection).send(any(ConditionalBlockEnd.class));
    answerRequest(connectionFactory, connection, location, MergeSegments.class, r -> {
        return new SegmentsMerged(r.getRequestId(), r.getSource(), r.getTarget(), -1);
    });
    LargeEventWriter writer = new LargeEventWriter(writerId, controller, connectionFactory);
    EmptyTokenProviderImpl tokenProvider = new EmptyTokenProviderImpl();
    writer.writeLargeEvent(segment, buffers, tokenProvider, EventWriterConfig.builder().build());
}
Also used : ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) CreateTransientSegment(io.pravega.shared.protocol.netty.WireCommands.CreateTransientSegment) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) SegmentCreated(io.pravega.shared.protocol.netty.WireCommands.SegmentCreated) EmptyTokenProviderImpl(io.pravega.client.security.auth.EmptyTokenProviderImpl) ConditionalBlockEnd(io.pravega.shared.protocol.netty.WireCommands.ConditionalBlockEnd) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) SegmentsMerged(io.pravega.shared.protocol.netty.WireCommands.SegmentsMerged) Test(org.junit.Test)

Example 25 with AppendSetup

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

the class LargeEventWriterTest method testRetries.

@Test(timeout = 5000)
public void testRetries() throws ConnectionFailedException, NoSuchSegmentException, AuthenticationException {
    Segment segment = Segment.fromScopedName("foo/bar/1");
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = new MockController("localhost", 0, connectionFactory, false);
    ClientConnection connection = Mockito.mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    connectionFactory.provideConnection(location, connection);
    AtomicInteger count = new AtomicInteger(0);
    answerRequest(connectionFactory, connection, location, CreateTransientSegment.class, r -> new SegmentCreated(r.getRequestId(), "transient-segment"));
    answerRequest(connectionFactory, connection, location, SetupAppend.class, r -> new AppendSetup(r.getRequestId(), segment.getScopedName(), r.getWriterId(), WireCommands.NULL_ATTRIBUTE_VALUE));
    answerRequest(connectionFactory, connection, location, ConditionalBlockEnd.class, r -> {
        count.getAndIncrement();
        return new WrongHost(r.getRequestId(), segment.getScopedName(), null, null);
    });
    LargeEventWriter writer = new LargeEventWriter(writerId, controller, connectionFactory);
    EmptyTokenProviderImpl tokenProvider = new EmptyTokenProviderImpl();
    ArrayList<ByteBuffer> buffers = new ArrayList<>();
    buffers.add(ByteBuffer.allocate(5));
    AssertExtensions.assertThrows(RetriesExhaustedException.class, () -> writer.writeLargeEvent(segment, buffers, tokenProvider, EventWriterConfig.builder().initialBackoffMillis(1).backoffMultiple(1).enableLargeEvents(true).retryAttempts(7).build()));
    assertEquals(8, count.get());
}
Also used : ArrayList(java.util.ArrayList) WrongHost(io.pravega.shared.protocol.netty.WireCommands.WrongHost) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) CreateTransientSegment(io.pravega.shared.protocol.netty.WireCommands.CreateTransientSegment) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) SegmentCreated(io.pravega.shared.protocol.netty.WireCommands.SegmentCreated) EmptyTokenProviderImpl(io.pravega.client.security.auth.EmptyTokenProviderImpl) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) 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