org.apache.flink.queryablestate.messages.KvStateRequest Java Examples

The following examples show how to use org.apache.flink.queryablestate.messages.KvStateRequest. 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: QueryableStateClient.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Create the Queryable State Client.
 * @param remoteAddress the {@link InetAddress address} of the {@code Client Proxy} to connect to.
 * @param remotePort the port of the proxy to connect to.
 */
public QueryableStateClient(final InetAddress remoteAddress, final int remotePort) {
	Preconditions.checkArgument(NetUtils.isValidHostPort(remotePort),
			"Remote Port " + remotePort + " is out of valid port range [0-65535].");

	this.remoteAddress = new InetSocketAddress(remoteAddress, remotePort);

	final MessageSerializer<KvStateRequest, KvStateResponse> messageSerializer =
			new MessageSerializer<>(
					new KvStateRequest.KvStateRequestDeserializer(),
					new KvStateResponse.KvStateResponseDeserializer());

	this.client = new Client<>(
			"Queryable State Client",
			1,
			messageSerializer,
			new DisabledKvStateRequestStats());
}
 
Example #2
Source File: KvStateClientProxyHandler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<KvStateResponse> getState(
		final KvStateRequest request,
		final boolean forceUpdate) {

	return getKvStateLookupInfo(request.getJobId(), request.getStateName(), forceUpdate)
			.thenComposeAsync((Function<KvStateLocation, CompletableFuture<KvStateResponse>>) location -> {
				final int keyGroupIndex = KeyGroupRangeAssignment.computeKeyGroupForKeyHash(
						request.getKeyHashCode(), location.getNumKeyGroups());

				final InetSocketAddress serverAddress = location.getKvStateServerAddress(keyGroupIndex);
				if (serverAddress == null) {
					return FutureUtils.completedExceptionally(new UnknownKvStateKeyGroupLocationException(getServerName()));
				} else {
					// Query server
					final KvStateID kvStateId = location.getKvStateID(keyGroupIndex);
					final KvStateInternalRequest internalRequest = new KvStateInternalRequest(
							kvStateId, request.getSerializedKeyAndNamespace());
					return kvStateClient.sendRequest(serverAddress, internalRequest);
				}
			}, queryExecutor);
}
 
Example #3
Source File: QueryableStateClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Create the Queryable State Client.
 * @param remoteAddress the {@link InetAddress address} of the {@code Client Proxy} to connect to.
 * @param remotePort the port of the proxy to connect to.
 */
public QueryableStateClient(final InetAddress remoteAddress, final int remotePort) {
	Preconditions.checkArgument(remotePort >= 0 && remotePort <= 65536,
			"Remote Port " + remotePort + " is out of valid port range (0-65536).");

	this.remoteAddress = new InetSocketAddress(remoteAddress, remotePort);

	final MessageSerializer<KvStateRequest, KvStateResponse> messageSerializer =
			new MessageSerializer<>(
					new KvStateRequest.KvStateRequestDeserializer(),
					new KvStateResponse.KvStateResponseDeserializer());

	this.client = new Client<>(
			"Queryable State Client",
			1,
			messageSerializer,
			new DisabledKvStateRequestStats());
}
 
Example #4
Source File: KvStateClientProxyHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<KvStateResponse> getState(
		final KvStateRequest request,
		final boolean forceUpdate) {

	return getKvStateLookupInfo(request.getJobId(), request.getStateName(), forceUpdate)
			.thenComposeAsync((Function<KvStateLocation, CompletableFuture<KvStateResponse>>) location -> {
				final int keyGroupIndex = KeyGroupRangeAssignment.computeKeyGroupForKeyHash(
						request.getKeyHashCode(), location.getNumKeyGroups());

				final InetSocketAddress serverAddress = location.getKvStateServerAddress(keyGroupIndex);
				if (serverAddress == null) {
					return FutureUtils.completedExceptionally(new UnknownKvStateKeyGroupLocationException(getServerName()));
				} else {
					// Query server
					final KvStateID kvStateId = location.getKvStateID(keyGroupIndex);
					final KvStateInternalRequest internalRequest = new KvStateInternalRequest(
							kvStateId, request.getSerializedKeyAndNamespace());
					return kvStateClient.sendRequest(serverAddress, internalRequest);
				}
			}, queryExecutor);
}
 
