forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a migration reindex cancel API (elastic#118291)
This introduces the migration reindex cancel API, which cancels a migration reindex task for a given data stream name that was started with elastic#118109. For example: ``` POST localhost:9200/_migration/reindex/my-data-stream/_cancel?pretty ``` returns ``` { "acknowledged" : true } ``` This cancels the task, and cancels any ongoing reindexing of backing indices, but does not do any cleanup.
- Loading branch information
Showing
13 changed files
with
371 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
pr: 118291 | ||
summary: Adding a migration reindex cancel API | ||
area: Data streams | ||
type: enhancement | ||
issues: [] |
30 changes: 30 additions & 0 deletions
30
rest-api-spec/src/main/resources/rest-api-spec/api/migrate.cancel_reindex.json
This file contains 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,30 @@ | ||
{ | ||
"migrate.cancel_reindex":{ | ||
"documentation":{ | ||
"url":"https://www.elastic.co/guide/en/elasticsearch/reference/master/data-stream-reindex.html", | ||
"description":"This API returns the status of a migration reindex attempt for a data stream or index" | ||
}, | ||
"stability":"experimental", | ||
"visibility":"private", | ||
"headers":{ | ||
"accept": [ "application/json"], | ||
"content_type": ["application/json"] | ||
}, | ||
"url":{ | ||
"paths":[ | ||
{ | ||
"path":"/_migration/reindex/{index}/_cancel", | ||
"methods":[ | ||
"POST" | ||
], | ||
"parts":{ | ||
"index":{ | ||
"type":"string", | ||
"description":"The index or data stream name" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains 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 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
90 changes: 90 additions & 0 deletions
90
...e/src/main/java/org/elasticsearch/xpack/migrate/action/CancelReindexDataStreamAction.java
This file contains 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,90 @@ | ||
/* | ||
* 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.ActionRequest; | ||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.action.ActionType; | ||
import org.elasticsearch.action.IndicesRequest; | ||
import org.elasticsearch.action.support.IndicesOptions; | ||
import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class CancelReindexDataStreamAction extends ActionType<AcknowledgedResponse> { | ||
|
||
public static final CancelReindexDataStreamAction INSTANCE = new CancelReindexDataStreamAction(); | ||
public static final String NAME = "indices:admin/data_stream/reindex_cancel"; | ||
|
||
public CancelReindexDataStreamAction() { | ||
super(NAME); | ||
} | ||
|
||
public static class Request extends ActionRequest implements IndicesRequest { | ||
private final String index; | ||
|
||
public Request(String index) { | ||
super(); | ||
this.index = index; | ||
} | ||
|
||
public Request(StreamInput in) throws IOException { | ||
super(in); | ||
this.index = in.readString(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeString(index); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean getShouldStoreResult() { | ||
return true; | ||
} | ||
|
||
public String getIndex() { | ||
return index; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(index); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other instanceof Request && index.equals(((Request) other).index); | ||
} | ||
|
||
public Request nodeRequest(String thisNodeId, long thisTaskId) { | ||
Request copy = new Request(index); | ||
copy.setParentTask(thisNodeId, thisTaskId); | ||
return copy; | ||
} | ||
|
||
@Override | ||
public String[] indices() { | ||
return new String[] { index }; | ||
} | ||
|
||
@Override | ||
public IndicesOptions indicesOptions() { | ||
return IndicesOptions.strictSingleIndexNoExpandForbidClosed(); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...n/java/org/elasticsearch/xpack/migrate/action/CancelReindexDataStreamTransportAction.java
This file contains 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,57 @@ | ||
/* | ||
* 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.ActionListener; | ||
import org.elasticsearch.action.support.ActionFilters; | ||
import org.elasticsearch.action.support.HandledTransportAction; | ||
import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
import org.elasticsearch.common.util.concurrent.EsExecutors; | ||
import org.elasticsearch.core.TimeValue; | ||
import org.elasticsearch.injection.guice.Inject; | ||
import org.elasticsearch.persistent.PersistentTasksCustomMetadata; | ||
import org.elasticsearch.persistent.PersistentTasksService; | ||
import org.elasticsearch.tasks.Task; | ||
import org.elasticsearch.transport.TransportService; | ||
import org.elasticsearch.xpack.migrate.action.CancelReindexDataStreamAction.Request; | ||
|
||
public class CancelReindexDataStreamTransportAction extends HandledTransportAction<Request, AcknowledgedResponse> { | ||
private final PersistentTasksService persistentTasksService; | ||
|
||
@Inject | ||
public CancelReindexDataStreamTransportAction( | ||
TransportService transportService, | ||
ActionFilters actionFilters, | ||
PersistentTasksService persistentTasksService | ||
) { | ||
super(CancelReindexDataStreamAction.NAME, transportService, actionFilters, Request::new, EsExecutors.DIRECT_EXECUTOR_SERVICE); | ||
this.persistentTasksService = persistentTasksService; | ||
} | ||
|
||
@Override | ||
protected void doExecute(Task task, Request request, ActionListener<AcknowledgedResponse> listener) { | ||
String index = request.getIndex(); | ||
String persistentTaskId = ReindexDataStreamAction.TASK_ID_PREFIX + index; | ||
/* | ||
* This removes the persistent task from the cluster state and results in the running task being cancelled (but not removed from | ||
* the task manager). The running task is removed from the task manager in ReindexDataStreamTask::onCancelled, which is called as | ||
* as result of this. | ||
*/ | ||
persistentTasksService.sendRemoveRequest(persistentTaskId, TimeValue.MAX_VALUE, new ActionListener<>() { | ||
@Override | ||
public void onResponse(PersistentTasksCustomMetadata.PersistentTask<?> persistentTask) { | ||
listener.onResponse(AcknowledgedResponse.TRUE); | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
listener.onFailure(e); | ||
} | ||
}); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...src/main/java/org/elasticsearch/xpack/migrate/rest/RestCancelReindexDataStreamAction.java
This file contains 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,39 @@ | ||
/* | ||
* 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.rest; | ||
|
||
import org.elasticsearch.client.internal.node.NodeClient; | ||
import org.elasticsearch.rest.BaseRestHandler; | ||
import org.elasticsearch.rest.RestRequest; | ||
import org.elasticsearch.rest.action.RestToXContentListener; | ||
import org.elasticsearch.xpack.migrate.action.CancelReindexDataStreamAction; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static org.elasticsearch.rest.RestRequest.Method.POST; | ||
|
||
public class RestCancelReindexDataStreamAction extends BaseRestHandler { | ||
|
||
@Override | ||
public String getName() { | ||
return "cancel_reindex_data_stream_action"; | ||
} | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return List.of(new Route(POST, "/_migration/reindex/{index}/_cancel")); | ||
} | ||
|
||
@Override | ||
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { | ||
String index = request.param("index"); | ||
CancelReindexDataStreamAction.Request cancelTaskRequest = new CancelReindexDataStreamAction.Request(index); | ||
return channel -> client.execute(CancelReindexDataStreamAction.INSTANCE, cancelTaskRequest, new RestToXContentListener<>(channel)); | ||
} | ||
} |
This file contains 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 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.