Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Index UUID to /_stats Response #31871

Merged
merged 4 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ public class IndexStats implements Iterable<IndexShardStats> {

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;
}

public String getIndex() {
return this.index;
}

public String getUuid() {
return uuid;
}

public ShardStats[] getShards() {
return this.shards;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,19 +85,22 @@ public Map<String, IndexStats> getIndices() {
}
Map<String, IndexStats> indicesStats = new HashMap<>();

Set<String> indices = new HashSet<>();
Set<Index> 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<ShardStats> 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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -153,12 +157,23 @@ 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())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of parsing json, could you use rsp.getIndex("test") like the other tests here do and then get the uuid from the IndexStats object returned?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rjernst I did that to get coverage on https://github.com/elastic/elasticsearch/pull/31871/files#diff-eaeb35137f4e84d0f731422d07701547R176
If I don't add in the serialization roundtrip I don't get the actual response rendered do I (just checking the return of rap.getIndex("test") also passes without that line for serializing the uuid added)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think testing this directly (not rest) is better as a start. Testing this via rest is a different matter, and could be done through a rest api test, and/or a client test when this is added to the high level rest client.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rjernst thanks for taking a look :)

so, make this test directly for now and leave the REST test for later since that API isn't in the REST client yet?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but you could add this to an existing test under rest-api-spec/src/main/resources/rest-api-spec/test/indices.stats to ensure it exists in the rest layer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 2b932e3 I think/hope :)

assertEquals(
uuid,
((Map<String, Object>)((Map<String,Object>) parser.map().get("indices")).get("test")).get("uuid")
);
}
}

/**
* Gives access to package private IndicesStatsResponse constructor for test purpose.
**/
public static IndicesStatsResponse newIndicesStatsResponse(ShardStats[] shards, int totalShards, int successfulShards,
int failedShards, List<DefaultShardOperationFailedException> shardFailures) {
return new IndicesStatsResponse(shards, totalShards, successfulShards, failedShards, shardFailures);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down