Copilot commented on code in PR #8020:
URL: https://github.com/apache/incubator-seata/pull/8020#discussion_r2986109199


##########
core/src/main/java/org/apache/seata/core/rpc/netty/NettyClientChannelManager.java:
##########
@@ -58,6 +58,8 @@ class NettyClientChannelManager {
 
     private final ConcurrentMap<String, Channel> channels = new 
ConcurrentHashMap<>();
 
+    private final ConcurrentMap<String, String> serverVersionMap = new 
ConcurrentHashMap<>();
+

Review Comment:
   `serverVersionMap` is maintained separately from `channels` but is only 
cleared in `clearServerVersions()`. When channels are destroyed/released (e.g., 
reconnects or server list changes), the version entries will remain and can 
become stale or leak over time. Consider removing the corresponding 
`serverAddress` entry whenever the channel is removed/destroyed, keeping the 
two maps in sync.



##########
core/src/main/java/org/apache/seata/core/rpc/netty/RmNettyRemotingClient.java:
##########
@@ -317,6 +364,13 @@ public String getMergedResourceKeys() {
 
     @Override
     public void destroy() {
+        if (resourceManager != null && 
StringUtils.isNotBlank(transactionServiceGroup)) {
+            String allResourceIds = getMergedResourceKeys();
+            if (StringUtils.isNotBlank(allResourceIds)) {
+                sendUnregisterToServers(allResourceIds);
+            }
+        }
+        getClientChannelManager().clearServerVersions();
         super.destroy();

Review Comment:
   `destroy()` sends `UnregisterRMRequest` via `sendAsyncRequest` and then 
immediately proceeds to shutdown (`super.destroy()` shuts down the Netty event 
loop). Since the unregister send is not awaited, the request may not be flushed 
before shutdown, reducing the reliability of server-side cleanup. Consider 
waiting for the `writeAndFlush` futures (with a short timeout) or using a sync 
request during destroy to make unregistration best-effort-but-deterministic.



##########
core/src/main/java/org/apache/seata/core/rpc/processor/server/UnregRmProcessor.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.seata.core.rpc.processor.server;
+
+import io.netty.channel.ChannelHandlerContext;
+import org.apache.seata.common.Constants;
+import org.apache.seata.common.util.NetUtil;
+import org.apache.seata.common.util.StringUtils;
+import org.apache.seata.core.protocol.RpcMessage;
+import org.apache.seata.core.protocol.UnregisterRMRequest;
+import org.apache.seata.core.protocol.UnregisterRMResponse;
+import org.apache.seata.core.rpc.RemotingServer;
+import org.apache.seata.core.rpc.netty.ChannelManager;
+import org.apache.seata.core.rpc.processor.RemotingProcessor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Process RM client unregister message.
+ * <p>
+ * process message type:
+ * {@link UnregisterRMRequest}
+ */
+public class UnregRmProcessor implements RemotingProcessor {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(UnregRmProcessor.class);
+
+    private RemotingServer remotingServer;
+
+    public UnregRmProcessor(RemotingServer remotingServer) {
+        this.remotingServer = remotingServer;
+    }
+
+    @Override
+    public void process(ChannelHandlerContext ctx, RpcMessage rpcMessage) 
throws Exception {
+        UnregisterRMRequest message = (UnregisterRMRequest) 
rpcMessage.getBody();
+        String ipAndPort = 
NetUtil.toStringAddress(ctx.channel().remoteAddress());
+        boolean isSuccess = false;
+        try {
+            String resourceIdStr = message.getResourceIds();
+            if (StringUtils.isBlank(resourceIdStr)) {
+                LOGGER.warn("RM unregister request has empty resourceIds, 
client:{}", ipAndPort);
+                UnregisterRMResponse response = new 
UnregisterRMResponse(false);
+                remotingServer.sendAsyncResponse(rpcMessage, ctx.channel(), 
response);
+                return;
+            }
+            Set<String> resourceIdSet = new 
HashSet<>(Arrays.asList(resourceIdStr.split(Constants.DBKEYS_SPLIT_CHAR)));
+            ChannelManager.unregisterRMChannel(ctx.channel(), resourceIdSet);
+            isSuccess = true;

Review Comment:
   `isSuccess` is set to `true` unconditionally after calling 
`ChannelManager.unregisterRMChannel(...)`, but that method returns early when 
the channel is not identified (rpcContext is null). This can report successful 
unregistration even when nothing was removed. Consider making 
`unregisterRMChannel` return a boolean/removed-count (or checking the 
identified RpcContext before/after) and setting the response accordingly; also 
filter out blank resourceIds produced by `split(",")` (e.g., trailing comma) 
before attempting to unregister.



##########
core/src/main/java/org/apache/seata/core/rpc/processor/server/UnregRmProcessor.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.seata.core.rpc.processor.server;
+
+import io.netty.channel.ChannelHandlerContext;
+import org.apache.seata.common.Constants;
+import org.apache.seata.common.util.NetUtil;
+import org.apache.seata.common.util.StringUtils;
+import org.apache.seata.core.protocol.RpcMessage;
+import org.apache.seata.core.protocol.UnregisterRMRequest;
+import org.apache.seata.core.protocol.UnregisterRMResponse;
+import org.apache.seata.core.rpc.RemotingServer;
+import org.apache.seata.core.rpc.netty.ChannelManager;
+import org.apache.seata.core.rpc.processor.RemotingProcessor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Process RM client unregister message.
+ * <p>
+ * process message type:
+ * {@link UnregisterRMRequest}
+ */
+public class UnregRmProcessor implements RemotingProcessor {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(UnregRmProcessor.class);
+
+    private RemotingServer remotingServer;
+
+    public UnregRmProcessor(RemotingServer remotingServer) {
+        this.remotingServer = remotingServer;
+    }
+
+    @Override
+    public void process(ChannelHandlerContext ctx, RpcMessage rpcMessage) 
throws Exception {
+        UnregisterRMRequest message = (UnregisterRMRequest) 
rpcMessage.getBody();
+        String ipAndPort = 
NetUtil.toStringAddress(ctx.channel().remoteAddress());
+        boolean isSuccess = false;
+        try {
+            String resourceIdStr = message.getResourceIds();
+            if (StringUtils.isBlank(resourceIdStr)) {
+                LOGGER.warn("RM unregister request has empty resourceIds, 
client:{}", ipAndPort);
+                UnregisterRMResponse response = new 
UnregisterRMResponse(false);
+                remotingServer.sendAsyncResponse(rpcMessage, ctx.channel(), 
response);
+                return;
+            }
+            Set<String> resourceIdSet = new 
HashSet<>(Arrays.asList(resourceIdStr.split(Constants.DBKEYS_SPLIT_CHAR)));
+            ChannelManager.unregisterRMChannel(ctx.channel(), resourceIdSet);
+            isSuccess = true;
+            if (LOGGER.isInfoEnabled()) {
+                LOGGER.info("RM unregister success, message:{}, channel:{}", 
message, ctx.channel());
+            }
+        } catch (Exception exx) {
+            LOGGER.error("RM unregister fail, client:{}, error message:{}", 
ipAndPort, exx.getMessage());

Review Comment:
   The catch block logs only `exx.getMessage()` and drops the stack trace, 
which makes diagnosing unregister failures difficult in production. Log the 
exception itself (pass `exx` as the last argument) so the full stack trace is 
captured.
   ```suggestion
               LOGGER.error("RM unregister fail, client:{}, error message:{}", 
ipAndPort, exx.getMessage(), exx);
   ```



##########
core/src/test/java/org/apache/seata/core/rpc/netty/RmNettyClientTest.java:
##########
@@ -362,6 +363,132 @@ public void testGetPoolKeyFunction() throws Exception {
         assertNotNull(function);
     }
 
+    @Test
+    public void unregisterResourceWithBlankParamsTest() {
+        RmNettyRemotingClient client = 
RmNettyRemotingClient.getInstance("app", "test_group");
+
+        NettyClientChannelManager channelManager = 
mock(NettyClientChannelManager.class);
+
+        client.setTransactionServiceGroup(null);
+        client.unregisterResource("group1", 
"jdbc:mysql://localhost:3306/test");
+        verify(channelManager, never()).getChannels();
+

Review Comment:
   This test creates a `NettyClientChannelManager` mock but never injects it 
into the client, so the `verify(channelManager, never()).getChannels()` 
assertions don't validate the real behavior (they will always pass). Inject the 
mock via `setChannelManager(client, channelManager)` (as done in other tests) 
before calling `unregisterResource`, or assert against the actual channel 
manager used by the client.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to