use of java.util.function.BiConsumer 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 java.util.function.BiConsumer 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 java.util.function.BiConsumer in project hazelcast by hazelcast.
the class RaftService method removeCPMember.
public InternalCompletableFuture<Void> removeCPMember(UUID cpMemberUuid) {
ClusterService clusterService = nodeEngine.getClusterService();
InternalCompletableFuture<Void> future = newCompletableFuture();
BiConsumer<Void, Throwable> removeMemberCallback = (response, t) -> {
if (t == null) {
future.complete(null);
} else {
if (t instanceof CannotRemoveCPMemberException) {
t = new IllegalStateException(t.getMessage());
}
complete(future, t);
}
};
invocationManager.<Collection<CPMember>>invoke(getMetadataGroupId(), new GetActiveCPMembersOp()).whenCompleteAsync((cpMembers, t) -> {
if (t == null) {
CPMemberInfo cpMemberToRemove = null;
for (CPMember cpMember : cpMembers) {
if (cpMember.getUuid().equals(cpMemberUuid)) {
cpMemberToRemove = (CPMemberInfo) cpMember;
break;
}
}
if (cpMemberToRemove == null) {
complete(future, new IllegalArgumentException("No CPMember found with uuid: " + cpMemberUuid));
return;
} else {
Member member = clusterService.getMember(cpMemberToRemove.getAddress());
if (member != null) {
logger.warning("Only unreachable/crashed CP members should be removed. " + member + " is alive but " + cpMemberToRemove + " with the same address is being removed.");
}
}
invokeTriggerRemoveMember(cpMemberToRemove).whenCompleteAsync(removeMemberCallback);
} else {
complete(future, t);
}
});
return future;
}
use of java.util.function.BiConsumer in project hazelcast by hazelcast.
the class RaftService method resetCPSubsystem.
InternalCompletableFuture<Void> resetCPSubsystem() {
checkState(cpSubsystemEnabled, "CP Subsystem is not enabled!");
InternalCompletableFuture<Void> future = newCompletableFuture();
ClusterService clusterService = nodeEngine.getClusterService();
Collection<Member> members = clusterService.getMembers(NON_LOCAL_MEMBER_SELECTOR);
if (!clusterService.isMaster()) {
return complete(future, new IllegalStateException("Only master can reset CP Subsystem!"));
}
if (config.getCPMemberCount() > members.size() + 1) {
return complete(future, new IllegalStateException("Not enough cluster members to reset CP Subsystem! " + "Required: " + config.getCPMemberCount() + ", available: " + (members.size() + 1)));
}
BiConsumer<Void, Throwable> callback = new BiConsumer<Void, Throwable>() {
final AtomicInteger latch = new AtomicInteger(members.size());
volatile Throwable failure;
@Override
public void accept(Void aVoid, Throwable throwable) {
if (throwable == null) {
if (latch.decrementAndGet() == 0) {
if (failure == null) {
future.complete(null);
} else {
complete(future, failure);
}
}
} else {
failure = throwable;
if (latch.decrementAndGet() == 0) {
complete(future, throwable);
}
}
}
};
long seed = newSeed();
logger.warning("Resetting CP Subsystem with groupId seed: " + seed);
resetLocal(seed);
OperationServiceImpl operationService = nodeEngine.getOperationService();
for (Member member : members) {
Operation op = new ResetCPMemberOp(seed);
operationService.<Void>invokeOnTarget(SERVICE_NAME, op, member.getAddress()).whenCompleteAsync(callback);
}
return future;
}
use of java.util.function.BiConsumer in project hazelcast by hazelcast.
the class SplitBrainTest method when_jobIsSubmittedToMinoritySide_then_jobIsCancelledDuringMerge.
@Test
public void when_jobIsSubmittedToMinoritySide_then_jobIsCancelledDuringMerge() {
int firstSubClusterSize = 3;
int secondSubClusterSize = 2;
NoOutputSourceP.executionStarted = new CountDownLatch(secondSubClusterSize * PARALLELISM);
Job[] jobRef = new Job[1];
BiConsumer<HazelcastInstance[], HazelcastInstance[]> onSplit = (firstSubCluster, secondSubCluster) -> {
MockPS processorSupplier = new MockPS(NoOutputSourceP::new, secondSubClusterSize);
DAG dag = new DAG().vertex(new Vertex("test", processorSupplier));
jobRef[0] = secondSubCluster[0].getJet().newJob(dag, new JobConfig().setSplitBrainProtection(true));
assertOpenEventually(NoOutputSourceP.executionStarted);
};
Consumer<HazelcastInstance[]> afterMerge = instances -> {
assertTrueEventually(() -> assertEquals(secondSubClusterSize, MockPS.receivedCloseErrors.size()), 20);
MockPS.receivedCloseErrors.forEach(t -> assertTrue("received: " + t, t instanceof CancellationException));
try {
jobRef[0].getFuture().get(30, TimeUnit.SECONDS);
fail();
} catch (CancellationException ignored) {
} catch (Exception e) {
throw new AssertionError(e);
}
};
testSplitBrain(firstSubClusterSize, secondSubClusterSize, null, onSplit, afterMerge);
}
Aggregations