[jboss-cvs] JBossAS SVN: r72696 - trunk/tomcat/src/main/org/jboss/web/tomcat/service/session.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Apr 24 15:36:32 EDT 2008


Author: bstansberry at jboss.com
Date: 2008-04-24 15:36:32 -0400 (Thu, 24 Apr 2008)
New Revision: 72696

Added:
   trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/PojoGravitationClusteredSessionValve.java
Modified:
   trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/FieldBasedJBossCacheService.java
   trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/JBossCacheManager.java
   trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/JBossCacheService.java
Log:
[JBAS-5377] Manage clustered caches using a JBC CacheManager
Properly handle data gravitation of shared pojos

Modified: trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/FieldBasedJBossCacheService.java
===================================================================
--- trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/FieldBasedJBossCacheService.java	2008-04-24 19:35:34 UTC (rev 72695)
+++ trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/FieldBasedJBossCacheService.java	2008-04-24 19:36:32 UTC (rev 72696)
@@ -44,6 +44,7 @@
 import org.jboss.cache.pojo.PojoCache;
 import org.jboss.cache.pojo.PojoCacheAlreadyDetachedException;
 import org.jboss.cache.pojo.PojoCacheThreadContext;
+import org.jboss.cache.pojo.impl.InternalConstant;
 import org.jboss.metadata.web.jboss.ReplicationGranularity;
 
 /**
@@ -69,6 +70,12 @@
       setCache(pojoCache_.getCache());
    }
    
+   private Fqn getJBossInternalFqn()
+   {
+      Object[] objs = new Object[]{SESSION, hostName_, webAppPath_, InternalConstant.JBOSS_INTERNAL_STRING };
+      return new Fqn(objs);
+   }
+   
    private Fqn getFieldFqn(String id, String key)
    {
       return getFieldFqn(hostName_, webAppPath_, id, key);
@@ -100,7 +107,6 @@
    public void start(ClassLoader tcl, JBossCacheManager manager)
    {
       super.start(tcl, manager);
-      
       if(manager.isPassivationEnabled())
       {
          initializeFieldGranularityEviction(tcl);
@@ -120,6 +126,11 @@
          evictionRegion_ = null;
       }
    }
+   
+   public final PojoCacheThreadContext getPojoCacheThreadContext()
+   {
+      return pojoCache_.getThreadContext();
+   }
 
    @Override
    public void processEviction()
@@ -265,27 +276,22 @@
     */
    public Object getPojo(String realId, String key)
    {
+      Fqn fqn = getFieldFqn(realId, key);
       if(log_.isTraceEnabled())
       {
-         log_.trace("getPojo(): session id: " +realId + " key: " +key);
+         log_.trace("getPojo(): session id: " +realId + " key: " + key +
+                    " fqn: " + fqn + " gravitationEnabled: " + 
+                    getPojoCacheThreadContext().isGravitationEnabled());
       }
-      // Construct the fqn.
-      Fqn fqn = getFieldFqn(realId, key);
       
-      PojoCacheThreadContext ctx = pojoCache_.getThreadContext();
       try 
       {
-         ctx.setGravitationEnabled(true);
-         return pojoCache_.find(fqn.toString());
+         return pojoCache_.find(fqn);
       }
       catch (CacheException e) 
       {
-         throw new RuntimeException("JBossCacheService: exception occurred in cache getPojo ... ", e);
+         throw new RuntimeException("JBossCacheService: exception occurred in cache find ... ", e);
       }
-      finally
-      {
-         ctx.clear();
-      }
    }
 
    /**
@@ -564,6 +570,7 @@
          if (maxNodes > 0 || maxIdle > 0)
          {
             EvictionPolicyConfig epc = getEvictionPolicyConfig(maxIdle, maxNodes);         
+//            evictionRegion_ = getCache().getRegion(getJBossInternalFqn(), true);           
             evictionRegion_ = getCache().getRegion(getWebappFqn(), true);            
             evictionRegion_.setEvictionPolicy(epc);
             EvictionPolicy policy = evictionRegion_.getEvictionPolicy();

Modified: trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/JBossCacheManager.java
===================================================================
--- trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/JBossCacheManager.java	2008-04-24 19:35:34 UTC (rev 72695)
+++ trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/JBossCacheManager.java	2008-04-24 19:36:32 UTC (rev 72696)
@@ -47,10 +47,13 @@
 import org.apache.catalina.core.ContainerBase;
 import org.jboss.cache.Cache;
 import org.jboss.cache.CacheException;
+import org.jboss.cache.CacheManager;
 import org.jboss.cache.CacheStatus;
 import org.jboss.cache.pojo.PojoCache;
-import org.jboss.ha.cachemanager.CacheManagerLocator;
-import org.jboss.ha.cachemanager.PojoCacheManager;
+import org.jboss.cache.pojo.PojoCacheThreadContext;
+import org.jboss.ha.framework.server.CacheManagerLocator;
+import org.jboss.ha.framework.server.PojoCacheManager;
+import org.jboss.ha.framework.server.PojoCacheManagerLocator;
 import org.jboss.metadata.web.jboss.JBossWebMetaData;
 import org.jboss.metadata.web.jboss.PassivationConfig;
 import org.jboss.metadata.web.jboss.ReplicationConfig;
@@ -795,8 +798,9 @@
             log_.trace("createSession(): active sessions = " + calcActiveSessions() +
                        " and max allowed sessions = " + maxActive_);
          }
-         processExpires();
          
+         processExpires(false);
+         
          if (calcActiveSessions() >= maxActive_)
          {
             // Exceeds limit. We need to reject it.
@@ -1252,6 +1256,7 @@
       synchronized (session)
       {
          boolean doTx = false;      
+         boolean pojoGravLocallyEnabled = establishPojoGravitation();
          try
          {
             // We need transaction so any data gravitation replication 
@@ -1269,6 +1274,11 @@
             SessionReplicationContext.startCacheActivity();
             
             session = proxy_.loadSession(realId, session);
+            
+            if (session != null)
+            {
+               session.initAfterLoad(this);
+            }
          }
          catch (Exception ex)
          {
@@ -1296,14 +1306,14 @@
                   endTransaction(realId);
             }
             finally {
+               if (pojoGravLocallyEnabled)
+                  resetPojoGravitation();
                SessionReplicationContext.finishCacheActivity();
             }
          }
 
          if (session != null)
-         {
-            // Need to initialize.
-            session.initAfterLoad(this);
+         {            
             if (mustAdd)
                add(session, false); // don't replicate
             long elapsed = System.currentTimeMillis() - begin;
@@ -1439,9 +1449,14 @@
    @Override
    protected void processExpires()
    {
+      processExpires(true);
+   }
+   
+   private void processExpires(boolean cleanPojos)
+   {
       boolean expire = maxInactiveInterval_ >= 0;
       boolean passivate = isPassivationEnabled();
-      if (!expire && !passivate)
+      if (cleanPojos && !expire && !passivate)
       {
          // Let the proxy clean up any pojos
          proxy_.processEviction();
@@ -1595,7 +1610,8 @@
       }
       
       // Finally, clean up the pojo region
-      proxy_.processEviction();
+      if (cleanPojos)
+         proxy_.processEviction();
       
       if (trace)
       { 
@@ -2125,7 +2141,19 @@
     * Add a JvmRouteValve and BatchReplicationClusteredSessionValve if needed.
     */
    private void installValves()
