Skip to content

Commit 2ecd87d

Browse files
martijnvgspinscale
authored andcommitted
[HLRC] Added support for CCR Get Auto Follow Pattern apis (#36049)
This change also adds documentation for the Get Auto Follow Pattern API. Relates to #33824
1 parent 0686a13 commit 2ecd87d

File tree

9 files changed

+487
-1
lines changed

9 files changed

+487
-1
lines changed

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

+48-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import org.elasticsearch.action.ActionListener;
2323
import org.elasticsearch.client.ccr.DeleteAutoFollowPatternRequest;
24+
import org.elasticsearch.client.ccr.GetAutoFollowPatternRequest;
25+
import org.elasticsearch.client.ccr.GetAutoFollowPatternResponse;
2426
import org.elasticsearch.client.ccr.PauseFollowRequest;
2527
import org.elasticsearch.client.ccr.PutAutoFollowPatternRequest;
2628
import org.elasticsearch.client.ccr.PutFollowRequest;
@@ -291,7 +293,7 @@ public AcknowledgedResponse deleteAutoFollowPattern(DeleteAutoFollowPatternReque
291293
}
292294

293295
/**
294-
* Deletes an auto follow pattern.
296+
* Asynchronously deletes an auto follow pattern.
295297
*
296298
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-delete-auto-follow-pattern.html">
297299
* the docs</a> for more.
@@ -313,4 +315,49 @@ public void deleteAutoFollowPatternAsync(DeleteAutoFollowPatternRequest request,
313315
);
314316
}
315317

318+
/**
319+
* Gets an auto follow pattern.
320+
*
321+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-get-auto-follow-pattern.html">
322+
* the docs</a> for more.
323+
*
324+
* @param request the request
325+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
326+
* @return the response
327+
* @throws IOException in case there is a problem sending the request or parsing back the response
328+
*/
329+
public GetAutoFollowPatternResponse getAutoFollowPattern(GetAutoFollowPatternRequest request,
330+
RequestOptions options) throws IOException {
331+
return restHighLevelClient.performRequestAndParseEntity(
332+
request,
333+
CcrRequestConverters::getAutoFollowPattern,
334+
options,
335+
GetAutoFollowPatternResponse::fromXContent,
336+
Collections.emptySet()
337+
);
338+
}
339+
340+
/**
341+
* Asynchronously gets an auto follow pattern.
342+
*
343+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-get-auto-follow-pattern.html">
344+
* the docs</a> for more.
345+
*
346+
* @param request the request
347+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
348+
* @param listener the listener to be notified upon request completion
349+
*/
350+
public void getAutoFollowPatternAsync(GetAutoFollowPatternRequest request,
351+
RequestOptions options,
352+
ActionListener<GetAutoFollowPatternResponse> listener) {
353+
restHighLevelClient.performRequestAsyncAndParseEntity(
354+
request,
355+
CcrRequestConverters::getAutoFollowPattern,
356+
options,
357+
GetAutoFollowPatternResponse::fromXContent,
358+
listener,
359+
Collections.emptySet()
360+
);
361+
}
362+
316363
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
package org.elasticsearch.client;
2121

2222
import org.apache.http.client.methods.HttpDelete;
23+
import org.apache.http.client.methods.HttpGet;
2324
import org.apache.http.client.methods.HttpPost;
2425
import org.apache.http.client.methods.HttpPut;
2526
import org.elasticsearch.client.ccr.DeleteAutoFollowPatternRequest;
27+
import org.elasticsearch.client.ccr.GetAutoFollowPatternRequest;
2628
import org.elasticsearch.client.ccr.PauseFollowRequest;
2729
import org.elasticsearch.client.ccr.PutAutoFollowPatternRequest;
2830
import org.elasticsearch.client.ccr.PutFollowRequest;
@@ -90,4 +92,12 @@ static Request deleteAutoFollowPattern(DeleteAutoFollowPatternRequest deleteAuto
9092
return new Request(HttpDelete.METHOD_NAME, endpoint);
9193
}
9294

95+
static Request getAutoFollowPattern(GetAutoFollowPatternRequest getAutoFollowPatternRequest) {
96+
String endpoint = new RequestConverters.EndpointBuilder()
97+
.addPathPartAsIs("_ccr", "auto_follow")
98+
.addPathPart(getAutoFollowPatternRequest.getName())
99+
.build();
100+
return new Request(HttpGet.METHOD_NAME, endpoint);
101+
}
102+
93103
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
/**
27+
* Request class for get auto follow pattern api.
28+
*/
29+
public final class GetAutoFollowPatternRequest implements Validatable {
30+
31+
private final String name;
32+
33+
/**
34+
* Get all auto follow patterns
35+
*/
36+
public GetAutoFollowPatternRequest() {
37+
this.name = null;
38+
}
39+
40+
/**
41+
* Get auto follow pattern with the specified name
42+
*
43+
* @param name The name of the auto follow pattern to get
44+
*/
45+
public GetAutoFollowPatternRequest(String name) {
46+
this.name = Objects.requireNonNull(name);
47+
}
48+
49+
public String getName() {
50+
return name;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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.common.unit.ByteSizeValue;
23+
import org.elasticsearch.common.unit.TimeValue;
24+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
25+
import org.elasticsearch.common.xcontent.ObjectParser;
26+
import org.elasticsearch.common.xcontent.XContentParser;
27+
import org.elasticsearch.common.xcontent.XContentParser.Token;
28+
29+
import java.io.IOException;
30+
import java.util.Collections;
31+
import java.util.HashMap;
32+
import java.util.List;
33+
import java.util.Map;
34+
import java.util.Objects;
35+
36+
public final class GetAutoFollowPatternResponse {
37+
38+
public static GetAutoFollowPatternResponse fromXContent(final XContentParser parser) throws IOException {
39+
final Map<String, Pattern> patterns = new HashMap<>();
40+
for (Token token = parser.nextToken(); token != Token.END_OBJECT; token = parser.nextToken()) {
41+
if (token == Token.FIELD_NAME) {
42+
final String name = parser.currentName();
43+
final Pattern pattern = Pattern.PARSER.parse(parser, null);
44+
patterns.put(name, pattern);
45+
}
46+
}
47+
return new GetAutoFollowPatternResponse(patterns);
48+
}
49+
50+
private final Map<String, Pattern> patterns;
51+
52+
GetAutoFollowPatternResponse(Map<String, Pattern> patterns) {
53+
this.patterns = Collections.unmodifiableMap(patterns);
54+
}
55+
56+
public Map<String, Pattern> getPatterns() {
57+
return patterns;
58+
}
59+
60+
@Override
61+
public boolean equals(Object o) {
62+
if (this == o) return true;
63+
if (o == null || getClass() != o.getClass()) return false;
64+
GetAutoFollowPatternResponse that = (GetAutoFollowPatternResponse) o;
65+
return Objects.equals(patterns, that.patterns);
66+
}
67+
68+
@Override
69+
public int hashCode() {
70+
return Objects.hash(patterns);
71+
}
72+
73+
public static class Pattern extends FollowConfig {
74+
75+
@SuppressWarnings("unchecked")
76+
private static final ConstructingObjectParser<Pattern, Void> PARSER = new ConstructingObjectParser<>(
77+
"pattern", args -> new Pattern((String) args[0], (List<String>) args[1], (String) args[2]));
78+
79+
static {
80+
PARSER.declareString(ConstructingObjectParser.constructorArg(), PutFollowRequest.REMOTE_CLUSTER_FIELD);
81+
PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), PutAutoFollowPatternRequest.LEADER_PATTERNS_FIELD);
82+
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), PutAutoFollowPatternRequest.FOLLOW_PATTERN_FIELD);
83+
PARSER.declareInt(Pattern::setMaxReadRequestOperationCount, FollowConfig.MAX_READ_REQUEST_OPERATION_COUNT);
84+
PARSER.declareField(
85+
Pattern::setMaxReadRequestSize,
86+
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_READ_REQUEST_SIZE.getPreferredName()),
87+
PutFollowRequest.MAX_READ_REQUEST_SIZE,
88+
ObjectParser.ValueType.STRING);
89+
PARSER.declareInt(Pattern::setMaxOutstandingReadRequests, FollowConfig.MAX_OUTSTANDING_READ_REQUESTS);
90+
PARSER.declareInt(Pattern::setMaxWriteRequestOperationCount, FollowConfig.MAX_WRITE_REQUEST_OPERATION_COUNT);
91+
PARSER.declareField(
92+
Pattern::setMaxWriteRequestSize,
93+
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_REQUEST_SIZE.getPreferredName()),
94+
PutFollowRequest.MAX_WRITE_REQUEST_SIZE,
95+
ObjectParser.ValueType.STRING);
96+
PARSER.declareInt(Pattern::setMaxOutstandingWriteRequests, FollowConfig.MAX_OUTSTANDING_WRITE_REQUESTS);
97+
PARSER.declareInt(Pattern::setMaxWriteBufferCount, FollowConfig.MAX_WRITE_BUFFER_COUNT);
98+
PARSER.declareField(
99+
Pattern::setMaxWriteBufferSize,
100+
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_BUFFER_SIZE.getPreferredName()),
101+
PutFollowRequest.MAX_WRITE_BUFFER_SIZE,
102+
ObjectParser.ValueType.STRING);
103+
PARSER.declareField(
104+
Pattern::setMaxRetryDelay,
105+
(p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.MAX_RETRY_DELAY_FIELD.getPreferredName()),
106+
PutFollowRequest.MAX_RETRY_DELAY_FIELD,
107+
ObjectParser.ValueType.STRING);
108+
PARSER.declareField(
109+
Pattern::setReadPollTimeout,
110+
(p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.READ_POLL_TIMEOUT.getPreferredName()),
111+
PutFollowRequest.READ_POLL_TIMEOUT,
112+
ObjectParser.ValueType.STRING);
113+
}
114+
115+
private final String remoteCluster;
116+
private final List<String> leaderIndexPatterns;
117+
private final String followIndexNamePattern;
118+
119+
Pattern(String remoteCluster, List<String> leaderIndexPatterns, String followIndexNamePattern) {
120+
this.remoteCluster = remoteCluster;
121+
this.leaderIndexPatterns = leaderIndexPatterns;
122+
this.followIndexNamePattern = followIndexNamePattern;
123+
}
124+
125+
public String getRemoteCluster() {
126+
return remoteCluster;
127+
}
128+
129+
public List<String> getLeaderIndexPatterns() {
130+
return leaderIndexPatterns;
131+
}
132+
133+
public String getFollowIndexNamePattern() {
134+
return followIndexNamePattern;
135+
}
136+
137+
@Override
138+
public boolean equals(Object o) {
139+
if (this == o) return true;
140+
if (o == null || getClass() != o.getClass()) return false;
141+
if (!super.equals(o)) return false;
142+
Pattern pattern = (Pattern) o;
143+
return Objects.equals(remoteCluster, pattern.remoteCluster) &&
144+
Objects.equals(leaderIndexPatterns, pattern.leaderIndexPatterns) &&
145+
Objects.equals(followIndexNamePattern, pattern.followIndexNamePattern);
146+
}
147+
148+
@Override
149+
public int hashCode() {
150+
return Objects.hash(
151+
super.hashCode(),
152+
remoteCluster,
153+
leaderIndexPatterns,
154+
followIndexNamePattern
155+
);
156+
}
157+
}
158+
159+
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.elasticsearch.action.search.SearchResponse;
3131
import org.elasticsearch.action.support.WriteRequest;
3232
import org.elasticsearch.client.ccr.DeleteAutoFollowPatternRequest;
33+
import org.elasticsearch.client.ccr.GetAutoFollowPatternRequest;
34+
import org.elasticsearch.client.ccr.GetAutoFollowPatternResponse;
3335
import org.elasticsearch.client.ccr.PauseFollowRequest;
3436
import org.elasticsearch.client.ccr.PutAutoFollowPatternRequest;
3537
import org.elasticsearch.client.ccr.PutFollowRequest;
@@ -48,6 +50,7 @@
4850

