Skip to content

Commit eb26139

Browse files
committed
[CCR] Added HLRC support for pause follow API (#35216)
* Moved `AcknowledgedResponse` to core package * Made AcknowledgedResponse not abstract and provided a default parser, so that in cases when the field name is not overwritten then there is no need for a subclass. Relates to #33824
1 parent 10be8ad commit eb26139

13 files changed

+417
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client;
21+
22+
import org.elasticsearch.action.ActionListener;
23+
import org.elasticsearch.client.ccr.PauseFollowRequest;
24+
import org.elasticsearch.client.core.AcknowledgedResponse;
25+
26+
import java.io.IOException;
27+
import java.util.Collections;
28+
29+
/**
30+
* A wrapper for the {@link RestHighLevelClient} that provides methods for
31+
* accessing the Elastic ccr related methods
32+
* <p>
33+
* See the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-apis.html">
34+
* X-Pack Rollup APIs on elastic.co</a> for more information.
35+
*/
36+
public final class CcrClient {
37+
38+
private final RestHighLevelClient restHighLevelClient;
39+
40+
CcrClient(RestHighLevelClient restHighLevelClient) {
41+
this.restHighLevelClient = restHighLevelClient;
42+
}
43+
44+
/**
45+
* Instructs a follower index the pause the following of a leader index.
46+
*
47+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-pause-follow.html">
48+
* the docs</a> for more.
49+
*
50+
* @param request the request
51+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
52+
* @return the response
53+
* @throws IOException in case there is a problem sending the request or parsing back the response
54+
*/
55+
public AcknowledgedResponse pauseFollow(PauseFollowRequest request, RequestOptions options) throws IOException {
56+
return restHighLevelClient.performRequestAndParseEntity(
57+
request,
58+
CcrRequestConverters::pauseFollow,
59+
options,
60+
AcknowledgedResponse::fromXContent,
61+
Collections.emptySet()
62+
);
63+
}
64+
65+
/**
66+
* Asynchronously instruct a follower index the pause the following of a leader index.
67+
*
68+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-pause-follow.html">
69+
* the docs</a> for more.
70+
*
71+
* @param request the request
72+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
73+
*/
74+
public void pauseFollowAsync(PauseFollowRequest request,
75+
RequestOptions options,
76+
ActionListener<AcknowledgedResponse> listener) {
77+
restHighLevelClient.performRequestAsyncAndParseEntity(
78+
request,
79+
CcrRequestConverters::pauseFollow,
80+
options,
81+
AcknowledgedResponse::fromXContent,
82+
listener,
83+
Collections.emptySet());
84+
}
85+
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client;
21+
22+
import org.apache.http.client.methods.HttpPost;
23+
import org.elasticsearch.client.ccr.PauseFollowRequest;
24+
25+
final class CcrRequestConverters {
26+
27+
static Request pauseFollow(PauseFollowRequest pauseFollowRequest) {
28+
String endpoint = new RequestConverters.EndpointBuilder()
29+
.addPathPart(pauseFollowRequest.getFollowerIndex())
30+
.addPathPartAsIs("_ccr", "pause_follow")
31+
.build();
32+
return new Request(HttpPost.METHOD_NAME, endpoint);
33+
}
34+
35+
}

client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java

+15
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ public class RestHighLevelClient implements Closeable {
226226
private final MachineLearningClient machineLearningClient = new MachineLearningClient(this);
227227
private final SecurityClient securityClient = new SecurityClient(this);
228228
private final RollupClient rollupClient = new RollupClient(this);
229+
private final CcrClient ccrClient = new CcrClient(this);
229230

230231
/**
231232
* Creates a {@link RestHighLevelClient} given the low level {@link RestClientBuilder} that allows to build the
@@ -319,6 +320,20 @@ public RollupClient rollup() {
319320
return rollupClient;
320321
}
321322

323+
/**
324+
* Provides methods for accessing the Elastic Licensed CCR APIs that
325+
* are shipped with the Elastic Stack distribution of Elasticsearch. All of
326+
* these APIs will 404 if run against the OSS distribution of Elasticsearch.
327+
* <p>
328+
* See the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-api.html">
329+
* CCR APIs on elastic.co</a> for more information.
330+
*
331+
* @return the client wrapper for making CCR API calls
332+
*/
333+
public final CcrClient ccr() {
334+
return ccrClient;
335+
}
336+
322337
/**
323338
* Provides a {@link TasksClient} which can be used to access the Tasks API.
324339
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.ccr;
21+
22+
import org.elasticsearch.client.Validatable;
23+
24+
import java.util.Objects;
25+
26+
public final class PauseFollowRequest implements Validatable {
27+
28+
private final String followerIndex;
29+
30+
public PauseFollowRequest(String followerIndex) {
31+
this.followerIndex = Objects.requireNonNull(followerIndex);
32+
}
33+
34+
public String getFollowerIndex() {
35+
return followerIndex;
36+
}
37+
}

client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/AcknowledgedResponse.java renamed to client/rest-high-level/src/main/java/org/elasticsearch/client/core/AcknowledgedResponse.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,28 @@
1717
* under the License.
1818
*/
1919

20-
package org.elasticsearch.client.rollup;
20+
package org.elasticsearch.client.core;
2121

2222
import org.elasticsearch.common.ParseField;
2323
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
2424
import org.elasticsearch.common.xcontent.ToXContent;
2525
import org.elasticsearch.common.xcontent.ToXContentObject;
2626
import org.elasticsearch.common.xcontent.XContentBuilder;
27+
import org.elasticsearch.common.xcontent.XContentParser;
2728

2829
import java.io.IOException;
2930
import java.util.Objects;
3031
import java.util.function.Function;
3132

3233
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
3334

34-
public abstract class AcknowledgedResponse implements ToXContentObject {
35-
private final boolean acknowledged;
35+
public class AcknowledgedResponse implements ToXContentObject {
3636

3737
protected static final String PARSE_FIELD_NAME = "acknowledged";
38+
private static final ConstructingObjectParser<AcknowledgedResponse, Void> PARSER = AcknowledgedResponse
39+
.generateParser("acknowledged_response", AcknowledgedResponse::new, AcknowledgedResponse.PARSE_FIELD_NAME);
40+
41+
private final boolean acknowledged;
3842

3943
public AcknowledgedResponse(final boolean acknowledged) {
4044
this.acknowledged = acknowledged;
@@ -50,6 +54,10 @@ protected static <T> ConstructingObjectParser<T, Void> generateParser(String nam
5054
return p;
5155
}
5256

57+
public static AcknowledgedResponse fromXContent(final XContentParser parser) throws IOException {
58+
return PARSER.parse(parser, null);
59+
}
60+
5361
@Override
5462
public boolean equals(Object o) {
5563
if (this == o) {

client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/DeleteRollupJobResponse.java

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.client.rollup;
2121

22+
import org.elasticsearch.client.core.AcknowledgedResponse;
2223
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
2324
import org.elasticsearch.common.xcontent.XContentParser;
2425

client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/PutRollupJobResponse.java

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.elasticsearch.client.rollup;
2020

21+
import org.elasticsearch.client.core.AcknowledgedResponse;
2122
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
2223
import org.elasticsearch.common.xcontent.XContentParser;
2324

client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/StartRollupJobResponse.java

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.client.rollup;
2121

22+
import org.elasticsearch.client.core.AcknowledgedResponse;
2223
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
2324
import org.elasticsearch.common.xcontent.XContentParser;
2425

client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,8 @@ public void testApiNamingConventions() throws Exception {
802802
apiName.startsWith("graph.") == false &&
803803
apiName.startsWith("migration.") == false &&
804804
apiName.startsWith("security.") == false &&
805-
apiName.startsWith("index_lifecycle.") == false) {
805+
apiName.startsWith("index_lifecycle.") == false &&
806+
apiName.startsWith("ccr.") == false) {
806807
apiNotFound.add(apiName);
807808
}
808809
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client.core;
20+
21+
import org.elasticsearch.common.xcontent.XContentParser;
22+
import org.elasticsearch.test.AbstractXContentTestCase;
23+
24+
import java.io.IOException;
25+
26+
public class AcknowledgedResponseTests extends AbstractXContentTestCase<AcknowledgedResponse> {
27+
28+
@Override
29+
protected AcknowledgedResponse createTestInstance() {
30+
return new AcknowledgedResponse(randomBoolean());
31+
}
32+
33+
@Override
34+
protected AcknowledgedResponse doParseInstance(XContentParser parser) throws IOException {
35+
return AcknowledgedResponse.fromXContent(parser);
36+
}
37+
38+
@Override
39+
protected boolean supportsUnknownFields() {
40+
return false;
41+
}
42+
43+
}

0 commit comments

Comments
 (0)