Search in sources :

Example 1 with ConditionalCheckFailed

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

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

BadOffsetException (io.pravega.segmentstore.contracts.BadOffsetException)2 ConditionalCheckFailed (io.pravega.shared.protocol.netty.WireCommands.ConditionalCheckFailed)2 DataAppended (io.pravega.shared.protocol.netty.WireCommands.DataAppended)2 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)1 Append (io.pravega.shared.protocol.netty.Append)1 FailingRequestProcessor (io.pravega.shared.protocol.netty.FailingRequestProcessor)1 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)1 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)1 UUID (java.util.UUID)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1