Search in sources :

Example 1 with NullCacheEntry

use of org.infinispan.container.entries.NullCacheEntry in project infinispan by infinispan.

the class ScatteredDistributionInterceptor method visitReadOnlyKeyCommand.

@Override
public Object visitReadOnlyKeyCommand(InvocationContext ctx, ReadOnlyKeyCommand command) throws Throwable {
    Object key = command.getKey();
    CacheEntry entry = ctx.lookupEntry(key);
    if (entry != null) {
        // the entry is owned locally (it is NullCacheEntry if it was not found), no need to go remote
        return invokeNext(ctx, command);
    }
    if (!ctx.isOriginLocal()) {
        return UnsureResponse.INSTANCE;
    }
    if (isLocalModeForced(command) || command.hasAnyFlag(FlagBitSets.SKIP_REMOTE_LOOKUP)) {
        if (ctx.lookupEntry(command.getKey()) == null) {
            entryFactory.wrapExternalEntry(ctx, command.getKey(), NullCacheEntry.getInstance(), false, false);
        }
        return invokeNext(ctx, command);
    }
    DistributionInfo info = checkTopology(command).getDistribution(command.getKey());
    if (info.primary() == null) {
        throw AllOwnersLostException.INSTANCE;
    }
    ResponseCollector<Response> collector = PassthroughSingleResponseCollector.INSTANCE;
    CompletionStage<Response> rpc = rpcManager.invokeCommand(info.primary(), command, collector, rpcManager.getSyncRpcOptions());
    return asyncValue(rpc.thenApply(response -> {
        if (response.isSuccessful()) {
            return ((SuccessfulResponse) response).getResponseValue();
        } else if (response instanceof UnsureResponse) {
            throw OutdatedTopologyException.RETRY_NEXT_TOPOLOGY;
        } else if (response instanceof CacheNotFoundResponse) {
            throw AllOwnersLostException.INSTANCE;
        } else if (response instanceof ExceptionResponse) {
            throw ResponseCollectors.wrapRemoteException(info.primary(), ((ExceptionResponse) response).getException());
        } else {
            throw new IllegalArgumentException("Unexpected response " + response);
        }
    }));
}
Also used : CacheNotFoundResponse(org.infinispan.remoting.responses.CacheNotFoundResponse) UnsuccessfulResponse(org.infinispan.remoting.responses.UnsuccessfulResponse) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) ExceptionResponse(org.infinispan.remoting.responses.ExceptionResponse) ValidResponse(org.infinispan.remoting.responses.ValidResponse) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) Response(org.infinispan.remoting.responses.Response) WriteCommand(org.infinispan.commands.write.WriteCommand) Arrays(java.util.Arrays) GetCacheEntryCommand(org.infinispan.commands.read.GetCacheEntryCommand) CacheNotFoundResponse(org.infinispan.remoting.responses.CacheNotFoundResponse) LogFactory(org.infinispan.util.logging.LogFactory) RemoteMetadata(org.infinispan.container.entries.RemoteMetadata) ClusteredGetAllCommand(org.infinispan.commands.remote.ClusteredGetAllCommand) Map(java.util.Map) RemoveCommand(org.infinispan.commands.write.RemoveCommand) ConsistentHash(org.infinispan.distribution.ch.ConsistentHash) EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) ReadWriteManyEntriesCommand(org.infinispan.commands.functional.ReadWriteManyEntriesCommand) UnsuccessfulResponse(org.infinispan.remoting.responses.UnsuccessfulResponse) Set(java.util.Set) GroupManager(org.infinispan.distribution.group.impl.GroupManager) CacheTopology(org.infinispan.topology.CacheTopology) ResponseCollector(org.infinispan.remoting.transport.ResponseCollector) DONT_LOAD(org.infinispan.commands.VisitableCommand.LoadType.DONT_LOAD) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) AbstractVisitor(org.infinispan.commands.AbstractVisitor) ArrayCollector(org.infinispan.commons.util.ArrayCollector) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) CacheEntryRemoved(org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved) PersistenceManager(org.infinispan.persistence.manager.PersistenceManager) ComputeCommand(org.infinispan.commands.write.ComputeCommand) MetadataAwareCommand(org.infinispan.commands.MetadataAwareCommand) EvictionManager(org.infinispan.eviction.EvictionManager) ReadOnlyKeyCommand(org.infinispan.commands.functional.ReadOnlyKeyCommand) ArrayList(java.util.ArrayList) WriteOnlyManyEntriesCommand(org.infinispan.commands.functional.WriteOnlyManyEntriesCommand) Start(org.infinispan.factories.annotations.Start) MapResponseCollector(org.infinispan.remoting.transport.impl.MapResponseCollector) PassthroughSingleResponseCollector(org.infinispan.remoting.transport.impl.PassthroughSingleResponseCollector) SingleResponseCollector(org.infinispan.remoting.transport.impl.SingleResponseCollector) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand) FunctionalNotifier(org.infinispan.functional.impl.FunctionalNotifier) ValueMatcher(org.infinispan.commands.write.ValueMatcher) ClearCommand(org.infinispan.commands.write.ClearCommand) Address(org.infinispan.remoting.transport.Address) ExceptionResponse(org.infinispan.remoting.responses.ExceptionResponse) CacheEntry(org.infinispan.container.entries.CacheEntry) NotifyHelper(org.infinispan.notifications.cachelistener.NotifyHelper) WriteOnlyKeyValueCommand(org.infinispan.commands.functional.WriteOnlyKeyValueCommand) ValidResponse(org.infinispan.remoting.responses.ValidResponse) CacheNotifier(org.infinispan.notifications.cachelistener.CacheNotifier) Inject(org.infinispan.factories.annotations.Inject) ReadWriteKeyCommand(org.infinispan.commands.functional.ReadWriteKeyCommand) ReadWriteManyCommand(org.infinispan.commands.functional.ReadWriteManyCommand) NullCacheEntry(org.infinispan.container.entries.NullCacheEntry) AggregateCompletionStage(org.infinispan.util.concurrent.AggregateCompletionStage) ReplaceCommand(org.infinispan.commands.write.ReplaceCommand) EntryVersion(org.infinispan.container.versioning.EntryVersion) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) MultiTargetCollector(org.infinispan.util.concurrent.CommandAckCollector.MultiTargetCollector) GetKeysInGroupCommand(org.infinispan.commands.remote.GetKeysInGroupCommand) ActivationManager(org.infinispan.eviction.impl.ActivationManager) RepeatableReadEntry(org.infinispan.container.entries.RepeatableReadEntry) InvocationContext(org.infinispan.context.InvocationContext) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) OutdatedTopologyException(org.infinispan.statetransfer.OutdatedTopologyException) RpcOptions(org.infinispan.remoting.rpc.RpcOptions) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ReadWriteKeyValueCommand(org.infinispan.commands.functional.ReadWriteKeyValueCommand) MetadataImmortalCacheEntry(org.infinispan.container.entries.metadata.MetadataImmortalCacheEntry) DataOperationOrderer(org.infinispan.util.concurrent.DataOperationOrderer) InequalVersionComparisonResult(org.infinispan.container.versioning.InequalVersionComparisonResult) InvocationSuccessFunction(org.infinispan.interceptors.InvocationSuccessFunction) List(java.util.List) SingletonMapResponseCollector(org.infinispan.remoting.transport.impl.SingletonMapResponseCollector) InvocationFinallyAction(org.infinispan.interceptors.InvocationFinallyAction) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) FlagAffectedCommand(org.infinispan.commands.FlagAffectedCommand) TopologyAffectedCommand(org.infinispan.commands.TopologyAffectedCommand) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Metadata(org.infinispan.metadata.Metadata) CompletionStages(org.infinispan.util.concurrent.CompletionStages) GetAllCommand(org.infinispan.commands.read.GetAllCommand) Function(java.util.function.Function) AllOwnersLostException(org.infinispan.statetransfer.AllOwnersLostException) FlagBitSets(org.infinispan.context.impl.FlagBitSets) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) ClusteredGetCommand(org.infinispan.commands.remote.ClusteredGetCommand) IracPutKeyValueCommand(org.infinispan.commands.write.IracPutKeyValueCommand) Log(org.infinispan.util.logging.Log) AbstractDataCommand(org.infinispan.commands.read.AbstractDataCommand) SegmentSpecificCommand(org.infinispan.commands.SegmentSpecificCommand) MetadataImmortalCacheValue(org.infinispan.container.entries.metadata.MetadataImmortalCacheValue) ResponseCollectors(org.infinispan.remoting.transport.ResponseCollectors) Response(org.infinispan.remoting.responses.Response) ClusteringInterceptor(org.infinispan.interceptors.impl.ClusteringInterceptor) GetKeyValueCommand(org.infinispan.commands.read.GetKeyValueCommand) CacheException(org.infinispan.commons.CacheException) Iterator(java.util.Iterator) EvictCommand(org.infinispan.commands.write.EvictCommand) WriteOnlyManyCommand(org.infinispan.commands.functional.WriteOnlyManyCommand) PutMapCommand(org.infinispan.commands.write.PutMapCommand) ReadOnlyManyCommand(org.infinispan.commands.functional.ReadOnlyManyCommand) ComputeIfAbsentCommand(org.infinispan.commands.write.ComputeIfAbsentCommand) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) Configuration(org.infinispan.configuration.cache.Configuration) DistributionInfo(org.infinispan.distribution.DistributionInfo) Operation(org.infinispan.util.concurrent.DataOperationOrderer.Operation) WriteOnlyKeyCommand(org.infinispan.commands.functional.WriteOnlyKeyCommand) VisitableCommand(org.infinispan.commands.VisitableCommand) DeliverOrder(org.infinispan.remoting.inboundhandler.DeliverOrder) Collections(java.util.Collections) TimeService(org.infinispan.commons.time.TimeService) ScatteredVersionManager(org.infinispan.scattered.ScatteredVersionManager) ExceptionResponse(org.infinispan.remoting.responses.ExceptionResponse) CacheNotFoundResponse(org.infinispan.remoting.responses.CacheNotFoundResponse) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) CacheEntry(org.infinispan.container.entries.CacheEntry) NullCacheEntry(org.infinispan.container.entries.NullCacheEntry) MetadataImmortalCacheEntry(org.infinispan.container.entries.metadata.MetadataImmortalCacheEntry) DistributionInfo(org.infinispan.distribution.DistributionInfo)

