|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.core.ilm; |
| 7 | + |
| 8 | +import org.apache.logging.log4j.LogManager; |
| 9 | +import org.apache.logging.log4j.Logger; |
| 10 | +import org.elasticsearch.cluster.ClusterState; |
| 11 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 12 | +import org.elasticsearch.cluster.metadata.MetaData; |
| 13 | +import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.index.Index; |
| 15 | + |
| 16 | +import java.util.Locale; |
| 17 | +import java.util.Objects; |
| 18 | + |
| 19 | +/** |
| 20 | + * Copy the provided settings from the source to the target index. |
| 21 | + * <p> |
| 22 | + * The target index is derived from the source index using the provided prefix. |
| 23 | + * This is useful for actions like shrink or searchable snapshot that create a new index and migrate the ILM execution from the source |
| 24 | + * to the target index. |
| 25 | + */ |
| 26 | +public class CopySettingsStep extends ClusterStateActionStep { |
| 27 | + public static final String NAME = "copy-settings"; |
| 28 | + |
| 29 | + private static final Logger logger = LogManager.getLogger(CopySettingsStep.class); |
| 30 | + |
| 31 | + private final String[] settingsKeys; |
| 32 | + private final String indexPrefix; |
| 33 | + |
| 34 | + public CopySettingsStep(StepKey key, StepKey nextStepKey, String indexPrefix, String... settingsKeys) { |
| 35 | + super(key, nextStepKey); |
| 36 | + Objects.requireNonNull(indexPrefix); |
| 37 | + Objects.requireNonNull(settingsKeys); |
| 38 | + this.indexPrefix = indexPrefix; |
| 39 | + this.settingsKeys = settingsKeys; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public boolean isRetryable() { |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + public String[] getSettingsKeys() { |
| 48 | + return settingsKeys; |
| 49 | + } |
| 50 | + |
| 51 | + public String getIndexPrefix() { |
| 52 | + return indexPrefix; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public ClusterState performAction(Index index, ClusterState clusterState) { |
| 57 | + String sourceIndexName = index.getName(); |
| 58 | + IndexMetaData sourceIndexMetadata = clusterState.metaData().index(sourceIndexName); |
| 59 | + String targetIndexName = indexPrefix + sourceIndexName; |
| 60 | + IndexMetaData targetIndexMetadata = clusterState.metaData().index(targetIndexName); |
| 61 | + |
| 62 | + if (sourceIndexMetadata == null) { |
| 63 | + // Index must have been since deleted, ignore it |
| 64 | + logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().getAction(), sourceIndexName); |
| 65 | + return clusterState; |
| 66 | + } |
| 67 | + |
| 68 | + if (settingsKeys == null || settingsKeys.length == 0) { |
| 69 | + return clusterState; |
| 70 | + } |
| 71 | + |
| 72 | + if (targetIndexMetadata == null) { |
| 73 | + String errorMessage = String.format(Locale.ROOT, "index [%s] is being referenced by ILM action [%s] on step [%s] but " + |
| 74 | + "it doesn't exist", targetIndexName, getKey().getAction(), getKey().getName()); |
| 75 | + logger.debug(errorMessage); |
| 76 | + throw new IllegalStateException(errorMessage); |
| 77 | + } |
| 78 | + |
| 79 | + Settings.Builder settings = Settings.builder().put(targetIndexMetadata.getSettings()); |
| 80 | + for (String key : settingsKeys) { |
| 81 | + String value = sourceIndexMetadata.getSettings().get(key); |
| 82 | + settings.put(key, value); |
| 83 | + } |
| 84 | + |
| 85 | + MetaData.Builder newMetaData = MetaData.builder(clusterState.getMetaData()) |
| 86 | + .put(IndexMetaData.builder(targetIndexMetadata) |
| 87 | + .settingsVersion(targetIndexMetadata.getSettingsVersion() + 1) |
| 88 | + .settings(settings)); |
| 89 | + return ClusterState.builder(clusterState).metaData(newMetaData).build(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean equals(Object o) { |
| 94 | + if (this == o) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + if (o == null || getClass() != o.getClass()) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + if (!super.equals(o)) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + CopySettingsStep that = (CopySettingsStep) o; |
| 104 | + return Objects.equals(settingsKeys, that.settingsKeys) && |
| 105 | + Objects.equals(indexPrefix, that.indexPrefix); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public int hashCode() { |
| 110 | + return Objects.hash(super.hashCode(), settingsKeys, indexPrefix); |
| 111 | + } |
| 112 | +} |
0 commit comments