You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "rangadi (via GitHub)" <gi...@apache.org> on 2023/07/12 23:48:40 UTC

[GitHub] [spark] rangadi commented on a diff in pull request #41969: [SPARK-44398][CONNECT] Scala foreachBatch API

rangadi commented on code in PR #41969:
URL: https://github.com/apache/spark/pull/41969#discussion_r1261827112


##########
connector/connect/server/src/main/scala/org/apache/spark/sql/connect/planner/StreamingForeachBatchHelper.scala:
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.connect.planner
+
+import java.util.UUID
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.DataFrame
+import org.apache.spark.sql.connect.service.SessionHolder
+
+/**
+ * A helper class for handling ForeachBatch related functionality in Spark Connect servers
+ */
+object StreamingForeachBatchHelper extends Logging {
+
+  type ForeachBatchFnType = (DataFrame, Long) => Unit
+
+  /**
+   * Return a new ForeachBatch function that wraps `fn`. It sets up DataFrame cache
+   * so that the user function can access it. The cache is cleared once ForeachBatch returns.
+   */
+  def dataFrameCachingWrapper(fn: ForeachBatchFnType, sessionHolder: SessionHolder)
+    : ForeachBatchFnType = {
+    (df: DataFrame, batchId: Long) => {
+      val dfId = UUID.randomUUID().toString
+      log.info(s"Caching DataFrame with id $dfId") // TODO: Add query id to the log.
+
+      // TODO: Sanity check there is no other active DataFrame for this query. Need to include
+      //       query id available in the cache for this check.
+
+      sessionHolder.cacheDataFrameById(dfId, df)
+      try {
+        fn(df, batchId)
+      } finally {
+        log.info(s"Removing DataFrame with id $dfId from the cache")
+        sessionHolder.removeCachedDataFrame(dfId)
+      }
+    }
+  }
+
+  /**
+   * Handles setting up Scala remote session and other Spark Connect environment and then
+   * runs the provided foreachBatch function `fn`.
+   *
+   * HACK ALERT: This version does not atually set up Spark connect. Directly passes the DataFrame,

Review Comment:
    * Yes, it is about setting up spark remote session. I don't think there are examples of doing that in Scala. 
    * Not sure about the second one. Usually `df.sparkSession` gives the access to session. 



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org