Skip to content

Commit

Permalink
Fixed CCR stats api serialization issues and (#33983)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
martijnvg authored Sep 28, 2018
1 parent 9cd4f70 commit 17b3b97
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<CcrStatsAction.StatsRequest> {

@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;
}
}
Original file line number Diff line number Diff line change
@@ -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<CcrStatsAction.StatsResponses> {

@Override
protected CcrStatsAction.StatsResponses createBlankInstance() {
return new CcrStatsAction.StatsResponses();
}

@Override
protected CcrStatsAction.StatsResponses createTestInstance() {
int numResponses = randomIntBetween(0, 8);
List<CcrStatsAction.StatsResponse> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CcrStatsAction.StatsResponses> {
Expand All @@ -45,7 +47,7 @@ public StatsResponses newResponse() {

public static class StatsResponses extends BaseTasksResponse implements ToXContentObject {

private final List<StatsResponse> statsResponse;
private List<StatsResponse> statsResponse;

public List<StatsResponse> getStatsResponses() {
return statsResponse;
Expand Down Expand Up @@ -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<StatsRequest> implements IndicesRequest {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.service.ClusterService;
import org.elasticsearch.common.settings.Setting;
Expand Down Expand Up @@ -51,10 +50,10 @@ Collection<MonitoringDoc> innerDoCollect(
long interval,
MonitoringDoc.Node node) throws Exception {

final CcrStatsAction.StatsRequest request = new CcrStatsAction.StatsRequest();
request.setIndices(getCollectionIndices());
request.setIndicesOptions(IndicesOptions.lenientExpandOpen());
final CcrStatsAction.StatsResponses responses = ccrClient.stats(request).actionGet(getCollectionTimeout());

final CcrStatsAction.StatsRequest request = new CcrStatsAction.StatsRequest();
request.setIndices(getCollectionIndices());
final CcrStatsAction.StatsResponses responses = ccrClient.stats(request).actionGet(getCollectionTimeout());

return responses
.getStatsResponses()
Expand Down

0 comments on commit 17b3b97

Please sign in to comment.