Skip to content

Commit c1657bc

Browse files
martijnvgkcm
authored andcommitted
Fixed CCR stats api serialization issues and (#33983)
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.
1 parent 1bece4a commit c1657bc

File tree

5 files changed

+141
-19
lines changed

5 files changed

+141
-19
lines changed

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestCcrStatsAction.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
package org.elasticsearch.xpack.ccr.rest;
88

9-
import org.elasticsearch.action.support.IndicesOptions;
109
import org.elasticsearch.client.node.NodeClient;
1110
import org.elasticsearch.common.Strings;
1211
import org.elasticsearch.common.settings.Settings;
@@ -35,7 +34,6 @@ public String getName() {
3534
protected RestChannelConsumer prepareRequest(final RestRequest restRequest, final NodeClient client) throws IOException {
3635
final CcrStatsAction.StatsRequest request = new CcrStatsAction.StatsRequest();
3736
request.setIndices(Strings.splitStringByCommaToArray(restRequest.param("index")));
38-
request.setIndicesOptions(IndicesOptions.fromRequest(restRequest, request.indicesOptions()));
3937
return channel -> client.execute(CcrStatsAction.INSTANCE, request, new RestToXContentListener<>(channel));
4038
}
4139

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.ccr.action;
7+
8+
import org.elasticsearch.test.AbstractStreamableTestCase;
9+
import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction;
10+
11+
public class StatsRequestTests extends AbstractStreamableTestCase<CcrStatsAction.StatsRequest> {
12+
13+
@Override
14+
protected CcrStatsAction.StatsRequest createBlankInstance() {
15+
return new CcrStatsAction.StatsRequest();
16+
}
17+
18+
@Override
19+
protected CcrStatsAction.StatsRequest createTestInstance() {
20+
CcrStatsAction.StatsRequest statsRequest = new CcrStatsAction.StatsRequest();
21+
if (randomBoolean()) {
22+
statsRequest.setIndices(generateRandomStringArray(8, 4, false));
23+
}
24+
return statsRequest;
25+
}
26+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.ccr.action;
7+
8+
import org.elasticsearch.test.AbstractStreamableTestCase;
9+
import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus;
10+
import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction;
11+
12+
import java.util.ArrayList;
13+
import java.util.Collections;
14+
import java.util.List;
15+
16+
public class StatsResponsesTests extends AbstractStreamableTestCase<CcrStatsAction.StatsResponses> {
17+
18+
@Override
19+
protected CcrStatsAction.StatsResponses createBlankInstance() {
20+
return new CcrStatsAction.StatsResponses();
21+
}
22+
23+
@Override
24+
protected CcrStatsAction.StatsResponses createTestInstance() {
25+
int numResponses = randomIntBetween(0, 8);
26+
List<CcrStatsAction.StatsResponse> responses = new ArrayList<>(numResponses);
27+
for (int i = 0; i < numResponses; i++) {
28+
ShardFollowNodeTaskStatus status = new ShardFollowNodeTaskStatus(
29+
randomAlphaOfLength(4),
30+
randomAlphaOfLength(4),
31+
randomInt(),
32+
randomNonNegativeLong(),
33+
randomNonNegativeLong(),
34+
randomNonNegativeLong(),
35+
randomNonNegativeLong(),
36+
randomNonNegativeLong(),
37+
randomIntBetween(0, Integer.MAX_VALUE),
38+
randomIntBetween(0, Integer.MAX_VALUE),
39+
randomIntBetween(0, Integer.MAX_VALUE),
40+
randomNonNegativeLong(),
41+
randomNonNegativeLong(),
42+
randomNonNegativeLong(),
43+
randomNonNegativeLong(),
44+
randomNonNegativeLong(),
45+
randomNonNegativeLong(),
46+
randomNonNegativeLong(),
47+
randomNonNegativeLong(),
48+
randomNonNegativeLong(),
49+
randomNonNegativeLong(),
50+
Collections.emptyNavigableMap(),
51+
randomLong());
52+
responses.add(new CcrStatsAction.StatsResponse(status));
53+
}
54+
return new CcrStatsAction.StatsResponses(Collections.emptyList(), Collections.emptyList(), responses);
55+
}
56+
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/CcrStatsAction.java

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus;
2424

2525
import java.io.IOException;
26+
import java.util.Arrays;
2627
import java.util.Collections;
2728
import java.util.List;
2829
import java.util.Map;
30+
import java.util.Objects;
2931
import java.util.TreeMap;
3032

3133
public class CcrStatsAction extends Action<CcrStatsAction.StatsResponses> {
@@ -45,7 +47,7 @@ public StatsResponses newResponse() {
4547

4648
public static class StatsResponses extends BaseTasksResponse implements ToXContentObject {
4749

48-
private final List<StatsResponse> statsResponse;
50+
private List<StatsResponse> statsResponse;
4951

5052
public List<StatsResponse> getStatsResponses() {
5153
return statsResponse;
@@ -87,6 +89,31 @@ public XContentBuilder toXContent(final XContentBuilder builder, final Params pa
8789
builder.endObject();
8890
return builder;
8991
}
92+
93+
@Override
94+
public void readFrom(StreamInput in) throws IOException {
95+
super.readFrom(in);
96+
statsResponse = in.readList(StatsResponse::new);
97+
}
98+
99+
@Override
100+
public void writeTo(StreamOutput out) throws IOException {
101+
super.writeTo(out);
102+
out.writeList(statsResponse);
103+
}
104+
105+
@Override
106+
public boolean equals(Object o) {
107+
if (this == o) return true;
108+
if (o == null || getClass() != o.getClass()) return false;
109+
StatsResponses that = (StatsResponses) o;
110+
return Objects.equals(statsResponse, that.statsResponse);
111+
}
112+
113+
@Override
114+
public int hashCode() {
115+
return Objects.hash(statsResponse);
116+
}
90117
}
91118

92119
public static class StatsRequest extends BaseTasksRequest<StatsRequest> implements IndicesRequest {
@@ -102,15 +129,9 @@ public void setIndices(final String[] indices) {
102129
this.indices = indices;
103130
}
104131

105-
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
106-
107132
@Override
108133
public IndicesOptions indicesOptions() {
109-
return indicesOptions;
110-
}
111-
112-
public void setIndicesOptions(final IndicesOptions indicesOptions) {
113-
this.indicesOptions = indicesOptions;
134+
return IndicesOptions.strictExpand();
114135
}
115136

116137
@Override
@@ -134,17 +155,27 @@ public ActionRequestValidationException validate() {
134155
@Override
135156
public void readFrom(final StreamInput in) throws IOException {
136157
super.readFrom(in);
137-
indices = in.readStringArray();
138-
indicesOptions = IndicesOptions.readIndicesOptions(in);
158+
indices = in.readOptionalStringArray();
139159
}
140160

141161
@Override
142162
public void writeTo(StreamOutput out) throws IOException {
143163
super.writeTo(out);
144-
out.writeStringArray(indices);
145-
indicesOptions.writeIndicesOptions(out);
164+
out.writeOptionalStringArray(indices);
165+
}
166+
167+
@Override
168+
public boolean equals(Object o) {
169+
if (this == o) return true;
170+
if (o == null || getClass() != o.getClass()) return false;
171+
StatsRequest that = (StatsRequest) o;
172+
return Arrays.equals(indices, that.indices);
146173
}
147174

175+
@Override
176+
public int hashCode() {
177+
return Arrays.hashCode(indices);
178+
}
148179
}
149180

150181
public static class StatsResponse implements Writeable {
@@ -168,6 +199,18 @@ public void writeTo(final StreamOutput out) throws IOException {
168199
status.writeTo(out);
169200
}
170201

202+
@Override
203+
public boolean equals(Object o) {
204+
if (this == o) return true;
205+
if (o == null || getClass() != o.getClass()) return false;
206+
StatsResponse that = (StatsResponse) o;
207+
return Objects.equals(status, that.status);
208+
}
209+
210+
@Override
211+
public int hashCode() {
212+
return Objects.hash(status);
213+
}
171214
}
172215

173216
}

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/CcrStatsCollector.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
package org.elasticsearch.xpack.monitoring.collector.ccr;
88

9-
import org.elasticsearch.action.support.IndicesOptions;
109
import org.elasticsearch.client.Client;
1110
import org.elasticsearch.cluster.service.ClusterService;
1211
import org.elasticsearch.common.settings.Setting;
@@ -51,10 +50,10 @@ Collection<MonitoringDoc> innerDoCollect(
5150
long interval,
5251
MonitoringDoc.Node node) throws Exception {
5352

54-
final CcrStatsAction.StatsRequest request = new CcrStatsAction.StatsRequest();
55-
request.setIndices(getCollectionIndices());
56-
request.setIndicesOptions(IndicesOptions.lenientExpandOpen());
57-
final CcrStatsAction.StatsResponses responses = ccrClient.stats(request).actionGet(getCollectionTimeout());
53+
54+
final CcrStatsAction.StatsRequest request = new CcrStatsAction.StatsRequest();
55+
request.setIndices(getCollectionIndices());
56+
final CcrStatsAction.StatsResponses responses = ccrClient.stats(request).actionGet(getCollectionTimeout());
5857

5958
return responses
6059
.getStatsResponses()

0 commit comments

Comments
 (0)