org.apache.flink.queryablestate.exceptions.UnknownKvStateIdException Java Examples

The following examples show how to use org.apache.flink.queryablestate.exceptions.UnknownKvStateIdException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KvStateServerHandler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@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;
	}
}
 
Example #2
Source File: KvStateServerHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
@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;
	}
}
 
Example #3
Source File: KvStateServerHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
@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;
	}
}
 
Example #4
Source File: KvStateClientProxyHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void executeActionAsync(
		final CompletableFuture<KvStateResponse> result,
		final KvStateRequest request,
		final boolean update) {

	if (!result.isDone()) {
		final CompletableFuture<KvStateResponse> operationFuture = getState(request, update);
		operationFuture.whenCompleteAsync(
				(t, throwable) -> {
					if (throwable != null) {
						if (
								throwable.getCause() instanceof UnknownKvStateIdException ||
								throwable.getCause() instanceof UnknownKvStateKeyGroupLocationException ||
								throwable.getCause() instanceof ConnectException
							) {

							// These failures are likely to be caused by out-of-sync
							// KvStateLocation. Therefore we retry this query and
							// force look up the location.

							LOG.debug("Retrying after failing to retrieve state due to: {}.", throwable.getCause().getMessage());
							executeActionAsync(result, request, true);
						} else {
							result.completeExceptionally(throwable);
						}
					} else {
						result.complete(t);
					}
				}, queryExecutor);

		result.whenComplete(
				(t, throwable) -> operationFuture.cancel(false));
	}
}
 
Example #5
Source File: KvStateServerHandlerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the failure response with {@link UnknownKvStateIdException} as cause on
 * queries for unregistered KvStateIDs.
 */
@Test
public void testQueryUnknownKvStateID() 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);

	long requestId = Integer.MAX_VALUE + 182828L;

	KvStateInternalRequest request = new KvStateInternalRequest(new KvStateID(), new byte[0]);

	ByteBuf serRequest = MessageSerializer.serializeRequest(channel.alloc(), requestId, request);

	// Write the request and wait for the response
	channel.writeInbound(serRequest);

	ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
	buf.skipBytes(4); // skip frame length

	// Verify the response
	assertEquals(MessageType.REQUEST_FAILURE, MessageSerializer.deserializeHeader(buf));
	RequestFailure response = MessageSerializer.deserializeRequestFailure(buf);

	assertEquals(requestId, response.getRequestId());

	assertTrue("Did not respond with expected failure cause", response.getCause() instanceof UnknownKvStateIdException);

	assertEquals(1L, stats.getNumRequests());
	assertEquals(1L, stats.getNumFailed());
}
 
Example #6
Source File: KvStateClientProxyHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
private void executeActionAsync(
		final CompletableFuture<KvStateResponse> result,
		final KvStateRequest request,
		final boolean update) {

	if (!result.isDone()) {
		final CompletableFuture<KvStateResponse> operationFuture = getState(request, update);
		operationFuture.whenCompleteAsync(
				(t, throwable) -> {
					if (throwable != null) {
						if (
								throwable.getCause() instanceof UnknownKvStateIdException ||
								throwable.getCause() instanceof UnknownKvStateKeyGroupLocationException ||
								throwable.getCause() instanceof ConnectException
							) {

							// These failures are likely to be caused by out-of-sync
							// KvStateLocation. Therefore we retry this query and
							// force look up the location.

							LOG.debug("Retrying after failing to retrieve state due to: {}.", throwable.getCause().getMessage());
							executeActionAsync(result, request, true);
						} else {
							result.completeExceptionally(throwable);
						}
					} else {
						result.complete(t);
					}
				}, queryExecutor);

		result.whenComplete(
				(t, throwable) -> operationFuture.cancel(false));
	}
}
 
Example #7
Source File: KvStateServerHandlerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the failure response with {@link UnknownKvStateIdException} as cause on
 * queries for unregistered KvStateIDs.
 */
@Test
public void testQueryUnknownKvStateID() 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);

	long requestId = Integer.MAX_VALUE + 182828L;

	KvStateInternalRequest request = new KvStateInternalRequest(new KvStateID(), new byte[0]);

	ByteBuf serRequest = MessageSerializer.serializeRequest(channel.alloc(), requestId, request);

	// Write the request and wait for the response
	channel.writeInbound(serRequest);

	ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
	buf.skipBytes(4); // skip frame length

	// Verify the response
	assertEquals(MessageType.REQUEST_FAILURE, MessageSerializer.deserializeHeader(buf));
	RequestFailure response = MessageSerializer.deserializeRequestFailure(buf);

	assertEquals(requestId, response.getRequestId());

	assertTrue("Did not respond with expected failure cause", response.getCause() instanceof UnknownKvStateIdException);

	assertEquals(1L, stats.getNumRequests());
	assertEquals(1L, stats.getNumFailed());
}
 
Example #8
Source File: KvStateClientProxyHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
private void executeActionAsync(
		final CompletableFuture<KvStateResponse> result,
		final KvStateRequest request,
		final boolean update) {

	if (!result.isDone()) {
		final CompletableFuture<KvStateResponse> operationFuture = getState(request, update);
		operationFuture.whenCompleteAsync(
				(t, throwable) -> {
					if (throwable != null) {
						if (
								throwable.getCause() instanceof UnknownKvStateIdException ||
								throwable.getCause() instanceof UnknownKvStateKeyGroupLocationException ||
								throwable.getCause() instanceof ConnectException
							) {

							// These failures are likely to be caused by out-of-sync
							// KvStateLocation. Therefore we retry this query and
							// force look up the location.

							LOG.debug("Retrying after failing to retrieve state due to: {}.", throwable.getCause().getMessage());
							executeActionAsync(result, request, true);
						} else {
							result.completeExceptionally(throwable);
						}
					} else {
						result.complete(t);
					}
				}, queryExecutor);

		result.whenComplete(
				(t, throwable) -> operationFuture.cancel(false));
	}
}
 
Example #9
Source File: KvStateServerHandlerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the failure response with {@link UnknownKvStateIdException} as cause on
 * queries for unregistered KvStateIDs.
 */
@Test
public void testQueryUnknownKvStateID() 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);

	long requestId = Integer.MAX_VALUE + 182828L;

	KvStateInternalRequest request = new KvStateInternalRequest(new KvStateID(), new byte[0]);

	ByteBuf serRequest = MessageSerializer.serializeRequest(channel.alloc(), requestId, request);

	// Write the request and wait for the response
	channel.writeInbound(serRequest);

	ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
	buf.skipBytes(4); // skip frame length

	// 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 UnknownKvStateIdException);

	assertEquals(1L, stats.getNumRequests());
	assertEquals(1L, stats.getNumFailed());
}