Skip to content

Commit a86831d

Browse files
committed
[SPARK-22043][PYTHON] Improves error message for show_profiles and dump_profiles
## What changes were proposed in this pull request? This PR proposes to improve error message from: ``` >>> sc.show_profiles() Traceback (most recent call last): File "<stdin>", line 1, in <module> File ".../spark/python/pyspark/context.py", line 1000, in show_profiles self.profiler_collector.show_profiles() AttributeError: 'NoneType' object has no attribute 'show_profiles' >>> sc.dump_profiles("/tmp/abc") Traceback (most recent call last): File "<stdin>", line 1, in <module> File ".../spark/python/pyspark/context.py", line 1005, in dump_profiles self.profiler_collector.dump_profiles(path) AttributeError: 'NoneType' object has no attribute 'dump_profiles' ``` to ``` >>> sc.show_profiles() Traceback (most recent call last): File "<stdin>", line 1, in <module> File ".../spark/python/pyspark/context.py", line 1003, in show_profiles raise RuntimeError("'spark.python.profile' configuration must be set " RuntimeError: 'spark.python.profile' configuration must be set to 'true' to enable Python profile. >>> sc.dump_profiles("/tmp/abc") Traceback (most recent call last): File "<stdin>", line 1, in <module> File ".../spark/python/pyspark/context.py", line 1012, in dump_profiles raise RuntimeError("'spark.python.profile' configuration must be set " RuntimeError: 'spark.python.profile' configuration must be set to 'true' to enable Python profile. ``` ## How was this patch tested? Unit tests added in `python/pyspark/tests.py` and manual tests. Author: hyukjinkwon <[email protected]> Closes #19260 from HyukjinKwon/profile-errors. (cherry picked from commit 7c72662) Signed-off-by: hyukjinkwon <[email protected]>
1 parent 309c401 commit a86831d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

python/pyspark/context.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,12 +994,20 @@ def runJob(self, rdd, partitionFunc, partitions=None, allowLocal=False):
994994

995995
def show_profiles(self):
996996
""" Print the profile stats to stdout """
997-
self.profiler_collector.show_profiles()
997+
if self.profiler_collector is not None:
998+
self.profiler_collector.show_profiles()
999+
else:
1000+
raise RuntimeError("'spark.python.profile' configuration must be set "
1001+
"to 'true' to enable Python profile.")
9981002

9991003
def dump_profiles(self, path):
10001004
""" Dump the profile stats into directory `path`
10011005
"""
1002-
self.profiler_collector.dump_profiles(path)
1006+
if self.profiler_collector is not None:
1007+
self.profiler_collector.dump_profiles(path)
1008+
else:
1009+
raise RuntimeError("'spark.python.profile' configuration must be set "
1010+
"to 'true' to enable Python profile.")
10031011

10041012
def getConf(self):
10051013
conf = SparkConf()

python/pyspark/tests.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,22 @@ def heavy_foo(x):
12881288
rdd.foreach(heavy_foo)
12891289

12901290

1291+
class ProfilerTests2(unittest.TestCase):
1292+
def test_profiler_disabled(self):
1293+
sc = SparkContext(conf=SparkConf().set("spark.python.profile", "false"))
1294+
try:
1295+
self.assertRaisesRegexp(
1296+
RuntimeError,
1297+
"'spark.python.profile' configuration must be set",
1298+
lambda: sc.show_profiles())
1299+
self.assertRaisesRegexp(
1300+
RuntimeError,
1301+
"'spark.python.profile' configuration must be set",
1302+
lambda: sc.dump_profiles("/tmp/abc"))
1303+
finally:
1304+
sc.stop()
1305+
1306+
12911307
class InputFormatTests(ReusedPySparkTestCase):
12921308

12931309
@classmethod

0 commit comments

Comments
 (0)