This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push:
new 318032a608 Validate sendAllSessionsSize in DeltaManager (#1042)
318032a608 is described below
commit 318032a608cba179a6049673ecfe01bf97f844f1
Author: lihongyi87 <[email protected]>
AuthorDate: Tue Aug 18 16:18:44 2026 +0800
Validate sendAllSessionsSize in DeltaManager (#1042)
Reject a sendAllSessionsSize of zero or less with an
IllegalArgumentException in the setter and document that the value
must be a positive integer. Previously a non-positive value caused
an infinite loop or a NegativeArraySizeException in the batching
loop of handleGET_ALL_SESSIONS.
---
.../apache/catalina/ha/session/DeltaManager.java | 8 +-
.../catalina/ha/session/LocalStrings.properties | 1 +
.../catalina/ha/session/TestDeltaManagerBatch.java | 136 +++++++++++++++++++++
webapps/docs/changelog.xml | 7 ++
webapps/docs/config/cluster-manager.xml | 2 +
5 files changed, 153 insertions(+), 1 deletion(-)
diff --git a/java/org/apache/catalina/ha/session/DeltaManager.java
b/java/org/apache/catalina/ha/session/DeltaManager.java
index 9423498ddc..f6a8269735 100644
--- a/java/org/apache/catalina/ha/session/DeltaManager.java
+++ b/java/org/apache/catalina/ha/session/DeltaManager.java
@@ -438,9 +438,15 @@ public class DeltaManager extends ClusterManagerBase {
/**
* Set the batch size for sending all sessions.
*
- * @param sendAllSessionsSize The batch size value
+ * @param sendAllSessionsSize The batch size value. Must be a positive
integer.
+ *
+ * @throws IllegalArgumentException if the batch size is not a positive
integer
*/
public void setSendAllSessionsSize(int sendAllSessionsSize) {
+ if (sendAllSessionsSize <= 0) {
+ throw new IllegalArgumentException(
+ sm.getString("deltaManager.sendAllSessionsSize.invalid",
Integer.valueOf(sendAllSessionsSize)));
+ }
this.sendAllSessionsSize = sendAllSessionsSize;
}
diff --git a/java/org/apache/catalina/ha/session/LocalStrings.properties
b/java/org/apache/catalina/ha/session/LocalStrings.properties
index 0b6a7b39fb..c01fd27f79 100644
--- a/java/org/apache/catalina/ha/session/LocalStrings.properties
+++ b/java/org/apache/catalina/ha/session/LocalStrings.properties
@@ -57,6 +57,7 @@ deltaManager.receiveMessage.unloadingAfter=Manager [{0}]:
unloading sessions com
deltaManager.receiveMessage.unloadingBegin=Manager [{0}]: start unloading
sessions
deltaManager.registerCluster=Register manager [{0}] to cluster element [{1}]
with name [{2}]
deltaManager.sendMessage.newSession=Manager [{0}] send new session [{1}]
+deltaManager.sendAllSessionsSize.invalid=The sendAllSessionsSize value [{0}]
is invalid. It must be a positive integer.
deltaManager.sessionReceived=Manager [{0}]; session state sent at [{1}]
received in [{2}] ms.
deltaManager.startClustering=Starting clustering manager at [{0}]
deltaManager.stopped=Manager [{0}] is stopping
diff --git a/test/org/apache/catalina/ha/session/TestDeltaManagerBatch.java
b/test/org/apache/catalina/ha/session/TestDeltaManagerBatch.java
new file mode 100644
index 0000000000..7969cc2916
--- /dev/null
+++ b/test/org/apache/catalina/ha/session/TestDeltaManagerBatch.java
@@ -0,0 +1,136 @@
+/*
+ * 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.session;
+
+import java.io.IOException;
+
+import org.easymock.EasyMock;
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.catalina.Session;
+import org.apache.catalina.ha.CatalinaCluster;
+import org.apache.catalina.tribes.Member;
+
+/**
+ * Tests for the <code>sendAllSessionsSize</code> validation in
+ * {@link DeltaManager} and the batched session sending in
+ * {@link DeltaManager#handleGET_ALL_SESSIONS}.
+ */
+public class TestDeltaManagerBatch {
+
+ /**
+ * Tracks how many times {@link #sendSessions(Member, Session[], long)} is
+ * invoked and what payloads it receives.
+ */
+ private static class TesterDeltaManager extends DeltaManager {
+
+ private int sendCount;
+ private int lastBatchSize = -1;
+ private final Session[] sessions;
+
+ TesterDeltaManager(Session[] sessions) {
+ this.sessions = sessions;
+ }
+
+ @Override
+ public Session[] findSessions() {
+ return sessions;
+ }
+
+ @Override
+ protected void sendSessions(Member sender, Session[] currentSessions,
long sendTimestamp)
+ throws IOException {
+ sendCount++;
+ lastBatchSize = currentSessions.length;
+ }
+ }
+
+ private static Session[] createSessions(int count) {
+ Session[] result = new Session[count];
+ TesterDeltaManager manager = new TesterDeltaManager(new Session[0]);
+ for (int i = 0; i < count; i++) {
+ result[i] = new DeltaSession(manager);
+ }
+ return result;
+ }
+
+ private static TesterDeltaManager createManager(Session[] sessions,
boolean sendAllSessions,
+ int sendAllSessionsSize) {
+ TesterDeltaManager manager = new TesterDeltaManager(sessions);
+ manager.setSendAllSessions(sendAllSessions);
+ manager.setSendAllSessionsSize(sendAllSessionsSize);
+ // Avoid the inter-batch sleep in the batching loop so the test does
not
+ // depend on the default sendAllSessionsWaitTime.
+ manager.setSendAllSessionsWaitTime(0);
+
+ // The send operations at the end of handleGET_ALL_SESSIONS require a
+ // cluster reference. Use a mock so no real cluster is needed.
+ CatalinaCluster cluster =
EasyMock.createNiceMock(CatalinaCluster.class);
+ EasyMock.replay(cluster);
+ manager.setCluster(cluster);
+ return manager;
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSendAllSessionsSizeZeroRejected() {
+ new TesterDeltaManager(new Session[0]).setSendAllSessionsSize(0);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testSendAllSessionsSizeNegativeRejected() {
+ new TesterDeltaManager(new Session[0]).setSendAllSessionsSize(-5);
+ }
+
+ @Test
+ public void testSendAllSessionsSizePositiveAccepted() {
+ TesterDeltaManager manager = new TesterDeltaManager(new Session[0]);
+ manager.setSendAllSessionsSize(1);
+ Assert.assertEquals(1, manager.getSendAllSessionsSize());
+ }
+
+ @Test
+ public void testPositiveBatchSizeSplitsSessions() throws Exception {
+ TesterDeltaManager manager = createManager(createSessions(5), false,
2);
+
+ manager.handleGET_ALL_SESSIONS(null, null);
+
+ // 5 sessions in batches of 2 => 2+2+1 = 3 sends
+ Assert.assertEquals(3, manager.sendCount);
+ }
+
+ @Test
+ public void testSendAllSessionsIgnoresBatchSize() throws Exception {
+ TesterDeltaManager manager = createManager(createSessions(3), true, 1);
+
+ manager.handleGET_ALL_SESSIONS(null, null);
+
+ // sendAllSessions is true so all sessions are sent in a single batch
+ Assert.assertEquals(1, manager.sendCount);
+ Assert.assertEquals(3, manager.lastBatchSize);
+ }
+
+ @Test
+ public void testNoSessionsNotSent() throws Exception {
+ TesterDeltaManager manager = createManager(createSessions(0), false,
2);
+
+ manager.handleGET_ALL_SESSIONS(null, null);
+
+ // No sessions means the batching loop body never runs
+ Assert.assertEquals(0, manager.sendCount);
+ }
+}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index cf54b01404..09d8d71a21 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -337,6 +337,13 @@
<subsection name="Cluster">
<changelog>
<!-- Entries for backport and removal before 12.0.0-M1 below this line
-->
+ <fix>
+ Validate that the <code>DeltaManager</code> attribute
+ <code>sendAllSessionsSize</code> is a positive integer. Zero or
+ negative values previously caused an infinite loop or a
+ <code>NegativeArraySizeException</code> during session state transfer.
+ (lihongyi87)
+ </fix>
</changelog>
</subsection>
<subsection name="WebSocket">
diff --git a/webapps/docs/config/cluster-manager.xml
b/webapps/docs/config/cluster-manager.xml
index ca3344fc38..ea7681d038 100644
--- a/webapps/docs/config/cluster-manager.xml
+++ b/webapps/docs/config/cluster-manager.xml
@@ -158,6 +158,8 @@
<attribute name="sendAllSessionsSize" required="false">
The number of sessions in a session block message. This value is
effective only when <code>sendAllSessions</code> is <code>false</code>.
+ It must be a positive integer; any other value is rejected with an
+ <code>IllegalArgumentException</code> during configuration.
Default is <code>1000</code>.
</attribute>
<attribute name="sendAllSessionsWaitTime" required="false">
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]