maytasm commented on a change in pull request #11643:
URL: https://github.com/apache/druid/pull/11643#discussion_r699998346



##########
File path: 
integration-tests/src/test/java/org/apache/druid/tests/query/ITSqlCancelTest.java
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.druid.tests.query;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableMap;
+import com.google.inject.Inject;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.RE;
+import org.apache.druid.java.util.http.client.response.StatusResponseHolder;
+import org.apache.druid.query.QueryException;
+import org.apache.druid.query.QueryInterruptedException;
+import org.apache.druid.sql.http.SqlQuery;
+import org.apache.druid.testing.IntegrationTestingConfig;
+import org.apache.druid.testing.clients.CoordinatorResourceTestClient;
+import org.apache.druid.testing.clients.SqlResourceTestClient;
+import org.apache.druid.testing.guice.DruidTestModuleFactory;
+import org.apache.druid.testing.utils.ITRetryUtil;
+import org.apache.druid.testing.utils.SqlTestQueryHelper;
+import org.apache.druid.tests.TestNGGroup;
+import org.jboss.netty.handler.codec.http.HttpResponseStatus;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Guice;
+import org.testng.annotations.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+@Test(groups = TestNGGroup.QUERY)
+@Guice(moduleFactory = DruidTestModuleFactory.class)
+public class ITSqlCancelTest
+{
+  private static final String WIKIPEDIA_DATA_SOURCE = "wikipedia_editstream";
+
+  private static final String QUERY
+      = "SELECT sleep(CASE WHEN added > 0 THEN 1 ELSE 0 END) FROM 
wikipedia_editstream LIMIT 15";
+
+  private static final int NUM_QUERIES = 3;
+
+  @Inject
+  private CoordinatorResourceTestClient coordinatorClient;
+  @Inject
+  private SqlTestQueryHelper sqlHelper;
+  @Inject
+  private SqlResourceTestClient sqlClient;
+  @Inject
+  private IntegrationTestingConfig config;
+  @Inject
+  private ObjectMapper jsonMapper;
+
+  @BeforeMethod
+  public void before()
+  {
+    // ensure that wikipedia segments are loaded completely
+    ITRetryUtil.retryUntilTrue(
+        () -> coordinatorClient.areSegmentsLoaded(WIKIPEDIA_DATA_SOURCE), 
"wikipedia segment load"
+    );
+  }
+
+  @Test
+  public void testCancelValidQuery() throws Exception
+  {
+    final String queryId = "sql-cancel-test";
+    final List<Future<StatusResponseHolder>> queryResponseFutures = new 
ArrayList<>();
+    for (int i = 0; i < NUM_QUERIES; i++) {
+      queryResponseFutures.add(
+          sqlClient.queryAsync(
+              sqlHelper.getQueryURL(config.getRouterUrl()),
+              new SqlQuery(QUERY, null, false, ImmutableMap.of("sqlQueryId", 
queryId), null)
+          )
+      );
+    }
+
+    // Wait until the sqlLifecycle is authorized and registered
+    Thread.sleep(1000);

Review comment:
       Is there any way to make this more deterministic? Could this cause test 
to be flaky?

##########
File path: sql/src/main/java/org/apache/druid/sql/SqlLifecycle.java
##########
@@ -304,12 +301,13 @@ public PrepareResult prepare() throws 
RelConversionException
    *
    * If successful, the lifecycle will first transition from {@link 
State#AUTHORIZED} to {@link State#PLANNED}.
    */
-  public PlannerContext plan() throws RelConversionException
+  public void plan() throws RelConversionException
   {
-    synchronized (lock) {
-      transition(State.AUTHORIZED, State.PLANNED);
+    if (transition(State.AUTHORIZED, State.PLANNED)) {
+      final DruidPlanner planner0;
       Preconditions.checkNotNull(plannerContext, "Cannot plan, plannerContext 
is null");
-      try (DruidPlanner planner = 
plannerFactory.createPlannerWithContext(plannerContext)) {
+      planner0 = plannerFactory.createPlannerWithContext(plannerContext);
+      try (DruidPlanner planner = planner0) {

Review comment:
       nit: Why the refactor from `try (DruidPlanner planner = 
plannerFactory.createPlannerWithContext(plannerContext))` ?

##########
File path: sql/src/main/java/org/apache/druid/sql/http/SqlResource.java
##########
@@ -182,27 +220,28 @@ public Response doPost(
       }
     }
     catch (QueryCapacityExceededException cap) {
-      lifecycle.emitLogsAndMetrics(cap, remoteAddr, -1);
+      endLifecycle(sqlQueryId, lifecycle, cap, remoteAddr, -1);
       return buildNonOkResponse(QueryCapacityExceededException.STATUS_CODE, 
cap);
     }
     catch (QueryUnsupportedException unsupported) {
-      lifecycle.emitLogsAndMetrics(unsupported, remoteAddr, -1);
+      endLifecycle(sqlQueryId, lifecycle, unsupported, remoteAddr, -1);
       return buildNonOkResponse(QueryUnsupportedException.STATUS_CODE, 
unsupported);
     }
     catch (QueryTimeoutException timeout) {
-      lifecycle.emitLogsAndMetrics(timeout, remoteAddr, -1);
+      endLifecycle(sqlQueryId, lifecycle, timeout, remoteAddr, -1);
       return buildNonOkResponse(QueryTimeoutException.STATUS_CODE, timeout);
     }
     catch (SqlPlanningException | ResourceLimitExceededException e) {
-      lifecycle.emitLogsAndMetrics(e, remoteAddr, -1);
+      endLifecycle(sqlQueryId, lifecycle, e, remoteAddr, -1);
       return buildNonOkResponse(BadQueryException.STATUS_CODE, e);
     }
     catch (ForbiddenException e) {
+      sqlLifecycleManager.remove(sqlQueryId, lifecycle);

Review comment:
       nit: maybe add a boolean argument for endLifecycle to emit metric or not 
and call endLifecycle here too?

##########
File path: sql/src/main/java/org/apache/druid/sql/avatica/DruidStatement.java
##########
@@ -216,7 +218,9 @@ public DruidStatement execute(List<TypedValue> parameters)
         sqlLifecycle.setParameters(parameters);
         sqlLifecycle.validateAndAuthorize(authenticationResult);
         sqlLifecycle.plan();
-        final Sequence<Object[]> baseSequence = 
yielderOpenCloseExecutor.submit(sqlLifecycle::execute).get();
+        Optional<Sequence<Object[]>> maybeSequence = 
yielderOpenCloseExecutor.submit(sqlLifecycle::execute).get();
+        assert maybeSequence.isPresent();

Review comment:
       Can this be not present?

##########
File path: sql/src/main/java/org/apache/druid/sql/SqlLifecycle.java
##########
@@ -80,8 +84,7 @@
  * <li>Logging ({@link #emitLogsAndMetrics(Throwable, String, long)})</li>
  * </ol>
  *
- * <p>Unlike QueryLifecycle, this class is designed to be <b>thread safe</b> 
so that it can be used in multi-threaded
- * scenario (JDBC) without external synchronization.
+ * Every method in this class must be called by the same thread except for 
{@link #cancel()}.

Review comment:
       What's the reasoning for removing the thread safe behavior? and will 
there be any problem with multi-threaded scenario (JDBC) mentioned in the old 
javadoc?

##########
File path: sql/src/main/java/org/apache/druid/sql/SqlLifecycle.java
##########
@@ -280,22 +277,22 @@ private void checkAccess(Access access)
    */
   public PrepareResult prepare() throws RelConversionException
   {
-    synchronized (lock) {
-      if (state != State.AUTHORIZED) {
-        throw new ISE("Cannot prepare because current state[%s] is not [%s].", 
state, State.AUTHORIZED);
-      }
-      Preconditions.checkNotNull(plannerContext, "Cannot prepare, 
plannerContext is null");
-      try (DruidPlanner planner = 
plannerFactory.createPlannerWithContext(plannerContext)) {
-        this.prepareResult = planner.prepare(sql);
-        return prepareResult;
-      }
-      // we can't collapse catch clauses since SqlPlanningException has 
type-sensitive constructors.
-      catch (SqlParseException e) {
-        throw new SqlPlanningException(e);
-      }
-      catch (ValidationException e) {
-        throw new SqlPlanningException(e);
-      }
+    if (state != State.AUTHORIZED) {
+      throw new ISE("Cannot prepare because current state[%s] is not [%s].", 
state, State.AUTHORIZED);
+    }
+    final DruidPlanner planner0;

Review comment:
       nit: Why the refactor from `try (DruidPlanner planner = 
plannerFactory.createPlannerWithContext(plannerContext))` ?

##########
File path: sql/src/main/java/org/apache/druid/sql/http/SqlResource.java
##########
@@ -222,11 +261,70 @@ public Response doPost(
     }
   }
 
-  Response buildNonOkResponse(int status, Exception e) throws 
JsonProcessingException
+  private void endLifecycle(
+      String sqlQueryId,
+      SqlLifecycle lifecycle,
+      @Nullable final Throwable e,
+      @Nullable final String remoteAddress,
+      final long bytesWritten
+  )
+  {
+    lifecycle.emitLogsAndMetrics(e, remoteAddress, bytesWritten);
+    sqlLifecycleManager.remove(sqlQueryId, lifecycle);
+  }
+
+  private Response buildCanceledResponse(String sqlQueryId) throws 
JsonProcessingException
+  {
+    return buildNonOkResponse(
+        Status.INTERNAL_SERVER_ERROR.getStatusCode(),
+        new QueryInterruptedException(
+            QueryInterruptedException.QUERY_CANCELLED,
+            StringUtils.format("Query is canceled [%s]", sqlQueryId),
+            null,
+            null
+        )
+    );
+  }
+
+  private Response buildNonOkResponse(int status, Exception e) throws 
JsonProcessingException
   {
     return Response.status(status)
                    .type(MediaType.APPLICATION_JSON_TYPE)
                    .entity(jsonMapper.writeValueAsBytes(e))
                    .build();
   }
+
+  @DELETE
+  @Path("{id}")
+  @Produces(MediaType.APPLICATION_JSON)
+  public Response cancelQuery(
+      @PathParam("id") String sqlQueryId,
+      @Context final HttpServletRequest req
+  )
+  {
+    log.debug("Received cancel request for query [%s]", sqlQueryId);
+
+    List<SqlLifecycle> lifecycles = sqlLifecycleManager.getAll(sqlQueryId);
+    if (lifecycles.isEmpty()) {
+      return Response.status(Status.NOT_FOUND).build();
+    }
+    Set<Resource> resources = lifecycles
+        .stream()
+        .flatMap(lifecycle -> lifecycle.getAuthorizedResources().stream())
+        .collect(Collectors.toSet());
+    Access access = AuthorizationUtils.authorizeAllResourceActions(
+        req,
+        Iterables.transform(resources, 
AuthorizationUtils.RESOURCE_READ_RA_GENERATOR),
+        authorizerMapper
+    );
+
+    if (access.isAllowed()) {
+      sqlLifecycleManager.removeAll(sqlQueryId, lifecycles);

Review comment:
       The sqlQueryId should have no lifecycle left right? Is it possible that 
the cancel happens when not **all** lifecycle are authorized and added to the 
manager yet?

##########
File path: sql/src/main/java/org/apache/druid/sql/SqlLifecycleManager.java
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.druid.sql;
+
+import com.google.common.collect.ImmutableList;
+import com.google.errorprone.annotations.concurrent.GuardedBy;
+import org.apache.druid.guice.LazySingleton;
+import org.apache.druid.sql.SqlLifecycle.State;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * This class manages only _authorized_ {@link SqlLifecycle}s. The main use 
case of this class is
+ * tracking running queries so that the cancel API can identify the lifecycles 
to cancel.
+ *
+ * This class is thread-safe as there are 2 or more threads that can access 
lifecycles at the same time
+ * for query running or query canceling.
+ */
+@LazySingleton
+public class SqlLifecycleManager
+{
+  private final Object lock = new Object();
+
+  @GuardedBy("lock")
+  private final Map<String, List<SqlLifecycle>> sqlLifecycles = new 
HashMap<>();
+
+  public void add(String sqlQueryId, SqlLifecycle lifecycle)
+  {
+    synchronized (lock) {
+      assert lifecycle.getState().ordinal() == State.AUTHORIZED.ordinal();
+      sqlLifecycles.computeIfAbsent(sqlQueryId, k -> new ArrayList<>())
+                   .add(lifecycle);
+    }
+  }
+
+  /**
+   * Removes the given lifecycle of the given query ID.
+   * This method uses {@link Object#equals} to find the lifecycle matched to 
the given parameter.
+   */
+  public void remove(String sqlQueryId, SqlLifecycle lifecycle)
+  {
+    synchronized (lock) {
+      List<SqlLifecycle> lifecycles = sqlLifecycles.get(sqlQueryId);
+      if (lifecycles != null) {
+        lifecycles.remove(lifecycle);
+        if (lifecycles.isEmpty()) {
+          sqlLifecycles.remove(sqlQueryId);
+        }
+      }
+    }
+  }
+
+  /**
+   * Removes all lifecycles of the given query ID.

Review comment:
       This javadoc should mention that only lifecycles matching the lifecycles 
in lifecyclesToRemove is removed




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to