|
| 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 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | +package org.elasticsearch.xpack.core.ilm; |
| 8 | + |
| 9 | +import org.apache.logging.log4j.LogManager; |
| 10 | +import org.apache.logging.log4j.Logger; |
| 11 | +import org.elasticsearch.action.ActionListener; |
| 12 | +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; |
| 13 | +import org.elasticsearch.action.support.master.AcknowledgedResponse; |
| 14 | +import org.elasticsearch.client.internal.Client; |
| 15 | +import org.elasticsearch.cluster.ClusterState; |
| 16 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 17 | +import org.elasticsearch.common.Strings; |
| 18 | +import org.elasticsearch.core.TimeValue; |
| 19 | +import org.elasticsearch.index.IndexNotFoundException; |
| 20 | + |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.function.Function; |
| 23 | + |
| 24 | +/** |
| 25 | + * Deletes the target index created by an operation such as shrink or rollup and |
| 26 | + * identified the target index name stored in the lifecycle state of the managed |
| 27 | + * index (if any was generated) |
| 28 | + */ |
| 29 | +public class CleanupTargetIndexStep extends AsyncRetryDuringSnapshotActionStep { |
| 30 | + public static final String NAME = "cleanup-target-index"; |
| 31 | + private static final Logger logger = LogManager.getLogger(CleanupTargetIndexStep.class); |
| 32 | + |
| 33 | + private final Function<IndexMetadata, String> sourceIndexNameSupplier; |
| 34 | + private final Function<IndexMetadata, String> targetIndexNameSupplier; |
| 35 | + |
| 36 | + public CleanupTargetIndexStep( |
| 37 | + StepKey key, |
| 38 | + StepKey nextStepKey, |
| 39 | + Client client, |
| 40 | + Function<IndexMetadata, String> sourceIndexNameSupplier, |
| 41 | + Function<IndexMetadata, String> targetIndexNameSupplier |
| 42 | + ) { |
| 43 | + super(key, nextStepKey, client); |
| 44 | + this.sourceIndexNameSupplier = sourceIndexNameSupplier; |
| 45 | + this.targetIndexNameSupplier = targetIndexNameSupplier; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean isRetryable() { |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + Function<IndexMetadata, String> getSourceIndexNameSupplier() { |
| 54 | + return sourceIndexNameSupplier; |
| 55 | + } |
| 56 | + |
| 57 | + Function<IndexMetadata, String> getTargetIndexNameSupplier() { |
| 58 | + return targetIndexNameSupplier; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentClusterState, ActionListener<Void> listener) { |
| 63 | + final String sourceIndexName = sourceIndexNameSupplier.apply(indexMetadata); |
| 64 | + if (Strings.isNullOrEmpty(sourceIndexName) == false) { |
| 65 | + // the current managed index is the target index |
| 66 | + if (currentClusterState.metadata().index(sourceIndexName) == null) { |
| 67 | + // if the source index does not exist, we'll skip deleting the |
| 68 | + // (managed) target index as that will cause data loss |
| 69 | + String policyName = indexMetadata.getLifecyclePolicyName(); |
| 70 | + logger.warn( |
| 71 | + "managed index [{}] has been created as part of policy [{}] and the source index [{}] does not exist " |
| 72 | + + "anymore. will skip the [{}] step", |
| 73 | + indexMetadata.getIndex().getName(), |
| 74 | + policyName, |
| 75 | + sourceIndexName, |
| 76 | + NAME |
| 77 | + ); |
| 78 | + listener.onResponse(null); |
| 79 | + return; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + final String targetIndexName = targetIndexNameSupplier.apply(indexMetadata); |
| 84 | + // if the target index was not generated there is nothing to delete so we move on |
| 85 | + if (Strings.hasText(targetIndexName) == false) { |
| 86 | + listener.onResponse(null); |
| 87 | + return; |
| 88 | + } |
| 89 | + getClient().admin() |
| 90 | + .indices() |
| 91 | + .delete(new DeleteIndexRequest(targetIndexName).masterNodeTimeout(TimeValue.MAX_VALUE), new ActionListener<>() { |
| 92 | + @Override |
| 93 | + public void onResponse(AcknowledgedResponse acknowledgedResponse) { |
| 94 | + // even if not all nodes acked the delete request yet we can consider this operation as successful as |
| 95 | + // we'll generate a new index name and attempt to create an index with the newly generated name |
| 96 | + listener.onResponse(null); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void onFailure(Exception e) { |
| 101 | + if (e instanceof IndexNotFoundException) { |
| 102 | + // we can move on if the index was deleted in the meantime |
| 103 | + listener.onResponse(null); |
| 104 | + } else { |
| 105 | + listener.onFailure(e); |
| 106 | + } |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public boolean equals(Object o) { |
| 113 | + if (this == o) { |
| 114 | + return true; |
| 115 | + } |
| 116 | + if (o == null || getClass() != o.getClass()) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + CleanupTargetIndexStep that = (CleanupTargetIndexStep) o; |
| 120 | + return super.equals(o) |
| 121 | + && Objects.equals(targetIndexNameSupplier, that.targetIndexNameSupplier) |
| 122 | + && Objects.equals(sourceIndexNameSupplier, that.sourceIndexNameSupplier); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public int hashCode() { |
| 127 | + return Objects.hash(super.hashCode(), targetIndexNameSupplier, sourceIndexNameSupplier); |
| 128 | + } |
| 129 | +} |
0 commit comments