Search in sources :

Example 26 with SetupAppend

use of io.pravega.shared.protocol.netty.WireCommands.SetupAppend in project pravega by pravega.

the class AppendTest method sendReceivingAppend.

@Test
public void sendReceivingAppend() 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());
    DataAppended ack = (DataAppended) sendRequest(channel, new Append(segment, uuid, data.readableBytes(), data, null));
    assertEquals(uuid, ack.getWriterId());
    assertEquals(data.readableBytes(), ack.getEventNumber());
    assertEquals(Long.MIN_VALUE, ack.getPreviousEventNumber());
}
Also used : StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) SegmentCreated(io.pravega.shared.protocol.netty.WireCommands.SegmentCreated) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) UUID(java.util.UUID) Cleanup(lombok.Cleanup) CreateSegment(io.pravega.shared.protocol.netty.WireCommands.CreateSegment) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) Test(org.junit.Test)

Example 27 with SetupAppend

use of io.pravega.shared.protocol.netty.WireCommands.SetupAppend in project pravega by pravega.

the class CommandEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
    log.trace("Encoding message to send over the wire {}", msg);
    if (msg instanceof Append) {
        Append append = (Append) msg;
        Session session = setupSegments.get(append.segment);
        if (session == null || !session.id.equals(append.getWriterId())) {
            throw new InvalidMessageException("Sending appends without setting up the append.");
        }
        if (append.getEventNumber() <= session.lastEventNumber) {
            throw new InvalidMessageException("Events written out of order. Received: " + append.getEventNumber() + " following: " + session.lastEventNumber);
        }
        if (append.isConditional()) {
            breakFromAppend(out);
            ConditionalAppend ca = new ConditionalAppend(append.writerId, append.eventNumber, append.getExpectedLength(), wrappedBuffer(serializeMessage(new Event(append.getData()))));
            writeMessage(ca, out);
        } else {
            Preconditions.checkState(bytesLeftInBlock == 0 || bytesLeftInBlock > TYPE_PLUS_LENGTH_SIZE, "Bug in CommandEncoder.encode, block is too small.");
            if (append.segment != segmentBeingAppendedTo) {
                breakFromAppend(out);
            }
            if (bytesLeftInBlock == 0) {
                currentBlockSize = Math.max(TYPE_PLUS_LENGTH_SIZE, blockSizeSupplier.getAppendBlockSize());
                bytesLeftInBlock = currentBlockSize;
                segmentBeingAppendedTo = append.segment;
                writeMessage(new AppendBlock(session.id), out);
                if (ctx != null) {
                    ctx.executor().schedule(new Flusher(ctx.channel(), currentBlockSize), blockSizeSupplier.getBatchTimeout(), TimeUnit.MILLISECONDS);
                }
            }
            session.lastEventNumber = append.getEventNumber();
            session.eventCount++;
            ByteBuf data = append.getData();
            int msgSize = TYPE_PLUS_LENGTH_SIZE + data.readableBytes();
            // Is there enough space for a subsequent message after this one?
            if (bytesLeftInBlock - msgSize > TYPE_PLUS_LENGTH_SIZE) {
                bytesLeftInBlock -= writeMessage(new Event(data), out);
            } else {
                byte[] serializedMessage = serializeMessage(new Event(data));
                int bytesInBlock = bytesLeftInBlock - TYPE_PLUS_LENGTH_SIZE;
                ByteBuf dataInsideBlock = wrappedBuffer(serializedMessage, 0, bytesInBlock);
                ByteBuf dataRemainging = wrappedBuffer(serializedMessage, bytesInBlock, serializedMessage.length - bytesInBlock);
                writeMessage(new PartialEvent(dataInsideBlock), out);
                writeMessage(new AppendBlockEnd(session.id, currentBlockSize - bytesLeftInBlock, dataRemainging, session.eventCount, session.lastEventNumber, 0L), out);
                bytesLeftInBlock = 0;
                session.eventCount = 0;
            }
        }
    } else if (msg instanceof SetupAppend) {
        breakFromAppend(out);
        writeMessage((SetupAppend) msg, out);
        SetupAppend setup = (SetupAppend) msg;
        setupSegments.put(setup.getSegment(), new Session(setup.getWriterId()));
    } else if (msg instanceof Flush) {
        Flush flush = (Flush) msg;
        if (currentBlockSize == flush.getBlockSize()) {
            breakFromAppend(out);
        }
    } else if (msg instanceof WireCommand) {
        breakFromAppend(out);
        writeMessage((WireCommand) msg, out);
    } else {
        throw new IllegalArgumentException("Expected a wire command and found: " + msg);
    }
}
Also used : ConditionalAppend(io.pravega.shared.protocol.netty.WireCommands.ConditionalAppend) Flush(io.pravega.shared.protocol.netty.WireCommands.Flush) ByteBuf(io.netty.buffer.ByteBuf) PartialEvent(io.pravega.shared.protocol.netty.WireCommands.PartialEvent) AppendBlock(io.pravega.shared.protocol.netty.WireCommands.AppendBlock) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) ConditionalAppend(io.pravega.shared.protocol.netty.WireCommands.ConditionalAppend) AppendBlockEnd(io.pravega.shared.protocol.netty.WireCommands.AppendBlockEnd) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) Event(io.pravega.shared.protocol.netty.WireCommands.Event) PartialEvent(io.pravega.shared.protocol.netty.WireCommands.PartialEvent)

