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
9 changes: 6 additions & 3 deletions python/pyspark/logger/worker_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,11 @@ def context_provider() -> dict[str, str]:
- class_name: Name of the class that initiated the logging if available
"""

def is_pyspark_module(module_name: str) -> bool:
def is_pyspark_module(frame: FrameType) -> bool:
module_name = frame.f_globals.get("__name__", "")
if module_name == "__main__":
if (mod := sys.modules.get("__main__", None)) and mod.__spec__:
module_name = mod.__spec__.name
Comment on lines +226 to +230
Copy link
Contributor

Choose a reason for hiding this comment

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

just so i understand, these logging changes were needed/discovered after adding the test or just a cleanup?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needed for the test. The original code did not consider the simple worker case, where the worker module is executed with python -m XXX - the __name__ would be __main__ and our method to check if it's pyspark module will give a wrong result.

return module_name.startswith("pyspark.") and ".tests." not in module_name

bottom: Optional[FrameType] = None
Expand All @@ -236,9 +240,8 @@ def is_pyspark_module(module_name: str) -> bool:
if frame:
while frame.f_back:
f_back = frame.f_back
module_name = f_back.f_globals.get("__name__", "")

if is_pyspark_module(module_name):
if is_pyspark_module(f_back):
if not is_in_pyspark_module:
bottom = frame
is_in_pyspark_module = True
Expand Down
39 changes: 30 additions & 9 deletions python/pyspark/sql/tests/test_python_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -1237,8 +1237,20 @@ def writer(self, schema, overwrite):

logs = self.spark.tvf.python_worker_logs()

# We could get either 1 or 2 "TestJsonWriter.write: abort test" logs because
# the operation is time sensitive. When the first partition gets aborted,
# the executor will cancel the rest of the tasks. Whether we are able to get
# the second log depends on whether the second partition starts before the
# cancellation. When we use simple worker, the second log is often missing
# because the spawn overhead is large.
non_abort_logs = logs.select("level", "msg", "context", "logger").filter(
"msg != 'TestJsonWriter.write: abort test'"
)
abort_logs = logs.select("level", "msg", "context", "logger").filter(
"msg == 'TestJsonWriter.write: abort test'"
)
assertDataFrameEqual(
logs.select("level", "msg", "context", "logger"),
non_abort_logs,
[
Row(
level="WARNING",
Expand Down Expand Up @@ -1283,21 +1295,24 @@ def writer(self, schema, overwrite):
"TestJsonWriter.__init__: ['abort', 'path']",
{"class_name": "TestJsonDataSource", "func_name": "writer"},
),
(
"TestJsonWriter.write: abort test",
{"class_name": "TestJsonWriter", "func_name": "write"},
),
(
"TestJsonWriter.write: abort test",
{"class_name": "TestJsonWriter", "func_name": "write"},
),
(
"TestJsonWriter.abort",
{"class_name": "TestJsonWriter", "func_name": "abort"},
),
]
],
)
assertDataFrameEqual(
abort_logs.dropDuplicates(["msg"]),
[
Row(
level="WARNING",
msg="TestJsonWriter.write: abort test",
context={"class_name": "TestJsonWriter", "func_name": "write"},
logger="test_datasource_writer",
)
],
)

def test_data_source_perf_profiler(self):
with self.sql_conf({"spark.sql.pyspark.dataSource.profiler": "perf"}):
Expand Down Expand Up @@ -1345,6 +1360,12 @@ class PythonDataSourceTests(BasePythonDataSourceTestsMixin, ReusedSQLTestCase):
...


class PythonDataSourceTestsWithSimpleWorker(PythonDataSourceTests):
@classmethod
def conf(self):
return super().conf().set("spark.python.use.daemon", "false")


if __name__ == "__main__":
from pyspark.testing import main

Expand Down