Example 2 with NullCacheEntry

use of org.infinispan.container.entries.NullCacheEntry in project infinispan by infinispan.

the class BaseDistributionInterceptor method visitReadOnlyKeyCommand.

@Override
public Object visitReadOnlyKeyCommand(InvocationContext ctx, ReadOnlyKeyCommand command) throws Throwable {
    // TODO: repeatable-reads are not implemented, these need to keep the read values on remote side for the duration
    // of the transaction, and that requires synchronous invocation of the readonly command on all owners.
    // For better consistency, use versioning and write skew check that will fail the transaction when we apply
    // the function on different version of the entry than the one previously read
    Object key = command.getKey();
    CacheEntry entry = ctx.lookupEntry(key);
    if (entry != null) {
        if (ctx.isOriginLocal()) {
            // the entry is owned locally (it is NullCacheEntry if it was not found), no need to go remote
            return invokeNext(ctx, command);
        } else {
            return invokeNextThenApply(ctx, command, (rCtx, rCommand, rv) -> wrapFunctionalResultOnNonOriginOnReturn(rv, entry));
        }
    }
    if (!ctx.isOriginLocal()) {
        return UnsureResponse.INSTANCE;
    }
    if (readNeedsRemoteValue(command)) {
        LocalizedCacheTopology cacheTopology = checkTopologyId(command);
        Collection<Address> owners = cacheTopology.getDistribution(key).readOwners();
        if (log.isTraceEnabled())
            log.tracef("Doing a remote get for key %s in topology %d to %s", key, cacheTopology.getTopologyId(), owners);
        ReadOnlyKeyCommand remoteCommand = remoteReadOnlyCommand(ctx, command);
        // make sure that the command topology is set to the value according which we route it
        remoteCommand.setTopologyId(cacheTopology.getTopologyId());
        CompletionStage<SuccessfulResponse> rpc = rpcManager.invokeCommandStaggered(owners, remoteCommand, new RemoteGetSingleKeyCollector(), rpcManager.getSyncRpcOptions());
        return asyncValue(rpc).thenApply(ctx, command, (rCtx, rCommand, response) -> {
            Object responseValue = ((SuccessfulResponse) response).getResponseValue();
            return unwrapFunctionalResultOnOrigin(rCtx, rCommand.getKey(), responseValue);
        });
    } else {
        // This has LOCAL flags, just wrap NullCacheEntry and let the command run
        entryFactory.wrapExternalEntry(ctx, key, NullCacheEntry.getInstance(), true, false);
        return invokeNext(ctx, command);
    }
}
Also used : SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) Address(org.infinispan.remoting.transport.Address) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) CacheEntry(org.infinispan.container.entries.CacheEntry) NullCacheEntry(org.infinispan.container.entries.NullCacheEntry) ReadOnlyKeyCommand(org.infinispan.commands.functional.ReadOnlyKeyCommand)

