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 b227557ff9e..a21c99b68e2 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 @@ -211,18 +211,10 @@ public class NodeStats extends BaseNodeResponse implements ToXContent { if (in.readBoolean()) { indices = NodeIndicesStats.readIndicesStats(in); } - if (in.readBoolean()) { - os = new OsStats(in); - } - if (in.readBoolean()) { - process = new ProcessStats(in); - } - if (in.readBoolean()) { - jvm = new JvmStats(in); - } - if (in.readBoolean()) { - threadPool = ThreadPoolStats.readThreadPoolStats(in); - } + os = in.readOptionalWriteable(OsStats::new); + process = in.readOptionalWriteable(ProcessStats::new); + jvm = in.readOptionalWriteable(JvmStats::new); + threadPool = in.readOptionalWriteable(ThreadPoolStats::new); if (in.readBoolean()) { fs = new FsInfo(in); } @@ -248,30 +240,10 @@ public class NodeStats extends BaseNodeResponse implements ToXContent { out.writeBoolean(true); indices.writeTo(out); } - if (os == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - os.writeTo(out); - } - if (process == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - process.writeTo(out); - } - if (jvm == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - jvm.writeTo(out); - } - if (threadPool == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - threadPool.writeTo(out); - } + out.writeOptionalWriteable(os); + out.writeOptionalWriteable(process); + out.writeOptionalWriteable(jvm); + out.writeOptionalWriteable(threadPool); if (fs == null) { out.writeBoolean(false); } else { diff --git a/core/src/main/java/org/elasticsearch/threadpool/ThreadPoolStats.java b/core/src/main/java/org/elasticsearch/threadpool/ThreadPoolStats.java index fdbbaef19d6..ead076fc83b 100644 --- a/core/src/main/java/org/elasticsearch/threadpool/ThreadPoolStats.java +++ b/core/src/main/java/org/elasticsearch/threadpool/ThreadPoolStats.java @@ -21,33 +21,26 @@ package org.elasticsearch.threadpool; 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.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; -/** - */ -public class ThreadPoolStats implements Streamable, ToXContent, Iterable { +public class ThreadPoolStats implements Writeable, ToXContent, Iterable { - public static class Stats implements Streamable, ToXContent, Comparable { + public static class Stats implements Writeable, ToXContent, Comparable { - private String name; - private int threads; - private int queue; - private int active; - private long rejected; - private int largest; - private long completed; - - Stats() { - - } + private final String name; + private final int threads; + private final int queue; + private final int active; + private final long rejected; + private final int largest; + private final long completed; public Stats(String name, int threads, int queue, int active, long rejected, int largest, long completed) { this.name = name; @@ -59,6 +52,27 @@ public class ThreadPoolStats implements Streamable, ToXContent, Iterable stats; - ThreadPoolStats() { - - } - public ThreadPoolStats(List stats) { Collections.sort(stats); this.stats = stats; } - @Override - public Iterator iterator() { - return stats.iterator(); - } - - public static ThreadPoolStats readThreadPoolStats(StreamInput in) throws IOException { - ThreadPoolStats stats = new ThreadPoolStats(); - stats.readFrom(in); - return stats; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - int size = in.readVInt(); - stats = new ArrayList<>(size); - for (int i = 0; i < size; i++) { - Stats stats1 = new Stats(); - stats1.readFrom(in); - stats.add(stats1); - } + public ThreadPoolStats(StreamInput in) throws IOException { + stats = in.readList(Stats::new); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(stats.size()); - for (Stats stat : stats) { - stat.writeTo(out); - } + out.writeList(stats); + } + + @Override + public Iterator iterator() { + return stats.iterator(); } static final class Fields {