Search in sources :

Example 6 with ByteBuf

use of io.netty.buffer.ByteBuf in project flink by apache.

the class KvStateRequestSerializer method serializeServerFailure.

/**
	 * Allocates a buffer and serializes the server failure into it.
	 *
	 * <p>The cause must not be or contain any user types as causes.
	 *
	 * @param alloc ByteBuf allocator for the buffer to serialize message into
	 * @param cause Failure cause
	 * @return Serialized server failure message
	 * @throws IOException Serialization failures are forwarded
	 */
public static ByteBuf serializeServerFailure(ByteBufAllocator alloc, Throwable cause) throws IOException {
    ByteBuf buf = alloc.ioBuffer();
    // Frame length is set at end
    buf.writeInt(0);
    writeHeader(buf, KvStateRequestType.SERVER_FAILURE);
    try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
        ObjectOutputStream out = new ObjectOutputStream(bbos)) {
        out.writeObject(cause);
    }
    // Set frame length
    int frameLength = buf.readableBytes() - 4;
    buf.setInt(0, frameLength);
    return buf;
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf) ObjectOutputStream(java.io.ObjectOutputStream)

Example 7 with ByteBuf

use of io.netty.buffer.ByteBuf in project flink by apache.

the class KvStateClientHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {
        ByteBuf buf = (ByteBuf) msg;
        KvStateRequestType msgType = KvStateRequestSerializer.deserializeHeader(buf);
        if (msgType == KvStateRequestType.REQUEST_RESULT) {
            KvStateRequestResult result = KvStateRequestSerializer.deserializeKvStateRequestResult(buf);
            callback.onRequestResult(result.getRequestId(), result.getSerializedResult());
        } else if (msgType == KvStateRequestType.REQUEST_FAILURE) {
            KvStateRequestFailure failure = KvStateRequestSerializer.deserializeKvStateRequestFailure(buf);
            callback.onRequestFailure(failure.getRequestId(), failure.getCause());
        } else if (msgType == KvStateRequestType.SERVER_FAILURE) {
            throw KvStateRequestSerializer.deserializeServerFailure(buf);
        } else {
            throw new IllegalStateException("Unexpected response type '" + msgType + "'");
        }
    } catch (Throwable t1) {
        try {
            callback.onFailure(t1);
        } catch (Throwable t2) {
            LOG.error("Failed to notify callback about failure", t2);
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}
Also used : KvStateRequestResult(org.apache.flink.runtime.query.netty.message.KvStateRequestResult) KvStateRequestType(org.apache.flink.runtime.query.netty.message.KvStateRequestType) KvStateRequestFailure(org.apache.flink.runtime.query.netty.message.KvStateRequestFailure) ByteBuf(io.netty.buffer.ByteBuf)

Example 8 with ByteBuf

use of io.netty.buffer.ByteBuf in project flink by apache.

the class PartitionRequestClientHandlerTest method createBufferResponse.

/**
	 * Returns a deserialized buffer message as it would be received during runtime.
	 */
private BufferResponse createBufferResponse(Buffer buffer, int sequenceNumber, InputChannelID receivingChannelId) throws IOException {
    // Mock buffer to serialize
    BufferResponse resp = new BufferResponse(buffer, sequenceNumber, receivingChannelId);
    ByteBuf serialized = resp.write(UnpooledByteBufAllocator.DEFAULT);
    // Skip general header bytes
    serialized.readBytes(NettyMessage.HEADER_LENGTH);
    BufferResponse deserialized = new BufferResponse();
    // Deserialize the bytes again. We have to go this way, because we only partly deserialize
    // the header of the response and wait for a buffer from the buffer pool to copy the payload
    // data into.
    deserialized.readFrom(serialized);
    return deserialized;
}
Also used : BufferResponse(org.apache.flink.runtime.io.network.netty.NettyMessage.BufferResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 9 with ByteBuf

use of io.netty.buffer.ByteBuf in project flink by apache.

the class KvStateServerHandlerTest method testQueryUnknownKvStateID.

/**
	 * Tests the failure response with {@link UnknownKvStateID} as cause on
	 * queries for unregistered KvStateIDs.
	 */
@Test
public void testQueryUnknownKvStateID() 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);
    long requestId = Integer.MAX_VALUE + 182828L;
    ByteBuf request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), requestId, new KvStateID(), new byte[0]);
    // Write the request and wait for the response
    channel.writeInbound(request);
    ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(KvStateRequestType.REQUEST_FAILURE, KvStateRequestSerializer.deserializeHeader(buf));
    KvStateRequestFailure response = KvStateRequestSerializer.deserializeKvStateRequestFailure(buf);
    assertEquals(requestId, response.getRequestId());
    assertTrue("Did not respond with expected failure cause", response.getCause() instanceof UnknownKvStateID);
    assertEquals(1, stats.getNumRequests());
    assertEquals(1, stats.getNumFailed());
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) KvStateRequestFailure(org.apache.flink.runtime.query.netty.message.KvStateRequestFailure) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) KvStateID(org.apache.flink.runtime.query.KvStateID) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 10 with ByteBuf

use of io.netty.buffer.ByteBuf in project flink by apache.

the class KvStateServerHandlerTest method testFailureOnGetSerializedValue.

/**
	 * Tests the failure response on a failure on the {@link InternalKvState#getSerializedValue(byte[])}
	 * call.
	 */
@Test
public void testFailureOnGetSerializedValue() 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);
    // Failing KvState
    InternalKvState<?> kvState = mock(InternalKvState.class);
    when(kvState.getSerializedValue(any(byte[].class))).thenThrow(new RuntimeException("Expected test Exception"));
    KvStateID kvStateId = registry.registerKvState(new JobID(), new JobVertexID(), new KeyGroupRange(0, 0), "vanilla", kvState);
    ByteBuf request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), 282872, kvStateId, new byte[0]);
    // Write the request and wait for the response
    channel.writeInbound(request);
    ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(KvStateRequestType.REQUEST_FAILURE, KvStateRequestSerializer.deserializeHeader(buf));
    KvStateRequestFailure response = KvStateRequestSerializer.deserializeKvStateRequestFailure(buf);
    assertTrue(response.getCause().getMessage().contains("Expected test Exception"));
    assertEquals(1, stats.getNumRequests());
    assertEquals(1, stats.getNumFailed());
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) KvStateRequestFailure(org.apache.flink.runtime.query.netty.message.KvStateRequestFailure) JobVertexID(org.apache.flink.runtime.jobgraph.JobVertexID) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) KvStateID(org.apache.flink.runtime.query.KvStateID) ByteBuf(io.netty.buffer.ByteBuf) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1517 Test (org.junit.Test)663 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)160 IOException (java.io.IOException)97 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)81 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)80 Test (org.testng.annotations.Test)68 InetSocketAddress (java.net.InetSocketAddress)59 Channel (io.netty.channel.Channel)57 ChannelFuture (io.netty.channel.ChannelFuture)56 ArrayList (java.util.ArrayList)53 Map (java.util.Map)44 ChannelPromise (io.netty.channel.ChannelPromise)41 AtomicReference (java.util.concurrent.atomic.AtomicReference)36 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)34 CountDownLatch (java.util.concurrent.CountDownLatch)34 HashMap (java.util.HashMap)33 RecyclableDuplicateByteBuf (io.netty.buffer.RecyclableDuplicateByteBuf)32 EventLoopGroup (io.netty.channel.EventLoopGroup)32 List (java.util.List)32