Skip to content

Commit 262d01d

Browse files
committed
refactor: extract replicator state retrieval to RaftReflectionUtil
1 parent 732df8c commit 262d01d

5 files changed

Lines changed: 91 additions & 90 deletions

File tree

hugegraph-commons/hugegraph-common/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@
223223
<groupId>org.projectlombok</groupId>
224224
<artifactId>lombok</artifactId>
225225
</dependency>
226+
<dependency>
227+
<groupId>com.alipay.sofa</groupId>
228+
<artifactId>jraft-core</artifactId>
229+
<version>1.3.14</version>
230+
<scope>compile</scope>
231+
</dependency>
226232
</dependencies>
227233

228234
<dependencyManagement>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.apache.hugegraph.util;
2+
3+
import com.alipay.sofa.jraft.Node;
4+
import com.alipay.sofa.jraft.ReplicatorGroup;
5+
import com.alipay.sofa.jraft.core.Replicator;
6+
import com.alipay.sofa.jraft.entity.PeerId;
7+
import com.alipay.sofa.jraft.util.ThreadId;
8+
9+
import lombok.extern.slf4j.Slf4j;
10+
import lombok.var;
11+
12+
@Slf4j
13+
public class RaftReflectionUtil {
14+
15+
public static Replicator.State getReplicatorState(Node node, PeerId peerId) {
16+
if (node == null || peerId == null) {
17+
return null;
18+
}
19+
20+
// Get ReplicatorGroup from Node
21+
var clz = node.getClass();
22+
ReplicatorGroup replicateGroup = null;
23+
try {
24+
var f = clz.getDeclaredField("replicatorGroup");
25+
f.setAccessible(true);
26+
try {
27+
replicateGroup = (ReplicatorGroup) f.get(node);
28+
} finally {
29+
f.setAccessible(false);
30+
}
31+
} catch (NoSuchFieldException | IllegalAccessException e) {
32+
log.info("getReplicatorGroup: error {}", e.getMessage());
33+
return null;
34+
}
35+
36+
if (replicateGroup == null) {
37+
return null;
38+
}
39+
40+
ThreadId threadId = replicateGroup.getReplicator(peerId);
41+
if (threadId == null) {
42+
return null;
43+
} else {
44+
Replicator r = (Replicator) threadId.lock();
45+
if (r == null) {
46+
return Replicator.State.Probe;
47+
}
48+
Replicator.State result = null;
49+
50+
// Get state from Replicator
51+
try {
52+
var replicatorClz = r.getClass();
53+
try {
54+
var f = replicatorClz.getDeclaredField("state");
55+
f.setAccessible(true);
56+
try {
57+
result = (Replicator.State) f.get(r);
58+
} finally {
59+
f.setAccessible(false);
60+
}
61+
} catch (NoSuchFieldException | IllegalAccessException e) {
62+
log.info("getReplicatorState: error {}", e.getMessage());
63+
result = null;
64+
}
65+
}
66+
finally {
67+
threadId.unlock();
68+
}
69+
return result;
70+
}
71+
}
72+
73+
}

hugegraph-pd/hg-pd-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,11 @@
8383
<artifactId>gson</artifactId>
8484
<version>2.8.9</version>
8585
</dependency>
86+
<dependency>
87+
<groupId>org.apache.hugegraph</groupId>
88+
<artifactId>hugegraph-common</artifactId>
89+
<version>1.7.0</version>
90+
<scope>compile</scope>
91+
</dependency>
8692
</dependencies>
8793
</project>

hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/raft/RaftEngine.java

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
import io.netty.channel.ChannelHandler;
6161
import lombok.extern.slf4j.Slf4j;
6262

