|
| 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 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.datastreams.lifecycle.downsampling; |
| 10 | + |
| 11 | +import org.apache.logging.log4j.LogManager; |
| 12 | +import org.apache.logging.log4j.Logger; |
| 13 | +import org.elasticsearch.action.ActionListener; |
| 14 | +import org.elasticsearch.cluster.ClusterState; |
| 15 | +import org.elasticsearch.cluster.ClusterStateTaskListener; |
| 16 | +import org.elasticsearch.cluster.metadata.DataStream; |
| 17 | +import org.elasticsearch.cluster.metadata.IndexAbstraction; |
| 18 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 19 | +import org.elasticsearch.cluster.metadata.Metadata; |
| 20 | +import org.elasticsearch.common.settings.Settings; |
| 21 | +import org.elasticsearch.core.TimeValue; |
| 22 | +import org.elasticsearch.index.IndexSettings; |
| 23 | + |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Locale; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Objects; |
| 28 | + |
| 29 | +import static org.elasticsearch.datastreams.DataStreamsPlugin.LIFECYCLE_CUSTOM_INDEX_METADATA_KEY; |
| 30 | + |
| 31 | +/** |
| 32 | + * Cluster state task that replaces a source index in a data stream with its downsample index. |
| 33 | + * In the process it will configure the origination date for the downsample index (so it can |
| 34 | + * have a correct generation time). |
| 35 | + */ |
| 36 | +public class ReplaceSourceWithDownsampleIndexTask implements ClusterStateTaskListener { |
| 37 | + private static final Logger LOGGER = LogManager.getLogger(ReplaceSourceWithDownsampleIndexTask.class); |
| 38 | + public static final String REPLACEMENT_SOURCE_INDEX = "replacement_source_index"; |
| 39 | + private ActionListener<Void> listener; |
| 40 | + private final String dataStreamName; |
| 41 | + private final String sourceBackingIndex; |
| 42 | + private final String downsampleIndex; |
| 43 | + |
| 44 | + public ReplaceSourceWithDownsampleIndexTask( |
| 45 | + String dataStreamName, |
| 46 | + String sourceBackingIndex, |
| 47 | + String downsampleIndex, |
| 48 | + ActionListener<Void> listener |
| 49 | + ) { |
| 50 | + this.dataStreamName = dataStreamName; |
| 51 | + this.sourceBackingIndex = sourceBackingIndex; |
| 52 | + this.downsampleIndex = downsampleIndex; |
| 53 | + this.listener = listener; |
| 54 | + } |
| 55 | + |
| 56 | + ClusterState execute(ClusterState state) { |
| 57 | + LOGGER.trace( |
| 58 | + "Updating cluster state to replace index [{}] with [{}] in data stream [{}]", |
| 59 | + sourceBackingIndex, |
| 60 | + downsampleIndex, |
| 61 | + dataStreamName |
| 62 | + ); |
| 63 | + IndexAbstraction sourceIndexAbstraction = state.metadata().getIndicesLookup().get(sourceBackingIndex); |
| 64 | + IndexMetadata downsampleIndexMeta = state.metadata().index(downsampleIndex); |
| 65 | + if (downsampleIndexMeta == null) { |
| 66 | + // the downsample index doesn't exist anymore so nothing to replace here |
| 67 | + LOGGER.trace( |
| 68 | + "Received request replace index [{}] with [{}] in data stream [{}] but the replacement index [{}] doesn't exist." |
| 69 | + + "Nothing to do here.", |
| 70 | + sourceBackingIndex, |
| 71 | + downsampleIndex, |
| 72 | + dataStreamName, |
| 73 | + downsampleIndex |
| 74 | + ); |
| 75 | + return state; |
| 76 | + } |
| 77 | + IndexMetadata sourceIndexMeta = state.metadata().index(sourceBackingIndex); |
| 78 | + DataStream dataStream = state.metadata().dataStreams().get(dataStreamName); |
| 79 | + if (sourceIndexAbstraction == null) { |
| 80 | + // index was deleted in the meantime, so let's check if we can make sure the downsample index ends up in the |
| 81 | + // data stream (if not already there) |
| 82 | + if (dataStream != null |
| 83 | + && dataStream.getIndices().stream().filter(index -> index.getName().equals(downsampleIndex)).findAny().isEmpty()) { |
| 84 | + // add downsample index to data stream |
| 85 | + LOGGER.trace( |
| 86 | + "unable find source index [{}] but adding index [{}] to data stream [{}]", |
| 87 | + sourceBackingIndex, |
| 88 | + downsampleIndex, |
| 89 | + dataStreamName |
| 90 | + ); |
| 91 | + Metadata.Builder newMetaData = Metadata.builder(state.metadata()) |
| 92 | + .put(dataStream.addBackingIndex(state.metadata(), downsampleIndexMeta.getIndex())); |
| 93 | + return ClusterState.builder(state).metadata(newMetaData).build(); |
| 94 | + } |
| 95 | + } else { |
| 96 | + // the source index exists |
| 97 | + DataStream sourceParentDataStream = sourceIndexAbstraction.getParentDataStream(); |
| 98 | + if (sourceParentDataStream != null) { |
| 99 | + assert sourceParentDataStream.getName().equals(dataStreamName) |
| 100 | + : "the backing index must be part of the provided data " |
| 101 | + + "stream [" |
| 102 | + + dataStreamName |
| 103 | + + "] but it is instead part of data stream [" |
| 104 | + + sourceParentDataStream.getName() |
| 105 | + + "]"; |
| 106 | + if (sourceParentDataStream.getWriteIndex().getName().equals(sourceBackingIndex)) { |
| 107 | + String errorMessage = String.format( |
| 108 | + Locale.ROOT, |
| 109 | + "index [%s] is the write index for data stream [%s] and cannot be replaced", |
| 110 | + sourceBackingIndex, |
| 111 | + sourceParentDataStream.getName() |
| 112 | + ); |
| 113 | + throw new IllegalStateException(errorMessage); |
| 114 | + } |
| 115 | + if (sourceIndexMeta != null) { |
| 116 | + // both indices exist, let's copy the origination date from the source index to the downsample index |
| 117 | + Metadata.Builder newMetaData = Metadata.builder(state.getMetadata()); |
| 118 | + TimeValue generationLifecycleDate = dataStream.getGenerationLifecycleDate(sourceIndexMeta); |
| 119 | + assert generationLifecycleDate != null : "write index must never be downsampled, or replaced"; |
| 120 | + IndexMetadata updatedDownsampleMetadata = copyDataStreamLifecycleState( |
| 121 | + sourceIndexMeta, |
| 122 | + downsampleIndexMeta, |
| 123 | + generationLifecycleDate.millis() |
| 124 | + ); |
| 125 | + |
| 126 | + newMetaData.put(updatedDownsampleMetadata, true); |
| 127 | + // replace source with downsample |
| 128 | + newMetaData.put(dataStream.replaceBackingIndex(sourceIndexMeta.getIndex(), downsampleIndexMeta.getIndex())); |
| 129 | + return ClusterState.builder(state).metadata(newMetaData).build(); |
| 130 | + } |
| 131 | + } else { |
| 132 | + // the source index is not part of a data stream, so let's check if we can make sure the downsample index ends up in the |
| 133 | + // data stream |
| 134 | + if (dataStream != null |
| 135 | + && dataStream.getIndices().stream().filter(index -> index.getName().equals(downsampleIndex)).findAny().isEmpty()) { |
| 136 | + Metadata.Builder newMetaData = Metadata.builder(state.getMetadata()); |
| 137 | + TimeValue generationLifecycleDate = dataStream.getGenerationLifecycleDate(sourceIndexMeta); |
| 138 | + assert generationLifecycleDate != null : "write index must never be downsampled, or replaced"; |
| 139 | + |
| 140 | + IndexMetadata updatedDownsampleMetadata = copyDataStreamLifecycleState( |
| 141 | + sourceIndexMeta, |
| 142 | + downsampleIndexMeta, |
| 143 | + generationLifecycleDate.millis() |
| 144 | + ); |
| 145 | + newMetaData.put(updatedDownsampleMetadata, true); |
| 146 | + // add downsample index to data stream |
| 147 | + newMetaData.put(dataStream.addBackingIndex(state.metadata(), downsampleIndexMeta.getIndex())); |
| 148 | + return ClusterState.builder(state).metadata(newMetaData).build(); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return state; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Copies the data stream lifecycle state information from the source index to the destination. |
| 158 | + * This ensures the destination index will have a generation time by setting the {@link IndexSettings#LIFECYCLE_ORIGINATION_DATE} and |
| 159 | + * that the source index is confingured in the |
| 160 | + * {@link org.elasticsearch.datastreams.DataStreamsPlugin#LIFECYCLE_CUSTOM_INDEX_METADATA_KEY} custom. |
| 161 | + */ |
| 162 | + private static IndexMetadata copyDataStreamLifecycleState( |
| 163 | + IndexMetadata source, |
| 164 | + IndexMetadata dest, |
| 165 | + long sourceIndexGenerationTimeMillis |
| 166 | + ) { |
| 167 | + IndexMetadata.Builder downsampleIndexBuilder = IndexMetadata.builder(dest); |
| 168 | + Map<String, String> lifecycleCustomMetadata = source.getCustomData(LIFECYCLE_CUSTOM_INDEX_METADATA_KEY); |
| 169 | + Map<String, String> newCustomMetadata = new HashMap<>(); |
| 170 | + if (lifecycleCustomMetadata != null) { |
| 171 | + newCustomMetadata.putAll(lifecycleCustomMetadata); |
| 172 | + } |
| 173 | + newCustomMetadata.put(REPLACEMENT_SOURCE_INDEX, source.getIndex().getName()); |
| 174 | + downsampleIndexBuilder.putCustom(LIFECYCLE_CUSTOM_INDEX_METADATA_KEY, newCustomMetadata); |
| 175 | + |
| 176 | + if (IndexSettings.LIFECYCLE_ORIGINATION_DATE_SETTING.exists(dest.getSettings()) == false) { |
| 177 | + downsampleIndexBuilder.settings( |
| 178 | + Settings.builder() |
| 179 | + .put(dest.getSettings()) |
| 180 | + .put(IndexSettings.LIFECYCLE_ORIGINATION_DATE, sourceIndexGenerationTimeMillis) |
| 181 | + .build() |
| 182 | + ).settingsVersion(dest.getSettingsVersion() + 1L); |
| 183 | + } |
| 184 | + return downsampleIndexBuilder.build(); |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public void onFailure(Exception e) { |
| 189 | + if (listener != null) { |
| 190 | + listener.onFailure(e); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + public String getDataStreamName() { |
| 195 | + return dataStreamName; |
| 196 | + } |
| 197 | + |
| 198 | + public String getSourceBackingIndex() { |
| 199 | + return sourceBackingIndex; |
| 200 | + } |
| 201 | + |
| 202 | + public String getDownsampleIndex() { |
| 203 | + return downsampleIndex; |
| 204 | + } |
| 205 | + |
| 206 | + public ActionListener<Void> getListener() { |
| 207 | + return listener; |
| 208 | + } |
| 209 | + |
| 210 | + public void setListener(ActionListener<Void> listener) { |
| 211 | + this.listener = listener; |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public boolean equals(Object o) { |
| 216 | + if (this == o) { |
| 217 | + return true; |
| 218 | + } |
| 219 | + if (o == null || getClass() != o.getClass()) { |
| 220 | + return false; |
| 221 | + } |
| 222 | + ReplaceSourceWithDownsampleIndexTask that = (ReplaceSourceWithDownsampleIndexTask) o; |
| 223 | + return Objects.equals(dataStreamName, that.dataStreamName) |
| 224 | + && Objects.equals(sourceBackingIndex, that.sourceBackingIndex) |
| 225 | + && Objects.equals(downsampleIndex, that.downsampleIndex); |
| 226 | + } |
| 227 | + |
| 228 | + @Override |
| 229 | + public int hashCode() { |
| 230 | + return Objects.hash(dataStreamName, sourceBackingIndex, downsampleIndex); |
| 231 | + } |
| 232 | +} |
0 commit comments