55 */
66package org .elasticsearch .xpack .indexlifecycle ;
77
8- import com .carrotsearch .hppc .cursors .ObjectCursor ;
98import org .apache .logging .log4j .LogManager ;
109import org .apache .logging .log4j .Logger ;
1110import org .elasticsearch .ElasticsearchException ;
2726import org .elasticsearch .xpack .core .indexlifecycle .ClusterStateActionStep ;
2827import org .elasticsearch .xpack .core .indexlifecycle .ClusterStateWaitStep ;
2928import org .elasticsearch .xpack .core .indexlifecycle .ErrorStep ;
30- import org .elasticsearch .xpack .core .indexlifecycle .IndexLifecycleMetadata ;
31- import org .elasticsearch .xpack .core .indexlifecycle .LifecycleAction ;
3229import org .elasticsearch .xpack .core .indexlifecycle .LifecyclePolicy ;
3330import org .elasticsearch .xpack .core .indexlifecycle .LifecycleSettings ;
34- import org .elasticsearch .xpack .core .indexlifecycle .Phase ;
3531import org .elasticsearch .xpack .core .indexlifecycle .RolloverAction ;
3632import org .elasticsearch .xpack .core .indexlifecycle .Step ;
3733import org .elasticsearch .xpack .core .indexlifecycle .Step .StepKey ;
3834import org .elasticsearch .xpack .core .indexlifecycle .TerminalPolicyStep ;
3935
4036import java .io .IOException ;
4137import java .util .List ;
42- import java .util .Map ;
4338import java .util .function .LongSupplier ;
4439
4540public class IndexLifecycleRunner {
@@ -325,8 +320,6 @@ private void setStepInfo(Index index, String policy, StepKey currentStepKey, ToX
325320
326321 public static ClusterState setPolicyForIndexes (final String newPolicyName , final Index [] indices , ClusterState currentState ,
327322 LifecyclePolicy newPolicy , List <String > failedIndexes , LongSupplier nowSupplier ) {
328- Map <String , LifecyclePolicy > policiesMap = ((IndexLifecycleMetadata ) currentState .metaData ().custom (IndexLifecycleMetadata .TYPE ))
329- .getPolicies ();
330323 MetaData .Builder newMetadata = MetaData .builder (currentState .getMetaData ());
331324 boolean clusterStateChanged = false ;
332325 for (Index index : indices ) {
@@ -335,8 +328,8 @@ public static ClusterState setPolicyForIndexes(final String newPolicyName, final
335328 // Index doesn't exist so fail it
336329 failedIndexes .add (index .getName ());
337330 } else {
338- IndexMetaData .Builder newIdxMetadata = IndexLifecycleRunner .setPolicyForIndex (newPolicyName , newPolicy , policiesMap ,
339- failedIndexes , index , indexMetadata , nowSupplier );
331+ IndexMetaData .Builder newIdxMetadata = IndexLifecycleRunner .setPolicyForIndex (newPolicyName ,
332+ newPolicy , indexMetadata , nowSupplier );
340333 if (newIdxMetadata != null ) {
341334 newMetadata .put (newIdxMetadata );
342335 clusterStateChanged = true ;
@@ -353,106 +346,24 @@ public static ClusterState setPolicyForIndexes(final String newPolicyName, final
353346 }
354347
355348 private static IndexMetaData .Builder setPolicyForIndex (final String newPolicyName , LifecyclePolicy newPolicy ,
356- Map <String , LifecyclePolicy > policiesMap , List <String > failedIndexes , Index index , IndexMetaData indexMetadata ,
357- LongSupplier nowSupplier ) {
349+ IndexMetaData indexMetadata , LongSupplier nowSupplier ) {
358350 Settings idxSettings = indexMetadata .getSettings ();
359- String currentPolicyName = LifecycleSettings .LIFECYCLE_NAME_SETTING .get (idxSettings );
360351 StepKey currentStepKey = IndexLifecycleRunner .getCurrentStepKey (idxSettings );
361- LifecyclePolicy currentPolicy = null ;
362- if (Strings .hasLength (currentPolicyName )) {
363- currentPolicy = policiesMap .get (currentPolicyName );
364- }
365-
366- if (canSetPolicy (currentStepKey , currentPolicy , newPolicy )) {
367- Settings .Builder newSettings = Settings .builder ().put (idxSettings );
368- if (currentStepKey != null ) {
369- // Check if current step exists in new policy and if not move to
370- // next available step
371- StepKey nextValidStepKey = newPolicy .getNextValidStep (currentStepKey );
372- if (nextValidStepKey .equals (currentStepKey ) == false ) {
373- newSettings = moveIndexSettingsToNextStep (idxSettings , currentStepKey , nextValidStepKey , nowSupplier );
374- }
375- }
376- newSettings .put (LifecycleSettings .LIFECYCLE_NAME_SETTING .getKey (), newPolicyName );
377- return IndexMetaData .builder (indexMetadata ).settings (newSettings );
378- } else {
379- failedIndexes .add (index .getName ());
380- return null ;
381- }
382- }
383-
384- private static boolean canSetPolicy (StepKey currentStepKey , LifecyclePolicy currentPolicy , LifecyclePolicy newPolicy ) {
385- if (currentPolicy != null ) {
386- if (currentPolicy .isActionSafe (currentStepKey )) {
387- return true ;
388- } else {
389- // Index is in an unsafe action so fail it if the action has changed between oldPolicy and newPolicy
390- return isActionChanged (currentStepKey , currentPolicy , newPolicy ) == false ;
391- }
392- } else {
393- // Index not previously managed by ILM so safe to change policy
394- return true ;
395- }
396- }
397-
398- private static boolean isActionChanged (StepKey stepKey , LifecyclePolicy currentPolicy , LifecyclePolicy newPolicy ) {
399- LifecycleAction currentAction = getActionFromPolicy (currentPolicy , stepKey .getPhase (), stepKey .getAction ());
400- LifecycleAction newAction = getActionFromPolicy (newPolicy , stepKey .getPhase (), stepKey .getAction ());
401- if (newAction == null ) {
402- return true ;
403- } else {
404- return currentAction .equals (newAction ) == false ;
405- }
406- }
407-
408- private static LifecycleAction getActionFromPolicy (LifecyclePolicy policy , String phaseName , String actionName ) {
409- Phase phase = policy .getPhases ().get (phaseName );
410- if (phase != null ) {
411- return phase .getActions ().get (actionName );
412- } else {
413- return null ;
414- }
415- }
416352
417- /**
418- * Returns <code>true</code> if the provided policy is allowed to be updated
419- * given the current {@link ClusterState}. In practice this method checks
420- * that all the indexes using the provided <code>policyName</code> is in a
421- * state where it is able to deal with the policy being updated to
422- * <code>newPolicy</code>. If any of these indexes is not in a state wheree
423- * it can deal with the update the method will return <code>false</code>.
424- *
425- * @param policyName
426- * the name of the policy being updated
427- * @param newPolicy
428- * the new version of the {@link LifecyclePolicy}
429- * @param currentState
430- * the current {@link ClusterState}
431- */
432- public static boolean canUpdatePolicy (String policyName , LifecyclePolicy newPolicy , ClusterState currentState ) {
433- Map <String , LifecyclePolicy > policiesMap = ((IndexLifecycleMetadata ) currentState .metaData ().custom (IndexLifecycleMetadata .TYPE ))
434- .getPolicies ();
435- for (ObjectCursor <IndexMetaData > cursor : currentState .getMetaData ().indices ().values ()) {
436- IndexMetaData idxMetadata = cursor .value ;
437- Settings idxSettings = idxMetadata .getSettings ();
438- String currentPolicyName = LifecycleSettings .LIFECYCLE_NAME_SETTING .get (idxSettings );
439- LifecyclePolicy currentPolicy = null ;
440- if (Strings .hasLength (currentPolicyName )) {
441- currentPolicy = policiesMap .get (currentPolicyName );
442- }
443- if (policyName .equals (currentPolicyName )) {
444- StepKey currentStepKey = IndexLifecycleRunner .getCurrentStepKey (idxSettings );
445- if (canSetPolicy (currentStepKey , currentPolicy , newPolicy ) == false ) {
446- return false ;
447- }
353+ Settings .Builder newSettings = Settings .builder ().put (idxSettings );
354+ if (currentStepKey != null ) {
355+ // Check if current step exists in new policy and if not move to
356+ // next available step
357+ StepKey nextValidStepKey = newPolicy .getNextValidStep (currentStepKey );
358+ if (nextValidStepKey .equals (currentStepKey ) == false ) {
359+ newSettings = moveIndexSettingsToNextStep (idxSettings , currentStepKey , nextValidStepKey , nowSupplier );
448360 }
449361 }
450- return true ;
362+ newSettings .put (LifecycleSettings .LIFECYCLE_NAME_SETTING .getKey (), newPolicyName );
363+ return IndexMetaData .builder (indexMetadata ).settings (newSettings );
451364 }
452365
453366 public static ClusterState removePolicyForIndexes (final Index [] indices , ClusterState currentState , List <String > failedIndexes ) {
454- Map <String , LifecyclePolicy > policiesMap = ((IndexLifecycleMetadata ) currentState .metaData ().custom (IndexLifecycleMetadata .TYPE ))
455- .getPolicies ();
456367 MetaData .Builder newMetadata = MetaData .builder (currentState .getMetaData ());
457368 boolean clusterStateChanged = false ;
458369 for (Index index : indices ) {
@@ -461,8 +372,7 @@ public static ClusterState removePolicyForIndexes(final Index[] indices, Cluster
461372 // Index doesn't exist so fail it
462373 failedIndexes .add (index .getName ());
463374 } else {
464- IndexMetaData .Builder newIdxMetadata = IndexLifecycleRunner .removePolicyForIndex (index , indexMetadata , policiesMap ,
465- failedIndexes );
375+ IndexMetaData .Builder newIdxMetadata = IndexLifecycleRunner .removePolicyForIndex (indexMetadata );
466376 if (newIdxMetadata != null ) {
467377 newMetadata .put (newIdxMetadata );
468378 clusterStateChanged = true ;
@@ -478,44 +388,22 @@ public static ClusterState removePolicyForIndexes(final Index[] indices, Cluster
478388 }
479389 }
480390
481- private static IndexMetaData .Builder removePolicyForIndex (Index index , IndexMetaData indexMetadata ,
482- Map <String , LifecyclePolicy > policiesMap , List <String > failedIndexes ) {
391+ private static IndexMetaData .Builder removePolicyForIndex (IndexMetaData indexMetadata ) {
483392 Settings idxSettings = indexMetadata .getSettings ();
484393 Settings .Builder newSettings = Settings .builder ().put (idxSettings );
485- String currentPolicyName = LifecycleSettings .LIFECYCLE_NAME_SETTING .get (idxSettings );
486- StepKey currentStepKey = IndexLifecycleRunner .getCurrentStepKey (idxSettings );
487- LifecyclePolicy currentPolicy = null ;
488- if (Strings .hasLength (currentPolicyName )) {
489- currentPolicy = policiesMap .get (currentPolicyName );
490- }
491-
492- if (canRemovePolicy (currentStepKey , currentPolicy )) {
493- newSettings .remove (LifecycleSettings .LIFECYCLE_NAME_SETTING .getKey ());
494- newSettings .remove (LifecycleSettings .LIFECYCLE_PHASE_SETTING .getKey ());
495- newSettings .remove (LifecycleSettings .LIFECYCLE_PHASE_TIME_SETTING .getKey ());
496- newSettings .remove (LifecycleSettings .LIFECYCLE_ACTION_SETTING .getKey ());
497- newSettings .remove (LifecycleSettings .LIFECYCLE_ACTION_TIME_SETTING .getKey ());
498- newSettings .remove (LifecycleSettings .LIFECYCLE_STEP_SETTING .getKey ());
499- newSettings .remove (LifecycleSettings .LIFECYCLE_STEP_TIME_SETTING .getKey ());
500- newSettings .remove (LifecycleSettings .LIFECYCLE_STEP_INFO_SETTING .getKey ());
501- newSettings .remove (LifecycleSettings .LIFECYCLE_FAILED_STEP_SETTING .getKey ());
502- newSettings .remove (LifecycleSettings .LIFECYCLE_INDEX_CREATION_DATE_SETTING .getKey ());
503- newSettings .remove (LifecycleSettings .LIFECYCLE_SKIP_SETTING .getKey ());
504- newSettings .remove (RolloverAction .LIFECYCLE_ROLLOVER_ALIAS_SETTING .getKey ());
505- return IndexMetaData .builder (indexMetadata ).settings (newSettings );
506- } else {
507- failedIndexes .add (index .getName ());
508- return null ;
509- }
510- }
511394
512- private static boolean canRemovePolicy (StepKey currentStepKey , LifecyclePolicy currentPolicy ) {
513- if (currentPolicy != null ) {
514- // Can't remove policy if the index is currently in an unsafe action
515- return currentPolicy .isActionSafe (currentStepKey );
516- } else {
517- // Index not previously managed by ILM
518- return true ;
519- }
395+ newSettings .remove (LifecycleSettings .LIFECYCLE_NAME_SETTING .getKey ());
396+ newSettings .remove (LifecycleSettings .LIFECYCLE_PHASE_SETTING .getKey ());
397+ newSettings .remove (LifecycleSettings .LIFECYCLE_PHASE_TIME_SETTING .getKey ());
398+ newSettings .remove (LifecycleSettings .LIFECYCLE_ACTION_SETTING .getKey ());
399+ newSettings .remove (LifecycleSettings .LIFECYCLE_ACTION_TIME_SETTING .getKey ());
400+ newSettings .remove (LifecycleSettings .LIFECYCLE_STEP_SETTING .getKey ());
401+ newSettings .remove (LifecycleSettings .LIFECYCLE_STEP_TIME_SETTING .getKey ());
402+ newSettings .remove (LifecycleSettings .LIFECYCLE_STEP_INFO_SETTING .getKey ());
403+ newSettings .remove (LifecycleSettings .LIFECYCLE_FAILED_STEP_SETTING .getKey ());
404+ newSettings .remove (LifecycleSettings .LIFECYCLE_INDEX_CREATION_DATE_SETTING .getKey ());
405+ newSettings .remove (LifecycleSettings .LIFECYCLE_SKIP_SETTING .getKey ());
406+ newSettings .remove (RolloverAction .LIFECYCLE_ROLLOVER_ALIAS_SETTING .getKey ());
407+ return IndexMetaData .builder (indexMetadata ).settings (newSettings );
520408 }
521409}
0 commit comments