Search in sources :

Example 1 with KvStateID

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

the class KvStateClientTest method testServerClosesChannel.

/**
	 * Tests that a server channel close, closes the connection and removes it
	 * from the established connections.
	 */
@Test
public void testServerClosesChannel() throws Exception {
    Deadline deadline = TEST_TIMEOUT.fromNow();
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    KvStateClient client = null;
    Channel serverChannel = null;
    try {
        client = new KvStateClient(1, stats);
        final AtomicBoolean received = new AtomicBoolean();
        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.set(true);
            }
        });
        KvStateServerAddress serverAddress = getKvStateServerAddress(serverChannel);
        // Requests
        Future<byte[]> future = client.getKvState(serverAddress, new KvStateID(), new byte[0]);
        while (!received.get() && deadline.hasTimeLeft()) {
            Thread.sleep(50);
        }
        assertTrue("Receive timed out", received.get());
        assertEquals(1, stats.getNumConnections());
        channel.get().close().await(deadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
        try {
            Await.result(future, deadline.timeLeft());
            fail("Did not throw expected server failure");
        } catch (ClosedChannelException ignored) {
        // Expected
        }
        assertEquals(0, stats.getNumConnections());
        // Counts can take some time to propagate
        while (deadline.hasTimeLeft() && (stats.getNumSuccessful() != 0 || stats.getNumFailed() != 1)) {
            Thread.sleep(100);
        }
        assertEquals(1, stats.getNumRequests());
        assertEquals(0, stats.getNumSuccessful());
        assertEquals(1, stats.getNumFailed());
    } finally {
        if (client != null) {
            client.shutDown();
        }
        if (serverChannel != null) {
            serverChannel.close();
        }
        assertEquals("Channel leak", 0, stats.getNumConnections());
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) Deadline(scala.concurrent.duration.Deadline) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) KvStateServerAddress(org.apache.flink.runtime.query.KvStateServerAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KvStateID(org.apache.flink.runtime.query.KvStateID) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 2 with KvStateID

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

the class KvStateClientTest method testRequestUnavailableHost.

/**
	 * Tests that a request to an unavailable host is failed with ConnectException.
	 */
@Test
public void testRequestUnavailableHost() throws Exception {
    Deadline deadline = TEST_TIMEOUT.fromNow();
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    KvStateClient client = null;
    try {
        client = new KvStateClient(1, stats);
        int availablePort = NetUtils.getAvailablePort();
        KvStateServerAddress serverAddress = new KvStateServerAddress(InetAddress.getLocalHost(), availablePort);
        Future<byte[]> future = client.getKvState(serverAddress, new KvStateID(), new byte[0]);
        try {
            Await.result(future, deadline.timeLeft());
            fail("Did not throw expected ConnectException");
        } catch (ConnectException ignored) {
        // Expected
        }
    } finally {
        if (client != null) {
            client.shutDown();
        }
        assertEquals("Channel leak", 0, stats.getNumConnections());
    }
}
Also used : Deadline(scala.concurrent.duration.Deadline) KvStateServerAddress(org.apache.flink.runtime.query.KvStateServerAddress) KvStateID(org.apache.flink.runtime.query.KvStateID) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 3 with KvStateID

use of org.apache.flink.runtime.query.KvStateID 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 4 with KvStateID

use of org.apache.flink.runtime.query.KvStateID 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)

Example 5 with KvStateID

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

the class KvStateRequestSerializerTest method testKvStateRequestSerializationWithZeroLengthKeyAndNamespace.

/**
	 * Tests KvState request serialization with zero-length serialized key and namespace.
	 */
@Test
public void testKvStateRequestSerializationWithZeroLengthKeyAndNamespace() throws Exception {
    byte[] serializedKeyAndNamespace = new byte[0];
    ByteBuf buf = KvStateRequestSerializer.serializeKvStateRequest(alloc, 1823, new KvStateID(), serializedKeyAndNamespace);
    int frameLength = buf.readInt();
    assertEquals(KvStateRequestType.REQUEST, KvStateRequestSerializer.deserializeHeader(buf));
    KvStateRequest request = KvStateRequestSerializer.deserializeKvStateRequest(buf);
    assertEquals(buf.readerIndex(), frameLength + 4);
    assertArrayEquals(serializedKeyAndNamespace, request.getSerializedKeyAndNamespace());
}
Also used : KvStateID(org.apache.flink.runtime.query.KvStateID) ByteBuf(io.netty.buffer.ByteBuf) 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