Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.infinispan.persistence.support;
import org.infinispan.Cache;
import org.infinispan.configuration.cache.SingletonStoreConfiguration;
import org.infinispan.container.DataContainer;
import org.infinispan.container.entries.InternalCacheEntry;
import org.infinispan.marshall.core.MarshalledEntryImpl;
import org.infinispan.persistence.spi.CacheWriter;
import org.infinispan.marshall.core.MarshalledEntry;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachemanagerlistener.annotation.CacheStarted;
import org.infinispan.notifications.cachemanagerlistener.annotation.ViewChanged;
import org.infinispan.notifications.cachemanagerlistener.event.Event;
import org.infinispan.notifications.cachemanagerlistener.event.ViewChangedEvent;
import org.infinispan.remoting.transport.Address;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static org.infinispan.persistence.PersistenceUtil.internalMetadata;
/**
* SingletonStore is a delegating cache store used for situations when only one instance should interact with the
* underlying store. The coordinator of the cluster will be responsible for the underlying CacheStore.
*
* SingletonStore is a simply facade to a real CacheStore implementation. It always delegates reads to the real
* CacheStore.
*
* Writes are delegated only if this SingletonStore is currently the coordinator. This avoids having all stores in
* a cluster writing the same data to the same underlying store. Although not incorrect (e.g. a DB will just discard
* additional INSERTs for the same key, and throw an exception), this will avoid a lot of redundant work.
*
* Whenever the current coordinator dies (or leaves), the second in line will take over. That SingletonStore will then
* pass writes through to its underlying CacheStore. Optionally, when a new coordinator takes over the Singleton, it can
* push the in-memory state to the cache cacheStore, within a time constraint.
*
* @author Mircea Markus
* @since 6.0
*/
public class SingletonCacheWriter extends DelegatingCacheWriter {
private static final Log log = LogFactory.getLog(SingletonCacheWriter.class);
private static final boolean trace = log.isTraceEnabled();
private SingletonStoreConfiguration singletonConfiguration;
/**
* Name of thread that should pushing in-memory state to cache loader.
*/
private static final String THREAD_NAME = "SingletonStorePusherThread";
/**
* Executor service used to submit tasks to push in-memory state.
*/
protected final ExecutorService executor;
/**
* Future result of the in-memory push state task. This allows SingletonStore to check whether there's any push taks
* on going.
*/
Future> pushStateFuture; /* FutureTask guarantees a safe publication of the result */
/**
* Address instance that allows SingletonStore to find out whether it became the coordinator of the cluster, or
* whether it stopped being it. This dictates whether the SingletonStore is active or not.
*/
private Address localAddress;
/**
* Whether the the current cache is the coordinator and therefore SingletonStore is active. Being active means
* delegating calls to the underlying cache loader.
*/
protected volatile boolean active;
public SingletonCacheWriter(CacheWriter actual, SingletonStoreConfiguration singletonConfiguration) {
super(actual);
executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, THREAD_NAME);
}
});
this.singletonConfiguration = singletonConfiguration;
}
@Override
public void start() {
ctx.getCache().getCacheManager().addListener(new SingletonStoreListener());
}
@Override
public void stop() {
executor.shutdownNow();
}
@Override
public void write(MarshalledEntry entry) {
if (active) {
if (trace)
log.tracef("Storing key %s. Instance: %s", entry.getKey(), this);
super.write(entry);
} else {
if (trace)
log.tracef("Not storing key %s. Instance: %s", entry.getKey(), this);
}
}
@Override
public boolean delete(Object key) {
return active && super.delete(key);
}
protected Callable> createPushStateTask() {
return new Callable