clintropolis commented on code in PR #19233: URL: https://github.com/apache/druid/pull/19233#discussion_r3029622321
########## multi-stage-query/src/main/java/org/apache/druid/msq/exec/ControllerHolder.java: ########## @@ -0,0 +1,429 @@ +/* + * 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.msq.exec; + +import com.google.common.base.Preconditions; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.errorprone.annotations.concurrent.GuardedBy; +import org.apache.druid.indexer.TaskState; +import org.apache.druid.indexer.report.TaskReport; +import org.apache.druid.java.util.common.DateTimes; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.msq.indexing.error.CanceledFault; +import org.apache.druid.msq.indexing.error.CancellationReason; +import org.apache.druid.msq.indexing.error.MSQErrorReport; +import org.apache.druid.msq.indexing.report.MSQStatusReport; +import org.apache.druid.msq.indexing.report.MSQTaskReport; +import org.apache.druid.msq.indexing.report.MSQTaskReportPayload; +import org.apache.druid.msq.util.MultiStageQueryContext; +import org.apache.druid.query.BaseQuery; +import org.apache.druid.query.QueryContext; +import org.apache.druid.query.QueryContexts; +import org.apache.druid.server.security.AuthenticationResult; +import org.apache.druid.sql.http.StandardQueryState; +import org.joda.time.DateTime; + +import javax.annotation.Nullable; +import java.util.Map; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +/** + * Holder for a {@link Controller} that manages its lifecycle, including cancellation and timeouts. + */ +public class ControllerHolder +{ + private static final Logger log = new Logger(ControllerHolder.class); + + private final Controller controller; + private final String sqlQueryId; + + @Nullable + private final String sql; + + @Nullable + private final AuthenticationResult authenticationResult; + + private final DateTime startTime; + + @GuardedBy("this") + private State state = State.ACCEPTED; + + /** + * Thread running the controller. Set inside the {@link #runAsync} runnable, cleared in its finally block. + */ + @GuardedBy("this") + private Thread controllerThread; + + /** + * If cancel was called, the reason for cancellation. Used to deliver the cancellation to the controller thread + * if {@link #cancel} is called before the controller starts running. + */ + @GuardedBy("this") + private CancellationReason cancelReason; + + public ControllerHolder( + final Controller controller, + final String sqlQueryId, + @Nullable final String sql, + @Nullable final AuthenticationResult authenticationResult, + final DateTime startTime + ) + { + this.controller = Preconditions.checkNotNull(controller, "controller"); + this.sqlQueryId = Preconditions.checkNotNull(sqlQueryId, "sqlQueryId"); + this.sql = sql; + this.authenticationResult = authenticationResult; + this.startTime = Preconditions.checkNotNull(startTime, "startTime"); + } + + public Controller getController() + { + return controller; + } + + @Nullable + public String getSqlQueryId() + { + return sqlQueryId; + } + + @Nullable + public String getSql() + { + return sql; + } + + public String getControllerHost() + { + return getControllerContext().selfNode().getHostAndPortToUse(); + } + + private ControllerContext getControllerContext() + { + return controller.getControllerContext(); + } + + @Nullable + public AuthenticationResult getAuthenticationResult() + { + return authenticationResult; + } + + public DateTime getStartTime() + { + return startTime; + } + + public synchronized State getState() + { + return state; + } + + /** + * Runs {@link Controller#run(QueryListener)} in the provided executor. Optionally registers the controller with + * the provided registry while it is running. Schedules a timeout on the provided {@code scheduledExec} based + * on the query deadline from the controller's query context. + * + * @return future that resolves when the controller is done or canceled + */ + public ListenableFuture<?> runAsync( + final QueryListener listener, + @Nullable final ControllerRegistry controllerRegistry, + final ListeningExecutorService exec, + final ScheduledExecutorService scheduledExec Review Comment: any chance we will need any other kinds of pools available? like wondering if we should we just pass the whole ControllerThreadPool in here instead of the 2 execs, or will callers not always have a ControllerThreadPool? -- 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]
