Search in sources :

Example 21 with KvStateResponse

use of org.apache.flink.queryablestate.messages.KvStateResponse in project flink-mirror by flink-ci.

the class MessageSerializerTest method testRequestSerialization.

/**
 * Tests request serialization.
 */
@Test
public void testRequestSerialization() throws Exception {
    long requestId = Integer.MAX_VALUE + 1337L;
    KvStateID kvStateId = new KvStateID();
    byte[] serializedKeyAndNamespace = randomByteArray(1024);
    final KvStateInternalRequest request = new KvStateInternalRequest(kvStateId, serializedKeyAndNamespace);
    final MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer = new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());
    ByteBuf buf = MessageSerializer.serializeRequest(alloc, requestId, request);
    int frameLength = buf.readInt();
    assertEquals(MessageType.REQUEST, MessageSerializer.deserializeHeader(buf));
    assertEquals(requestId, MessageSerializer.getRequestId(buf));
    KvStateInternalRequest requestDeser = serializer.deserializeRequest(buf);
    assertEquals(buf.readerIndex(), frameLength + 4);
    assertEquals(kvStateId, requestDeser.getKvStateId());
    assertArrayEquals(serializedKeyAndNamespace, requestDeser.getSerializedKeyAndNamespace());
}
Also used : MessageSerializer(org.apache.flink.queryablestate.network.messages.MessageSerializer) KvStateInternalRequest(org.apache.flink.queryablestate.messages.KvStateInternalRequest) KvStateID(org.apache.flink.queryablestate.KvStateID) KvStateResponse(org.apache.flink.queryablestate.messages.KvStateResponse) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 22 with KvStateResponse

use of org.apache.flink.queryablestate.messages.KvStateResponse in project flink-mirror by flink-ci.

the class MessageSerializerTest method testRequestSerializationWithZeroLengthKeyAndNamespace.

/**
 * Tests request serialization with zero-length serialized key and namespace.
 */
@Test
public void testRequestSerializationWithZeroLengthKeyAndNamespace() throws Exception {
    long requestId = Integer.MAX_VALUE + 1337L;
    KvStateID kvStateId = new KvStateID();
    byte[] serializedKeyAndNamespace = new byte[0];
    final KvStateInternalRequest request = new KvStateInternalRequest(kvStateId, serializedKeyAndNamespace);
    final MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer = new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());
    ByteBuf buf = MessageSerializer.serializeRequest(alloc, requestId, request);
    int frameLength = buf.readInt();
    assertEquals(MessageType.REQUEST, MessageSerializer.deserializeHeader(buf));
    assertEquals(requestId, MessageSerializer.getRequestId(buf));
    KvStateInternalRequest requestDeser = serializer.deserializeRequest(buf);
    assertEquals(buf.readerIndex(), frameLength + 4);
    assertEquals(kvStateId, requestDeser.getKvStateId());
    assertArrayEquals(serializedKeyAndNamespace, requestDeser.getSerializedKeyAndNamespace());
}
Also used : MessageSerializer(org.apache.flink.queryablestate.network.messages.MessageSerializer) KvStateInternalRequest(org.apache.flink.queryablestate.messages.KvStateInternalRequest) KvStateID(org.apache.flink.queryablestate.KvStateID) KvStateResponse(org.apache.flink.queryablestate.messages.KvStateResponse) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 23 with KvStateResponse

use of org.apache.flink.queryablestate.messages.KvStateResponse in project flink-mirror by flink-ci.

the class KvStateServerHandler method handleRequest.

