Search in sources :

Example 81 with BiConsumer

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);
        }
    }
}
Also used : MapGetAllCodec(com.hazelcast.client.impl.protocol.codec.MapGetAllCodec) MapSetTtlCodec(com.hazelcast.client.impl.protocol.codec.MapSetTtlCodec) MapForceUnlockCodec(com.hazelcast.client.impl.protocol.codec.MapForceUnlockCodec) PartitionPredicate(com.hazelcast.query.PartitionPredicate) MapRemoveEntryListenerCodec(com.hazelcast.client.impl.protocol.codec.MapRemoveEntryListenerCodec) MapTryLockCodec(com.hazelcast.client.impl.protocol.codec.MapTryLockCodec) ReadResultSetImpl(com.hazelcast.ringbuffer.impl.ReadResultSetImpl) Thread.currentThread(java.lang.Thread.currentThread) ExceptionUtil.rethrow(com.hazelcast.internal.util.ExceptionUtil.rethrow) MapContainsValueCodec(com.hazelcast.client.impl.protocol.codec.MapContainsValueCodec) MapEntrySetCodec(com.hazelcast.client.impl.protocol.codec.MapEntrySetCodec) SerializationUtil(com.hazelcast.internal.serialization.impl.SerializationUtil) ListenerMessageCodec(com.hazelcast.client.impl.spi.impl.ListenerMessageCodec) Future(java.util.concurrent.Future) MapAddEntryListenerWithPredicateCodec(com.hazelcast.client.impl.protocol.codec.MapAddEntryListenerWithPredicateCodec) MapLockCodec(com.hazelcast.client.impl.protocol.codec.MapLockCodec) MapPutTransientWithMaxIdleCodec(com.hazelcast.client.impl.protocol.codec.MapPutTransientWithMaxIdleCodec) SerializationService(com.hazelcast.internal.serialization.SerializationService) Map(java.util.Map) MapSizeCodec(com.hazelcast.client.impl.protocol.codec.MapSizeCodec) MapSetWithMaxIdleCodec(com.hazelcast.client.impl.protocol.codec.MapSetWithMaxIdleCodec) ClientMapQueryPartitionIterable(com.hazelcast.client.map.impl.iterator.ClientMapQueryPartitionIterable) MapValuesWithPagingPredicateCodec(com.hazelcast.client.impl.protocol.codec.MapValuesWithPagingPredicateCodec) MapExecuteOnAllKeysCodec(com.hazelcast.client.impl.protocol.codec.MapExecuteOnAllKeysCodec) MapClearCodec(com.hazelcast.client.impl.protocol.codec.MapClearCodec) ClientPartitionService(com.hazelcast.client.impl.spi.ClientPartitionService) Set(java.util.Set) EntryView(com.hazelcast.core.EntryView) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) MapRemovePartitionLostListenerCodec(com.hazelcast.client.impl.protocol.codec.MapRemovePartitionLostListenerCodec) MapReplaceCodec(com.hazelcast.client.impl.protocol.codec.MapReplaceCodec) MapRemoveIfSameCodec(com.hazelcast.client.impl.protocol.codec.MapRemoveIfSameCodec) MapKeySetCodec(com.hazelcast.client.impl.protocol.codec.MapKeySetCodec) UnmodifiableLazyList(com.hazelcast.spi.impl.UnmodifiableLazyList) MapPutIfAbsentCodec(com.hazelcast.client.impl.protocol.codec.MapPutIfAbsentCodec) MapRemoveCodec(com.hazelcast.client.impl.protocol.codec.MapRemoveCodec) Preconditions.checkNotInstanceOf(com.hazelcast.internal.util.Preconditions.checkNotInstanceOf) Preconditions.checkPositive(com.hazelcast.internal.util.Preconditions.checkPositive) IterationType(com.hazelcast.internal.util.IterationType) MapAggregateCodec(com.hazelcast.client.impl.protocol.codec.MapAggregateCodec) ClientMapQueryPartitionIterator(com.hazelcast.client.map.impl.iterator.ClientMapQueryPartitionIterator) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) MapAddInterceptorCodec(com.hazelcast.client.impl.protocol.codec.MapAddInterceptorCodec) MapFlushCodec(com.hazelcast.client.impl.protocol.codec.MapFlushCodec) MapPutCodec(com.hazelcast.client.impl.protocol.codec.MapPutCodec) ClientMapQueryIterable(com.hazelcast.client.map.impl.iterator.ClientMapQueryIterable) ClientQueryCacheContext(com.hazelcast.client.map.impl.querycache.ClientQueryCacheContext) MapAddEntryListenerToKeyCodec(com.hazelcast.client.impl.protocol.codec.MapAddEntryListenerToKeyCodec) DataAwareEntryEvent(com.hazelcast.map.impl.DataAwareEntryEvent) IMapEvent(com.hazelcast.map.IMapEvent) UNSET(com.hazelcast.map.impl.record.Record.UNSET) MapProjectWithPredicateCodec(com.hazelcast.client.impl.protocol.codec.MapProjectWithPredicateCodec) ArrayList(java.util.ArrayList) MapAddEntryListenerToKeyWithPredicateCodec(com.hazelcast.client.impl.protocol.codec.MapAddEntryListenerToKeyWithPredicateCodec) MapLoadAllCodec(com.hazelcast.client.impl.protocol.codec.MapLoadAllCodec) ClientContext(com.hazelcast.client.impl.spi.ClientContext) BiConsumer(java.util.function.BiConsumer) PagingPredicateHolder(com.hazelcast.client.impl.protocol.codec.holder.PagingPredicateHolder) MapAddEntryListenerCodec(com.hazelcast.client.impl.protocol.codec.MapAddEntryListenerCodec) Nullable(javax.annotation.Nullable) LocalMapStats(com.hazelcast.map.LocalMapStats) MapEntriesWithPredicateCodec(com.hazelcast.client.impl.protocol.codec.MapEntriesWithPredicateCodec) MapKeySetWithPagingPredicateCodec(com.hazelcast.client.impl.protocol.codec.MapKeySetWithPagingPredicateCodec) ThreadUtil.getThreadId(com.hazelcast.internal.util.ThreadUtil.getThreadId) MapSubmitToKeyCodec(com.hazelcast.client.impl.protocol.codec.MapSubmitToKeyCodec) ClientMapIterator(com.hazelcast.client.map.impl.iterator.ClientMapIterator) QueryCacheEndToEndProvider(com.hazelcast.map.impl.querycache.subscriber.QueryCacheEndToEndProvider) MapGetCodec(com.hazelcast.client.impl.protocol.codec.MapGetCodec) MapDeleteCodec(com.hazelcast.client.impl.protocol.codec.MapDeleteCodec) TimeUtil.timeInMsOrTimeIfNullUnit(com.hazelcast.internal.util.TimeUtil.timeInMsOrTimeIfNullUnit) MapProjectCodec(com.hazelcast.client.impl.protocol.codec.MapProjectCodec) Preconditions.checkNotNull(com.hazelcast.internal.util.Preconditions.checkNotNull) LocalMapStatsImpl(com.hazelcast.internal.monitor.impl.LocalMapStatsImpl) ClientMapPartitionIterator(com.hazelcast.client.map.impl.iterator.ClientMapPartitionIterator) MapSetCodec(com.hazelcast.client.impl.protocol.codec.MapSetCodec) EntryProcessor(com.hazelcast.map.EntryProcessor) ReadOnly(com.hazelcast.core.ReadOnly) ClientMapIterable(com.hazelcast.client.map.impl.iterator.ClientMapIterable) ClientMapPartitionIterable(com.hazelcast.client.map.impl.iterator.ClientMapPartitionIterable) MapAddIndexCodec(com.hazelcast.client.impl.protocol.codec.MapAddIndexCodec) MapEvent(com.hazelcast.map.MapEvent) ListenerAdapter(com.hazelcast.map.impl.ListenerAdapter) MapExecuteOnKeyCodec(com.hazelcast.client.impl.protocol.codec.MapExecuteOnKeyCodec) MapLoadGivenKeysCodec(com.hazelcast.client.impl.protocol.codec.MapLoadGivenKeysCodec) Member(com.hazelcast.cluster.Member) BiFunction(java.util.function.BiFunction) ListenerAdapters.createListenerAdapter(com.hazelcast.map.impl.ListenerAdapters.createListenerAdapter) MapPutIfAbsentWithMaxIdleCodec(com.hazelcast.client.impl.protocol.codec.MapPutIfAbsentWithMaxIdleCodec) CollectionUtil(com.hazelcast.internal.util.CollectionUtil) MapValuesWithPredicateCodec(com.hazelcast.client.impl.protocol.codec.MapValuesWithPredicateCodec) EventHandler(com.hazelcast.client.impl.spi.EventHandler) MapContainsKeyCodec(com.hazelcast.client.impl.protocol.codec.MapContainsKeyCodec) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) MapAggregateWithPredicateCodec(com.hazelcast.client.impl.protocol.codec.MapAggregateWithPredicateCodec) MapListener(com.hazelcast.map.listener.MapListener) MapAddPartitionLostListenerCodec(com.hazelcast.client.impl.protocol.codec.MapAddPartitionLostListenerCodec) PredicateUtils.unwrapPagingPredicate(com.hazelcast.query.impl.predicates.PredicateUtils.unwrapPagingPredicate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexUtils(com.hazelcast.query.impl.IndexUtils) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) Predicate(com.hazelcast.query.Predicate) ClientLockReferenceIdGenerator(com.hazelcast.client.impl.clientside.ClientLockReferenceIdGenerator) EntryEvent(com.hazelcast.core.EntryEvent) MapPutWithMaxIdleCodec(com.hazelcast.client.impl.protocol.codec.MapPutWithMaxIdleCodec) MapIsLockedCodec(com.hazelcast.client.impl.protocol.codec.MapIsLockedCodec) Collection(java.util.Collection) UUID(java.util.UUID) SubscriberContext(com.hazelcast.map.impl.querycache.subscriber.SubscriberContext) IndexConfig(com.hazelcast.config.IndexConfig) MapEvictCodec(com.hazelcast.client.impl.protocol.codec.MapEvictCodec) List(java.util.List) MapEntriesWithPagingPredicateCodec(com.hazelcast.client.impl.protocol.codec.MapEntriesWithPagingPredicateCodec) MapGetEntryViewCodec(com.hazelcast.client.impl.protocol.codec.MapGetEntryViewCodec) EventJournalMapEvent(com.hazelcast.map.EventJournalMapEvent) ClientInvocationFuture(com.hazelcast.client.impl.spi.impl.ClientInvocationFuture) MapEventJournalSubscribeCodec(com.hazelcast.client.impl.protocol.codec.MapEventJournalSubscribeCodec) MapReplaceIfSameCodec(com.hazelcast.client.impl.protocol.codec.MapReplaceIfSameCodec) QueryCacheRequest(com.hazelcast.map.impl.querycache.subscriber.QueryCacheRequest) TimeUtil.timeInMsOrOneIfResultIsZero(com.hazelcast.internal.util.TimeUtil.timeInMsOrOneIfResultIsZero) MapPartitionLostEvent(com.hazelcast.map.MapPartitionLostEvent) Projection(com.hazelcast.projection.Projection) MapExecuteWithPredicateCodec(com.hazelcast.client.impl.protocol.codec.MapExecuteWithPredicateCodec) MapEventJournalReadCodec(com.hazelcast.client.impl.protocol.codec.MapEventJournalReadCodec) QueryCacheRequest.newQueryCacheRequest(com.hazelcast.map.impl.querycache.subscriber.QueryCacheRequest.newQueryCacheRequest) MapKeySetWithPredicateCodec(com.hazelcast.client.impl.protocol.codec.MapKeySetWithPredicateCodec) MapRemoveInterceptorCodec(com.hazelcast.client.impl.protocol.codec.MapRemoveInterceptorCodec) ResponseParameters(com.hazelcast.client.impl.protocol.codec.MapEventJournalSubscribeCodec.ResponseParameters) CollectionUtil.objectToDataCollection(com.hazelcast.internal.util.CollectionUtil.objectToDataCollection) MapTryRemoveCodec(com.hazelcast.client.impl.protocol.codec.MapTryRemoveCodec) SimpleEntryView(com.hazelcast.map.impl.SimpleEntryView) HashMap(java.util.HashMap) Function(java.util.function.Function) MapUtil.createHashMap(com.hazelcast.internal.util.MapUtil.createHashMap) Aggregator(com.hazelcast.aggregation.Aggregator) MapIsEmptyCodec(com.hazelcast.client.impl.protocol.codec.MapIsEmptyCodec) PagingPredicate(com.hazelcast.query.PagingPredicate) MapValuesCodec(com.hazelcast.client.impl.protocol.codec.MapValuesCodec) MapRemoveAllCodec(com.hazelcast.client.impl.protocol.codec.MapRemoveAllCodec) MapUnlockCodec(com.hazelcast.client.impl.protocol.codec.MapUnlockCodec) Nonnull(javax.annotation.Nonnull) MapListenerFlagOperator.setAndGetListenerFlags(com.hazelcast.map.impl.MapListenerFlagOperator.setAndGetListenerFlags) MapPutTransientCodec(com.hazelcast.client.impl.protocol.codec.MapPutTransientCodec) Collections.emptyMap(java.util.Collections.emptyMap) Iterator(java.util.Iterator) EntryEventType(com.hazelcast.core.EntryEventType) EventJournalReader(com.hazelcast.internal.journal.EventJournalReader) MapInterceptor(com.hazelcast.map.MapInterceptor) UnmodifiableLazySet(com.hazelcast.spi.impl.UnmodifiableLazySet) Data(com.hazelcast.internal.serialization.Data) MapPutAllCodec(com.hazelcast.client.impl.protocol.codec.MapPutAllCodec) MapEvictAllCodec(com.hazelcast.client.impl.protocol.codec.MapEvictAllCodec) MapTryPutCodec(com.hazelcast.client.impl.protocol.codec.MapTryPutCodec) ClientProxy(com.hazelcast.client.impl.spi.ClientProxy) MapExecuteOnKeysCodec(com.hazelcast.client.impl.protocol.codec.MapExecuteOnKeysCodec) TimeUnit(java.util.concurrent.TimeUnit) AbstractMap(java.util.AbstractMap) PagingPredicateImpl(com.hazelcast.query.impl.predicates.PagingPredicateImpl) ClientDelegatingFuture(com.hazelcast.client.impl.ClientDelegatingFuture) QueryCache(com.hazelcast.map.QueryCache) MapPartitionLostListener(com.hazelcast.map.listener.MapPartitionLostListener) ReadResultSet(com.hazelcast.ringbuffer.ReadResultSet) EventJournalInitialSubscriberState(com.hazelcast.internal.journal.EventJournalInitialSubscriberState) Collections(java.util.Collections) MapReplaceAllCodec(com.hazelcast.client.impl.protocol.codec.MapReplaceAllCodec) IMap(com.hazelcast.map.IMap) HashMap(java.util.HashMap) MapUtil.createHashMap(com.hazelcast.internal.util.MapUtil.createHashMap) Data(com.hazelcast.internal.serialization.Data) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractMap(java.util.AbstractMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UnmodifiableLazyList(com.hazelcast.spi.impl.UnmodifiableLazyList) ArrayList(java.util.ArrayList) List(java.util.List) ClientPartitionService(com.hazelcast.client.impl.spi.ClientPartitionService) Map(java.util.Map) HashMap(java.util.HashMap) MapUtil.createHashMap(com.hazelcast.internal.util.MapUtil.createHashMap) Collections.emptyMap(java.util.Collections.emptyMap) AbstractMap(java.util.AbstractMap) IMap(com.hazelcast.map.IMap)

Example 82 with BiConsumer

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);
    }
}
Also used : MultiMapPutCodec(com.hazelcast.client.impl.protocol.codec.MultiMapPutCodec) MultiMapContainsValueCodec(com.hazelcast.client.impl.protocol.codec.MultiMapContainsValueCodec) MultiMapAddEntryListenerCodec(com.hazelcast.client.impl.protocol.codec.MultiMapAddEntryListenerCodec) MultiMapForceUnlockCodec(com.hazelcast.client.impl.protocol.codec.MultiMapForceUnlockCodec) Member(com.hazelcast.cluster.Member) ListenerAdapters.createListenerAdapter(com.hazelcast.map.impl.ListenerAdapters.createListenerAdapter) MultiMapSizeCodec(com.hazelcast.client.impl.protocol.codec.MultiMapSizeCodec) Thread.currentThread(java.lang.Thread.currentThread) CollectionUtil(com.hazelcast.internal.util.CollectionUtil) EventHandler(com.hazelcast.client.impl.spi.EventHandler) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) MultiMapClearCodec(com.hazelcast.client.impl.protocol.codec.MultiMapClearCodec) ListenerMessageCodec(com.hazelcast.client.impl.spi.impl.ListenerMessageCodec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MultiMapContainsEntryCodec(com.hazelcast.client.impl.protocol.codec.MultiMapContainsEntryCodec) Map(java.util.Map) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) ClientLockReferenceIdGenerator(com.hazelcast.client.impl.clientside.ClientLockReferenceIdGenerator) MultiMapIsLockedCodec(com.hazelcast.client.impl.protocol.codec.MultiMapIsLockedCodec) MultiMapPutAllCodec(com.hazelcast.client.impl.protocol.codec.MultiMapPutAllCodec) EntryEvent(com.hazelcast.core.EntryEvent) MultiMapEntrySetCodec(com.hazelcast.client.impl.protocol.codec.MultiMapEntrySetCodec) LocalMultiMapStats(com.hazelcast.multimap.LocalMultiMapStats) ClientPartitionService(com.hazelcast.client.impl.spi.ClientPartitionService) MultiMapLockCodec(com.hazelcast.client.impl.protocol.codec.MultiMapLockCodec) Collection(java.util.Collection) Set(java.util.Set) EntryListener(com.hazelcast.core.EntryListener) UUID(java.util.UUID) MultiMapRemoveEntryCodec(com.hazelcast.client.impl.protocol.codec.MultiMapRemoveEntryCodec) MultiMapDeleteCodec(com.hazelcast.client.impl.protocol.codec.MultiMapDeleteCodec) UnmodifiableLazyList(com.hazelcast.spi.impl.UnmodifiableLazyList) CompletionStage(java.util.concurrent.CompletionStage) Preconditions.checkPositive(com.hazelcast.internal.util.Preconditions.checkPositive) MultiMapAddEntryListenerToKeyCodec(com.hazelcast.client.impl.protocol.codec.MultiMapAddEntryListenerToKeyCodec) MultiMapGetCodec(com.hazelcast.client.impl.protocol.codec.MultiMapGetCodec) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) MultiMapRemoveEntryListenerCodec(com.hazelcast.client.impl.protocol.codec.MultiMapRemoveEntryListenerCodec) MultiMapTryLockCodec(com.hazelcast.client.impl.protocol.codec.MultiMapTryLockCodec) DataAwareEntryEvent(com.hazelcast.map.impl.DataAwareEntryEvent) HashMap(java.util.HashMap) IMapEvent(com.hazelcast.map.IMapEvent) MultiMapUnlockCodec(com.hazelcast.client.impl.protocol.codec.MultiMapUnlockCodec) ArrayList(java.util.ArrayList) MultiMapValuesCodec(com.hazelcast.client.impl.protocol.codec.MultiMapValuesCodec) MultiMap(com.hazelcast.multimap.MultiMap) ClientContext(com.hazelcast.client.impl.spi.ClientContext) BiConsumer(java.util.function.BiConsumer) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) EntryEventType(com.hazelcast.core.EntryEventType) UnmodifiableLazySet(com.hazelcast.spi.impl.UnmodifiableLazySet) MultiMapRemoveCodec(com.hazelcast.client.impl.protocol.codec.MultiMapRemoveCodec) Data(com.hazelcast.internal.serialization.Data) MultiMapContainsKeyCodec(com.hazelcast.client.impl.protocol.codec.MultiMapContainsKeyCodec) MultiMapKeySetCodec(com.hazelcast.client.impl.protocol.codec.MultiMapKeySetCodec) Preconditions.checkNotNull(com.hazelcast.internal.util.Preconditions.checkNotNull) ClientProxy(com.hazelcast.client.impl.spi.ClientProxy) TimeUnit(java.util.concurrent.TimeUnit) AbstractMap(java.util.AbstractMap) MultiMapValueCountCodec(com.hazelcast.client.impl.protocol.codec.MultiMapValueCountCodec) MapEvent(com.hazelcast.map.MapEvent) ListenerAdapter(com.hazelcast.map.impl.ListenerAdapter) ThreadUtil(com.hazelcast.internal.util.ThreadUtil) HashMap(java.util.HashMap) Data(com.hazelcast.internal.serialization.Data) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractMap(java.util.AbstractMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) ClientPartitionService(com.hazelcast.client.impl.spi.ClientPartitionService) Map(java.util.Map) HashMap(java.util.HashMap) MultiMap(com.hazelcast.multimap.MultiMap) AbstractMap(java.util.AbstractMap)

