Search in sources :

Example 1 with DataAppended

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

the class AppendProcessor method handleAppendResult.

private void handleAppendResult(final Append append, Throwable exception) {
    try {
        boolean conditionalFailed = exception != null && (Exceptions.unwrap(exception) instanceof BadOffsetException);
        long previousEventNumber;
        synchronized (lock) {
            previousEventNumber = latestEventNumbers.get(Pair.of(append.getSegment(), append.getWriterId()));
            Preconditions.checkState(outstandingAppend == append, "Synchronization error in: %s while processing append: %s.", AppendProcessor.this.getClass().getName(), append);
        }
        if (exception != null) {
            if (conditionalFailed) {
                log.debug("Conditional append failed due to incorrect offset: {}, {}", append, exception.getMessage());
                connection.send(new ConditionalCheckFailed(append.getWriterId(), append.getEventNumber()));
            } else {
                handleException(append.getWriterId(), append.getEventNumber(), append.getSegment(), "appending data", exception);
            }
        } else {
            if (statsRecorder != null) {
                statsRecorder.record(append.getSegment(), append.getDataLength(), append.getEventCount());
            }
            final DataAppended dataAppendedAck = new DataAppended(append.getWriterId(), append.getEventNumber(), previousEventNumber);
            log.trace("Sending DataAppended : {}", dataAppendedAck);
            connection.send(dataAppendedAck);
            DYNAMIC_LOGGER.incCounterValue(nameFromSegment(SEGMENT_WRITE_BYTES, append.getSegment()), append.getDataLength());
            DYNAMIC_LOGGER.incCounterValue(nameFromSegment(SEGMENT_WRITE_EVENTS, append.getSegment()), append.getEventCount());
        }
        /* Reply (DataAppended in case of success, else an error Reply based on exception) has been sent. Next,
             *   - clear outstandingAppend to handle the next Append message.
             *   - ensure latestEventNumbers and waitingAppends are updated.
             */
        synchronized (lock) {
            Preconditions.checkState(outstandingAppend == append, "Synchronization error in: %s while processing append: %s.", AppendProcessor.this.getClass().getName(), append);
            outstandingAppend = null;
            if (exception == null) {
                latestEventNumbers.put(Pair.of(append.getSegment(), append.getWriterId()), append.getEventNumber());
            } else {
                if (!conditionalFailed) {
                    waitingAppends.removeAll(append.getWriterId());
                    latestEventNumbers.remove(Pair.of(append.getSegment(), append.getWriterId()));
                }
            }
        }
        pauseOrResumeReading();
        performNextWrite();
    } catch (Throwable e) {
        handleException(append.getWriterId(), append.getEventNumber(), append.getSegment(), "handling append result", e);
    }
}
Also used : ConditionalCheckFailed(io.pravega.shared.protocol.netty.WireCommands.ConditionalCheckFailed) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException)

Example 2 with DataAppended

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

the class AppendProcessorTest method testAppend.

@Test
public void testAppend() {
    String streamSegmentName = "testAppendSegment";
    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);
    AppendProcessor processor = new AppendProcessor(store, connection, new FailingRequestProcessor(), null);
    setupGetStreamSegmentInfo(streamSegmentName, clientId, store);
    CompletableFuture<Void> result = CompletableFuture.completedFuture(null);
    when(store.append(streamSegmentName, data, updateEventNumber(clientId, data.length), AppendProcessor.TIMEOUT)).thenReturn(result);
    processor.setupAppend(new SetupAppend(1, clientId, streamSegmentName, ""));
    processor.append(new Append(streamSegmentName, clientId, data.length, Unpooled.wrappedBuffer(data), null));
    verify(store).getStreamSegmentInfo(anyString(), eq(true), eq(AppendProcessor.TIMEOUT));
    verify(store).append(streamSegmentName, data, updateEventNumber(clientId, data.length), AppendProcessor.TIMEOUT);
    verify(connection).send(new AppendSetup(1, streamSegmentName, clientId, 0));
    verify(connection, atLeast(0)).resumeReading();
    verify(connection).send(new DataAppended(clientId, data.length, 0L));
    verifyNoMoreInteractions(connection);
    verifyNoMoreInteractions(store);
}
Also used : FailingRequestProcessor(io.pravega.shared.protocol.netty.FailingRequestProcessor) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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 3 with DataAppended

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

the class AppendProcessorTest method testSwitchingStream.

@Test
public void testSwitchingStream() {
    String segment1 = "segment1";
    String segment2 = "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);
    AppendProcessor processor = new AppendProcessor(store, connection, new FailingRequestProcessor(), null);
    setupGetStreamSegmentInfo(segment1, clientId1, store);
    CompletableFuture<Void> result = CompletableFuture.completedFuture(null);
    when(store.append(segment1, data, updateEventNumber(clientId1, data.length), AppendProcessor.TIMEOUT)).thenReturn(result);
    setupGetStreamSegmentInfo(segment2, clientId2, store);
    result = CompletableFuture.completedFuture(null);
    when(store.append(segment2, data, updateEventNumber(clientId2, data.length), AppendProcessor.TIMEOUT)).thenReturn(result);
    processor.setupAppend(new SetupAppend(1, clientId1, segment1, ""));
    processor.append(new Append(segment1, clientId1, data.length, Unpooled.wrappedBuffer(data), null));
    processor.setupAppend(new SetupAppend(2, clientId2, segment2, ""));
    processor.append(new Append(segment2, clientId2, data.length, Unpooled.wrappedBuffer(data), null));
    verify(store).getStreamSegmentInfo(eq(segment1), eq(true), eq(AppendProcessor.TIMEOUT));
    verify(store).append(segment1, data, updateEventNumber(clientId1, data.length), AppendProcessor.TIMEOUT);
    verify(store).getStreamSegmentInfo(eq(segment2), eq(true), eq(AppendProcessor.TIMEOUT));
    verify(store).append(segment2, data, updateEventNumber(clientId2, data.length), AppendProcessor.TIMEOUT);
    verify(connection, atLeast(0)).resumeReading();
    verify(connection).send(new AppendSetup(1, segment1, clientId1, 0));
    verify(connection).send(new DataAppended(clientId1, data.length, 0));
    verify(connection).send(new AppendSetup(2, segment2, clientId2, 0));
    verify(connection).send(new DataAppended(clientId2, data.length, 0));
    verifyNoMoreInteractions(connection);
    verifyNoMoreInteractions(store);
}
Also used : FailingRequestProcessor(io.pravega.shared.protocol.netty.FailingRequestProcessor) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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 4 with DataAppended

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

