Search in sources :

Example 6 with AppendBatchSizeTracker

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());
}
Also used : WireCommand(io.pravega.shared.protocol.netty.WireCommand) AppendBatchSizeTracker(io.pravega.shared.protocol.netty.AppendBatchSizeTracker) ByteBuf(io.netty.buffer.ByteBuf) AppendBlock(io.pravega.shared.protocol.netty.WireCommands.AppendBlock) Append(io.pravega.shared.protocol.netty.Append) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) AppendBlockEnd(io.pravega.shared.protocol.netty.WireCommands.AppendBlockEnd) Event(io.pravega.shared.protocol.netty.WireCommands.Event) UUID(java.util.UUID) Test(org.junit.Test)

Example 7 with AppendBatchSizeTracker

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());
}
Also used : WireCommand(io.pravega.shared.protocol.netty.WireCommand) AppendBatchSizeTracker(io.pravega.shared.protocol.netty.AppendBatchSizeTracker) ByteBuf(io.netty.buffer.ByteBuf) AppendBlock(io.pravega.shared.protocol.netty.WireCommands.AppendBlock) Append(io.pravega.shared.protocol.netty.Append) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) AppendBlockEnd(io.pravega.shared.protocol.netty.WireCommands.AppendBlockEnd) Event(io.pravega.shared.protocol.netty.WireCommands.Event) UUID(java.util.UUID) Test(org.junit.Test)

Example 8 with AppendBatchSizeTracker

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));
}
Also used : AppendBatchSizeTracker(io.pravega.shared.protocol.netty.AppendBatchSizeTracker) Test(org.junit.Test)

Example 9 with AppendBatchSizeTracker

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());
}
Also used : AppendBatchSizeTracker(io.pravega.shared.protocol.netty.AppendBatchSizeTracker) Duration(java.time.Duration) BiConsumer(java.util.function.BiConsumer) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Assert(org.junit.Assert) AppendBatchSizeTracker(io.pravega.shared.protocol.netty.AppendBatchSizeTracker) CompletableFuture(java.util.concurrent.CompletableFuture) Duration(java.time.Duration) Test(org.junit.Test)

Example 10 with AppendBatchSizeTracker

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());
}
Also used : WireCommand(io.pravega.shared.protocol.netty.WireCommand) AppendBatchSizeTracker(io.pravega.shared.protocol.netty.AppendBatchSizeTracker) ByteBuf(io.netty.buffer.ByteBuf) AppendBlock(io.pravega.shared.protocol.netty.WireCommands.AppendBlock) Append(io.pravega.shared.protocol.netty.Append) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) AppendBlockEnd(io.pravega.shared.protocol.netty.WireCommands.AppendBlockEnd) Event(io.pravega.shared.protocol.netty.WireCommands.Event) UUID(java.util.UUID) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Event(io.pravega.shared.protocol.netty.WireCommands.Event) Test(org.junit.Test)

Aggregations

AppendBatchSizeTracker (io.pravega.shared.protocol.netty.AppendBatchSizeTracker)12 Test (org.junit.Test)10 ByteBuf (io.netty.buffer.ByteBuf)9 Append (io.pravega.shared.protocol.netty.Append)8 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)8 WireCommand (io.pravega.shared.protocol.netty.WireCommand)8 AppendBlock (io.pravega.shared.protocol.netty.WireCommands.AppendBlock)8 AppendBlockEnd (io.pravega.shared.protocol.netty.WireCommands.AppendBlockEnd)8 Event (io.pravega.shared.protocol.netty.WireCommands.Event)8 UUID (java.util.UUID)8 WireCommands (io.pravega.shared.protocol.netty.WireCommands)6 CompletableFuture (java.util.concurrent.CompletableFuture)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 EpollSocketChannel (io.netty.channel.epoll.EpollSocketChannel)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1