77
88package org .elasticsearch .xpack .migrate .action ;
99
10- import org .elasticsearch .ElasticsearchException ;
1110import org .elasticsearch .ResourceNotFoundException ;
1211import org .elasticsearch .action .DocWriteRequest ;
1312import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
1615import org .elasticsearch .action .admin .indices .refresh .RefreshRequest ;
1716import org .elasticsearch .action .admin .indices .rollover .RolloverRequest ;
1817import org .elasticsearch .action .admin .indices .settings .get .GetSettingsRequest ;
19- import org .elasticsearch .action .admin .indices .settings .get .GetSettingsResponse ;
2018import org .elasticsearch .action .admin .indices .settings .put .UpdateSettingsRequest ;
2119import org .elasticsearch .action .admin .indices .template .delete .DeleteIndexTemplateRequest ;
2220import org .elasticsearch .action .admin .indices .template .delete .TransportDeleteIndexTemplateAction ;
2624import org .elasticsearch .action .index .IndexRequest ;
2725import org .elasticsearch .action .ingest .PutPipelineRequest ;
2826import org .elasticsearch .action .ingest .PutPipelineTransportAction ;
29- import org .elasticsearch .cluster .block .ClusterBlockException ;
3027import org .elasticsearch .cluster .metadata .ComposableIndexTemplate ;
3128import org .elasticsearch .cluster .metadata .IndexMetadata ;
3229import org .elasticsearch .cluster .metadata .MappingMetadata ;
33- import org .elasticsearch .cluster .metadata .MetadataIndexStateService ;
3430import org .elasticsearch .cluster .metadata .Template ;
3531import org .elasticsearch .common .bytes .BytesArray ;
3632import org .elasticsearch .common .compress .CompressedXContent ;
4945import org .elasticsearch .xcontent .XContentType ;
5046import org .elasticsearch .xpack .migrate .MigratePlugin ;
5147import org .elasticsearch .xpack .migrate .MigrateTemplateRegistry ;
52- import org .junit .After ;
5348import org .junit .Before ;
5449
5550import java .io .IOException ;
6459import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
6560import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertHitCount ;
6661import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertResponse ;
67- import static org .elasticsearch .xcontent .XContentFactory .jsonBuilder ;
6862import static org .hamcrest .Matchers .equalTo ;
6963
7064public class ReindexDatastreamIndexTransportActionIT extends ESIntegTestCase {
7165
72- private String sourceIndex ;
73-
7466 @ Before
7567 private void setup () throws Exception {
76- sourceIndex = null ;
7768 deletePipeline (MigrateTemplateRegistry .REINDEX_DATA_STREAM_PIPELINE_NAME );
7869 assertBusy (() -> { assertTrue (getPipelines (MigrateTemplateRegistry .REINDEX_DATA_STREAM_PIPELINE_NAME ).isFound ()); });
7970 }
8071
81- @ After
82- private void cleanup () {
83- if (sourceIndex != null ) {
84- cleanupMetadataBlocks (sourceIndex );
85- }
86- }
87-
8872 private static final String MAPPING = """
8973 {
9074 "_doc":{
@@ -121,7 +105,7 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
121105 """ ;
122106
123107 public void testTimestamp0AddedIfMissing () {
124- sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
108+ var sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
125109 safeGet (indicesAdmin ().create (new CreateIndexRequest (sourceIndex )));
126110
127111 // add doc without timestamp
@@ -146,7 +130,7 @@ public void testTimestamp0AddedIfMissing() {
146130
147131 public void testTimestampNotAddedIfExists () {
148132
149- sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
133+ var sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
150134 safeGet (indicesAdmin ().create (new CreateIndexRequest (sourceIndex )));
151135
152136 // add doc with timestamp
@@ -196,7 +180,7 @@ public void testCustomReindexPipeline() {
196180
197181 safeGet (clusterAdmin ().execute (PutPipelineTransportAction .TYPE , putRequest ));
198182
199- sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
183+ var sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
200184 safeGet (indicesAdmin ().create (new CreateIndexRequest (sourceIndex )));
201185
202186 // add doc with timestamp
@@ -223,7 +207,7 @@ public void testCustomReindexPipeline() {
223207
224208 public void testDestIndexDeletedIfExists () throws Exception {
225209 // empty source index
226- sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
210+ var sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
227211 safeGet (indicesAdmin ().create (new CreateIndexRequest (sourceIndex )));
228212
229213 // dest index with docs
@@ -242,7 +226,7 @@ public void testDestIndexDeletedIfExists() throws Exception {
242226 }
243227
244228 public void testDestIndexNameSet_noDotPrefix () throws Exception {
245- sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
229+ var sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
246230 safeGet (indicesAdmin ().create (new CreateIndexRequest (sourceIndex )));
247231
248232 // call reindex
@@ -255,7 +239,7 @@ public void testDestIndexNameSet_noDotPrefix() throws Exception {
255239 }
256240
257241 public void testDestIndexNameSet_withDotPrefix () {
258- sourceIndex = "." + randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
242+ var sourceIndex = "." + randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
259243 safeGet (indicesAdmin ().create (new CreateIndexRequest (sourceIndex )));
260244
261245 // call reindex
@@ -270,10 +254,16 @@ public void testDestIndexNameSet_withDotPrefix() {
270254 public void testDestIndexContainsDocs () {
271255 // source index with docs
272256 var numDocs = randomIntBetween (1 , 100 );
273- sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
257+ var sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
274258 safeGet (indicesAdmin ().create (new CreateIndexRequest (sourceIndex )));
275259 indexDocs (sourceIndex , numDocs );
276260
261+ var settings = Settings .builder ()
262+ .put (IndexMetadata .SETTING_BLOCKS_METADATA , randomBoolean ())
263+ .put (IndexMetadata .SETTING_READ_ONLY , randomBoolean ())
264+ .build ();
265+ safeGet (indicesAdmin ().updateSettings (new UpdateSettingsRequest (settings , sourceIndex )));
266+
277267 // call reindex
278268 var response = safeGet (
279269 client ().execute (ReindexDataStreamIndexAction .INSTANCE , new ReindexDataStreamIndexAction .Request (sourceIndex ))
@@ -284,29 +274,6 @@ public void testDestIndexContainsDocs() {
284274 assertHitCount (prepareSearch (response .getDestIndex ()).setSize (0 ), numDocs );
285275 }
286276
287- public void testSetSourceToReadOnly () throws Exception {
288- var settings = randomBoolean () ? Settings .builder ().put (IndexMetadata .SETTING_READ_ONLY , true ).build () : Settings .EMPTY ;
289-
290- // empty source index
291- sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
292- safeGet (indicesAdmin ().create (new CreateIndexRequest (sourceIndex , settings )));
293-
294- // call reindex
295- safeGet (client ().execute (ReindexDataStreamIndexAction .INSTANCE , new ReindexDataStreamIndexAction .Request (sourceIndex )));
296-
297- // Assert that source index is now read-only but not verified read-only
298- GetSettingsResponse getSettingsResponse = safeGet (admin ().indices ().getSettings (new GetSettingsRequest ().indices (sourceIndex )));
299- assertTrue (parseBoolean (getSettingsResponse .getSetting (sourceIndex , IndexMetadata .SETTING_READ_ONLY )));
300- assertFalse (
301- parseBoolean (getSettingsResponse .getSetting (sourceIndex , MetadataIndexStateService .VERIFIED_READ_ONLY_SETTING .getKey ()))
302- );
303-
304- // assert that write to source fails
305- var indexReq = new IndexRequest (sourceIndex ).source (jsonBuilder ().startObject ().field ("field" , "1" ).endObject ());
306- expectThrows (ClusterBlockException .class , client ().index (indexReq ));
307- assertHitCount (prepareSearch (sourceIndex ).setSize (0 ), 0 );
308- }
309-
310277 public void testMissingSourceIndex () {
311278 var nonExistentSourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
312279 expectThrows (
@@ -319,7 +286,7 @@ public void testSettingsAddedBeforeReindex() {
319286 // start with a static setting
320287 var numShards = randomIntBetween (1 , 10 );
321288 var staticSettings = Settings .builder ().put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , numShards ).build ();
322- sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
289+ var sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
323290 safeGet (indicesAdmin ().create (new CreateIndexRequest (sourceIndex , staticSettings )));
324291
325292 // update with a dynamic setting
@@ -344,7 +311,7 @@ public void testSettingsAddedBeforeReindex() {
344311 }
345312
346313 public void testMappingsAddedToDestIndex () {
347- sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
314+ var sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
348315 safeGet (indicesAdmin ().create (new CreateIndexRequest (sourceIndex ).mapping (MAPPING )));
349316
350317 // call reindex
@@ -364,32 +331,8 @@ public void testMappingsAddedToDestIndex() {
364331 assertEquals ("text" , XContentMapValues .extractValue ("properties.foo1.type" , destMappings ));
365332 }
366333
367- public void testFailIfMetadataBlockSet () {
368- sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
369- var settings = Settings .builder ().put (IndexMetadata .SETTING_BLOCKS_METADATA , true ).build ();
370- safeGet (indicesAdmin ().create (new CreateIndexRequest (sourceIndex , settings )));
371-
372- ElasticsearchException e = expectThrows (
373- ElasticsearchException .class ,
374- client ().execute (ReindexDataStreamIndexAction .INSTANCE , new ReindexDataStreamIndexAction .Request (sourceIndex ))
375- );
376- assertTrue (e .getMessage ().contains ("Cannot reindex index" ) || e .getCause ().getMessage ().equals ("Cannot reindex index" ));
377- }
378-
379- public void testFailIfReadBlockSet () {
380- sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
381- var settings = Settings .builder ().put (IndexMetadata .SETTING_BLOCKS_READ , true ).build ();
382- safeGet (indicesAdmin ().create (new CreateIndexRequest (sourceIndex , settings )));
383-
384- ElasticsearchException e = expectThrows (
385- ElasticsearchException .class ,
386- client ().execute (ReindexDataStreamIndexAction .INSTANCE , new ReindexDataStreamIndexAction .Request (sourceIndex ))
387- );
388- assertTrue (e .getMessage ().contains ("Cannot reindex index" ) || e .getCause ().getMessage ().equals ("Cannot reindex index" ));
389- }
390-
391334 public void testReadOnlyBlocksNotAddedBack () {
392- sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
335+ var sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
393336 var settings = Settings .builder ()
394337 .put (IndexMetadata .SETTING_READ_ONLY , randomBoolean ())
395338 .put (IndexMetadata .SETTING_READ_ONLY_ALLOW_DELETE , randomBoolean ())
@@ -419,7 +362,7 @@ public void testUpdateSettingsDefaultsRestored() {
419362 indicesAdmin ().execute (TransportDeleteIndexTemplateAction .TYPE , new DeleteIndexTemplateRequest ("random_index_template" ))
420363 );
421364
422- sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
365+ var sourceIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
423366 assertAcked (indicesAdmin ().create (new CreateIndexRequest (sourceIndex )));
424367
425368 // call reindex
@@ -458,7 +401,7 @@ public void testSettingsAndMappingsFromTemplate() throws IOException {
458401 request .indexTemplate (template );
459402 safeGet (client ().execute (TransportPutComposableIndexTemplateAction .TYPE , request ));
460403
461- sourceIndex = "logs-" + randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
404+ var sourceIndex = "logs-" + randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
462405 safeGet (indicesAdmin ().create (new CreateIndexRequest (sourceIndex )));
463406
464407 {
0 commit comments