org.infinispan.loaders.decorators
Class SingletonStore

java.lang.Object
  extended by org.infinispan.loaders.decorators.AbstractDelegatingStore
      extended by org.infinispan.loaders.decorators.SingletonStore
All Implemented Interfaces:
CacheLoader, CacheStore

public class SingletonStore
extends AbstractDelegatingStore

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.

Since:
4.0
Author:
Bela Ban, Galder Zamarreno, Manik Surtani

Nested Class Summary
static class SingletonStore.PushStateException
          Exception representing any issues that arise from pushing the in-memory state to the cache loader.
 class SingletonStore.SingletonStoreListener
          Cache listener that reacts to cluster topology changes to find out whether a new coordinator is elected.
 
Constructor Summary
SingletonStore(CacheStore delegate, Cache cache, SingletonStoreConfig config)
           
 
Method Summary
protected  void activeStatusChanged(boolean newActiveState)
          Method called when the cache either becomes the coordinator or stops being the coordinator.
protected  void awaitForPushToFinish(Future future, long timeout, TimeUnit unit)
          Method that waits for the in-memory to cache loader state to finish.
 void clear()
          Clears all entries in the store
 void commit(GlobalTransaction tx)
          Commits a transaction that has been previously prepared.
protected  Callable<?> createPushStateTask()
          Factory method for the creation of a Callable task in charge of pushing in-memory state to cache loader.
 void fromStream(ObjectInput inputStream)
          Writes contents of the stream to the store.
 void prepare(List<? extends Modification> list, GlobalTransaction tx, boolean isOnePhase)
          Issues a prepare call with a set of modifications to be applied to the cache store
 void purgeExpired()
          Purges expired entries from the store.
protected  void pushState(Cache cache)
          Pushes the state of a specific cache by reading the cache's data and putting in the cache store.
 boolean remove(Object key)
          Removes an entry in the store.
 void rollback(GlobalTransaction tx)
          Rolls back a transaction that has been previously prepared

This method may be invoked on a transaction for which there is no prior CacheStore.prepare(java.util.List, org.infinispan.transaction.xa.GlobalTransaction, boolean).

 void start()
           
 void stop()
           
 void store(InternalCacheEntry ed)
          Stores an entry
 String toString()
           
 
Methods inherited from class org.infinispan.loaders.decorators.AbstractDelegatingStore
containsKey, getCacheStoreConfig, getConfigurationClass, getDelegate, init, load, load, loadAll, loadAllKeys, removeAll, setDelegate, toStream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SingletonStore

public SingletonStore(CacheStore delegate,
                      Cache cache,
                      SingletonStoreConfig config)
Method Detail

store

public void store(InternalCacheEntry ed)
           throws CacheLoaderException
Description copied from interface: CacheStore
Stores an entry

Specified by:
store in interface CacheStore
Overrides:
store in class AbstractDelegatingStore
Parameters:
ed - entry to store
Throws:
CacheLoaderException - in the event of problems writing to the store

fromStream

public void fromStream(ObjectInput inputStream)
                throws CacheLoaderException
Description copied from interface: CacheStore
Writes contents of the stream to the store. Implementations should expect that the stream contains data in an implementation-specific format, typically generated using CacheStore.toStream(java.io.ObjectOutput). While not a requirement, it is recommended that implementations make use of the StreamingMarshaller when dealing with the stream to make use of efficient marshalling.

It is imperative that implementations do not close the stream after finishing with it.

It is also recommended that implementations use their own start and end markers on the stream since other processes may write additional data to the stream after the cache store has written to it. As such, either markers or some other mechanism to prevent the store from reading too much information should be employed when writing to the stream in CacheStore.fromStream(java.io.ObjectInput) to prevent data corruption.

It can be assumed that the stream passed in already performs buffering such that the cache store implementation doesn't have to.

Specified by:
fromStream in interface CacheStore
Overrides:
fromStream in class AbstractDelegatingStore
Parameters:
inputStream - stream to read from
Throws:
CacheLoaderException - in the event of problems writing to the store

clear

public void clear()
           throws CacheLoaderException
Description copied from interface: CacheStore
Clears all entries in the store

Specified by:
clear in interface CacheStore
Overrides:
clear in class AbstractDelegatingStore
Throws:
CacheLoaderException - in the event of problems writing to the store

remove

public boolean remove(Object key)
               throws CacheLoaderException
Description copied from interface: CacheStore
Removes an entry in the store.

