1919import org .elasticsearch .common .xcontent .XContentBuilder ;
2020import org .elasticsearch .common .xcontent .XContentHelper ;
2121import org .elasticsearch .common .xcontent .XContentType ;
22+ import org .elasticsearch .common .xcontent .json .JsonXContent ;
2223import org .elasticsearch .index .IndexSettings ;
2324import org .elasticsearch .index .engine .FrozenEngine ;
2425import org .elasticsearch .test .rest .ESRestTestCase ;
5859import static org .hamcrest .Matchers .equalTo ;
5960import static org .hamcrest .Matchers .greaterThan ;
6061import static org .hamcrest .Matchers .not ;
62+ import static org .hamcrest .Matchers .startsWith ;
6163
6264public class TimeSeriesLifecycleActionsIT extends ESRestTestCase {
6365 private String index ;
@@ -357,6 +359,46 @@ public void testDeleteOnlyShouldNotMakeIndexReadonly() throws Exception {
357359 indexDocument ();
358360 }
359361
362+ public void testDeleteDuringSnapshot () throws Exception {
363+ // Create the repository before taking the snapshot.
364+ Request request = new Request ("PUT" , "/_snapshot/repo" );
365+ request .setJsonEntity (Strings
366+ .toString (JsonXContent .contentBuilder ()
367+ .startObject ()
368+ .field ("type" , "fs" )
369+ .startObject ("settings" )
370+ .field ("compress" , randomBoolean ())
371+ .field ("location" , System .getProperty ("tests.path.repo" ))
372+ .field ("max_snapshot_bytes_per_sec" , "256b" )
373+ .endObject ()
374+ .endObject ()));
375+ assertOK (client ().performRequest (request ));
376+ // create delete policy
377+ createNewSingletonPolicy ("delete" , new DeleteAction (), TimeValue .timeValueMillis (0 ));
378+ // create index without policy
379+ createIndexWithSettings (index , Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
380+ .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ));
381+ // index document so snapshot actually does something
382+ indexDocument ();
383+ // start snapshot
384+ request = new Request ("PUT" , "/_snapshot/repo/snapshot" );
385+ request .addParameter ("wait_for_completion" , "false" );
386+ request .setJsonEntity ("{\" indices\" : \" " + index + "\" }" );
387+ assertOK (client ().performRequest (request ));
388+ // add policy and expect it to trigger delete immediately (while snapshot in progress)
389+ updatePolicy (index , policy );
390+ // assert that exception was thrown
391+ assertBusy (() -> {
392+ assertThat (getStepKeyForIndex (index ).getName (), equalTo ("ERROR" ));
393+ assertThat (getReasonForIndex (index ), startsWith ("Cannot delete indices that are being snapshotted" ));
394+ });
395+ assertThat (getSnapshotState ("snapshot" ), equalTo ("IN_PROGRESS" ));
396+ assertOK (client ().performRequest (new Request ("DELETE" , "/_snapshot/repo/snapshot" )));
397+ ResponseException e = expectThrows (ResponseException .class ,
398+ () -> client ().performRequest (new Request ("GET" , "/_snapshot/repo/snapshot" )));
399+ assertThat (e .getResponse ().getStatusLine ().getStatusCode (), equalTo (404 ));
400+ }
401+
360402 public void testReadOnly () throws Exception {
361403 createIndexWithSettings (index , Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
362404 .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ));
@@ -426,6 +468,46 @@ public void testShrinkAction() throws Exception {
426468 expectThrows (ResponseException .class , this ::indexDocument );
427469 }
428470
471+ public void testShrinkDuringSnapshot () throws Exception {
472+ // Create the repository before taking the snapshot.
473+ Request request = new Request ("PUT" , "/_snapshot/repo" );
474+ request .setJsonEntity (Strings
475+ .toString (JsonXContent .contentBuilder ()
476+ .startObject ()
477+ .field ("type" , "fs" )
478+ .startObject ("settings" )
479+ .field ("compress" , randomBoolean ())
480+ .field ("location" , System .getProperty ("tests.path.repo" ))
481+ .field ("max_snapshot_bytes_per_sec" , "256b" )
482+ .endObject ()
483+ .endObject ()));
484+ assertOK (client ().performRequest (request ));
485+ // create delete policy
486+ createNewSingletonPolicy ("warm" , new ShrinkAction (1 ), TimeValue .timeValueMillis (0 ));
487+ // create index without policy
488+ createIndexWithSettings (index , Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 2 )
489+ .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ));
490+ // index document so snapshot actually does something
491+ indexDocument ();
492+ // start snapshot
493+ request = new Request ("PUT" , "/_snapshot/repo/snapshot" );
494+ request .addParameter ("wait_for_completion" , "false" );
495+ request .setJsonEntity ("{\" indices\" : \" " + index + "\" }" );
496+ assertOK (client ().performRequest (request ));
497+ // add policy and expect it to trigger delete immediately (while snapshot in progress)
498+ updatePolicy (index , policy );
499+ // assert that exception was thrown
500+ assertBusy (() -> {
501+ assertThat (getStepKeyForIndex (index ).getName (), equalTo ("ERROR" ));
502+ assertThat (getReasonForIndex (index ), startsWith ("Cannot delete indices that are being snapshotted" ));
503+ });
504+ assertThat (getSnapshotState ("snapshot" ), equalTo ("IN_PROGRESS" ));
505+ assertOK (client ().performRequest (new Request ("DELETE" , "/_snapshot/repo/snapshot" )));
506+ ResponseException e = expectThrows (ResponseException .class ,
507+ () -> client ().performRequest (new Request ("GET" , "/_snapshot/repo/snapshot" )));
508+ assertThat (e .getResponse ().getStatusLine ().getStatusCode (), equalTo (404 ));
509+ }
510+
429511 public void testFreezeAction () throws Exception {
430512 createIndexWithSettings (index , Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
431513 .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ));
@@ -440,6 +522,46 @@ public void testFreezeAction() throws Exception {
440522 });
441523 }
442524
525+ public void testFreezeDuringSnapshot () throws Exception {
526+ // Create the repository before taking the snapshot.
527+ Request request = new Request ("PUT" , "/_snapshot/repo" );
528+ request .setJsonEntity (Strings
529+ .toString (JsonXContent .contentBuilder ()
530+ .startObject ()
531+ .field ("type" , "fs" )
532+ .startObject ("settings" )
533+ .field ("compress" , randomBoolean ())
534+ .field ("location" , System .getProperty ("tests.path.repo" ))
535+ .field ("max_snapshot_bytes_per_sec" , "256b" )
536+ .endObject ()
537+ .endObject ()));
538+ assertOK (client ().performRequest (request ));
539+ // create delete policy
540+ createNewSingletonPolicy ("cold" , new FreezeAction (), TimeValue .timeValueMillis (0 ));
541+ // create index without policy
542+ createIndexWithSettings (index , Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
543+ .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ));
544+ // index document so snapshot actually does something
545+ indexDocument ();
546+ // start snapshot
547+ request = new Request ("PUT" , "/_snapshot/repo/snapshot" );
548+ request .addParameter ("wait_for_completion" , "false" );
549+ request .setJsonEntity ("{\" indices\" : \" " + index + "\" }" );
550+ assertOK (client ().performRequest (request ));
551+ // add policy and expect it to trigger delete immediately (while snapshot in progress)
552+ updatePolicy (index , policy );
553+ // assert that exception was thrown
554+ assertBusy (() -> {
555+ assertThat (getStepKeyForIndex (index ).getName (), equalTo ("ERROR" ));
556+ assertThat (getReasonForIndex (index ), startsWith ("Cannot close indices that are being snapshotted" ));
557+ });
558+ assertThat (getSnapshotState ("snapshot" ), equalTo ("IN_PROGRESS" ));
559+ assertOK (client ().performRequest (new Request ("DELETE" , "/_snapshot/repo/snapshot" )));
560+ ResponseException e = expectThrows (ResponseException .class ,
561+ () -> client ().performRequest (new Request ("GET" , "/_snapshot/repo/snapshot" )));
562+ assertThat (e .getResponse ().getStatusLine ().getStatusCode (), equalTo (404 ));
563+ }
564+
443565 @ SuppressWarnings ("unchecked" )
444566 public void testNonexistentPolicy () throws Exception {
445567 String indexPrefix = randomAlphaOfLengthBetween (5 ,15 ).toLowerCase (Locale .ROOT );
@@ -732,4 +854,15 @@ private void indexDocument() throws IOException {
732854 Response response = client ().performRequest (indexRequest );
733855 logger .info (response .getStatusLine ());
734856 }
857+
858+ private String getSnapshotState (String snapshot ) throws IOException {
859+ Response response = client ().performRequest (new Request ("GET" , "/_snapshot/repo/snapshot" ));
860+ Map <String , Object > responseMap ;
861+ try (InputStream is = response .getEntity ().getContent ()) {
862+ responseMap = XContentHelper .convertToMap (XContentType .JSON .xContent (), is , true );
863+ }
864+ Map <String , Object > snapResponse = ((List <Map <String , Object >>) responseMap .get ("snapshots" )).get (0 );
865+ assertThat (snapResponse .get ("snapshot" ), equalTo ("snapshot" ));
866+ return (String ) snapResponse .get ("state" );
867+ }
735868}
0 commit comments