|
8 | 8 |
|
9 | 9 | package org.opensearch.cluster.routing.allocation.decider;
|
10 | 10 |
|
| 11 | +import org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest; |
| 12 | +import org.opensearch.action.support.clustermanager.AcknowledgedResponse; |
11 | 13 | import org.opensearch.cluster.ClusterState;
|
12 | 14 | import org.opensearch.cluster.routing.IndexShardRoutingTable;
|
13 | 15 | import org.opensearch.cluster.routing.ShardRouting;
|
@@ -99,6 +101,75 @@ public void testIndexPrimaryShardLimit() throws Exception {
|
99 | 101 | });
|
100 | 102 | }
|
101 | 103 |
|
| 104 | + public void testUpdatingIndexPrimaryShardLimit() throws Exception { |
| 105 | + // Create first index with primary shard limit |
| 106 | + Settings firstIndexSettings = Settings.builder() |
| 107 | + .put(remoteStoreIndexSettings(0, 4)) // 4 shards, 0 replicas |
| 108 | + .put(INDEX_TOTAL_PRIMARY_SHARDS_PER_NODE_SETTING.getKey(), 1) |
| 109 | + .build(); |
| 110 | + |
| 111 | + // Create first index |
| 112 | + createIndex("test1", firstIndexSettings); |
| 113 | + |
| 114 | + // Update the index settings to set INDEX_TOTAL_PRIMARY_SHARDS_PER_NODE_SETTING |
| 115 | + UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest("test1"); |
| 116 | + Settings updatedSettings = Settings.builder().put(INDEX_TOTAL_PRIMARY_SHARDS_PER_NODE_SETTING.getKey(), 1).build(); |
| 117 | + updateSettingsRequest.settings(updatedSettings); |
| 118 | + |
| 119 | + AcknowledgedResponse response = client().admin().indices().updateSettings(updateSettingsRequest).actionGet(); |
| 120 | + |
| 121 | + assertTrue(response.isAcknowledged()); |
| 122 | + |
| 123 | + // Create second index |
| 124 | + createIndex("test2", remoteStoreIndexSettings(0, 4)); |
| 125 | + |
| 126 | + assertBusy(() -> { |
| 127 | + ClusterState state = client().admin().cluster().prepareState().get().getState(); |
| 128 | + |
| 129 | + // Check total number of shards (8 total: 4 from each index) |
| 130 | + assertEquals("Total shards should be 8", 8, state.getRoutingTable().allShards().size()); |
| 131 | + |
| 132 | + // Count assigned and unassigned shards for test1 |
| 133 | + int test1AssignedShards = 0; |
| 134 | + int test1UnassignedShards = 0; |
| 135 | + Map<String, Integer> nodePrimaryCount = new HashMap<>(); |
| 136 | + |
| 137 | + // Check test1 shard distribution |
| 138 | + for (IndexShardRoutingTable shardRouting : state.routingTable().index("test1")) { |
| 139 | + for (ShardRouting shard : shardRouting) { |
| 140 | + if (shard.assignedToNode()) { |
| 141 | + test1AssignedShards++; |
| 142 | + // Count primaries per node for test1 |
| 143 | + String nodeId = shard.currentNodeId(); |
| 144 | + nodePrimaryCount.merge(nodeId, 1, Integer::sum); |
| 145 | + } else { |
| 146 | + test1UnassignedShards++; |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // Check test2 shard assignment |
| 152 | + int test2UnassignedShards = 0; |
| 153 | + for (IndexShardRoutingTable shardRouting : state.routingTable().index("test2")) { |
| 154 | + for (ShardRouting shard : shardRouting) { |
| 155 | + if (!shard.assignedToNode()) { |
| 156 | + test2UnassignedShards++; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + // Assertions |
| 162 | + assertEquals("test1 should have 3 assigned shards", 3, test1AssignedShards); |
| 163 | + assertEquals("test1 should have 1 unassigned shard", 1, test1UnassignedShards); |
| 164 | + assertEquals("test2 should have no unassigned shards", 0, test2UnassignedShards); |
| 165 | + |
| 166 | + // Verify no node has more than one primary shard of test1 |
| 167 | + for (Integer count : nodePrimaryCount.values()) { |
| 168 | + assertTrue("No node should have more than 1 primary shard of test1", count <= 1); |
| 169 | + } |
| 170 | + }); |
| 171 | + } |
| 172 | + |
102 | 173 | public void testClusterPrimaryShardLimitss() throws Exception {
|
103 | 174 | // Update cluster setting to limit primary shards per node
|
104 | 175 | updateClusterSetting(CLUSTER_TOTAL_PRIMARY_SHARDS_PER_NODE_SETTING.getKey(), 1);
|
|
0 commit comments