This is an automated email from the ASF dual-hosted git repository.

rmaucher pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 151ca206507047c216a3df5901245c4b1add2c54
Author: opencode <[email protected]>
AuthorDate: Wed Sep 30 22:55:15 2026 +0200

    Correct the calculation of the total number of fragment messages in 
FileMessageFactory which declared one message too many for files whose size is 
an exact multiple of the fragment size, so WAR deployments via FarmWarDeployer 
never completed for those files
---
 .../catalina/ha/deploy/FileMessageFactory.java     |   4 +-
 .../catalina/ha/deploy/TestFileMessageFactory.java | 108 +++++++++++++++++++++
 2 files changed, 111 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/catalina/ha/deploy/FileMessageFactory.java 
b/java/org/apache/catalina/ha/deploy/FileMessageFactory.java
index a0aa8bc2ba..25590922c4 100644
--- a/java/org/apache/catalina/ha/deploy/FileMessageFactory.java
+++ b/java/org/apache/catalina/ha/deploy/FileMessageFactory.java
@@ -146,7 +146,9 @@ public class FileMessageFactory {
             out = new FileOutputStream(f);
         } else {
             size = file.length();
-            totalNrOfMessages = (size / READ_SIZE) + 1;
+            // The number of messages the file is split into. One full message
+            // per READ_SIZE bytes plus one for a non-empty remainder
+            totalNrOfMessages = (size + READ_SIZE - 1) / READ_SIZE;
             in = new FileInputStream(f);
         } // end if
         lastModified = System.currentTimeMillis();
diff --git a/test/org/apache/catalina/ha/deploy/TestFileMessageFactory.java 
b/test/org/apache/catalina/ha/deploy/TestFileMessageFactory.java
new file mode 100644
index 0000000000..b923fed699
--- /dev/null
+++ b/test/org/apache/catalina/ha/deploy/TestFileMessageFactory.java
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.ha.deploy;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.nio.file.Files;
+import java.util.Random;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestFileMessageFactory {
+
+    @Test
+    public void testRoundTripSmallerThanReadSize() throws Exception {
+        roundTripTest(100);
+    }
+
+    @Test
+    public void testRoundTripExactMultipleOfReadSize() throws Exception {
+        roundTripTest(2 * FileMessageFactory.READ_SIZE);
+    }
+
+    @Test
+    public void testRoundTripNonExactMultipleOfReadSize() throws Exception {
+        roundTripTest(2 * FileMessageFactory.READ_SIZE + 100);
+    }
+
+    private void roundTripTest(int size) throws Exception {
+        byte[] content = new byte[size];
+        new Random(12345L).nextBytes(content);
+
+        File source = File.createTempFile("test-source", ".war");
+        File target = File.createTempFile("test-target", ".war");
+        try {
+            try (FileOutputStream out = new FileOutputStream(source)) {
+                out.write(content);
+            }
+
+            FileMessageFactory readFactory = 
FileMessageFactory.getInstance(source, false);
+            FileMessageFactory writeFactory = 
FileMessageFactory.getInstance(target, true);
+
+            FileMessage msg = new FileMessage(null, source.getName(), "/" + 
source.getName());
+            int messageCount = 0;
+            boolean complete = false;
+            msg = readFactory.readMessage(msg);
+            while (msg != null) {
+                messageCount++;
+                complete = writeFactory.writeMessage(msg);
+                msg = readFactory.readMessage(msg);
+            }
+
+            int expectedCount = (size + FileMessageFactory.READ_SIZE - 1) / 
FileMessageFactory.READ_SIZE;
+            Assert.assertEquals("Number of messages", expectedCount, 
messageCount);
+            // The transfer must be signalled complete by the last message,
+            // otherwise the receiving node never deploys the WAR
+            Assert.assertEquals(messageCount > 0, complete);
+            Assert.assertArrayEquals("Target file content", content, 
Files.readAllBytes(target.toPath()));
+        } finally {
+            if (!source.delete()) {
+                System.out.println("Could not delete " + source);
+            }
+            if (!target.delete()) {
+                System.out.println("Could not delete " + target);
+            }
+        }
+    }
+
+    @Test
+    public void testTotalNrOfMsgsSetOnFirstMessage() throws Exception {
+        int size = FileMessageFactory.READ_SIZE;
+        byte[] content = new byte[size];
+
+        File source = File.createTempFile("test-source", ".war");
+        try {
+            try (FileOutputStream out = new FileOutputStream(source)) {
+                out.write(content);
+            }
+
+            FileMessageFactory readFactory = 
FileMessageFactory.getInstance(source, false);
+            FileMessage msg = new FileMessage(null, source.getName(), "/" + 
source.getName());
+            msg = readFactory.readMessage(msg);
+            Assert.assertEquals("Message number", 1, msg.getMessageNumber());
+            Assert.assertEquals("Total number of messages", 1, 
msg.getTotalNrOfMsgs());
+            // One message for one full buffer, so EOF comes next
+            Assert.assertNull("No further message", 
readFactory.readMessage(msg));
+        } finally {
+            if (!source.delete()) {
+                System.out.println("Could not delete " + source);
+            }
+        }
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to