CommonStats and CommonStatsFlags to implement Writeable rather than Streamable

This commit is contained in:
javanna 2016-09-02 17:36:15 +02:00 committed by Luca Cavanna
parent b36bad6cc2
commit f1b1d1cae0
8 changed files with 224 additions and 256 deletions

View File

@ -269,11 +269,9 @@ public class NodeStats extends BaseNodeResponse implements ToXContent {
builder.endObject(); builder.endObject();
} }
} }
if (getIndices() != null) { if (getIndices() != null) {
getIndices().toXContent(builder, params); getIndices().toXContent(builder, params);
} }
if (getOs() != null) { if (getOs() != null) {
getOs().toXContent(builder, params); getOs().toXContent(builder, params);
} }
@ -301,15 +299,12 @@ public class NodeStats extends BaseNodeResponse implements ToXContent {
if (getScriptStats() != null) { if (getScriptStats() != null) {
getScriptStats().toXContent(builder, params); getScriptStats().toXContent(builder, params);
} }
if (getDiscoveryStats() != null) { if (getDiscoveryStats() != null) {
getDiscoveryStats().toXContent(builder, params); getDiscoveryStats().toXContent(builder, params);
} }
if (getIngestStats() != null) { if (getIngestStats() != null) {
getIngestStats().toXContent(builder, params); getIngestStats().toXContent(builder, params);
} }
return builder; return builder;
} }
} }

View File

@ -268,7 +268,7 @@ public class NodesStatsRequest extends BaseNodesRequest<NodesStatsRequest> {
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
super.readFrom(in); super.readFrom(in);
indices = CommonStatsFlags.readCommonStatsFlags(in); indices = new CommonStatsFlags(in);
os = in.readBoolean(); os = in.readBoolean();
process = in.readBoolean(); process = in.readBoolean();
jvm = in.readBoolean(); jvm = in.readBoolean();
@ -298,5 +298,4 @@ public class NodesStatsRequest extends BaseNodesRequest<NodesStatsRequest> {
out.writeBoolean(discovery); out.writeBoolean(discovery);
out.writeBoolean(ingest); out.writeBoolean(ingest);
} }
} }

View File