Example 83 with BiConsumer

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;
}
Also used : RaftNodeStatus(com.hazelcast.cp.internal.raft.impl.RaftNodeStatus) AppendSuccessResponse(com.hazelcast.cp.internal.raft.impl.dto.AppendSuccessResponse) Clock(com.hazelcast.internal.util.Clock) CP_METRIC_RAFT_SERVICE_TERMINATED_RAFT_NODE_GROUP_IDS(com.hazelcast.internal.metrics.MetricDescriptorConstants.CP_METRIC_RAFT_SERVICE_TERMINATED_RAFT_NODE_GROUP_IDS) InstallSnapshot(com.hazelcast.cp.internal.raft.impl.dto.InstallSnapshot) RaftIntegration(com.hazelcast.cp.internal.raft.impl.RaftIntegration) ManagedService(com.hazelcast.internal.services.ManagedService) PartitionMigratingException(com.hazelcast.spi.exception.PartitionMigratingException) MemberImpl(com.hazelcast.cluster.impl.MemberImpl) VoteResponse(com.hazelcast.cp.internal.raft.impl.dto.VoteResponse) ClusterService(com.hazelcast.internal.cluster.ClusterService) Future(java.util.concurrent.Future) CPGroup(com.hazelcast.cp.CPGroup) CPGroupDestroyedException(com.hazelcast.cp.exception.CPGroupDestroyedException) RaftNodeImpl(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl) Map(java.util.Map) CPSubsystemConfig(com.hazelcast.config.cp.CPSubsystemConfig) RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) StringUtil.equalsIgnoreCase(com.hazelcast.internal.util.StringUtil.equalsIgnoreCase) CP_PREFIX_RAFT_METADATA(com.hazelcast.internal.metrics.MetricDescriptorConstants.CP_PREFIX_RAFT_METADATA) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) RaftRemoteService(com.hazelcast.cp.internal.datastructures.spi.RaftRemoteService) CPPersistenceService(com.hazelcast.cp.internal.persistence.CPPersistenceService) ExceptionUtil(com.hazelcast.internal.util.ExceptionUtil) Set(java.util.Set) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) CP_PREFIX_RAFT_GROUP(com.hazelcast.internal.metrics.MetricDescriptorConstants.CP_PREFIX_RAFT_GROUP) EventListener(java.util.EventListener) PartitionReplicationEvent(com.hazelcast.internal.partition.PartitionReplicationEvent) CPMember(com.hazelcast.cp.CPMember) CP_DISCRIMINATOR_GROUPID(com.hazelcast.internal.metrics.MetricDescriptorConstants.CP_DISCRIMINATOR_GROUPID) GetActiveRaftGroupIdsOp(com.hazelcast.cp.internal.raftop.metadata.GetActiveRaftGroupIdsOp) CPGroupAvailabilityEventImpl(com.hazelcast.cp.event.impl.CPGroupAvailabilityEventImpl) Versions(com.hazelcast.internal.cluster.Versions) GetInitialRaftGroupMembersIfCurrentGroupMemberOp(com.hazelcast.cp.internal.raftop.GetInitialRaftGroupMembersIfCurrentGroupMemberOp) CP_METRIC_RAFT_SERVICE_NODES(com.hazelcast.internal.metrics.MetricDescriptorConstants.CP_METRIC_RAFT_SERVICE_NODES) RaftManagedService(com.hazelcast.cp.internal.datastructures.spi.RaftManagedService) VoteRequest(com.hazelcast.cp.internal.raft.impl.dto.VoteRequest) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ArrayList(java.util.ArrayList) MigrationEndpoint(com.hazelcast.internal.partition.MigrationEndpoint) CP_PREFIX_RAFT(com.hazelcast.internal.metrics.MetricDescriptorConstants.CP_PREFIX_RAFT) SYSTEM_EXECUTOR(com.hazelcast.spi.impl.executionservice.ExecutionService.SYSTEM_EXECUTOR) RestoredRaftState(com.hazelcast.cp.internal.raft.impl.persistence.RestoredRaftState) LINEARIZABLE(com.hazelcast.cp.internal.raft.QueryPolicy.LINEARIZABLE) BiConsumer(java.util.function.BiConsumer) CPMembershipListener(com.hazelcast.cp.event.CPMembershipListener) RaftNode(com.hazelcast.cp.internal.raft.impl.RaftNode) GetRaftGroupIdsOp(com.hazelcast.cp.internal.raftop.metadata.GetRaftGroupIdsOp) MembershipServiceEvent(com.hazelcast.internal.services.MembershipServiceEvent) DEFAULT_GROUP_NAME(com.hazelcast.cp.CPGroup.DEFAULT_GROUP_NAME) NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) InternalCompletableFuture.newCompletedFuture(com.hazelcast.spi.impl.InternalCompletableFuture.newCompletedFuture) Properties(java.util.Properties) RaftLog(com.hazelcast.cp.internal.raft.impl.log.RaftLog) MetricsCollectionContext(com.hazelcast.internal.metrics.MetricsCollectionContext) MembershipAwareService(com.hazelcast.internal.services.MembershipAwareService) Preconditions.checkNotNull(com.hazelcast.internal.util.Preconditions.checkNotNull) GetActiveCPMembersOp(com.hazelcast.cp.internal.raftop.metadata.GetActiveCPMembersOp) ExecutionException(java.util.concurrent.ExecutionException) AddCPMemberOp(com.hazelcast.cp.internal.raftop.metadata.AddCPMemberOp) CPGroupAvailabilityEvent(com.hazelcast.cp.event.CPGroupAvailabilityEvent) ManagedExecutorService(com.hazelcast.internal.util.executor.ManagedExecutorService) RaftStateStore(com.hazelcast.cp.internal.raft.impl.persistence.RaftStateStore) Member(com.hazelcast.cluster.Member) ResetCPMemberOp(com.hazelcast.cp.internal.operation.ResetCPMemberOp) PreVoteRequest(com.hazelcast.cp.internal.raft.impl.dto.PreVoteRequest) ForceDestroyRaftGroupOp(com.hazelcast.cp.internal.raftop.metadata.ForceDestroyRaftGroupOp) SnapshotAwareService(com.hazelcast.cp.internal.raft.SnapshotAwareService) RaftServicePreJoinOp(com.hazelcast.cp.internal.raftop.metadata.RaftServicePreJoinOp) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) AppendFailureResponse(com.hazelcast.cp.internal.raft.impl.dto.AppendFailureResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CPGroupAvailabilityListener(com.hazelcast.cp.event.CPGroupAvailabilityListener) CannotRemoveCPMemberException(com.hazelcast.cp.internal.exception.CannotRemoveCPMemberException) DynamicMetricsProvider(com.hazelcast.internal.metrics.DynamicMetricsProvider) GetRaftGroupOp(com.hazelcast.cp.internal.raftop.metadata.GetRaftGroupOp) ProbeLevel(com.hazelcast.internal.metrics.ProbeLevel) CP_METRIC_RAFT_SERVICE_DESTROYED_GROUP_IDS(com.hazelcast.internal.metrics.MetricDescriptorConstants.CP_METRIC_RAFT_SERVICE_DESTROYED_GROUP_IDS) Preconditions.checkTrue(com.hazelcast.internal.util.Preconditions.checkTrue) ResponseAlreadySentException(com.hazelcast.spi.exception.ResponseAlreadySentException) UnsafeStateReplicationOp(com.hazelcast.cp.internal.operation.unsafe.UnsafeStateReplicationOp) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) ServiceInfo(com.hazelcast.spi.impl.servicemanager.ServiceInfo) LogFileStructure(com.hazelcast.cp.internal.raft.impl.persistence.LogFileStructure) Probe(com.hazelcast.internal.metrics.Probe) Collection(java.util.Collection) METADATA_CP_GROUP_NAME(com.hazelcast.cp.CPGroup.METADATA_CP_GROUP_NAME) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TriggerLeaderElection(com.hazelcast.cp.internal.raft.impl.dto.TriggerLeaderElection) RaftRole(com.hazelcast.cp.internal.raft.impl.RaftRole) MetricsPlugin(com.hazelcast.internal.diagnostics.MetricsPlugin) UUID(java.util.UUID) RaftNodeImpl.newRaftNode(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl.newRaftNode) List(java.util.List) LEADER_LOCAL(com.hazelcast.cp.internal.raft.QueryPolicy.LEADER_LOCAL) Entry(java.util.Map.Entry) Preconditions.checkFalse(com.hazelcast.internal.util.Preconditions.checkFalse) CP_METRIC_RAFT_SERVICE_MISSING_MEMBERS(com.hazelcast.internal.metrics.MetricDescriptorConstants.CP_METRIC_RAFT_SERVICE_MISSING_MEMBERS) PartitionMigrationEvent(com.hazelcast.internal.partition.PartitionMigrationEvent) MetricsRegistry(com.hazelcast.internal.metrics.MetricsRegistry) ExecutionService(com.hazelcast.spi.impl.executionservice.ExecutionService) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) MigrationAwareService(com.hazelcast.internal.partition.MigrationAwareService) OperationServiceImpl(com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl) PartitionOperationThread(com.hazelcast.spi.impl.operationexecutor.impl.PartitionOperationThread) ConcurrentMap(java.util.concurrent.ConcurrentMap) AppendRequest(com.hazelcast.cp.internal.raft.impl.dto.AppendRequest) MetricDescriptor(com.hazelcast.internal.metrics.MetricDescriptor) Collections.newSetFromMap(java.util.Collections.newSetFromMap) CPMembershipEvent(com.hazelcast.cp.event.CPMembershipEvent) ILogger(com.hazelcast.logging.ILogger) Operation(com.hazelcast.spi.impl.operationservice.Operation) EventPublishingService(com.hazelcast.spi.impl.eventservice.EventPublishingService) CPGroupId(com.hazelcast.cp.CPGroupId) Timer(com.hazelcast.internal.util.Timer) NodeEngine(com.hazelcast.spi.impl.NodeEngine) GetActiveRaftGroupByNameOp(com.hazelcast.cp.internal.raftop.metadata.GetActiveRaftGroupByNameOp) Data(com.hazelcast.internal.serialization.Data) NON_LOCAL_MEMBER_SELECTOR(com.hazelcast.cluster.memberselector.MemberSelectors.NON_LOCAL_MEMBER_SELECTOR) RemoveCPMemberOp(com.hazelcast.cp.internal.raftop.metadata.RemoveCPMemberOp) Preconditions.checkState(com.hazelcast.internal.util.Preconditions.checkState) ConfigValidator.checkCPSubsystemConfig(com.hazelcast.internal.config.ConfigValidator.checkCPSubsystemConfig) CP_TAG_NAME(com.hazelcast.internal.metrics.MetricDescriptorConstants.CP_TAG_NAME) TimeUnit(java.util.concurrent.TimeUnit) PreVoteResponse(com.hazelcast.cp.internal.raft.impl.dto.PreVoteResponse) GracefulShutdownAwareService(com.hazelcast.internal.services.GracefulShutdownAwareService) PreJoinAwareService(com.hazelcast.internal.services.PreJoinAwareService) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) MANAGEMENT_TASK_PERIOD_IN_MILLIS(com.hazelcast.cp.internal.RaftGroupMembershipManager.MANAGEMENT_TASK_PERIOD_IN_MILLIS) CannotRemoveCPMemberException(com.hazelcast.cp.internal.exception.CannotRemoveCPMemberException) GetActiveCPMembersOp(com.hazelcast.cp.internal.raftop.metadata.GetActiveCPMembersOp) CPMember(com.hazelcast.cp.CPMember) ClusterService(com.hazelcast.internal.cluster.ClusterService) CPMember(com.hazelcast.cp.CPMember) Member(com.hazelcast.cluster.Member)

