jihoonson commented on a change in pull request #11643:
URL: https://github.com/apache/druid/pull/11643#discussion_r700615404
##########
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:
Good catch. I think they remained unreverted after I reverted some
unnecessary changes.
##########
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:
Reverted.
##########
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:
Fixed javadoc.
##########
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:
I added `endLifecycleWithoutEmittingMetrics`.
--
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]