Search in sources :

Example 1 with ReductionPublisherRequestCommand

use of org.infinispan.reactive.publisher.impl.commands.reduction.ReductionPublisherRequestCommand in project infinispan by infinispan.

the class ClusterPublisherManagerImpl method startSegmentPublisher.

private <I, R> void startSegmentPublisher(boolean parallelPublisher, IntSet segments, InvocationContext ctx, long explicitFlags, DeliveryGuarantee deliveryGuarantee, ComposedType<K, I, R> composedType, Function<? super Publisher<I>, ? extends CompletionStage<R>> transformer, Function<? super Publisher<R>, ? extends CompletionStage<R>> finalizer, FlowableProcessor<R> flowableProcessor) {
    LocalizedCacheTopology topology = distributionManager.getCacheTopology();
    Address localAddress = topology.getLocalAddress();
    Map<Address, IntSet> targets = determineSegmentTargets(topology, segments, localAddress);
    int targetSize = targets.size();
    if (targetSize == 0) {
        handleNoTargets(transformer, flowableProcessor);
        return;
    }
    // used to determine that last parallel completion, to either complete or retry
    AtomicInteger parallelCount;
    boolean useContext = ctx != null && ctx.lookedUpEntriesCount() > 0;
    Map<Address, Set<K>> keysToExcludeByAddress;
    if (useContext) {
        parallelCount = new AtomicInteger(targetSize + 1);
        keysToExcludeByAddress = determineKeyTargets(topology, (Set<K>) ctx.getLookedUpEntries().keySet(), localAddress, segments, null);
    } else {
        parallelCount = new AtomicInteger(targetSize);
        keysToExcludeByAddress = Collections.emptyMap();
    }
    // This way we only have to allocate 1 per request chain
    BiConsumer<PublisherResult<R>, Throwable> biConsumer = new SegmentSpecificConsumer<>(flowableProcessor, parallelCount, topology.getTopologyId(), parallelPublisher, ctx, explicitFlags, deliveryGuarantee, composedType, transformer, finalizer);
    IntSet localSegments = targets.remove(localAddress);
    // If any targets left, they are all remote
    if (!targets.isEmpty()) {
        // We submit the remote ones first as they will not block at all, just to send remote tasks
        for (Map.Entry<Address, IntSet> remoteTarget : targets.entrySet()) {
            Address remoteAddress = remoteTarget.getKey();
            IntSet remoteSegments = remoteTarget.getValue();
            ReductionPublisherRequestCommand<K> command = composedType.remoteInvocation(parallelPublisher, remoteSegments, null, keysToExcludeByAddress.get(remoteAddress), explicitFlags, deliveryGuarantee, transformer, finalizer);
            command.setTopologyId(topology.getTopologyId());
            CompletionStage<PublisherResult<R>> stage = rpcManager.invokeCommand(remoteAddress, command, new SegmentPublisherResultCollector<>(remoteSegments), rpcManager.getSyncRpcOptions());
            stage.whenComplete(biConsumer);
        }
    }
    if (localSegments != null) {
        CompletionStage<PublisherResult<R>> localStage = composedType.localInvocation(parallelPublisher, localSegments, null, keysToExcludeByAddress.get(localAddress), explicitFlags, deliveryGuarantee, transformer, finalizer);
        if (log.isTraceEnabled()) {
            // Make sure the trace occurs before response is processed
            localStage = localStage.whenComplete((results, t) -> {
                if (t != null) {
                    log.tracef(t, "Received exception while processing segments %s from %s", localSegments, localAddress);
                } else {
                    log.tracef("Result was: %s for segments %s from %s with %s suspected segments", results.getResult(), localSegments, localAddress, results.getSuspectedSegments());
                }
            });
        }
        // Map to the same collector, so we can reuse the same BiConsumer
        localStage.whenComplete(biConsumer);
    }
    if (useContext) {
        handleContextInvocation(segments, null, ctx, composedType, transformer, biConsumer);
    }
}
Also used : IntSets(org.infinispan.commons.util.IntSets) RxJavaInterop(org.infinispan.reactive.RxJavaInterop) LogFactory(org.infinispan.util.logging.LogFactory) BiFunction(java.util.function.BiFunction) PersistenceConfiguration(org.infinispan.configuration.cache.PersistenceConfiguration) Stop(org.infinispan.factories.annotations.Stop) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) Scopes(org.infinispan.factories.scopes.Scopes) NextPublisherCommand(org.infinispan.reactive.publisher.impl.commands.batch.NextPublisherCommand) InvocationContext(org.infinispan.context.InvocationContext) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) PublisherResponse(org.infinispan.reactive.publisher.impl.commands.batch.PublisherResponse) Scope(org.infinispan.factories.scopes.Scope) RpcOptions(org.infinispan.remoting.rpc.RpcOptions) InitialPublisherCommand(org.infinispan.reactive.publisher.impl.commands.batch.InitialPublisherCommand) SegmentPublisherResult(org.infinispan.reactive.publisher.impl.commands.reduction.SegmentPublisherResult) CommandsFactory(org.infinispan.commands.CommandsFactory) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ValidResponseCollector(org.infinispan.remoting.transport.ValidResponseCollector) Set(java.util.Set) MarshallableFunctions(org.infinispan.marshall.core.MarshallableFunctions) UnicastProcessor(io.reactivex.rxjava3.processors.UnicastProcessor) Objects(java.util.Objects) IntSet(org.infinispan.commons.util.IntSet) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) CancelPublisherCommand(org.infinispan.reactive.publisher.impl.commands.batch.CancelPublisherCommand) ReductionPublisherRequestCommand(org.infinispan.reactive.publisher.impl.commands.reduction.ReductionPublisherRequestCommand) SerializableFunction(org.infinispan.util.function.SerializableFunction) PersistenceManager(org.infinispan.persistence.manager.PersistenceManager) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ComponentRegistry(org.infinispan.factories.ComponentRegistry) ObjIntConsumer(java.util.function.ObjIntConsumer) Function(java.util.function.Function) Supplier(java.util.function.Supplier) StateTransferLock(org.infinispan.statetransfer.StateTransferLock) ArrayList(java.util.ArrayList) Start(org.infinispan.factories.annotations.Start) PublisherResult(org.infinispan.reactive.publisher.impl.commands.reduction.PublisherResult) HashSet(java.util.HashSet) StoreConfiguration(org.infinispan.configuration.cache.StoreConfiguration) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) SingleResponseCollector(org.infinispan.remoting.transport.impl.SingleResponseCollector) Log(org.infinispan.util.logging.Log) BiConsumer(java.util.function.BiConsumer) VoidResponseCollector(org.infinispan.remoting.transport.impl.VoidResponseCollector) StoreChangeListener(org.infinispan.persistence.manager.PersistenceManager.StoreChangeListener) Subscriber(org.reactivestreams.Subscriber) Address(org.infinispan.remoting.transport.Address) KeyPublisherResult(org.infinispan.reactive.publisher.impl.commands.reduction.KeyPublisherResult) CacheException(org.infinispan.commons.CacheException) Flowable(io.reactivex.rxjava3.core.Flowable) Iterator(java.util.Iterator) CacheEntry(org.infinispan.container.entries.CacheEntry) Publisher(org.reactivestreams.Publisher) Util(org.infinispan.commons.util.Util) RpcManager(org.infinispan.remoting.rpc.RpcManager) ValidResponse(org.infinispan.remoting.responses.ValidResponse) Inject(org.infinispan.factories.annotations.Inject) FlowableProcessor(io.reactivex.rxjava3.processors.FlowableProcessor) PrimitiveIterator(java.util.PrimitiveIterator) TimeUnit(java.util.concurrent.TimeUnit) IllegalLifecycleStateException(org.infinispan.commons.IllegalLifecycleStateException) AbstractMap(java.util.AbstractMap) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) Configuration(org.infinispan.configuration.cache.Configuration) DistributionInfo(org.infinispan.distribution.DistributionInfo) DeliverOrder(org.infinispan.remoting.inboundhandler.DeliverOrder) Collections(java.util.Collections) ClusteringConfiguration(org.infinispan.configuration.cache.ClusteringConfiguration) DistributionManager(org.infinispan.distribution.DistributionManager) SuspectException(org.infinispan.remoting.transport.jgroups.SuspectException) Set(java.util.Set) IntSet(org.infinispan.commons.util.IntSet) HashSet(java.util.HashSet) Address(org.infinispan.remoting.transport.Address) IntSet(org.infinispan.commons.util.IntSet) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) SegmentPublisherResult(org.infinispan.reactive.publisher.impl.commands.reduction.SegmentPublisherResult) PublisherResult(org.infinispan.reactive.publisher.impl.commands.reduction.PublisherResult) KeyPublisherResult(org.infinispan.reactive.publisher.impl.commands.reduction.KeyPublisherResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Example 2 with ReductionPublisherRequestCommand

use of org.infinispan.reactive.publisher.impl.commands.reduction.ReductionPublisherRequestCommand in project infinispan by infinispan.

the class ClusterPublisherManagerImpl method startKeyPublisher.

private <I, R> void startKeyPublisher(boolean parallelPublisher, IntSet segments, Set<K> keysToInclude, InvocationContext ctx, long explicitFlags, DeliveryGuarantee deliveryGuarantee, ComposedType<K, I, R> composedType, Function<? super Publisher<I>, ? extends CompletionStage<R>> transformer, Function<? super Publisher<R>, ? extends CompletionStage<R>> finalizer, FlowableProcessor<R> flowableProcessor) {
    LocalizedCacheTopology topology = distributionManager.getCacheTopology();
    Address localAddress = topology.getLocalAddress();
    // This excludes the keys from the various address targets
    Map<Address, Set<K>> keyTargets = determineKeyTargets(topology, keysToInclude, localAddress, segments, ctx);
    int keyTargetSize = keyTargets.size();
    if (keyTargetSize == 0) {
        handleNoTargets(transformer, flowableProcessor);
        return;
    }
    AtomicInteger parallelCount;
    boolean useContext = ctx != null && ctx.lookedUpEntriesCount() > 0;
    if (useContext) {
        parallelCount = new AtomicInteger(keyTargetSize + 1);
    } else {
        parallelCount = new AtomicInteger(keyTargetSize);
    }
    // This way we only have to allocate 1 per request chain
    BiConsumer<PublisherResult<R>, Throwable> biConsumer = new KeyBiConsumer<>(flowableProcessor, parallelCount, topology.getTopologyId(), parallelPublisher, explicitFlags, deliveryGuarantee, composedType, transformer, finalizer);
    Set<K> localKeys = keyTargets.remove(localAddress);
    // If any targets left, they are all remote
    if (!keyTargets.isEmpty()) {
        // We submit the remote ones first as they will not block at all, just to send remote tasks
        for (Map.Entry<Address, Set<K>> remoteTarget : keyTargets.entrySet()) {
            Address remoteAddress = remoteTarget.getKey();
            Set<K> remoteKeys = remoteTarget.getValue();
            ReductionPublisherRequestCommand<K> command = composedType.remoteInvocation(parallelPublisher, null, remoteKeys, null, explicitFlags, deliveryGuarantee, transformer, finalizer);
            command.setTopologyId(topology.getTopologyId());
            CompletionStage<PublisherResult<R>> stage = rpcManager.invokeCommand(remoteAddress, command, new KeyPublisherResultCollector<>(remoteKeys), rpcManager.getSyncRpcOptions());
            stage.whenComplete(biConsumer);
        }
    }
    if (localKeys != null) {
        CompletionStage<PublisherResult<R>> localStage = composedType.localInvocation(parallelPublisher, null, localKeys, null, explicitFlags, deliveryGuarantee, transformer, finalizer);
        if (log.isTraceEnabled()) {
            // Make sure the trace occurs before response is processed
            localStage = localStage.whenComplete((results, t) -> {
                if (t != null) {
                    log.tracef(t, "Received exception while processing keys %s from %s", localKeys, localAddress);
                } else {
                    log.tracef("Result was: %s for keys %s from %s with %s suspected keys", results.getResult(), localKeys, localAddress, results.getSuspectedKeys());
                }
            });
        }
        // Map to the same collector, so we can reuse the same BiConsumer
        localStage.whenComplete(biConsumer);
    }
    if (useContext) {
        handleContextInvocation(segments, keysToInclude, ctx, composedType, transformer, biConsumer);
    }
}
Also used : IntSets(org.infinispan.commons.util.IntSets) RxJavaInterop(org.infinispan.reactive.RxJavaInterop) LogFactory(org.infinispan.util.logging.LogFactory) BiFunction(java.util.function.BiFunction) PersistenceConfiguration(org.infinispan.configuration.cache.PersistenceConfiguration) Stop(org.infinispan.factories.annotations.Stop) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) Scopes(org.infinispan.factories.scopes.Scopes) NextPublisherCommand(org.infinispan.reactive.publisher.impl.commands.batch.NextPublisherCommand) InvocationContext(org.infinispan.context.InvocationContext) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) PublisherResponse(org.infinispan.reactive.publisher.impl.commands.batch.PublisherResponse) Scope(org.infinispan.factories.scopes.Scope) RpcOptions(org.infinispan.remoting.rpc.RpcOptions) InitialPublisherCommand(org.infinispan.reactive.publisher.impl.commands.batch.InitialPublisherCommand) SegmentPublisherResult(org.infinispan.reactive.publisher.impl.commands.reduction.SegmentPublisherResult) CommandsFactory(org.infinispan.commands.CommandsFactory) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ValidResponseCollector(org.infinispan.remoting.transport.ValidResponseCollector) Set(java.util.Set) MarshallableFunctions(org.infinispan.marshall.core.MarshallableFunctions) UnicastProcessor(io.reactivex.rxjava3.processors.UnicastProcessor) Objects(java.util.Objects) IntSet(org.infinispan.commons.util.IntSet) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) CancelPublisherCommand(org.infinispan.reactive.publisher.impl.commands.batch.CancelPublisherCommand) ReductionPublisherRequestCommand(org.infinispan.reactive.publisher.impl.commands.reduction.ReductionPublisherRequestCommand) SerializableFunction(org.infinispan.util.function.SerializableFunction) PersistenceManager(org.infinispan.persistence.manager.PersistenceManager) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ComponentRegistry(org.infinispan.factories.ComponentRegistry) ObjIntConsumer(java.util.function.ObjIntConsumer) Function(java.util.function.Function) Supplier(java.util.function.Supplier) StateTransferLock(org.infinispan.statetransfer.StateTransferLock) ArrayList(java.util.ArrayList) Start(org.infinispan.factories.annotations.Start) PublisherResult(org.infinispan.reactive.publisher.impl.commands.reduction.PublisherResult) HashSet(java.util.HashSet) StoreConfiguration(org.infinispan.configuration.cache.StoreConfiguration) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) SingleResponseCollector(org.infinispan.remoting.transport.impl.SingleResponseCollector) Log(org.infinispan.util.logging.Log) BiConsumer(java.util.function.BiConsumer) VoidResponseCollector(org.infinispan.remoting.transport.impl.VoidResponseCollector) StoreChangeListener(org.infinispan.persistence.manager.PersistenceManager.StoreChangeListener) Subscriber(org.reactivestreams.Subscriber) Address(org.infinispan.remoting.transport.Address) KeyPublisherResult(org.infinispan.reactive.publisher.impl.commands.reduction.KeyPublisherResult) CacheException(org.infinispan.commons.CacheException) Flowable(io.reactivex.rxjava3.core.Flowable) Iterator(java.util.Iterator) CacheEntry(org.infinispan.container.entries.CacheEntry) Publisher(org.reactivestreams.Publisher) Util(org.infinispan.commons.util.Util) RpcManager(org.infinispan.remoting.rpc.RpcManager) ValidResponse(org.infinispan.remoting.responses.ValidResponse) Inject(org.infinispan.factories.annotations.Inject) FlowableProcessor(io.reactivex.rxjava3.processors.FlowableProcessor) PrimitiveIterator(java.util.PrimitiveIterator) TimeUnit(java.util.concurrent.TimeUnit) IllegalLifecycleStateException(org.infinispan.commons.IllegalLifecycleStateException) AbstractMap(java.util.AbstractMap) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) Configuration(org.infinispan.configuration.cache.Configuration) DistributionInfo(org.infinispan.distribution.DistributionInfo) DeliverOrder(org.infinispan.remoting.inboundhandler.DeliverOrder) Collections(java.util.Collections) ClusteringConfiguration(org.infinispan.configuration.cache.ClusteringConfiguration) DistributionManager(org.infinispan.distribution.DistributionManager) SuspectException(org.infinispan.remoting.transport.jgroups.SuspectException) Set(java.util.Set) IntSet(org.infinispan.commons.util.IntSet) HashSet(java.util.HashSet) Address(org.infinispan.remoting.transport.Address) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) SegmentPublisherResult(org.infinispan.reactive.publisher.impl.commands.reduction.SegmentPublisherResult) PublisherResult(org.infinispan.reactive.publisher.impl.commands.reduction.PublisherResult) KeyPublisherResult(org.infinispan.reactive.publisher.impl.commands.reduction.KeyPublisherResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Aggregations

Flowable (io.reactivex.rxjava3.core.Flowable)2 FlowableProcessor (io.reactivex.rxjava3.processors.FlowableProcessor)2 UnicastProcessor (io.reactivex.rxjava3.processors.UnicastProcessor)2 MethodHandles (java.lang.invoke.MethodHandles)2 AbstractMap (java.util.AbstractMap)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 PrimitiveIterator (java.util.PrimitiveIterator)2 Set (java.util.Set)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 TimeUnit (java.util.concurrent.TimeUnit)2