Search in sources :

Example 1 with KvStateInternalRequest

use of org.apache.flink.queryablestate.messages.KvStateInternalRequest in project flink by apache.

the class ClientTest method testServerClosesChannel.

/**
 * Tests that a server channel close, closes the connection and removes it from the established
 * connections.
 */
@Test
public void testServerClosesChannel() throws Exception {
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    final MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer = new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());
    Client<KvStateInternalRequest, KvStateResponse> client = null;
    Channel serverChannel = null;
    try {
        client = new Client<>("Test Client", 1, serializer, stats);
        final LinkedBlockingQueue<ByteBuf> received = new LinkedBlockingQueue<>();
        final AtomicReference<Channel> channel = new AtomicReference<>();
        serverChannel = createServerChannel(new ChannelDataCollectingHandler(channel, received));
        InetSocketAddress serverAddress = getKvStateServerAddress(serverChannel);
        // Requests
        KvStateInternalRequest request = new KvStateInternalRequest(new KvStateID(), new byte[0]);
        Future<KvStateResponse> future = client.sendRequest(serverAddress, request);
        received.take();
        assertEquals(1, stats.getNumConnections());
        channel.get().close().await();
        try {
            future.get();
            fail("Did not throw expected server failure");
        } catch (ExecutionException e) {
            if (!(e.getCause() instanceof ClosedChannelException)) {
                fail("Did not throw expected Exception");
            }
        // Expected
        }
        assertEquals(0L, stats.getNumConnections());
        // Counts can take some time to propagate
        while (stats.getNumSuccessful() != 0L || stats.getNumFailed() != 1L) {
            Thread.sleep(100L);
        }
        assertEquals(1L, stats.getNumRequests());
        assertEquals(0L, stats.getNumSuccessful());
        assertEquals(1L, stats.getNumFailed());
    } finally {
        if (client != null) {
            try {
                client.shutdown().get();
            } catch (Exception e) {
                e.printStackTrace();
            }
            Assert.assertTrue(client.isEventGroupShutdown());
        }
        if (serverChannel != null) {
            serverChannel.close();
        }
        assertEquals("Channel leak", 0L, stats.getNumConnections());
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) MessageSerializer(org.apache.flink.queryablestate.network.messages.MessageSerializer) InetSocketAddress(java.net.InetSocketAddress) SocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel) NioServerSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioServerSocketChannel) Channel(org.apache.flink.shaded.netty4.io.netty.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) KvStateInternalRequest(org.apache.flink.queryablestate.messages.KvStateInternalRequest) KvStateResponse(org.apache.flink.queryablestate.messages.KvStateResponse) KvStateID(org.apache.flink.queryablestate.KvStateID) ExecutionException(java.util.concurrent.ExecutionException) AtomicKvStateRequestStats(org.apache.flink.queryablestate.network.stats.AtomicKvStateRequestStats) Test(org.junit.Test)

Example 2 with KvStateInternalRequest

use of org.apache.flink.queryablestate.messages.KvStateInternalRequest in project flink by apache.

the class KvStateClientHandlerTest method testReadCallbacksAndBufferRecycling.

/**
 * Tests that on reads the expected callback methods are called and read buffers are recycled.
 */
@Test
public void testReadCallbacksAndBufferRecycling() throws Exception {
    final ClientHandlerCallback<KvStateResponse> callback = mock(ClientHandlerCallback.class);
    final MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer = new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());
    final EmbeddedChannel channel = new EmbeddedChannel(new ClientHandler<>("Test Client", serializer, callback));
    final byte[] content = new byte[0];
    final KvStateResponse response = new KvStateResponse(content);
    // 
    // Request success
    // 
    ByteBuf buf = MessageSerializer.serializeResponse(channel.alloc(), 1222112277L, response);
    // skip frame length
    buf.skipBytes(4);
    // Verify callback
    channel.writeInbound(buf);
    verify(callback, times(1)).onRequestResult(eq(1222112277L), any(KvStateResponse.class));
    assertEquals("Buffer not recycled", 0, buf.refCnt());
    // 
    // Request failure
    // 
    buf = MessageSerializer.serializeRequestFailure(channel.alloc(), 1222112278, new RuntimeException("Expected test Exception"));
    // skip frame length
    buf.skipBytes(4);
    // Verify callback
    channel.writeInbound(buf);
    verify(callback, times(1)).onRequestFailure(eq(1222112278L), isA(RuntimeException.class));
    assertEquals("Buffer not recycled", 0, buf.refCnt());
    // 
    // Server failure
    // 
    buf = MessageSerializer.serializeServerFailure(channel.alloc(), new RuntimeException("Expected test Exception"));
    // skip frame length
    buf.skipBytes(4);
    // Verify callback
    channel.writeInbound(buf);
    verify(callback, times(1)).onFailure(isA(RuntimeException.class));
    // 
    // Unexpected messages
    // 
    buf = channel.alloc().buffer(4).writeInt(1223823);
    // Verify callback
    channel.writeInbound(buf);
    verify(callback, times(1)).onFailure(isA(IllegalStateException.class));
    assertEquals("Buffer not recycled", 0, buf.refCnt());
    // 
    // Exception caught
    // 
    channel.pipeline().fireExceptionCaught(new RuntimeException("Expected test Exception"));
    verify(callback, times(3)).onFailure(isA(RuntimeException.class));
    // 
    // Channel inactive
    // 
    channel.pipeline().fireChannelInactive();
    verify(callback, times(1)).onFailure(isA(ClosedChannelException.class));
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) MessageSerializer(org.apache.flink.queryablestate.network.messages.MessageSerializer) EmbeddedChannel(org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) KvStateInternalRequest(org.apache.flink.queryablestate.messages.KvStateInternalRequest) KvStateResponse(org.apache.flink.queryablestate.messages.KvStateResponse) Test(org.junit.Test)

