-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-56505][SQL][TESTS] Add SparkSessionBinder to replace SharedSparkSession #56190
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
Open
fwc
wants to merge
12
commits into
apache:master
Choose a base branch
from
fwc:sharedsparksession-refactor-mostly-nonbreaking
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5e059ba
Add classic.SparkSessionProvider
be85c95
Move test.SharedSparkSessionBase functionality to sql.SharedSparkSession
73a8535
[API CHANGE]: Move doThreadPreAudit, doThreadPostAudit to sql.SharedS…
c361709
Rename sql.SharedSparkSession to sql.SparkSessionBinder to prevent sh…
032a684
Deprecate test.SharedSparkSession
c9ea754
Add connect.SparkSession{Provider,Binder}, connect.QueryTest and demo
c9c8538
Add classic.SparkSessionBinder with usage demonstration
4c35b22
fixup: fix compile error
c52b50f
Restructure so that SparkSessionBinder implements QueryTest, address …
642166b
fixup
d51a967
Have SharedSparkSession as empty alias of classic.SparkSessionBinder
6b5dba7
fixup
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
sql/connect/server/src/test/scala/org/apache/spark/sql/connect/QueryTest.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| import org.apache.spark.{sql => sqlApi} | ||
|
|
||
| /** | ||
| * Extends [[sqlApi.QueryTest]] to provide connect-specific overrides to helpers like | ||
| * [[checkAnswer]] that avoid classic-only APIs. | ||
| * | ||
| * Can be used together with [[SparkSessionBinder connect.SparkSessionBinder]] to create a | ||
| * 'connect variant' of a test. | ||
| * | ||
| * Note: broader use will require more overrides. | ||
| */ | ||
| trait QueryTest extends sqlApi.QueryTest with SparkSessionProvider { | ||
|
|
||
| override protected def checkAnswer( | ||
| df: => sqlApi.DataFrame, expectedAnswer: Seq[sqlApi.Row]): Unit = { | ||
| val sparkAnswer = df.collect().toSeq | ||
| sqlApi.QueryTest.sameRows(expectedAnswer, sparkAnswer) match { | ||
| case Some(errorMessage) => fail(errorMessage) | ||
| case None => | ||
| } | ||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
...onnect/server/src/test/scala/org/apache/spark/sql/connect/QueryTestWithConnectSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| import org.apache.spark.sql.QueryTestSuite | ||
|
|
||
| /** | ||
| * Runs [[QueryTestSuite]] tests through a Connect session. | ||
| * | ||
| * This validates the `FooSuite with connect.SparkSessionBinder` pattern: the existing | ||
| * [[QueryTestSuite]] tests are inherited unchanged, but execute against a | ||
| * [[SparkSession connect.SparkSession]] instead of a classic one. | ||
| */ | ||
| class QueryTestWithConnectSuite | ||
| extends QueryTestSuite | ||
| with SparkSessionBinder |
84 changes: 84 additions & 0 deletions
84
sql/connect/server/src/test/scala/org/apache/spark/sql/connect/SparkSessionBinder.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| import java.util.UUID | ||
|
|
||
| import org.apache.spark.SparkEnv | ||
| import org.apache.spark.sql | ||
| import org.apache.spark.sql.classic | ||
| import org.apache.spark.sql.connect.client.SparkConnectClient | ||
| import org.apache.spark.sql.connect.config.Connect | ||
| import org.apache.spark.sql.connect.service.SparkConnectService | ||
|
|
||
| /** | ||
| * Provides a [[SparkSession connect.SparkSession]] backed by an in-process gRPC server. | ||
| * Extends [[sql.SparkSessionBinder sql.SparkSessionBinder]] (which creates a | ||
| * [[classic.SparkSession classic.SparkSession]] and SparkContext), then layers a Connect client | ||
| * session on top by starting the gRPC service in-process. | ||
| * | ||
| * Mix in this trait to exercise existing sql/core test suites through the Connect path: | ||
| * {{{ | ||
| * class FooWithConnectSuite | ||
| * extends FooSuite | ||
| * with connect.SparkSessionBinder | ||
| * }}} | ||
| */ | ||
| trait SparkSessionBinder extends sql.SparkSessionBinder with QueryTest { | ||
|
|
||
| private var _connectSpark: SparkSession = _ | ||
|
|
||
| protected override def spark: SparkSession = _connectSpark | ||
|
|
||
| /** The underlying classic session used by the in-process server. */ | ||
| private def classicSpark: classic.SparkSession = super.spark.asInstanceOf[classic.SparkSession] | ||
|
|
||
| override def beforeAll(): Unit = { | ||
| super.beforeAll() | ||
| val prevPort = SparkEnv.get.conf.get(Connect.CONNECT_GRPC_BINDING_PORT) | ||
| try { | ||
| // set GRPC_BINDING_PORT to 0 so that the server picks a random, freely available port. | ||
| SparkEnv.get.conf.set(Connect.CONNECT_GRPC_BINDING_PORT, 0) | ||
| SparkConnectService.start(classicSpark.sparkContext) | ||
| } finally { | ||
| SparkEnv.get.conf.set(Connect.CONNECT_GRPC_BINDING_PORT, prevPort) | ||
| } | ||
| val client = SparkConnectClient | ||
| .builder() | ||
| .port(SparkConnectService.localPort) | ||
| .sessionId(UUID.randomUUID().toString) | ||
| .userId("test") | ||
| .build() | ||
| _connectSpark = SparkSession | ||
| .builder() | ||
| .client(client) | ||
| .create() | ||
| } | ||
|
|
||
| override def afterAll(): Unit = { | ||
| try { | ||
| if (_connectSpark != null) { | ||
| _connectSpark.close() | ||
| _connectSpark = null | ||
| } | ||
| SparkConnectService.stop() | ||
| } finally { | ||
| super.afterAll() | ||
| } | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
sql/connect/server/src/test/scala/org/apache/spark/sql/connect/SparkSessionProvider.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| import org.apache.spark.sql | ||
|
|
||
| /** | ||
| * A common trait for test suites or utils that require a connect [[SparkSession]]. | ||
| * Use together with e.g. [[SparkSessionBinder]]. | ||
| */ | ||
| trait SparkSessionProvider extends sql.SparkSessionProvider { | ||
| protected override def spark: SparkSession | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
sql/core/src/test/scala/org/apache/spark/sql/SparkSessionBinder.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| /* | ||
| * 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.concurrent.duration._ | ||
|
|
||
| import org.scalatest.{BeforeAndAfterEach, Suite} | ||
| import org.scalatest.concurrent.Eventually | ||
|
|
||
| import org.apache.spark.{DebugFilesystem, SparkConf} | ||
| import org.apache.spark.internal.config.UNSAFE_EXCEPTION_ON_MEMORY_LEAK | ||
| import org.apache.spark.sql.catalyst.expressions.CodegenObjectFactoryMode | ||
| import org.apache.spark.sql.catalyst.optimizer.ConvertToLocalRelation | ||
| import org.apache.spark.sql.internal.{SQLConf, StaticSQLConf} | ||
| import org.apache.spark.sql.test.TestSparkSession | ||
|
|
||
| trait SparkSessionBinder extends QueryTest with SparkSessionBinderBase { | ||
|
|
||
| /** | ||
| * Suites extending this trait are sharing resources (e.g. SparkSession) in their | ||
| * tests. This trait initializes the spark session in its [[beforeAll()]] implementation before | ||
| * the automatic thread snapshot is performed, so the audit code could fail to report threads | ||
| * leaked by that shared session. | ||
| * | ||
| * The behavior is overridden here to take the snapshot before the spark session is initialized. | ||
| */ | ||
| override protected val enableAutoThreadAudit = false | ||
|
|
||
| protected override def beforeAll(): Unit = { | ||
| doThreadPreAudit() | ||
| super.beforeAll() | ||
| } | ||
|
|
||
| protected override def afterAll(): Unit = { | ||
| try { | ||
| super.afterAll() | ||
| } finally { | ||
| doThreadPostAudit() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| trait SparkSessionBinderBase | ||
| extends QueryTestBase | ||
| with SparkSessionProvider | ||
| with BeforeAndAfterEach | ||
| with Eventually { self: Suite => | ||
|
|
||
| protected def sparkConf = { | ||
| val conf = new SparkConf() | ||
| .set("spark.hadoop.fs.file.impl", classOf[DebugFilesystem].getName) | ||
| .set(UNSAFE_EXCEPTION_ON_MEMORY_LEAK, true) | ||
| .set(SQLConf.CODEGEN_FALLBACK.key, "false") | ||
| .set(SQLConf.CODEGEN_FACTORY_MODE.key, CodegenObjectFactoryMode.CODEGEN_ONLY.toString) | ||
| // Disable ConvertToLocalRelation for better test coverage. Test cases built on | ||
| // LocalRelation will exercise the optimization rules better by disabling it as | ||
| // this rule may potentially block testing of other optimization rules such as | ||
| // ConstantPropagation etc. | ||
| .set(SQLConf.OPTIMIZER_EXCLUDED_RULES.key, ConvertToLocalRelation.ruleName) | ||
| conf.set( | ||
| StaticSQLConf.WAREHOUSE_PATH, | ||
| conf.get(StaticSQLConf.WAREHOUSE_PATH) + "/" + getClass.getCanonicalName) | ||
| conf.set(StaticSQLConf.LOAD_SESSION_EXTENSIONS_FROM_CLASSPATH, false) | ||
| conf.set(StaticSQLConf.SHUFFLE_EXCHANGE_MAX_THREAD_THRESHOLD, | ||
| sys.env.getOrElse("SPARK_TEST_SQL_SHUFFLE_EXCHANGE_MAX_THREAD_THRESHOLD", | ||
| StaticSQLConf.SHUFFLE_EXCHANGE_MAX_THREAD_THRESHOLD.defaultValueString).toInt) | ||
| conf.set(StaticSQLConf.RESULT_QUERY_STAGE_MAX_THREAD_THRESHOLD, | ||
| sys.env.getOrElse("SPARK_TEST_SQL_RESULT_QUERY_STAGE_MAX_THREAD_THRESHOLD", | ||
| StaticSQLConf.RESULT_QUERY_STAGE_MAX_THREAD_THRESHOLD.defaultValueString).toInt) | ||
| } | ||
|
|
||
| /** | ||
| * The [[TestSparkSession]] to use for all tests in this suite. | ||
| * | ||
| * By default, the underlying [[org.apache.spark.SparkContext]] will be run in local | ||
| * mode with the default test configurations. | ||
| */ | ||
| private var _spark: classic.SparkSession = null | ||
|
|
||
| protected override def spark: SparkSession = _spark | ||
|
|
||
| /** | ||
| * The [[TestSQLContext]] to use for all tests in this suite. | ||
| */ | ||
| protected implicit def sqlContext: SQLContext = _spark.sqlContext | ||
|
|
||
| protected def createSparkSession: classic.SparkSession = { | ||
| classic.SparkSession.cleanupAnyExistingSession() | ||
| new TestSparkSession(sparkConf) | ||
| } | ||
|
|
||
| protected def sqlConf: SQLConf = _spark.sessionState.conf | ||
|
|
||
| /** | ||
| * Initialize the [[TestSparkSession]]. Generally, this is just called from | ||
| * beforeAll; however, in test using styles other than FunSuite, there is | ||
| * often code that relies on the session between test group constructs and | ||
| * the actual tests, which may need this session. It is purely a semantic | ||
| * difference, but semantically, it makes more sense to call | ||
| * 'initializeSession' between a 'describe' and an 'it' call than it does to | ||
| * call 'beforeAll'. | ||
| */ | ||
| protected def initializeSession(): Unit = { | ||
| if (_spark == null) { | ||
| _spark = createSparkSession | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Make sure the [[TestSparkSession]] is initialized before any tests are run. | ||
| */ | ||
| protected override def beforeAll(): Unit = { | ||
| initializeSession() | ||
|
|
||
| // Ensure we have initialized the context before calling parent code | ||
| super.beforeAll() | ||
| } | ||
|
|
||
| /** | ||
| * Stop the underlying [[org.apache.spark.SparkContext]], if any. | ||
| */ | ||
| protected override def afterAll(): Unit = { | ||
| try { | ||
| super.afterAll() | ||
| } finally { | ||
| try { | ||
| if (_spark != null) { | ||
| try { | ||
| _spark.sessionState.catalog.reset() | ||
| } finally { | ||
| _spark.stop() | ||
| _spark = null | ||
| } | ||
| } | ||
| } finally { | ||
| SparkSession.clearActiveSession() | ||
| SparkSession.clearDefaultSession() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected override def beforeEach(): Unit = { | ||
| super.beforeEach() | ||
| DebugFilesystem.clearOpenStreams() | ||
| } | ||
|
|
||
| protected override def afterEach(): Unit = { | ||
| super.afterEach() | ||
| // Clear all persistent datasets after each test | ||
| _spark.sharedState.cacheManager.clearCache() | ||
| // files can be closed from other threads, so wait a bit | ||
| // normally this doesn't take more than 1s | ||
| eventually(timeout(10.seconds), interval(2.seconds)) { | ||
| DebugFilesystem.assertNoOpenStreams() | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This overrides only the
checkAnswer(df, Seq[Row])variant, which is enough forQueryTestSuite. But the stated goal is re-running arbitrarysql/coresuites over Connect, and the otherQueryTesthelpers (othercheckAnsweroverloads,checkDataset, ...) still reach classic-only paths likequeryExecution/logicalPlan. Worth a line in the trait doc noting that broader reuse will need more overrides.