-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[CCR] Add unfollow API #34132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[CCR] Add unfollow API #34132
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
.../plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportUnfollowAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| * 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.action.ActionListener; | ||
| import org.elasticsearch.action.support.ActionFilters; | ||
| import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
| import org.elasticsearch.action.support.master.TransportMasterNodeAction; | ||
| import org.elasticsearch.cluster.ClusterState; | ||
| import org.elasticsearch.cluster.ClusterStateUpdateTask; | ||
| import org.elasticsearch.cluster.block.ClusterBlockException; | ||
| import org.elasticsearch.cluster.block.ClusterBlockLevel; | ||
| import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
| import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
| import org.elasticsearch.cluster.metadata.MetaData; | ||
| import org.elasticsearch.cluster.service.ClusterService; | ||
| import org.elasticsearch.common.inject.Inject; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.persistent.PersistentTasksCustomMetaData; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.transport.TransportService; | ||
| import org.elasticsearch.xpack.ccr.Ccr; | ||
| import org.elasticsearch.xpack.ccr.CcrSettings; | ||
| import org.elasticsearch.xpack.core.ccr.action.UnfollowAction; | ||
|
|
||
| public class TransportUnfollowAction extends TransportMasterNodeAction<UnfollowAction.Request, AcknowledgedResponse> { | ||
|
|
||
| @Inject | ||
| public TransportUnfollowAction(Settings settings, TransportService transportService, ClusterService clusterService, | ||
| ThreadPool threadPool, ActionFilters actionFilters, | ||
| IndexNameExpressionResolver indexNameExpressionResolver) { | ||
| super(settings, UnfollowAction.NAME, transportService, clusterService, threadPool, actionFilters, | ||
| UnfollowAction.Request::new, indexNameExpressionResolver); | ||
| } | ||
|
|
||
| @Override | ||
| protected String executor() { | ||
| return ThreadPool.Names.SAME; | ||
| } | ||
|
|
||
| @Override | ||
| protected AcknowledgedResponse newResponse() { | ||
| return new AcknowledgedResponse(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void masterOperation(UnfollowAction.Request request, | ||
| ClusterState state, | ||
| ActionListener<AcknowledgedResponse> listener) throws Exception { | ||
| clusterService.submitStateUpdateTask("unfollow_action", new ClusterStateUpdateTask() { | ||
|
|
||
| @Override | ||
| public ClusterState execute(ClusterState current) throws Exception { | ||
| String followerIndex = request.getFollowerIndex(); | ||
| return unfollow(followerIndex, current); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(String source, Exception e) { | ||
| listener.onFailure(e); | ||
| } | ||
|
|
||
| @Override | ||
| public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { | ||
| listener.onResponse(new AcknowledgedResponse(true)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| protected ClusterBlockException checkBlock(UnfollowAction.Request request, ClusterState state) { | ||
| return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_WRITE); | ||
| } | ||
|
|
||
| static ClusterState unfollow(String followerIndex, ClusterState current) { | ||
| IndexMetaData followerIMD = current.metaData().index(followerIndex); | ||
|
|
||
| PersistentTasksCustomMetaData persistentTasks = current.metaData().custom(PersistentTasksCustomMetaData.TYPE); | ||
| if (persistentTasks != null) { | ||
| for (PersistentTasksCustomMetaData.PersistentTask<?> persistentTask : persistentTasks.tasks()) { | ||
| if (persistentTask.getTaskName().equals(ShardFollowTask.NAME)) { | ||
| ShardFollowTask shardFollowTask = (ShardFollowTask) persistentTask.getParams(); | ||
| if (shardFollowTask.getFollowShardId().getIndexName().equals(followerIndex)) { | ||
| throw new IllegalArgumentException("cannot convert the follower index [" + followerIndex + | ||
| "] to a non-follower, because it has not been paused"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (followerIMD.getState() != IndexMetaData.State.CLOSE) { | ||
| throw new IllegalArgumentException("cannot convert the follower index [" + followerIndex + | ||
| "] to a non-follower, because it has not been closed"); | ||
| } | ||
|
|
||
| IndexMetaData.Builder newIMD = IndexMetaData.builder(followerIMD); | ||
| // Remove index.xpack.ccr.following_index setting | ||
| Settings.Builder builder = Settings.builder(); | ||
| builder.put(followerIMD.getSettings()); | ||
| builder.remove(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey()); | ||
|
|
||
| newIMD.settings(builder); | ||
| // Remove ccr custom metadata | ||
| newIMD.removeCustom(Ccr.CCR_CUSTOM_METADATA_KEY); | ||
|
|
||
| MetaData newMetaData = MetaData.builder(current.metaData()) | ||
| .put(newIMD) | ||
| .build(); | ||
| return ClusterState.builder(current) | ||
| .metaData(newMetaData) | ||
| .build(); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestUnfollowAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * 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.rest; | ||
|
|
||
| import org.elasticsearch.client.node.NodeClient; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.rest.BaseRestHandler; | ||
| import org.elasticsearch.rest.RestController; | ||
| import org.elasticsearch.rest.RestRequest; | ||
| import org.elasticsearch.rest.action.RestToXContentListener; | ||
| import org.elasticsearch.xpack.core.ccr.action.UnfollowAction; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import static org.elasticsearch.xpack.core.ccr.action.UnfollowAction.INSTANCE; | ||
|
|
||
| public class RestUnfollowAction extends BaseRestHandler { | ||
|
|
||
| public RestUnfollowAction(Settings settings, RestController controller) { | ||
| super(settings); | ||
| controller.registerHandler(RestRequest.Method.POST, "/{index}/_ccr/unfollow", this); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "ccr_unfollow_action"; | ||
| } | ||
|
|
||
| @Override | ||
| protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { | ||
| UnfollowAction.Request request = new UnfollowAction.Request(restRequest.param("index")); | ||
| return channel -> client.execute(INSTANCE, request, new RestToXContentListener<>(channel)); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...in/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportUnfollowActionTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * 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.Version; | ||
| import org.elasticsearch.cluster.ClusterName; | ||
| import org.elasticsearch.cluster.ClusterState; | ||
| import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
| import org.elasticsearch.cluster.metadata.MetaData; | ||
| import org.elasticsearch.common.unit.TimeValue; | ||
| import org.elasticsearch.index.shard.ShardId; | ||
| import org.elasticsearch.persistent.PersistentTasksCustomMetaData; | ||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.elasticsearch.xpack.ccr.Ccr; | ||
| import org.elasticsearch.xpack.ccr.CcrSettings; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
|
|
||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.nullValue; | ||
|
|
||
| public class TransportUnfollowActionTests extends ESTestCase { | ||
|
|
||
| public void testUnfollow() { | ||
| IndexMetaData.Builder followerIndex = IndexMetaData.builder("follow_index") | ||
| .settings(settings(Version.CURRENT).put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true)) | ||
| .numberOfShards(1) | ||
| .numberOfReplicas(0) | ||
| .state(IndexMetaData.State.CLOSE) | ||
| .putCustom(Ccr.CCR_CUSTOM_METADATA_KEY, new HashMap<>()); | ||
|
|
||
| ClusterState current = ClusterState.builder(new ClusterName("cluster_name")) | ||
| .metaData(MetaData.builder() | ||
| .put(followerIndex) | ||
| .build()) | ||
| .build(); | ||
| ClusterState result = TransportUnfollowAction.unfollow("follow_index", current); | ||
|
|
||
| IndexMetaData resultIMD = result.metaData().index("follow_index"); | ||
| assertThat(resultIMD.getSettings().get(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey()), nullValue()); | ||
| assertThat(resultIMD.getCustomData(Ccr.CCR_CUSTOM_METADATA_KEY), nullValue()); | ||
| } | ||
|
|
||
| public void testUnfollowIndexOpen() { | ||
| IndexMetaData.Builder followerIndex = IndexMetaData.builder("follow_index") | ||
| .settings(settings(Version.CURRENT).put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true)) | ||
| .numberOfShards(1) | ||
| .numberOfReplicas(0) | ||
| .putCustom(Ccr.CCR_CUSTOM_METADATA_KEY, new HashMap<>()); | ||
|
|
||
| ClusterState current = ClusterState.builder(new ClusterName("cluster_name")) | ||
| .metaData(MetaData.builder() | ||
| .put(followerIndex) | ||
| .build()) | ||
| .build(); | ||
| Exception e = expectThrows(IllegalArgumentException.class, () -> TransportUnfollowAction.unfollow("follow_index", current)); | ||
| assertThat(e.getMessage(), | ||
| equalTo("cannot convert the follower index [follow_index] to a non-follower, because it has not been closed")); | ||
| } | ||
|
|
||
| public void testUnfollowRunningShardFollowTasks() { | ||
| IndexMetaData.Builder followerIndex = IndexMetaData.builder("follow_index") | ||
| .settings(settings(Version.CURRENT).put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true)) | ||
| .numberOfShards(1) | ||
| .numberOfReplicas(0) | ||
| .state(IndexMetaData.State.CLOSE) | ||
| .putCustom(Ccr.CCR_CUSTOM_METADATA_KEY, new HashMap<>()); | ||
|
|
||
|
|
||
| ShardFollowTask params = new ShardFollowTask( | ||
| null, | ||
| new ShardId("follow_index", "", 0), | ||
| new ShardId("leader_index", "", 0), | ||
| 1024, | ||
| 1, | ||
| TransportResumeFollowAction.DEFAULT_MAX_BATCH_SIZE_IN_BYTES, | ||
| 1, | ||
| 10240, | ||
| TimeValue.timeValueMillis(10), | ||
| TimeValue.timeValueMillis(10), | ||
| "uuid", | ||
| Collections.emptyMap() | ||
| ); | ||
| PersistentTasksCustomMetaData.PersistentTask<?> task = | ||
| new PersistentTasksCustomMetaData.PersistentTask<>("id", ShardFollowTask.NAME, params, 0, null); | ||
|
|
||
| ClusterState current = ClusterState.builder(new ClusterName("cluster_name")) | ||
| .metaData(MetaData.builder() | ||
| .put(followerIndex) | ||
| .putCustom(PersistentTasksCustomMetaData.TYPE, new PersistentTasksCustomMetaData(0, Collections.singletonMap("id", task))) | ||
| .build()) | ||
| .build(); | ||
| Exception e = expectThrows(IllegalArgumentException.class, () -> TransportUnfollowAction.unfollow("follow_index", current)); | ||
| assertThat(e.getMessage(), | ||
| equalTo("cannot convert the follower index [follow_index] to a non-follower, because it has not been paused")); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is not really related to the unfollow api, but it allows this: https://github.com/elastic/elasticsearch/pull/34132/files#diff-bd430812001c493a52293cc54e56ea24R36 (immutable fields)
and I think it good that at some point we can not use
Streamableanymore. I want to go over other ccr request classes in a follow up change.