Skip to content

Commit 78152ad

Browse files
committed
Expose min/max open file descriptors in Cluster Stats API
Also changes the response format of that section to: ``` "open_file_descriptors": { "min": 200, "max": 346, "avg": 273 } ``` Closes elastic#4681
1 parent 63ef050 commit 78152ad

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

docs/reference/cluster/stats.asciidoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ Will return, for example:
110110
"cpu": {
111111
"percent": 3
112112
},
113-
"avg_open_file_descriptors": 218
113+
"open_file_descriptors": {
114+
"min": 200,
115+
"max": 346,
116+
"avg": 273
117+
}
114118
},
115119
"jvm": {
116120
"max_uptime": "24s",

src/main/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsNodes.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ public static class ProcessStats implements ToXContent, Streamable {
397397
int count;
398398
int cpuPercent;
399399
long totalOpenFileDescriptors;
400+
long minOpenFileDescriptors = Integer.MAX_VALUE;
401+
long maxOpenFileDescriptors = Integer.MIN_VALUE;
400402

401403
public void addNodeStats(NodeStats nodeStats) {
402404
if (nodeStats.getProcess() == null) {
@@ -407,7 +409,10 @@ public void addNodeStats(NodeStats nodeStats) {
407409
// with no sigar, this may not be available
408410
cpuPercent += nodeStats.getProcess().cpu().getPercent();
409411
}
410-
totalOpenFileDescriptors += nodeStats.getProcess().openFileDescriptors();
412+
long fd = nodeStats.getProcess().openFileDescriptors();
413+
totalOpenFileDescriptors += fd;
414+
minOpenFileDescriptors = Math.min(minOpenFileDescriptors, fd);
415+
maxOpenFileDescriptors = Math.max(maxOpenFileDescriptors, fd);
411416
}
412417

413418
/**
@@ -424,6 +429,20 @@ public long getAvgOpenFileDescriptors() {
424429
return totalOpenFileDescriptors / count;
425430
}
426431

432+
public long getMaxOpenFileDescriptors() {
433+
if (count == 0) {
434+
return -1;
435+
}
436+
return maxOpenFileDescriptors;
437+
}
438+
439+
public long getMinOpenFileDescriptors() {
440+
if (count == 0) {
441+
return -1;
442+
}
443+
return minOpenFileDescriptors;
444+
}
445+
427446
@Override
428447
public void readFrom(StreamInput in) throws IOException {
429448
count = in.readVInt();
@@ -447,13 +466,22 @@ public static ProcessStats readStats(StreamInput in) throws IOException {
447466
static final class Fields {
448467
static final XContentBuilderString CPU = new XContentBuilderString("cpu");
449468
static final XContentBuilderString PERCENT = new XContentBuilderString("percent");
450-
static final XContentBuilderString AVG_OPEN_FD = new XContentBuilderString("avg_open_file_descriptors");
469+
static final XContentBuilderString OPEN_FILE_DESCRIPTORS = new XContentBuilderString("open_file_descriptors");
470+
static final XContentBuilderString MIN = new XContentBuilderString("min");
471+
static final XContentBuilderString MAX = new XContentBuilderString("max");
472+
static final XContentBuilderString AVG = new XContentBuilderString("avg");
451473
}
452474

453475
@Override
454476
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
455477
builder.startObject(Fields.CPU).field(Fields.PERCENT, cpuPercent).endObject();
456-
builder.field(Fields.AVG_OPEN_FD, getAvgOpenFileDescriptors());
478+
if (count > 0) {
479+
builder.startObject(Fields.OPEN_FILE_DESCRIPTORS);
480+
builder.field(Fields.MIN, getMinOpenFileDescriptors());
481+
builder.field(Fields.MAX, getMaxOpenFileDescriptors());
482+
builder.field(Fields.AVG, getAvgOpenFileDescriptors());
483+
builder.endObject();
484+
}
457485
return builder;
458486
}
459487
}

src/test/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,9 @@ public void testValuesSmokeScreen() {
136136
assertThat(response.nodesStats.getVersions().contains(Version.CURRENT), Matchers.equalTo(true));
137137
assertThat(response.nodesStats.getPlugins().size(), Matchers.greaterThanOrEqualTo(0));
138138

139+
assertThat(response.nodesStats.getProcess().getAvgOpenFileDescriptors(), Matchers.greaterThan(0L));
140+
assertThat(response.nodesStats.getProcess().getMinOpenFileDescriptors(), Matchers.greaterThan(0L));
141+
assertThat(response.nodesStats.getProcess().getMaxOpenFileDescriptors(), Matchers.greaterThan(0L));
142+
139143
}
140144
}

0 commit comments

Comments
 (0)