use of com.hazelcast.spi.impl.InternalCompletableFuture in project hazelcast by hazelcast.
the class RaftNodeImpl method toFollower.
/**
* Switches this node to follower role by clearing the known leader
* endpoint and (pre) candidate states, and updating the term. If this Raft
* node was leader before switching to the follower state, it may have some
* queries waiting to be executed. Those queries are also failed with
* {@link LeaderDemotedException}. After the state switch,
* {@link RaftIntegration#onNodeStatusChange(RaftNodeStatus)} is called.
*
* @param term the new term to switch
*/
public void toFollower(int term) {
LeaderState leaderState = state.leaderState();
if (leaderState != null) {
for (BiTuple<Object, InternalCompletableFuture> t : leaderState.queryState().operations()) {
t.element2.completeExceptionally(new LeaderDemotedException(state.localEndpoint(), null));
}
}
state.toFollower(term);
printMemberState();
}
use of com.hazelcast.spi.impl.InternalCompletableFuture in project hazelcast by hazelcast.
the class RaftNodeImpl method forceSetTerminatedStatus.
@Override
public InternalCompletableFuture forceSetTerminatedStatus() {
InternalCompletableFuture resultFuture = raftIntegration.newCompletableFuture();
if (isTerminatedOrSteppedDown()) {
if (logger.isFineEnabled()) {
logger.fine("Already stepped down or terminated, not setting `TERMINATED` status.");
}
resultFuture.complete(null);
return resultFuture;
}
execute(() -> {
Throwable failure = null;
try {
if (isTerminatedOrSteppedDown()) {
return;
} else if (status == INITIAL) {
setStatus(TERMINATED);
return;
}
invalidateFuturesFrom(state.commitIndex() + 1);
LeaderState leaderState = state.leaderState();
if (leaderState != null) {
for (BiTuple<Object, InternalCompletableFuture> t : leaderState.queryState().operations()) {
t.element2.completeExceptionally(new LeaderDemotedException(state.localEndpoint(), null));
}
}
state.completeLeadershipTransfer(new LeaderDemotedException(state.localEndpoint(), null));
setStatus(TERMINATED);
} catch (Throwable t) {
failure = t;
logger.severe("Failure during force-termination", t);
if (status != TERMINATED && status != STEPPED_DOWN) {
setStatus(TERMINATED);
}
} finally {
if (failure == null) {
resultFuture.complete(null);
} else {
resultFuture.completeExceptionally(failure);
}
}
});
return resultFuture;
}
use of com.hazelcast.spi.impl.InternalCompletableFuture in project hazelcast by hazelcast.
the class ClientMapProxy method putAllInternal.
@SuppressWarnings("checkstyle:npathcomplexity")
private void putAllInternal(@Nonnull Map<? extends K, ? extends V> map, @Nullable InternalCompletableFuture<Void> future, boolean triggerMapLoader) {
if (map.isEmpty()) {
if (future != null) {
future.complete(null);
}
return;
}
checkNotNull(map, "Null argument map is not allowed");
ClientPartitionService partitionService = getContext().getPartitionService();
int partitionCount = partitionService.getPartitionCount();
Map<Integer, List<Map.Entry<Data, Data>>> entryMap = new HashMap<>(partitionCount);
for (Entry<? extends K, ? extends V> entry : map.entrySet()) {
checkNotNull(entry.getKey(), NULL_KEY_IS_NOT_ALLOWED);
checkNotNull(entry.getValue(), NULL_VALUE_IS_NOT_ALLOWED);
Data keyData = toData(entry.getKey());
int partitionId = partitionService.getPartitionId(keyData);
List<Map.Entry<Data, Data>> partition = entryMap.get(partitionId);
if (partition == null) {
partition = new ArrayList<>();
entryMap.put(partitionId, partition);
}
partition.add(new AbstractMap.SimpleEntry<>(keyData, toData(entry.getValue())));
}
assert entryMap.size() > 0;
AtomicInteger counter = new AtomicInteger(entryMap.size());
InternalCompletableFuture<Void> resultFuture = future != null ? future : new InternalCompletableFuture<>();
BiConsumer<ClientMessage, Throwable> callback = (response, t) -> {
if (t != null) {
resultFuture.completeExceptionally(t);
}
if (counter.decrementAndGet() == 0) {
finalizePutAll(map, entryMap);
if (!resultFuture.isDone()) {
resultFuture.complete(null);
}
}
};
for (Entry<Integer, List<Map.Entry<Data, Data>>> entry : entryMap.entrySet()) {
Integer partitionId = entry.getKey();
// if there is only one entry, consider how we can use MapPutRequest
// without having to get back the return value
ClientMessage request = MapPutAllCodec.encodeRequest(name, entry.getValue(), triggerMapLoader);
new ClientInvocation(getClient(), request, getName(), partitionId).invoke().whenCompleteAsync(callback);
}
// if executing in sync mode, block for the responses
if (future == null) {
try {
resultFuture.get();
} catch (Throwable e) {
throw rethrow(e);
}
}
}
use of com.hazelcast.spi.impl.InternalCompletableFuture in project hazelcast by hazelcast.
the class ClientMultiMapProxy method putAllInternal.
@SuppressWarnings({ "checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity", "checkstyle:methodlength" })
private void putAllInternal(@Nonnull Map<Data, Collection<Data>> map, @Nonnull InternalCompletableFuture<Void> future) {
if (map.isEmpty()) {
future.complete(null);
return;
}
ClientPartitionService partitionService = getContext().getPartitionService();
int partitionCount = partitionService.getPartitionCount();
Map<Integer, Collection<Map.Entry<Data, Collection<Data>>>> entryMap = new HashMap<>(partitionCount);
for (Map.Entry<Data, Collection<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);
Collection<Map.Entry<Data, Collection<Data>>> partition = entryMap.get(partitionId);
if (partition == null) {
partition = new ArrayList<>();
entryMap.put(partitionId, partition);
}
partition.add(new AbstractMap.SimpleEntry<>(keyData, entry.getValue()));
}
assert entryMap.size() > 0;
AtomicInteger counter = new AtomicInteger(entryMap.size());
InternalCompletableFuture<Void> resultFuture = future;
BiConsumer<ClientMessage, Throwable> callback = (response, t) -> {
if (t != null) {
resultFuture.completeExceptionally(t);
}
if (counter.decrementAndGet() == 0) {
if (!resultFuture.isDone()) {
resultFuture.complete(null);
}
}
};
for (Map.Entry<Integer, Collection<Map.Entry<Data, Collection<Data>>>> entry : entryMap.entrySet()) {
Integer partitionId = entry.getKey();
// if there is only one entry, consider how we can use MapPutRequest
// without having to get back the return value
ClientMessage request = MultiMapPutAllCodec.encodeRequest(name, entry.getValue());
new ClientInvocation(getClient(), request, getName(), partitionId).invoke().whenCompleteAsync(callback);
}
}
use of com.hazelcast.spi.impl.InternalCompletableFuture in project hazelcast by hazelcast.
the class ClientExecutorServiceProxy method submitToRandomWithCallbackInternal.
private <T> void submitToRandomWithCallbackInternal(Data task, ExecutionCallback<T> callback) {
checkNotNull(task, "task should not be null");
UUID uuid = getUUID();
int partitionId = randomPartitionId();
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());
}
}
Aggregations