|
| 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 | +package org.opensearch.cluster.routing; |
| 10 | + |
| 11 | +import org.opensearch.action.admin.cluster.health.ClusterHealthResponse; |
| 12 | +import org.opensearch.action.admin.cluster.shards.routing.weighted.get.ClusterGetWeightedRoutingResponse; |
| 13 | +import org.opensearch.action.admin.cluster.shards.routing.weighted.put.ClusterPutWeightedRoutingResponse; |
| 14 | +import org.opensearch.common.settings.Settings; |
| 15 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import static org.hamcrest.Matchers.equalTo; |
| 22 | + |
| 23 | +@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0, minNumDataNodes = 3) |
| 24 | +public class WeightedRoutingIT extends OpenSearchIntegTestCase { |
| 25 | + |
| 26 | + public void testPutWeightedRouting() { |
| 27 | + Settings commonSettings = Settings.builder() |
| 28 | + .put("cluster.routing.allocation.awareness.attributes", "zone") |
| 29 | + .put("cluster.routing.allocation.awareness.force.zone.values", "a,b,c") |
| 30 | + .build(); |
| 31 | + |
| 32 | + logger.info("--> starting 6 nodes on different zones"); |
| 33 | + int nodeCountPerAZ = 2; |
| 34 | + |
| 35 | + logger.info("--> starting a dedicated cluster manager node"); |
| 36 | + internalCluster().startClusterManagerOnlyNode(Settings.builder().put(commonSettings).build()); |
| 37 | + |
| 38 | + logger.info("--> starting 1 nodes on zones 'a' & 'b' & 'c'"); |
| 39 | + List<String> nodes_in_zone_a = internalCluster().startDataOnlyNodes( |
| 40 | + nodeCountPerAZ, |
| 41 | + Settings.builder().put(commonSettings).put("node.attr.zone", "a").build() |
| 42 | + ); |
| 43 | + List<String> nodes_in_zone_b = internalCluster().startDataOnlyNodes( |
| 44 | + nodeCountPerAZ, |
| 45 | + Settings.builder().put(commonSettings).put("node.attr.zone", "b").build() |
| 46 | + ); |
| 47 | + List<String> nodes_in_zone_c = internalCluster().startDataOnlyNodes( |
| 48 | + nodeCountPerAZ, |
| 49 | + Settings.builder().put(commonSettings).put("node.attr.zone", "c").build() |
| 50 | + ); |
| 51 | + |
| 52 | + logger.info("--> waiting for nodes to form a cluster"); |
| 53 | + ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForNodes("7").execute().actionGet(); |
| 54 | + assertThat(health.isTimedOut(), equalTo(false)); |
| 55 | + |
| 56 | + ensureGreen(); |
| 57 | + |
| 58 | + logger.info("--> setting shard routing weights for weighted round robin"); |
| 59 | + Map<String, Double> weights = Map.of("a", 1.0, "b", 2.0, "c", 3.0); |
| 60 | + WeightedRouting weightedRouting = new WeightedRouting("zone", weights); |
| 61 | + |
| 62 | + ClusterPutWeightedRoutingResponse response = client().admin() |
| 63 | + .cluster() |
| 64 | + .prepareWeightedRouting() |
| 65 | + .setWeightedRouting(weightedRouting) |
| 66 | + .get(); |
| 67 | + assertEquals(response.isAcknowledged(), true); |
| 68 | + |
| 69 | + // put call made on a data node in zone a |
| 70 | + response = internalCluster().client(randomFrom(nodes_in_zone_a.get(0), nodes_in_zone_a.get(1))) |
| 71 | + .admin() |
| 72 | + .cluster() |
| 73 | + .prepareWeightedRouting() |
| 74 | + .setWeightedRouting(weightedRouting) |
| 75 | + .get(); |
| 76 | + assertEquals(response.isAcknowledged(), true); |
| 77 | + } |
| 78 | + |
| 79 | + public void testPutWeightedRouting_InvalidAwarenessAttribute() { |
| 80 | + Settings commonSettings = Settings.builder() |
| 81 | + .put("cluster.routing.allocation.awareness.attributes", "zone") |
| 82 | + .put("cluster.routing.allocation.awareness.force.zone.values", "a,b,c") |
| 83 | + .build(); |
| 84 | + |
| 85 | + internalCluster().startNodes( |
| 86 | + Settings.builder().put(commonSettings).put("node.attr.zone", "a").build(), |
| 87 | + Settings.builder().put(commonSettings).put("node.attr.zone", "b").build(), |
| 88 | + Settings.builder().put(commonSettings).put("node.attr.zone", "c").build() |
| 89 | + ); |
| 90 | + |
| 91 | + logger.info("--> waiting for nodes to form a cluster"); |
| 92 | + ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForNodes("3").execute().actionGet(); |
| 93 | + assertThat(health.isTimedOut(), equalTo(false)); |
| 94 | + |
| 95 | + ensureGreen(); |
| 96 | + |
| 97 | + logger.info("--> setting shard routing weights for weighted round robin"); |
| 98 | + Map<String, Double> weights = Map.of("a", 1.0, "b", 2.0, "c", 3.0); |
| 99 | + WeightedRouting weightedRouting = new WeightedRouting("zone1", weights); |
| 100 | + |
| 101 | + assertThrows( |
| 102 | + IllegalArgumentException.class, |
| 103 | + () -> client().admin().cluster().prepareWeightedRouting().setWeightedRouting(weightedRouting).get() |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + public void testPutWeightedRouting_MoreThanOneZoneHasZeroWeight() { |
| 108 | + Settings commonSettings = Settings.builder() |
| 109 | + .put("cluster.routing.allocation.awareness.attributes", "zone") |
| 110 | + .put("cluster.routing.allocation.awareness.force.zone.values", "a,b,c") |
| 111 | + .build(); |
| 112 | + |
| 113 | + internalCluster().startNodes( |
| 114 | + Settings.builder().put(commonSettings).put("node.attr.zone", "a").build(), |
| 115 | + Settings.builder().put(commonSettings).put("node.attr.zone", "b").build(), |
| 116 | + Settings.builder().put(commonSettings).put("node.attr.zone", "c").build() |
| 117 | + ); |
| 118 | + |
| 119 | + logger.info("--> waiting for nodes to form a cluster"); |
| 120 | + ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForNodes("3").execute().actionGet(); |
| 121 | + assertThat(health.isTimedOut(), equalTo(false)); |
| 122 | + |
| 123 | + ensureGreen(); |
| 124 | + |
| 125 | + logger.info("--> setting shard routing weights for weighted round robin"); |
| 126 | + Map<String, Double> weights = Map.of("a", 1.0, "b", 0.0, "c", 0.0); |
| 127 | + WeightedRouting weightedRouting = new WeightedRouting("zone1", weights); |
| 128 | + |
| 129 | + assertThrows( |
| 130 | + IllegalArgumentException.class, |
| 131 | + () -> client().admin().cluster().prepareWeightedRouting().setWeightedRouting(weightedRouting).get() |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + public void testGetWeightedRouting_WeightsNotSet() { |
| 136 | + Settings commonSettings = Settings.builder() |
| 137 | + .put("cluster.routing.allocation.awareness.attributes", "zone") |
| 138 | + .put("cluster.routing.allocation.awareness.force.zone.values", "a,b,c") |
| 139 | + .build(); |
| 140 | + |
| 141 | + internalCluster().startNodes( |
| 142 | + Settings.builder().put(commonSettings).put("node.attr.zone", "a").build(), |
| 143 | + Settings.builder().put(commonSettings).put("node.attr.zone", "b").build(), |
| 144 | + Settings.builder().put(commonSettings).put("node.attr.zone", "c").build() |
| 145 | + ); |
| 146 | + |
| 147 | + logger.info("--> waiting for nodes to form a cluster"); |
| 148 | + ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForNodes("3").execute().actionGet(); |
| 149 | + assertThat(health.isTimedOut(), equalTo(false)); |
| 150 | + |
| 151 | + ensureGreen(); |
| 152 | + |
| 153 | + ClusterGetWeightedRoutingResponse weightedRoutingResponse = client().admin() |
| 154 | + .cluster() |
| 155 | + .prepareGetWeightedRouting() |
| 156 | + .setAwarenessAttribute("zone") |
| 157 | + .get(); |
| 158 | + assertNull(weightedRoutingResponse.weights()); |
| 159 | + } |
| 160 | + |
| 161 | + public void testGetWeightedRouting_WeightsAreSet() throws IOException { |
| 162 | + |
| 163 | + Settings commonSettings = Settings.builder() |
| 164 | + .put("cluster.routing.allocation.awareness.attributes", "zone") |
| 165 | + .put("cluster.routing.allocation.awareness.force.zone.values", "a,b,c") |
| 166 | + .build(); |
| 167 | + |
| 168 | + int nodeCountPerAZ = 2; |
| 169 | + |
| 170 | + logger.info("--> starting a dedicated cluster manager node"); |
| 171 | + internalCluster().startClusterManagerOnlyNode(Settings.builder().put(commonSettings).build()); |
| 172 | + |
| 173 | + logger.info("--> starting 2 nodes on zones 'a' & 'b' & 'c'"); |
| 174 | + List<String> nodes_in_zone_a = internalCluster().startDataOnlyNodes( |
| 175 | + nodeCountPerAZ, |
| 176 | + Settings.builder().put(commonSettings).put("node.attr.zone", "a").build() |
| 177 | + ); |
| 178 | + List<String> nodes_in_zone_b = internalCluster().startDataOnlyNodes( |
| 179 | + nodeCountPerAZ, |
| 180 | + Settings.builder().put(commonSettings).put("node.attr.zone", "b").build() |
| 181 | + ); |
| 182 | + List<String> nodes_in_zone_c = internalCluster().startDataOnlyNodes( |
| 183 | + nodeCountPerAZ, |
| 184 | + Settings.builder().put(commonSettings).put("node.attr.zone", "c").build() |
| 185 | + ); |
| 186 | + |
| 187 | + logger.info("--> waiting for nodes to form a cluster"); |
| 188 | + ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForNodes("7").execute().actionGet(); |
| 189 | + assertThat(health.isTimedOut(), equalTo(false)); |
| 190 | + |
| 191 | + ensureGreen(); |
| 192 | + |
| 193 | + logger.info("--> setting shard routing weights for weighted round robin"); |
| 194 | + Map<String, Double> weights = Map.of("a", 1.0, "b", 2.0, "c", 3.0); |
| 195 | + WeightedRouting weightedRouting = new WeightedRouting("zone", weights); |
| 196 | + // put api call to set weights |
| 197 | + ClusterPutWeightedRoutingResponse response = client().admin() |
| 198 | + .cluster() |
| 199 | + .prepareWeightedRouting() |
| 200 | + .setWeightedRouting(weightedRouting) |
| 201 | + .get(); |
| 202 | + assertEquals(response.isAcknowledged(), true); |
| 203 | + |
| 204 | + // get api call to fetch weights |
| 205 | + ClusterGetWeightedRoutingResponse weightedRoutingResponse = client().admin() |
| 206 | + .cluster() |
| 207 | + .prepareGetWeightedRouting() |
| 208 | + .setAwarenessAttribute("zone") |
| 209 | + .get(); |
| 210 | + assertEquals(weightedRouting, weightedRoutingResponse.weights()); |
| 211 | + |
| 212 | + // get api to fetch local node weight for a node in zone a |
| 213 | + weightedRoutingResponse = internalCluster().client(randomFrom(nodes_in_zone_a.get(0), nodes_in_zone_a.get(1))) |
| 214 | + .admin() |
| 215 | + .cluster() |
| 216 | + .prepareGetWeightedRouting() |
| 217 | + .setAwarenessAttribute("zone") |
| 218 | + .setRequestLocal(true) |
| 219 | + .get(); |
| 220 | + assertEquals(weightedRouting, weightedRoutingResponse.weights()); |
| 221 | + assertEquals("1.0", weightedRoutingResponse.getLocalNodeWeight()); |
| 222 | + |
| 223 | + // get api to fetch local node weight for a node in zone b |
| 224 | + weightedRoutingResponse = internalCluster().client(randomFrom(nodes_in_zone_b.get(0), nodes_in_zone_b.get(1))) |
| 225 | + .admin() |
| 226 | + .cluster() |
| 227 | + .prepareGetWeightedRouting() |
| 228 | + .setAwarenessAttribute("zone") |
| 229 | + .setRequestLocal(true) |
| 230 | + .get(); |
| 231 | + assertEquals(weightedRouting, weightedRoutingResponse.weights()); |
| 232 | + assertEquals("2.0", weightedRoutingResponse.getLocalNodeWeight()); |
| 233 | + |
| 234 | + // get api to fetch local node weight for a node in zone c |
| 235 | + weightedRoutingResponse = internalCluster().client(randomFrom(nodes_in_zone_c.get(0), nodes_in_zone_c.get(1))) |
| 236 | + .admin() |
| 237 | + .cluster() |
| 238 | + .prepareGetWeightedRouting() |
| 239 | + .setAwarenessAttribute("zone") |
| 240 | + .setRequestLocal(true) |
| 241 | + .get(); |
| 242 | + assertEquals(weightedRouting, weightedRoutingResponse.weights()); |
| 243 | + assertEquals("3.0", weightedRoutingResponse.getLocalNodeWeight()); |
| 244 | + } |
| 245 | +} |
0 commit comments