Search in sources :

Example 6 with CompletedFuture

use of com.hazelcast.util.executor.CompletedFuture in project hazelcast by hazelcast.

the class DurableExecutorServiceProxy method submitToPartition.

private <T> DurableExecutorServiceFuture<T> submitToPartition(Callable<T> task, int partitionId, T defaultValue) {
    checkNotNull(task, "task can't be null");
    SerializationService serializationService = getNodeEngine().getSerializationService();
    Data taskData = serializationService.toData(task);
    TaskOperation operation = new TaskOperation(name, taskData);
    operation.setPartitionId(partitionId);
    InternalCompletableFuture<Integer> future = invokeOnPartition(operation);
    int sequence;
    try {
        sequence = future.get();
    } catch (Throwable t) {
        CompletedFuture<T> completedFuture = new CompletedFuture<T>(serializationService, t, getAsyncExecutor());
        return new DurableExecutorServiceDelegateFuture<T>(completedFuture, serializationService, null, -1);
    }
    Operation op = new RetrieveResultOperation(name, sequence).setPartitionId(partitionId);
    InternalCompletableFuture<T> internalCompletableFuture = invokeOnPartition(op);
    long taskId = Bits.combineToLong(partitionId, sequence);
    return new DurableExecutorServiceDelegateFuture<T>(internalCompletableFuture, serializationService, defaultValue, taskId);
}
Also used : SerializationService(com.hazelcast.spi.serialization.SerializationService) Data(com.hazelcast.nio.serialization.Data) TaskOperation(com.hazelcast.durableexecutor.impl.operations.TaskOperation) ShutdownOperation(com.hazelcast.durableexecutor.impl.operations.ShutdownOperation) TaskOperation(com.hazelcast.durableexecutor.impl.operations.TaskOperation) Operation(com.hazelcast.spi.Operation) DisposeResultOperation(com.hazelcast.durableexecutor.impl.operations.DisposeResultOperation) RetrieveAndDisposeResultOperation(com.hazelcast.durableexecutor.impl.operations.RetrieveAndDisposeResultOperation) RetrieveResultOperation(com.hazelcast.durableexecutor.impl.operations.RetrieveResultOperation) RetrieveResultOperation(com.hazelcast.durableexecutor.impl.operations.RetrieveResultOperation) CompletedFuture(com.hazelcast.util.executor.CompletedFuture)

Example 7 with CompletedFuture

use of com.hazelcast.util.executor.CompletedFuture in project hazelcast by hazelcast.

the class ExecutorServiceProxy method submitToMember.

@Override
public <T> Future<T> submitToMember(Callable<T> task, Member member) {
    checkNotNull(task, "task can't be null");
    checkNotShutdown();
    NodeEngine nodeEngine = getNodeEngine();
    Data taskData = nodeEngine.toData(task);
    String uuid = newUnsecureUuidString();
    Address target = ((MemberImpl) member).getAddress();
    boolean sync = checkSync();
    MemberCallableTaskOperation op = new MemberCallableTaskOperation(name, uuid, taskData);
    InternalCompletableFuture future = nodeEngine.getOperationService().invokeOnTarget(DistributedExecutorService.SERVICE_NAME, op, target);
    if (sync) {
        Object response;
        try {
            response = future.get();
        } catch (Exception e) {
            response = e;
        }
        return new CompletedFuture<T>(nodeEngine.getSerializationService(), response, getAsyncExecutor());
    }
    return new CancellableDelegatingFuture<T>(future, nodeEngine, uuid, target);
}
Also used : NodeEngine(com.hazelcast.spi.NodeEngine) Address(com.hazelcast.nio.Address) MemberImpl(com.hazelcast.instance.MemberImpl) InternalCompletableFuture(com.hazelcast.spi.InternalCompletableFuture) Data(com.hazelcast.nio.serialization.Data) AbstractDistributedObject(com.hazelcast.spi.AbstractDistributedObject) UuidUtil.newUnsecureUuidString(com.hazelcast.util.UuidUtil.newUnsecureUuidString) MemberCallableTaskOperation(com.hazelcast.executor.impl.operations.MemberCallableTaskOperation) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) TimeoutException(java.util.concurrent.TimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) CompletedFuture(com.hazelcast.util.executor.CompletedFuture)

Example 8 with CompletedFuture

use of com.hazelcast.util.executor.CompletedFuture in project hazelcast by hazelcast.

the class ExecutorServiceProxy method wait.

private <T> boolean wait(long timeoutNanos, List<Future<T>> futures) throws InterruptedException {
    boolean done = true;
    for (int i = 0, size = futures.size(); i < size; i++) {
        long start = System.nanoTime();
        Object value;
        try {
            Future<T> future = futures.get(i);
            value = future.get(timeoutNanos, TimeUnit.NANOSECONDS);
        } catch (ExecutionException e) {
            value = e;
        } catch (TimeoutException e) {
            done = false;
            for (int l = i; l < size; l++) {
                Future<T> f = futures.get(i);
                if (f.isDone()) {
                    Object v;
                    try {
                        v = f.get();
                    } catch (ExecutionException ex) {
                        v = ex;
                    }
                    futures.set(l, new CompletedFuture<T>(getNodeEngine().getSerializationService(), v, getAsyncExecutor()));
                }
            }
            break;
        }
        futures.set(i, new CompletedFuture<T>(getNodeEngine().getSerializationService(), value, getAsyncExecutor()));
        timeoutNanos -= System.nanoTime() - start;
    }
    return done;
}
Also used : InternalCompletableFuture(com.hazelcast.spi.InternalCompletableFuture) Future(java.util.concurrent.Future) CompletedFuture(com.hazelcast.util.executor.CompletedFuture) AbstractDistributedObject(com.hazelcast.spi.AbstractDistributedObject) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) CompletedFuture(com.hazelcast.util.executor.CompletedFuture)

