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);
}
}
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);
}
Aggregations