-   {  
+   {           
+      // Turn on data gravitation for pojos
+      if (replicationGranularity_ == ReplicationGranularity.FIELD)
+      {
+         FieldBasedJBossCacheService fieldProxy = getPojoCacheService();
+         if (fieldProxy.isBuddyReplicationEnabled())
+         {
+            Valve pojoGrav = new PojoGravitationClusteredSessionValve(fieldProxy);
+            log_.debug("Adding PojoGravitationClusteredSessionValve for pojo data gravitation.");
+            installContextValve(pojoGrav);            
+         }
+      }
+      
       // If JK usage wasn't explicitly configured, default to enabling
       // it if jvmRoute is set on our containing Engine
       if (useJK_ == null)
@@ -2137,13 +2165,13 @@
       {
          log_.debug("We are using JK for load-balancing. Adding JvmRouteValve.");         
          installContextValve(new JvmRouteValve(this));         
-      }
-         
+      }   
+      
       // Add batch replication valve if needed.
       // TODO -- should we add this even if not FIELD in case a cross-context
-      // call traverses a field-based webapp?
-      if (replicationGranularity_ == ReplicationGranularity.FIELD &&
-          Boolean.TRUE.equals(replicationFieldBatchMode_))
+      // call traverses a field-based webapp?     
+      if (replicationGranularity_ == ReplicationGranularity.FIELD
+            && Boolean.TRUE.equals(replicationFieldBatchMode_))
       {
          Valve batchValve = new BatchReplicationClusteredSessionValve(this);
          log_.debug("Adding BatchReplicationClusteredSessionValve for batch replication.");
@@ -2152,6 +2180,7 @@
 
       // Add clustered session valve
       ClusteredSessionValve valve = new ClusteredSessionValve();
+      log_.debug("Adding ClusteredSessionValve");
       installContextValve(valve);
    }
 
@@ -2268,12 +2297,43 @@
       }
    }
    
