Author: kfujino Date: Tue May 7 08:26:30 2013 New Revision: 1479805 URL: http://svn.apache.org/r1479805 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/trunk/java/org/apache/catalina/ha/deploy/FarmWarDeployer.java tomcat/trunk/java/org/apache/catalina/ha/deploy/FileMessageFactory.java tomcat/trunk/java/org/apache/catalina/ha/deploy/mbeans-descriptors.xml tomcat/trunk/webapps/docs/config/cluster-deployer.xml Modified: tomcat/trunk/java/org/apache/catalina/ha/deploy/FarmWarDeployer.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/ha/deploy/FarmWarDeployer.java?rev=1479805&r1=1479804&r2=1479805&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/ha/deploy/FarmWarDeployer.java (original) +++ tomcat/trunk/java/org/apache/catalina/ha/deploy/FarmWarDeployer.java Tue May 7 08:26:30 2013 @@ -126,6 +126,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() { } @@ -298,6 +303,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; @@ -560,11 +566,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(); } } @@ -703,6 +712,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 @@ -737,6 +754,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); if (!dir.isAbsolute()) { Modified: tomcat/trunk/java/org/apache/catalina/ha/deploy/FileMessageFactory.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/ha/deploy/FileMessageFactory.java?rev=1479805&r1=1479804&r2=1479805&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/ha/deploy/FileMessageFactory.java (original) +++ tomcat/trunk/java/org/apache/catalina/ha/deploy/FileMessageFactory.java Tue May 7 08:26:30 2013 @@ -121,6 +121,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> @@ -155,7 +165,7 @@ public class FileMessageFactory { totalNrOfMessages = (size / READ_SIZE) + 1; in = new FileInputStream(f); }//end if - + creationTime = System.currentTimeMillis(); } /** @@ -379,4 +389,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/trunk/java/org/apache/catalina/ha/deploy/mbeans-descriptors.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/ha/deploy/mbeans-descriptors.xml?rev=1479805&r1=1479804&r2=1479805&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/ha/deploy/mbeans-descriptors.xml (original) +++ tomcat/trunk/java/org/apache/catalina/ha/deploy/mbeans-descriptors.xml Tue May 7 08:26:30 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/trunk/webapps/docs/config/cluster-deployer.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/cluster-deployer.xml?rev=1479805&r1=1479804&r2=1479805&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/config/cluster-deployer.xml (original) +++ tomcat/trunk/webapps/docs/config/cluster-deployer.xml Tue May 7 08:26:30 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-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org