Example #5
Source File: KvStateClientProxyHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<KvStateResponse> getState(
		final KvStateRequest request,
		final boolean forceUpdate) {

	return getKvStateLookupInfo(request.getJobId(), request.getStateName(), forceUpdate)
			.thenComposeAsync((Function<KvStateLocation, CompletableFuture<KvStateResponse>>) location -> {
				final int keyGroupIndex = KeyGroupRangeAssignment.computeKeyGroupForKeyHash(
						request.getKeyHashCode(), location.getNumKeyGroups());

				final InetSocketAddress serverAddress = location.getKvStateServerAddress(keyGroupIndex);
				if (serverAddress == null) {
					return FutureUtils.completedExceptionally(new UnknownKvStateKeyGroupLocationException(getServerName()));
				} else {
					// Query server
					final KvStateID kvStateId = location.getKvStateID(keyGroupIndex);
					final KvStateInternalRequest internalRequest = new KvStateInternalRequest(
							kvStateId, request.getSerializedKeyAndNamespace());
					return kvStateClient.sendRequest(serverAddress, internalRequest);
				}
			}, queryExecutor);
}
 
Example #6
Source File: QueryableStateClient.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Create the Queryable State Client.
 * @param remoteAddress the {@link InetAddress address} of the {@code Client Proxy} to connect to.
 * @param remotePort the port of the proxy to connect to.
 */
public QueryableStateClient(final InetAddress remoteAddress, final int remotePort) {
	Preconditions.checkArgument(remotePort >= 0 && remotePort <= 65536,
			"Remote Port " + remotePort + " is out of valid port range (0-65536).");

	this.remoteAddress = new InetSocketAddress(remoteAddress, remotePort);

	final MessageSerializer<KvStateRequest, KvStateResponse> messageSerializer =
			new MessageSerializer<>(
					new KvStateRequest.KvStateRequestDeserializer(),
					new KvStateResponse.KvStateResponseDeserializer());

	this.client = new Client<>(
			"Queryable State Client",
			1,
			messageSerializer,
			new DisabledKvStateRequestStats());
}
 
