use of org.infinispan.remoting.transport.ResponseCollector in project infinispan by infinispan.
the class DistAsyncFuncTest method createCacheManagers.
@Override
protected void createCacheManagers() throws Throwable {
super.createCacheManagers();
r1 = new ReplListener(c1, true, true);
r2 = new ReplListener(c2, true, true);
r3 = new ReplListener(c3, true, true);
r4 = new ReplListener(c4, true, true);
r = new ReplListener[] { r1, r2, r3, r4 };
listenerLookup = new HashMap<>();
for (ReplListener rl : r) listenerLookup.put(rl.getCache().getCacheManager().getAddress(), rl);
for (Cache c : caches) {
TestingUtil.wrapComponent(c, RpcManager.class, original -> new AbstractDelegatingRpcManager(original) {
@Override
protected <T> CompletionStage<T> performRequest(Collection<Address> targets, ReplicableCommand command, ResponseCollector<T> collector, Function<ResponseCollector<T>, CompletionStage<T>> invoker, RpcOptions rpcOptions) {
if (command instanceof SingleRpcCommand) {
command = ((SingleRpcCommand) command).getCommand();
}
if (command instanceof InvalidateL1Command) {
InvalidateL1Command invalidateL1Command = (InvalidateL1Command) command;
log.tracef("Sending invalidation %s to %s", command, targets);
Collection<Address> realTargets = targets != null ? targets : cacheAddresses;
for (Address target : realTargets) {
expectedL1Invalidations.computeIfAbsent(target, ignored -> Collections.synchronizedList(new ArrayList<>())).add(invalidateL1Command);
}
}
return super.performRequest(targets, command, collector, invoker, rpcOptions);
}
});
}
}
use of org.infinispan.remoting.transport.ResponseCollector in project infinispan by infinispan.
the class ScatteredDistributionInterceptor method handleReadCommand.
private Object handleReadCommand(InvocationContext ctx, AbstractDataCommand command) throws Throwable {
LocalizedCacheTopology cacheTopology = checkTopology(command);
// SKIP_OWNERSHIP_CHECK is added when the entry is prefetched from remote node
// TODO [rvansa]: local lookup and hinted read, see improvements in package-info
CacheEntry entry = ctx.lookupEntry(command.getKey());
if (entry != null) {
return invokeNext(ctx, command);
}
DistributionInfo info = cacheTopology.getSegmentDistribution(command.getSegment());
if (info.isPrimary()) {
if (log.isTraceEnabled()) {
log.tracef("In topology %d this is primary owner", cacheTopology.getTopologyId());
}
return invokeNext(ctx, command);
} else if (command.hasAnyFlag(FlagBitSets.SKIP_OWNERSHIP_CHECK)) {
if (log.isTraceEnabled()) {
log.trace("Ignoring ownership");
}
return invokeNext(ctx, command);
} else if (info.primary() == null) {
throw OutdatedTopologyException.RETRY_NEXT_TOPOLOGY;
} else if (ctx.isOriginLocal()) {
if (isLocalModeForced(command) || command.hasAnyFlag(FlagBitSets.SKIP_REMOTE_LOOKUP)) {
entryFactory.wrapExternalEntry(ctx, command.getKey(), NullCacheEntry.getInstance(), false, false);
return invokeNext(ctx, command);
}
ClusteredGetCommand clusteredGetCommand = cf.buildClusteredGetCommand(command.getKey(), info.segmentId(), command.getFlagsBitSet());
clusteredGetCommand.setTopologyId(command.getTopologyId());
ResponseCollector<Response> collector = PassthroughSingleResponseCollector.INSTANCE;
CompletionStage<Response> rpcFuture = rpcManager.invokeCommand(info.primary(), clusteredGetCommand, collector, rpcManager.getSyncRpcOptions());
Object key = clusteredGetCommand.getKey();
return asyncInvokeNext(ctx, command, rpcFuture.thenAccept(response -> {
if (response.isSuccessful()) {
InternalCacheValue value = (InternalCacheValue) ((SuccessfulResponse) response).getResponseValue();
if (value != null) {
InternalCacheEntry cacheEntry = value.toInternalCacheEntry(key);
entryFactory.wrapExternalEntry(ctx, key, cacheEntry, true, false);
} else {
entryFactory.wrapExternalEntry(ctx, key, NullCacheEntry.getInstance(), false, false);
}
} 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);
}
}));
} else {
return UnsureResponse.INSTANCE;
}
}
use of org.infinispan.remoting.transport.ResponseCollector in project infinispan by infinispan.
the class AllClusterExecutor method executeRunnable.
private CompletableFuture<?> executeRunnable(Runnable runnable) {
CompletableFuture<?> localFuture = startLocalInvocation(runnable);
List<Address> targets = getRealTargets(false);
int size = targets.size();
CompletableFuture<?> remoteFuture;
if (size == 1) {
Address target = targets.get(0);
if (log.isTraceEnabled()) {
log.tracef("Submitting runnable to single remote node - JGroups Address %s", target);
}
remoteFuture = new CompletableFuture<>();
ReplicableCommand command = new ReplicableRunnableCommand(runnable);
CompletionStage<Response> request = transport.invokeCommand(target, command, PassthroughSingleResponseCollector.INSTANCE, DeliverOrder.NONE, time, unit);
request.handle((r, t) -> {
if (t != null) {
remoteFuture.completeExceptionally(t);
} else {
consumeResponse(r, target, remoteFuture::completeExceptionally);
// This won't override exception if there was one
remoteFuture.complete(null);
}
return null;
});
} else if (size > 1) {
remoteFuture = new CompletableFuture<>();
ReplicableCommand command = new ReplicableRunnableCommand(runnable);
ResponseCollector<Map<Address, Response>> collector = new PassthroughMapResponseCollector(targets.size());
CompletionStage<Map<Address, Response>> request = transport.invokeCommand(targets, command, collector, DeliverOrder.NONE, time, unit);
request.handle((r, t) -> {
if (t != null) {
remoteFuture.completeExceptionally(t);
} else {
r.forEach((key, value) -> consumeResponse(value, key, remoteFuture::completeExceptionally));
remoteFuture.complete(null);
}
return null;
});
} else if (localFuture != null) {
return localFuture;
} else {
return CompletableFutures.completedExceptionFuture(new SuspectException("No available nodes!"));
}
// remoteFuture is guaranteed to be non null at this point
if (localFuture != null) {
CompletableFuture<Void> future = new CompletableFuture<>();
CompletableFuture.allOf(localFuture, remoteFuture).whenComplete((v, t) -> {
if (t != null) {
if (t instanceof CompletionException) {
future.completeExceptionally(t.getCause());
} else {
future.completeExceptionally(t);
}
} else {
future.complete(null);
}
});
return future;
}
return remoteFuture;
}
use of org.infinispan.remoting.transport.ResponseCollector 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.remoting.transport.ResponseCollector in project infinispan by infinispan.
the class DefaultIracManager method createAndSendBatch.
private Completable createAndSendBatch(Address dst, Collection<? extends IracManagerKeyState> batch) {
if (log.isTraceEnabled()) {
log.tracef("Sending state response to %s. Batch=%s", dst, Util.toStr(batch));
}
RpcOptions rpcOptions = rpcManager.getSyncRpcOptions();
ResponseCollector<Void> rspCollector = ignoreLeavers();
IracStateResponseCommand cmd = commandsFactory.buildIracStateResponseCommand(batch.size());
for (IracManagerKeyState state : batch) {
IracMetadata tombstone = iracTombstoneManager.getTombstone(state.getKey());
cmd.add(state, tombstone);
}
return Completable.fromCompletionStage(rpcManager.invokeCommand(dst, cmd, rspCollector, rpcOptions).exceptionally(throwable -> {
if (log.isTraceEnabled()) {
log.tracef(throwable, "Batch sent to %s failed! Batch=%s", dst, Util.toStr(batch));
}
return null;
}));
}
Aggregations