@Override protected void decode(ChannelHandlerContext ctx, WireCommand command, List<Object> out) throws Exception { Object result = processCommand(command); if (result != null) { out.add(result); } }
private void read(ByteBuf in, List<Object> results) throws Exception { ByteBuf segmented = (ByteBuf) lengthDecoder.decode(null, in); while (segmented != null) { int before = results.size(); WireCommand command = CommandDecoder.parseCommand(segmented); if (appendDecoder.acceptInboundMessage(command)) { Request request = appendDecoder.processCommand(command); if (request != null) { results.add(request); } } else { results.add(command); } assertTrue(results.size() == before || results.size() == before + 1); segmented.release(); segmented = (ByteBuf) lengthDecoder.decode(null, in); } }
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { SslHandler handler = sslCtx.newHandler(ch.alloc()); p.addLast(handler); } ServerConnectionInboundHandler lsh = new ServerConnectionInboundHandler(); // p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new ExceptionLoggingHandler(ch.remoteAddress().toString()), new CommandEncoder(null), new LengthFieldBasedFrameDecoder(MAX_WIRECOMMAND_SIZE, 4, 4), new CommandDecoder(), new AppendDecoder(), lsh); lsh.setRequestProcessor(new AppendProcessor(store, lsh, new PravegaRequestProcessor(store, tableStore, lsh, statsRecorder, tokenVerifier, MetricsProvider.getDynamicLogger(), replyWithStackTraceOnError), statsRecorder, tokenVerifier, MetricsProvider.getDynamicLogger(), replyWithStackTraceOnError)); } });
case CONDITIONAL_APPEND: WireCommands.ConditionalAppend ca = (WireCommands.ConditionalAppend) command; segment = getSegment(ca.getWriterId()); if (ca.getEventNumber() < segment.lastEventNumber) { throw new InvalidMessageException("Last event number went backwards."); break; case APPEND_BLOCK: getSegment(((WireCommands.AppendBlock) command).getWriterId()); currentBlock = (WireCommands.AppendBlock) command; result = null; throw new InvalidMessageException("AppendBlockEnd for wrong connection."); segment = getSegment(writerId); if (blockEnd.getLastEventNumber() < segment.lastEventNumber) { throw new InvalidMessageException("Last event number went backwards."); throw new InvalidMessageException("Invalid SizeOfWholeEvents in block"); ByteBuf appendDataBuf = getAppendDataBuf(blockEnd, sizeOfWholeEventsInBlock); segment.lastEventNumber = blockEnd.getLastEventNumber(); currentBlock = null;
private ArrayList<Object> setupAppend(String testStream, UUID writerId, ByteBuf fakeNetwork) throws Exception { SetupAppend setupAppend = new SetupAppend(1, writerId, testStream, ""); encoder.encode(null, setupAppend, fakeNetwork); ArrayList<Object> received = new ArrayList<>(); WireCommand command = CommandDecoder.parseCommand(fakeNetwork); assertTrue(appendDecoder.acceptInboundMessage(command)); assertEquals(setupAppend, appendDecoder.processCommand(command)); return received; }
static EmbeddedChannel createChannel(StreamSegmentStore store) { ServerConnectionInboundHandler lsh = new ServerConnectionInboundHandler(); EmbeddedChannel channel = new EmbeddedChannel(new ExceptionLoggingHandler(""), new CommandEncoder(null), new LengthFieldBasedFrameDecoder(MAX_WIRECOMMAND_SIZE, 4, 4), new CommandDecoder(), new AppendDecoder(), lsh); lsh.setRequestProcessor(new AppendProcessor(store, lsh, new PravegaRequestProcessor(store, mock(TableStore.class), lsh), null)); return channel; }
static EmbeddedChannel createChannel(StreamSegmentStore store) { ServerConnectionInboundHandler lsh = new ServerConnectionInboundHandler(); EmbeddedChannel channel = new EmbeddedChannel(new ExceptionLoggingHandler(""), new CommandEncoder(null), new LengthFieldBasedFrameDecoder(MAX_WIRECOMMAND_SIZE, 4, 4), new CommandDecoder(), new AppendDecoder(), lsh); lsh.setRequestProcessor(new AppendProcessor(store, lsh, new PravegaRequestProcessor(store, mock(TableStore.class), lsh), null)); return channel; }
@Test public void testVerySmallBlockSize() throws Exception { @Cleanup("release") ByteBuf fakeNetwork = ByteBufAllocator.DEFAULT.buffer(); byte[] content = new byte[100]; Arrays.fill(content, (byte) 1); Event event = new Event(Unpooled.wrappedBuffer(content)); Append msg = new Append("segment", writerId, 1, event); 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(), readAppend.data.readableBytes()); assertEquals(content.length + TYPE_PLUS_LENGTH_SIZE, readAppend.data.readableBytes()); }