List of usage examples for io.netty.buffer ByteBuf skipBytes
public abstract ByteBuf skipBytes(int length);
From source file:org.apache.flink.runtime.query.netty.KvStateServerHandlerTest.java
License:Apache License
/** * Tests that the channel is closed if an Exception reaches the channel * handler./*from w ww . j av a 2 s. com*/ */ @Test public void testCloseChannelOnExceptionCaught() throws Exception { KvStateRegistry registry = new KvStateRegistry(); AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats(); KvStateServerHandler handler = new KvStateServerHandler(registry, TEST_THREAD_POOL, stats); EmbeddedChannel channel = new EmbeddedChannel(handler); channel.pipeline().fireExceptionCaught(new RuntimeException("Expected test Exception")); ByteBuf buf = (ByteBuf) readInboundBlocking(channel); buf.skipBytes(4); // skip frame length // Verify the response assertEquals(KvStateRequestType.SERVER_FAILURE, KvStateRequestSerializer.deserializeHeader(buf)); Throwable response = KvStateRequestSerializer.deserializeServerFailure(buf); assertTrue(response.getMessage().contains("Expected test Exception")); channel.closeFuture().await(READ_TIMEOUT_MILLIS); assertFalse(channel.isActive()); }
From source file:org.apache.flink.runtime.query.netty.KvStateServerHandlerTest.java
License:Apache License
/** * Tests the failure response on a rejected execution, because the query * executor has been closed.// ww w .j a v a 2s .co m */ @Test public void testQueryExecutorShutDown() throws Exception { KvStateRegistry registry = new KvStateRegistry(); AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats(); ExecutorService closedExecutor = Executors.newSingleThreadExecutor(); closedExecutor.shutdown(); assertTrue(closedExecutor.isShutdown()); KvStateServerHandler handler = new KvStateServerHandler(registry, closedExecutor, stats); EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler); int numKeyGroups = 1; AbstractStateBackend abstractBackend = new MemoryStateBackend(); DummyEnvironment dummyEnv = new DummyEnvironment("test", 1, 0); dummyEnv.setKvStateRegistry(registry); KeyedStateBackend<Integer> backend = abstractBackend.createKeyedStateBackend(dummyEnv, new JobID(), "test_op", IntSerializer.INSTANCE, numKeyGroups, new KeyGroupRange(0, 0), registry.createTaskRegistry(dummyEnv.getJobID(), dummyEnv.getJobVertexId())); final TestRegistryListener registryListener = new TestRegistryListener(); registry.registerListener(registryListener); // Register state ValueStateDescriptor<Integer> desc = new ValueStateDescriptor<>("any", IntSerializer.INSTANCE); desc.setQueryable("vanilla"); backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, desc); assertTrue(registryListener.registrationName.equals("vanilla")); ByteBuf request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), 282872, registryListener.kvStateId, new byte[0]); // Write the request and wait for the response channel.writeInbound(request); ByteBuf buf = (ByteBuf) readInboundBlocking(channel); buf.skipBytes(4); // skip frame length // Verify the response assertEquals(KvStateRequestType.REQUEST_FAILURE, KvStateRequestSerializer.deserializeHeader(buf)); KvStateRequestFailure response = KvStateRequestSerializer.deserializeKvStateRequestFailure(buf); assertTrue(response.getCause().getMessage().contains("RejectedExecutionException")); assertEquals(1, stats.getNumRequests()); assertEquals(1, stats.getNumFailed()); }
From source file:org.apache.flink.runtime.query.netty.KvStateServerHandlerTest.java
License:Apache License
/** * Tests response on unexpected messages. *//*from www . j a v a2s .c o m*/ @Test public void testUnexpectedMessage() throws Exception { KvStateRegistry registry = new KvStateRegistry(); AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats(); KvStateServerHandler handler = new KvStateServerHandler(registry, TEST_THREAD_POOL, stats); EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler); // Write the request and wait for the response ByteBuf unexpectedMessage = Unpooled.buffer(8); unexpectedMessage.writeInt(4); unexpectedMessage.writeInt(123238213); channel.writeInbound(unexpectedMessage); ByteBuf buf = (ByteBuf) readInboundBlocking(channel); buf.skipBytes(4); // skip frame length // Verify the response assertEquals(KvStateRequestType.SERVER_FAILURE, KvStateRequestSerializer.deserializeHeader(buf)); Throwable response = KvStateRequestSerializer.deserializeServerFailure(buf); assertEquals(0, stats.getNumRequests()); assertEquals(0, stats.getNumFailed()); unexpectedMessage = KvStateRequestSerializer.serializeKvStateRequestResult(channel.alloc(), 192, new byte[0]); channel.writeInbound(unexpectedMessage); buf = (ByteBuf) readInboundBlocking(channel); buf.skipBytes(4); // skip frame length // Verify the response assertEquals(KvStateRequestType.SERVER_FAILURE, KvStateRequestSerializer.deserializeHeader(buf)); response = KvStateRequestSerializer.deserializeServerFailure(buf); assertTrue("Unexpected failure cause " + response.getClass().getName(), response instanceof IllegalArgumentException); assertEquals(0, stats.getNumRequests()); assertEquals(0, stats.getNumFailed()); }
From source file:org.apache.flink.runtime.query.netty.KvStateServerHandlerTest.java
License:Apache License
/** * Tests the failure response if the serializers don't match. *//* ww w .ja va2 s. co m*/ @Test public void testSerializerMismatch() throws Exception { KvStateRegistry registry = new KvStateRegistry(); AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats(); KvStateServerHandler handler = new KvStateServerHandler(registry, TEST_THREAD_POOL, stats); EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler); int numKeyGroups = 1; AbstractStateBackend abstractBackend = new MemoryStateBackend(); DummyEnvironment dummyEnv = new DummyEnvironment("test", 1, 0); dummyEnv.setKvStateRegistry(registry); AbstractKeyedStateBackend<Integer> backend = abstractBackend.createKeyedStateBackend(dummyEnv, new JobID(), "test_op", IntSerializer.INSTANCE, numKeyGroups, new KeyGroupRange(0, 0), registry.createTaskRegistry(dummyEnv.getJobID(), dummyEnv.getJobVertexId())); final TestRegistryListener registryListener = new TestRegistryListener(); registry.registerListener(registryListener); // Register state ValueStateDescriptor<Integer> desc = new ValueStateDescriptor<>("any", IntSerializer.INSTANCE); desc.setQueryable("vanilla"); ValueState<Integer> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, desc); int key = 99812822; // Update the KvState backend.setCurrentKey(key); state.update(712828289); byte[] wrongKeyAndNamespace = KvStateRequestSerializer.serializeKeyAndNamespace("wrong-key-type", StringSerializer.INSTANCE, "wrong-namespace-type", StringSerializer.INSTANCE); byte[] wrongNamespace = KvStateRequestSerializer.serializeKeyAndNamespace(key, IntSerializer.INSTANCE, "wrong-namespace-type", StringSerializer.INSTANCE); assertTrue(registryListener.registrationName.equals("vanilla")); ByteBuf request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), 182828, registryListener.kvStateId, wrongKeyAndNamespace); // Write the request and wait for the response channel.writeInbound(request); ByteBuf buf = (ByteBuf) readInboundBlocking(channel); buf.skipBytes(4); // skip frame length // Verify the response assertEquals(KvStateRequestType.REQUEST_FAILURE, KvStateRequestSerializer.deserializeHeader(buf)); KvStateRequestFailure response = KvStateRequestSerializer.deserializeKvStateRequestFailure(buf); assertEquals(182828, response.getRequestId()); assertTrue(response.getCause().getMessage().contains("IOException")); // Repeat with wrong namespace only request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), 182829, registryListener.kvStateId, wrongNamespace); // Write the request and wait for the response channel.writeInbound(request); buf = (ByteBuf) readInboundBlocking(channel); buf.skipBytes(4); // skip frame length // Verify the response assertEquals(KvStateRequestType.REQUEST_FAILURE, KvStateRequestSerializer.deserializeHeader(buf)); response = KvStateRequestSerializer.deserializeKvStateRequestFailure(buf); assertEquals(182829, response.getRequestId()); assertTrue(response.getCause().getMessage().contains("IOException")); assertEquals(2, stats.getNumRequests()); assertEquals(2, stats.getNumFailed()); }
From source file:org.apache.hadoop.hbase.ipc.AsyncServerResponseHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf inBuffer = (ByteBuf) msg; ByteBufInputStream in = new ByteBufInputStream(inBuffer); int totalSize = inBuffer.readableBytes(); try {/*from ww w . j a v a2 s. c om*/ // Read the header RPCProtos.ResponseHeader responseHeader = RPCProtos.ResponseHeader.parseDelimitedFrom(in); int id = responseHeader.getCallId(); AsyncCall call = channel.removePendingCall(id); if (call == null) { // So we got a response for which we have no corresponding 'call' here on the client-side. // We probably timed out waiting, cleaned up all references, and now the server decides // to return a response. There is nothing we can do w/ the response at this stage. Clean // out the wire of the response so its out of the way and we can get other responses on // this connection. int readSoFar = IPCUtil.getTotalSizeWhenWrittenDelimited(responseHeader); int whatIsLeftToRead = totalSize - readSoFar; // This is done through a Netty ByteBuf which has different behavior than InputStream. // It does not return number of bytes read but will update pointer internally and throws an // exception when too many bytes are to be skipped. inBuffer.skipBytes(whatIsLeftToRead); return; } if (responseHeader.hasException()) { RPCProtos.ExceptionResponse exceptionResponse = responseHeader.getException(); RemoteException re = createRemoteException(exceptionResponse); if (exceptionResponse.getExceptionClassName().equals(FatalConnectionException.class.getName())) { channel.close(re); } else { call.setFailed(re); } } else { Message value = null; // Call may be null because it may have timedout and been cleaned up on this side already if (call.responseDefaultType != null) { Message.Builder builder = call.responseDefaultType.newBuilderForType(); builder.mergeDelimitedFrom(in); value = builder.build(); } CellScanner cellBlockScanner = null; if (responseHeader.hasCellBlockMeta()) { int size = responseHeader.getCellBlockMeta().getLength(); byte[] cellBlock = new byte[size]; inBuffer.readBytes(cellBlock, 0, cellBlock.length); cellBlockScanner = channel.client.createCellScanner(cellBlock); } call.setSuccess(value, cellBlockScanner); } } catch (IOException e) { // Treat this as a fatal condition and close this connection channel.close(e); } finally { inBuffer.release(); } }
From source file:org.apache.hive.spark.client.rpc.KryoMessageCodec.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;/*from ww w. ja v a 2 s .com*/ } in.markReaderIndex(); int msgSize = in.readInt(); checkSize(msgSize); if (in.readableBytes() < msgSize) { // Incomplete message in buffer. in.resetReaderIndex(); return; } try { ByteBuffer nioBuffer = maybeDecrypt(in.nioBuffer(in.readerIndex(), msgSize)); Input kryoIn = new Input(new ByteBufferInputStream(nioBuffer)); Object msg = kryos.get().readClassAndObject(kryoIn); LOG.debug("Decoded message of type {} ({} bytes)", msg != null ? msg.getClass().getName() : msg, msgSize); out.add(msg); } finally { in.skipBytes(msgSize); } }
From source file:org.apache.jackrabbit.oak.plugins.segment.NetworkErrorProxy.java
License:Apache License
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof ByteBuf) { ByteBuf bb = (ByteBuf) msg; if (this.nrOfBytes > 0) { if (this.transferredBytes >= this.skipStartingPos) { bb.skipBytes(this.nrOfBytes); this.nrOfBytes = 0; } else { this.skipStartingPos -= messageSize(msg); }//from w w w . jav a 2 s. c om } } super.channelRead(ctx, msg); }
From source file:org.apache.pulsar.common.api.DoubleByteBufTest.java
License:Apache License
/** * Verify that readableBytes() returns writerIndex - readerIndex. In this case writerIndex is the end of the buffer * and readerIndex is increased by 64./*from w w w .java 2 s . c o m*/ * * @throws Exception */ @Test public void testReadableBytes() throws Exception { ByteBuf b1 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b1.writerIndex(b1.capacity()); ByteBuf b2 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128); b2.writerIndex(b2.capacity()); ByteBuf buf = DoubleByteBuf.get(b1, b2); assertEquals(buf.readerIndex(), 0); assertEquals(buf.writerIndex(), 256); assertEquals(buf.readableBytes(), 256); for (int i = 0; i < 4; ++i) { buf.skipBytes(64); assertEquals(buf.readableBytes(), 256 - 64 * (i + 1)); } }
From source file:org.apache.spark.network.protocol.MessageWithHeader.java
License:Apache License
private int copyByteBuf(ByteBuf buf, WritableByteChannel target) throws IOException { ByteBuffer buffer = buf.nioBuffer(); int written = (buffer.remaining() <= NIO_BUFFER_LIMIT) ? target.write(buffer) : writeNioBuffer(target, buffer); buf.skipBytes(written); return written; }
From source file:org.apache.spark.sql.hive.thriftserver.rsc.KryoMessageCodec.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;/* w w w .j av a 2 s. c o m*/ } in.markReaderIndex(); int msgSize = in.readInt(); checkSize(msgSize); if (in.readableBytes() < msgSize) { // Incomplete message in buffer. in.resetReaderIndex(); return; } try { ByteBuffer nioBuffer = maybeDecrypt(in.nioBuffer(in.readerIndex(), msgSize)); Object msg = serializer.deserialize(nioBuffer); LOG.info("Decoded message of type {} ({} bytes)", msg != null ? msg.getClass().getName() : msg, msgSize); out.add(msg); } catch (Exception e) { Throwable throwable = e; while (throwable != null) { LOG.info("tlitest cause: " + throwable.getCause()); LOG.info("tlitest message: " + throwable.getMessage()); StringBuilder builder = new StringBuilder(); for (StackTraceElement elem : throwable.getStackTrace()) { builder.append(elem); builder.append("\n"); } LOG.info(builder.toString()); throwable = throwable.getCause(); } throw e; } finally { in.skipBytes(msgSize); } }