You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by "slievrly (via GitHub)" <gi...@apache.org> on 2026/10/02 15:38:11 UTC

Re: [PR] feature: expose TC version to client side via ServerVersionHolder [incubator-seata]

slievrly commented on code in PR #8183:
URL: https://github.com/apache/incubator-seata/pull/8183#discussion_r4167283761


##########
core/src/main/java/org/apache/seata/core/protocol/ServerVersionHolder.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.protocol;
+
+import org.apache.seata.common.util.StringUtils;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Holds the version of each server (TC) the client has registered to.
+ * <p>
+ * The server always echoes its own version back in the register response
+ * ({@link AbstractIdentifyResponse#getVersion()}). This holder keeps that version so that upper
+ * layers can decide whether a feature is supported by the target server before sending a request,
+ * which is the client side counterpart of {@code RpcContext#getVersion()} on the server side.
+ * <p>
+ * Note that the key is the server address used by the client to connect (the same key as
+ * {@code NettyClientChannelManager#channels}), NOT the resolved remote address of a channel, so it
+ * must not be mixed up with {@link Version#getChannelVersion(io.netty.channel.Channel)}.
+ * <p>
+ * Entries are not removed when a single channel disconnects, and that is intentional: TM and RM
+ * connect to the same servers through two independent channel managers, so dropping an entry when
+ * one of them disconnects would break feature detection for the other one. A version is an
+ * intrinsic attribute of the server at a given address and is overwritten on every successful
+ * registration, so a stale entry can never be read through a live channel, since such a channel can
+ * only exist after a registration that has just refreshed the entry. A caller that looks a version
+ * up by address without holding a channel to that server is not covered by this and has to handle a
+ * stale or absent version itself.
+ * <p>
+ * The recorded versions are discarded once every client that could read them has been destroyed,
+ * see {@link #attach(String)} and {@link #detach(String)}, so the map does not outlive the clients
+ * that populated it.
+ *
+ * @since 2.6.0
+ */
+public class ServerVersionHolder {
+
+    private static final Map<String, String> SERVER_VERSION_MAP = new ConcurrentHashMap<>();
+
+    private static final Set<String> ACTIVE_CLIENTS = ConcurrentHashMap.newKeySet();
+
+    private ServerVersionHolder() {}
+
+    /**
+     * Mark a client as active. Should be called by a client once it is initialized, so that the
+     * recorded versions are kept as long as that client may read them.
+     *
+     * @param clientRole the role of the client, that is the name of a
+     *                   {@code NettyPoolKey.TransactionRole}
+     */
+    public static void attach(String clientRole) {
+        if (StringUtils.isBlank(clientRole)) {
+            return;
+        }
+        ACTIVE_CLIENTS.add(clientRole);
+    }
+
+    /**
+     * Mark a client as destroyed, and discard the recorded versions once no client is left. This is
+     * the only point at which entries are removed, see the class javadoc for why a single channel
+     * disconnection must not remove them.
+     *
+     * @param clientRole the role of the client, that is the name of a
+     *                   {@code NettyPoolKey.TransactionRole}
+     */
+    public static void detach(String clientRole) {
+        if (StringUtils.isBlank(clientRole)) {
+            return;
+        }
+        if (ACTIVE_CLIENTS.remove(clientRole) && ACTIVE_CLIENTS.isEmpty()) {
+            clear();

Review Comment:
   [P2] Make the last-client cleanup atomic with attach. A TM destroy can remove TMROLE and observe an empty set, then a concurrently initializing RM can attach RMROLE and record its successful registration, and finally this clear() erases that live RM version. RM unregister subsequently treats the connected server as unknown and skips UnregisterRMRequest. ConcurrentHashMap makes each operation safe, but does not make remove/isEmpty/clear atomic with attach/put. Coordinate the lifecycle operations (or use a generation/ownership model) and cover this interleaving.



-- 
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: notifications-unsubscribe@seata.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org