[jboss-remoting-commits] JBoss Remoting SVN: r5169 - remoting2/branches/2.2/src/main/org/jboss/remoting.

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Sun May 10 01:36:47 EDT 2009


Author: ron.sigal at jboss.com
Date: 2009-05-10 01:36:45 -0400 (Sun, 10 May 2009)
New Revision: 5169

Modified:
   remoting2/branches/2.2/src/main/org/jboss/remoting/Lease.java
Log:
(1) JBREM-1128: Adopted use of leasePingerId; (2) JBREM-1129: adopted use of time stamp.

Modified: remoting2/branches/2.2/src/main/org/jboss/remoting/Lease.java
===================================================================
--- remoting2/branches/2.2/src/main/org/jboss/remoting/Lease.java	2009-05-10 05:23:37 UTC (rev 5168)
+++ remoting2/branches/2.2/src/main/org/jboss/remoting/Lease.java	2009-05-10 05:36:45 UTC (rev 5169)
@@ -25,6 +25,7 @@
 import org.jboss.remoting.util.TimerUtil;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.TimerTask;
@@ -48,8 +49,12 @@
    private long leaseWindow = -1;
    private long pingStart = -1;
    private Map clientLeases = null;
+   private Object lock = new Object();
+   private String leasePingerId;
+   private boolean stopped;
 
    private boolean leaseUpdated = false;
+   private long lastUpdate;
 
    private static final Logger log = Logger.getLogger(Lease.class);
    private static final boolean isTraceEnabled = log.isTraceEnabled();
@@ -64,6 +69,11 @@
       if(requestPayload != null)
       {
          this.requestPayload = (Map)requestPayload.get(ClientHolder.CLIENT_HOLDER_KEY);
+         this.leasePingerId = (String) requestPayload.get(LeasePinger.LEASE_PINGER_ID);
+         this.lastUpdate = Long.valueOf((String) requestPayload.get(LeasePinger.TIME_STAMP)).longValue();
+         if (isTraceEnabled) log.trace(this + " initialized with lastUpdate: " + lastUpdate);
+         if (isTraceEnabled) log.trace(this + " initialized with requestPayload: " + this.requestPayload);
+         if (isTraceEnabled) log.trace("leasePingerId: " + leasePingerId);
       }
       this.leaseWindow = leasePeriod * 2;
       this.clientLeases = clientLeases;
@@ -84,9 +94,46 @@
    {
       if(requestMap != null)
       {
-         this.requestPayload = (Map)requestMap.get(ClientHolder.CLIENT_HOLDER_KEY);
+         if (isTraceEnabled) log.trace(this + " requestMap: " + requestMap);
+         long time = 0;
+         String timeString = (String) requestMap.get(LeasePinger.TIME_STAMP);
+         if (isTraceEnabled) log.trace(this + " timeString: " + timeString);
+         time = Long.valueOf(timeString).longValue();
+         if (isTraceEnabled) log.trace(this + " last update: " + lastUpdate + ", this update: " + time);
+         
+         if (time > lastUpdate)
+         {
+            synchronized (lock)
+            {
+               this.requestPayload = (Map)requestMap.get(ClientHolder.CLIENT_HOLDER_KEY);
+
+               if (isTraceEnabled) log.trace(this + " updating: new Client list:");
+               Collection clientHoldersCol = requestPayload.values();
+               Iterator itr = clientHoldersCol.iterator();
+               while (itr.hasNext())
+               {
+                  Object val = itr.next();
+                  if (val != null && val instanceof ClientHolder)
+                  {
+                     ClientHolder clientHolder = (ClientHolder) val;
+                     if (isTraceEnabled) log.trace(leasePingerId + ":  " + clientHolder.getSessionId());
+                  }
+               }
+
+               lastUpdate = time;
+               updateLease(leasePeriod);
+            }
+         }
+         else
+         {
+            if (isTraceEnabled) log.trace(this + " updating lease but not client list");
+            leaseUpdated = true;
+         }
       }
-      updateLease(leasePeriod);
+      else
+      {
+         if (isTraceEnabled) log.trace(this + " requestPayload == null");
+      }
    }
 
    public void updateLease(long leasePeriod)
@@ -124,13 +171,14 @@
 
    public void terminateLease(String sessionId)
    {
-      if(isTraceEnabled)
-      {
-         log.trace("Terminating lease for session id " + sessionId);
-      }
       // is this terminate for all clients
       if (clientSessionId.equals(sessionId))
       {
+         if(isTraceEnabled)
+         {
+            log.trace(this + " Terminating lease group for session id " + sessionId);
+         }
+         
          stopLease();
          // should be ok to call this will null as all the client should have
          // already been disconnected and there been a notification for each
@@ -140,33 +188,101 @@
       }
       else
       {
+         if(isTraceEnabled)
+         {
+            log.trace(this + " Terminating individual lease for session id " + sessionId);
+         }
          notifyClientTermination(sessionId);
       }
    }
