imbajin commented on code in PR #2906:
URL: 
https://github.com/apache/incubator-hugegraph/pull/2906#discussion_r2531931568


##########
hugegraph-commons/hugegraph-common/pom.xml:
##########
@@ -223,6 +223,12 @@
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.alipay.sofa</groupId>
+            <artifactId>jraft-core</artifactId>
+            <version>1.3.14</version>

Review Comment:
   ‼️ **Critical: 避免在 hugegraph-common 引入 jraft-core 依赖**
   
   为了修复一个小的反射参数错误 (`f.get(this.raftNode)` → `f.get(r)`) 而在 `hugegraph-common` 
模块引入 `jraft-core` 依赖,代价过高:
   
   1. **依赖污染**: hugegraph-common 是基础工具模块,不应依赖具体的 Raft 实现
   2. **循环依赖风险**: 增加了模块间的耦合
   3. **非必要**: `hg-pd-core` 和 `hg-store-core` 已经有 jraft-core 依赖
   
   **建议方案**:
   1. **直接修复原 bug** (最小改动):
      - 在 `RaftEngine.getState()` 和 `PartitionEngine.getState()` 中将 
`f.get(this.raftNode)` 改为 `f.get(r)`
      - 同时修复 `finally` 块缺失问题
   
   2. **提取工具类到合适位置** (推荐):
      - 将 `RaftReflectionUtil` 放到 `hg-pd-core` 模块 (作为共享工具)
      - `hg-store-core` 通过 Maven 依赖使用 `hg-pd-core` 的工具类
      - 或者在 `hg-pd-core` 和 `hg-store-core` 各自保留一份简单实现 (90 行代码重复可接受)



##########
hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/util/RaftReflectionUtil.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.hugegraph.util;
+
+import com.alipay.sofa.jraft.Node;
+import com.alipay.sofa.jraft.ReplicatorGroup;
+import com.alipay.sofa.jraft.core.Replicator;
+import com.alipay.sofa.jraft.entity.PeerId;
+import com.alipay.sofa.jraft.util.ThreadId;
+
+import lombok.extern.slf4j.Slf4j;
+import lombok.var;
+
+@Slf4j
+public class RaftReflectionUtil {
+
+    public static Replicator.State getReplicatorState(Node node, PeerId 
peerId) {
+        if (node == null || peerId == null) {
+            return null;
+        }
+
+        // Get ReplicatorGroup from Node
+        var clz = node.getClass();
+        ReplicatorGroup replicateGroup = null;
+        try {
+            var f = clz.getDeclaredField("replicatorGroup");
+            f.setAccessible(true);
+            try {
+                replicateGroup = (ReplicatorGroup) f.get(node);
+            } finally {
+                f.setAccessible(false);
+            }
+        } catch (NoSuchFieldException | IllegalAccessException e) {
+            log.info("getReplicatorGroup: error {}", e.getMessage());
+            return null;
+        }
+
+        if (replicateGroup == null) {
+            return null;
+        }
+
+        ThreadId threadId = replicateGroup.getReplicator(peerId);
+        if (threadId == null) {
+            return null;
+        } else {
+            Replicator r = (Replicator) threadId.lock();
+            if (r == null) {
+                return Replicator.State.Probe;
+            }
+            Replicator.State result = null;
+
+            // Get state from Replicator
+            try {
+                var replicatorClz = r.getClass();
+                try {
+                    var f = replicatorClz.getDeclaredField("state");
+                    f.setAccessible(true);
+                    try {
+                        result = (Replicator.State) f.get(r);
+                    } finally {
+                        f.setAccessible(false);
+                    }
+                } catch (NoSuchFieldException | IllegalAccessException e) {
+                    log.info("getReplicatorState: error {}", e.getMessage());
+                    result = null;
+                }
+            }

Review Comment:
   ⚠️ **代码健壮性问题: finally 块处理不完整**
   
   当前实现在第 46 行有 `finally` 块恢复 accessibility,但第 75 行缺少对称的 finally 块:
   
   ```suggestion
                   try {
                       var f = replicatorClz.getDeclaredField("state");
                       f.setAccessible(true);
                       try {
                           result = (Replicator.State) f.get(r);
                       } finally {
                           f.setAccessible(false);
                       }
                   } catch (NoSuchFieldException | IllegalAccessException e) {
                       log.info("getReplicatorState: error {}", e.getMessage());
                       result = null;
                   } finally {
                       threadId.unlock();
                   }
   ```
   
   **关键改进**:
   - 即使在获取 field 或赋值时抛出异常,也要确保 `threadId.unlock()` 被执行
   - 避免潜在的死锁风险



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