Specified by:
remove in interface CacheStore
Overrides:
remove in class AbstractDelegatingStore
Parameters:
key - key to remove
Returns:
true if the entry was removed; false if the entry wasn't found.
Throws:
CacheLoaderException - in the event of problems writing to the store

purgeExpired

public void purgeExpired()
                  throws CacheLoaderException
Description copied from interface: CacheStore
Purges expired entries from the store.

Specified by:
purgeExpired in interface CacheStore
Overrides:
purgeExpired in class AbstractDelegatingStore
Throws:
CacheLoaderException - in the event of problems writing to the store

commit

public void commit(GlobalTransaction tx)
            throws CacheLoaderException
Description copied from interface: CacheStore
Commits a transaction that has been previously prepared.

This method may be invoked on a transaction for which there is no prior CacheStore.prepare(java.util.List, org.infinispan.transaction.xa.GlobalTransaction, boolean). The implementation would need to deal with this case accordingly. Typically, this would be a no-op, after ensuring any resources attached to the transaction are cleared up.

Also note that this method may be invoked on a thread which is different from the CacheStore.prepare(java.util.List, org.infinispan.transaction.xa.GlobalTransaction, boolean) invocation. As such, ThreadLocals should not be relied upon to maintain transaction context.

Specified by:
commit in interface CacheStore
Overrides:
commit in class AbstractDelegatingStore
Parameters:
tx - tx to commit
Throws:
CacheLoaderException - in the event of problems writing to the store

rollback

public void rollback(GlobalTransaction tx)
Description copied from interface: CacheStore
Rolls back a transaction that has been previously prepared

This method may be invoked on a transaction for which there is no prior CacheStore.prepare(java.util.List, org.infinispan.transaction.xa.GlobalTransaction, boolean). The implementation would need to deal with this case accordingly. Typically, this would be a no-op, after ensuring any resources attached to the transaction are cleared up.

Also note that this method may be invoked on a thread which is different from the CacheStore.prepare(java.util.List, org.infinispan.transaction.xa.GlobalTransaction, boolean) invocation. As such, ThreadLocals should not be relied upon to maintain transaction context.

Specified by:
rollback in interface CacheStore
Overrides:
rollback in class AbstractDelegatingStore
Parameters:
tx - tx to roll back

prepare

public void prepare(List<? extends Modification> list,
                    GlobalTransaction tx,
                    boolean isOnePhase)
             throws CacheLoaderException
Description copied from interface: CacheStore
Issues a prepare call with a set of modifications to be applied to the cache store

Specified by:
prepare in interface CacheStore
Overrides:
prepare in class AbstractDelegatingStore
Parameters:
list - modifications to be applied
tx - transaction identifier
isOnePhase - if true, there will not be a commit or rollback phase and changes should be flushed immediately
Throws:
CacheLoaderException - in the event of problems writing to the store

start

public void start()
           throws CacheLoaderException
Specified by:
start in interface CacheLoader
Overrides:
start in class AbstractDelegatingStore
Throws:
CacheLoaderException

stop

public void stop()
          throws CacheLoaderException
Specified by:
stop in interface CacheLoader
Overrides:
stop in class AbstractDelegatingStore
Throws:
CacheLoaderException

createPushStateTask

protected Callable<?> createPushStateTask()
Factory method for the creation of a Callable task in charge of pushing in-memory state to cache loader.

Returns:
new instance of Callable whose call() method either throws an exception or returns null if the task was successfull.

pushState

protected void pushState(Cache cache)
                  throws Exception
Pushes the state of a specific cache by reading the cache's data and putting in the cache store. This method is called recursively so that it iterates through the whole cache.

Throws:
Exception - if there's any issues reading the data from the cache or pushing data to the cache store.

awaitForPushToFinish

protected void awaitForPushToFinish(Future future,
                                    long timeout,
                                    TimeUnit unit)
Method that waits for the in-memory to cache loader state to finish. This method's called in case a push state is already in progress and we need to wait for it to finish.

Parameters:
future - instance of Future representing the on going push task
timeout - time to wait for the push task to finish
unit - instance of TimeUnit representing the unit of timeout

activeStatusChanged

protected void activeStatusChanged(boolean newActiveState)
                            throws SingletonStore.PushStateException
Method called when the cache either becomes the coordinator or stops being the coordinator. If it becomes the coordinator, it can optionally start the in-memory state transfer to the underlying cache store.

Parameters:
newActiveState - true if the cache just became the coordinator, false if the cache stopped being the coordinator.
Throws:
SingletonStore.PushStateException

toString

public String toString()
Overrides:
toString in class Object

-->

Copyright © 2011 JBoss, a division of Red Hat. All Rights Reserved.