Skip to content

Commit 4b494c1

Browse files
committed
Review "Keep monitor, heartbeat and JMX alive across partial ... stops"
Clean-up TLS if all services have stopped Keep channel on partial stop for Interceptors
1 parent 940f630 commit 4b494c1

3 files changed

Lines changed: 60 additions & 7 deletions

File tree

‎java/org/apache/catalina/tribes/group/ChannelInterceptorBase.java‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,34 @@ public void stop(int svc) throws ChannelException {
187187
if (getNext() != null) {
188188
getNext().stop(svc);
189189
}
190-
if (oname != null) {
191-
JmxRegistry jmxRegistry = JmxRegistry.getRegistry(channel);
192-
if (jmxRegistry != null) {
193-
jmxRegistry.unregisterJmx(oname);
190+
// Only release the channel and the JMX registration once all services have been stopped. A partial stop leaves
191+
// services running that still use them.
192+
if (isFullyStopped()) {
193+
if (oname != null) {
194+
JmxRegistry jmxRegistry = JmxRegistry.getRegistry(channel);
195+
if (jmxRegistry != null) {
196+
jmxRegistry.unregisterJmx(oname);
197+
}
198+
oname = null;
194199
}
195-
oname = null;
200+
channel = null;
196201
}
197-
channel = null;
202+
}
203+
204+
/**
205+
* Determine if all services of the coordinator at the end of the interceptor stack have been stopped.
206+
*
207+
* @return {@code true} if no service is started, or if there is no coordinator after this interceptor
208+
*/
209+
private boolean isFullyStopped() {
210+
ChannelInterceptor interceptor = getNext();
211+
while (interceptor != null) {
212+
if (interceptor instanceof ChannelCoordinator coordinator) {
213+
return coordinator.getStartLevel() == 0;
214+
}
215+
interceptor = interceptor.getNext();
216+
}
217+
return true;
198218
}
199219

200220
@Override

‎java/org/apache/catalina/tribes/group/GroupChannel.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ public synchronized void stop(int svc) throws ChannelException {
533533
heartbeatFuture.cancel(true);
534534
heartbeatFuture = null;
535535
}
536-
if ((svc & DEFAULT) == DEFAULT && sslContext != null) {
536+
if (sslContext != null) {
537537
sslContext.close();
538538
sslContext = null;
539539
}

‎test/org/apache/catalina/tribes/group/TestGroupChannelStartStop.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616
*/
1717
package org.apache.catalina.tribes.group;
1818

19+
import java.util.Iterator;
1920
import java.util.concurrent.ScheduledFuture;
2021

2122
import org.junit.After;
2223
import org.junit.Assert;
24+
import org.junit.Assume;
2325
import org.junit.Before;
2426
import org.junit.Test;
2527

2628
import org.apache.catalina.tribes.Channel;
29+
import org.apache.catalina.tribes.ChannelInterceptor;
2730
import org.apache.catalina.tribes.membership.McastService;
2831
import org.apache.catalina.tribes.transport.ReceiverBase;
2932
import org.apache.catalina.tribes.transport.ReplicationTransmitter;
@@ -166,6 +169,36 @@ public void testPartialStopKeepsHeartbeatAlive() throws Exception {
166169
Assert.assertTrue(heartbeat.isCancelled());
167170
}
168171

172+
@Test
173+
public void testStagedStopReleasesSslContext() throws Exception {
174+
channel.setPskIdentity("tribes-test");
175+
channel.setPskKey("000102030405060708090a0b0c0d0e0f");
176+
channel.start(Channel.DEFAULT);
177+
// The TLS context is only available when the TLS implementation is
178+
Assume.assumeNotNull(channel.getSslContext());
179+
// Stop in stages: no single call stops all of the services
180+
channel.stop(Channel.SND_RX_SEQ);
181+
Assert.assertNotNull(channel.getSslContext());
182+
channel.stop(Channel.SND_TX_SEQ | Channel.MBR_RX_SEQ | Channel.MBR_TX_SEQ);
183+
Assert.assertNull(channel.getSslContext());
184+
}
185+
186+
@Test
187+
public void testPartialStopKeepsInterceptorChannel() throws Exception {
188+
channel.start(Channel.DEFAULT);
189+
channel.stop(Channel.SND_RX_SEQ);
190+
// services are still running, the interceptors must still be attached to the channel
191+
for (Iterator<ChannelInterceptor> it = channel.getInterceptors(); it.hasNext();) {
192+
ChannelInterceptor interceptor = it.next();
193+
Assert.assertSame(interceptor.getClass().getName(), channel, interceptor.getChannel());
194+
}
195+
channel.stop(Channel.SND_TX_SEQ | Channel.MBR_RX_SEQ | Channel.MBR_TX_SEQ);
196+
for (Iterator<ChannelInterceptor> it = channel.getInterceptors(); it.hasNext();) {
197+
ChannelInterceptor interceptor = it.next();
198+
Assert.assertNull(interceptor.getClass().getName(), interceptor.getChannel());
199+
}
200+
}
201+
169202
@Test
170203
public void testOverlappingPartialStart() throws Exception {
171204
// start one service, then start everything: the already

0 commit comments

Comments
 (0)