use of org.infinispan.remoting.responses.SuccessfulResponse in project infinispan by infinispan.
the class NonTxDistributionInterceptor method handleRemoteSegmentsForReadWriteManyCommand.
private <C extends WriteCommand, Item> void handleRemoteSegmentsForReadWriteManyCommand(C command, WriteManyCommandHelper<C, ?, Item> helper, MergingCompletableFuture<Object> allFuture, MutableInt offset, Address member, IntSet segments, LocalizedCacheTopology topology) {
final int myOffset = offset.value;
// TODO: here we iterate through all entries - is the ReadOnlySegmentAwareMap really worth it?
C copy = helper.copyForPrimary(command, topology, segments);
copy.setTopologyId(command.getTopologyId());
int size = helper.getItems(copy).size();
offset.value += size;
if (size <= 0) {
allFuture.countDown();
return;
}
// Send the command to primary owner
SingletonMapResponseCollector collector = SingletonMapResponseCollector.validOnly();
rpcManager.invokeCommand(member, copy, collector, rpcManager.getSyncRpcOptions()).whenComplete((responses, throwable) -> {
if (throwable != null) {
allFuture.completeExceptionally(throwable);
} else {
// FIXME Dan: The response cannot be a CacheNotFoundResponse at this point
SuccessfulResponse response = getSuccessfulResponseOrFail(responses, allFuture, rsp -> allFuture.completeExceptionally(OutdatedTopologyException.RETRY_NEXT_TOPOLOGY));
if (response == null) {
return;
}
Object responseValue = response.getResponseValue();
MergingCompletableFuture.moveListItemsToFuture(responseValue, allFuture, myOffset);
allFuture.countDown();
}
});
}
use of org.infinispan.remoting.responses.SuccessfulResponse in project infinispan by infinispan.
the class PrefetchInterceptor method retrieveRemoteValues.
// TODO: this is not completely aligned with single-entry prefetch
private <C extends VisitableCommand & TopologyAffectedCommand> InvocationStage retrieveRemoteValues(InvocationContext ctx, C originCommand, List<?> keys) {
if (log.isTraceEnabled()) {
log.tracef("Prefetching entries for keys %s using broadcast", keys);
}
ClusteredGetAllCommand<?, ?> command = commandsFactory.buildClusteredGetAllCommand(keys, FlagBitSets.SKIP_OWNERSHIP_CHECK, null);
command.setTopologyId(originCommand.getTopologyId());
CompletionStage<Map<Address, Response>> rpcFuture = rpcManager.invokeCommandOnAll(command, MapResponseCollector.ignoreLeavers(), rpcManager.getSyncRpcOptions());
return asyncValue(rpcFuture).thenApplyMakeStage(ctx, originCommand, (rCtx, topologyAffectedCommand, rv) -> {
Map<Address, Response> responseMap = (Map<Address, Response>) rv;
InternalCacheValue<V>[] maxValues = new InternalCacheValue[keys.size()];
for (Response response : responseMap.values()) {
if (!response.isSuccessful()) {
throw OutdatedTopologyException.RETRY_NEXT_TOPOLOGY;
}
InternalCacheValue<V>[] values = ((SuccessfulResponse<InternalCacheValue<V>[]>) response).getResponseValue();
int i = 0;
for (InternalCacheValue<V> icv : values) {
if (icv != null) {
Metadata metadata = icv.getMetadata();
if (metadata instanceof RemoteMetadata) {
// not sure if this can happen, but let's be on the safe side
throw OutdatedTopologyException.RETRY_NEXT_TOPOLOGY;
}
if (maxValues[i] == null) {
maxValues[i] = icv;
} else if (metadata != null && metadata.version() != null) {
Metadata maxMetadata;
if ((maxMetadata = maxValues[i].getMetadata()) == null || maxMetadata.version() == null || maxMetadata.version().compareTo(metadata.version()) == InequalVersionComparisonResult.BEFORE) {
maxValues[i] = icv;
}
}
}
++i;
}
}
Map<Object, InternalCacheValue<V>> map = new HashMap<>(keys.size());
for (int i = 0; i < maxValues.length; ++i) {
if (maxValues[i] != null) {
map.put(keys.get(i), maxValues[i]);
}
}
if (log.isTraceEnabled()) {
log.tracef("Prefetched values are %s", map);
}
if (map.isEmpty()) {
return CompletableFutures.completedNull();
}
// from the main command correct.
for (Map.Entry<Object, InternalCacheValue<V>> entry : map.entrySet()) {
entryFactory.wrapExternalEntry(rCtx, entry.getKey(), entry.getValue().toInternalCacheEntry(entry.getKey()), true, true);
}
PutMapCommand putMapCommand = commandsFactory.buildPutMapCommand(map, null, STATE_TRANSFER_FLAGS);
putMapCommand.setTopologyId(topologyAffectedCommand.getTopologyId());
return invokeNext(rCtx, putMapCommand);
});
}
use of org.infinispan.remoting.responses.SuccessfulResponse in project infinispan by infinispan.
the class RpcManagerCustomReplicableCommandTest method testInvokeRemotelyWithResponseMode.
/**
* Test to make sure that invokeRemotely with a ResponseMode argument returns the result from the remote side.
*/
public void testInvokeRemotelyWithResponseMode() {
RpcManager rpcManager = cache(0, "testCache").getAdvancedCache().getRpcManager();
ReplicableCommand command = createReplicableCommandForTest(EXPECTED_RETURN_VALUE);
Map<Address, Response> remoteResponses = invoke(rpcManager, command);
log.tracef("Responses were: %s", remoteResponses);
assertEquals(1, remoteResponses.size());
Response response = remoteResponses.values().iterator().next();
assertNotNull(response);
assertTrue(response.isValid());
assertTrue(response.isSuccessful());
assertTrue(response instanceof SuccessfulResponse);
Object value = ((SuccessfulResponse) response).getResponseValue();
assertEquals(EXPECTED_RETURN_VALUE, value);
}
use of org.infinispan.remoting.responses.SuccessfulResponse in project infinispan by infinispan.
the class StateConsumerImpl method getClusterListeners.
private CompletionStage<Collection<ClusterListenerReplicateCallable<Object, Object>>> getClusterListeners(int topologyId, List<Address> sources) {
// Try the first member. If the request fails, fall back to the second member and so on.
if (sources.isEmpty()) {
if (// TODO Ignore self again
log.isTraceEnabled())
log.trace("Unable to acquire cluster listeners from other members, assuming none are present");
return CompletableFuture.completedFuture(Collections.emptySet());
}
Address source = sources.get(0);
// Don't send the request to self
if (sources.get(0).equals(rpcManager.getAddress())) {
return getClusterListeners(topologyId, sources.subList(1, sources.size()));
}
if (log.isTraceEnabled())
log.tracef("Requesting cluster listeners of cache %s from node %s", cacheName, sources);
CacheRpcCommand cmd = commandsFactory.buildStateTransferGetListenersCommand(topologyId);
CompletionStage<ValidResponse> remoteStage = rpcManager.invokeCommand(source, cmd, SingleResponseCollector.validOnly(), rpcOptions);
return handleAndCompose(remoteStage, (response, throwable) -> {
if (throwable != null) {
log.exceptionDuringClusterListenerRetrieval(source, throwable);
}
if (response instanceof SuccessfulResponse) {
return CompletableFuture.completedFuture((Collection<ClusterListenerReplicateCallable<Object, Object>>) response.getResponseValue());
} else {
log.unsuccessfulResponseForClusterListeners(source, response);
return getClusterListeners(topologyId, sources.subList(1, sources.size()));
}
});
}
use of org.infinispan.remoting.responses.SuccessfulResponse in project infinispan by infinispan.
the class InboundTransferTask method startTransfer.
/**
* Request the segments from the source
*
* @return A {@code CompletionStage} that completes when the segments have been applied
*/
private CompletionStage<Void> startTransfer(Function<IntSet, CacheRpcCommand> transferCommand) {
if (isCancelled)
return completionFuture;
IntSet segmentsCopy = getSegments();
if (segmentsCopy.isEmpty()) {
if (log.isTraceEnabled())
log.tracef("Segments list is empty, skipping source %s", source);
completionFuture.complete(null);
return completionFuture;
}
CacheRpcCommand cmd = transferCommand.apply(segmentsCopy);
if (log.isTraceEnabled()) {
log.tracef("Requesting state (%s) from node %s for segments %s", cmd, source, segmentsCopy);
}
CompletionStage<Response> remoteStage = rpcManager.invokeCommand(source, cmd, PassthroughSingleResponseCollector.INSTANCE, rpcOptions);
return handleAndCompose(remoteStage, (response, throwable) -> {
if (throwable != null) {
if (!isCancelled) {
log.failedToRequestSegments(cacheName, source, segmentsCopy, throwable);
completionFuture.completeExceptionally(throwable);
}
} else if (response instanceof SuccessfulResponse) {
if (log.isTraceEnabled()) {
log.tracef("Successfully requested state (%s) from node %s for segments %s", cmd, source, segmentsCopy);
}
} else if (response instanceof CacheNotFoundResponse) {
if (log.isTraceEnabled())
log.tracef("State source %s was suspected, another source will be selected", source);
completionFuture.completeExceptionally(new SuspectException());
} else {
Exception e = new CacheException(String.valueOf(response));
log.failedToRequestSegments(cacheName, source, segmentsCopy, e);
completionFuture.completeExceptionally(e);
}
return completionFuture;
});
}
Aggregations