-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Avoid infinite loop in DeltaManager with invalid batch size #1042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
markt-asf
merged 1 commit into
apache:main
from
lihongyi87:fix-deltamanager-sendallsessionssize-loop
Aug 18, 2026
+153
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
test/org/apache/catalina/ha/session/TestDeltaManagerBatch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be below the line since it will be back-ported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved below the line. Thanks for the pointer.