From 732df8c2318b08724b8cbf763119bbc041a6073f Mon Sep 17 00:00:00 2001 From: contrueCT Date: Wed, 12 Nov 2025 22:46:20 +0800 Subject: [PATCH 1/8] Fix incorrect reflection parameter in RaftEngine.getState method --- .../src/main/java/org/apache/hugegraph/pd/raft/RaftEngine.java | 2 +- .../main/java/org/apache/hugegraph/store/PartitionEngine.java | 2 +- 2 files changed, 2 insertions(+), 2 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 342594ef74..b1c4d59376 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 @@ -408,7 +408,7 @@ private Replicator.State getState(Replicator r) { try { var f = clz.getDeclaredField("state"); f.setAccessible(true); - var state = (Replicator.State) f.get(this.raftNode); + var state = (Replicator.State) f.get(r); f.setAccessible(false); return state; } catch (NoSuchFieldException | IllegalAccessException e) { 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 3b4a8427ed..c375db86e9 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 @@ -1184,7 +1184,7 @@ private Replicator.State getState(Replicator r) { try { var f = clz.getDeclaredField("state"); f.setAccessible(true); - var state = (Replicator.State) f.get(this.raftNode); + var state = (Replicator.State) f.get(r); f.setAccessible(false); return state; } catch (NoSuchFieldException | IllegalAccessException e) { From ee5f52d943cf58a2b88d678c1ff3b5323e68da9e Mon Sep 17 00:00:00 2001 From: contrueCT Date: Mon, 17 Nov 2025 19:20:03 +0800 Subject: [PATCH 2/8] refactor: extract replicator state retrieval to RaftReflectionUtil --- .../apache/hugegraph/pd/raft/RaftEngine.java | 46 +-------- .../hugegraph/pd/raft/RaftReflectionUtil.java | 94 +++++++++++++++++++ .../hugegraph/store/PartitionEngine.java | 47 +--------- 3 files changed, 97 insertions(+), 90 deletions(-) create mode 100644 hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/RaftReflectionUtil.java 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 b1c4d59376..e70ac92340 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 @@ private boolean peerEquals(PeerId p1, PeerId p2) { } 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(r); - 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 0000000000..f85baae1eb --- /dev/null +++ b/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/RaftReflectionUtil.java @@ -0,0 +1,94 @@ +/* + * 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.info("getReplicatorGroup: error {}", e.getMessage()); + return null; + } + + if (replicateGroup == null) { + return null; + } + + ThreadId threadId = replicateGroup.getReplicator(peerId); + if (threadId == null) { + return null; + } + else { + try { + Replicator r = (Replicator)threadId.lock(); + 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); + } + finally { + f.setAccessible(false); + } + } + catch (NoSuchFieldException | IllegalAccessException e) { + log.info("getReplicatorState: error {}", e.getMessage()); + result = null; + } + return result; + } + finally { + threadId.unlock(); + } + } + } +} 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 c375db86e9..a70f17465f 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.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 Configuration getCurrentConf() { } 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(r); - 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 { From 79eb69e6e4ec8882ab1d2fc37f791cef90e957fe Mon Sep 17 00:00:00 2001 From: contrueCT Date: Mon, 17 Nov 2025 19:34:22 +0800 Subject: [PATCH 3/8] fix: correct locking mechanism in Replicator state retrieval --- .../java/org/apache/hugegraph/pd/raft/RaftReflectionUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index f85baae1eb..313186829c 100644 --- 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 @@ -60,8 +60,8 @@ public static Replicator.State getReplicatorState(Node node, PeerId peerId) { return null; } else { + Replicator r = (Replicator)threadId.lock(); try { - Replicator r = (Replicator)threadId.lock(); if (r == null) { return Replicator.State.Probe; } From f16c97d6588c219bb9e8074354b365bf7a163c6d Mon Sep 17 00:00:00 2001 From: contrueCT Date: Mon, 17 Nov 2025 22:48:44 +0800 Subject: [PATCH 4/8] fix: correct locking mechanism in Replicator state retrieval --- .../org/apache/hugegraph/pd/raft/RaftReflectionUtil.java | 3 +-- hugegraph-store/hg-store-core/pom.xml | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) 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 index 313186829c..23e6d394c8 100644 --- 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 @@ -85,8 +85,7 @@ public static Replicator.State getReplicatorState(Node node, PeerId peerId) { result = null; } return result; - } - finally { + } finally { threadId.unlock(); } } diff --git a/hugegraph-store/hg-store-core/pom.xml b/hugegraph-store/hg-store-core/pom.xml index 6f3c4c305b..afae968b79 100644 --- a/hugegraph-store/hg-store-core/pom.xml +++ b/hugegraph-store/hg-store-core/pom.xml @@ -178,6 +178,12 @@ hg-store-client test + + org.apache.hugegraph + hg-pd-core + 1.7.0 + compile + From b3250cd8e6a9d40a106d423ec00aeaee35c3acdc Mon Sep 17 00:00:00 2001 From: contrueCT Date: Thu, 20 Nov 2025 20:27:23 +0800 Subject: [PATCH 5/8] fix:optimize code comments and error logs --- .../org/apache/hugegraph/pd/raft/RaftReflectionUtil.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 index 23e6d394c8..16cb5941d6 100644 --- 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 @@ -47,7 +47,7 @@ public static Replicator.State getReplicatorState(Node node, PeerId peerId) { } } catch (NoSuchFieldException | IllegalAccessException e) { - log.info("getReplicatorGroup: error {}", e.getMessage()); + log.warn("Failed to get replicator state via reflection: {}", e.getMessage(), e); return null; } @@ -75,13 +75,15 @@ public static Replicator.State getReplicatorState(Node node, PeerId peerId) { 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 | IllegalAccessException e) { - log.info("getReplicatorState: error {}", e.getMessage()); + catch (NoSuchFieldException e) { + log.warn("Failed to get replicator state via reflection: {}", e.getMessage(), e); result = null; } return result; From d90faf0f298b4452aae0682e66b0c37677579ff5 Mon Sep 17 00:00:00 2001 From: contrueCT Date: Thu, 20 Nov 2025 20:28:34 +0800 Subject: [PATCH 6/8] test:add RaftReflectionUtilTest.java --- .../pd/raft/RaftReflectionUtilTest.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/raft/RaftReflectionUtilTest.java 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 0000000000..c29e2a95a6 --- /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); + } +} From d43a8f6bea0c34e223f4da3a4e606d9b5ebe87fb Mon Sep 17 00:00:00 2001 From: contrueCT Date: Thu, 20 Nov 2025 20:29:21 +0800 Subject: [PATCH 7/8] chore:optimize dependency --- hugegraph-store/hg-store-core/pom.xml | 2 -- 1 file changed, 2 deletions(-) diff --git a/hugegraph-store/hg-store-core/pom.xml b/hugegraph-store/hg-store-core/pom.xml index afae968b79..af0cf7811d 100644 --- a/hugegraph-store/hg-store-core/pom.xml +++ b/hugegraph-store/hg-store-core/pom.xml @@ -181,8 +181,6 @@ org.apache.hugegraph hg-pd-core - 1.7.0 - compile From 82300c065b8251c4b7e82fe15cdc1c5a45921872 Mon Sep 17 00:00:00 2001 From: contrueCT Date: Sun, 23 Nov 2025 17:58:17 +0800 Subject: [PATCH 8/8] chore:optimize dependency --- hugegraph-store/hg-store-core/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/hugegraph-store/hg-store-core/pom.xml b/hugegraph-store/hg-store-core/pom.xml index af0cf7811d..0ecf723280 100644 --- a/hugegraph-store/hg-store-core/pom.xml +++ b/hugegraph-store/hg-store-core/pom.xml @@ -181,6 +181,7 @@ org.apache.hugegraph hg-pd-core + ${revision}