Search in sources :

Example 6 with SuccessfulResponse

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();
        }
    });
}
Also used : SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) SingletonMapResponseCollector(org.infinispan.remoting.transport.impl.SingletonMapResponseCollector)

Example 7 with SuccessfulResponse

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);
    });
}
Also used : SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) RemoteMetadata(org.infinispan.container.entries.RemoteMetadata) Address(org.infinispan.remoting.transport.Address) HashMap(java.util.HashMap) RemoteMetadata(org.infinispan.container.entries.RemoteMetadata) Metadata(org.infinispan.metadata.Metadata) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) Response(org.infinispan.remoting.responses.Response) PutMapCommand(org.infinispan.commands.write.PutMapCommand) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with SuccessfulResponse

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);
}
Also used : Response(org.infinispan.remoting.responses.Response) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) Address(org.infinispan.remoting.transport.Address) ReplicableCommand(org.infinispan.commands.ReplicableCommand)

Example 9 with SuccessfulResponse

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()));
        }
    });
}
Also used : SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) Address(org.infinispan.remoting.transport.Address) ValidResponse(org.infinispan.remoting.responses.ValidResponse) ClusterListenerReplicateCallable(org.infinispan.notifications.cachelistener.cluster.ClusterListenerReplicateCallable) CacheRpcCommand(org.infinispan.commands.remote.CacheRpcCommand)

Example 10 with SuccessfulResponse

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;
    });
}
Also used : Response(org.infinispan.remoting.responses.Response) CacheNotFoundResponse(org.infinispan.remoting.responses.CacheNotFoundResponse) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) CacheNotFoundResponse(org.infinispan.remoting.responses.CacheNotFoundResponse) CacheException(org.infinispan.commons.CacheException) IntSet(org.infinispan.commons.util.IntSet) CacheRpcCommand(org.infinispan.commands.remote.CacheRpcCommand) CacheException(org.infinispan.commons.CacheException) CancellationException(java.util.concurrent.CancellationException) SuspectException(org.infinispan.remoting.transport.jgroups.SuspectException) SuspectException(org.infinispan.remoting.transport.jgroups.SuspectException)

Aggregations

SuccessfulResponse (org.infinispan.remoting.responses.SuccessfulResponse)22 Address (org.infinispan.remoting.transport.Address)16 Response (org.infinispan.remoting.responses.Response)15 Map (java.util.Map)11 HashMap (java.util.HashMap)8 CacheException (org.infinispan.commons.CacheException)7 CacheEntry (org.infinispan.container.entries.CacheEntry)7 InternalCacheValue (org.infinispan.container.entries.InternalCacheValue)7 InternalCacheEntry (org.infinispan.container.entries.InternalCacheEntry)6 CacheNotFoundResponse (org.infinispan.remoting.responses.CacheNotFoundResponse)6 UnsureResponse (org.infinispan.remoting.responses.UnsureResponse)6 ValidResponse (org.infinispan.remoting.responses.ValidResponse)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 CompletionStage (java.util.concurrent.CompletionStage)5 NullCacheEntry (org.infinispan.container.entries.NullCacheEntry)5 LocalizedCacheTopology (org.infinispan.distribution.LocalizedCacheTopology)5 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 ReadOnlyKeyCommand (org.infinispan.commands.functional.ReadOnlyKeyCommand)4