use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.
the class AppendProcessorTest method testAppendPipeliningWithSeal.
@Test(timeout = 15 * 1000)
public void testAppendPipeliningWithSeal() {
String streamSegmentName = "scope/stream/testAppendSegment";
UUID clientId = UUID.randomUUID();
byte[] data1 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
byte[] data2 = new byte[] { 1, 2, 3, 4, 5 };
byte[] data3 = new byte[] { 1, 2, 3 };
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();
InOrder connectionVerifier = Mockito.inOrder(connection);
setupGetAttributes(streamSegmentName, clientId, store);
processor.setupAppend(new SetupAppend(1, clientId, streamSegmentName, ""));
verify(store).getAttributes(anyString(), eq(Collections.singleton(AttributeId.fromUUID(clientId))), eq(true), eq(AppendProcessor.TIMEOUT));
connectionVerifier.verify(connection).send(new AppendSetup(1, streamSegmentName, clientId, 0));
// Initiate one append.
val store1 = new CompletableFuture<Long>();
val ac1 = interceptAppend(store, streamSegmentName, updateEventNumber(clientId, 1, 0, 1), store1);
processor.append(new Append(streamSegmentName, clientId, 1, 1, Unpooled.wrappedBuffer(data1), null, requestId));
verifyStoreAppend(ac1, data1);
verify(tracker).updateOutstandingBytes(connection, data1.length, data1.length);
// Second append will fail with StreamSegmentSealedException (this simulates having one append, immediately followed
// by a Seal, then another append).
val ac2 = interceptAppend(store, streamSegmentName, updateEventNumber(clientId, 2, 1, 1), Futures.failedFuture(new StreamSegmentSealedException(streamSegmentName)));
processor.append(new Append(streamSegmentName, clientId, 2, 1, Unpooled.wrappedBuffer(data2), null, requestId));
verifyStoreAppend(ac2, data2);
verify(tracker).updateOutstandingBytes(connection, data2.length, data1.length + data2.length);
verify(tracker).updateOutstandingBytes(connection, -data2.length, data1.length);
// Third append should fail just like the second one. However this will have a higher event number than that one
// and we use it to verify it won't prevent sending the SegmentIsSealed message or send it multiple times.
val ac3 = interceptAppend(store, streamSegmentName, updateEventNumber(clientId, 3, 2, 1), Futures.failedFuture(new StreamSegmentSealedException(streamSegmentName)));
processor.append(new Append(streamSegmentName, clientId, 3, 1, Unpooled.wrappedBuffer(data3), null, requestId));
verifyStoreAppend(ac3, data3);
verify(tracker).updateOutstandingBytes(connection, data3.length, data1.length + data3.length);
// Complete the first one and verify it is acked properly.
store1.complete(100L);
connectionVerifier.verify(connection).send(new DataAppended(requestId, clientId, 1, 0L, 100L));
// Verify that a SegmentIsSealed message is sent AFTER the ack from the first one (for the second append).
connectionVerifier.verify(connection).send(new WireCommands.SegmentIsSealed(requestId, streamSegmentName, "", 2L));
// Verify that a second SegmentIsSealed is sent (for the third append).
connectionVerifier.verify(connection).send(new WireCommands.SegmentIsSealed(requestId, streamSegmentName, "", 3L));
verify(tracker).updateOutstandingBytes(connection, -data3.length, data1.length);
verify(tracker).updateOutstandingBytes(connection, -data1.length, 0);
interceptAppend(store, streamSegmentName, updateEventNumber(clientId, 4, 3, 1), Futures.failedFuture(new IntentionalException(streamSegmentName)));
AssertExtensions.assertThrows("append() accepted a new request after sending a SegmentIsSealed message.", () -> processor.append(new Append(streamSegmentName, clientId, 4, 1, Unpooled.wrappedBuffer(data1), null, requestId)), ex -> ex instanceof IllegalStateException);
// Verify no more messages are sent over the connection.
connectionVerifier.verifyNoMoreInteractions();
verifyNoMoreInteractions(store);
}
use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.
the class AppendProcessorTest method testAppendFails.
@Test
public void testAppendFails() {
String streamSegmentName = "scope/stream/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);
val mockedRecorder = 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);
interceptAppend(store, streamSegmentName, updateEventNumber(clientId, data.length), Futures.failedFuture(new IntentionalException()));
processor.setupAppend(new SetupAppend(1, clientId, streamSegmentName, ""));
processor.append(new Append(streamSegmentName, clientId, data.length, 1, Unpooled.wrappedBuffer(data), null, requestId));
try {
processor.append(new Append(streamSegmentName, clientId, data.length * 2, 1, Unpooled.wrappedBuffer(data), null, requestId));
fail();
} catch (IllegalStateException e) {
// Expected
}
verify(connection).send(new AppendSetup(1, streamSegmentName, clientId, 0));
verify(tracker).updateOutstandingBytes(connection, data.length, data.length);
verify(connection).close();
verify(tracker).updateOutstandingBytes(connection, -data.length, 0);
verify(store, atMost(1)).append(any(), any(), any(), any());
verifyNoMoreInteractions(connection);
verify(mockedRecorder, never()).recordAppend(eq(streamSegmentName), eq(8L), eq(1), any());
}
use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.
the class SegmentOutputStreamImpl method write.
/**
* @see SegmentOutputStream#write(PendingEvent)
*/
@Override
public void write(PendingEvent event) {
// State is set to sealed during a Transaction abort and the segment writer should not throw an {@link IllegalStateException} in such a case.
checkState(!state.isAlreadySealed() || NameUtils.isTransactionSegment(segmentName), "Segment: %s is already sealed", segmentName);
synchronized (writeOrderLock) {
ClientConnection connection;
try {
// if connection is null getConnection() establishes a connection and retransmits all events in inflight
// list.
connection = Futures.getThrowingException(getConnection());
} catch (SegmentSealedException | NoSuchSegmentException e) {
// Add the event to inflight, this will be resent to the successor during the execution of resendToSuccessorsCallback
state.addToInflight(event);
return;
} catch (RetriesExhaustedException e) {
event.getAckFuture().completeExceptionally(e);
log.error("Failed to write event to Pravega due connectivity error ", e);
return;
}
long eventNumber = state.addToInflight(event);
try {
Append append = new Append(segmentName, writerId, eventNumber, event.getEventCount(), event.getData(), null, requestId);
log.trace("Sending append request: {}", append);
connection.send(append);
} catch (ConnectionFailedException e) {
log.warn("Failed writing event through writer " + writerId + " due to: ", e);
// As the message is inflight, this will perform the retransmission.
reconnect();
}
}
}
use of io.pravega.shared.protocol.netty.Append 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());
}
Aggregations