[jboss-cvs] jboss-ejb3/src/main/org/jboss/ejb3/cache/tree ...
Ben Wang
bwang at jboss.com
Tue Jul 25 02:55:51 EDT 2006
User: bwang
Date: 06/07/25 02:55:51
Modified: src/main/org/jboss/ejb3/cache/tree Tag: Branch_4_0
PassivationCacheLoader.java
PassivationEvictionPolicy.java
PassivationTreeCache.java StatefulCacheLoader.java
StatefulEvictionPolicy.java StatefulTreeCache.java
Log:
Backported from head. Uses JBossCache 1.4 passivation feature now. InUse is also handled properly now. Except StatefulTreeCache.java, the rest have been deprecated.
Revision Changes Path
No revision
No revision
1.1.6.9 +2 -1 jboss-ejb3/src/main/org/jboss/ejb3/cache/tree/PassivationCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: PassivationCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/jboss-ejb3/src/main/org/jboss/ejb3/cache/tree/PassivationCacheLoader.java,v
retrieving revision 1.1.6.8
retrieving revision 1.1.6.9
diff -u -b -r1.1.6.8 -r1.1.6.9
--- PassivationCacheLoader.java 20 Mar 2006 20:06:32 -0000 1.1.6.8
+++ PassivationCacheLoader.java 25 Jul 2006 06:55:50 -0000 1.1.6.9
@@ -33,8 +33,9 @@
/**
* Comment
*
+ * @deprecated since RC9. Will use JBoss Cache 1.4 feature instead.
* @author <a href="mailto:bill at jboss.org">Bill Burke</a>
- * @version $Revision: 1.1.6.8 $
+ * @version $Revision: 1.1.6.9 $
*/
public class PassivationCacheLoader extends FileCacheLoader
{
1.1.6.9 +2 -1 jboss-ejb3/src/main/org/jboss/ejb3/cache/tree/PassivationEvictionPolicy.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: PassivationEvictionPolicy.java
===================================================================
RCS file: /cvsroot/jboss/jboss-ejb3/src/main/org/jboss/ejb3/cache/tree/PassivationEvictionPolicy.java,v
retrieving revision 1.1.6.8
retrieving revision 1.1.6.9
diff -u -b -r1.1.6.8 -r1.1.6.9
--- PassivationEvictionPolicy.java 20 Mar 2006 20:06:32 -0000 1.1.6.8
+++ PassivationEvictionPolicy.java 25 Jul 2006 06:55:50 -0000 1.1.6.9
@@ -36,8 +36,9 @@
/**
* Comment
*
+ * @deprecated since RC9. Will use JBoss Cache 1.4 feature instead.
* @author <a href="mailto:bill at jboss.org">Bill Burke</a>
- * @version $Revision: 1.1.6.8 $
+ * @version $Revision: 1.1.6.9 $
*/
public class PassivationEvictionPolicy extends LRUPolicy
{
1.1.6.9 +2 -1 jboss-ejb3/src/main/org/jboss/ejb3/cache/tree/PassivationTreeCache.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: PassivationTreeCache.java
===================================================================
RCS file: /cvsroot/jboss/jboss-ejb3/src/main/org/jboss/ejb3/cache/tree/PassivationTreeCache.java,v
retrieving revision 1.1.6.8
retrieving revision 1.1.6.9
diff -u -b -r1.1.6.8 -r1.1.6.9
--- PassivationTreeCache.java 20 Mar 2006 20:06:32 -0000 1.1.6.8
+++ PassivationTreeCache.java 25 Jul 2006 06:55:50 -0000 1.1.6.9
@@ -27,8 +27,9 @@
/**
* Comment
*
+ * @deprecated since RC9. Will use JBoss Cache 1.4 feature instead.
* @author <a href="mailto:bill at jboss.org">Bill Burke</a>
- * @version $Revision: 1.1.6.8 $
+ * @version $Revision: 1.1.6.9 $
*/
public class PassivationTreeCache extends TreeCache
{
1.1.6.9 +2 -1 jboss-ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: StatefulCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/jboss-ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulCacheLoader.java,v
retrieving revision 1.1.6.8
retrieving revision 1.1.6.9
diff -u -b -r1.1.6.8 -r1.1.6.9
--- StatefulCacheLoader.java 20 Mar 2006 20:06:32 -0000 1.1.6.8
+++ StatefulCacheLoader.java 25 Jul 2006 06:55:50 -0000 1.1.6.9
@@ -28,8 +28,9 @@
/**
* Comment
*
+ * @deprecated since RC9. Will use JBoss Cache 1.4 feature instead.
* @author <a href="mailto:bill at jboss.org">Bill Burke</a>
- * @version $Revision: 1.1.6.8 $
+ * @version $Revision: 1.1.6.9 $
*/
public class StatefulCacheLoader extends PassivationCacheLoader
{
1.1.6.12 +2 -1 jboss-ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulEvictionPolicy.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: StatefulEvictionPolicy.java
===================================================================
RCS file: /cvsroot/jboss/jboss-ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulEvictionPolicy.java,v
retrieving revision 1.1.6.11
retrieving revision 1.1.6.12
diff -u -b -r1.1.6.11 -r1.1.6.12
--- StatefulEvictionPolicy.java 20 May 2006 04:27:49 -0000 1.1.6.11
+++ StatefulEvictionPolicy.java 25 Jul 2006 06:55:50 -0000 1.1.6.12
@@ -31,8 +31,9 @@
/**
* Comment
*
+ * @deprecated since RC9. Will use JBoss Cache 1.4 feature instead.
* @author <a href="mailto:bill at jboss.org">Bill Burke</a>
- * @version $Revision: 1.1.6.11 $
+ * @version $Revision: 1.1.6.12 $
*/
public class StatefulEvictionPolicy extends PassivationEvictionPolicy
{
1.5.2.11 +156 -29 jboss-ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulTreeCache.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: StatefulTreeCache.java
===================================================================
RCS file: /cvsroot/jboss/jboss-ejb3/src/main/org/jboss/ejb3/cache/tree/StatefulTreeCache.java,v
retrieving revision 1.5.2.10
retrieving revision 1.5.2.11
diff -u -b -r1.5.2.10 -r1.5.2.11
--- StatefulTreeCache.java 3 Apr 2006 12:28:00 -0000 1.5.2.10
+++ StatefulTreeCache.java 25 Jul 2006 06:55:50 -0000 1.5.2.11
@@ -26,10 +26,14 @@
import javax.management.MBeanServer;
import javax.management.ObjectName;
-import org.jboss.annotation.ejb.cache.tree.CacheConfig;
import org.jboss.aop.Advisor;
+import org.jboss.cache.xml.XmlHelper;
+import org.jboss.cache.eviction.RegionManager;
+import org.jboss.cache.eviction.Region;
import org.jboss.cache.CacheException;
-import org.jboss.cache.Fqn;
+import org.jboss.cache.AbstractTreeCacheListener;
+import org.jboss.cache.Node;
+import org.jboss.cache.TreeCache;
import org.jboss.cache.TreeCacheMBean;
import org.jboss.ejb3.Container;
import org.jboss.ejb3.Pool;
@@ -37,19 +41,30 @@
import org.jboss.ejb3.stateful.StatefulBeanContext;
import org.jboss.mx.util.MBeanProxyExt;
import org.jboss.mx.util.MBeanServerLocator;
+import org.jboss.logging.Logger;
+import org.jboss.annotation.ejb.cache.tree.CacheConfig;
+import org.w3c.dom.Element;
+
+import org.jboss.cache.Fqn;
+import org.jboss.cache.config.Option;
/**
* Comment
*
* @author <a href="mailto:bill at jboss.org">Bill Burke</a>
- * @version $Revision: 1.5.2.10 $
+ * @version $Revision: 1.5.2.11 $
*/
public class StatefulTreeCache implements ClusteredStatefulCache
{
+ protected static Logger log = Logger.getLogger(StatefulTreeCache.class);
+ static int FQN_SIZE = 2; // depth of fqn that we store the session in.
+
private Pool pool;
- private PassivationTreeCache cache;
- private String cacheNode;
- private StatefulEvictionPolicy eviction;
+ private TreeCache cache;
+ private Fqn cacheNode;
+ private ClusteredStatefulCacheListener listener;
+ private RegionManager evictRegionManager;
+ public static long MarkInUseWaitTime = 15000;
public StatefulBeanContext create()
{
@@ -58,6 +73,12 @@
{
ctx = (StatefulBeanContext) pool.get();
cache.put(cacheNode + "/" + ctx.getId(), "bean", ctx);
+ ctx.inUse = true;
+ ctx.lastUsed = System.currentTimeMillis();
+ }
+ catch (EJBException e)
+ {
+ throw e;
}
catch (Exception e)
{
@@ -72,7 +93,14 @@
try
{
ctx = (StatefulBeanContext) pool.get(initTypes, initValues);
- cache.put(cacheNode + "/" + ctx.getId(), "bean", ctx);
+ Fqn id = Fqn.fromString(cacheNode + "/" + ctx.getId());
+ cache.put(id, "bean", ctx);
+ ctx.inUse = true;
+ ctx.lastUsed = System.currentTimeMillis();
+ }
+ catch (EJBException e)
+ {
+ throw e;
}
catch (Exception e)
{
@@ -84,10 +112,10 @@
public StatefulBeanContext get(Object key) throws EJBException
{
StatefulBeanContext entry = null;
+ Fqn id = Fqn.fromString(cacheNode + "/" +key);
try
{
- Object obj = cache.get(cacheNode + "/" + key, "bean");
- //System.out.println("obj: instanceof: " + obj.getClass().getName());
+ Object obj = cache.get(id, "bean");
entry = (StatefulBeanContext) obj;
}
catch (CacheException e)
@@ -99,6 +127,13 @@
throw new EJBNoSuchObjectException("Could not find Stateful bean: " + key);
}
entry.inUse = true;
+ // Mark it to eviction thread that don't passivate it yet.
+ evictRegionManager.markNodeCurrentlyInUse(id, MarkInUseWaitTime);
+ if(log.isDebugEnabled())
+ {
+ log.debug("get: retrieved bean with cache id " +id.toString());
+ }
+
entry.lastUsed = System.currentTimeMillis();
return entry;
}
@@ -106,16 +141,20 @@
public void remove(Object key)
{
StatefulBeanContext ctx = null;
+ Fqn id = Fqn.fromString(cacheNode + "/" +key);
try
{
- cache.remove(cacheNode + "/" + key);
+ if(log.isDebugEnabled())
+ {
+ log.debug("remove: cache id " +id.toString());
+ }
+ cache.remove(id);
}
catch (CacheException e)
{
throw new RuntimeException(e);
}
- if (ctx != null) pool.remove(ctx);
-
+// if (ctx != null) pool.remove(ctx);
}
public void finished(StatefulBeanContext ctx)
@@ -124,18 +163,9 @@
{
ctx.inUse = false;
ctx.lastUsed = System.currentTimeMillis();
- if (ctx.markedForPassivation)
- {
- try
- {
- Fqn fqn = Fqn.fromString(cacheNode + "/" + ctx.getId());
- eviction.passivate(fqn, ctx);
- }
- catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
+ Fqn id = Fqn.fromString(cacheNode + "/" + ctx.getId());
+ // OK, it is free to passivate now.
+ evictRegionManager.unmarkNodeCurrentlyInUse(id);
}
}
@@ -159,18 +189,115 @@
MBeanServer server = MBeanServerLocator.locateJBoss();
ObjectName cacheON = new ObjectName(config.name());
TreeCacheMBean mbean = (TreeCacheMBean) MBeanProxyExt.create(TreeCacheMBean.class, cacheON, server);
- cache = (PassivationTreeCache) mbean.getInstance();
- cacheNode = "/" + container.getEjbName();
- eviction = (StatefulEvictionPolicy) cache.getEvictionPolicy();
- eviction.createRegion(cacheNode, config.maxSize(), config.idleTimeoutSeconds());
+ cache = (TreeCache) mbean.getInstance();
+
+ cacheNode = Fqn.fromString("/" + container.getEjbName() + "/");
+
+ // Try to create an eviction region per ejb
+ evictRegionManager = cache.getEvictionRegionManager();
+ Element element = getElementConfig(cacheNode.toString(), config.idleTimeoutSeconds(),
+ config.maxSize());
+ Region region = evictRegionManager.createRegion(cacheNode, element);
+ log.debug("initialize(): create eviction region: " +region + " for ejb: " +container.getEjbName());
+ }
+
+ protected Element getElementConfig(String regionName, long timeToLiveSeconds, int maxNodes) throws Exception {
+ String xml = "<region name=\"" +regionName +"\" policyClass=\"org.jboss.cache.eviction.LRUPolicy\">\n" +
+ "<attribute name=\"maxNodes\">" +maxNodes +"</attribute>\n" +
+ "<attribute name=\"timeToLiveSeconds\">"+ timeToLiveSeconds +"</attribute>\n" +
+ "</region>";
+ return XmlHelper.stringToElement(xml);
}
public void start()
{
-
+ // register to listen for cache event
+ // TODO this approach may not be scalable when there are many beans since then we will need to go thru
+ // N listeners to figure out which this event this belongs to.
+ listener = new ClusteredStatefulCacheListener();
+ cache.addTreeCacheListener(listener);
}
public void stop()
{
+ // Remove the listener
+ cache.removeTreeCacheListener(listener);
+
+ Option opt = new Option();
+ opt.setCacheModeLocal(true);
+ try {
+ // remove locally.
+ cache.remove(cacheNode, opt);
+ } catch (CacheException e) {
+ log.error("Stop(): can't remove bean from the underlying distributed cache");
+ }
+
+ // Remove the eviction region
+ RegionManager rm = cache.getEvictionRegionManager();
+ rm.removeRegion(cacheNode);
+
+ log.info("stop(): StatefulTreeCache stopped successfully for " +cacheNode);
+ }
+
+ /**
+ * A TreeCacheListener. Note that extends it from AbstractTreeCacheListener is a bit heavy since
+ * it will get all the treecache events (instead of just passivation/activation). But we will have to
+ * wait untill JBossCache2.0 for the refactoring then.
+ */
+ public class ClusteredStatefulCacheListener extends AbstractTreeCacheListener
+ {
+ protected Logger log = Logger.getLogger(ClusteredStatefulCacheListener.class);
+
+ public void nodeActivate(Fqn fqn, boolean pre) {
+ if(pre) return; // we are not interested in postActivate event
+ if(fqn.size() != FQN_SIZE) return;
+ if(!fqn.isChildOrEquals(cacheNode)) return; // don't care about fqn that doesn't belong to me.
+
+ StatefulBeanContext bean = null;
+ try {
+ // TODO Can this cause deadlock in the cache level? Should be ok but need review.
+ bean = (StatefulBeanContext) cache.get(fqn, "bean");
+ } catch (CacheException e) {
+ log.error("nodeActivate(): can't retrieve bean instance from: " +fqn + " with exception: " +e);
+ return;
+ }
+ if(bean == null)
+ {
+ throw new IllegalStateException("StatefuleTreeCache.nodeActivate(): null bean instance.");
+ }
+
+// log.debug("nodeActivate(): send postActivate event on fqn: " +fqn);
+ if(log.isTraceEnabled())
+ {
+ log.trace("nodeActivate(): send postActivate event on fqn: " +fqn);
+ }
+
+ bean.postActivate();
+ }
+
+ public void nodePassivate(Fqn fqn, boolean pre) {
+ if(!pre) return; // we are not interested in postPassivate event
+ if(fqn.size() != FQN_SIZE) return;
+ if(!fqn.isChildOrEquals(cacheNode)) return; // don't care about fqn that doesn't belong to me.
+
+ try {
+ // TODO Can this cause deadlock in the cache level? Should be ok but need review.
+ Node node = cache.get(fqn);
+ StatefulBeanContext bean = (StatefulBeanContext) node.getData().get("bean");
+ if (bean != null)
+ bean.prePassivate();
+
+ } catch (CacheException e) {
+ log.error("nodePassivate(): can't retrieve bean instance from: " +fqn + " with exception: " +e);
+ return;
+ }
+
+// log.debug("nodePassivate(): send prePassivate event on fqn: " +fqn);
+ if(log.isTraceEnabled())
+ {
+ log.trace("nodePassivate(): send prePassivate event on fqn: " +fqn);
+ }
+
+ }
}
}
More information about the jboss-cvs-commits
mailing list