+   private boolean establishPojoGravitation()
+   {
+      boolean changed = false;
+      if (replicationGranularity_ == ReplicationGranularity.FIELD )
+      {
+         FieldBasedJBossCacheService pojoProxy = getPojoCacheService();
+         PojoCacheThreadContext ctx = pojoProxy.getPojoCacheThreadContext();
+         if (pojoProxy.isBuddyReplicationEnabled() && !ctx.isGravitationEnabled())
+         {
+            log_.trace("enabling pojo data gravitation");
+            ctx.setGravitationEnabled(true);
+            changed = true;
+         }
+      }
+      return changed;
+   }
+   
+   private void resetPojoGravitation()
+   {
+      log_.trace("disabling pojo data gravitation");
+      getPojoCacheService().getPojoCacheThreadContext().setGravitationEnabled(false);
+   }
+   
    public void releaseCacheToManager()
    {      
       try
       {
-         PojoCacheManager pcm = CacheManagerLocator.getCacheManagerLocator().getCacheManager(null);
-         pcm.releaseCache(cacheConfigName_);
+         if (replicationGranularity_ == ReplicationGranularity.FIELD)
+         {
+            PojoCacheManager pcm = PojoCacheManagerLocator.getCacheManagerLocator().getCacheManager(null);
+            pcm.releaseCache(cacheConfigName_);
+         }
+         else
+         {
+            CacheManager cm = CacheManagerLocator.getCacheManagerLocator().getCacheManager(null);
+            cm.releaseCache(cacheConfigName_);
+         }
       }
       catch (Exception e)
       {

Modified: trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/JBossCacheService.java
===================================================================
--- trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/JBossCacheService.java	2008-04-24 19:35:34 UTC (rev 72695)
+++ trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/JBossCacheService.java	2008-04-24 19:36:32 UTC (rev 72696)
@@ -40,6 +40,7 @@
 import org.jboss.cache.Node;
 import org.jboss.cache.Region;
 import org.jboss.cache.buddyreplication.BuddyManager;
+import org.jboss.cache.config.BuddyReplicationConfig;
 import org.jboss.cache.config.CacheLoaderConfig;
 import org.jboss.cache.pojo.impl.InternalConstant;
 import org.jboss.cache.transaction.BatchModeTransactionManager;
@@ -162,6 +163,12 @@
             log_.debug("UseMarshalling is true. We will register the fqn: " +
                         pathFqn + " with class loader" +tcl +
                         " and activate the webapp's Region");
+            Node root = plainCache_.getRoot();
+            if (root.hasChild(pathFqn) == false)
+            {
+               plainCache_.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
+               root.addChild(pathFqn);
+            }
             Region region = plainCache_.getRegion(pathFqn, true);
             region.registerContextClassLoader(tcl);
             region.activate(); 
@@ -551,6 +558,12 @@
          }
       }
    }
