Skip to content

Commit ee5f52d

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

File tree

3 files changed

+97
-90
lines changed

3 files changed

+97
-90
lines changed

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

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -370,50 +370,6 @@ private boolean peerEquals(PeerId p1, PeerId p2) {
370370
}
371371

372372
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-
}
373+
return RaftReflectionUtil.getReplicatorState(this.raftNode, peerId);
418374
}
419375
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hugegraph.pd.raft;
19+
20+
import com.alipay.sofa.jraft.Node;
21+
import com.alipay.sofa.jraft.ReplicatorGroup;
22+
import com.alipay.sofa.jraft.core.Replicator;
23+
import com.alipay.sofa.jraft.entity.PeerId;
24+
import com.alipay.sofa.jraft.util.ThreadId;
25+
26+
import lombok.extern.slf4j.Slf4j;
27+
28+
@Slf4j
29+
public class RaftReflectionUtil {
30+
31+
public static Replicator.State getReplicatorState(Node node, PeerId peerId) {
32+
if (node == null || peerId == null) {
33+
return null;
34+
}
35+
36+
// Get ReplicatorGroup from Node
37+
var clz = node.getClass();
38+
ReplicatorGroup replicateGroup = null;
39+
try {
40+
var f = clz.getDeclaredField("replicatorGroup");
41+
f.setAccessible(true);
42+
try {
43+
replicateGroup = (ReplicatorGroup)f.get(node);
44+
}
45+
finally {
46+
f.setAccessible(false);
47+
}
48+
}
49+
catch (NoSuchFieldException | IllegalAccessException e) {
50+
log.info("getReplicatorGroup: error {}", e.getMessage());
51+
return null;
52+
}
53+
54+
if (replicateGroup == null) {
55+
return null;
56+
}
57+
58+
ThreadId threadId = replicateGroup.getReplicator(peerId);
59+
if (threadId == null) {
60+
return null;
61+
}
62+
else {
63+
try {
64+
Replicator r = (Replicator)threadId.lock();
65+
if (r == null) {
66+
return Replicator.State.Probe;
67+
}
68+
Replicator.State result = null;
69+
70+
// Get state from Replicator
71+
72+
var replicatorClz = r.getClass();
73+
try {
74+
var f = replicatorClz.getDeclaredField("state");
75+
f.setAccessible(true);
76+
try {
77+
result = (Replicator.State)f.get(r);
78+
}
79+
finally {
80+
f.setAccessible(false);
81+
}
82+
}
83+
catch (NoSuchFieldException | IllegalAccessException e) {
84+
log.info("getReplicatorState: error {}", e.getMessage());
85+
result = null;
86+
}
87+
return result;
88+
}
89+
finally {
90+
threadId.unlock();
91+
}
92+
}
93+
}
94+
}

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

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.hugegraph.pd.common.PDException;
4343
import org.apache.hugegraph.pd.grpc.MetaTask;
4444
import org.apache.hugegraph.pd.grpc.Metapb;
45+
import org.apache.hugegraph.pd.raft.RaftReflectionUtil;
4546
import org.apache.hugegraph.store.business.BusinessHandler;
4647
import org.apache.hugegraph.store.business.BusinessHandlerImpl;
4748
import org.apache.hugegraph.store.cmd.HgCmdClient;
@@ -1146,51 +1147,7 @@ public Configuration getCurrentConf() {
11461147
}
11471148

11481149
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-
}
1150+
return RaftReflectionUtil.getReplicatorState(this.raftNode, peerId);
11941151
}
11951152

11961153
class ReplicatorStateListener implements Replicator.ReplicatorStateListener {

0 commit comments

Comments
 (0)