Skip to content

Commit f4aac8d

Browse files
authored
[HLRC] Added support for Follow Stats API (#36253)
This change also adds documentation for the Follow Stats API. Relates to #33824
1 parent 633ab24 commit f4aac8d

File tree

10 files changed

+480
-8
lines changed

10 files changed

+480
-8
lines changed

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

+47-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.elasticsearch.client.ccr.CcrStatsRequest;
2424
import org.elasticsearch.client.ccr.CcrStatsResponse;
2525
import org.elasticsearch.client.ccr.DeleteAutoFollowPatternRequest;
26+
import org.elasticsearch.client.ccr.FollowStatsRequest;
27+
import org.elasticsearch.client.ccr.FollowStatsResponse;
2628
import org.elasticsearch.client.ccr.GetAutoFollowPatternRequest;
2729
import org.elasticsearch.client.ccr.GetAutoFollowPatternResponse;
2830
import org.elasticsearch.client.ccr.PauseFollowRequest;
@@ -385,7 +387,7 @@ public CcrStatsResponse getCcrStats(CcrStatsRequest request,
385387
}
386388

387389
/**
388-
* Gets all CCR stats.
390+
* Asynchronously gets all CCR stats.
389391
*
390392
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-get-stats.html">
391393
* the docs</a> for more.
@@ -406,4 +408,48 @@ public void getCcrStatsAsync(CcrStatsRequest request,
406408
);
407409
}
408410

411+
/**
412+
* Gets follow stats for specific indices.
413+
*
414+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-get-follow-stats.html">
415+
* the docs</a> for more.
416+
*
417+
* @param request the request
418+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
419+
* @return the response
420+
* @throws IOException in case there is a problem sending the request or parsing back the response
421+
*/
422+
public FollowStatsResponse getFollowStats(FollowStatsRequest request,
423+
RequestOptions options) throws IOException {
424+
return restHighLevelClient.performRequestAndParseEntity(
425+
request,
426+
CcrRequestConverters::getFollowStats,
427+
options,
428+
FollowStatsResponse::fromXContent,
429+
Collections.emptySet()
430+
);
431+
}
432+
433+
/**
434+
* Asynchronously gets follow stats for specific indices.
435+
*
436+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-get-follow-stats.html">
437+
* the docs</a> for more.
438+
*
439+
* @param request the request
440+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
441+
*/
442+
public void getFollowStatsAsync(FollowStatsRequest request,
443+
RequestOptions options,
444+
ActionListener<FollowStatsResponse> listener) {
445+
restHighLevelClient.performRequestAsyncAndParseEntity(
446+
request,
447+
CcrRequestConverters::getFollowStats,
448+
options,
449+
FollowStatsResponse::fromXContent,
450+
listener,
451+
Collections.emptySet()
452+
);
453+
}
454+
409455
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.http.client.methods.HttpPut;
2626
import org.elasticsearch.client.ccr.CcrStatsRequest;
2727
import org.elasticsearch.client.ccr.DeleteAutoFollowPatternRequest;
28+
import org.elasticsearch.client.ccr.FollowStatsRequest;
2829
import org.elasticsearch.client.ccr.GetAutoFollowPatternRequest;
2930
import org.elasticsearch.client.ccr.PauseFollowRequest;
3031
import org.elasticsearch.client.ccr.PutAutoFollowPatternRequest;
@@ -108,4 +109,12 @@ static Request getCcrStats(CcrStatsRequest ccrStatsRequest) {
108109
return new Request(HttpGet.METHOD_NAME, endpoint);
109110
}
110111

112+
static Request getFollowStats(FollowStatsRequest followStatsRequest) {
113+
String endpoint = new RequestConverters.EndpointBuilder()
114+
.addPathPart(followStatsRequest.getFollowerIndex())
115+
.addPathPartAsIs("_ccr", "stats")
116+
.build();
117+
return new Request(HttpGet.METHOD_NAME, endpoint);
118+
}
119+
111120
}
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 FollowStatsRequest implements Validatable {
27+
28+
private final String followerIndex;
29+
30+
public FollowStatsRequest(String followerIndex) {
31+
this.followerIndex = Objects.requireNonNull(followerIndex);
32+
}
33+
34+
public String getFollowerIndex() {
35+
return followerIndex;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.xcontent.XContentParser;
23+
24+
public final class FollowStatsResponse {
25+
26+
public static FollowStatsResponse fromXContent(XContentParser parser) {
27+
return new FollowStatsResponse(IndicesFollowStats.PARSER.apply(parser, null));
28+
}
29+
30+
private final IndicesFollowStats indicesFollowStats;
31+
32+
public FollowStatsResponse(IndicesFollowStats indicesFollowStats) {
33+
this.indicesFollowStats = indicesFollowStats;
34+
}
35+
36+
public IndicesFollowStats getIndicesFollowStats() {
37+
return indicesFollowStats;
38+
}
39+
}

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.elasticsearch.client.ccr.CcrStatsRequest;
3333
import org.elasticsearch.client.ccr.CcrStatsResponse;
3434
import org.elasticsearch.client.ccr.DeleteAutoFollowPatternRequest;
35+
import org.elasticsearch.client.ccr.FollowStatsRequest;
36+
import org.elasticsearch.client.ccr.FollowStatsResponse;
3537
import org.elasticsearch.client.ccr.GetAutoFollowPatternRequest;
3638
import org.elasticsearch.client.ccr.GetAutoFollowPatternResponse;
3739
import org.elasticsearch.client.ccr.IndicesFollowStats.ShardFollowStats;
@@ -108,9 +110,10 @@ public void testIndexFollowing() throws Exception {
108110
assertThat(leaderSearchResponse.getHits().getTotalHits().value, equalTo(1L));
109111

110112
assertBusy(() -> {
111-
CcrStatsRequest ccrStatsRequest = new CcrStatsRequest();
112-
CcrStatsResponse ccrStatsResponse = execute(ccrStatsRequest, ccrClient::getCcrStats, ccrClient::getCcrStatsAsync);
113-
List<ShardFollowStats> shardFollowStats = ccrStatsResponse.getIndicesFollowStats().getShardFollowStats("follower");
113+
FollowStatsRequest followStatsRequest = new FollowStatsRequest("follower");
114+
FollowStatsResponse followStatsResponse =
115+
execute(followStatsRequest, ccrClient::getFollowStats, ccrClient::getFollowStatsAsync);
116+
List<ShardFollowStats> shardFollowStats = followStatsResponse.getIndicesFollowStats().getShardFollowStats("follower");
114117
long followerGlobalCheckpoint = shardFollowStats.stream()
115118
.mapToLong(ShardFollowStats::getFollowerGlobalCheckpoint)
116119
.max()
@@ -133,9 +136,10 @@ public void testIndexFollowing() throws Exception {
133136
assertThat(resumeFollowResponse.isAcknowledged(), is(true));
134137

135138
assertBusy(() -> {
136-
CcrStatsRequest ccrStatsRequest = new CcrStatsRequest();
137-
CcrStatsResponse ccrStatsResponse = execute(ccrStatsRequest, ccrClient::getCcrStats, ccrClient::getCcrStatsAsync);
138-
List<ShardFollowStats> shardFollowStats = ccrStatsResponse.getIndicesFollowStats().getShardFollowStats("follower");
139+
FollowStatsRequest followStatsRequest = new FollowStatsRequest("follower");
140+
FollowStatsResponse followStatsResponse =
141+
execute(followStatsRequest, ccrClient::getFollowStats, ccrClient::getFollowStatsAsync);
142+
List<ShardFollowStats> shardFollowStats = followStatsResponse.getIndicesFollowStats().getShardFollowStats("follower");
139143
long followerGlobalCheckpoint = shardFollowStats.stream()
140144
.mapToLong(ShardFollowStats::getFollowerGlobalCheckpoint)
141145
.max()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ private static AutoFollowStats randomAutoFollowStats() {
323323
);
324324
}
325325

326-
private static IndicesFollowStats randomIndicesFollowStats() {
326+
static IndicesFollowStats randomIndicesFollowStats() {
327327
int numIndices = randomIntBetween(0, 16);
328328
NavigableMap<String, List<ShardFollowStats>> shardFollowStats = new TreeMap<>();
329329
for (int i = 0; i < numIndices; i++) {

0 commit comments

Comments
 (0)