Example 9 with CompletedFuture

use of com.hazelcast.util.executor.CompletedFuture in project hazelcast by hazelcast.

the class ExecutorServiceProxy method submit.

@Override
public <T> Future<T> submit(Runnable task, T result) {
    checkNotNull(task, "task can't be null");
    checkNotShutdown();
    NodeEngine nodeEngine = getNodeEngine();
    Callable<T> callable = createRunnableAdapter(task);
    Data callableData = nodeEngine.toData(callable);
    String uuid = newUnsecureUuidString();
    int partitionId = getTaskPartitionId(callable);
    Operation op = new CallableTaskOperation(name, uuid, callableData).setPartitionId(partitionId);
    InternalCompletableFuture future = invokeOnPartition(op);
    boolean sync = checkSync();
    if (sync) {
        try {
            future.get();
        } catch (Exception exception) {
            logger.warning(exception);
        }
        return new CompletedFuture<T>(nodeEngine.getSerializationService(), result, getAsyncExecutor());
    }
    return new CancellableDelegatingFuture<T>(future, result, nodeEngine, uuid, partitionId);
}
Also used : InternalCompletableFuture(com.hazelcast.spi.InternalCompletableFuture) Data(com.hazelcast.nio.serialization.Data) UuidUtil.newUnsecureUuidString(com.hazelcast.util.UuidUtil.newUnsecureUuidString) ShutdownOperation(com.hazelcast.executor.impl.operations.ShutdownOperation) CallableTaskOperation(com.hazelcast.executor.impl.operations.CallableTaskOperation) Operation(com.hazelcast.spi.Operation) MemberCallableTaskOperation(com.hazelcast.executor.impl.operations.MemberCallableTaskOperation) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) TimeoutException(java.util.concurrent.TimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) NodeEngine(com.hazelcast.spi.NodeEngine) CallableTaskOperation(com.hazelcast.executor.impl.operations.CallableTaskOperation) MemberCallableTaskOperation(com.hazelcast.executor.impl.operations.MemberCallableTaskOperation) CompletedFuture(com.hazelcast.util.executor.CompletedFuture)

Example 10 with CompletedFuture

use of com.hazelcast.util.executor.CompletedFuture in project hazelcast by hazelcast.

the class ClientExecutorServiceProxy method checkSync.

private <T> Future<T> checkSync(ClientInvocationFuture f, String uuid, Address address, boolean preventSync, T defaultValue) {
    boolean sync = isSyncComputation(preventSync);
    if (sync) {
        Object response = retrieveResultFromMessage(f);
        Executor userExecutor = getContext().getExecutionService().getUserExecutor();
        return new CompletedFuture<T>(getContext().getSerializationService(), response, userExecutor);
    } else {
        return new ClientAddressCancellableDelegatingFuture<T>(f, getContext(), uuid, address, defaultValue, SUBMIT_TO_ADDRESS_DECODER);
    }
}
Also used : Executor(java.util.concurrent.Executor) ClientAddressCancellableDelegatingFuture(com.hazelcast.client.util.ClientAddressCancellableDelegatingFuture) CompletedFuture(com.hazelcast.util.executor.CompletedFuture)

Aggregations

CompletedFuture (com.hazelcast.util.executor.CompletedFuture)10 InternalCompletableFuture (com.hazelcast.spi.InternalCompletableFuture)5 ExecutionException (java.util.concurrent.ExecutionException)5 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)5 Data (com.hazelcast.nio.serialization.Data)4 AbstractDistributedObject (com.hazelcast.spi.AbstractDistributedObject)4 Future (java.util.concurrent.Future)4 TimeoutException (java.util.concurrent.TimeoutException)4 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)3 MemberCallableTaskOperation (com.hazelcast.executor.impl.operations.MemberCallableTaskOperation)3 NodeEngine (com.hazelcast.spi.NodeEngine)3 Operation (com.hazelcast.spi.Operation)3 UuidUtil.newUnsecureUuidString (com.hazelcast.util.UuidUtil.newUnsecureUuidString)3 Executor (java.util.concurrent.Executor)3 ClientAddressCancellableDelegatingFuture (com.hazelcast.client.util.ClientAddressCancellableDelegatingFuture)2 ClientPartitionCancellableDelegatingFuture (com.hazelcast.client.util.ClientPartitionCancellableDelegatingFuture)2 CallableTaskOperation (com.hazelcast.executor.impl.operations.CallableTaskOperation)2 ShutdownOperation (com.hazelcast.executor.impl.operations.ShutdownOperation)2 ArrayList (java.util.ArrayList)2 ClientInvocationFuture (com.hazelcast.client.spi.impl.ClientInvocationFuture)1