Skip to content

Commit 81d63ba

Browse files
committed
Address review comments
Signed-off-by: Suraj Singh <[email protected]>
1 parent 39d47be commit 81d63ba

File tree

7 files changed

+96
-136
lines changed

7 files changed

+96
-136
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
122122
- [BWC and API enforcement] Introduce checks for enforcing the API restrictions ([#11175](https://github.com/opensearch-project/OpenSearch/pull/11175))
123123
- Create separate transport action for render search template action ([#11170](https://github.com/opensearch-project/OpenSearch/pull/11170))
124124
- Add additional handling in SearchTemplateRequest when simulate is set to true ([#11591](https://github.com/opensearch-project/OpenSearch/pull/11591))
125-
- Introduce cluster level setting `cluster.force.index.replication.type` to prevent replication type setting override during index creations([#11583](https://github.com/opensearch-project/OpenSearch/pull/11583))
125+
- Introduce cluster level setting `cluster.index.restrict.replication.type` to prevent replication type setting override during index creations([#11583](https://github.com/opensearch-project/OpenSearch/pull/11583))
126126

127127
### Dependencies
128128
- Bump Lucene from 9.7.0 to 9.8.0 ([10276](https://github.com/opensearch-project/OpenSearch/pull/10276))

server/src/internalClusterTest/java/org/opensearch/indices/replication/SegmentReplicationClusterSettingIT.java

Lines changed: 80 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,18 @@
1717
import org.opensearch.common.settings.Settings;
1818
import org.opensearch.core.index.Index;
1919
import org.opensearch.index.IndexModule;
20-
import org.opensearch.index.query.TermsQueryBuilder;
2120
import org.opensearch.indices.IndicesService;
2221
import org.opensearch.indices.replication.common.ReplicationType;
2322
import org.opensearch.test.OpenSearchIntegTestCase;
2423

2524
import java.util.ArrayList;
26-
import java.util.Arrays;
2725
import java.util.Collections;
2826
import java.util.List;
2927
import java.util.Locale;
30-
import java.util.concurrent.TimeUnit;
31-
import java.util.function.BiConsumer;
3228

3329
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REPLICATION_TYPE;
34-
import static org.opensearch.indices.IndicesService.CLUSTER_FORCE_INDEX_REPLICATION_TYPE_SETTING;
30+
import static org.opensearch.indices.IndicesService.CLUSTER_INDEX_RESTRICT_REPLICATION_TYPE_SETTING;
3531
import static org.opensearch.indices.IndicesService.CLUSTER_SETTING_REPLICATION_TYPE;
36-
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount;
3732
import static org.hamcrest.Matchers.hasSize;
3833

3934
@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0)
@@ -45,7 +40,7 @@ public class SegmentReplicationClusterSettingIT extends OpenSearchIntegTestCase
4540
protected static final int REPLICA_COUNT = 1;
4641

4742
protected static final String REPLICATION_MISMATCH_VALIDATION_ERROR =
48-
"Validation Failed: 1: index setting [index.replication.type] is not allowed to be set as [cluster.force.index.replication.type=true];";
43+
"Validation Failed: 1: index setting [index.replication.type] is not allowed to be set as [cluster.index.restrict.replication.type=true];";
4944

5045
@Override
5146
public Settings indexSettings() {
@@ -133,36 +128,72 @@ public void testIndexReplicationSettingOverridesDocRepClusterSetting() throws Ex
133128
assertEquals(indicesService.indexService(anotherIndex).getIndexSettings().isSegRepEnabled(), false);
134129
}
135130

136-
public void testDifferentReplicationTypes_CreateIndex_StrictMode() throws Throwable {
137-
final int documentCount = scaledRandomIntBetween(1, 10);
138-
BiConsumer<List<ReplicationType>, List<String>> consumer = (replicationList, dataNodesList) -> {
139-
Settings indexSettings = Settings.builder().put(indexSettings()).put(SETTING_REPLICATION_TYPE, replicationList.get(1)).build();
140-
createIndex(INDEX_NAME, indexSettings);
141-
};
142-
executeTest(true, consumer, INDEX_NAME, documentCount);
131+
public void testReplicationTypesOverrideNotAllowed_IndexAPI() {
132+
// Generate mutually exclusive replication strategies at cluster and index level
133+
List<ReplicationType> replicationStrategies = getRandomReplicationTypesAsList();
134+
ReplicationType clusterLevelReplication = replicationStrategies.get(0);
135+
ReplicationType indexLevelReplication = replicationStrategies.get(1);
136+
Settings nodeSettings = Settings.builder()
137+
.put(CLUSTER_SETTING_REPLICATION_TYPE, clusterLevelReplication)
138+
.put(CLUSTER_INDEX_RESTRICT_REPLICATION_TYPE_SETTING.getKey(), true)
139+
.build();
140+
internalCluster().startClusterManagerOnlyNode(nodeSettings);
141+
internalCluster().startDataOnlyNode(nodeSettings);
142+
Settings indexSettings = Settings.builder().put(indexSettings()).put(SETTING_REPLICATION_TYPE, indexLevelReplication).build();
143+
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> createIndex(INDEX_NAME, indexSettings));
144+
assertEquals(REPLICATION_MISMATCH_VALIDATION_ERROR, exception.getMessage());
143145
}
144146

145-
public void testDifferentReplicationTypes_IndexTemplates_StrictMode() throws Throwable {
146-
final int documentCount = scaledRandomIntBetween(1, 10);
147-
148-
BiConsumer<List<ReplicationType>, List<String>> consumer = (replicationList, dataNodesList) -> {
149-
client().admin()
150-
.indices()
151-
.preparePutTemplate("template_1")
152-
.setPatterns(Collections.singletonList("test-idx*"))
153-
.setSettings(Settings.builder().put(indexSettings()).put(SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT).build())
154-
.setOrder(0)
155-
.get();
156-
157-
GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get();
158-
assertThat(response.getIndexTemplates(), hasSize(1));
159-
160-
createIndex(INDEX_NAME);
161-
};
162-
executeTest(true, consumer, INDEX_NAME, documentCount);
147+
public void testReplicationTypesOverrideNotAllowed_WithTemplates() {
148+
// Generate mutually exclusive replication strategies at cluster and index level
149+
List<ReplicationType> replicationStrategies = getRandomReplicationTypesAsList();
150+
ReplicationType clusterLevelReplication = replicationStrategies.get(0);
151+
ReplicationType indexLevelReplication = replicationStrategies.get(1);
152+
Settings nodeSettings = Settings.builder()
153+
.put(CLUSTER_SETTING_REPLICATION_TYPE, clusterLevelReplication)
154+
.put(CLUSTER_INDEX_RESTRICT_REPLICATION_TYPE_SETTING.getKey(), true)
155+
.build();
156+
internalCluster().startClusterManagerOnlyNode(nodeSettings);
157+
internalCluster().startDataOnlyNode(nodeSettings);
158+
internalCluster().startDataOnlyNode(nodeSettings);
159+
logger.info(
160+
"--> Create index with index level replication {} and cluster level replication {}",
161+
indexLevelReplication,
162+
clusterLevelReplication
163+
);
164+
// Create index template
165+
client().admin()
166+
.indices()
167+
.preparePutTemplate("template_1")
168+
.setPatterns(Collections.singletonList("test-idx*"))
169+
.setSettings(Settings.builder().put(indexSettings()).put(SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT).build())
170+
.setOrder(0)
171+
.get();
172+
173+
GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get();
174+
assertThat(response.getIndexTemplates(), hasSize(1));
175+
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> createIndex(INDEX_NAME));
176+
assertEquals(REPLICATION_MISMATCH_VALIDATION_ERROR, exception.getMessage());
163177
}
164178

165-
public void testMismatchingReplicationType_ResizeAction_StrictMode() throws Throwable {
179+
public void testReplicationTypesOverrideNotAllowed_WithResizeAction() {
180+
// Generate mutually exclusive replication strategies at cluster and index level
181+
List<ReplicationType> replicationStrategies = getRandomReplicationTypesAsList();
182+
ReplicationType clusterLevelReplication = replicationStrategies.get(0);
183+
ReplicationType indexLevelReplication = replicationStrategies.get(1);
184+
Settings nodeSettings = Settings.builder()
185+
.put(CLUSTER_SETTING_REPLICATION_TYPE, clusterLevelReplication)
186+
.put(CLUSTER_INDEX_RESTRICT_REPLICATION_TYPE_SETTING.getKey(), true)
187+
.build();
188+
internalCluster().startClusterManagerOnlyNode(nodeSettings);
189+
internalCluster().startDataOnlyNode(nodeSettings);
190+
internalCluster().startDataOnlyNode(nodeSettings);
191+
logger.info(
192+
"--> Create index with index level replication {} and cluster level replication {}",
193+
indexLevelReplication,
194+
clusterLevelReplication
195+
);
196+
166197
// Define resize action and target shard count.
167198
List<Tuple<ResizeType, Integer>> resizeActionsList = new ArrayList<>();
168199
final int initialShardCount = 2;
@@ -172,46 +203,24 @@ public void testMismatchingReplicationType_ResizeAction_StrictMode() throws Thro
172203

173204
Tuple<ResizeType, Integer> resizeActionTuple = resizeActionsList.get(random().nextInt(resizeActionsList.size()));
174205
final String targetIndexName = resizeActionTuple.v1().name().toLowerCase(Locale.ROOT) + "-target";
175-
final int documentCount = scaledRandomIntBetween(1, 10);
176206

177207
logger.info("--> Performing resize action {} with shard count {}", resizeActionTuple.v1(), resizeActionTuple.v2());
178208

179-
// Create an index using current cluster level settings
180-
BiConsumer<List<ReplicationType>, List<String>> consumer = (replicationList, dataNodesList) -> {
181-
Settings indexSettings = Settings.builder()
182-
.put(indexSettings())
183-
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, initialShardCount)
184-
.put(SETTING_REPLICATION_TYPE, replicationList.get(0))
185-
.build();
186-
createIndex(INDEX_NAME, indexSettings);
187-
188-
for (int i = 0; i < documentCount; i++) {
189-
client().prepareIndex(INDEX_NAME).setId(String.valueOf(i)).setSource("foo", "bar").get();
190-
}
191-
192-
// Block writes
193-
client().admin()
194-
.indices()
195-
.prepareUpdateSettings(INDEX_NAME)
196-
.setSettings(Settings.builder().put("index.blocks.write", true))
197-
.get();
198-
ensureGreen();
209+
Settings indexSettings = Settings.builder()
210+
.put(indexSettings())
211+
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, initialShardCount)
212+
.put(SETTING_REPLICATION_TYPE, clusterLevelReplication)
213+
.build();
214+
createIndex(INDEX_NAME, indexSettings);
199215

200-
try {
201-
for (String node : dataNodesList) {
202-
assertBusy(() -> {
203-
assertHitCount(
204-
client(node).prepareSearch(INDEX_NAME).setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(),
205-
documentCount
206-
);
207-
}, 30, TimeUnit.SECONDS);
208-
}
209-
} catch (Exception e) {
210-
throw new RuntimeException(e);
211-
}
216+
// Block writes
217+
client().admin().indices().prepareUpdateSettings(INDEX_NAME).setSettings(Settings.builder().put("index.blocks.write", true)).get();
218+
ensureGreen();
212219

213-
// Test create index fails
214-
client().admin()
220+
// Validate resize action fails
221+
IllegalArgumentException exception = expectThrows(
222+
IllegalArgumentException.class,
223+
() -> client().admin()
215224
.indices()
216225
.prepareResizeIndex(INDEX_NAME, targetIndexName)
217226
.setResizeType(resizeActionTuple.v1())
@@ -220,61 +229,12 @@ public void testMismatchingReplicationType_ResizeAction_StrictMode() throws Thro
220229
.put("index.number_of_replicas", 0)
221230
.put("index.number_of_shards", resizeActionTuple.v2())
222231
.putNull("index.blocks.write")
223-
.put(SETTING_REPLICATION_TYPE, replicationList.get(1))
232+
.put(SETTING_REPLICATION_TYPE, indexLevelReplication)
224233
.build()
225234
)
226-
.get();
227-
};
228-
executeTest(true, consumer, targetIndexName, documentCount);
229-
}
230-
231-
// Creates a cluster with mis-matching cluster level and index level replication strategies and validates that index
232-
// creation fails when cluster level setting `cluster.force.index.replication.type` is set to true and creation goes
233-
// through when it is false.
234-
private void executeTest(
235-
boolean restrictIndexLevelReplicationTypeSetting,
236-
BiConsumer<List<ReplicationType>, List<String>> createIndexRunnable,
237-
String targetIndexName,
238-
int documentCount
239-
) throws Throwable {
240-
// Generate mutually exclusive replication strategies at cluster and index level
241-
List<ReplicationType> replicationStrategies = getRandomReplicationTypesAsList();
242-
ReplicationType clusterLevelReplication = replicationStrategies.get(0);
243-
ReplicationType indexLevelReplication = replicationStrategies.get(1);
244-
245-
Settings settings = Settings.builder()
246-
.put(CLUSTER_SETTING_REPLICATION_TYPE, clusterLevelReplication)
247-
.put(CLUSTER_FORCE_INDEX_REPLICATION_TYPE_SETTING.getKey(), restrictIndexLevelReplicationTypeSetting)
248-
.build();
249-
internalCluster().startClusterManagerOnlyNode(settings);
250-
final String dataNodeOne = internalCluster().startDataOnlyNode(settings);
251-
final String dataNodeTwo = internalCluster().startDataOnlyNode(settings);
252-
List<String> dataNodes = Arrays.asList(dataNodeOne, dataNodeTwo);
253-
254-
logger.info(
255-
"--> Create index with cluster level setting {} and index level replication setting {}",
256-
clusterLevelReplication,
257-
indexLevelReplication
235+
.get()
258236
);
259-
260-
if (restrictIndexLevelReplicationTypeSetting) {
261-
try {
262-
createIndexRunnable.accept(replicationStrategies, dataNodes);
263-
} catch (IllegalArgumentException exception) {
264-
assertEquals(REPLICATION_MISMATCH_VALIDATION_ERROR, exception.getMessage());
265-
}
266-
} else {
267-
createIndexRunnable.accept(replicationStrategies, dataNodes);
268-
ensureGreen(targetIndexName);
269-
for (String node : dataNodes) {
270-
assertBusy(() -> {
271-
assertHitCount(
272-
client(node).prepareSearch(targetIndexName).setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(),
273-
documentCount
274-
);
275-
});
276-
}
277-
}
237+
assertEquals(REPLICATION_MISMATCH_VALIDATION_ERROR, exception.getMessage());
278238
}
279239

280240
/**

server/src/internalClusterTest/java/org/opensearch/snapshots/SegmentReplicationSnapshotIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import java.util.concurrent.TimeUnit;
3232

3333
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REPLICATION_TYPE;
34-
import static org.opensearch.indices.IndicesService.CLUSTER_FORCE_INDEX_REPLICATION_TYPE_SETTING;
34+
import static org.opensearch.indices.IndicesService.CLUSTER_INDEX_RESTRICT_REPLICATION_TYPE_SETTING;
3535
import static org.opensearch.indices.IndicesService.CLUSTER_SETTING_REPLICATION_TYPE;
3636
import static org.opensearch.indices.replication.SegmentReplicationBaseIT.waitForSearchableDocs;
3737
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
@@ -49,7 +49,7 @@ public class SegmentReplicationSnapshotIT extends AbstractSnapshotIntegTestCase
4949
private static final String SNAPSHOT_NAME = "test-segrep-snapshot";
5050

5151
protected static final String REPLICATION_MISMATCH_VALIDATION_ERROR =
52-
"Validation Failed: 1: index setting [index.replication.type] is not allowed to be set as [cluster.force.index.replication.type=true];";
52+
"Validation Failed: 1: index setting [index.replication.type] is not allowed to be set as [cluster.index.restrict.replication.type=true];";
5353

5454
public Settings segRepEnableIndexSettings() {
5555
return getShardSettings().put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT).build();
@@ -314,7 +314,7 @@ public void testSnapshotRestoreOnIndexWithSegRepClusterSetting() throws Exceptio
314314
/**
315315
* 1. Create index in DOCUMENT replication type
316316
* 2. Snapshot index
317-
* 3. Add new set of nodes with `cluster.indices.replication.strategy` set to SEGMENT and `cluster.force.index.replication.type`
317+
* 3. Add new set of nodes with `cluster.indices.replication.strategy` set to SEGMENT and `cluster.index.restrict.replication.type`
318318
* set to true.
319319
* 4. Perform restore on new set of nodes to validate restored index has `DOCUMENT` replication.
320320
*/
@@ -349,7 +349,7 @@ public void testSnapshotRestoreOnRestrictReplicationSetting() throws Exception {
349349
// Start new set of nodes with cluster level replication type setting and restrict replication type setting.
350350
Settings settings = Settings.builder()
351351
.put(CLUSTER_SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT)
352-
.put(CLUSTER_FORCE_INDEX_REPLICATION_TYPE_SETTING.getKey(), true)
352+
.put(CLUSTER_INDEX_RESTRICT_REPLICATION_TYPE_SETTING.getKey(), true)
353353
.build();
354354

355355
// Start new cluster manager node

server/src/main/java/org/opensearch/cluster/metadata/MetadataCreateIndexService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,19 +1332,19 @@ private static List<String> validateIndexCustomPath(Settings settings, @Nullable
13321332

13331333
/**
13341334
* Validates {@code index.replication.type} is matches with cluster level setting {@code cluster.indices.replication.strategy}
1335-
* when {@code cluster.force.index.replication.type} is set to true.
1335+
* when {@code cluster.index.restrict.replication.type} is set to true.
13361336
*
13371337
* @param requestSettings settings passed in during index create request
13381338
* @param clusterSettings cluster setting
13391339
*/
13401340
private static Optional<String> validateIndexReplicationTypeSettings(Settings requestSettings, ClusterSettings clusterSettings) {
1341-
if (clusterSettings.get(IndicesService.CLUSTER_FORCE_INDEX_REPLICATION_TYPE_SETTING)
1341+
if (clusterSettings.get(IndicesService.CLUSTER_INDEX_RESTRICT_REPLICATION_TYPE_SETTING)
13421342
&& requestSettings.hasValue(SETTING_REPLICATION_TYPE)
13431343
&& requestSettings.get(INDEX_REPLICATION_TYPE_SETTING.getKey())
13441344
.equals(clusterSettings.get(CLUSTER_REPLICATION_TYPE_SETTING).name()) == false) {
13451345
return Optional.of(
13461346
"index setting [index.replication.type] is not allowed to be set as ["
1347-
+ IndicesService.CLUSTER_FORCE_INDEX_REPLICATION_TYPE_SETTING.getKey()
1347+
+ IndicesService.CLUSTER_INDEX_RESTRICT_REPLICATION_TYPE_SETTING.getKey()
13481348
+ "=true]"
13491349
);
13501350
}

server/src/main/java/org/opensearch/common/settings/ClusterSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ public void apply(Settings value, Settings current, Settings previous) {
702702
CpuBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE,
703703
CpuBasedAdmissionControllerSettings.INDEXING_CPU_USAGE_LIMIT,
704704
CpuBasedAdmissionControllerSettings.SEARCH_CPU_USAGE_LIMIT,
705-
IndicesService.CLUSTER_FORCE_INDEX_REPLICATION_TYPE_SETTING
705+
IndicesService.CLUSTER_INDEX_RESTRICT_REPLICATION_TYPE_SETTING
706706
)
707707
)
708708
);

server/src/main/java/org/opensearch/indices/IndicesService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ public class IndicesService extends AbstractLifecycleComponent
307307
* does not match the cluster setting. If disabled, a user can choose a replication type on a per-index basis using
308308
* the index.replication.type setting.
309309
*/
310-
public static final Setting<Boolean> CLUSTER_FORCE_INDEX_REPLICATION_TYPE_SETTING = Setting.boolSetting(
311-
"cluster.force.index.replication.type",
310+
public static final Setting<Boolean> CLUSTER_INDEX_RESTRICT_REPLICATION_TYPE_SETTING = Setting.boolSetting(
311+
"cluster.index.restrict.replication.type",
312312
false,
313313
Property.NodeScope,
314314
Property.Final

0 commit comments

Comments
 (0)