org.jboss.cache.loader
Class SingletonStoreCacheLoader

java.lang.Object
  extended by org.jboss.cache.loader.AbstractCacheLoader
      extended by org.jboss.cache.loader.AbstractDelegatingCacheLoader
          extended by org.jboss.cache.loader.SingletonStoreCacheLoader
All Implemented Interfaces:
CacheLoader

public class SingletonStoreCacheLoader
extends AbstractDelegatingCacheLoader

SingletonStoreCacheLoader is a delegating cache loader used for situations when only one node should interact with the underlying store. The coordinator of the cluster will be responsible for the underlying CacheLoader. SingletonStoreCacheLoader is a simply facade to a real CacheLoader implementation. It always delegates reads to the real CacheLoader.

Writes are forwarded only if this SingletonStoreCacheLoader is currently the cordinator. This avoid having all CacheLoaders 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 SingletonStoreCacheLoader will then pass writes through to its underlying CacheLoader. Optionally, when a new coordinator takes over the Singleton, it can push the in-memory state to the cache cacheLoader.

Author:
Bela Ban, Galder Zamarreno

Nested Class Summary
 class SingletonStoreCacheLoader.SingletonStoreListener
          Cache listener that reacts to cluster topology changes to find out whether a new coordinator is elected.
 
Field Summary
 
Fields inherited from class org.jboss.cache.loader.AbstractCacheLoader
cache, regionManager
 
Constructor Summary
SingletonStoreCacheLoader(CacheLoader cacheLoader, boolean pushConfiguration)
           
 
Method Summary
protected  void activeStatusChanged(boolean newActiveState)
           
 void commit(java.lang.Object tx)
          Commits the transaction.
protected  java.lang.Thread createPushStateThread()
           
 java.lang.Object getCacheListener()
           
 java.lang.Thread getPushStateThread()
           
 void prepare(java.lang.Object tx, java.util.List<Modification> modifications, boolean one_phase)
          Prepares a list of modifications.
 void put(Fqn name, java.util.Map attributes)
          Puts all entries of the map into the existing map of the given node, overwriting existing keys, but not clearing the existing map before insertion.
 java.lang.Object put(Fqn name, java.lang.Object key, java.lang.Object value)
          Puts a key and value into the attribute map of a given node.
 void put(java.util.List<Modification> modifications)
          Applies all modifications to the backend store.
 void remove(Fqn fqn)
          Removes the given node and all its subnodes, does nothing if the node does not exist.
 java.lang.Object remove(Fqn fqn, java.lang.Object key)
          Removes the given key and value from the attributes of the given node.
 void removeData(Fqn fqn)
          Removes all attributes from a given node, but doesn't delete the node itself or any subnodes.
 void rollback(java.lang.Object tx)
          Rolls the transaction back.
 void storeEntireState(java.io.ObjectInputStream is)
          Stores the entire state for this cache by reading it from a provided ObjectInputStream.
 void storeState(Fqn subtree, java.io.ObjectInputStream is)
          Stores the given portion of the cache tree's state in secondary storage.
 java.lang.String toString()
           
 
Methods inherited from class org.jboss.cache.loader.AbstractDelegatingCacheLoader
create, destroy, exists, get, getCacheLoader, getChildrenNames, getConfig, loadEntireState, loadState, setCache, setCacheLoader, setConfig, setRegionManager, start, stop
 
Methods inherited from class org.jboss.cache.loader.AbstractCacheLoader
getMarshaller, getNodeDataList, loadStateHelper, put
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SingletonStoreCacheLoader

public SingletonStoreCacheLoader(CacheLoader cacheLoader,
                                 boolean pushConfiguration)
Method Detail

getCacheListener

public java.lang.Object getCacheListener()

activeStatusChanged

protected void activeStatusChanged(boolean newActiveState)

createPushStateThread

protected java.lang.Thread createPushStateThread()

put

public java.lang.Object put(Fqn name,
                            java.lang.Object key,
                            java.lang.Object value)
                     throws java.lang.Exception
Description copied from interface: CacheLoader
Puts a key and value into the attribute map of a given node. If the node does not exist, all parent nodes from the root down are created automatically. Returns the old value.

Specified by:
put in interface CacheLoader
Overrides:
put in class AbstractDelegatingCacheLoader
Throws:
java.lang.Exception

put

public void put(Fqn name,
                java.util.Map attributes)
         throws java.lang.Exception
Description copied from interface: CacheLoader
Puts all entries of the map into the existing map of the given node, overwriting existing keys, but not clearing the existing map before insertion. This is the same behavior as Map.putAll(java.util.Map). If the node does not exist, all parent nodes from the root down are created automatically

Specified by:
put in interface CacheLoader
Overrides:
put in class AbstractDelegatingCacheLoader
Parameters:
name - The fully qualified name of the node
attributes - A Map of attributes. Can be null
Throws:
java.lang.Exception

put

public void put(java.util.List<Modification> modifications)
         throws java.lang.Exception
Description copied from interface: CacheLoader
Applies all modifications to the backend store. Changes may be applied in a single operation.

Specified by:
put in interface CacheLoader
Overrides:
put in class AbstractDelegatingCacheLoader
Parameters:
modifications - A List of modifications
Throws:
java.lang.Exception

remove

public java.lang.Object remove(Fqn fqn,
                               java.lang.Object key)
                        throws java.lang.Exception
Description copied from interface: CacheLoader
Removes the given key and value from the attributes of the given node. Does nothing if the node doesn't exist Returns the removed value.

Specified by:
remove in interface CacheLoader
Overrides:
remove in class AbstractDelegatingCacheLoader
Throws:
java.lang.Exception

