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());
}
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);
}
}
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());
}
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);
}
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);
}
Aggregations