|
| 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.benchmark.routing.allocation; |
| 10 | + |
| 11 | +import org.opensearch.Version; |
| 12 | +import org.opensearch.cluster.ClusterName; |
| 13 | +import org.opensearch.cluster.ClusterState; |
| 14 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 15 | +import org.opensearch.cluster.metadata.Metadata; |
| 16 | +import org.opensearch.cluster.node.DiscoveryNodes; |
| 17 | +import org.opensearch.cluster.routing.RoutingTable; |
| 18 | +import org.opensearch.cluster.routing.ShardRouting; |
| 19 | +import org.opensearch.cluster.routing.allocation.AllocationService; |
| 20 | +import org.opensearch.common.logging.LogConfigurator; |
| 21 | +import org.opensearch.common.settings.Settings; |
| 22 | +import org.openjdk.jmh.annotations.Benchmark; |
| 23 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 24 | +import org.openjdk.jmh.annotations.Fork; |
| 25 | +import org.openjdk.jmh.annotations.Measurement; |
| 26 | +import org.openjdk.jmh.annotations.Mode; |
| 27 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 28 | +import org.openjdk.jmh.annotations.Param; |
| 29 | +import org.openjdk.jmh.annotations.Scope; |
| 30 | +import org.openjdk.jmh.annotations.Setup; |
| 31 | +import org.openjdk.jmh.annotations.State; |
| 32 | +import org.openjdk.jmh.annotations.Warmup; |
| 33 | + |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | + |
| 39 | +import static org.opensearch.cluster.routing.ShardRoutingState.INITIALIZING; |
| 40 | + |
| 41 | +@Fork(1) |
| 42 | +@Warmup(iterations = 3) |
| 43 | +@Measurement(iterations = 3) |
| 44 | +@BenchmarkMode(Mode.AverageTime) |
| 45 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 46 | +@State(Scope.Benchmark) |
| 47 | +@SuppressWarnings("unused") // invoked by benchmarking framework |
| 48 | +public class RerouteBenchmark { |
| 49 | + @Param({ |
| 50 | + // indices| nodes |
| 51 | + " 10000| 500|", }) |
| 52 | + public String indicesNodes = "1|1"; |
| 53 | + public int numIndices; |
| 54 | + public int numNodes; |
| 55 | + public int numShards = 10; |
| 56 | + public int numReplicas = 1; |
| 57 | + |
| 58 | + private AllocationService allocationService; |
| 59 | + private ClusterState initialClusterState; |
| 60 | + |
| 61 | + @Setup |
| 62 | + public void setUp() throws Exception { |
| 63 | + LogConfigurator.setNodeName("test"); |
| 64 | + final String[] params = indicesNodes.split("\\|"); |
| 65 | + numIndices = toInt(params[0]); |
| 66 | + numNodes = toInt(params[1]); |
| 67 | + |
| 68 | + int totalShardCount = (numReplicas + 1) * numShards * numIndices; |
| 69 | + Metadata.Builder mb = Metadata.builder(); |
| 70 | + for (int i = 1; i <= numIndices; i++) { |
| 71 | + mb.put( |
| 72 | + IndexMetadata.builder("test_" + i) |
| 73 | + .settings(Settings.builder().put("index.version.created", Version.CURRENT)) |
| 74 | + .numberOfShards(numShards) |
| 75 | + .numberOfReplicas(numReplicas) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + Metadata metadata = mb.build(); |
| 80 | + RoutingTable.Builder rb = RoutingTable.builder(); |
| 81 | + for (int i = 1; i <= numIndices; i++) { |
| 82 | + rb.addAsNew(metadata.index("test_" + i)); |
| 83 | + } |
| 84 | + RoutingTable routingTable = rb.build(); |
| 85 | + initialClusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)) |
| 86 | + .metadata(metadata) |
| 87 | + .routingTable(routingTable) |
| 88 | + .nodes(setUpClusterNodes(numNodes)) |
| 89 | + .build(); |
| 90 | + } |
| 91 | + |
| 92 | + @Benchmark |
| 93 | + public ClusterState measureShardAllocationEmptyCluster() throws Exception { |
| 94 | + ClusterState clusterState = initialClusterState; |
| 95 | + allocationService = Allocators.createAllocationService( |
| 96 | + Settings.builder() |
| 97 | + .put("cluster.routing.allocation.awareness.attributes", "zone") |
| 98 | + .put("cluster.routing.allocation.load_awareness.provisioned_capacity", numNodes) |
| 99 | + .put("cluster.routing.allocation.load_awareness.skew_factor", "50") |
| 100 | + .put("cluster.routing.allocation.node_concurrent_recoveries", "2") |
| 101 | + .build() |
| 102 | + ); |
| 103 | + clusterState = allocationService.reroute(clusterState, "reroute"); |
| 104 | + while (clusterState.getRoutingNodes().hasUnassignedShards()) { |
| 105 | + clusterState = startInitializingShardsAndReroute(allocationService, clusterState); |
| 106 | + } |
| 107 | + return clusterState; |
| 108 | + } |
| 109 | + |
| 110 | + private int toInt(String v) { |
| 111 | + return Integer.valueOf(v.trim()); |
| 112 | + } |
| 113 | + |
| 114 | + private DiscoveryNodes.Builder setUpClusterNodes(int nodes) { |
| 115 | + DiscoveryNodes.Builder nb = DiscoveryNodes.builder(); |
| 116 | + for (int i = 1; i <= nodes; i++) { |
| 117 | + Map<String, String> attributes = new HashMap<>(); |
| 118 | + attributes.put("zone", "zone_" + (i % 3)); |
| 119 | + nb.add(Allocators.newNode("node_0_" + i, attributes)); |
| 120 | + } |
| 121 | + return nb; |
| 122 | + } |
| 123 | + |
| 124 | + private static ClusterState startInitializingShardsAndReroute(AllocationService allocationService, ClusterState clusterState) { |
| 125 | + return startShardsAndReroute(allocationService, clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); |
| 126 | + } |
| 127 | + |
| 128 | + private static ClusterState startShardsAndReroute( |
| 129 | + AllocationService allocationService, |
| 130 | + ClusterState clusterState, |
| 131 | + List<ShardRouting> initializingShards |
| 132 | + ) { |
| 133 | + return allocationService.reroute(allocationService.applyStartedShards(clusterState, initializingShards), "reroute after starting"); |
| 134 | + } |
| 135 | +} |
0 commit comments