Search in sources :

Example 1 with IgniteClientFuture

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);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClientRetryPolicy(org.apache.ignite.client.ClientRetryPolicy) ClientException(org.apache.ignite.client.ClientException) Map(java.util.Map) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) IgniteBinary(org.apache.ignite.IgniteBinary) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ClientAuthorizationException(org.apache.ignite.client.ClientAuthorizationException) ClientAuthenticationException(org.apache.ignite.client.ClientAuthenticationException) GridNioClientConnectionMultiplexer(org.apache.ignite.internal.client.thin.io.gridnioserver.GridNioClientConnectionMultiplexer) F(org.apache.ignite.internal.util.typedef.F) HostAndPortRange(org.apache.ignite.internal.util.HostAndPortRange) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteClientFuture(org.apache.ignite.client.IgniteClientFuture) Set(java.util.Set) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ClientConnectionMultiplexer(org.apache.ignite.internal.client.thin.io.ClientConnectionMultiplexer) Consumer(java.util.function.Consumer) List(java.util.List) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) ForkJoinPool(java.util.concurrent.ForkJoinPool) ClientOperationType(org.apache.ignite.client.ClientOperationType) ClientRetryPolicyContext(org.apache.ignite.client.ClientRetryPolicyContext) ClientConnectorConfiguration(org.apache.ignite.configuration.ClientConnectorConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) UUID(java.util.UUID)

Example 2 with IgniteClientFuture

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;
    });
}
Also used : ClientClusterGroup(org.apache.ignite.client.ClientClusterGroup) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) ClientFeatureNotSupportedByServerException(org.apache.ignite.client.ClientFeatureNotSupportedByServerException) U(org.apache.ignite.internal.util.typedef.internal.U) CompletableFuture(java.util.concurrent.CompletableFuture) RESOURCE_CLOSE(org.apache.ignite.internal.client.thin.ClientOperation.RESOURCE_CLOSE) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) Future(java.util.concurrent.Future) ClientException(org.apache.ignite.client.ClientException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) COMPUTE_TASK_FINISHED(org.apache.ignite.internal.client.thin.ClientNotificationType.COMPUTE_TASK_FINISHED) ClientStatus(org.apache.ignite.internal.processors.platform.client.ClientStatus) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) F(org.apache.ignite.internal.util.typedef.F) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteClientFuture(org.apache.ignite.client.IgniteClientFuture) UUID(java.util.UUID) COMPUTE_TASK_EXECUTE(org.apache.ignite.internal.client.thin.ClientOperation.COMPUTE_TASK_EXECUTE) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) BinaryByteBufferInputStream(org.apache.ignite.internal.binary.streams.BinaryByteBufferInputStream) ClientCompute(org.apache.ignite.client.ClientCompute) AtomicReference(java.util.concurrent.atomic.AtomicReference) CompletableFuture(java.util.concurrent.CompletableFuture) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) ClientException(org.apache.ignite.client.ClientException) UUID(java.util.UUID)

Aggregations

Collection (java.util.Collection)2 UUID (java.util.UUID)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Consumer (java.util.function.Consumer)2 Function (java.util.function.Function)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 ClientConnectionException (org.apache.ignite.client.ClientConnectionException)2 ClientException (org.apache.ignite.client.ClientException)2 IgniteClientFuture (org.apache.ignite.client.IgniteClientFuture)2 F (org.apache.ignite.internal.util.typedef.F)2 U (org.apache.ignite.internal.util.typedef.internal.U)2 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1