From 931a164b1f744f27d180f731823cbb4ec3bb0f8a Mon Sep 17 00:00:00 2001 From: javanna Date: Fri, 2 Sep 2016 15:02:54 +0200 Subject: [PATCH] ProcessStats to implement Writeable rather than Streamable --- .../admin/cluster/node/stats/NodeStats.java | 2 +- .../monitor/process/ProcessProbe.java | 18 +-- .../monitor/process/ProcessStats.java | 111 +++++++----------- .../monitor/process/ProcessProbeTests.java | 4 +- 4 files changed, 46 insertions(+), 89 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/stats/NodeStats.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/stats/NodeStats.java index 0a750e571e3..f66899bb04b 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/node/stats/NodeStats.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/node/stats/NodeStats.java @@ -215,7 +215,7 @@ public class NodeStats extends BaseNodeResponse implements ToXContent { os = new OsStats(in); } if (in.readBoolean()) { - process = ProcessStats.readProcessStats(in); + process = new ProcessStats(in); } if (in.readBoolean()) { jvm = JvmStats.readJvmStats(in); diff --git a/core/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java b/core/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java index e4307f724c5..c6434d24800 100644 --- a/core/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java +++ b/core/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java @@ -131,21 +131,9 @@ public class ProcessProbe { } public ProcessStats processStats() { - ProcessStats stats = new ProcessStats(); - stats.timestamp = System.currentTimeMillis(); - stats.openFileDescriptors = getOpenFileDescriptorCount(); - stats.maxFileDescriptors = getMaxFileDescriptorCount(); - - ProcessStats.Cpu cpu = new ProcessStats.Cpu(); - cpu.percent = getProcessCpuPercent(); - cpu.total = getProcessCpuTotalTime(); - stats.cpu = cpu; - - ProcessStats.Mem mem = new ProcessStats.Mem(); - mem.totalVirtual = getTotalVirtualMemorySize(); - stats.mem = mem; - - return stats; + ProcessStats.Cpu cpu = new ProcessStats.Cpu(getProcessCpuPercent(), getProcessCpuTotalTime()); + ProcessStats.Mem mem = new ProcessStats.Mem(getTotalVirtualMemorySize()); + return new ProcessStats(System.currentTimeMillis(), getOpenFileDescriptorCount(), getMaxFileDescriptorCount(), cpu, mem); } /** diff --git a/core/src/main/java/org/elasticsearch/monitor/process/ProcessStats.java b/core/src/main/java/org/elasticsearch/monitor/process/ProcessStats.java index 310cb215ae5..2a9b8952779 100644 --- a/core/src/main/java/org/elasticsearch/monitor/process/ProcessStats.java +++ b/core/src/main/java/org/elasticsearch/monitor/process/ProcessStats.java @@ -21,7 +21,7 @@ package org.elasticsearch.monitor.process; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.io.stream.Streamable; +import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ToXContent; @@ -29,18 +29,37 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; -public class ProcessStats implements Streamable, ToXContent { +public class ProcessStats implements Writeable, ToXContent { - long timestamp = -1; + private final long timestamp; + private final long openFileDescriptors; + private final long maxFileDescriptors; + private final Cpu cpu; + private final Mem mem; - long openFileDescriptors = -1; - long maxFileDescriptors = -1; + public ProcessStats(long timestamp, long openFileDescriptors, long maxFileDescriptors, Cpu cpu, Mem mem) { + this.timestamp = timestamp; + this.openFileDescriptors = openFileDescriptors; + this.maxFileDescriptors = maxFileDescriptors; + this.cpu = cpu; + this.mem = mem; + } - Cpu cpu = null; + public ProcessStats(StreamInput in) throws IOException { + timestamp = in.readVLong(); + openFileDescriptors = in.readLong(); + maxFileDescriptors = in.readLong(); + cpu = in.readOptionalWriteable(Cpu::new); + mem = in.readOptionalWriteable(Mem::new); + } - Mem mem = null; - - ProcessStats() { + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeVLong(timestamp); + out.writeLong(openFileDescriptors); + out.writeLong(maxFileDescriptors); + out.writeOptionalWriteable(cpu); + out.writeOptionalWriteable(mem); } public long getTimestamp() { @@ -100,59 +119,15 @@ public class ProcessStats implements Streamable, ToXContent { return builder; } - public static ProcessStats readProcessStats(StreamInput in) throws IOException { - ProcessStats stats = new ProcessStats(); - stats.readFrom(in); - return stats; - } + public static class Mem implements Writeable { - @Override - public void readFrom(StreamInput in) throws IOException { - timestamp = in.readVLong(); - openFileDescriptors = in.readLong(); - maxFileDescriptors = in.readLong(); - if (in.readBoolean()) { - cpu = Cpu.readCpu(in); - } - if (in.readBoolean()) { - mem = Mem.readMem(in); - } - } + private final long totalVirtual; - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeVLong(timestamp); - out.writeLong(openFileDescriptors); - out.writeLong(maxFileDescriptors); - if (cpu == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - cpu.writeTo(out); - } - if (mem == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - mem.writeTo(out); - } - } - - public static class Mem implements Streamable { - - long totalVirtual = -1; - - Mem() { + public Mem(long totalVirtual) { + this.totalVirtual = totalVirtual; } - public static Mem readMem(StreamInput in) throws IOException { - Mem mem = new Mem(); - mem.readFrom(in); - return mem; - } - - @Override - public void readFrom(StreamInput in) throws IOException { + public Mem(StreamInput in) throws IOException { totalVirtual = in.readLong(); } @@ -166,23 +141,17 @@ public class ProcessStats implements Streamable, ToXContent { } } - public static class Cpu implements Streamable { + public static class Cpu implements Writeable { - short percent = -1; - long total = -1; - - Cpu() { + private final short percent; + private final long total; + public Cpu(short percent, long total) { + this.percent = percent; + this.total = total; } - public static Cpu readCpu(StreamInput in) throws IOException { - Cpu cpu = new Cpu(); - cpu.readFrom(in); - return cpu; - } - - @Override - public void readFrom(StreamInput in) throws IOException { + public Cpu(StreamInput in) throws IOException { percent = in.readShort(); total = in.readLong(); } diff --git a/core/src/test/java/org/elasticsearch/monitor/process/ProcessProbeTests.java b/core/src/test/java/org/elasticsearch/monitor/process/ProcessProbeTests.java index 5423242ccd8..61c91368fa0 100644 --- a/core/src/test/java/org/elasticsearch/monitor/process/ProcessProbeTests.java +++ b/core/src/test/java/org/elasticsearch/monitor/process/ProcessProbeTests.java @@ -65,11 +65,11 @@ public class ProcessProbeTests extends ESTestCase { assertThat(cpu.getPercent(), anyOf(lessThan((short) 0), allOf(greaterThanOrEqualTo((short) 0), lessThanOrEqualTo((short) 100)))); // CPU time can return -1 if the platform does not support this operation, let's see which platforms fail - assertThat(cpu.total, greaterThan(0L)); + assertThat(cpu.getTotal().millis(), greaterThan(0L)); ProcessStats.Mem mem = stats.getMem(); assertNotNull(mem); // Commited total virtual memory can return -1 if not supported, let's see which platforms fail - assertThat(mem.totalVirtual, greaterThan(0L)); + assertThat(mem.getTotalVirtual().bytes(), greaterThan(0L)); } }