From b36bad6cc2d99bd01184c4441ef622181fff903f Mon Sep 17 00:00:00 2001 From: javanna Date: Fri, 2 Sep 2016 16:59:09 +0200 Subject: [PATCH] AllCircuitBreakerStats and CircuitBreakerStats to implement Writeable rather than Streamable --- .../admin/cluster/node/stats/NodeStats.java | 4 +- .../breaker/AllCircuitBreakerStats.java | 46 ++++---------- .../indices/breaker/CircuitBreakerStats.java | 60 ++++++++----------- 3 files changed, 39 insertions(+), 71 deletions(-) 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 21415314303..eab33d8109a 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 @@ -218,7 +218,7 @@ public class NodeStats extends BaseNodeResponse implements ToXContent { fs = in.readOptionalWriteable(FsInfo::new); transport = in.readOptionalWriteable(TransportStats::new); http = in.readOptionalWriteable(HttpStats::new); - breaker = AllCircuitBreakerStats.readOptionalAllCircuitBreakerStats(in); + breaker = in.readOptionalWriteable(AllCircuitBreakerStats::new); scriptStats = in.readOptionalWriteable(ScriptStats::new); discoveryStats = in.readOptionalWriteable(DiscoveryStats::new); ingestStats = in.readOptionalWriteable(IngestStats::new); @@ -241,7 +241,7 @@ public class NodeStats extends BaseNodeResponse implements ToXContent { out.writeOptionalWriteable(fs); out.writeOptionalWriteable(transport); out.writeOptionalWriteable(http); - out.writeOptionalStreamable(breaker); + out.writeOptionalWriteable(breaker); out.writeOptionalWriteable(scriptStats); out.writeOptionalWriteable(discoveryStats); out.writeOptionalWriteable(ingestStats); diff --git a/core/src/main/java/org/elasticsearch/indices/breaker/AllCircuitBreakerStats.java b/core/src/main/java/org/elasticsearch/indices/breaker/AllCircuitBreakerStats.java index 09f0cd99001..3022d885e1a 100644 --- a/core/src/main/java/org/elasticsearch/indices/breaker/AllCircuitBreakerStats.java +++ b/core/src/main/java/org/elasticsearch/indices/breaker/AllCircuitBreakerStats.java @@ -21,7 +21,7 @@ package org.elasticsearch.indices.breaker; 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; @@ -30,18 +30,23 @@ import java.io.IOException; /** * 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]; - - public AllCircuitBreakerStats() { - - } + private final CircuitBreakerStats[] allStats; public AllCircuitBreakerStats(CircuitBreakerStats[] 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() { return this.allStats; } @@ -55,33 +60,6 @@ public class AllCircuitBreakerStats implements Streamable, ToXContent { 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 public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(Fields.BREAKERS); diff --git a/core/src/main/java/org/elasticsearch/indices/breaker/CircuitBreakerStats.java b/core/src/main/java/org/elasticsearch/indices/breaker/CircuitBreakerStats.java index eca235711bb..b2d6ef55dc7 100644 --- a/core/src/main/java/org/elasticsearch/indices/breaker/CircuitBreakerStats.java +++ b/core/src/main/java/org/elasticsearch/indices/breaker/CircuitBreakerStats.java @@ -21,7 +21,7 @@ package org.elasticsearch.indices.breaker; 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; @@ -32,17 +32,13 @@ import java.util.Locale; /** * Class encapsulating stats about the circuit breaker */ -public class CircuitBreakerStats implements Streamable, ToXContent { +public class CircuitBreakerStats implements Writeable, ToXContent { - private String name; - private long limit; - private long estimated; - private long trippedCount; - private double overhead; - - CircuitBreakerStats() { - - } + private final String name; + private final long limit; + private final long estimated; + private final long trippedCount; + private final double overhead; public CircuitBreakerStats(String name, long limit, long estimated, double overhead, long trippedCount) { this.name = name; @@ -52,6 +48,24 @@ public class CircuitBreakerStats implements Streamable, ToXContent { 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() { return this.name; } @@ -72,30 +86,6 @@ public class CircuitBreakerStats implements Streamable, ToXContent { 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 public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(name.toLowerCase(Locale.ROOT));