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

    https://github.com/apache/spark/pull/11030#discussion_r51662372
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/ContinuousQueryManager.scala ---
    @@ -0,0 +1,106 @@
    +/*
    + * 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.{Sink, StreamExecution}
    +
    +/**
    + * :: Experimental ::
    + * A class to manage all the [[org.apache.spark.sql.ContinuousQuery 
ContinuousQueries]] active
    + * on a [[SQLContext]].
    + * 
    + * @since 2.0.0
    + */
    +@Experimental
    +class ContinuousQueryManager {
    +
    +  private val activeQueries = new mutable.HashMap[String, ContinuousQuery]
    +  private val activeQueriesLock = new Object
    +  private val awaitTerminationLock = new Object
    +
    +  @volatile
    +  private var lastTerminatedQuery: ContinuousQuery = null
    +
    +  /** Returns a list of active queries associated with this SQLContext */
    +  def active: Array[ContinuousQuery] = activeQueriesLock.synchronized {
    +    activeQueries.values.toArray
    +  }
    +
    +  /** Returns an active query from this SQLContext or throws exception if 
bad name */
    +  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 this SQLContext is terminated, with 
or without
    +   * exceptions. Returns the query that has been terminated.
    +   */
    +  def awaitAnyTermination(): ContinuousQuery = {
    +    awaitTerminationLock.synchronized {
    +      lastTerminatedQuery = null
    +      while (lastTerminatedQuery == null) {
    +        awaitTerminationLock.wait(10)
    +      }
    +      lastTerminatedQuery
    +    }
    +  }
    +
    +  /**
    +   * Wait until any of the queries on this SQLContext is terminated.
    +   * Returns the stopped query if any query was terminated.
    +   */
    +  def awaitAnyTermination(timeoutMs: Long): Option[ContinuousQuery] = {
    +    val endTime = System.currentTimeMillis + timeoutMs
    +    def timeLeft = math.max(endTime - System.currentTimeMillis, 0)
    +
    +    awaitTerminationLock.synchronized {
    +      lastTerminatedQuery = null
    +      while (timeLeft > 0 && lastTerminatedQuery == null) {
    +        awaitTerminationLock.wait(10)
    --- End diff --
    
    Why use `wait(10)` to wake up every 10 milliseconds? Looks waste a lot of 
CPU cycles and unnecessary thread context switches.


---
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