Пример #1
0
  public static boolean isCacheViewComplete(
      EmbeddedCacheManager cacheContainer, int memberCount, boolean barfIfTooManyMembers) {
    List<Address> members = cacheContainer.getMembers();
    if (members == null || memberCount > members.size()) {
      return false;
    } else if (memberCount < members.size()) {
      if (barfIfTooManyMembers) {
        // This is an exceptional condition
        StringBuffer sb = new StringBuffer("Cache at address ");
        sb.append(cacheContainer.getAddress());
        sb.append(" had ");
        sb.append(members.size());
        sb.append(" members; expecting ");
        sb.append(memberCount);
        sb.append(". Members were (");
        for (int j = 0; j < members.size(); j++) {
          if (j > 0) {
            sb.append(", ");
          }
          sb.append(members.get(j));
        }
        sb.append(')');

        throw new IllegalStateException(sb.toString());
      }

      return false;
    }

    return true;
  }
 @Override
 public Address getAddress() {
   Address address = cacheManager.getAddress();
   if (address == null) {
     address = LOCAL_MODE_ADDRESS;
   }
   return address;
 }
Пример #3
0
    @Override
    public void run() {
      try {
        int size = cacheManagers.size();
        int index = random.nextInt(size);
        EmbeddedCacheManager cacheManager =
            cacheManagers.remove(
                index); // This is not thread safe, but should be ok for this test since the main
        // thread is the only writrer to this list.

        log.info("Shutting down " + cacheManager.getAddress());
        cacheManager.stop();
        log.info("Shut down " + cacheManager.getAddress() + " complete");
      } catch (Exception e) {
        log.warn("Error during node removal", e);
      }
    }
Пример #4
0
 private static void removeInMemoryData(Cache cache) {
   EmbeddedCacheManager mgr = cache.getCacheManager();
   Address a = mgr.getAddress();
   String str;
   if (a == null) str = "a non-clustered cache manager";
   else str = "a cache manager at address " + a;
   log.debugf("Cleaning data for cache '%s' on %s", cache.getName(), str);
   DataContainer dataContainer = TestingUtil.extractComponent(cache, DataContainer.class);
   log.debugf("removeInMemoryData(): dataContainerBefore == %s", dataContainer.entrySet());
   dataContainer.clear();
   log.debugf("removeInMemoryData(): dataContainerAfter == %s", dataContainer.entrySet());
 }
 public static void dumpCacheContents(List caches) {
   System.out.println("**** START: Cache Contents ****");
   int count = 1;
   for (Object o : caches) {
     Cache c = (Cache) o;
     if (c == null) {
       System.out.println("  ** Cache " + count + " is null!");
     } else {
       EmbeddedCacheManager cacheManager = c.getCacheManager();
       System.out.println("  ** Cache " + count + " is " + cacheManager.getAddress());
     }
     count++;
   }
   System.out.println("**** END: Cache Contents ****");
 }
  public static boolean areCacheViewsComplete(Cache[] caches, boolean barfIfTooManyMembers) {
    int memberCount = caches.length;

    for (int i = 0; i < memberCount; i++) {
      EmbeddedCacheManager cacheManager = caches[i].getCacheManager();
      if (!isCacheViewComplete(
          cacheManager.getMembers(),
          cacheManager.getAddress(),
          memberCount,
          barfIfTooManyMembers)) {
        return false;
      }
    }

    return true;
  }
  public static boolean areCacheViewsComplete(
      boolean barfIfTooManyMembers, CacheContainer... cacheContainers) {
    if (cacheContainers == null) throw new NullPointerException("Cache Manager array is null");
    int memberCount = cacheContainers.length;

    for (int i = 0; i < memberCount; i++) {
      EmbeddedCacheManager cacheManager = (EmbeddedCacheManager) cacheContainers[i];
      if (!isCacheViewComplete(
          cacheManager.getMembers(),
          cacheManager.getAddress(),
          memberCount,
          barfIfTooManyMembers)) {
        return false;
      }
    }

    return true;
  }