@ -22,7 +22,7 @@ package org.elasticsearch.action.admin.indices.stats;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
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,13 +32,13 @@ import org.elasticsearch.index.engine.SegmentsStats;
import org.elasticsearch.index.fielddata.FieldDataStats; import org.elasticsearch.index.fielddata.FieldDataStats;
import org.elasticsearch.index.flush.FlushStats; import org.elasticsearch.index.flush.FlushStats;
import org.elasticsearch.index.get.GetStats; import org.elasticsearch.index.get.GetStats;
import org.elasticsearch.index.shard.IndexingStats;
import org.elasticsearch.index.merge.MergeStats; import org.elasticsearch.index.merge.MergeStats;
import org.elasticsearch.index.recovery.RecoveryStats; import org.elasticsearch.index.recovery.RecoveryStats;
import org.elasticsearch.index.refresh.RefreshStats; import org.elasticsearch.index.refresh.RefreshStats;
import org.elasticsearch.index.search.stats.SearchStats; import org.elasticsearch.index.search.stats.SearchStats;
import org.elasticsearch.index.shard.DocsStats; import org.elasticsearch.index.shard.DocsStats;
import org.elasticsearch.index.shard.IndexShard; import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.index.shard.IndexingStats;
import org.elasticsearch.index.store.StoreStats; import org.elasticsearch.index.store.StoreStats;
import org.elasticsearch.index.translog.TranslogStats; import org.elasticsearch.index.translog.TranslogStats;
import org.elasticsearch.index.warmer.WarmerStats; import org.elasticsearch.index.warmer.WarmerStats;
@ -47,9 +47,55 @@ import org.elasticsearch.search.suggest.completion.CompletionStats;
import java.io.IOException; import java.io.IOException;
/** public class CommonStats implements Writeable, ToXContent {
*/
public class CommonStats implements Streamable, ToXContent { @Nullable
public DocsStats docs;
@Nullable
public StoreStats store;
@Nullable
public IndexingStats indexing;
@Nullable
public GetStats get;
@Nullable
public SearchStats search;
@Nullable
public MergeStats merge;
@Nullable
public RefreshStats refresh;
@Nullable
public FlushStats flush;
@Nullable
public WarmerStats warmer;
@Nullable
public QueryCacheStats queryCache;
@Nullable
public FieldDataStats fieldData;
@Nullable
public CompletionStats completion;
@Nullable
public SegmentsStats segments;
@Nullable
public TranslogStats translog;
@Nullable
public RequestCacheStats requestCache;
@Nullable
public RecoveryStats recoveryStats;
public CommonStats() { public CommonStats() {
this(CommonStatsFlags.NONE); this(CommonStatsFlags.NONE);
@ -117,11 +163,8 @@ public class CommonStats implements Streamable, ToXContent {
} }
} }
public CommonStats(IndicesQueryCache indicesQueryCache, IndexShard indexShard, CommonStatsFlags flags) { public CommonStats(IndicesQueryCache indicesQueryCache, IndexShard indexShard, CommonStatsFlags flags) {
CommonStatsFlags.Flag[] setFlags = flags.getFlags(); CommonStatsFlags.Flag[] setFlags = flags.getFlags();
for (CommonStatsFlags.Flag flag : setFlags) { for (CommonStatsFlags.Flag flag : setFlags) {
switch (flag) { switch (flag) {
case Docs: case Docs:
@ -181,53 +224,135 @@ public class CommonStats implements Streamable, ToXContent {
} }
} }
@Nullable public CommonStats(StreamInput in) throws IOException {
public DocsStats docs; if (in.readBoolean()) {
docs = DocsStats.readDocStats(in);
}
if (in.readBoolean()) {
store = StoreStats.readStoreStats(in);
}
if (in.readBoolean()) {
indexing = IndexingStats.readIndexingStats(in);
}
if (in.readBoolean()) {
get = GetStats.readGetStats(in);
}
if (in.readBoolean()) {
search = SearchStats.readSearchStats(in);
}
if (in.readBoolean()) {
merge = MergeStats.readMergeStats(in);
}
if (in.readBoolean()) {
refresh = RefreshStats.readRefreshStats(in);
}
if (in.readBoolean()) {
flush = FlushStats.readFlushStats(in);
}
if (in.readBoolean()) {
warmer = WarmerStats.readWarmerStats(in);
}
if (in.readBoolean()) {
queryCache = QueryCacheStats.readQueryCacheStats(in);
}
if (in.readBoolean()) {
fieldData = FieldDataStats.readFieldDataStats(in);
}
if (in.readBoolean()) {
completion = CompletionStats.readCompletionStats(in);
}
if (in.readBoolean()) {
segments = SegmentsStats.readSegmentsStats(in);
}
translog = in.readOptionalStreamable(TranslogStats::new);
requestCache = in.readOptionalStreamable(RequestCacheStats::new);
recoveryStats = in.readOptionalStreamable(RecoveryStats::new);
}
@Nullable @Override
public StoreStats store; public void writeTo(StreamOutput out) throws IOException {
if (docs == null) {
@Nullable out.writeBoolean(false);
public IndexingStats indexing; } else {
out.writeBoolean(true);
@Nullable docs.writeTo(out);
public GetStats get; }
if (store == null) {
@Nullable out.writeBoolean(false);
public SearchStats search; } else {
out.writeBoolean(true);
@Nullable store.writeTo(out);
public MergeStats merge; }
if (indexing == null) {
@Nullable out.writeBoolean(false);
public RefreshStats refresh; } else {
out.writeBoolean(true);
@Nullable indexing.writeTo(out);
public FlushStats flush; }
if (get == null) {
@Nullable out.writeBoolean(false);
public WarmerStats warmer; } else {
out.writeBoolean(true);
@Nullable get.writeTo(out);
public QueryCacheStats queryCache; }
if (search == null) {
@Nullable out.writeBoolean(false);
public FieldDataStats fieldData; } else {
out.writeBoolean(true);
@Nullable search.writeTo(out);
public CompletionStats completion; }
if (merge == null) {
@Nullable out.writeBoolean(false);
public SegmentsStats segments; } else {
out.writeBoolean(true);
@Nullable merge.writeTo(out);
public TranslogStats translog; }
if (refresh == null) {
@Nullable out.writeBoolean(false);
public RequestCacheStats requestCache; } else {
out.writeBoolean(true);
@Nullable refresh.writeTo(out);
public RecoveryStats recoveryStats; }
if (flush == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
flush.writeTo(out);
}
if (warmer == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
warmer.writeTo(out);
}
if (queryCache == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
queryCache.writeTo(out);
}
if (fieldData == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
fieldData.writeTo(out);
}
if (completion == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
completion.writeTo(out);
}
if (segments == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
segments.writeTo(out);
}
out.writeOptionalStreamable(translog);
out.writeOptionalStreamable(requestCache);
out.writeOptionalStreamable(recoveryStats);
}
public void add(CommonStats stats) { public void add(CommonStats stats) {
if (docs == null) { if (docs == null) {
@ -441,12 +566,6 @@ public class CommonStats implements Streamable, ToXContent {
return recoveryStats; return recoveryStats;
} }
public static CommonStats readCommonStats(StreamInput in) throws IOException {
CommonStats stats = new CommonStats();
stats.readFrom(in);
return stats;
}
/** /**
* Utility method which computes total memory by adding * Utility method which computes total memory by adding
* FieldData, PercolatorCache, Segments (memory, index writer, version map) * FieldData, PercolatorCache, Segments (memory, index writer, version map)
@ -468,137 +587,6 @@ public class CommonStats implements Streamable, ToXContent {
return new ByteSizeValue(size); return new ByteSizeValue(size);
} }
@Override
public void readFrom(StreamInput in) throws IOException {
if (in.readBoolean()) {
docs = DocsStats.readDocStats(in);
}
if (in.readBoolean()) {
store = StoreStats.readStoreStats(in);
}
if (in.readBoolean()) {
indexing = IndexingStats.readIndexingStats(in);
}
if (in.readBoolean()) {
get = GetStats.readGetStats(in);
}
if (in.readBoolean()) {
search = SearchStats.readSearchStats(in);
}
if (in.readBoolean()) {
merge = MergeStats.readMergeStats(in);
}
if (in.readBoolean()) {
refresh = RefreshStats.readRefreshStats(in);
}
if (in.readBoolean()) {
flush = FlushStats.readFlushStats(in);
}
if (in.readBoolean()) {
warmer = WarmerStats.readWarmerStats(in);
}
if (in.readBoolean()) {
queryCache = QueryCacheStats.readQueryCacheStats(in);
}
if (in.readBoolean()) {
fieldData = FieldDataStats.readFieldDataStats(in);
}
if (in.readBoolean()) {
completion = CompletionStats.readCompletionStats(in);
}
if (in.readBoolean()) {
segments = SegmentsStats.readSegmentsStats(in);
}
translog = in.readOptionalStreamable(TranslogStats::new);
requestCache = in.readOptionalStreamable(RequestCacheStats::new);
recoveryStats = in.readOptionalStreamable(RecoveryStats::new);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
if (docs == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
docs.writeTo(out);
}
if (store == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
store.writeTo(out);
}
if (indexing == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
indexing.writeTo(out);
}
if (get == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
get.writeTo(out);
}
if (search == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
search.writeTo(out);
}
if (merge == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
merge.writeTo(out);
}
if (refresh == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
refresh.writeTo(out);
}
if (flush == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
flush.writeTo(out);
}
if (warmer == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
warmer.writeTo(out);
}
if (queryCache == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
queryCache.writeTo(out);
}
if (fieldData == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
fieldData.writeTo(out);
}
if (completion == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
completion.writeTo(out);
}
if (segments == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
segments.writeTo(out);
}
out.writeOptionalStreamable(translog);
out.writeOptionalStreamable(requestCache);
out.writeOptionalStreamable(recoveryStats);
}
// note, requires a wrapping object // note, requires a wrapping object
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {

View File

@ -22,14 +22,12 @@ package org.elasticsearch.action.admin.indices.stats;
import org.elasticsearch.Version; import org.elasticsearch.Version;
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 java.io.IOException; import java.io.IOException;
import java.util.EnumSet; import java.util.EnumSet;
/** public class CommonStatsFlags implements Writeable, Cloneable {
*/
public class CommonStatsFlags implements Streamable, Cloneable {
public static final CommonStatsFlags ALL = new CommonStatsFlags().all(); public static final CommonStatsFlags ALL = new CommonStatsFlags().all();
public static final CommonStatsFlags NONE = new CommonStatsFlags().clear(); public static final CommonStatsFlags NONE = new CommonStatsFlags().clear();
@ -41,7 +39,6 @@ public class CommonStatsFlags implements Streamable, Cloneable {
private String[] completionDataFields = null; private String[] completionDataFields = null;
private boolean includeSegmentFileSizes = false; private boolean includeSegmentFileSizes = false;
/** /**
* @param flags flags to set. If no flags are supplied, default flags will be set. * @param flags flags to set. If no flags are supplied, default flags will be set.
*/ */
@ -54,6 +51,41 @@ public class CommonStatsFlags implements Streamable, Cloneable {
} }
} }
public CommonStatsFlags(StreamInput in) throws IOException {
final long longFlags = in.readLong();
flags.clear();
for (Flag flag : Flag.values()) {
if ((longFlags & (1 << flag.ordinal())) != 0) {
flags.add(flag);
}
}
types = in.readStringArray();
groups = in.readStringArray();
fieldDataFields = in.readStringArray();
completionDataFields = in.readStringArray();
if (in.getVersion().onOrAfter(Version.V_5_0_0_alpha1)) {
includeSegmentFileSizes = in.readBoolean();
} else {
includeSegmentFileSizes = false;
}
}
@Override
public void writeTo(StreamOutput out) throws IOException {
long longFlags = 0;
for (Flag flag : flags) {
longFlags |= (1 << flag.ordinal());
}
out.writeLong(longFlags);
out.writeStringArrayNullable(types);
out.writeStringArrayNullable(groups);
out.writeStringArrayNullable(fieldDataFields);
out.writeStringArrayNullable(completionDataFields);
if (out.getVersion().onOrAfter(Version.V_5_0_0_alpha1)) {
out.writeBoolean(includeSegmentFileSizes);
}
}
/** /**
* Sets all flags to return all stats. * Sets all flags to return all stats.
@ -162,7 +194,6 @@ public class CommonStatsFlags implements Streamable, Cloneable {
flags.add(flag); flags.add(flag);
} }
public CommonStatsFlags set(Flag flag, boolean add) { public CommonStatsFlags set(Flag flag, boolean add) {
if (add) { if (add) {
set(flag); set(flag);
@ -172,49 +203,6 @@ public class CommonStatsFlags implements Streamable, Cloneable {
return this; return this;
} }
public static CommonStatsFlags readCommonStatsFlags(StreamInput in) throws IOException {
CommonStatsFlags flags = new CommonStatsFlags();
flags.readFrom(in);
return flags;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
long longFlags = 0;
for (Flag flag : flags) {
longFlags |= (1 << flag.ordinal());
}
out.writeLong(longFlags);
out.writeStringArrayNullable(types);
out.writeStringArrayNullable(groups);
out.writeStringArrayNullable(fieldDataFields);
out.writeStringArrayNullable(completionDataFields);
if (out.getVersion().onOrAfter(Version.V_5_0_0_alpha1)) {
out.writeBoolean(includeSegmentFileSizes);
}
}
@Override
public void readFrom(StreamInput in) throws IOException {
final long longFlags = in.readLong();
flags.clear();
for (Flag flag : Flag.values()) {
if ((longFlags & (1 << flag.ordinal())) != 0) {
flags.add(flag);
}
}
types = in.readStringArray();
groups = in.readStringArray();
fieldDataFields = in.readStringArray();
completionDataFields = in.readStringArray();
if (in.getVersion().onOrAfter(Version.V_5_0_0_alpha1)) {
includeSegmentFileSizes = in.readBoolean();
} else {
includeSegmentFileSizes = false;
}
}
@Override @Override
public CommonStatsFlags clone() { public CommonStatsFlags clone() {
try { try {
@ -226,7 +214,7 @@ public class CommonStatsFlags implements Streamable, Cloneable {
} }
} }
public static enum Flag { public enum Flag {
// Do not change the order of these flags we use // Do not change the order of these flags we use
// the ordinal for encoding! Only append to the end! // the ordinal for encoding! Only append to the end!
Store("store"), Store("store"),
@ -247,7 +235,6 @@ public class CommonStatsFlags implements Streamable, Cloneable {
RequestCache("request_cache"), RequestCache("request_cache"),
Recovery("recovery"); Recovery("recovery");
private final String restName; private final String restName;
Flag(String restName) { Flag(String restName) {
@ -257,6 +244,5 @@ public class CommonStatsFlags implements Streamable, Cloneable {
public String getRestName() { public String getRestName() {
return restName; return restName;
} }
} }
} }

View File

@ -274,6 +274,6 @@ public class IndicesStatsRequest extends BroadcastRequest<IndicesStatsRequest> {
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
super.readFrom(in); super.readFrom(in);
flags = CommonStatsFlags.readCommonStatsFlags(in); flags = new CommonStatsFlags(in);
} }
} }

View File

@ -90,7 +90,7 @@ public class ShardStats implements Streamable, ToXContent {
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
shardRouting = new ShardRouting(in); shardRouting = new ShardRouting(in);
commonStats = CommonStats.readCommonStats(in); commonStats = new CommonStats(in);
commitStats = CommitStats.readOptionalCommitStatsFrom(in); commitStats = CommitStats.readOptionalCommitStatsFrom(in);
statePath = in.readString(); statePath = in.readString();
dataPath = in.readString(); dataPath = in.readString();

View File

@ -35,12 +35,12 @@ import org.elasticsearch.index.engine.SegmentsStats;
import org.elasticsearch.index.fielddata.FieldDataStats; import org.elasticsearch.index.fielddata.FieldDataStats;
import org.elasticsearch.index.flush.FlushStats; import org.elasticsearch.index.flush.FlushStats;
import org.elasticsearch.index.get.GetStats; import org.elasticsearch.index.get.GetStats;
import org.elasticsearch.index.shard.IndexingStats;
import org.elasticsearch.index.merge.MergeStats; import org.elasticsearch.index.merge.MergeStats;
import org.elasticsearch.index.recovery.RecoveryStats; import org.elasticsearch.index.recovery.RecoveryStats;
import org.elasticsearch.index.refresh.RefreshStats; import org.elasticsearch.index.refresh.RefreshStats;
import org.elasticsearch.index.search.stats.SearchStats; import org.elasticsearch.index.search.stats.SearchStats;
import org.elasticsearch.index.shard.DocsStats; import org.elasticsearch.index.shard.DocsStats;
import org.elasticsearch.index.shard.IndexingStats;
import org.elasticsearch.index.store.StoreStats; import org.elasticsearch.index.store.StoreStats;
import org.elasticsearch.search.suggest.completion.CompletionStats; import org.elasticsearch.search.suggest.completion.CompletionStats;
@ -154,7 +154,7 @@ public class NodeIndicesStats implements Streamable, ToXContent {
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
stats = CommonStats.readCommonStats(in); stats = new CommonStats(in);
if (in.readBoolean()) { if (in.readBoolean()) {
int entries = in.readVInt(); int entries = in.readVInt();
statsByShard = new HashMap<>(); statsByShard = new HashMap<>();

View File

@ -647,7 +647,7 @@ public class IndexStatsIT extends ESIntegTestCase {
flags.writeTo(out); flags.writeTo(out);
out.close(); out.close();
BytesReference bytes = out.bytes(); BytesReference bytes = out.bytes();
CommonStatsFlags readStats = CommonStatsFlags.readCommonStatsFlags(bytes.streamInput()); CommonStatsFlags readStats = new CommonStatsFlags(bytes.streamInput());
for (Flag flag : values) { for (Flag flag : values) {
assertThat(flags.isSet(flag), equalTo(readStats.isSet(flag))); assertThat(flags.isSet(flag), equalTo(readStats.isSet(flag)));
} }
@ -661,7 +661,7 @@ public class IndexStatsIT extends ESIntegTestCase {
flags.writeTo(out); flags.writeTo(out);
out.close(); out.close();
BytesReference bytes = out.bytes(); BytesReference bytes = out.bytes();
CommonStatsFlags readStats = CommonStatsFlags.readCommonStatsFlags(bytes.streamInput()); CommonStatsFlags readStats = new CommonStatsFlags(bytes.streamInput());
for (Flag flag : values) { for (Flag flag : values) {
assertThat(flags.isSet(flag), equalTo(readStats.isSet(flag))); assertThat(flags.isSet(flag), equalTo(readStats.isSet(flag)));
} }