Exemplo n.º 1
0
  /**
   * Default Constructor
   *
   * @param cache the Terracotta clustered Cache to snapshot
   * @param interval the interval to do the snapshots on
   * @param doKeySnapshotOnDedicatedThread whether the snapshots have to be done on a dedicated
   *     thread
   * @param rotatingWriter the RotatingSnapshotFile to write to
   * @throws IllegalArgumentException if interval is less than or equal to zero
   */
  KeySnapshotter(
      final Ehcache cache,
      final long interval,
      final boolean doKeySnapshotOnDedicatedThread,
      final RotatingSnapshotFile rotatingWriter)
      throws IllegalArgumentException {
    final Store store = new CacheStoreHelper((Cache) cache).getStore();
    if (!(store instanceof TerracottaStore)) {
      throw new IllegalArgumentException(
          "Cache '"
              + cache.getName()
              + "' isn't backed by a "
              + TerracottaStore.class.getSimpleName()
              + " but uses a "
              + store.getClass().getName()
              + " instead");
    }

    if (interval <= 0) {
      throw new IllegalArgumentException("Interval needs to be a positive & non-zero value");
    }

    if (rotatingWriter == null) {
      throw new NullPointerException();
    }

    this.cacheName = cache.getName();
    this.rotatingWriter = rotatingWriter;
    this.tcStore = (TerracottaStore) store;

    if (doKeySnapshotOnDedicatedThread) {
      thread = new SnapShottingThread(this, interval, "KeySnapshotter for cache " + cacheName);
      thread.start();
    } else {
      ScheduledExecutorService scheduledExecutorService = INSTANCES.get(cache.getCacheManager());
      if (scheduledExecutorService == null) {
        scheduledExecutorService = new ScheduledThreadPoolExecutor(POOL_SIZE);
        final ScheduledExecutorService previous =
            INSTANCES.putIfAbsent(cache.getCacheManager(), scheduledExecutorService);
        if (previous != null) {
          scheduledExecutorService.shutdownNow();
          scheduledExecutorService = previous;
        }
      }
      scheduledExecutorService.scheduleWithFixedDelay(this, interval, interval, TimeUnit.SECONDS);
      thread = null;
    }
  }
Exemplo n.º 2
0
  /**
   * Acquires the cache peers for this cache.
   *
   * @param cache
   */
  protected List acquireCachePeers(Ehcache cache) {

    long timeForClusterToForm = 0;
    CacheManagerPeerProvider cacheManagerPeerProvider =
        cache.getCacheManager().getCacheManagerPeerProvider("RMI");
    if (cacheManagerPeerProvider != null) {
      timeForClusterToForm = cacheManagerPeerProvider.getTimeForClusterToForm();
    }
    if (LOG.isDebugEnabled()) {
      LOG.debug(
          "Attempting to acquire cache peers for cache "
              + cache.getName()
              + " to bootstrap from. Will wait up to "
              + timeForClusterToForm
              + "ms for cache to join cluster.");
    }
    List cachePeers = null;
    for (int i = 0; i <= timeForClusterToForm; i = i + ONE_SECOND) {
      cachePeers = listRemoteCachePeers(cache);
      if (cachePeers == null) {
        break;
      }
      if (cachePeers.size() > 0) {
        break;
      }
      try {
        Thread.sleep(ONE_SECOND);
      } catch (InterruptedException e) {
        LOG.debug("doLoad for " + cache.getName() + " interrupted.");
      }
    }

    LOG.debug("cache peers: {}", cachePeers);
    return cachePeers;
  }
Exemplo n.º 3
0
 /**
  * Package protected List of cache peers
  *
  * @param cache
  */
 protected List listRemoteCachePeers(Ehcache cache) {
   CacheManagerPeerProvider provider = cache.getCacheManager().getCacheManagerPeerProvider("RMI");
   if (provider == null) {
     return null;
   } else {
     return provider.listRemoteCachePeers(cache);
   }
 }
Exemplo n.º 4
0
 private void localDispose() throws IllegalStateException {
   synchronized (this) {
     if (refreshWorkQueue != null) {
       refreshWorkQueue.shutdown();
       refreshWorkQueue = null;
     }
     if (supportCache != null) {
       try {
         supportCache.getCacheManager().removeCache(getName());
       } catch (Throwable t) {
       }
       supportCache = null;
     }
   }
 }
Exemplo n.º 5
0
  /** @return milliseconds left before timeout */
  private long assertNotTimedOut() {
    try {
      if (Thread.interrupted()) {
        throw new TransactionInterruptedException("transaction interrupted");
      }

      Transaction transaction = getCurrentTransaction();
      Long timeoutTimestamp = transactionToTimeoutMap.get(transaction);
      long now = MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS);
      if (timeoutTimestamp == null) {
        long timeout;
        EhcacheXAResource xaResource = transactionToXAResourceMap.get(transaction);
        if (xaResource != null) {
          int xaResourceTimeout = xaResource.getTransactionTimeout();
          timeout = MILLISECONDS.convert(xaResourceTimeout, TimeUnit.SECONDS);
        } else {
          int defaultTransactionTimeout =
              cache.getCacheManager().getTransactionController().getDefaultTransactionTimeout();
          timeout = MILLISECONDS.convert(defaultTransactionTimeout, TimeUnit.SECONDS);
        }
        timeoutTimestamp = now + timeout;
        transactionToTimeoutMap.put(transaction, timeoutTimestamp);
        try {
          transaction.registerSynchronization(new CleanupTimeout(transaction));
        } catch (RollbackException e) {
          throw new TransactionException("transaction has been marked as rollback only", e);
        }
        return timeout;
      } else {
        long timeToExpiry = timeoutTimestamp - now;
        if (timeToExpiry <= 0) {
          throw new TransactionTimeoutException("transaction timed out");
        } else {
          return timeToExpiry;
        }
      }
    } catch (SystemException e) {
      throw new TransactionException("cannot get the current transaction", e);
    } catch (XAException e) {
      throw new TransactionException("cannot get the XAResource transaction timeout", e);
    }
  }
 /** Given an {@link Ehcache} get the corresponding instance of this class. */
 public static JGroupsCacheManagerPeerProvider getCachePeerProvider(Ehcache cache) {
   final CacheManager cacheManager = cache.getCacheManager();
   return getCachePeerProvider(cacheManager);
 }