Skip to content

Commit 7afce9d

Browse files
caglarekerimbajin
andauthored
fix(pd): populate memberSize in GET / endpoint response (#3003)
* fix(pd): complete GET / stats fix and add test coverage - Use pdService.getMembers() for memberSize (consistent with cluster()) instead of RaftEngine directly, as suggested in issue #3002 discussion - Add dataState field to BriefStatistics: exposes worst partition health state across all graphs, the most useful missing operational indicator - Align graphSize to count only user-facing graphs (endsWith("/g")), matching the semantics of cluster() to avoid silent count discrepancy - Add testQueryIndexInfo() to both RestApiTest classes to assert state, leader, memberSize > 0, and storeSize > 0 — catches this class of bug * fix(pd): relax storeSize assertion in PD-only test environment --------- Co-authored-by: imbajin <jin@apache.org>
1 parent a8ae76b commit 7afce9d

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

hugegraph-pd/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/IndexAPI.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,35 @@ public BriefStatistics index() throws PDException, ExecutionException, Interrupt
6666
BriefStatistics statistics = new BriefStatistics();
6767
statistics.leader = RaftEngine.getInstance().getLeaderGrpcAddress();
6868
statistics.state = pdService.getStoreNodeService().getClusterStats().getState().toString();
69+
70+
// Use pdService (consistent with cluster()) rather than RaftEngine directly
71+
CallStreamObserverWrap<Pdpb.GetMembersResponse> membersResp =
72+
new CallStreamObserverWrap<>();
73+
pdService.getMembers(Pdpb.GetMembersRequest.newBuilder().build(), membersResp);
74+
statistics.memberSize = membersResp.get().get(0).getMembersList().size();
75+
6976
statistics.storeSize = pdService.getStoreNodeService().getActiveStores().size();
70-
statistics.graphSize = pdService.getPartitionService().getGraphs().size();
77+
// Filter to user-facing graphs only (consistent with cluster())
78+
List<Metapb.Graph> graphs = pdRestService.getGraphs();
79+
statistics.graphSize = (int) graphs.stream()
80+
.filter(g -> g.getGraphName() != null &&
81+
g.getGraphName().endsWith("/g"))
82+
.count();
7183
statistics.partitionSize = pdService.getStoreNodeService().getShardGroups().size();
84+
85+
// Derive worst partition health state across all graphs
86+
Metapb.PartitionState dataState = Metapb.PartitionState.PState_Normal;
87+
for (Metapb.Graph graph : graphs) {
88+
if (graph.getState() == Metapb.PartitionState.UNRECOGNIZED) {
89+
continue;
90+
}
91+
if (graph.getState() != null &&
92+
graph.getState().getNumber() > dataState.getNumber()) {
93+
dataState = graph.getState();
94+
}
95+
}
96+
statistics.dataState = dataState.name();
97+
7298
return statistics;
7399

74100
}
@@ -157,9 +183,13 @@ public RestApiResponse cluster() throws InterruptedException, ExecutionException
157183
class BriefStatistics {
158184

159185
String state;
186+
/** Worst partition health state across all graphs (mirrors cluster().dataState) */
187+
String dataState;
160188
String leader;
161189
int memberSize;
190+
/** Active (online) store count only */
162191
int storeSize;
192+
/** User-facing graphs count (graphName ending with /g) */
163193
int graphSize;
164194
int partitionSize;
165195
}

hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/RestApiTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@
2929

3030
public class RestApiTest extends BaseServerTest {
3131

32+
@Test
33+
public void testQueryIndexInfo() throws URISyntaxException, IOException, InterruptedException,
34+
JSONException {
35+
String url = pdRestAddr + "/";
36+
HttpRequest request = HttpRequest.newBuilder()
37+
.uri(new URI(url))
38+
.header("Authorization", "Basic c3RvcmU6MTIz")
39+
.GET()
40+
.build();
41+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
42+
assert response.statusCode() == 200;
43+
JSONObject obj = new JSONObject(response.body());
44+
assert obj.getString("state") != null;
45+
assert obj.getString("leader") != null;
46+
assert obj.getInt("memberSize") > 0 : "memberSize should be > 0 for a running cluster";
47+
// storeSize can be 0 in PD-only test environments with no store nodes registered
48+
assert obj.getInt("storeSize") >= 0;
49+
}
50+
3251
@Test
3352
public void testQueryClusterInfo() throws URISyntaxException, IOException, InterruptedException,
3453
JSONException {

hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/service/RestApiTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@
2929

3030
public class RestApiTest extends BaseServerTest {
3131

32+
@Test
33+
public void testQueryIndexInfo() throws URISyntaxException, IOException, InterruptedException,
34+
JSONException {
35+
String url = pdRestAddr + "/";
36+
HttpRequest request = HttpRequest.newBuilder()
37+
.uri(new URI(url)).header(key, value)
38+
.GET()
39+
.build();
40+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
41+
assert response.statusCode() == 200;
42+
JSONObject obj = new JSONObject(response.body());
43+
assert obj.getString("state") != null;
44+
assert obj.getString("leader") != null;
45+
assert obj.getInt("memberSize") > 0 : "memberSize should be > 0 for a running cluster";
46+
// storeSize can be 0 in PD-only test environments with no store nodes registered
47+
assert obj.getInt("storeSize") >= 0;
48+
}
49+
3250
@Test
3351
public void testQueryClusterInfo() throws URISyntaxException, IOException, InterruptedException,
3452
JSONException {

0 commit comments

Comments
 (0)