This is an automated email from the ASF dual-hosted git repository.

vgalaxies pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git


The following commit(s) were added to refs/heads/master by this push:
     new 9a3daf824 refactor(store): fix reflection parameter error and extract 
duplicate methods to RaftReflectionUtil (#2906)
9a3daf824 is described below

commit 9a3daf824bb56109a32b8a9839a7ba97b6d571a2
Author: contrueCT <[email protected]>
AuthorDate: Thu Nov 27 13:54:11 2025 +0800

    refactor(store): fix reflection parameter error and extract duplicate 
methods to RaftReflectionUtil (#2906)
---
 .../org/apache/hugegraph/pd/raft/RaftEngine.java   | 46 +----------
 .../hugegraph/pd/raft/RaftReflectionUtil.java      | 95 ++++++++++++++++++++++
 .../hugegraph/pd/raft/RaftReflectionUtilTest.java  | 63 ++++++++++++++
 hugegraph-store/hg-store-core/pom.xml              |  5 ++
 .../apache/hugegraph/store/PartitionEngine.java    | 47 +----------
 5 files changed, 166 insertions(+), 90 deletions(-)

diff --git 
a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/RaftEngine.java
 
b/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/RaftEngine.java
index 342594ef7..e70ac9234 100644
--- 
a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/RaftEngine.java
+++ 
b/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/RaftEngine.java
@@ -370,50 +370,6 @@ public class RaftEngine {
     }
 
     private Replicator.State getReplicatorState(PeerId peerId) {
-        var replicateGroup = getReplicatorGroup();
-        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 = getState(r);
-            threadId.unlock();
-            return result;
-        }
-    }
-
-    private ReplicatorGroup getReplicatorGroup() {
-        var clz = this.raftNode.getClass();
-        try {
-            var f = clz.getDeclaredField("replicatorGroup");
-            f.setAccessible(true);
-            var group = (ReplicatorGroup) f.get(this.raftNode);
-            f.setAccessible(false);
-            return group;
-        } catch (NoSuchFieldException | IllegalAccessException e) {
-            log.info("getReplicatorGroup: error {}", e.getMessage());
-            return null;
-        }
-    }
-
-    private Replicator.State getState(Replicator r) {
-        var clz = r.getClass();
-        try {
-            var f = clz.getDeclaredField("state");
-            f.setAccessible(true);
-            var state = (Replicator.State) f.get(this.raftNode);
-            f.setAccessible(false);
-            return state;
-        } catch (NoSuchFieldException | IllegalAccessException e) {
-            log.info("getReplicatorGroup: error {}", e.getMessage());
-            return null;
-        }
+        return RaftReflectionUtil.getReplicatorState(this.raftNode, peerId);
     }
 }
diff --git 
a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/RaftReflectionUtil.java
 