4951
import static org.hamcrest.Matchers.equalTo;
5052
import static org.hamcrest.Matchers.is;
53+
import static org.hamcrest.Matchers.notNullValue;
5154

5255
public class CCRIT extends ESRestHighLevelClientTestCase {
5356

@@ -148,6 +151,17 @@ public void testAutoFollowing() throws Exception {
148151
assertThat(indexExists("copy-logs-20200101"), is(true));
149152
});
150153

154+
GetAutoFollowPatternRequest getAutoFollowPatternRequest =
155+
randomBoolean() ? new GetAutoFollowPatternRequest("pattern1") : new GetAutoFollowPatternRequest();
156+
GetAutoFollowPatternResponse getAutoFollowPatternResponse =
157+
execute(getAutoFollowPatternRequest, ccrClient::getAutoFollowPattern, ccrClient::getAutoFollowPatternAsync);
158+
assertThat(getAutoFollowPatternResponse.getPatterns().size(), equalTo(1L));
159+
GetAutoFollowPatternResponse.Pattern pattern = getAutoFollowPatternResponse.getPatterns().get("patterns1");
160+
assertThat(pattern, notNullValue());
161+
assertThat(pattern.getRemoteCluster(), equalTo(putAutoFollowPatternRequest.getRemoteCluster()));
162+
assertThat(pattern.getLeaderIndexPatterns(), equalTo(putAutoFollowPatternRequest.getLeaderIndexPatterns()));
163+
assertThat(pattern.getFollowIndexNamePattern(), equalTo(putAutoFollowPatternRequest.getFollowIndexNamePattern()));
164+
151165
// Cleanup:
152166
final DeleteAutoFollowPatternRequest deleteAutoFollowPatternRequest = new DeleteAutoFollowPatternRequest("pattern1");
153167
AcknowledgedResponse deleteAutoFollowPatternResponse =

0 commit comments

Comments
 (0)