the class AppendTest method testMultipleAppends.

@Test(timeout = 10000)
public void testMultipleAppends() throws Exception {
    String segment = "123";
    ByteBuf data = Unpooled.wrappedBuffer("Hello world\n".getBytes());
    StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
    @Cleanup EmbeddedChannel channel = createChannel(store);
    SegmentCreated created = (SegmentCreated) sendRequest(channel, new CreateSegment(1, segment, CreateSegment.NO_SCALE, 0, ""));
    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());
    data.retain();
    DataAppended ack = (DataAppended) sendRequest(channel, new Append(segment, uuid, 1, data, null));
    assertEquals(uuid, ack.getWriterId());
    assertEquals(1, ack.getEventNumber());
    assertEquals(Long.MIN_VALUE, ack.getPreviousEventNumber());
    DataAppended ack2 = (DataAppended) sendRequest(channel, new Append(segment, uuid, 2, data, null));
    assertEquals(uuid, ack2.getWriterId());
    assertEquals(2, ack2.getEventNumber());
    assertEquals(1, ack2.getPreviousEventNumber());
}
Also used : 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) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) UUID(java.util.UUID) Cleanup(lombok.Cleanup) CreateSegment(io.pravega.shared.protocol.netty.WireCommands.CreateSegment) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) Test(org.junit.Test)

Example 5 with DataAppended

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

the class AppendProcessorTest method testConditionalAppendFailure.

@Test
public void testConditionalAppendFailure() {
    String streamSegmentName = "testConditionalAppendFailure";
    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);
    AppendProcessor processor = new AppendProcessor(store, connection, new FailingRequestProcessor(), null);
    setupGetStreamSegmentInfo(streamSegmentName, clientId, store);
    CompletableFuture<Void> result = CompletableFuture.completedFuture(null);
    when(store.append(streamSegmentName, data, updateEventNumber(clientId, 1), AppendProcessor.TIMEOUT)).thenReturn(result);
    processor.setupAppend(new SetupAppend(1, clientId, streamSegmentName, ""));
    processor.append(new Append(streamSegmentName, clientId, 1, Unpooled.wrappedBuffer(data), null));
    result = Futures.failedFuture(new BadOffsetException(streamSegmentName, data.length, 0));
    when(store.append(streamSegmentName, 0, data, updateEventNumber(clientId, 2, 1, 1), AppendProcessor.TIMEOUT)).thenReturn(result);
    processor.append(new Append(streamSegmentName, clientId, 2, Unpooled.wrappedBuffer(data), 0L));
    verify(store).getStreamSegmentInfo(anyString(), eq(true), eq(AppendProcessor.TIMEOUT));
    verify(store).append(streamSegmentName, data, updateEventNumber(clientId, 1), AppendProcessor.TIMEOUT);
    verify(store).append(streamSegmentName, 0L, data, updateEventNumber(clientId, 2, 1, 1), AppendProcessor.TIMEOUT);
    verify(connection).send(new AppendSetup(1, streamSegmentName, clientId, 0));
    verify(connection, atLeast(0)).resumeReading();
    verify(connection).send(new DataAppended(clientId, 1, 0));
    verify(connection).send(new ConditionalCheckFailed(clientId, 2));
    verifyNoMoreInteractions(connection);
    verifyNoMoreInteractions(store);
}
Also used : FailingRequestProcessor(io.pravega.shared.protocol.netty.FailingRequestProcessor) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) ConditionalCheckFailed(io.pravega.shared.protocol.netty.WireCommands.ConditionalCheckFailed) 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)

Aggregations

DataAppended (io.pravega.shared.protocol.netty.WireCommands.DataAppended)9 UUID (java.util.UUID)8 Test (org.junit.Test)8 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)7 Append (io.pravega.shared.protocol.netty.Append)7 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)7 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)7 FailingRequestProcessor (io.pravega.shared.protocol.netty.FailingRequestProcessor)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 Cleanup (lombok.Cleanup)4 BadOffsetException (io.pravega.segmentstore.contracts.BadOffsetException)3 ConditionalCheckFailed (io.pravega.shared.protocol.netty.WireCommands.ConditionalCheckFailed)3 ByteBuf (io.netty.buffer.ByteBuf)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 CreateSegment (io.pravega.shared.protocol.netty.WireCommands.CreateSegment)2 SegmentCreated (io.pravega.shared.protocol.netty.WireCommands.SegmentCreated)2 Unpooled (io.netty.buffer.Unpooled)1 Segment (io.pravega.client.segment.impl.Segment)1 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)1 MockController (io.pravega.client.stream.mock.MockController)1