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;
}
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);
}
}
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;
}
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);
}
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);
}
}
Aggregations