Search in sources :

Example 6 with InternalCompletableFuture

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

the class MapProxySupport method submitToKeysInternal.

public <R> InternalCompletableFuture<Map<K, R>> submitToKeysInternal(Set<K> keys, Set<Data> dataKeys, EntryProcessor<K, V, R> entryProcessor) {
    if (dataKeys.isEmpty()) {
        toDataCollectionWithNonNullKeyValidation(keys, dataKeys);
    }
    Collection<Integer> partitionsForKeys = getPartitionsForKeys(dataKeys);
    OperationFactory operationFactory = operationProvider.createMultipleEntryOperationFactory(name, dataKeys, entryProcessor);
    final InternalCompletableFuture resultFuture = new InternalCompletableFuture();
    operationService.invokeOnPartitionsAsync(SERVICE_NAME, operationFactory, partitionsForKeys).whenCompleteAsync((response, throwable) -> {
        if (throwable == null) {
            Map<K, Object> result = null;
            try {
                result = createHashMap(response.size());
                for (Object object : response.values()) {
                    MapEntries mapEntries = (MapEntries) object;
                    mapEntries.putAllToMap(serializationService, result);
                }
            } catch (Throwable e) {
                resultFuture.completeExceptionally(e);
            }
            resultFuture.complete(result);
        } else {
            resultFuture.completeExceptionally(throwable);
        }
    });
    return resultFuture;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) MapEntries(com.hazelcast.map.impl.MapEntries) AbstractDistributedObject(com.hazelcast.spi.impl.AbstractDistributedObject) InitializingObject(com.hazelcast.spi.impl.InitializingObject) 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)

Example 7 with InternalCompletableFuture

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

the class MultiMapProxySupport method putAllInternal.

// NB: this method is generally copied from MapProxySupport#putAllInternal
@SuppressWarnings({ "checkstyle:npathcomplexity", "checkstyle:methodlength" })
protected void putAllInternal(Map<Data, Data> map, @Nullable InternalCompletableFuture<Void> future) {
    // get partition to entries mapping
    try {
        int mapSize = map.size();
        if (mapSize == 0) {
            if (future != null) {
                future.complete(null);
            }
            return;
        }
        int partitionCount = partitionService.getPartitionCount();
        int initialSize = getPutAllInitialSize(mapSize, partitionCount);
        // get node to partition mapping
        Map<Address, List<Integer>> memberPartitionsMap = partitionService.getMemberPartitionsMap();
        // fill entriesPerPartition
        MapEntries[] entriesPerPartition = new MapEntries[partitionCount];
        for (Map.Entry<Data, Data> entry : map.entrySet()) {
            checkNotNull(entry.getKey(), NULL_KEY_IS_NOT_ALLOWED);
            checkNotNull(entry.getValue(), NULL_VALUE_IS_NOT_ALLOWED);
            Data keyData = entry.getKey();
            int partitionId = partitionService.getPartitionId(keyData);
            MapEntries entries = entriesPerPartition[partitionId];
            if (entries == null) {
                entries = new MapEntries(initialSize);
                entriesPerPartition[partitionId] = entries;
            }
            entries.add(keyData, entry.getValue());
        }
        // invoke operations for entriesPerPartition
        AtomicInteger counter = new AtomicInteger(memberPartitionsMap.size());
        InternalCompletableFuture<Void> resultFuture = future != null ? future : new InternalCompletableFuture<>();
        BiConsumer<Void, Throwable> callback = (response, t) -> {
            if (t != null) {
                resultFuture.completeExceptionally(t);
            }
            if (counter.decrementAndGet() == 0) {
                if (!resultFuture.isDone()) {
                    resultFuture.complete(null);
                }
            }
        };
        for (Map.Entry<Address, List<Integer>> entry : memberPartitionsMap.entrySet()) {
            invokePutAllOperation(entry.getKey(), entry.getValue(), entriesPerPartition).whenCompleteAsync(callback);
        }
        // if executing in sync mode, block for the responses
        if (future == null) {
            resultFuture.get();
        }
    } catch (Throwable e) {
        throw rethrow(e);
    }
}
Also used : Address(com.hazelcast.cluster.Address) Arrays(java.util.Arrays) CompletableFuture(java.util.concurrent.CompletableFuture) ExceptionUtil.rethrow(com.hazelcast.internal.util.ExceptionUtil.rethrow) AbstractDistributedObject(com.hazelcast.spi.impl.AbstractDistributedObject) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) MultiMapConfig(com.hazelcast.config.MultiMapConfig) NULL_VALUE_IS_NOT_ALLOWED(com.hazelcast.multimap.impl.MultiMapProxyImpl.NULL_VALUE_IS_NOT_ALLOWED) MapEntries(com.hazelcast.map.impl.MapEntries) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PutOperation(com.hazelcast.multimap.impl.operations.PutOperation) Operation(com.hazelcast.spi.impl.operationservice.Operation) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) GetAllOperation(com.hazelcast.multimap.impl.operations.GetAllOperation) MultiMapOperationFactory(com.hazelcast.multimap.impl.operations.MultiMapOperationFactory) Collections.singletonMap(java.util.Collections.singletonMap) MapUtil.toIntSize(com.hazelcast.internal.util.MapUtil.toIntSize) MultiMapResponse(com.hazelcast.multimap.impl.operations.MultiMapResponse) OperationFactoryType(com.hazelcast.multimap.impl.operations.MultiMapOperationFactory.OperationFactoryType) LockSupportServiceImpl(com.hazelcast.internal.locksupport.LockSupportServiceImpl) Nullable(javax.annotation.Nullable) Timer(com.hazelcast.internal.util.Timer) CALLER_RUNS(com.hazelcast.internal.util.ConcurrencyUtil.CALLER_RUNS) RemoveOperation(com.hazelcast.multimap.impl.operations.RemoveOperation) InternalCompletableFuture.newCompletedFuture(com.hazelcast.spi.impl.InternalCompletableFuture.newCompletedFuture) NodeEngine(com.hazelcast.spi.impl.NodeEngine) EntryEventType(com.hazelcast.core.EntryEventType) IPartitionService(com.hazelcast.internal.partition.IPartitionService) DistributedObjectNamespace(com.hazelcast.internal.services.DistributedObjectNamespace) CollectionUtil.asIntegerList(com.hazelcast.internal.util.CollectionUtil.asIntegerList) Data(com.hazelcast.internal.serialization.Data) ExceptionUtil(com.hazelcast.internal.util.ExceptionUtil) Set(java.util.Set) LocalMultiMapStatsImpl(com.hazelcast.internal.monitor.impl.LocalMultiMapStatsImpl) Preconditions.checkNotNull(com.hazelcast.internal.util.Preconditions.checkNotNull) DeleteOperation(com.hazelcast.multimap.impl.operations.DeleteOperation) MultiMapPutAllOperationFactory(com.hazelcast.multimap.impl.operations.MultiMapPutAllOperationFactory) RemoveAllOperation(com.hazelcast.multimap.impl.operations.RemoveAllOperation) List(java.util.List) LockProxySupport(com.hazelcast.internal.locksupport.LockProxySupport) OperationFactory(com.hazelcast.spi.impl.operationservice.OperationFactory) OperationService(com.hazelcast.spi.impl.operationservice.OperationService) NULL_KEY_IS_NOT_ALLOWED(com.hazelcast.multimap.impl.MultiMapProxyImpl.NULL_KEY_IS_NOT_ALLOWED) CountOperation(com.hazelcast.multimap.impl.operations.CountOperation) ThreadUtil(com.hazelcast.internal.util.ThreadUtil) Address(com.hazelcast.cluster.Address) Data(com.hazelcast.internal.serialization.Data) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MapEntries(com.hazelcast.map.impl.MapEntries) CollectionUtil.asIntegerList(com.hazelcast.internal.util.CollectionUtil.asIntegerList) List(java.util.List) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap)