-
+   
+   public void terminateLeaseUponFailure(String sessionId)
+   {
+      // is this terminate for all clients
+      if (clientSessionId.equals(sessionId))
+      {
+         if(isTraceEnabled)
+         {
+            log.trace(this + " Terminating lease group for session id " + sessionId);
+         }
+         
+         stopLease();
+         // should be ok to call this will null as all the client should have
+         // already been disconnected and there been a notification for each
+         // of these client disconnections (which would remove the client from
+         // the lease, thus leaving the collection empty
+         notifyClientLost();
+      }
+      else
+      {
+         if(true)
+         {
+            log.warn(this + " Expected invoker session id: " + sessionId);
+         }
+         notifyClientLost();
+      }
+   }
+   
+   public String toString()
+   {
+      String hash = Integer.toHexString(System.identityHashCode(this));
+      return "Lease[" + hash + ":" + clientSessionId + ":" + leasePingerId + "]";
+   }
+   
    private void notifyClientTermination(String sessionId)
    {
-      // is for a particular client, so need to inspect request payload for client
-      if (requestPayload != null)
+      Map localRequestPayload = null;
+      synchronized (lock)
       {
+         if (requestPayload != null)
+         {  
+            localRequestPayload = new HashMap(requestPayload);
+            if (sessionId != null)
+            {
+               requestPayload.remove(sessionId);
+            }
+         }
+      }
+      
+      if (localRequestPayload != null)
+      {  
          // should notify for one client or all?
          if (sessionId != null)
          {
-            Object clientHolderObj = requestPayload.remove(sessionId);
+            synchronized (lock)
+            {
+               if (stopped)
+               {
+                  if (isTraceEnabled) log.trace(this + " already stopped");
+                  return;
+               }
+            }
+            
+            Object clientHolderObj =  localRequestPayload.get(sessionId);
             if (clientHolderObj != null && clientHolderObj instanceof ClientHolder)
             {
                ClientHolder clientHolder = (ClientHolder) clientHolderObj;
                notifier.connectionTerminated(locatorURL, clientHolder.getSessionId(), clientHolder.getConfig());
                if(isTraceEnabled)
                {
-                  log.trace("Notified connection listener of lease termination due to disconnect from client (client session id = " + clientHolder.getSessionId());
+                  log.trace(this + " Notified connection listener of lease termination due to disconnect from client (client session id = " + clientHolder.getSessionId());
                }
             }
          }
          else
          {
+            synchronized (lock)
+            {
+               if (stopped)
+               {
+                  if (isTraceEnabled) log.trace(this + " already stopped");
+                  return;
+               }
+               stopped = true;
+            }
+            
             // loop through and notify for all clients
-            Collection clientHoldersCol = requestPayload.values();
+            Collection clientHoldersCol = localRequestPayload.values();
             if (clientHoldersCol != null && clientHoldersCol.size() > 0)
             {
                Iterator itr = clientHoldersCol.iterator();
@@ -179,7 +295,7 @@
                      notifier.connectionTerminated(locatorURL, clientHolder.getSessionId(), clientHolder.getConfig());
                      if(isTraceEnabled)
                      {
-                        log.trace("Notified connection listener of lease termination due to disconnect from client (client session id = " + clientHolder.getSessionId());
+                        log.trace(this + " Notified connection lif (isTraceEnabled) log.tracef lease termination due to disconnect from client (client session id = " + clientHolder.getSessionId());
                      }
                   }
                }
@@ -188,17 +304,32 @@
       }
       else
       {
-         log.warn("Tried to terminate lease for session id " + sessionId + ", but no collection of clients have been set.");
+         log.warn(this + " Tried to terminate lease for session id " + sessionId + ", but no collection of clients have been set.");
       }
    }
 
    private void notifyClientLost()
    {
-      // is not for a particular client (but all clients associated with client invoker), so need to inspect request payload for client
-      if (requestPayload != null)
+      Map localRequestPayload = null;
+      synchronized (lock)
       {
+         if (stopped)
+         {
+            if (isTraceEnabled) log.trace(this + " already stopped");
+            return;
+         }
+         stopped = true;
+         if (requestPayload != null)
+         {  
+            localRequestPayload = new HashMap(requestPayload);
+         }
+      }
+      
+      if (localRequestPayload != null)
+      {
          // loop through and notify for all clients
-         Collection clientHoldersCol = requestPayload.values();
+         Collection clientHoldersCol = localRequestPayload.values();
+         if (isTraceEnabled) log.trace(this + " notifying listeners about " + clientHoldersCol.size() + " expired client(s)");
          if (clientHoldersCol != null && clientHoldersCol.size() > 0)
          {
             Iterator itr = clientHoldersCol.iterator();
@@ -211,7 +342,7 @@
                   notifier.connectionLost(locatorURL, clientHolder.getSessionId(), clientHolder.getConfig());
                   if(isTraceEnabled)
                   {
-                     log.trace("Notified connection listener of lease expired due to lost connection from client (client session id = " + clientHolder.getSessionId());
+                     log.trace(this + " Notified connection listener of lease expired due to lost connection from client (client session id = " + clientHolder.getSessionId());
                   }
                }
             }
@@ -219,11 +350,16 @@
       }
       else
       {
-         log.debug("requestPayload == null, calling ConnectionNotifier.connectionTerminated()");
-         notifier.connectionTerminated(locatorURL, clientSessionId, null);
+         if (isTraceEnabled) log.trace(this + " requestPayload == null, calling ConnectionNotifier.connectionLost()");
+         notifier.connectionLost(locatorURL, clientSessionId, null);
       }
    }
 
+   protected String getLeasePingerId()
+   {
+      return leasePingerId;
+   }
+
    private void stopLease()
    {
       leaseTimerTask.cancel();
@@ -245,14 +381,14 @@
          {
             try
             {
-               if (log.isTraceEnabled()) log.trace("did not receive ping: " + clientSessionId);
+               if (isTraceEnabled) log.trace(Lease.this + " did not receive ping: " + clientSessionId);
                stopLease();
                notifyClientLost();
                if (clientLeases != null)
                {
                   clientLeases.remove(clientSessionId);
                }
-               if (log.isTraceEnabled()) log.trace("removed lease:" + clientSessionId);
+               if (isTraceEnabled) log.trace(Lease.this + " removed lease:" + clientSessionId);
             }
             catch (Throwable thr)
             {




More information about the jboss-remoting-commits mailing list