[ 
https://issues.apache.org/jira/browse/ARTEMIS-2937?focusedWorklogId=505804&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-505804
 ]

ASF GitHub Bot logged work on ARTEMIS-2937:
-------------------------------------------

                Author: ASF GitHub Bot
            Created on: 28/Oct/20 16:44
            Start Date: 28/Oct/20 16:44
    Worklog Time Spent: 10m 
      Work Description: gemmellr commented on a change in pull request #3316:
URL: https://github.com/apache/activemq-artemis/pull/3316#discussion_r513599482



##########
File path: 
artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/connect/AMQPBrokerConnection.java
##########
@@ -0,0 +1,556 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Stream;
+
+import org.apache.activemq.artemis.api.core.ActiveMQException;
+import org.apache.activemq.artemis.api.core.QueueConfiguration;
+import org.apache.activemq.artemis.api.core.RoutingType;
+import org.apache.activemq.artemis.api.core.SimpleString;
+import org.apache.activemq.artemis.api.core.TransportConfiguration;
+import 
org.apache.activemq.artemis.core.config.amqpBrokerConnectivity.AMQPBrokerConnectConfiguration;
+import 
org.apache.activemq.artemis.core.config.amqpBrokerConnectivity.AMQPBrokerConnectionAddressType;
+import 
org.apache.activemq.artemis.core.config.amqpBrokerConnectivity.AMQPBrokerConnectionElement;
+import 
org.apache.activemq.artemis.core.config.amqpBrokerConnectivity.AMQPMirrorBrokerConnectionElement;
+import org.apache.activemq.artemis.core.postoffice.Binding;
+import org.apache.activemq.artemis.core.postoffice.QueueBinding;
+import org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection;
+import org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector;
+import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants;
+import org.apache.activemq.artemis.core.server.ActiveMQComponent;
+import org.apache.activemq.artemis.core.server.ActiveMQServer;
+import org.apache.activemq.artemis.core.server.BrokerConnection;
+import org.apache.activemq.artemis.core.server.Consumer;
+import org.apache.activemq.artemis.core.server.Queue;
+import org.apache.activemq.artemis.core.server.impl.AddressInfo;
+import org.apache.activemq.artemis.core.server.mirror.MirrorController;
+import 
org.apache.activemq.artemis.core.server.plugin.ActiveMQServerQueuePlugin;
+import org.apache.activemq.artemis.protocol.amqp.broker.AMQPSessionCallback;
+import 
org.apache.activemq.artemis.protocol.amqp.broker.ActiveMQProtonRemotingConnection;
+import org.apache.activemq.artemis.protocol.amqp.broker.ProtonProtocolManager;
+import 
org.apache.activemq.artemis.protocol.amqp.connect.mirror.AMQPMirrorControllerAggregation;
+import 
org.apache.activemq.artemis.protocol.amqp.connect.mirror.AMQPMirrorControllerSource;
+import 
org.apache.activemq.artemis.protocol.amqp.logger.ActiveMQAMQPProtocolLogger;
+import org.apache.activemq.artemis.protocol.amqp.proton.AMQPSessionContext;
+import 
org.apache.activemq.artemis.protocol.amqp.proton.ProtonServerSenderContext;
+import org.apache.activemq.artemis.protocol.amqp.proton.SenderController;
+import org.apache.activemq.artemis.protocol.amqp.sasl.ClientSASL;
+import org.apache.activemq.artemis.protocol.amqp.sasl.ClientSASLFactory;
+import org.apache.activemq.artemis.spi.core.protocol.ConnectionEntry;
+import 
org.apache.activemq.artemis.spi.core.remoting.ClientConnectionLifeCycleListener;
+import org.apache.activemq.artemis.spi.core.remoting.ClientProtocolManager;
+import org.apache.activemq.artemis.spi.core.remoting.Connection;
+import org.apache.activemq.artemis.utils.ConfigurationHelper;
+import org.apache.activemq.artemis.utils.UUIDGenerator;
+import org.apache.qpid.proton.amqp.Symbol;
+import org.apache.qpid.proton.amqp.messaging.Source;
+import org.apache.qpid.proton.amqp.messaging.Target;
+import org.apache.qpid.proton.amqp.transport.ReceiverSettleMode;
+import org.apache.qpid.proton.amqp.transport.SenderSettleMode;
+import org.apache.qpid.proton.engine.Receiver;
+import org.apache.qpid.proton.engine.Sender;
+import org.apache.qpid.proton.engine.Session;
+import org.jboss.logging.Logger;
+
+public class AMQPBrokerConnection implements 
ClientConnectionLifeCycleListener, ActiveMQServerQueuePlugin, BrokerConnection {
+
+   private static final Logger logger = 
Logger.getLogger(AMQPBrokerConnection.class);
+
+   private final AMQPBrokerConnectConfiguration brokerConnectConfiguration;
+   private final ProtonProtocolManager protonProtocolManager;
+   private final ActiveMQServer server;
+   private final NettyConnector bridgesConnector;
+   private NettyConnection connection;
+   private Session session;
+   private AMQPSessionContext sessionContext;
+   private ActiveMQProtonRemotingConnection protonRemotingConnection;
+   private volatile boolean started = false;
+   private final AMQPBrokerConnectionManager bridgeManager;
+   private int retryCounter = 0;
+   private volatile ScheduledFuture reconnectFuture;
+   private Set<Queue> senders = new HashSet<>();
+   private Set<Queue> receivers = new HashSet<>();
+
+   final Executor connectExecutor;
+   final ScheduledExecutorService scheduledExecutorService;
+
+   /** This is just for logging.
+    *  the actual connection will come from the amqpConnection configuration*/
+   String host;
+
+   /** This is just for logging.
+    *  the actual connection will come from the amqpConnection configuration*/
+   int port;
+
+   public AMQPBrokerConnection(AMQPBrokerConnectionManager bridgeManager, 
AMQPBrokerConnectConfiguration brokerConnectConfiguration,
+                               ProtonProtocolManager protonProtocolManager,
+                               ActiveMQServer server,
+                               NettyConnector bridgesConnector) {
+      this.bridgeManager = bridgeManager;
+      this.brokerConnectConfiguration = brokerConnectConfiguration;
+      this.protonProtocolManager = protonProtocolManager;
+      this.server = server;
+      this.bridgesConnector = bridgesConnector;
+      connectExecutor = server.getExecutorFactory().getExecutor();
+      scheduledExecutorService = server.getScheduledPool();
+   }
+
+   @Override
+   public String getName() {
+      return brokerConnectConfiguration.getName();
+   }
+
+   @Override
+   public String getProtocol() {
+      return "AMQP";
+   }
+
+   @Override
+   public boolean isStarted() {
+      return started;
+   }
+
+   @Override
+   public void stop() {
+      if (connection != null) {
+         connection.close();
+      }
+      ScheduledFuture scheduledFuture = reconnectFuture;
+      reconnectFuture = null;
+      if (scheduledFuture != null) {
+         scheduledFuture.cancel(true);
+      }
+      started = false;
+   }
+
+   @Override
+   public void start() throws Exception {
+      started = true;
+      server.getConfiguration().registerBrokerPlugin(this);
+      try {
+
+         for (AMQPBrokerConnectionElement connectionElement : 
brokerConnectConfiguration.getConnectionElements()) {
+            if (connectionElement.getType() == 
AMQPBrokerConnectionAddressType.MIRROR) {
+               
installMirrorController((AMQPMirrorBrokerConnectionElement)connectionElement, 
server);
+            }
+         }
+      } catch (Throwable e) {
+         logger.warn(e.getMessage(), e);
+         return;
+      }
+      connectExecutor.execute(() -> doConnect());
+   }
+
+   public NettyConnection getConnection() {
+      return connection;
+   }
+
+   @Override
+   public void afterCreateQueue(Queue queue) {
+      connectExecutor.execute(() -> {
+         for (AMQPBrokerConnectionElement connectionElement : 
brokerConnectConfiguration.getConnectionElements()) {
+            validateMatching(queue, connectionElement);
+         }
+      });
+   }
+
+   public void validateMatching(Queue queue, AMQPBrokerConnectionElement 
connectionElement) {
+      if (connectionElement.getType() != 
AMQPBrokerConnectionAddressType.MIRROR) {
+         if (connectionElement.getQueueName() != null) {
+            if (queue.getName().equals(connectionElement.getQueueName())) {
+               createLink(queue, connectionElement);
+            }
+         } else if (connectionElement.match(queue.getAddress(), 
server.getConfiguration().getWildcardConfiguration())) {
+            createLink(queue, connectionElement);
+         }
+      }
+   }
+
+   public void createLink(Queue queue, AMQPBrokerConnectionElement 
connectionElement) {
+      if (connectionElement.getType() == AMQPBrokerConnectionAddressType.PEER) 
{
+         connectSender(queue, queue.getAddress().toString(), 
Symbol.valueOf("qd.waypoint"));
+         connectReceiver(protonRemotingConnection, session, sessionContext, 
queue, Symbol.valueOf("qd.waypoint"));
+      } else {
+         if (connectionElement.getType() == 
AMQPBrokerConnectionAddressType.SENDER) {
+            connectSender(queue, queue.getAddress().toString());
+         }
+         if (connectionElement.getType() == 
AMQPBrokerConnectionAddressType.RECEIVER) {
+            connectReceiver(protonRemotingConnection, session, sessionContext, 
queue);
+         }
+      }
+   }
+
+   private void doConnect() {
+      try {
+         List<TransportConfiguration> configurationList = 
brokerConnectConfiguration.getTransportConfigurations();
+
+         TransportConfiguration tpConfig = configurationList.get(0);
+
+         String hostOnParameter = 
ConfigurationHelper.getStringProperty(TransportConstants.HOST_PROP_NAME, 
TransportConstants.DEFAULT_HOST, tpConfig.getParams());
+         int portOnParameter = 
ConfigurationHelper.getIntProperty(TransportConstants.PORT_PROP_NAME, 
TransportConstants.DEFAULT_PORT, tpConfig.getParams());
+         this.host = hostOnParameter;
+         this.port = portOnParameter;
+         connection = bridgesConnector.createConnection(null, hostOnParameter, 
portOnParameter);
+
+         if (connection == null) {
+            retryConnection();
+            return;
+         }
+
+         reconnectFuture = null;
+         retryCounter = 0;
+
+         // before we retry the connection we need to remove any previous links
+         // as they will need to be recreated
+         senders.clear();
+         receivers.clear();
+
+         ClientSASLFactory saslFactory = null;
+
+         if (brokerConnectConfiguration.getUser() != null && 
brokerConnectConfiguration.getPassword() != null) {
+            saslFactory = availableMechanims -> {
+               if (availableMechanims != null && 
Arrays.asList(availableMechanims).contains("PLAIN")) {
+                  return new 
PlainSASLMechanism(brokerConnectConfiguration.getUser(), 
brokerConnectConfiguration.getPassword());
+               } else {
+                  return null;
+               }
+            };
+         }
+
+         ConnectionEntry entry = 
protonProtocolManager.createOutgoingConnectionEntry(connection, saslFactory);
+         protonRemotingConnection = (ActiveMQProtonRemotingConnection) 
entry.connection;
+         connection.getChannel().pipeline().addLast(new 
AMQPBrokerConnectionChannelHandler(bridgesConnector.getChannelGroup(), 
protonRemotingConnection.getAmqpConnection().getHandler()));
+
+         protonRemotingConnection.getAmqpConnection().runLater(() -> {
+            protonRemotingConnection.getAmqpConnection().open();
+            protonRemotingConnection.getAmqpConnection().flush();
+         });
+
+         session = 
protonRemotingConnection.getAmqpConnection().getHandler().getConnection().session();
+         sessionContext = 
protonRemotingConnection.getAmqpConnection().getSessionExtension(session);
+         protonRemotingConnection.getAmqpConnection().runLater(() -> {
+            session.open();
+            protonRemotingConnection.getAmqpConnection().flush();
+         });
+
+         if (brokerConnectConfiguration.getConnectionElements() != null) {
+            Stream<Binding> bindingStream = 
server.getPostOffice().getAllBindings();
+
+            bindingStream.forEach(binding -> {
+               if (binding instanceof QueueBinding) {
+                  Queue queue = ((QueueBinding) binding).getQueue();
+                  for (AMQPBrokerConnectionElement connectionElement : 
brokerConnectConfiguration.getConnectionElements()) {
+                     validateMatching(queue, connectionElement);
+                  }
+               }
+            });
+
+            for (AMQPBrokerConnectionElement connectionElement : 
brokerConnectConfiguration.getConnectionElements()) {
+               if (connectionElement.getType() == 
AMQPBrokerConnectionAddressType.MIRROR) {
+                  AMQPMirrorBrokerConnectionElement replica = 
(AMQPMirrorBrokerConnectionElement)connectionElement;
+                  Queue queue = 
server.locateQueue(replica.getSourceMirrorAddress());
+
+                  connectSender(queue, ProtonProtocolManager.MIRROR_ADDRESS);
+               }
+            }
+         }
+
+         protonRemotingConnection.getAmqpConnection().flush();
+
+         bridgeManager.connected(connection, this);
+      } catch (Throwable e) {
+         error(e);
+      }
+   }
+
+   public void retryConnection() {
+      if (bridgeManager.isStarted() && started) {
+         if (brokerConnectConfiguration.getReconnectAttempts() < 0 || 
retryCounter < brokerConnectConfiguration.getReconnectAttempts()) {
+            retryCounter++;
+            
ActiveMQAMQPProtocolLogger.LOGGER.retryConnection(brokerConnectConfiguration.getName(),
 host, port, retryCounter, brokerConnectConfiguration.getReconnectAttempts());
+            if (logger.isDebugEnabled()) {
+               logger.debug("Reconnecting in " + 
brokerConnectConfiguration.getRetryInterval() + ", this is the " + retryCounter 
+ " of " + brokerConnectConfiguration.getReconnectAttempts());
+            }
+            reconnectFuture = scheduledExecutorService.schedule(() -> 
connectExecutor.execute(() -> doConnect()), 
brokerConnectConfiguration.getRetryInterval(), TimeUnit.MILLISECONDS);
+         } else {
+            
ActiveMQAMQPProtocolLogger.LOGGER.retryConnectionFailed(brokerConnectConfiguration.getName(),
 host, port, retryCounter, brokerConnectConfiguration.getReconnectAttempts());
+            if (logger.isDebugEnabled()) {
+               logger.debug("no more reconnections as the retry counter 
reached " + retryCounter + " out of " + 
brokerConnectConfiguration.getReconnectAttempts());
+            }
+         }
+      }
+   }
+
+   /** The reason this method is static is the following:
+    *
+    *  It is returning the snfQueue to the replica, and I needed isolation 
from the actual instance.
+    *  During development I had a mistake where I used a property from the 
Object,
+    *  so, I needed this isolation for my organization and making sure nothing 
would be shared. */
+   private static QueueBinding 
installMirrorController(AMQPMirrorBrokerConnectionElement replicaConfig, 
ActiveMQServer server) throws Exception {
+
+      AddressInfo addressInfo = 
server.getAddressInfo(replicaConfig.getSourceMirrorAddress());
+      if (addressInfo == null) {
+         addressInfo = new 
AddressInfo(replicaConfig.getSourceMirrorAddress()).addRoutingType(RoutingType.ANYCAST).setAutoCreated(false).setTemporary(!replicaConfig.isDurable());
+         server.addAddressInfo(addressInfo);
+      }
+
+      if (addressInfo.getRoutingType() != RoutingType.ANYCAST) {
+         throw new IllegalArgumentException("sourceMirrorAddress is not 
ANYCAST");
+      }
+
+      Queue mirrorControlQueue = 
server.locateQueue(replicaConfig.getSourceMirrorAddress());
+
+      if (mirrorControlQueue == null) {
+         mirrorControlQueue = server.createQueue(new 
QueueConfiguration(replicaConfig.getSourceMirrorAddress()).setAddress(replicaConfig.getSourceMirrorAddress()).setRoutingType(RoutingType.ANYCAST).setDurable(replicaConfig.isDurable()),
 true);
+      }
+
+      mirrorControlQueue.setMirrorController(true);
+
+      QueueBinding snfReplicaQueueBinding = 
(QueueBinding)server.getPostOffice().getBinding(replicaConfig.getSourceMirrorAddress());
+      if (snfReplicaQueueBinding == null) {
+         logger.warn("Queue does not exist even after creation! " + 
replicaConfig);
+         throw new IllegalAccessException("Cannot start replica");
+      }
+
+      Queue snfQueue = snfReplicaQueueBinding.getQueue();
+
+      if 
(!snfQueue.getAddress().equals(replicaConfig.getSourceMirrorAddress())) {
+         logger.warn("Queue " + snfQueue + " belong to a different address (" 
+ snfQueue.getAddress() + "), while we expected it to be " + 
addressInfo.getName());
+         throw new IllegalAccessException("Cannot start replica");
+      }
+
+      AMQPMirrorControllerSource newPartition = new 
AMQPMirrorControllerSource(snfQueue, server, 
replicaConfig.isMessageAcknowledgements());
+
+      server.scanAddresses(newPartition);
+
+      MirrorController currentMirrorController = server.getMirrorController();
+
+      if (currentMirrorController == null) {
+         server.installMirrorController(newPartition);
+      } else {
+         // Replace a standard implementation by an aggregated supporting 
multiple targets
+         if (currentMirrorController instanceof AMQPMirrorControllerSource) {
+            // replacing the simple mirror control for an aggregator
+            AMQPMirrorControllerAggregation remoteAggregation = new 
AMQPMirrorControllerAggregation();
+            remoteAggregation.addPartition((AMQPMirrorControllerSource) 
currentMirrorController);
+            currentMirrorController = remoteAggregation;
+            server.installMirrorController(remoteAggregation);
+         }
+         ((AMQPMirrorControllerAggregation) 
currentMirrorController).addPartition(newPartition);
+      }
+
+      return snfReplicaQueueBinding;
+   }
+
+   private void connectReceiver(ActiveMQProtonRemotingConnection 
protonRemotingConnection,
+                                Session session,
+                                AMQPSessionContext sessionContext,
+                                Queue queue,
+                                Symbol... capabilities) {
+      if (logger.isDebugEnabled()) {
+         logger.debug("Connecting inbound for " + queue);
+      }
+
+      if (session == null) {
+         logger.debug("session is null");
+         return;
+      }
+
+      protonRemotingConnection.getAmqpConnection().runLater(() -> {
+
+         if (receivers.contains(queue)) {
+            logger.debug("Receiver for queue " + queue + " already exists, 
just giving up");
+            return;
+         }
+         receivers.add(queue);
+         Receiver receiver = session.receiver(queue.getName().toString() + 
UUIDGenerator.getInstance().generateStringUUID());

Review comment:
       also, the link is named based on the queue name, but then the address 
name as the source address....I'd tend to expect the receiver link name to 
incorporate the source address




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 505804)
    Time Spent: 29h 10m  (was: 29h)

> AMQP Server Connectivity
> ------------------------
>
>                 Key: ARTEMIS-2937
>                 URL: https://issues.apache.org/jira/browse/ARTEMIS-2937
>             Project: ActiveMQ Artemis
>          Issue Type: New Feature
>          Components: AMQP
>            Reporter: Clebert Suconic
>            Assignee: Clebert Suconic
>            Priority: Major
>             Fix For: 2.16.0
>
>          Time Spent: 29h 10m
>  Remaining Estimate: 0h
>
> This feature adds server side connectivity.
>  
> It is possible to link two brokers directly using AMQP with this feature, and 
> have a Queue transferring messages to another broker directly. 
>  
> For this we would have options called <sender and <receiver
>  
>  
> it would also be possible to use qpid-dispatch as an intermediary between 
> clients and the brokers (or eventually between brokers), on that case the 
> option will be <peer
>  
> it would also be possible to use <mirror with a few option to replicate data 
> between two brokers, bringing the possibility of using it for Disaster & 
> Recovery and Failover.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to