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);
}
}
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());
}
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;
}
}
Aggregations