org.jboss.cache.eviction
Class BaseEvictionAlgorithm

java.lang.Object
  extended by org.jboss.cache.eviction.BaseEvictionAlgorithm
All Implemented Interfaces:
EvictionAlgorithm
Direct Known Subclasses:
BaseSortedEvictionAlgorithm, ExpirationAlgorithm, FIFOAlgorithm, LRUAlgorithm, MRUAlgorithm

public abstract class BaseEvictionAlgorithm
extends Object
implements EvictionAlgorithm

Abstract Event Processing Eviction Algorithm. This class is used to implement basic event processing for Eviction Algorithms. To extend this abstract class to make an Eviction Algorithm, implement the abstract methods and a policy.

Version:
$Revision: 7168 $
Author:
Daniel Huang - dhuang@jboss.org 10/2005, Galder Zamarreno

Field Summary
protected  boolean allowTombstones
           
protected  CacheSPI<?,?> cache
           
protected  Configuration configuration
           
protected  EvictionActionPolicy evictionActionPolicy
           
protected  EvictionAlgorithmConfig evictionAlgorithmConfig
           
protected  EvictionQueue evictionQueue
          Contains NodeEntry instances.
protected  BlockingQueue<Fqn> recycleQueue
          Contains Fqn instances.
protected  Fqn regionFqn
           
 
Constructor Summary
protected BaseEvictionAlgorithm()
           
 
Method Summary
 void assignToRegion(Fqn fqn, CacheSPI<?,?> cache, EvictionAlgorithmConfig evictionAlgorithmConfig, Configuration configuration)
          Assigns the algorithm instance to a specific region.
 boolean canIgnoreEvent(EvictionEvent.Type eventType)
          Tests whether the algorithm would ignore certain event types on certain Fqns.
protected  void emptyRecycleQueue()
          Empty the Recycle Queue.
protected  void evict(NodeEntry ne)
           
protected  boolean evictCacheNode(Fqn fqn)
          Evict a node from cache.
 EvictionActionPolicy getEvictionActionPolicy()
           
 EvictionAlgorithmConfig getEvictionAlgorithmConfig()
           
 EvictionQueue getEvictionQueue()
          Get the underlying EvictionQueue implementation.
protected  EvictionEvent getNextInQueue(BlockingQueue<EvictionEvent> queue)
           
 void initialize()
          Invoked by the region manager when the enclosing region is initialized.
protected  boolean isNodeInUseAndNotTimedOut(NodeEntry ne)
           
protected  boolean isYoungerThanMinimumTimeToLive(NodeEntry entry)
          Tests whether a node entry is younger than the minimum time to live - if one is configured.
 void process(BlockingQueue<EvictionEvent> eventQueue)
          Process the given eviction event queue.
protected  void processAddedElement(EvictionEvent evictedEventNode)
           
protected  void processAddedNodes(EvictionEvent evictedEventNode)
          Convenience method, which calls processAddedNodes(EvictionEvent, int) using values in the evictedEventNode for number of added elements and the resetElementCount flag.
protected  void processAddedNodes(EvictionEvent evictedEventNode, int numAddedElements)
           
protected  void processMarkInUseNodes(Fqn fqn, long inUseTimeout)
           
protected  void processQueues(BlockingQueue<EvictionEvent> queue)
          Event processing for Evict/Add/Visiting of nodes.
protected  void processRemovedElement(EvictionEvent evictedEventNode)
           
protected  void processRemovedNodes(EvictionEvent evictedEventNode)
          Remove a node from cache.
protected  void processUnmarkInUseNodes(Fqn fqn)
           
protected  void processVisitedNodes(EvictionEvent evictedEventNode)
          Visit a node in cache.
protected  void prune()
           
 void resetEvictionQueue()
          Reset the whole eviction queue.
 void setEvictionActionPolicy(EvictionActionPolicy evictionActionPolicy)
          Sets the eviction action policy, so the algorithm knows what to do when a node is to be evicted.
protected abstract  EvictionQueue setupEvictionQueue()
          This method will create an EvictionQueue implementation and prepare it for use.
