Skip to content

Commit 35dbd9c

Browse files
Add Index UUID to /_stats Response (#31871)
* Add "uuid" field to each index's section in the `/_stats` response * closes #31791
1 parent 34083c6 commit 35dbd9c

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/indices.stats/10_index.yml

+2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ setup:
4747
- match: { _shards.total: 18 }
4848
- is_true: _all
4949
- is_true: indices.test1
50+
- is_true: indices.test1.uuid
5051
- is_true: indices.test2
52+
- is_true: indices.test2.uuid
5153

5254

5355
---

server/src/main/java/org/elasticsearch/action/admin/indices/stats/IndexStats.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,24 @@ public class IndexStats implements Iterable<IndexShardStats> {
2929

3030
private final String index;
3131

32+
private final String uuid;
33+
3234
private final ShardStats shards[];
3335

34-
public IndexStats(String index, ShardStats[] shards) {
36+
public IndexStats(String index, String uuid, ShardStats[] shards) {
3537
this.index = index;
38+
this.uuid = uuid;
3639
this.shards = shards;
3740
}
3841

3942
public String getIndex() {
4043
return this.index;
4144
}
4245

46+
public String getUuid() {
47+
return uuid;
48+
}
49+
4350
public ShardStats[] getShards() {
4451
return this.shards;
4552
}

server/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponse.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.common.io.stream.StreamInput;
2727
import org.elasticsearch.common.io.stream.StreamOutput;
2828
import org.elasticsearch.common.xcontent.XContentBuilder;
29+
import org.elasticsearch.index.Index;
2930

3031
import java.io.IOException;
3132
import java.util.ArrayList;
@@ -84,19 +85,22 @@ public Map<String, IndexStats> getIndices() {
8485
}
8586
Map<String, IndexStats> indicesStats = new HashMap<>();
8687

87-
Set<String> indices = new HashSet<>();
88+
Set<Index> indices = new HashSet<>();
8889
for (ShardStats shard : shards) {
89-
indices.add(shard.getShardRouting().getIndexName());
90+
indices.add(shard.getShardRouting().index());
9091
}
9192

92-
for (String indexName : indices) {
93+
for (Index index : indices) {
9394
List<ShardStats> shards = new ArrayList<>();
95+
String indexName = index.getName();
9496
for (ShardStats shard : this.shards) {
9597
if (shard.getShardRouting().getIndexName().equals(indexName)) {
9698
shards.add(shard);
9799
}
98100
}
99-
indicesStats.put(indexName, new IndexStats(indexName, shards.toArray(new ShardStats[shards.size()])));
101+
indicesStats.put(
102+
indexName, new IndexStats(indexName, index.getUUID(), shards.toArray(new ShardStats[shards.size()]))
103+
);
100104
}
101105
this.indicesStats = indicesStats;
102106
return indicesStats;
@@ -169,7 +173,7 @@ protected void addCustomXContentFields(XContentBuilder builder, Params params) t
169173
builder.startObject(Fields.INDICES);
170174
for (IndexStats indexStats : getIndices().values()) {
171175
builder.startObject(indexStats.getIndex());
172-
176+
builder.field("uuid", indexStats.getUuid());
173177
builder.startObject("primaries");
174178
indexStats.getPrimaries().toXContent(builder, params);
175179
builder.endObject();

server/src/test/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsTests.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,18 @@ public void testRefreshListeners() throws Exception {
153153
assertEquals(0, common.refresh.getListeners());
154154
}
155155

156+
@SuppressWarnings("unchecked")
157+
public void testUuidOnRootStatsIndices() {
158+
String uuid = createIndex("test").indexUUID();
159+
IndicesStatsResponse rsp = client().admin().indices().prepareStats().get();
160+
assertEquals(uuid, rsp.getIndex("test").getUuid());
161+
}
162+
156163
/**
157164
* Gives access to package private IndicesStatsResponse constructor for test purpose.
158165
**/
159166
public static IndicesStatsResponse newIndicesStatsResponse(ShardStats[] shards, int totalShards, int successfulShards,
160167
int failedShards, List<DefaultShardOperationFailedException> shardFailures) {
161168
return new IndicesStatsResponse(shards, totalShards, successfulShards, failedShards, shardFailures);
162169
}
163-
164170
}

x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class IndicesStatsMonitoringDocTests extends BaseFilteredMonitoringDocTes
4545
@Before
4646
public void setUp() throws Exception {
4747
super.setUp();
48-
indicesStats = Collections.singletonList(new IndexStats("index-0", new ShardStats[] {
48+
indicesStats = Collections.singletonList(new IndexStats("index-0", "dcvO5uZATE-EhIKc3tk9Bg", new ShardStats[] {
4949
// Primaries
5050
new ShardStats(mockShardRouting(true), mockShardPath(), mockCommonStats(), null, null),
5151
new ShardStats(mockShardRouting(true), mockShardPath(), mockCommonStats(), null, null),

0 commit comments

Comments
 (0)