use of org.infinispan.commands.functional.ReadOnlyKeyCommand 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);
}
}));
}
use of org.infinispan.commands.functional.ReadOnlyKeyCommand 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);
}
}
Aggregations