Skip to content

Commit 9b4701c

Browse files
committed
Merge remote-tracking branch 'origin/fix_getState(Replicator-r)' into fix_getState(Replicator-r)
2 parents ee5f52d + 8317875 commit 9b4701c

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.util;
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+
import lombok.var;
28+
29+
@Slf4j
30+
public class RaftReflectionUtil {
31+
32+
public static Replicator.State getReplicatorState(Node node, PeerId peerId) {
33+
if (node == null || peerId == null) {
34+
return null;
35+
}
36+
37+
// Get ReplicatorGroup from Node
38+
var clz = node.getClass();
39+
ReplicatorGroup replicateGroup = null;
40+
try {
41+
var f = clz.getDeclaredField("replicatorGroup");
42+
f.setAccessible(true);
43+
try {
44+
replicateGroup = (ReplicatorGroup) f.get(node);
45+
} finally {
46+
f.setAccessible(false);
47+
}
48+
} catch (NoSuchFieldException | IllegalAccessException e) {
49+
log.info("getReplicatorGroup: error {}", e.getMessage());
50+
return null;
51+
}
52+
53+
if (replicateGroup == null) {
54+
return null;
55+
}
56+
57+
ThreadId threadId = replicateGroup.getReplicator(peerId);
58+
if (threadId == null) {
59+
return null;
60+
} else {
61+
Replicator r = (Replicator) threadId.lock();
62+
if (r == null) {
63+
return Replicator.State.Probe;
64+
}
65+
Replicator.State result = null;
66+
67+
// Get state from Replicator
68+
try {
69+
var replicatorClz = r.getClass();
70+
try {
71+
var f = replicatorClz.getDeclaredField("state");
72+
f.setAccessible(true);
73+
try {
74+
result = (Replicator.State) f.get(r);
75+
} finally {
76+
f.setAccessible(false);
77+
}
78+
} catch (NoSuchFieldException | IllegalAccessException e) {
79+
log.info("getReplicatorState: error {}", e.getMessage());
80+
result = null;
81+
}
82+
}
83+
finally {
84+
threadId.unlock();
85+
}
86+
return result;
87+
}
88+
}
89+
90+
}

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: 2 additions & 0 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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
import lombok.Getter;
100100
import lombok.extern.slf4j.Slf4j;
101101

102+
import org.apache.hugegraph.util.RaftReflectionUtil;
103+
102104
/**
103105
* Partition processing engine
104106
*/

0 commit comments

Comments
 (0)