Skip to content

Commit e73140e

Browse files
authored
Use CcrRepository to init follower index (#35719) (#37988)
This commit modifies the put follow index action to use a CcrRepository when creating a follower index. It routes the logic through the snapshot/restore process. A wait_for_active_shards parameter can be used to configure how long to wait before returning the response.
1 parent 0b794d4 commit e73140e

File tree

34 files changed

+668
-183
lines changed

34 files changed

+668
-183
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.http.client.methods.HttpGet;
2424
import org.apache.http.client.methods.HttpPost;
2525
import org.apache.http.client.methods.HttpPut;
26+
import org.elasticsearch.action.support.ActiveShardCount;
2627
import org.elasticsearch.client.ccr.CcrStatsRequest;
2728
import org.elasticsearch.client.ccr.DeleteAutoFollowPatternRequest;
2829
import org.elasticsearch.client.ccr.FollowStatsRequest;
@@ -46,6 +47,8 @@ static Request putFollow(PutFollowRequest putFollowRequest) throws IOException {
4647
.addPathPartAsIs("_ccr", "follow")
4748
.build();
4849
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
50+
RequestConverters.Params parameters = new RequestConverters.Params(request);
51+
parameters.withWaitForActiveShards(putFollowRequest.waitForActiveShards(), ActiveShardCount.NONE);
4952
request.setEntity(createEntity(putFollowRequest, REQUEST_BODY_CONTENT_TYPE));
5053
return request;
5154
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.client.ccr;
2121

22+
import org.elasticsearch.action.support.ActiveShardCount;
2223
import org.elasticsearch.client.Validatable;
2324
import org.elasticsearch.common.ParseField;
2425
import org.elasticsearch.common.xcontent.ToXContentObject;
@@ -36,11 +37,17 @@ public final class PutFollowRequest extends FollowConfig implements Validatable,
3637
private final String remoteCluster;
3738
private final String leaderIndex;
3839
private final String followerIndex;
40+
private final ActiveShardCount waitForActiveShards;
3941

4042
public PutFollowRequest(String remoteCluster, String leaderIndex, String followerIndex) {
43+
this(remoteCluster, leaderIndex, followerIndex, ActiveShardCount.NONE);
44+
}
45+
46+
public PutFollowRequest(String remoteCluster, String leaderIndex, String followerIndex, ActiveShardCount waitForActiveShards) {
4147
this.remoteCluster = Objects.requireNonNull(remoteCluster, "remoteCluster");
4248
this.leaderIndex = Objects.requireNonNull(leaderIndex, "leaderIndex");
4349
this.followerIndex = Objects.requireNonNull(followerIndex, "followerIndex");
50+
this.waitForActiveShards = waitForActiveShards;
4451
}
4552

4653
@Override
@@ -66,13 +73,18 @@ public String getFollowerIndex() {
6673
return followerIndex;
6774
}
6875

76+
public ActiveShardCount waitForActiveShards() {
77+
return waitForActiveShards;
78+
}
79+
6980
@Override
7081
public boolean equals(Object o) {
7182
if (this == o) return true;
7283
if (o == null || getClass() != o.getClass()) return false;
7384
if (!super.equals(o)) return false;
7485
PutFollowRequest that = (PutFollowRequest) o;
75-
return Objects.equals(remoteCluster, that.remoteCluster) &&
86+
return Objects.equals(waitForActiveShards, that.waitForActiveShards) &&
87+
Objects.equals(remoteCluster, that.remoteCluster) &&
7688
Objects.equals(leaderIndex, that.leaderIndex) &&
7789
Objects.equals(followerIndex, that.followerIndex);
7890
}
@@ -83,7 +95,7 @@ public int hashCode() {
8395
super.hashCode(),
8496
remoteCluster,
8597
leaderIndex,
86-
followerIndex
87-
);
98+
followerIndex,
99+
waitForActiveShards);
88100
}
89101
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.action.index.IndexRequest;
2727
import org.elasticsearch.action.search.SearchRequest;
2828
import org.elasticsearch.action.search.SearchResponse;
29+
import org.elasticsearch.action.support.ActiveShardCount;
2930
import org.elasticsearch.action.support.WriteRequest;
3031
import org.elasticsearch.client.ccr.CcrStatsRequest;
3132
import org.elasticsearch.client.ccr.CcrStatsResponse;
@@ -98,7 +99,7 @@ public void testIndexFollowing() throws Exception {
9899
CreateIndexResponse response = highLevelClient().indices().create(createIndexRequest, RequestOptions.DEFAULT);
99100
assertThat(response.isAcknowledged(), is(true));
100101

101-
PutFollowRequest putFollowRequest = new PutFollowRequest("local_cluster", "leader", "follower");
102+
PutFollowRequest putFollowRequest = new PutFollowRequest("local_cluster", "leader", "follower", ActiveShardCount.ONE);
102103
PutFollowResponse putFollowResponse = execute(putFollowRequest, ccrClient::putFollow, ccrClient::putFollowAsync);
103104
assertThat(putFollowResponse.isFollowIndexCreated(), is(true));
104105
assertThat(putFollowResponse.isFollowIndexShardsAcked(), is(true));

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse;
2727
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
2828
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
29+
import org.elasticsearch.action.support.ActiveShardCount;
2930
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
3031
import org.elasticsearch.client.Request;
3132
import org.elasticsearch.client.RequestOptions;
@@ -97,7 +98,8 @@ public void testPutFollow() throws Exception {
9798
PutFollowRequest putFollowRequest = new PutFollowRequest(
9899
"local", // <1>
99100
"leader", // <2>
100-
"follower" // <3>
101+
"follower", // <3>
102+
ActiveShardCount.ONE // <4>
101103
);
102104
// end::ccr-put-follow-request
103105

@@ -175,7 +177,7 @@ public void testPauseFollow() throws Exception {
175177
String followIndex = "follower";
176178
// Follow index, so that it can be paused:
177179
{
178-
PutFollowRequest putFollowRequest = new PutFollowRequest("local", "leader", followIndex);
180+
PutFollowRequest putFollowRequest = new PutFollowRequest("local", "leader", followIndex, ActiveShardCount.ONE);
179181
PutFollowResponse putFollowResponse = client.ccr().putFollow(putFollowRequest, RequestOptions.DEFAULT);
180182
assertThat(putFollowResponse.isFollowIndexCreated(), is(true));
181183
assertThat(putFollowResponse.isFollowIndexShardsAcked(), is(true));
@@ -241,7 +243,7 @@ public void testResumeFollow() throws Exception {
241243
String followIndex = "follower";
242244
// Follow index, so that it can be paused:
243245
{
244-
PutFollowRequest putFollowRequest = new PutFollowRequest("local", "leader", followIndex);
246+
PutFollowRequest putFollowRequest = new PutFollowRequest("local", "leader", followIndex, ActiveShardCount.ONE);
245247
PutFollowResponse putFollowResponse = client.ccr().putFollow(putFollowRequest, RequestOptions.DEFAULT);
246248
assertThat(putFollowResponse.isFollowIndexCreated(), is(true));
247249
assertThat(putFollowResponse.isFollowIndexShardsAcked(), is(true));
@@ -317,7 +319,7 @@ public void testUnfollow() throws Exception {
317319
String followIndex = "follower";
318320
// Follow index, pause and close, so that it can be unfollowed:
319321
{
320-
PutFollowRequest putFollowRequest = new PutFollowRequest("local", "leader", followIndex);
322+
PutFollowRequest putFollowRequest = new PutFollowRequest("local", "leader", followIndex, ActiveShardCount.ONE);
321323
PutFollowResponse putFollowResponse = client.ccr().putFollow(putFollowRequest, RequestOptions.DEFAULT);
322324
assertThat(putFollowResponse.isFollowIndexCreated(), is(true));
323325
assertThat(putFollowResponse.isFollowIndexShardsAcked(), is(true));
@@ -349,7 +351,7 @@ public void testUnfollow() throws Exception {
349351
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(followIndex);
350352
assertThat(client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT).isAcknowledged(), is(true));
351353

352-
PutFollowRequest putFollowRequest = new PutFollowRequest("local", "leader", followIndex);
354+
PutFollowRequest putFollowRequest = new PutFollowRequest("local", "leader", followIndex, ActiveShardCount.ONE);
353355
PutFollowResponse putFollowResponse = client.ccr().putFollow(putFollowRequest, RequestOptions.DEFAULT);
354356
assertThat(putFollowResponse.isFollowIndexCreated(), is(true));
355357
assertThat(putFollowResponse.isFollowIndexShardsAcked(), is(true));
@@ -639,7 +641,7 @@ public void testGetFollowStats() throws Exception {
639641
}
640642
{
641643
// Follow index, so that we can query for follow stats:
642-
PutFollowRequest putFollowRequest = new PutFollowRequest("local", "leader", "follower");
644+
PutFollowRequest putFollowRequest = new PutFollowRequest("local", "leader", "follower", ActiveShardCount.ONE);
643645
PutFollowResponse putFollowResponse = client.ccr().putFollow(putFollowRequest, RequestOptions.DEFAULT);
644646
assertThat(putFollowResponse.isFollowIndexCreated(), is(true));
645647
assertThat(putFollowResponse.isFollowIndexShardsAcked(), is(true));

docs/java-rest/high-level/ccr/put_follow.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ include-tagged::{doc-tests-file}[{api}-request]
2020
<1> The name of the remote cluster alias.
2121
<2> The name of the leader in the remote cluster.
2222
<3> The name of the follower index that gets created as part of the put follow API call.
23+
<4> The number of active shard copies to wait for before the put follow API returns a
24+
response, as an `ActiveShardCount`
2325

2426
[id="{upid}-{api}-response"]
2527
==== Response

docs/reference/ccr/apis/follow/get-follow-info.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ replication options and whether the follower indices are active or paused.
2222
2323
[source,js]
2424
--------------------------------------------------
25-
PUT /follower_index/_ccr/follow
25+
PUT /follower_index/_ccr/follow?wait_for_active_shards=1
2626
{
2727
"remote_cluster" : "remote_cluster",
2828
"leader_index" : "leader_index"

docs/reference/ccr/apis/follow/get-follow-stats.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ following tasks associated with each shard for the specified indices.
2121
2222
[source,js]
2323
--------------------------------------------------
24-
PUT /follower_index/_ccr/follow
24+
PUT /follower_index/_ccr/follow?wait_for_active_shards=1
2525
{
2626
"remote_cluster" : "remote_cluster",
2727
"leader_index" : "leader_index"

docs/reference/ccr/apis/follow/post-pause-follow.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ following task.
2424
2525
[source,js]
2626
--------------------------------------------------
27-
PUT /follower_index/_ccr/follow
27+
PUT /follower_index/_ccr/follow?wait_for_active_shards=1
2828
{
2929
"remote_cluster" : "remote_cluster",
3030
"leader_index" : "leader_index"

docs/reference/ccr/apis/follow/post-resume-follow.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ returns, the follower index will resume fetching operations from the leader inde
2323
2424
[source,js]
2525
--------------------------------------------------
26-
PUT /follower_index/_ccr/follow
26+
PUT /follower_index/_ccr/follow?wait_for_active_shards=1
2727
{
2828
"remote_cluster" : "remote_cluster",
2929
"leader_index" : "leader_index"

docs/reference/ccr/apis/follow/post-unfollow.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ irreversible operation.
2727
2828
[source,js]
2929
--------------------------------------------------
30-
PUT /follower_index/_ccr/follow
30+
PUT /follower_index/_ccr/follow?wait_for_active_shards=1
3131
{
3232
"remote_cluster" : "remote_cluster",
3333
"leader_index" : "leader_index"

0 commit comments

Comments
 (0)