Skip to content

Commit

Permalink
[Transform] Make validate calls cancellable
Browse files Browse the repository at this point in the history
Validate will initiate a search request.  In the event that the search
request needs to be cancelled, rather than manually stopping the task,
cancelling the Validate task will now propagate the cancel request to
the Search task.

Relate elastic#88010
  • Loading branch information
prwhelan committed Jul 16, 2024
1 parent 5f5893c commit 27666b6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.support.master.AcknowledgedTransportMasterNodeAction;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.client.internal.ParentTaskAssigningClient;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
Expand All @@ -25,6 +26,7 @@
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.persistent.PersistentTasksCustomMetadata;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xpack.core.ClientHelper;
Expand Down Expand Up @@ -110,9 +112,11 @@ protected void masterOperation(Task task, Request request, ClusterState clusterS
);

// <2> Validate source and destination indices

var parentTaskId = new TaskId(clusterService.localNode().getId(), task.getId());
ActionListener<Void> checkPrivilegesListener = validateTransformListener.delegateFailureAndWrap(
(l, aVoid) -> ClientHelper.executeAsyncWithOrigin(
client,
new ParentTaskAssigningClient(client, parentTaskId),
ClientHelper.TRANSFORM_ORIGIN,
ValidateTransformAction.INSTANCE,
new ValidateTransformAction.Request(config, request.isDeferValidation(), request.ackTimeout()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.client.internal.ParentTaskAssigningClient;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
Expand All @@ -31,6 +32,7 @@
import org.elasticsearch.persistent.PersistentTasksService;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xpack.core.ClientHelper;
Expand Down Expand Up @@ -126,23 +128,25 @@ protected TransportStartTransformAction(

@Override
protected void masterOperation(
Task ignoredTask,
Task task,
StartTransformAction.Request request,
ClusterState state,
ActionListener<StartTransformAction.Response> listener
) {
TransformNodes.warnIfNoTransformNodes(state);

final SetOnce<TransformTaskParams> transformTaskParamsHolder = new SetOnce<>();
final SetOnce<TransformConfig> transformConfigHolder = new SetOnce<>();
var transformTaskParamsHolder = new SetOnce<TransformTaskParams>();
var transformConfigHolder = new SetOnce<TransformConfig>();
var parentTaskId = new TaskId(clusterService.localNode().getId(), task.getId());
var parentClient = new ParentTaskAssigningClient(client, parentTaskId);

// <5> Wait for the allocated task's state to STARTED
ActionListener<PersistentTasksCustomMetadata.PersistentTask<TransformTaskParams>> newPersistentTaskActionListener = ActionListener
.wrap(task -> {
.wrap(t -> {
TransformTaskParams transformTask = transformTaskParamsHolder.get();
assert transformTask != null;
waitForTransformTaskStarted(
task.getId(),
t.getId(),
transformTask,
request.ackTimeout(),
ActionListener.wrap(taskStarted -> listener.onResponse(new StartTransformAction.Response(true)), listener::onFailure)
Expand Down Expand Up @@ -196,7 +200,7 @@ protected void masterOperation(
return;
}
TransformIndex.createDestinationIndex(
client,
parentClient,
auditor,
indexNameExpressionResolver,
state,
Expand Down Expand Up @@ -257,7 +261,7 @@ protected void masterOperation(
)
);
ClientHelper.executeAsyncWithOrigin(
client,
parentClient,
ClientHelper.TRANSFORM_ORIGIN,
ValidateTransformAction.INSTANCE,
new ValidateTransformAction.Request(config, false, request.ackTimeout()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.HandledTransportAction;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.client.internal.ParentTaskAssigningClient;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.node.DiscoveryNode;
Expand All @@ -23,15 +24,14 @@
import org.elasticsearch.license.License;
import org.elasticsearch.license.RemoteClusterLicenseChecker;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xpack.core.common.validation.SourceDestValidator;
import org.elasticsearch.xpack.core.transform.TransformDeprecations;
import org.elasticsearch.xpack.core.transform.TransformMessages;
import org.elasticsearch.xpack.core.transform.action.ValidateTransformAction;
import org.elasticsearch.xpack.core.transform.action.ValidateTransformAction.Request;
import org.elasticsearch.xpack.core.transform.action.ValidateTransformAction.Response;
import org.elasticsearch.xpack.core.transform.transforms.TransformConfig;
import org.elasticsearch.xpack.transform.transforms.Function;
import org.elasticsearch.xpack.transform.transforms.FunctionFactory;
import org.elasticsearch.xpack.transform.transforms.TransformNodes;
import org.elasticsearch.xpack.transform.utils.SourceDestValidations;
Expand Down Expand Up @@ -99,8 +99,10 @@ protected void doExecute(Task task, Request request, ActionListener<Response> li

TransformNodes.warnIfNoTransformNodes(clusterState);

final TransformConfig config = request.getConfig();
final Function function = FunctionFactory.create(config);
var config = request.getConfig();
var function = FunctionFactory.create(config);
var parentTaskId = new TaskId(clusterService.localNode().getId(), task.getId());
var parentClient = new ParentTaskAssigningClient(client, parentTaskId);

if (config.getVersion() == null || config.getVersion().before(TransformDeprecations.MIN_TRANSFORM_VERSION)) {
listener.onFailure(
Expand Down Expand Up @@ -130,7 +132,7 @@ protected void doExecute(Task task, Request request, ActionListener<Response> li
if (request.isDeferValidation()) {
deduceMappingsListener.onResponse(emptyMap());
} else {
function.deduceMappings(client, config.getHeaders(), config.getId(), config.getSource(), deduceMappingsListener);
function.deduceMappings(parentClient, config.getHeaders(), config.getId(), config.getSource(), deduceMappingsListener);
}
}, listener::onFailure);

Expand All @@ -139,7 +141,7 @@ protected void doExecute(Task task, Request request, ActionListener<Response> li
if (request.isDeferValidation()) {
validateQueryListener.onResponse(true);
} else {
function.validateQuery(client, config.getHeaders(), config.getSource(), request.ackTimeout(), validateQueryListener);
function.validateQuery(parentClient, config.getHeaders(), config.getSource(), request.ackTimeout(), validateQueryListener);
}
}, listener::onFailure);

Expand Down

0 comments on commit 27666b6

Please sign in to comment.