b/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/RaftReflectionUtil.java
new file mode 100644
index 000000000..16cb5941d
--- /dev/null
+++ 
b/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/RaftReflectionUtil.java
@@ -0,0 +1,95 @@
+/*
+ * 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.pd.raft;
+
+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;
+
+@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.warn("Failed to get replicator state via reflection: {}", 
e.getMessage(), e);
+            return null;
+        }
+
+        if (replicateGroup == null) {
+            return null;
+        }
+
+        ThreadId threadId = replicateGroup.getReplicator(peerId);
+        if (threadId == null) {
+            return null;
+        }
+        else {
+            Replicator r = (Replicator)threadId.lock();
+            try {
+                if (r == null) {
+                    return Replicator.State.Probe;
+                }
+                Replicator.State result = null;
+
+                // Get state from Replicator
+
+                var replicatorClz = r.getClass();
+                try {
+                    var f = replicatorClz.getDeclaredField("state");
+                    f.setAccessible(true);
+                    try {
+                        result = (Replicator.State)f.get(r);
+                    }catch (Exception e){
+                        log.warn("Failed to get replicator state for peerId: 
{}, error: {}", peerId, e.getMessage());
+                    }
+                    finally {
+                        f.setAccessible(false);
+                    }
+                }
+                catch (NoSuchFieldException e) {
+                    log.warn("Failed to get replicator state via reflection: 
{}", e.getMessage(), e);
+                    result = null;
+                }
+                return result;
+            } finally {
+                threadId.unlock();
+            }
+        }
+    }
+}
diff --git 
a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/raft/RaftReflectionUtilTest.java
 
b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/raft/RaftReflectionUtilTest.java
new file mode 100644
index 000000000..c29e2a95a
--- /dev/null
+++ 
b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/raft/RaftReflectionUtilTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.pd.raft;
+
+import com.alipay.sofa.jraft.Node;
+import com.alipay.sofa.jraft.core.Replicator;
+import com.alipay.sofa.jraft.entity.PeerId;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import static org.mockito.Mockito.mock;
+
+public class RaftReflectionUtilTest {
+
+    @Test
+    public void testGetReplicatorStateWithNullNode() {
+        // Setup
+        PeerId peerId = mock(PeerId.class);
+
+        // Run the test
+        Replicator.State result = RaftReflectionUtil.getReplicatorState(null, 
peerId);
+
+        // Verify the results
+        Assert.assertNull(result);
+    }
+
+    @Test
+    public void testGetReplicatorStateWithNullPeerId() {
+        // Setup
+        Node node = mock(Node.class);
+
+        // Run the test
+        Replicator.State result = RaftReflectionUtil.getReplicatorState(node, 
null);
+
+        // Verify the results
+        Assert.assertNull(result);
+    }
+
+    @Test
+    public void testGetReplicatorStateWithBothNull() {
+        // Run the test
+        Replicator.State result = RaftReflectionUtil.getReplicatorState(null, 
null);
+
+        // Verify the results
+        Assert.assertNull(result);
+    }
+}
diff --git a/hugegraph-store/hg-store-core/pom.xml 
b/hugegraph-store/hg-store-core/pom.xml
index 6f3c4c305..0ecf72328 100644
--- a/hugegraph-store/hg-store-core/pom.xml
+++ b/hugegraph-store/hg-store-core/pom.xml
@@ -178,6 +178,11 @@
             <artifactId>hg-store-client</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.hugegraph</groupId>
+            <artifactId>hg-pd-core</artifactId>
+            <version>${revision}</version>
+        </dependency>
     </dependencies>
     <build>
         <plugins>
diff --git 
a/hugegraph-store/hg-store-core/src/main/java/org/apache/hugegraph/store/PartitionEngine.java
 
b/hugegraph-store/hg-store-core/src/main/java/org/apache/hugegraph/store/PartitionEngine.java
index 3b4a8427e..a70f17465 100644
--- 
a/hugegraph-store/hg-store-core/src/main/java/org/apache/hugegraph/store/PartitionEngine.java
+++ 
b/hugegraph-store/hg-store-core/src/main/java/org/apache/hugegraph/store/PartitionEngine.java
@@ -42,6 +42,7 @@ import org.apache.commons.lang.StringUtils;
 import org.apache.hugegraph.pd.common.PDException;
 import org.apache.hugegraph.pd.grpc.MetaTask;
 import org.apache.hugegraph.pd.grpc.Metapb;
+import org.apache.hugegraph.pd.raft.RaftReflectionUtil;
 import org.apache.hugegraph.store.business.BusinessHandler;
 import org.apache.hugegraph.store.business.BusinessHandlerImpl;
 import org.apache.hugegraph.store.cmd.HgCmdClient;
@@ -1146,51 +1147,7 @@ public class PartitionEngine implements 
Lifecycle<PartitionEngineOptions>, RaftS
     }
 
     private Replicator.State getReplicatorState(PeerId peerId) {
-        var replicateGroup = getReplicatorGroup();
-        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 = getState(r);
-            threadId.unlock();
-            return result;
-        }
-    }
-
-    private ReplicatorGroup getReplicatorGroup() {
-        var clz = this.raftNode.getClass();
-        try {
-            var f = clz.getDeclaredField("replicatorGroup");
-            f.setAccessible(true);
-            var group = (ReplicatorGroup) f.get(this.raftNode);
-            f.setAccessible(false);
-            return group;
-        } catch (NoSuchFieldException | IllegalAccessException e) {
-            log.info("getReplicatorGroup: error {}", e.getMessage());
-            return null;
-        }
-    }
-
-    private Replicator.State getState(Replicator r) {
-        var clz = r.getClass();
-        try {
-            var f = clz.getDeclaredField("state");
-            f.setAccessible(true);
-            var state = (Replicator.State) f.get(this.raftNode);
-            f.setAccessible(false);
-            return state;
-        } catch (NoSuchFieldException | IllegalAccessException e) {
-            log.info("getReplicatorGroup: error {}", e.getMessage());
-            return null;
-        }
+        return RaftReflectionUtil.getReplicatorState(this.raftNode, peerId);
     }
 
     class ReplicatorStateListener implements 
Replicator.ReplicatorStateListener {

Reply via email to