-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Add action to copy index metadata when reindexing data stream indices #122535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
parkertimmins
merged 21 commits into
elastic:main
from
parkertimmins:reindex-data-stream-copy-index-metadata
Feb 14, 2025
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f460716
Setup index metadata update action skeleton
parkertimmins c3d9973
copy index metadata from source to dest
parkertimmins 47384d2
Set ilm and data stream lifecycle on dest index metadata
parkertimmins dbdc51d
Add create date, call from reindex
parkertimmins 0da5a08
Add IT tests
parkertimmins 9a7d620
Merge branch 'main' into reindex-data-stream-copy-index-metadata
parkertimmins d0c18c5
Add test for creation date
parkertimmins b0a593f
Merge branch 'main' into reindex-data-stream-copy-index-metadata
parkertimmins bc54710
rollover info test
parkertimmins b06098a
trying add ilm tests
parkertimmins 5917abc
Merge branch 'main' into reindex-data-stream-copy-index-metadata
parkertimmins 68b070a
add ilm tests
parkertimmins 0a171ed
[CI] Auto commit changes from spotless
cc3558a
Fix ILM state tests
parkertimmins 5d4b95e
Merge branch 'main' into reindex-data-stream-copy-index-metadata
parkertimmins 37f7f4d
unneeded code
parkertimmins 21355d5
[CI] Auto commit changes from spotless
87b8c4b
Fixes from review:
parkertimmins a8f7edc
Fix DataStreamsUpgradeIT ILM test
parkertimmins 8f77d59
Remove references to reindex data stream feature flag
parkertimmins f2f57c4
[CI] Auto commit changes from spotless
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
290 changes: 290 additions & 0 deletions
290
...a/org/elasticsearch/xpack/migrate/action/CopyLifecycleIndexMetadataTransportActionIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,290 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.migrate.action; | ||
|
|
||
| import org.elasticsearch.action.DocWriteRequest; | ||
| import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; | ||
| import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; | ||
| import org.elasticsearch.action.admin.indices.get.GetIndexRequest; | ||
| import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; | ||
| import org.elasticsearch.action.admin.indices.rollover.RolloverRequest; | ||
| import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; | ||
| import org.elasticsearch.action.admin.indices.template.put.TransportPutComposableIndexTemplateAction; | ||
| import org.elasticsearch.action.datastreams.CreateDataStreamAction; | ||
| import org.elasticsearch.action.index.IndexRequest; | ||
| import org.elasticsearch.action.support.IndicesOptions; | ||
| import org.elasticsearch.action.support.master.AcknowledgedRequest; | ||
| import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; | ||
| import org.elasticsearch.cluster.metadata.IndexMetadata; | ||
| import org.elasticsearch.cluster.metadata.LifecycleExecutionState; | ||
| import org.elasticsearch.cluster.metadata.Metadata; | ||
| import org.elasticsearch.cluster.metadata.Template; | ||
| import org.elasticsearch.common.compress.CompressedXContent; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.core.TimeValue; | ||
| import org.elasticsearch.datastreams.DataStreamsPlugin; | ||
| import org.elasticsearch.ingest.common.IngestCommonPlugin; | ||
| import org.elasticsearch.plugins.Plugin; | ||
| import org.elasticsearch.test.ESIntegTestCase; | ||
| import org.elasticsearch.xcontent.json.JsonXContent; | ||
| import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; | ||
| import org.elasticsearch.xpack.core.ilm.LifecyclePolicy; | ||
| import org.elasticsearch.xpack.core.ilm.LifecycleSettings; | ||
| import org.elasticsearch.xpack.core.ilm.OperationMode; | ||
| import org.elasticsearch.xpack.core.ilm.Phase; | ||
| import org.elasticsearch.xpack.core.ilm.StartILMRequest; | ||
| import org.elasticsearch.xpack.core.ilm.StopILMRequest; | ||
| import org.elasticsearch.xpack.core.ilm.action.GetStatusAction; | ||
| import org.elasticsearch.xpack.core.ilm.action.ILMActions; | ||
| import org.elasticsearch.xpack.core.ilm.action.PutLifecycleRequest; | ||
| import org.elasticsearch.xpack.ilm.IndexLifecycle; | ||
| import org.elasticsearch.xpack.migrate.MigratePlugin; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | ||
|
|
||
| public class CopyLifecycleIndexMetadataTransportActionIT extends ESIntegTestCase { | ||
|
|
||
| @Override | ||
| protected Collection<Class<? extends Plugin>> nodePlugins() { | ||
| return List.of( | ||
| LocalStateCompositeXPackPlugin.class, | ||
| MigratePlugin.class, | ||
| DataStreamsPlugin.class, | ||
| IngestCommonPlugin.class, | ||
| IndexLifecycle.class | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { | ||
| return Settings.builder() | ||
| .put(super.nodeSettings(nodeOrdinal, otherSettings)) | ||
| .put(LifecycleSettings.LIFECYCLE_POLL_INTERVAL, "1s") | ||
| // This just generates less churn and makes it easier to read the log file if needed | ||
| .put(LifecycleSettings.LIFECYCLE_HISTORY_INDEX_ENABLED, false) | ||
| .build(); | ||
| } | ||
|
|
||
| public void testCreationDate() { | ||
| var sourceIndex = randomAlphaOfLength(20).toLowerCase(Locale.ROOT); | ||
| safeGet(indicesAdmin().create(new CreateIndexRequest(sourceIndex))); | ||
|
|
||
| // so creation date is different | ||
| safeSleep(2); | ||
|
|
||
| var destIndex = randomAlphaOfLength(20).toLowerCase(Locale.ROOT); | ||
| safeGet(indicesAdmin().create(new CreateIndexRequest(destIndex))); | ||
|
|
||
| // verify source and dest date are actually different before copying | ||
| var settingsResponse = indicesAdmin().getSettings(new GetSettingsRequest().indices(sourceIndex, destIndex)).actionGet(); | ||
| var indexToSettings = settingsResponse.getIndexToSettings(); | ||
| var sourceDate = indexToSettings.get(sourceIndex).getAsLong(IndexMetadata.SETTING_CREATION_DATE, 0L); | ||
| { | ||
| var destDate = indexToSettings.get(destIndex).getAsLong(IndexMetadata.SETTING_CREATION_DATE, 0L); | ||
| assertTrue(sourceDate > 0); | ||
| assertTrue(destDate > 0); | ||
| assertNotEquals(sourceDate, destDate); | ||
| } | ||
|
|
||
| // copy over the metadata | ||
| copyMetadata(sourceIndex, destIndex); | ||
|
|
||
| var destDate = indicesAdmin().getSettings(new GetSettingsRequest().indices(sourceIndex, destIndex)) | ||
| .actionGet() | ||
| .getIndexToSettings() | ||
| .get(destIndex) | ||
| .getAsLong(IndexMetadata.SETTING_CREATION_DATE, 0L); | ||
| assertEquals(sourceDate, destDate); | ||
| } | ||
|
|
||
| public void testILMState() throws Exception { | ||
|
|
||
| Map<String, Phase> phases = Map.of( | ||
| "hot", | ||
| new Phase( | ||
| "hot", | ||
| TimeValue.ZERO, | ||
| Map.of( | ||
| "rollover", | ||
| new org.elasticsearch.xpack.core.ilm.RolloverAction(null, null, null, 1L, null, null, null, null, null, null) | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| var policyName = "my-policy"; | ||
| LifecyclePolicy policy = new LifecyclePolicy(policyName, phases); | ||
| PutLifecycleRequest putLifecycleRequest = new PutLifecycleRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, policy); | ||
| assertAcked(client().execute(ILMActions.PUT, putLifecycleRequest).actionGet()); | ||
|
|
||
| // create data stream with a document and wait for ILM to roll it over | ||
| var dataStream = createDataStream(policyName); | ||
| createDocument(dataStream); | ||
| assertAcked(safeGet(client().execute(ILMActions.START, new StartILMRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)))); | ||
| assertBusy(() -> { | ||
| var getIndexResponse = safeGet(indicesAdmin().getIndex(new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(dataStream))); | ||
| assertTrue(getIndexResponse.indices().length > 1); | ||
| }); | ||
| // stop ILM so source does not change after copying metadata | ||
| assertAcked(safeGet(client().execute(ILMActions.STOP, new StopILMRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)))); | ||
| assertBusy(() -> { | ||
| var statusResponse = safeGet( | ||
| client().execute(GetStatusAction.INSTANCE, new AcknowledgedRequest.Plain(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)) | ||
| ); | ||
| assertEquals(OperationMode.STOPPED, statusResponse.getMode()); | ||
| }); | ||
|
|
||
| var getIndexResponse = safeGet(indicesAdmin().getIndex(new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(dataStream))); | ||
| for (var backingIndex : getIndexResponse.indices()) { | ||
| var destIndex = randomAlphaOfLength(20).toLowerCase(Locale.ROOT); | ||
| safeGet(indicesAdmin().create(new CreateIndexRequest(destIndex))); | ||
|
|
||
| IndexMetadata destBefore = getClusterMetadata(destIndex).index(destIndex); | ||
| assertNull(destBefore.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY)); | ||
|
|
||
| // copy over the metadata | ||
| copyMetadata(backingIndex, destIndex); | ||
|
|
||
| var metadataAfter = getClusterMetadata(backingIndex, destIndex); | ||
| IndexMetadata sourceAfter = metadataAfter.index(backingIndex); | ||
| IndexMetadata destAfter = metadataAfter.index(destIndex); | ||
| assertNotNull(destAfter.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY)); | ||
| assertEquals( | ||
| sourceAfter.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY), | ||
| destAfter.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY) | ||
| ); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| public void testRolloverInfos() throws Exception { | ||
| var dataStream = createDataStream(null); | ||
|
|
||
| // rollover a few times | ||
| createDocument(dataStream); | ||
| rollover(dataStream); | ||
| createDocument(dataStream); | ||
| rollover(dataStream); | ||
| createDocument(dataStream); | ||
| var writeIndex = rollover(dataStream); | ||
|
|
||
| var getIndexResponse = safeGet(indicesAdmin().getIndex(new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(dataStream))); | ||
| for (var backingIndex : getIndexResponse.indices()) { | ||
|
|
||
| var destIndex = randomAlphaOfLength(20).toLowerCase(Locale.ROOT); | ||
| safeGet(indicesAdmin().create(new CreateIndexRequest(destIndex))); | ||
|
|
||
| var metadataBefore = getClusterMetadata(backingIndex, destIndex); | ||
| IndexMetadata source = metadataBefore.index(backingIndex); | ||
| IndexMetadata destBefore = metadataBefore.index(destIndex); | ||
|
|
||
| // sanity check not equal before the copy | ||
| if (backingIndex.equals(writeIndex)) { | ||
| assertTrue(source.getRolloverInfos().isEmpty()); | ||
| assertTrue(destBefore.getRolloverInfos().isEmpty()); | ||
| } else { | ||
| assertNotEquals(source.getRolloverInfos(), destBefore.getRolloverInfos()); | ||
| } | ||
|
|
||
| // copy over the metadata | ||
| copyMetadata(backingIndex, destIndex); | ||
|
|
||
| // now rollover info should be equal | ||
| IndexMetadata destAfter = getClusterMetadata(destIndex).index(destIndex); | ||
| assertEquals(source.getRolloverInfos(), destAfter.getRolloverInfos()); | ||
| } | ||
| } | ||
|
|
||
| private String createDataStream(String ilmPolicy) throws Exception { | ||
| String dataStreamName = randomAlphaOfLength(10).toLowerCase(Locale.getDefault()); | ||
|
|
||
| Settings settings = ilmPolicy != null ? Settings.builder().put(IndexMetadata.LIFECYCLE_NAME, ilmPolicy).build() : null; | ||
|
|
||
| String mapping = """ | ||
| { | ||
| "properties": { | ||
| "@timestamp": { | ||
| "type":"date" | ||
| }, | ||
| "data":{ | ||
| "type":"keyword" | ||
| } | ||
| } | ||
| } | ||
| """; | ||
| Template idxTemplate = new Template(settings, new CompressedXContent(mapping), null); | ||
|
|
||
| ComposableIndexTemplate template = ComposableIndexTemplate.builder() | ||
| .indexPatterns(List.of(dataStreamName + "*")) | ||
| .template(idxTemplate) | ||
| .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(false, false)) | ||
| .build(); | ||
|
|
||
| assertAcked( | ||
| client().execute( | ||
| TransportPutComposableIndexTemplateAction.TYPE, | ||
| new TransportPutComposableIndexTemplateAction.Request(dataStreamName + "_template").indexTemplate(template) | ||
| ) | ||
| ); | ||
| assertAcked( | ||
| client().execute( | ||
| CreateDataStreamAction.INSTANCE, | ||
| new CreateDataStreamAction.Request(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, dataStreamName) | ||
| ) | ||
| ); | ||
| return dataStreamName; | ||
| } | ||
|
|
||
| private long createDocument(String dataStreamName) throws Exception { | ||
| // Get some randomized but reasonable timestamps on the data since not all of it is guaranteed to arrive in order. | ||
| long timeSeed = System.currentTimeMillis(); | ||
| long timestamp = randomLongBetween(timeSeed - TimeUnit.HOURS.toMillis(5), timeSeed); | ||
| safeGet( | ||
| client().index( | ||
| new IndexRequest(dataStreamName).opType(DocWriteRequest.OpType.CREATE) | ||
| .source( | ||
| JsonXContent.contentBuilder() | ||
| .startObject() | ||
| .field("@timestamp", timestamp) | ||
| .field("data", randomAlphaOfLength(25)) | ||
| .endObject() | ||
| ) | ||
| ) | ||
| ); | ||
| safeGet( | ||
| indicesAdmin().refresh( | ||
| new RefreshRequest(".ds-" + dataStreamName + "*").indicesOptions(IndicesOptions.lenientExpandOpenHidden()) | ||
| ) | ||
| ); | ||
| return timestamp; | ||
| } | ||
|
|
||
| private void copyMetadata(String sourceIndex, String destIndex) { | ||
| assertAcked( | ||
| client().execute( | ||
| CopyLifecycleIndexMetadataAction.INSTANCE, | ||
| new CopyLifecycleIndexMetadataAction.Request(TEST_REQUEST_TIMEOUT, sourceIndex, destIndex) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| private String rollover(String dataStream) { | ||
| var rolloverResponse = safeGet(indicesAdmin().rolloverIndex(new RolloverRequest(dataStream, null))); | ||
| assertTrue(rolloverResponse.isAcknowledged()); | ||
| return rolloverResponse.getNewIndex(); | ||
| } | ||
|
|
||
| private Metadata getClusterMetadata(String... indices) { | ||
| return safeGet(clusterAdmin().state(new ClusterStateRequest(TEST_REQUEST_TIMEOUT).indices(indices))).getState().metadata(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clearing the existing infos is a bit weirds, but seems better than making rolloverInfos non-final