-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Initial commit to support cancelling a long running query (WIP) #9141
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ | |
| import org.apache.pinot.core.query.request.context.QueryContext; | ||
| import org.apache.pinot.core.query.request.context.ThreadTimer; | ||
| import org.apache.pinot.core.query.scheduler.resources.ResourceManager; | ||
| import org.apache.pinot.core.util.trace.TraceContext; | ||
| import org.apache.pinot.core.util.trace.TraceRunnable; | ||
| import org.apache.pinot.spi.exception.EarlyTerminationException; | ||
| import org.apache.pinot.spi.trace.Tracing; | ||
|
|
@@ -73,6 +74,13 @@ protected BaseCombineOperator(List<Operator> operators, QueryContext queryContex | |
| // The parallelism is bounded by the task count. | ||
| _numTasks = CombineOperatorUtils.getNumTasksForQuery(operators.size(), queryContext.getMaxExecutionThreads()); | ||
| _futures = new Future[_numTasks]; | ||
|
|
||
| try { | ||
| long requestId = Long.parseLong(queryContext.getQueryOptions().get("requestId")); | ||
| TraceContext.register(requestId, queryContext, _futures); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would suggest not to reuse But in general, I'd prefer add cancel() on Operator and Plan interface to make it explicit that query is cancellable. The reason to add cancel() on Plan interface is because |
||
| } catch (Exception e) { | ||
| //ignore.. this will only mean that we cannot cancel the query.. this can happen if requestId is null or not a long etc | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,13 +22,17 @@ | |
| import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Queue; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentLinkedQueue; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import javax.annotation.Nullable; | ||
| import org.apache.commons.lang3.tuple.Pair; | ||
| import org.apache.pinot.core.query.request.context.QueryContext; | ||
| import org.apache.pinot.spi.utils.JsonUtils; | ||
|
|
||
|
|
||
|
|
@@ -45,9 +49,28 @@ | |
| * request from tracing to prevent resource leak. | ||
| */ | ||
| public final class TraceContext { | ||
|
|
||
| static Map<Long, Pair<QueryContext, Future[]>> requestId2FuturesMap = new ConcurrentHashMap<>(); | ||
|
|
||
| private TraceContext() { | ||
| } | ||
|
|
||
| public static void register(long requestId, QueryContext queryContext, Future[] futures) { | ||
| requestId2FuturesMap.put(requestId, Pair.of(queryContext, futures)); | ||
| } | ||
|
|
||
| public static void cancel(long requestId) { | ||
| Pair<QueryContext, Future[]> queryContextFuturesPair = requestId2FuturesMap.get(requestId); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use remove() instead? to avoid keeping futures around. |
||
| if (queryContextFuturesPair.getRight() != null) { | ||
| // Cancel all ongoing jobs | ||
| for (Future future : queryContextFuturesPair.getRight()) { | ||
| if (!future.isDone()) { | ||
| future.cancel(true); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Trace represents the logs for a single thread. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.pinot.server.api.resources; | ||
|
|
||
| import io.swagger.annotations.Api; | ||
| import io.swagger.annotations.ApiKeyAuthDefinition; | ||
| import io.swagger.annotations.ApiOperation; | ||
| import io.swagger.annotations.ApiParam; | ||
| import io.swagger.annotations.Authorization; | ||
| import io.swagger.annotations.SecurityDefinition; | ||
| import io.swagger.annotations.SwaggerDefinition; | ||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import javax.inject.Inject; | ||
| import javax.ws.rs.GET; | ||
| import javax.ws.rs.Path; | ||
| import javax.ws.rs.PathParam; | ||
| import javax.ws.rs.Produces; | ||
| import javax.ws.rs.WebApplicationException; | ||
| import javax.ws.rs.core.HttpHeaders; | ||
| import javax.ws.rs.core.MediaType; | ||
| import org.apache.commons.io.FileUtils; | ||
| import org.apache.pinot.common.restlet.resources.SegmentConsumerInfo; | ||
| import org.apache.pinot.common.restlet.resources.SegmentErrorInfo; | ||
| import org.apache.pinot.common.restlet.resources.SegmentServerDebugInfo; | ||
| import org.apache.pinot.core.data.manager.offline.ImmutableSegmentDataManager; | ||
| import org.apache.pinot.core.data.manager.realtime.RealtimeSegmentDataManager; | ||
| import org.apache.pinot.core.util.trace.TraceContext; | ||
| import org.apache.pinot.segment.local.data.manager.SegmentDataManager; | ||
| import org.apache.pinot.segment.local.data.manager.TableDataManager; | ||
| import org.apache.pinot.segment.spi.ImmutableSegment; | ||
| import org.apache.pinot.server.starter.ServerInstance; | ||
| import org.apache.pinot.spi.config.table.TableType; | ||
| import org.apache.pinot.spi.utils.builder.TableNameBuilder; | ||
|
|
||
| import static org.apache.pinot.spi.utils.CommonConstants.SWAGGER_AUTHORIZATION_KEY; | ||
|
|
||
|
|
||
| /** | ||
| * Debug resource for Pinot Server. | ||
| */ | ||
| @Api(tags = "Query", authorizations = {@Authorization(value = SWAGGER_AUTHORIZATION_KEY)}) | ||
| @SwaggerDefinition(securityDefinition = @SecurityDefinition(apiKeyAuthDefinitions = @ApiKeyAuthDefinition(name = | ||
| HttpHeaders.AUTHORIZATION, in = ApiKeyAuthDefinition.ApiKeyLocation.HEADER, key = SWAGGER_AUTHORIZATION_KEY))) | ||
| @Path("/query/") | ||
| public class QueryResource { | ||
|
|
||
| @Inject | ||
| private ServerInstance _serverInstance; | ||
|
|
||
| @GET | ||
| @Path("query/cancel") | ||
| @Produces(MediaType.APPLICATION_JSON) | ||
| @ApiOperation(value = "Cancel a request", | ||
| notes = "This will cancel all the threads for a specific request") | ||
| public String getSegmentsDebugInfo( | ||
| @ApiParam(value = "RequestId to cancel", required = true) @PathParam("requestId") long requestId) { | ||
| TraceContext.cancel(requestId); | ||
| return "successfully deleted requestId"; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this is just to pass
requestIdto servers? TherequestIdis already passed over, and can be read viaqueryRequest.getRequestId()at server sidee.g.