|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +/* |
| 10 | + * Licensed to Elasticsearch under one or more contributor |
| 11 | + * license agreements. See the NOTICE file distributed with |
| 12 | + * this work for additional information regarding copyright |
| 13 | + * ownership. Elasticsearch licenses this file to you under |
| 14 | + * the Apache License, Version 2.0 (the "License"); you may |
| 15 | + * not use this file except in compliance with the License. |
| 16 | + * You may obtain a copy of the License at |
| 17 | + * |
| 18 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | + * |
| 20 | + * Unless required by applicable law or agreed to in writing, |
| 21 | + * software distributed under the License is distributed on an |
| 22 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 23 | + * KIND, either express or implied. See the License for the |
| 24 | + * specific language governing permissions and limitations |
| 25 | + * under the License. |
| 26 | + */ |
| 27 | + |
| 28 | +/* |
| 29 | + * Modifications Copyright OpenSearch Contributors. See |
| 30 | + * GitHub history for details. |
| 31 | + */ |
| 32 | + |
| 33 | +package org.opensearch.cluster.allocation; |
| 34 | + |
| 35 | +import org.apache.logging.log4j.LogManager; |
| 36 | +import org.apache.logging.log4j.Logger; |
| 37 | +import org.opensearch.action.admin.cluster.health.ClusterHealthResponse; |
| 38 | +import org.opensearch.cluster.ClusterState; |
| 39 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 40 | +import org.opensearch.cluster.routing.IndexRoutingTable; |
| 41 | +import org.opensearch.cluster.routing.IndexShardRoutingTable; |
| 42 | +import org.opensearch.cluster.routing.ShardRouting; |
| 43 | +import org.opensearch.cluster.routing.allocation.decider.AwarenessAllocationDecider; |
| 44 | +import org.opensearch.common.settings.Settings; |
| 45 | +import org.opensearch.common.util.FeatureFlags; |
| 46 | +import org.opensearch.indices.replication.common.ReplicationType; |
| 47 | +import org.opensearch.remotestore.RemoteStoreBaseIntegTestCase; |
| 48 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 49 | +import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope; |
| 50 | + |
| 51 | +import java.util.HashMap; |
| 52 | +import java.util.List; |
| 53 | +import java.util.Map; |
| 54 | + |
| 55 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REPLICATION_TYPE; |
| 56 | +import static org.hamcrest.Matchers.anyOf; |
| 57 | +import static org.hamcrest.Matchers.equalTo; |
| 58 | + |
| 59 | +@ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0) |
| 60 | +public class SearchReplicaAwarenessAllocationIT extends RemoteStoreBaseIntegTestCase { |
| 61 | + |
| 62 | + private final Logger logger = LogManager.getLogger(SearchReplicaAwarenessAllocationIT.class); |
| 63 | + |
| 64 | + @Override |
| 65 | + protected Settings featureFlagSettings() { |
| 66 | + return Settings.builder().put(super.featureFlagSettings()).put(FeatureFlags.READER_WRITER_SPLIT_EXPERIMENTAL, Boolean.TRUE).build(); |
| 67 | + } |
| 68 | + |
| 69 | + public void testAllocationAwarenessZones() { |
| 70 | + Settings commonSettings = Settings.builder() |
| 71 | + .put(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_FORCE_GROUP_SETTING.getKey() + "zone.values", "a,b") |
| 72 | + .put(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.getKey(), "zone") |
| 73 | + .build(); |
| 74 | + |
| 75 | + logger.info("--> starting 8 nodes on different zones"); |
| 76 | + List<String> nodes = internalCluster().startNodes( |
| 77 | + Settings.builder().put(commonSettings).put("node.attr.zone", "a").build(), |
| 78 | + Settings.builder().put(commonSettings).put("node.attr.zone", "b").build(), |
| 79 | + Settings.builder().put(commonSettings).put("node.attr.zone", "b").build(), |
| 80 | + Settings.builder().put(commonSettings).put("node.attr.zone", "a").build(), |
| 81 | + Settings.builder().put(commonSettings).put("node.attr.zone", "a").build(), |
| 82 | + Settings.builder().put(commonSettings).put("node.attr.zone", "b").build(), |
| 83 | + Settings.builder().put(commonSettings).put("node.attr.zone", "b").build(), |
| 84 | + Settings.builder().put(commonSettings).put("node.attr.zone", "a").build() |
| 85 | + ); |
| 86 | + |
| 87 | + logger.info("--> waiting for nodes to form a cluster"); |
| 88 | + ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForNodes("8").execute().actionGet(); |
| 89 | + assertThat(health.isTimedOut(), equalTo(false)); |
| 90 | + |
| 91 | + logger.info("--> set search dedicated for nodes"); |
| 92 | + setSearchDedicatedNodeSettings(nodes.get(4) + ", " + nodes.get(5) + ", " + nodes.get(6) + ", " + nodes.get(7)); |
| 93 | + |
| 94 | + logger.info("--> create index"); |
| 95 | + createIndex( |
| 96 | + "test", |
| 97 | + Settings.builder() |
| 98 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 5) |
| 99 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) |
| 100 | + .put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, 2) |
| 101 | + .put(SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT) |
| 102 | + .build() |
| 103 | + ); |
| 104 | + |
| 105 | + logger.info("--> waiting for shards to be allocated"); |
| 106 | + ensureGreen("test"); |
| 107 | + |
| 108 | + ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState(); |
| 109 | + final Map<String, Integer> counts = new HashMap<>(); |
| 110 | + |
| 111 | + for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) { |
| 112 | + for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) { |
| 113 | + for (ShardRouting shardRouting : indexShardRoutingTable) { |
| 114 | + counts.merge(clusterState.nodes().get(shardRouting.currentNodeId()).getName(), 1, Integer::sum); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + assertThat(counts.get(nodes.get(3)), anyOf(equalTo(2), equalTo(3))); |
| 120 | + assertThat(counts.get(nodes.get(2)), anyOf(equalTo(2), equalTo(3))); |
| 121 | + assertThat(counts.get(nodes.get(0)), anyOf(equalTo(2), equalTo(3))); |
| 122 | + assertThat(counts.get(nodes.get(1)), anyOf(equalTo(2), equalTo(3))); |
| 123 | + assertThat(counts.get(nodes.get(4)), anyOf(equalTo(2), equalTo(3))); |
| 124 | + assertThat(counts.get(nodes.get(5)), anyOf(equalTo(2), equalTo(3))); |
| 125 | + assertThat(counts.get(nodes.get(6)), anyOf(equalTo(2), equalTo(3))); |
| 126 | + assertThat(counts.get(nodes.get(7)), anyOf(equalTo(2), equalTo(3))); |
| 127 | + } |
| 128 | + |
| 129 | + public void testAwarenessZonesIncrementalNodes() { |
| 130 | + Settings commonSettings = Settings.builder() |
| 131 | + .put("cluster.routing.allocation.awareness.force.zone.values", "a,b") |
| 132 | + .put("cluster.routing.allocation.awareness.attributes", "zone") |
| 133 | + .build(); |
| 134 | + |
| 135 | + logger.info("--> starting 2 nodes on zones 'a' & 'b'"); |
| 136 | + List<String> nodes = internalCluster().startNodes( |
| 137 | + Settings.builder().put(commonSettings).put("node.attr.zone", "a").build(), |
| 138 | + Settings.builder().put(commonSettings).put("node.attr.zone", "b").build(), |
| 139 | + Settings.builder().put(commonSettings).put("node.attr.zone", "a").build(), |
| 140 | + Settings.builder().put(commonSettings).put("node.attr.zone", "b").build() |
| 141 | + ); |
| 142 | + |
| 143 | + setSearchDedicatedNodeSettings(nodes.get(2) + "," + nodes.get(3)); |
| 144 | + |
| 145 | + createIndex( |
| 146 | + "test", |
| 147 | + Settings.builder() |
| 148 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 5) |
| 149 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) |
| 150 | + .put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, 2) |
| 151 | + .build() |
| 152 | + ); |
| 153 | + |
| 154 | + ensureGreen("test"); |
| 155 | + |
| 156 | + ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState(); |
| 157 | + Map<String, Integer> counts = new HashMap<>(); |
| 158 | + |
| 159 | + for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) { |
| 160 | + for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) { |
| 161 | + for (ShardRouting shardRouting : indexShardRoutingTable) { |
| 162 | + counts.merge(clusterState.nodes().get(shardRouting.currentNodeId()).getName(), 1, Integer::sum); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + assertThat(counts.get(nodes.get(0)), equalTo(5)); |
| 167 | + assertThat(counts.get(nodes.get(1)), equalTo(5)); |
| 168 | + assertThat(counts.get(nodes.get(2)), equalTo(5)); |
| 169 | + assertThat(counts.get(nodes.get(3)), equalTo(5)); |
| 170 | + |
| 171 | + logger.info("--> starting another node in zone 'b'"); |
| 172 | + |
| 173 | + String B_2 = internalCluster().startNode(Settings.builder().put(commonSettings).put("node.attr.zone", "b").build()); |
| 174 | + String B_3 = internalCluster().startNode(Settings.builder().put(commonSettings).put("node.attr.zone", "b").build()); |
| 175 | + |
| 176 | + setSearchDedicatedNodeSettings(nodes.get(2) + "," + nodes.get(3) + "," + B_3); |
| 177 | + |
| 178 | + ensureGreen("test"); |
| 179 | + |
| 180 | + client().admin().cluster().prepareReroute().get(); |
| 181 | + |
| 182 | + ensureGreen("test"); |
| 183 | + |
| 184 | + clusterState = client().admin().cluster().prepareState().execute().actionGet().getState(); |
| 185 | + |
| 186 | + counts = new HashMap<>(); |
| 187 | + |
| 188 | + for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) { |
| 189 | + for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) { |
| 190 | + for (ShardRouting shardRouting : indexShardRoutingTable) { |
| 191 | + counts.merge(clusterState.nodes().get(shardRouting.currentNodeId()).getName(), 1, Integer::sum); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + assertThat(counts.get(nodes.get(0)), equalTo(5)); |
| 197 | + assertThat(counts.get(nodes.get(1)), equalTo(3)); |
| 198 | + assertThat(counts.get(nodes.get(2)), equalTo(5)); |
| 199 | + assertThat(counts.get(nodes.get(3)), equalTo(3)); |
| 200 | + assertThat(counts.get(B_2), equalTo(2)); |
| 201 | + assertThat(counts.get(B_3), equalTo(2)); |
| 202 | + |
| 203 | + String noZoneNode = internalCluster().startNode(); |
| 204 | + ensureGreen("test"); |
| 205 | + client().admin().cluster().prepareReroute().get(); |
| 206 | + ensureGreen("test"); |
| 207 | + clusterState = client().admin().cluster().prepareState().execute().actionGet().getState(); |
| 208 | + |
| 209 | + counts = new HashMap<>(); |
| 210 | + |
| 211 | + for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) { |
| 212 | + for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) { |
| 213 | + for (ShardRouting shardRouting : indexShardRoutingTable) { |
| 214 | + counts.merge(clusterState.nodes().get(shardRouting.currentNodeId()).getName(), 1, Integer::sum); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + assertThat(counts.get(nodes.get(0)), equalTo(5)); |
| 220 | + assertThat(counts.get(nodes.get(1)), equalTo(3)); |
| 221 | + assertThat(counts.get(nodes.get(2)), equalTo(5)); |
| 222 | + assertThat(counts.get(nodes.get(3)), equalTo(3)); |
| 223 | + assertThat(counts.get(B_2), equalTo(2)); |
| 224 | + assertThat(counts.get(B_3), equalTo(2)); |
| 225 | + assertThat(counts.containsKey(noZoneNode), equalTo(false)); |
| 226 | + |
| 227 | + client().admin() |
| 228 | + .cluster() |
| 229 | + .prepareUpdateSettings() |
| 230 | + .setTransientSettings(Settings.builder().put("cluster.routing.allocation.awareness.attributes", "").build()) |
| 231 | + .get(); |
| 232 | + |
| 233 | + ensureGreen("test"); |
| 234 | + clusterState = client().admin().cluster().prepareState().execute().actionGet().getState(); |
| 235 | + |
| 236 | + counts = new HashMap<>(); |
| 237 | + |
| 238 | + for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) { |
| 239 | + for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) { |
| 240 | + for (ShardRouting shardRouting : indexShardRoutingTable) { |
| 241 | + counts.merge(clusterState.nodes().get(shardRouting.currentNodeId()).getName(), 1, Integer::sum); |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + assertThat(counts.get(nodes.get(0)), equalTo(3)); |
| 247 | + assertThat(counts.get(nodes.get(1)), equalTo(3)); |
| 248 | + assertThat(counts.get(nodes.get(2)), equalTo(4)); |
| 249 | + assertThat(counts.get(nodes.get(3)), equalTo(3)); |
| 250 | + assertThat(counts.get(B_2), equalTo(2)); |
| 251 | + assertThat(counts.get(B_3), equalTo(3)); |
| 252 | + assertThat(counts.get(noZoneNode), equalTo(2)); |
| 253 | + } |
| 254 | +} |
0 commit comments