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:
javanna 2016-08-31 14:28:21 +02:00 committed by Luca Cavanna
parent 34aaea641d
commit 0a7a52a31e
5 changed files with 153 additions and 148 deletions

View File

@ -212,7 +212,7 @@ public class NodeStats extends BaseNodeResponse implements ToXContent {
indices = NodeIndicesStats.readIndicesStats(in);
}
if (in.readBoolean()) {
os = OsStats.readOsStats(in);
os = new OsStats(in);
}
if (in.readBoolean()) {
process = ProcessStats.readProcessStats(in);

View File

@ -173,23 +173,10 @@ public class OsProbe {
}
public OsStats osStats() {
OsStats stats = new OsStats();
stats.timestamp = System.currentTimeMillis();
stats.cpu = new OsStats.Cpu();
stats.cpu.percent = getSystemCpuPercent();
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;
OsStats.Cpu cpu = new OsStats.Cpu(getSystemCpuPercent(), getSystemLoadAverage());
OsStats.Mem mem = new OsStats.Mem(getTotalPhysicalMemorySize(), getFreePhysicalMemorySize());
OsStats.Swap swap = new OsStats.Swap(getTotalSwapSpaceSize(), getFreeSwapSpaceSize());
return new OsStats(System.currentTimeMillis(), cpu, mem , swap);
}
/**

View File

@ -21,7 +21,7 @@ package org.elasticsearch.monitor.os;
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.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -29,20 +29,33 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.Arrays;
/**
*
*/
public class OsStats implements Streamable, ToXContent {
public class OsStats implements Writeable, 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;
OsStats() {
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(timestamp);
cpu.writeTo(out);
mem.writeTo(out);
swap.writeTo(out);
}
public long getTimestamp() {
@ -65,9 +78,9 @@ public class OsStats implements Streamable, ToXContent {
static final String CPU = "cpu";
static final String PERCENT = "percent";
static final String LOAD_AVERAGE = "load_average";
static final String LOAD_AVERAGE_1M = new String("1m");
static final String LOAD_AVERAGE_5M = new String("5m");
static final String LOAD_AVERAGE_15M = new String("15m");
static final String LOAD_AVERAGE_1M = "1m";
static final String LOAD_AVERAGE_5M = "5m";
static final String LOAD_AVERAGE_15M = "15m";
static final String MEM = "mem";
static final String SWAP = "swap";
@ -86,105 +99,59 @@ public class OsStats implements Streamable, ToXContent {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(Fields.OS);
builder.field(Fields.TIMESTAMP, getTimestamp());
if (cpu != null) {
builder.startObject(Fields.CPU);
builder.field(Fields.PERCENT, cpu.getPercent());
if (cpu.getLoadAverage() != null && Arrays.stream(cpu.getLoadAverage()).anyMatch(load -> load != -1)) {
builder.startObject(Fields.LOAD_AVERAGE);
if (cpu.getLoadAverage()[0] != -1) {
builder.field(Fields.LOAD_AVERAGE_1M, cpu.getLoadAverage()[0]);
}
if (cpu.getLoadAverage()[1] != -1) {
builder.field(Fields.LOAD_AVERAGE_5M, cpu.getLoadAverage()[1]);
}
if (cpu.getLoadAverage()[2] != -1) {
builder.field(Fields.LOAD_AVERAGE_15M, cpu.getLoadAverage()[2]);
}
builder.endObject();
builder.startObject(Fields.CPU);
builder.field(Fields.PERCENT, cpu.getPercent());
if (cpu.getLoadAverage() != null && Arrays.stream(cpu.getLoadAverage()).anyMatch(load -> load != -1)) {
builder.startObject(Fields.LOAD_AVERAGE);
if (cpu.getLoadAverage()[0] != -1) {
builder.field(Fields.LOAD_AVERAGE_1M, cpu.getLoadAverage()[0]);
}
if (cpu.getLoadAverage()[1] != -1) {
builder.field(Fields.LOAD_AVERAGE_5M, cpu.getLoadAverage()[1]);
}
if (cpu.getLoadAverage()[2] != -1) {
builder.field(Fields.LOAD_AVERAGE_15M, cpu.getLoadAverage()[2]);
}
builder.endObject();
}
builder.endObject();
if (mem != null) {
builder.startObject(Fields.MEM);
builder.byteSizeField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, mem.getTotal());
builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, mem.getFree());
builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, mem.getUsed());
builder.startObject(Fields.MEM);
builder.byteSizeField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, mem.getTotal());
builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, mem.getFree());
builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, mem.getUsed());
builder.field(Fields.FREE_PERCENT, mem.getFreePercent());
builder.field(Fields.USED_PERCENT, mem.getUsedPercent());
builder.field(Fields.FREE_PERCENT, mem.getFreePercent());
builder.field(Fields.USED_PERCENT, mem.getUsedPercent());
builder.endObject();
}
builder.endObject();
if (swap != null) {
builder.startObject(Fields.SWAP);
builder.byteSizeField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, swap.getTotal());
builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, swap.getFree());
builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, swap.getUsed());
builder.endObject();
}
builder.startObject(Fields.SWAP);
builder.byteSizeField(Fields.TOTAL_IN_BYTES, Fields.TOTAL, swap.getTotal());
builder.byteSizeField(Fields.FREE_IN_BYTES, Fields.FREE, swap.getFree());
builder.byteSizeField(Fields.USED_IN_BYTES, Fields.USED, swap.getUsed());
builder.endObject();
builder.endObject();
return builder;
}
public static OsStats readOsStats(StreamInput in) throws IOException {
OsStats stats = new OsStats();
stats.readFrom(in);
return stats;
}
public static class Cpu implements Writeable {
@Override
public void readFrom(StreamInput in) throws IOException {
timestamp = in.readVLong();
cpu = in.readOptionalStreamable(Cpu::new);
if (in.readBoolean()) {
mem = Mem.readMem(in);
}
if (in.readBoolean()) {
swap = Swap.readSwap(in);
}
}
private final short percent;
private final double[] loadAverage;
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(timestamp);
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;
public Cpu(short systemCpuPercent, double[] systemLoadAverage) {
this.percent = systemCpuPercent;
this.loadAverage = systemLoadAverage;
}
@Override
public void readFrom(StreamInput in) throws IOException {
percent = in.readShort();
public Cpu(StreamInput in) throws IOException {
this.percent = in.readShort();
if (in.readBoolean()) {
loadAverage = in.readDoubleArray();
this.loadAverage = in.readDoubleArray();
} 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;
long free = -1;
private final long total;
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() {
return new ByteSizeValue(free);
@ -224,41 +207,21 @@ public class OsStats implements Streamable, ToXContent {
public ByteSizeValue getTotal() {
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;
long free = -1;
private final long total;
private final long free;
public static Mem readMem(StreamInput in) throws IOException {
Mem mem = new Mem();
mem.readFrom(in);
return mem;
public Mem(long total, long free) {
this.total = total;
this.free = free;
}
@Override
public void readFrom(StreamInput in) throws IOException {
total = in.readLong();
free = in.readLong();
public Mem(StreamInput in) throws IOException {
this.total = in.readLong();
this.free = in.readLong();
}
@Override
@ -276,7 +239,7 @@ public class OsStats implements Streamable, ToXContent {
}
public short getUsedPercent() {
return calculatePercentage(getUsed().bytes(), getTotal().bytes());
return calculatePercentage(getUsed().bytes(), total);
}
public ByteSizeValue getFree() {
@ -284,7 +247,7 @@ public class OsStats implements Streamable, ToXContent {
}
public short getFreePercent() {
return calculatePercentage(getFree().bytes(), getTotal().bytes());
return calculatePercentage(free, total);
}
}

View File

@ -49,7 +49,7 @@ public class OsProbeTests extends ESTestCase {
assertNotNull(stats);
assertThat(stats.getTimestamp(), greaterThan(0L));
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) {
assertThat(loadAverage.length, equalTo(3));
}

View File

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