|
13 | 13 | import org.elasticsearch.cluster.ClusterState; |
14 | 14 | import org.elasticsearch.cluster.metadata.IndexMetadata; |
15 | 15 | import org.elasticsearch.cluster.metadata.LifecycleExecutionState; |
| 16 | +import org.elasticsearch.cluster.metadata.Metadata; |
| 17 | +import org.elasticsearch.common.settings.Settings; |
16 | 18 | import org.elasticsearch.core.Tuple; |
17 | 19 | import org.elasticsearch.index.Index; |
18 | 20 |
|
19 | 21 | import java.util.Objects; |
20 | 22 | import java.util.function.BiFunction; |
21 | 23 |
|
| 24 | +import static org.elasticsearch.cluster.metadata.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; |
| 25 | + |
22 | 26 | /** |
23 | 27 | * Copies the execution state data from one index to another, typically after a |
24 | 28 | * new index has been created. As part of the execution state copy it will set the target index |
@@ -101,11 +105,21 @@ public ClusterState performAction(Index index, ClusterState clusterState) { |
101 | 105 | newLifecycleState.setAction(action); |
102 | 106 | newLifecycleState.setStep(step); |
103 | 107 |
|
104 | | - return LifecycleExecutionStateUtils.newClusterStateWithLifecycleState( |
105 | | - clusterState, |
106 | | - targetIndexMetadata.getIndex(), |
107 | | - newLifecycleState.build() |
108 | | - ); |
| 108 | + // Build a new index metadata with the version incremented and the new lifecycle state. |
| 109 | + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(targetIndexMetadata) |
| 110 | + .version(targetIndexMetadata.getVersion() + 1) |
| 111 | + .putCustom(ILM_CUSTOM_METADATA_KEY, newLifecycleState.build().asMap()); |
| 112 | + |
| 113 | + // Remove the skip setting if it's present. |
| 114 | + if (targetIndexMetadata.getSettings().hasValue(LifecycleSettings.LIFECYCLE_SKIP)) { |
| 115 | + final var newSettings = Settings.builder().put(targetIndexMetadata.getSettings()); |
| 116 | + newSettings.remove(LifecycleSettings.LIFECYCLE_SKIP); |
| 117 | + indexMetadataBuilder.settingsVersion(targetIndexMetadata.getSettingsVersion() + 1).settings(newSettings); |
| 118 | + } |
| 119 | + |
| 120 | + return ClusterState.builder(clusterState) |
| 121 | + .metadata(Metadata.builder(clusterState.metadata()).put(indexMetadataBuilder.build(), false)) |
| 122 | + .build(); |
109 | 123 | } |
110 | 124 |
|
111 | 125 | @Override |
|
0 commit comments