RockteMQ-AI commented on code in PR #4761:
URL: 
https://github.com/apache/rocketmq-dashboard/pull/4761#discussion_r4062163863


##########
server/src/main/java/org/apache/rocketmq/studio/cluster/broker/ClientLease.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.rocketmq.studio.cluster.broker;
+
+import java.util.function.Consumer;
+
+/** Retires a cached client without shutting it down during an in-flight 
operation. */
+final class ClientLease<T> {
+
+    private final T client;
+    private final Consumer<T> shutdown;
+    private int active;
+    private boolean retired;
+    private boolean closed;
+
+    ClientLease(T client, Consumer<T> shutdown) {
+        this.client = client;
+        this.shutdown = shutdown;
+    }
+
+    T client() {
+        return client;
+    }
+
+    synchronized boolean acquire() {
+        if (retired) {
+            return false;
+        }
+        active++;
+        return true;
+    }
+
+    void release() {

Review Comment:
   The `release()` method calls `shutdown.accept(client)` outside the 
`synchronized` block, which is correct — it avoids holding the monitor during a 
potentially slow shutdown. Since `safeShutdown` catches all throwables, the 
lease will not be left in an inconsistent state even if shutdown throws. Good 
design.



##########
server/src/main/java/org/apache/rocketmq/studio/cluster/broker/MqAdminExtFactory.java:
##########
@@ -90,22 +95,46 @@ public <T> T execute(String namesrvAddr, RPCHook rpcHook, 
String authenticationI
         }
         AdminClientCacheKey cacheKey = new 
AdminClientCacheKey(normalizedNamesrvAddr,
                 normalizeAuthenticationIdentity(authenticationIdentity));
-        DefaultMQAdminExt admin = cache.computeIfAbsent(cacheKey,
-                key -> {
-                    // Re-check under the cache lock so a request that passed 
the initial closed check
-                    // cannot create a fresh connection while the factory is 
shutting down.
-                    if (closed) {
-                        throw new BusinessException(503, "Admin factory is 
shutting down");
-                    }
-                    return createAndStart(key.namesrvAddr(), rpcHook);
-                });
+        ClientLease<DefaultMQAdminExt> lease;
+        while (true) {
+            if (closed) {
+                throw new BusinessException(503, "Admin factory is shutting 
down");
+            }
+            lease = cache.computeIfAbsent(cacheKey, key -> {
+                // Re-check before client creation; admission re-checks again 
after creation.
+                if (closed) {
+                    throw new BusinessException(503, "Admin factory is 
shutting down");
+                }
+                return new ClientLease<>(

Review Comment:
   The `while (true)` retry loop correctly handles the race where a lease is 
retired between `computeIfAbsent` and `acquire()`. The conditional 
`cache.remove(cacheKey, lease)` ensures only the stale lease is removed. 
Consider adding a brief comment explaining why the loop is needed (e.g., "retry 
if the lease was retired between creation and acquisition") to help future 
readers.



##########
server/src/main/java/org/apache/rocketmq/studio/cluster/broker/MqAdminExtFactory.java:
##########
@@ -50,7 +54,8 @@ public class MqAdminExtFactory {
     /** Default admin RPC timeout in milliseconds. */
     private static final long DEFAULT_TIMEOUT_MILLIS = 5000L;
 
-    private final Map<AdminClientCacheKey, DefaultMQAdminExt> cache = new 
ConcurrentHashMap<>();
+    private final Map<AdminClientCacheKey, ClientLease<DefaultMQAdminExt>> 
cache = new ConcurrentHashMap<>();

Review Comment:
   Using `ReentrantReadWriteLock(fair=true)` prevents writer (shutdown) 
starvation at the cost of some throughput. For a dashboard admin tool with low 
contention, this is the right trade-off.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to