Search in sources :

Example 11 with KvStateID

use of org.apache.flink.runtime.query.KvStateID in project flink by apache.

the class KvStateServerHandlerTest method testIncomingBufferIsRecycled.

/**
	 * Tests that incoming buffer instances are recycled.
	 */
@Test
public void testIncomingBufferIsRecycled() 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);
    ByteBuf request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), 282872, new KvStateID(), new byte[0]);
    assertEquals(1, request.refCnt());
    // Write regular request
    channel.writeInbound(request);
    assertEquals("Buffer not recycled", 0, request.refCnt());
    // Write unexpected msg
    ByteBuf unexpected = channel.alloc().buffer(8);
    unexpected.writeInt(4);
    unexpected.writeInt(4);
    assertEquals(1, unexpected.refCnt());
    channel.writeInbound(unexpected);
    assertEquals("Buffer not recycled", 0, unexpected.refCnt());
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) KvStateID(org.apache.flink.runtime.query.KvStateID) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 12 with KvStateID

use of org.apache.flink.runtime.query.KvStateID in project flink by apache.

the class KvStateRequestSerializerTest method testKvStateRequestSerialization.

/**
	 * Tests KvState request serialization.
	 */
@Test
public void testKvStateRequestSerialization() throws Exception {
    long requestId = Integer.MAX_VALUE + 1337L;
    KvStateID kvStateId = new KvStateID();
    byte[] serializedKeyAndNamespace = randomByteArray(1024);
    ByteBuf buf = KvStateRequestSerializer.serializeKvStateRequest(alloc, requestId, kvStateId, serializedKeyAndNamespace);
    int frameLength = buf.readInt();
    assertEquals(KvStateRequestType.REQUEST, KvStateRequestSerializer.deserializeHeader(buf));
    KvStateRequest request = KvStateRequestSerializer.deserializeKvStateRequest(buf);
    assertEquals(buf.readerIndex(), frameLength + 4);
    assertEquals(requestId, request.getRequestId());
    assertEquals(kvStateId, request.getKvStateId());
    assertArrayEquals(serializedKeyAndNamespace, request.getSerializedKeyAndNamespace());
}
Also used : KvStateID(org.apache.flink.runtime.query.KvStateID) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 13 with KvStateID

use of org.apache.flink.runtime.query.KvStateID in project flink by apache.

the class KvStateClientTest method testFailureClosesChannel.

/**
	 * Tests that a server failure closes the connection and removes it from
	 * the established connections.
	 */
@Test
public void testFailureClosesChannel() throws Exception {
    Deadline deadline = TEST_TIMEOUT.fromNow();
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    KvStateClient client = null;
    Channel serverChannel = null;
    try {
        client = new KvStateClient(1, stats);
        final LinkedBlockingQueue<ByteBuf> received = new LinkedBlockingQueue<>();
        final AtomicReference<Channel> channel = new AtomicReference<>();
        serverChannel = createServerChannel(new ChannelInboundHandlerAdapter() {

            @Override
            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                channel.set(ctx.channel());
            }

            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                received.add((ByteBuf) msg);
            }
        });
        KvStateServerAddress serverAddress = getKvStateServerAddress(serverChannel);
        // Requests
        List<Future<byte[]>> futures = new ArrayList<>();
        futures.add(client.getKvState(serverAddress, new KvStateID(), new byte[0]));
        futures.add(client.getKvState(serverAddress, new KvStateID(), new byte[0]));
        ByteBuf buf = received.poll(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
        assertNotNull("Receive timed out", buf);
        buf.release();
        buf = received.poll(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
        assertNotNull("Receive timed out", buf);
        buf.release();
        assertEquals(1, stats.getNumConnections());
        Channel ch = channel.get();
        assertNotNull("Channel not active", ch);
        // Respond with failure
        ch.writeAndFlush(KvStateRequestSerializer.serializeServerFailure(serverChannel.alloc(), new RuntimeException("Expected test server failure")));
        try {
            Await.result(futures.remove(0), deadline.timeLeft());
            fail("Did not throw expected server failure");
        } catch (RuntimeException ignored) {
        // Expected
        }
        try {
            Await.result(futures.remove(0), deadline.timeLeft());
            fail("Did not throw expected server failure");
        } catch (RuntimeException ignored) {
        // Expected
        }
        assertEquals(0, stats.getNumConnections());
        // Counts can take some time to propagate
        while (deadline.hasTimeLeft() && (stats.getNumSuccessful() != 0 || stats.getNumFailed() != 2)) {
            Thread.sleep(100);
        }
        assertEquals(2, stats.getNumRequests());
        assertEquals(0, stats.getNumSuccessful());
        assertEquals(2, stats.getNumFailed());
    } finally {
        if (client != null) {
            client.shutDown();
        }
        if (serverChannel != null) {
            serverChannel.close();
        }
        assertEquals("Channel leak", 0, stats.getNumConnections());
    }
}
Also used : Deadline(scala.concurrent.duration.Deadline) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) KvStateServerAddress(org.apache.flink.runtime.query.KvStateServerAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ByteBuf(io.netty.buffer.ByteBuf) Future(scala.concurrent.Future) KvStateID(org.apache.flink.runtime.query.KvStateID) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Aggregations

KvStateID (org.apache.flink.runtime.query.KvStateID)13 Test (org.junit.Test)12 ByteBuf (io.netty.buffer.ByteBuf)8 KvStateServerAddress (org.apache.flink.runtime.query.KvStateServerAddress)6 Deadline (scala.concurrent.duration.Deadline)6 Channel (io.netty.channel.Channel)4 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)4 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)4 SocketChannel (io.netty.channel.socket.SocketChannel)4 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)4 ArrayList (java.util.ArrayList)4 KvStateRegistry (org.apache.flink.runtime.query.KvStateRegistry)4 Future (scala.concurrent.Future)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 JobID (org.apache.flink.api.common.JobID)3 JobVertexID (org.apache.flink.runtime.jobgraph.JobVertexID)3 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)3 ConnectException (java.net.ConnectException)2