Search in sources :

Example 51 with InternalCompletableFuture

use of com.hazelcast.spi.impl.InternalCompletableFuture in project hazelcast by hazelcast.

the class XAResourceImpl method recover.

@Override
public Xid[] recover(int flag) throws XAException {
    NodeEngine nodeEngine = getNodeEngine();
    XAService xaService = getService();
    OperationService operationService = nodeEngine.getOperationService();
    ClusterService clusterService = nodeEngine.getClusterService();
    Collection<Member> memberList = clusterService.getMembers();
    List<Future<SerializableList>> futureList = new ArrayList<Future<SerializableList>>();
    for (Member member : memberList) {
        if (member.localMember()) {
            continue;
        }
        CollectRemoteTransactionsOperation op = new CollectRemoteTransactionsOperation();
        Address address = member.getAddress();
        InternalCompletableFuture<SerializableList> future = operationService.invokeOnTarget(SERVICE_NAME, op, address);
        futureList.add(future);
    }
    Set<SerializableXID> xids = new HashSet<SerializableXID>(xaService.getPreparedXids());
    for (Future<SerializableList> future : futureList) {
        try {
            SerializableList xidSet = future.get();
            for (Data xidData : xidSet) {
                SerializableXID xid = nodeEngine.toObject(xidData);
                xids.add(xid);
            }
        } catch (InterruptedException e) {
            currentThread().interrupt();
            throw new XAException(XAException.XAER_RMERR);
        } catch (MemberLeftException e) {
            logger.warning("Member left while recovering", e);
        } catch (ExecutionException e) {
            Throwable cause = e.getCause();
            if (cause instanceof HazelcastInstanceNotActiveException || cause instanceof TargetNotMemberException) {
                logger.warning("Member left while recovering", e);
            } else {
                throw new XAException(XAException.XAER_RMERR);
            }
        }
    }
    return xids.toArray(new SerializableXID[0]);
}
Also used : Address(com.hazelcast.cluster.Address) SerializableList(com.hazelcast.spi.impl.SerializableList) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) NodeEngine(com.hazelcast.spi.impl.NodeEngine) CollectRemoteTransactionsOperation(com.hazelcast.transaction.impl.xa.operations.CollectRemoteTransactionsOperation) ExecutionException(java.util.concurrent.ExecutionException) Member(com.hazelcast.cluster.Member) HashSet(java.util.HashSet) MemberLeftException(com.hazelcast.core.MemberLeftException) XAException(javax.transaction.xa.XAException) Data(com.hazelcast.internal.serialization.Data) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) TargetNotMemberException(com.hazelcast.spi.exception.TargetNotMemberException) ClusterService(com.hazelcast.internal.cluster.ClusterService) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) Future(java.util.concurrent.Future) OperationService(com.hazelcast.spi.impl.operationservice.OperationService)

Example 52 with InternalCompletableFuture

use of com.hazelcast.spi.impl.InternalCompletableFuture in project hazelcast by hazelcast.

the class MapKeyLoader method sendBatch.

/**
 * Sends the key batches to the partition owners for value
 * loading. The returned futures represent pending offloading
 * of the value loading on the partition owner. This means
 * that once the partition owner receives the keys, it will
 * offload the value loading task and return immediately,
 * thus completing the future. The future does not mean
 * the value loading tasks have been completed or that the
 * entries have been loaded and put into the record store.
 *
 * @param batch                    a map from partition ID
 *                                 to a batch of keys for that partition
 * @param replaceExistingValues    if the existing
 *                                 entries for the loaded keys should be replaced
 * @param nodeWideLoadedKeyLimiter controls number of loaded keys
 * @return a list of futures representing pending
 * completion of the value offloading task
 */
private List<Future> sendBatch(Map<Integer, List<Data>> batch, boolean replaceExistingValues, Semaphore nodeWideLoadedKeyLimiter) {
    Set<Entry<Integer, List<Data>>> entries = batch.entrySet();
    List<Future> futures = new ArrayList<>(entries.size());
    Iterator<Entry<Integer, List<Data>>> iterator = entries.iterator();
    while (iterator.hasNext()) {
        Entry<Integer, List<Data>> e = iterator.next();
        int partitionId = e.getKey();
        List<Data> keys = e.getValue();
        int numberOfLoadedKeys = keys.size();
        try {
            MapOperation op = operationProvider.createLoadAllOperation(mapName, keys, replaceExistingValues);
            InternalCompletableFuture<Object> future = opService.invokeOnPartition(SERVICE_NAME, op, partitionId);
            futures.add(future);
        } finally {
            nodeWideLoadedKeyLimiter.release(numberOfLoadedKeys);
        }
        iterator.remove();
    }
    return futures;
}
Also used : ArrayList(java.util.ArrayList) Data(com.hazelcast.internal.serialization.Data) MapOperation(com.hazelcast.map.impl.operation.MapOperation) Entry(java.util.Map.Entry) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) Future(java.util.concurrent.Future) List(java.util.List) ArrayList(java.util.ArrayList)

Example 53 with InternalCompletableFuture

use of com.hazelcast.spi.impl.InternalCompletableFuture in project hazelcast by hazelcast.

the class MapProxySupport method invokePutAllOperation.