Пример #8
0
 /** The same as {@link #waitForRpc()} except that you are allowed to specify the max wait time. */
 public void waitForRpc(long time, TimeUnit unit) {
   assert expectedCommands != null
       : "there are no replication expectations; please use ReplListener.expect() before calling this method";
   try {
     boolean successful =
         (expectAny && sawAtLeastOneInvocation) || (!expectAny && expectedCommands.isEmpty());
     info(
         "Expect Any is "
             + expectAny
             + ", saw at least one? "
             + sawAtLeastOneInvocation
             + " Successful? "
             + successful
             + " Expected commands "
             + expectedCommands);
     if (!successful && !latch.await(time, unit)) {
       EmbeddedCacheManager cacheManager = c.getCacheManager();
       assert false
           : "Waiting for more than "
               + time
               + " "
               + unit
               + " and following commands did not replicate: "
               + expectedCommands
               + " on cache ["
               + cacheManager.getAddress()
               + "]";
     } else {
       info("Exiting wait for rpc with expected commands " + expectedCommands);
     }
   } catch (InterruptedException e) {
     throw new IllegalStateException("unexpected", e);
   } finally {
     expectationSetupLock.lock();
     expectedCommands = null;
     expectationSetupLock.unlock();
     expectAny = false;
     sawAtLeastOneInvocation = false;
     latch = new CountDownLatch(1);
     eagerCommands.clear();
   }
 }
  public static void blockUntilViewReceived(
      Cache cache, int groupSize, long timeout, boolean barfIfTooManyMembersInView) {
    long failTime = System.currentTimeMillis() + timeout;

    while (System.currentTimeMillis() < failTime) {
      sleepThread(100);
      EmbeddedCacheManager cacheManager = cache.getCacheManager();
      if (isCacheViewComplete(
          cacheManager.getMembers(),
          cacheManager.getAddress(),
          groupSize,
          barfIfTooManyMembersInView)) {
        return;
      }
    }

    throw new RuntimeException(
        String.format(
            "Timed out before cache had %d members.  View is %s",
            groupSize, cache.getCacheManager().getMembers()));
  }
Пример #10
0
    public void setMembers(List<Address> m) {
      if (m != null && !m.isEmpty()) {
        members = new ArrayList<String>(m.size());
        for (Address ma : m) members.add(ma.toString());

        memberStates = new ArrayList<String>(m.size());
        for (Address a : m) {
          String extraInfo = "Member";
          // if this is the first member then this is the coordinator
          if (memberStates.isEmpty()) extraInfo += " (coord)";
          EmbeddedCacheManager cacheManager = (EmbeddedCacheManager) cache.getCacheManager();
          if (a.equals(cacheManager.getAddress())) extraInfo += " (me)";

          memberStates.add(extraInfo);
        }
      } else {
        members = Collections.singletonList("me!");
        memberStates = Collections.singletonList("(local mode)");
      }

      fireTableDataChanged();
    }
Пример #11
0
 private String getLocalAddress() {
   EmbeddedCacheManager cacheManager = (EmbeddedCacheManager) cache.getCacheManager();
   Address a = cacheManager.getAddress();
   if (a == null) return "(LOCAL mode)";
   else return a.toString();
 }
 /**
  * @param c
  * @param memberCount
  */
 public static boolean isCacheViewComplete(Cache c, int memberCount) {
   EmbeddedCacheManager cacheManager = c.getCacheManager();
   return isCacheViewComplete(
       cacheManager.getMembers(), cacheManager.getAddress(), memberCount, true);
 }