Example 8 with InternalCompletableFuture

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

the class MultiMapProxySupport method invokePutAllOperation.

// NB: this method is generally copied from MapProxySupport#invokePutAllOperation
private InternalCompletableFuture<Void> invokePutAllOperation(Address address, List<Integer> memberPartitions, MapEntries[] entriesPerPartition) {
    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) {
        totalSize += entriesPerPartition[partitionId].size();
        entries[index++] = entriesPerPartition[partitionId];
        entriesPerPartition[partitionId] = null;
    }
    if (totalSize == 0) {
        return newCompletedFuture(null);
    }
    OperationFactory factory = new MultiMapPutAllOperationFactory(name, partitions, entries);
    long startTimeNanos = System.nanoTime();
    CompletableFuture<Map<Integer, Object>> future = operationService.invokeOnPartitionsAsync(MultiMapService.SERVICE_NAME, factory, singletonMap(address, asIntegerList(partitions)));
    InternalCompletableFuture<Void> resultFuture = new InternalCompletableFuture<>();
    long finalTotalSize = totalSize;
    future.whenCompleteAsync((response, t) -> {
        if (t == null) {
            getService().getLocalMultiMapStatsImpl(name).incrementPutLatencyNanos(finalTotalSize, System.nanoTime() - startTimeNanos);
            resultFuture.complete(null);
        } else {
            resultFuture.completeExceptionally(t);
        }
    }, CALLER_RUNS);
    return resultFuture;
}
Also used : InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MultiMapPutAllOperationFactory(com.hazelcast.multimap.impl.operations.MultiMapPutAllOperationFactory) MapEntries(com.hazelcast.map.impl.MapEntries) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) MultiMapOperationFactory(com.hazelcast.multimap.impl.operations.MultiMapOperationFactory) MultiMapPutAllOperationFactory(com.hazelcast.multimap.impl.operations.MultiMapPutAllOperationFactory) OperationFactory(com.hazelcast.spi.impl.operationservice.OperationFactory)

Example 9 with InternalCompletableFuture

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

the class AbstractCacheService method createCacheConfigOnAllMembers.

@Override
public <K, V> void createCacheConfigOnAllMembers(PreJoinCacheConfig<K, V> cacheConfig) {
    InternalCompletableFuture future = createCacheConfigOnAllMembersAsync(cacheConfig);
    FutureUtil.waitForever(singleton(future), RETHROW_EVERYTHING);
}
Also used : InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture)

Example 10 with InternalCompletableFuture

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

the class ClientProxySessionManager method shutdownAndAwait.

public void shutdownAndAwait() {
    Map<RaftGroupId, InternalCompletableFuture<Object>> futures = super.shutdown();
    ILogger logger = client.getLoggingService().getLogger(getClass());
    long remainingTimeNanos = TimeUnit.SECONDS.toNanos(SHUTDOWN_TIMEOUT_SECONDS);
    while (remainingTimeNanos > 0) {
        int closed = 0;
        for (Entry<RaftGroupId, InternalCompletableFuture<Object>> entry : futures.entrySet()) {
            CPGroupId groupId = entry.getKey();
            InternalCompletableFuture<Object> f = entry.getValue();
            if (f.isDone()) {
                closed++;
                try {
                    f.get();
                    logger.fine("Session closed for " + groupId);
                } catch (Exception e) {
                    logger.warning("Close session failed for " + groupId, e);
                }
            }
        }
        if (closed == futures.size()) {
            break;
        }
        try {
            Thread.sleep(SHUTDOWN_WAIT_SLEEP_MILLIS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return;
        }
        remainingTimeNanos -= MILLISECONDS.toNanos(SHUTDOWN_WAIT_SLEEP_MILLIS);
    }
}
Also used : CPGroupId(com.hazelcast.cp.CPGroupId) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) ILogger(com.hazelcast.logging.ILogger)

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