Example 3 with KvStateInternalRequest

use of org.apache.flink.queryablestate.messages.KvStateInternalRequest in project flink by apache.

the class KvStateServerHandlerTest method testChunkedResponse.

/**
 * Tests that large responses are chunked.
 */
@Test
public void testChunkedResponse() throws Exception {
    KvStateRegistry registry = new KvStateRegistry();
    KvStateRequestStats stats = new AtomicKvStateRequestStats();
    MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer = new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());
    KvStateServerHandler handler = new KvStateServerHandler(testServer, registry, serializer, 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 = createKeyedStateBackend(registry, numKeyGroups, abstractBackend, dummyEnv);
    final TestRegistryListener registryListener = new TestRegistryListener();
    registry.registerListener(dummyEnv.getJobID(), registryListener);
    // Register state
    ValueStateDescriptor<byte[]> desc = new ValueStateDescriptor<>("any", BytePrimitiveArraySerializer.INSTANCE);
    desc.setQueryable("vanilla");
    ValueState<byte[]> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, desc);
    // Update KvState
    byte[] bytes = new byte[2 * channel.config().getWriteBufferHighWaterMark()];
    byte current = 0;
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = current++;
    }
    int key = 99812822;
    backend.setCurrentKey(key);
    state.update(bytes);
    // Request
    byte[] serializedKeyAndNamespace = KvStateSerializer.serializeKeyAndNamespace(key, IntSerializer.INSTANCE, VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE);
    long requestId = Integer.MAX_VALUE + 182828L;
    assertTrue(registryListener.registrationName.equals("vanilla"));
    KvStateInternalRequest request = new KvStateInternalRequest(registryListener.kvStateId, serializedKeyAndNamespace);
    ByteBuf serRequest = MessageSerializer.serializeRequest(channel.alloc(), requestId, request);
    // Write the request and wait for the response
    channel.writeInbound(serRequest);
    Object msg = readInboundBlocking(channel);
    assertTrue("Not ChunkedByteBuf", msg instanceof ChunkedByteBuf);
    ((ChunkedByteBuf) msg).close();
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) MessageSerializer(org.apache.flink.queryablestate.network.messages.MessageSerializer) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) DisabledKvStateRequestStats(org.apache.flink.queryablestate.network.stats.DisabledKvStateRequestStats) AtomicKvStateRequestStats(org.apache.flink.queryablestate.network.stats.AtomicKvStateRequestStats) KvStateRequestStats(org.apache.flink.queryablestate.network.stats.KvStateRequestStats) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) KvStateServerHandler(org.apache.flink.queryablestate.server.KvStateServerHandler) KvStateResponse(org.apache.flink.queryablestate.messages.KvStateResponse) AbstractStateBackend(org.apache.flink.runtime.state.AbstractStateBackend) EmbeddedChannel(org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel) DummyEnvironment(org.apache.flink.runtime.operators.testutils.DummyEnvironment) KvStateInternalRequest(org.apache.flink.queryablestate.messages.KvStateInternalRequest) AtomicKvStateRequestStats(org.apache.flink.queryablestate.network.stats.AtomicKvStateRequestStats) Test(org.junit.Test)

Example 4 with KvStateInternalRequest

use of org.apache.flink.queryablestate.messages.KvStateInternalRequest in project flink by apache.

the class KvStateServerHandlerTest method testCloseChannelOnExceptionCaught.

/**
 * Tests that the channel is closed if an Exception reaches the channel handler.
 */
@Test
public void testCloseChannelOnExceptionCaught() throws Exception {
    KvStateRegistry registry = new KvStateRegistry();
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer = new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());
    KvStateServerHandler handler = new KvStateServerHandler(testServer, registry, serializer, stats);
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    channel.pipeline().fireExceptionCaught(new RuntimeException("Expected test Exception"));
    ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(MessageType.SERVER_FAILURE, MessageSerializer.deserializeHeader(buf));
    Throwable response = MessageSerializer.deserializeServerFailure(buf);
    buf.release();
    assertTrue(response.getMessage().contains("Expected test Exception"));
    channel.closeFuture().await(READ_TIMEOUT_MILLIS);
    assertFalse(channel.isActive());
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) MessageSerializer(org.apache.flink.queryablestate.network.messages.MessageSerializer) EmbeddedChannel(org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) KvStateServerHandler(org.apache.flink.queryablestate.server.KvStateServerHandler) KvStateInternalRequest(org.apache.flink.queryablestate.messages.KvStateInternalRequest) KvStateResponse(org.apache.flink.queryablestate.messages.KvStateResponse) AtomicKvStateRequestStats(org.apache.flink.queryablestate.network.stats.AtomicKvStateRequestStats) Test(org.junit.Test)