Example 84 with BiConsumer

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;
}
Also used : Operation(com.hazelcast.spi.impl.operationservice.Operation) ResetCPMemberOp(com.hazelcast.cp.internal.operation.ResetCPMemberOp) ClusterService(com.hazelcast.internal.cluster.ClusterService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CPMember(com.hazelcast.cp.CPMember) Member(com.hazelcast.cluster.Member) BiConsumer(java.util.function.BiConsumer) OperationServiceImpl(com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl)

Example 85 with BiConsumer

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);
}
Also used : MasterContext(com.hazelcast.jet.impl.MasterContext) NOT_RUNNING(com.hazelcast.jet.core.JobStatus.NOT_RUNNING) RunWith(org.junit.runner.RunWith) HazelcastSerialClassRunner(com.hazelcast.test.HazelcastSerialClassRunner) ClusterService(com.hazelcast.internal.cluster.ClusterService) Future(java.util.concurrent.Future) STARTING(com.hazelcast.jet.core.JobStatus.STARTING) BiConsumer(java.util.function.BiConsumer) Assert.fail(org.junit.Assert.fail) ExpectedException(org.junit.rules.ExpectedException) Job(com.hazelcast.jet.Job) JobRepository(com.hazelcast.jet.impl.JobRepository) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MockPS(com.hazelcast.jet.core.TestProcessors.MockPS) NightlyTest(com.hazelcast.test.annotation.NightlyTest) CancellationException(java.util.concurrent.CancellationException) Assert.assertNotNull(org.junit.Assert.assertNotNull) JobConfig(com.hazelcast.jet.config.JobConfig) MAX_BACKUP_COUNT(com.hazelcast.internal.partition.IPartition.MAX_BACKUP_COUNT) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) NoOutputSourceP(com.hazelcast.jet.core.TestProcessors.NoOutputSourceP) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Rule(org.junit.Rule) RUNNING(com.hazelcast.jet.core.JobStatus.RUNNING) JobExecutionRecord(com.hazelcast.jet.impl.JobExecutionRecord) COMPLETED(com.hazelcast.jet.core.JobStatus.COMPLETED) Lists.newArrayList(org.assertj.core.util.Lists.newArrayList) Assert.assertEquals(org.junit.Assert.assertEquals) JetServiceBackend(com.hazelcast.jet.impl.JetServiceBackend) MockPS(com.hazelcast.jet.core.TestProcessors.MockPS) CountDownLatch(java.util.concurrent.CountDownLatch) JobConfig(com.hazelcast.jet.config.JobConfig) ExpectedException(org.junit.rules.ExpectedException) CancellationException(java.util.concurrent.CancellationException) CancellationException(java.util.concurrent.CancellationException) Job(com.hazelcast.jet.Job) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Aggregations

BiConsumer (java.util.function.BiConsumer)255 Test (org.junit.Test)110 List (java.util.List)106 Map (java.util.Map)77 IOException (java.io.IOException)75 Consumer (java.util.function.Consumer)69 ArrayList (java.util.ArrayList)68 HashMap (java.util.HashMap)64 Collectors (java.util.stream.Collectors)53 CountDownLatch (java.util.concurrent.CountDownLatch)52 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)50 Collections (java.util.Collections)46 Set (java.util.Set)46 Collection (java.util.Collection)45 Arrays (java.util.Arrays)44 TimeUnit (java.util.concurrent.TimeUnit)43 Assert (org.junit.Assert)43 Function (java.util.function.Function)41 Optional (java.util.Optional)40 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)35