+   
+   public boolean isBuddyReplicationEnabled()
+   {
+      BuddyReplicationConfig brc = plainCache_.getConfiguration().getBuddyReplicationConfig();
+      return brc != null && brc.isEnabled();
+   }
  
    public boolean isCachePassivationEnabled()
    {

Added: trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/PojoGravitationClusteredSessionValve.java
===================================================================
--- trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/PojoGravitationClusteredSessionValve.java	                        (rev 0)
+++ trunk/tomcat/src/main/org/jboss/web/tomcat/service/session/PojoGravitationClusteredSessionValve.java	2008-04-24 19:36:32 UTC (rev 72696)
@@ -0,0 +1,120 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.web.tomcat.service.session;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+import org.apache.catalina.Lifecycle;
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.LifecycleListener;
+import org.apache.catalina.connector.Request;
+import org.apache.catalina.connector.Response;
+import org.apache.catalina.util.LifecycleSupport;
+import org.apache.catalina.valves.ValveBase;
+import org.jboss.cache.pojo.PojoCacheThreadContext;
+import org.jboss.logging.Logger;
+
+/**
+ * This Valve turns on PojoCache data gravitation for the span of the request.
+ *
+ * @author Brian Stansberry
+ * @version $Revision: 60079 $
+ */
+public class PojoGravitationClusteredSessionValve extends ValveBase implements Lifecycle
+{
+   private static final Logger log = Logger.getLogger(PojoGravitationClusteredSessionValve.class);
+   
+   // The info string for this Valve
+   private static final String info = "PojoGravitationClusteredSessionValve/1.0";
+
+   // Valve-lifecycle_ helper object
+   protected LifecycleSupport support = new LifecycleSupport(this);
+
+   protected FieldBasedJBossCacheService pojoSvc;
+
+   /**
+    * Create a new Valve.
+    *
+    */
+   public PojoGravitationClusteredSessionValve(FieldBasedJBossCacheService pojoSvc)
+   {
+      super();
+      this.pojoSvc = pojoSvc;
+   }
+
+   /**
+    * Get information about this Valve.
+    */
+   public String getInfo()
+   {
+      return info;
+   }
+
+   public void invoke(Request request, Response response) throws IOException, ServletException
+   {
+      PojoCacheThreadContext ctx = pojoSvc.getPojoCacheThreadContext();
+      boolean enabled = ctx.isGravitationEnabled();
+      try
+      {
+         if (!enabled)
+         {
+            log.trace("enabled pojo data gravitation");
+            ctx.setGravitationEnabled(true);
+         }
+         getNext().invoke(request, response);
+      }
+      finally
+      {         
+         if (!enabled)
+            ctx.setGravitationEnabled(false);
+      }
+   }
+
+   // Lifecycle-interface
+   public void addLifecycleListener(LifecycleListener listener)
+   {
+      support.addLifecycleListener(listener);
+   }
+
+   public void removeLifecycleListener(LifecycleListener listener)
+   {
+      support.removeLifecycleListener(listener);
+   }
+
+   public LifecycleListener[] findLifecycleListeners()
+   {
+      return support.findLifecycleListeners();
+   }
+
+   public void start() throws LifecycleException
+   {
+      support.fireLifecycleEvent(START_EVENT, this);
+   }
+
+   public void stop() throws LifecycleException
+   {
+      support.fireLifecycleEvent(STOP_EVENT, this);
+   }
+
+}




More information about the jboss-cvs-commits mailing list