|
41 | 41 | import org.opensearch.cluster.node.DiscoveryNodeRole;
|
42 | 42 | import org.opensearch.cluster.routing.allocation.decider.AwarenessAllocationDecider;
|
43 | 43 | import org.opensearch.cluster.service.ClusterService;
|
| 44 | +import org.opensearch.common.SuppressForbidden; |
44 | 45 | import org.opensearch.common.settings.ClusterSettings;
|
45 | 46 | import org.opensearch.common.settings.Settings;
|
46 | 47 | import org.opensearch.common.unit.TimeValue;
|
| 48 | +import org.opensearch.common.util.FeatureFlags; |
47 | 49 | import org.opensearch.common.util.io.IOUtils;
|
48 | 50 | import org.opensearch.core.index.Index;
|
49 | 51 | import org.opensearch.core.index.shard.ShardId;
|
@@ -1054,6 +1056,68 @@ public void testSearchableSnapshotPrimaryDefault() throws Exception {
|
1054 | 1056 | }
|
1055 | 1057 | }
|
1056 | 1058 |
|
| 1059 | + @SuppressForbidden(reason = "feature flag overrides") |
| 1060 | + public void testPartialIndexPrimaryDefault() throws Exception { |
| 1061 | + System.setProperty(FeatureFlags.TIERED_REMOTE_INDEX, "true"); |
| 1062 | + final int numIndices = 1; |
| 1063 | + final int numShards = 2; |
| 1064 | + final int numReplicas = 2; |
| 1065 | + final String[] indexNames = new String[numIndices]; |
| 1066 | + for (int i = 0; i < numIndices; i++) { |
| 1067 | + indexNames[i] = "test" + i; |
| 1068 | + } |
| 1069 | + // The first index is a partial index |
| 1070 | + final String indexName = indexNames[0]; |
| 1071 | + ClusterService clusterService = null; |
| 1072 | + ThreadPool threadPool = null; |
| 1073 | + |
| 1074 | + try { |
| 1075 | + OperationRouting opRouting = new OperationRouting( |
| 1076 | + Settings.EMPTY, |
| 1077 | + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS) |
| 1078 | + ); |
| 1079 | + |
| 1080 | + ClusterState state = ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(indexNames, numShards, numReplicas); |
| 1081 | + threadPool = new TestThreadPool("testPartialIndexPrimaryDefault"); |
| 1082 | + clusterService = ClusterServiceUtils.createClusterService(threadPool); |
| 1083 | + |
| 1084 | + // Update the index config within the cluster state to modify the index to a partial index |
| 1085 | + IndexMetadata partialIndexMetadata = IndexMetadata.builder(indexName) |
| 1086 | + .settings( |
| 1087 | + Settings.builder() |
| 1088 | + .put(state.metadata().index(indexName).getSettings()) |
| 1089 | + .put(IndexModule.INDEX_STORE_LOCALITY_SETTING.getKey(), IndexModule.DataLocalityType.PARTIAL) |
| 1090 | + .build() |
| 1091 | + ) |
| 1092 | + .build(); |
| 1093 | + Metadata.Builder metadataBuilder = Metadata.builder(state.metadata()) |
| 1094 | + .put(partialIndexMetadata, false) |
| 1095 | + .generateClusterUuidIfNeeded(); |
| 1096 | + state = ClusterState.builder(state).metadata(metadataBuilder.build()).build(); |
| 1097 | + |
| 1098 | + // Verify default preference is primary only |
| 1099 | + GroupShardsIterator<ShardIterator> groupIterator = opRouting.searchShards(state, indexNames, null, null); |
| 1100 | + assertThat("One group per index shard", groupIterator.size(), equalTo(numIndices * numShards)); |
| 1101 | + |
| 1102 | + for (ShardIterator shardIterator : groupIterator) { |
| 1103 | + assertTrue("Only primary should exist with no preference", shardIterator.nextOrNull().primary()); |
| 1104 | + } |
| 1105 | + |
| 1106 | + // Verify alternative preference can be applied to a partial index |
| 1107 | + groupIterator = opRouting.searchShards(state, indexNames, null, "_replica"); |
| 1108 | + assertThat("One group per index shard", groupIterator.size(), equalTo(numIndices * numShards)); |
| 1109 | + |
| 1110 | + for (ShardIterator shardIterator : groupIterator) { |
| 1111 | + assertThat("Replica shards will be returned", shardIterator.size(), equalTo(numReplicas)); |
| 1112 | + assertFalse("Returned shard should be a replica", shardIterator.nextOrNull().primary()); |
| 1113 | + } |
| 1114 | + } finally { |
| 1115 | + IOUtils.close(clusterService); |
| 1116 | + terminate(threadPool); |
| 1117 | + System.setProperty(FeatureFlags.TIERED_REMOTE_INDEX, "false"); |
| 1118 | + } |
| 1119 | + } |
| 1120 | + |
1057 | 1121 | private DiscoveryNode[] setupNodes() {
|
1058 | 1122 | // Sets up two data nodes in zone-a and one data node in zone-b
|
1059 | 1123 | List<String> zones = Arrays.asList("a", "a", "b");
|
|
0 commit comments