From e3e0fcf861afd35bbc9b1c5be3491377e6ce4bdd Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 24 Sep 2018 11:38:04 +0200 Subject: [PATCH] Fixed CCR stats api serialization issues and always use `IndicesOptions.strictExpand()` for indices options. The follow index may be closed and we still want to get stats from shard follow task and the whether the provided index name matches with follow index name is checked when locating the task itself in the ccr stats transport action. --- .../xpack/ccr/rest/RestCcrStatsAction.java | 2 - .../xpack/ccr/action/StatsRequestTests.java | 26 +++++++ .../xpack/ccr/action/StatsResponsesTests.java | 56 ++++++++++++++++ .../xpack/core/ccr/action/CcrStatsAction.java | 67 +++++++++++++++---- .../collector/ccr/CcrStatsCollector.java | 2 - 5 files changed, 137 insertions(+), 16 deletions(-) create mode 100644 x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsRequestTests.java create mode 100644 x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsResponsesTests.java diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestCcrStatsAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestCcrStatsAction.java index de285dba19ec2..15c82d1d4e9d6 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestCcrStatsAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestCcrStatsAction.java @@ -6,7 +6,6 @@ package org.elasticsearch.xpack.ccr.rest; -import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; @@ -35,7 +34,6 @@ public String getName() { protected RestChannelConsumer prepareRequest(final RestRequest restRequest, final NodeClient client) throws IOException { final CcrStatsAction.StatsRequest request = new CcrStatsAction.StatsRequest(); request.setIndices(Strings.splitStringByCommaToArray(restRequest.param("index"))); - request.setIndicesOptions(IndicesOptions.fromRequest(restRequest, request.indicesOptions())); return channel -> client.execute(CcrStatsAction.INSTANCE, request, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsRequestTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsRequestTests.java new file mode 100644 index 0000000000000..ea1e887491412 --- /dev/null +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsRequestTests.java @@ -0,0 +1,26 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.ccr.action; + +import org.elasticsearch.test.AbstractStreamableTestCase; +import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction; + +public class StatsRequestTests extends AbstractStreamableTestCase { + + @Override + protected CcrStatsAction.StatsRequest createBlankInstance() { + return new CcrStatsAction.StatsRequest(); + } + + @Override + protected CcrStatsAction.StatsRequest createTestInstance() { + CcrStatsAction.StatsRequest statsRequest = new CcrStatsAction.StatsRequest(); + if (randomBoolean()) { + statsRequest.setIndices(generateRandomStringArray(8, 4, false)); + } + return statsRequest; + } +} diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsResponsesTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsResponsesTests.java new file mode 100644 index 0000000000000..b79f8db1923f5 --- /dev/null +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsResponsesTests.java @@ -0,0 +1,56 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.ccr.action; + +import org.elasticsearch.test.AbstractStreamableTestCase; +import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; +import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class StatsResponsesTests extends AbstractStreamableTestCase { + + @Override + protected CcrStatsAction.StatsResponses createBlankInstance() { + return new CcrStatsAction.StatsResponses(); + } + + @Override + protected CcrStatsAction.StatsResponses createTestInstance() { + int numResponses = randomIntBetween(0, 8); + List responses = new ArrayList<>(numResponses); + for (int i = 0; i < numResponses; i++) { + ShardFollowNodeTaskStatus status = new ShardFollowNodeTaskStatus( + randomAlphaOfLength(4), + randomAlphaOfLength(4), + randomInt(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomIntBetween(0, Integer.MAX_VALUE), + randomIntBetween(0, Integer.MAX_VALUE), + randomIntBetween(0, Integer.MAX_VALUE), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + Collections.emptyNavigableMap(), + randomLong()); + responses.add(new CcrStatsAction.StatsResponse(status)); + } + return new CcrStatsAction.StatsResponses(Collections.emptyList(), Collections.emptyList(), responses); + } +} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/CcrStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/CcrStatsAction.java index 863cb678d7e58..a69ecbf7cdf03 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/CcrStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/CcrStatsAction.java @@ -23,9 +23,11 @@ import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; import java.io.IOException; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.TreeMap; public class CcrStatsAction extends Action { @@ -45,7 +47,7 @@ public StatsResponses newResponse() { public static class StatsResponses extends BaseTasksResponse implements ToXContentObject { - private final List statsResponse; + private List statsResponse; public List getStatsResponses() { return statsResponse; @@ -87,6 +89,31 @@ public XContentBuilder toXContent(final XContentBuilder builder, final Params pa builder.endObject(); return builder; } + + @Override + public void readFrom(StreamInput in) throws IOException { + super.readFrom(in); + statsResponse = in.readList(StatsResponse::new); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeList(statsResponse); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + StatsResponses that = (StatsResponses) o; + return Objects.equals(statsResponse, that.statsResponse); + } + + @Override + public int hashCode() { + return Objects.hash(statsResponse); + } } public static class StatsRequest extends BaseTasksRequest implements IndicesRequest { @@ -102,15 +129,9 @@ public void setIndices(final String[] indices) { this.indices = indices; } - private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed(); - @Override public IndicesOptions indicesOptions() { - return indicesOptions; - } - - public void setIndicesOptions(final IndicesOptions indicesOptions) { - this.indicesOptions = indicesOptions; + return IndicesOptions.strictExpand(); } @Override @@ -134,17 +155,27 @@ public ActionRequestValidationException validate() { @Override public void readFrom(final StreamInput in) throws IOException { super.readFrom(in); - indices = in.readStringArray(); - indicesOptions = IndicesOptions.readIndicesOptions(in); + indices = in.readOptionalStringArray(); } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - out.writeStringArray(indices); - indicesOptions.writeIndicesOptions(out); + out.writeOptionalStringArray(indices); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + StatsRequest that = (StatsRequest) o; + return Arrays.equals(indices, that.indices); } + @Override + public int hashCode() { + return Arrays.hashCode(indices); + } } public static class StatsResponse implements Writeable { @@ -168,6 +199,18 @@ public void writeTo(final StreamOutput out) throws IOException { status.writeTo(out); } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + StatsResponse that = (StatsResponse) o; + return Objects.equals(status, that.status); + } + + @Override + public int hashCode() { + return Objects.hash(status); + } } } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollector.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollector.java index 510f430d19647..5e017edc4cb8e 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollector.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollector.java @@ -6,7 +6,6 @@ package org.elasticsearch.xpack.monitoring.collector.ccr; -import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.service.ClusterService; @@ -73,7 +72,6 @@ protected Collection doCollect( try (ThreadContext.StoredContext ignore = stashWithOrigin(threadContext, MONITORING_ORIGIN)) { final CcrStatsAction.StatsRequest request = new CcrStatsAction.StatsRequest(); request.setIndices(getCollectionIndices()); - request.setIndicesOptions(IndicesOptions.lenientExpandOpen()); final CcrStatsAction.StatsResponses responses = ccrClient.stats(request).actionGet(getCollectionTimeout()); final long timestamp = timestamp();