Skip to content

Commit 887bcaf

Browse files
committed
Swap free for used in HeapUsage
1 parent 23eb8e6 commit 887bcaf

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

server/src/internalClusterTest/java/org/elasticsearch/index/shard/IndexShardIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,8 +830,8 @@ public void getClusterHeapUsage(ActionListener<Map<String, HeapUsage>> listener)
830830
listener,
831831
() -> clusterService.state().nodes().stream().collect(Collectors.toUnmodifiableMap(DiscoveryNode::getId, node -> {
832832
final long maxHeap = randomNonNegativeLong();
833-
final long freeHeap = (long) (randomFloat() * maxHeap);
834-
return new HeapUsage(node.getId(), node.getName(), maxHeap, freeHeap);
833+
final long usedHeap = (long) (randomFloat() * maxHeap);
834+
return new HeapUsage(node.getId(), node.getName(), maxHeap, usedHeap);
835835
}))
836836
);
837837
}

server/src/main/java/org/elasticsearch/cluster/HeapUsage.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
/**
2222
* Record representing the heap usage for a single cluster node
2323
*/
24-
public record HeapUsage(String nodeId, String nodeName, long totalBytes, long freeBytes) implements ToXContentFragment, Writeable {
24+
public record HeapUsage(String nodeId, String nodeName, long totalBytes, long usedBytes) implements ToXContentFragment, Writeable {
25+
26+
public HeapUsage {
27+
assert usedBytes <= totalBytes;
28+
}
2529

2630
public HeapUsage(StreamInput in) throws IOException {
2731
this(in.readString(), in.readString(), in.readVLong(), in.readVLong());
@@ -32,14 +36,14 @@ public void writeTo(StreamOutput out) throws IOException {
3236
out.writeString(this.nodeId);
3337
out.writeString(this.nodeName);
3438
out.writeVLong(this.totalBytes);
35-
out.writeVLong(this.freeBytes);
39+
out.writeVLong(this.usedBytes);
3640
}
3741

3842
public XContentBuilder toShortXContent(XContentBuilder builder) throws IOException {
3943
builder.field("node_name", this.nodeName);
4044
builder.humanReadableField("total_heap_bytes", "total", ByteSizeValue.ofBytes(this.totalBytes));
41-
builder.humanReadableField("used_heap_bytes", "used", ByteSizeValue.ofBytes(this.usedBytes()));
42-
builder.humanReadableField("free_heap_bytes", "free", ByteSizeValue.ofBytes(this.freeBytes));
45+
builder.humanReadableField("used_heap_bytes", "used", ByteSizeValue.ofBytes(this.usedBytes));
46+
builder.humanReadableField("free_heap_bytes", "free", ByteSizeValue.ofBytes(this.freeBytes()));
4347
builder.field("free_heap_percent", truncatePercent(this.freeHeapAsPercentage()));
4448
builder.field("used_heap_percent", truncatePercent(this.usedHeapAsPercentage()));
4549
return builder;
@@ -53,15 +57,15 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
5357
}
5458

5559
public double freeHeapAsPercentage() {
56-
return 100.0 * freeBytes / (double) totalBytes;
60+
return 100.0 - usedHeapAsPercentage();
5761
}
5862

5963
public double usedHeapAsPercentage() {
60-
return 100.0 - freeHeapAsPercentage();
64+
return 100.0 * usedBytes / (double) totalBytes;
6165
}
6266

63-
public long usedBytes() {
64-
return totalBytes - freeBytes;
67+
public long freeBytes() {
68+
return totalBytes - usedBytes;
6569
}
6670

6771
private static double truncatePercent(double pct) {

0 commit comments

Comments
 (0)