Skip to content

Commit b35136a

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 99de4b8 commit b35136a

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
@@ -970,12 +970,20 @@ def runJob(self, rdd, partitionFunc, partitions=None, allowLocal=False):
970970

971971
def show_profiles(self):
972972
""" Print the profile stats to stdout """
973-
self.profiler_collector.show_profiles()
973+
if self.profiler_collector is not None:
974+
self.profiler_collector.show_profiles()
975+
else:
976+
raise RuntimeError("'spark.python.profile' configuration must be set "
977+
"to 'true' to enable Python profile.")
974978

975979
def dump_profiles(self, path):
976980
""" Dump the profile stats into directory `path`
977981
"""
978-
self.profiler_collector.dump_profiles(path)
982+
if self.profiler_collector is not None:
983+
self.profiler_collector.dump_profiles(path)
984+
else:
985+
raise RuntimeError("'spark.python.profile' configuration must be set "
986+
"to 'true' to enable Python profile.")
979987

980988
def getConf(self):
981989
conf = SparkConf()

python/pyspark/tests.py

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

12251225

1226+
class ProfilerTests2(unittest.TestCase):
1227+
def test_profiler_disabled(self):
1228+
sc = SparkContext(conf=SparkConf().set("spark.python.profile", "false"))
1229+
try:
1230+
self.assertRaisesRegexp(
1231+
RuntimeError,
1232+
"'spark.python.profile' configuration must be set",
1233+
lambda: sc.show_profiles())
1234+
self.assertRaisesRegexp(
1235+
RuntimeError,
1236+
"'spark.python.profile' configuration must be set",
1237+
lambda: sc.dump_profiles("/tmp/abc"))
1238+
finally:
1239+
sc.stop()
1240+
1241+
12261242
class InputFormatTests(ReusedPySparkTestCase):
12271243

12281244
@classmethod

0 commit comments

Comments
 (0)