Skip to content

Commit

Permalink
[ML] make ML stats APIs cancellable (#88030)
Browse files Browse the repository at this point in the history
Many machine learning stats APIs make multiple searches per call. Making them cancellable allows for those searches to be cancelled if the HTTP connection is closed.

This improves scalability and performance.

Relates #88010
  • Loading branch information
benwtrent authored Jun 28, 2022
1 parent aa7f8cb commit 6b0beb7
Show file tree
Hide file tree
Showing 30 changed files with 400 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
Expand All @@ -39,6 +41,8 @@
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.core.Strings.format;

public class GetDataFrameAnalyticsStatsAction extends ActionType<GetDataFrameAnalyticsStatsAction.Response> {

public static final GetDataFrameAnalyticsStatsAction INSTANCE = new GetDataFrameAnalyticsStatsAction();
Expand Down Expand Up @@ -141,6 +145,11 @@ public boolean equals(Object obj) {
Request other = (Request) obj;
return Objects.equals(id, other.id) && allowNoMatch == other.allowNoMatch && Objects.equals(pageParams, other.pageParams);
}

@Override
public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
return new CancellableTask(id, type, action, format("get_data_frame_analytics_stats[%s]", id), parentTaskId, headers);
}
}

public static class Response extends BaseTasksResponse implements ToXContentObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.ml.MlTasks;
Expand All @@ -29,6 +31,8 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.elasticsearch.core.Strings.format;

/**
* Internal only action to get the current running state of a datafeed
*/
Expand Down Expand Up @@ -68,6 +72,11 @@ public Set<String> getDatafeedTaskIds() {
public boolean match(Task task) {
return task instanceof StartDatafeedAction.DatafeedTaskMatcher && datafeedTaskIds.contains(task.getDescription());
}

@Override
public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
return new CancellableTask(id, type, action, format("get_datafeed_running_state[%s]", datafeedTaskIds), parentTaskId, headers);
}
}

public static class Response extends BaseTasksResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.persistent.PersistentTasksCustomMetadata;
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.action.AbstractGetResourcesResponse;
Expand All @@ -36,6 +39,8 @@
import java.util.Objects;
import java.util.stream.Collectors;

import static org.elasticsearch.core.Strings.format;

public class GetDatafeedsStatsAction extends ActionType<GetDatafeedsStatsAction.Response> {

public static final GetDatafeedsStatsAction INSTANCE = new GetDatafeedsStatsAction();
Expand Down Expand Up @@ -114,6 +119,11 @@ public boolean equals(Object obj) {
Request other = (Request) obj;
return Objects.equals(datafeedId, other.datafeedId) && Objects.equals(allowNoMatch, other.allowNoMatch);
}

@Override
public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
return new CancellableTask(id, type, action, format("get_datafeed_stats[%s]", datafeedId), parentTaskId, headers);
}
}

public static class Response extends AbstractGetResourcesResponse<Response.DatafeedStats> implements ToXContentObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
Expand All @@ -28,6 +31,7 @@
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.core.Strings.format;
import static org.elasticsearch.xpack.core.ml.action.UpgradeJobModelSnapshotAction.Request.SNAPSHOT_ID;

public class GetJobModelSnapshotsUpgradeStatsAction extends ActionType<GetJobModelSnapshotsUpgradeStatsAction.Response> {
Expand Down Expand Up @@ -115,6 +119,18 @@ public boolean equals(Object obj) {
&& Objects.equals(snapshotId, other.snapshotId)
&& Objects.equals(allowNoMatch, other.allowNoMatch);
}

@Override
public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
return new CancellableTask(
id,
type,
action,
format("get_job_model_snapshot_upgrade_stats[%s:%s]", jobId, snapshotId),
parentTaskId,
headers
);
}
}

public static class Response extends AbstractGetResourcesResponse<Response.JobModelSnapshotUpgradeStats> implements ToXContentObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.action.util.QueryPage;
Expand All @@ -38,6 +40,8 @@
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.core.Strings.format;

public class GetJobsStatsAction extends ActionType<GetJobsStatsAction.Response> {

public static final GetJobsStatsAction INSTANCE = new GetJobsStatsAction();
Expand Down Expand Up @@ -134,6 +138,11 @@ public boolean equals(Object obj) {
&& Objects.equals(allowNoMatch, other.allowNoMatch)
&& Objects.equals(getTimeout(), other.getTimeout());
}

@Override
public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
return new CancellableTask(id, type, action, format("get_job_stats[%s]", id), parentTaskId, headers);
}
}

public static class Response extends BaseTasksResponse implements ToXContentObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,10 @@ public void testUpdateWithValidatorFunctionThatErrors() throws Exception {
List<String> updateIndices = Collections.singletonList("a-different-index");
update.setIndices(updateIndices);

BiConsumer<DatafeedConfig, ActionListener<Boolean>> validateErrorFunction = (updatedConfig, listener) -> {
new Thread(() -> listener.onFailure(new IllegalArgumentException("this is a bad update")), getTestName()).start();
};
BiConsumer<DatafeedConfig, ActionListener<Boolean>> validateErrorFunction = (updatedConfig, listener) -> new Thread(
() -> listener.onFailure(new IllegalArgumentException("this is a bad update")),
getTestName()
).start();

AtomicReference<DatafeedConfig> configHolder = new AtomicReference<>();
AtomicReference<Exception> exceptionHolder = new AtomicReference<>();
Expand All @@ -277,7 +278,7 @@ public void testAllowNoMatch() throws InterruptedException {
AtomicReference<Exception> exceptionHolder = new AtomicReference<>();

blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedIds("_all", false, null, false, actionListener),
actionListener -> datafeedConfigProvider.expandDatafeedIds("_all", false, null, false, null, actionListener),
datafeedIdsHolder,
exceptionHolder
);
Expand All @@ -289,7 +290,7 @@ public void testAllowNoMatch() throws InterruptedException {

exceptionHolder.set(null);
blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedIds("_all", true, null, false, actionListener),
actionListener -> datafeedConfigProvider.expandDatafeedIds("_all", true, null, false, null, actionListener),
datafeedIdsHolder,
exceptionHolder
);
Expand All @@ -298,7 +299,7 @@ public void testAllowNoMatch() throws InterruptedException {

AtomicReference<List<DatafeedConfig.Builder>> datafeedsHolder = new AtomicReference<>();
blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", false, actionListener),
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", false, null, actionListener),
datafeedsHolder,
exceptionHolder
);
Expand All @@ -310,7 +311,7 @@ public void testAllowNoMatch() throws InterruptedException {

exceptionHolder.set(null);
blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", true, actionListener),
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", true, null, actionListener),
datafeedsHolder,
exceptionHolder
);
Expand All @@ -329,53 +330,59 @@ public void testExpandDatafeeds() throws Exception {

// Test datafeed IDs only
SortedSet<String> expandedIds = blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedIds("foo*", true, null, false, actionListener)
actionListener -> datafeedConfigProvider.expandDatafeedIds("foo*", true, null, false, null, actionListener)
);
assertEquals(new TreeSet<>(Arrays.asList("foo-1", "foo-2")), expandedIds);

expandedIds = blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("*-1", true, null, false, actionListener));
expandedIds = blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedIds("*-1", true, null, false, null, actionListener)
);
assertEquals(new TreeSet<>(Arrays.asList("bar-1", "foo-1")), expandedIds);

expandedIds = blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("bar*", true, null, false, actionListener));
expandedIds = blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedIds("bar*", true, null, false, null, actionListener)
);
assertEquals(new TreeSet<>(Arrays.asList("bar-1", "bar-2")), expandedIds);

expandedIds = blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("b*r-1", true, null, false, actionListener));
expandedIds = blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedIds("b*r-1", true, null, false, null, actionListener)
);
assertEquals(new TreeSet<>(Collections.singletonList("bar-1")), expandedIds);

expandedIds = blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedIds("bar-1,foo*", true, null, false, actionListener)
actionListener -> datafeedConfigProvider.expandDatafeedIds("bar-1,foo*", true, null, false, null, actionListener)
);
assertEquals(new TreeSet<>(Arrays.asList("bar-1", "foo-1", "foo-2")), expandedIds);

// Test full datafeed config
List<DatafeedConfig.Builder> expandedDatafeedBuilders = blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("foo*", true, actionListener)
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("foo*", true, null, actionListener)
);
List<DatafeedConfig> expandedDatafeeds = expandedDatafeedBuilders.stream()
.map(DatafeedConfig.Builder::build)
.collect(Collectors.toList());
assertThat(expandedDatafeeds, containsInAnyOrder(foo1, foo2));

expandedDatafeedBuilders = blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*-1", true, actionListener)
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*-1", true, null, actionListener)
);
expandedDatafeeds = expandedDatafeedBuilders.stream().map(DatafeedConfig.Builder::build).collect(Collectors.toList());
assertThat(expandedDatafeeds, containsInAnyOrder(foo1, bar1));

expandedDatafeedBuilders = blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("bar*", true, actionListener)
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("bar*", true, null, actionListener)
);
expandedDatafeeds = expandedDatafeedBuilders.stream().map(DatafeedConfig.Builder::build).collect(Collectors.toList());
assertThat(expandedDatafeeds, containsInAnyOrder(bar1, bar2));

expandedDatafeedBuilders = blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("b*r-1", true, actionListener)
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("b*r-1", true, null, actionListener)
);
expandedDatafeeds = expandedDatafeedBuilders.stream().map(DatafeedConfig.Builder::build).collect(Collectors.toList());
assertThat(expandedDatafeeds, containsInAnyOrder(bar1));

expandedDatafeedBuilders = blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("bar-1,foo*", true, actionListener)
actionListener -> datafeedConfigProvider.expandDatafeedConfigs("bar-1,foo*", true, null, actionListener)
);
expandedDatafeeds = expandedDatafeedBuilders.stream().map(DatafeedConfig.Builder::build).collect(Collectors.toList());
assertThat(expandedDatafeeds, containsInAnyOrder(bar1, foo1, foo2));
Expand All @@ -398,12 +405,12 @@ public void testExpandDatafeedsWithTaskData() throws Exception {
AtomicReference<SortedSet<String>> datafeedIdsHolder = new AtomicReference<>();
// Test datafeed IDs only
SortedSet<String> expandedIds = blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedIds("foo*", false, tasks, true, actionListener)
actionListener -> datafeedConfigProvider.expandDatafeedIds("foo*", false, tasks, true, null, actionListener)
);
assertEquals(new TreeSet<>(Arrays.asList("foo-1", "foo-2")), expandedIds);

blockingCall(
actionListener -> datafeedConfigProvider.expandDatafeedIds("foo-1*,foo-2*", false, tasks, false, actionListener),
actionListener -> datafeedConfigProvider.expandDatafeedIds("foo-1*,foo-2*", false, tasks, false, null, actionListener),
datafeedIdsHolder,
exceptionHolder
);
Expand Down
Loading

0 comments on commit 6b0beb7

Please sign in to comment.