Author: mriou
Date: Wed Jun  4 10:16:48 2008
New Revision: 663313

URL: http://svn.apache.org/viewvc?rev=663313&view=rev
Log:
In-mem DAO leak fix has been lost in translation between 1.1 and trunk.

Modified:
    
ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/memdao/BpelDAOConnectionImpl.java
    
ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/memdao/ProcessDaoImpl.java

Modified: 
ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/memdao/BpelDAOConnectionImpl.java
URL: 
http://svn.apache.org/viewvc/ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/memdao/BpelDAOConnectionImpl.java?rev=663313&r1=663312&r2=663313&view=diff
==============================================================================
--- 
ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/memdao/BpelDAOConnectionImpl.java
 (original)
+++ 
ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/memdao/BpelDAOConnectionImpl.java
 Wed Jun  4 10:16:48 2008
@@ -55,7 +55,7 @@
  */
 class BpelDAOConnectionImpl implements BpelDAOConnection {
     private static final Log __log = 
LogFactory.getLog(BpelDAOConnectionImpl.class);
-    public static long TIME_TO_LIVE = 10*60*1000;
+    public static long TIME_TO_LIVE = 2*60*1000;
 
     private TransactionManager _txm;
 
@@ -220,7 +220,7 @@
     private void cleanupDeadWood() {
         long now = System.currentTimeMillis();
         
-        if (now  > _lastRemoval + (TIME_TO_LIVE/10)) {
+        if (now  > _lastRemoval + (TIME_TO_LIVE/4)) {
             _lastRemoval = now;
             
             synchronized (_mexStore) {

Modified: 
ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/memdao/ProcessDaoImpl.java
URL: 
http://svn.apache.org/viewvc/ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/memdao/ProcessDaoImpl.java?rev=663313&r1=663312&r2=663313&view=diff
==============================================================================
--- 
ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/memdao/ProcessDaoImpl.java
 (original)
+++ 
ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/memdao/ProcessDaoImpl.java
 Wed Jun  4 10:16:48 2008
@@ -42,6 +42,7 @@
  */
 class ProcessDaoImpl extends DaoBaseImpl implements ProcessDAO {
     private static final Log __log = LogFactory.getLog(ProcessDaoImpl.class);
+    public static long TIME_TO_LIVE = 2*60*1000;
 
     private QName _processId;
     private QName _type;
@@ -51,17 +52,17 @@
     protected final Map<Long, Long> _instancesAge = new 
ConcurrentHashMap<Long, Long>();
     protected final Map<Integer, PartnerLinkDAO> _plinks = new 
ConcurrentHashMap<Integer, PartnerLinkDAO>();
     private Map<QName, ProcessDaoImpl> _store;
-    private final BpelDAOConnectionImpl _conn;
+    private BpelDAOConnectionImpl _conn;
+    private int _executionCount = 0;
     private Collection<Long> _instancesToRemove = new 
ConcurrentLinkedQueue<Long>();
     private static volatile long _lastRemoval = 0;
 
-
     private String _guid;
 
     public ProcessDaoImpl(BpelDAOConnectionImpl conn, Map<QName, 
ProcessDaoImpl> store,
                           QName processId, QName type, String guid, long 
version) {
         if (__log.isDebugEnabled()) {
-            __log.debug("Creating ProcessDao object for process \"" + 
processId + "\". (conn=" + conn + ")");
+            __log.debug("Creating ProcessDao object for process \"" + 
processId + "\".");
         }
 
         _guid = guid;
@@ -108,25 +109,12 @@
         _conn.defer(new Runnable() {
             public void run() {
                 _instances.put(newInstance.getInstanceId(), newInstance);
+                _instancesAge.put(newInstance.getInstanceId(), 
System.currentTimeMillis());
             }
         });
-        long now = System.currentTimeMillis();
-
-        // Checking for old instances that could still be around because of a 
failure
-        // or completion problem
-        if (now > _lastRemoval + (BpelDAOConnectionImpl.TIME_TO_LIVE/10)) {
-            _lastRemoval = now;
-            Object[] oldInstances = _instancesAge.keySet().toArray();
-            for (int i=oldInstances.length-1; i>0; i--) {
-                Long old = (Long) oldInstances[i];
-                Long age = _instancesAge.get(old);
-                if (age != null && now-age > 
BpelDAOConnectionImpl.TIME_TO_LIVE) {
-                    _instances.remove(old);
-                    _instancesAge.remove(old);
-                }
-            }
-        }
 
+        discardOldInstances();
+        
         // Removing right away on rollback
         final Long iid = newInstance.getInstanceId();
         _conn.onRollback(new Runnable() {
@@ -136,6 +124,7 @@
             }
         });
 
+        _executionCount++;
         return newInstance;
     }
 
@@ -156,7 +145,7 @@
     public void instanceCompleted(ProcessInstanceDAO instance) {
         // Cleaning up
         if (__log.isDebugEnabled())
-        __log.debug("Removing completed process instance " + 
instance.getInstanceId() + " from in-memory store.");
+          __log.debug("Removing completed process instance " + 
instance.getInstanceId() + " from in-memory store.");
         _instancesAge.remove(instance.getInstanceId());
         ProcessInstanceDAO removed = 
_instances.remove(instance.getInstanceId());
         if (removed == null) {
@@ -203,7 +192,8 @@
     }
 
     public int getNumInstances() {
-        return _instances.size();
+        // Instances are removed after execution, using a counter instead
+        return _executionCount;
     }
 
     public ProcessInstanceDAO getInstanceWithLock(Long iid) {
@@ -225,4 +215,24 @@
     public void setGuid(String guid) {
         _guid = guid;
     }
+
+    /**
+     * Discard in-memory instances that exceeded their time-to-live to prevent 
memory leaks
+     */
+    void discardOldInstances() {
+        long now = System.currentTimeMillis();
+        if (now > _lastRemoval + (TIME_TO_LIVE / 4)) {
+            _lastRemoval = now;
+            Object[] oldInstances = _instancesAge.keySet().toArray();
+            for (int i=oldInstances.length-1; i>=0; i--) {
+                Long id = (Long) oldInstances[i];
+                Long age = _instancesAge.get(id);
+                if (age != null && now-age > TIME_TO_LIVE) {
+                    __log.warn("Discarding in-memory instance "+id+" because 
it exceeded its time-to-live: "+_instances.get(id));
+                    _instances.remove(id);
+                    _instancesAge.remove(id);
+                }
+            }
+        }
+    }
 }


Reply via email to