You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by cl...@apache.org on 2022/08/10 23:29:37 UTC

[activemq-artemis] 01/02: ARTEMIS-3929 Improving OpenWire clientIDSet

This is an automated email from the ASF dual-hosted git repository.

clebertsuconic pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git

commit 568eb70fcda9737b38871a3bff1e111bad784091
Author: Clebert Suconic <cl...@apache.org>
AuthorDate: Sun Aug 7 22:20:45 2022 -0400

    ARTEMIS-3929 Improving OpenWire clientIDSet
    
    I have seen this contention while I was testing ARTEMIS-3928
    
    This does change any semantics and current tests should be enough to validate this change
---
 .../protocol/openwire/OpenWireProtocolManager.java | 68 ++++++++++------------
 1 file changed, 32 insertions(+), 36 deletions(-)

diff --git a/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireProtocolManager.java b/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireProtocolManager.java
index 8d26a8f197..1d645f7861 100644
--- a/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireProtocolManager.java
+++ b/artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireProtocolManager.java
@@ -106,7 +106,7 @@ public class OpenWireProtocolManager  extends AbstractProtocolManager<Command, O
 
    private final CopyOnWriteArrayList<OpenWireConnection> connections = new CopyOnWriteArrayList<>();
 
-   private final Map<String, AMQConnectionContext> clientIdSet = new HashMap<>();
+   private final Map<String, AMQConnectionContext> clientIdSet = new ConcurrentHashMap<>();
 
    private String brokerName;
 
@@ -226,18 +226,16 @@ public class OpenWireProtocolManager  extends AbstractProtocolManager<Command, O
    }
 
    public void removeConnection(ConnectionInfo info, Throwable error) throws InvalidClientIDException {
-      synchronized (clientIdSet) {
-         String clientId = info.getClientId();
-         if (clientId != null) {
-            AMQConnectionContext context = this.clientIdSet.remove(clientId);
-            if (context != null) {
-               //connection is still there and need to close
-               context.getConnection().disconnect(error != null);
-               this.connections.remove(context.getConnection());
-            }
-         } else {
-            throw new InvalidClientIDException("No clientID specified for connection disconnect request");
+      String clientId = info.getClientId();
+      if (clientId != null) {
+         AMQConnectionContext context = this.clientIdSet.remove(clientId);
+         if (context != null) {
+            //connection is still there and need to close
+            context.getConnection().disconnect(error != null);
+            this.connections.remove(context.getConnection());
          }
+      } else {
+         throw new InvalidClientIDException("No clientID specified for connection disconnect request");
       }
    }
 
@@ -404,35 +402,33 @@ public class OpenWireProtocolManager  extends AbstractProtocolManager<Command, O
          throw new InvalidClientIDException("No clientID specified for connection request");
       }
 
-      synchronized (clientIdSet) {
-         AMQConnectionContext context;
-         context = clientIdSet.get(clientId);
-         if (context != null) {
-            if (info.isFailoverReconnect()) {
-               OpenWireConnection oldConnection = context.getConnection();
-               oldConnection.disconnect(true);
-               connections.remove(oldConnection);
-               connection.reconnect(context, info);
-            } else {
-               throw new InvalidClientIDException("Broker: " + getBrokerName() + " - Client: " + clientId + " already connected from " + context.getConnection().getRemoteAddress());
-            }
+      AMQConnectionContext context;
+      context = clientIdSet.get(clientId);
+      if (context != null) {
+         if (info.isFailoverReconnect()) {
+            OpenWireConnection oldConnection = context.getConnection();
+            oldConnection.disconnect(true);
+            connections.remove(oldConnection);
+            connection.reconnect(context, info);
          } else {
-            //new connection
-            context = connection.initContext(info);
-            clientIdSet.put(clientId, context);
+            throw new InvalidClientIDException("Broker: " + getBrokerName() + " - Client: " + clientId + " already connected from " + context.getConnection().getRemoteAddress());
          }
+      } else {
+         //new connection
+         context = connection.initContext(info);
+         clientIdSet.put(clientId, context);
+      }
 
-         connections.add(connection);
+      connections.add(connection);
 
-         ActiveMQTopic topic = AdvisorySupport.getConnectionAdvisoryTopic();
-         // do not distribute passwords in advisory messages. usernames okay
-         ConnectionInfo copy = info.copy();
-         copy.setPassword("");
-         fireAdvisory(context, topic, copy);
+      ActiveMQTopic topic = AdvisorySupport.getConnectionAdvisoryTopic();
+      // do not distribute passwords in advisory messages. usernames okay
+      ConnectionInfo copy = info.copy();
+      copy.setPassword("");
+      fireAdvisory(context, topic, copy);
 
-         // init the conn
-         context.getConnection().addSessions(context.getConnectionState().getSessionIds());
-      }
+      // init the conn
+      context.getConnection().addSessions(context.getConnectionState().getSessionIds());
    }
 
    public void fireAdvisory(AMQConnectionContext context, ActiveMQTopic topic, Command copy) throws Exception {