From 08faea06a8fb262222b89d7f3d127c91447f5556 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 15 Oct 2018 17:22:07 +0200 Subject: [PATCH 1/3] Changed the resource id of auto follow patterns to be a user defined name instead of being the leader cluster alias name. --- .../xpack/ccr/CcrMultiClusterLicenseIT.java | 4 +- .../xpack/ccr/FollowIndexSecurityIT.java | 10 ++-- .../xpack/ccr/FollowIndexIT.java | 4 +- .../rest-api-spec/test/ccr/auto_follow.yml | 19 +++--- .../ccr/action/AutoFollowCoordinator.java | 24 ++++---- ...ransportDeleteAutoFollowPatternAction.java | 18 +++--- .../TransportGetAutoFollowPatternAction.java | 14 ++--- .../TransportPutAutoFollowPatternAction.java | 19 +++--- .../RestDeleteAutoFollowPatternAction.java | 4 +- .../rest/RestGetAutoFollowPatternAction.java | 4 +- .../rest/RestPutAutoFollowPatternAction.java | 4 +- .../xpack/ccr/AutoFollowMetadataTests.java | 1 + .../elasticsearch/xpack/ccr/CcrLicenseIT.java | 7 ++- .../action/AutoFollowCoordinatorTests.java | 18 +++--- .../xpack/ccr/action/AutoFollowTests.java | 8 ++- .../DeleteAutoFollowPatternRequestTests.java | 2 +- .../GetAutoFollowPatternRequestTests.java | 2 +- .../GetAutoFollowPatternResponseTests.java | 1 + .../PutAutoFollowPatternRequestTests.java | 12 +++- ...ortDeleteAutoFollowPatternActionTests.java | 42 ++++++------- ...nsportGetAutoFollowPatternActionTests.java | 20 +++---- ...nsportPutAutoFollowPatternActionTests.java | 45 ++++++++------ .../xpack/core/ccr/AutoFollowMetadata.java | 23 +++++-- .../action/DeleteAutoFollowPatternAction.java | 22 +++---- .../action/GetAutoFollowPatternAction.java | 18 +++--- .../action/PutAutoFollowPatternAction.java | 60 ++++++++++++------- .../api/ccr.delete_auto_follow_pattern.json | 8 +-- .../api/ccr.get_auto_follow_pattern.json | 8 +-- .../api/ccr.put_auto_follow_pattern.json | 8 +-- 29 files changed, 245 insertions(+), 184 deletions(-) diff --git a/x-pack/plugin/ccr/qa/multi-cluster-with-non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java b/x-pack/plugin/ccr/qa/multi-cluster-with-non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java index 07de603f28a6e..68ebd6e9779cb 100644 --- a/x-pack/plugin/ccr/qa/multi-cluster-with-non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java +++ b/x-pack/plugin/ccr/qa/multi-cluster-with-non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java @@ -50,8 +50,8 @@ public void testFollow() { public void testAutoFollow() throws Exception { assumeFalse("windows is the worst", Constants.WINDOWS); if (runningAgainstLeaderCluster == false) { - final Request request = new Request("PUT", "/_ccr/auto_follow/leader_cluster"); - request.setJsonEntity("{\"leader_index_patterns\":[\"*\"]}"); + final Request request = new Request("PUT", "/_ccr/auto_follow/test_pattern"); + request.setJsonEntity("{\"leader_index_patterns\":[\"*\"], \"leader_cluster\": \"leader_cluster\"}"); client().performRequest(request); // parse the logs and ensure that the auto-coordinator skipped coordination on the leader cluster diff --git a/x-pack/plugin/ccr/qa/multi-cluster-with-security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java b/x-pack/plugin/ccr/qa/multi-cluster-with-security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java index 761260f8ac656..a9a318b442f40 100644 --- a/x-pack/plugin/ccr/qa/multi-cluster-with-security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java +++ b/x-pack/plugin/ccr/qa/multi-cluster-with-security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java @@ -148,14 +148,14 @@ public void testAutoFollowPatterns() throws Exception { String disallowedIndex = "logs-us-20190101"; { - Request request = new Request("PUT", "/_ccr/auto_follow/leader_cluster"); - request.setJsonEntity("{\"leader_index_patterns\": [\"logs-*\"]}"); + Request request = new Request("PUT", "/_ccr/auto_follow/test_pattern"); + request.setJsonEntity("{\"leader_index_patterns\": [\"logs-*\"], \"leader_cluster\": \"leader_cluster\"}"); Exception e = expectThrows(ResponseException.class, () -> assertOK(client().performRequest(request))); assertThat(e.getMessage(), containsString("insufficient privileges to follow index [logs-*]")); } - Request request = new Request("PUT", "/_ccr/auto_follow/leader_cluster"); - request.setJsonEntity("{\"leader_index_patterns\": [\"logs-eu-*\"]}"); + Request request = new Request("PUT", "/_ccr/auto_follow/test_pattern"); + request.setJsonEntity("{\"leader_index_patterns\": [\"logs-eu-*\"], \"leader_cluster\": \"leader_cluster\"}"); assertOK(client().performRequest(request)); try (RestClient leaderClient = buildLeaderClient()) { @@ -187,7 +187,7 @@ public void testAutoFollowPatterns() throws Exception { }); // Cleanup by deleting auto follow pattern and pause following: - request = new Request("DELETE", "/_ccr/auto_follow/leader_cluster"); + request = new Request("DELETE", "/_ccr/auto_follow/test_pattern"); assertOK(client().performRequest(request)); pauseFollow(allowedIndex); } diff --git a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java index 515534e214b43..a907bb7512966 100644 --- a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java +++ b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java @@ -104,8 +104,8 @@ public void testFollowNonExistingLeaderIndex() throws Exception { public void testAutoFollowPatterns() throws Exception { assumeFalse("Test should only run when both clusters are running", runningAgainstLeaderCluster); - Request request = new Request("PUT", "/_ccr/auto_follow/leader_cluster"); - request.setJsonEntity("{\"leader_index_patterns\": [\"logs-*\"]}"); + Request request = new Request("PUT", "/_ccr/auto_follow/test_pattern"); + request.setJsonEntity("{\"leader_index_patterns\": [\"logs-*\"], \"leader_cluster\": \"leader_cluster\"}"); assertOK(client().performRequest(request)); try (RestClient leaderClient = buildLeaderClient()) { diff --git a/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/auto_follow.yml b/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/auto_follow.yml index 2bf7820c10f6a..e562fd5eb9b62 100644 --- a/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/auto_follow.yml +++ b/x-pack/plugin/ccr/qa/rest/src/test/resources/rest-api-spec/test/ccr/auto_follow.yml @@ -2,29 +2,32 @@ "Test put and delete auto follow pattern": - do: ccr.put_auto_follow_pattern: - leader_cluster_alias: _local_ + name: my_pattern body: + leader_cluster: _local_ leader_index_patterns: ['logs-*'] max_concurrent_read_batches: 2 - is_true: acknowledged - do: ccr.get_auto_follow_pattern: - leader_cluster_alias: _local_ - - match: { _local_.leader_index_patterns: ['logs-*'] } - - match: { _local_.max_concurrent_read_batches: 2 } + name: my_pattern + - match: { my_pattern.leader_cluster: '_local_' } + - match: { my_pattern.leader_index_patterns: ['logs-*'] } + - match: { my_pattern.max_concurrent_read_batches: 2 } - do: ccr.get_auto_follow_pattern: {} - - match: { _local_.leader_index_patterns: ['logs-*'] } - - match: { _local_.max_concurrent_read_batches: 2 } + - match: { my_pattern.leader_cluster: '_local_' } + - match: { my_pattern.leader_index_patterns: ['logs-*'] } + - match: { my_pattern.max_concurrent_read_batches: 2 } - do: ccr.delete_auto_follow_pattern: - leader_cluster_alias: _local_ + name: my_pattern - is_true: acknowledged - do: catch: missing ccr.get_auto_follow_pattern: - leader_cluster_alias: _local_ + name: my_pattern diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java index b3ed608bd2ab8..e841eb2f7af28 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java @@ -252,21 +252,23 @@ void autoFollowIndices() { int i = 0; for (Map.Entry entry : autoFollowMetadata.getPatterns().entrySet()) { final int slot = i; - final String clusterAlias = entry.getKey(); + final String autoFollowPattenName = entry.getKey(); final AutoFollowPattern autoFollowPattern = entry.getValue(); + final String clusterAlias = autoFollowPattern.getLeaderCluster(); - Map headers = autoFollowMetadata.getHeaders().get(clusterAlias); + Map headers = autoFollowMetadata.getHeaders().get(autoFollowPattenName); getLeaderClusterState(headers, clusterAlias, (leaderClusterState, e) -> { if (leaderClusterState != null) { assert e == null; - final List followedIndices = autoFollowMetadata.getFollowedLeaderIndexUUIDs().get(clusterAlias); + final List followedIndices = autoFollowMetadata.getFollowedLeaderIndexUUIDs().get(autoFollowPattenName); final List leaderIndicesToFollow = getLeaderIndicesToFollow(clusterAlias, autoFollowPattern, leaderClusterState, followerClusterState, followedIndices); if (leaderIndicesToFollow.isEmpty()) { finalise(slot, new AutoFollowResult(clusterAlias)); } else { Consumer resultHandler = result -> finalise(slot, result); - checkAutoFollowPattern(clusterAlias, autoFollowPattern, leaderIndicesToFollow, headers, resultHandler); + checkAutoFollowPattern(autoFollowPattenName, clusterAlias, autoFollowPattern, leaderIndicesToFollow, headers, + resultHandler); } } else { finalise(slot, new AutoFollowResult(clusterAlias, e)); @@ -276,7 +278,8 @@ void autoFollowIndices() { } } - private void checkAutoFollowPattern(String clusterAlias, + private void checkAutoFollowPattern(String autoFollowPattenName, + String clusterAlias, AutoFollowPattern autoFollowPattern, List leaderIndicesToFollow, Map headers, @@ -287,7 +290,7 @@ private void checkAutoFollowPattern(String clusterAlias, for (int i = 0; i < leaderIndicesToFollow.size(); i++) { final Index indexToFollow = leaderIndicesToFollow.get(i); final int slot = i; - followLeaderIndex(clusterAlias, indexToFollow, autoFollowPattern, headers, error -> { + followLeaderIndex(autoFollowPattenName, clusterAlias, indexToFollow, autoFollowPattern, headers, error -> { results.set(slot, new Tuple<>(indexToFollow, error)); if (leaderIndicesCountDown.countDown()) { resultHandler.accept(new AutoFollowResult(clusterAlias, results.asList())); @@ -296,7 +299,8 @@ private void checkAutoFollowPattern(String clusterAlias, } } - private void followLeaderIndex(String clusterAlias, + private void followLeaderIndex(String autoFollowPattenName, + String clusterAlias, Index indexToFollow, AutoFollowPattern pattern, Map headers, @@ -323,7 +327,7 @@ private void followLeaderIndex(String clusterAlias, // This function updates the auto follow metadata in the cluster to record that the leader index has been followed: // (so that we do not try to follow it in subsequent auto follow runs) - Function function = recordLeaderIndexAsFollowFunction(clusterAlias, indexToFollow); + Function function = recordLeaderIndexAsFollowFunction(autoFollowPattenName, indexToFollow); // The coordinator always runs on the elected master node, so we can update cluster state here: updateAutoFollowMetadata(function, onResult); }; @@ -374,12 +378,12 @@ static String getFollowerIndexName(AutoFollowPattern autoFollowPattern, String l } } - static Function recordLeaderIndexAsFollowFunction(String clusterAlias, + static Function recordLeaderIndexAsFollowFunction(String name, Index indexToFollow) { return currentState -> { AutoFollowMetadata currentAutoFollowMetadata = currentState.metaData().custom(AutoFollowMetadata.TYPE); Map> newFollowedIndexUUIDS = new HashMap<>(currentAutoFollowMetadata.getFollowedLeaderIndexUUIDs()); - newFollowedIndexUUIDS.compute(clusterAlias, (key, existingUUIDs) -> { + newFollowedIndexUUIDS.compute(name, (key, existingUUIDs) -> { assert existingUUIDs != null; List newUUIDs = new ArrayList<>(existingUUIDs); newUUIDs.add(indexToFollow.getUUID()); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternAction.java index 974bca988593a..5d5885294f357 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternAction.java @@ -54,7 +54,7 @@ protected AcknowledgedResponse newResponse() { protected void masterOperation(DeleteAutoFollowPatternAction.Request request, ClusterState state, ActionListener listener) throws Exception { - clusterService.submitStateUpdateTask("put-auto-follow-pattern-" + request.getLeaderClusterAlias(), + clusterService.submitStateUpdateTask("put-auto-follow-pattern-" + request.getName(), new AckedClusterStateUpdateTask(request, listener) { @Override @@ -72,23 +72,23 @@ public ClusterState execute(ClusterState currentState) throws Exception { static ClusterState innerDelete(DeleteAutoFollowPatternAction.Request request, ClusterState currentState) { AutoFollowMetadata currentAutoFollowMetadata = currentState.metaData().custom(AutoFollowMetadata.TYPE); if (currentAutoFollowMetadata == null) { - throw new ResourceNotFoundException("no auto-follow patterns for cluster alias [{}] found", - request.getLeaderClusterAlias()); + throw new ResourceNotFoundException("auto-follow pattern [{}] is missing", + request.getName()); } Map patterns = currentAutoFollowMetadata.getPatterns(); - AutoFollowPattern autoFollowPatternToRemove = patterns.get(request.getLeaderClusterAlias()); + AutoFollowPattern autoFollowPatternToRemove = patterns.get(request.getName()); if (autoFollowPatternToRemove == null) { - throw new ResourceNotFoundException("no auto-follow patterns for cluster alias [{}] found", - request.getLeaderClusterAlias()); + throw new ResourceNotFoundException("auto-follow pattern [{}] is missing", + request.getName()); } final Map patternsCopy = new HashMap<>(patterns); final Map> followedLeaderIndexUUIDSCopy = new HashMap<>(currentAutoFollowMetadata.getFollowedLeaderIndexUUIDs()); final Map> headers = new HashMap<>(currentAutoFollowMetadata.getHeaders()); - patternsCopy.remove(request.getLeaderClusterAlias()); - followedLeaderIndexUUIDSCopy.remove(request.getLeaderClusterAlias()); - headers.remove(request.getLeaderClusterAlias()); + patternsCopy.remove(request.getName()); + followedLeaderIndexUUIDSCopy.remove(request.getName()); + headers.remove(request.getName()); AutoFollowMetadata newAutoFollowMetadata = new AutoFollowMetadata(patternsCopy, followedLeaderIndexUUIDSCopy, headers); ClusterState.Builder newState = ClusterState.builder(currentState); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternAction.java index b5b1387315730..9f738026ef640 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternAction.java @@ -55,7 +55,7 @@ protected GetAutoFollowPatternAction.Response newResponse() { protected void masterOperation(GetAutoFollowPatternAction.Request request, ClusterState state, ActionListener listener) throws Exception { - Map autoFollowPatterns = getAutoFollowPattern(state.metaData(), request.getLeaderClusterAlias()); + Map autoFollowPatterns = getAutoFollowPattern(state.metaData(), request.getName()); listener.onResponse(new GetAutoFollowPatternAction.Response(autoFollowPatterns)); } @@ -64,20 +64,20 @@ protected ClusterBlockException checkBlock(GetAutoFollowPatternAction.Request re return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ); } - static Map getAutoFollowPattern(MetaData metaData, String leaderClusterAlias) { + static Map getAutoFollowPattern(MetaData metaData, String name) { AutoFollowMetadata autoFollowMetadata = metaData.custom(AutoFollowMetadata.TYPE); if (autoFollowMetadata == null) { - throw new ResourceNotFoundException("no auto-follow patterns for cluster alias [{}] found", leaderClusterAlias); + throw new ResourceNotFoundException("auto-follow pattern [{}] is missing", name); } - if (leaderClusterAlias == null) { + if (name == null) { return autoFollowMetadata.getPatterns(); } - AutoFollowPattern autoFollowPattern = autoFollowMetadata.getPatterns().get(leaderClusterAlias); + AutoFollowPattern autoFollowPattern = autoFollowMetadata.getPatterns().get(name); if (autoFollowPattern == null) { - throw new ResourceNotFoundException("no auto-follow patterns for cluster alias [{}] found", leaderClusterAlias); + throw new ResourceNotFoundException("auto-follow pattern [{}] is missing", name); } - return Collections.singletonMap(leaderClusterAlias, autoFollowPattern); + return Collections.singletonMap(name, autoFollowPattern); } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternAction.java index c9aa50cfaeb17..2cfec8a4a063b 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternAction.java @@ -77,10 +77,10 @@ protected void masterOperation(PutAutoFollowPatternAction.Request request, return; } final Client leaderClient; - if (request.getLeaderClusterAlias().equals("_local_")) { + if (request.getLeaderCluster().equals("_local_")) { leaderClient = client; } else { - leaderClient = client.getRemoteClusterClient(request.getLeaderClusterAlias()); + leaderClient = client.getRemoteClusterClient(request.getLeaderCluster()); } final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(); @@ -99,7 +99,7 @@ protected void masterOperation(PutAutoFollowPatternAction.Request request, ActionListener.wrap( clusterStateResponse -> { final ClusterState leaderClusterState = clusterStateResponse.getState(); - clusterService.submitStateUpdateTask("put-auto-follow-pattern-" + request.getLeaderClusterAlias(), + clusterService.submitStateUpdateTask("put-auto-follow-pattern-" + request.getLeaderCluster(), new AckedClusterStateUpdateTask(request, listener) { @Override @@ -141,14 +141,14 @@ static ClusterState innerPut(PutAutoFollowPatternAction.Request request, headers = new HashMap<>(); } - AutoFollowPattern previousPattern = patterns.get(request.getLeaderClusterAlias()); + AutoFollowPattern previousPattern = patterns.get(request.getName()); final List followedIndexUUIDs; - if (followedLeaderIndices.containsKey(request.getLeaderClusterAlias())) { - followedIndexUUIDs = new ArrayList<>(followedLeaderIndices.get(request.getLeaderClusterAlias())); + if (followedLeaderIndices.containsKey(request.getName())) { + followedIndexUUIDs = new ArrayList<>(followedLeaderIndices.get(request.getName())); } else { followedIndexUUIDs = new ArrayList<>(); } - followedLeaderIndices.put(request.getLeaderClusterAlias(), followedIndexUUIDs); + followedLeaderIndices.put(request.getName(), followedIndexUUIDs); // Mark existing leader indices as already auto followed: if (previousPattern != null) { markExistingIndicesAsAutoFollowedForNewPatterns(request.getLeaderIndexPatterns(), leaderClusterState.metaData(), @@ -159,10 +159,11 @@ static ClusterState innerPut(PutAutoFollowPatternAction.Request request, } if (filteredHeaders != null) { - headers.put(request.getLeaderClusterAlias(), filteredHeaders); + headers.put(request.getName(), filteredHeaders); } AutoFollowPattern autoFollowPattern = new AutoFollowPattern( + request.getLeaderCluster(), request.getLeaderIndexPatterns(), request.getFollowIndexNamePattern(), request.getMaxBatchOperationCount(), @@ -172,7 +173,7 @@ static ClusterState innerPut(PutAutoFollowPatternAction.Request request, request.getMaxWriteBufferSize(), request.getMaxRetryDelay(), request.getPollTimeout()); - patterns.put(request.getLeaderClusterAlias(), autoFollowPattern); + patterns.put(request.getName(), autoFollowPattern); ClusterState.Builder newState = ClusterState.builder(localState); newState.metaData(MetaData.builder(localState.getMetaData()) .putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(patterns, followedLeaderIndices, headers)) diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestDeleteAutoFollowPatternAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestDeleteAutoFollowPatternAction.java index 91a607de27b6e..72c44a48f985e 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestDeleteAutoFollowPatternAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestDeleteAutoFollowPatternAction.java @@ -21,7 +21,7 @@ public class RestDeleteAutoFollowPatternAction extends BaseRestHandler { public RestDeleteAutoFollowPatternAction(Settings settings, RestController controller) { super(settings); - controller.registerHandler(RestRequest.Method.DELETE, "/_ccr/auto_follow/{leader_cluster_alias}", this); + controller.registerHandler(RestRequest.Method.DELETE, "/_ccr/auto_follow/{name}", this); } @Override @@ -32,7 +32,7 @@ public String getName() { @Override protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { Request request = new Request(); - request.setLeaderClusterAlias(restRequest.param("leader_cluster_alias")); + request.setName(restRequest.param("name")); return channel -> client.execute(INSTANCE, request, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestGetAutoFollowPatternAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestGetAutoFollowPatternAction.java index e9c3073f6c7c3..40858f7f326b8 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestGetAutoFollowPatternAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestGetAutoFollowPatternAction.java @@ -21,7 +21,7 @@ public class RestGetAutoFollowPatternAction extends BaseRestHandler { public RestGetAutoFollowPatternAction(Settings settings, RestController controller) { super(settings); - controller.registerHandler(RestRequest.Method.GET, "/_ccr/auto_follow/{leader_cluster_alias}", this); + controller.registerHandler(RestRequest.Method.GET, "/_ccr/auto_follow/{name}", this); controller.registerHandler(RestRequest.Method.GET, "/_ccr/auto_follow", this); } @@ -33,7 +33,7 @@ public String getName() { @Override protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { Request request = new Request(); - request.setLeaderClusterAlias(restRequest.param("leader_cluster_alias")); + request.setName(restRequest.param("name")); return channel -> client.execute(INSTANCE, request, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestPutAutoFollowPatternAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestPutAutoFollowPatternAction.java index 6b9a4aeff20ed..957312ff78d80 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestPutAutoFollowPatternAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestPutAutoFollowPatternAction.java @@ -22,7 +22,7 @@ public class RestPutAutoFollowPatternAction extends BaseRestHandler { public RestPutAutoFollowPatternAction(Settings settings, RestController controller) { super(settings); - controller.registerHandler(RestRequest.Method.PUT, "/_ccr/auto_follow/{leader_cluster_alias}", this); + controller.registerHandler(RestRequest.Method.PUT, "/_ccr/auto_follow/{name}", this); } @Override @@ -38,7 +38,7 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient static Request createRequest(RestRequest restRequest) throws IOException { try (XContentParser parser = restRequest.contentOrSourceParamParser()) { - return Request.fromXContent(parser, restRequest.param("leader_cluster_alias")); + return Request.fromXContent(parser, restRequest.param("name")); } } } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowMetadataTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowMetadataTests.java index 89bac141ee3d9..67071bd1be5b3 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowMetadataTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowMetadataTests.java @@ -41,6 +41,7 @@ protected AutoFollowMetadata createTestInstance() { for (int i = 0; i < numEntries; i++) { List leaderPatterns = Arrays.asList(generateRandomStringArray(4, 4, false)); AutoFollowMetadata.AutoFollowPattern autoFollowPattern = new AutoFollowMetadata.AutoFollowPattern( + randomAlphaOfLength(4), leaderPatterns, randomAlphaOfLength(4), randomIntBetween(0, Integer.MAX_VALUE), diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrLicenseIT.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrLicenseIT.java index eb0b29912b28a..628f8476e80e4 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrLicenseIT.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrLicenseIT.java @@ -113,7 +113,8 @@ public void onFailure(final Exception e) { public void testThatPutAutoFollowPatternsIsUnavailableWithNonCompliantLicense() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); final PutAutoFollowPatternAction.Request request = new PutAutoFollowPatternAction.Request(); - request.setLeaderClusterAlias("leader"); + request.setName("name"); + request.setLeaderCluster("leader"); request.setLeaderIndexPatterns(Collections.singletonList("*")); client().execute( PutAutoFollowPatternAction.INSTANCE, @@ -142,8 +143,8 @@ public void testAutoFollowCoordinatorLogsSkippingAutoFollowCoordinationWithNonCo @Override public ClusterState execute(ClusterState currentState) throws Exception { - AutoFollowPattern autoFollowPattern = - new AutoFollowPattern(Collections.singletonList("logs-*"), null, null, null, null, null, null, null, null); + AutoFollowPattern autoFollowPattern = new AutoFollowPattern("test_alias", Collections.singletonList("logs-*"), + null, null, null, null, null, null, null, null); AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata( Collections.singletonMap("test_alias", autoFollowPattern), Collections.emptyMap(), diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java index 32d7ea205a980..d81b866ac182a 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java @@ -57,7 +57,7 @@ public void testAutoFollower() { .build(); AutoFollowPattern autoFollowPattern = - new AutoFollowPattern(Collections.singletonList("logs-*"), null, null, null, null, null, null, null, null); + new AutoFollowPattern("remote", Collections.singletonList("logs-*"), null, null, null, null, null, null, null, null); Map patterns = new HashMap<>(); patterns.put("remote", autoFollowPattern); Map> followedLeaderIndexUUIDS = new HashMap<>(); @@ -120,7 +120,7 @@ public void testAutoFollowerClusterStateApiFailure() { when(client.getRemoteClusterClient(anyString())).thenReturn(client); AutoFollowPattern autoFollowPattern = - new AutoFollowPattern(Collections.singletonList("logs-*"), null, null, null, null, null, null, null, null); + new AutoFollowPattern("remote", Collections.singletonList("logs-*"), null, null, null, null, null, null, null, null); Map patterns = new HashMap<>(); patterns.put("remote", autoFollowPattern); Map> followedLeaderIndexUUIDS = new HashMap<>(); @@ -178,7 +178,7 @@ public void testAutoFollowerUpdateClusterStateFailure() { .build(); AutoFollowPattern autoFollowPattern = - new AutoFollowPattern(Collections.singletonList("logs-*"), null, null, null, null, null, null, null, null); + new AutoFollowPattern("remote", Collections.singletonList("logs-*"), null, null, null, null, null, null, null, null); Map patterns = new HashMap<>(); patterns.put("remote", autoFollowPattern); Map> followedLeaderIndexUUIDS = new HashMap<>(); @@ -240,7 +240,7 @@ public void testAutoFollowerCreateAndFollowApiCallFailure() { .build(); AutoFollowPattern autoFollowPattern = - new AutoFollowPattern(Collections.singletonList("logs-*"), null, null, null, null, null, null, null, null); + new AutoFollowPattern("remote", Collections.singletonList("logs-*"), null, null, null, null, null, null, null, null); Map patterns = new HashMap<>(); patterns.put("remote", autoFollowPattern); Map> followedLeaderIndexUUIDS = new HashMap<>(); @@ -293,7 +293,7 @@ void updateAutoFollowMetadata(Function updateFunctio public void testGetLeaderIndicesToFollow() { AutoFollowPattern autoFollowPattern = - new AutoFollowPattern(Collections.singletonList("metrics-*"), null, null, null, null, null, null, null, null); + new AutoFollowPattern("remote", Collections.singletonList("metrics-*"), null, null, null, null, null, null, null, null); Map> headers = new HashMap<>(); ClusterState followerState = ClusterState.builder(new ClusterName("remote")) .metaData(MetaData.builder().putCustom(AutoFollowMetadata.TYPE, @@ -352,7 +352,7 @@ public void testGetLeaderIndicesToFollowDoNotSelectFollowIndicesInTheSameCluster .numberOfReplicas(0)); AutoFollowPattern autoFollowPattern = - new AutoFollowPattern(Collections.singletonList("metrics-*"), null, null, null, null, null, null, null, null); + new AutoFollowPattern("remote", Collections.singletonList("metrics-*"), null, null, null, null, null, null, null, null); imdBuilder.putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(Collections.singletonMap("remote", autoFollowPattern), Collections.emptyMap(), Collections.emptyMap())); @@ -368,15 +368,15 @@ public void testGetLeaderIndicesToFollowDoNotSelectFollowIndicesInTheSameCluster } public void testGetFollowerIndexName() { - AutoFollowPattern autoFollowPattern = new AutoFollowPattern(Collections.singletonList("metrics-*"), null, null, + AutoFollowPattern autoFollowPattern = new AutoFollowPattern("remote", Collections.singletonList("metrics-*"), null, null, null, null, null, null, null, null); assertThat(AutoFollower.getFollowerIndexName(autoFollowPattern, "metrics-0"), equalTo("metrics-0")); - autoFollowPattern = new AutoFollowPattern(Collections.singletonList("metrics-*"), "eu-metrics-0", null, null, + autoFollowPattern = new AutoFollowPattern("remote", Collections.singletonList("metrics-*"), "eu-metrics-0", null, null, null, null, null, null, null); assertThat(AutoFollower.getFollowerIndexName(autoFollowPattern, "metrics-0"), equalTo("eu-metrics-0")); - autoFollowPattern = new AutoFollowPattern(Collections.singletonList("metrics-*"), "eu-{{leader_index}}", null, + autoFollowPattern = new AutoFollowPattern("remote", Collections.singletonList("metrics-*"), "eu-{{leader_index}}", null, null, null, null, null, null, null); assertThat(AutoFollower.getFollowerIndexName(autoFollowPattern, "metrics-0"), equalTo("eu-metrics-0")); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowTests.java index 1101d260dcff4..76a1fad521c0f 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowTests.java @@ -119,7 +119,8 @@ public void testAutoFollowParameterAreDelegated() throws Exception { // Enabling auto following: PutAutoFollowPatternAction.Request request = new PutAutoFollowPatternAction.Request(); - request.setLeaderClusterAlias("_local_"); + request.setName("my-pattern"); + request.setLeaderCluster("_local_"); request.setLeaderIndexPatterns(Collections.singletonList("logs-*")); // Need to set this, because following an index in the same cluster request.setFollowIndexNamePattern("copy-{{leader_index}}"); @@ -181,7 +182,8 @@ public void testAutoFollowParameterAreDelegated() throws Exception { private void putAutoFollowPatterns(String... patterns) { PutAutoFollowPatternAction.Request request = new PutAutoFollowPatternAction.Request(); - request.setLeaderClusterAlias("_local_"); + request.setName("my-pattern"); + request.setLeaderCluster("_local_"); request.setLeaderIndexPatterns(Arrays.asList(patterns)); // Need to set this, because following an index in the same cluster request.setFollowIndexNamePattern("copy-{{leader_index}}"); @@ -190,7 +192,7 @@ private void putAutoFollowPatterns(String... patterns) { private void deleteAutoFollowPatternSetting() { DeleteAutoFollowPatternAction.Request request = new DeleteAutoFollowPatternAction.Request(); - request.setLeaderClusterAlias("_local_"); + request.setName("my-pattern"); assertTrue(client().execute(DeleteAutoFollowPatternAction.INSTANCE, request).actionGet().isAcknowledged()); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/DeleteAutoFollowPatternRequestTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/DeleteAutoFollowPatternRequestTests.java index 251f99800f750..b993132bcfa6c 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/DeleteAutoFollowPatternRequestTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/DeleteAutoFollowPatternRequestTests.java @@ -18,7 +18,7 @@ protected DeleteAutoFollowPatternAction.Request createBlankInstance() { @Override protected DeleteAutoFollowPatternAction.Request createTestInstance() { DeleteAutoFollowPatternAction.Request request = new DeleteAutoFollowPatternAction.Request(); - request.setLeaderClusterAlias(randomAlphaOfLength(4)); + request.setName(randomAlphaOfLength(4)); return request; } } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/GetAutoFollowPatternRequestTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/GetAutoFollowPatternRequestTests.java index 05b731127cc6d..5284dd0fa61c1 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/GetAutoFollowPatternRequestTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/GetAutoFollowPatternRequestTests.java @@ -20,7 +20,7 @@ protected Writeable.Reader instanceReader() protected GetAutoFollowPatternAction.Request createTestInstance() { GetAutoFollowPatternAction.Request request = new GetAutoFollowPatternAction.Request(); if (randomBoolean()) { - request.setLeaderClusterAlias(randomAlphaOfLength(4)); + request.setName(randomAlphaOfLength(4)); } return request; } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/GetAutoFollowPatternResponseTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/GetAutoFollowPatternResponseTests.java index f9850c4e2ed99..e67509f7ee825 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/GetAutoFollowPatternResponseTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/GetAutoFollowPatternResponseTests.java @@ -29,6 +29,7 @@ protected GetAutoFollowPatternAction.Response createTestInstance() { Map patterns = new HashMap<>(numPatterns); for (int i = 0; i < numPatterns; i++) { AutoFollowPattern autoFollowPattern = new AutoFollowPattern( + "remote", Collections.singletonList(randomAlphaOfLength(4)), randomAlphaOfLength(4), randomIntBetween(0, Integer.MAX_VALUE), diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/PutAutoFollowPatternRequestTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/PutAutoFollowPatternRequestTests.java index 865b8884dbe67..2cefc163ee909 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/PutAutoFollowPatternRequestTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/PutAutoFollowPatternRequestTests.java @@ -41,7 +41,8 @@ protected PutAutoFollowPatternAction.Request createBlankInstance() { @Override protected PutAutoFollowPatternAction.Request createTestInstance() { PutAutoFollowPatternAction.Request request = new PutAutoFollowPatternAction.Request(); - request.setLeaderClusterAlias(randomAlphaOfLength(4)); + request.setName(randomAlphaOfLength(4)); + request.setLeaderCluster(randomAlphaOfLength(4)); request.setLeaderIndexPatterns(Arrays.asList(generateRandomStringArray(4, 4, false))); if (randomBoolean()) { request.setFollowIndexNamePattern(randomAlphaOfLength(4)); @@ -74,9 +75,14 @@ public void testValidate() { PutAutoFollowPatternAction.Request request = new PutAutoFollowPatternAction.Request(); ActionRequestValidationException validationException = request.validate(); assertThat(validationException, notNullValue()); - assertThat(validationException.getMessage(), containsString("[leader_cluster_alias] is missing")); + assertThat(validationException.getMessage(), containsString("[name] is missing")); - request.setLeaderClusterAlias("_alias"); + request.setName("name"); + validationException = request.validate(); + assertThat(validationException, notNullValue()); + assertThat(validationException.getMessage(), containsString("[leader_cluster] is missing")); + + request.setLeaderCluster("_alias"); validationException = request.validate(); assertThat(validationException, notNullValue()); assertThat(validationException.getMessage(), containsString("[leader_index_patterns] is missing")); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternActionTests.java index e228031626407..84df243bd9499 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternActionTests.java @@ -10,6 +10,7 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata.AutoFollowPattern; import org.elasticsearch.xpack.core.ccr.action.DeleteAutoFollowPatternAction.Request; import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata; @@ -27,28 +28,28 @@ public class TransportDeleteAutoFollowPatternActionTests extends ESTestCase { public void testInnerDelete() { Map> existingAlreadyFollowedIndexUUIDS = new HashMap<>(); Map> existingHeaders = new HashMap<>(); - Map existingAutoFollowPatterns = new HashMap<>(); + Map existingAutoFollowPatterns = new HashMap<>(); { List existingPatterns = new ArrayList<>(); existingPatterns.add("transactions-*"); - existingAutoFollowPatterns.put("eu_cluster", - new AutoFollowMetadata.AutoFollowPattern(existingPatterns, null, null, null, null, null, null, null, null)); + existingAutoFollowPatterns.put("name1", + new AutoFollowPattern("eu_cluster", existingPatterns, null, null, null, null, null, null, null, null)); List existingUUIDS = new ArrayList<>(); existingUUIDS.add("_val"); - existingAlreadyFollowedIndexUUIDS.put("eu_cluster", existingUUIDS); - existingHeaders.put("eu_cluster", Collections.singletonMap("key", "val")); + existingAlreadyFollowedIndexUUIDS.put("name1", existingUUIDS); + existingHeaders.put("name1", Collections.singletonMap("key", "val")); } { List existingPatterns = new ArrayList<>(); existingPatterns.add("logs-*"); - existingAutoFollowPatterns.put("asia_cluster", - new AutoFollowMetadata.AutoFollowPattern(existingPatterns, null, null, null, null, null, null, null, null)); + existingAutoFollowPatterns.put("name2", + new AutoFollowPattern("asia_cluster", existingPatterns, null, null, null, null, null, null, null, null)); List existingUUIDS = new ArrayList<>(); existingUUIDS.add("_val"); - existingAlreadyFollowedIndexUUIDS.put("asia_cluster", existingUUIDS); - existingHeaders.put("asia_cluster", Collections.singletonMap("key", "val")); + existingAlreadyFollowedIndexUUIDS.put("name2", existingUUIDS); + existingHeaders.put("name2", Collections.singletonMap("key", "val")); } ClusterState clusterState = ClusterState.builder(new ClusterName("us_cluster")) .metaData(MetaData.builder().putCustom(AutoFollowMetadata.TYPE, @@ -56,27 +57,28 @@ public void testInnerDelete() { .build(); Request request = new Request(); - request.setLeaderClusterAlias("eu_cluster"); + request.setName("name1"); AutoFollowMetadata result = TransportDeleteAutoFollowPatternAction.innerDelete(request, clusterState) .getMetaData() .custom(AutoFollowMetadata.TYPE); assertThat(result.getPatterns().size(), equalTo(1)); - assertThat(result.getPatterns().get("asia_cluster"), notNullValue()); + assertThat(result.getPatterns().get("name2"), notNullValue()); + assertThat(result.getPatterns().get("name2").getLeaderCluster(), equalTo("asia_cluster")); assertThat(result.getFollowedLeaderIndexUUIDs().size(), equalTo(1)); - assertThat(result.getFollowedLeaderIndexUUIDs().get("asia_cluster"), notNullValue()); + assertThat(result.getFollowedLeaderIndexUUIDs().get("name2"), notNullValue()); assertThat(result.getHeaders().size(), equalTo(1)); - assertThat(result.getHeaders().get("asia_cluster"), notNullValue()); + assertThat(result.getHeaders().get("name2"), notNullValue()); } public void testInnerDeleteDoesNotExist() { Map> existingAlreadyFollowedIndexUUIDS = new HashMap<>(); - Map existingAutoFollowPatterns = new HashMap<>(); + Map existingAutoFollowPatterns = new HashMap<>(); Map> existingHeaders = new HashMap<>(); { List existingPatterns = new ArrayList<>(); existingPatterns.add("transactions-*"); - existingAutoFollowPatterns.put("eu_cluster", - new AutoFollowMetadata.AutoFollowPattern(existingPatterns, null, null, null, null, null, null, null, null)); + existingAutoFollowPatterns.put("name1", + new AutoFollowPattern("eu_cluster", existingPatterns, null, null, null, null, null, null, null, null)); existingHeaders.put("key", Collections.singletonMap("key", "val")); } ClusterState clusterState = ClusterState.builder(new ClusterName("us_cluster")) @@ -85,10 +87,10 @@ public void testInnerDeleteDoesNotExist() { .build(); Request request = new Request(); - request.setLeaderClusterAlias("asia_cluster"); + request.setName("name2"); Exception e = expectThrows(ResourceNotFoundException.class, () -> TransportDeleteAutoFollowPatternAction.innerDelete(request, clusterState)); - assertThat(e.getMessage(), equalTo("no auto-follow patterns for cluster alias [asia_cluster] found")); + assertThat(e.getMessage(), equalTo("auto-follow pattern [name2] is missing")); } public void testInnerDeleteNoAutoFollowMetadata() { @@ -97,10 +99,10 @@ public void testInnerDeleteNoAutoFollowMetadata() { .build(); Request request = new Request(); - request.setLeaderClusterAlias("asia_cluster"); + request.setName("name1"); Exception e = expectThrows(ResourceNotFoundException.class, () -> TransportDeleteAutoFollowPatternAction.innerDelete(request, clusterState)); - assertThat(e.getMessage(), equalTo("no auto-follow patterns for cluster alias [asia_cluster] found")); + assertThat(e.getMessage(), equalTo("auto-follow pattern [name1] is missing")); } } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternActionTests.java index 187e404abbb66..ffc2d115091af 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternActionTests.java @@ -23,22 +23,22 @@ public class TransportGetAutoFollowPatternActionTests extends ESTestCase { public void testGetAutoFollowPattern() { Map patterns = new HashMap<>(); - patterns.put("test_alias1", - new AutoFollowPattern(Collections.singletonList("index-*"), null, null, null, null, null, null, null, null)); - patterns.put("test_alias2", - new AutoFollowPattern(Collections.singletonList("index-*"), null, null, null, null, null, null, null, null)); + patterns.put("name1", + new AutoFollowPattern("test_alias1", Collections.singletonList("index-*"), null, null, null, null, null, null, null, null)); + patterns.put("name2", + new AutoFollowPattern("test_alias1", Collections.singletonList("index-*"), null, null, null, null, null, null, null, null)); MetaData metaData = MetaData.builder() .putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap())) .build(); - Map result = TransportGetAutoFollowPatternAction.getAutoFollowPattern(metaData, "test_alias1"); + Map result = TransportGetAutoFollowPatternAction.getAutoFollowPattern(metaData, "name1"); assertThat(result.size(), equalTo(1)); - assertThat(result, hasEntry("test_alias1", patterns.get("test_alias1"))); + assertThat(result, hasEntry("name1", patterns.get("name1"))); result = TransportGetAutoFollowPatternAction.getAutoFollowPattern(metaData, null); assertThat(result.size(), equalTo(2)); - assertThat(result, hasEntry("test_alias1", patterns.get("test_alias1"))); - assertThat(result, hasEntry("test_alias2", patterns.get("test_alias2"))); + assertThat(result, hasEntry("name1", patterns.get("name1"))); + assertThat(result, hasEntry("name2", patterns.get("name2"))); expectThrows(ResourceNotFoundException.class, () -> TransportGetAutoFollowPatternAction.getAutoFollowPattern(metaData, "another_alias")); @@ -51,13 +51,13 @@ public void testGetAutoFollowPatternNoAutoFollowPatterns() { .putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata) .build(); expectThrows(ResourceNotFoundException.class, - () -> TransportGetAutoFollowPatternAction.getAutoFollowPattern(metaData, "test_alias")); + () -> TransportGetAutoFollowPatternAction.getAutoFollowPattern(metaData, "name1")); } public void testGetAutoFollowPatternNoAutoFollowMetadata() { MetaData metaData = MetaData.builder().build(); expectThrows(ResourceNotFoundException.class, - () -> TransportGetAutoFollowPatternAction.getAutoFollowPattern(metaData, "test_alias")); + () -> TransportGetAutoFollowPatternAction.getAutoFollowPattern(metaData, "name1")); } } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternActionTests.java index c208a4b042d04..7c4368d317f24 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternActionTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata; +import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata.AutoFollowPattern; import org.elasticsearch.xpack.core.ccr.action.PutAutoFollowPatternAction; import java.util.ArrayList; @@ -28,7 +29,8 @@ public class TransportPutAutoFollowPatternActionTests extends ESTestCase { public void testInnerPut() { PutAutoFollowPatternAction.Request request = new PutAutoFollowPatternAction.Request(); - request.setLeaderClusterAlias("eu_cluster"); + request.setName("name1"); + request.setLeaderCluster("eu_cluster"); request.setLeaderIndexPatterns(Collections.singletonList("logs-*")); ClusterState localState = ClusterState.builder(new ClusterName("us_cluster")) @@ -43,15 +45,17 @@ public void testInnerPut() { AutoFollowMetadata autoFollowMetadata = result.metaData().custom(AutoFollowMetadata.TYPE); assertThat(autoFollowMetadata, notNullValue()); assertThat(autoFollowMetadata.getPatterns().size(), equalTo(1)); - assertThat(autoFollowMetadata.getPatterns().get("eu_cluster").getLeaderIndexPatterns().size(), equalTo(1)); - assertThat(autoFollowMetadata.getPatterns().get("eu_cluster").getLeaderIndexPatterns().get(0), equalTo("logs-*")); + assertThat(autoFollowMetadata.getPatterns().get("name1").getLeaderCluster(), equalTo("eu_cluster")); + assertThat(autoFollowMetadata.getPatterns().get("name1").getLeaderIndexPatterns().size(), equalTo(1)); + assertThat(autoFollowMetadata.getPatterns().get("name1").getLeaderIndexPatterns().get(0), equalTo("logs-*")); assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().size(), equalTo(1)); - assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("eu_cluster").size(), equalTo(0)); + assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("name1").size(), equalTo(0)); } public void testInnerPut_existingLeaderIndices() { PutAutoFollowPatternAction.Request request = new PutAutoFollowPatternAction.Request(); - request.setLeaderClusterAlias("eu_cluster"); + request.setName("name1"); + request.setLeaderCluster("eu_cluster"); request.setLeaderIndexPatterns(Collections.singletonList("logs-*")); ClusterState localState = ClusterState.builder(new ClusterName("us_cluster")) @@ -82,28 +86,30 @@ public void testInnerPut_existingLeaderIndices() { AutoFollowMetadata autoFollowMetadata = result.metaData().custom(AutoFollowMetadata.TYPE); assertThat(autoFollowMetadata, notNullValue()); assertThat(autoFollowMetadata.getPatterns().size(), equalTo(1)); - assertThat(autoFollowMetadata.getPatterns().get("eu_cluster").getLeaderIndexPatterns().size(), equalTo(1)); - assertThat(autoFollowMetadata.getPatterns().get("eu_cluster").getLeaderIndexPatterns().get(0), equalTo("logs-*")); + assertThat(autoFollowMetadata.getPatterns().get("name1").getLeaderCluster(), equalTo("eu_cluster")); + assertThat(autoFollowMetadata.getPatterns().get("name1").getLeaderIndexPatterns().size(), equalTo(1)); + assertThat(autoFollowMetadata.getPatterns().get("name1").getLeaderIndexPatterns().get(0), equalTo("logs-*")); assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().size(), equalTo(1)); - assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("eu_cluster").size(), equalTo(numMatchingLeaderIndices)); + assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("name1").size(), equalTo(numMatchingLeaderIndices)); } public void testInnerPut_existingLeaderIndicesAndAutoFollowMetadata() { PutAutoFollowPatternAction.Request request = new PutAutoFollowPatternAction.Request(); - request.setLeaderClusterAlias("eu_cluster"); + request.setName("name1"); + request.setLeaderCluster("eu_cluster"); request.setLeaderIndexPatterns(Arrays.asList("logs-*", "transactions-*")); - Map existingAutoFollowPatterns = new HashMap<>(); + Map existingAutoFollowPatterns = new HashMap<>(); List existingPatterns = new ArrayList<>(); existingPatterns.add("transactions-*"); - existingAutoFollowPatterns.put("eu_cluster", - new AutoFollowMetadata.AutoFollowPattern(existingPatterns, null, null, null, null, null, null, null, null)); + existingAutoFollowPatterns.put("name1", + new AutoFollowPattern("eu_cluster", existingPatterns, null, null, null, null, null, null, null, null)); Map> existingAlreadyFollowedIndexUUIDS = new HashMap<>(); List existingUUIDS = new ArrayList<>(); existingUUIDS.add("_val"); - existingAlreadyFollowedIndexUUIDS.put("eu_cluster", existingUUIDS); + existingAlreadyFollowedIndexUUIDS.put("name1", existingUUIDS); Map> existingHeaders = new HashMap<>(); - existingHeaders.put("eu_cluster", Collections.singletonMap("key", "val")); + existingHeaders.put("name1", Collections.singletonMap("key", "val")); ClusterState localState = ClusterState.builder(new ClusterName("us_cluster")) .metaData(MetaData.builder().putCustom(AutoFollowMetadata.TYPE, @@ -127,13 +133,14 @@ public void testInnerPut_existingLeaderIndicesAndAutoFollowMetadata() { AutoFollowMetadata autoFollowMetadata = result.metaData().custom(AutoFollowMetadata.TYPE); assertThat(autoFollowMetadata, notNullValue()); assertThat(autoFollowMetadata.getPatterns().size(), equalTo(1)); - assertThat(autoFollowMetadata.getPatterns().get("eu_cluster").getLeaderIndexPatterns().size(), equalTo(2)); - assertThat(autoFollowMetadata.getPatterns().get("eu_cluster").getLeaderIndexPatterns().get(0), equalTo("logs-*")); - assertThat(autoFollowMetadata.getPatterns().get("eu_cluster").getLeaderIndexPatterns().get(1), equalTo("transactions-*")); + assertThat(autoFollowMetadata.getPatterns().get("name1").getLeaderCluster(), equalTo("eu_cluster")); + assertThat(autoFollowMetadata.getPatterns().get("name1").getLeaderIndexPatterns().size(), equalTo(2)); + assertThat(autoFollowMetadata.getPatterns().get("name1").getLeaderIndexPatterns().get(0), equalTo("logs-*")); + assertThat(autoFollowMetadata.getPatterns().get("name1").getLeaderIndexPatterns().get(1), equalTo("transactions-*")); assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().size(), equalTo(1)); - assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("eu_cluster").size(), equalTo(numLeaderIndices + 1)); + assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("name1").size(), equalTo(numLeaderIndices + 1)); assertThat(autoFollowMetadata.getHeaders().size(), equalTo(1)); - assertThat(autoFollowMetadata.getHeaders().get("eu_cluster"), notNullValue()); + assertThat(autoFollowMetadata.getHeaders().get("name1"), notNullValue()); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowMetadata.java index e063a85d0b173..8f01c56c3f064 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowMetadata.java @@ -175,6 +175,7 @@ public int hashCode() { public static class AutoFollowPattern implements Writeable, ToXContentObject { + public static final ParseField LEADER_CLUSTER_FIELD = new ParseField("leader_cluster"); public static final ParseField LEADER_PATTERNS_FIELD = new ParseField("leader_index_patterns"); public static final ParseField FOLLOW_PATTERN_FIELD = new ParseField("follow_index_pattern"); public static final ParseField MAX_BATCH_OPERATION_COUNT = new ParseField("max_batch_operation_count"); @@ -188,10 +189,12 @@ public static class AutoFollowPattern implements Writeable, ToXContentObject { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("auto_follow_pattern", - args -> new AutoFollowPattern((List) args[0], (String) args[1], (Integer) args[2], (Integer) args[3], - (ByteSizeValue) args[4], (Integer) args[5], (Integer) args[6], (TimeValue) args[7], (TimeValue) args[8])); + args -> new AutoFollowPattern((String) args[0], (List) args[1], (String) args[2], (Integer) args[3], + (Integer) args[4], (ByteSizeValue) args[5], (Integer) args[6], (Integer) args[7], (TimeValue) args[8], + (TimeValue) args[9])); static { + PARSER.declareString(ConstructingObjectParser.constructorArg(), LEADER_CLUSTER_FIELD); PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), LEADER_PATTERNS_FIELD); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), FOLLOW_PATTERN_FIELD); PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), MAX_BATCH_OPERATION_COUNT); @@ -211,6 +214,7 @@ public static class AutoFollowPattern implements Writeable, ToXContentObject { POLL_TIMEOUT, ObjectParser.ValueType.STRING); } + private final String leaderCluster; private final List leaderIndexPatterns; private final String followIndexPattern; private final Integer maxBatchOperationCount; @@ -221,7 +225,8 @@ public static class AutoFollowPattern implements Writeable, ToXContentObject { private final TimeValue maxRetryDelay; private final TimeValue pollTimeout; - public AutoFollowPattern(List leaderIndexPatterns, + public AutoFollowPattern(String leaderCluster, + List leaderIndexPatterns, String followIndexPattern, Integer maxBatchOperationCount, Integer maxConcurrentReadBatches, @@ -230,6 +235,7 @@ public AutoFollowPattern(List leaderIndexPatterns, Integer maxWriteBufferSize, TimeValue maxRetryDelay, TimeValue pollTimeout) { + this.leaderCluster = leaderCluster; this.leaderIndexPatterns = leaderIndexPatterns; this.followIndexPattern = followIndexPattern; this.maxBatchOperationCount = maxBatchOperationCount; @@ -242,6 +248,7 @@ public AutoFollowPattern(List leaderIndexPatterns, } public AutoFollowPattern(StreamInput in) throws IOException { + leaderCluster = in.readString(); leaderIndexPatterns = in.readList(StreamInput::readString); followIndexPattern = in.readOptionalString(); maxBatchOperationCount = in.readOptionalVInt(); @@ -261,6 +268,10 @@ public static boolean match(List leaderIndexPatterns, String indexName) return Regex.simpleMatch(leaderIndexPatterns, indexName); } + public String getLeaderCluster() { + return leaderCluster; + } + public List getLeaderIndexPatterns() { return leaderIndexPatterns; } @@ -299,6 +310,7 @@ public TimeValue getPollTimeout() { @Override public void writeTo(StreamOutput out) throws IOException { + out.writeString(leaderCluster); out.writeStringList(leaderIndexPatterns); out.writeOptionalString(followIndexPattern); out.writeOptionalVInt(maxBatchOperationCount); @@ -312,6 +324,7 @@ public void writeTo(StreamOutput out) throws IOException { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.field(LEADER_CLUSTER_FIELD.getPreferredName(), leaderCluster); builder.array(LEADER_PATTERNS_FIELD.getPreferredName(), leaderIndexPatterns.toArray(new String[0])); if (followIndexPattern != null) { builder.field(FOLLOW_PATTERN_FIELD.getPreferredName(), followIndexPattern); @@ -350,7 +363,8 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AutoFollowPattern that = (AutoFollowPattern) o; - return Objects.equals(leaderIndexPatterns, that.leaderIndexPatterns) && + return Objects.equals(leaderCluster, that.leaderCluster) && + Objects.equals(leaderIndexPatterns, that.leaderIndexPatterns) && Objects.equals(followIndexPattern, that.followIndexPattern) && Objects.equals(maxBatchOperationCount, that.maxBatchOperationCount) && Objects.equals(maxConcurrentReadBatches, that.maxConcurrentReadBatches) && @@ -364,6 +378,7 @@ public boolean equals(Object o) { @Override public int hashCode() { return Objects.hash( + leaderCluster, leaderIndexPatterns, followIndexPattern, maxBatchOperationCount, diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/DeleteAutoFollowPatternAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/DeleteAutoFollowPatternAction.java index 6d49a370a344c..7b389ec6cf1d9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/DeleteAutoFollowPatternAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/DeleteAutoFollowPatternAction.java @@ -33,35 +33,35 @@ public AcknowledgedResponse newResponse() { public static class Request extends AcknowledgedRequest { - private String leaderClusterAlias; + private String name; @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; - if (leaderClusterAlias == null) { - validationException = addValidationError("leaderClusterAlias is missing", validationException); + if (name == null) { + validationException = addValidationError("name is missing", validationException); } return validationException; } - public String getLeaderClusterAlias() { - return leaderClusterAlias; + public String getName() { + return name; } - public void setLeaderClusterAlias(String leaderClusterAlias) { - this.leaderClusterAlias = leaderClusterAlias; + public void setName(String name) { + this.name = name; } @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); - leaderClusterAlias = in.readString(); + name = in.readString(); } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - out.writeString(leaderClusterAlias); + out.writeString(name); } @Override @@ -69,12 +69,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return Objects.equals(leaderClusterAlias, request.leaderClusterAlias); + return Objects.equals(name, request.name); } @Override public int hashCode() { - return Objects.hash(leaderClusterAlias); + return Objects.hash(name); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/GetAutoFollowPatternAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/GetAutoFollowPatternAction.java index ff7f50e66c88e..a226118e17b1e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/GetAutoFollowPatternAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/GetAutoFollowPatternAction.java @@ -36,14 +36,14 @@ public Response newResponse() { public static class Request extends MasterNodeReadRequest { - private String leaderClusterAlias; + private String name; public Request() { } public Request(StreamInput in) throws IOException { super(in); - this.leaderClusterAlias = in.readOptionalString(); + this.name = in.readOptionalString(); } @Override @@ -51,18 +51,18 @@ public ActionRequestValidationException validate() { return null; } - public String getLeaderClusterAlias() { - return leaderClusterAlias; + public String getName() { + return name; } - public void setLeaderClusterAlias(String leaderClusterAlias) { - this.leaderClusterAlias = leaderClusterAlias; + public void setName(String name) { + this.name = name; } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - out.writeOptionalString(leaderClusterAlias); + out.writeOptionalString(name); } @Override @@ -70,12 +70,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return Objects.equals(leaderClusterAlias, request.leaderClusterAlias); + return Objects.equals(name, request.name); } @Override public int hashCode() { - return Objects.hash(leaderClusterAlias); + return Objects.hash(name); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/PutAutoFollowPatternAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/PutAutoFollowPatternAction.java index d35365b0d32a2..c76285843a475 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/PutAutoFollowPatternAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/PutAutoFollowPatternAction.java @@ -42,12 +42,13 @@ public AcknowledgedResponse newResponse() { public static class Request extends AcknowledgedRequest implements ToXContentObject { - static final ParseField LEADER_CLUSTER_ALIAS_FIELD = new ParseField("leader_cluster_alias"); - private static final ObjectParser PARSER = new ObjectParser<>("put_auto_follow_pattern_request", Request::new); + private static final ParseField NAME_FIELD = new ParseField("name"); + static { - PARSER.declareString(Request::setLeaderClusterAlias, LEADER_CLUSTER_ALIAS_FIELD); + PARSER.declareString(Request::setName, NAME_FIELD); + PARSER.declareString(Request::setLeaderCluster, AutoFollowPattern.LEADER_CLUSTER_FIELD); PARSER.declareStringArray(Request::setLeaderIndexPatterns, AutoFollowPattern.LEADER_PATTERNS_FIELD); PARSER.declareString(Request::setFollowIndexNamePattern, AutoFollowPattern.FOLLOW_PATTERN_FIELD); PARSER.declareInt(Request::setMaxBatchOperationCount, AutoFollowPattern.MAX_BATCH_OPERATION_COUNT); @@ -67,21 +68,22 @@ public static class Request extends AcknowledgedRequest implements ToXC AutoFollowPattern.POLL_TIMEOUT, ObjectParser.ValueType.STRING); } - public static Request fromXContent(XContentParser parser, String remoteClusterAlias) throws IOException { + public static Request fromXContent(XContentParser parser, String name) throws IOException { Request request = PARSER.parse(parser, null); - if (remoteClusterAlias != null) { - if (request.leaderClusterAlias == null) { - request.leaderClusterAlias = remoteClusterAlias; + if (name != null) { + if (request.name == null) { + request.name = name; } else { - if (request.leaderClusterAlias.equals(remoteClusterAlias) == false) { - throw new IllegalArgumentException("provided leaderClusterAlias is not equal"); + if (request.name.equals(name) == false) { + throw new IllegalArgumentException("provided name is not equal"); } } } return request; } - private String leaderClusterAlias; + private String name; + private String leaderCluster; private List leaderIndexPatterns; private String followIndexNamePattern; @@ -96,8 +98,11 @@ public static Request fromXContent(XContentParser parser, String remoteClusterAl @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; - if (leaderClusterAlias == null) { - validationException = addValidationError("[" + LEADER_CLUSTER_ALIAS_FIELD.getPreferredName() + + if (name == null) { + validationException = addValidationError("[" + NAME_FIELD.getPreferredName() + "] is missing", validationException); + } + if (leaderCluster == null) { + validationException = addValidationError("[" + AutoFollowPattern.LEADER_CLUSTER_FIELD.getPreferredName() + "] is missing", validationException); } if (leaderIndexPatterns == null || leaderIndexPatterns.isEmpty()) { @@ -120,12 +125,20 @@ public ActionRequestValidationException validate() { return validationException; } - public String getLeaderClusterAlias() { - return leaderClusterAlias; + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLeaderCluster() { + return leaderCluster; } - public void setLeaderClusterAlias(String leaderClusterAlias) { - this.leaderClusterAlias = leaderClusterAlias; + public void setLeaderCluster(String leaderCluster) { + this.leaderCluster = leaderCluster; } public List getLeaderIndexPatterns() { @@ -203,7 +216,8 @@ public void setPollTimeout(TimeValue pollTimeout) { @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); - leaderClusterAlias = in.readString(); + name = in.readString(); + leaderCluster = in.readString(); leaderIndexPatterns = in.readList(StreamInput::readString); followIndexNamePattern = in.readOptionalString(); maxBatchOperationCount = in.readOptionalVInt(); @@ -218,7 +232,8 @@ public void readFrom(StreamInput in) throws IOException { @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - out.writeString(leaderClusterAlias); + out.writeString(name); + out.writeString(leaderCluster); out.writeStringList(leaderIndexPatterns); out.writeOptionalString(followIndexNamePattern); out.writeOptionalVInt(maxBatchOperationCount); @@ -234,7 +249,8 @@ public void writeTo(StreamOutput out) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); { - builder.field(LEADER_CLUSTER_ALIAS_FIELD.getPreferredName(), leaderClusterAlias); + builder.field(NAME_FIELD.getPreferredName(), name); + builder.field(AutoFollowPattern.LEADER_CLUSTER_FIELD.getPreferredName(), leaderCluster); builder.field(AutoFollowPattern.LEADER_PATTERNS_FIELD.getPreferredName(), leaderIndexPatterns); if (followIndexNamePattern != null) { builder.field(AutoFollowPattern.FOLLOW_PATTERN_FIELD.getPreferredName(), followIndexNamePattern); @@ -270,7 +286,8 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return Objects.equals(leaderClusterAlias, request.leaderClusterAlias) && + return Objects.equals(name, request.name) && + Objects.equals(leaderCluster, request.leaderCluster) && Objects.equals(leaderIndexPatterns, request.leaderIndexPatterns) && Objects.equals(followIndexNamePattern, request.followIndexNamePattern) && Objects.equals(maxBatchOperationCount, request.maxBatchOperationCount) && @@ -285,7 +302,8 @@ public boolean equals(Object o) { @Override public int hashCode() { return Objects.hash( - leaderClusterAlias, + name, + leaderCluster, leaderIndexPatterns, followIndexNamePattern, maxBatchOperationCount, diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.delete_auto_follow_pattern.json b/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.delete_auto_follow_pattern.json index c958c842b54bb..a5c8de7483613 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.delete_auto_follow_pattern.json +++ b/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.delete_auto_follow_pattern.json @@ -3,13 +3,13 @@ "documentation": "http://www.elastic.co/guide/en/elasticsearch/reference/current", "methods": [ "DELETE" ], "url": { - "path": "/_ccr/auto_follow/{leader_cluster_alias}", - "paths": [ "/_ccr/auto_follow/{leader_cluster_alias}" ], + "path": "/_ccr/auto_follow/{name}", + "paths": [ "/_ccr/auto_follow/{name}" ], "parts": { - "leader_cluster_alias": { + "name": { "type": "string", "required": true, - "description": "The name of the leader cluster alias." + "description": "The name of the auto follow pattern." } } } diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.get_auto_follow_pattern.json b/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.get_auto_follow_pattern.json index c8826dc5f1baa..65d0b9c8dea1e 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.get_auto_follow_pattern.json +++ b/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.get_auto_follow_pattern.json @@ -3,12 +3,12 @@ "documentation": "http://www.elastic.co/guide/en/elasticsearch/reference/current", "methods": [ "GET" ], "url": { - "path": "/_ccr/auto_follow/{leader_cluster_alias}", - "paths": [ "/_ccr/auto_follow", "/_ccr/auto_follow/{leader_cluster_alias}" ], + "path": "/_ccr/auto_follow/{name}", + "paths": [ "/_ccr/auto_follow", "/_ccr/auto_follow/{name}" ], "parts": { - "leader_cluster_alias": { + "name": { "type": "string", - "description": "The name of the leader cluster alias." + "description": "The name of the auto follow pattern." } } } diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.put_auto_follow_pattern.json b/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.put_auto_follow_pattern.json index ca9c255097f01..5f7aac790a00c 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.put_auto_follow_pattern.json +++ b/x-pack/plugin/src/test/resources/rest-api-spec/api/ccr.put_auto_follow_pattern.json @@ -3,13 +3,13 @@ "documentation": "http://www.elastic.co/guide/en/elasticsearch/reference/current", "methods": [ "PUT" ], "url": { - "path": "/_ccr/auto_follow/{leader_cluster_alias}", - "paths": [ "/_ccr/auto_follow/{leader_cluster_alias}" ], + "path": "/_ccr/auto_follow/{name}", + "paths": [ "/_ccr/auto_follow/{name}" ], "parts": { - "leader_cluster_alias": { + "name": { "type": "string", "required": true, - "description": "The name of the leader cluster alias." + "description": "The name of the auto follow pattern." } } }, From a723f8f9086804bd5d4c06a88cf1204183f2e44d Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Tue, 16 Oct 2018 14:30:34 +0200 Subject: [PATCH 2/3] fail when an unfollowed leader index matches with two or more auto follow patterns --- .../xpack/ccr/CcrMultiClusterLicenseIT.java | 2 +- .../ccr/action/AutoFollowCoordinator.java | 71 ++++++++++++------- .../xpack/ccr/action/AutoFollowTests.java | 61 ++++++++++++++-- 3 files changed, 104 insertions(+), 30 deletions(-) diff --git a/x-pack/plugin/ccr/qa/multi-cluster-with-non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java b/x-pack/plugin/ccr/qa/multi-cluster-with-non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java index 68ebd6e9779cb..e974ac628f57f 100644 --- a/x-pack/plugin/ccr/qa/multi-cluster-with-non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java +++ b/x-pack/plugin/ccr/qa/multi-cluster-with-non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java @@ -64,7 +64,7 @@ public void testAutoFollow() throws Exception { while (it.hasNext()) { final String line = it.next(); if (line.matches(".*\\[WARN\\s*\\]\\[o\\.e\\.x\\.c\\.a\\.AutoFollowCoordinator\\s*\\] \\[node-0\\] " + - "failure occurred while fetching cluster state in leader cluster \\[leader_cluster\\]")) { + "failure occurred while fetching cluster state for auto follow pattern \\[test_pattern\\]")) { warn = true; break; } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java index e841eb2f7af28..59d7923875a07 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java @@ -48,6 +48,7 @@ import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; +import java.util.stream.Collectors; /** * A component that runs only on the elected master node and follows leader indices automatically @@ -106,19 +107,19 @@ public synchronized AutoFollowStats getStats() { synchronized void updateStats(List results) { for (AutoFollowResult result : results) { if (result.clusterStateFetchException != null) { - recentAutoFollowErrors.put(result.clusterAlias, + recentAutoFollowErrors.put(result.autoFollowPatternName, new ElasticsearchException(result.clusterStateFetchException)); numberOfFailedRemoteClusterStateRequests++; - LOGGER.warn(new ParameterizedMessage("failure occurred while fetching cluster state in leader cluster [{}]", - result.clusterAlias), result.clusterStateFetchException); + LOGGER.warn(new ParameterizedMessage("failure occurred while fetching cluster state for auto follow pattern [{}]", + result.autoFollowPatternName), result.clusterStateFetchException); } else { for (Map.Entry entry : result.autoFollowExecutionResults.entrySet()) { if (entry.getValue() != null) { numberOfFailedIndicesAutoFollowed++; - recentAutoFollowErrors.put(result.clusterAlias + ":" + entry.getKey().getName(), + recentAutoFollowErrors.put(result.autoFollowPatternName + ":" + entry.getKey().getName(), ExceptionsHelper.convertToElastic(entry.getValue())); - LOGGER.warn(new ParameterizedMessage("failure occurred while auto following index [{}] in leader cluster [{}]", - entry.getKey(), result.clusterAlias), entry.getValue()); + LOGGER.warn(new ParameterizedMessage("failure occurred while auto following index [{}] for auto follow " + + "pattern [{}]", entry.getKey(), result.autoFollowPatternName), entry.getValue()); } else { numberOfSuccessfulIndicesAutoFollowed++; } @@ -254,24 +255,31 @@ void autoFollowIndices() { final int slot = i; final String autoFollowPattenName = entry.getKey(); final AutoFollowPattern autoFollowPattern = entry.getValue(); - final String clusterAlias = autoFollowPattern.getLeaderCluster(); + final String leaderCluster = autoFollowPattern.getLeaderCluster(); Map headers = autoFollowMetadata.getHeaders().get(autoFollowPattenName); - getLeaderClusterState(headers, clusterAlias, (leaderClusterState, e) -> { + getLeaderClusterState(headers, leaderCluster, (leaderClusterState, e) -> { if (leaderClusterState != null) { assert e == null; final List followedIndices = autoFollowMetadata.getFollowedLeaderIndexUUIDs().get(autoFollowPattenName); - final List leaderIndicesToFollow = getLeaderIndicesToFollow(clusterAlias, autoFollowPattern, + final List leaderIndicesToFollow = getLeaderIndicesToFollow(leaderCluster, autoFollowPattern, leaderClusterState, followerClusterState, followedIndices); if (leaderIndicesToFollow.isEmpty()) { - finalise(slot, new AutoFollowResult(clusterAlias)); + finalise(slot, new AutoFollowResult(autoFollowPattenName)); } else { + List> patternsForTheSameLeaderCluster = autoFollowMetadata.getPatterns() + .entrySet().stream() + .filter(item -> autoFollowPattenName.equals(item.getKey()) == false) + .filter(item -> leaderCluster.equals(item.getValue().getLeaderCluster())) + .map(item -> new Tuple<>(item.getKey(), item.getValue())) + .collect(Collectors.toList()); + Consumer resultHandler = result -> finalise(slot, result); - checkAutoFollowPattern(autoFollowPattenName, clusterAlias, autoFollowPattern, leaderIndicesToFollow, headers, - resultHandler); + checkAutoFollowPattern(autoFollowPattenName, leaderCluster, autoFollowPattern, leaderIndicesToFollow, headers, + patternsForTheSameLeaderCluster, resultHandler); } } else { - finalise(slot, new AutoFollowResult(clusterAlias, e)); + finalise(slot, new AutoFollowResult(autoFollowPattenName, e)); } }); i++; @@ -283,6 +291,7 @@ private void checkAutoFollowPattern(String autoFollowPattenName, AutoFollowPattern autoFollowPattern, List leaderIndicesToFollow, Map headers, + List> patternsForTheSameLeaderCluster, Consumer resultHandler) { final CountDown leaderIndicesCountDown = new CountDown(leaderIndicesToFollow.size()); @@ -290,12 +299,26 @@ private void checkAutoFollowPattern(String autoFollowPattenName, for (int i = 0; i < leaderIndicesToFollow.size(); i++) { final Index indexToFollow = leaderIndicesToFollow.get(i); final int slot = i; - followLeaderIndex(autoFollowPattenName, clusterAlias, indexToFollow, autoFollowPattern, headers, error -> { - results.set(slot, new Tuple<>(indexToFollow, error)); + + List otherMatchingPatterns = patternsForTheSameLeaderCluster.stream() + .filter(otherPattern -> otherPattern.v2().match(indexToFollow.getName())) + .map(Tuple::v1) + .collect(Collectors.toList()); + if (otherMatchingPatterns.size() != 0) { + results.set(slot, new Tuple<>(indexToFollow, new ElasticsearchException("index to follow [" + indexToFollow.getName() + + "] for pattern [" + autoFollowPattenName + "] matches with other patterns " + otherMatchingPatterns + ""))); if (leaderIndicesCountDown.countDown()) { - resultHandler.accept(new AutoFollowResult(clusterAlias, results.asList())); + resultHandler.accept(new AutoFollowResult(autoFollowPattenName, results.asList())); } - }); + } else { + followLeaderIndex(autoFollowPattenName, clusterAlias, indexToFollow, autoFollowPattern, headers, error -> { + results.set(slot, new Tuple<>(indexToFollow, error)); + if (leaderIndicesCountDown.countDown()) { + resultHandler.accept(new AutoFollowResult(autoFollowPattenName, results.asList())); + } + }); + } + } } @@ -427,12 +450,12 @@ abstract void updateAutoFollowMetadata( static class AutoFollowResult { - final String clusterAlias; + final String autoFollowPatternName; final Exception clusterStateFetchException; final Map autoFollowExecutionResults; - AutoFollowResult(String clusterAlias, List> results) { - this.clusterAlias = clusterAlias; + AutoFollowResult(String autoFollowPatternName, List> results) { + this.autoFollowPatternName = autoFollowPatternName; Map autoFollowExecutionResults = new HashMap<>(); for (Tuple result : results) { @@ -443,14 +466,14 @@ static class AutoFollowResult { this.autoFollowExecutionResults = Collections.unmodifiableMap(autoFollowExecutionResults); } - AutoFollowResult(String clusterAlias, Exception e) { - this.clusterAlias = clusterAlias; + AutoFollowResult(String autoFollowPatternName, Exception e) { + this.autoFollowPatternName = autoFollowPatternName; this.clusterStateFetchException = e; this.autoFollowExecutionResults = Collections.emptyMap(); } - AutoFollowResult(String clusterAlias) { - this(clusterAlias, (Exception) null); + AutoFollowResult(String autoFollowPatternName) { + this(autoFollowPatternName, (Exception) null); } } } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowTests.java index 76a1fad521c0f..7e7de53463973 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowTests.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.ccr.action; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaData; @@ -27,6 +28,7 @@ import java.util.Collections; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.notNullValue; public class AutoFollowTests extends ESSingleNodeTestCase { @@ -51,7 +53,12 @@ public void testAutoFollow() throws Exception { createIndex("logs-201812", leaderIndexSettings, "_doc"); // Enabling auto following: - putAutoFollowPatterns("logs-*", "transactions-*"); + if (randomBoolean()) { + putAutoFollowPatterns("my-pattern", new String[] {"logs-*", "transactions-*"}); + } else { + putAutoFollowPatterns("my-pattern1", new String[] {"logs-*"}); + putAutoFollowPatterns("my-pattern2", new String[] {"transactions-*"}); + } createIndex("metrics-201901", leaderIndexSettings, "_doc"); @@ -82,7 +89,7 @@ public void testAutoFollowManyIndices() throws Exception { .put(IndexMetaData.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) .build(); - putAutoFollowPatterns("logs-*"); + putAutoFollowPatterns("my-pattern", new String[] {"logs-*"}); int numIndices = randomIntBetween(4, 32); for (int i = 0; i < numIndices; i++) { createIndex("logs-" + i, leaderIndexSettings, "_doc"); @@ -96,7 +103,7 @@ public void testAutoFollowManyIndices() throws Exception { deleteAutoFollowPatternSetting(); createIndex("logs-does-not-count", leaderIndexSettings, "_doc"); - putAutoFollowPatterns("logs-*"); + putAutoFollowPatterns("my-pattern", new String[] {"logs-*"}); int i = numIndices; numIndices = numIndices + randomIntBetween(4, 32); for (; i < numIndices; i++) { @@ -180,9 +187,53 @@ public void testAutoFollowParameterAreDelegated() throws Exception { }); } - private void putAutoFollowPatterns(String... patterns) { + public void testConflictingPatterns() throws Exception { + Settings leaderIndexSettings = Settings.builder() + .put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true) + .put(IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) + .put(IndexMetaData.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) + .build(); + + // Enabling auto following: + putAutoFollowPatterns("my-pattern1", new String[] {"logs-*"}); + putAutoFollowPatterns("my-pattern2", new String[] {"logs-2018*"}); + + createIndex("logs-201701", leaderIndexSettings, "_doc"); + assertBusy(() -> { + AutoFollowStats autoFollowStats = getAutoFollowStats(); + assertThat(autoFollowStats.getNumberOfSuccessfulFollowIndices(), equalTo(1L)); + assertThat(autoFollowStats.getNumberOfFailedFollowIndices(), equalTo(0L)); + assertThat(autoFollowStats.getNumberOfFailedRemoteClusterStateRequests(), equalTo(0L)); + }); + IndicesExistsRequest request = new IndicesExistsRequest("copy-logs-201701"); + assertTrue(client().admin().indices().exists(request).actionGet().isExists()); + + createIndex("logs-201801", leaderIndexSettings, "_doc"); + assertBusy(() -> { + AutoFollowStats autoFollowStats = getAutoFollowStats(); + assertThat(autoFollowStats.getNumberOfSuccessfulFollowIndices(), equalTo(1L)); + assertThat(autoFollowStats.getNumberOfFailedFollowIndices(), greaterThanOrEqualTo(1L)); + assertThat(autoFollowStats.getNumberOfFailedRemoteClusterStateRequests(), equalTo(0L)); + + assertThat(autoFollowStats.getRecentAutoFollowErrors().size(), equalTo(2)); + ElasticsearchException autoFollowError1 = autoFollowStats.getRecentAutoFollowErrors().get("my-pattern1:logs-201801"); + assertThat(autoFollowError1, notNullValue()); + assertThat(autoFollowError1.getRootCause().getMessage(), equalTo("index to follow [logs-201801] for pattern [my-pattern1] " + + "matches with other patterns [my-pattern2]")); + + ElasticsearchException autoFollowError2 = autoFollowStats.getRecentAutoFollowErrors().get("my-pattern2:logs-201801"); + assertThat(autoFollowError2, notNullValue()); + assertThat(autoFollowError2.getRootCause().getMessage(), equalTo("index to follow [logs-201801] for pattern [my-pattern2] " + + "matches with other patterns [my-pattern1]")); + }); + + request = new IndicesExistsRequest("copy-logs-201801"); + assertFalse(client().admin().indices().exists(request).actionGet().isExists()); + } + + private void putAutoFollowPatterns(String name, String[] patterns) { PutAutoFollowPatternAction.Request request = new PutAutoFollowPatternAction.Request(); - request.setName("my-pattern"); + request.setName(name); request.setLeaderCluster("_local_"); request.setLeaderIndexPatterns(Arrays.asList(patterns)); // Need to set this, because following an index in the same cluster From 35e780232f627522fef6baed1265df239c342061 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 22 Oct 2018 15:08:43 +0200 Subject: [PATCH 3/3] fixed tests post merge --- .../test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java index e9d0534d45840..dea6db659049e 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java @@ -683,6 +683,7 @@ public void testUnknownClusterAlias() throws Exception { () -> followerClient().execute(ResumeFollowAction.INSTANCE, followRequest.getFollowRequest()).actionGet()); assertThat(e.getMessage(), equalTo("unknown cluster alias [another_cluster]")); PutAutoFollowPatternAction.Request putAutoFollowRequest = new PutAutoFollowPatternAction.Request(); + putAutoFollowRequest.setName("name"); putAutoFollowRequest.setLeaderCluster("another_cluster"); putAutoFollowRequest.setLeaderIndexPatterns(Collections.singletonList("logs-*")); e = expectThrows(IllegalArgumentException.class,