Search in sources :

Example 16 with AbstractStateBackend

use of org.apache.flink.runtime.state.AbstractStateBackend in project flink by apache.

the class KvStateServerHandlerTest method testSimpleQuery.

/**
 * Tests a simple successful query via an EmbeddedChannel.
 */
@Test
public void testSimpleQuery() 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);
    // Register state
    ValueStateDescriptor<Integer> desc = new ValueStateDescriptor<>("any", IntSerializer.INSTANCE);
    desc.setQueryable("vanilla");
    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);
    // Update the KvState and request it
    int expectedValue = 712828289;
    int key = 99812822;
    backend.setCurrentKey(key);
    ValueState<Integer> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, desc);
    state.update(expectedValue);
    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);
    ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(MessageType.REQUEST_RESULT, MessageSerializer.deserializeHeader(buf));
    long deserRequestId = MessageSerializer.getRequestId(buf);
    KvStateResponse response = serializer.deserializeResponse(buf);
    buf.release();
    assertEquals(requestId, deserRequestId);
    int actualValue = KvStateSerializer.deserializeValue(response.getContent(), IntSerializer.INSTANCE);
    assertEquals(expectedValue, actualValue);
    assertEquals(stats.toString(), 1, stats.getNumRequests());
    // Wait for async successful request report
    long deadline = System.nanoTime() + TimeUnit.NANOSECONDS.convert(30, TimeUnit.SECONDS);
    while (stats.getNumSuccessful() != 1L && System.nanoTime() <= deadline) {
        Thread.sleep(10L);
    }
    assertEquals(stats.toString(), 1L, stats.getNumSuccessful());
}
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) 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) AtomicKvStateRequestStats(org.apache.flink.queryablestate.network.stats.AtomicKvStateRequestStats) AbstractStateBackend(org.apache.flink.runtime.state.AbstractStateBackend) Test(org.junit.Test)

Aggregations

AbstractStateBackend (org.apache.flink.runtime.state.AbstractStateBackend)16 MemoryStateBackend (org.apache.flink.runtime.state.memory.MemoryStateBackend)15 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)14 DummyEnvironment (org.apache.flink.runtime.operators.testutils.DummyEnvironment)14 KvStateRegistry (org.apache.flink.runtime.query.KvStateRegistry)14 Test (org.junit.Test)14 JobID (org.apache.flink.api.common.JobID)9 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)9 KvStateInternalRequest (org.apache.flink.queryablestate.messages.KvStateInternalRequest)7 KvStateResponse (org.apache.flink.queryablestate.messages.KvStateResponse)7 AtomicKvStateRequestStats (org.apache.flink.queryablestate.network.stats.AtomicKvStateRequestStats)7 ByteBuf (io.netty.buffer.ByteBuf)6 MessageSerializer (org.apache.flink.queryablestate.network.messages.MessageSerializer)6 ByteBuf (org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)5 KvStateServerHandler (org.apache.flink.queryablestate.server.KvStateServerHandler)5 EmbeddedChannel (org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel)5 JobVertexID (org.apache.flink.runtime.jobgraph.JobVertexID)4 ExecutorService (java.util.concurrent.ExecutorService)3 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)3