OsStats and subobjects to implement Writeable rather than Streamable
We can now have final instance members, also drop some optional values and related null checks that weren't needed.
This commit is contained in:
parent
34aaea641d
commit
0a7a52a31e
|
@ -212,7 +212,7 @@ public class NodeStats extends BaseNodeResponse implements ToXContent {
|
||||||
indices = NodeIndicesStats.readIndicesStats(in);
|
indices = NodeIndicesStats.readIndicesStats(in);
|
||||||
}
|
}
|
||||||
if (in.readBoolean()) {
|
if (in.readBoolean()) {
|
||||||
os = OsStats.readOsStats(in);
|
os = new OsStats(in);
|
||||||
}
|
}
|
||||||
if (in.readBoolean()) {
|
if (in.readBoolean()) {
|
||||||
process = ProcessStats.readProcessStats(in);
|
process = ProcessStats.readProcessStats(in);
|
||||||
|
|
|
@ -173,23 +173,10 @@ public class OsProbe {
|
||||||
}
|
}
|
||||||
|
|
||||||
public OsStats osStats() {
|
public OsStats osStats() {
|
||||||
OsStats stats = new OsStats();
|
OsStats.Cpu cpu = new OsStats.Cpu(getSystemCpuPercent(), getSystemLoadAverage());
|
||||||
stats.timestamp = System.currentTimeMillis();
|
OsStats.Mem mem = new OsStats.Mem(getTotalPhysicalMemorySize(), getFreePhysicalMemorySize());
|
||||||
stats.cpu = new OsStats.Cpu();
|
OsStats.Swap swap = new OsStats.Swap(getTotalSwapSpaceSize(), getFreeSwapSpaceSize());
|
||||||
stats.cpu.percent = getSystemCpuPercent();
|
return new OsStats(System.currentTimeMillis(), cpu, mem , swap);
|
||||||
stats.cpu.loadAverage = getSystemLoadAverage();
|
|
||||||
|
|
||||||
OsStats.Mem mem = new OsStats.Mem();
|
|
||||||
mem.total = getTotalPhysicalMemorySize();
|
|
||||||
mem.free = getFreePhysicalMemorySize();
|
|
||||||
stats.mem = mem;
|
|
||||||
|
|
||||||
OsStats.Swap swap = new OsStats.Swap();
|
|
||||||
swap.total = getTotalSwapSpaceSize();
|
|
||||||
swap.free = getFreeSwapSpaceSize();
|
|
||||||
stats.swap = swap;
|
|
||||||
|
|
||||||
return stats;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -21,7 +21,7 @@ package org.elasticsearch.monitor.os;
|
||||||
|
|
||||||
import org.elasticsearch.common.io.stream.StreamInput;
|
import org.elasticsearch.common.io.stream.StreamInput;
|
||||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
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.ByteSizeValue;
|
||||||
import org.elasticsearch.common.xcontent.ToXContent;
|
import org.elasticsearch.common.xcontent.ToXContent;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
|
@ -29,20 +29,33 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
public class OsStats implements Writeable, ToXContent {
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class OsStats implements Streamable, ToXContent {
|
|
||||||
|
|
||||||
long timestamp;
|
private final long timestamp;
|
||||||
|
private final Cpu cpu;
|
||||||
|
private final Mem mem;
|
||||||
|
private final Swap swap;
|
||||||
|
|
||||||
Cpu cpu = null;
|
public OsStats(long timestamp, Cpu cpu, Mem mem, Swap swap) {
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
this.cpu = cpu;
|
||||||
|
this.mem = mem;
|
||||||
|
this.swap = swap;
|
||||||
|
}
|
||||||
|
|
||||||
Mem mem = null;
|
public OsStats(StreamInput in) throws IOException {
|
||||||
|
this.timestamp = in.readVLong();
|
||||||
|
this.cpu = new Cpu(in);
|
||||||
|
this.mem = new Mem(in);
|
||||||
|
this.swap = new Swap(in);
|
||||||
|
}
|
||||||
|
|
||||||
Swap swap = null;
|
@Override
|
||||||
|
public void writeTo(StreamOutput out) throws IOException {
|
||||||
OsStats() {
|
out.writeVLong(timestamp);
|
||||||
|
cpu.writeTo(out);
|
||||||
|
mem.writeTo(out);
|
||||||
|
swap.writeTo(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getTimestamp() {
|
public long getTimestamp() {
|
||||||
|
@ -65,9 +78,9 @@ public class OsStats implements Streamable, ToXContent {
|
||||||
static final String CPU = "cpu";
|
static final String CPU = "cpu";
|
||||||
static final String PERCENT = "percent";
|
static final String PERCENT = "percent";
|
||||||
static final String LOAD_AVERAGE = "load_average";
|
static final String LOAD_AVERAGE = "load_average";
|
||||||
static final String LOAD_AVERAGE_1M = new String("1m");
|
static final String LOAD_AVERAGE_1M = "1m";
|
||||||
static final String LOAD_AVERAGE_5M = new String("5m");
|
static final String LOAD_AVERAGE_5M = "5m";
|
||||||
static final String LOAD_AVERAGE_15M = new String("15m");
|
static final String LOAD_AVERAGE_15M = "15m";
|
||||||
|
|
||||||
static final String MEM = "mem";
|
static final String MEM = "mem";
|
||||||
static final String SWAP = "swap";
|
static final String SWAP = "swap";
|
||||||
|
@ -86,105 +99,59 @@ public class OsStats implements Streamable, ToXContent {
|
||||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||||
builder.startObject(Fields.OS);
|
builder.startObject(Fields.OS);
|
||||||
builder.field(Fields.TIMESTAMP, getTimestamp());
|
builder.field(Fields.TIMESTAMP, getTimestamp());
|
||||||
if (cpu != null) {
|
builder.startObject(Fields.CPU);
|
||||||
builder.startObject(Fields.CPU);
|
builder.field(Fields.PERCENT, cpu.getPercent());
|
||||||
builder.field(Fields.PERCENT, cpu.getPercent());
|
if (cpu.getLoadAverage() != null && Arrays.stream(cpu.getLoadAverage()).anyMatch(load -> load != -1)) {
|
||||||
if (cpu.getLoadAverage() != null && Arrays.stream(cpu.getLoadAverage()).anyMatch(load -> load != -1)) {
|
builder.startObject(Fields.LOAD_AVERAGE);
|
||||||
builder.startObject(Fields.LOAD_AVERAGE);
|
if (cpu.getLoadAverage()[0] != -1) {
|
||||||
if (cpu.getLoadAverage()[0] != -1) {
|
builder.field(Fields.LOAD_AVERAGE_1M, cpu.getLoadAverage()[0]);
|
||||||
builder.field(Fields.LOAD_AVERAGE_1M, cpu.getLoadAverage()[0]);
|
}
|
||||||
}
|
if (cpu.getLoadAverage()[1] != -1) {
|
||||||
if (cpu.getLoadAverage()[1] != -1) {
|
builder.field(Fields.LOAD_AVERAGE_5M, cpu.getLoadAverage()[1]);
|
||||||
builder.field(Fields.LOAD_AVERAGE_5M, cpu.getLoadAverage()[1]);
|
}
|
||||||
}
|
if (cpu.getLoadAverage()[2] != -1) {
|
||||||
if (cpu.getLoadAverage()[2] != -1) {
|
builder.field(Fields.LOAD_AVERAGE_15M, cpu.getLoadAverage()[2]);
|
||||||
builder.field(Fields.LOAD_AVERAGE_15M, cpu.getLoadAverage()[2]);
|
|
||||||
}
|
|
||||||
builder.endObject();
|
|
||||||
}
|
}
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
}
|
}
|
||||||
|
builder.endObject();
|
||||||
|
|
||||||
if (mem != null) {
|
builder.startObject(Fields.MEM);
|
||||||
builder.startObject(Fields.MEM);
|
builder.byteSizeField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, mem.getTotal());
|
||||||
builder.byteSizeField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, mem.getTotal());
|
builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, mem.getFree());
|
||||||
builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, mem.getFree());
|
builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, mem.getUsed());
|
||||||
builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, mem.getUsed());
|
|
||||||
|
|
||||||
builder.field(Fields.FREE_PERCENT, mem.getFreePercent());
|
builder.field(Fields.FREE_PERCENT, mem.getFreePercent());
|
||||||
builder.field(Fields.USED_PERCENT, mem.getUsedPercent());
|
builder.field(Fields.USED_PERCENT, mem.getUsedPercent());
|
||||||
|
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
}
|
|
||||||
|
|
||||||
if (swap != null) {
|
builder.startObject(Fields.SWAP);
|
||||||
builder.startObject(Fields.SWAP);
|
builder.byteSizeField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, swap.getTotal());
|
||||||
builder.byteSizeField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, swap.getTotal());
|
builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, swap.getFree());
|
||||||
builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, swap.getFree());
|
builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, swap.getUsed());
|
||||||
builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, swap.getUsed());
|
builder.endObject();
|
||||||
builder.endObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static OsStats readOsStats(StreamInput in) throws IOException {
|
public static class Cpu implements Writeable {
|
||||||
OsStats stats = new OsStats();
|
|
||||||
stats.readFrom(in);
|
|
||||||
return stats;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
private final short percent;
|
||||||
public void readFrom(StreamInput in) throws IOException {
|
private final double[] loadAverage;
|
||||||
timestamp = in.readVLong();
|
|
||||||
cpu = in.readOptionalStreamable(Cpu::new);
|
|
||||||
if (in.readBoolean()) {
|
|
||||||
mem = Mem.readMem(in);
|
|
||||||
}
|
|
||||||
if (in.readBoolean()) {
|
|
||||||
swap = Swap.readSwap(in);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public Cpu(short systemCpuPercent, double[] systemLoadAverage) {
|
||||||
public void writeTo(StreamOutput out) throws IOException {
|
this.percent = systemCpuPercent;
|
||||||
out.writeVLong(timestamp);
|
this.loadAverage = systemLoadAverage;
|
||||||
out.writeOptionalStreamable(cpu);
|
|
||||||
if (mem == null) {
|
|
||||||
out.writeBoolean(false);
|
|
||||||
} else {
|
|
||||||
out.writeBoolean(true);
|
|
||||||
mem.writeTo(out);
|
|
||||||
}
|
|
||||||
if (swap == null) {
|
|
||||||
out.writeBoolean(false);
|
|
||||||
} else {
|
|
||||||
out.writeBoolean(true);
|
|
||||||
swap.writeTo(out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Cpu implements Streamable {
|
|
||||||
|
|
||||||
short percent = -1;
|
|
||||||
double[] loadAverage = null;
|
|
||||||
|
|
||||||
Cpu() {}
|
|
||||||
|
|
||||||
public static Cpu readCpu(StreamInput in) throws IOException {
|
|
||||||
Cpu cpu = new Cpu();
|
|
||||||
cpu.readFrom(in);
|
|
||||||
return cpu;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public Cpu(StreamInput in) throws IOException {
|
||||||
public void readFrom(StreamInput in) throws IOException {
|
this.percent = in.readShort();
|
||||||
percent = in.readShort();
|
|
||||||
if (in.readBoolean()) {
|
if (in.readBoolean()) {
|
||||||
loadAverage = in.readDoubleArray();
|
this.loadAverage = in.readDoubleArray();
|
||||||
} else {
|
} else {
|
||||||
loadAverage = null;
|
this.loadAverage = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,10 +175,26 @@ public class OsStats implements Streamable, ToXContent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Swap implements Streamable {
|
public static class Swap implements Writeable {
|
||||||
|
|
||||||
long total = -1;
|
private final long total;
|
||||||
long free = -1;
|
private final long free;
|
||||||
|
|
||||||
|
public Swap(long total, long free) {
|
||||||
|
this.total = total;
|
||||||
|
this.free = free;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Swap(StreamInput in) throws IOException {
|
||||||
|
this.total = in.readLong();
|
||||||
|
this.free = in.readLong();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeTo(StreamOutput out) throws IOException {
|
||||||
|
out.writeLong(total);
|
||||||
|
out.writeLong(free);
|
||||||
|
}
|
||||||
|
|
||||||
public ByteSizeValue getFree() {
|
public ByteSizeValue getFree() {
|
||||||
return new ByteSizeValue(free);
|
return new ByteSizeValue(free);
|
||||||
|
@ -224,41 +207,21 @@ public class OsStats implements Streamable, ToXContent {
|
||||||
public ByteSizeValue getTotal() {
|
public ByteSizeValue getTotal() {
|
||||||
return new ByteSizeValue(total);
|
return new ByteSizeValue(total);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Swap readSwap(StreamInput in) throws IOException {
|
|
||||||
Swap swap = new Swap();
|
|
||||||
swap.readFrom(in);
|
|
||||||
return swap;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void readFrom(StreamInput in) throws IOException {
|
|
||||||
total = in.readLong();
|
|
||||||
free = in.readLong();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void writeTo(StreamOutput out) throws IOException {
|
|
||||||
out.writeLong(total);
|
|
||||||
out.writeLong(free);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Mem implements Streamable {
|
public static class Mem implements Writeable {
|
||||||
|
|
||||||
long total = -1;
|
private final long total;
|
||||||
long free = -1;
|
private final long free;
|
||||||
|
|
||||||
public static Mem readMem(StreamInput in) throws IOException {
|
public Mem(long total, long free) {
|
||||||
Mem mem = new Mem();
|
this.total = total;
|
||||||
mem.readFrom(in);
|
this.free = free;
|
||||||
return mem;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public Mem(StreamInput in) throws IOException {
|
||||||
public void readFrom(StreamInput in) throws IOException {
|
this.total = in.readLong();
|
||||||
total = in.readLong();
|
this.free = in.readLong();
|
||||||
free = in.readLong();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -276,7 +239,7 @@ public class OsStats implements Streamable, ToXContent {
|
||||||
}
|
}
|
||||||
|
|
||||||
public short getUsedPercent() {
|
public short getUsedPercent() {
|
||||||
return calculatePercentage(getUsed().bytes(), getTotal().bytes());
|
return calculatePercentage(getUsed().bytes(), total);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ByteSizeValue getFree() {
|
public ByteSizeValue getFree() {
|
||||||
|
@ -284,7 +247,7 @@ public class OsStats implements Streamable, ToXContent {
|
||||||
}
|
}
|
||||||
|
|
||||||
public short getFreePercent() {
|
public short getFreePercent() {
|
||||||
return calculatePercentage(getFree().bytes(), getTotal().bytes());
|
return calculatePercentage(free, total);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ public class OsProbeTests extends ESTestCase {
|
||||||
assertNotNull(stats);
|
assertNotNull(stats);
|
||||||
assertThat(stats.getTimestamp(), greaterThan(0L));
|
assertThat(stats.getTimestamp(), greaterThan(0L));
|
||||||
assertThat(stats.getCpu().getPercent(), anyOf(equalTo((short) -1), is(both(greaterThanOrEqualTo((short) 0)).and(lessThanOrEqualTo((short) 100)))));
|
assertThat(stats.getCpu().getPercent(), anyOf(equalTo((short) -1), is(both(greaterThanOrEqualTo((short) 0)).and(lessThanOrEqualTo((short) 100)))));
|
||||||
double[] loadAverage = stats.getCpu().loadAverage;
|
double[] loadAverage = stats.getCpu().getLoadAverage();
|
||||||
if (loadAverage != null) {
|
if (loadAverage != null) {
|
||||||
assertThat(loadAverage.length, equalTo(3));
|
assertThat(loadAverage.length, equalTo(3));
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
/*
|
||||||
|
* Licensed to Elasticsearch under one or more contributor
|
||||||
|
* license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright
|
||||||
|
* ownership. Elasticsearch licenses this file to you under
|
||||||
|
* the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
* not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.elasticsearch.monitor.os;
|
||||||
|
|
||||||
|
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
||||||
|
import org.elasticsearch.common.io.stream.StreamInput;
|
||||||
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class OsStatsTests extends ESTestCase {
|
||||||
|
|
||||||
|
public void testSerialization() throws IOException {
|
||||||
|
int numLoadAverages = randomIntBetween(1, 5);
|
||||||
|
double loadAverages[] = new double[numLoadAverages];
|
||||||
|
for (int i = 0; i < loadAverages.length; i++) {
|
||||||
|
loadAverages[i] = randomDouble();
|
||||||
|
}
|
||||||
|
OsStats.Cpu cpu = new OsStats.Cpu(randomShort(), loadAverages);
|
||||||
|
OsStats.Mem mem = new OsStats.Mem(randomLong(), randomLong());
|
||||||
|
OsStats.Swap swap = new OsStats.Swap(randomLong(), randomLong());
|
||||||
|
OsStats osStats = new OsStats(System.currentTimeMillis(), cpu, mem, swap);
|
||||||
|
|
||||||
|
try (BytesStreamOutput out = new BytesStreamOutput()) {
|
||||||
|
osStats.writeTo(out);
|
||||||
|
try (StreamInput in = out.bytes().streamInput()) {
|
||||||
|
OsStats deserializedOsStats = new OsStats(in);
|
||||||
|
assertEquals(osStats.getTimestamp(), deserializedOsStats.getTimestamp());
|
||||||
|
assertEquals(osStats.getCpu().getPercent(), deserializedOsStats.getCpu().getPercent());
|
||||||
|
assertArrayEquals(osStats.getCpu().getLoadAverage(), deserializedOsStats.getCpu().getLoadAverage(), 0);
|
||||||
|
assertEquals(osStats.getMem().getFree(), deserializedOsStats.getMem().getFree());
|
||||||
|
assertEquals(osStats.getMem().getTotal(), deserializedOsStats.getMem().getTotal());
|
||||||
|
assertEquals(osStats.getSwap().getFree(), deserializedOsStats.getSwap().getFree());
|
||||||
|
assertEquals(osStats.getSwap().getTotal(), deserializedOsStats.getSwap().getTotal());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue