|
| 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.cluster.ClusterState; |
| 10 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 11 | +import org.elasticsearch.cluster.routing.IndexShardRoutingTable; |
| 12 | +import org.elasticsearch.cluster.routing.ShardRouting; |
| 13 | +import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.common.xcontent.XContentType; |
| 15 | +import org.elasticsearch.node.Node; |
| 16 | +import org.elasticsearch.xpack.CcrIntegTestCase; |
| 17 | +import org.elasticsearch.xpack.core.ccr.action.PutFollowAction; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 22 | +import static org.hamcrest.Matchers.in; |
| 23 | + |
| 24 | +public class PrimaryFollowerAllocationIT extends CcrIntegTestCase { |
| 25 | + |
| 26 | + @Override |
| 27 | + protected boolean reuseClusters() { |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + public void testAllocateFollowerPrimaryToNodesWithRemoteClusterClientRole() throws Exception { |
| 32 | + final String leaderIndex = "leader-allow-index"; |
| 33 | + final String followerIndex = "follower-allow-index"; |
| 34 | + final List<String> dataOnlyNodes = getFollowerCluster().startNodes(between(2, 3), |
| 35 | + Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), true).build()); |
| 36 | + final List<String> dataAndRemoteNodes = getFollowerCluster().startNodes(between(1, 2), |
| 37 | + Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), true).put(Node.NODE_REMOTE_CLUSTER_CLIENT.getKey(), true).build()); |
| 38 | + assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex) |
| 39 | + .setSource(getIndexSettings(between(1, 2), between(0, 1)), XContentType.JSON)); |
| 40 | + final PutFollowAction.Request putFollowRequest = putFollow(leaderIndex, followerIndex); |
| 41 | + final PutFollowAction.Response response = followerClient().execute(PutFollowAction.INSTANCE, putFollowRequest).get(); |
| 42 | + assertTrue(response.isFollowIndexShardsAcked()); |
| 43 | + assertTrue(response.isIndexFollowingStarted()); |
| 44 | + ensureFollowerGreen(followerIndex); |
| 45 | + int numDocs = between(0, 20); |
| 46 | + for (int i = 0; i < numDocs; i++) { |
| 47 | + leaderClient().prepareIndex(leaderIndex, "_doc").setSource("f", i).get(); |
| 48 | + } |
| 49 | + // Follower primaries can be relocated to nodes without the remote cluster client role |
| 50 | + followerClient().admin().indices().prepareUpdateSettings(followerIndex) |
| 51 | + .setSettings(Settings.builder().put("index.routing.allocation.include._name", String.join(",", dataOnlyNodes))) |
| 52 | + .get(); |
| 53 | + assertBusy(() -> { |
| 54 | + final ClusterState state = getFollowerCluster().client().admin().cluster().prepareState().get().getState(); |
| 55 | + for (IndexShardRoutingTable shardRoutingTable : state.routingTable().index(followerIndex)) { |
| 56 | + for (ShardRouting shard : shardRoutingTable) { |
| 57 | + assertNotNull(shard.currentNodeId()); |
| 58 | + final DiscoveryNode assignedNode = state.nodes().get(shard.currentNodeId()); |
| 59 | + assertThat(assignedNode.getName(), in(dataOnlyNodes)); |
| 60 | + } |
| 61 | + } |
| 62 | + }); |
| 63 | + assertIndexFullyReplicatedToFollower(leaderIndex, followerIndex); |
| 64 | + // Follower primaries can be recovered from the existing copies on nodes without the remote cluster client role |
| 65 | + getFollowerCluster().fullRestart(); |
| 66 | + ensureFollowerGreen(followerIndex); |
| 67 | + assertBusy(() -> { |
| 68 | + final ClusterState state = getFollowerCluster().client().admin().cluster().prepareState().get().getState(); |
| 69 | + for (IndexShardRoutingTable shardRoutingTable : state.routingTable().index(followerIndex)) { |
| 70 | + for (ShardRouting shard : shardRoutingTable) { |
| 71 | + assertNotNull(shard.currentNodeId()); |
| 72 | + final DiscoveryNode assignedNode = state.nodes().get(shard.currentNodeId()); |
| 73 | + assertThat(assignedNode.getName(), in(dataOnlyNodes)); |
| 74 | + } |
| 75 | + } |
| 76 | + }); |
| 77 | + int moreDocs = between(0, 20); |
| 78 | + for (int i = 0; i < moreDocs; i++) { |
| 79 | + leaderClient().prepareIndex(leaderIndex, "_doc").setSource("f", i).get(); |
| 80 | + } |
| 81 | + assertIndexFullyReplicatedToFollower(leaderIndex, followerIndex); |
| 82 | + } |
| 83 | +} |
0 commit comments