use of io.pravega.shared.protocol.netty.AppendBatchSizeTracker 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.AppendBatchSizeTracker 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.AppendBatchSizeTracker in project pravega by pravega.
the class FlowToBatchSizeTrackerTest method testGetAppendBatchSizeTrackerByFlowId.
@Test(timeout = 10000L)
public void testGetAppendBatchSizeTrackerByFlowId() {
FlowToBatchSizeTracker flowToBatchSizeTracker = new FlowToBatchSizeTracker();
// First, check the behavior when no flow id is in the cache.
AppendBatchSizeTracker tracker = flowToBatchSizeTracker.getAppendBatchSizeTrackerByFlowId(0);
Assert.assertNotNull(tracker);
int newTrackerBatchSize = tracker.getAppendBlockSize();
Assert.assertEquals("Error in newly instantiated tracker", newTrackerBatchSize, 0);
tracker.recordAppend(0, 1);
tracker.recordAppend(1, 1);
tracker.recordAppend(2, 1);
Assert.assertNotEquals(newTrackerBatchSize, tracker.getAppendBlockSize());
Assert.assertNotEquals(tracker, flowToBatchSizeTracker.getAppendBatchSizeTrackerByFlowId(1));
Assert.assertEquals(tracker, flowToBatchSizeTracker.getAppendBatchSizeTrackerByFlowId(0));
}
use of io.pravega.shared.protocol.netty.AppendBatchSizeTracker in project pravega by pravega.
the class FlowToBatchSizeTrackerTest method testFlowToBatchSizeTrackerEviction.
@Test(timeout = 10000L)
public void testFlowToBatchSizeTrackerEviction() throws InterruptedException {
Duration evictionTime = Duration.ofSeconds(1);
int cacheMaxSize = 10;
CompletableFuture<Void> latch = new CompletableFuture<>();
BiConsumer<Integer, AppendBatchSizeTracker> callback = (flow, tracker) -> latch.complete(null);
FlowToBatchSizeTracker flowToBatchSizeTracker = new FlowToBatchSizeTracker(cacheMaxSize, evictionTime, callback);
// First, add one entry in the flow cache.
AppendBatchSizeTracker tracker = flowToBatchSizeTracker.getAppendBatchSizeTrackerByFlowId(0);
Assert.assertNotNull(tracker);
// Confirm that the entry is stored in the cache.
Assert.assertEquals(tracker, flowToBatchSizeTracker.getAppendBatchSizeTrackerByFlowId(0));
// Wait for the expiration time to complete.
Thread.sleep(1000);
// Add another entry and wait for the cache clean up to get triggered.
flowToBatchSizeTracker.getAppendBatchSizeTrackerByFlowId(1);
// Wait for the eviction timeout to complete.
latch.join();
// Check that, if we get the tracker for the same flow, the object is a new one (i.e., it has been evicted and re-created)
Assert.assertNotEquals(tracker, flowToBatchSizeTracker.getAppendBatchSizeTrackerByFlowId(0));
// Check that the size constraints on the map are enforced.
for (int i = 2; i < 20; i++) {
flowToBatchSizeTracker.getAppendBatchSizeTrackerByFlowId(i);
}
Assert.assertEquals(cacheMaxSize, flowToBatchSizeTracker.getFlowToBatchSizeTrackerMap().size());
}
use of io.pravega.shared.protocol.netty.AppendBatchSizeTracker in project pravega by pravega.
the class CommandEncoderTest method testBatchTimeout.
@Test
public void testBatchTimeout() 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[40]);
WireCommands.Event event = new WireCommands.Event(data);
Append append1 = new Append("seg", writerId, 1, event, 0);
commandEncoder.write(append1);
long l = commandEncoder.batchTimeout(0);
// Triggers a timeout
commandEncoder.batchTimeout(l);
AppendBlock block = (AppendBlock) output.decoded.remove(0);
assertEquals(108, block.getData().readableBytes());
AppendBlockEnd blockEnd = (AppendBlockEnd) output.decoded.remove(0);
assertEquals(writerId, blockEnd.getWriterId());
assertEquals(48, blockEnd.getSizeOfWholeEvents());
assertEquals(0, blockEnd.getData().readableBytes());
assertEquals(1, blockEnd.getNumEvents());
assertTrue(output.decoded.isEmpty());
}
Aggregations