@Override
public CompletableFuture<KvStateResponse> handleRequest(final long requestId, final KvStateInternalRequest request) {
    final CompletableFuture<KvStateResponse> responseFuture = new CompletableFuture<>();
    try {
        final KvStateEntry<?, ?, ?> kvState = registry.getKvState(request.getKvStateId());
        if (kvState == null) {
            responseFuture.completeExceptionally(new UnknownKvStateIdException(getServerName(), request.getKvStateId()));
        } else {
            byte[] serializedKeyAndNamespace = request.getSerializedKeyAndNamespace();
            byte[] serializedResult = getSerializedValue(kvState, serializedKeyAndNamespace);
            if (serializedResult != null) {
                responseFuture.complete(new KvStateResponse(serializedResult));
            } else {
                responseFuture.completeExceptionally(new UnknownKeyOrNamespaceException(getServerName()));
            }
        }
        return responseFuture;
    } catch (Throwable t) {
        String errMsg = "Error while processing request with ID " + requestId + ". Caused by: " + ExceptionUtils.stringifyException(t);
        responseFuture.completeExceptionally(new RuntimeException(errMsg));
        return responseFuture;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) UnknownKeyOrNamespaceException(org.apache.flink.queryablestate.exceptions.UnknownKeyOrNamespaceException) UnknownKvStateIdException(org.apache.flink.queryablestate.exceptions.UnknownKvStateIdException) KvStateResponse(org.apache.flink.queryablestate.messages.KvStateResponse)

Example 24 with KvStateResponse

use of org.apache.flink.queryablestate.messages.KvStateResponse in project flink-mirror by flink-ci.

the class KvStateServerHandlerTest method testQueryUnknownKey.

/**
 * Tests the failure response with {@link UnknownKeyOrNamespaceException} as cause on queries
 * for non-existing keys.
 */
@Test
public void testQueryUnknownKey() 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(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);
    byte[] serializedKeyAndNamespace = KvStateSerializer.serializeKeyAndNamespace(1238283, IntSerializer.INSTANCE, VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE);
    long requestId = Integer.MAX_VALUE + 22982L;
    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);
    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();
    assertEquals(requestId, response.getRequestId());
    assertTrue("Did not respond with expected failure cause", response.getCause() instanceof UnknownKeyOrNamespaceException);
    assertEquals(1L, stats.getNumRequests());
    assertEquals(1L, stats.getNumFailed());
}
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) UnknownKeyOrNamespaceException(org.apache.flink.queryablestate.exceptions.UnknownKeyOrNamespaceException) 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) 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) AtomicKvStateRequestStats(org.apache.flink.queryablestate.network.stats.AtomicKvStateRequestStats) AbstractStateBackend(org.apache.flink.runtime.state.AbstractStateBackend) Test(org.junit.Test)

Example 25 with KvStateResponse

use of org.apache.flink.queryablestate.messages.KvStateResponse in project flink by splunk.

the class ClientTest method testFailureClosesChannel.

/**
 * Tests that a server failure closes the connection and removes it from the established
 * connections.
 */
@Test
public void testFailureClosesChannel() 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
        List<Future<KvStateResponse>> futures = new ArrayList<>();
        KvStateInternalRequest request = new KvStateInternalRequest(new KvStateID(), new byte[0]);
        futures.add(client.sendRequest(serverAddress, request));
        futures.add(client.sendRequest(serverAddress, request));
        ByteBuf buf = received.take();
        assertNotNull("Receive timed out", buf);
        buf.release();
        buf = received.take();
        assertNotNull("Receive timed out", buf);
        buf.release();
        assertEquals(1L, stats.getNumConnections());
        Channel ch = channel.get();
        assertNotNull("Channel not active", ch);
        // Respond with failure
        ch.writeAndFlush(MessageSerializer.serializeServerFailure(serverChannel.alloc(), new RuntimeException("Expected test server failure")));
        try {
            futures.remove(0).get();
            fail("Did not throw expected server failure");
        } catch (ExecutionException e) {
            if (!(e.getCause() instanceof RuntimeException)) {
                fail("Did not throw expected Exception");
            }
        // Expected
        }
        try {
            futures.remove(0).get();
            fail("Did not throw expected server failure");
        } catch (ExecutionException e) {
            if (!(e.getCause() instanceof RuntimeException)) {
                fail("Did not throw expected Exception");
            }
        // Expected
        }
        assertEquals(0L, stats.getNumConnections());
        // Counts can take some time to propagate
        while (stats.getNumSuccessful() != 0L || stats.getNumFailed() != 2L) {
            Thread.sleep(100L);
        }
        assertEquals(2L, stats.getNumRequests());
        assertEquals(0L, stats.getNumSuccessful());
        assertEquals(2L, 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 : 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) ArrayList(java.util.ArrayList) 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) Future(java.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture) 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)

Aggregations

KvStateResponse (org.apache.flink.queryablestate.messages.KvStateResponse)69 KvStateInternalRequest (org.apache.flink.queryablestate.messages.KvStateInternalRequest)66 Test (org.junit.Test)66 MessageSerializer (org.apache.flink.queryablestate.network.messages.MessageSerializer)63 ByteBuf (org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)57 AtomicKvStateRequestStats (org.apache.flink.queryablestate.network.stats.AtomicKvStateRequestStats)51 KvStateRegistry (org.apache.flink.runtime.query.KvStateRegistry)36 KvStateID (org.apache.flink.queryablestate.KvStateID)33 EmbeddedChannel (org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel)33 KvStateServerHandler (org.apache.flink.queryablestate.server.KvStateServerHandler)30 ClosedChannelException (java.nio.channels.ClosedChannelException)21 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)21 DummyEnvironment (org.apache.flink.runtime.operators.testutils.DummyEnvironment)21 AbstractStateBackend (org.apache.flink.runtime.state.AbstractStateBackend)21 MemoryStateBackend (org.apache.flink.runtime.state.memory.MemoryStateBackend)21 ConnectException (java.net.ConnectException)18 InetSocketAddress (java.net.InetSocketAddress)18 UnknownHostException (java.net.UnknownHostException)18 ExecutionException (java.util.concurrent.ExecutionException)18 CompletableFuture (java.util.concurrent.CompletableFuture)15