You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ro...@apache.org on 2024/12/18 15:09:40 UTC
(activemq-artemis) branch main updated: ARTEMIS-5215 AMQP federation consumer close can allow duplicates on remote
This is an automated email from the ASF dual-hosted git repository.
robbie 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 6e96968742 ARTEMIS-5215 AMQP federation consumer close can allow duplicates on remote
6e96968742 is described below
commit 6e969687427df10b4f1d3f51b68e08656034d04a
Author: Timothy Bish <ta...@gmail.com>
AuthorDate: Fri Dec 13 18:52:20 2024 -0500
ARTEMIS-5215 AMQP federation consumer close can allow duplicates on remote
Messages that are in-flight on a link when the federation consumer is closed
due to removal of local demand might get enqueued on the local broker but the
disposition might not get sent to the remote leading to a release of the message
back to the source queue leading to a duplicate being left on the remote. The
AMQP receiver links need to be drained and any pending settlements need to be
sent before the link can be allowed to be detached.
---
.../amqp/connect/AMQPBrokerConnection.java | 15 +-
.../amqp/connect/federation/AMQPFederation.java | 17 +-
.../federation/AMQPFederationAddressConsumer.java | 284 ++++-----
.../federation/AMQPFederationAddressEntry.java} | 37 +-
.../AMQPFederationAddressPolicyManager.java | 649 ++++++++++++++++-----
.../federation/AMQPFederationAsyncCompletion.java} | 30 +-
.../federation/AMQPFederationCommandProcessor.java | 12 +-
.../federation/AMQPFederationConfiguration.java | 29 +-
.../federation/AMQPFederationConstants.java | 7 +
.../connect/federation/AMQPFederationConsumer.java | 276 ++++++++-
.../AMQPFederationConsumerConfiguration.java | 12 +
.../federation/AMQPFederationEventProcessor.java | 12 +-
.../AMQPFederationGenericConsumerInfo.java} | 14 +-
.../federation/AMQPFederationPolicyManager.java | 328 +++++++++++
.../federation/AMQPFederationQueueConsumer.java | 268 +++------
.../federation/AMQPFederationQueueEntry.java} | 37 +-
.../AMQPFederationQueuePolicyManager.java | 471 ++++++++++++---
.../connect/mirror/AMQPMirrorControllerTarget.java | 15 +-
.../internal/FederationAddressPolicyManager.java | 549 -----------------
.../internal/FederationConsumerInternal.java | 56 --
.../internal/FederationPolicyManager.java | 140 -----
.../internal/FederationQueuePolicyManager.java | 389 ------------
.../amqp/proton/ProtonAbstractReceiver.java | 326 ++++++++---
.../amqp/proton/ProtonServerReceiverContext.java | 124 ++--
.../amqp/broker/AMQPSessionCallbackTest.java | 57 +-
.../connect/AMQPFederationAddressPolicyTest.java | 400 +++++++++++++
.../AMQPFederationConfigurationReloadTest.java | 3 +
.../amqp/connect/AMQPFederationConnectTest.java | 4 +
.../amqp/connect/AMQPFederationManagementTest.java | 12 +
.../connect/AMQPFederationQueuePolicyTest.java | 425 +++++++++++++-
.../connect/AMQPFederationServerToServerTest.java | 105 +++-
31 files changed, 3107 insertions(+), 1996 deletions(-)
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/AMQPBrokerConnection.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/AMQPBrokerConnection.java
index b6aab4ee44..2c07e1f553 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/AMQPBrokerConnection.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/AMQPBrokerConnection.java
@@ -751,7 +751,7 @@ public class AMQPBrokerConnection implements ClientConnectionLifeCycleListener,
throw new ActiveMQAMQPInternalErrorException(e.getMessage(), e);
}
- flow();
+ topUpCreditIfNeeded();
}
};
});
@@ -1018,15 +1018,18 @@ public class AMQPBrokerConnection implements ClientConnectionLifeCycleListener,
}
private void redoConnection() {
-
// avoiding retro-feeding an error call from the close after anything else that happened.
- if (protonRemotingConnection != null) {
- protonRemotingConnection.getAmqpConnection().clearLinkRemoteCloseListeners();
+ final ActiveMQProtonRemotingConnection remotingConnection = this.protonRemotingConnection;
+
+ if (remotingConnection != null) {
+ remotingConnection.getAmqpConnection().clearLinkRemoteCloseListeners();
}
- if (brokerFederation != null) {
+ final AMQPFederationSource federation = this.brokerFederation;
+
+ if (federation != null) {
try {
- brokerFederation.handleConnectionDropped();
+ federation.handleConnectionDropped();
} catch (ActiveMQException e) {
logger.debug("Broker Federation on connection {} threw an error on stop before connection attempt", getName());
}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederation.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederation.java
index 48c7e5e874..fb3974da53 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederation.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederation.java
@@ -32,9 +32,9 @@ import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.core.config.WildcardConfiguration;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.ActiveMQServerLogger;
+import org.apache.activemq.artemis.protocol.amqp.federation.Federation;
import org.apache.activemq.artemis.protocol.amqp.federation.FederationReceiveFromAddressPolicy;
import org.apache.activemq.artemis.protocol.amqp.federation.FederationReceiveFromQueuePolicy;
-import org.apache.activemq.artemis.protocol.amqp.federation.internal.FederationInternal;
import org.apache.activemq.artemis.protocol.amqp.proton.AMQPConnectionContext;
import org.apache.activemq.artemis.protocol.amqp.proton.AMQPSessionContext;
import org.apache.qpid.proton.engine.Link;
@@ -46,7 +46,7 @@ import org.slf4j.LoggerFactory;
* used on a remote peer to control the reverse case of when the remote configures the
* target side of the connection.
*/
-public abstract class AMQPFederation implements FederationInternal {
+public abstract class AMQPFederation implements Federation {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@@ -139,7 +139,11 @@ public abstract class AMQPFederation implements FederationInternal {
*/
public abstract AMQPFederationConfiguration getConfiguration();
- @Override
+ /**
+ * Starts this federation instance if not already started.
+ *
+ * @throws ActiveMQException if an error occurs during the start process.
+ */
public final synchronized void start() throws ActiveMQException {
if (!started) {
handleFederationStarted();
@@ -148,7 +152,12 @@ public abstract class AMQPFederation implements FederationInternal {
}
}
- @Override
+ /**
+ * Stops this federation instance and shuts down all remote resources that
+ * the federation currently has open and active.
+ *
+ * @throws ActiveMQException if an error occurs during the stop process.
+ */
public final synchronized void stop() throws ActiveMQException {
if (started) {
handleFederationStopped();
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAddressConsumer.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAddressConsumer.java
index b59a6e8f77..e79a56aa5a 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAddressConsumer.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAddressConsumer.java
@@ -33,18 +33,12 @@ import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;
-
-import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.ICoreMessage;
import org.apache.activemq.artemis.api.core.Message;
import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString;
-import org.apache.activemq.artemis.core.config.TransformerConfiguration;
-import org.apache.activemq.artemis.core.server.ActiveMQServerLogger;
import org.apache.activemq.artemis.core.server.AddressQueryResult;
-import org.apache.activemq.artemis.core.server.transformer.Transformer;
import org.apache.activemq.artemis.core.transaction.Transaction;
import org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage;
import org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPException;
@@ -89,32 +83,17 @@ public class AMQPFederationAddressConsumer extends AMQPFederationConsumer {
private static final SimpleString MESSAGE_HOPS_ANNOTATION =
SimpleString.of(AMQPFederationPolicySupport.MESSAGE_HOPS_ANNOTATION.toString());
- // Sequence ID value used to keep links that would otherwise have the same name from overlapping
- // this generally occurs when a remote link detach is delayed and new demand is added before it
- // arrives resulting in an unintended link stealing scenario in the proton engine.
- private static final AtomicLong LINK_SEQUENCE_ID = new AtomicLong();
-
private final AMQPFederationAddressPolicyManager manager;
private final FederationReceiveFromAddressPolicy policy;
- private final Transformer transformer;
-
- private AMQPFederatedAddressDeliveryReceiver receiver;
public AMQPFederationAddressConsumer(AMQPFederationAddressPolicyManager manager,
AMQPFederationConsumerConfiguration configuration,
AMQPSessionContext session, FederationConsumerInfo consumerInfo,
BiConsumer<FederationConsumerInfo, Message> messageObserver) {
- super(manager.getFederation(), configuration, session, consumerInfo, messageObserver);
+ super(manager.getFederation(), configuration, session, consumerInfo, manager.getPolicy(), messageObserver);
this.manager = manager;
this.policy = manager.getPolicy();
-
- final TransformerConfiguration transformerConfiguration = policy.getTransformerConfiguration();
- if (transformerConfiguration != null) {
- this.transformer = federation.getServer().getServiceRegistry().getFederationTransformer(policy.getPolicyName(), transformerConfiguration);
- } else {
- this.transformer = (m) -> m;
- }
}
/**
@@ -124,30 +103,6 @@ public class AMQPFederationAddressConsumer extends AMQPFederationConsumer {
return policy;
}
- private void signalBeforeFederationConsumerMessageHandled(Message message) throws ActiveMQException {
- try {
- federation.getServer().callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- ((ActiveMQServerAMQPFederationPlugin) plugin).beforeFederationConsumerMessageHandled(this, message);
- }
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("beforeFederationConsumerMessageHandled", t);
- }
- }
-
- private void signalAfterFederationConsumerMessageHandled(Message message) throws ActiveMQException {
- try {
- federation.getServer().callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- ((ActiveMQServerAMQPFederationPlugin) plugin).afterFederationConsumerMessageHandled(this, message);
- }
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("afterFederationConsumerMessageHandled", t);
- }
- }
-
private String generateLinkName() {
return "federation-" + federation.getName() +
"-address-receiver-" + consumerInfo.getAddress() +
@@ -156,155 +111,120 @@ public class AMQPFederationAddressConsumer extends AMQPFederationConsumer {
}
@Override
- protected final void asyncCreateReceiver() {
- connection.runLater(() -> {
- if (closed) {
- return;
+ protected final void doCreateReceiver() {
+ try {
+ final Receiver protonReceiver = session.getSession().receiver(generateLinkName());
+ final Target target = new Target();
+ final Source source = new Source();
+ final String address = consumerInfo.getAddress();
+
+ if (RoutingType.ANYCAST.equals(consumerInfo.getRoutingType())) {
+ source.setCapabilities(AmqpSupport.QUEUE_CAPABILITY);
+ } else {
+ source.setCapabilities(AmqpSupport.TOPIC_CAPABILITY);
}
- try {
- final Receiver protonReceiver = session.getSession().receiver(generateLinkName());
- final Target target = new Target();
- final Source source = new Source();
- final String address = consumerInfo.getAddress();
+ final Map<Symbol, Object> filtersMap = new HashMap<>();
+ filtersMap.put(AmqpSupport.NO_LOCAL_NAME, AmqpNoLocalFilter.NO_LOCAL);
- if (RoutingType.ANYCAST.equals(consumerInfo.getRoutingType())) {
- source.setCapabilities(AmqpSupport.QUEUE_CAPABILITY);
- } else {
- source.setCapabilities(AmqpSupport.TOPIC_CAPABILITY);
- }
+ if (consumerInfo.getFilterString() != null && !consumerInfo.getFilterString().isEmpty()) {
+ final AmqpJmsSelectorFilter jmsFilter = new AmqpJmsSelectorFilter(consumerInfo.getFilterString());
- final Map<Symbol, Object> filtersMap = new HashMap<>();
- filtersMap.put(AmqpSupport.NO_LOCAL_NAME, AmqpNoLocalFilter.NO_LOCAL);
+ filtersMap.put(AmqpSupport.JMS_SELECTOR_KEY, jmsFilter);
+ }
- if (consumerInfo.getFilterString() != null && !consumerInfo.getFilterString().isEmpty()) {
- final AmqpJmsSelectorFilter jmsFilter = new AmqpJmsSelectorFilter(consumerInfo.getFilterString());
+ source.setOutcomes(Arrays.copyOf(OUTCOMES, OUTCOMES.length));
+ source.setDefaultOutcome(DEFAULT_OUTCOME);
+ source.setDurable(TerminusDurability.NONE);
+ source.setExpiryPolicy(TerminusExpiryPolicy.LINK_DETACH);
+ source.setAddress(address);
+ source.setFilter(filtersMap);
+
+ target.setAddress(address);
+
+ final Map<String, Object> addressSourceProperties = new HashMap<>();
+ // If the remote needs to create the address then it should apply these
+ // settings during the create.
+ addressSourceProperties.put(ADDRESS_AUTO_DELETE, policy.isAutoDelete());
+ addressSourceProperties.put(ADDRESS_AUTO_DELETE_DELAY, policy.getAutoDeleteDelay());
+ addressSourceProperties.put(ADDRESS_AUTO_DELETE_MSG_COUNT, policy.getAutoDeleteMessageCount());
+
+ final Map<Symbol, Object> receiverProperties = new HashMap<>();
+ receiverProperties.put(FEDERATED_ADDRESS_SOURCE_PROPERTIES, addressSourceProperties);
+
+ protonReceiver.setSenderSettleMode(SenderSettleMode.UNSETTLED);
+ protonReceiver.setReceiverSettleMode(ReceiverSettleMode.FIRST);
+ protonReceiver.setDesiredCapabilities(new Symbol[] {FEDERATION_ADDRESS_RECEIVER});
+ // If enabled offer core tunneling which we prefer to AMQP conversions of core as
+ // the large ones will be converted to standard AMQP messages in memory. When not
+ // offered the remote must not use core tunneling and AMQP conversion will be the
+ // fallback.
+ if (configuration.isCoreMessageTunnelingEnabled()) {
+ protonReceiver.setOfferedCapabilities(new Symbol[] {AmqpSupport.CORE_MESSAGE_TUNNELING_SUPPORT});
+ }
+ protonReceiver.setProperties(receiverProperties);
+ protonReceiver.setTarget(target);
+ protonReceiver.setSource(source);
+ protonReceiver.open();
+
+ final ScheduledFuture<?> openTimeoutTask;
+ final AtomicBoolean openTimedOut = new AtomicBoolean(false);
+
+ if (configuration.getLinkAttachTimeout() > 0) {
+ openTimeoutTask = federation.getServer().getScheduledPool().schedule(() -> {
+ openTimedOut.set(true);
+ federation.signalResourceCreateError(ActiveMQAMQPProtocolMessageBundle.BUNDLE.brokerConnectionTimeout());
+ }, configuration.getLinkAttachTimeout(), TimeUnit.SECONDS);
+ } else {
+ openTimeoutTask = null;
+ }
- filtersMap.put(AmqpSupport.JMS_SELECTOR_KEY, jmsFilter);
- }
+ this.protonReceiver = protonReceiver;
- source.setOutcomes(Arrays.copyOf(OUTCOMES, OUTCOMES.length));
- source.setDefaultOutcome(DEFAULT_OUTCOME);
- source.setDurable(TerminusDurability.NONE);
- source.setExpiryPolicy(TerminusExpiryPolicy.LINK_DETACH);
- source.setAddress(address);
- source.setFilter(filtersMap);
-
- target.setAddress(address);
-
- final Map<String, Object> addressSourceProperties = new HashMap<>();
- // If the remote needs to create the address then it should apply these
- // settings during the create.
- addressSourceProperties.put(ADDRESS_AUTO_DELETE, policy.isAutoDelete());
- addressSourceProperties.put(ADDRESS_AUTO_DELETE_DELAY, policy.getAutoDeleteDelay());
- addressSourceProperties.put(ADDRESS_AUTO_DELETE_MSG_COUNT, policy.getAutoDeleteMessageCount());
-
- final Map<Symbol, Object> receiverProperties = new HashMap<>();
- receiverProperties.put(FEDERATED_ADDRESS_SOURCE_PROPERTIES, addressSourceProperties);
-
- protonReceiver.setSenderSettleMode(SenderSettleMode.UNSETTLED);
- protonReceiver.setReceiverSettleMode(ReceiverSettleMode.FIRST);
- protonReceiver.setDesiredCapabilities(new Symbol[] {FEDERATION_ADDRESS_RECEIVER});
- // If enabled offer core tunneling which we prefer to AMQP conversions of core as
- // the large ones will be converted to standard AMQP messages in memory. When not
- // offered the remote must not use core tunneling and AMQP conversion will be the
- // fallback.
- if (configuration.isCoreMessageTunnelingEnabled()) {
- protonReceiver.setOfferedCapabilities(new Symbol[] {AmqpSupport.CORE_MESSAGE_TUNNELING_SUPPORT});
- }
- protonReceiver.setProperties(receiverProperties);
- protonReceiver.setTarget(target);
- protonReceiver.setSource(source);
- protonReceiver.open();
-
- final ScheduledFuture<?> openTimeoutTask;
- final AtomicBoolean openTimedOut = new AtomicBoolean(false);
-
- if (configuration.getLinkAttachTimeout() > 0) {
- openTimeoutTask = federation.getServer().getScheduledPool().schedule(() -> {
- openTimedOut.set(true);
- federation.signalResourceCreateError(ActiveMQAMQPProtocolMessageBundle.BUNDLE.brokerConnectionTimeout());
- }, configuration.getLinkAttachTimeout(), TimeUnit.SECONDS);
- } else {
- openTimeoutTask = null;
- }
+ protonReceiver.attachments().set(AMQP_LINK_INITIALIZER_KEY, Runnable.class, () -> {
+ try {
+ if (openTimeoutTask != null) {
+ openTimeoutTask.cancel(false);
+ }
- this.protonReceiver = protonReceiver;
+ if (openTimedOut.get()) {
+ return;
+ }
- protonReceiver.attachments().set(AMQP_LINK_INITIALIZER_KEY, Runnable.class, () -> {
- try {
- if (openTimeoutTask != null) {
- openTimeoutTask.cancel(false);
- }
-
- if (openTimedOut.get()) {
- return;
- }
-
- // Remote must support federation receivers otherwise we fail the connection unless the
- // Attach indicates that a detach is incoming in which case we just allow the normal handling
- // to occur.
- if (protonReceiver.getRemoteSource() != null && !AmqpSupport.verifyOfferedCapabilities(protonReceiver, FEDERATION_ADDRESS_RECEIVER)) {
- federation.signalResourceCreateError(
- ActiveMQAMQPProtocolMessageBundle.BUNDLE.missingOfferedCapability(FEDERATION_ADDRESS_RECEIVER.toString()));
- return;
- }
-
- // Intercept remote close and check for valid reasons for remote closure such as
- // the remote peer not having a matching queue for this subscription or from an
- // operator manually closing the link.
- federation.addLinkClosedInterceptor(consumerInfo.getId(), remoteCloseInterceptor);
-
- receiver = new AMQPFederatedAddressDeliveryReceiver(session, consumerInfo, protonReceiver);
-
- if (protonReceiver.getRemoteSource() != null) {
- logger.debug("AMQP Federation {} address consumer {} completed open", federation.getName(), consumerInfo);
- } else {
- logger.debug("AMQP Federation {} address consumer {} rejected by remote", federation.getName(), consumerInfo);
- }
-
- session.addReceiver(protonReceiver, (session, protonRcvr) -> {
- return this.receiver;
- });
- } catch (Exception e) {
- federation.signalError(e);
+ // Remote must support federation receivers otherwise we fail the connection unless the
+ // Attach indicates that a detach is incoming in which case we just allow the normal handling
+ // to occur.
+ if (protonReceiver.getRemoteSource() != null && !AmqpSupport.verifyOfferedCapabilities(protonReceiver, FEDERATION_ADDRESS_RECEIVER)) {
+ federation.signalResourceCreateError(
+ ActiveMQAMQPProtocolMessageBundle.BUNDLE.missingOfferedCapability(FEDERATION_ADDRESS_RECEIVER.toString()));
+ return;
}
- });
- } catch (Exception e) {
- federation.signalError(e);
- }
- connection.flush();
- });
- }
+ // Intercept remote close and check for valid reasons for remote closure such as
+ // the remote peer not having a matching queue for this subscription or from an
+ // operator manually closing the link.
+ federation.addLinkClosedInterceptor(consumerInfo.getId(), remoteCloseInterceptor);
- @Override
- protected final void asyncCloseReceiver() {
- connection.runLater(() -> {
- federation.removeLinkClosedInterceptor(consumerInfo.getId());
+ receiver = new AMQPFederatedAddressDeliveryReceiver(session, consumerInfo, protonReceiver);
- if (receiver != null) {
- try {
- receiver.close(false);
- } catch (ActiveMQAMQPException e) {
- } finally {
- receiver = null;
- }
- }
+ if (protonReceiver.getRemoteSource() != null) {
+ logger.debug("AMQP Federation {} address consumer {} completed open", federation.getName(), consumerInfo);
+ } else {
+ logger.debug("AMQP Federation {} address consumer {} rejected by remote", federation.getName(), consumerInfo);
+ }
- // Need to track the proton receiver and close it here as the default
- // context implementation doesn't do that and could result in no detach
- // being sent in some cases and possible resources leaks.
- if (protonReceiver != null) {
- try {
- protonReceiver.close();
- } finally {
- protonReceiver = null;
+ session.addReceiver(protonReceiver, (session, protonRcvr) -> {
+ return this.receiver;
+ });
+ } catch (Exception e) {
+ federation.signalError(e);
}
- }
+ });
+ } catch (Exception e) {
+ federation.signalError(e);
+ }
- connection.flush();
- });
+ connection.flush();
}
private static AMQPMessage incrementAMQPMessageHops(AMQPMessage message) {
@@ -446,7 +366,7 @@ public class AMQPFederationAddressConsumer extends AMQPFederationConsumer {
logger.debug("Error caught when trying to add federation address consumer to management", e);
}
- flow();
+ topUpCreditIfNeeded();
}
@Override
@@ -490,9 +410,9 @@ public class AMQPFederationAddressConsumer extends AMQPFederationConsumer {
transformer, baseMessage, theMessage);
}
- signalBeforeFederationConsumerMessageHandled(theMessage);
+ signalPluginBeforeFederationConsumerMessageHandled(theMessage);
sessionSPI.serverSend(this, tx, receiver, delivery, cachedAddress, routingContext, theMessage);
- signalAfterFederationConsumerMessageHandled(theMessage);
+ signalPluginAfterFederationConsumerMessageHandled(theMessage);
} catch (Exception e) {
logger.warn("Inbound delivery for {} encountered an error: {}", consumerInfo, e.getMessage(), e);
deliveryFailed(delivery, receiver, e);
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationAddressEntry.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAddressEntry.java
similarity index 79%
rename from artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationAddressEntry.java
rename to artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAddressEntry.java
index dce40f8fb4..baf960acde 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationAddressEntry.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAddressEntry.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.activemq.artemis.protocol.amqp.federation.internal;
+package org.apache.activemq.artemis.protocol.amqp.connect.federation;
import java.util.HashSet;
import java.util.Objects;
@@ -25,7 +25,7 @@ import org.apache.activemq.artemis.core.postoffice.Binding;
import org.apache.activemq.artemis.core.server.impl.AddressInfo;
/**
- * An entry type class used to hold a {@link FederationConsumerInternal} and
+ * An entry type class used to hold a {@link AMQPFederationConsumer} and
* any other state data needed by the manager that is creating them based on the
* policy configuration for the federation instance. The entry can be extended
* by federation implementation to hold additional state data for the federation
@@ -35,12 +35,12 @@ import org.apache.activemq.artemis.core.server.impl.AddressInfo;
* on a federation resource such that it is not torn down until all demand has been
* removed from the local resource.
*/
-public class FederationAddressEntry {
+public class AMQPFederationAddressEntry {
private final AddressInfo addressInfo;
private final Set<Binding> demandBindings = new HashSet<>();
- private FederationConsumerInternal consumer;
+ private AMQPFederationConsumer consumer;
/**
* Creates a new address entry for tracking demand on a federated address
@@ -48,7 +48,7 @@ public class FederationAddressEntry {
* @param addressInfo
* The address information object that this entry hold demand state for.
*/
- public FederationAddressEntry(AddressInfo addressInfo) {
+ public AMQPFederationAddressEntry(AddressInfo addressInfo) {
this.addressInfo = addressInfo;
}
@@ -76,7 +76,7 @@ public class FederationAddressEntry {
/**
* @return the consumer managed by this entry
*/
- public FederationConsumerInternal getConsumer() {
+ public AMQPFederationConsumer getConsumer() {
return consumer;
}
@@ -88,7 +88,7 @@ public class FederationAddressEntry {
*
* @return this federation address consumer entry.
*/
- public FederationAddressEntry setConsumer(FederationConsumerInternal consumer) {
+ public AMQPFederationAddressEntry setConsumer(AMQPFederationConsumer consumer) {
Objects.requireNonNull(consumer, "Cannot assign a null consumer to this entry, call clear to unset");
this.consumer = consumer;
return this;
@@ -97,11 +97,14 @@ public class FederationAddressEntry {
/**
* Clears the currently assigned consumer from this entry.
*
- * @return this federation address consumer entry.
+ * @return the consumer that was stored here previously or null if none was set
*/
- public FederationAddressEntry clearConsumer() {
+ public AMQPFederationConsumer clearConsumer() {
+ final AMQPFederationConsumer taken = consumer;
+
this.consumer = null;
- return this;
+
+ return taken;
}
/**
@@ -116,7 +119,7 @@ public class FederationAddressEntry {
*
* @return this federation address consumer entry.
*/
- public FederationAddressEntry addDemand(Binding binding) {
+ public AMQPFederationAddressEntry addDemand(Binding binding) {
demandBindings.add(binding);
return this;
}
@@ -126,8 +129,18 @@ public class FederationAddressEntry {
*
* @return this federation address consumer entry.
*/
- public FederationAddressEntry removeDemand(Binding binding) {
+ public AMQPFederationAddressEntry removeDemand(Binding binding) {
demandBindings.remove(binding);
return this;
}
+
+ /**
+ * Remove demand on this federation address consumer from all previous bindings.
+ *
+ * @return this federation address consumer entry.
+ */
+ public AMQPFederationAddressEntry removeAllDemand() {
+ demandBindings.clear();
+ return this;
+ }
}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAddressPolicyManager.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAddressPolicyManager.java
index 4fcc498147..0d65c72b2b 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAddressPolicyManager.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAddressPolicyManager.java
@@ -20,26 +20,26 @@ package org.apache.activemq.artemis.protocol.amqp.connect.federation;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationPolicySupport.generateAddressFilter;
import java.lang.invoke.MethodHandles;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
import java.util.Objects;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.function.BiConsumer;
+import java.util.Set;
import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration;
import org.apache.activemq.artemis.api.core.ActiveMQException;
-import org.apache.activemq.artemis.api.core.Message;
+import org.apache.activemq.artemis.api.core.RoutingType;
+import org.apache.activemq.artemis.api.core.SimpleString;
+import org.apache.activemq.artemis.core.postoffice.Binding;
+import org.apache.activemq.artemis.core.postoffice.QueueBinding;
+import org.apache.activemq.artemis.core.postoffice.impl.DivertBinding;
import org.apache.activemq.artemis.core.server.ActiveMQServerLogger;
-import org.apache.activemq.artemis.core.server.Divert;
-import org.apache.activemq.artemis.core.server.Queue;
import org.apache.activemq.artemis.core.server.impl.AddressInfo;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumer;
+import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerAddressPlugin;
+import org.apache.activemq.artemis.core.transaction.Transaction;
+import org.apache.activemq.artemis.protocol.amqp.federation.Federation;
import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumerInfo;
import org.apache.activemq.artemis.protocol.amqp.federation.FederationReceiveFromAddressPolicy;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationReceiveFromResourcePolicy;
import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumerInfo.Role;
-import org.apache.activemq.artemis.protocol.amqp.federation.internal.FederationAddressPolicyManager;
-import org.apache.activemq.artemis.protocol.amqp.federation.internal.FederationConsumerInternal;
-import org.apache.activemq.artemis.protocol.amqp.federation.internal.FederationGenericConsumerInfo;
-import org.apache.activemq.artemis.protocol.amqp.proton.AMQPSessionContext;
import org.apache.activemq.artemis.utils.CompositeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -47,197 +47,562 @@ import org.slf4j.LoggerFactory;
/**
* The AMQP Federation implementation of an federation address policy manager.
*/
-public class AMQPFederationAddressPolicyManager extends FederationAddressPolicyManager {
+public final class AMQPFederationAddressPolicyManager extends AMQPFederationPolicyManager implements ActiveMQServerAddressPlugin {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
- protected final AtomicLong messageCount = new AtomicLong();
- protected final BiConsumer<FederationConsumerInfo, Message> messageObserver = (i, m) -> messageCount.incrementAndGet();
- protected final AMQPFederation federation;
protected final String remoteQueueFilter;
-
- protected volatile AMQPFederationConsumerConfiguration configuration;
- protected volatile AMQPSessionContext session;
+ protected final FederationReceiveFromAddressPolicy policy;
+ protected final Map<String, AMQPFederationAddressEntry> demandTracking = new HashMap<>();
+ protected final Map<DivertBinding, Set<QueueBinding>> divertsTracking = new HashMap<>();
public AMQPFederationAddressPolicyManager(AMQPFederation federation, FederationReceiveFromAddressPolicy addressPolicy) throws ActiveMQException {
- super(federation, addressPolicy);
+ super(federation);
+
+ Objects.requireNonNull(addressPolicy, "The Address match policy cannot be null");
- this.federation = federation;
+ this.policy = addressPolicy;
this.remoteQueueFilter = generateAddressFilter(policy.getMaxHops());
}
+ /**
+ * @return the receive from address policy that backs the address policy manager.
+ */
@Override
- public AMQPFederation getFederation() {
- return federation;
+ public FederationReceiveFromAddressPolicy getPolicy() {
+ return policy;
}
/**
- * @return the number of messages that all federation consumer of this policy have received from the remote.
+ * Stops the address policy manager which will close any open remote receivers that are
+ * active for local queue demand. Stop should generally be called whenever the parent
+ * {@link Federation} loses its connection to the remote.
*/
- public long getMessagesReceived() {
- return messageCount.get();
+ public synchronized void stop() {
+ if (started) {
+ // Ensures that on shutdown of a federation broker connection we don't leak
+ // broker plugin instances.
+ server.unRegisterBrokerPlugin(this);
+ started = false;
+ demandTracking.values().forEach((entry) -> {
+ if (entry != null) {
+ tryCloseFederationConsumer(entry.removeAllDemand().clearConsumer());
+ }
+ });
+ demandTracking.clear();
+ divertsTracking.clear();
+ }
}
@Override
- protected void handlePolicyManagerStarted(FederationReceiveFromResourcePolicy policy) {
- // Capture state for the current connection on each start of the policy manager.
- configuration = new AMQPFederationConsumerConfiguration(federation.getConfiguration(), policy.getProperties());
- session = federation.getSessionContext();
+ public synchronized void afterRemoveAddress(SimpleString address, AddressInfo addressInfo) throws ActiveMQException {
+ if (started) {
+ final AMQPFederationAddressEntry entry = demandTracking.remove(address.toString());
+
+ if (entry != null && entry.removeAllDemand().hasConsumer()) {
+ logger.trace("Federated address {} was removed, closing federation consumer", address);
+
+ // Demand is gone because the Address is gone and any in-flight messages can be
+ // allowed to be released back to the remote as they will not be processed.
+ // We removed the consumer information from demand tracking to prevent build up
+ // of data for entries that may never return and to prevent interference from the
+ // next set of events which will be the close of all local consumers for this now
+ // removed Address.
+ tryCloseFederationConsumer(entry.clearConsumer());
+ }
+ }
}
@Override
- protected FederationGenericConsumerInfo createConsumerInfo(AddressInfo address) {
- final String addressName = address.getName().toString();
- final String generatedQueueName = generateQueueName(address);
-
- return new FederationGenericConsumerInfo(Role.ADDRESS_CONSUMER,
- addressName,
- generatedQueueName,
- address.getRoutingType(),
- remoteQueueFilter,
- CompositeAddress.toFullyQualified(addressName, generatedQueueName),
- ActiveMQDefaultConfiguration.getDefaultConsumerPriority());
+ public synchronized void afterRemoveBinding(Binding binding, Transaction tx, boolean deleteData) throws ActiveMQException {
+ if (started) {
+ if (binding instanceof QueueBinding) {
+ final AMQPFederationAddressEntry entry = demandTracking.get(binding.getAddress().toString());
+
+ logger.trace("Federated address {} binding was removed, stopping federation consumer", binding.getAddress());
+
+ if (entry != null) {
+ // This is QueueBinding that was mapped to a federated address so we can directly remove
+ // demand from the federation consumer and close it if demand is now gone.
+ tryRemoveDemandOnAddress(entry, binding);
+ } else if (policy.isEnableDivertBindings()) {
+ // See if there is any matching diverts that are forwarding to an address where this QueueBinding
+ // is bound and remove the mapping for any matches, diverts can have a composite set of address
+ // forwards so each divert must be checked in turn to see if it contains the address the removed
+ // binding was bound to.
+ divertsTracking.entrySet().forEach(divertEntry -> {
+ final String sourceAddress = divertEntry.getKey().getAddress().toString();
+ final SimpleString forwardAddress = divertEntry.getKey().getDivert().getForwardAddress();
+
+ if (isAddressInDivertForwards(binding.getAddress(), forwardAddress)) {
+ // Try and remove the queue binding from the set of registered bindings
+ // for the divert and if that removes all mapped bindings then we can
+ // remove the divert from the federated address entry and check if that
+ // removed all local demand which means we can close the consumer.
+ divertEntry.getValue().remove(binding);
+
+ if (divertEntry.getValue().isEmpty()) {
+ tryRemoveDemandOnAddress(demandTracking.get(sourceAddress), divertEntry.getKey());
+ }
+ }
+ });
+ }
+ } else if (policy.isEnableDivertBindings() && binding instanceof DivertBinding) {
+ final DivertBinding divert = (DivertBinding) binding;
+
+ if (divertsTracking.remove(divert) != null) {
+ // The divert binding is treated as one unit of demand on a federated address and
+ // when the divert is removed that unit of demand is removed regardless of existing
+ // bindings still remaining on the divert forwards. If the divert demand was the
+ // only thing keeping the federated address consumer open this will result in it
+ // being closed.
+ try {
+ tryRemoveDemandOnAddress(demandTracking.get(divert.getAddress().toString()), divert);
+ } catch (Exception e) {
+ ActiveMQServerLogger.LOGGER.federationBindingsLookupError(divert.getDivert().getForwardAddress(), e);
+ }
+ }
+ }
+ }
}
- protected String generateQueueName(AddressInfo address) {
- return "federation." + federation.getName() + ".address." + address.getName() + ".node." + server.getNodeID();
- }
+ private void tryRemoveDemandOnAddress(AMQPFederationAddressEntry entry, Binding binding) {
+ if (entry != null) {
+ entry.removeDemand(binding);
- @Override
- protected FederationConsumerInternal createFederationConsumer(FederationConsumerInfo consumerInfo) {
- Objects.requireNonNull(consumerInfo, "Federation Address consumer information object was null");
+ logger.trace("Reducing demand on federated address {}, remaining demand? {}", entry.getAddress(), entry.hasDemand());
- if (logger.isTraceEnabled()) {
- logger.trace("AMQP Federation {} creating address consumer: {} for policy: {}", federation.getName(), consumerInfo, policy.getPolicyName());
+ if (!entry.hasDemand() && entry.hasConsumer()) {
+ // A started consumer should be allowed to stop before possible close either because demand
+ // is still not present or the remote did not respond before the configured stop timeout elapsed.
+ // A successfully stopped receiver can be restarted but if the stop times out the receiver should
+ // be closed and a new receiver created if demand is present. The completions occur on the connection
+ // thread which requires the handler method to use synchronized to ensure thread safety.
+ entry.getConsumer().stopAsync(new AMQPFederationAsyncCompletion<AMQPFederationConsumer>() {
+
+ @Override
+ public void onComplete(AMQPFederationConsumer context) {
+ handleFederationConsumerStopped(entry, true);
+ }
+
+ @Override
+ public void onException(AMQPFederationConsumer context, Exception error) {
+ logger.trace("Stop of federation consumer {} failed, closing consumer: ", context, error);
+ handleFederationConsumerStopped(entry, false);
+ }
+ });
+ }
}
+ }
- // Don't initiate anything yet as the caller might need to register error handlers etc
- // before the attach is sent otherwise they could miss the failure case.
- return new AMQPFederationAddressConsumer(this, configuration, session, consumerInfo, messageObserver);
+ private synchronized void handleFederationConsumerStopped(AMQPFederationAddressEntry entry, boolean didStop) {
+ final AMQPFederationConsumer federationConsuner = entry.getConsumer();
+
+ // Remote close or local address remove could have beaten us here and already cleaned up the consumer.
+ if (federationConsuner != null) {
+ // If the consumer has no demand or it didn't stop in time or some other error occurred we
+ // assume the worst and close it here, the follow on code will recreate or cleanup as needed.
+ if (!didStop || !entry.hasDemand()) {
+ tryCloseFederationConsumer(entry.clearConsumer());
+ }
+
+ // Demand may have returned while the consumer was stopping in which case
+ // we either restart an existing stopped consumer or recreate if the stop
+ // timed out and we closed it above. If there's still no demand then we
+ // should remove it from demand tracking to reduce resource consumption.
+ if (started && entry.hasDemand()) {
+ tryRestartFederationConsumerForAddress(entry);
+ } else {
+ demandTracking.remove(entry.getAddress());
+ }
+ }
}
+ /**
+ * Scans all bindings and push them through the normal bindings checks that
+ * would be done on an add. We filter here based on whether diverts are enabled
+ * just to reduce the result set but the check call should also filter as
+ * during normal operations divert bindings could be added.
+ */
@Override
- protected boolean testIfAddressMatchesPolicy(AddressInfo addressInfo) {
- if (!policy.test(addressInfo)) {
- return false;
- }
+ protected void scanAllBindings() {
+ server.getPostOffice()
+ .getAllBindings()
+ .filter(binding -> binding instanceof QueueBinding || (policy.isEnableDivertBindings() && binding instanceof DivertBinding))
+ .forEach(binding -> afterAddBinding(binding));
+ }
- // Address consumers can't pull as we have no real metric to indicate when / how much
- // we should pull so instead we refuse to match if credit set to zero.
- if (configuration.getReceiverCredits() <= 0) {
- logger.debug("Federation address policy rejecting match on {} because credit is set to zero:", addressInfo.getName());
- return false;
- } else {
- return true;
+ @Override
+ public synchronized void afterAddAddress(AddressInfo addressInfo, boolean reload) {
+ if (started && policy.isEnableDivertBindings() && policy.test(addressInfo)) {
+ try {
+ // A Divert can exist in configuration prior to the address having been auto created
+ // etc so upon address add this check needs to be run to capture addresses that now
+ // match the divert.
+ server.getPostOffice()
+ .getDirectBindings(addressInfo.getName())
+ .stream()
+ .filter(binding -> binding instanceof DivertBinding)
+ .forEach(this::checkBindingForMatch);
+ } catch (Exception e) {
+ ActiveMQServerLogger.LOGGER.federationBindingsLookupError(addressInfo.getName(), e);
+ }
}
}
@Override
- protected void signalBeforeCreateFederationConsumer(FederationConsumerInfo info) {
- try {
- server.callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- ((ActiveMQServerAMQPFederationPlugin) plugin).beforeCreateFederationConsumer(info);
- }
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("beforeCreateFederationConsumer", t);
+ public synchronized void afterAddBinding(Binding binding) {
+ if (started) {
+ checkBindingForMatch(binding);
}
}
- @Override
- protected void signalAfterCreateFederationConsumer(FederationConsumer consumer) {
- try {
- server.callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- ((ActiveMQServerAMQPFederationPlugin) plugin).afterCreateFederationConsumer(consumer);
+ /**
+ * Called under lock this method should check if the given {@link Binding} matches the
+ * configured address federation policy and federate the address if so. The incoming
+ * {@link Binding} can be either a {@link QueueBinding} or a {@link DivertBinding} so
+ * the code should check both.
+ *
+ * @param binding
+ * The binding that should be checked against the federated address policy,
+ */
+ private void checkBindingForMatch(Binding binding) {
+ if (binding instanceof QueueBinding) {
+ final QueueBinding queueBinding = (QueueBinding) binding;
+ final AddressInfo addressInfo = server.getPostOffice().getAddressInfo(binding.getAddress());
+
+ if (testIfAddressMatchesPolicy(addressInfo)) {
+ // A plugin can block address federation for a given queue and if another queue
+ // binding does trigger address federation we don't want to track the rejected
+ // queue as demand so we always run this check before trying to create the address
+ // consumer.
+ if (isPluginBlockingFederationConsumerCreate(queueBinding.getQueue())) {
+ return;
}
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("afterCreateFederationConsumer", t);
+
+ createOrUpdateFederatedAddressConsumerForBinding(addressInfo, queueBinding);
+ } else {
+ reactIfQueueBindingMatchesAnyDivertTarget(queueBinding);
+ }
+ } else if (binding instanceof DivertBinding) {
+ reactIfAnyQueueBindingMatchesDivertTarget((DivertBinding) binding);
}
}
- @Override
- protected void signalBeforeCloseFederationConsumer(FederationConsumer consumer) {
- try {
- server.callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- ((ActiveMQServerAMQPFederationPlugin) plugin).beforeCloseFederationConsumer(consumer);
+ private void reactIfAnyQueueBindingMatchesDivertTarget(DivertBinding divertBinding) {
+ if (!policy.isEnableDivertBindings()) {
+ return;
+ }
+
+ final AddressInfo addressInfo = server.getPostOffice().getAddressInfo(divertBinding.getAddress());
+
+ if (!testIfAddressMatchesPolicy(addressInfo)) {
+ return;
+ }
+
+ // We only need to check if we've never seen the divert before, afterwards we will
+ // be checking it any time a new QueueBinding is added instead.
+ if (divertsTracking.get(divertBinding) == null) {
+ final Set<QueueBinding> matchingQueues = new HashSet<>();
+ divertsTracking.put(divertBinding, matchingQueues);
+
+ // We must account for the composite divert case by splitting the address and
+ // getting the bindings on each one.
+ final SimpleString forwardAddress = divertBinding.getDivert().getForwardAddress();
+ final SimpleString[] forwardAddresses = forwardAddress.split(',');
+
+ try {
+ for (SimpleString forward : forwardAddresses) {
+ server.getPostOffice().getBindingsForAddress(forward).getBindings()
+ .stream()
+ .filter(b -> b instanceof QueueBinding)
+ .map(b -> (QueueBinding) b)
+ .forEach(queueBinding -> {
+ // The plugin can block the demand totally here either based on the divert itself
+ // or the queue that's attached to the divert.
+ if (isPluginBlockingFederationConsumerCreate(divertBinding.getDivert(), queueBinding.getQueue())) {
+ return;
+ }
+
+ // The plugin can block the demand selectively based on a single queue attached to
+ // the divert target(s).
+ if (isPluginBlockingFederationConsumerCreate(queueBinding.getQueue())) {
+ return;
+ }
+
+ matchingQueues.add(queueBinding);
+
+ createOrUpdateFederatedAddressConsumerForBinding(addressInfo, divertBinding);
+ });
}
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("beforeCloseFederationConsumer", t);
+ } catch (Exception e) {
+ ActiveMQServerLogger.LOGGER.federationBindingsLookupError(forwardAddress, e);
+ }
}
}
- @Override
- protected void signalAfterCloseFederationConsumer(FederationConsumer consumer) {
- try {
- server.callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- ((ActiveMQServerAMQPFederationPlugin) plugin).afterCloseFederationConsumer(consumer);
+ private void reactIfQueueBindingMatchesAnyDivertTarget(QueueBinding queueBinding) {
+ if (!policy.isEnableDivertBindings()) {
+ return;
+ }
+
+ final SimpleString queueAddress = queueBinding.getAddress();
+
+ divertsTracking.entrySet().forEach((e) -> {
+ final SimpleString forwardAddress = e.getKey().getDivert().getForwardAddress();
+ final DivertBinding divertBinding = e.getKey();
+
+ // Check matched diverts to see if the QueueBinding address matches the address or
+ // addresses (composite diverts) of the Divert and if so then we can check if we need
+ // to create demand on the source address on the remote if we haven't done so already.
+
+ if (!e.getValue().contains(queueBinding) && isAddressInDivertForwards(queueAddress, forwardAddress)) {
+ // The plugin can block the demand totally here either based on the divert itself
+ // or the queue that's attached to the divert.
+ if (isPluginBlockingFederationConsumerCreate(divertBinding.getDivert(), queueBinding.getQueue())) {
+ return;
}
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("afterCloseFederationConsumer", t);
+
+ // The plugin can block the demand selectively based on a single queue attached to
+ // the divert target(s).
+ if (isPluginBlockingFederationConsumerCreate(queueBinding.getQueue())) {
+ return;
+ }
+
+ // Each divert that forwards to the address the queue is bound to we add demand
+ // in the diverts tracker.
+ e.getValue().add(queueBinding);
+
+ final AddressInfo addressInfo = server.getPostOffice().getAddressInfo(divertBinding.getAddress());
+
+ createOrUpdateFederatedAddressConsumerForBinding(addressInfo, divertBinding);
+ }
+ });
+ }
+
+ private void createOrUpdateFederatedAddressConsumerForBinding(AddressInfo addressInfo, Binding binding) {
+ logger.trace("Federation Address Policy matched on for demand on address: {} : binding: {}", addressInfo, binding);
+
+ final String addressName = addressInfo.getName().toString();
+ final AMQPFederationAddressEntry entry;
+
+ // Check for existing consumer add demand from a additional local consumer to ensure
+ // the remote consumer remains active until all local demand is withdrawn.
+ if (demandTracking.containsKey(addressName)) {
+ entry = demandTracking.get(addressName);
+ } else {
+ entry = new AMQPFederationAddressEntry(addressInfo);
+ demandTracking.put(addressName, entry);
}
+
+ // Demand passed all binding plugin blocking checks so we track it, plugin can still
+ // stop federation of the address based on some external criteria but once it does
+ // (if ever) allow it we will have tracked all allowed demand.
+ entry.addDemand(binding);
+
+ // This will create a new consumer only if there isn't one currently assigned to the entry
+ // and any configured federation plugins don't block it from doing so.
+ tryCreateFederationConsumerForAddress(entry);
}
- @Override
- protected final boolean isPluginBlockingFederationConsumerCreate(AddressInfo address) {
- final AtomicBoolean canCreate = new AtomicBoolean(true);
-
- try {
- server.callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- if (canCreate.get()) {
- canCreate.set(((ActiveMQServerAMQPFederationPlugin) plugin).shouldCreateFederationConsumerForAddress(address));
+ private void tryCreateFederationConsumerForAddress(AMQPFederationAddressEntry entry) {
+ if (entry.hasDemand()) {
+ final AddressInfo addressInfo = entry.getAddressInfo();
+
+ if (!entry.hasConsumer() && !isPluginBlockingFederationConsumerCreate(addressInfo)) {
+ logger.trace("Federation Address Policy manager creating remote consumer for address: {}", addressInfo);
+
+ final FederationConsumerInfo consumerInfo = createConsumerInfo(addressInfo);
+ final AMQPFederationConsumer addressConsumer = createFederationConsumer(consumerInfo);
+
+ signalPluginBeforeCreateFederationConsumer(consumerInfo);
+
+ // Handle remote close with remove of consumer which means that future demand will
+ // attempt to create a new consumer for that demand. Ensure that thread safety is
+ // accounted for here as the notification can be asynchronous.
+ addressConsumer.setRemoteClosedHandler((closedConsumer) -> {
+ synchronized (AMQPFederationAddressPolicyManager.this) {
+ try {
+ final AMQPFederationAddressEntry tracked = demandTracking.get(closedConsumer.getConsumerInfo().getAddress());
+
+ if (tracked != null) {
+ tracked.clearConsumer();
+ }
+ } finally {
+ closedConsumer.close();
+ }
}
- }
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("shouldCreateFederationConsumerForAddress", t);
- }
+ });
+
+ entry.setConsumer(addressConsumer);
+
+ // Now that we are tracking it we can initialize it which will start it once
+ // the link has fully attached.
+ addressConsumer.initialize();
- return !canCreate.get();
+ signalPluginAfterCreateFederationConsumer(addressConsumer);
+ }
+ }
}
- @Override
- protected final boolean isPluginBlockingFederationConsumerCreate(Divert divert, Queue queue) {
- final AtomicBoolean canCreate = new AtomicBoolean(true);
-
- try {
- server.callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- if (canCreate.get()) {
- canCreate.set(((ActiveMQServerAMQPFederationPlugin) plugin).shouldCreateFederationConsumerForDivert(divert, queue));
+ private void tryRestartFederationConsumerForAddress(AMQPFederationAddressEntry entry) {
+ // There might be a consumer that was previously stopped due to demand having been
+ // removed in which case we can attempt to recover it with a simple restart but if
+ // that fails ensure the old consumer is closed and then attempt to recreate as we
+ // know there is demand currently.
+ if (entry.hasConsumer()) {
+ final AMQPFederationConsumer federationConsuner = entry.getConsumer();
+
+ try {
+ federationConsuner.startAsync(new AMQPFederationAsyncCompletion<AMQPFederationConsumer>() {
+
+ @Override
+ public void onComplete(AMQPFederationConsumer context) {
+ logger.trace("Restarted federation consumer after new demand added." );
}
- }
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("shouldCreateFederationConsumerForDivert", t);
+
+ @Override
+ public void onException(AMQPFederationConsumer context, Exception error) {
+ if (error instanceof IllegalStateException) {
+ // The receiver might be stopping or it could be closed, either of which
+ // was initiated from this manager so we can ignore and let those complete.
+ return;
+ } else {
+ // This is unexpected and our reaction is to close the consumer since we
+ // have no idea what its state is now. Later new demand or remote events
+ // will trigger a new consumer to get added.
+ logger.trace("Start of federation consumer {} threw unexpected error, closing consumer: ", context, error);
+ tryCloseFederationConsumer(entry.clearConsumer());
+ }
+ }
+ });
+ } catch (Exception ex) {
+ // The consumer might have been remotely closed, we can't be certain but since we
+ // are responding to demand having been added we will close it and clear the entry
+ // so that the follow on code can try and create a new one.
+ logger.trace("Caught error on attempted restart of existing federation consumer", ex);
+ tryCloseFederationConsumer(entry.clearConsumer());
+ tryCreateFederationConsumerForAddress(entry);
+ }
+ } else {
+ // The consumer was likely closed because it didn't stop in time, create a new one and
+ // let the normal setup process start federation again.
+ tryCreateFederationConsumerForAddress(entry);
}
+ }
- return !canCreate.get();
+ /**
+ * Checks if the remote address added falls within the set of addresses that match the
+ * configured address policy and if so scans for local demand on that address to see
+ * if a new attempt to federate the address is needed.
+ *
+ * @param addressName
+ * The address that was added on the remote.
+ *
+ * @throws Exception if an error occurs while processing the address added event.
+ */
+ synchronized void afterRemoteAddressAdded(String addressName) throws Exception {
+ // Assume that the remote address that matched a previous federation attempt is MULTICAST
+ // so that we retry if current local state matches the policy and if it isn't we will once
+ // again record the federation attempt with the remote and be updated if the remote removes
+ // and adds the address again (hopefully with the correct routing type). We retrain all the
+ // current demand and don't need to re-check the server state before trying to create the
+ // remote address consumer.
+ if (started && testIfAddressMatchesPolicy(addressName, RoutingType.MULTICAST) && demandTracking.containsKey(addressName)) {
+ tryCreateFederationConsumerForAddress(demandTracking.get(addressName));
+ }
+ }
+
+ /**
+ * Performs the test against the configured address policy to check if the target
+ * address is a match or not. A subclass can override this method and provide its
+ * own match tests in combination with the configured matching policy.
+ *
+ * @param addressInfo
+ * The address that is being tested for a policy match.
+ *
+ * @return <code>true</code> if the address given is a match against the policy.
+ */
+ private boolean testIfAddressMatchesPolicy(AddressInfo addressInfo) {
+ if (!policy.test(addressInfo)) {
+ return false;
+ }
+
+ // Address consumers can't pull as we have no real metric to indicate when / how much
+ // we should pull so instead we refuse to match if credit set to zero.
+ if (configuration.getReceiverCredits() <= 0) {
+ logger.debug("Federation address policy rejecting match on {} because credit is set to zero:", addressInfo.getName());
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ /**
+ * Performs the test against the configured address policy to check if the target
+ * address is a match or not. A subclass can override this method and provide its
+ * own match tests in combination with the configured matching policy.
+ *
+ * @param address
+ * The address that is being tested for a policy match.
+ * @param type
+ * The routing type of the address to test against the policy.
+ *
+ * @return <code>true</code> if the address given is a match against the policy.
+ */
+ private boolean testIfAddressMatchesPolicy(String address, RoutingType type) {
+ return policy.test(address, type);
+ }
+
+ /**
+ * Create a new {@link FederationConsumerInfo} based on the given {@link AddressInfo}
+ * and the configured {@link FederationReceiveFromAddressPolicy}. A subclass must override this
+ * method to return a consumer information object with the data used be that implementation.
+ *
+ * @param address
+ * The {@link AddressInfo} to use as a basis for the consumer information object.
+ *
+ * @return a new {@link FederationConsumerInfo} instance based on the given address.
+ */
+ private AMQPFederationGenericConsumerInfo createConsumerInfo(AddressInfo address) {
+ final String addressName = address.getName().toString();
+ final String generatedQueueName = generateQueueName(address);
+
+ return new AMQPFederationGenericConsumerInfo(Role.ADDRESS_CONSUMER,
+ addressName,
+ generatedQueueName,
+ address.getRoutingType(),
+ remoteQueueFilter,
+ CompositeAddress.toFullyQualified(addressName, generatedQueueName),
+ ActiveMQDefaultConfiguration.getDefaultConsumerPriority());
}
@Override
- protected final boolean isPluginBlockingFederationConsumerCreate(Queue queue) {
- final AtomicBoolean canCreate = new AtomicBoolean(true);
-
- try {
- server.callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- if (canCreate.get()) {
- canCreate.set(((ActiveMQServerAMQPFederationPlugin) plugin).shouldCreateFederationConsumerForQueue(queue));
- }
- }
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("shouldCreateFederationConsumerForQueue", t);
+ protected AMQPFederationConsumer createFederationConsumer(FederationConsumerInfo consumerInfo) {
+ Objects.requireNonNull(consumerInfo, "Federation Address consumer information object was null");
+
+ if (logger.isTraceEnabled()) {
+ logger.trace("AMQP Federation {} creating address consumer: {} for policy: {}", federation.getName(), consumerInfo, policy.getPolicyName());
+ }
+
+ // Don't initiate anything yet as the caller might need to register error handlers etc
+ // before the attach is sent otherwise they could miss the failure case.
+ return new AMQPFederationAddressConsumer(this, configuration, session, consumerInfo, messageObserver);
+ }
+
+ private String generateQueueName(AddressInfo address) {
+ return "federation." + federation.getName() + ".address." + address.getName() + ".node." + server.getNodeID();
+ }
+
+ private static boolean isAddressInDivertForwards(final SimpleString targetAddress, final SimpleString forwardAddress) {
+ final SimpleString[] forwardAddresses = forwardAddress.split(',');
+
+ for (SimpleString forward : forwardAddresses) {
+ if (targetAddress.equals(forward)) {
+ return true;
+ }
}
- return !canCreate.get();
+ return false;
}
}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationInternal.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAsyncCompletion.java
similarity index 51%
rename from artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationInternal.java
rename to artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAsyncCompletion.java
index de52b8db3f..5feec874c3 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationInternal.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationAsyncCompletion.java
@@ -14,29 +14,33 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
-package org.apache.activemq.artemis.protocol.amqp.federation.internal;
-
-import org.apache.activemq.artemis.api.core.ActiveMQException;
-import org.apache.activemq.artemis.protocol.amqp.federation.Federation;
+package org.apache.activemq.artemis.protocol.amqp.connect.federation;
/**
- * Internal federated server API that is subject to change without notice.
+ * AMQPFederationAsyncCompletion type used to implement the handlers for asynchronous calls in AMQP
+ * federation types.
+ *
+ * @param <E>
+ * The type that defines the context provided to the completion events
*/
-public interface FederationInternal extends Federation {
+public interface AMQPFederationAsyncCompletion<E> {
/**
- * Start the federation instance if not already started.
+ * Called when the asynchronous operation has succeeded.
*
- * @throws ActiveMQException if an error occurs during the start.
+ * @param context
+ * The context object provided for this asynchronous event.
*/
- void start() throws ActiveMQException;
+ void onComplete(E context);
/**
- * Stop the federation instance if not already stopped.
+ * Called when the asynchronous operation has failed due to an error.
*
- * @throws ActiveMQException if an error occurs during the stop.
+ * @param context
+ * The context object provided for this asynchronous event.
+ * @param error
+ * The error that describes the failure that occurred.
*/
- void stop() throws ActiveMQException;
+ void onException(E context, Exception error);
}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationCommandProcessor.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationCommandProcessor.java
index f88d4387cc..fc355e0f40 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationCommandProcessor.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationCommandProcessor.java
@@ -20,6 +20,7 @@ package org.apache.activemq.artemis.protocol.amqp.connect.federation;
import java.lang.invoke.MethodHandles;
import org.apache.activemq.artemis.api.core.Message;
+import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.transaction.Transaction;
import org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage;
@@ -109,7 +110,7 @@ public class AMQPFederationCommandProcessor extends ProtonAbstractReceiver {
// opening its sender link.
receiver.setOfferedCapabilities(OFFERED_LINK_CAPABILITIES);
- flow();
+ topUpCreditIfNeeded();
}
@Override
@@ -141,7 +142,7 @@ public class AMQPFederationCommandProcessor extends ProtonAbstractReceiver {
delivery.disposition(Accepted.getInstance());
delivery.settle();
- flow();
+ topUpCreditIfNeeded();
connection.flush();
} catch (Throwable e) {
@@ -160,7 +161,12 @@ public class AMQPFederationCommandProcessor extends ProtonAbstractReceiver {
}
@Override
- public void flow() {
+ protected void doCreditTopUpRun() {
creditRunnable.run();
}
+
+ @Override
+ protected SimpleString getAddressInUse() {
+ return SimpleString.of(receiver.getName());
+ }
}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConfiguration.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConfiguration.java
index a27443d690..b2931591fc 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConfiguration.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConfiguration.java
@@ -22,6 +22,7 @@ import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPF
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.PULL_RECEIVER_BATCH_SIZE;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_CREDITS;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_CREDITS_LOW;
+import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_QUIESCE_TIMEOUT;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.IGNORE_QUEUE_CONSUMER_FILTERS;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.IGNORE_QUEUE_CONSUMER_PRIORITIES;
@@ -65,14 +66,21 @@ public final class AMQPFederationConfiguration {
* the filter specified by a consumer subscription is used or if the higher level queue
* filter only is applied when creating a federation queue consumer.
*/
- private static final boolean DEFAULT_IGNNORE_QUEUE_CONSUMER_FILTERS = false;
+ public static final boolean DEFAULT_IGNNORE_QUEUE_CONSUMER_FILTERS = false;
/**
* Default value for the priority applied to federation queue consumers that controls if
* the priority specified by a consumer subscription is used or if the policy priority
* offset value is simply applied to the default consumer priority value.
*/
- private static final boolean DEFAULT_IGNNORE_QUEUE_CONSUMER_PRIORITIES = false;
+ public static final boolean DEFAULT_IGNNORE_QUEUE_CONSUMER_PRIORITIES = false;
+
+ /**
+ * Default timeout (milliseconds) applied to federation receivers that are being closed due to removal
+ * of local demand and need to drain link credit and process any in-flight deliveries before closure.
+ * If the timeout elapses before the link has quiesced the link is forcibly closed.
+ */
+ public static final int DEFAULT_RECEIVER_QUIESCE_TIMEOUT = 60_000;
private final Map<String, Object> properties;
private final AMQPConnectionContext connection;
@@ -147,7 +155,7 @@ public final class AMQPFederationConfiguration {
}
/**
- * @return the size in bytes of an incoming message after which the {@link Receiver} treats it as large.
+ * @return the timeout value to use when waiting for a corresponding link attach from the remote.
*/
public int getLinkAttachTimeout() {
final Object property = properties.get(LINK_ATTACH_TIMEOUT);
@@ -202,6 +210,20 @@ public final class AMQPFederationConfiguration {
}
}
+ /**
+ * @return the receive quiesce timeout when shutting down a {@link Receiver} when local demand is removed.
+ */
+ public int getReceiverQuiesceTimeout() {
+ final Object property = properties.get(RECEIVER_QUIESCE_TIMEOUT);
+ if (property instanceof Number) {
+ return ((Number) property).intValue();
+ } else if (property instanceof String) {
+ return Integer.parseInt((String) property);
+ } else {
+ return DEFAULT_RECEIVER_QUIESCE_TIMEOUT;
+ }
+ }
+
/**
* Enumerate the configuration options in this configuration object and return a {@link Map} that
* contains the values which can be sent to a remote peer
@@ -213,6 +235,7 @@ public final class AMQPFederationConfiguration {
configMap.put(RECEIVER_CREDITS, getReceiverCredits());
configMap.put(RECEIVER_CREDITS_LOW, getReceiverCreditsLow());
+ configMap.put(RECEIVER_QUIESCE_TIMEOUT, getReceiverQuiesceTimeout());
configMap.put(PULL_RECEIVER_BATCH_SIZE, getPullReceiverBatchSize());
configMap.put(LARGE_MESSAGE_THRESHOLD, getLargeMessageThreshold());
configMap.put(LINK_ATTACH_TIMEOUT, getLinkAttachTimeout());
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConstants.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConstants.java
index b0e75163bb..0888ffa940 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConstants.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConstants.java
@@ -294,4 +294,11 @@ public final class AMQPFederationConstants {
*/
public static final String REQUESTED_ADDRESS_NAME = "REQUESTED_ADDRESS_NAME";
+ /**
+ * When a federation receiver link is being closed due to removal of local demand this timeout
+ * value enforces a maximum wait for drain and processing of in-flight messages before the link
+ * is forcibly terminated with the assumption that the remote is no longer responding.
+ */
+ public static final String RECEIVER_QUIESCE_TIMEOUT = "receiverQuiesceTimeout";
+
}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConsumer.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConsumer.java
index 28db68c22c..80ce0f7278 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConsumer.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConsumer.java
@@ -20,16 +20,27 @@ import static org.apache.activemq.artemis.protocol.amqp.proton.AmqpSupport.DETAC
import static org.apache.activemq.artemis.protocol.amqp.proton.AmqpSupport.NOT_FOUND;
import static org.apache.activemq.artemis.protocol.amqp.proton.AmqpSupport.RESOURCE_DELETED;
+import java.lang.invoke.MethodHandles;
+import java.util.Objects;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
+import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.Message;
+import org.apache.activemq.artemis.core.config.TransformerConfiguration;
+import org.apache.activemq.artemis.core.server.ActiveMQServerLogger;
+import org.apache.activemq.artemis.core.server.transformer.Transformer;
+import org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPException;
+import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumer;
import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumerInfo;
-import org.apache.activemq.artemis.protocol.amqp.federation.internal.FederationConsumerInternal;
+import org.apache.activemq.artemis.protocol.amqp.federation.FederationReceiveFromResourcePolicy;
import org.apache.activemq.artemis.protocol.amqp.proton.AMQPConnectionContext;
import org.apache.activemq.artemis.protocol.amqp.proton.AMQPSessionContext;
+import org.apache.activemq.artemis.protocol.amqp.proton.ProtonAbstractReceiver;
import org.apache.qpid.proton.amqp.Symbol;
import org.apache.qpid.proton.amqp.messaging.Accepted;
import org.apache.qpid.proton.amqp.messaging.Modified;
@@ -37,11 +48,15 @@ import org.apache.qpid.proton.amqp.messaging.Rejected;
import org.apache.qpid.proton.amqp.messaging.Released;
import org.apache.qpid.proton.engine.Link;
import org.apache.qpid.proton.engine.Receiver;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Base class for AMQP Federation consumers that implements some of the common functionality.
*/
-public abstract class AMQPFederationConsumer implements FederationConsumerInternal {
+public abstract class AMQPFederationConsumer implements FederationConsumer {
+
+ private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
protected static final Symbol[] OUTCOMES = new Symbol[]{Accepted.DESCRIPTOR_SYMBOL, Rejected.DESCRIPTOR_SYMBOL,
Released.DESCRIPTOR_SYMBOL, Modified.DESCRIPTOR_SYMBOL};
@@ -52,6 +67,12 @@ public abstract class AMQPFederationConsumer implements FederationConsumerIntern
DEFAULT_OUTCOME.setDeliveryFailed(true);
}
+ // Sequence ID value used to keep links that would otherwise have the same name from overlapping
+ // this generally occurs when a remote link detach is delayed and new demand is added before it
+ // arrives resulting in an unintended link stealing scenario in the proton engine but can also
+ // occur when consumers on the same queue have differing filters.
+ protected static final AtomicLong LINK_SEQUENCE_ID = new AtomicLong();
+
protected final AMQPFederation federation;
protected final AMQPFederationConsumerConfiguration configuration;
protected final FederationConsumerInfo consumerInfo;
@@ -60,14 +81,17 @@ public abstract class AMQPFederationConsumer implements FederationConsumerIntern
protected final Predicate<Link> remoteCloseInterceptor = this::remoteLinkClosedInterceptor;
protected final BiConsumer<FederationConsumerInfo, Message> messageObserver;
protected final AtomicLong messageCount = new AtomicLong();
+ protected final Transformer transformer;
+ protected final AtomicBoolean closed = new AtomicBoolean();
+ protected volatile boolean initialized;
+ protected ProtonAbstractReceiver receiver;
protected Receiver protonReceiver;
- protected boolean started;
- protected volatile boolean closed;
- protected Consumer<FederationConsumerInternal> remoteCloseHandler;
+ protected Consumer<AMQPFederationConsumer> remoteCloseHandler;
public AMQPFederationConsumer(AMQPFederation federation, AMQPFederationConsumerConfiguration configuration,
AMQPSessionContext session, FederationConsumerInfo consumerInfo,
+ FederationReceiveFromResourcePolicy policy,
BiConsumer<FederationConsumerInfo, Message> messageObserver) {
this.federation = federation;
this.consumerInfo = consumerInfo;
@@ -75,6 +99,13 @@ public abstract class AMQPFederationConsumer implements FederationConsumerIntern
this.session = session;
this.configuration = configuration;
this.messageObserver = messageObserver;
+
+ final TransformerConfiguration transformerConfiguration = policy.getTransformerConfiguration();
+ if (transformerConfiguration != null) {
+ this.transformer = federation.getServer().getServiceRegistry().getFederationTransformer(policy.getPolicyName(), transformerConfiguration);
+ } else {
+ this.transformer = (m) -> m;
+ }
}
/**
@@ -94,28 +125,192 @@ public abstract class AMQPFederationConsumer implements FederationConsumerIntern
return consumerInfo;
}
- @Override
- public final synchronized void start() {
- if (!started && !closed) {
- started = true;
- asyncCreateReceiver();
+ /**
+ * @return <code>true</code> if the consumer has previously been initialized.
+ */
+ public final boolean isInitialized() {
+ return initialized;
+ }
+
+ /**
+ * Called to initialize the AMQP federation consumer which will trigger an asynchronous
+ * task to attach the link and handle all setup receiver and eventually start the flow
+ * of credit to the remote. This method should be called once after the basic configuration
+ * of the consumer is complete and should not be called again after that.
+ */
+ public void initialize() {
+ if (initialized) {
+ throw new IllegalStateException("A receiver should only be initialized once");
}
+
+ initialized = true;
+ connection.runLater(this::doCreateReceiver);
}
- @Override
- public final synchronized void close() {
- if (!closed) {
- closed = true;
- if (started) {
- started = false;
- asyncCloseReceiver();
+ /**
+ * Called during the initialization of the consumer to trigger an asynchronous link
+ * attach of the underlying AMQP receiver that backs this federation consumer. The new
+ * receiver should be initialized in a started state. This method executes on the
+ * connection thread and should not block. This method will be called from the thread
+ * of the connection this consumer operates on.
+ */
+ protected abstract void doCreateReceiver();
+
+ /**
+ * Asynchronously starts a previously stopped federation consumer which should trigger a grant
+ * of credit to the remote thereby allowing new incoming messages to be federated. In general
+ * the start should only happen when the receiver is know to be stopped but given the asynchronous
+ * nature of the receiver handling this won't always be the case, below the outcomes of various
+ * cases that could result from calls to this method. The completion methods are always called
+ * from a different thread than this method is called in which means the caller should ensure
+ * that the handling accounts for thread safety of those methods.
+ * <p>
+ * Calling start on an already closed consumer should immediately throw an {@link IllegalStateException} immediately.
+ * Calling start on an non-initialized consumer should immediately throw an {@link IllegalStateException} immediately.
+ * <p>
+ * Calling start on a stopped consumer should start the consumer and signal success to the completion.
+ * Calling start on an already started consumer should simply signal success to the completion.
+ * Calling start on a stopping consumer should fail the completion with an {@link IllegalStateException}.
+ * Calling start on a consumer that closes while the start is in-flight should fail the completion
+ * with an {@link IllegalStateException}
+ *
+ * @param completion
+ * A {@link AMQPFederationAsyncCompletion} that will be notified when the stop request succeeds or fails.
+ */
+ public final void startAsync(AMQPFederationAsyncCompletion<AMQPFederationConsumer> completion) {
+ Objects.requireNonNull(completion, "The asynchronous completion object cannot be null");
+
+ if (closed.get()) {
+ throw new IllegalStateException("The consumer has already been closed.");
+ }
+
+ if (!initialized) {
+ throw new IllegalStateException("A consumer must be initialized before a start call");
+ }
+
+ connection.runLater(() -> {
+ try {
+ if (receiver == null) {
+ throw new IllegalStateException("The consumer was either not initialized or the receiver create failed");
+ }
+
+ receiver.start();
+ completion.onComplete(this);
+ } catch (Exception error) {
+ completion.onException(this, error);
+ }
+
+ connection.flush();
+ });
+ }
+
+ /**
+ * Stops message consumption on this consumer instance but leaves the consumer in
+ * a state where it could be restarted by a call to {@link #startAsync(AMQPFederationAsyncCompletion)}
+ * once the consumer enters the stopped state.
+ * <p>
+ * Since the request to stop can take time to complete and this method cannot block
+ * a completion must be provided by the caller that will respond when the consumer
+ * has fully come to rest and all pending work is complete. Before the stopped
+ * completion is signaled the state of the underlying consumer will be stopping and
+ * attempt to restart it should fail until the stopped state has been reached.
+ * <p>
+ * The supplied {@link AMQPFederationAsyncCompletion} will be completed successfully
+ * once the underling AMQP receiver has drained and pending work is completed. If the
+ * stop does not complete by the supplied timeout the completion will be signaled that
+ * a failure has occurred with a {@link TimeoutException}. The completion methods are
+ * always called from a different thread than this method is called in which means the
+ * caller should ensure that the handling accounts for thread safety of those methods.
+ *
+ * @param completion
+ * A {@link AMQPFederationAsyncCompletion} that will be notified when the stop request succeeds or fails.
+ */
+ public final void stopAsync(AMQPFederationAsyncCompletion<AMQPFederationConsumer> completion) {
+ Objects.requireNonNull(completion, "The asynchronous completion object cannot be null");
+
+ if (!initialized) {
+ throw new IllegalStateException("A receiver must be initialized before a stop call");
+ }
+
+ connection.runLater(() -> {
+ try {
+ if (receiver == null) {
+ throw new IllegalStateException("The consumer was either not yet initialized or the receiver create failed");
+ }
+
+ receiver.stop(configuration.getReceiverQuiesceTimeout(), (rcvr, stopped) -> {
+ try {
+ if (stopped) {
+ completion.onComplete(this);
+ } else {
+ completion.onException(this, new TimeoutException("Timed out waiting for the AMQP link to stop"));
+ }
+ } catch (Exception ex) {
+ logger.trace("Caught error running provided completion callback: ", ex);
+ }
+ });
+ } catch (Exception error) {
+ completion.onException(this, error);
}
+
+ connection.flush();
+ });
+ }
+
+ /**
+ * Close the federation consumer instance and cleans up its resources. This method
+ * should not block and the actual resource shutdown work should occur asynchronously
+ * however the closed state should be indicated immediately and any further attempts
+ * start the consumer should result in an exception being thrown.
+ */
+ public final void close() {
+ if (closed.compareAndSet(false, true)) {
+ connection.runLater(() -> {
+ federation.removeLinkClosedInterceptor(consumerInfo.getId());
+
+ if (receiver != null) {
+ try {
+ receiver.close(false);
+ } catch (ActiveMQAMQPException e) {
+ } finally {
+ receiver = null;
+ }
+ }
+
+ // Need to track the proton receiver and close it here as the default
+ // context implementation doesn't do that and could result in no detach
+ // being sent in some cases and possible resources leaks.
+ if (protonReceiver != null) {
+ try {
+ protonReceiver.close();
+ } finally {
+ protonReceiver = null;
+ }
+ }
+
+ connection.flush();
+ });
}
}
- @Override
- public final AMQPFederationConsumer setRemoteClosedHandler(Consumer<FederationConsumerInternal> handler) {
- if (started) {
+ /**
+ * @return <code>true</code> if the receiver has already been closed.
+ */
+ public boolean isClosed() {
+ return closed.get();
+ }
+
+ /**
+ * Provides and event point for notification of the consumer having been closed by
+ * the remote.
+ *
+ * @param handler
+ * The handler that will be invoked when the remote closes this consumer.
+ *
+ * @return this consumer instance.
+ */
+ public final AMQPFederationConsumer setRemoteClosedHandler(Consumer<AMQPFederationConsumer> handler) {
+ if (protonReceiver != null) {
throw new IllegalStateException("Cannot set a remote close handler after the consumer is started");
}
@@ -161,15 +356,42 @@ public abstract class AMQPFederationConsumer implements FederationConsumerIntern
}
/**
- * Called during the start of the consumer to trigger an asynchronous link attach
- * of the underlying AMQP receiver that backs this federation consumer.
+ * Called before the message is dispatched to the broker for processing.
+ *
+ * @param message
+ * The message after any processing which is about to be dispatched.
+ *
+ * @throws ActiveMQException if any broker plugin throws an exception during its processing.
*/
- protected abstract void asyncCreateReceiver();
+ protected final void signalPluginBeforeFederationConsumerMessageHandled(Message message) throws ActiveMQException {
+ try {
+ federation.getServer().callBrokerAMQPFederationPlugins((plugin) -> {
+ if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
+ ((ActiveMQServerAMQPFederationPlugin) plugin).beforeFederationConsumerMessageHandled(this, message);
+ }
+ });
+ } catch (ActiveMQException t) {
+ ActiveMQServerLogger.LOGGER.federationPluginExecutionError("beforeFederationConsumerMessageHandled", t);
+ }
+ }
/**
- * Called during the close of the consumer to trigger an asynchronous link detach
- * of the underlying AMQP receiver that backs this federation consumer.
+ * Called after the message is dispatched to the broker for processing.
+ *
+ * @param message
+ * The message after any processing which has been dispatched to the broker.
+ *
+ * @throws ActiveMQException if any broker plugin throws an exception during its processing.
*/
- protected abstract void asyncCloseReceiver();
-
+ protected final void signalPluginAfterFederationConsumerMessageHandled(Message message) throws ActiveMQException {
+ try {
+ federation.getServer().callBrokerAMQPFederationPlugins((plugin) -> {
+ if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
+ ((ActiveMQServerAMQPFederationPlugin) plugin).afterFederationConsumerMessageHandled(this, message);
+ }
+ });
+ } catch (ActiveMQException t) {
+ ActiveMQServerLogger.LOGGER.federationPluginExecutionError("afterFederationConsumerMessageHandled", t);
+ }
+ }
}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConsumerConfiguration.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConsumerConfiguration.java
index 0192558c29..971e73a5d1 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConsumerConfiguration.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationConsumerConfiguration.java
@@ -24,6 +24,7 @@ import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPF
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.PULL_RECEIVER_BATCH_SIZE;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_CREDITS;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_CREDITS_LOW;
+import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_QUIESCE_TIMEOUT;
import java.util.Collections;
import java.util.HashMap;
@@ -79,6 +80,17 @@ public final class AMQPFederationConsumerConfiguration {
}
}
+ public int getReceiverQuiesceTimeout() {
+ final Object property = properties.get(RECEIVER_QUIESCE_TIMEOUT);
+ if (property instanceof Number) {
+ return ((Number) property).intValue();
+ } else if (property instanceof String) {
+ return Integer.parseInt((String) property);
+ } else {
+ return configuration.getReceiverQuiesceTimeout();
+ }
+ }
+
/**
* @return the credit batch size offered to a {@link Receiver} link that is in pull mode.
*/
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationEventProcessor.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationEventProcessor.java
index 435e275575..d2749609b2 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationEventProcessor.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationEventProcessor.java
@@ -28,6 +28,7 @@ import java.lang.invoke.MethodHandles;
import java.util.Map;
import org.apache.activemq.artemis.api.core.Message;
+import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.transaction.Transaction;
import org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage;
@@ -109,7 +110,7 @@ public class AMQPFederationEventProcessor extends ProtonAbstractReceiver {
// Inform the federation that there is an event processor in play.
federation.registerEventReceiver(this);
- flow();
+ topUpCreditIfNeeded();
}
@Override
@@ -148,7 +149,7 @@ public class AMQPFederationEventProcessor extends ProtonAbstractReceiver {
delivery.disposition(Accepted.getInstance());
delivery.settle();
- flow();
+ topUpCreditIfNeeded();
connection.flush();
} catch (Throwable e) {
@@ -167,7 +168,12 @@ public class AMQPFederationEventProcessor extends ProtonAbstractReceiver {
}
@Override
- public void flow() {
+ protected void doCreditTopUpRun() {
creditRunnable.run();
}
+
+ @Override
+ protected SimpleString getAddressInUse() {
+ return SimpleString.of(receiver.getName());
+ }
}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationGenericConsumerInfo.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationGenericConsumerInfo.java
similarity index 87%
rename from artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationGenericConsumerInfo.java
rename to artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationGenericConsumerInfo.java
index a473b2ee00..6e13a2bafa 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationGenericConsumerInfo.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationGenericConsumerInfo.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.activemq.artemis.protocol.amqp.federation.internal;
+package org.apache.activemq.artemis.protocol.amqp.connect.federation;
import java.util.Objects;
import java.util.UUID;
@@ -32,7 +32,7 @@ import org.apache.activemq.artemis.utils.CompositeAddress;
* queues and addresses. Instances of this class should be usable in Collections
* classes where equality and hashing support is needed.
*/
-public class FederationGenericConsumerInfo implements FederationConsumerInfo {
+public class AMQPFederationGenericConsumerInfo implements FederationConsumerInfo {
public static final String FEDERATED_QUEUE_PREFIX = "federated";
public static final String QUEUE_NAME_FORMAT_STRING = "${address}::${routeType}";
@@ -46,7 +46,7 @@ public class FederationGenericConsumerInfo implements FederationConsumerInfo {
private final int priority;
private final String id;
- public FederationGenericConsumerInfo(Role role, String address, String queueName, RoutingType routingType,
+ public AMQPFederationGenericConsumerInfo(Role role, String address, String queueName, RoutingType routingType,
String filterString, String fqqn, int priority) {
this.role = role;
this.address = address;
@@ -76,8 +76,8 @@ public class FederationGenericConsumerInfo implements FederationConsumerInfo {
*
* @return a newly created and configured {@link FederationConsumerInfo} instance.
*/
- public static FederationGenericConsumerInfo build(String address, String queueName, RoutingType routingType, String filterString, Federation federation, FederationReceiveFromAddressPolicy policy) {
- return new FederationGenericConsumerInfo(Role.ADDRESS_CONSUMER,
+ public static AMQPFederationGenericConsumerInfo build(String address, String queueName, RoutingType routingType, String filterString, Federation federation, FederationReceiveFromAddressPolicy policy) {
+ return new AMQPFederationGenericConsumerInfo(Role.ADDRESS_CONSUMER,
address,
queueName,
routingType,
@@ -132,11 +132,11 @@ public class FederationGenericConsumerInfo implements FederationConsumerInfo {
return true;
}
- if (!(o instanceof FederationGenericConsumerInfo)) {
+ if (!(o instanceof AMQPFederationGenericConsumerInfo)) {
return false;
}
- final FederationGenericConsumerInfo that = (FederationGenericConsumerInfo) o;
+ final AMQPFederationGenericConsumerInfo that = (AMQPFederationGenericConsumerInfo) o;
return role == that.role &&
priority == that.priority &&
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationPolicyManager.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationPolicyManager.java
new file mode 100644
index 0000000000..13e6fbe6c7
--- /dev/null
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationPolicyManager.java
@@ -0,0 +1,328 @@
+/*
+ * 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.artemis.protocol.amqp.connect.federation;
+
+import java.lang.invoke.MethodHandles;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.BiConsumer;
+
+import org.apache.activemq.artemis.api.core.ActiveMQException;
+import org.apache.activemq.artemis.api.core.Message;
+import org.apache.activemq.artemis.core.server.ActiveMQServer;
+import org.apache.activemq.artemis.core.server.ActiveMQServerLogger;
+import org.apache.activemq.artemis.core.server.Divert;
+import org.apache.activemq.artemis.core.server.Queue;
+import org.apache.activemq.artemis.core.server.impl.AddressInfo;
+import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerBindingPlugin;
+import org.apache.activemq.artemis.protocol.amqp.federation.Federation;
+import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumer;
+import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumerInfo;
+import org.apache.activemq.artemis.protocol.amqp.federation.FederationReceiveFromResourcePolicy;
+import org.apache.activemq.artemis.protocol.amqp.federation.FederationType;
+import org.apache.activemq.artemis.protocol.amqp.proton.AMQPSessionContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Base Federation policy manager that declares some common APIs that address or queue policy
+ * managers must provide implementations for.
+ */
+public abstract class AMQPFederationPolicyManager implements ActiveMQServerBindingPlugin {
+
+ private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+ protected final AtomicLong messageCount = new AtomicLong();
+ protected final BiConsumer<FederationConsumerInfo, Message> messageObserver = (i, m) -> messageCount.incrementAndGet();
+ protected final ActiveMQServer server;
+ protected final AMQPFederation federation;
+
+ protected volatile boolean started;
+ protected volatile AMQPFederationConsumerConfiguration configuration;
+ protected volatile AMQPSessionContext session;
+
+ public AMQPFederationPolicyManager(AMQPFederation federation) throws ActiveMQException {
+ Objects.requireNonNull(federation, "The Federation instance cannot be null");
+
+ this.federation = federation;
+ this.server = federation.getServer();
+ }
+
+ /**
+ * @return the number of messages that all federation consumer of this policy have received from the remote.
+ */
+ public long getMessagesReceived() {
+ return messageCount.get();
+ }
+
+ /**
+ * @return the immutable federation policy configuration that backs this manager.
+ */
+ public abstract FederationReceiveFromResourcePolicy getPolicy();
+
+ /**
+ * @return the federation type this policy manager implements.
+ */
+ public final FederationType getPolicyType() {
+ return getPolicy().getPolicyType();
+ }
+
+ /**
+ * @return the assigned name of the policy that is being managed.
+ */
+ public final String getPolicyName() {
+ return getPolicy().getPolicyName();
+ }
+
+ /**
+ * @return <code>true</code> if the policy is started at the time this method was called.
+ */
+ public final boolean isStarted() {
+ return started;
+ }
+
+ /**
+ * @return the {@link AMQPFederation} instance that owns this policy manager.
+ */
+ public AMQPFederation getFederation() {
+ return federation;
+ }
+
+ /**
+ * Start the federation policy manager which will initiate a scan of all broker
+ * bindings and create and matching remote receivers. Start on a policy manager
+ * should only be called after its parent {@link Federation} is started and the
+ * federation connection has been established.
+ */
+ public synchronized void start() {
+ if (!federation.isStarted()) {
+ throw new IllegalStateException("Cannot start a federation policy manager when the federation is stopped.");
+ }
+
+ if (!started) {
+ started = true;
+ // Capture state for the current connection on each start of the policy manager.
+ configuration = new AMQPFederationConsumerConfiguration(federation.getConfiguration(), getPolicy().getProperties());
+ session = federation.getSessionContext();
+ server.registerBrokerPlugin(this);
+ scanAllBindings(); // Create federation consumers for any matches to the policy at startup.
+ }
+ }
+
+ /**
+ * On start a federation policy manager needs to scan all the current bindings for matches
+ * to its configuration among the domain it manages (address or queue).
+ */
+ protected abstract void scanAllBindings();
+
+ /**
+ * Create a new {@link AMQPFederationConsumer} instance using the consumer information
+ * given. This is called when local demand for a matched resource requires a new consumer to
+ * be created. This method by default will call the configured consumer factory function that
+ * was provided when the manager was created, a subclass can override this to perform additional
+ * actions for the create operation.
+ *
+ * @param consumerInfo
+ * The {@link FederationConsumerInfo} that defines the consumer to be created.
+ *
+ * @return a new {@link AMQPFederationConsumer} instance that will reside in this manager.
+ */
+ protected abstract AMQPFederationConsumer createFederationConsumer(FederationConsumerInfo consumerInfo);
+
+ /**
+ * Attempts to close a federation consumer and signals the installed federation plugin
+ * of the impending and post closed state. The method will not double close a consumer
+ * as it checks the closed state. The method is synchronized to allow for use in asynchronous
+ * call backs from federation consumers.
+ *
+ * @param federationConsuner
+ * A federation consumer to close, or null in which case no action is taken.
+ */
+ protected synchronized void tryCloseFederationConsumer(AMQPFederationConsumer federationConsuner) {
+ if (federationConsuner != null) {
+ try {
+ if (!federationConsuner.isClosed()) {
+ signalPluginBeforeCloseFederationConsumer(federationConsuner);
+ federationConsuner.close();
+ signalPluginAfterCloseFederationConsumer(federationConsuner);
+ }
+ } catch (Exception ignore) {
+ logger.trace("Caught error on attempted close of existing federation consumer", ignore);
+ }
+ }
+ }
+
+ /**
+ * Signal any registered plugins for this federation instance that a remote consumer
+ * is being created.
+ *
+ * @param info
+ * The {@link FederationConsumerInfo} that describes the remote federation consumer
+ */
+ protected final void signalPluginBeforeCreateFederationConsumer(FederationConsumerInfo info) {
+ try {
+ server.callBrokerAMQPFederationPlugins((plugin) -> {
+ if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
+ ((ActiveMQServerAMQPFederationPlugin) plugin).beforeCreateFederationConsumer(info);
+ }
+ });
+ } catch (ActiveMQException t) {
+ ActiveMQServerLogger.LOGGER.federationPluginExecutionError("beforeCreateFederationConsumer", t);
+ }
+ }
+
+ /**
+ * Signal any registered plugins for this federation instance that a remote consumer
+ * has been created.
+ *
+ * @param consumer
+ * The {@link FederationConsumerInfo} that describes the remote consumer
+ */
+ protected final void signalPluginAfterCreateFederationConsumer(FederationConsumer consumer) {
+ try {
+ server.callBrokerAMQPFederationPlugins((plugin) -> {
+ if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
+ ((ActiveMQServerAMQPFederationPlugin) plugin).afterCreateFederationConsumer(consumer);
+ }
+ });
+ } catch (ActiveMQException t) {
+ ActiveMQServerLogger.LOGGER.federationPluginExecutionError("afterCreateFederationConsumer", t);
+ }
+ }
+
+ /**
+ * Signal any registered plugins for this federation instance that a remote consumer
+ * is about to be closed.
+ *
+ * @param consumer
+ * The {@link FederationConsumer} that that is about to be closed.
+ */
+ protected final void signalPluginBeforeCloseFederationConsumer(FederationConsumer consumer) {
+ try {
+ server.callBrokerAMQPFederationPlugins((plugin) -> {
+ if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
+ ((ActiveMQServerAMQPFederationPlugin) plugin).beforeCloseFederationConsumer(consumer);
+ }
+ });
+ } catch (ActiveMQException t) {
+ ActiveMQServerLogger.LOGGER.federationPluginExecutionError("beforeCloseFederationConsumer", t);
+ }
+ }
+
+ /**
+ * Signal any registered plugins for this federation instance that a remote consumer
+ * has now been closed.
+ *
+ * @param consumer
+ * The {@link FederationConsumer} that that has been closed.
+ */
+ protected final void signalPluginAfterCloseFederationConsumer(FederationConsumer consumer) {
+ try {
+ server.callBrokerAMQPFederationPlugins((plugin) -> {
+ if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
+ ((ActiveMQServerAMQPFederationPlugin) plugin).afterCloseFederationConsumer(consumer);
+ }
+ });
+ } catch (ActiveMQException t) {
+ ActiveMQServerLogger.LOGGER.federationPluginExecutionError("afterCloseFederationConsumer", t);
+ }
+ }
+
+ /**
+ * Query all registered plugins for this federation instance to determine if any wish to
+ * prevent a federation consumer from being created for the given resource.
+ *
+ * @param address
+ * The address on which the manager is intending to create a remote consumer for.
+ *
+ * @return true if any registered plugin signaled that creation should be suppressed.
+ */
+ protected final boolean isPluginBlockingFederationConsumerCreate(AddressInfo address) {
+ final AtomicBoolean canCreate = new AtomicBoolean(true);
+
+ try {
+ server.callBrokerAMQPFederationPlugins((plugin) -> {
+ if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
+ if (canCreate.get()) {
+ canCreate.set(((ActiveMQServerAMQPFederationPlugin) plugin).shouldCreateFederationConsumerForAddress(address));
+ }
+ }
+ });
+ } catch (ActiveMQException t) {
+ ActiveMQServerLogger.LOGGER.federationPluginExecutionError("shouldCreateFederationConsumerForAddress", t);
+ }
+
+ return !canCreate.get();
+ }
+
+ /**
+ * Query all registered plugins for this federation instance to determine if any wish to
+ * prevent a federation consumer from being created for the given Queue.
+ *
+ * @param divert
+ * The {@link Divert} that triggered the manager to attempt to create a remote consumer.
+ * @param queue
+ * The {@link Queue} that triggered the manager to attempt to create a remote consumer.
+ *
+ * @return true if any registered plugin signaled that creation should be suppressed.
+ */
+ protected final boolean isPluginBlockingFederationConsumerCreate(Divert divert, Queue queue) {
+ final AtomicBoolean canCreate = new AtomicBoolean(true);
+
+ try {
+ server.callBrokerAMQPFederationPlugins((plugin) -> {
+ if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
+ if (canCreate.get()) {
+ canCreate.set(((ActiveMQServerAMQPFederationPlugin) plugin).shouldCreateFederationConsumerForDivert(divert, queue));
+ }
+ }
+ });
+ } catch (ActiveMQException t) {
+ ActiveMQServerLogger.LOGGER.federationPluginExecutionError("shouldCreateFederationConsumerForDivert", t);
+ }
+
+ return !canCreate.get();
+ }
+
+ /**
+ * Query all registered plugins for this federation instance to determine if any wish to
+ * prevent a federation consumer from being created for the given Queue.
+ *
+ * @param queue
+ * The {@link Queue} that triggered the manager to attempt to create a remote consumer.
+ *
+ * @return true if any registered plugin signaled that creation should be suppressed.
+ */
+ protected final boolean isPluginBlockingFederationConsumerCreate(Queue queue) {
+ final AtomicBoolean canCreate = new AtomicBoolean(true);
+
+ try {
+ server.callBrokerAMQPFederationPlugins((plugin) -> {
+ if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
+ if (canCreate.get()) {
+ canCreate.set(((ActiveMQServerAMQPFederationPlugin) plugin).shouldCreateFederationConsumerForQueue(queue));
+ }
+ }
+ });
+ } catch (ActiveMQException t) {
+ ActiveMQServerLogger.LOGGER.federationPluginExecutionError("shouldCreateFederationConsumerForQueue", t);
+ }
+
+ return !canCreate.get();
+ }
+}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationQueueConsumer.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationQueueConsumer.java
index 48f14cd880..7721bb260c 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationQueueConsumer.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationQueueConsumer.java
@@ -29,17 +29,12 @@ import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;
-import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.Message;
import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString;
-import org.apache.activemq.artemis.core.config.TransformerConfiguration;
-import org.apache.activemq.artemis.core.server.ActiveMQServerLogger;
import org.apache.activemq.artemis.core.server.Queue;
import org.apache.activemq.artemis.core.server.QueueQueryResult;
-import org.apache.activemq.artemis.core.server.transformer.Transformer;
import org.apache.activemq.artemis.core.transaction.Transaction;
import org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPException;
import org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPInternalErrorException;
@@ -81,31 +76,17 @@ public class AMQPFederationQueueConsumer extends AMQPFederationConsumer {
public static final int DEFAULT_PENDING_MSG_CHECK_BACKOFF_MULTIPLIER = 2;
public static final int DEFAULT_PENDING_MSG_CHECK_MAX_DELAY = 30;
- // Sequence ID value used to keep links that would otherwise have the same name from overlapping
- // this generally occurs when consumers on the same queue have differing filters.
- private static final AtomicLong LINK_SEQUENCE_ID = new AtomicLong();
-
private final AMQPFederationQueuePolicyManager manager;
private final FederationReceiveFromQueuePolicy policy;
- private final Transformer transformer;
-
- private AMQPFederatedQueueDeliveryReceiver receiver;
public AMQPFederationQueueConsumer(AMQPFederationQueuePolicyManager manager,
AMQPFederationConsumerConfiguration configuration,
AMQPSessionContext session, FederationConsumerInfo consumerInfo,
BiConsumer<FederationConsumerInfo, Message> messageObserver) {
- super(manager.getFederation(), configuration, session, consumerInfo, messageObserver);
+ super(manager.getFederation(), configuration, session, consumerInfo, manager.getPolicy(), messageObserver);
this.manager = manager;
this.policy = manager.getPolicy();
-
- final TransformerConfiguration transformerConfiguration = policy.getTransformerConfiguration();
- if (transformerConfiguration != null) {
- this.transformer = federation.getServer().getServiceRegistry().getFederationTransformer(policy.getPolicyName(), transformerConfiguration);
- } else {
- this.transformer = (m) -> m;
- }
}
/**
@@ -115,30 +96,6 @@ public class AMQPFederationQueueConsumer extends AMQPFederationConsumer {
return policy;
}
- private void signalBeforeFederationConsumerMessageHandled(Message message) throws ActiveMQException {
- try {
- federation.getServer().callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- ((ActiveMQServerAMQPFederationPlugin) plugin).beforeFederationConsumerMessageHandled(this, message);
- }
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("beforeFederationConsumerMessageHandled", t);
- }
- }
-
- private void signalAfterFederationConsumerMessageHandled(Message message) throws ActiveMQException {
- try {
- federation.getServer().callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- ((ActiveMQServerAMQPFederationPlugin) plugin).afterFederationConsumerMessageHandled(this, message);
- }
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("afterFederationConsumerMessageHandled", t);
- }
- }
-
private String generateLinkName() {
return "federation-" + federation.getName() +
"-queue-receiver-" + consumerInfo.getFqqn() +
@@ -147,147 +104,112 @@ public class AMQPFederationQueueConsumer extends AMQPFederationConsumer {
}
@Override
- protected void asyncCreateReceiver() {
- connection.runLater(() -> {
- if (closed) {
- return;
+ protected void doCreateReceiver() {
+ try {
+ final Receiver protonReceiver = session.getSession().receiver(generateLinkName());
+ final Target target = new Target();
+ final Source source = new Source();
+ final String address = consumerInfo.getFqqn();
+ final Queue localQueue = federation.getServer().locateQueue(consumerInfo.getQueueName());
+
+ if (RoutingType.ANYCAST.equals(consumerInfo.getRoutingType())) {
+ source.setCapabilities(AmqpSupport.QUEUE_CAPABILITY);
+ } else {
+ source.setCapabilities(AmqpSupport.TOPIC_CAPABILITY);
}
- try {
- final Receiver protonReceiver = session.getSession().receiver(generateLinkName());
- final Target target = new Target();
- final Source source = new Source();
- final String address = consumerInfo.getFqqn();
- final Queue localQueue = federation.getServer().locateQueue(consumerInfo.getQueueName());
-
- if (RoutingType.ANYCAST.equals(consumerInfo.getRoutingType())) {
- source.setCapabilities(AmqpSupport.QUEUE_CAPABILITY);
- } else {
- source.setCapabilities(AmqpSupport.TOPIC_CAPABILITY);
- }
+ source.setOutcomes(Arrays.copyOf(OUTCOMES, OUTCOMES.length));
+ source.setDefaultOutcome(DEFAULT_OUTCOME);
+ source.setDurable(TerminusDurability.NONE);
+ source.setExpiryPolicy(TerminusExpiryPolicy.LINK_DETACH);
+ source.setAddress(address);
- source.setOutcomes(Arrays.copyOf(OUTCOMES, OUTCOMES.length));
- source.setDefaultOutcome(DEFAULT_OUTCOME);
- source.setDurable(TerminusDurability.NONE);
- source.setExpiryPolicy(TerminusExpiryPolicy.LINK_DETACH);
- source.setAddress(address);
+ if (consumerInfo.getFilterString() != null && !consumerInfo.getFilterString().isEmpty()) {
+ final AmqpJmsSelectorFilter jmsFilter = new AmqpJmsSelectorFilter(consumerInfo.getFilterString());
+ final Map<Symbol, Object> filtersMap = new HashMap<>();
+ filtersMap.put(AmqpSupport.JMS_SELECTOR_KEY, jmsFilter);
- if (consumerInfo.getFilterString() != null && !consumerInfo.getFilterString().isEmpty()) {
- final AmqpJmsSelectorFilter jmsFilter = new AmqpJmsSelectorFilter(consumerInfo.getFilterString());
- final Map<Symbol, Object> filtersMap = new HashMap<>();
- filtersMap.put(AmqpSupport.JMS_SELECTOR_KEY, jmsFilter);
+ source.setFilter(filtersMap);
+ }
- source.setFilter(filtersMap);
- }
+ target.setAddress(address);
- target.setAddress(address);
+ final Map<Symbol, Object> receiverProperties = new HashMap<>();
+ receiverProperties.put(FEDERATION_RECEIVER_PRIORITY, consumerInfo.getPriority());
- final Map<Symbol, Object> receiverProperties = new HashMap<>();
- receiverProperties.put(FEDERATION_RECEIVER_PRIORITY, consumerInfo.getPriority());
+ protonReceiver.setSenderSettleMode(SenderSettleMode.UNSETTLED);
+ protonReceiver.setReceiverSettleMode(ReceiverSettleMode.FIRST);
+ protonReceiver.setDesiredCapabilities(new Symbol[] {FEDERATION_QUEUE_RECEIVER});
+ // If enabled offer core tunneling which we prefer to AMQP conversions of core as
+ // the large ones will be converted to standard AMQP messages in memory. When not
+ // offered the remote must not use core tunneling and AMQP conversion will be the
+ // fallback.
+ if (configuration.isCoreMessageTunnelingEnabled()) {
+ protonReceiver.setOfferedCapabilities(new Symbol[] {AmqpSupport.CORE_MESSAGE_TUNNELING_SUPPORT});
+ }
+ protonReceiver.setProperties(receiverProperties);
+ protonReceiver.setTarget(target);
+ protonReceiver.setSource(source);
+ protonReceiver.open();
+
+ final ScheduledFuture<?> openTimeoutTask;
+ final AtomicBoolean openTimedOut = new AtomicBoolean(false);
+
+ if (configuration.getLinkAttachTimeout() > 0) {
+ openTimeoutTask = federation.getServer().getScheduledPool().schedule(() -> {
+ openTimedOut.set(true);
+ federation.signalResourceCreateError(ActiveMQAMQPProtocolMessageBundle.BUNDLE.brokerConnectionTimeout());
+ }, configuration.getLinkAttachTimeout(), TimeUnit.SECONDS);
+ } else {
+ openTimeoutTask = null;
+ }
- protonReceiver.setSenderSettleMode(SenderSettleMode.UNSETTLED);
- protonReceiver.setReceiverSettleMode(ReceiverSettleMode.FIRST);
- protonReceiver.setDesiredCapabilities(new Symbol[] {FEDERATION_QUEUE_RECEIVER});
- // If enabled offer core tunneling which we prefer to AMQP conversions of core as
- // the large ones will be converted to standard AMQP messages in memory. When not
- // offered the remote must not use core tunneling and AMQP conversion will be the
- // fallback.
- if (configuration.isCoreMessageTunnelingEnabled()) {
- protonReceiver.setOfferedCapabilities(new Symbol[] {AmqpSupport.CORE_MESSAGE_TUNNELING_SUPPORT});
- }
- protonReceiver.setProperties(receiverProperties);
- protonReceiver.setTarget(target);
- protonReceiver.setSource(source);
- protonReceiver.open();
-
- final ScheduledFuture<?> openTimeoutTask;
- final AtomicBoolean openTimedOut = new AtomicBoolean(false);
-
- if (configuration.getLinkAttachTimeout() > 0) {
- openTimeoutTask = federation.getServer().getScheduledPool().schedule(() -> {
- openTimedOut.set(true);
- federation.signalResourceCreateError(ActiveMQAMQPProtocolMessageBundle.BUNDLE.brokerConnectionTimeout());
- }, configuration.getLinkAttachTimeout(), TimeUnit.SECONDS);
- } else {
- openTimeoutTask = null;
- }
+ this.protonReceiver = protonReceiver;
- this.protonReceiver = protonReceiver;
+ protonReceiver.attachments().set(AMQP_LINK_INITIALIZER_KEY, Runnable.class, () -> {
+ try {
+ if (openTimeoutTask != null) {
+ openTimeoutTask.cancel(false);
+ }
- protonReceiver.attachments().set(AMQP_LINK_INITIALIZER_KEY, Runnable.class, () -> {
- try {
- if (openTimeoutTask != null) {
- openTimeoutTask.cancel(false);
- }
-
- if (openTimedOut.get()) {
- return;
- }
-
- // Remote must support federation receivers otherwise we fail the connection unless the
- // Attach indicates that a detach is incoming in which case we just allow the normal handling
- // to occur.
- if (protonReceiver.getRemoteSource() != null && !AmqpSupport.verifyOfferedCapabilities(protonReceiver, FEDERATION_QUEUE_RECEIVER)) {
- federation.signalResourceCreateError(
- ActiveMQAMQPProtocolMessageBundle.BUNDLE.missingOfferedCapability(FEDERATION_QUEUE_RECEIVER.toString()));
- return;
- }
-
- // Intercept remote close and check for valid reasons for remote closure such as
- // the remote peer not having a matching queue for this subscription or from an
- // operator manually closing the link.
- federation.addLinkClosedInterceptor(consumerInfo.getId(), remoteCloseInterceptor);
-
- receiver = new AMQPFederatedQueueDeliveryReceiver(localQueue, protonReceiver);
-
- if (protonReceiver.getRemoteSource() != null) {
- logger.debug("AMQP Federation {} queue consumer {} completed open", federation.getName(), consumerInfo);
- } else {
- logger.debug("AMQP Federation {} queue consumer {} rejected by remote", federation.getName(), consumerInfo);
- }
-
- session.addReceiver(protonReceiver, (session, protonRcvr) -> {
- return this.receiver;
- });
- } catch (Exception e) {
- federation.signalError(e);
+ if (openTimedOut.get()) {
+ return;
}
- });
- } catch (Exception e) {
- federation.signalError(e);
- }
- connection.flush();
- });
- }
+ // Remote must support federation receivers otherwise we fail the connection unless the
+ // Attach indicates that a detach is incoming in which case we just allow the normal handling
+ // to occur.
+ if (protonReceiver.getRemoteSource() != null && !AmqpSupport.verifyOfferedCapabilities(protonReceiver, FEDERATION_QUEUE_RECEIVER)) {
+ federation.signalResourceCreateError(
+ ActiveMQAMQPProtocolMessageBundle.BUNDLE.missingOfferedCapability(FEDERATION_QUEUE_RECEIVER.toString()));
+ return;
+ }
- @Override
- protected final void asyncCloseReceiver() {
- connection.runLater(() -> {
- federation.removeLinkClosedInterceptor(consumerInfo.getId());
+ // Intercept remote close and check for valid reasons for remote closure such as
+ // the remote peer not having a matching queue for this subscription or from an
+ // operator manually closing the link.
+ federation.addLinkClosedInterceptor(consumerInfo.getId(), remoteCloseInterceptor);
- if (receiver != null) {
- try {
- receiver.close(false);
- } catch (ActiveMQAMQPException e) {
- } finally {
- receiver = null;
- }
- }
+ receiver = new AMQPFederatedQueueDeliveryReceiver(localQueue, protonReceiver);
- // Need to track the proton receiver and close it here as the default
- // context implementation doesn't do that and could result in no detach
- // being sent in some cases and possible resources leaks.
- if (protonReceiver != null) {
- try {
- protonReceiver.close();
- } finally {
- protonReceiver = null;
+ if (protonReceiver.getRemoteSource() != null) {
+ logger.debug("AMQP Federation {} queue consumer {} completed open", federation.getName(), consumerInfo);
+ } else {
+ logger.debug("AMQP Federation {} queue consumer {} rejected by remote", federation.getName(), consumerInfo);
+ }
+
+ session.addReceiver(protonReceiver, (session, protonRcvr) -> {
+ return this.receiver;
+ });
+ } catch (Exception e) {
+ federation.signalError(e);
}
- }
+ });
+ } catch (Exception e) {
+ federation.signalError(e);
+ }
- connection.flush();
- });
+ connection.flush();
}
private static int caclulateNextDelay(int lastDelay, int backoffMultiplier, int maxDelay) {
@@ -401,7 +323,7 @@ public class AMQPFederationQueueConsumer extends AMQPFederationConsumer {
logger.debug("Error caught when trying to add federation queue consumer to management", e);
}
- flow();
+ topUpCreditIfNeeded();
}
@Override
@@ -432,9 +354,9 @@ public class AMQPFederationQueueConsumer extends AMQPFederationConsumer {
transformer, message, theMessage);
}
- signalBeforeFederationConsumerMessageHandled(theMessage);
+ signalPluginBeforeFederationConsumerMessageHandled(theMessage);
sessionSPI.serverSend(this, tx, receiver, delivery, cachedFqqn, routingContext, theMessage);
- signalAfterFederationConsumerMessageHandled(theMessage);
+ signalPluginAfterFederationConsumerMessageHandled(theMessage);
} catch (Exception e) {
logger.warn("Inbound delivery for {} encountered an error: {}", consumerInfo, e.getMessage(), e);
deliveryFailed(delivery, receiver, e);
@@ -509,8 +431,8 @@ public class AMQPFederationQueueConsumer extends AMQPFederationConsumer {
private void performCreditTopUp() {
connection.requireInHandler();
- if (receiver.getLocalState() != EndpointState.ACTIVE) {
- return; // Closed before this was triggered.
+ if (!isStarted() || receiver.getLocalState() != EndpointState.ACTIVE) {
+ return; // Closed or stopped before this was triggered.
}
receiver.flow(configuration.getPullReceiverBatchSize());
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationQueueEntry.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationQueueEntry.java
similarity index 79%
rename from artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationQueueEntry.java
rename to artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationQueueEntry.java
index 0f4655f5ba..3219643d57 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationQueueEntry.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationQueueEntry.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.activemq.artemis.protocol.amqp.federation.internal;
+package org.apache.activemq.artemis.protocol.amqp.connect.federation;
import java.util.HashSet;
import java.util.Objects;
@@ -25,7 +25,7 @@ import org.apache.activemq.artemis.core.server.ServerConsumer;
import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumerInfo;
/**
- * An entry type class used to hold a {@link FederationConsumerInternal} and
+ * An entry type class used to hold a {@link AMQPFederationConsumer} and
* any other state data needed by the manager that is creating them based on the
* policy configuration for the federation instance. The entry can be extended
* by federation implementation to hold additional state data for the federation
@@ -35,12 +35,12 @@ import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumerIn
* on a federation resource such that it is not torn down until all demand has been
* removed from the local resource.
*/
-public class FederationQueueEntry {
+public class AMQPFederationQueueEntry {
private final FederationConsumerInfo consumerInfo;
private final Set<String> consumerDemand = new HashSet<>();
- private FederationConsumerInternal consumer;
+ private AMQPFederationConsumer consumer;
/**
* Creates a new queue entry with a single reference
@@ -48,7 +48,7 @@ public class FederationQueueEntry {
* @param consumerInfo
* Consumer information object used to define the federation queue consumer
*/
- public FederationQueueEntry(FederationConsumerInfo consumerInfo) {
+ public AMQPFederationQueueEntry(FederationConsumerInfo consumerInfo) {
this.consumerInfo = consumerInfo;
}
@@ -76,7 +76,7 @@ public class FederationQueueEntry {
/**
* @return the consumer managed by this entry
*/
- public FederationConsumerInternal getConsumer() {
+ public AMQPFederationConsumer getConsumer() {
return consumer;
}
@@ -88,7 +88,7 @@ public class FederationQueueEntry {
*
* @return this federation queue consumer entry.
*/
- public FederationQueueEntry setConsumer(FederationConsumerInternal consumer) {
+ public AMQPFederationQueueEntry setConsumer(AMQPFederationConsumer consumer) {
Objects.requireNonNull(consumer, "Cannot assign a null consumer to this entry, call clear to unset");
this.consumer = consumer;
return this;
@@ -97,11 +97,14 @@ public class FederationQueueEntry {
/**
* Clears the currently assigned consumer from this entry.
*
- * @return this federation queue consumer entry.
+ * @return the consumer that was stored here previously or null if none was set
*/
- public FederationQueueEntry clearConsumer() {
+ public AMQPFederationConsumer clearConsumer() {
+ final AMQPFederationConsumer taken = consumer;
+
this.consumer = null;
- return this;
+
+ return taken;
}
/**
@@ -119,7 +122,7 @@ public class FederationQueueEntry {
*
* @return this federation queue entry instance.
*/
- public FederationQueueEntry addDemand(ServerConsumer consumer) {
+ public AMQPFederationQueueEntry addDemand(ServerConsumer consumer) {
consumerDemand.add(identifyConsumer(consumer));
return this;
}
@@ -132,11 +135,21 @@ public class FederationQueueEntry {
*
* @return this federation queue entry instance.
*/
- public FederationQueueEntry removeDemand(ServerConsumer consumer) {
+ public AMQPFederationQueueEntry removeDemand(ServerConsumer consumer) {
consumerDemand.remove(identifyConsumer(consumer));
return this;
}
+ /**
+ * Remove all known demand on the resource from any previously added {@link ServerConsumer}s.
+ *
+ * @return this federation queue entry instance.
+ */
+ public AMQPFederationQueueEntry removeAllDemand() {
+ consumerDemand.clear();
+ return this;
+ }
+
private static String identifyConsumer(ServerConsumer consumer) {
return consumer.getConnectionID().toString() + ":" +
consumer.getSessionID() + ":" +
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationQueuePolicyManager.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationQueuePolicyManager.java
index 94beef1fba..96d36da30c 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationQueuePolicyManager.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/federation/AMQPFederationQueuePolicyManager.java
@@ -17,27 +17,31 @@
package org.apache.activemq.artemis.protocol.amqp.connect.federation;
+import static org.apache.activemq.artemis.protocol.amqp.federation.FederationConstants.FEDERATION_NAME;
+
import java.lang.invoke.MethodHandles;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Objects;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.function.BiConsumer;
+import java.util.function.Predicate;
+
import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration;
import org.apache.activemq.artemis.api.core.ActiveMQException;
-import org.apache.activemq.artemis.api.core.Message;
import org.apache.activemq.artemis.core.filter.Filter;
-import org.apache.activemq.artemis.core.server.ActiveMQServerLogger;
+import org.apache.activemq.artemis.core.filter.impl.FilterImpl;
+import org.apache.activemq.artemis.core.postoffice.Binding;
+import org.apache.activemq.artemis.core.postoffice.QueueBinding;
+import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.Queue;
import org.apache.activemq.artemis.core.server.ServerConsumer;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumer;
+import org.apache.activemq.artemis.core.server.ServerSession;
+import org.apache.activemq.artemis.core.server.federation.Federation;
+import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerBindingPlugin;
+import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerConsumerPlugin;
+import org.apache.activemq.artemis.core.transaction.Transaction;
import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumerInfo;
import org.apache.activemq.artemis.protocol.amqp.federation.FederationReceiveFromQueuePolicy;
import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumerInfo.Role;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationReceiveFromResourcePolicy;
-import org.apache.activemq.artemis.protocol.amqp.federation.internal.FederationConsumerInternal;
-import org.apache.activemq.artemis.protocol.amqp.federation.internal.FederationGenericConsumerInfo;
-import org.apache.activemq.artemis.protocol.amqp.federation.internal.FederationQueuePolicyManager;
-import org.apache.activemq.artemis.protocol.amqp.proton.AMQPSessionContext;
import org.apache.activemq.artemis.utils.CompositeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -45,44 +49,353 @@ import org.slf4j.LoggerFactory;
/**
* The AMQP Federation implementation of an federation queue policy manager.
*/
-public class AMQPFederationQueuePolicyManager extends FederationQueuePolicyManager {
+public final class AMQPFederationQueuePolicyManager extends AMQPFederationPolicyManager implements ActiveMQServerConsumerPlugin, ActiveMQServerBindingPlugin {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
- protected final AtomicLong messageCount = new AtomicLong();
- protected final BiConsumer<FederationConsumerInfo, Message> messageObserver = (i, m) -> messageCount.incrementAndGet();
- protected final AMQPFederation federation;
-
- protected volatile AMQPFederationConsumerConfiguration configuration;
- protected volatile AMQPSessionContext session;
+ protected final Predicate<ServerConsumer> federationConsumerMatcher;
+ protected final FederationReceiveFromQueuePolicy policy;
+ protected final Map<FederationConsumerInfo, AMQPFederationQueueEntry> demandTracking = new HashMap<>();
public AMQPFederationQueuePolicyManager(AMQPFederation federation, FederationReceiveFromQueuePolicy queuePolicy) throws ActiveMQException {
- super(federation, queuePolicy);
+ super(federation);
+
+ Objects.requireNonNull(queuePolicy, "The Queue match policy cannot be null");
- this.federation = federation;
+ this.policy = queuePolicy;
+ this.federationConsumerMatcher = createFederationConsumerMatcher(server, queuePolicy);
}
+ /**
+ * @return the receive from address policy that backs the address policy manager.
+ */
@Override
- public AMQPFederation getFederation() {
- return federation;
+ public FederationReceiveFromQueuePolicy getPolicy() {
+ return policy;
}
/**
- * @return the number of messages that all federation consumer of this policy have received from the remote.
+ * Stops the queue policy manager which will close any open remote receivers that are
+ * active for local queue demand. Stop should generally be called whenever the parent
+ * {@link Federation} loses its connection to the remote.
*/
- public long getMessagesReceived() {
- return messageCount.get();
+ public synchronized void stop() {
+ if (started) {
+ // Ensures that on shutdown of a federation broker connection we don't leak
+ // broker plugin instances.
+ server.unRegisterBrokerPlugin(this);
+ started = false;
+ demandTracking.values().forEach((entry) -> {
+ if (entry != null) {
+ tryCloseFederationConsumer(entry.removeAllDemand().clearConsumer());
+ }
+ });
+ demandTracking.clear();
+ }
}
@Override
- protected void handlePolicyManagerStarted(FederationReceiveFromResourcePolicy policy) {
- // Capture state for the current connection on each start of the policy manager.
- configuration = new AMQPFederationConsumerConfiguration(federation.getConfiguration(), policy.getProperties());
- session = federation.getSessionContext();
+ public synchronized void afterCreateConsumer(ServerConsumer consumer) {
+ if (started) {
+ reactIfConsumerMatchesPolicy(consumer);
+ }
}
@Override
- protected FederationConsumerInfo createConsumerInfo(ServerConsumer consumer) {
+ public synchronized void afterCloseConsumer(ServerConsumer consumer, boolean failed) {
+ if (started) {
+ final String queueName = consumer.getQueue().getName().toString();
+ final FederationConsumerInfo consumerInfo = createConsumerInfo(consumer);
+ final AMQPFederationQueueEntry entry = demandTracking.get(consumerInfo);
+
+ if (entry == null) {
+ return;
+ }
+
+ entry.removeDemand(consumer);
+
+ logger.trace("Reducing demand on federated queue {}, remaining demand? {}", queueName, entry.hasDemand());
+
+ if (!entry.hasDemand() && entry.hasConsumer()) {
+ // A started consumer should be allowed to stop before possible close either because demand
+ // is still not present or the remote did not respond before the configured stop timeout elapsed.
+ // A successfully stopped receiver can be restarted but if the stop times out the receiver should
+ // be closed and a new receiver created if demand is present.
+ entry.getConsumer().stopAsync(new AMQPFederationAsyncCompletion<AMQPFederationConsumer>() {
+
+ @Override
+ public void onComplete(AMQPFederationConsumer context) {
+ handleFederationConsumerStopped(consumer.getQueue(), entry, true);
+ }
+
+ @Override
+ public void onException(AMQPFederationConsumer context, Exception error) {
+ logger.trace("Stop of federation consumer {} failed, closing consumer: ", context, error);
+ handleFederationConsumerStopped(consumer.getQueue(), entry, false);
+ }
+ });
+ }
+ }
+ }
+
+ @Override
+ public synchronized void afterRemoveBinding(Binding binding, Transaction tx, boolean deleteData) throws ActiveMQException {
+ if (binding instanceof QueueBinding) {
+ final QueueBinding queueBinding = (QueueBinding) binding;
+ final String queueName = queueBinding.getQueue().getName().toString();
+
+ demandTracking.values().removeIf((entry) -> {
+ if (entry.getConsumerInfo().getQueueName().equals(queueName) && entry.removeAllDemand().hasConsumer()) {
+ logger.trace("Federated queue {} was removed, closing federation consumer", queueName);
+
+ // Demand is gone because the Queue binding is gone and any in-flight messages
+ // can be allowed to be released back to the remote as they will not be processed.
+ // We removed the consumer information from demand tracking to prevent build up
+ // of data for entries that may never return and to prevent interference from the
+ // next set of events which will be the close of all local consumers for this now
+ // removed Queue.
+ tryCloseFederationConsumer(entry.clearConsumer());
+
+ return true;
+ } else {
+ return false;
+ }
+ });
+ }
+ }
+
+ private synchronized void handleFederationConsumerStopped(Queue queue, AMQPFederationQueueEntry entry, boolean didStop) {
+ final AMQPFederationConsumer federationConsuner = entry.getConsumer();
+
+ // Remote close or local queue remove could have beaten us here and already cleaned up the consumer.
+ if (federationConsuner != null) {
+ // If the consumer has no demand or it didn't stop in time or some other error occurred we
+ // assume the worst and close it here, the follow on code will recreate or cleanup as needed.
+ if (!didStop || !entry.hasDemand()) {
+ tryCloseFederationConsumer(entry.clearConsumer());
+ }
+
+ // Demand may have returned while the consumer was stopping in which case
+ // we either restart an existing stopped consumer or recreate if the stop
+ // timed out and we closed it above. If there's still no demand then we
+ // should remove it from demand tracking to reduce resource consumption.
+ if (started && entry.hasDemand()) {
+ tryRestartFederationConsumerForQueue(entry, queue);
+ } else {
+ demandTracking.remove(entry.getConsumerInfo());
+ }
+ }
+ }
+
+ @Override
+ protected void scanAllBindings() {
+ server.getPostOffice()
+ .getAllBindings()
+ .filter(b -> b instanceof QueueBinding)
+ .map(b -> (QueueBinding) b)
+ .forEach(b -> checkQueueForMatch(b.getQueue()));
+ }
+
+ protected void checkQueueForMatch(Queue queue) {
+ queue.getConsumers()
+ .stream()
+ .filter(consumer -> consumer instanceof ServerConsumer)
+ .map(c -> (ServerConsumer) c)
+ .forEach(this::reactIfConsumerMatchesPolicy);
+ }
+
+ protected void reactIfConsumerMatchesPolicy(ServerConsumer consumer) {
+ final String queueName = consumer.getQueue().getName().toString();
+
+ if (testIfQueueMatchesPolicy(consumer.getQueueAddress().toString(), queueName)) {
+ // We should ignore federation consumers from remote peers but configuration does allow
+ // these to be federated again for some very specific use cases so we check before then
+ // moving onto any server plugin checks kick in.
+ if (federationConsumerMatcher.test(consumer)) {
+ return;
+ }
+
+ logger.trace("Federation Policy matched on consumer for binding: {}", consumer.getBinding());
+
+ final AMQPFederationQueueEntry entry;
+ final FederationConsumerInfo consumerInfo = createConsumerInfo(consumer);
+
+ // Check for existing consumer add demand from a additional local consumer to ensure
+ // the remote consumer remains active until all local demand is withdrawn.
+ if (demandTracking.containsKey(consumerInfo)) {
+ logger.trace("Federation Queue Policy manager found existing demand for queue: {}, adding demand", queueName);
+ entry = demandTracking.get(consumerInfo);
+ } else {
+ demandTracking.put(consumerInfo, entry = new AMQPFederationQueueEntry(consumerInfo));
+ }
+
+ // Demand passed all binding plugin blocking checks so we track it, plugin can still
+ // stop federation of the queue based on some external criteria but once it does
+ // (if ever) allow it we will have tracked all allowed demand.
+ entry.addDemand(consumer);
+
+ // This will create a new consumer only if there isn't one currently assigned to the entry
+ // and any configured federation plugins don't block it from doing so.
+ tryCreateFederationConsumerForQueue(entry, consumer.getQueue());
+ }
+ }
+
+ private void tryCreateFederationConsumerForQueue(AMQPFederationQueueEntry queueEntry, Queue queue) {
+ if (queueEntry.hasDemand()) {
+ if (!queueEntry.hasConsumer() && !isPluginBlockingFederationConsumerCreate(queue)) {
+ logger.trace("Federation Queue Policy manager creating remote consumer for queue: {}", queueEntry.getQueueName());
+
+ signalPluginBeforeCreateFederationConsumer(queueEntry.getConsumerInfo());
+
+ final AMQPFederationConsumer queueConsumer = createFederationConsumer(queueEntry.getConsumerInfo());
+
+ // Handle remote close with remove of consumer which means that future demand will
+ // attempt to create a new consumer for that demand. Ensure that thread safety is
+ // accounted for here as the notification can be asynchronous.
+ queueConsumer.setRemoteClosedHandler((closedConsumer) -> {
+ synchronized (AMQPFederationQueuePolicyManager.this) {
+ try {
+ final AMQPFederationQueueEntry tracked = demandTracking.get(closedConsumer.getConsumerInfo());
+
+ if (tracked != null) {
+ tracked.clearConsumer();
+ }
+ } finally {
+ tryCloseFederationConsumer(closedConsumer);
+ }
+ }
+ });
+
+ queueEntry.setConsumer(queueConsumer);
+
+ // Now that we are tracking it we can initialize it which will start it once
+ // the link has fully attached.
+ queueConsumer.initialize();
+
+ signalPluginAfterCreateFederationConsumer(queueConsumer);
+ }
+ }
+ }
+
+ private void tryRestartFederationConsumerForQueue(AMQPFederationQueueEntry entry, Queue queue) {
+ // There might be a consumer that was previously stopped due to demand having been
+ // removed in which case we can attempt to recover it with a simple restart but if
+ // that fails ensure the old consumer is closed and then attempt to recreate as we
+ // know there is demand currently.
+ if (entry.hasConsumer()) {
+ final AMQPFederationConsumer federationConsuner = entry.getConsumer();
+
+ try {
+ federationConsuner.startAsync(new AMQPFederationAsyncCompletion<AMQPFederationConsumer>() {
+
+ @Override
+ public void onComplete(AMQPFederationConsumer context) {
+ logger.trace("Restarted federation consumer after new demand added." );
+ }
+
+ @Override
+ public void onException(AMQPFederationConsumer context, Exception error) {
+ if (error instanceof IllegalStateException) {
+ // The receiver might be stopping or it could be closed, either of which
+ // was initiated from this manager so we can ignore and let those complete.
+ return;
+ } else {
+ // This is unexpected and our reaction is to close the consumer since we
+ // have no idea what its state is now. Later new demand or remote events
+ // will trigger a new consumer to get added.
+ logger.trace("Start of federation consumer {} threw unexpected error, closing consumer: ", context, error);
+ tryCloseFederationConsumer(entry.clearConsumer());
+ }
+ }
+ });
+ } catch (Exception ex) {
+ // The consumer might have been remotely closed, we can't be certain but since we
+ // are responding to demand having been added we will close it and clear the entry
+ // so that the follow on code can try and create a new one.
+ logger.trace("Caught error on attempted restart of existing federation consumer", ex);
+ tryCloseFederationConsumer(entry.clearConsumer());
+ tryCreateFederationConsumerForQueue(entry, queue);
+ }
+ } else {
+ tryCreateFederationConsumerForQueue(entry, queue);
+ }
+ }
+
+ /**
+ * Checks if the remote queue added falls within the set of queues that match the
+ * configured queue policy and if so scans for local demand on that queue to see
+ * if a new attempt to federate the queue is needed.
+ *
+ * @param addressName
+ * The address that was added on the remote.
+ * @param queueName
+ * The queue that was added on the remote.
+ *
+ * @throws Exception if an error occurs while processing the queue added event.
+ */
+ public synchronized void afterRemoteQueueAdded(String addressName, String queueName) throws Exception {
+ // We ignore the remote address as locally the policy can be a wild card match and we can
+ // try to federate based on the Queue only, if the remote rejects the federation consumer
+ // binding again the request will once more be recorded and we will get another event if
+ // the queue were recreated such that a match could be made. We retain all the current
+ // demand and don't need to re-check the server state before trying to create the
+ // remote queue consumer.
+ if (started && testIfQueueMatchesPolicy(queueName)) {
+ final Queue queue = server.locateQueue(queueName);
+
+ if (queue != null) {
+ demandTracking.forEach((k, v) -> {
+ if (k.getQueueName().equals(queueName)) {
+ tryCreateFederationConsumerForQueue(v, queue);
+ }
+ });
+ }
+ }
+ }
+
+ /**
+ * Performs the test against the configured queue policy to check if the target
+ * queue and its associated address is a match or not. A subclass can override
+ * this method and provide its own match tests in combination with the configured
+ * matching policy.
+ *
+ * @param address
+ * The address that is being tested for a policy match.
+ * @param queueName
+ * The name of the queue that is being tested for a policy match.
+ *
+ * @return <code>true</code> if the address given is a match against the policy.
+ */
+ private boolean testIfQueueMatchesPolicy(String address, String queueName) {
+ return policy.test(address, queueName);
+ }
+
+ /**
+ * Performs the test against the configured queue policy to check if the target
+ * queue minus its associated address is a match or not. A subclass can override
+ * this method and provide its own match tests in combination with the configured
+ * matching policy.
+ *
+ * @param queueName
+ * The name of the queue that is being tested for a policy match.
+ *
+ * @return <code>true</code> if the address given is a match against the policy.
+ */
+ private boolean testIfQueueMatchesPolicy(String queueName) {
+ return policy.testQueue(queueName);
+ }
+
+ /**
+ * Create a new {@link FederationConsumerInfo} based on the given {@link ServerConsumer}
+ * and the configured {@link FederationReceiveFromQueuePolicy}. A subclass must override this
+ * method to return a consumer information object with additional data used be that implementation.
+ *
+ * @param consumer
+ * The {@link ServerConsumer} to use as a basis for the consumer information object.
+ *
+ * @return a new {@link FederationConsumerInfo} instance based on the server consumer
+ */
+ private FederationConsumerInfo createConsumerInfo(ServerConsumer consumer) {
final Queue queue = consumer.getQueue();
final String queueName = queue.getName().toString();
final String address = queue.getAddress().toString();
@@ -94,7 +407,7 @@ public class AMQPFederationQueuePolicyManager extends FederationQueuePolicyManag
final String filterString =
selectFilter(queue.getFilter(), configuration.isIgnoreSubscriptionFilters() ? null : consumer.getFilter());
- return new FederationGenericConsumerInfo(Role.QUEUE_CONSUMER,
+ return new AMQPFederationGenericConsumerInfo(Role.QUEUE_CONSUMER,
address,
queueName,
queue.getRoutingType(),
@@ -104,7 +417,7 @@ public class AMQPFederationQueuePolicyManager extends FederationQueuePolicyManag
}
@Override
- protected FederationConsumerInternal createFederationConsumer(FederationConsumerInfo consumerInfo) {
+ protected AMQPFederationConsumer createFederationConsumer(FederationConsumerInfo consumerInfo) {
Objects.requireNonNull(consumerInfo, "Federation Queue consumer information object was null");
if (logger.isTraceEnabled()) {
@@ -116,75 +429,41 @@ public class AMQPFederationQueuePolicyManager extends FederationQueuePolicyManag
return new AMQPFederationQueueConsumer(this, configuration, session, consumerInfo, messageObserver);
}
- @Override
- protected void signalBeforeCreateFederationConsumer(FederationConsumerInfo info) {
- try {
- server.callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- ((ActiveMQServerAMQPFederationPlugin) plugin).beforeCreateFederationConsumer(info);
- }
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("beforeCreateFederationConsumer", t);
- }
- }
-
- @Override
- protected void signalAfterCreateFederationConsumer(FederationConsumer consumer) {
- try {
- server.callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- ((ActiveMQServerAMQPFederationPlugin) plugin).afterCreateFederationConsumer(consumer);
- }
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("afterCreateFederationConsumer", t);
- }
- }
+ /**
+ * Creates a {@link Predicate} that should return true if the given consumer is a federation
+ * created consumer which should not be further federated.
+ *
+ * @param server
+ * The server instance for use in creating the filtering {@link Predicate}.
+ * @param policy
+ * The configured Queue matching policy that can provide additional match criteria.
+ *
+ * @return a {@link Predicate} that will return true if the consumer should be filtered.
+ *
+ * @throws ActiveMQException if an error occurs while creating the new consumer filter.
+ */
+ private Predicate<ServerConsumer> createFederationConsumerMatcher(ActiveMQServer server, FederationReceiveFromQueuePolicy policy) throws ActiveMQException {
+ if (policy.isIncludeFederated()) {
+ return (consumer) -> false; // Configuration says to federate these
+ } else {
+ // This filter matches on the same criteria as the original Core client based
+ // Federation code which allows this implementation to see those consumers as
+ // well as its own which in this methods implementation must also use this same
+ // mechanism to mark federation resources.
- @Override
- protected void signalBeforeCloseFederationConsumer(FederationConsumer consumer) {
- try {
- server.callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- ((ActiveMQServerAMQPFederationPlugin) plugin).beforeCloseFederationConsumer(consumer);
- }
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("beforeCloseFederationConsumer", t);
- }
- }
+ final Filter metaDataMatcher =
+ FilterImpl.createFilter("\"" + FEDERATION_NAME + "\" IS NOT NULL");
- @Override
- protected void signalAfterCloseFederationConsumer(FederationConsumer consumer) {
- try {
- server.callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- ((ActiveMQServerAMQPFederationPlugin) plugin).afterCloseFederationConsumer(consumer);
- }
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("afterCloseFederationConsumer", t);
- }
- }
+ return (consumer) -> {
+ final ServerSession serverSession = server.getSessionByID(consumer.getSessionID());
- @Override
- protected final boolean isPluginBlockingFederationConsumerCreate(Queue queue) {
- final AtomicBoolean canCreate = new AtomicBoolean(true);
-
- try {
- server.callBrokerAMQPFederationPlugins((plugin) -> {
- if (plugin instanceof ActiveMQServerAMQPFederationPlugin) {
- if (canCreate.get()) {
- canCreate.set(((ActiveMQServerAMQPFederationPlugin) plugin).shouldCreateFederationConsumerForQueue(queue));
- }
+ if (serverSession != null && serverSession.getMetaData() != null) {
+ return metaDataMatcher.match(serverSession.getMetaData());
+ } else {
+ return false;
}
- });
- } catch (ActiveMQException t) {
- ActiveMQServerLogger.LOGGER.federationPluginExecutionError("shouldCreateFederationConsumerForQueue", t);
+ };
}
-
- return !canCreate.get();
}
private static String selectFilter(Filter queueFilter, Filter consumerFilter) {
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/mirror/AMQPMirrorControllerTarget.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/mirror/AMQPMirrorControllerTarget.java
index 27177f6ab3..683e226cc0 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/mirror/AMQPMirrorControllerTarget.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/mirror/AMQPMirrorControllerTarget.java
@@ -256,7 +256,7 @@ public class AMQPMirrorControllerTarget extends ProtonAbstractReceiver implement
}
@Override
- public void flow() {
+ protected void doCreditTopUpRun() {
creditRunnable.run();
}
@@ -352,10 +352,10 @@ public class AMQPMirrorControllerTarget extends ProtonAbstractReceiver implement
// Match the settlement mode of the remote instead of relying on the default of MIXED.
receiver.setSenderSettleMode(receiver.getRemoteSenderSettleMode());
- // We don't currently support SECOND so enforce that the answer is anlways FIRST
+ // We don't currently support SECOND so enforce that the answer is always FIRST
receiver.setReceiverSettleMode(ReceiverSettleMode.FIRST);
- flow();
+ topUpCreditIfNeeded();
}
@Override
@@ -528,7 +528,7 @@ public class AMQPMirrorControllerTarget extends ProtonAbstractReceiver implement
if (duplicateIDCache.contains(duplicateIDBytes)) {
message.usageDown(); // large messages would be removed here
- flow();
+ topUpCreditIfNeeded();
return false;
}
@@ -557,7 +557,7 @@ public class AMQPMirrorControllerTarget extends ProtonAbstractReceiver implement
// We use this as part of a transaction because of the duplicate detection cache that needs to be done atomically
transaction.commit();
server.getStorageManager().afterCompleteOperations(messageCompletionAck, OperationConsistencyLevel.FULL);
- flow();
+ topUpCreditIfNeeded();
// return true here will instruct the caller to ignore any references to messageCompletionAck
return true;
@@ -590,4 +590,9 @@ public class AMQPMirrorControllerTarget extends ProtonAbstractReceiver implement
// Do nothing
}
+ @Override
+ protected SimpleString getAddressInUse() {
+ // We are not using server session level flow control here so this is unused.
+ return null;
+ }
}
\ No newline at end of file
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationAddressPolicyManager.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationAddressPolicyManager.java
deleted file mode 100644
index 55eb90fb83..0000000000
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationAddressPolicyManager.java
+++ /dev/null
@@ -1,549 +0,0 @@
-/*
- * 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.artemis.protocol.amqp.federation.internal;
-
-import java.lang.invoke.MethodHandles;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
-import org.apache.activemq.artemis.api.core.ActiveMQException;
-import org.apache.activemq.artemis.api.core.RoutingType;
-import org.apache.activemq.artemis.api.core.SimpleString;
-import org.apache.activemq.artemis.core.postoffice.Binding;
-import org.apache.activemq.artemis.core.postoffice.QueueBinding;
-import org.apache.activemq.artemis.core.postoffice.impl.DivertBinding;
-import org.apache.activemq.artemis.core.server.ActiveMQServerLogger;
-import org.apache.activemq.artemis.core.server.Divert;
-import org.apache.activemq.artemis.core.server.Queue;
-import org.apache.activemq.artemis.core.server.impl.AddressInfo;
-import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerAddressPlugin;
-import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerBindingPlugin;
-import org.apache.activemq.artemis.core.transaction.Transaction;
-import org.apache.activemq.artemis.protocol.amqp.federation.Federation;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumer;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumerInfo;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationReceiveFromAddressPolicy;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Manager for a federation which has address federation configuration which requires
- * monitoring broker addresses and diverts for demand and creating a consumer on the
- * remote side to federate messages back to this peer.
- *
- * Address federation replicates messages from the remote broker's address to an address
- * on this broker but only when there is local demand on that address. If there is no
- * local demand then federation if already established is halted. The manager creates
- * a remote consumer on the federated address without any filtering other than that
- * required for internal functionality in order to allow for a single remote consumer
- * which can federate all messages to the local side where the existing queues can apply
- * any filtering they have in place.
- */
-public abstract class FederationAddressPolicyManager extends FederationPolicyManager implements ActiveMQServerBindingPlugin, ActiveMQServerAddressPlugin {
-
- private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-
- protected final FederationReceiveFromAddressPolicy policy;
- protected final Map<String, FederationAddressEntry> demandTracking = new HashMap<>();
- protected final Map<DivertBinding, Set<QueueBinding>> divertsTracking = new HashMap<>();
-
- public FederationAddressPolicyManager(FederationInternal federation, FederationReceiveFromAddressPolicy addressPolicy) throws ActiveMQException {
- super(federation);
-
- Objects.requireNonNull(addressPolicy, "The Address match policy cannot be null");
-
- this.policy = addressPolicy;
- }
-
- /**
- * @return the receive from address policy that backs the address policy manager.
- */
- @Override
- public FederationReceiveFromAddressPolicy getPolicy() {
- return policy;
- }
-
- /**
- * Start the address policy manager which will initiate a scan of all broker divert
- * bindings and create and matching remote receivers. Start on a policy manager
- * should only be called after its parent {@link Federation} is started and the
- * federation connection has been established.
- */
- public synchronized void start() {
- if (!federation.isStarted()) {
- throw new IllegalStateException("Cannot start a federation policy manager when the federation is stopped.");
- }
-
- if (!started) {
- started = true;
- handlePolicyManagerStarted(policy);
- server.registerBrokerPlugin(this);
- scanAllBindings(); // Create remote consumers for existing addresses with demand.
- }
- }
-
- /**
- * Stops the address policy manager which will close any open remote receivers that are
- * active for local queue demand. Stop should generally be called whenever the parent
- * {@link Federation} loses its connection to the remote.
- */
- public synchronized void stop() {
- if (started) {
- started = false;
- server.unRegisterBrokerPlugin(this);
- demandTracking.forEach((k, v) -> {
- if (v.hasConsumer()) {
- v.getConsumer().close();
- }
- });
- demandTracking.clear();
- divertsTracking.clear();
- }
- }
-
- @Override
- public synchronized void afterRemoveAddress(SimpleString address, AddressInfo addressInfo) throws ActiveMQException {
- if (started) {
- final FederationAddressEntry entry = demandTracking.remove(address.toString());
-
- if (entry != null && entry.hasConsumer()) {
- entry.getConsumer().close();
- }
- }
- }
-
- @Override
- public synchronized void afterRemoveBinding(Binding binding, Transaction tx, boolean deleteData) throws ActiveMQException {
- if (started) {
- if (binding instanceof QueueBinding) {
- final FederationAddressEntry entry = demandTracking.get(binding.getAddress().toString());
-
- if (entry != null) {
- // This is QueueBinding that was mapped to a federated address so we can directly remove
- // demand from the federation consumer and close it if demand is now gone.
- tryRemoveDemandOnAddress(entry, binding);
- } else if (policy.isEnableDivertBindings()) {
- // See if there is any matching diverts that are forwarding to an address where this QueueBinding
- // is bound and remove the mapping for any matches, diverts can have a composite set of address
- // forwards so each divert must be checked in turn to see if it contains the address the removed
- // binding was bound to.
- divertsTracking.entrySet().forEach(divertEntry -> {
- final String sourceAddress = divertEntry.getKey().getAddress().toString();
- final SimpleString forwardAddress = divertEntry.getKey().getDivert().getForwardAddress();
-
- if (isAddressInDivertForwards(binding.getAddress(), forwardAddress)) {
- // Try and remove the queue binding from the set of registered bindings
- // for the divert and if that removes all mapped bindings then we can
- // remove the divert from the federated address entry and check if that
- // removed all local demand which means we can close the consumer.
- divertEntry.getValue().remove(binding);
-
- if (divertEntry.getValue().isEmpty()) {
- tryRemoveDemandOnAddress(demandTracking.get(sourceAddress), divertEntry.getKey());
- }
- }
- });
- }
- } else if (policy.isEnableDivertBindings() && binding instanceof DivertBinding) {
- final DivertBinding divert = (DivertBinding) binding;
-
- if (divertsTracking.remove(divert) != null) {
- // The divert binding is treated as one unit of demand on a federated address and
- // when the divert is removed that unit of demand is removed regardless of existing
- // bindings still remaining on the divert forwards. If the divert demand was the
- // only thing keeping the federated address consumer open this will result in it
- // being closed.
- try {
- tryRemoveDemandOnAddress(demandTracking.get(divert.getAddress().toString()), divert);
- } catch (Exception e) {
- ActiveMQServerLogger.LOGGER.federationBindingsLookupError(divert.getDivert().getForwardAddress(), e);
- }
- }
- }
- }
- }
-
- protected final void tryRemoveDemandOnAddress(FederationAddressEntry entry, Binding binding) {
- if (entry != null) {
- entry.removeDemand(binding);
-
- logger.trace("Reducing demand on federated address {}, remaining demand? {}", entry.getAddress(), entry.hasDemand());
-
- if (!entry.hasDemand() && entry.hasConsumer()) {
- final FederationConsumerInternal federationConsuner = entry.getConsumer();
-
- try {
- signalBeforeCloseFederationConsumer(federationConsuner);
- federationConsuner.close();
- signalAfterCloseFederationConsumer(federationConsuner);
- } finally {
- demandTracking.remove(entry.getAddress());
- }
- }
- }
- }
-
- /**
- * Scans all bindings and push them through the normal bindings checks that
- * would be done on an add. We filter here based on whether diverts are enabled
- * just to reduce the result set but the check call should also filter as
- * during normal operations divert bindings could be added.
- */
- protected final void scanAllBindings() {
- server.getPostOffice()
- .getAllBindings()
- .filter(binding -> binding instanceof QueueBinding || (policy.isEnableDivertBindings() && binding instanceof DivertBinding))
- .forEach(binding -> afterAddBinding(binding));
- }
-
- @Override
- public synchronized void afterAddAddress(AddressInfo addressInfo, boolean reload) {
- if (started && policy.isEnableDivertBindings() && policy.test(addressInfo)) {
- try {
- // A Divert can exist in configuration prior to the address having been auto created
- // etc so upon address add this check needs to be run to capture addresses that now
- // match the divert.
- server.getPostOffice()
- .getDirectBindings(addressInfo.getName())
- .stream()
- .filter(binding -> binding instanceof DivertBinding)
- .forEach(this::checkBindingForMatch);
- } catch (Exception e) {
- ActiveMQServerLogger.LOGGER.federationBindingsLookupError(addressInfo.getName(), e);
- }
- }
- }
-
- @Override
- public synchronized void afterAddBinding(Binding binding) {
- if (started) {
- checkBindingForMatch(binding);
- }
- }
-
- /**
- * Called under lock this method should check if the given {@link Binding} matches the
- * configured address federation policy and federate the address if so. The incoming
- * {@link Binding} can be either a {@link QueueBinding} or a {@link DivertBinding} so
- * the code should check both.
- *
- * @param binding
- * The binding that should be checked against the federated address policy,
- */
- protected final void checkBindingForMatch(Binding binding) {
- if (binding instanceof QueueBinding) {
- final QueueBinding queueBinding = (QueueBinding) binding;
- final AddressInfo addressInfo = server.getPostOffice().getAddressInfo(binding.getAddress());
-
- if (testIfAddressMatchesPolicy(addressInfo)) {
- // A plugin can block address federation for a given queue and if another queue
- // binding does trigger address federation we don't want to track the rejected
- // queue as demand so we always run this check before trying to create the address
- // consumer.
- if (isPluginBlockingFederationConsumerCreate(queueBinding.getQueue())) {
- return;
- }
-
- createOrUpdateFederatedAddressConsumerForBinding(addressInfo, queueBinding);
- } else {
- reactIfQueueBindingMatchesAnyDivertTarget(queueBinding);
- }
- } else if (binding instanceof DivertBinding) {
- reactIfAnyQueueBindingMatchesDivertTarget((DivertBinding) binding);
- }
- }
-
- protected final void reactIfAnyQueueBindingMatchesDivertTarget(DivertBinding divertBinding) {
- if (!policy.isEnableDivertBindings()) {
- return;
- }
-
- final AddressInfo addressInfo = server.getPostOffice().getAddressInfo(divertBinding.getAddress());
-
- if (!testIfAddressMatchesPolicy(addressInfo)) {
- return;
- }
-
- // We only need to check if we've never seen the divert before, afterwards we will
- // be checking it any time a new QueueBinding is added instead.
- if (divertsTracking.get(divertBinding) == null) {
- final Set<QueueBinding> matchingQueues = new HashSet<>();
- divertsTracking.put(divertBinding, matchingQueues);
-
- // We must account for the composite divert case by splitting the address and
- // getting the bindings on each one.
- final SimpleString forwardAddress = divertBinding.getDivert().getForwardAddress();
- final SimpleString[] forwardAddresses = forwardAddress.split(',');
-
- try {
- for (SimpleString forward : forwardAddresses) {
- server.getPostOffice().getBindingsForAddress(forward).getBindings()
- .stream()
- .filter(b -> b instanceof QueueBinding)
- .map(b -> (QueueBinding) b)
- .forEach(queueBinding -> {
- // The plugin can block the demand totally here either based on the divert itself
- // or the queue that's attached to the divert.
- if (isPluginBlockingFederationConsumerCreate(divertBinding.getDivert(), queueBinding.getQueue())) {
- return;
- }
-
- // The plugin can block the demand selectively based on a single queue attached to
- // the divert target(s).
- if (isPluginBlockingFederationConsumerCreate(queueBinding.getQueue())) {
- return;
- }
-
- matchingQueues.add(queueBinding);
-
- createOrUpdateFederatedAddressConsumerForBinding(addressInfo, divertBinding);
- });
- }
- } catch (Exception e) {
- ActiveMQServerLogger.LOGGER.federationBindingsLookupError(forwardAddress, e);
- }
- }
- }
-
- protected final void reactIfQueueBindingMatchesAnyDivertTarget(QueueBinding queueBinding) {
- if (!policy.isEnableDivertBindings()) {
- return;
- }
-
- final SimpleString queueAddress = queueBinding.getAddress();
-
- divertsTracking.entrySet().forEach((e) -> {
- final SimpleString forwardAddress = e.getKey().getDivert().getForwardAddress();
- final DivertBinding divertBinding = e.getKey();
-
- // Check matched diverts to see if the QueueBinding address matches the address or
- // addresses (composite diverts) of the Divert and if so then we can check if we need
- // to create demand on the source address on the remote if we haven't done so already.
-
- if (!e.getValue().contains(queueBinding) && isAddressInDivertForwards(queueAddress, forwardAddress)) {
- // The plugin can block the demand totally here either based on the divert itself
- // or the queue that's attached to the divert.
- if (isPluginBlockingFederationConsumerCreate(divertBinding.getDivert(), queueBinding.getQueue())) {
- return;
- }
-
- // The plugin can block the demand selectively based on a single queue attached to
- // the divert target(s).
- if (isPluginBlockingFederationConsumerCreate(queueBinding.getQueue())) {
- return;
- }
-
- // Each divert that forwards to the address the queue is bound to we add demand
- // in the diverts tracker.
- e.getValue().add(queueBinding);
-
- final AddressInfo addressInfo = server.getPostOffice().getAddressInfo(divertBinding.getAddress());
-
- createOrUpdateFederatedAddressConsumerForBinding(addressInfo, divertBinding);
- }
- });
- }
-
- private static boolean isAddressInDivertForwards(final SimpleString targetAddress, final SimpleString forwardAddress) {
- final SimpleString[] forwardAddresses = forwardAddress.split(',');
-
- for (SimpleString forward : forwardAddresses) {
- if (targetAddress.equals(forward)) {
- return true;
- }
- }
-
- return false;
- }
-
- protected final void createOrUpdateFederatedAddressConsumerForBinding(AddressInfo addressInfo, Binding binding) {
- logger.trace("Federation Address Policy matched on for demand on address: {} : binding: {}", addressInfo, binding);
-
- final String addressName = addressInfo.getName().toString();
- final FederationAddressEntry entry;
-
- // Check for existing consumer add demand from a additional local consumer to ensure
- // the remote consumer remains active until all local demand is withdrawn.
- if (demandTracking.containsKey(addressName)) {
- entry = demandTracking.get(addressName);
- } else {
- entry = new FederationAddressEntry(addressInfo);
- demandTracking.put(addressName, entry);
- }
-
- // Demand passed all binding plugin blocking checks so we track it, plugin can still
- // stop federation of the address based on some external criteria but once it does
- // (if ever) allow it we will have tracked all allowed demand.
- entry.addDemand(binding);
-
- tryCreateFederationConsumerForAddress(entry);
- }
-
- private void tryCreateFederationConsumerForAddress(FederationAddressEntry addressEntry) {
- final AddressInfo addressInfo = addressEntry.getAddressInfo();
-
- if (addressEntry.hasDemand() && !addressEntry.hasConsumer() && !isPluginBlockingFederationConsumerCreate(addressInfo)) {
- logger.trace("Federation Address Policy manager creating remote consumer for address: {}", addressInfo);
-
- final FederationConsumerInfo consumerInfo = createConsumerInfo(addressInfo);
- final FederationConsumerInternal addressConsumer = createFederationConsumer(consumerInfo);
-
- signalBeforeCreateFederationConsumer(consumerInfo);
-
- // Handle remote close with remove of consumer which means that future demand will
- // attempt to create a new consumer for that demand. Ensure that thread safety is
- // accounted for here as the notification can be asynchronous.
- addressConsumer.setRemoteClosedHandler((closedConsumer) -> {
- synchronized (this) {
- try {
- final FederationAddressEntry tracked = demandTracking.get(closedConsumer.getConsumerInfo().getAddress());
-
- if (tracked != null) {
- tracked.clearConsumer();
- }
- } finally {
- closedConsumer.close();
- }
- }
- });
-
- addressEntry.setConsumer(addressConsumer);
-
- addressConsumer.start();
-
- signalAfterCreateFederationConsumer(addressConsumer);
- }
- }
-
- /**
- * Checks if the remote address added falls within the set of addresses that match the
- * configured address policy and if so scans for local demand on that address to see
- * if a new attempt to federate the address is needed.
- *
- * @param addressName
- * The address that was added on the remote.
- *
- * @throws Exception if an error occurs while processing the address added event.
- */
- public synchronized void afterRemoteAddressAdded(String addressName) throws Exception {
- // Assume that the remote address that matched a previous federation attempt is MULTICAST
- // so that we retry if current local state matches the policy and if it isn't we will once
- // again record the federation attempt with the remote and be updated if the remote removes
- // and adds the address again (hopefully with the correct routing type). We retrain all the
- // current demand and don't need to re-check the server state before trying to create the
- // remote address consumer.
- if (started && testIfAddressMatchesPolicy(addressName, RoutingType.MULTICAST) && demandTracking.containsKey(addressName)) {
- tryCreateFederationConsumerForAddress(demandTracking.get(addressName));
- }
- }
-
- /**
- * Performs the test against the configured address policy to check if the target
- * address is a match or not. A subclass can override this method and provide its
- * own match tests in combination with the configured matching policy.
- *
- * @param addressInfo
- * The address that is being tested for a policy match.
- *
- * @return <code>true</code> if the address given is a match against the policy.
- */
- protected boolean testIfAddressMatchesPolicy(AddressInfo addressInfo) {
- return policy.test(addressInfo);
- }
-
- /**
- * Performs the test against the configured address policy to check if the target
- * address is a match or not. A subclass can override this method and provide its
- * own match tests in combination with the configured matching policy.
- *
- * @param address
- * The address that is being tested for a policy match.
- * @param type
- * The routing type of the address to test against the policy.
- *
- * @return <code>true</code> if the address given is a match against the policy.
- */
- protected boolean testIfAddressMatchesPolicy(String address, RoutingType type) {
- return policy.test(address, type);
- }
-
- /**
- * Create a new {@link FederationConsumerInfo} based on the given {@link AddressInfo}
- * and the configured {@link FederationReceiveFromAddressPolicy}. A subclass must override this
- * method to return a consumer information object with the data used be that implementation.
- *
- * @param address
- * The {@link AddressInfo} to use as a basis for the consumer information object.
- *
- * @return a new {@link FederationConsumerInfo} instance based on the given address.
- */
- protected abstract FederationConsumerInfo createConsumerInfo(AddressInfo address);
-
- /**
- * Creates a {@link FederationAddressEntry} instance that will be used to store an instance of
- * an {@link FederationConsumer} along with other state data needed to manage a federation consumer
- * instance lifetime. A subclass can override this method to return a more customized entry type with
- * additional state data.
- *
- * @param addressInfo
- * The address information that the created entry is meant to track demand for.
- *
- * @return a new {@link FederationAddressEntry} that tracks demand on an address.
- */
- protected FederationAddressEntry createConsumerEntry(AddressInfo addressInfo) {
- return new FederationAddressEntry(addressInfo);
- }
-
- /**
- * Query all registered plugins for this federation instance to determine if any wish to
- * prevent a federation consumer from being created for the given resource.
- *
- * @param address
- * The address on which the manager is intending to create a remote consumer for.
- *
- * @return true if any registered plugin signaled that creation should be suppressed.
- */
- protected abstract boolean isPluginBlockingFederationConsumerCreate(AddressInfo address);
-
- /**
- * Query all registered plugins for this federation instance to determine if any wish to
- * prevent a federation consumer from being created for the given Queue.
- *
- * @param divert
- * The {@link Divert} that triggered the manager to attempt to create a remote consumer.
- * @param queue
- * The {@link Queue} that triggered the manager to attempt to create a remote consumer.
- *
- * @return true if any registered plugin signaled that creation should be suppressed.
- */
- protected abstract boolean isPluginBlockingFederationConsumerCreate(Divert divert, Queue queue);
-
- /**
- * Query all registered plugins for this federation instance to determine if any wish to
- * prevent a federation consumer from being created for the given Queue.
- *
- * @param queue
- * The {@link Queue} that triggered the manager to attempt to create a remote consumer.
- *
- * @return true if any registered plugin signaled that creation should be suppressed.
- */
- protected abstract boolean isPluginBlockingFederationConsumerCreate(Queue queue);
-
-}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationConsumerInternal.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationConsumerInternal.java
deleted file mode 100644
index b9240c62ea..0000000000
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationConsumerInternal.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * 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.artemis.protocol.amqp.federation.internal;
-
-import java.util.function.Consumer;
-
-import org.apache.activemq.artemis.protocol.amqp.federation.Federation;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumer;
-
-/**
- * Internal federated consumer API that is subject to change without notice.
- */
-public interface FederationConsumerInternal extends FederationConsumer {
-
- /**
- * Starts the consumer instance which includes creating the remote resources
- * and performing any internal initialization needed to fully establish the
- * consumer instance. This call should not block and any errors encountered
- * on creation of the backing consumer resources should utilize the error
- * handling mechanisms of this {@link Federation} instance.
- */
- void start();
-
- /**
- * Close the federation consumer instance and cleans up its resources. This method
- * should not block and the actual resource shutdown work should occur asynchronously.
- */
- void close();
-
- /**
- * Provides and event point for notification of the consumer having been closed by
- * the remote.
- *
- * @param handler
- * The handler that will be invoked when the remote closes this consumer.
- *
- * @return this consumer instance.
- */
- FederationConsumerInternal setRemoteClosedHandler(Consumer<FederationConsumerInternal> handler);
-
-}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationPolicyManager.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationPolicyManager.java
deleted file mode 100644
index a015e2d960..0000000000
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationPolicyManager.java
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * 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.artemis.protocol.amqp.federation.internal;
-
-import java.util.Objects;
-
-import org.apache.activemq.artemis.api.core.ActiveMQException;
-import org.apache.activemq.artemis.core.server.ActiveMQServer;
-import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerBindingPlugin;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumer;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumerInfo;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationReceiveFromResourcePolicy;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationType;
-
-/**
- * Base Federation policy manager that declares some common APIs that address or queue policy
- * managers must provide implementations for.
- */
-public abstract class FederationPolicyManager implements ActiveMQServerBindingPlugin {
-
- protected final ActiveMQServer server;
- protected final FederationInternal federation;
-
- protected volatile boolean started;
-
- public FederationPolicyManager(FederationInternal federation) throws ActiveMQException {
- Objects.requireNonNull(federation, "The Federation instance cannot be null");
-
- this.federation = federation;
- this.server = federation.getServer();
- }
-
- /**
- * @return the immutable federation policy configuration that backs this manager.
- */
- public abstract FederationReceiveFromResourcePolicy getPolicy();
-
- /**
- * @return the federation type this policy manager implements.
- */
- public final FederationType getPolicyType() {
- return getPolicy().getPolicyType();
- }
-
- /**
- * @return the assigned name of the policy that is being managed.
- */
- public final String getPolicyName() {
- return getPolicy().getPolicyName();
- }
-
- /**
- * @return <code>true</code> if the policy is started at the time this method was called.
- */
- public final boolean isStarted() {
- return started;
- }
-
- /**
- * @return the {@link FederationInternal} instance that owns this policy manager.
- */
- public FederationInternal getFederation() {
- return federation;
- }
-
- /**
- * Called on start of the manager before any other actions are taken to allow the subclass time
- * to configure itself and prepare any needed state prior to starting management of federated
- * resources.
- *
- * @param policy
- * The policy configuration for this policy manager.
- */
- protected abstract void handlePolicyManagerStarted(FederationReceiveFromResourcePolicy policy);
-
- /**
- * Create a new {@link FederationConsumerInternal} instance using the consumer information
- * given. This is called when local demand for a matched resource requires a new consumer to
- * be created. This method by default will call the configured consumer factory function that
- * was provided when the manager was created, a subclass can override this to perform additional
- * actions for the create operation.
- *
- * @param consumerInfo
- * The {@link FederationConsumerInfo} that defines the consumer to be created.
- *
- * @return a new {@link FederationConsumerInternal} instance that will reside in this manager.
- */
- protected abstract FederationConsumerInternal createFederationConsumer(FederationConsumerInfo consumerInfo);
-
- /**
- * Signal any registered plugins for this federation instance that a remote consumer
- * is being created.
- *
- * @param info
- * The {@link FederationConsumerInfo} that describes the remote federation consumer
- */
- protected abstract void signalBeforeCreateFederationConsumer(FederationConsumerInfo info);
-
- /**
- * Signal any registered plugins for this federation instance that a remote consumer
- * has been created.
- *
- * @param consumer
- * The {@link FederationConsumerInfo} that describes the remote consumer
- */
- protected abstract void signalAfterCreateFederationConsumer(FederationConsumer consumer);
-
- /**
- * Signal any registered plugins for this federation instance that a remote consumer
- * is about to be closed.
- *
- * @param consumer
- * The {@link FederationConsumer} that that is about to be closed.
- */
- protected abstract void signalBeforeCloseFederationConsumer(FederationConsumer consumer);
-
- /**
- * Signal any registered plugins for this federation instance that a remote consumer
- * has now been closed.
- *
- * @param consumer
- * The {@link FederationConsumer} that that has been closed.
- */
- protected abstract void signalAfterCloseFederationConsumer(FederationConsumer consumer);
-
-}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationQueuePolicyManager.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationQueuePolicyManager.java
deleted file mode 100644
index 34d6cb6170..0000000000
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/federation/internal/FederationQueuePolicyManager.java
+++ /dev/null
@@ -1,389 +0,0 @@
-/*
- * 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.artemis.protocol.amqp.federation.internal;
-
-import static org.apache.activemq.artemis.protocol.amqp.federation.FederationConstants.FEDERATION_NAME;
-
-import java.lang.invoke.MethodHandles;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Objects;
-import java.util.function.Predicate;
-
-import org.apache.activemq.artemis.api.core.ActiveMQException;
-import org.apache.activemq.artemis.core.filter.Filter;
-import org.apache.activemq.artemis.core.filter.impl.FilterImpl;
-import org.apache.activemq.artemis.core.postoffice.Binding;
-import org.apache.activemq.artemis.core.postoffice.QueueBinding;
-import org.apache.activemq.artemis.core.server.ActiveMQServer;
-import org.apache.activemq.artemis.core.server.Queue;
-import org.apache.activemq.artemis.core.server.ServerConsumer;
-import org.apache.activemq.artemis.core.server.ServerSession;
-import org.apache.activemq.artemis.core.server.federation.Federation;
-import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerBindingPlugin;
-import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerConsumerPlugin;
-import org.apache.activemq.artemis.core.transaction.Transaction;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumer;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationConsumerInfo;
-import org.apache.activemq.artemis.protocol.amqp.federation.FederationReceiveFromQueuePolicy;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Manager for a federation which has queue federation configuration which requires
- * monitoring broker queues for demand and creating a consumer for on the remote side
- * to federate messages back to this peer.
- */
-public abstract class FederationQueuePolicyManager extends FederationPolicyManager implements ActiveMQServerConsumerPlugin, ActiveMQServerBindingPlugin {
-
- private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-
- protected final Predicate<ServerConsumer> federationConsumerMatcher;
- protected final FederationReceiveFromQueuePolicy policy;
- protected final Map<FederationConsumerInfo, FederationQueueEntry> demandTracking = new HashMap<>();
-
- public FederationQueuePolicyManager(FederationInternal federation, FederationReceiveFromQueuePolicy queuePolicy) throws ActiveMQException {
- super(federation);
-
- Objects.requireNonNull(queuePolicy, "The Queue match policy cannot be null");
-
- this.policy = queuePolicy;
- this.federationConsumerMatcher = createFederationConsumerMatcher(server, queuePolicy);
- }
-
- /**
- * @return the receive from queue policy that backs the queue policy manager.
- */
- @Override
- public FederationReceiveFromQueuePolicy getPolicy() {
- return policy;
- }
-
- /**
- * Start the queue policy manager which will initiate a scan of all broker queue
- * bindings and create and matching remote receivers. Start on a policy manager
- * should only be called after its parent {@link Federation} is started and the
- * federation connection has been established.
- */
- public synchronized void start() {
- if (!federation.isStarted()) {
- throw new IllegalStateException("Cannot start a federation policy manager when the federation is stopped.");
- }
-
- if (!started) {
- started = true;
- handlePolicyManagerStarted(policy);
- server.registerBrokerPlugin(this);
- scanAllQueueBindings(); // Create consumers for existing queue with demand.
- }
- }
-
- /**
- * Stops the queue policy manager which will close any open remote receivers that are
- * active for local queue demand. Stop should generally be called whenever the parent
- * {@link Federation} loses its connection to the remote.
- */
- public synchronized void stop() {
- if (started) {
- // Ensures that on shutdown of a federation broker connection we don't leak
- // broker plugin instances.
- server.unRegisterBrokerPlugin(this);
- started = false;
- demandTracking.forEach((k, v) -> {
- if (v.hasConsumer()) {
- v.getConsumer().close();
- }
- });
- demandTracking.clear();
- }
- }
-
- @Override
- public synchronized void afterCreateConsumer(ServerConsumer consumer) {
- if (started) {
- reactIfConsumerMatchesPolicy(consumer);
- }
- }
-
- @Override
- public synchronized void afterCloseConsumer(ServerConsumer consumer, boolean failed) {
- if (started) {
- final String queueName = consumer.getQueue().getName().toString();
- final FederationConsumerInfo consumerInfo = createConsumerInfo(consumer);
- final FederationQueueEntry entry = demandTracking.get(consumerInfo);
-
- if (entry == null) {
- return;
- }
-
- entry.removeDemand(consumer);
-
- logger.trace("Reducing demand on federated queue {}, remaining demand? {}", queueName, entry.hasDemand());
-
- if (!entry.hasDemand() && entry.hasConsumer()) {
- final FederationConsumerInternal federationConsuner = entry.getConsumer();
-
- try {
- signalBeforeCloseFederationConsumer(federationConsuner);
- federationConsuner.close();
- signalAfterCloseFederationConsumer(federationConsuner);
- } finally {
- demandTracking.remove(consumerInfo);
- }
- }
- }
- }
-
- @Override
- public synchronized void afterRemoveBinding(Binding binding, Transaction tx, boolean deleteData) throws ActiveMQException {
- if (binding instanceof QueueBinding) {
- final QueueBinding queueBinding = (QueueBinding) binding;
- final String queueName = queueBinding.getQueue().getName().toString();
-
- demandTracking.values().forEach((entry) -> {
- if (entry.getConsumerInfo().getQueueName().equals(queueName) && entry.hasConsumer()) {
- entry.getConsumer().close();
- }
- });
- }
- }
-
- protected final void scanAllQueueBindings() {
- server.getPostOffice()
- .getAllBindings()
- .filter(b -> b instanceof QueueBinding)
- .map(b -> (QueueBinding) b)
- .forEach(b -> checkQueueForMatch(b.getQueue()));
- }
-
- protected final void checkQueueForMatch(Queue queue) {
- queue.getConsumers()
- .stream()
- .filter(consumer -> consumer instanceof ServerConsumer)
- .map(c -> (ServerConsumer) c)
- .forEach(this::reactIfConsumerMatchesPolicy);
- }
-
- protected final void reactIfConsumerMatchesPolicy(ServerConsumer consumer) {
- final String queueName = consumer.getQueue().getName().toString();
-
- if (testIfQueueMatchesPolicy(consumer.getQueueAddress().toString(), queueName)) {
- // We should ignore federation consumers from remote peers but configuration does allow
- // these to be federated again for some very specific use cases so we check before then
- // moving onto any server plugin checks kick in.
- if (federationConsumerMatcher.test(consumer)) {
- return;
- }
-
- logger.trace("Federation Policy matched on consumer for binding: {}", consumer.getBinding());
-
- final FederationQueueEntry entry;
- final FederationConsumerInfo consumerInfo = createConsumerInfo(consumer);
-
- // Check for existing consumer add demand from a additional local consumer to ensure
- // the remote consumer remains active until all local demand is withdrawn.
- if (demandTracking.containsKey(consumerInfo)) {
- logger.trace("Federation Queue Policy manager found existing demand for queue: {}, adding demand", queueName);
- entry = demandTracking.get(consumerInfo);
- } else {
- demandTracking.put(consumerInfo, entry = createConsumerEntry(consumerInfo));
- }
-
- // Demand passed all binding plugin blocking checks so we track it, plugin can still
- // stop federation of the queue based on some external criteria but once it does
- // (if ever) allow it we will have tracked all allowed demand.
- entry.addDemand(consumer);
-
- tryCreateFederationConsumerForQueue(entry, consumer.getQueue());
- }
- }
-
- private void tryCreateFederationConsumerForQueue(FederationQueueEntry queueEntry, Queue queue) {
- if (queueEntry.hasDemand() && !queueEntry.hasConsumer() && !isPluginBlockingFederationConsumerCreate(queue)) {
- logger.trace("Federation Queue Policy manager creating remote consumer for queue: {}", queueEntry.getQueueName());
-
- signalBeforeCreateFederationConsumer(queueEntry.getConsumerInfo());
-
- final FederationConsumerInternal queueConsumer = createFederationConsumer(queueEntry.getConsumerInfo());
-
- // Handle remote close with remove of consumer which means that future demand will
- // attempt to create a new consumer for that demand. Ensure that thread safety is
- // accounted for here as the notification can be asynchronous.
- queueConsumer.setRemoteClosedHandler((closedConsumer) -> {
- synchronized (this) {
- try {
- final FederationQueueEntry tracked = demandTracking.get(closedConsumer.getConsumerInfo());
-
- if (tracked != null) {
- tracked.clearConsumer();
- }
- } finally {
- closedConsumer.close();
- }
- }
- });
-
- queueEntry.setConsumer(queueConsumer);
-
- // Now that we are tracking it we can start it
- queueConsumer.start();
-
- signalAfterCreateFederationConsumer(queueConsumer);
- }
- }
-
- /**
- * Checks if the remote queue added falls within the set of queues that match the
- * configured queue policy and if so scans for local demand on that queue to see
- * if a new attempt to federate the queue is needed.
- *
- * @param addressName
- * The address that was added on the remote.
- * @param queueName
- * The queue that was added on the remote.
- *
- * @throws Exception if an error occurs while processing the queue added event.
- */
- public synchronized void afterRemoteQueueAdded(String addressName, String queueName) throws Exception {
- // We ignore the remote address as locally the policy can be a wild card match and we can
- // try to federate based on the Queue only, if the remote rejects the federation consumer
- // binding again the request will once more be recorded and we will get another event if
- // the queue were recreated such that a match could be made. We retain all the current
- // demand and don't need to re-check the server state before trying to create the
- // remote queue consumer.
- if (started && testIfQueueMatchesPolicy(queueName)) {
- final Queue queue = server.locateQueue(queueName);
-
- if (queue != null) {
- demandTracking.forEach((k, v) -> {
- if (k.getQueueName().equals(queueName)) {
- tryCreateFederationConsumerForQueue(v, queue);
- }
- });
- }
- }
- }
-
- /**
- * Performs the test against the configured queue policy to check if the target
- * queue and its associated address is a match or not. A subclass can override
- * this method and provide its own match tests in combination with the configured
- * matching policy.
- *
- * @param address
- * The address that is being tested for a policy match.
- * @param queueName
- * The name of the queue that is being tested for a policy match.
- *
- * @return <code>true</code> if the address given is a match against the policy.
- */
- protected boolean testIfQueueMatchesPolicy(String address, String queueName) {
- return policy.test(address, queueName);
- }
-
- /**
- * Performs the test against the configured queue policy to check if the target
- * queue minus its associated address is a match or not. A subclass can override
- * this method and provide its own match tests in combination with the configured
- * matching policy.
- *
- * @param queueName
- * The name of the queue that is being tested for a policy match.
- *
- * @return <code>true</code> if the address given is a match against the policy.
- */
- protected boolean testIfQueueMatchesPolicy(String queueName) {
- return policy.testQueue(queueName);
- }
-
- /**
- * Create a new {@link FederationConsumerInfo} based on the given {@link ServerConsumer}
- * and the configured {@link FederationReceiveFromQueuePolicy}. A subclass must override this
- * method to return a consumer information object with additional data used be that implementation.
- *
- * @param consumer
- * The {@link ServerConsumer} to use as a basis for the consumer information object.
- *
- * @return a new {@link FederationConsumerInfo} instance based on the server consumer
- */
- protected abstract FederationConsumerInfo createConsumerInfo(ServerConsumer consumer);
-
- /**
- * Creates a {@link FederationQueueEntry} instance that will be used to store an instance of
- * a {@link FederationConsumer} along with other state data needed to manage a federation consumer
- * instance. A subclass can override this method to return a more customized entry type with additional
- * state data.
- *
- * @param consumerInfo
- * The consumer information that defines characteristics of the federation queue consumer
- *
- * @return a new {@link FederationQueueEntry} that holds the given queue name.
- */
- protected FederationQueueEntry createConsumerEntry(FederationConsumerInfo consumerInfo) {
- return new FederationQueueEntry(consumerInfo);
- }
-
- /**
- * Creates a {@link Predicate} that should return true if the given consumer is a federation
- * created consumer which should not be further federated.
- *
- * @param server
- * The server instance for use in creating the filtering {@link Predicate}.
- * @param policy
- * The configured Queue matching policy that can provide additional match criteria.
- *
- * @return a {@link Predicate} that will return true if the consumer should be filtered.
- *
- * @throws ActiveMQException if an error occurs while creating the new consumer filter.
- */
- protected Predicate<ServerConsumer> createFederationConsumerMatcher(ActiveMQServer server, FederationReceiveFromQueuePolicy policy) throws ActiveMQException {
- if (policy.isIncludeFederated()) {
- return (consumer) -> false; // Configuration says to federate these
- } else {
- // This filter matches on the same criteria as the original Core client based
- // Federation code which allows this implementation to see those consumers as
- // well as its own which in this methods implementation must also use this same
- // mechanism to mark federation resources.
-
- final Filter metaDataMatcher =
- FilterImpl.createFilter("\"" + FEDERATION_NAME + "\" IS NOT NULL");
-
- return (consumer) -> {
- final ServerSession serverSession = server.getSessionByID(consumer.getSessionID());
-
- if (serverSession != null && serverSession.getMetaData() != null) {
- return metaDataMatcher.match(serverSession.getMetaData());
- } else {
- return false;
- }
- };
- }
- }
-
- /**
- * Query all registered plugins for this federation instance to determine if any wish to
- * prevent a federation consumer from being created for the given Queue.
- *
- * @param queue
- * The {@link Queue} that the federation queue manager is attempting to create a remote consumer for.
- *
- * @return true if any registered plugin signaled that creation should be suppressed.
- */
- protected abstract boolean isPluginBlockingFederationConsumerCreate(Queue queue);
-
-}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java
index 188eb9eec0..49263bcda4 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonAbstractReceiver.java
@@ -17,8 +17,13 @@
package org.apache.activemq.artemis.protocol.amqp.proton;
import java.lang.invoke.MethodHandles;
+import java.util.Objects;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.function.BiConsumer;
import org.apache.activemq.artemis.api.core.Message;
+import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.core.persistence.OperationContext;
import org.apache.activemq.artemis.core.persistence.impl.nullpm.NullStorageManager;
import org.apache.activemq.artemis.core.server.RoutingContext;
@@ -40,39 +45,32 @@ public abstract class ProtonAbstractReceiver extends ProtonInitializable impleme
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
- protected final AMQPConnectionContext connection;
+ protected enum ReceiverState {
+ STARTED,
+ STOPPING,
+ STOPPED,
+ CLOSED
+ }
+ protected final AMQPConnectionContext connection;
protected final AMQPSessionContext protonSession;
-
protected final Receiver receiver;
-
protected final int minLargeMessageSize;
-
protected final RoutingContext routingContext;
-
protected final AMQPSessionCallback sessionSPI;
-
// Cached instances used for this receiver which will be swapped as message of varying types
// are sent to this receiver from the remote peer.
protected final MessageReader standardMessageReader = new AMQPMessageReader(this);
-
protected final MessageReader largeMessageReader = new AMQPLargeMessageReader(this);
-
- protected volatile MessageReader messageReader;
-
protected final Runnable creditRunnable;
-
protected final boolean useModified;
+ protected final Runnable creditTopUpRunner = this::doCreditTopUpRun;
+ protected volatile MessageReader messageReader;
protected int pendingSettles = 0;
-
- public static boolean isBellowThreshold(int credit, int pending, int threshold) {
- return credit <= threshold - pending;
- }
-
- public static int calculatedUpdateRefill(int refill, int credits, int pending) {
- return refill - credits - pending;
- }
+ protected volatile ReceiverState state = ReceiverState.STARTED;
+ protected BiConsumer<ProtonAbstractReceiver, Boolean> pendingStop;
+ protected ScheduledFuture<?> pendingStopTimeout;
public ProtonAbstractReceiver(AMQPSessionCallback sessionSPI,
AMQPConnectionContext connection,
@@ -84,7 +82,7 @@ public abstract class ProtonAbstractReceiver extends ProtonInitializable impleme
this.receiver = receiver;
this.minLargeMessageSize = getConfiguredMinLargeMessageSize(connection);
this.creditRunnable = createCreditRunnable(connection);
- useModified = this.connection.getProtocolManager().isUseModifiedForTransientDeliveryErrors();
+ this.useModified = this.connection.getProtocolManager().isUseModifiedForTransientDeliveryErrors();
this.routingContext = new RoutingContextImpl(null).setDuplicateDetection(connection.getProtocolManager().isAmqpDuplicateDetection());
}
@@ -92,6 +90,108 @@ public abstract class ProtonAbstractReceiver extends ProtonInitializable impleme
return protonSession;
}
+ /**
+ * Starts the receiver if not already started which triggers a flow of credit
+ * to the remote to begin the processing of incoming messages. This must be
+ * called on the connection thread and will throw and exception if not.
+ *
+ * @throws IllegalStateException if not called from the connection thread or is closed or stopping.
+ */
+ public void start() {
+ connection.requireInHandler();
+
+ if (state == ReceiverState.CLOSED) {
+ throw new IllegalStateException("Cannot start a closed receiver");
+ }
+
+ if (state == ReceiverState.STOPPING) {
+ throw new IllegalStateException("Cannot start a receiver that is not yet stopped");
+ }
+
+ if (state == ReceiverState.STOPPED) {
+ state = ReceiverState.STARTED;
+ topUpCreditIfNeeded();
+ }
+ }
+
+ /**
+ * Stop the receiver from granting additional credit and drains any granted credit
+ * from the link already. If any pending settles or queued message remain in the work
+ * queue then the stop occurs asynchronously and the stop callback is signaled later
+ * otherwise it will be triggered on the current thread to avoid state changes from
+ * making an asynchronous call invalid. The stop call allows a timeout to be specified
+ * which will signal the stopped consumer if the timeout elapses and leaves the receiver
+ * in the stopping state which does not allow for a restart.
+ *
+ * @param stopTimeout
+ * A time in milliseconds to wait for the stop to complete before considering it as having failed.
+ * @param onStopped
+ * A consumer that is signaled once the receiver has stopped or the timeout elapsed.
+ *
+ * @throws IllegalStateException if the receiver is currently in the stopping state.
+ */
+ public void stop(int stopTimeout, BiConsumer<ProtonAbstractReceiver, Boolean> onStopped) {
+ Objects.requireNonNull(onStopped, "The stopped callback must not be null");
+ connection.requireInHandler();
+
+ if (isStarted()) {
+ state = ReceiverState.STOPPING;
+ pendingStop = onStopped;
+ if (!checkIfPendingStopCanComplete()) {
+ if (receiver.getCredit() != 0) {
+ receiver.drain(0);
+ }
+
+ if (stopTimeout > 0) {
+ pendingStopTimeout = protonSession.getServer().getScheduledPool().schedule(() -> {
+ connection.runNow(() -> signalStoppedCallback(false));
+ }, stopTimeout, TimeUnit.MILLISECONDS);
+ }
+ }
+ } else if (isStopped() || isClosed()) {
+ pendingStop = onStopped;
+ signalStoppedCallback(true);
+ } else {
+ throw new IllegalStateException("Receiver is currently in the process of stopping");
+ }
+ }
+
+ @Override
+ public void close(boolean remoteLinkClose) throws ActiveMQAMQPException {
+ state = ReceiverState.CLOSED;
+ protonSession.removeReceiver(receiver);
+ closeCurrentReader();
+ connection.runNow(() -> {
+ signalStoppedCallback(true);
+ });
+ }
+
+ @Override
+ public void close(ErrorCondition condition) throws ActiveMQAMQPException {
+ receiver.setCondition(condition);
+ close(false);
+ }
+
+ public AMQPConnectionContext getConnection() {
+ return connection;
+ }
+
+ public boolean isStarted() {
+ return state == ReceiverState.STARTED;
+ }
+
+ public boolean isStopping() {
+ return state == ReceiverState.STOPPING;
+ }
+
+ public boolean isStopped() {
+ return state == ReceiverState.STOPPED;
+ }
+
+ public boolean isClosed() {
+ return state == ReceiverState.CLOSED;
+ }
+
/** Set the proper operation context in the Thread Local.
* Return the old context*/
protected OperationContext recoverContext() {
@@ -158,36 +258,11 @@ public abstract class ProtonAbstractReceiver extends ProtonInitializable impleme
}
/**
- * This Credit Runnable can be used to manage the credit replenishment of a target AMQP receiver.
- * <p>
- * This method is generally used for tests as it does not account for the receiver context that is
- * assigned to the given receiver instance which does not allow for tracking pending settles.
- *
- * @param refill
- * The number of credit to top off the receiver to
- * @param threshold
- * The low water mark for credit before refill is done
- * @param receiver
- * The proton receiver that will have its credit refilled
- * @param connection
- * The connection that own the receiver
- *
- * @return A new Runnable that can be used to keep receiver credit replenished.
+ * This servers as the default credit runnable which grants credit in batches based on a
+ * low water mark and a configured credit size to top the credit up to once the low water
+ * mark has been reached.
*/
- public static Runnable createCreditRunnable(int refill,
- int threshold,
- Receiver receiver,
- AMQPConnectionContext connection) {
- return new FlowControlRunner(refill, threshold, receiver, connection, null);
- }
-
- /**
- * The reason why we use the AtomicRunnable here
- * is because PagingManager will call Runnable in case it was blocked.
- * however it could call many Runnable
- * and this serves as a control to avoid duplicated calls
- * */
- static class FlowControlRunner implements Runnable {
+ protected static class FlowControlRunner implements Runnable {
/*
* The number of credits sent to the remote when the runnable decides that a top off is needed.
@@ -204,6 +279,10 @@ public abstract class ProtonAbstractReceiver extends ProtonInitializable impleme
final ProtonAbstractReceiver context;
FlowControlRunner(int refill, int threshold, Receiver receiver, AMQPConnectionContext connection, ProtonAbstractReceiver context) {
+ Objects.requireNonNull(receiver, "Given proton receiver cannot be null");
+ Objects.requireNonNull(connection, "Given connection context cannot be null");
+ Objects.requireNonNull(context, "Given receiver context cannot be null");
+
this.refill = refill;
this.threshold = threshold;
this.receiver = receiver;
@@ -213,21 +292,23 @@ public abstract class ProtonAbstractReceiver extends ProtonInitializable impleme
@Override
public void run() {
- if (!connection.isHandler()) {
- // for the case where the paging manager is resuming flow due to blockage
- // this should then move back to the connection thread.
- connection.runLater(this);
- return;
- }
-
- connection.requireInHandler();
- int pending = context != null ? context.pendingSettles : 0;
- if (isBellowThreshold(receiver.getCredit(), pending, threshold)) {
- int topUp = calculatedUpdateRefill(refill, receiver.getCredit(), pending);
- if (topUp > 0) {
- receiver.flow(topUp);
- connection.instantFlush();
+ if (connection.isHandler()) {
+ connection.requireInHandler();
+
+ if (context.isStarted()) {
+ final int pending = context.pendingSettles;
+
+ if (isBellowThreshold(receiver.getCredit(), pending, threshold)) {
+ int topUp = calculatedUpdateRefill(refill, receiver.getCredit(), pending);
+ if (topUp > 0) {
+ receiver.flow(topUp);
+ connection.instantFlush();
+ }
+ }
}
+ } else {
+ // This must run on the connection thread as it interacts with proton
+ connection.runLater(this);
}
}
}
@@ -243,12 +324,31 @@ public abstract class ProtonAbstractReceiver extends ProtonInitializable impleme
pendingSettles--;
assert pendingSettles >= 0;
settlement.settle();
- flow();
+ if (isStarted()) {
+ topUpCreditIfNeeded();
+ } else {
+ checkIfPendingStopCanComplete();
+ }
+ }
+
+ private boolean checkIfPendingStopCanComplete() {
+ if (isStopping() && pendingSettles == 0 && receiver.getQueued() == 0 && receiver.getCredit() == 0) {
+ state = ReceiverState.STOPPED;
+ signalStoppedCallback(true);
+
+ return true;
+ }
+
+ return false;
}
@Override
public void onFlow(int credits, boolean drain) {
- flow();
+ if (isStopping()) {
+ checkIfPendingStopCanComplete();
+ } else {
+ topUpCreditIfNeeded();
+ }
}
private void handleAbortedDelivery(Delivery delivery) {
@@ -261,9 +361,12 @@ public abstract class ProtonAbstractReceiver extends ProtonInitializable impleme
receiver.advance();
delivery.settle();
- // Replenish the credit if not doing a drain
- if (!receiver.getDrain()) {
+ // Replenish the credit if not doing a drain and the receiver is still
+ // started and has not initiated a stop request or has been closed
+ if (!receiver.getDrain() && isStarted()) {
receiver.flow(1);
+ } else {
+ checkIfPendingStopCanComplete();
}
}
@@ -341,8 +444,7 @@ public abstract class ProtonAbstractReceiver extends ProtonInitializable impleme
}
}
- public void onMessageComplete(Delivery delivery,
- Message message, DeliveryAnnotations deliveryAnnotations) {
+ public void onMessageComplete(Delivery delivery, Message message, DeliveryAnnotations deliveryAnnotations) {
connection.requireInHandler();
try {
@@ -366,12 +468,6 @@ public abstract class ProtonAbstractReceiver extends ProtonInitializable impleme
}
}
- @Override
- public void close(boolean remoteLinkClose) throws ActiveMQAMQPException {
- protonSession.removeReceiver(receiver);
- closeCurrentReader();
- }
-
public void onExceptionWhileReading(Throwable e) {
logger.warn(e.getMessage(), e);
connection.runNow(() -> {
@@ -383,19 +479,81 @@ public abstract class ProtonAbstractReceiver extends ProtonInitializable impleme
});
}
- @Override
- public void close(ErrorCondition condition) throws ActiveMQAMQPException {
- receiver.setCondition(condition);
- close(false);
+ /**
+ * Returns either the fixed address assigned to this sender, or the last address used by
+ * an anonymous relay sender. If this is an anonymous relay and no send has occurred then
+ * this method returns null.
+ *
+ * @return the assigned address or the last used address if any for anonymous relay senders.
+ */
+ protected abstract SimpleString getAddressInUse();
+
+ /**
+ * Perform the actual message processing for an inbound message. The subclass either consumes and settles
+ * the message in place or hands it off to another intermediary who is responsible for eventually settling
+ * the newly read message.
+ *
+ * @param message
+ * The message as provided from the remote or after local transformation by subclass.
+ * @param delivery
+ * The proton delivery where the message bytes where read from
+ * @param deliveryAnnotations
+ * The delivery annotations if present that accompanied the incoming message.
+ * @param receiver
+ * The proton receiver that represents the link over which the message was sent.
+ * @param tx
+ * The transaction under which the incoming message was sent.
+ */
+ protected abstract void actualDelivery(Message message, Delivery delivery, DeliveryAnnotations deliveryAnnotations, Receiver receiver, Transaction tx);
+
+ /**
+ * Final credit top up request API that will trigger a credit top up if the receiver is in
+ * a state where a grant of additional receiver credit is allowable.
+ */
+ protected final void topUpCreditIfNeeded() {
+ connection.requireInHandler();
+ // this will configure a flow control event to happen once after the event loop has completed
+ if (isStarted()) {
+ connection.afterFlush(creditTopUpRunner);
+ }
}
- public AMQPConnectionContext getConnection() {
- return connection;
+ private void signalStoppedCallback(boolean stopped) {
+ if (pendingStopTimeout != null) {
+ pendingStopTimeout.cancel(false);
+ pendingStopTimeout = null;
+ }
+
+ if (pendingStop != null) {
+ try {
+ pendingStop.accept(this, stopped);
+ } catch (Exception e) {
+ logger.trace("Suppressed error from pending stop callback: ", e);
+ } finally {
+ pendingStop = null;
+ }
+ }
}
- protected abstract void actualDelivery(Message message, Delivery delivery, DeliveryAnnotations deliveryAnnotations, Receiver receiver, Transaction tx);
+ /**
+ * Performs the actual credit top up logic for the receiver.
+ *
+ * This can be overridden in the subclass to run its own logic for credit top
+ * up instead of using the default logic used in this abstract base.
+ */
+ protected void doCreditTopUpRun() {
+ connection.requireInHandler();
+ if (isStarted()) {
+ // Use the SessionSPI to allocate producer credits, or default, always allocate credit.
+ sessionSPI.flow(getAddressInUse(), creditRunnable);
+ }
+ }
- // TODO: how to implement flow here?
- public abstract void flow();
+ public static boolean isBellowThreshold(int credit, int pending, int threshold) {
+ return credit <= threshold - pending;
+ }
+ public static int calculatedUpdateRefill(int refill, int credits, int pending) {
+ return refill - credits - pending;
+ }
}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonServerReceiverContext.java b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonServerReceiverContext.java
index 9e6cf00769..b7ec4457f1 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonServerReceiverContext.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/proton/ProtonServerReceiverContext.java
@@ -66,9 +66,6 @@ public class ProtonServerReceiverContext extends ProtonAbstractReceiver {
protected SimpleString lastAddress;
protected AddressFullMessagePolicy lastAddressPolicy;
protected boolean addressAlreadyClashed = false;
-
- protected final Runnable spiFlow = this::sessionSPIFlow;
-
protected RoutingType defRoutingType;
public ProtonServerReceiverContext(AMQPSessionCallback sessionSPI,
@@ -150,7 +147,12 @@ public class ProtonServerReceiverContext extends ProtonAbstractReceiver {
}
}
- flow();
+ topUpCreditIfNeeded();
+ }
+
+ @Override
+ protected SimpleString getAddressInUse() {
+ return address != null ? address : lastAddress;
}
public RoutingType getDefRoutingType() {
@@ -162,7 +164,7 @@ public class ProtonServerReceiverContext extends ProtonAbstractReceiver {
return target != null ? getRoutingType(target.getCapabilities(), address) : getRoutingType((Symbol[]) null, address);
}
- protected RoutingType getRoutingType(Symbol[] symbols, SimpleString address) {
+ protected final RoutingType getRoutingType(Symbol[] symbols, SimpleString address) {
RoutingType explicitRoutingType = getExplicitRoutingType(symbols);
if (explicitRoutingType != null) {
return explicitRoutingType;
@@ -176,29 +178,11 @@ public class ProtonServerReceiverContext extends ProtonAbstractReceiver {
if (addressInfo != null && addressInfo.getRoutingTypes().size() == 1) {
return addressInfo.getRoutingType();
} else {
- return getDefaultRoutingType(address);
+ return getDefaultRoutingType(sessionSPI, address);
}
}
}
- private RoutingType getDefaultRoutingType(SimpleString address) {
- RoutingType defaultRoutingType = sessionSPI.getRoutingTypeFromPrefix(address, sessionSPI.getDefaultRoutingType(address));
- return defaultRoutingType == null ? ActiveMQDefaultConfiguration.getDefaultRoutingType() : defaultRoutingType;
- }
-
- private RoutingType getExplicitRoutingType(Symbol[] symbols) {
- if (symbols != null) {
- for (Symbol symbol : symbols) {
- if (AmqpSupport.TEMP_TOPIC_CAPABILITY.equals(symbol) || AmqpSupport.TOPIC_CAPABILITY.equals(symbol)) {
- return RoutingType.MULTICAST;
- } else if (AmqpSupport.TEMP_QUEUE_CAPABILITY.equals(symbol) || AmqpSupport.QUEUE_CAPABILITY.equals(symbol)) {
- return RoutingType.ANYCAST;
- }
- }
- }
- return null;
- }
-
@Override
protected void actualDelivery(Message message, Delivery delivery, DeliveryAnnotations deliveryAnnotations, Receiver receiver, Transaction tx) {
try {
@@ -235,16 +219,32 @@ public class ProtonServerReceiverContext extends ProtonAbstractReceiver {
public void deliveryFailed(Delivery delivery, Receiver receiver, Exception e) {
connection.runNow(() -> {
- DeliveryState deliveryState = determineDeliveryState(((Source) receiver.getSource()),
- useModified,
- e);
- delivery.disposition(deliveryState);
+ delivery.disposition(determineDeliveryState(((Source) receiver.getSource()), useModified, e));
settle(delivery);
connection.flush();
});
}
- private DeliveryState determineDeliveryState(final Source source, final boolean useModified, final Exception e) {
+ @Override
+ public void close(boolean remoteLinkClose) throws ActiveMQAMQPException {
+ super.close(remoteLinkClose);
+ sessionSPI.removeProducer(receiver.getName());
+ org.apache.qpid.proton.amqp.messaging.Target target = (org.apache.qpid.proton.amqp.messaging.Target) receiver.getRemoteTarget();
+ if (target != null && target.getDynamic() && (target.getExpiryPolicy() == TerminusExpiryPolicy.LINK_DETACH || target.getExpiryPolicy() == TerminusExpiryPolicy.SESSION_END)) {
+ try {
+ sessionSPI.removeTemporaryQueue(SimpleString.of(target.getAddress()));
+ } catch (Exception e) {
+ // Ignored as the temporary resource will be auto removed later.
+ }
+ }
+ }
+
+ private static RoutingType getDefaultRoutingType(AMQPSessionCallback sessionSPI, SimpleString address) {
+ RoutingType defaultRoutingType = sessionSPI.getRoutingTypeFromPrefix(address, sessionSPI.getDefaultRoutingType(address));
+ return defaultRoutingType == null ? ActiveMQDefaultConfiguration.getDefaultRoutingType() : defaultRoutingType;
+ }
+
+ private static DeliveryState determineDeliveryState(final Source source, final boolean useModified, final Exception e) {
Outcome defaultOutcome = getEffectiveDefaultOutcome(source);
if (isAddressFull(e) && useModified &&
@@ -266,14 +266,22 @@ public class ProtonServerReceiverContext extends ProtonAbstractReceiver {
}
}
- private boolean isAddressFull(final Exception e) {
- return e instanceof ActiveMQException && ActiveMQExceptionType.ADDRESS_FULL.equals(((ActiveMQException) e).getType());
+ private static RoutingType getExplicitRoutingType(Symbol[] symbols) {
+ if (symbols != null) {
+ for (Symbol symbol : symbols) {
+ if (AmqpSupport.TEMP_TOPIC_CAPABILITY.equals(symbol) || AmqpSupport.TOPIC_CAPABILITY.equals(symbol)) {
+ return RoutingType.MULTICAST;
+ } else if (AmqpSupport.TEMP_QUEUE_CAPABILITY.equals(symbol) || AmqpSupport.QUEUE_CAPABILITY.equals(symbol)) {
+ return RoutingType.ANYCAST;
+ }
+ }
+ }
+ return null;
}
- private Rejected createRejected(final Exception e) {
+ private static Rejected createRejected(final Exception e) {
ErrorCondition condition = new ErrorCondition();
- // Set condition
if (e instanceof ActiveMQSecurityException) {
condition.setCondition(AmqpError.UNAUTHORIZED_ACCESS);
} else if (isAddressFull(e)) {
@@ -285,62 +293,22 @@ public class ProtonServerReceiverContext extends ProtonAbstractReceiver {
Rejected rejected = new Rejected();
rejected.setError(condition);
- return rejected;
- }
- @Override
- public void close(boolean remoteLinkClose) throws ActiveMQAMQPException {
- super.close(remoteLinkClose);
- sessionSPI.removeProducer(receiver.getName());
- org.apache.qpid.proton.amqp.messaging.Target target = (org.apache.qpid.proton.amqp.messaging.Target) receiver.getRemoteTarget();
- if (target != null && target.getDynamic() && (target.getExpiryPolicy() == TerminusExpiryPolicy.LINK_DETACH || target.getExpiryPolicy() == TerminusExpiryPolicy.SESSION_END)) {
- try {
- sessionSPI.removeTemporaryQueue(SimpleString.of(target.getAddress()));
- } catch (Exception e) {
- //ignore on close, its temp anyway and will be removed later
- }
- }
- }
-
- @Override
- public void flow() {
- // this will mark flow control to happen once after the event loop
- connection.afterFlush(spiFlow);
- }
-
- protected void sessionSPIFlow() {
- connection.requireInHandler();
- // Use the SessionSPI to allocate producer credits, or default, always allocate credit.
- if (sessionSPI != null) {
- sessionSPI.flow(address != null ? address : lastAddress, creditRunnable);
- } else {
- creditRunnable.run();
- }
- }
-
- public void drain(int credits) {
- connection.runNow(() -> {
- receiver.drain(credits);
- connection.flush();
- });
- }
-
- public int drained() {
- return receiver.drained();
+ return rejected;
}
- public boolean isDraining() {
- return receiver.draining();
+ private static boolean isAddressFull(final Exception e) {
+ return e instanceof ActiveMQException && ActiveMQExceptionType.ADDRESS_FULL.equals(((ActiveMQException) e).getType());
}
- private boolean outcomeSupported(final Source source, final Symbol outcome) {
+ private static boolean outcomeSupported(final Source source, final Symbol outcome) {
if (source != null && source.getOutcomes() != null) {
return Arrays.asList(( source).getOutcomes()).contains(outcome);
}
return false;
}
- private Outcome getEffectiveDefaultOutcome(final Source source) {
+ private static Outcome getEffectiveDefaultOutcome(final Source source) {
return (source.getOutcomes() == null || source.getOutcomes().length == 0) ? source.getDefaultOutcome() : null;
}
}
diff --git a/artemis-protocols/artemis-amqp-protocol/src/test/java/org/apache/activemq/artemis/protocol/amqp/broker/AMQPSessionCallbackTest.java b/artemis-protocols/artemis-amqp-protocol/src/test/java/org/apache/activemq/artemis/protocol/amqp/broker/AMQPSessionCallbackTest.java
index 53e351d7ae..c64485497b 100644
--- a/artemis-protocols/artemis-amqp-protocol/src/test/java/org/apache/activemq/artemis/protocol/amqp/broker/AMQPSessionCallbackTest.java
+++ b/artemis-protocols/artemis-amqp-protocol/src/test/java/org/apache/activemq/artemis/protocol/amqp/broker/AMQPSessionCallbackTest.java
@@ -33,6 +33,7 @@ import org.apache.activemq.artemis.core.persistence.OperationContext;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.protocol.amqp.proton.AMQPConnectionContext;
import org.apache.activemq.artemis.protocol.amqp.proton.AMQPSessionContext;
+import org.apache.activemq.artemis.protocol.amqp.proton.ProtonAbstractReceiver;
import org.apache.activemq.artemis.protocol.amqp.proton.ProtonServerReceiverContext;
import org.apache.activemq.artemis.spi.core.remoting.Connection;
import org.apache.qpid.proton.engine.Receiver;
@@ -86,11 +87,13 @@ public class AMQPSessionCallbackTest {
// Capture credit runnable and invoke to trigger credit top off
ArgumentCaptor<Runnable> argument = ArgumentCaptor.forClass(Runnable.class);
AMQPSessionCallback session = new AMQPSessionCallback(protonSPI, manager, connection, transportConnection, executor, operationContext);
+ ProtonAbstractReceiver context = Mockito.mock(ProtonAbstractReceiver.class);
// Credit is above threshold
Mockito.when(receiver.getCredit()).thenReturn(AMQP_LOW_CREDITS_DEFAULT + 1);
+ Mockito.when(context.isStarted()).thenReturn(true);
- session.flow(null, ProtonServerReceiverContext.createCreditRunnable(AMQP_CREDITS_DEFAULT, AMQP_LOW_CREDITS_DEFAULT, receiver, connection));
+ session.flow(null, ProtonServerReceiverContext.createCreditRunnable(AMQP_CREDITS_DEFAULT, AMQP_LOW_CREDITS_DEFAULT, receiver, connection, context));
// Run the credit refill code.
Mockito.verify(pagingManager).checkMemory(argument.capture());
@@ -116,11 +119,13 @@ public class AMQPSessionCallbackTest {
// Capture credit runnable and invoke to trigger credit top off
ArgumentCaptor<Runnable> argument = ArgumentCaptor.forClass(Runnable.class);
AMQPSessionCallback session = new AMQPSessionCallback(protonSPI, manager, connection, transportConnection, executor, operationContext);
+ ProtonAbstractReceiver context = Mockito.mock(ProtonAbstractReceiver.class);
// Credit is at threshold
Mockito.when(receiver.getCredit()).thenReturn(AMQP_LOW_CREDITS_DEFAULT);
+ Mockito.when(context.isStarted()).thenReturn(true);
- session.flow(null, ProtonServerReceiverContext.createCreditRunnable(AMQP_CREDITS_DEFAULT, AMQP_LOW_CREDITS_DEFAULT, receiver, connection));
+ session.flow(null, ProtonServerReceiverContext.createCreditRunnable(AMQP_CREDITS_DEFAULT, AMQP_LOW_CREDITS_DEFAULT, receiver, connection, context));
// Run the credit refill code.
Mockito.verify(pagingManager).checkMemory(argument.capture());
@@ -147,11 +152,13 @@ public class AMQPSessionCallbackTest {
// Capture credit runnable and invoke to trigger credit top off
ArgumentCaptor<Runnable> argument = ArgumentCaptor.forClass(Runnable.class);
AMQPSessionCallback session = new AMQPSessionCallback(protonSPI, manager, connection, transportConnection, executor, operationContext);
+ ProtonAbstractReceiver context = Mockito.mock(ProtonAbstractReceiver.class);
// Credit is above threshold
Mockito.when(receiver.getCredit()).thenReturn(AMQP_LOW_CREDITS_DEFAULT + 1);
+ Mockito.when(context.isStarted()).thenReturn(true);
- session.flow(SimpleString.of("test"), ProtonServerReceiverContext.createCreditRunnable(AMQP_CREDITS_DEFAULT, AMQP_LOW_CREDITS_DEFAULT, receiver, connection));
+ session.flow(SimpleString.of("test"), ProtonServerReceiverContext.createCreditRunnable(AMQP_CREDITS_DEFAULT, AMQP_LOW_CREDITS_DEFAULT, receiver, connection, context));
// Run the credit refill code.
Mockito.verify(pagingStore).checkMemory(argument.capture(), Mockito.isA(Consumer.class));
@@ -178,11 +185,13 @@ public class AMQPSessionCallbackTest {
// Capture credit runnable and invoke to trigger credit top off
ArgumentCaptor<Runnable> argument = ArgumentCaptor.forClass(Runnable.class);
AMQPSessionCallback session = new AMQPSessionCallback(protonSPI, manager, connection, transportConnection, executor, operationContext);
+ ProtonAbstractReceiver context = Mockito.mock(ProtonAbstractReceiver.class);
// Credit is at threshold
+ Mockito.when(context.isStarted()).thenReturn(true);
Mockito.when(receiver.getCredit()).thenReturn(AMQP_LOW_CREDITS_DEFAULT);
- session.flow(SimpleString.of("test"), ProtonServerReceiverContext.createCreditRunnable(AMQP_CREDITS_DEFAULT, AMQP_LOW_CREDITS_DEFAULT, receiver, connection));
+ session.flow(SimpleString.of("test"), ProtonServerReceiverContext.createCreditRunnable(AMQP_CREDITS_DEFAULT, AMQP_LOW_CREDITS_DEFAULT, receiver, connection, context));
// Run the credit refill code.
Mockito.verify(pagingStore).checkMemory(argument.capture(), Mockito.isA(Consumer.class));
@@ -208,11 +217,13 @@ public class AMQPSessionCallbackTest {
// Capture credit runnable and invoke to trigger credit top off
ArgumentCaptor<Runnable> argument = ArgumentCaptor.forClass(Runnable.class);
AMQPSessionCallback session = new AMQPSessionCallback(protonSPI, manager, connection, transportConnection, executor, operationContext);
+ ProtonAbstractReceiver context = Mockito.mock(ProtonAbstractReceiver.class);
// Credit is at threshold
Mockito.when(receiver.getCredit()).thenReturn(AMQP_LOW_CREDITS_DEFAULT);
+ Mockito.when(context.isStarted()).thenReturn(true);
- session.flow(null, ProtonServerReceiverContext.createCreditRunnable(1, AMQP_LOW_CREDITS_DEFAULT, receiver, connection));
+ session.flow(null, ProtonServerReceiverContext.createCreditRunnable(1, AMQP_LOW_CREDITS_DEFAULT, receiver, connection, context));
// Run the credit refill code.
Mockito.verify(pagingManager).checkMemory(argument.capture());
@@ -239,11 +250,13 @@ public class AMQPSessionCallbackTest {
// Capture credit runnable and invoke to trigger credit top off
ArgumentCaptor<Runnable> argument = ArgumentCaptor.forClass(Runnable.class);
AMQPSessionCallback session = new AMQPSessionCallback(protonSPI, manager, connection, transportConnection, executor, operationContext);
+ ProtonAbstractReceiver context = Mockito.mock(ProtonAbstractReceiver.class);
// Credit is at threshold
Mockito.when(receiver.getCredit()).thenReturn(AMQP_LOW_CREDITS_DEFAULT);
+ Mockito.when(context.isStarted()).thenReturn(true);
- session.flow(SimpleString.of("test"), ProtonServerReceiverContext.createCreditRunnable(1, AMQP_LOW_CREDITS_DEFAULT, receiver, connection));
+ session.flow(SimpleString.of("test"), ProtonServerReceiverContext.createCreditRunnable(1, AMQP_LOW_CREDITS_DEFAULT, receiver, connection, context));
// Run the credit refill code.
Mockito.verify(pagingStore).checkMemory(argument.capture(), Mockito.isA(Consumer.class));
@@ -272,4 +285,36 @@ public class AMQPSessionCallbackTest {
Mockito.verify(protonSession).close();
}
+
+ /**
+ * Test that a producer that is not started will not offer credit to top off current.
+ */
+ @Test
+ public void testProducerThatIsNotStartedDoesNotTopOffCredit() throws Exception {
+ // Mock returns to get at the runnable that grants credit.
+ Mockito.when(manager.getServer()).thenReturn(server);
+ Mockito.when(server.getPagingManager()).thenReturn(pagingManager);
+ Mockito.when(pagingManager.getPageStore(any(SimpleString.class))).thenReturn(pagingStore);
+
+ // Capture credit runnable and invoke to trigger credit top off
+ ArgumentCaptor<Runnable> argument = ArgumentCaptor.forClass(Runnable.class);
+ AMQPSessionCallback session = new AMQPSessionCallback(protonSPI, manager, connection, transportConnection, executor, operationContext);
+ ProtonAbstractReceiver context = Mockito.mock(ProtonAbstractReceiver.class);
+
+ // Credit is at threshold
+ Mockito.when(context.isStarted()).thenReturn(false);
+
+ session.flow(SimpleString.of("test"), ProtonServerReceiverContext.createCreditRunnable(AMQP_CREDITS_DEFAULT, AMQP_LOW_CREDITS_DEFAULT, receiver, connection, context));
+
+ // Run the credit refill code.
+ Mockito.verify(pagingStore).checkMemory(argument.capture(), Mockito.isA(Consumer.class));
+ assertNotNull(argument.getValue());
+ argument.getValue().run();
+
+ // Ensure we aren't looking at remote credit as that gives us the wrong view of what credit is at the broker
+ Mockito.verify(receiver, never()).getRemoteCredit();
+
+ // Credit runnable should not top off credit to configured value
+ Mockito.verifyNoInteractions(receiver);
+ }
}
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationAddressPolicyTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationAddressPolicyTest.java
index ac22936ce9..877950c7a1 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationAddressPolicyTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationAddressPolicyTest.java
@@ -35,6 +35,7 @@ import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPF
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.POLICY_NAME;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_CREDITS;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_CREDITS_LOW;
+import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_QUIESCE_TIMEOUT;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.REQUESTED_ADDRESS_ADDED;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.REQUESTED_ADDRESS_NAME;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.TRANSFORMER_CLASS_NAME;
@@ -236,6 +237,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
}
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
// This should trigger the federation consumer to be shutdown as the statically defined queue
@@ -636,6 +640,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
connection.start();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
// Demand is removed so receiver should be detached.
@@ -724,6 +731,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
consumer.close();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
session.unsubscribe("shared-subscription");
@@ -806,6 +816,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
// Will fail if any frames arrive
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
consumer2.close(); // Now demand is gone
@@ -871,6 +884,7 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
connection.start();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true).optional();
peer.expectDetach().respond();
server.removeAddressInfo(SimpleString.of("test"), null, true);
@@ -895,6 +909,10 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
connection.start();
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
}
@@ -976,6 +994,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
connection.start();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
consumer.close();
@@ -1067,6 +1088,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
consumer1.close();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
consumer2.close();
@@ -1152,6 +1176,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
consumer1.close(); // One is gone but another remains
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
consumer2.close(); // Now demand is gone
@@ -1333,6 +1360,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
consumer1.close(); // One is gone but another remains
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
consumer2.close(); // Now demand is gone from the divert
@@ -1417,6 +1447,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
connection.start();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
server.destroyDivert(SimpleString.of("test-divert"));
@@ -1511,6 +1544,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
server.destroyDivert(SimpleString.of("test-divert"));
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
consumer.close();
@@ -1586,6 +1622,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
assertEquals("test-message", ((TextMessage) message).getText());
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(999).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach(); // demand will be gone and receiver link should close.
}
@@ -1728,6 +1767,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
assertEquals("test-message", ((TextMessage) message).getText());
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(999).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach(); // demand will be gone and receiver link should close.
}
@@ -1793,6 +1835,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
assertEquals("test-message", ((TextMessage) message).getText());
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(10).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(11).withDrain(true);
peer.expectDetach(); // demand will be gone and receiver link should close.
}
@@ -1863,6 +1908,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
assertEquals("test-message", ((TextMessage) message).getText());
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(40).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(41).withDrain(true);
peer.expectDetach(); // demand will be gone and receiver link should close.
}
@@ -1937,6 +1985,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
assertEquals("value2", message.getStringProperty("key2"));
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(999).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach(); // demand will be gone and receiver link should close.
}
@@ -2238,6 +2289,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
assertEquals("two", message.getStringProperty("appProperty2"));
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(999).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach(); // demand will be gone and receiver link should close.
}
@@ -2564,6 +2618,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
connection.close();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
// This should trigger the federation consumer to be shutdown as the statically defined queue
@@ -2676,6 +2733,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
session.createConsumer(target);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
// This removes the connection demand, but leaves behind the static queue
@@ -2795,6 +2855,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
server.destroyDivert(SimpleString.of(divert1.getName()));
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
server.destroyDivert(SimpleString.of(divert2.getName()));
@@ -3475,6 +3538,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
consumer2.close();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
// Demand should be gone now
@@ -3565,6 +3631,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
consumer2.close();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
// Demand should be gone now
@@ -4234,6 +4303,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
.respond()
.withOfferedCapabilities(FEDERATION_ADDRESS_RECEIVER.toString());
peer.expectFlow().withLinkCredit(1000);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond().afterDelay(40); // Defer the detach response for a bit
server.addAddressInfo(new AddressInfo(SimpleString.of(getTestName()), RoutingType.MULTICAST));
@@ -4260,6 +4332,9 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
.respond()
.withOfferedCapabilities(FEDERATION_ADDRESS_RECEIVER.toString());
peer.expectFlow().withLinkCredit(1000);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
// Create demand on the address which creates a federation consumer again quickly which
@@ -4283,6 +4358,331 @@ public class AMQPFederationAddressPolicyTest extends AmqpClientTestSupport {
}
}
+ @Test
+ @Timeout(20)
+ public void testFederationLinksRestartedBeforeRemoteFinishesDraining() throws Exception {
+ try (ProtonTestServer peer = new ProtonTestServer()) {
+ peer.expectSASLAnonymousConnect();
+ peer.expectOpen().respond();
+ peer.expectBegin().respond();
+ peer.expectAttach().ofSender()
+ .withDesiredCapability(FEDERATION_CONTROL_LINK.toString())
+ .respondInKind();
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_EVENT_LINK.toString())
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(10);
+ peer.start();
+
+ final URI remoteURI = peer.getServerURI();
+ logger.info("Connect test started, peer listening on: {}", remoteURI);
+
+ final AMQPFederationAddressPolicyElement receiveFromAddress = new AMQPFederationAddressPolicyElement();
+ receiveFromAddress.setName("address-policy");
+ receiveFromAddress.addToIncludes(getTestName());
+
+ final AMQPFederatedBrokerConnectionElement element = new AMQPFederatedBrokerConnectionElement();
+ element.setName(getTestName());
+ element.addLocalAddressPolicy(receiveFromAddress);
+
+ final AMQPBrokerConnectConfiguration amqpConnection =
+ new AMQPBrokerConnectConfiguration(getTestName(), "tcp://" + remoteURI.getHost() + ":" + remoteURI.getPort());
+ amqpConnection.setReconnectAttempts(0);// No reconnects
+ amqpConnection.addElement(element);
+
+ server.getConfiguration().addAMQPConnection(amqpConnection);
+ server.start();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_ADDRESS_RECEIVER.toString())
+ .withName(allOf(containsString(getTestName()),
+ containsString("address-receiver"),
+ containsString(server.getNodeID().toString())))
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(1000);
+
+ final ConnectionFactory factory = CFUtil.createConnectionFactory("AMQP", "tcp://localhost:" + AMQP_PORT);
+
+ try (Connection connection = factory.createConnection()) {
+ final Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
+ session.createConsumer(session.createTopic(getTestName()));
+
+ connection.start();
+
+ peer.expectFlow().withLinkCredit(1000).withDrain(true); // Don't answer drained
+ }
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+
+ try (Connection connection = factory.createConnection()) {
+ final Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
+ session.createConsumer(session.createTopic(getTestName()));
+
+ connection.start();
+
+ // This should result from the answer to the next flow that drains the link.
+ peer.expectFlow().withLinkCredit(1000);
+
+ // Now answer the drained and expect that the queue policy manager will then
+ // restart flow of credit based on new demand as expected above.
+ peer.remoteFlow().withLinkCredit(0).withDeliveryCount(1000).withDrain(true).now();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(2000).withDrain(true);
+ peer.expectDetach().respond();
+ }
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.close();
+ }
+ }
+
+ @Test
+ @Timeout(20)
+ public void testFederationLinksDetachesAfterLinkQuiesceTimeout() throws Exception {
+ try (ProtonTestServer peer = new ProtonTestServer()) {
+ peer.expectSASLAnonymousConnect();
+ peer.expectOpen().respond();
+ peer.expectBegin().respond();
+ peer.expectAttach().ofSender()
+ .withDesiredCapability(FEDERATION_CONTROL_LINK.toString())
+ .respondInKind();
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_EVENT_LINK.toString())
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(10);
+ peer.start();
+
+ final URI remoteURI = peer.getServerURI();
+ logger.info("Connect test started, peer listening on: {}", remoteURI);
+
+ final AMQPFederationAddressPolicyElement receiveFromAddress = new AMQPFederationAddressPolicyElement();
+ receiveFromAddress.setName("address-policy");
+ receiveFromAddress.addToIncludes(getTestName());
+ receiveFromAddress.addProperty(RECEIVER_QUIESCE_TIMEOUT, 20);
+
+ final AMQPFederatedBrokerConnectionElement element = new AMQPFederatedBrokerConnectionElement();
+ element.setName(getTestName());
+ element.addLocalAddressPolicy(receiveFromAddress);
+
+ final AMQPBrokerConnectConfiguration amqpConnection =
+ new AMQPBrokerConnectConfiguration(getTestName(), "tcp://" + remoteURI.getHost() + ":" + remoteURI.getPort());
+ amqpConnection.setReconnectAttempts(0);// No reconnects
+ amqpConnection.addElement(element);
+
+ server.getConfiguration().addAMQPConnection(amqpConnection);
+ server.start();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_ADDRESS_RECEIVER.toString())
+ .withName(allOf(containsString(getTestName()),
+ containsString("address-receiver"),
+ containsString(server.getNodeID().toString())))
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(1000);
+
+ final ConnectionFactory factory = CFUtil.createConnectionFactory("AMQP", "tcp://localhost:" + AMQP_PORT);
+
+ try (Connection connection = factory.createConnection()) {
+ final Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
+ session.createConsumer(session.createTopic(getTestName()));
+
+ connection.start();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true); // Don't answer drained then wait for the
+ peer.expectDetach().respond(); // timeout to see the link is detached.
+ }
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_ADDRESS_RECEIVER.toString())
+ .withName(allOf(containsString(getTestName()),
+ containsString("address-receiver"),
+ containsString(server.getNodeID().toString())))
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(1000);
+
+ // New demand should create a new federation consumer after the last drain timed out and closed the link
+ try (Connection connection = factory.createConnection()) {
+ final Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
+ session.createConsumer(session.createTopic(getTestName()));
+
+ connection.start();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(2000).withDrain(true);
+ peer.expectDetach().respond();
+ }
+
+ peer.expectFlow().withLinkCredit(1000);
+ peer.close();
+ }
+ }
+
+ @Test
+ @Timeout(20)
+ public void testFederationLinksRecoveredAfterLinkQuiesceTimeoutWithRenewedDemand() throws Exception {
+ try (ProtonTestServer peer = new ProtonTestServer()) {
+ peer.expectSASLAnonymousConnect();
+ peer.expectOpen().respond();
+ peer.expectBegin().respond();
+ peer.expectAttach().ofSender()
+ .withDesiredCapability(FEDERATION_CONTROL_LINK.toString())
+ .respondInKind();
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_EVENT_LINK.toString())
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(10);
+ peer.start();
+
+ final URI remoteURI = peer.getServerURI();
+ logger.info("Connect test started, peer listening on: {}", remoteURI);
+
+ final AMQPFederationAddressPolicyElement receiveFromAddress = new AMQPFederationAddressPolicyElement();
+ receiveFromAddress.setName("address-policy");
+ receiveFromAddress.addToIncludes(getTestName());
+ receiveFromAddress.addProperty(RECEIVER_QUIESCE_TIMEOUT, 300);
+
+ final AMQPFederatedBrokerConnectionElement element = new AMQPFederatedBrokerConnectionElement();
+ element.setName(getTestName());
+ element.addLocalAddressPolicy(receiveFromAddress);
+
+ final AMQPBrokerConnectConfiguration amqpConnection =
+ new AMQPBrokerConnectConfiguration(getTestName(), "tcp://" + remoteURI.getHost() + ":" + remoteURI.getPort());
+ amqpConnection.setReconnectAttempts(0);// No reconnects
+ amqpConnection.addElement(element);
+
+ server.getConfiguration().addAMQPConnection(amqpConnection);
+ server.start();
+ server.addAddressInfo(new AddressInfo(SimpleString.of(getTestName()), RoutingType.MULTICAST));
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_ADDRESS_RECEIVER.toString())
+ .withName(allOf(containsString(getTestName()),
+ containsString("address-receiver"),
+ containsString(server.getNodeID().toString())))
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(1000);
+
+ final ConnectionFactory factory = CFUtil.createConnectionFactory("AMQP", "tcp://localhost:" + AMQP_PORT);
+
+ try (Connection connection = factory.createConnection()) {
+ final Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
+ final MessageConsumer consumer = session.createConsumer(session.createTopic(getTestName()));
+
+ connection.start();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ // Demand is removed so expect a drain, don't respond then add new consumer to add
+ // demand that must wait on drain to timeout and recover.
+ peer.expectFlow().withLinkCredit(1000).withDrain(true);
+
+ consumer.close();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_ADDRESS_RECEIVER.toString())
+ .withName(allOf(containsString(getTestName()),
+ containsString("address-receiver"),
+ containsString(server.getNodeID().toString())))
+ .respondInKind();
+ peer.expectDetach().respond();
+ peer.expectFlow().withLinkCredit(1000);
+
+ session.createConsumer(session.createTopic(getTestName()));
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+
+ // Demand goes away and the federation link is closed.
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(2000).withDrain(true);
+ peer.expectDetach().respond();
+ }
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.close();
+ }
+ }
+
+ @Test
+ @Timeout(20)
+ public void testFederationResourceDeletedBeforeLinkQuiesceCompletes() throws Exception {
+ try (ProtonTestServer peer = new ProtonTestServer()) {
+ peer.expectSASLAnonymousConnect();
+ peer.expectOpen().respond();
+ peer.expectBegin().respond();
+ peer.expectAttach().ofSender()
+ .withDesiredCapability(FEDERATION_CONTROL_LINK.toString())
+ .respondInKind();
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_EVENT_LINK.toString())
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(10);
+ peer.start();
+
+ final URI remoteURI = peer.getServerURI();
+ logger.info("Connect test started, peer listening on: {}", remoteURI);
+
+ final AMQPFederationAddressPolicyElement receiveFromAddress = new AMQPFederationAddressPolicyElement();
+ receiveFromAddress.setName("address-policy");
+ receiveFromAddress.addToIncludes(getTestName());
+
+ final AMQPFederatedBrokerConnectionElement element = new AMQPFederatedBrokerConnectionElement();
+ element.setName(getTestName());
+ element.addLocalAddressPolicy(receiveFromAddress);
+
+ final AMQPBrokerConnectConfiguration amqpConnection =
+ new AMQPBrokerConnectConfiguration(getTestName(), "tcp://" + remoteURI.getHost() + ":" + remoteURI.getPort());
+ amqpConnection.setReconnectAttempts(0);// No reconnects
+ amqpConnection.addElement(element);
+
+ server.getConfiguration().addAMQPConnection(amqpConnection);
+ server.start();
+ server.addAddressInfo(new AddressInfo(SimpleString.of(getTestName()), RoutingType.MULTICAST));
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_ADDRESS_RECEIVER.toString())
+ .withName(allOf(containsString(getTestName()),
+ containsString("address-receiver"),
+ containsString(server.getNodeID().toString())))
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(1000);
+
+ final ConnectionFactory factory = CFUtil.createConnectionFactory("AMQP", "tcp://localhost:" + AMQP_PORT);
+
+ try (Connection connection = factory.createConnection()) {
+ final Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
+ session.createConsumer(session.createTopic(getTestName()));
+
+ connection.start();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true); // No answer to allow for race of answer plus delete
+ }
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectDetach().respond();
+
+ // Now answer the drain request and then immediately remove the resource.
+ peer.remoteFlow().withLinkCredit(0).withDeliveryCount(1000).withDrain(true).now();
+
+ server.removeAddressInfo(SimpleString.of(getTestName()), null);
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.close();
+ }
+ }
+
private static void sendAddressAddedEvent(ProtonTestPeer peer, String address, int handle, int deliveryId) {
final Map<String, Object> eventMap = new LinkedHashMap<>();
eventMap.put(REQUESTED_ADDRESS_NAME, address);
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationConfigurationReloadTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationConfigurationReloadTest.java
index 03319f3afe..db6331f4da 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationConfigurationReloadTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationConfigurationReloadTest.java
@@ -182,6 +182,9 @@ public class AMQPFederationConfigurationReloadTest extends AmqpClientTestSupport
connection.start();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
consumer.close();
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationConnectTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationConnectTest.java
index e7d37343c5..fa537a5c55 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationConnectTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationConnectTest.java
@@ -43,6 +43,7 @@ import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPF
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.QUEUE_PRIORITY_ADJUSTMENT;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_CREDITS;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_CREDITS_LOW;
+import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_QUIESCE_TIMEOUT;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.IGNORE_QUEUE_CONSUMER_FILTERS;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.IGNORE_QUEUE_CONSUMER_PRIORITIES;
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -234,6 +235,7 @@ public class AMQPFederationConnectTest extends AmqpClientTestSupport {
final int AMQP_MIN_LARGE_MESSAGE_SIZE = 10_000;
final int AMQP_CREDITS = 100;
final int AMQP_CREDITS_LOW = 50;
+ final int AMQP_RECEIVER_QUIESCE_TIMEOUT = 10_000;
final int AMQP_PULL_CREDITS_BATCH = 50;
final int AMQP_LINK_ATTACH_TIMEOUT = 60;
final boolean AMQP_TUNNEL_CORE_MESSAGES = false;
@@ -243,6 +245,7 @@ public class AMQPFederationConnectTest extends AmqpClientTestSupport {
final Map<String, Object> federationConfiguration = new HashMap<>();
federationConfiguration.put(RECEIVER_CREDITS, AMQP_CREDITS);
federationConfiguration.put(RECEIVER_CREDITS_LOW, AMQP_CREDITS_LOW);
+ federationConfiguration.put(RECEIVER_QUIESCE_TIMEOUT, AMQP_RECEIVER_QUIESCE_TIMEOUT);
federationConfiguration.put(PULL_RECEIVER_BATCH_SIZE, AMQP_PULL_CREDITS_BATCH);
federationConfiguration.put(LARGE_MESSAGE_THRESHOLD, AMQP_MIN_LARGE_MESSAGE_SIZE);
federationConfiguration.put(LINK_ATTACH_TIMEOUT, AMQP_LINK_ATTACH_TIMEOUT);
@@ -281,6 +284,7 @@ public class AMQPFederationConnectTest extends AmqpClientTestSupport {
federation.addProperty(LINK_ATTACH_TIMEOUT, AMQP_LINK_ATTACH_TIMEOUT);
federation.addProperty(AmqpSupport.TUNNEL_CORE_MESSAGES, Boolean.toString(AMQP_TUNNEL_CORE_MESSAGES));
federation.addProperty(PULL_RECEIVER_BATCH_SIZE, AMQP_PULL_CREDITS_BATCH);
+ federation.addProperty(RECEIVER_QUIESCE_TIMEOUT, AMQP_RECEIVER_QUIESCE_TIMEOUT);
amqpConnection.addElement(federation);
server.getConfiguration().addAMQPConnection(amqpConnection);
server.start();
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationManagementTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationManagementTest.java
index bf83af73ad..204c6e383c 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationManagementTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationManagementTest.java
@@ -358,6 +358,9 @@ class AMQPFederationManagementTest extends AmqpClientTestSupport {
assertEquals(1, addressPolicyControl.getMessagesReceived());
assertEquals(1, consumerControl.getMessagesReceived());
+ peer.expectFlow().withLinkCredit(999).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond(); // demand will be gone and receiver link should close.
}
@@ -410,6 +413,9 @@ class AMQPFederationManagementTest extends AmqpClientTestSupport {
assertEquals(2, addressPolicyControl.getMessagesReceived());
assertEquals(1, consumerControl.getMessagesReceived());
+ peer.expectFlow().withLinkCredit(999).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond(); // demand will be gone and receiver link should close.
}
@@ -497,6 +503,9 @@ class AMQPFederationManagementTest extends AmqpClientTestSupport {
assertEquals(1, queuePolicyControl.getMessagesReceived());
assertEquals(1, consumerControl.getMessagesReceived());
+ peer.expectFlow().withLinkCredit(999).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond(); // demand will be gone and receiver link should close.
}
@@ -549,6 +558,9 @@ class AMQPFederationManagementTest extends AmqpClientTestSupport {
assertEquals(2, queuePolicyControl.getMessagesReceived());
assertEquals(1, consumerControl.getMessagesReceived());
+ peer.expectFlow().withLinkCredit(999).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond(); // demand will be gone and receiver link should close.
}
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationQueuePolicyTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationQueuePolicyTest.java
index f7462c7783..66dad900bf 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationQueuePolicyTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationQueuePolicyTest.java
@@ -35,6 +35,7 @@ import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPF
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.QUEUE_PRIORITY_ADJUSTMENT;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_CREDITS;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_CREDITS_LOW;
+import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_QUIESCE_TIMEOUT;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.REQUESTED_ADDRESS_NAME;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.REQUESTED_QUEUE_ADDED;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.REQUESTED_QUEUE_NAME;
@@ -44,6 +45,7 @@ import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPF
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.PULL_RECEIVER_BATCH_SIZE;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationPolicySupport.DEFAULT_QUEUE_RECEIVER_PRIORITY_ADJUSTMENT;
import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConfiguration.DEFAULT_PULL_CREDIT_BATCH_SIZE;
+import static org.apache.activemq.artemis.protocol.amqp.proton.AMQPTunneledMessageConstants.AMQP_TUNNELED_CORE_LARGE_MESSAGE_FORMAT;
import static org.apache.activemq.artemis.protocol.amqp.proton.AMQPTunneledMessageConstants.AMQP_TUNNELED_CORE_MESSAGE_FORMAT;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.allOf;
@@ -598,6 +600,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
connection.start();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
}
@@ -687,6 +692,10 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
connection.start();
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
}
@@ -753,6 +762,10 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
connection.start();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
+
peer.expectDetach().respond();
}
@@ -834,6 +847,12 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
connection.start();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true).afterDelay(2);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true).afterDelay(2);
peer.expectDetach().respond();
peer.expectDetach().respond();
}
@@ -900,6 +919,10 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
connection.start();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
+
peer.expectDetach().respond();
}
@@ -1053,6 +1076,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
session.createConsumer(session.createQueue("test.queue"));
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
}
@@ -1146,6 +1172,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
session.createConsumer(session.createQueue("test.queue"));
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
}
@@ -1242,6 +1271,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
peer.remoteDetach().withErrorCondition("amqp:internal-error", "the resource suffered an internal error").afterDelay(15).now();
peer.waitForScriptToComplete(50, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach(); // demand will be gone and receiver link should close.
}
@@ -1319,6 +1351,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
assertEquals("test-message", ((TextMessage) message).getText());
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(999).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach(); // demand will be gone and receiver link should close.
}
@@ -1586,6 +1621,15 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
connection.start();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true).afterDelay(2);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true).afterDelay(2);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true).afterDelay(2);
peer.expectDetach().respond();
peer.expectDetach().respond();
peer.expectDetach().respond();
@@ -1726,6 +1770,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
assertEquals("test-message", ((TextMessage) message).getText());
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(999).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach(); // demand will be gone and receiver link should close.
}
@@ -1790,6 +1837,10 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
assertEquals("test-message", ((TextMessage) message).getText());
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(10).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(11).withDrain(true);
+
peer.expectDetach(); // demand will be gone and receiver link should close.
}
@@ -1859,6 +1910,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
assertEquals("test-message", ((TextMessage) message).getText());
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(40).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(41).withDrain(true);
peer.expectDetach(); // demand will be gone and receiver link should close.
}
@@ -1933,6 +1987,10 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
assertEquals("value2", message.getStringProperty("key2"));
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(999).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true).afterDelay(2);
+
peer.expectDetach(); // demand will be gone and receiver link should close.
}
@@ -2512,6 +2570,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
// should normally never be deleted by user management but if it is the state needs to be
// rebuilt in order for proper demand tracking.
target.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ target.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
target.expectDetach().respond();
client.expectDetach();
@@ -2603,6 +2664,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
assertEquals("two", message.getStringProperty("appProperty2"));
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(999).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true).afterDelay(2);
peer.expectDetach(); // demand will be gone and receiver link should close.
}
@@ -2901,7 +2965,7 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
// which should leave the queue with a pending message so no credit
// should be offered.
for (int i = 0; i < expected - 1; ++i) {
- assertNotNull(consumer.receiveNoWait());
+ assertNotNull(consumer.receive(100));
}
// We should not get a new batch yet as there is still one pending
@@ -2913,6 +2977,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
assertNotNull(consumer.receiveNoWait());
peer.waitForScriptToComplete(20, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(expected).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(expected + expected).withDrain(true).afterDelay(2);
peer.expectDetach().respond();
consumer.close(); // Remove local demand and federation consumer is torn down.
@@ -3019,15 +3086,15 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
private void doTestCoreLargeMessageHandlingBasedOnTunnelingState(boolean tunneling) throws Exception {
server.start();
server.createQueue(QueueConfiguration.of("test").setRoutingType(RoutingType.ANYCAST)
- .setAddress("test")
- .setAutoCreated(false));
+ .setAddress("test")
+ .setAutoCreated(false));
final String[] receiverOfferedCapabilities;
final int messageFormat;
if (tunneling) {
receiverOfferedCapabilities = new String[] {AmqpSupport.CORE_MESSAGE_TUNNELING_SUPPORT.toString()};
- messageFormat = AMQP_TUNNELED_CORE_MESSAGE_FORMAT;
+ messageFormat = AMQP_TUNNELED_CORE_LARGE_MESSAGE_FORMAT;
} else {
receiverOfferedCapabilities = null;
messageFormat = 0;
@@ -3064,7 +3131,8 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
peer.expectTransfer().withNonNullPayload()
.withMessageFormat(messageFormat).accept();
- final ConnectionFactory factory = CFUtil.createConnectionFactory("CORE", "tcp://localhost:" + AMQP_PORT);
+ final ConnectionFactory factory = CFUtil.createConnectionFactory(
+ "CORE", "tcp://localhost:" + AMQP_PORT + "?minLargeMessageSize=512");
try (Connection connection = factory.createConnection()) {
final Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
@@ -3170,6 +3238,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
.withOfferedCapabilities(FEDERATION_QUEUE_RECEIVER.toString());
peer.expectFlow().withLinkCredit(1000);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
// Add more demand after the broker connection starts
@@ -3742,6 +3813,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
consumer2.close();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
// Demand should be gone now
@@ -3833,6 +3907,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
consumer2.close();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
// Demand should be gone now
@@ -4044,6 +4121,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
.respond()
.withOfferedCapabilities(FEDERATION_QUEUE_RECEIVER.toString());
peer.expectFlow().withLinkCredit(1000);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond().afterDelay(40); // Defer the detach response for a bit
server.createQueue(QueueConfiguration.of(getTestName()).setRoutingType(RoutingType.ANYCAST)
@@ -4072,6 +4152,9 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
.respond()
.withOfferedCapabilities(FEDERATION_QUEUE_RECEIVER.toString());
peer.expectFlow().withLinkCredit(1000);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(1000).withDrain(true);
peer.expectDetach().respond();
try (Connection connection = factory.createConnection()) {
@@ -4091,6 +4174,338 @@ public class AMQPFederationQueuePolicyTest extends AmqpClientTestSupport {
}
}
+ @Test
+ @Timeout(20)
+ public void testFederationLinksRestartedBeforeRemoteFinishesDraining() throws Exception {
+ try (ProtonTestServer peer = new ProtonTestServer()) {
+ peer.expectSASLAnonymousConnect();
+ peer.expectOpen().respond();
+ peer.expectBegin().respond();
+ peer.expectAttach().ofSender()
+ .withDesiredCapability(FEDERATION_CONTROL_LINK.toString())
+ .respondInKind();
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_EVENT_LINK.toString())
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(10);
+ peer.start();
+
+ final URI remoteURI = peer.getServerURI();
+ logger.info("Connect test started, peer listening on: {}", remoteURI);
+
+ final AMQPFederationQueuePolicyElement receiveFromQueue = new AMQPFederationQueuePolicyElement();
+ receiveFromQueue.setName("queue-policy");
+ receiveFromQueue.addToIncludes(getTestName(), getTestName());
+
+ final AMQPFederatedBrokerConnectionElement element = new AMQPFederatedBrokerConnectionElement();
+ element.setName(getTestName());
+ element.addLocalQueuePolicy(receiveFromQueue);
+
+ final AMQPBrokerConnectConfiguration amqpConnection =
+ new AMQPBrokerConnectConfiguration(getTestName(), "tcp://" + remoteURI.getHost() + ":" + remoteURI.getPort());
+ amqpConnection.setReconnectAttempts(0);// No reconnects
+ amqpConnection.addElement(element);
+
+ server.getConfiguration().addAMQPConnection(amqpConnection);
+ server.start();
+ server.createQueue(QueueConfiguration.of(getTestName()).setRoutingType(RoutingType.ANYCAST)
+ .setAddress(getTestName())
+ .setAutoCreated(false));
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_QUEUE_RECEIVER.toString())
+ .withName(allOf(containsString(getTestName()),
+ containsString("queue-receiver"),
+ containsString(server.getNodeID().toString())))
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(1000);
+
+ final ConnectionFactory factory = CFUtil.createConnectionFactory("AMQP", "tcp://localhost:" + AMQP_PORT);
+
+ try (Connection connection = factory.createConnection()) {
+ final Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
+ session.createConsumer(session.createQueue(getTestName()));
+
+ connection.start();
+
+ peer.expectFlow().withLinkCredit(1000).withDrain(true); // Don't answer drained
+ }
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+
+ try (Connection connection = factory.createConnection()) {
+ final Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
+ session.createConsumer(session.createQueue(getTestName()));
+
+ connection.start();
+
+ // This should result from the answer to the next flow that drains the link.
+ peer.expectFlow().withLinkCredit(1000);
+
+ // Now answer the drained and expect that the queue policy manager will then
+ // restart flow of credit based on new demand as expected above.
+ peer.remoteFlow().withLinkCredit(0).withDeliveryCount(1000).withDrain(true).now();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(2000).withDrain(true);
+ peer.expectDetach().respond();
+ }
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.close();
+ }
+ }
+
+ @Test
+ @Timeout(20)
+ public void testFederationLinksDetachesAfterLinkQuiesceTimeout() throws Exception {
+ try (ProtonTestServer peer = new ProtonTestServer()) {
+ peer.expectSASLAnonymousConnect();
+ peer.expectOpen().respond();
+ peer.expectBegin().respond();
+ peer.expectAttach().ofSender()
+ .withDesiredCapability(FEDERATION_CONTROL_LINK.toString())
+ .respondInKind();
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_EVENT_LINK.toString())
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(10);
+ peer.start();
+
+ final URI remoteURI = peer.getServerURI();
+ logger.info("Connect test started, peer listening on: {}", remoteURI);
+
+ final AMQPFederationQueuePolicyElement receiveFromQueue = new AMQPFederationQueuePolicyElement();
+ receiveFromQueue.setName("queue-policy");
+ receiveFromQueue.addToIncludes(getTestName(), getTestName());
+ receiveFromQueue.addProperty(RECEIVER_QUIESCE_TIMEOUT, 20);
+
+ final AMQPFederatedBrokerConnectionElement element = new AMQPFederatedBrokerConnectionElement();
+ element.setName(getTestName());
+ element.addLocalQueuePolicy(receiveFromQueue);
+
+ final AMQPBrokerConnectConfiguration amqpConnection =
+ new AMQPBrokerConnectConfiguration(getTestName(), "tcp://" + remoteURI.getHost() + ":" + remoteURI.getPort());
+ amqpConnection.setReconnectAttempts(0);// No reconnects
+ amqpConnection.addElement(element);
+
+ server.getConfiguration().addAMQPConnection(amqpConnection);
+ server.start();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_QUEUE_RECEIVER.toString())
+ .withName(allOf(containsString(getTestName()),
+ containsString("queue-receiver"),
+ containsString(server.getNodeID().toString())))
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(1000);
+
+ final ConnectionFactory factory = CFUtil.createConnectionFactory("AMQP", "tcp://localhost:" + AMQP_PORT);
+
+ try (Connection connection = factory.createConnection()) {
+ final Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
+ session.createConsumer(session.createQueue(getTestName()));
+
+ connection.start();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true); // Don't answer drained then wait for the
+ peer.expectDetach().respond(); // timeout to see the link is detached.
+ }
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_QUEUE_RECEIVER.toString())
+ .withName(allOf(containsString(getTestName()),
+ containsString("queue-receiver"),
+ containsString(server.getNodeID().toString())))
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(1000);
+
+ // New demand should create a new federation consumer after the last drain timed out and closed the link
+ try (Connection connection = factory.createConnection()) {
+ final Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
+ session.createConsumer(session.createQueue(getTestName()));
+
+ connection.start();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(2000).withDrain(true);
+ peer.expectDetach().respond();
+ }
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.close();
+ }
+ }
+
+ @Test
+ @Timeout(20)
+ public void testFederationLinksRecoveredAfterLinkQuiesceTimeoutWithRenewedDemand() throws Exception {
+ try (ProtonTestServer peer = new ProtonTestServer()) {
+ peer.expectSASLAnonymousConnect();
+ peer.expectOpen().respond();
+ peer.expectBegin().respond();
+ peer.expectAttach().ofSender()
+ .withDesiredCapability(FEDERATION_CONTROL_LINK.toString())
+ .respondInKind();
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_EVENT_LINK.toString())
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(10);
+ peer.start();
+
+ final URI remoteURI = peer.getServerURI();
+ logger.info("Connect test started, peer listening on: {}", remoteURI);
+
+ final AMQPFederationQueuePolicyElement receiveFromQueue = new AMQPFederationQueuePolicyElement();
+ receiveFromQueue.setName("queue-policy");
+ receiveFromQueue.addToIncludes(getTestName(), getTestName());
+ receiveFromQueue.addProperty(RECEIVER_QUIESCE_TIMEOUT, 300);
+
+ final AMQPFederatedBrokerConnectionElement element = new AMQPFederatedBrokerConnectionElement();
+ element.setName(getTestName());
+ element.addLocalQueuePolicy(receiveFromQueue);
+
+ final AMQPBrokerConnectConfiguration amqpConnection =
+ new AMQPBrokerConnectConfiguration(getTestName(), "tcp://" + remoteURI.getHost() + ":" + remoteURI.getPort());
+ amqpConnection.setReconnectAttempts(0);// No reconnects
+ amqpConnection.addElement(element);
+
+ server.getConfiguration().addAMQPConnection(amqpConnection);
+ server.start();
+ server.createQueue(QueueConfiguration.of(getTestName()).setRoutingType(RoutingType.ANYCAST)
+ .setAddress(getTestName())
+ .setAutoCreated(false));
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_QUEUE_RECEIVER.toString())
+ .withName(allOf(containsString(getTestName()),
+ containsString("queue-receiver"),
+ containsString(server.getNodeID().toString())))
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(1000);
+
+ final ConnectionFactory factory = CFUtil.createConnectionFactory("AMQP", "tcp://localhost:" + AMQP_PORT);
+
+ try (Connection connection = factory.createConnection()) {
+ final Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
+ final MessageConsumer consumer = session.createConsumer(session.createQueue(getTestName()));
+
+ connection.start();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ // Demand is removed so expect a drain, don't respond then add new consumer to add
+ // demand that must wait on drain to timeout and recover.
+ peer.expectFlow().withLinkCredit(1000).withDrain(true);
+
+ consumer.close();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_QUEUE_RECEIVER.toString())
+ .withName(allOf(containsString(getTestName()),
+ containsString("queue-receiver"),
+ containsString(server.getNodeID().toString())))
+ .respondInKind();
+ peer.expectDetach().respond();
+ peer.expectFlow().withLinkCredit(1000);
+
+ session.createConsumer(session.createQueue(getTestName()));
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+
+ // Demand goes away and the federation link is closed.
+ peer.expectFlow().withLinkCredit(1000).withDrain(true)
+ .respond()
+ .withLinkCredit(0).withDeliveryCount(2000).withDrain(true);
+ peer.expectDetach().respond();
+ }
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.close();
+ }
+ }
+
+ @Test
+ @Timeout(20)
+ public void testFederationResourceDeletedBeforeLinkQuiesceCompletes() throws Exception {
+ try (ProtonTestServer peer = new ProtonTestServer()) {
+ peer.expectSASLAnonymousConnect();
+ peer.expectOpen().respond();
+ peer.expectBegin().respond();
+ peer.expectAttach().ofSender()
+ .withDesiredCapability(FEDERATION_CONTROL_LINK.toString())
+ .respondInKind();
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_EVENT_LINK.toString())
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(10);
+ peer.start();
+
+ final URI remoteURI = peer.getServerURI();
+ logger.info("Connect test started, peer listening on: {}", remoteURI);
+
+ final AMQPFederationQueuePolicyElement receiveFromQueue = new AMQPFederationQueuePolicyElement();
+ receiveFromQueue.setName("queue-policy");
+ receiveFromQueue.addToIncludes(getTestName(), getTestName());
+
+ final AMQPFederatedBrokerConnectionElement element = new AMQPFederatedBrokerConnectionElement();
+ element.setName(getTestName());
+ element.addLocalQueuePolicy(receiveFromQueue);
+
+ final AMQPBrokerConnectConfiguration amqpConnection =
+ new AMQPBrokerConnectConfiguration(getTestName(), "tcp://" + remoteURI.getHost() + ":" + remoteURI.getPort());
+ amqpConnection.setReconnectAttempts(0);// No reconnects
+ amqpConnection.addElement(element);
+
+ server.getConfiguration().addAMQPConnection(amqpConnection);
+ server.start();
+ server.createQueue(QueueConfiguration.of(getTestName()).setRoutingType(RoutingType.ANYCAST)
+ .setAddress(getTestName())
+ .setAutoCreated(false));
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectAttach().ofReceiver()
+ .withDesiredCapability(FEDERATION_QUEUE_RECEIVER.toString())
+ .withName(allOf(containsString(getTestName()),
+ containsString("queue-receiver"),
+ containsString(server.getNodeID().toString())))
+ .respondInKind();
+ peer.expectFlow().withLinkCredit(1000);
+
+ final ConnectionFactory factory = CFUtil.createConnectionFactory("AMQP", "tcp://localhost:" + AMQP_PORT);
+
+ try (Connection connection = factory.createConnection()) {
+ final Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE);
+ session.createConsumer(session.createQueue(getTestName()));
+
+ connection.start();
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectFlow().withLinkCredit(1000).withDrain(true); // No answer to allow for race of answer plus delete
+ }
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.expectDetach().respond();
+
+ // Now answer the drain request and then immediately remove the resource.
+ peer.remoteFlow().withLinkCredit(0).withDeliveryCount(1000).withDrain(true).now();
+
+ server.destroyQueue(SimpleString.of(getTestName()));
+
+ peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+ peer.close();
+ }
+ }
+
private static void sendQueueAddedEvent(ProtonTestPeer peer, String address, String queue, int handle, int deliveryId) {
final Map<String, Object> eventMap = new LinkedHashMap<>();
eventMap.put(REQUESTED_ADDRESS_NAME, address);
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationServerToServerTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationServerToServerTest.java
index 8bfaf45c84..1fedf81192 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationServerToServerTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/amqp/connect/AMQPFederationServerToServerTest.java
@@ -17,6 +17,8 @@
package org.apache.activemq.artemis.tests.integration.amqp.connect;
+import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.PULL_RECEIVER_BATCH_SIZE;
+import static org.apache.activemq.artemis.protocol.amqp.connect.federation.AMQPFederationConstants.RECEIVER_CREDITS;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -24,7 +26,9 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.invoke.MethodHandles;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.List;
import java.util.Map;
import javax.jms.BytesMessage;
@@ -54,7 +58,9 @@ import org.apache.activemq.artemis.protocol.amqp.proton.AmqpSupport;
import org.apache.activemq.artemis.tests.integration.amqp.AmqpClientTestSupport;
import org.apache.activemq.artemis.tests.util.CFUtil;
import org.apache.activemq.artemis.utils.Wait;
+import org.apache.qpid.jms.JmsConnectionFactory;
import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.slf4j.Logger;
@@ -1383,5 +1389,102 @@ public class AMQPFederationServerToServerTest extends AmqpClientTestSupport {
assertNull(consumerR.receiveNoWait());
}
}
-}
+ @RepeatedTest(1)
+ @Timeout(20)
+ public void testQueueConsumerPullsMessagesAndRemovesDemandLeavingSomeOnRemoteAMQP() throws Exception {
+ doTestQueueConsumerPullsMessagesAndRemovesDemandLeavingSomeOnRemote("AMQP", 0);
+ }
+
+ @RepeatedTest(1)
+ @Timeout(20)
+ public void testQueueConsumerPullsMessagesAndRemovesDemandLeavingSomeOnRemoteAMQPWithPrefetch() throws Exception {
+ doTestQueueConsumerPullsMessagesAndRemovesDemandLeavingSomeOnRemote("AMQP", 1);
+ }
+
+ private void doTestQueueConsumerPullsMessagesAndRemovesDemandLeavingSomeOnRemote(String clientProtocol, int prefetch) throws Exception {
+ logger.info("Test started: {}", getTestName());
+
+ final AMQPFederationQueuePolicyElement localQueuePolicy = new AMQPFederationQueuePolicyElement();
+ localQueuePolicy.setName("test-policy");
+ localQueuePolicy.addToIncludes(getTestName(), getTestName());
+ localQueuePolicy.addProperty(RECEIVER_CREDITS, 0); // Enable Pull mode
+ localQueuePolicy.addProperty(PULL_RECEIVER_BATCH_SIZE, 1); // Pull mode batch is one
+
+ final AMQPFederatedBrokerConnectionElement element = new AMQPFederatedBrokerConnectionElement();
+ element.setName(getTestName());
+ element.addLocalQueuePolicy(localQueuePolicy);
+
+ final AMQPBrokerConnectConfiguration amqpConnection =
+ new AMQPBrokerConnectConfiguration(getTestName(), "tcp://localhost:" + SERVER_PORT_REMOTE);
+ amqpConnection.setReconnectAttempts(10);// Limit reconnects
+ amqpConnection.addElement(element);
+
+ server.getConfiguration().addAMQPConnection(amqpConnection);
+ remoteServer.start();
+ remoteServer.createQueue(QueueConfiguration.of(getTestName()).setRoutingType(RoutingType.ANYCAST)
+ .setAddress(getTestName())
+ .setAutoCreated(false));
+ server.start();
+
+ final int MESSAGE_COUNT = 20;
+ final int REMAINING_COUNT = MESSAGE_COUNT / 2;
+ final List<Message> messages = new ArrayList<>();
+
+ final JmsConnectionFactory factoryRemote = new JmsConnectionFactory("amqp://localhost:" + SERVER_PORT_REMOTE);
+
+ try (Connection connectionR = factoryRemote.createConnection();
+ Session sessionR = connectionR.createSession(Session.AUTO_ACKNOWLEDGE)) {
+
+ final Queue queue = sessionR.createQueue(getTestName());
+ final MessageProducer producer = sessionR.createProducer(queue);
+
+ for (int i = 0; i < MESSAGE_COUNT; ++i) {
+ TextMessage message = sessionR.createTextMessage("test-message:" + i);
+
+ message.setIntProperty("messageNo", i);
+
+ producer.send(message);
+ }
+ }
+
+ // Consume half the messages on the
+ final JmsConnectionFactory factoryLocal = new JmsConnectionFactory(
+ "amqp://localhost:" + SERVER_PORT + "?jms.prefetchPolicy.all=" + prefetch);
+
+ try (Connection connectionL = factoryLocal.createConnection();
+ Session sessionL = connectionL.createSession(Session.AUTO_ACKNOWLEDGE)) {
+
+ connectionL.start();
+
+ final Queue queue = sessionL.createQueue(getTestName());
+ final MessageConsumer consumer = sessionL.createConsumer(queue);
+
+ for (int i = 0; i < REMAINING_COUNT; ++i) {
+ Message received = consumer.receive(5_000);
+ assertNotNull(received);
+ messages.add(received);
+
+ logger.info("Read new message #{} federated from remote: ", i);
+ }
+
+ consumer.close();
+ }
+
+ assertEquals(REMAINING_COUNT, messages.size());
+
+ Wait.assertTrue(() -> server.queueQuery(SimpleString.of(getTestName())).isExists(), 10_000);
+ Wait.assertTrue(() -> remoteServer.queueQuery(SimpleString.of(getTestName())).isExists(), 10_000);
+
+ final org.apache.activemq.artemis.core.server.Queue queueL = server.locateQueue(getTestName());
+ final org.apache.activemq.artemis.core.server.Queue queueR = remoteServer.locateQueue(getTestName());
+
+ assertNotNull(queueL);
+ assertNotNull(queueR);
+
+ // Will tell you counts are off which indicates a message was left on the remote meaning it
+ // is now a duplicate and will be federated in error if this consumer attached again.
+ final long pendingMessages = queueL.getMessageCount() + queueR.getMessageCount();
+ assertEquals(REMAINING_COUNT, pendingMessages);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@activemq.apache.org
For additional commands, e-mail: commits-help@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact