Github user zsxwing commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11030#discussion_r52369032
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/ContinuousQueryManager.scala ---
    @@ -0,0 +1,180 @@
    +/*
    + * 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.spark.sql
    +
    +import scala.collection.mutable
    +
    +import org.apache.spark.annotation.Experimental
    +import 
org.apache.spark.sql.execution.streaming.{ContinuousQueryListenerBus, Sink, 
StreamExecution}
    +import org.apache.spark.sql.util.ContinuousQueryListener
    +
    +/**
    + * :: Experimental ::
    + * A class to manage all the [[org.apache.spark.sql.ContinuousQuery 
ContinuousQueries]] active
    + * on a [[SQLContext]].
    + *
    + * @since 2.0.0
    + */
    +@Experimental
    +class ContinuousQueryManager(sqlContext: SQLContext) {
    +
    +  private val listenerBus = new 
ContinuousQueryListenerBus(sqlContext.sparkContext.listenerBus)
    +  private val activeQueries = new mutable.HashMap[String, ContinuousQuery]
    +  private val activeQueriesLock = new Object
    +  private val awaitTerminationLock = new Object
    +
    +  private var lastTerminatedQuery: ContinuousQuery = null
    +
    +  /**
    +   * Returns a list of active queries associated with this SQLContext
    +   *
    +   * @since 2.0.0
    +   */
    +  def active: Array[ContinuousQuery] = activeQueriesLock.synchronized {
    +    activeQueries.values.toArray
    +  }
    +
    +  /**
    +   * Returns an active query from this SQLContext or throws exception if 
bad name
    +   *
    +   * @since 2.0.0
    +   */
    +  def get(name: String): ContinuousQuery = activeQueriesLock.synchronized {
    +    activeQueries.get(name).getOrElse {
    +      throw new IllegalArgumentException(s"There is no active query with 
name $name")
    +    }
    +  }
    +
    +  /**
    +   * Wait until any of the queries on the associated SQLContext has 
terminated since the
    +   * creation of the context, or since `clearTermination()` was called. If 
any query was terminated
    +   * with an exception, then the exception will be thrown.
    +   *
    +   * If a query has terminated, then subsequent calls to 
`awaitAnyTermination()` will either
    +   * return immediately (if the query was terminated by `query.stop()`),
    +   * or throw the exception immediately (if the query was terminated with 
exception). Use
    +   * `resetTerminated()` to clear past terminations and wait for new 
terminations.
    +   *
    +   * Note that if multiple queries have terminated
    +   * @throws ContinuousQueryException, if any query has terminated with an 
exception without
    +   *         `timeoutMs` milliseconds.
    +   *
    +   * @since 2.0.0
    +   */
    +  def awaitAnyTermination(): Unit = {
    +    awaitTerminationLock.synchronized {
    +      while (lastTerminatedQuery == null) {
    +        awaitTerminationLock.wait(10)
    +      }
    +      if (lastTerminatedQuery != null && 
lastTerminatedQuery.exception.nonEmpty) {
    +        throw lastTerminatedQuery.exception.get
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Wait until any of the queries on the associated SQLContext has 
terminated since the
    +   * creation of the context, or since `clearTermination()` was called. 
Returns whether the query
    +   * has terminated or not. If the query has terminated with an exception,
    +   * then the exception will be thrown.
    +   *
    +   * If a query has terminated, then subsequent calls to 
`awaitAnyTermination()` will either
    +   * return `true` immediately (if the query was terminated by 
`query.stop()`),
    +   * or throw the exception immediately (if the query was terminated with 
exception). Use
    +   * `resetTerminated()` to clear past terminations and wait for new 
terminations.
    +   *
    +   * @throws ContinuousQueryException, if any query has terminated with an 
exception
    +   *
    +   * @since 2.0.0
    +   */
    +  def awaitAnyTermination(timeoutMs: Long): Boolean = {
    +    val endTime = System.currentTimeMillis + timeoutMs
    --- End diff --
    
    I guess it's possible that the user may call 
`awaitAnyTermination(Long.MaxValue)` then this will overflow. How about:
    ```
      def awaitAnyTermination(timeoutMs: Long): Boolean = {
        val startTime = System.currentTimeMillis
        def isTimeout = timeoutMs <= System.currentTimeMillis - startTime
    
        awaitTerminationLock.synchronized {
          while (!isTimeout && lastTerminatedQuery == null) {
            awaitTerminationLock.wait(10)
          }
          if (lastTerminatedQuery != null && 
lastTerminatedQuery.exception.nonEmpty) {
            throw lastTerminatedQuery.exception.get
          }
          lastTerminatedQuery != null
        }
      }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

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

Reply via email to