Example #7
Source File: QueryableStateClient.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a future holding the serialized request result.
 *
 * @param jobId                     JobID of the job the queryable state
 *                                  belongs to
 * @param queryableStateName        Name under which the state is queryable
 * @param keyHashCode               Integer hash code of the key (result of
 *                                  a call to {@link Object#hashCode()}
 * @param serializedKeyAndNamespace Serialized key and namespace to query
 *                                  KvState instance with
 * @return Future holding the serialized result
 */
private CompletableFuture<KvStateResponse> getKvState(
		final JobID jobId,
		final String queryableStateName,
		final int keyHashCode,
		final byte[] serializedKeyAndNamespace) {
	LOG.debug("Sending State Request to {}.", remoteAddress);
	try {
		KvStateRequest request = new KvStateRequest(jobId, queryableStateName, keyHashCode, serializedKeyAndNamespace);
		return client.sendRequest(remoteAddress, request);
	} catch (Exception e) {
		LOG.error("Unable to send KVStateRequest: ", e);
		return FutureUtils.getFailedFuture(e);
	}
}
 
Example #8
Source File: KvStateClientProxyHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Create the handler used by the {@link KvStateClientProxyImpl}.
 *
 * @param proxy the {@link KvStateClientProxyImpl proxy} using the handler.
 * @param queryExecutorThreads the number of threads used to process incoming requests.
 * @param serializer the {@link MessageSerializer} used to (de-) serialize the different messages.
 * @param stats server statistics collector.
 */
public KvStateClientProxyHandler(
		final KvStateClientProxyImpl proxy,
		final int queryExecutorThreads,
		final MessageSerializer<KvStateRequest, KvStateResponse> serializer,
		final KvStateRequestStats stats) {

	super(proxy, serializer, stats);
	this.proxy = Preconditions.checkNotNull(proxy);
	this.kvStateClient = createInternalClient(queryExecutorThreads);
}
 
Example #9
Source File: KvStateClientProxyImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public AbstractServerHandler<KvStateRequest, KvStateResponse> initializeHandler() {
	MessageSerializer<KvStateRequest, KvStateResponse> serializer =
			new MessageSerializer<>(
					new KvStateRequest.KvStateRequestDeserializer(),
					new KvStateResponse.KvStateResponseDeserializer());
	return new KvStateClientProxyHandler(this, queryExecutorThreads, serializer, stats);
}
 
Example #10
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 #11
Source File: KvStateClientProxyHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<KvStateResponse> handleRequest(
		final long requestId,
		final KvStateRequest request) {
	CompletableFuture<KvStateResponse> response = new CompletableFuture<>();
	executeActionAsync(response, request, false);
	return response;
}
 
Example #12
Source File: KvStateClientProxyHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Create the handler used by the {@link KvStateClientProxyImpl}.
 *
 * @param proxy the {@link KvStateClientProxyImpl proxy} using the handler.
 * @param queryExecutorThreads the number of threads used to process incoming requests.
 * @param serializer the {@link MessageSerializer} used to (de-) serialize the different messages.
 * @param stats server statistics collector.
 */
public KvStateClientProxyHandler(
		final KvStateClientProxyImpl proxy,
		final int queryExecutorThreads,
		final MessageSerializer<KvStateRequest, KvStateResponse> serializer,
		final KvStateRequestStats stats) {

	super(proxy, serializer, stats);
	this.proxy = Preconditions.checkNotNull(proxy);
	this.kvStateClient = createInternalClient(queryExecutorThreads);
}
 
Example #13
Source File: QueryableStateClient.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a future holding the serialized request result.
 *
 * @param jobId                     JobID of the job the queryable state
 *                                  belongs to
 * @param queryableStateName        Name under which the state is queryable
 * @param keyHashCode               Integer hash code of the key (result of
 *                                  a call to {@link Object#hashCode()}
 * @param serializedKeyAndNamespace Serialized key and namespace to query
 *                                  KvState instance with
 * @return Future holding the serialized result
 */
private CompletableFuture<KvStateResponse> getKvState(
		final JobID jobId,
		final String queryableStateName,
		final int keyHashCode,
		final byte[] serializedKeyAndNamespace) {
	LOG.debug("Sending State Request to {}.", remoteAddress);
	try {
		KvStateRequest request = new KvStateRequest(jobId, queryableStateName, keyHashCode, serializedKeyAndNamespace);
		return client.sendRequest(remoteAddress, request);
	} catch (Exception e) {
		LOG.error("Unable to send KVStateRequest: ", e);
		return FutureUtils.getFailedFuture(e);
	}
}
 
Example #14
Source File: KvStateClientProxyImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public AbstractServerHandler<KvStateRequest, KvStateResponse> initializeHandler() {
	MessageSerializer<KvStateRequest, KvStateResponse> serializer =
			new MessageSerializer<>(
					new KvStateRequest.KvStateRequestDeserializer(),
					new KvStateResponse.KvStateResponseDeserializer());
	return new KvStateClientProxyHandler(this, queryExecutorThreads, serializer, stats);
}
 
Example #15
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 #16
Source File: KvStateClientProxyHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<KvStateResponse> handleRequest(
		final long requestId,
		final KvStateRequest request) {
	CompletableFuture<KvStateResponse> response = new CompletableFuture<>();
	executeActionAsync(response, request, false);
	return response;
}
 
Example #17
Source File: KvStateClientProxyHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Create the handler used by the {@link KvStateClientProxyImpl}.
 *
 * @param proxy the {@link KvStateClientProxyImpl proxy} using the handler.
 * @param queryExecutorThreads the number of threads used to process incoming requests.
 * @param serializer the {@link MessageSerializer} used to (de-) serialize the different messages.
 * @param stats server statistics collector.
 */
public KvStateClientProxyHandler(
		final KvStateClientProxyImpl proxy,
		final int queryExecutorThreads,
		final MessageSerializer<KvStateRequest, KvStateResponse> serializer,
		final KvStateRequestStats stats) {

	super(proxy, serializer, stats);
	this.proxy = Preconditions.checkNotNull(proxy);
	this.kvStateClient = createInternalClient(queryExecutorThreads);
}
 
Example #18
Source File: QueryableStateClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a future holding the serialized request result.
 *
 * @param jobId                     JobID of the job the queryable state
 *                                  belongs to
 * @param queryableStateName        Name under which the state is queryable
 * @param keyHashCode               Integer hash code of the key (result of
 *                                  a call to {@link Object#hashCode()}
 * @param serializedKeyAndNamespace Serialized key and namespace to query
 *                                  KvState instance with
 * @return Future holding the serialized result
 */
private CompletableFuture<KvStateResponse> getKvState(
		final JobID jobId,
		final String queryableStateName,
		final int keyHashCode,
		final byte[] serializedKeyAndNamespace) {
	LOG.debug("Sending State Request to {}.", remoteAddress);
	try {
		KvStateRequest request = new KvStateRequest(jobId, queryableStateName, keyHashCode, serializedKeyAndNamespace);
		return client.sendRequest(remoteAddress, request);
	} catch (Exception e) {
		LOG.error("Unable to send KVStateRequest: ", e);
		return FutureUtils.getFailedFuture(e);
	}
}
 
Example #19
Source File: KvStateClientProxyImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public AbstractServerHandler<KvStateRequest, KvStateResponse> initializeHandler() {
	MessageSerializer<KvStateRequest, KvStateResponse> serializer =
			new MessageSerializer<>(
					new KvStateRequest.KvStateRequestDeserializer(),
					new KvStateResponse.KvStateResponseDeserializer());
	return new KvStateClientProxyHandler(this, queryExecutorThreads, serializer, stats);
}
 
Example #20
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 #21
Source File: KvStateClientProxyHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<KvStateResponse> handleRequest(
		final long requestId,
		final KvStateRequest request) {
	CompletableFuture<KvStateResponse> response = new CompletableFuture<>();
	executeActionAsync(response, request, false);
	return response;
}