Example 3 with NullCacheEntry

use of org.infinispan.container.entries.NullCacheEntry in project infinispan by infinispan.

the class DefaultConflictManager method doResolveConflicts.

private void doResolveConflicts(final LocalizedCacheTopology topology, final EntryMergePolicy<K, V> mergePolicy, final Set<Address> preferredNodes) {
    boolean userCall = preferredNodes == null;
    final Set<Address> preferredPartition = userCall ? new HashSet<>(topology.getCurrentCH().getMembers()) : preferredNodes;
    if (log.isTraceEnabled())
        log.tracef("Cache %s attempting to resolve conflicts.  All Members %s, Installed topology %s, Preferred Partition %s", cacheName, topology.getMembers(), topology, preferredPartition);
    final Phaser phaser = new Phaser(1);
    getConflicts(topology).forEach(conflictMap -> {
        phaser.register();
        if (log.isTraceEnabled())
            log.tracef("Cache %s conflict detected %s", cacheName, conflictMap);
        Collection<CacheEntry<K, V>> entries = conflictMap.values();
        Optional<K> optionalEntry = entries.stream().filter(entry -> !(entry instanceof NullCacheEntry)).map(CacheEntry::getKey).findAny();
        final K key = optionalEntry.orElseThrow(() -> new CacheException("All returned conflicts are NullCacheEntries. This should not happen!"));
        Address primaryReplica = topology.getDistribution(key).primary();
        List<Address> preferredEntries = conflictMap.entrySet().stream().map(Map.Entry::getKey).filter(preferredPartition::contains).collect(Collectors.toList());
        // If only one entry exists in the preferred partition, then use that entry
        CacheEntry<K, V> preferredEntry;
        if (preferredEntries.size() == 1) {
            preferredEntry = conflictMap.remove(preferredEntries.get(0));
        } else {
            // If multiple conflicts exist in the preferred partition, then use primary replica from the preferred partition
            // If not a merge, then also use primary as preferred entry
            // Preferred is null if no entry exists in preferred partition
            preferredEntry = conflictMap.remove(primaryReplica);
        }
        if (log.isTraceEnabled())
            log.tracef("Cache %s applying EntryMergePolicy %s to PreferredEntry %s, otherEntries %s", cacheName, mergePolicy.getClass().getName(), preferredEntry, entries);
        CacheEntry<K, V> entry = preferredEntry instanceof NullCacheEntry ? null : preferredEntry;
        List<CacheEntry<K, V>> otherEntries = entries.stream().filter(e -> !(e instanceof NullCacheEntry)).collect(Collectors.toList());
        CacheEntry<K, V> mergedEntry = mergePolicy.merge(entry, otherEntries);
        CompletableFuture<V> future;
        future = applyMergeResult(userCall, key, mergedEntry);
        future.whenComplete((responseMap, exception) -> {
            if (log.isTraceEnabled())
                log.tracef("Cache %s resolveConflicts future complete for key %s: ResponseMap=%s", cacheName, key, responseMap);
            phaser.arriveAndDeregister();
            if (exception != null)
                log.exceptionDuringConflictResolution(key, exception);
        });
    });
    phaser.arriveAndAwaitAdvance();
    if (log.isTraceEnabled())
        log.tracef("Cache %s finished resolving conflicts for topologyId=%s", cacheName, topology.getTopologyId());
}
Also used : ComponentName(org.infinispan.factories.annotations.ComponentName) GetCacheEntryCommand(org.infinispan.commands.read.GetCacheEntryCommand) CacheNotFoundResponse(org.infinispan.remoting.responses.CacheNotFoundResponse) LogFactory(org.infinispan.util.logging.LogFactory) Spliterators(java.util.Spliterators) EntryMergePolicyFactoryRegistry(org.infinispan.conflict.EntryMergePolicyFactoryRegistry) TimeoutException(java.util.concurrent.TimeoutException) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) InvocationHelper(org.infinispan.cache.impl.InvocationHelper) KnownComponentNames(org.infinispan.factories.KnownComponentNames) Stop(org.infinispan.factories.annotations.Stop) Scopes(org.infinispan.factories.scopes.Scopes) InvocationContext(org.infinispan.context.InvocationContext) ComponentRef(org.infinispan.factories.impl.ComponentRef) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) Map(java.util.Map) Scope(org.infinispan.factories.scopes.Scope) CommandsFactory(org.infinispan.commands.CommandsFactory) EntryMergePolicy(org.infinispan.conflict.EntryMergePolicy) CancellationException(java.util.concurrent.CancellationException) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) CacheTopology(org.infinispan.topology.CacheTopology) Collectors(java.util.stream.Collectors) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) CLUSTER(org.infinispan.util.logging.Log.CLUSTER) Optional(java.util.Optional) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) Queue(java.util.Queue) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Start(org.infinispan.factories.annotations.Start) MapResponseCollector(org.infinispan.remoting.transport.impl.MapResponseCollector) HashSet(java.util.HashSet) PartitionHandlingConfiguration(org.infinispan.configuration.cache.PartitionHandlingConfiguration) FlagBitSets(org.infinispan.context.impl.FlagBitSets) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) ClusteredGetCommand(org.infinispan.commands.remote.ClusteredGetCommand) Log(org.infinispan.util.logging.Log) StreamSupport(java.util.stream.StreamSupport) Address(org.infinispan.remoting.transport.Address) InvocationContextFactory(org.infinispan.context.InvocationContextFactory) Response(org.infinispan.remoting.responses.Response) InternalEntryFactory(org.infinispan.container.impl.InternalEntryFactory) CacheException(org.infinispan.commons.CacheException) Iterator(java.util.Iterator) CacheEntry(org.infinispan.container.entries.CacheEntry) RpcManager(org.infinispan.remoting.rpc.RpcManager) Inject(org.infinispan.factories.annotations.Inject) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) StateConsumer(org.infinispan.statetransfer.StateConsumer) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) Phaser(java.util.concurrent.Phaser) Configuration(org.infinispan.configuration.cache.Configuration) DistributionInfo(org.infinispan.distribution.DistributionInfo) VisitableCommand(org.infinispan.commands.VisitableCommand) TransactionManager(javax.transaction.TransactionManager) BlockingManager(org.infinispan.util.concurrent.BlockingManager) Collections(java.util.Collections) TimeService(org.infinispan.commons.time.TimeService) NullCacheEntry(org.infinispan.container.entries.NullCacheEntry) DistributionManager(org.infinispan.distribution.DistributionManager) Address(org.infinispan.remoting.transport.Address) CacheException(org.infinispan.commons.CacheException) NullCacheEntry(org.infinispan.container.entries.NullCacheEntry) CacheEntry(org.infinispan.container.entries.CacheEntry) NullCacheEntry(org.infinispan.container.entries.NullCacheEntry) CacheEntry(org.infinispan.container.entries.CacheEntry) NullCacheEntry(org.infinispan.container.entries.NullCacheEntry) Phaser(java.util.concurrent.Phaser)

Aggregations

CacheEntry (org.infinispan.container.entries.CacheEntry)3 NullCacheEntry (org.infinispan.container.entries.NullCacheEntry)3 LocalizedCacheTopology (org.infinispan.distribution.LocalizedCacheTopology)3 SuccessfulResponse (org.infinispan.remoting.responses.SuccessfulResponse)3 Address (org.infinispan.remoting.transport.Address)3 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2 Predicate (java.util.function.Predicate)2 Stream (java.util.stream.Stream)2 VisitableCommand (org.infinispan.commands.VisitableCommand)2 ReadOnlyKeyCommand (org.infinispan.commands.functional.ReadOnlyKeyCommand)2 GetCacheEntryCommand (org.infinispan.commands.read.GetCacheEntryCommand)2 ClusteredGetCommand (org.infinispan.commands.remote.ClusteredGetCommand)2