-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix network bridge local-side close #2475
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
Open
mattrpav
wants to merge
2
commits into
apache:main
Choose a base branch
from
mattrpav:amq-harden-netbridge-local-close
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+173
−2
Open
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
166 changes: 166 additions & 0 deletions
166
...test/java/org/apache/activemq/network/NetworkBridgeLocalConnectionCloseReconnectTest.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,166 @@ | ||
| /** | ||
| * 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.activemq.network; | ||
|
|
||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import jakarta.jms.Connection; | ||
| import jakarta.jms.Session; | ||
|
|
||
| import org.apache.activemq.ActiveMQConnectionFactory; | ||
| import org.apache.activemq.broker.BrokerService; | ||
| import org.apache.activemq.broker.TransportConnection; | ||
| import org.apache.activemq.broker.region.RegionBroker; | ||
| import org.apache.activemq.command.ActiveMQQueue; | ||
| import org.apache.activemq.test.annotations.ParallelTest; | ||
| import org.apache.activemq.util.Wait; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.experimental.categories.Category; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * A network bridge whose LOCAL connection is closed server-side while the broker keeps running | ||
| * must fail the bridge to signal reconnect. | ||
| */ | ||
| @Category(ParallelTest.class) | ||
| public class NetworkBridgeLocalConnectionCloseReconnectTest { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(NetworkBridgeLocalConnectionCloseReconnectTest.class); | ||
|
|
||
| private static final String QUEUE_NAME = "BRIDGE.RECONNECT.TEST"; | ||
|
|
||
| private BrokerService localBroker; | ||
| private BrokerService remoteBroker; | ||
| private Connection remoteConnection; | ||
| private final AtomicInteger remoteReceived = new AtomicInteger(); | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| remoteBroker = new BrokerService(); | ||
| remoteBroker.setBrokerName("remote"); | ||
| remoteBroker.setPersistent(false); | ||
| remoteBroker.setUseJmx(false); | ||
| remoteBroker.addConnector("tcp://127.0.0.1:0"); | ||
| remoteBroker.start(); | ||
| remoteBroker.waitUntilStarted(); | ||
|
|
||
| localBroker = new BrokerService(); | ||
| localBroker.setBrokerName("local"); | ||
| localBroker.setPersistent(false); | ||
| localBroker.setUseJmx(false); | ||
| localBroker.start(); | ||
| localBroker.waitUntilStarted(); | ||
|
|
||
| var remoteUri = remoteBroker.getTransportConnectors().get(0).getPublishableConnectURI(); | ||
| remoteConnection = new ActiveMQConnectionFactory(remoteUri).createConnection(); | ||
| remoteConnection.start(); | ||
| remoteConnection.createSession(false, Session.AUTO_ACKNOWLEDGE) | ||
| .createConsumer(new ActiveMQQueue(QUEUE_NAME)) | ||
| .setMessageListener(m -> remoteReceived.incrementAndGet()); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() throws Exception { | ||
| if (remoteConnection != null) { | ||
| try { remoteConnection.close(); } catch (Exception ignored) {} | ||
| } | ||
| if (localBroker != null) { | ||
| localBroker.stop(); | ||
| localBroker.waitUntilStopped(); | ||
| } | ||
| if (remoteBroker != null) { | ||
| remoteBroker.stop(); | ||
| remoteBroker.waitUntilStopped(); | ||
| } | ||
| } | ||
|
|
||
| @Test(timeout = 120_000) | ||
| public void testBridgeReconnectsAfterLocalConnectionClosedServerSide() throws Exception { | ||
| var remoteUri = remoteBroker.getTransportConnectors().get(0).getPublishableConnectURI(); | ||
| var nc = localBroker.addNetworkConnector("static:(" + remoteUri + ")"); | ||
| nc.setName("to-remote"); | ||
| nc.setDuplex(false); | ||
| nc.addStaticallyIncludedDestination(new ActiveMQQueue(QUEUE_NAME)); | ||
| nc.start(); | ||
| assertTrue("bridge should establish", | ||
| Wait.waitFor(() -> !nc.activeBridges().isEmpty(), 20_000, 10)); | ||
|
|
||
| produce(5); | ||
| assertTrue("bridge should forward before the close", | ||
| Wait.waitFor(() -> remoteReceived.get() >= 5, 20_000, 10)); | ||
|
|
||
| // The bridge's local side is a vm:// client of the local broker. | ||
| var oldBridge = nc.activeBridges().iterator().next(); | ||
| var bridgeLocalConnection = findVmConnection(localBroker); | ||
| assertNotNull("expected the bridge's local vm:// connection on the local broker", bridgeLocalConnection); | ||
| LOG.info("stopping the bridge's local connection server-side: {}", bridgeLocalConnection); | ||
| bridgeLocalConnection.stop(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably simulate passing an IOException just like the slow consumer strategy does |
||
|
|
||
| // Connection stop is different from broker stop | ||
| var reconnected = Wait.waitFor(() -> { | ||
| for (var bridge : nc.activeBridges()) { | ||
| if (bridge != oldBridge) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| }, 30_000, 10); | ||
| assertTrue("bridge must reconnect after local-side connection closed and not a broker shutdown", reconnected); | ||
| assertTrue("stopped bridge instance should be removed from activeBridges", | ||
| Wait.waitFor(() -> !nc.activeBridges().contains(oldBridge), 20_000, 10)); | ||
|
|
||
| var before = remoteReceived.get(); | ||
| produce(5); | ||
| assertTrue("messages produced after the reconnect must flow across the new bridge " | ||
| + "(remote had " + before + ", waiting for " + (before + 5) + ")", | ||
| Wait.waitFor(() -> remoteReceived.get() >= before + 5, 20_000, 10)); | ||
| LOG.info("bridge reconnected and resumed forwarding after server-side local connection stop"); | ||
| nc.stop(); | ||
| } | ||
|
|
||
| private void produce(int n) throws Exception { | ||
| try (var c = new ActiveMQConnectionFactory( | ||
| localBroker.getTransportConnectors().isEmpty() | ||
| ? localBroker.getVmConnectorURI() | ||
| : localBroker.getTransportConnectors().get(0).getPublishableConnectURI()).createConnection()) { | ||
| c.start(); | ||
| var session = c.createSession(false, Session.AUTO_ACKNOWLEDGE); | ||
| var producer = session.createProducer(new ActiveMQQueue(QUEUE_NAME)); | ||
| for (var i = 0; i < n; i++) { | ||
| producer.send(session.createTextMessage("m-" + i)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // The bridge's local side is the only vm:// client connection on the local broker | ||
| private TransportConnection findVmConnection(BrokerService broker) throws Exception { | ||
| for (var connection : | ||
| ((RegionBroker) broker.getRegionBroker()).getClients()) { | ||
| var address = connection.getRemoteAddress(); | ||
| if (address != null && address.startsWith("vm:") && connection instanceof TransportConnection) { | ||
| return (TransportConnection) connection; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.