ThreadPoolStats to implement Writeable rather than Streamable

This commit is contained in:
javanna 2016-09-02 16:19:08 +02:00 committed by Luca Cavanna
parent 102dac2cd9
commit 9c62a12fee
2 changed files with 47 additions and 103 deletions

View File

@ -211,18 +211,10 @@ public class NodeStats extends BaseNodeResponse implements ToXContent {
if (in.readBoolean()) { if (in.readBoolean()) {
indices = NodeIndicesStats.readIndicesStats(in); indices = NodeIndicesStats.readIndicesStats(in);
} }
if (in.readBoolean()) { os = in.readOptionalWriteable(OsStats::new);
os = new OsStats(in); process = in.readOptionalWriteable(ProcessStats::new);
} jvm = in.readOptionalWriteable(JvmStats::new);
if (in.readBoolean()) { threadPool = in.readOptionalWriteable(ThreadPoolStats::new);
process = new ProcessStats(in);
}
if (in.readBoolean()) {
jvm = new JvmStats(in);
}
if (in.readBoolean()) {
threadPool = ThreadPoolStats.readThreadPoolStats(in);
}
if (in.readBoolean()) { if (in.readBoolean()) {
fs = new FsInfo(in); fs = new FsInfo(in);
} }
@ -248,30 +240,10 @@ public class NodeStats extends BaseNodeResponse implements ToXContent {
out.writeBoolean(true); out.writeBoolean(true);
indices.writeTo(out); indices.writeTo(out);
} }
if (os == null) { out.writeOptionalWriteable(os);
out.writeBoolean(false); out.writeOptionalWriteable(process);
} else { out.writeOptionalWriteable(jvm);
out.writeBoolean(true); out.writeOptionalWriteable(threadPool);
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);
}
if (fs == null) { if (fs == null) {
out.writeBoolean(false); out.writeBoolean(false);
} else { } else {

View File

@ -21,33 +21,26 @@ package org.elasticsearch.threadpool;
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.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
/** public class ThreadPoolStats implements Writeable, ToXContent, Iterable<ThreadPoolStats.Stats> {
*/
public class ThreadPoolStats implements Streamable, ToXContent, Iterable<ThreadPoolStats.Stats> {
public static class Stats implements Streamable, ToXContent, Comparable<Stats> { public static class Stats implements Writeable, ToXContent, Comparable<Stats> {
private String name; private final String name;
private int threads; private final int threads;
private int queue; private final int queue;
private int active; private final int active;
private long rejected; private final long rejected;
private int largest; private final int largest;
private long completed; private final long completed;
Stats() {
}
public Stats(String name, int threads, int queue, int active, long rejected, int largest, long completed) { public Stats(String name, int threads, int queue, int active, long rejected, int largest, long completed) {
this.name = name; this.name = name;
@ -59,6 +52,27 @@ public class ThreadPoolStats implements Streamable, ToXContent, Iterable<ThreadP
this.completed = completed; this.completed = completed;
} }
public Stats(StreamInput in) throws IOException {
name = in.readString();
threads = in.readInt();
queue = in.readInt();
active = in.readInt();
rejected = in.readLong();
largest = in.readInt();
completed = in.readLong();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeInt(threads);
out.writeInt(queue);
out.writeInt(active);
out.writeLong(rejected);
out.writeInt(largest);
out.writeLong(completed);
}
public String getName() { public String getName() {
return this.name; return this.name;
} }
@ -87,28 +101,6 @@ public class ThreadPoolStats implements Streamable, ToXContent, Iterable<ThreadP
return this.completed; return this.completed;
} }
@Override
public void readFrom(StreamInput in) throws IOException {
name = in.readString();
threads = in.readInt();
queue = in.readInt();
active = in.readInt();
rejected = in.readLong();
largest = in.readInt();
completed = in.readLong();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeInt(threads);
out.writeInt(queue);
out.writeInt(active);
out.writeLong(rejected);
out.writeInt(largest);
out.writeLong(completed);
}
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(name); builder.startObject(name);
@ -154,43 +146,23 @@ public class ThreadPoolStats implements Streamable, ToXContent, Iterable<ThreadP
private List<Stats> stats; private List<Stats> stats;
ThreadPoolStats() {
}
public ThreadPoolStats(List<Stats> stats) { public ThreadPoolStats(List<Stats> stats) {
Collections.sort(stats); Collections.sort(stats);
this.stats = stats; this.stats = stats;
} }
@Override public ThreadPoolStats(StreamInput in) throws IOException {
public Iterator<Stats> iterator() { stats = in.readList(Stats::new);
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);
}
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(stats.size()); out.writeList(stats);
for (Stats stat : stats) {
stat.writeTo(out);
} }
@Override
public Iterator<Stats> iterator() {
return stats.iterator();
} }
static final class Fields { static final class Fields {