Skip to content

Commit 11e7a02

Browse files
committed
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 c1dd06a commit 11e7a02

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
@@ -25,9 +25,11 @@
2525
import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus;
2626

2727
import java.io.IOException;
28+
import java.util.Arrays;
2829
import java.util.Collections;
2930
import java.util.List;
3031
import java.util.Map;
32+
import java.util.Objects;
3133
import java.util.TreeMap;
3234

3335
public class CcrStatsAction extends Action<CcrStatsAction.StatsRequest, CcrStatsAction.StatsResponses, CcrStatsAction.StatsRequestBuilder> {
@@ -47,7 +49,7 @@ public StatsResponses newResponse() {
4749

4850
public static class StatsResponses extends BaseTasksResponse implements ToXContentObject {
4951

50-
private final List<StatsResponse> statsResponse;
52+
private List<StatsResponse> statsResponse;
5153

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

94121
public static class StatsRequest extends BaseTasksRequest<StatsRequest> implements IndicesRequest {
@@ -104,15 +131,9 @@ public void setIndices(final String[] indices) {
104131
this.indices = indices;
105132
}
106133

107-
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
108-
109134
@Override
110135
public IndicesOptions indicesOptions() {
111-
return indicesOptions;
112-
}
113-
114-
public void setIndicesOptions(final IndicesOptions indicesOptions) {
115-
this.indicesOptions = indicesOptions;
136+
return IndicesOptions.strictExpand();
116137
}
117138

118139
@Override
@@ -136,17 +157,27 @@ public ActionRequestValidationException validate() {
136157
@Override
137158
public void readFrom(final StreamInput in) throws IOException {
138159
super.readFrom(in);
139-
indices = in.readStringArray();
140-
indicesOptions = IndicesOptions.readIndicesOptions(in);
160+
indices = in.readOptionalStringArray();
141161
}
142162

143163
@Override
144164
public void writeTo(StreamOutput out) throws IOException {
145165
super.writeTo(out);
146-
out.writeStringArray(indices);
147-
indicesOptions.writeIndicesOptions(out);
166+
out.writeOptionalStringArray(indices);
167+
}
168+
169+
@Override
170+
public boolean equals(Object o) {
171+
if (this == o) return true;
172+
if (o == null || getClass() != o.getClass()) return false;
173+
StatsRequest that = (StatsRequest) o;
174+
return Arrays.equals(indices, that.indices);
148175
}
149176

177+
@Override
178+
public int hashCode() {
179+
return Arrays.hashCode(indices);
180+
}
150181
}
151182

152183
public static class StatsResponse implements Writeable {
@@ -170,6 +201,18 @@ public void writeTo(final StreamOutput out) throws IOException {
170201
status.writeTo(out);
171202
}
172203

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

175218
public static class StatsRequestBuilder extends ActionRequestBuilder<StatsRequest, StatsResponses, StatsRequestBuilder> {

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)