protected abstract  boolean shouldEvictNode(NodeEntry ne)
          This method will check whether the given node should be evicted or not.
 String toString()
          Returns debug information.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.jboss.cache.eviction.EvictionAlgorithm
getConfigurationClass
 

Field Detail

evictionActionPolicy

protected EvictionActionPolicy evictionActionPolicy

evictionAlgorithmConfig

protected EvictionAlgorithmConfig evictionAlgorithmConfig

recycleQueue

protected BlockingQueue<Fqn> recycleQueue
Contains Fqn instances.


evictionQueue

protected EvictionQueue evictionQueue
Contains NodeEntry instances.


allowTombstones

protected boolean allowTombstones

configuration

protected Configuration configuration

regionFqn

protected Fqn regionFqn

cache

protected CacheSPI<?,?> cache
Constructor Detail

BaseEvictionAlgorithm

protected BaseEvictionAlgorithm()
Method Detail

setupEvictionQueue

protected abstract EvictionQueue setupEvictionQueue()
                                             throws EvictionException
This method will create an EvictionQueue implementation and prepare it for use.

Returns:
The created EvictionQueue to be used as the eviction queue for this algorithm.
Throws:
EvictionException - if there are problems
See Also:
EvictionQueue

shouldEvictNode

protected abstract boolean shouldEvictNode(NodeEntry ne)
This method will check whether the given node should be evicted or not.

Parameters:
ne - NodeEntry to test eviction for.
Returns:
True if the given node should be evicted. False if the given node should not be evicted.

initialize

public void initialize()
Description copied from interface: EvictionAlgorithm
Invoked by the region manager when the enclosing region is initialized.

Specified by:
initialize in interface EvictionAlgorithm

getEvictionActionPolicy

public EvictionActionPolicy getEvictionActionPolicy()

setEvictionActionPolicy

public void setEvictionActionPolicy(EvictionActionPolicy evictionActionPolicy)
Description copied from interface: EvictionAlgorithm
Sets the eviction action policy, so the algorithm knows what to do when a node is to be evicted.

Specified by:
setEvictionActionPolicy in interface EvictionAlgorithm
Parameters:
evictionActionPolicy - to set

getEvictionAlgorithmConfig

public EvictionAlgorithmConfig getEvictionAlgorithmConfig()

assignToRegion

public void assignToRegion(Fqn fqn,
                           CacheSPI<?,?> cache,
                           EvictionAlgorithmConfig evictionAlgorithmConfig,
                           Configuration configuration)
Description copied from interface: EvictionAlgorithm
Assigns the algorithm instance to a specific region.

Specified by:
assignToRegion in interface EvictionAlgorithm
Parameters:
fqn - of the region to be assigned to
cache - cache reference
evictionAlgorithmConfig - configuration for the current algorithm instance.
configuration - for the entire cache.

canIgnoreEvent

public boolean canIgnoreEvent(EvictionEvent.Type eventType)
Description copied from interface: EvictionAlgorithm
Tests whether the algorithm would ignore certain event types on certain Fqns.

Specified by:
canIgnoreEvent in interface EvictionAlgorithm
Parameters:
eventType - event type to test for
Returns:
true if the event representing the parameters would be ignored by this algorithm or not.

process

public void process(BlockingQueue<EvictionEvent> eventQueue)
             throws EvictionException
Process the given eviction event queue. Eviction Processing encompasses the following:

- Add/Remove/Visit Nodes - Prune according to Eviction Algorithm - Empty/Retry the recycle queue of previously evicted but locked (during actual cache eviction) nodes.

Specified by:
process in interface EvictionAlgorithm
Parameters:
eventQueue - queue containing eviction events
Throws:
EvictionException

resetEvictionQueue

public void resetEvictionQueue()
Description copied from interface: EvictionAlgorithm
Reset the whole eviction queue. The queue may need to be reset due to corrupted state, for example.

Specified by:
resetEvictionQueue in interface EvictionAlgorithm

getEvictionQueue

public EvictionQueue getEvictionQueue()
Get the underlying EvictionQueue implementation.

