You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by kf...@apache.org on 2013/05/07 10:36:40 UTC

svn commit: r1479807 - in /tomcat/tc7.0.x/trunk: java/org/apache/catalina/ha/deploy/ webapps/docs/ webapps/docs/config/

Author: kfujino
Date: Tue May  7 08:36:39 2013
New Revision: 1479807

URL: http://svn.apache.org/r1479807
Log:
Avoid FileMessageFactory leak.
FileMessageFactory will be removed immediately after receiving the complete WAR file but when failing to receive a FileMessage which was sent dividing, FileMessageFactory will leak without being removed.
Add a newly maxValidTime attribute to prevent the leak of FileMessageFactory.
FileMessageFactory that is leaking will be automatically removed after maxValidTime.

Modified:
    tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/FarmWarDeployer.java
    tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/FileMessageFactory.java
    tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/mbeans-descriptors.xml
    tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml
    tomcat/tc7.0.x/trunk/webapps/docs/config/cluster-deployer.xml

Modified: tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/FarmWarDeployer.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/FarmWarDeployer.java?rev=1479807&r1=1479806&r2=1479807&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/FarmWarDeployer.java (original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/FarmWarDeployer.java Tue May  7 08:36:39 2013
@@ -137,6 +137,11 @@ public class FarmWarDeployer extends Clu
      */
     protected ObjectName oname = null;
 
+    /**
+     * The maximum valid time(in seconds) for FileMessageFactory.
+     */
+    protected int maxValidTime = 5 * 60;
+
     /*--Constructor---------------------------------------------*/
     public FarmWarDeployer() {
     }
@@ -334,6 +339,7 @@ public class FarmWarDeployer extends Clu
         FileMessageFactory factory = fileFactories.get(msg.getFileName());
         if (factory == null) {
             factory = FileMessageFactory.getInstance(writeToFile, true);
+            factory.setMaxValidTime(maxValidTime);
             fileFactories.put(msg.getFileName(), factory);
         }
         return factory;
@@ -619,11 +625,14 @@ public class FarmWarDeployer extends Clu
      */
     @Override
     public void backgroundProcess() {
-        if (started && watchEnabled) {
-            count = (count + 1) % processDeployFrequency;
-            if (count == 0) {
-                watcher.check();
+        if (started) {
+            if (watchEnabled) {
+                count = (count + 1) % processDeployFrequency;
+                if (count == 0) {
+                    watcher.check();
+                }
             }
+            removeInvalidFileFactories();
         }
 
     }
@@ -762,6 +771,14 @@ public class FarmWarDeployer extends Clu
         this.processDeployFrequency = processExpiresFrequency;
     }
 
+    public int getMaxValidTime() {
+        return maxValidTime;
+    }
+
+    public void setMaxValidTime(int maxValidTime) {
+        this.maxValidTime = maxValidTime;
+    }
+
     /**
      * Copy a file to the specified temp directory.
      * @param from copy from temp
@@ -796,6 +813,16 @@ public class FarmWarDeployer extends Clu
         return true;
     }
 
+    protected void removeInvalidFileFactories() {
+        String[] fileNames = fileFactories.keySet().toArray(new String[0]);
+        for (String fileName : fileNames) {
+            FileMessageFactory factory = fileFactories.get(fileName);
+            if (!factory.isValid()) {
+                fileFactories.remove(fileName);
+            }
+        }
+    }
+
     private File getAbsolutePath(String path) {
         File dir = new File(path);
         File base = new File(System.getProperty(Globals.CATALINA_BASE_PROP));

Modified: tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/FileMessageFactory.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/FileMessageFactory.java?rev=1479807&r1=1479806&r2=1479807&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/FileMessageFactory.java (original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/FileMessageFactory.java Tue May  7 08:36:39 2013
@@ -122,6 +122,16 @@ public class FileMessageFactory {
     protected boolean isWriting = false;
 
     /**
+     * The time this instance was created. (in milliseconds)
+     */
+    protected long creationTime = 0;
+
+    /**
+     * The maximum valid time(in seconds) from creationTime.
+     */
+    protected int maxValidTime = -1;
+
+    /**
      * Private constructor, either instantiates a factory to read or write. <BR>
      * When openForWrite==true, then a the file, f, will be created and an
      * output stream is opened to write to it. <BR>
@@ -156,7 +166,7 @@ public class FileMessageFactory {
             totalNrOfMessages = (size / READ_SIZE) + 1;
             in = new FileInputStream(f);
         }//end if
-
+        creationTime = System.currentTimeMillis();
     }
 
     /**
@@ -381,4 +391,24 @@ public class FileMessageFactory {
         return file;
     }
 
+    public boolean isValid() {
+        if (maxValidTime > 0) {
+            long timeNow = System.currentTimeMillis();
+            int timeIdle = (int) ((timeNow - creationTime) / 1000L);
+            if (timeIdle > maxValidTime) {
+                cleanup();
+                return false;
+            }
+        }
+        return true;
+    }
+
+    public int getMaxValidTime() {
+        return maxValidTime;
+    }
+
+    public void setMaxValidTime(int maxValidTime) {
+        this.maxValidTime = maxValidTime;
+    }
+
 }

Modified: tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/mbeans-descriptors.xml
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/mbeans-descriptors.xml?rev=1479807&r1=1479806&r2=1479807&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/mbeans-descriptors.xml (original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/catalina/ha/deploy/mbeans-descriptors.xml Tue May  7 08:36:39 2013
@@ -43,5 +43,9 @@
       name="processDeployFrequency"
       description="Frequency of the Farm watchDir check."
       type="int"/>
+    <attribute
+      name="maxValidTime"
+      description="The maximum valid time of FileMessageFactory."
+      type="int"/>
   </mbean>
 </mbeans-descriptors>

Modified: tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml?rev=1479807&r1=1479806&r2=1479807&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Tue May  7 08:36:39 2013
@@ -55,6 +55,17 @@
   They eventually become mixed with the numbered issues. (I.e., numbered
   issues to not "pop up" wrt. others).
 -->
+<section name="Tomcat 7.0.41 (markt)">
+  <subsection name="Cluster">
+    <changelog>
+      <add>
+        Add <code>maxValidTime</code> attribute to prevent the leak of
+        <code>FileMessageFactory</code> in <code>FarmWarDeployer</code>.
+        (kfujino)
+      </add>
+    </changelog>
+  </subsection>
+</section>
 <section name="Tomcat 7.0.40 (markt)">
   <subsection name="Catalina">
     <changelog>

Modified: tomcat/tc7.0.x/trunk/webapps/docs/config/cluster-deployer.xml
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/webapps/docs/config/cluster-deployer.xml?rev=1479807&r1=1479806&r2=1479807&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/webapps/docs/config/cluster-deployer.xml (original)
+++ tomcat/tc7.0.x/trunk/webapps/docs/config/cluster-deployer.xml Tue May  7 08:36:39 2013
@@ -83,6 +83,16 @@
         <strong>Note: </strong> if <strong>watchEnabled</strong> is false, this
         attribute will have no effect.
       </attribute>
+      <attribute name="maxValidTime" required="false">
+        The maximum valid time(in seconds) of FileMessageFactory.
+        FileMessageFactory will be removed immediately after receiving the
+        complete WAR file but when failing to receive a FileMessage which was
+        sent dividing, FileMessageFactory will leak without being removed.
+        FileMessageFactory that is leaking will be automatically removed after
+        maxValidTime. If a negative value specified, FileMessageFactory will
+        never be removed. If the attribute is not provided, a default of 300
+        seconds (5 minutes) is used.
+      </attribute>
     </attributes>
 
   </subsection>



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org