use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.
the class CommandEncoderTest method testExactBatch.
@Test
public void testExactBatch() throws IOException {
AppendBatchSizeTracker batchSizeTracker = new FixedBatchSizeTracker(100);
DecodingOutputStream output = new DecodingOutputStream();
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
CommandEncoder commandEncoder = new CommandEncoder(x -> batchSizeTracker, null, output, null, endpoint);
UUID writerId = UUID.randomUUID();
WireCommand setupAppend = new WireCommands.SetupAppend(0, writerId, "seg", "");
commandEncoder.write(setupAppend);
assertEquals(output.decoded.remove(0), setupAppend);
ByteBuf data = Unpooled.wrappedBuffer(new byte[100]);
Event event = new WireCommands.Event(data);
Append append = new Append("seg", writerId, 1, event, 0);
commandEncoder.write(append);
AppendBlock block = (AppendBlock) output.decoded.remove(0);
assertEquals(108, block.getData().readableBytes());
AppendBlockEnd blockEnd = (AppendBlockEnd) output.decoded.remove(0);
assertEquals(writerId, blockEnd.getWriterId());
assertEquals(108, blockEnd.getSizeOfWholeEvents());
assertEquals(0, blockEnd.getData().readableBytes());
assertEquals(1, blockEnd.getNumEvents());
}
use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.
the class CommandEncoderTest method testOverBatchSize.
@Test
public void testOverBatchSize() throws IOException {
AppendBatchSizeTracker batchSizeTracker = new FixedBatchSizeTracker(100);
DecodingOutputStream output = new DecodingOutputStream();
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
CommandEncoder commandEncoder = new CommandEncoder(x -> batchSizeTracker, null, output, null, endpoint);
UUID writerId = UUID.randomUUID();
WireCommand setupAppend = new WireCommands.SetupAppend(0, writerId, "seg", "");
commandEncoder.write(setupAppend);
assertEquals(output.decoded.remove(0), setupAppend);
ByteBuf data = Unpooled.wrappedBuffer(new byte[200]);
Event event = new WireCommands.Event(data);
Append append = new Append("seg", writerId, 1, event, 0);
commandEncoder.write(append);
AppendBlock block = (AppendBlock) output.decoded.remove(0);
assertEquals(208, block.getData().readableBytes());
AppendBlockEnd blockEnd = (AppendBlockEnd) output.decoded.remove(0);
assertEquals(writerId, blockEnd.getWriterId());
assertEquals(208, blockEnd.getSizeOfWholeEvents());
assertEquals(0, blockEnd.getData().readableBytes());
assertEquals(1, blockEnd.getNumEvents());
}
use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.
the class AppendTest method sendLargeAppend.
@Test(timeout = 10000)
public void sendLargeAppend() throws Exception {
String segment = "sendLargeAppend";
ByteBuf data = Unpooled.wrappedBuffer(new byte[Serializer.MAX_EVENT_SIZE]);
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());
}
use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.
the class AppendTest method testMultipleAppends.
@Test(timeout = 10000)
public void testMultipleAppends() throws Exception {
String segment = "testMultipleAppends";
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());
data.retain();
DataAppended ack = (DataAppended) sendRequest(channel, new Append(segment, uuid, 1, new Event(data), 1L));
assertEquals(uuid, ack.getWriterId());
assertEquals(1, ack.getEventNumber());
assertEquals(Long.MIN_VALUE, ack.getPreviousEventNumber());
DataAppended ack2 = (DataAppended) sendRequest(channel, new Append(segment, uuid, 2, new Event(data), 1L));
assertEquals(uuid, ack2.getWriterId());
assertEquals(2, ack2.getEventNumber());
assertEquals(1, ack2.getPreviousEventNumber());
}
use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.
the class AppendProcessor method append.
/**
* Append data to the store.
*/
@Override
public void append(Append append) {
long traceId = LoggerHelpers.traceEnter(log, "append", append);
UUID id = append.getWriterId();
WriterState state = this.writerStates.get(Pair.of(append.getSegment(), id));
Preconditions.checkState(state != null, "Data from unexpected connection: Segment=%s, WriterId=%s.", append.getSegment(), id);
long previousEventNumber = state.beginAppend(append.getEventNumber());
int appendLength = append.getData().readableBytes();
this.connection.adjustOutstandingBytes(appendLength);
Timer timer = new Timer();
storeAppend(append, previousEventNumber).whenComplete((newLength, ex) -> {
handleAppendResult(append, newLength, ex, state, timer);
LoggerHelpers.traceLeave(log, "storeAppend", traceId, append, ex);
}).whenComplete((v, e) -> {
this.connection.adjustOutstandingBytes(-appendLength);
append.getData().release();
});
}
Aggregations