|
| 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 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.cluster.routing.allocation.allocator; |
| 10 | + |
| 11 | +import org.elasticsearch.cluster.ClusterInfo; |
| 12 | +import org.elasticsearch.cluster.ClusterInfoSimulator; |
| 13 | +import org.elasticsearch.cluster.DiskUsage; |
| 14 | +import org.elasticsearch.cluster.routing.ShardRouting; |
| 15 | +import org.elasticsearch.test.ESTestCase; |
| 16 | + |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import static org.elasticsearch.cluster.routing.ShardRoutingState.INITIALIZING; |
| 21 | +import static org.elasticsearch.cluster.routing.ShardRoutingState.STARTED; |
| 22 | +import static org.elasticsearch.cluster.routing.TestShardRouting.newShardRouting; |
| 23 | +import static org.hamcrest.Matchers.equalTo; |
| 24 | + |
| 25 | +public class ClusterInfoSimulatorTests extends ESTestCase { |
| 26 | + |
| 27 | + public void testInitializeNewPrimary() { |
| 28 | + |
| 29 | + var newPrimary = newShardRouting("index-1", 0, "node-0", true, INITIALIZING); |
| 30 | + |
| 31 | + var simulator = new ClusterInfoSimulator( |
| 32 | + new ClusterInfoTestBuilder() // |
| 33 | + .withNode("node-0", new DiskUsageBuilder(1000, 1000)) |
| 34 | + .withNode("node-1", new DiskUsageBuilder(1000, 1000)) |
| 35 | + .withShard(newPrimary, 0) |
| 36 | + .build() |
| 37 | + ); |
| 38 | + simulator.simulate(newPrimary); |
| 39 | + |
| 40 | + assertThat( |
| 41 | + simulator.getClusterInfo(), |
| 42 | + equalTo( |
| 43 | + new ClusterInfoTestBuilder() // |
| 44 | + .withNode("node-0", new DiskUsageBuilder(1000, 1000)) |
| 45 | + .withNode("node-1", new DiskUsageBuilder(1000, 1000)) |
| 46 | + .withShard(newPrimary, 0) |
| 47 | + .build() |
| 48 | + ) |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + public void testInitializeNewReplica() { |
| 53 | + |
| 54 | + var existingPrimary = newShardRouting("index-1", 0, "node-0", true, STARTED); |
| 55 | + var newReplica = newShardRouting("index-1", 0, "node-1", false, INITIALIZING); |
| 56 | + |
| 57 | + var simulator = new ClusterInfoSimulator( |
| 58 | + new ClusterInfoTestBuilder() // |
| 59 | + .withNode("node-0", new DiskUsageBuilder(1000, 900)) |
| 60 | + .withNode("node-1", new DiskUsageBuilder(1000, 1000)) |
| 61 | + .withShard(existingPrimary, 100) |
| 62 | + .withShard(newReplica, 0) |
| 63 | + .build() |
| 64 | + ); |
| 65 | + simulator.simulate(newReplica); |
| 66 | + |
| 67 | + assertThat( |
| 68 | + simulator.getClusterInfo(), |
| 69 | + equalTo( |
| 70 | + new ClusterInfoTestBuilder() // |
| 71 | + .withNode("node-0", new DiskUsageBuilder(1000, 900)) |
| 72 | + .withNode("node-1", new DiskUsageBuilder(1000, 900)) |
| 73 | + .withShard(existingPrimary, 100) |
| 74 | + .withShard(newReplica, 100) |
| 75 | + .build() |
| 76 | + ) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + public void testRelocateShard() { |
| 81 | + |
| 82 | + var fromNodeId = "node-0"; |
| 83 | + var toNodeId = "node-1"; |
| 84 | + |
| 85 | + var shard = newShardRouting("index-1", 0, toNodeId, fromNodeId, true, INITIALIZING); |
| 86 | + |
| 87 | + var simulator = new ClusterInfoSimulator( |
| 88 | + new ClusterInfoTestBuilder() // |
| 89 | + .withNode(fromNodeId, new DiskUsageBuilder(1000, 900)) |
| 90 | + .withNode(toNodeId, new DiskUsageBuilder(1000, 1000)) |
| 91 | + .withShard(shard, 100) |
| 92 | + .build() |
| 93 | + ); |
| 94 | + simulator.simulate(shard); |
| 95 | + |
| 96 | + assertThat( |
| 97 | + simulator.getClusterInfo(), |
| 98 | + equalTo( |
| 99 | + new ClusterInfoTestBuilder() // |
| 100 | + .withNode(fromNodeId, new DiskUsageBuilder(1000, 1000)) |
| 101 | + .withNode(toNodeId, new DiskUsageBuilder(1000, 900)) |
| 102 | + .withShard(shard, 100) |
| 103 | + .build() |
| 104 | + ) |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + public void testRelocateShardWithMultipleDataPath1() { |
| 109 | + |
| 110 | + var fromNodeId = "node-0"; |
| 111 | + var toNodeId = "node-1"; |
| 112 | + |
| 113 | + var shard = newShardRouting("index-1", 0, toNodeId, fromNodeId, true, INITIALIZING); |
| 114 | + |
| 115 | + var simulator = new ClusterInfoSimulator( |
| 116 | + new ClusterInfoTestBuilder() // |
| 117 | + .withNode(fromNodeId, new DiskUsageBuilder("/data-1", 1000, 500), new DiskUsageBuilder("/data-2", 1000, 750)) |
| 118 | + .withNode(toNodeId, new DiskUsageBuilder("/data-1", 1000, 750), new DiskUsageBuilder("/data-2", 1000, 900)) |
| 119 | + .withShard(shard, 100) |
| 120 | + .build() |
| 121 | + ); |
| 122 | + simulator.simulate(shard); |
| 123 | + |
| 124 | + assertThat( |
| 125 | + simulator.getClusterInfo(), |
| 126 | + equalTo( |
| 127 | + new ClusterInfoTestBuilder() // |
| 128 | + .withNode(fromNodeId, new DiskUsageBuilder("/data-1", 1000, 500), new DiskUsageBuilder("/data-2", 1000, 850)) |
| 129 | + .withNode(toNodeId, new DiskUsageBuilder("/data-1", 1000, 650), new DiskUsageBuilder("/data-2", 1000, 900)) |
| 130 | + .withShard(shard, 100) |
| 131 | + .build() |
| 132 | + ) |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + private static class ClusterInfoTestBuilder { |
| 137 | + |
| 138 | + private final Map<String, DiskUsage> leastAvailableSpaceUsage = new HashMap<>(); |
| 139 | + private final Map<String, DiskUsage> mostAvailableSpaceUsage = new HashMap<>(); |
| 140 | + private final Map<String, Long> shardSizes = new HashMap<>(); |
| 141 | + |
| 142 | + public ClusterInfoTestBuilder withNode(String name, DiskUsageBuilder diskUsageBuilderBuilder) { |
| 143 | + leastAvailableSpaceUsage.put(name, diskUsageBuilderBuilder.toDiskUsage(name)); |
| 144 | + mostAvailableSpaceUsage.put(name, diskUsageBuilderBuilder.toDiskUsage(name)); |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + public ClusterInfoTestBuilder withNode(String name, DiskUsageBuilder leastAvailableSpace, DiskUsageBuilder mostAvailableSpace) { |
| 149 | + leastAvailableSpaceUsage.put(name, leastAvailableSpace.toDiskUsage(name)); |
| 150 | + mostAvailableSpaceUsage.put(name, mostAvailableSpace.toDiskUsage(name)); |
| 151 | + return this; |
| 152 | + } |
| 153 | + |
| 154 | + public ClusterInfoTestBuilder withShard(ShardRouting shard, long size) { |
| 155 | + shardSizes.put(ClusterInfo.shardIdentifierFromRouting(shard), size); |
| 156 | + return this; |
| 157 | + } |
| 158 | + |
| 159 | + public ClusterInfo build() { |
| 160 | + return new ClusterInfo(leastAvailableSpaceUsage, mostAvailableSpaceUsage, shardSizes, Map.of(), Map.of(), Map.of()); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private record DiskUsageBuilder(String path, long total, long free) { |
| 165 | + |
| 166 | + private DiskUsageBuilder(long total, long free) { |
| 167 | + this("/data", total, free); |
| 168 | + } |
| 169 | + |
| 170 | + public DiskUsage toDiskUsage(String name) { |
| 171 | + return new DiskUsage(name, name, name + '/' + path, total, free); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments