Skip to content

Make GetIndexAction cancellable #87681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/87681.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 87681
summary: Make `GetIndexAction` cancellable
area: Indices APIs
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.http;

import org.apache.http.client.methods.HttpGet;
import org.elasticsearch.action.admin.indices.get.GetIndexAction;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsAction;
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
Expand Down Expand Up @@ -37,21 +38,28 @@
import static org.hamcrest.core.IsEqual.equalTo;

@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0)
public class RestGetMappingsCancellationIT extends HttpSmokeTestCase {
public class RestClusterInfoActionCancellationIT extends HttpSmokeTestCase {

public void testGetMappingsCancellation() throws Exception {
runTest(GetMappingsAction.NAME, "/test/_mappings");
}

public void testGetIndicesCancellation() throws Exception {
runTest(GetIndexAction.NAME, "/test");
}

private void runTest(String actionName, String endpoint) throws Exception {
internalCluster().startMasterOnlyNode();
internalCluster().startDataOnlyNode();
ensureStableCluster(2);

createIndex("test");
ensureGreen("test");
final String actionName = GetMappingsAction.NAME;
// Add a retryable cluster block that would block the request execution
updateClusterState(currentState -> {
ClusterBlock clusterBlock = new ClusterBlock(
1000,
"Get mappings cancellation test cluster block",
actionName + " cancellation test cluster block",
true,
false,
false,
Expand All @@ -62,7 +70,7 @@ public void testGetMappingsCancellation() throws Exception {
return ClusterState.builder(currentState).blocks(ClusterBlocks.builder().addGlobalBlock(clusterBlock).build()).build();
});

final Request request = new Request(HttpGet.METHOD_NAME, "/test/_mappings");
final Request request = new Request(HttpGet.METHOD_NAME, endpoint);
final PlainActionFuture<Response> future = new PlainActionFuture<>();
final Cancellable cancellable = getRestClient().performRequestAsync(request, wrapAsRestResponseListener(future));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
registerHandler.accept(new RestResetFeatureStateAction());
registerHandler.accept(new RestGetFeatureUpgradeStatusAction());
registerHandler.accept(new RestPostFeatureUpgradeAction());
registerHandler.accept(new RestGetIndicesAction());
registerHandler.accept(new RestGetIndicesAction(threadPool));
registerHandler.accept(new RestIndicesStatsAction());
registerHandler.accept(new RestIndicesSegmentsAction(threadPool));
registerHandler.accept(new RestIndicesShardStoresAction());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.util.ArrayUtils;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.tasks.TaskId;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
Expand Down Expand Up @@ -161,4 +165,9 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(humanReadable);
out.writeBoolean(includeDefaults);
}

@Override
public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
return new CancellableTask(id, type, action, getDescription(), parentTaskId, headers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsFilter;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
Expand Down Expand Up @@ -94,11 +94,12 @@ protected void doMasterOperation(
boolean doneMappings = false;
boolean doneSettings = false;
for (Feature feature : features) {
checkCancellation(task);
switch (feature) {
case MAPPINGS:
if (doneMappings == false) {
mappingsResult = state.metadata()
.findMappings(concreteIndices, indicesService.getFieldFilter(), Metadata.ON_NEXT_INDEX_FIND_MAPPINGS_NOOP);
.findMappings(concreteIndices, indicesService.getFieldFilter(), () -> checkCancellation(task));
doneMappings = true;
}
break;
Expand All @@ -113,6 +114,7 @@ protected void doMasterOperation(
ImmutableOpenMap.Builder<String, Settings> settingsMapBuilder = ImmutableOpenMap.builder();
ImmutableOpenMap.Builder<String, Settings> defaultSettingsMapBuilder = ImmutableOpenMap.builder();
for (String index : concreteIndices) {
checkCancellation(task);
Settings indexSettings = state.metadata().index(index).getSettings();
if (request.humanReadable()) {
indexSettings = IndexMetadata.addHumanReadableSettings(indexSettings);
Expand All @@ -137,4 +139,10 @@ protected void doMasterOperation(
}
listener.onResponse(new GetIndexResponse(concreteIndices, mappingsResult, aliasesResult, settings, defaultSettings, dataStreams));
}

private static void checkCancellation(Task task) {
if (task instanceof CancellableTask cancellableTask) {
cancellableTask.ensureNotCancelled();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.elasticsearch.transport.TransportService;

import java.util.Map;
import java.util.concurrent.CancellationException;

public class TransportGetMappingsAction extends TransportClusterInfoAction<GetMappingsRequest, GetMappingsResponse> {

Expand Down Expand Up @@ -75,8 +74,8 @@ protected void doMasterOperation(
}

private static void checkCancellation(Task task) {
if (task instanceof CancellableTask && ((CancellableTask) task).isCancelled()) {
throw new CancellationException("Task cancelled");
if (task instanceof CancellableTask cancellableTask) {
cancellableTask.ensureNotCancelled();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import org.elasticsearch.core.RestApiVersion;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener;
import org.elasticsearch.rest.action.DispatchingRestToXContentListener;
import org.elasticsearch.rest.action.RestCancellableNodeClient;
import org.elasticsearch.threadpool.ThreadPool;

import java.io.IOException;
import java.util.Collections;
Expand All @@ -42,6 +44,12 @@ public class RestGetIndicesAction extends BaseRestHandler {
.collect(Collectors.toSet())
);

private final ThreadPool threadPool;

public RestGetIndicesAction(ThreadPool threadPool) {
this.threadPool = threadPool;
}

@Override
public List<Route> routes() {
return List.of(new Route(GET, "/{index}"), new Route(HEAD, "/{index}"));
Expand Down Expand Up @@ -70,7 +78,13 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
getIndexRequest.humanReadable(request.paramAsBoolean("human", false));
getIndexRequest.includeDefaults(request.paramAsBoolean("include_defaults", false));
getIndexRequest.features(GetIndexRequest.Feature.fromRequest(request));
return channel -> client.admin().indices().getIndex(getIndexRequest, new RestToXContentListener<>(channel));
final var httpChannel = request.getHttpChannel();
return channel -> new RestCancellableNodeClient(client, httpChannel).admin()
.indices()
.getIndex(
getIndexRequest,
new DispatchingRestToXContentListener<>(threadPool.executor(ThreadPool.Names.MANAGEMENT), channel, request)
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.rest.action.admin.indices;

import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.common.util.concurrent.DeterministicTaskQueue;
import org.elasticsearch.core.RestApiVersion;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.test.ESTestCase;
Expand Down Expand Up @@ -36,7 +37,7 @@ public void testIncludeTypeNamesWarning() throws IOException {
Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader)
).withMethod(RestRequest.Method.GET).withPath("/some_index").withParams(params).build();

RestGetIndicesAction handler = new RestGetIndicesAction();
RestGetIndicesAction handler = new RestGetIndicesAction(new DeterministicTaskQueue().getThreadPool());
handler.prepareRequest(request, mock(NodeClient.class));
assertCriticalWarnings(RestGetIndicesAction.TYPES_DEPRECATION_MESSAGE);

Expand All @@ -57,7 +58,7 @@ public void testIncludeTypeNamesWarningExists() throws IOException {
Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader)
).withMethod(RestRequest.Method.HEAD).withPath("/some_index").withParams(params).build();

RestGetIndicesAction handler = new RestGetIndicesAction();
RestGetIndicesAction handler = new RestGetIndicesAction(new DeterministicTaskQueue().getThreadPool());
handler.prepareRequest(request, mock(NodeClient.class));
}
}