You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2008/09/26 19:37:34 UTC

svn commit: r699427 - in /tomcat/trunk/java/org/apache/catalina/ha/deploy: FarmWarDeployer.java FileMessageFactory.java

Author: markt
Date: Fri Sep 26 10:37:34 2008
New Revision: 699427

URL: http://svn.apache.org/viewvc?rev=699427&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=45851
Correct NPE when cluster is defined at engine level
Ensure that only 1 thread writes the replicated WAR to disk and that the messages containing the WAR are processed in the correct order.

Modified:
    tomcat/trunk/java/org/apache/catalina/ha/deploy/FarmWarDeployer.java
    tomcat/trunk/java/org/apache/catalina/ha/deploy/FileMessageFactory.java

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=699427&r1=699426&r2=699427&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/ha/deploy/FarmWarDeployer.java (original)
+++ tomcat/trunk/java/org/apache/catalina/ha/deploy/FarmWarDeployer.java Fri Sep 26 10:37:34 2008
@@ -158,6 +158,7 @@
         }else {
             engine = (Engine)parent;
             hostname = engine.getDefaultHost();
+            host = (Host) engine.findChild(hostname);
         }
         try {
             oname = new ObjectName(engine.getName() + ":type=Deployer,host="

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=699427&r1=699426&r2=699427&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/ha/deploy/FileMessageFactory.java (original)
+++ tomcat/trunk/java/org/apache/catalina/ha/deploy/FileMessageFactory.java Fri Sep 26 10:37:34 2008
@@ -22,6 +22,9 @@
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.FileNotFoundException;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
 
 /**
  * This factory is used to read files and write files by splitting them up into
@@ -74,7 +77,7 @@
     protected FileOutputStream out;
 
     /**
-     * The number of messages we have read or written
+     * The number of messages we have written
      */
     protected int nrOfMessagesProcessed = 0;
 
@@ -87,6 +90,19 @@
      * The total number of packets that we split this file into
      */
     protected long totalNrOfMessages = 0;
+    
+    /**
+     * The number of the last message procssed. Message IDs are 1 based.
+     */
+    protected AtomicLong lastMessageProcessed = new AtomicLong(0);
+    
+    /**
+     * Messages received out of order are held in the buffer until required. If
+     * everything is worked as expected, messages will spend very little time in
+     * the buffer.
+     */
+    protected Map<Long, FileMessage> msgBuffer =
+        new ConcurrentHashMap<Long, FileMessage>();
 
     /**
      * The bytes that we hold the data in, not thread safe.
@@ -94,6 +110,12 @@
     protected byte[] data = new byte[READ_SIZE];
 
     /**
+     * Flag that indicates if a thread is writing messages to disk. Access to
+     * this flag must be synchronised.
+     */
+    protected boolean isWriting = false;
+
+    /**
      * 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>
@@ -205,25 +227,65 @@
         if (log.isDebugEnabled())
             log.debug("Message " + msg + " data " + msg.getData()
                     + " data length " + msg.getDataLength() + " out " + out);
-        if (out != null) {
-            out.write(msg.getData(), 0, msg.getDataLength());
-            nrOfMessagesProcessed++;
+        
+        if (msg.getMessageNumber() <= lastMessageProcessed.get()) {
+            // Duplicate of message already processed
+            log.warn("Receive Message again -- Sender ActTimeout too short [ path: "
+                    + msg.getContextPath()
+                    + " war: "
+                    + msg.getFileName()
+                    + " data: "
+                    + msg.getData()
+                    + " data length: " + msg.getDataLength() + " ]");
+            return false;
+        }
+        
+        FileMessage previous =
+            msgBuffer.put(new Long(msg.getMessageNumber()), msg);
+        if (previous !=null) {
+            // Duplicate of message not yet processed
+            log.warn("Receive Message again -- Sender ActTimeout too short [ path: "
+                    + msg.getContextPath()
+                    + " war: "
+                    + msg.getFileName()
+                    + " data: "
+                    + msg.getData()
+                    + " data length: " + msg.getDataLength() + " ]");
+            return false;
+        }
+        
+        FileMessage next = null;
+        synchronized (this) {
+            if (!isWriting) {
+                next = msgBuffer.get(new Long(lastMessageProcessed.get() + 1));
+                if (next != null) {
+                    isWriting = true;
+                } else {
+                    return false;
+                }
+            } else {
+                return false;
+            }
+        }
+        
+        while (next != null) {
+            out.write(next.getData(), 0, next.getDataLength());
+            lastMessageProcessed.incrementAndGet();
             out.flush();
-            if (msg.getMessageNumber() == msg.getTotalNrOfMsgs()) {
+            if (next.getMessageNumber() == next.getTotalNrOfMsgs()) {
                 out.close();
                 cleanup();
                 return true;
-            }//end if
-        } else {
-            if (log.isWarnEnabled())
-                log.warn("Receive Message again -- Sender ActTimeout to short [ path: "
-                                + msg.getContextPath()
-                                + " war: "
-                                + msg.getFileName()
-                                + " data: "
-                                + msg.getData()
-                                + " data length: " + msg.getDataLength() + " ]");
+            }
+            synchronized(this) {
+                next =
+                    msgBuffer.get(new Long(lastMessageProcessed.get() + 1));
+                if (next == null) {
+                    isWriting = false;
+                }
+            }
         }
+        
         return false;
     }//writeMessage
 
@@ -248,6 +310,8 @@
         data = null;
         nrOfMessagesProcessed = 0;
         totalNrOfMessages = 0;
+        msgBuffer.clear();
+        lastMessageProcessed = null;
     }
 
     /**
@@ -309,4 +373,4 @@
         return file;
     }
 
-}
\ No newline at end of file
+}



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