@@ -911,6 +911,162 @@ public void testSetPolicyForIndexIndexInShrink() {
911911 assertSame (clusterState , newClusterState );
912912 }
913913
914+ public void testCanUpdatePolicy () {
915+ String indexName = randomAlphaOfLength (10 );
916+ String oldPolicyName = "old_policy" ;
917+ String newPolicyName = "new_policy" ;
918+ LifecyclePolicy newPolicy = new LifecyclePolicy (TestLifecycleType .INSTANCE , newPolicyName , Collections .emptyMap ());
919+ StepKey currentStep = AbstractStepTestCase .randomStepKey ();
920+ Settings .Builder indexSettingsBuilder = Settings .builder ().put (LifecycleSettings .LIFECYCLE_NAME , oldPolicyName )
921+ .put (LifecycleSettings .LIFECYCLE_PHASE , currentStep .getPhase ())
922+ .put (LifecycleSettings .LIFECYCLE_ACTION , currentStep .getAction ())
923+ .put (LifecycleSettings .LIFECYCLE_STEP , currentStep .getName ()).put (LifecycleSettings .LIFECYCLE_SKIP , true );
924+ ClusterState clusterState = buildClusterState (indexName , indexSettingsBuilder );
925+
926+ boolean canUpdatePolicy = IndexLifecycleRunner .canUpdatePolicy (oldPolicyName , newPolicy , clusterState );
927+
928+ assertTrue (canUpdatePolicy );
929+ }
930+
931+ public void testCanUpdatePolicyIndexInShrink () {
932+ String indexName = randomAlphaOfLength (10 );
933+ String oldPolicyName = "old_policy" ;
934+ String newPolicyName = "new_policy" ;
935+ LifecyclePolicy newPolicy = new LifecyclePolicy (TestLifecycleType .INSTANCE , newPolicyName , Collections .emptyMap ());
936+ StepKey currentStep = new StepKey (randomAlphaOfLength (10 ), ShrinkAction .NAME , randomAlphaOfLength (10 ));
937+ Settings .Builder indexSettingsBuilder = Settings .builder ().put (LifecycleSettings .LIFECYCLE_NAME , oldPolicyName )
938+ .put (LifecycleSettings .LIFECYCLE_PHASE , currentStep .getPhase ())
939+ .put (LifecycleSettings .LIFECYCLE_ACTION , currentStep .getAction ())
940+ .put (LifecycleSettings .LIFECYCLE_STEP , currentStep .getName ()).put (LifecycleSettings .LIFECYCLE_SKIP , true );
941+ ClusterState clusterState = buildClusterState (indexName , indexSettingsBuilder );
942+
943+ boolean canUpdatePolicy = IndexLifecycleRunner .canUpdatePolicy (oldPolicyName , newPolicy , clusterState );
944+
945+ assertFalse (canUpdatePolicy );
946+ }
947+
948+ public void testCanUpdatePolicyIndexNotManaged () {
949+ String indexName = randomAlphaOfLength (10 );
950+ String oldPolicyName = "old_policy" ;
951+ String newPolicyName = "new_policy" ;
952+ LifecyclePolicy newPolicy = new LifecyclePolicy (TestLifecycleType .INSTANCE , newPolicyName , Collections .emptyMap ());
953+ Settings .Builder indexSettingsBuilder = Settings .builder ();
954+ ClusterState clusterState = buildClusterState (indexName , indexSettingsBuilder );
955+
956+ boolean canUpdatePolicy = IndexLifecycleRunner .canUpdatePolicy (oldPolicyName , newPolicy , clusterState );
957+
958+ assertTrue (canUpdatePolicy );
959+ }
960+
961+ public void testCanUpdatePolicyDifferentPolicy () {
962+ String indexName = randomAlphaOfLength (10 );
963+ String oldPolicyName = "old_policy" ;
964+ String newPolicyName = "new_policy" ;
965+ LifecyclePolicy newPolicy = new LifecyclePolicy (TestLifecycleType .INSTANCE , newPolicyName , Collections .emptyMap ());
966+ StepKey currentStep = new StepKey (randomAlphaOfLength (10 ), ShrinkAction .NAME , randomAlphaOfLength (10 ));
967+ Settings .Builder indexSettingsBuilder = Settings .builder ().put (LifecycleSettings .LIFECYCLE_NAME , "different_policy" )
968+ .put (LifecycleSettings .LIFECYCLE_PHASE , currentStep .getPhase ())
969+ .put (LifecycleSettings .LIFECYCLE_ACTION , currentStep .getAction ())
970+ .put (LifecycleSettings .LIFECYCLE_STEP , currentStep .getName ()).put (LifecycleSettings .LIFECYCLE_SKIP , true );
971+ ClusterState clusterState = buildClusterState (indexName , indexSettingsBuilder );
972+
973+ boolean canUpdatePolicy = IndexLifecycleRunner .canUpdatePolicy (oldPolicyName , newPolicy , clusterState );
974+
975+ assertTrue (canUpdatePolicy );
976+ }
977+
978+ public void testCanUpdatePolicyMultipleIndexesUpdateAllowed () {
979+ String oldPolicyName = "old_policy" ;
980+ String newPolicyName = "new_policy" ;
981+ LifecyclePolicy newPolicy = new LifecyclePolicy (TestLifecycleType .INSTANCE , newPolicyName , Collections .emptyMap ());
982+
983+ String index1Name = randomAlphaOfLength (10 );
984+ StepKey currentStep1 = AbstractStepTestCase .randomStepKey ();
985+ Settings .Builder indexSettingsBuilder1 = Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
986+ .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ).put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT )
987+ .put (LifecycleSettings .LIFECYCLE_NAME , oldPolicyName ).put (LifecycleSettings .LIFECYCLE_PHASE , currentStep1 .getPhase ())
988+ .put (LifecycleSettings .LIFECYCLE_ACTION , currentStep1 .getAction ())
989+ .put (LifecycleSettings .LIFECYCLE_STEP , currentStep1 .getName ()).put (LifecycleSettings .LIFECYCLE_SKIP , true );
990+ IndexMetaData indexMetadata1 = IndexMetaData .builder (index1Name ).settings (indexSettingsBuilder1 ).build ();
991+
992+ String index2Name = randomAlphaOfLength (10 );
993+ StepKey currentStep2 = AbstractStepTestCase .randomStepKey ();
994+ Settings .Builder indexSettingsBuilder2 = Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
995+ .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ).put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT )
996+ .put (LifecycleSettings .LIFECYCLE_NAME , oldPolicyName ).put (LifecycleSettings .LIFECYCLE_PHASE , currentStep2 .getPhase ())
997+ .put (LifecycleSettings .LIFECYCLE_ACTION , currentStep2 .getAction ())
998+ .put (LifecycleSettings .LIFECYCLE_STEP , currentStep2 .getName ()).put (LifecycleSettings .LIFECYCLE_SKIP , true );
999+ IndexMetaData indexMetadata2 = IndexMetaData .builder (index2Name ).settings (indexSettingsBuilder2 ).build ();
1000+
1001+ String index3Name = randomAlphaOfLength (10 );
1002+ StepKey currentStep3 = new StepKey (randomAlphaOfLength (10 ), ShrinkAction .NAME , randomAlphaOfLength (10 ));
1003+ Settings .Builder indexSettingsBuilder3 = Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
1004+ .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ).put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT )
1005+ .put (LifecycleSettings .LIFECYCLE_NAME , "different_policy" ).put (LifecycleSettings .LIFECYCLE_PHASE , currentStep3 .getPhase ())
1006+ .put (LifecycleSettings .LIFECYCLE_ACTION , currentStep3 .getAction ())
1007+ .put (LifecycleSettings .LIFECYCLE_STEP , currentStep3 .getName ()).put (LifecycleSettings .LIFECYCLE_SKIP , true );
1008+ IndexMetaData indexMetadata3 = IndexMetaData .builder (index3Name ).settings (indexSettingsBuilder3 ).build ();
1009+
1010+ String index4Name = randomAlphaOfLength (10 );
1011+ Settings .Builder indexSettingsBuilder4 = Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
1012+ .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ).put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT );
1013+ IndexMetaData indexMetadata4 = IndexMetaData .builder (index4Name ).settings (indexSettingsBuilder4 ).build ();
1014+
1015+ MetaData metadata = MetaData .builder ().put (indexMetadata1 , true ).put (indexMetadata2 , true ).put (indexMetadata3 , true )
1016+ .put (indexMetadata4 , true ).build ();
1017+ ClusterState clusterState = ClusterState .builder (new ClusterName ("my_cluster" )).metaData (metadata ).build ();
1018+
1019+ boolean canUpdatePolicy = IndexLifecycleRunner .canUpdatePolicy (oldPolicyName , newPolicy , clusterState );
1020+
1021+ assertTrue (canUpdatePolicy );
1022+ }
1023+
1024+ public void testCanUpdatePolicyMultipleIndexesUpdateForbidden () {
1025+ String oldPolicyName = "old_policy" ;
1026+ String newPolicyName = "new_policy" ;
1027+ LifecyclePolicy newPolicy = new LifecyclePolicy (TestLifecycleType .INSTANCE , newPolicyName , Collections .emptyMap ());
1028+
1029+ String index1Name = randomAlphaOfLength (10 );
1030+ StepKey currentStep1 = AbstractStepTestCase .randomStepKey ();
1031+ Settings .Builder indexSettingsBuilder1 = Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
1032+ .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ).put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT )
1033+ .put (LifecycleSettings .LIFECYCLE_NAME , oldPolicyName ).put (LifecycleSettings .LIFECYCLE_PHASE , currentStep1 .getPhase ())
1034+ .put (LifecycleSettings .LIFECYCLE_ACTION , currentStep1 .getAction ())
1035+ .put (LifecycleSettings .LIFECYCLE_STEP , currentStep1 .getName ()).put (LifecycleSettings .LIFECYCLE_SKIP , true );
1036+ IndexMetaData indexMetadata1 = IndexMetaData .builder (index1Name ).settings (indexSettingsBuilder1 ).build ();
1037+
1038+ String index2Name = randomAlphaOfLength (10 );
1039+ StepKey currentStep2 = new StepKey (randomAlphaOfLength (10 ), ShrinkAction .NAME , randomAlphaOfLength (10 ));
1040+ Settings .Builder indexSettingsBuilder2 = Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
1041+ .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ).put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT )
1042+ .put (LifecycleSettings .LIFECYCLE_NAME , oldPolicyName ).put (LifecycleSettings .LIFECYCLE_PHASE , currentStep2 .getPhase ())
1043+ .put (LifecycleSettings .LIFECYCLE_ACTION , currentStep2 .getAction ())
1044+ .put (LifecycleSettings .LIFECYCLE_STEP , currentStep2 .getName ()).put (LifecycleSettings .LIFECYCLE_SKIP , true );
1045+ IndexMetaData indexMetadata2 = IndexMetaData .builder (index2Name ).settings (indexSettingsBuilder2 ).build ();
1046+
1047+ String index3Name = randomAlphaOfLength (10 );
1048+ StepKey currentStep3 = new StepKey (randomAlphaOfLength (10 ), ShrinkAction .NAME , randomAlphaOfLength (10 ));
1049+ Settings .Builder indexSettingsBuilder3 = Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
1050+ .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ).put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT )
1051+ .put (LifecycleSettings .LIFECYCLE_NAME , "different_policy" ).put (LifecycleSettings .LIFECYCLE_PHASE , currentStep3 .getPhase ())
1052+ .put (LifecycleSettings .LIFECYCLE_ACTION , currentStep3 .getAction ())
1053+ .put (LifecycleSettings .LIFECYCLE_STEP , currentStep3 .getName ()).put (LifecycleSettings .LIFECYCLE_SKIP , true );
1054+ IndexMetaData indexMetadata3 = IndexMetaData .builder (index3Name ).settings (indexSettingsBuilder3 ).build ();
1055+
1056+ String index4Name = randomAlphaOfLength (10 );
1057+ Settings .Builder indexSettingsBuilder4 = Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
1058+ .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 ).put (IndexMetaData .SETTING_VERSION_CREATED , Version .CURRENT );
1059+ IndexMetaData indexMetadata4 = IndexMetaData .builder (index4Name ).settings (indexSettingsBuilder4 ).build ();
1060+
1061+ MetaData metadata = MetaData .builder ().put (indexMetadata1 , true ).put (indexMetadata2 , true ).put (indexMetadata3 , true )
1062+ .put (indexMetadata4 , true ).build ();
1063+ ClusterState clusterState = ClusterState .builder (new ClusterName ("my_cluster" )).metaData (metadata ).build ();
1064+
1065+ boolean canUpdatePolicy = IndexLifecycleRunner .canUpdatePolicy (oldPolicyName , newPolicy , clusterState );
1066+
1067+ assertFalse (canUpdatePolicy );
1068+ }
1069+
9141070 public void testRemovePolicyForIndex () {
9151071 String indexName = randomAlphaOfLength (10 );
9161072 String oldPolicyName = "old_policy" ;
0 commit comments