@Nonnull
private InternalCompletableFuture<Void> invokePutAllOperation(Address address, List<Integer> memberPartitions, MapEntries[] entriesPerPartition, boolean triggerMapLoader) {
    int size = memberPartitions.size();
    int[] partitions = new int[size];
    int index = 0;
    for (Integer partitionId : memberPartitions) {
        if (entriesPerPartition[partitionId] != null) {
            partitions[index++] = partitionId;
        }
    }
    if (index == 0) {
        return newCompletedFuture(null);
    }
    // trim partition array to real size
    if (index < size) {
        partitions = Arrays.copyOf(partitions, index);
        size = index;
    }
    index = 0;
    MapEntries[] entries = new MapEntries[size];
    long totalSize = 0;
    for (int partitionId : partitions) {
        int batchSize = entriesPerPartition[partitionId].size();
        assert (putAllBatchSize == 0 || batchSize <= putAllBatchSize);
        entries[index++] = entriesPerPartition[partitionId];
        totalSize += batchSize;
        entriesPerPartition[partitionId] = null;
    }
    if (totalSize == 0) {
        return newCompletedFuture(null);
    }
    OperationFactory factory = operationProvider.createPutAllOperationFactory(name, partitions, entries, triggerMapLoader);
    long startTimeNanos = Timer.nanos();
    CompletableFuture<Map<Integer, Object>> future = operationService.invokeOnPartitionsAsync(SERVICE_NAME, factory, singletonMap(address, asIntegerList(partitions)));
    InternalCompletableFuture<Void> resultFuture = new InternalCompletableFuture<>();
    long finalTotalSize = totalSize;
    future.whenCompleteAsync((response, t) -> {
        putAllVisitSerializedKeys(entries);
        if (t == null) {
            localMapStats.incrementPutLatencyNanos(finalTotalSize, Timer.nanosElapsed(startTimeNanos));
            resultFuture.complete(null);
        } else {
            resultFuture.completeExceptionally(t);
        }
    }, CALLER_RUNS);
    return resultFuture;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MapEntries(com.hazelcast.map.impl.MapEntries) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) Map(java.util.Map) HashMap(java.util.HashMap) MapUtil.createHashMap(com.hazelcast.internal.util.MapUtil.createHashMap) Collections.singletonMap(java.util.Collections.singletonMap) IMap(com.hazelcast.map.IMap) BinaryOperationFactory(com.hazelcast.spi.impl.operationservice.BinaryOperationFactory) IsPartitionLoadedOperationFactory(com.hazelcast.map.impl.operation.IsPartitionLoadedOperationFactory) OperationFactory(com.hazelcast.spi.impl.operationservice.OperationFactory) IsEmptyOperationFactory(com.hazelcast.map.impl.operation.IsEmptyOperationFactory) Nonnull(javax.annotation.Nonnull)

Example 54 with InternalCompletableFuture

use of com.hazelcast.spi.impl.InternalCompletableFuture in project hazelcast by hazelcast.

the class TotalOrderedTopicProxy method publish.

@Override
public void publish(@Nonnull E message) {
    checkNotNull(message, NULL_MESSAGE_IS_NOT_ALLOWED);
    Operation operation = new PublishOperation(getName(), toData(message)).setPartitionId(partitionId);
    InternalCompletableFuture f = invokeOnPartition(operation);
    f.joinInternal();
}
Also used : InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) Operation(com.hazelcast.spi.impl.operationservice.Operation)

Example 55 with InternalCompletableFuture

use of com.hazelcast.spi.impl.InternalCompletableFuture in project hazelcast by hazelcast.

the class ClientExecutorServiceProxy method submitToKeyOwnerInternal.

private <T> Future<T> submitToKeyOwnerInternal(@Nonnull Data task, @Nonnull Object key, @Nullable ExecutionCallback<T> callback) {
    checkNotNull(task, "task should not be null");
    checkNotNull(key, "key should not be null");
    UUID uuid = getUUID();
    int partitionId = getPartitionId(key);
    ClientMessage request = ExecutorServiceSubmitToPartitionCodec.encodeRequest(name, uuid, task);
    ClientInvocationFuture f = invokeOnPartitionOwner(request, partitionId);
    InternalCompletableFuture<T> delegatingFuture = (InternalCompletableFuture<T>) delegatingFuture(f, uuid, partitionId, (T) null);
    if (callback != null) {
        delegatingFuture.whenCompleteAsync(new ExecutionCallbackAdapter<>(callback)).whenCompleteAsync((v, t) -> {
            if (t instanceof RejectedExecutionException) {
                callback.onFailure(t);
            }
        }, ConcurrencyUtil.getDefaultAsyncExecutor());
    }
    return delegatingFuture;
}
Also used : ExecutionCallbackAdapter(com.hazelcast.executor.impl.ExecutionCallbackAdapter) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) UUID(java.util.UUID) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ClientInvocationFuture(com.hazelcast.client.impl.spi.impl.ClientInvocationFuture)

Aggregations

InternalCompletableFuture (com.hazelcast.spi.impl.InternalCompletableFuture)90 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)47 QuickTest (com.hazelcast.test.annotation.QuickTest)47 Test (org.junit.Test)47 OperationService (com.hazelcast.spi.impl.operationservice.OperationService)19 HazelcastInstance (com.hazelcast.core.HazelcastInstance)17 Accessors.getOperationService (com.hazelcast.test.Accessors.getOperationService)15 Data (com.hazelcast.internal.serialization.Data)10 ArrayList (java.util.ArrayList)10 Map (java.util.Map)10 Operation (com.hazelcast.spi.impl.operationservice.Operation)9 UUID (java.util.UUID)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Member (com.hazelcast.cluster.Member)7 ApplyRaftRunnable (com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable)7 Future (java.util.concurrent.Future)7 Address (com.hazelcast.cluster.Address)6 List (java.util.List)6 BiConsumer (java.util.function.BiConsumer)6 Nonnull (javax.annotation.Nonnull)6