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

    https://github.com/apache/spark/pull/11645#discussion_r56386741
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/StateStore.scala
 ---
    @@ -0,0 +1,200 @@
    +/*
    + * 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.execution.streaming.state
    +
    +import java.util.{Timer, TimerTask}
    +
    +import scala.collection.mutable
    +import scala.util.control.NonFatal
    +
    +import org.apache.spark.{Logging, SparkEnv}
    +import org.apache.spark.sql.catalyst.InternalRow
    +
    +/** Unique identifier for a [[StateStore]] */
    +case class StateStoreId(operatorId: Long, partitionId: Int)
    +
    +/**
    + * Base trait for a versioned key-value store used for streaming 
aggregations
    + */
    +trait StateStore {
    +
    +  /** Unique identifier of the store */
    +  def id: StateStoreId
    +
    +  /** Version of the data in this store before committing updates. */
    +  def version: Long
    +
    +  /**
    +   * Update the value of a key using the value generated by the update 
function.
    +   * This can be called only after prepareForUpdates() has been called in 
the same thread.
    +   */
    +  def update(key: InternalRow, updateFunc: Option[InternalRow] => 
InternalRow): Unit
    +
    +  /**
    +   * Remove keys that match the following condition.
    +   * This can be called only after prepareForUpdates() has been called in 
the current thread.
    +   */
    +  def remove(condition: InternalRow => Boolean): Unit
    +
    +  /**
    +   * Commit all the updates that have been made to the store.
    +   * This can be called only after prepareForUpdates() has been called in 
the current thread.
    +   */
    +  def commit(): Long
    +
    +  /** Cancel all the updates that have been made to the store. */
    +  def cancel(): Unit
    +
    +  /**
    +   * Iterator of store data after a set of updates have been committed.
    +   * This can be called only after commitUpdates() has been called in the 
current thread.
    +   */
    +  def iterator(): Iterator[InternalRow]
    +
    +  /**
    +   * Iterator of the updates that have been committed.
    +   * This can be called only after commitUpdates() has been called in the 
current thread.
    +   */
    +  def updates(): Iterator[StoreUpdate]
    +
    +  /**
    +   * Whether all updates have been committed
    +   */
    +  def hasCommitted: Boolean
    +}
    +
    +
    +trait StateStoreProvider {
    +
    +  /** Get the store with the existing version. */
    +  def getStore(version: Long): StateStore
    +
    +  /** Optional method for providers to allow for background management */
    +  def manage(): Unit = { }
    +}
    +
    +sealed trait StoreUpdate
    +case class ValueAdded(key: InternalRow, value: InternalRow) extends 
StoreUpdate
    +case class ValueUpdated(key: InternalRow, value: InternalRow) extends 
StoreUpdate
    +case class KeyRemoved(key: InternalRow) extends StoreUpdate
    +
    +
    +/**
    + * Companion object to [[StateStore]] that provides helper methods to 
create and retrive stores
    + * by their unique ids.
    + */
    +private[state] object StateStore extends Logging {
    +
    +  private val MANAGEMENT_TASK_INTERVAL_SECS = 60
    +
    +  private val loadedProviders = new mutable.HashMap[StateStoreId, 
StateStoreProvider]()
    +  private val managementTimer = new Timer("StateStore Timer", true)
    +  @volatile private var managementTask: TimerTask = null
    +
    +  /** Get or create a store associated with the id. */
    +  def get(storeId: StateStoreId, directory: String, version: Long): 
StateStore = {
    +    require(version >= 0)
    +    val storeProvider = loadedProviders.synchronized {
    +      startIfNeeded()
    +      val provider = loadedProviders.getOrElseUpdate(
    +          storeId, new HDFSBackedStateStoreProvider(storeId, directory))
    +      reportActiveInstance(storeId)
    +      provider
    +    }
    +    storeProvider.getStore(version)
    +  }
    +
    +  def remove(storeId: StateStoreId): Unit = loadedProviders.synchronized {
    +    loadedProviders.remove(storeId)
    +  }
    +
    +  def isLoaded(storeId: StateStoreId): Boolean = 
loadedProviders.synchronized {
    +    loadedProviders.contains(storeId)
    +  }
    +
    +  /** Unload and stop all state store provider */
    +  def stop(): Unit = loadedProviders.synchronized {
    +    loadedProviders.clear()
    +    if (managementTask != null) {
    +      managementTask.cancel()
    +      managementTask = null
    +      logInfo("StateStore stopped")
    +    }
    +  }
    +
    +  private def startIfNeeded(): Unit = loadedProviders.synchronized {
    +    if (managementTask == null) {
    +      managementTask = new TimerTask {
    +        override def run(): Unit = { manageAll() }
    +      }
    +      val periodMs = Option(SparkEnv.get).map(_.conf) match {
    --- End diff --
    
    SparkEnv.get returns null when a SparkContext is not active. I am afraid 
that this StateStore object's timer will keep ticking even if there is not 
SparkContext active (in local mode), and this is to make sure that this 
background this does not fail unexpectedly. 
    
    Actually on second thought, the timer does not need to be started if 
SparkEnv.get returns nulls, it only needs to be lazily stopped when 
SparkEnv.get becomes null (which it already does). I will modify this logic.


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