Пример #13
0
  /**
   * Adds the listener using the provided filter converter and class loader. The provided builder is
   * used to add additional configuration including (clustered, onlyPrimary & identifier) which can
   * be used after this method is completed to see what values were used in the addition of this
   * listener
   *
   * @param listener
   * @param filter
   * @param converter
   * @param classLoader
   * @param <C>
   * @return
   */
  public <C> void addListener(
      Object listener,
      CacheEventFilter<? super K, ? super V> filter,
      CacheEventConverter<? super K, ? super V, C> converter,
      ClassLoader classLoader) {
    Listener l = testListenerClassValidity(listener.getClass());
    UUID generatedId = UUID.randomUUID();
    CacheMode cacheMode = config.clustering().cacheMode();
    CacheInvocationBuilder builder = new CacheInvocationBuilder();
    builder
        .setIncludeCurrentState(l.includeCurrentState())
        .setClustered(l.clustered())
        .setOnlyPrimary(
            l.clustered() ? (cacheMode.isDistributed() ? true : false) : l.primaryOnly())
        .setFilter(filter)
        .setConverter(converter)
        .setIdentifier(generatedId)
        .setClassLoader(classLoader);
    boolean foundMethods = validateAndAddListenerInvocation(listener, builder);

    if (foundMethods && l.clustered()) {
      if (cacheMode.isInvalidation()) {
        throw new UnsupportedOperationException(
            "Cluster listeners cannot be used with Invalidation Caches!");
      } else if (cacheMode.isDistributed()) {
        clusterListenerIDs.put(listener, generatedId);
        EmbeddedCacheManager manager = cache.getCacheManager();
        Address ourAddress = manager.getAddress();

        List<Address> members = manager.getMembers();
        // If we are the only member don't even worry about sending listeners
        if (members != null && members.size() > 1) {
          DistributedExecutionCompletionService decs =
              new DistributedExecutionCompletionService(distExecutorService);

          if (log.isTraceEnabled()) {
            log.tracef(
                "Replicating cluster listener to other nodes %s for cluster listener with id %s",
                members, generatedId);
          }
          Callable callable =
              new ClusterListenerReplicateCallable(
                  generatedId, ourAddress, filter, converter, l.sync());
          for (Address member : members) {
            if (!member.equals(ourAddress)) {
              decs.submit(member, callable);
            }
          }

          for (int i = 0; i < members.size() - 1; ++i) {
            try {
              decs.take().get();
            } catch (InterruptedException e) {
              throw new CacheListenerException(e);
            } catch (ExecutionException e) {
              throw new CacheListenerException(e);
            }
          }

          int extraCount = 0;
          // If anyone else joined since we sent these we have to send the listeners again, since
          // they may have queried
          // before the other nodes got the new listener
          List<Address> membersAfter = manager.getMembers();
          for (Address member : membersAfter) {
            if (!members.contains(member) && !member.equals(ourAddress)) {
              if (log.isTraceEnabled()) {
                log.tracef(
                    "Found additional node %s that joined during replication of cluster listener with id %s",
                    member, generatedId);
              }
              extraCount++;
              decs.submit(member, callable);
            }
          }

          for (int i = 0; i < extraCount; ++i) {
            try {
              decs.take().get();
            } catch (InterruptedException e) {
              throw new CacheListenerException(e);
            } catch (ExecutionException e) {
              throw new CacheListenerException(e);
            }
          }
        }
      }
    }

    // If we have a segment listener handler, it means we have to do initial state
    QueueingSegmentListener handler = segmentHandler.remove(generatedId);
    if (handler != null) {
      if (log.isTraceEnabled()) {
        log.tracef("Listener %s requests initial state for cache", generatedId);
      }
      try (CloseableIterator<CacheEntry<K, C>> iterator =
          entryRetriever.retrieveEntries(
              filter == null ? null : new CacheEventFilterAsKeyValueFilter(filter),
              converter == null ? null : new CacheEventConverterAsConverter(converter),
              null,
              handler)) {
        while (iterator.hasNext()) {
          CacheEntry<K, C> entry = iterator.next();
          // Mark the key as processed and see if we had a concurrent update
          Object value = handler.markKeyAsProcessing(entry.getKey());
          if (value == BaseQueueingSegmentListener.REMOVED) {
            // Don't process this value if we had a concurrent remove
            continue;
          }
          raiseEventForInitialTransfer(generatedId, entry, builder.isClustered());

          handler.notifiedKey(entry.getKey());
        }
      }

      Set<CacheEntry> entries = handler.findCreatedEntries();

      for (CacheEntry entry : entries) {
        raiseEventForInitialTransfer(generatedId, entry, builder.isClustered());
      }

      if (log.isTraceEnabled()) {
        log.tracef("Listener %s initial state for cache completed", generatedId);
      }

      handler.transferComplete();
    }
  }