Skip to content

Commit 007149c

Browse files
Allow data stream reindex tasks to be re-run after completion (elastic#122510) (elastic#123035)
* Allow data stream reindex tasks to be re-run after completion * Docs update * Update docs/reference/migration/apis/data-stream-reindex.asciidoc --------- Co-authored-by: Keith Massey <[email protected]>
1 parent 96fd807 commit 007149c

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

docs/reference/migration/apis/data-stream-reindex.asciidoc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ from the original backing indices are copied to the resulting backing indices.
2121
This api runs in the background because reindexing all indices in a large data stream
2222
is expected to take a large amount of time and resources. The endpoint will return immediately and a persistent
2323
task will be created to run in the background. The current status of the task can be checked with
24-
the <<data-stream-reindex-status-api,reindex status API>>. This status will be available for 24 hours after the task completes, whether
25-
it finished successfully or failed. If the status is still available for a task, the task must be cancelled before it can be re-run.
26-
A running or recently completed data stream reindex task can be cancelled using the <<data-stream-reindex-cancel-api,reindex cancel API>>.
24+
the <<data-stream-reindex-status-api,reindex status API>>. This status will be available for 24 hours after the task
25+
completes, whether it finished successfully or failed. However, only the last status is retained so re-running a reindex
26+
will overwrite the previous status for that data stream. A running or recently completed data stream reindex task can be
27+
cancelled using the <<data-stream-reindex-cancel-api,reindex cancel API>>.
2728

2829
///////////////////////////////////////////////////////////
2930
[source,console]

x-pack/plugin/migrate/src/main/java/org/elasticsearch/xpack/migrate/action/ReindexDataStreamTransportAction.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
import org.elasticsearch.action.support.ActionFilters;
1414
import org.elasticsearch.action.support.HandledTransportAction;
1515
import org.elasticsearch.action.support.master.AcknowledgedResponse;
16+
import org.elasticsearch.client.internal.Client;
1617
import org.elasticsearch.cluster.metadata.DataStream;
1718
import org.elasticsearch.cluster.metadata.Metadata;
1819
import org.elasticsearch.cluster.service.ClusterService;
1920
import org.elasticsearch.core.TimeValue;
2021
import org.elasticsearch.injection.guice.Inject;
22+
import org.elasticsearch.persistent.PersistentTasksCustomMetadata;
2123
import org.elasticsearch.persistent.PersistentTasksService;
2224
import org.elasticsearch.tasks.Task;
2325
import org.elasticsearch.threadpool.ThreadPool;
@@ -38,13 +40,15 @@ public class ReindexDataStreamTransportAction extends HandledTransportAction<Rei
3840
private final PersistentTasksService persistentTasksService;
3941
private final TransportService transportService;
4042
private final ClusterService clusterService;
43+
private final Client client;
4144

4245
@Inject
4346
public ReindexDataStreamTransportAction(
4447
TransportService transportService,
4548
ActionFilters actionFilters,
4649
PersistentTasksService persistentTasksService,
47-
ClusterService clusterService
50+
ClusterService clusterService,
51+
Client client
4852
) {
4953
super(
5054
ReindexDataStreamAction.NAME,
@@ -57,6 +61,7 @@ public ReindexDataStreamTransportAction(
5761
this.transportService = transportService;
5862
this.persistentTasksService = persistentTasksService;
5963
this.clusterService = clusterService;
64+
this.client = client;
6065
}
6166

6267
@Override
@@ -78,6 +83,40 @@ protected void doExecute(Task task, ReindexDataStreamRequest request, ActionList
7883
ClientHelper.getPersistableSafeSecurityHeaders(transportService.getThreadPool().getThreadContext(), clusterService.state())
7984
);
8085
String persistentTaskId = getPersistentTaskId(sourceDataStreamName);
86+
87+
PersistentTasksCustomMetadata persistentTasksCustomMetadata = clusterService.state()
88+
.getMetadata()
89+
.custom(PersistentTasksCustomMetadata.TYPE);
90+
PersistentTasksCustomMetadata.PersistentTask<?> persistentTask = persistentTasksCustomMetadata.getTask(persistentTaskId);
91+
92+
if (persistentTask == null) {
93+
startTask(listener, persistentTaskId, params);
94+
} else {
95+
GetMigrationReindexStatusAction.Request statusRequest = new GetMigrationReindexStatusAction.Request(sourceDataStreamName);
96+
statusRequest.setParentTask(task.getParentTaskId());
97+
client.execute(
98+
GetMigrationReindexStatusAction.INSTANCE,
99+
statusRequest,
100+
listener.delegateFailureAndWrap((getListener, getResponse) -> {
101+
if (getResponse.getEnrichedStatus().complete() == false) {
102+
throw new ResourceAlreadyExistsException("Reindex task for data stream [{}] already exists", sourceDataStreamName);
103+
}
104+
CancelReindexDataStreamAction.Request cancelRequest = new CancelReindexDataStreamAction.Request(sourceDataStreamName);
105+
cancelRequest.setParentTask(task.getParentTaskId());
106+
client.execute(
107+
CancelReindexDataStreamAction.INSTANCE,
108+
cancelRequest,
109+
getListener.delegateFailureAndWrap(
110+
(cancelListener, cancelResponse) -> startTask(cancelListener, persistentTaskId, params)
111+
)
112+
);
113+
})
114+
);
115+
}
116+
117+
}
118+
119+
private void startTask(ActionListener<AcknowledgedResponse> listener, String persistentTaskId, ReindexDataStreamTaskParams params) {
81120
persistentTasksService.sendStartRequest(
82121
persistentTaskId,
83122
ReindexDataStreamTask.TASK_NAME,

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/DataStreamsUpgradeIT.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,11 @@ private void upgradeDataStream(String dataStreamName, int numRolloversOnOldClust
623623
assertThat(statusResponseString, ((List<Object>) statusResponseMap.get("errors")).size(), equalTo(expectedErrorCount));
624624
}
625625
}, 60, TimeUnit.SECONDS);
626+
627+
// Verify it's possible to reindex again after a successful reindex
628+
reindexResponse = upgradeUserClient.performRequest(reindexRequest);
629+
assertOK(reindexResponse);
630+
626631
Request cancelRequest = new Request("POST", "_migration/reindex/" + dataStreamName + "/_cancel");
627632
Response cancelResponse = upgradeUserClient.performRequest(cancelRequest);
628633
assertOK(cancelResponse);

0 commit comments

Comments
 (0)