-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-27958][SQL] Stopping a SparkSession should not always stop Spark Context #24807
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 3 commits
bcdd070
f333a30
8fc95e9
ff60b84
f69cabb
3fafd2a
0c9c426
843491f
92c7b22
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 |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ package org.apache.spark.sql | |
|
|
||
| import java.io.Closeable | ||
| import java.util.concurrent.TimeUnit._ | ||
| import java.util.concurrent.atomic.AtomicReference | ||
| import java.util.concurrent.atomic.{AtomicInteger, AtomicReference} | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
| import scala.collection.mutable | ||
|
|
@@ -711,12 +711,15 @@ class SparkSession private( | |
| // scalastyle:on | ||
|
|
||
| /** | ||
| * Stop the underlying `SparkContext`. | ||
| * Stop the underlying `SparkContext` if there are are no active sessions remaining. | ||
| * | ||
| * @since 2.0.0 | ||
| */ | ||
| def stop(): Unit = { | ||
|
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. Hey, I think this was a design decision that stopping sessions stops spark context too. Why don't you just don't call
Contributor
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 idea is that if one creates a multi-tenant Spark process, and you give each user a Spark session, you want to be able to close down the resources for one session (e.g. connections to JDBC, perhaps), but not stop the entire Spark Context, thus keeping the Spark Context alive for the other users.
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. |
||
| sparkContext.stop() | ||
| SparkSession.clearActiveSession() | ||
|
Contributor
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. Should we also clear the defaultSession() ? Otherwise, the
Contributor
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. I thought about this as well and here were my thoughts:
Contributor
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 problem is now we update both |
||
| if (SparkSession.numActiveSessions.get() == 0) { | ||
|
Contributor
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. Shouldn't this remove itself from the active sessions before checking that there are 0 remaining?
Contributor
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. ah, good catch |
||
| sparkContext.stop() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -776,6 +779,8 @@ class SparkSession private( | |
| @Stable | ||
| object SparkSession extends Logging { | ||
|
|
||
| private[spark] val numActiveSessions: AtomicInteger = new AtomicInteger(0) | ||
|
Contributor
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. This is error prone and also not easy to debug, we may need to keep track of each active SparkSession in SparkContext. We don't need to pass in the SparkSession instance, maybe only compute the
Contributor
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. Yeah, I think the identity hash code could be an interesting solution here. I'll experiment with that |
||
|
|
||
| /** | ||
| * Builder for [[SparkSession]]. | ||
| */ | ||
|
|
@@ -958,6 +963,8 @@ object SparkSession extends Logging { | |
| sparkContext.addSparkListener(new SparkListener { | ||
| override def onApplicationEnd(applicationEnd: SparkListenerApplicationEnd): Unit = { | ||
| defaultSession.set(null) | ||
| // Should remove listener after this event fires | ||
| sparkContext.removeSparkListener(this) | ||
|
Contributor
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. I think this should be called in the
Contributor
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. Yeah, that would be ideal - the problem is that we lost the handle to this listener outside of this method. I could create a global
Contributor
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. It should be fine to have a global var of SparkListener inside SparkSession.
Contributor
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. Okay, will add this in! |
||
| } | ||
| }) | ||
| } | ||
|
|
@@ -981,17 +988,30 @@ object SparkSession extends Logging { | |
| * @since 2.0.0 | ||
| */ | ||
| def setActiveSession(session: SparkSession): Unit = { | ||
| activeThreadSession.set(session) | ||
| if (getActiveSession.isEmpty | ||
| || (session != getActiveSession.get && getActiveSession.isDefined)) { | ||
|
||
| numActiveSessions.getAndIncrement | ||
|
Contributor
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 problem I see here is: if I have 2 sessions, and I set one as active then set another. If I keep doing this then the count here will be wrong. I don't have a good idea to track the last alive session. Even if we can, users may want to create more sessions later and not stop the SparkContext. The
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. Another problem is that the current behaviour is clearly documented. It doesn't look particularly wrong either:
We're trying to make a behaviour change just based on the new design choice.
Contributor
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. yea, the right way to "free" a session is to leave it and wait for it to be GCed. If there is something that can't be GCed, it's a memory leak and we should fix it.
Contributor
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. @cloud-fan @HyukjinKwon - thanks for the thoughts. @cloud-fan - The memory leak is detailed here #24807 (comment). @HyukjinKwon - I actually think that despite the fact that the current behavior is clearly documented, it actually doesn't make sense and is error prone. I detailed an example here #24807 (comment) of how this could bit us. Is there a world where a user should be able to call
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. If the leak is problem, we should fix it rather than changing the behaviour. It is documented and users are relying on this behaviour I don't think we should just change without guarding. All other projects related to Spark such as Zeppelin would need to revisit their behaviour about how to stop, and it would make it difficult them to support multiple Spark versions for instance. |
||
| activeThreadSession.set(session) | ||
| } else if (session == null) { | ||
| this.clearActiveSession() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Clears the active SparkSession for current thread. Subsequent calls to getOrCreate will | ||
| * return the first created context instead of a thread-local override. | ||
| * Clears the active SparkSession for current thread assuming it is defined. | ||
| * Subsequent calls to getOrCreate will return the first created context | ||
| * instead of a thread-local override. | ||
| * | ||
| * @since 2.0.0 | ||
| */ | ||
| def clearActiveSession(): Unit = { | ||
| activeThreadSession.remove() | ||
| if (getActiveSession.isDefined) { | ||
| activeThreadSession.remove() | ||
| numActiveSessions.decrementAndGet() | ||
| } else { | ||
| logWarning("Calling clearActiveSession() on a SparkSession " + | ||
| "without an active session is a noop.") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1004,12 +1024,17 @@ object SparkSession extends Logging { | |
| } | ||
|
|
||
| /** | ||
| * Clears the default SparkSession that is returned by the builder. | ||
| * | ||
| * Clears the default SparkSession that is returned by the builder | ||
| * if it is not null. | ||
| * @since 2.0.0 | ||
| */ | ||
| def clearDefaultSession(): Unit = { | ||
| defaultSession.set(null) | ||
| if (getDefaultSession.isDefined) { | ||
| defaultSession.set(null) | ||
| } else { | ||
| logWarning("Calling clearDefaultSession() on a SparkSession " + | ||
| "without an default session is a noop.") | ||
| } | ||
|
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. For me, the existing behavior of this function is a quiet no-op and looks better. |
||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * 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 org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.internal.config.UI.UI_ENABLED | ||
|
|
||
|
|
||
| /** | ||
| * Test cases for the lifecycle of a [[SparkSession]]. | ||
| */ | ||
| class SparkSessionLifecycleSuite extends SparkFunSuite { | ||
| test("test SparkContext stopped when last SparkSession is stopped ") { | ||
|
||
| val session1 = SparkSession.builder() | ||
| .master("local") | ||
| .config(UI_ENABLED.key, value = false) | ||
| .config("some-config", "a") | ||
| .getOrCreate() | ||
|
|
||
| assert(!session1.sparkContext.isStopped) | ||
|
|
||
| val session2 = SparkSession.builder() | ||
| .master("local") | ||
| .config(UI_ENABLED.key, value = false) | ||
| .config("some-config", "b") | ||
| .getOrCreate() | ||
|
|
||
| session1.stop() | ||
|
||
| session2.stop() | ||
| assert(session1.sparkContext.isStopped) | ||
| } | ||
|
|
||
| test("test SparkContext is not stopped when other sessions exist") { | ||
| val session1 = SparkSession.builder() | ||
| .master("local") | ||
| .config(UI_ENABLED.key, value = false) | ||
| .config("some-config", "a") | ||
| .getOrCreate() | ||
|
|
||
| assert(!session1.sparkContext.isStopped) | ||
|
|
||
| val session2 = SparkSession.builder() | ||
| .master("local") | ||
| .config(UI_ENABLED.key, value = false) | ||
| .config("some-config", "b") | ||
| .getOrCreate() | ||
|
|
||
| session1.stop() | ||
| assert(!session1.sparkContext.isStopped) | ||
|
||
| } | ||
| } | ||
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.
are are->are.