Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private[spark] class PythonWorkerFactory(
@GuardedBy("self")
private var daemonPort: Int = 0
@GuardedBy("self")
private val daemonWorkers = new mutable.WeakHashMap[PythonWorker, Long]()
private val daemonWorkers = new mutable.WeakHashMap[PythonWorker, ProcessHandle]()
@GuardedBy("self")
private val idleWorkers = new mutable.Queue[PythonWorker]()
@GuardedBy("self")
Expand All @@ -95,10 +95,20 @@ private[spark] class PythonWorkerFactory(
def create(): (PythonWorker, Option[Long]) = {
if (useDaemon) {
self.synchronized {
if (idleWorkers.nonEmpty) {
// Pull from idle workers until we one that is alive, otherwise create a new one.
while (idleWorkers.nonEmpty) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that there is a chance to introduce an infinite loop to Apache Spark. Maybe, limit the number of retry? WDYT, @sebastianhillig-db ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On each iteration, a worker is pulled from idleWorkers, this will end up "emptying" the pool. The synchronization around this will ensure that no other workers are added while this happens. (see https://github.com/apache/spark/pull/45635/files/ba3c6f6ee19762278004594735f25ab4f6fafb3e#diff-1bd846874b06327e6abd0803aa74eed890352dfa974d5c1da1a12dc7477e20d0L411-L413)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On each iteration, a worker is pulled from idleWorkers, this will end up "emptying" the pool. The synchronization around this will ensure that no other workers are added while this happens. (see https://github.com/apache/spark/pull/45635/files/ba3c6f6ee19762278004594735f25ab4f6fafb3e#diff-1bd846874b06327e6abd0803aa74eed890352dfa974d5c1da1a12dc7477e20d0L411-L413)

The link seems to be broken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, sorry - the force push broke that link. I'm referring to "releaseWorker" using the same synchronization, so we should not be adding new workers while this code runs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val worker = idleWorkers.dequeue()
worker.selectionKey.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE)
return (worker, daemonWorkers.get(worker))
val workerHandle = daemonWorkers(worker)
if (workerHandle.isAlive()) {
try {
worker.selectionKey.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE)
return (worker, Some(workerHandle.pid()))
} catch {
case c: CancelledKeyException => /* pass */
}
}
logWarning(s"Worker ${worker} process from idle queue is dead, discarding.")
stopWorker(worker)
}
}
createThroughDaemon()
Expand All @@ -121,15 +131,16 @@ private[spark] class PythonWorkerFactory(
if (pid < 0) {
throw new IllegalStateException("Python daemon failed to launch worker with code " + pid)
}

val processHandle = ProcessHandle.of(pid).orElseThrow(
() => new IllegalStateException("Python daemon failed to launch worker.")
)
authHelper.authToServer(socketChannel.socket())
socketChannel.configureBlocking(false)
val selector = Selector.open()
val selectionKey = socketChannel.register(selector,
SelectionKey.OP_READ | SelectionKey.OP_WRITE)
val worker = PythonWorker(socketChannel, selector, selectionKey)

daemonWorkers.put(worker, pid)
daemonWorkers.put(worker, processHandle)
(worker, Some(pid))
}

Expand Down Expand Up @@ -391,10 +402,10 @@ private[spark] class PythonWorkerFactory(
self.synchronized {
if (useDaemon) {
if (daemon != null) {
daemonWorkers.get(worker).foreach { pid =>
daemonWorkers.get(worker).foreach { processHandle =>
// tell daemon to kill worker by pid
val output = new DataOutputStream(daemon.getOutputStream)
output.writeLong(pid)
output.writeLong(processHandle.pid())
output.flush()
daemon.getOutputStream.flush()
}
Expand Down
16 changes: 16 additions & 0 deletions python/pyspark/tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# limitations under the License.
#
import os
import signal
import sys
import tempfile
import threading
Expand Down Expand Up @@ -256,6 +257,21 @@ def conf(cls):
return _conf


class WorkerPoolCrashTest(PySparkTestCase):
def test_worker_crash(self):
# SPARK-47565: Kill a worker that is currently idling
rdd = self.sc.parallelize(range(20), 4)
# first ensure that workers are reused
worker_pids1 = set(rdd.map(lambda x: os.getpid()).collect())
worker_pids2 = set(rdd.map(lambda x: os.getpid()).collect())
self.assertEqual(worker_pids1, worker_pids2)
for pid in list(worker_pids1)[1:]: # kill all workers except for one
os.kill(pid, signal.SIGTERM)
# give things a moment to settle
time.sleep(5)
rdd.map(lambda x: os.getpid()).collect()


if __name__ == "__main__":
import unittest
from pyspark.tests.test_worker import * # noqa: F401
Expand Down