gaogaotiantian commented on code in PR #60259:
URL: https://github.com/apache/spark/pull/60259#discussion_r4519867995


##########
python/pyspark/sql/profiler.py:
##########
@@ -92,6 +92,12 @@ def addInPlace(
 ProfileResultsParam = _ProfileResultsParam()
 
 
+# Guards the profile results map on the driver. The accumulator server thread 
merges worker
+# updates via _ProfileResultsParamV2.addInPlace while user threads read or 
clear the same dict
+# through AccumulatorProfilerCollector. It is module-level so the param stays 
picklable.
+_profile_results_lock = RLock()

Review Comment:
   Done in 63b014f07cc. The comment on `_ProfileResultsParamV2.lock` now 
records the constraint.



##########
python/pyspark/sql/profiler.py:
##########
@@ -569,6 +576,8 @@ def clear_sampling_profiles(self, id: Optional[Union[int, 
str]] = None) -> None:
 class AccumulatorProfilerCollector(ProfilerCollector):
     def __init__(self) -> None:
         super().__init__()
+        # Share the lock with the accumulator merge, which mutates the same 
dict.
+        self._lock = _profile_results_lock

Review Comment:
   Thanks. I reproduced it: with two threads, `show(type="perf")` raised 
`RuntimeError: dictionary changed size during iteration` every time. It is 
small, so I fixed it here in 2441ffb346d. `_perf_profile_results` copies the 
`Stats` under the lock, and show, dump and render format the copies. Memory 
merges only replace each file's list, so iterating cannot raise. Sampling is 
not formatted on master yet, so that is left for #60150. I updated the PR 
description.



##########
python/pyspark/sql/tests/test_udf_profiler.py:
##########
@@ -829,6 +831,32 @@ def setUp(self) -> None:
         super().setUp()
         self.spark._profiler_collector._accumulator._value = {}
 
+    def test_clear_during_accumulator_merge(self):
+        # The accumulator server thread merges worker updates into the same 
dict that
+        # spark.profile.clear() pops from. Pause a merge midway and clear from 
another thread.
+        accumulator = self.spark._profiler_collector._accumulator
+        merging, cleared = threading.Event(), threading.Event()
+        add_in_place = SamplingResultParam.addInPlace
+
+        def paused_add_in_place(value1, value2):
+            merging.set()
+            # Without a shared lock, the clear runs while the merge is waiting 
here.
+            cleared.wait(timeout=0.1)
+            return add_in_place(value1, value2)
+
+        def clear():
+            merging.wait()
+            self.spark.profile.clear()
+            cleared.set()
+
+        accumulator += {"id": {"sampling": {"count": 1}}}
+        with mock.patch.object(SamplingResultParam, "addInPlace", 
paused_add_in_place):
+            thread = threading.Thread(target=clear)

Review Comment:
   Done in 709fbc1e451: the helper is a daemon with bounded waits, and the test 
now asserts that the pause was reached.



##########
python/pyspark/sql/tests/test_udf_profiler.py:
##########
@@ -829,6 +831,32 @@ def setUp(self) -> None:
         super().setUp()
         self.spark._profiler_collector._accumulator._value = {}
 
+    def test_clear_during_accumulator_merge(self):
+        # The accumulator server thread merges worker updates into the same 
dict that
+        # spark.profile.clear() pops from. Pause a merge midway and clear from 
another thread.
+        accumulator = self.spark._profiler_collector._accumulator
+        merging, cleared = threading.Event(), threading.Event()
+        add_in_place = SamplingResultParam.addInPlace
+
+        def paused_add_in_place(value1, value2):
+            merging.set()
+            # Without a shared lock, the clear runs while the merge is waiting 
here.
+            cleared.wait(timeout=0.1)
+            return add_in_place(value1, value2)
+
+        def clear():
+            merging.wait()

Review Comment:
   Done in 709fbc1e451, including skipping the clear on timeout and asserting 
that the pause was reached and the join finished. Removing either the merge's 
lock or the collector's use of it still fails with `KeyError`. A merge to a new 
key now fails with the assertion in about 13s instead of hanging.



##########
python/pyspark/sql/profiler.py:
##########
@@ -569,6 +576,8 @@ def clear_sampling_profiles(self, id: Optional[Union[int, 
str]] = None) -> None:
 class AccumulatorProfilerCollector(ProfilerCollector):
     def __init__(self) -> None:
         super().__init__()
+        # Share the lock with the accumulator merge, which mutates the same 
dict.
+        self._lock = _profile_results_lock

Review Comment:
   Thanks for measuring. I reproduced the effect, though smaller: here the 
merge waited 37 to 39 ms with 5,000 retained 202-function perf profiles and a 
few ms at 1,000, and nearly all of the clear's time goes to freeing the removed 
profiles. We will leave it for now: `spark.profile.clear()` is mostly called 
interactively, the stall is a one-off of a few ms, and we will revisit it if it 
causes a real problem.



-- 
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]

Reply via email to