Copilot commented on code in PR #8020:
URL: https://github.com/apache/incubator-seata/pull/8020#discussion_r3009249849
##########
core/src/main/java/org/apache/seata/core/rpc/netty/RmNettyRemotingClient.java:
##########
@@ -292,6 +300,59 @@ public void sendRegisterMessage(String serverAddress,
Channel channel, String re
}
}
+ public void unregisterResource(String resourceGroupId, String resourceId) {
+ if (StringUtils.isBlank(transactionServiceGroup) ||
StringUtils.isBlank(resourceId)) {
+ return;
+ }
+ sendUnregisterToServers(resourceId);
+ }
+
+ private static final long UNREGISTER_FLUSH_TIMEOUT_MS = 1000;
+
+ private List<ChannelFuture> sendUnregisterToServers(String resourceIds) {
+ List<ChannelFuture> futures = new ArrayList<>();
+ try {
+ for (Map.Entry<String, Channel> entry :
+ getClientChannelManager().getChannels().entrySet()) {
+ String serverAddress = entry.getKey();
+ Channel channel = entry.getValue();
+ if (!channel.isActive()) {
+ continue;
+ }
+ String serverVersion =
getClientChannelManager().getServerVersion(serverAddress);
+ if (serverVersion == null ||
!Version.isAboveOrEqualVersion260(serverVersion)) {
+ LOGGER.warn(
+ "Server {} does not support UnregisterRMRequest
(version: {})",
+ serverAddress,
+ serverVersion);
+ continue;
+ }
+ UnregisterRMRequest message = new
UnregisterRMRequest(applicationId, transactionServiceGroup);
+ message.setResourceIds(resourceIds);
+ try {
+ if (!channel.isWritable()) {
+ throw new FrameworkException(
+ "msg:" + message.toString(),
FrameworkErrorCode.ChannelIsNotWritable);
+ }
+ RpcMessage rpcMessage = buildRequestMessage(message,
ProtocolConstants.MSGTYPE_RESQUEST_ONEWAY);
+ futures.add(channel.writeAndFlush(rpcMessage));
Review Comment:
`sendUnregisterToServers` may serialize `UnregisterRMRequest` using the
globally configured codec. With `serializer.type=PROTOBUF`,
ProtobufConvertManager has no convertors registered for
`UnregisterRMRequest/Response`, so sending this message will fail at runtime.
Either add protobuf support for these new message types in this PR, or
explicitly skip sending when `ProtocolConstants.CONFIGURED_CODEC` is
`SerializerType.PROTOBUF` (with a clear log) until support is added.
##########
core/src/main/java/org/apache/seata/core/rpc/netty/RmNettyRemotingClient.java:
##########
@@ -216,6 +223,7 @@ public void onRegisterMsgSuccess(
channel);
}
getClientChannelManager().registerChannel(serverAddress, channel,
registerRMRequest.getVersion());
+ getClientChannelManager().putServerVersion(serverAddress,
registerRMResponse.getVersion());
String dbKey = getMergedResourceKeys();
Review Comment:
PR description says the server version is cached in
`Version.SERVER_VERSION_MAP`, but the implementation caches it in
`NettyClientChannelManager.serverVersionMap` via `putServerVersion(...)`.
Either update the PR description/docs to match the implementation or move the
cache to the documented location to avoid confusion for future maintainers.
##########
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)));
+ resourceIdSet.removeIf(StringUtils::isBlank);
+ isSuccess = ChannelManager.unregisterRMChannel(ctx.channel(),
resourceIdSet);
+ if (LOGGER.isInfoEnabled()) {
+ LOGGER.info("RM unregister success, message:{}, channel:{}",
message, ctx.channel());
Review Comment:
`UnregRmProcessor` logs "RM unregister success" unconditionally, even when
`isSuccess` is false. This can mislead operators during troubleshooting; log
should reflect the actual result (or include `isSuccess` in the message) and
ideally use a lower level for failures/unknown channels.
```suggestion
if (isSuccess) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("RM unregister success, message:{},
channel:{}", message, ctx.channel());
}
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("RM unregister not successful, message:{},
channel:{}", message, ctx.channel());
}
```
##########
core/src/test/java/org/apache/seata/core/rpc/processor/server/UnregRmProcessorTest.java:
##########
@@ -0,0 +1,118 @@
+/*
+ * 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.Channel;
+import io.netty.channel.ChannelHandlerContext;
+import org.apache.seata.core.protocol.RegisterRMRequest;
+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.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+import java.net.InetSocketAddress;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test for UnregRmProcessor
+ */
+public class UnregRmProcessorTest {
+
+ private UnregRmProcessor processor;
+ private RemotingServer remotingServer;
+ private ChannelHandlerContext ctx;
+ private Channel channel;
+
+ @BeforeEach
+ public void setUp() {
+ remotingServer = mock(RemotingServer.class);
+ processor = new UnregRmProcessor(remotingServer);
+
+ ctx = mock(ChannelHandlerContext.class);
+ channel = mock(Channel.class);
+
+ when(ctx.channel()).thenReturn(channel);
+ when(channel.remoteAddress()).thenReturn(new
InetSocketAddress("127.0.0.1", 8080));
+ }
+
+ @AfterEach
+ public void tearDown() {
+ try {
+ ChannelManager.releaseRpcContext(channel);
+ } catch (Exception e) {
+ // Ignore cleanup errors
+ }
+ }
+
+ @Test
+ public void processUnregisterSuccessTest() throws Exception {
+ // First register the RM
+ RegisterRMRequest registerRequest = new RegisterRMRequest();
+ registerRequest.setApplicationId("test-app");
+ registerRequest.setTransactionServiceGroup("test-group");
+ registerRequest.setVersion("2.6.0");
+ registerRequest.setResourceIds("jdbc:mysql://localhost:3306/db1");
+ ChannelManager.registerRMChannel(registerRequest, channel);
+
+ // Unregister
+ UnregisterRMRequest unregRequest = new UnregisterRMRequest();
+ unregRequest.setApplicationId("test-app");
+ unregRequest.setTransactionServiceGroup("test-group");
+ unregRequest.setResourceIds("jdbc:mysql://localhost:3306/db1");
+
+ RpcMessage rpcMessage = new RpcMessage();
+ rpcMessage.setId(1);
+ rpcMessage.setBody(unregRequest);
+
+ processor.process(ctx, rpcMessage);
+
+ ArgumentCaptor<UnregisterRMResponse> responseCaptor =
ArgumentCaptor.forClass(UnregisterRMResponse.class);
+ verify(remotingServer).sendAsyncResponse(eq(rpcMessage), eq(channel),
responseCaptor.capture());
+
+ UnregisterRMResponse response = responseCaptor.getValue();
+ assertNotNull(response);
+ assertTrue(response.isIdentified());
+ }
+
+ @Test
+ public void processUnregisterUnknownChannelTest() throws Exception {
+ UnregisterRMRequest unregRequest = new UnregisterRMRequest();
+ unregRequest.setApplicationId("test-app");
+ unregRequest.setTransactionServiceGroup("test-group");
+ unregRequest.setResourceIds("jdbc:mysql://localhost:3306/db1");
+
+ RpcMessage rpcMessage = new RpcMessage();
+ rpcMessage.setId(1);
+ rpcMessage.setBody(unregRequest);
+
+ processor.process(ctx, rpcMessage);
+
+ verify(remotingServer).sendAsyncResponse(eq(rpcMessage), eq(channel),
any(UnregisterRMResponse.class));
+ }
Review Comment:
`processUnregisterUnknownChannelTest` verifies a response is sent, but it
doesn't assert whether the response indicates failure (e.g., `identified=false`
/ `ResultCode.Failed`). Capturing the response and asserting the expected
failure result would make this test actually validate the behavior for
unknown/unregistered channels.
--
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]