Beispiel #1
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;
  }