ericm-db commented on code in PR #56907: URL: https://github.com/apache/spark/pull/56907#discussion_r3547839639
########## python/pyspark/sql/connect/local_server.py: ########## @@ -0,0 +1,212 @@ +# +# 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. +# + +""" +Long-lived local Spark Connect server for the opt-in reuse path. + +This module is launched as a *detached* child process by +``SparkSession._reuse_or_start_local_connect_server`` (in ``pyspark.sql.connect.session``) when +``SPARK_LOCAL_CONNECT_REUSE`` / ``spark.local.connect.reuse`` is enabled. It starts a regular +(classic) local Spark session with the Spark Connect plugin -- the same mechanism the in-process +``SparkSession._start_connect_server`` uses -- and then blocks, so one warm JVM and Spark Connect +server keeps serving many short-lived client processes. Each client connection gets its own +isolated server-side session, so session-local state (temp views, runtime SQL confs, isolated +artifacts) does not leak between runs. + +Once the server is accepting connections it writes a discovery file (host, the actually bound port, +the auth token, its pid and the Spark version) that later client processes read to reconnect. + +It is launched by file path rather than ``python -m`` so it does not require the Spark Connect +*client* dependencies (grpc, etc.): a server only needs a classic PySpark install plus the Connect +server jar, like ``sbin/start-connect-server.sh``. It imports only the classic ``pyspark.sql`` API. +""" + +import sys + +# Launching by file path puts this file's directory -- pyspark/sql/connect -- at the front of +# sys.path, where modules such as `types` and `logging` shadow the standard library and break +# ordinary imports. Drop it before importing anything else (`import sys` cannot be shadowed); +# pyspark stays importable via the remaining sys.path entries. +if sys.path: + del sys.path[0] + +import argparse +import json +import os +import signal +import time +from typing import Any + + +def _write_discovery(path: str, host: str, port: int, token: str, version: str) -> None: + """Atomically write the discovery file with ``0600`` perms (it holds the auth token).""" + parent = os.path.dirname(path) + if parent and not os.path.isdir(parent): + os.makedirs(parent, exist_ok=True) + payload = { + "host": host, + "port": port, + "token": token, + "pid": os.getpid(), + "spark_version": version, + } + tmp = "{}.{}.tmp".format(path, os.getpid()) + fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) + with os.fdopen(fd, "w") as f: + f.write(json.dumps(payload)) + os.replace(tmp, path) + + +def _remove_discovery_if_ours(path: str) -> None: + """Remove the discovery file, but only if it still points at this process.""" + try: + with open(path, "r") as f: + disc = json.load(f) + except (OSError, ValueError): + return + if disc.get("pid") == os.getpid(): Review Comment: Yeah there was one, between read and unlink. A freshly started server could have replaced the file and the exiting daemon would delete the newcomer's entry. It's fixed now though, the launching client has a lock, which removal needs to fetch. If the removal can't fetch it (startup is currently being executed), removal is skipped. Added a test for this. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