remove

public void remove(Fqn fqn)
            throws java.lang.Exception
Description copied from interface: CacheLoader
Removes the given node and all its subnodes, does nothing if the node does not exist.

Specified by:
remove in interface CacheLoader
Overrides:
remove in class AbstractDelegatingCacheLoader
Parameters:
fqn - the Fqn of the node
Throws:
java.lang.Exception

removeData

public void removeData(Fqn fqn)
                throws java.lang.Exception
Description copied from interface: CacheLoader
Removes all attributes from a given node, but doesn't delete the node itself or any subnodes.

Specified by:
removeData in interface CacheLoader
Overrides:
removeData in class AbstractDelegatingCacheLoader
Parameters:
fqn - the Fqn of the node
Throws:
java.lang.Exception

prepare

public void prepare(java.lang.Object tx,
                    java.util.List<Modification> modifications,
                    boolean one_phase)
             throws java.lang.Exception
Description copied from interface: CacheLoader
Prepares a list of modifications. For example, for a DB-based CacheLoader:
  1. Create a local (JDBC) transaction
  2. Associate the local transaction with tx (tx is the key)
  3. Execute the corresponding SQL statements against the DB (statements derived from modifications)
For non-transactional CacheLoader (e.g. file-based), the implementation could attempt to implement it's own transactional logic, attempting to write data to a temp location (or memory) and writing it to the proper location upon commit.

Specified by:
prepare in interface CacheLoader
Overrides:
prepare in class AbstractDelegatingCacheLoader
Parameters:
tx - The transaction, indended to be used by implementations as an identifier of the transaction (and not necessarily a JTA Transaction object)
modifications - A List containing Modifications, for the given transaction
one_phase - Persist immediately and (for example) commit the local JDBC transaction as well. When true, we won't get a CacheLoader.commit(Object) or CacheLoader.rollback(Object) method call later
Throws:
java.lang.Exception

commit

public void commit(java.lang.Object tx)
            throws java.lang.Exception
Description copied from interface: CacheLoader
Commits the transaction. A DB-based CacheLoader would look up the local JDBC transaction asociated with tx and commit that transaction. Non-transactional CacheLoaders could simply write the data that was previously saved transiently under the given tx key, to (for example) a file system.

Note this only holds if the previous prepare() did not define

one_phase=true

Specified by:
commit in interface CacheLoader
Overrides:
commit in class AbstractDelegatingCacheLoader
Parameters:
tx - transaction to commit
Throws:
java.lang.Exception

rollback

public void rollback(java.lang.Object tx)
Description copied from interface: CacheLoader
Rolls the transaction back. A DB-based CacheLoader would look up the local JDBC transaction asociated with tx and roll back that transaction.

Specified by:
rollback in interface CacheLoader
Overrides:
rollback in class AbstractDelegatingCacheLoader
Parameters:
tx - transaction to roll back

storeEntireState

public void storeEntireState(java.io.ObjectInputStream is)
                      throws java.lang.Exception
Description copied from interface: CacheLoader
Stores the entire state for this cache by reading it from a provided ObjectInputStream. The state was provided to this cache by calling CacheLoader.loadEntireState(ObjectOutputStream)} on some other cache instance. State currently in storage gets overwritten.

Implementations of this method should not catch any exception or close the given ObjectInputStream parameter. In order to ensure cacheloader interoperability contents of the cache are read from the ObjectInputStream as a sequence of NodeData objects.

Default implementation is provided by AbstractCacheLoader and ensures cacheloader interoperability. Implementors are encouraged to consider extending AbstractCacheLoader prior to implementing completely custom cacheloader.

Specified by:
storeEntireState in interface CacheLoader
Overrides:
storeEntireState in class AbstractDelegatingCacheLoader
Parameters:
is - ObjectInputStream to read state
Throws:
java.lang.Exception
See Also:
AbstractCacheLoader.storeEntireState(ObjectInputStream), NodeData

storeState

public void storeState(Fqn subtree,
                       java.io.ObjectInputStream is)
                throws java.lang.Exception
Description copied from interface: CacheLoader
Stores the given portion of the cache tree's state in secondary storage. Overwrite whatever is currently in secondary storage. If the transferred state has Fqns equal to or children of parameter subtree, then no special behavior is required. Otherwise, ensure that the state is integrated under the given subtree. Typically in the latter case subtree would be the Fqn of the buddy backup region for a buddy group; e.g.

If the the transferred state had Fqns starting with "/a" and subtree was "/_BUDDY_BACKUP_/192.168.1.2:5555" then the state should be stored in the local persistent store under "/_BUDDY_BACKUP_/192.168.1.2:5555/a"

Implementations of this method should not catch any exception or close the given ObjectInputStream parameter. In order to ensure cacheloader interoperability contents of the cache are read from the ObjectInputStream as a sequence of NodeData objects.

Default implementation is provided by AbstractCacheLoader and ensures cacheloader interoperability. Implementors are encouraged to consider extending AbstractCacheLoader prior to implementing completely custom cacheloader.

Specified by:
storeState in interface CacheLoader
Overrides:
storeState in class AbstractDelegatingCacheLoader
Parameters:
subtree - Fqn naming the root (i.e. highest level parent) node of the subtree included in state. If the Fqns of the data included in state are not already children of subtree, then their Fqns should be altered to make them children of subtree before they are persisted.
is - ObjectInputStream to read state
Throws:
java.lang.Exception
See Also:
AbstractCacheLoader.storeState(Fqn,ObjectInputStream), NodeData

getPushStateThread

public java.lang.Thread getPushStateThread()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object