|
20 | 20 | import org.elasticsearch.cluster.routing.IndexShardRoutingTable; |
21 | 21 | import org.elasticsearch.cluster.routing.RoutingNodesHelper; |
22 | 22 | import org.elasticsearch.cluster.routing.ShardRoutingState; |
| 23 | +import org.elasticsearch.cluster.routing.allocation.ExistingShardsAllocator; |
23 | 24 | import org.elasticsearch.common.settings.Settings; |
24 | 25 | import org.elasticsearch.core.Strings; |
| 26 | +import org.elasticsearch.index.IndexVersion; |
25 | 27 | import org.elasticsearch.indices.cluster.ClusterStateChanges; |
26 | 28 | import org.elasticsearch.test.ESTestCase; |
27 | 29 | import org.elasticsearch.threadpool.TestThreadPool; |
|
31 | 33 | import java.util.Collections; |
32 | 34 | import java.util.HashSet; |
33 | 35 | import java.util.List; |
| 36 | +import java.util.Map; |
34 | 37 | import java.util.Set; |
35 | 38 | import java.util.concurrent.atomic.AtomicInteger; |
36 | 39 | import java.util.stream.Collectors; |
37 | 40 |
|
| 41 | +import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_AUTO_EXPAND_REPLICAS_SETTING; |
38 | 42 | import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS; |
| 43 | +import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS; |
39 | 44 | import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS; |
40 | 45 | import static org.hamcrest.Matchers.equalTo; |
41 | 46 | import static org.hamcrest.Matchers.everyItem; |
@@ -221,4 +226,48 @@ public void testCalculateDesiredNumberOfReplicas() { |
221 | 226 | assertThat(autoExpandReplicas.calculateDesiredNumberOfReplicas(matchingNodes), equalTo(Math.max(lowerBound, matchingNodes - 1))); |
222 | 227 | assertThat(autoExpandReplicas.calculateDesiredNumberOfReplicas(max + 1), equalTo(max)); |
223 | 228 | } |
| 229 | + |
| 230 | + public void testGetAutoExpandReplicaChangesStatelessIndices() { |
| 231 | + { |
| 232 | + // number of replicas is adjusted to 1 when it is initialized to 0 |
| 233 | + Metadata metadata = Metadata.builder() |
| 234 | + .put( |
| 235 | + IndexMetadata.builder("test") |
| 236 | + .settings( |
| 237 | + Settings.builder() |
| 238 | + .put(ExistingShardsAllocator.EXISTING_SHARDS_ALLOCATOR_SETTING.getKey(), "stateless") |
| 239 | + .put("index.version.created", IndexVersion.current()) |
| 240 | + .put(SETTING_NUMBER_OF_SHARDS, 1) |
| 241 | + .put(SETTING_NUMBER_OF_REPLICAS, 0) |
| 242 | + .put(INDEX_AUTO_EXPAND_REPLICAS_SETTING.getKey(), "0-all") |
| 243 | + ) |
| 244 | + ) |
| 245 | + .build(); |
| 246 | + Map<Integer, List<String>> autoExpandReplicaChanges = AutoExpandReplicas.getAutoExpandReplicaChanges(metadata, null); |
| 247 | + assertEquals(1, autoExpandReplicaChanges.size()); |
| 248 | + List<String> indices = autoExpandReplicaChanges.get(1); |
| 249 | + assertEquals(1, indices.size()); |
| 250 | + assertEquals("test", indices.getFirst()); |
| 251 | + } |
| 252 | + { |
| 253 | + // no changes when number of replicas is set to anything other than 0 |
| 254 | + Metadata metadata = Metadata.builder() |
| 255 | + .put( |
| 256 | + IndexMetadata.builder("test") |
| 257 | + .settings( |
| 258 | + Settings.builder() |
| 259 | + .put(ExistingShardsAllocator.EXISTING_SHARDS_ALLOCATOR_SETTING.getKey(), "stateless") |
| 260 | + .put("index.version.created", IndexVersion.current()) |
| 261 | + .put(SETTING_NUMBER_OF_SHARDS, 1) |
| 262 | + .put(SETTING_NUMBER_OF_REPLICAS, randomIntBetween(1, 10)) |
| 263 | + .put(INDEX_AUTO_EXPAND_REPLICAS_SETTING.getKey(), "0-all") |
| 264 | + ) |
| 265 | + ) |
| 266 | + .build(); |
| 267 | + Map<Integer, List<String>> autoExpandReplicaChanges = AutoExpandReplicas.getAutoExpandReplicaChanges(metadata, () -> { |
| 268 | + throw new UnsupportedOperationException(); |
| 269 | + }); |
| 270 | + assertEquals(0, autoExpandReplicaChanges.size()); |
| 271 | + } |
| 272 | + } |
224 | 273 | } |
0 commit comments