From c36f3827b464e79bac5a6a4e5a55772cb9073223 Mon Sep 17 00:00:00 2001 From: Armin Date: Fri, 6 Jul 2018 17:40:05 +0200 Subject: [PATCH 1/2] Add Index UUID to `/_stats` Response * Add "uuid" field to each index's section in the `/_stats` response * closes #31791 --- .../action/admin/indices/stats/IndexStats.java | 9 ++++++++- .../indices/stats/IndicesStatsResponse.java | 14 +++++++++----- .../admin/indices/stats/IndicesStatsTests.java | 17 ++++++++++++++++- .../indices/IndicesStatsMonitoringDocTests.java | 2 +- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/stats/IndexStats.java b/server/src/main/java/org/elasticsearch/action/admin/indices/stats/IndexStats.java index 7903a340628d2..d8480519e5def 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/stats/IndexStats.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/stats/IndexStats.java @@ -29,10 +29,13 @@ public class IndexStats implements Iterable { private final String index; + private final String uuid; + private final ShardStats shards[]; - public IndexStats(String index, ShardStats[] shards) { + public IndexStats(String index, String uuid, ShardStats[] shards) { this.index = index; + this.uuid = uuid; this.shards = shards; } @@ -40,6 +43,10 @@ public String getIndex() { return this.index; } + public String getUuid() { + return uuid; + } + public ShardStats[] getShards() { return this.shards; } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponse.java index 7406dc4f2d12c..cc563948160da 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponse.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.index.Index; import java.io.IOException; import java.util.ArrayList; @@ -84,19 +85,22 @@ public Map getIndices() { } Map indicesStats = new HashMap<>(); - Set indices = new HashSet<>(); + Set indices = new HashSet<>(); for (ShardStats shard : shards) { - indices.add(shard.getShardRouting().getIndexName()); + indices.add(shard.getShardRouting().index()); } - for (String indexName : indices) { + for (Index index : indices) { List shards = new ArrayList<>(); + String indexName = index.getName(); for (ShardStats shard : this.shards) { if (shard.getShardRouting().getIndexName().equals(indexName)) { shards.add(shard); } } - indicesStats.put(indexName, new IndexStats(indexName, shards.toArray(new ShardStats[shards.size()]))); + indicesStats.put( + indexName, new IndexStats(indexName, index.getUUID(), shards.toArray(new ShardStats[shards.size()])) + ); } this.indicesStats = indicesStats; return indicesStats; @@ -169,7 +173,7 @@ protected void addCustomXContentFields(XContentBuilder builder, Params params) t builder.startObject(Fields.INDICES); for (IndexStats indexStats : getIndices().values()) { builder.startObject(indexStats.getIndex()); - + builder.field("uuid", indexStats.getUuid()); builder.startObject("primaries"); indexStats.getPrimaries().toXContent(builder, params); builder.endObject(); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsTests.java index 26785d2c8706c..c5b2823a39704 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsTests.java @@ -27,12 +27,16 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.engine.CommitStats; import org.elasticsearch.index.engine.SegmentsStats; import org.elasticsearch.index.translog.Translog; import org.elasticsearch.test.ESSingleNodeTestCase; +import java.io.IOException; import java.util.List; +import java.util.Map; import java.util.concurrent.TimeUnit; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; @@ -153,6 +157,18 @@ public void testRefreshListeners() throws Exception { assertEquals(0, common.refresh.getListeners()); } + @SuppressWarnings("unchecked") + public void testUuidOnRootStatsIndices() throws IOException { + String uuid = createIndex("test").indexUUID(); + IndicesStatsResponse rsp = client().admin().indices().prepareStats().get(); + try (XContentParser parser = createParser(JsonXContent.jsonXContent, rsp.toString())) { + assertEquals( + uuid, + ((Map)((Map) parser.map().get("indices")).get("test")).get("uuid") + ); + } + } + /** * Gives access to package private IndicesStatsResponse constructor for test purpose. **/ @@ -160,5 +176,4 @@ public static IndicesStatsResponse newIndicesStatsResponse(ShardStats[] shards, int failedShards, List shardFailures) { return new IndicesStatsResponse(shards, totalShards, successfulShards, failedShards, shardFailures); } - } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java index 86fa89f4c01bb..66b41d40943d0 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java @@ -45,7 +45,7 @@ public class IndicesStatsMonitoringDocTests extends BaseFilteredMonitoringDocTes @Before public void setUp() throws Exception { super.setUp(); - indicesStats = Collections.singletonList(new IndexStats("index-0", new ShardStats[] { + indicesStats = Collections.singletonList(new IndexStats("index-0", "dcvO5uZATE-EhIKc3tk9Bg", new ShardStats[] { // Primaries new ShardStats(mockShardRouting(true), mockShardPath(), mockCommonStats(), null, null), new ShardStats(mockShardRouting(true), mockShardPath(), mockCommonStats(), null, null), From 2b932e39b3950cf96d09180ac732ea64976d5282 Mon Sep 17 00:00:00 2001 From: Armin Date: Mon, 16 Jul 2018 22:39:36 +0200 Subject: [PATCH 2/2] Test serialization via REST test --- .../rest-api-spec/test/indices.stats/10_index.yml | 2 ++ .../admin/indices/stats/IndicesStatsTests.java | 13 ++----------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.stats/10_index.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.stats/10_index.yml index b70fac8cf0480..42847b05cd149 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.stats/10_index.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.stats/10_index.yml @@ -47,7 +47,9 @@ setup: - match: { _shards.total: 18 } - is_true: _all - is_true: indices.test1 + - is_true: indices.test1.uuid - is_true: indices.test2 + - is_true: indices.test2.uuid --- diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsTests.java index c5b2823a39704..264a92137be96 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsTests.java @@ -27,16 +27,12 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.engine.CommitStats; import org.elasticsearch.index.engine.SegmentsStats; import org.elasticsearch.index.translog.Translog; import org.elasticsearch.test.ESSingleNodeTestCase; -import java.io.IOException; import java.util.List; -import java.util.Map; import java.util.concurrent.TimeUnit; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; @@ -158,15 +154,10 @@ public void testRefreshListeners() throws Exception { } @SuppressWarnings("unchecked") - public void testUuidOnRootStatsIndices() throws IOException { + public void testUuidOnRootStatsIndices() { String uuid = createIndex("test").indexUUID(); IndicesStatsResponse rsp = client().admin().indices().prepareStats().get(); - try (XContentParser parser = createParser(JsonXContent.jsonXContent, rsp.toString())) { - assertEquals( - uuid, - ((Map)((Map) parser.map().get("indices")).get("test")).get("uuid") - ); - } + assertEquals(uuid, rsp.getIndex("test").getUuid()); } /**