use enum to represent flags and fail if flags are not respected

This commit is contained in:
Simon Willnauer 2013-04-08 15:11:31 +02:00
parent 2a588dc1f1
commit 7e77ddb88f
5 changed files with 414 additions and 296 deletions

View File

@ -24,23 +24,12 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import java.io.IOException;
import java.util.EnumSet;
/**
*/
public class CommonStatsFlags implements Streamable {
private boolean docs = true;
private boolean store = true;
private boolean indexing = true;
private boolean get = true;
private boolean search = true;
private boolean merge = false;
private boolean refresh = false;
private boolean flush = false;
private boolean warmer = false;
private boolean filterCache = false;
private boolean idCache = false;
private boolean fieldData = false;
private EnumSet<Flag> flags = EnumSet.of(Flag.Docs, Flag.Store, Flag.Indexing, Flag.Get, Flag.Search);
private String[] types = null;
private String[] groups = null;
@ -48,18 +37,7 @@ public class CommonStatsFlags implements Streamable {
* Sets all flags to return all stats.
*/
public CommonStatsFlags all() {
docs = true;
store = true;
get = true;
indexing = true;
search = true;
merge = true;
refresh = true;
flush = true;
warmer = true;
filterCache = true;
idCache = true;
fieldData = true;
flags = EnumSet.allOf(Flag.class);
types = null;
groups = null;
return this;
@ -69,25 +47,18 @@ public class CommonStatsFlags implements Streamable {
* Clears all stats.
*/
public CommonStatsFlags clear() {
docs = false;
store = false;
get = false;
indexing = false;
search = false;
merge = false;
refresh = false;
flush = false;
warmer = false;
filterCache = false;
idCache = false;
fieldData = false;
flags = EnumSet.noneOf(Flag.class);
types = null;
groups = null;
return this;
}
public boolean anySet() {
return docs || store || get || indexing || search || merge || refresh || flush || warmer || filterCache || idCache || fieldData;
return !flags.isEmpty();
}
public Flag[] getFlags() {
return flags.toArray(new Flag[flags.size()]);
}
/**
@ -119,115 +90,28 @@ public class CommonStatsFlags implements Streamable {
public String[] groups() {
return this.groups;
}
public CommonStatsFlags docs(boolean docs) {
this.docs = docs;
public boolean isSet(Flag flag) {
return flags.contains(flag);
}
boolean unSet(Flag flag) {
return flags.remove(flag);
}
void set(Flag flag) {
flags.add(flag);
}
public CommonStatsFlags set(Flag flag, boolean add) {
if (add) {
set(flag);
} else {
unSet(flag);
}
return this;
}
public boolean docs() {
return this.docs;
}
public CommonStatsFlags store(boolean store) {
this.store = store;
return this;
}
public boolean store() {
return this.store;
}
public CommonStatsFlags indexing(boolean indexing) {
this.indexing = indexing;
return this;
}
public boolean indexing() {
return this.indexing;
}
public CommonStatsFlags get(boolean get) {
this.get = get;
return this;
}
public boolean get() {
return this.get;
}
public CommonStatsFlags search(boolean search) {
this.search = search;
return this;
}
public boolean search() {
return this.search;
}
public CommonStatsFlags merge(boolean merge) {
this.merge = merge;
return this;
}
public boolean merge() {
return this.merge;
}
public CommonStatsFlags refresh(boolean refresh) {
this.refresh = refresh;
return this;
}
public boolean refresh() {
return this.refresh;
}
public CommonStatsFlags flush(boolean flush) {
this.flush = flush;
return this;
}
public boolean flush() {
return this.flush;
}
public CommonStatsFlags warmer(boolean warmer) {
this.warmer = warmer;
return this;
}
public boolean warmer() {
return this.warmer;
}
public CommonStatsFlags filterCache(boolean filterCache) {
this.filterCache = filterCache;
return this;
}
public boolean filterCache() {
return this.filterCache;
}
public CommonStatsFlags idCache(boolean idCache) {
this.idCache = idCache;
return this;
}
public boolean idCache() {
return this.idCache;
}
public CommonStatsFlags fieldData(boolean fieldData) {
this.fieldData = fieldData;
return this;
}
public boolean fieldData() {
return this.fieldData;
}
public static CommonStatsFlags readCommonStatsFlags(StreamInput in) throws IOException {
CommonStatsFlags flags = new CommonStatsFlags();
flags.readFrom(in);
@ -236,18 +120,11 @@ public class CommonStatsFlags implements Streamable {
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(docs);
out.writeBoolean(store);
out.writeBoolean(indexing);
out.writeBoolean(get);
out.writeBoolean(search);
out.writeBoolean(merge);
out.writeBoolean(flush);
out.writeBoolean(refresh);
out.writeBoolean(warmer);
out.writeBoolean(filterCache);
out.writeBoolean(idCache);
out.writeBoolean(fieldData);
long longFlags = 0;
for (Flag flag : flags) {
longFlags |= (1 << flag.ordinal());
}
out.writeLong(longFlags);
if (types == null) {
out.writeVInt(0);
} else {
@ -268,18 +145,13 @@ public class CommonStatsFlags implements Streamable {
@Override
public void readFrom(StreamInput in) throws IOException {
docs = in.readBoolean();
store = in.readBoolean();
indexing = in.readBoolean();
get = in.readBoolean();
search = in.readBoolean();
merge = in.readBoolean();
flush = in.readBoolean();
refresh = in.readBoolean();
warmer = in.readBoolean();
filterCache = in.readBoolean();
idCache = in.readBoolean();
fieldData = in.readBoolean();
final long longFlags = in.readLong();
flags.clear();
for(Flag flag : Flag.values()) {
if ((longFlags & (1 << flag.ordinal())) != 0) {
flags.add(flag);
}
}
int size = in.readVInt();
if (size > 0) {
types = new String[size];
@ -295,4 +167,30 @@ public class CommonStatsFlags implements Streamable {
}
}
}
public static enum Flag {
Store("store"),
Indexing("indexing"),
Get("get"),
Search("search"),
Merge("merge"),
Flush("flush"),
Refresh("refresh"),
FilterCache("filter_cache"),
IdCache("id_cache"),
FieldData("fielddata"),
Docs("docs"),
Warmer("warmer");
private final String restName;
Flag(String restName) {
this.restName = restName;
}
public String getRestName() {
return restName;
}
}
}

View File

@ -19,12 +19,13 @@
package org.elasticsearch.action.admin.indices.stats;
import java.io.IOException;
import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag;
import org.elasticsearch.action.support.broadcast.BroadcastOperationRequest;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException;
/**
* A request to get indices level stats. Allow to enable different stats to be returned.
* <p/>
@ -85,111 +86,112 @@ public class IndicesStatsRequest extends BroadcastOperationRequest<IndicesStatsR
}
public IndicesStatsRequest docs(boolean docs) {
flags.docs(docs);
flags.set(Flag.Docs, docs);
return this;
}
public boolean docs() {
return flags.docs();
return flags.isSet(Flag.Docs);
}
public IndicesStatsRequest store(boolean store) {
flags.store(store);
flags.set(Flag.Store, store);
return this;
}
public boolean store() {
return flags.store();
return flags.isSet(Flag.Store);
}
public IndicesStatsRequest indexing(boolean indexing) {
flags.indexing(indexing);
flags.set(Flag.Indexing, indexing);
return this;
}
public boolean indexing() {
return flags.indexing();
return flags.isSet(Flag.Indexing);
}
public IndicesStatsRequest get(boolean get) {
flags.get(get);
flags.set(Flag.Get, get);
return this;
}
public boolean get() {
return flags.get();
return flags.isSet(Flag.Get);
}
public IndicesStatsRequest search(boolean search) {
flags.search(search);
flags.set(Flag.Search, search);
return this;
}
public boolean search() {
return flags.search();
return flags.isSet(Flag.Search);
}
public IndicesStatsRequest merge(boolean merge) {
flags.merge(merge);
flags.set(Flag.Merge, merge);
return this;
}
public boolean merge() {
return flags.merge();
return flags.isSet(Flag.Merge);
}
public IndicesStatsRequest refresh(boolean refresh) {
flags.refresh(refresh);
flags.set(Flag.Refresh, refresh);
return this;
}
public boolean refresh() {
return flags.refresh();
return flags.isSet(Flag.Refresh);
}
public IndicesStatsRequest flush(boolean flush) {
flags.flush(flush);
flags.set(Flag.Flush, flush);
return this;
}
public boolean flush() {
return flags.flush();
return flags.isSet(Flag.Flush);
}
public IndicesStatsRequest warmer(boolean warmer) {
flags.warmer(warmer);
flags.set(Flag.Warmer, warmer);
return this;
}
public boolean warmer() {
return flags.warmer();
return flags.isSet(Flag.Warmer);
}
public IndicesStatsRequest filterCache(boolean filterCache) {
flags.filterCache(filterCache);
flags.set(Flag.FilterCache, filterCache);
return this;
}
public boolean filterCache() {
return flags.filterCache();
return flags.isSet(Flag.FilterCache);
}
public IndicesStatsRequest idCache(boolean idCache) {
flags.idCache(idCache);
flags.set(Flag.IdCache, idCache);
return this;
}
public boolean idCache() {
return flags.idCache();
return flags.isSet(Flag.IdCache);
}
public IndicesStatsRequest fieldData(boolean fieldData) {
flags.fieldData(fieldData);
flags.set(Flag.FieldData, fieldData);
return this;
}
public boolean fieldData() {
return flags.fieldData();
return flags.isSet(Flag.FieldData);
}
@Override

View File

@ -26,6 +26,7 @@ import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.ElasticSearchIllegalStateException;
import org.elasticsearch.action.admin.indices.stats.CommonStats;
import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags;
import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.*;
@ -71,6 +72,7 @@ import org.elasticsearch.index.similarity.SimilarityModule;
import org.elasticsearch.index.store.IndexStore;
import org.elasticsearch.index.store.IndexStoreModule;
import org.elasticsearch.index.store.StoreStats;
import org.elasticsearch.index.warmer.WarmerStats;
import org.elasticsearch.indices.analysis.IndicesAnalysisService;
import org.elasticsearch.indices.recovery.RecoverySettings;
import org.elasticsearch.indices.store.IndicesStore;
@ -189,39 +191,101 @@ public class InternalIndicesService extends AbstractLifecycleComponent<IndicesSe
@Override
public NodeIndicesStats stats(boolean includePrevious, CommonStatsFlags flags) {
CommonStats stats = new CommonStats();
if (flags.docs()) stats.docs = new DocsStats();
if (flags.store()) stats.store = new StoreStats();
if (flags.get()) stats.get = new GetStats();
if (flags.indexing()) stats.indexing = new IndexingStats();
if (flags.search()) stats.search = new SearchStats();
if (flags.merge()) stats.merge = new MergeStats();
if (flags.refresh()) stats.refresh = new RefreshStats();
if (flags.flush()) stats.flush = new FlushStats();
if (flags.fieldData()) stats.fieldData = new FieldDataStats();
if (flags.idCache()) stats.idCache = new IdCacheStats();
if (flags.filterCache()) stats.filterCache = new FilterCacheStats();
if (includePrevious) {
if (flags.get()) stats.get.add(oldShardsStats.getStats);
if (flags.indexing()) stats.indexing.add(oldShardsStats.indexingStats);
if (flags.search()) stats.search.add(oldShardsStats.searchStats);
if (flags.merge()) stats.merge.add(oldShardsStats.mergeStats);
if (flags.refresh()) stats.refresh.add(oldShardsStats.refreshStats);
if (flags.flush()) stats.flush.add(oldShardsStats.flushStats);
Flag[] setFlags = flags.getFlags();
for(Flag flag : setFlags) {
switch (flag) {
case Docs:
stats.docs = new DocsStats();
break;
case Store:
stats.store = new StoreStats();
break;
case Warmer:
stats.warmer = new WarmerStats();
break;
case Get:
stats.get = new GetStats();
if (includePrevious) {stats.get.add(oldShardsStats.getStats);}
break;
case Indexing:
stats.indexing = new IndexingStats();
if (includePrevious) {stats.indexing.add(oldShardsStats.indexingStats);}
break;
case Search:
stats.search = new SearchStats();
if (includePrevious) {stats.search.add(oldShardsStats.searchStats);}
break;
case Merge:
stats.merge = new MergeStats();
if (includePrevious) {stats.merge.add(oldShardsStats.mergeStats);}
break;
case Refresh:
stats.refresh = new RefreshStats();
if (includePrevious) {stats.refresh.add(oldShardsStats.refreshStats);}
break;
case Flush:
stats.flush = new FlushStats();
if (includePrevious) {stats.flush.add(oldShardsStats.flushStats);}
break;
case FieldData:
stats.fieldData = new FieldDataStats();
break;
case IdCache:
stats.idCache = new IdCacheStats();
break;
case FilterCache:
stats.filterCache = new FilterCacheStats();
break;
default:
throw new IllegalStateException("Unknown Flag: " + flag);
}
}
for (IndexService indexService : indices.values()) {
for (IndexShard indexShard : indexService) {
if (flags.store()) stats.store.add(indexShard.storeStats());
if (flags.docs()) stats.docs.add(indexShard.docStats());
if (flags.get()) stats.get.add(indexShard.getStats());
if (flags.indexing()) stats.indexing.add(indexShard.indexingStats());
if (flags.search()) stats.search.add(indexShard.searchStats());
if (flags.merge()) stats.merge.add(indexShard.mergeStats());
if (flags.refresh()) stats.refresh.add(indexShard.refreshStats());
if (flags.flush()) stats.flush.add(indexShard.flushStats());
if (flags.filterCache()) stats.filterCache.add(indexShard.filterCacheStats());
if (flags.idCache()) stats.idCache.add(indexShard.idCacheStats());
if (flags.fieldData()) stats.fieldData.add(indexShard.fieldDataStats());
for(Flag flag : setFlags) {
switch (flag) {
case Store:
stats.store.add(indexShard.storeStats());
break;
case Docs:
stats.docs.add(indexShard.docStats());
break;
case Get:
stats.get.add(indexShard.getStats());
break;
case Indexing:
stats.indexing.add(indexShard.indexingStats());
break;
case Search:
stats.search.add(indexShard.searchStats());
break;
case Merge:
stats.merge.add(indexShard.mergeStats());
break;
case Refresh:
stats.refresh.add(indexShard.refreshStats());
break;
case Flush:
stats.flush.add(indexShard.flushStats());
break;
case FilterCache:
stats.filterCache.add(indexShard.filterCacheStats());
break;
case IdCache:
stats.idCache.add(indexShard.idCacheStats());
break;
case FieldData:
stats.fieldData.add(indexShard.fieldDataStats());
break;
case Warmer:
stats.warmer.add(indexShard.warmerStats());
break;
default:
throw new IllegalStateException("Unknown Flag: " + flag);
}
}
}
}
return new NodeIndicesStats(stats);

View File

@ -23,6 +23,7 @@ import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest;
import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse;
import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags;
import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
@ -52,78 +53,13 @@ public class RestNodesStatsAction extends BaseRestHandler {
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/stats", indicesHandler);
indicesHandler = new RestIndicesHandler(new CommonStatsFlags().clear().docs(true));
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/indices/docs", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices/docs", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/docs/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/docs/stats", indicesHandler);
indicesHandler = new RestIndicesHandler(new CommonStatsFlags().clear().store(true));
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/indices/store", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices/store", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/store/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/store/stats", indicesHandler);
indicesHandler = new RestIndicesHandler(new CommonStatsFlags().clear().merge(true));
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/indices/merge", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices/merge", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/merge/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/merge/stats", indicesHandler);
indicesHandler = new RestIndicesHandler(new CommonStatsFlags().clear().refresh(true));
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/indices/refresh", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices/refresh", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/refresh/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/refresh/stats", indicesHandler);
indicesHandler = new RestIndicesHandler(new CommonStatsFlags().clear().flush(true));
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/indices/flush", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices/flush", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/flush/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/flush/stats", indicesHandler);
indicesHandler = new RestIndicesHandler(new CommonStatsFlags().clear().warmer(true));
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/indices/warmer", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices/warmer", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/warmer/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/warmer/stats", indicesHandler);
indicesHandler = new RestIndicesHandler(new CommonStatsFlags().clear().filterCache(true));
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/indices/filter_cache", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices/filter_cache", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/filter_cache/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/filter_cache/stats", indicesHandler);
indicesHandler = new RestIndicesHandler(new CommonStatsFlags().clear().fieldData(true));
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/indices/fielddata", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices/fielddata", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/fielddata/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/fielddata/stats", indicesHandler);
indicesHandler = new RestIndicesHandler(new CommonStatsFlags().clear().idCache(true));
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/indices/id_cache", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices/id_cache", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/id_cache/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/id_cache/stats", indicesHandler);
indicesHandler = new RestIndicesHandler(new CommonStatsFlags().clear().indexing(true));
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/indices/indexing", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices/indexing", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/indexing/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/indexing/stats", indicesHandler);
indicesHandler = new RestIndicesHandler(new CommonStatsFlags().clear().search(true));
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/indices/search", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices/search", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/search/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/search/stats", indicesHandler);
indicesHandler = new RestIndicesHandler(new CommonStatsFlags().clear().get(true));
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/indices/get", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices/get", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/get/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/get/stats", indicesHandler);
for (Flag flag : CommonStatsFlags.Flag.values()) {
indicesHandler = new RestIndicesHandler(new CommonStatsFlags().clear().set(flag, true));
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/indices/" + flag.getRestName(), indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/stats/indices/" + flag.getRestName(), indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/indices/" + flag.getRestName() + "/stats", indicesHandler);
controller.registerHandler(RestRequest.Method.GET, "/_nodes/{nodeId}/indices/" + flag.getRestName() + "/stats", indicesHandler);
}
RestOsHandler osHandler = new RestOsHandler();
controller.registerHandler(RestRequest.Method.GET, "/_nodes/stats/os", osHandler);
@ -229,7 +165,7 @@ public class RestNodesStatsAction extends BaseRestHandler {
class RestIndicesHandler implements RestHandler {
private CommonStatsFlags flags;
private final CommonStatsFlags flags;
RestIndicesHandler(CommonStatsFlags flags) {
this.flags = flags;

View File

@ -19,11 +19,23 @@
package org.elasticsearch.test.integration.indices.stats;
import java.io.IOException;
import java.util.EnumSet;
import java.util.Random;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.indices.stats.CommonStats;
import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags;
import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequestBuilder;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamInput;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
@ -142,5 +154,211 @@ public class SimpleIndexStatsTests extends AbstractNodesTests {
assertThat(stats.getTotal().getGet().getCount(), equalTo(2l));
assertThat(stats.getTotal().getGet().getExistsCount(), equalTo(1l));
assertThat(stats.getTotal().getGet().getMissingCount(), equalTo(1l));
// clear all
stats = client.admin().indices().prepareStats()
.setDocs(false)
.setStore(false)
.setIndexing(false)
.setFlush(true)
.setRefresh(true)
.setMerge(true)
.clear() // reset defaults
.execute().actionGet();
assertThat(stats.getTotal().getDocs(), nullValue());
assertThat(stats.getTotal().getStore(), nullValue());
assertThat(stats.getTotal().getIndexing(), nullValue());
assertThat(stats.getTotal().getGet(), nullValue());
assertThat(stats.getTotal().getSearch(), nullValue());
}
@Test
public void testAllFlags() throws Exception {
client.admin().indices().prepareDelete().execute().actionGet();
// rely on 1 replica for this tests
client.admin().indices().prepareCreate("test1").execute().actionGet();
client.admin().indices().prepareCreate("test2").execute().actionGet();
ClusterHealthResponse clusterHealthResponse = client.admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));
client.prepareIndex("test1", "type1", Integer.toString(1)).setSource("field", "value").execute().actionGet();
client.prepareIndex("test1", "type2", Integer.toString(1)).setSource("field", "value").execute().actionGet();
client.prepareIndex("test2", "type", Integer.toString(1)).setSource("field", "value").execute().actionGet();
client.admin().indices().prepareRefresh().execute().actionGet();
IndicesStatsRequestBuilder builder = client.admin().indices().prepareStats();
Flag[] values = CommonStatsFlags.Flag.values();
for (Flag flag : values) {
set(flag, builder, false);
}
IndicesStatsResponse stats = builder.execute().actionGet();
for (Flag flag : values) {
assertThat(isSet(flag, stats.getPrimaries()), equalTo(false));
assertThat(isSet(flag, stats.getTotal()), equalTo(false));
}
for (Flag flag : values) {
set(flag, builder, true);
}
stats = builder.execute().actionGet();
for (Flag flag : values) {
assertThat(isSet(flag, stats.getPrimaries()), equalTo(true));
assertThat(isSet(flag, stats.getTotal()), equalTo(true));
}
long seed = System.currentTimeMillis();
System.out.println("seed: " + seed);
Random random = new Random(seed);
EnumSet<Flag> flags = EnumSet.noneOf(Flag.class);
for (Flag flag : values) {
if (random.nextBoolean()) {
flags.add(flag);
}
}
for (Flag flag : values) {
set(flag, builder, false); // clear all
}
for (Flag flag : flags) { // set the flags
set(flag, builder, true);
}
stats = builder.execute().actionGet();
for (Flag flag : flags) { // check the flags
assertThat(isSet(flag, stats.getPrimaries()), equalTo(true));
assertThat(isSet(flag, stats.getTotal()), equalTo(true));
}
for (Flag flag : EnumSet.complementOf(flags)) { // check the complement
assertThat(isSet(flag, stats.getPrimaries()), equalTo(false));
assertThat(isSet(flag, stats.getTotal()), equalTo(false));
}
}
@Test
public void testEncodeDecodeCommonStats() throws IOException {
CommonStatsFlags flags = new CommonStatsFlags();
Flag[] values = CommonStatsFlags.Flag.values();
assertThat(flags.anySet(), equalTo(true));
for (Flag flag : values) {
flags.set(flag, false);
}
assertThat(flags.anySet(), equalTo(false));
for (Flag flag : values) {
flags.set(flag, true);
}
assertThat(flags.anySet(), equalTo(true));
long seed = System.currentTimeMillis();
System.out.println("seed: " + seed);
Random random = new Random(seed);
flags.set(values[random.nextInt(values.length)], false);
assertThat(flags.anySet(), equalTo(true));
{
BytesStreamOutput out = new BytesStreamOutput();
flags.writeTo(out);
out.close();
BytesReference bytes = out.bytes();
CommonStatsFlags readStats = CommonStatsFlags.readCommonStatsFlags(new BytesStreamInput(bytes));
for (Flag flag : values) {
assertThat(flags.isSet(flag), equalTo(readStats.isSet(flag)));
}
}
{
for (Flag flag : values) {
flags.set(flag, random.nextBoolean());
}
BytesStreamOutput out = new BytesStreamOutput();
flags.writeTo(out);
out.close();
BytesReference bytes = out.bytes();
CommonStatsFlags readStats = CommonStatsFlags.readCommonStatsFlags(new BytesStreamInput(bytes));
for (Flag flag : values) {
assertThat(flags.isSet(flag), equalTo(readStats.isSet(flag)));
}
}
}
private static void set(Flag flag, IndicesStatsRequestBuilder builder, boolean set) {
switch(flag) {
case Docs:
builder.setDocs(set);
break;
case FieldData:
builder.setFieldData(set);
break;
case FilterCache:
builder.setFilterCache(set);
break;
case Flush:
builder.setFlush(set);
break;
case Get:
builder.setGet(set);
break;
case IdCache:
builder.setIdCache(set);
break;
case Indexing:
builder.setIndexing(set);
break;
case Merge:
builder.setMerge(set);
break;
case Refresh:
builder.setRefresh(set);
break;
case Search:
builder.setSearch(set);
break;
case Store:
builder.setStore(set);
break;
case Warmer:
builder.setWarmer(set);
break;
default:
assert false : "new flag? " + flag;
break;
}
}
private static boolean isSet(Flag flag, CommonStats response) {
switch(flag) {
case Docs:
return response.getDocs() != null;
case FieldData:
return response.getFieldData() != null;
case FilterCache:
return response.getFilterCache() != null;
case Flush:
return response.getFlush() != null;
case Get:
return response.getGet() != null;
case IdCache:
return response.getIdCache() != null;
case Indexing:
return response.getIndexing() != null;
case Merge:
return response.getMerge() != null;
case Refresh:
return response.getRefresh() != null;
case Search:
return response.getSearch() != null;
case Store:
return response.getStore() != null;
case Warmer:
return response.getWarmer() != null;
default:
assert false : "new flag? " + flag;
return false;
}
}
}