public InternalCacheEntry retrieveFromRemoteSource(Object key, InvocationContext ctx)
      throws Exception {
    ClusteredGetCommand get = cf.buildClusteredGetCommand(key, ctx.getFlags());

    List<Address> targets = locate(key);
    targets.remove(getSelf());
    ResponseFilter filter = new ClusteredGetResponseValidityFilter(targets);
    Map<Address, Response> responses =
        rpcManager.invokeRemotely(
            targets,
            get,
            ResponseMode.SYNCHRONOUS,
            configuration.getSyncReplTimeout(),
            false,
            filter);

    if (!responses.isEmpty()) {
      for (Response r : responses.values()) {
        if (r instanceof SuccessfulResponse) {
          InternalCacheValue cacheValue =
              (InternalCacheValue) ((SuccessfulResponse) r).getResponseValue();
          return cacheValue.toInternalCacheEntry(key);
        }
      }
    }

    return null;
  }
 private InternalCacheEntry unmarshall(Object o, Object key)
     throws IOException, ClassNotFoundException {
   if (o == null) return null;
   byte b[] = (byte[]) o;
   InternalCacheValue v = (InternalCacheValue) getMarshaller().objectFromByteBuffer(b);
   return v.toInternalCacheEntry(key);
 }
Beispiel #3
0
  @Override
  public MarshalledEntry load(Object key) throws PersistenceException {
    if (!isCacheReady()) return null;

    ClusteredGetCommand clusteredGetCommand =
        new ClusteredGetCommand(
            key,
            cache.getName(),
            EnumUtil.EMPTY_BIT_SET,
            false,
            null,
            cache.getCacheConfiguration().dataContainer().keyEquivalence());

    Collection<Response> responses = doRemoteCall(clusteredGetCommand);
    if (responses.isEmpty()) return null;

    Response response;
    if (responses.size() > 1) {
      // Remove duplicates before deciding if multiple responses were received
      Set<Response> setResponses = new HashSet<>(responses);
      if (setResponses.size() > 1)
        throw new PersistenceException(
            String.format(
                "Responses contains more than 1 element and these elements are not equal, so can't decide which one to use: %s",
                setResponses));
      response = setResponses.iterator().next();
    } else {
      response = responses.iterator().next();
    }

    if (response.isSuccessful() && response instanceof SuccessfulResponse) {
      InternalCacheValue value =
          (InternalCacheValue) ((SuccessfulResponse) response).getResponseValue();
      return value == null
          ? null
          : ctx.getMarshalledEntryFactory().newMarshalledEntry(key, value.getValue(), null);
    }

    log.unknownResponsesFromRemoteCache(responses);
    throw new PersistenceException("Unknown responses");
  }
  private Map<Object, InternalCacheValue> applyStateMap(
      Map<Object, InternalCacheValue> state, boolean withRetry) {
    Map<Object, InternalCacheValue> retry =
        withRetry ? new HashMap<Object, InternalCacheValue>() : null;

    for (Map.Entry<Object, InternalCacheValue> e : state.entrySet()) {
      InternalCacheValue v = e.getValue();
      InvocationContext ctx = icc.createInvocationContext();
      // locking not necessary in the case of a join since the node isn't doing anything else
      // TODO what if the node is already running?
      ctx.setFlags(
          CACHE_MODE_LOCAL,
          SKIP_CACHE_LOAD,
          SKIP_REMOTE_LOOKUP,
          SKIP_SHARED_CACHE_STORE,
          SKIP_LOCKING,
          SKIP_OWNERSHIP_CHECK);
      try {
        PutKeyValueCommand put =
            cf.buildPutKeyValueCommand(
                e.getKey(), v.getValue(), v.getLifespan(), v.getMaxIdle(), ctx.getFlags());
        interceptorChain.invoke(ctx, put);
      } catch (Exception ee) {
        if (withRetry) {
          if (trace)
            log.tracef(
                "Problem %s encountered when applying state for key %s. Adding entry to retry queue.",
                ee.getMessage(), e.getKey());
          retry.put(e.getKey(), e.getValue());
        } else {
          log.problemApplyingStateForKey(ee.getMessage(), e.getKey());
        }
      }
    }
    return retry;
  }
  public InternalCacheEntry load(Object key) throws CacheLoaderException {
    if (!(isCacheReady() && isLocalCall())) return null;
    ClusteredGetCommand clusteredGetCommand = new ClusteredGetCommand(key, cache.getName());
    Collection<Response> responses = doRemoteCall(clusteredGetCommand);
    if (responses.isEmpty()) return null;

    // Remove duplicates before deciding if multiple responses were received
    Set<Response> setResponses = new HashSet(responses);
    if (setResponses.size() > 1)
      throw new CacheLoaderException(
          String.format(
              "Responses contains more than 1 element and these elements are not equal, so can't decide which one to use: %s",
              setResponses));

    for (Response r : setResponses) {
      if (r.isSuccessful() && r instanceof SuccessfulResponse) {
        InternalCacheValue value = (InternalCacheValue) ((SuccessfulResponse) r).getResponseValue();
        return value.toInternalCacheEntry(key);
      }
    }
    String message = "Unknown responses from remote cache: " + responses;
    log.error(message);
    throw new CacheLoaderException(message);
  }