Example 28 with SetupAppend

use of io.pravega.shared.protocol.netty.WireCommands.SetupAppend in project pravega by pravega.

the class AppendEncodeDecodeTest method testVerySmallBlockSize.

@Test
public void testVerySmallBlockSize() throws Exception {
    @Cleanup("release") ByteBuf fakeNetwork = ByteBufAllocator.DEFAULT.buffer();
    byte[] content = new byte[100];
    Arrays.fill(content, (byte) 1);
    ByteBuf buffer = Unpooled.wrappedBuffer(content);
    Append msg = new Append("segment", writerId, 1, buffer, null);
    CommandEncoder commandEncoder = new CommandEncoder(new FixedBatchSizeTracker(3));
    SetupAppend setupAppend = new SetupAppend(1, writerId, "segment", "");
    commandEncoder.encode(null, setupAppend, fakeNetwork);
    appendDecoder.processCommand(setupAppend);
    ArrayList<Object> received = new ArrayList<>();
    commandEncoder.encode(null, msg, fakeNetwork);
    read(fakeNetwork, received);
    assertEquals(2, received.size());
    Append readAppend = (Append) received.get(1);
    assertEquals(msg.data.readableBytes() + TYPE_PLUS_LENGTH_SIZE, readAppend.data.readableBytes());
}
Also used : SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 29 with SetupAppend

use of io.pravega.shared.protocol.netty.WireCommands.SetupAppend in project pravega by pravega.

the class AppendEncodeDecodeTest method testAppendAtBlockBound.

@Test
public void testAppendAtBlockBound() throws Exception {
    int size = appendBlockSize;
    @Cleanup("release") ByteBuf fakeNetwork = ByteBufAllocator.DEFAULT.buffer();
    ArrayList<Object> received = setupAppend(streamName, writerId, fakeNetwork);
    append(streamName, writerId, size, 1, size, fakeNetwork);
    read(fakeNetwork, received);
    assertEquals(1, received.size());
    append(streamName, writerId, size + size / 2, 2, size / 2, fakeNetwork);
    read(fakeNetwork, received);
    assertEquals(1, received.size());
    KeepAlive keepAlive = new KeepAlive();
    encoder.encode(null, keepAlive, fakeNetwork);
    read(fakeNetwork, received);
    assertEquals(3, received.size());
    Append one = (Append) received.get(0);
    Append two = (Append) received.get(1);
    assertEquals(size + TYPE_PLUS_LENGTH_SIZE, one.getData().readableBytes());
    assertEquals(size / 2 + TYPE_PLUS_LENGTH_SIZE, two.getData().readableBytes());
    KeepAlive three = (KeepAlive) received.get(2);
    assertEquals(keepAlive, three);
}
Also used : SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) KeepAlive(io.pravega.shared.protocol.netty.WireCommands.KeepAlive) ByteBuf(io.netty.buffer.ByteBuf) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 30 with SetupAppend

use of io.pravega.shared.protocol.netty.WireCommands.SetupAppend in project pravega by pravega.

the class AppendEncodeDecodeTest method testFlush.

private void testFlush(int size) throws Exception {
    @Cleanup("release") ByteBuf fakeNetwork = ByteBufAllocator.DEFAULT.buffer();
    ArrayList<Object> received = setupAppend(streamName, writerId, fakeNetwork);
    append(streamName, writerId, size, 0, size, fakeNetwork);
    read(fakeNetwork, received);
    KeepAlive keepAlive = new KeepAlive();
    encoder.encode(null, keepAlive, fakeNetwork);
    read(fakeNetwork, received);
    assertEquals(2, received.size());
    Append one = (Append) received.get(0);
    assertEquals(size + TYPE_PLUS_LENGTH_SIZE, one.getData().readableBytes());
    KeepAlive two = (KeepAlive) received.get(1);
    assertEquals(keepAlive, two);
}
Also used : SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) KeepAlive(io.pravega.shared.protocol.netty.WireCommands.KeepAlive) ByteBuf(io.netty.buffer.ByteBuf) Cleanup(lombok.Cleanup)

Aggregations

SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)39 Test (org.junit.Test)35 UUID (java.util.UUID)34 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)27 Append (io.pravega.shared.protocol.netty.Append)26 ClientConnection (io.pravega.client.netty.impl.ClientConnection)20 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)20 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)19 MockController (io.pravega.client.stream.mock.MockController)19 CompletableFuture (java.util.concurrent.CompletableFuture)17 Cleanup (lombok.Cleanup)17 PendingEvent (io.pravega.client.stream.impl.PendingEvent)15 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 InOrder (org.mockito.InOrder)13 FailingRequestProcessor (io.pravega.shared.protocol.netty.FailingRequestProcessor)11 WireCommands (io.pravega.shared.protocol.netty.WireCommands)11 ByteBuffer (java.nio.ByteBuffer)11 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)11 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)11