@@ -445,6 +445,62 @@ public void testRolloverMaxSize() throws Exception {
445445 }
446446 }
447447
448+ public void testRolloverMaxSinglePrimarySize () throws Exception {
449+ assertAcked (prepareCreate ("test-1" ).addAlias (new Alias ("test_alias" )).get ());
450+ int numDocs = randomIntBetween (10 , 20 );
451+ for (int i = 0 ; i < numDocs ; i ++) {
452+ indexDoc ("test-1" , Integer .toString (i ), "field" , "foo-" + i );
453+ }
454+ flush ("test-1" );
455+ refresh ("test_alias" );
456+
457+ // A large max_single_primary_size
458+ {
459+ final RolloverResponse response = client ().admin ().indices ()
460+ .prepareRolloverIndex ("test_alias" )
461+ .addMaxSinglePrimarySizeCondition (new ByteSizeValue (randomIntBetween (100 , 50 * 1024 ), ByteSizeUnit .MB ))
462+ .get ();
463+ assertThat (response .getOldIndex (), equalTo ("test-1" ));
464+ assertThat (response .getNewIndex (), equalTo ("test-000002" ));
465+ assertThat ("No rollover with a large max_single_primary_size condition" , response .isRolledOver (), equalTo (false ));
466+ final IndexMetadata oldIndex = client ().admin ().cluster ().prepareState ().get ().getState ().metadata ().index ("test-1" );
467+ assertThat (oldIndex .getRolloverInfos ().size (), equalTo (0 ));
468+ }
469+
470+ // A small max_single_primary_size
471+ {
472+ ByteSizeValue maxSinglePrimarySizeCondition = new ByteSizeValue (randomIntBetween (1 , 20 ), ByteSizeUnit .BYTES );
473+ long beforeTime = client ().threadPool ().absoluteTimeInMillis () - 1000L ;
474+ final RolloverResponse response = client ().admin ().indices ()
475+ .prepareRolloverIndex ("test_alias" )
476+ .addMaxSinglePrimarySizeCondition (maxSinglePrimarySizeCondition )
477+ .get ();
478+ assertThat (response .getOldIndex (), equalTo ("test-1" ));
479+ assertThat (response .getNewIndex (), equalTo ("test-000002" ));
480+ assertThat ("Should rollover with a small max_single_primary_size condition" , response .isRolledOver (), equalTo (true ));
481+ final IndexMetadata oldIndex = client ().admin ().cluster ().prepareState ().get ().getState ().metadata ().index ("test-1" );
482+ List <Condition <?>> metConditions = oldIndex .getRolloverInfos ().get ("test_alias" ).getMetConditions ();
483+ assertThat (metConditions .size (), equalTo (1 ));
484+ assertThat (metConditions .get (0 ).toString (),
485+ equalTo (new MaxSinglePrimarySizeCondition (maxSinglePrimarySizeCondition ).toString ()));
486+ assertThat (oldIndex .getRolloverInfos ().get ("test_alias" ).getTime (),
487+ is (both (greaterThanOrEqualTo (beforeTime )).and (lessThanOrEqualTo (client ().threadPool ().absoluteTimeInMillis () + 1000L ))));
488+ }
489+
490+ // An empty index
491+ {
492+ final RolloverResponse response = client ().admin ().indices ()
493+ .prepareRolloverIndex ("test_alias" )
494+ .addMaxSinglePrimarySizeCondition (new ByteSizeValue (randomNonNegativeLong (), ByteSizeUnit .BYTES ))
495+ .get ();
496+ assertThat (response .getOldIndex (), equalTo ("test-000002" ));
497+ assertThat (response .getNewIndex (), equalTo ("test-000003" ));
498+ assertThat ("No rollover with an empty index" , response .isRolledOver (), equalTo (false ));
499+ final IndexMetadata oldIndex = client ().admin ().cluster ().prepareState ().get ().getState ().metadata ().index ("test-000002" );
500+ assertThat (oldIndex .getRolloverInfos ().size (), equalTo (0 ));
501+ }
502+ }
503+
448504 public void testRejectIfAliasFoundInTemplate () throws Exception {
449505 client ().admin ().indices ().preparePutTemplate ("logs" )
450506 .setPatterns (Collections .singletonList ("logs-*" )).addAlias (new Alias ("logs-write" )).get ();
0 commit comments