Search in sources :

Example 1 with UnknownKeyOrNamespaceException

use of org.apache.flink.queryablestate.exceptions.UnknownKeyOrNamespaceException in project flink by apache.

the class QsStateClient method main.

public static void main(final String[] args) throws Exception {
    ParameterTool parameters = ParameterTool.fromArgs(args);
    // setup values
    String jobId = parameters.getRequired("job-id");
    String host = parameters.get("host", "localhost");
    int port = parameters.getInt("port", 9069);
    int numIterations = parameters.getInt("iterations", 1500);
    QueryableStateClient client = new QueryableStateClient(host, port);
    client.setExecutionConfig(new ExecutionConfig());
    MapStateDescriptor<EmailId, EmailInformation> stateDescriptor = new MapStateDescriptor<>(QsConstants.STATE_NAME, TypeInformation.of(new TypeHint<EmailId>() {
    }), TypeInformation.of(new TypeHint<EmailInformation>() {
    }));
    System.out.println("Wait until the state can be queried.");
    // wait for state to exist
    for (int i = 0; i < BOOTSTRAP_RETRIES; i++) {
        // ~120s
        try {
            getMapState(jobId, client, stateDescriptor);
            break;
        } catch (ExecutionException e) {
            if (e.getCause() instanceof UnknownKeyOrNamespaceException) {
                System.err.println("State does not exist yet; sleeping 500ms");
                Thread.sleep(500L);
            } else {
                throw e;
            }
        }
        if (i == (BOOTSTRAP_RETRIES - 1)) {
            throw new RuntimeException("Timeout: state doesn't exist after 120s");
        }
    }
    System.out.println(String.format("State exists. Start querying it %d times.", numIterations));
    // query state
    for (int iterations = 0; iterations < numIterations; iterations++) {
        MapState<EmailId, EmailInformation> mapState = getMapState(jobId, client, stateDescriptor);
        int counter = 0;
        for (Map.Entry<EmailId, EmailInformation> entry : mapState.entries()) {
            // this is to force deserialization
            entry.getKey();
            entry.getValue();
            counter++;
        }
        System.out.println(// we look for it in the test
        "MapState has " + counter + " entries");
        Thread.sleep(100L);
    }
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) TypeHint(org.apache.flink.api.common.typeinfo.TypeHint) UnknownKeyOrNamespaceException(org.apache.flink.queryablestate.exceptions.UnknownKeyOrNamespaceException) QueryableStateClient(org.apache.flink.queryablestate.client.QueryableStateClient) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) TypeHint(org.apache.flink.api.common.typeinfo.TypeHint) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map)

Example 2 with UnknownKeyOrNamespaceException

use of org.apache.flink.queryablestate.exceptions.UnknownKeyOrNamespaceException in project flink by apache.

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 3 with UnknownKeyOrNamespaceException

use of org.apache.flink.queryablestate.exceptions.UnknownKeyOrNamespaceException in project flink by apache.

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)

Aggregations

UnknownKeyOrNamespaceException (org.apache.flink.queryablestate.exceptions.UnknownKeyOrNamespaceException)3 KvStateResponse (org.apache.flink.queryablestate.messages.KvStateResponse)2 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)1 MapStateDescriptor (org.apache.flink.api.common.state.MapStateDescriptor)1 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)1 TypeHint (org.apache.flink.api.common.typeinfo.TypeHint)1 ParameterTool (org.apache.flink.api.java.utils.ParameterTool)1 QueryableStateClient (org.apache.flink.queryablestate.client.QueryableStateClient)1 UnknownKvStateIdException (org.apache.flink.queryablestate.exceptions.UnknownKvStateIdException)1 KvStateInternalRequest (org.apache.flink.queryablestate.messages.KvStateInternalRequest)1 MessageSerializer (org.apache.flink.queryablestate.network.messages.MessageSerializer)1 RequestFailure (org.apache.flink.queryablestate.network.messages.RequestFailure)1 AtomicKvStateRequestStats (org.apache.flink.queryablestate.network.stats.AtomicKvStateRequestStats)1 KvStateServerHandler (org.apache.flink.queryablestate.server.KvStateServerHandler)1 DummyEnvironment (org.apache.flink.runtime.operators.testutils.DummyEnvironment)1 KvStateRegistry (org.apache.flink.runtime.query.KvStateRegistry)1 AbstractStateBackend (org.apache.flink.runtime.state.AbstractStateBackend)1