Specified by:
getEvictionQueue in interface EvictionAlgorithm
Returns:
the EvictionQueue used by this algorithm
See Also:
EvictionQueue

getNextInQueue

protected EvictionEvent getNextInQueue(BlockingQueue<EvictionEvent> queue)

processQueues

protected void processQueues(BlockingQueue<EvictionEvent> queue)
                      throws EvictionException
Event processing for Evict/Add/Visiting of nodes.

- On AddEvents a new element is added into the eviction queue - On RemoveEvents, the removed element is removed from the eviction queue. - On VisitEvents, the visited node has its eviction statistics updated (idleTime, numberOfNodeVisists, etc..)

Parameters:
queue - queue to inspect
Throws:
EvictionException - in the event of problems

evict

protected void evict(NodeEntry ne)

evictCacheNode

protected boolean evictCacheNode(Fqn fqn)
Evict a node from cache.

Parameters:
fqn - node corresponds to this fqn
Returns:
True if successful

processMarkInUseNodes

protected void processMarkInUseNodes(Fqn fqn,
                                     long inUseTimeout)
                              throws EvictionException
Throws:
EvictionException

processUnmarkInUseNodes

protected void processUnmarkInUseNodes(Fqn fqn)
                                throws EvictionException
Throws:
EvictionException

processAddedNodes

protected void processAddedNodes(EvictionEvent evictedEventNode)
                          throws EvictionException
Convenience method, which calls processAddedNodes(EvictionEvent, int) using values in the evictedEventNode for number of added elements and the resetElementCount flag.

Parameters:
evictedEventNode - an evictedEventNode to process
Throws:
EvictionException - on problems

processAddedNodes

protected void processAddedNodes(EvictionEvent evictedEventNode,
                                 int numAddedElements)
                          throws EvictionException
Throws:
EvictionException

processRemovedNodes

protected void processRemovedNodes(EvictionEvent evictedEventNode)
                            throws EvictionException
Remove a node from cache.

This method will remove the node from the eviction queue as well as evict the node from cache.

If a node cannot be removed from cache, this method will remove it from the eviction queue and place the element into the recycleQueue. Each node in the recycle queue will get retried until proper cache eviction has taken place.

Because EvictionQueues are collections, when iterating them from an iterator, use iterator.remove() to avoid ConcurrentModificationExceptions. Use the boolean parameter to indicate the calling context.

Throws:
EvictionException

processVisitedNodes

protected void processVisitedNodes(EvictionEvent evictedEventNode)
                            throws EvictionException
Visit a node in cache.

This method will update the numVisits and modifiedTimestamp properties of the Node. These properties are used as statistics to determine eviction (LRU, LFU, MRU, etc..)

*Note* that this method updates Node Entries by reference and does not put them back into the queue. For some sorted collections, a remove, and a re-add is required to maintain the sorted order of the elements.

Throws:
EvictionException

processRemovedElement

protected void processRemovedElement(EvictionEvent evictedEventNode)
                              throws EvictionException
Throws:
EvictionException

processAddedElement

protected void processAddedElement(EvictionEvent evictedEventNode)
                            throws EvictionException
Throws:
EvictionException

emptyRecycleQueue

protected void emptyRecycleQueue()
                          throws EvictionException
Empty the Recycle Queue.

This method will go through the recycle queue and retry to evict the nodes from cache.

Throws:
EvictionException

isNodeInUseAndNotTimedOut

protected boolean isNodeInUseAndNotTimedOut(NodeEntry ne)

prune

protected void prune()
              throws EvictionException
Throws:
EvictionException

toString

public String toString()
Returns debug information.

Overrides:
toString in class Object

isYoungerThanMinimumTimeToLive

protected boolean isYoungerThanMinimumTimeToLive(NodeEntry entry)
Tests whether a node entry is younger than the minimum time to live - if one is configured.

Parameters:
entry - the node entry being examined
Returns:
true if the node is younger than - or exactly equal to - the minimum time to live, if one is configured for the given region. False otherwise.


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