Github user zsxwing commented on a diff in the pull request:
https://github.com/apache/spark/pull/11645#discussion_r57065449
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/StateStoreCoordinator.scala
---
@@ -0,0 +1,140 @@
+/*
+ * 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 scala.collection.mutable
+
+import org.apache.spark.SparkEnv
+import org.apache.spark.internal.Logging
+import org.apache.spark.rpc.{RpcCallContext, RpcEndpoint, RpcEndpointRef,
RpcEnv}
+import org.apache.spark.scheduler.ExecutorCacheTaskLocation
+import org.apache.spark.util.RpcUtils
+
+/** Trait representing all messages to [[StateStoreCoordinator]] */
+private sealed trait StateStoreCoordinatorMessage extends Serializable
+
+/** Classes representing messages */
+private case class ReportActiveInstance(storeId: StateStoreId, host:
String, executorId: String)
+ extends StateStoreCoordinatorMessage
+
+private case class VerifyIfInstanceActive(storeId: StateStoreId,
executorId: String)
+ extends StateStoreCoordinatorMessage
+
+private case class GetLocation(storeId: StateStoreId)
+ extends StateStoreCoordinatorMessage
+
+private case class DeactivateInstances(storeRootLocation: String)
+ extends StateStoreCoordinatorMessage
+
+private object StopCoordinator
+ extends StateStoreCoordinatorMessage
+
+/** Helper object used to create reference to [[StateStoreCoordinator]]. */
+private[sql] object StateStoreCoordinatorRef extends Logging {
+
+ private val endpointName = "StateStoreCoordinator"
+
+ /**
+ * Create a reference to a [[StateStoreCoordinator]], This can be called
from driver as well as
+ * executors.
+ */
+ def apply(env: SparkEnv): StateStoreCoordinatorRef = synchronized {
+ try {
+ val coordinator = new StateStoreCoordinator(env.rpcEnv)
+ val coordinatorRef = env.rpcEnv.setupEndpoint(endpointName,
coordinator)
+ logInfo("Registered StateStoreCoordinator endpoint")
+ new StateStoreCoordinatorRef(coordinatorRef)
+ } catch {
+ case e: IllegalArgumentException =>
+ logDebug("Retrieving existing StateStoreCoordinator endpoint")
+ val rpcEndpointRef = RpcUtils.makeDriverRef(endpointName,
env.conf, env.rpcEnv)
+ new StateStoreCoordinatorRef(rpcEndpointRef)
+ }
+ }
+}
+
+/**
+ * Reference to a [[StateStoreCoordinator]] that can be used to
coordinator instances of
+ * [[StateStore]]s across all the executors, and get their locations for
job scheduling.
+ */
+private[sql] class StateStoreCoordinatorRef private(rpcEndpointRef:
RpcEndpointRef) {
+
+ private[state] def reportActiveInstance(
+ storeId: StateStoreId,
+ host: String,
+ executorId: String): Unit = {
+ rpcEndpointRef.send(ReportActiveInstance(storeId, host, executorId))
+ }
+
+ /** Verify whether the given executor has the active instance of a state
store */
+ private[state] def verifyIfInstanceActive(storeId: StateStoreId,
executorId: String): Boolean = {
+ rpcEndpointRef.askWithRetry[Boolean](VerifyIfInstanceActive(storeId,
executorId))
+ }
+
+ /** Get the location of the state store */
+ private[state] def getLocation(storeId: StateStoreId): Option[String] = {
+ rpcEndpointRef.askWithRetry[Option[String]](GetLocation(storeId))
+ }
+
+ /** Deactivate instances related to a set of operator */
+ private[state] def deactivateInstances(storeRootLocation: String): Unit
= {
+
rpcEndpointRef.askWithRetry[Boolean](DeactivateInstances(storeRootLocation))
+ }
+
+ private[state] def stop(): Unit = {
+ rpcEndpointRef.askWithRetry[Boolean](StopCoordinator)
+ }
+}
+
+
+/**
+ * Class for coordinating instances of [[StateStore]]s loaded in executors
across the cluster,
+ * and get their locations for job scheduling.
+ */
+private class StateStoreCoordinator(override val rpcEnv: RpcEnv) extends
RpcEndpoint {
+ private val instances = new mutable.HashMap[StateStoreId,
ExecutorCacheTaskLocation]
+
+ override def receive: PartialFunction[Any, Unit] = {
+ case ReportActiveInstance(id, host, executorId) =>
+ instances.put(id, ExecutorCacheTaskLocation(host, executorId))
+ }
+
+ override def receiveAndReply(context: RpcCallContext):
PartialFunction[Any, Unit] = {
+ case VerifyIfInstanceActive(id, execId) =>
+ val response = instances.get(id) match {
+ case Some(location) => location.executorId == execId
+ case None => false
+ }
+ context.reply(response)
+
+ case GetLocation(id) =>
+ context.reply(instances.get(id).map(_.toString))
+
+ case DeactivateInstances(loc) =>
+ val storeIdsToRemove =
+ instances.keys.filter(_.checkpointLocation == loc).toSeq
+ instances --= storeIdsToRemove
--- End diff --
nit: You can use one line code here: `instances.retain((storeId, _) =>
storeId.checkpointLocation != loc)`
---
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]