Skip to content

Commit 6c8d44b

Browse files
Allow data stream reindex tasks to be re-run after completion (elastic#122510) (elastic#123036)
* 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 f776ae0 commit 6c8d44b

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,10 +13,12 @@
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.injection.guice.Inject;
21+
import org.elasticsearch.persistent.PersistentTasksCustomMetadata;
2022
import org.elasticsearch.persistent.PersistentTasksService;
2123
import org.elasticsearch.tasks.Task;
2224
import org.elasticsearch.threadpool.ThreadPool;
@@ -37,13 +39,15 @@ public class ReindexDataStreamTransportAction extends HandledTransportAction<Rei
3739
private final PersistentTasksService persistentTasksService;
3840
private final TransportService transportService;
3941
private final ClusterService clusterService;
42+
private final Client client;
4043

4144
@Inject
4245
public ReindexDataStreamTransportAction(
4346
TransportService transportService,
4447
ActionFilters actionFilters,
4548
PersistentTasksService persistentTasksService,
46-
ClusterService clusterService
49+
ClusterService clusterService,
50+
Client client
4751
) {
4852
super(
4953
ReindexDataStreamAction.NAME,
@@ -56,6 +60,7 @@ public ReindexDataStreamTransportAction(
5660
this.transportService = transportService;
5761
this.persistentTasksService = persistentTasksService;
5862
this.clusterService = clusterService;
63+
this.client = client;
5964
}
6065

6166
@Override
@@ -77,6 +82,40 @@ protected void doExecute(Task task, ReindexDataStreamRequest request, ActionList
7782
ClientHelper.getPersistableSafeSecurityHeaders(transportService.getThreadPool().getThreadContext(), clusterService.state())
7883
);
7984
String persistentTaskId = getPersistentTaskId(sourceDataStreamName);
85+
86+
PersistentTasksCustomMetadata persistentTasksCustomMetadata = clusterService.state()
87+
.getMetadata()
88+
.custom(PersistentTasksCustomMetadata.TYPE);
89+
PersistentTasksCustomMetadata.PersistentTask<?> persistentTask = persistentTasksCustomMetadata.getTask(persistentTaskId);
90+
91+
if (persistentTask == null) {
92+
startTask(listener, persistentTaskId, params);
93+
} else {
94+
GetMigrationReindexStatusAction.Request statusRequest = new GetMigrationReindexStatusAction.Request(sourceDataStreamName);
95+
statusRequest.setParentTask(task.getParentTaskId());
96+
client.execute(
97+
GetMigrationReindexStatusAction.INSTANCE,
98+
statusRequest,
99+
listener.delegateFailureAndWrap((getListener, getResponse) -> {
100+
if (getResponse.getEnrichedStatus().complete() == false) {
101+
throw new ResourceAlreadyExistsException("Reindex task for data stream [{}] already exists", sourceDataStreamName);
102+
}
103+
CancelReindexDataStreamAction.Request cancelRequest = new CancelReindexDataStreamAction.Request(sourceDataStreamName);
104+
cancelRequest.setParentTask(task.getParentTaskId());
105+
client.execute(
106+
CancelReindexDataStreamAction.INSTANCE,
107+
cancelRequest,
108+
getListener.delegateFailureAndWrap(
109+
(cancelListener, cancelResponse) -> startTask(cancelListener, persistentTaskId, params)
110+
)
111+
);
112+
})
113+
);
114+
}
115+
116+
}
117+
118+
private void startTask(ActionListener<AcknowledgedResponse> listener, String persistentTaskId, ReindexDataStreamTaskParams params) {
80119
persistentTasksService.sendStartRequest(
81120
persistentTaskId,
82121
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
@@ -641,6 +641,11 @@ private void upgradeDataStream(String dataStreamName, int numRolloversOnOldClust
641641
assertThat(statusResponseString, ((List<Object>) statusResponseMap.get("errors")).size(), equalTo(expectedErrorCount));
642642
}
643643
}, 60, TimeUnit.SECONDS);
644+
645+
// Verify it's possible to reindex again after a successful reindex
646+
reindexResponse = upgradeUserClient.performRequest(reindexRequest);
647+
assertOK(reindexResponse);
648+
644649
Request cancelRequest = new Request("POST", "_migration/reindex/" + dataStreamName + "/_cancel");
645650
Response cancelResponse = upgradeUserClient.performRequest(cancelRequest);
646651
assertOK(cancelResponse);

0 commit comments

Comments
 (0)