-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-58751][SS][PYTHON] Stop leaking Python workers on TransformWithState init failure #57941
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
jon-gao-db
wants to merge
2
commits into
apache:master
Choose a base branch
from
jon-gao-db:fix/transform-with-state-pre-init-cleanup
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
2 commits
Select commit
Hold shift + click to select a range
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
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
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
166 changes: 166 additions & 0 deletions
166
...spark/sql/execution/python/streaming/TransformWithStateInPySparkPreInitCleanupSuite.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,166 @@ | ||
| /* | ||
| * 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.python.streaming | ||
|
|
||
| import java.io.{DataInputStream, DataOutputStream} | ||
| import java.util.{ArrayList, HashMap} | ||
| import java.util.concurrent.atomic.{AtomicBoolean, AtomicInteger} | ||
|
|
||
| import org.apache.spark.SparkException | ||
| import org.apache.spark.api.python.{PythonFunction, SimplePythonFunction} | ||
| import org.apache.spark.sql.execution.streaming.operators.stateful.transformwithstate.statefulprocessor.DriverStatefulProcessorHandleImpl | ||
| import org.apache.spark.sql.streaming.TimeMode | ||
| import org.apache.spark.sql.test.SharedSparkSession | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
| class TransformWithStateInPySparkPreInitCleanupSuite extends SharedSparkSession { | ||
|
|
||
| private val groupingKeySchema = StructType(Nil) | ||
|
|
||
| private def newPythonFunction(): PythonFunction = { | ||
| new SimplePythonFunction( | ||
| command = Seq.empty[Byte], | ||
| envVars = new HashMap[String, String](), | ||
| pythonIncludes = new ArrayList[String](), | ||
| pythonExec = "python3", | ||
| pythonVer = "3", | ||
| broadcastVars = null, | ||
| accumulator = null) | ||
| } | ||
|
|
||
| private def newDriverHandle(): DriverStatefulProcessorHandleImpl = { | ||
| new DriverStatefulProcessorHandleImpl(TimeMode.None(), null) | ||
| } | ||
|
|
||
| private class StubPreInitRunner( | ||
| failInitWith: Option[Throwable] = None, | ||
| failProcessWith: Option[Throwable] = None, | ||
| failStopWith: Option[Throwable] = None) | ||
| extends TransformWithStateInPySparkPythonPreInitRunner( | ||
| newPythonFunction(), | ||
| "pyspark.sql.streaming.transform_with_state_driver_worker", | ||
| groupingKeySchema, | ||
| newDriverHandle()) { | ||
|
|
||
| val workerAlive = new AtomicBoolean(false) | ||
| val initCount = new AtomicInteger(0) | ||
| val processCount = new AtomicInteger(0) | ||
| val stopCount = new AtomicInteger(0) | ||
|
|
||
| override def init(): (DataOutputStream, DataInputStream) = { | ||
| initCount.incrementAndGet() | ||
| workerAlive.set(true) | ||
| failInitWith.foreach(error => throw error) | ||
| (null, null) | ||
| } | ||
|
|
||
| override def process(): Unit = { | ||
| processCount.incrementAndGet() | ||
| failProcessWith.foreach(error => throw error) | ||
| } | ||
|
|
||
| override def stop(): Unit = { | ||
| stopCount.incrementAndGet() | ||
| workerAlive.set(false) | ||
| failStopWith.foreach(error => throw error) | ||
| } | ||
| } | ||
|
|
||
| private def runPreInit(runner: TransformWithStateInPySparkPythonPreInitRunner): Unit = { | ||
| TransformWithStateInPySparkExec.runPreInitRunner(runner) | ||
| } | ||
|
|
||
| test("init failure after worker creation still stops the runner") { | ||
| val initFailure = new RuntimeException("init failed") | ||
| val runner = new StubPreInitRunner(failInitWith = Some(initFailure)) | ||
|
|
||
| val thrown = intercept[Throwable] { | ||
| runPreInit(runner) | ||
| } | ||
|
|
||
| assert(thrown eq initFailure) | ||
| assert(runner.initCount.get() === 1) | ||
| assert(runner.stopCount.get() === 1) | ||
| assert(!runner.workerAlive.get()) | ||
| assert(runner.processCount.get() === 0) | ||
| } | ||
|
|
||
| test("repeated init failures do not accumulate live workers") { | ||
| val runners = (1 to 20).map { _ => | ||
| val runner = new StubPreInitRunner(failInitWith = Some(new RuntimeException("init failed"))) | ||
| intercept[Throwable] { | ||
| runPreInit(runner) | ||
| } | ||
| runner | ||
| } | ||
|
|
||
| assert(runners.forall(_.initCount.get() === 1)) | ||
| assert(runners.count(_.workerAlive.get()) === 0) | ||
| } | ||
|
|
||
| test("a stop failure does not mask the original init failure") { | ||
|
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. do we need to also test stop + process failures? (since this is only stop + init) |
||
| val initFailure = new RuntimeException("init failed") | ||
| val stopFailure = new IllegalStateException("cleanup failed") | ||
| val runner = | ||
| new StubPreInitRunner(failInitWith = Some(initFailure), failStopWith = Some(stopFailure)) | ||
|
|
||
| val thrown = intercept[Throwable] { | ||
| runPreInit(runner) | ||
| } | ||
|
|
||
| assert(thrown eq initFailure) | ||
| assert(thrown.getSuppressed.contains(stopFailure)) | ||
| assert(runner.stopCount.get() === 1) | ||
| assert(!runner.workerAlive.get()) | ||
| } | ||
|
|
||
| test("process failure is wrapped but still stops the runner") { | ||
| val processFailure = new RuntimeException("worker crashed") | ||
| val runner = new StubPreInitRunner(failProcessWith = Some(processFailure)) | ||
|
|
||
| val thrown = intercept[SparkException] { | ||
| runPreInit(runner) | ||
| } | ||
|
|
||
| assert(thrown.getMessage.contains("exited unexpectedly (crashed)")) | ||
| assert(thrown.getCause eq processFailure) | ||
| assert(runner.stopCount.get() === 1) | ||
| assert(!runner.workerAlive.get()) | ||
| } | ||
|
|
||
| test("success path stops the runner exactly once") { | ||
| val runner = new StubPreInitRunner() | ||
|
|
||
| runPreInit(runner) | ||
|
|
||
| assert(runner.initCount.get() === 1) | ||
| assert(runner.processCount.get() === 1) | ||
| assert(runner.stopCount.get() === 1) | ||
| assert(!runner.workerAlive.get()) | ||
| } | ||
|
|
||
| test("stop before startStateServer ran does not throw NPE") { | ||
| val runner = new TransformWithStateInPySparkPythonPreInitRunner( | ||
| newPythonFunction(), | ||
| "pyspark.sql.streaming.transform_with_state_driver_worker", | ||
| groupingKeySchema, | ||
| newDriverHandle()) | ||
|
|
||
| runner.stop() | ||
| runner.stop() | ||
| } | ||
| } | ||
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.
Is there any actual concurrency here? Seems like regular ints/booleans would suffice? (Doesn't hurt to have atomic though)