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