ProcessStats to implement Writeable rather than Streamable

This commit is contained in:
javanna 2016-09-02 15:02:54 +02:00 committed by Luca Cavanna
parent 55d9e99f51
commit 931a164b1f
4 changed files with 46 additions and 89 deletions

View File

@ -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);

View File

@ -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);
}
/**

View File

@ -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();
}

View File

@ -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));
}
}