|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.ccr; |
| 8 | + |
| 9 | +import org.elasticsearch.action.admin.cluster.allocation.ClusterAllocationExplanation; |
| 10 | +import org.elasticsearch.action.support.ActiveShardCount; |
| 11 | +import org.elasticsearch.cluster.ClusterState; |
| 12 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 13 | +import org.elasticsearch.cluster.node.DiscoveryNodeRole; |
| 14 | +import org.elasticsearch.cluster.routing.IndexShardRoutingTable; |
| 15 | +import org.elasticsearch.cluster.routing.ShardRouting; |
| 16 | +import org.elasticsearch.cluster.routing.allocation.AllocationDecision; |
| 17 | +import org.elasticsearch.cluster.routing.allocation.NodeAllocationResult; |
| 18 | +import org.elasticsearch.common.settings.Settings; |
| 19 | +import org.elasticsearch.common.unit.TimeValue; |
| 20 | +import org.elasticsearch.common.xcontent.XContentType; |
| 21 | +import org.elasticsearch.test.NodeRoles; |
| 22 | +import org.elasticsearch.xpack.CcrIntegTestCase; |
| 23 | +import org.elasticsearch.xpack.core.ccr.action.PutFollowAction; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.stream.Collectors; |
| 28 | +import java.util.stream.Stream; |
| 29 | + |
| 30 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 31 | +import static org.hamcrest.Matchers.equalTo; |
| 32 | +import static org.hamcrest.Matchers.in; |
| 33 | + |
| 34 | +public class PrimaryFollowerAllocationIT extends CcrIntegTestCase { |
| 35 | + |
| 36 | + @Override |
| 37 | + protected boolean reuseClusters() { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + public void testDoNotAllocateFollowerPrimaryToNodesWithoutRemoteClusterClientRole() throws Exception { |
| 42 | + final String leaderIndex = "leader-not-allow-index"; |
| 43 | + final String followerIndex = "follower-not-allow-index"; |
| 44 | + final List<String> dataOnlyNodes = getFollowerCluster().startNodes(between(1, 2), |
| 45 | + NodeRoles.onlyRoles(Set.of(DiscoveryNodeRole.DATA_ROLE))); |
| 46 | + assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex) |
| 47 | + .setSource(getIndexSettings(between(1, 2), between(0, 1)), XContentType.JSON)); |
| 48 | + final PutFollowAction.Request putFollowRequest = putFollow(leaderIndex, followerIndex); |
| 49 | + putFollowRequest.setSettings(Settings.builder() |
| 50 | + .put("index.routing.allocation.include._name", String.join(",", dataOnlyNodes)) |
| 51 | + .build()); |
| 52 | + putFollowRequest.waitForActiveShards(ActiveShardCount.ONE); |
| 53 | + putFollowRequest.timeout(TimeValue.timeValueSeconds(2)); |
| 54 | + final PutFollowAction.Response response = followerClient().execute(PutFollowAction.INSTANCE, putFollowRequest).get(); |
| 55 | + assertFalse(response.isFollowIndexShardsAcked()); |
| 56 | + assertFalse(response.isIndexFollowingStarted()); |
| 57 | + final ClusterAllocationExplanation explanation = followerClient().admin().cluster().prepareAllocationExplain() |
| 58 | + .setIndex(followerIndex).setShard(0).setPrimary(true).get().getExplanation(); |
| 59 | + for (NodeAllocationResult nodeDecision : explanation.getShardAllocationDecision().getAllocateDecision().getNodeDecisions()) { |
| 60 | + assertThat(nodeDecision.getNodeDecision(), equalTo(AllocationDecision.NO)); |
| 61 | + if (dataOnlyNodes.contains(nodeDecision.getNode().getName())) { |
| 62 | + final List<String> decisions = nodeDecision.getCanAllocateDecision().getDecisions() |
| 63 | + .stream().map(Object::toString).collect(Collectors.toList()); |
| 64 | + assertThat("NO(shard is a primary follower and being bootstrapped, but node does not have the remote_cluster_client role)", |
| 65 | + in(decisions)); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public void testAllocateFollowerPrimaryToNodesWithRemoteClusterClientRole() throws Exception { |
| 71 | + final String leaderIndex = "leader-allow-index"; |
| 72 | + final String followerIndex = "follower-allow-index"; |
| 73 | + final List<String> dataOnlyNodes = getFollowerCluster().startNodes(between(2, 3), |
| 74 | + NodeRoles.onlyRoles(Set.of(DiscoveryNodeRole.DATA_ROLE))); |
| 75 | + final List<String> dataAndRemoteNodes = getFollowerCluster().startNodes(between(1, 2), |
| 76 | + NodeRoles.onlyRoles(Set.of(DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE))); |
| 77 | + assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex) |
| 78 | + .setSource(getIndexSettings(between(1, 2), between(0, 1)), XContentType.JSON)); |
| 79 | + final PutFollowAction.Request putFollowRequest = putFollow(leaderIndex, followerIndex); |
| 80 | + putFollowRequest.setSettings(Settings.builder() |
| 81 | + .put("index.routing.rebalance.enable", "none") |
| 82 | + .put("index.routing.allocation.include._name", |
| 83 | + Stream.concat(dataOnlyNodes.stream(), dataAndRemoteNodes.stream()).collect(Collectors.joining(","))) |
| 84 | + .build()); |
| 85 | + final PutFollowAction.Response response = followerClient().execute(PutFollowAction.INSTANCE, putFollowRequest).get(); |
| 86 | + assertTrue(response.isFollowIndexShardsAcked()); |
| 87 | + assertTrue(response.isIndexFollowingStarted()); |
| 88 | + ensureFollowerGreen(followerIndex); |
| 89 | + int numDocs = between(0, 20); |
| 90 | + for (int i = 0; i < numDocs; i++) { |
| 91 | + leaderClient().prepareIndex(leaderIndex).setSource("f", i).get(); |
| 92 | + } |
| 93 | + // Empty follower primaries must be assigned to nodes with the remote cluster client role |
| 94 | + assertBusy(() -> { |
| 95 | + final ClusterState state = getFollowerCluster().client().admin().cluster().prepareState().get().getState(); |
| 96 | + for (IndexShardRoutingTable shardRoutingTable : state.routingTable().index(followerIndex)) { |
| 97 | + final ShardRouting primaryShard = shardRoutingTable.primaryShard(); |
| 98 | + assertTrue(primaryShard.assignedToNode()); |
| 99 | + final DiscoveryNode assignedNode = state.nodes().get(primaryShard.currentNodeId()); |
| 100 | + assertThat(assignedNode.getName(), in(dataAndRemoteNodes)); |
| 101 | + } |
| 102 | + }); |
| 103 | + // Follower primaries can be relocated to nodes without the remote cluster client role |
| 104 | + followerClient().admin().indices().prepareUpdateSettings(followerIndex) |
| 105 | + .setSettings(Settings.builder().put("index.routing.allocation.include._name", String.join(",", dataOnlyNodes))) |
| 106 | + .get(); |
| 107 | + assertBusy(() -> { |
| 108 | + final ClusterState state = getFollowerCluster().client().admin().cluster().prepareState().get().getState(); |
| 109 | + for (IndexShardRoutingTable shardRoutingTable : state.routingTable().index(followerIndex)) { |
| 110 | + for (ShardRouting shard : shardRoutingTable) { |
| 111 | + assertNotNull(shard.currentNodeId()); |
| 112 | + final DiscoveryNode assignedNode = state.nodes().get(shard.currentNodeId()); |
| 113 | + assertThat(assignedNode.getName(), in(dataOnlyNodes)); |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | + assertIndexFullyReplicatedToFollower(leaderIndex, followerIndex); |
| 118 | + // Follower primaries can be recovered from the existing copies on nodes without the remote cluster client role |
| 119 | + getFollowerCluster().fullRestart(); |
| 120 | + ensureFollowerGreen(followerIndex); |
| 121 | + assertBusy(() -> { |
| 122 | + final ClusterState state = getFollowerCluster().client().admin().cluster().prepareState().get().getState(); |
| 123 | + for (IndexShardRoutingTable shardRoutingTable : state.routingTable().index(followerIndex)) { |
| 124 | + for (ShardRouting shard : shardRoutingTable) { |
| 125 | + assertNotNull(shard.currentNodeId()); |
| 126 | + final DiscoveryNode assignedNode = state.nodes().get(shard.currentNodeId()); |
| 127 | + assertThat(assignedNode.getName(), in(dataOnlyNodes)); |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | + int moreDocs = between(0, 20); |
| 132 | + for (int i = 0; i < moreDocs; i++) { |
| 133 | + leaderClient().prepareIndex(leaderIndex).setSource("f", i).get(); |
| 134 | + } |
| 135 | + assertIndexFullyReplicatedToFollower(leaderIndex, followerIndex); |
| 136 | + } |
| 137 | +} |
0 commit comments