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