-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-43474] [SS] [CONNECT] Add a spark connect access to runtime Dataframes by ID. #41580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e50fa97
e0c711d
647ca67
1ca1f3a
5d25a32
8a05f9b
dde3e36
ef62238
d3c2503
74b98ca
b6e8f3d
bb2ad33
5af56c9
5c0c2de
6dca3e6
8de5197
dbca798
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,10 +28,13 @@ import org.json4s.JsonDSL._ | |
| import org.json4s.jackson.JsonMethods.{compact, render} | ||
|
|
||
| import org.apache.spark.JobArtifactSet | ||
| import org.apache.spark.SparkException | ||
| import org.apache.spark.connect.proto | ||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.sql.DataFrame | ||
| import org.apache.spark.sql.SparkSession | ||
| import org.apache.spark.sql.connect.artifact.SparkConnectArtifactManager | ||
| import org.apache.spark.sql.connect.common.InvalidPlanInput | ||
| import org.apache.spark.util.Utils | ||
|
|
||
| /** | ||
|
|
@@ -43,6 +46,10 @@ case class SessionHolder(userId: String, sessionId: String, session: SparkSessio | |
| val executePlanOperations: ConcurrentMap[String, ExecutePlanHolder] = | ||
| new ConcurrentHashMap[String, ExecutePlanHolder]() | ||
|
|
||
| // Mapping from relation ID (passed to client) to runtime dataframe. Used for callbacks like | ||
| // foreachBatch() in Streaming. Lazy since most sessions don't need it. | ||
| private lazy val dataFrameCache: ConcurrentMap[String, DataFrame] = new ConcurrentHashMap() | ||
|
|
||
| private[connect] def createExecutePlanHolder( | ||
| request: proto.ExecutePlanRequest): ExecutePlanHolder = { | ||
|
|
||
|
|
@@ -163,6 +170,31 @@ case class SessionHolder(userId: String, sessionId: String, session: SparkSessio | |
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Caches given DataFrame with the ID. The cache does not expire. The entry needs to be | ||
| * explicitly removed by the owners of the DataFrame once it is not needed. | ||
| */ | ||
| private[connect] def cacheDataFrameById(dfId: String, df: DataFrame): Unit = { | ||
| if (dataFrameCache.putIfAbsent(dfId, df) != null) { | ||
| SparkException.internalError(s"A dataframe is already associated with id $dfId") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns [[DataFrame]] cached for DataFrame ID `dfId`. If it is not found, throw | ||
| * [[InvalidPlanInput]]. | ||
| */ | ||
| private[connect] def getDataFrameOrThrow(dfId: String): DataFrame = { | ||
| Option(dataFrameCache.get(dfId)) | ||
| .getOrElse { | ||
| throw InvalidPlanInput(s"No DataFrame with id $dfId is found in the session $sessionId") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same here, how about to introduce an error class?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be not needed since |
||
| } | ||
| } | ||
|
|
||
| private[connect] def removeCachedDataFrame(dfId: String): DataFrame = { | ||
| dataFrameCache.remove(dfId) | ||
| } | ||
| } | ||
|
|
||
| object SessionHolder { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * 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.service | ||
|
|
||
| import org.apache.spark.sql.connect.common.InvalidPlanInput | ||
| import org.apache.spark.sql.test.SharedSparkSession | ||
|
|
||
| class SparkConnectSessionHolderSuite extends SharedSparkSession { | ||
|
|
||
| test("DataFrame cache: Successful put and get") { | ||
| val sessionHolder = SessionHolder.forTesting(spark) | ||
| import sessionHolder.session.implicits._ | ||
|
|
||
| val data1 = Seq(("k1", "v1"), ("k2", "v2"), ("k3", "v3")) | ||
| val df1 = data1.toDF() | ||
| val id1 = "df_id_1" | ||
| sessionHolder.cacheDataFrameById(id1, df1) | ||
|
|
||
| val expectedDf1 = sessionHolder.getDataFrameOrThrow(id1) | ||
| assert(expectedDf1 == df1) | ||
|
|
||
| val data2 = Seq(("k4", "v4"), ("k5", "v5")) | ||
| val df2 = data2.toDF() | ||
| val id2 = "df_id_2" | ||
| sessionHolder.cacheDataFrameById(id2, df2) | ||
|
|
||
| val expectedDf2 = sessionHolder.getDataFrameOrThrow(id2) | ||
| assert(expectedDf2 == df2) | ||
| } | ||
|
|
||
| test("DataFrame cache: Should throw when dataframe is not found") { | ||
| val sessionHolder = SessionHolder.forTesting(spark) | ||
| import sessionHolder.session.implicits._ | ||
|
|
||
| val key1 = "key_1" | ||
|
|
||
| assertThrows[InvalidPlanInput] { | ||
| sessionHolder.getDataFrameOrThrow(key1) | ||
| } | ||
|
|
||
| val data1 = Seq(("k1", "v1"), ("k2", "v2"), ("k3", "v3")) | ||
| val df1 = data1.toDF() | ||
| sessionHolder.cacheDataFrameById(key1, df1) | ||
| sessionHolder.getDataFrameOrThrow(key1) | ||
|
|
||
| val key2 = "key_2" | ||
| assertThrows[InvalidPlanInput] { | ||
| sessionHolder.getDataFrameOrThrow(key2) | ||
| } | ||
| } | ||
|
|
||
| test("DataFrame cache: Remove cache and then get should fail") { | ||
| val sessionHolder = SessionHolder.forTesting(spark) | ||
| import sessionHolder.session.implicits._ | ||
|
|
||
| val key1 = "key_1" | ||
| val data1 = Seq(("k1", "v1"), ("k2", "v2"), ("k3", "v3")) | ||
| val df1 = data1.toDF() | ||
| sessionHolder.cacheDataFrameById(key1, df1) | ||
| sessionHolder.getDataFrameOrThrow(key1) | ||
|
|
||
| sessionHolder.removeCachedDataFrame(key1) | ||
| assertThrows[InvalidPlanInput] { | ||
| sessionHolder.getDataFrameOrThrow(key1) | ||
| } | ||
| } | ||
| } |
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.