This is an automated email from the ASF dual-hosted git repository.
tabish pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git
The following commit(s) were added to refs/heads/main by this push:
new 8a1f9e8874 ARTEMIS-5085 use retry parameters on initial connection
8a1f9e8874 is described below
commit 8a1f9e8874d4f4fb175c558fa737b30cde172218
Author: Justin Bertram <[email protected]>
AuthorDate: Wed Oct 2 13:58:16 2024 -0500
ARTEMIS-5085 use retry parameters on initial connection
When the Core client attempts to create the initial connection to a
broker when initialConnectAttempts > 1 it will adhere to retryInterval,
but it will ignore retryIntervalMultiplier & maxRetryInterval. This
commit fixes that so that these parameters are taken into account.
---
.../core/client/impl/ClientSessionFactoryImpl.java | 15 ++-------
.../core/client/impl/ServerLocatorImpl.java | 16 ++++++++-
.../core/client/impl/ServerLocatorInternal.java | 2 ++
.../integration/client/InitialConnectionTest.java | 39 ++++++++++++++++++++++
4 files changed, 58 insertions(+), 14 deletions(-)
diff --git
a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ClientSessionFactoryImpl.java
b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ClientSessionFactoryImpl.java
index c609dd0d88..7298074a1d 100644
---
a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ClientSessionFactoryImpl.java
+++
b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ClientSessionFactoryImpl.java
@@ -764,7 +764,7 @@ public class ClientSessionFactoryImpl implements
ClientSessionFactoryInternal, C
failoverRetries++;
if (failoverRetryPredicate.test(false, failoverRetries)) {
waitForRetry(failoverRetryInterval);
- failoverRetryInterval =
getNextRetryInterval(failoverRetryInterval);
+ failoverRetryInterval =
serverLocator.getNextRetryInterval(failoverRetryInterval,
retryIntervalMultiplier, maxRetryInterval);
}
}
}
@@ -989,7 +989,7 @@ public class ClientSessionFactoryImpl implements
ClientSessionFactoryInternal, C
if (waitForRetry(interval))
return count;
- interval = getNextRetryInterval(interval);
+ interval = serverLocator.getNextRetryInterval(interval,
retryIntervalMultiplier, maxRetryInterval);
} else {
logger.debug("Could not connect to any server. Didn't have
reconnection configured on the ClientSessionFactory");
return count;
@@ -1000,17 +1000,6 @@ public class ClientSessionFactoryImpl implements
ClientSessionFactoryInternal, C
return count;
}
- private long getNextRetryInterval(long retryInterval) {
- // Exponential back-off
- long nextRetryInterval = (long) (retryInterval *
retryIntervalMultiplier);
-
- if (nextRetryInterval > maxRetryInterval) {
- nextRetryInterval = maxRetryInterval;
- }
-
- return nextRetryInterval;
- }
-
@Override
public boolean waitForRetry(long interval) {
try {
diff --git
a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ServerLocatorImpl.java
b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ServerLocatorImpl.java
index dd250a9b58..e235821bc2 100644
---
a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ServerLocatorImpl.java
+++
b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ServerLocatorImpl.java
@@ -687,6 +687,7 @@ public final class ServerLocatorImpl implements
ServerLocatorInternal, Discovery
boolean topologyArrayTried = !config.useTopologyForLoadBalancing ||
topologyArray == null || topologyArray.length == 0;
boolean staticTried = false;
boolean shouldTryStatic = useInitConnector();
+ long interval = config.retryInterval;
while (retry && !isClosed()) {
retry = false;
@@ -746,9 +747,10 @@ public final class ServerLocatorImpl implements
ServerLocatorInternal, Discovery
}
}
}
- if (factory.waitForRetry(config.retryInterval)) {
+ if (factory.waitForRetry(interval)) {
throw
ActiveMQClientMessageBundle.BUNDLE.cannotConnectToServers();
}
+ interval = getNextRetryInterval(interval,
config.retryIntervalMultiplier, config.maxRetryInterval);
retry = true;
} else {
throw e;
@@ -779,6 +781,18 @@ public final class ServerLocatorImpl implements
ServerLocatorInternal, Discovery
return factory;
}
+ @Override
+ public long getNextRetryInterval(long retryInterval, double
retryIntervalMultiplier, long maxRetryInterval) {
+ // Exponential back-off
+ long nextRetryInterval = (long) (retryInterval *
retryIntervalMultiplier);
+
+ if (nextRetryInterval > maxRetryInterval) {
+ nextRetryInterval = maxRetryInterval;
+ }
+
+ return nextRetryInterval;
+ }
+
private void executeDiscovery() throws ActiveMQException {
boolean discoveryOK = false;
boolean retryDiscovery = false;
diff --git
a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ServerLocatorInternal.java
b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ServerLocatorInternal.java
index 83bf219300..2c145c6c0c 100644
---
a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ServerLocatorInternal.java
+++
b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/impl/ServerLocatorInternal.java
@@ -94,4 +94,6 @@ public interface ServerLocatorInternal extends ServerLocator {
int getConnectorsSize();
Pair<TransportConfiguration, TransportConfiguration>
selectNextConnectorPair();
+
+ long getNextRetryInterval(long retryInterval, double
retryIntervalMultiplier, long maxRetryInterval);
}
diff --git
a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/client/InitialConnectionTest.java
b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/client/InitialConnectionTest.java
index 8d625d3c41..be2bbbc5db 100644
---
a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/client/InitialConnectionTest.java
+++
b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/client/InitialConnectionTest.java
@@ -19,6 +19,7 @@ package org.apache.activemq.artemis.tests.integration.client;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
@@ -75,4 +76,42 @@ public class InitialConnectionTest extends ActiveMQTestBase {
long timeEnd = System.currentTimeMillis();
assertTrue(timeEnd - timeStart >= 500, "3 connectors, at 100
milliseconds each try, initialConnectAttempt=2, it should have waited at least
600 (- 100 from the last try that we don't actually wait, just throw )
milliseconds");
}
+
+ @Test
+ public void testRetryIntervalMultiplier() {
+ int interval = 100;
+ double multiplier = 10.0;
+ int attempts = 3;
+ ConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("tcp://localhost:61610?retryInterval=" + interval +
"&retryIntervalMultiplier=" + multiplier + "&initialConnectAttempts=" +
attempts);
+ long timeStart = System.currentTimeMillis();
+ try {
+ connectionFactory.createConnection();
+ fail("Creating connection here should have failed");
+ } catch (JMSException e) {
+ // expected
+ }
+ long duration = System.currentTimeMillis() - timeStart;
+ long toWait = 1100;
+ assertTrue(duration >= toWait, "Waited only " + duration + "ms, but
should have waiting " + toWait);
+ }
+
+ @Test
+ public void testMaxRetryInterval() {
+ int interval = 100;
+ double multiplier = 50.0;
+ int attempts = 3;
+ int maxInterval = 1000;
+ ConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("tcp://localhost:61610?retryInterval=" + interval +
"&retryIntervalMultiplier=" + multiplier + "&initialConnectAttempts=" +
attempts + "&maxRetryInterval=" + maxInterval);
+ long timeStart = System.currentTimeMillis();
+ try {
+ connectionFactory.createConnection();
+ fail("Creating connection here should have failed");
+ } catch (JMSException e) {
+ // expected
+ }
+ long duration = System.currentTimeMillis() - timeStart;
+ long toWait = 1100;
+ assertTrue(duration >= toWait, "Waited only " + duration + "ms, but
should have waited " + toWait);
+ assertTrue(duration <= toWait + 500, "Waited " + duration + "ms, but
should have only waited " + (toWait + 500));
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information, visit: https://activemq.apache.org/contact