Example 5 with KvStateInternalRequest

use of org.apache.flink.queryablestate.messages.KvStateInternalRequest in project flink by apache.

the class KvStateServerHandlerTest method testQueryExecutorShutDown.

/**
 * Tests the failure response on a rejected execution, because the query executor has been
 * closed.
 */
@Test
public void testQueryExecutorShutDown() throws Throwable {
    KvStateRegistry registry = new KvStateRegistry();
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    KvStateServerImpl localTestServer = new KvStateServerImpl(InetAddress.getLocalHost().getHostName(), Collections.singletonList(0).iterator(), 1, 1, new KvStateRegistry(), new DisabledKvStateRequestStats());
    localTestServer.start();
    localTestServer.shutdown();
    assertTrue(localTestServer.getQueryExecutor().isTerminated());
    MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer = new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());
    KvStateServerHandler handler = new KvStateServerHandler(localTestServer, registry, serializer, 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 = createKeyedStateBackend(registry, numKeyGroups, abstractBackend, dummyEnv);
    final TestRegistryListener registryListener = new TestRegistryListener();
    registry.registerListener(dummyEnv.getJobID(), 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"));
    KvStateInternalRequest request = new KvStateInternalRequest(registryListener.kvStateId, new byte[0]);
    ByteBuf serRequest = MessageSerializer.serializeRequest(channel.alloc(), 282872L, request);
    // Write the request and wait for the response
    channel.writeInbound(serRequest);
    ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(MessageType.REQUEST_FAILURE, MessageSerializer.deserializeHeader(buf));
    RequestFailure response = MessageSerializer.deserializeRequestFailure(buf);
    buf.release();
    assertTrue(response.getCause().getMessage().contains("RejectedExecutionException"));
    assertEquals(1L, stats.getNumRequests());
    assertEquals(1L, stats.getNumFailed());
    localTestServer.shutdown();
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) MessageSerializer(org.apache.flink.queryablestate.network.messages.MessageSerializer) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) EmbeddedChannel(org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel) DummyEnvironment(org.apache.flink.runtime.operators.testutils.DummyEnvironment) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) KvStateServerImpl(org.apache.flink.queryablestate.server.KvStateServerImpl) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) KvStateServerHandler(org.apache.flink.queryablestate.server.KvStateServerHandler) KvStateInternalRequest(org.apache.flink.queryablestate.messages.KvStateInternalRequest) KvStateResponse(org.apache.flink.queryablestate.messages.KvStateResponse) RequestFailure(org.apache.flink.queryablestate.network.messages.RequestFailure) DisabledKvStateRequestStats(org.apache.flink.queryablestate.network.stats.DisabledKvStateRequestStats) AtomicKvStateRequestStats(org.apache.flink.queryablestate.network.stats.AtomicKvStateRequestStats) AbstractStateBackend(org.apache.flink.runtime.state.AbstractStateBackend) Test(org.junit.Test)

Aggregations

KvStateInternalRequest (org.apache.flink.queryablestate.messages.KvStateInternalRequest)22 KvStateResponse (org.apache.flink.queryablestate.messages.KvStateResponse)22 Test (org.junit.Test)22 MessageSerializer (org.apache.flink.queryablestate.network.messages.MessageSerializer)21 ByteBuf (org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)19 AtomicKvStateRequestStats (org.apache.flink.queryablestate.network.stats.AtomicKvStateRequestStats)17 KvStateRegistry (org.apache.flink.runtime.query.KvStateRegistry)12 KvStateID (org.apache.flink.queryablestate.KvStateID)11 EmbeddedChannel (org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel)11 KvStateServerHandler (org.apache.flink.queryablestate.server.KvStateServerHandler)10 ClosedChannelException (java.nio.channels.ClosedChannelException)7 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)7 DummyEnvironment (org.apache.flink.runtime.operators.testutils.DummyEnvironment)7 AbstractStateBackend (org.apache.flink.runtime.state.AbstractStateBackend)7 MemoryStateBackend (org.apache.flink.runtime.state.memory.MemoryStateBackend)7 ConnectException (java.net.ConnectException)6 InetSocketAddress (java.net.InetSocketAddress)6 UnknownHostException (java.net.UnknownHostException)6 ExecutionException (java.util.concurrent.ExecutionException)6 RequestFailure (org.apache.flink.queryablestate.network.messages.RequestFailure)5