use of org.apache.ignite.client.IgniteClientFuture in project ignite by apache.
the class ReliableChannel method affinityServiceAsync.
/**
* Send request to affinity node and handle response.
*/
public <T> IgniteClientFuture<T> affinityServiceAsync(int cacheId, Object key, ClientOperation op, Consumer<PayloadOutputChannel> payloadWriter, Function<PayloadInputChannel, T> payloadReader) throws ClientException, ClientError {
if (partitionAwarenessEnabled && affinityInfoIsUpToDate(cacheId)) {
UUID affNodeId = affinityCtx.affinityNode(cacheId, key);
if (affNodeId != null) {
CompletableFuture<T> fut = new CompletableFuture<>();
Object result = applyOnNodeChannel(affNodeId, channel -> channel.serviceAsync(op, payloadWriter, payloadReader).handle((res, err) -> {
if (err == null) {
fut.complete(res);
return null;
}
try {
// Will try to reinit channels if topology changed.
onChannelFailure(channel);
} catch (Throwable ex) {
fut.completeExceptionally(ex);
return null;
}
if (err instanceof ClientConnectionException) {
ClientConnectionException failure = (ClientConnectionException) err;
int attemptsLimit = getRetryLimit() - 1;
if (attemptsLimit == 0 || !shouldRetry(op, 0, failure)) {
fut.completeExceptionally(err);
return null;
}
handleServiceAsync(fut, op, payloadWriter, payloadReader, attemptsLimit, failure);
return null;
}
fut.completeExceptionally(err);
return null;
}));
if (result != null)
return new IgniteClientFutureImpl<>(fut);
}
}
return serviceAsync(op, payloadWriter, payloadReader);
}
use of org.apache.ignite.client.IgniteClientFuture in project ignite by apache.
the class ClientComputeImpl method executeAsync0.
/**
* @param taskName Task name.
* @param arg Argument.
* @param clusterGrp Cluster group.
* @param flags Flags.
* @param timeout Timeout.
*/
private <T, R> IgniteClientFuture<R> executeAsync0(String taskName, @Nullable T arg, ClientClusterGroupImpl clusterGrp, byte flags, long timeout) throws ClientException {
Collection<UUID> nodeIds = clusterGrp.nodeIds();
if (F.isEmpty(taskName))
throw new ClientException("Task name can't be null or empty.");
if (nodeIds != null && nodeIds.isEmpty())
throw new ClientException("Cluster group is empty.");
Consumer<PayloadOutputChannel> payloadWriter = ch -> writeExecuteTaskRequest(ch, taskName, arg, nodeIds, flags, timeout);
Function<PayloadInputChannel, ClientComputeTask<R>> payloadReader = ch -> {
Long taskId = ch.in().readLong();
ClientComputeTask<R> task = new ClientComputeTask<>(utils, ch.clientChannel(), taskId);
ch.clientChannel().addNotificationListener(COMPUTE_TASK_FINISHED, taskId, task);
return task;
};
IgniteClientFuture<ClientComputeTask<R>> initFut = ch.serviceAsync(COMPUTE_TASK_EXECUTE, payloadWriter, payloadReader);
CompletableFuture<R> resFut = new CompletableFuture<>();
AtomicReference<Object> cancellationToken = new AtomicReference<>();
initFut.handle((task, err) -> handleExecuteInitFuture(resFut, cancellationToken, task, err));
return new IgniteClientFutureImpl<>(resFut, mayInterruptIfRunning -> {
// 2. initFut has completed - cancel compute future.
if (!cancellationToken.compareAndSet(null, mayInterruptIfRunning)) {
GridFutureAdapter<?> fut = (GridFutureAdapter<?>) cancellationToken.get();
if (!cancelGridFuture(fut, mayInterruptIfRunning))
return false;
}
resFut.cancel(mayInterruptIfRunning);
return true;
});
}
Aggregations