63+
import org.apache.hugegraph.util.RaftReflectionUtil;
64+
6365
@Slf4j
6466
public class RaftEngine {
6567

@@ -370,50 +372,6 @@ private boolean peerEquals(PeerId p1, PeerId p2) {
370372
}
371373

372374
private Replicator.State getReplicatorState(PeerId peerId) {
373-
var replicateGroup = getReplicatorGroup();
374-
if (replicateGroup == null) {
375-
return null;
376-
}
377-
378-
ThreadId threadId = replicateGroup.getReplicator(peerId);
379-
if (threadId == null) {
380-
return null;
381-
} else {
382-
Replicator r = (Replicator) threadId.lock();
383-
if (r == null) {
384-
return Replicator.State.Probe;
385-
}
386-
Replicator.State result = getState(r);
387-
threadId.unlock();
388-
return result;
389-
}
390-
}
391-
392-
private ReplicatorGroup getReplicatorGroup() {
393-
var clz = this.raftNode.getClass();
394-
try {
395-
var f = clz.getDeclaredField("replicatorGroup");
396-
f.setAccessible(true);
397-
var group = (ReplicatorGroup) f.get(this.raftNode);
398-
f.setAccessible(false);
399-
return group;
400-
} catch (NoSuchFieldException | IllegalAccessException e) {
401-
log.info("getReplicatorGroup: error {}", e.getMessage());
402-
return null;
403-
}
404-
}
405-
406-
private Replicator.State getState(Replicator r) {
407-
var clz = r.getClass();
408-
try {
409-
var f = clz.getDeclaredField("state");
410-
f.setAccessible(true);
411-
var state = (Replicator.State) f.get(r);
412-
f.setAccessible(false);
413-
return state;
414-
} catch (NoSuchFieldException | IllegalAccessException e) {
415-
log.info("getReplicatorGroup: error {}", e.getMessage());
416-
return null;
417-
}
375+
return RaftReflectionUtil.getReplicatorState(this.raftNode, peerId);
418376
}
419377
}

hugegraph-store/hg-store-core/src/main/java/org/apache/hugegraph/store/PartitionEngine.java

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
import lombok.Getter;
9999
import lombok.extern.slf4j.Slf4j;
100100

101+
import org.apache.hugegraph.util.RaftReflectionUtil;
102+
101103
/**
102104
* Partition processing engine
103105
*/
@@ -1146,51 +1148,7 @@ public Configuration getCurrentConf() {
11461148
}
11471149

11481150
private Replicator.State getReplicatorState(PeerId peerId) {
1149-
var replicateGroup = getReplicatorGroup();
1150-
if (replicateGroup == null) {
1151-
return null;
1152-
}
1153-
1154-
ThreadId threadId = replicateGroup.getReplicator(peerId);
1155-
if (threadId == null) {
1156-
return null;
1157-
} else {
1158-
Replicator r = (Replicator) threadId.lock();
1159-
if (r == null) {
1160-
return Replicator.State.Probe;
1161-
}
1162-
Replicator.State result = getState(r);
1163-
threadId.unlock();
1164-
return result;
1165-
}
1166-
}
1167-
1168-
private ReplicatorGroup getReplicatorGroup() {
1169-
var clz = this.raftNode.getClass();
1170-
try {
1171-
var f = clz.getDeclaredField("replicatorGroup");
1172-
f.setAccessible(true);
1173-
var group = (ReplicatorGroup) f.get(this.raftNode);
1174-
f.setAccessible(false);
1175-
return group;
1176-
} catch (NoSuchFieldException | IllegalAccessException e) {
1177-
log.info("getReplicatorGroup: error {}", e.getMessage());
1178-
return null;
1179-
}
1180-
}
1181-
1182-
private Replicator.State getState(Replicator r) {
1183-
var clz = r.getClass();
1184-
try {
1185-
var f = clz.getDeclaredField("state");
1186-
f.setAccessible(true);
1187-
var state = (Replicator.State) f.get(r);
1188-
f.setAccessible(false);
1189-
return state;
1190-
} catch (NoSuchFieldException | IllegalAccessException e) {
1191-
log.info("getReplicatorGroup: error {}", e.getMessage());
1192-
return null;
1193-
}
1151+
return RaftReflectionUtil.getReplicatorState(this.raftNode, peerId);
11941152
}
11951153

11961154
class ReplicatorStateListener implements Replicator.ReplicatorStateListener {

0 commit comments

Comments
 (0)