17
17
import org .opensearch .common .settings .Settings ;
18
18
import org .opensearch .core .index .Index ;
19
19
import org .opensearch .index .IndexModule ;
20
- import org .opensearch .index .query .TermsQueryBuilder ;
21
20
import org .opensearch .indices .IndicesService ;
22
21
import org .opensearch .indices .replication .common .ReplicationType ;
23
22
import org .opensearch .test .OpenSearchIntegTestCase ;
24
23
25
24
import java .util .ArrayList ;
26
- import java .util .Arrays ;
27
25
import java .util .Collections ;
28
26
import java .util .List ;
29
27
import java .util .Locale ;
30
- import java .util .concurrent .TimeUnit ;
31
- import java .util .function .BiConsumer ;
32
28
33
29
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 ;
35
31
import static org .opensearch .indices .IndicesService .CLUSTER_SETTING_REPLICATION_TYPE ;
36
- import static org .opensearch .test .hamcrest .OpenSearchAssertions .assertHitCount ;
37
32
import static org .hamcrest .Matchers .hasSize ;
38
33
39
34
@ OpenSearchIntegTestCase .ClusterScope (scope = OpenSearchIntegTestCase .Scope .TEST , numDataNodes = 0 )
@@ -45,7 +40,7 @@ public class SegmentReplicationClusterSettingIT extends OpenSearchIntegTestCase
45
40
protected static final int REPLICA_COUNT = 1 ;
46
41
47
42
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];" ;
49
44
50
45
@ Override
51
46
public Settings indexSettings () {
@@ -133,36 +128,72 @@ public void testIndexReplicationSettingOverridesDocRepClusterSetting() throws Ex
133
128
assertEquals (indicesService .indexService (anotherIndex ).getIndexSettings ().isSegRepEnabled (), false );
134
129
}
135
130
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 ());
143
145
}
144
146
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 ());
163
177
}
164
178
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
+
166
197
// Define resize action and target shard count.
167
198
List <Tuple <ResizeType , Integer >> resizeActionsList = new ArrayList <>();
168
199
final int initialShardCount = 2 ;
@@ -172,46 +203,24 @@ public void testMismatchingReplicationType_ResizeAction_StrictMode() throws Thro
172
203
173
204
Tuple <ResizeType , Integer > resizeActionTuple = resizeActionsList .get (random ().nextInt (resizeActionsList .size ()));
174
205
final String targetIndexName = resizeActionTuple .v1 ().name ().toLowerCase (Locale .ROOT ) + "-target" ;
175
- final int documentCount = scaledRandomIntBetween (1 , 10 );
176
206
177
207
logger .info ("--> Performing resize action {} with shard count {}" , resizeActionTuple .v1 (), resizeActionTuple .v2 ());
178
208
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 );
199
215
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 ();
212
219
213
- // Test create index fails
214
- client ().admin ()
220
+ // Validate resize action fails
221
+ IllegalArgumentException exception = expectThrows (
222
+ IllegalArgumentException .class ,
223
+ () -> client ().admin ()
215
224
.indices ()
216
225
.prepareResizeIndex (INDEX_NAME , targetIndexName )
217
226
.setResizeType (resizeActionTuple .v1 ())
@@ -220,61 +229,12 @@ public void testMismatchingReplicationType_ResizeAction_StrictMode() throws Thro
220
229
.put ("index.number_of_replicas" , 0 )
221
230
.put ("index.number_of_shards" , resizeActionTuple .v2 ())
222
231
.putNull ("index.blocks.write" )
223
- .put (SETTING_REPLICATION_TYPE , replicationList . get ( 1 ) )
232
+ .put (SETTING_REPLICATION_TYPE , indexLevelReplication )
224
233
.build ()
225
234
)
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 ()
258
236
);
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 ());
278
238
}
279
239
280
240
/**
0 commit comments