AllCircuitBreakerStats and CircuitBreakerStats to implement Writeable rather than Streamable

This commit is contained in:
javanna 2016-09-02 16:59:09 +02:00 committed by Luca Cavanna
parent 38a7427c51
commit b36bad6cc2
3 changed files with 39 additions and 71 deletions

View File

@ -218,7 +218,7 @@ public class NodeStats extends BaseNodeResponse implements ToXContent {
fs = in.readOptionalWriteable(FsInfo::new); fs = in.readOptionalWriteable(FsInfo::new);
transport = in.readOptionalWriteable(TransportStats::new); transport = in.readOptionalWriteable(TransportStats::new);
http = in.readOptionalWriteable(HttpStats::new); http = in.readOptionalWriteable(HttpStats::new);
breaker = AllCircuitBreakerStats.readOptionalAllCircuitBreakerStats(in); breaker = in.readOptionalWriteable(AllCircuitBreakerStats::new);
scriptStats = in.readOptionalWriteable(ScriptStats::new); scriptStats = in.readOptionalWriteable(ScriptStats::new);
discoveryStats = in.readOptionalWriteable(DiscoveryStats::new); discoveryStats = in.readOptionalWriteable(DiscoveryStats::new);
ingestStats = in.readOptionalWriteable(IngestStats::new); ingestStats = in.readOptionalWriteable(IngestStats::new);
@ -241,7 +241,7 @@ public class NodeStats extends BaseNodeResponse implements ToXContent {
out.writeOptionalWriteable(fs); out.writeOptionalWriteable(fs);
out.writeOptionalWriteable(transport); out.writeOptionalWriteable(transport);
out.writeOptionalWriteable(http); out.writeOptionalWriteable(http);
out.writeOptionalStreamable(breaker); out.writeOptionalWriteable(breaker);
out.writeOptionalWriteable(scriptStats); out.writeOptionalWriteable(scriptStats);
out.writeOptionalWriteable(discoveryStats); out.writeOptionalWriteable(discoveryStats);
out.writeOptionalWriteable(ingestStats); out.writeOptionalWriteable(ingestStats);

View File

@ -21,7 +21,7 @@ package org.elasticsearch.indices.breaker;
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;
@ -30,18 +30,23 @@ import java.io.IOException;
/** /**
* Stats class encapsulating all of the different circuit breaker stats * Stats class encapsulating all of the different circuit breaker stats
*/ */
public class AllCircuitBreakerStats implements Streamable, ToXContent { public class AllCircuitBreakerStats implements Writeable, ToXContent {
private CircuitBreakerStats[] allStats = new CircuitBreakerStats[0]; private final CircuitBreakerStats[] allStats;
public AllCircuitBreakerStats() {
}
public AllCircuitBreakerStats(CircuitBreakerStats[] allStats) { public AllCircuitBreakerStats(CircuitBreakerStats[] allStats) {
this.allStats = allStats; this.allStats = allStats;
} }
public AllCircuitBreakerStats(StreamInput in) throws IOException {
allStats = in.readArray(CircuitBreakerStats::new, CircuitBreakerStats[]::new);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeArray(allStats);
}
public CircuitBreakerStats[] getAllStats() { public CircuitBreakerStats[] getAllStats() {
return this.allStats; return this.allStats;
} }
@ -55,33 +60,6 @@ public class AllCircuitBreakerStats implements Streamable, ToXContent {
return null; return null;
} }
public static AllCircuitBreakerStats readOptionalAllCircuitBreakerStats(StreamInput in) throws IOException {
AllCircuitBreakerStats stats = in.readOptionalStreamable(AllCircuitBreakerStats::new);
return stats;
}
@Override
public void readFrom(StreamInput in) throws IOException {
int statCount = in.readVInt();
CircuitBreakerStats[] newStats = new CircuitBreakerStats[statCount];
for (int i = 0; i < statCount; i++) {
CircuitBreakerStats stats = new CircuitBreakerStats();
stats.readFrom(in);
newStats[i] = stats;
}
allStats = newStats;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(allStats.length);
for (CircuitBreakerStats stats : allStats) {
if (stats != null) {
stats.writeTo(out);
}
}
}
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(Fields.BREAKERS); builder.startObject(Fields.BREAKERS);

View File

@ -21,7 +21,7 @@ package org.elasticsearch.indices.breaker;
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;
@ -32,17 +32,13 @@ import java.util.Locale;
/** /**
* Class encapsulating stats about the circuit breaker * Class encapsulating stats about the circuit breaker
*/ */
public class CircuitBreakerStats implements Streamable, ToXContent { public class CircuitBreakerStats implements Writeable, ToXContent {
private String name; private final String name;
private long limit; private final long limit;
private long estimated; private final long estimated;
private long trippedCount; private final long trippedCount;
private double overhead; private final double overhead;
CircuitBreakerStats() {
}
public CircuitBreakerStats(String name, long limit, long estimated, double overhead, long trippedCount) { public CircuitBreakerStats(String name, long limit, long estimated, double overhead, long trippedCount) {
this.name = name; this.name = name;
@ -52,6 +48,24 @@ public class CircuitBreakerStats implements Streamable, ToXContent {
this.overhead = overhead; this.overhead = overhead;
} }
public CircuitBreakerStats(StreamInput in) throws IOException {
// limit is the maximum from the old circuit breaker stats for backwards compatibility
limit = in.readLong();
estimated = in.readLong();
overhead = in.readDouble();
this.trippedCount = in.readLong();
this.name = in.readString();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeLong(limit);
out.writeLong(estimated);
out.writeDouble(overhead);
out.writeLong(trippedCount);
out.writeString(name);
}
public String getName() { public String getName() {
return this.name; return this.name;
} }
@ -72,30 +86,6 @@ public class CircuitBreakerStats implements Streamable, ToXContent {
return this.overhead; return this.overhead;
} }
public static CircuitBreakerStats readOptionalCircuitBreakerStats(StreamInput in) throws IOException {
CircuitBreakerStats stats = in.readOptionalStreamable(CircuitBreakerStats::new);
return stats;
}
@Override
public void readFrom(StreamInput in) throws IOException {
// limit is the maximum from the old circuit breaker stats for backwards compatibility
limit = in.readLong();
estimated = in.readLong();
overhead = in.readDouble();
this.trippedCount = in.readLong();
this.name = in.readString();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeLong(limit);
out.writeLong(estimated);
out.writeDouble(overhead);
out.writeLong(trippedCount);
out.writeString(name);
}
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(name.toLowerCase(Locale.ROOT)); builder.startObject(name.toLowerCase(Locale.ROOT));