HyukjinKwon opened a new pull request #26588: SPARK-22340][PYTHON][FOLLOW-UP] 
Add a better message and improve documentation for pinned thread mode
URL: https://github.com/apache/spark/pull/26588
 
 
   ### What changes were proposed in this pull request?
   
   This PR proposes two things:
   
   1. Show an explicit workaround codes in the docstring to mimic local 
property inheritance when pinned thread mode is enabled.
   
       ![Screen Shot 2019-11-19 at 11 16 44 
AM](https://user-images.githubusercontent.com/6477701/69110461-38ba0300-0abe-11ea-9051-fe730643e73d.png)
   
   2. Show different warning message when the pinned thread mode is enabled:
   
       When enabled:
   
       > PYSPARK_PIN_THREAD feature is enabled. However, note that it cannot 
inherit the local properties from the parent thread although it isolates each 
thread on PVM and JVM with its own local properties.
       > To work around this, you should manually copy and set the local 
properties from the parent thread to the child thread when you create another 
thread.
   
       When disabled:
   
       > Currently, 'setLocalProperty' (set to local properties) with multiple 
threads does not properly work.
       > Internally threads on PVM and JVM are not synced, and JVM thread can 
be reused for multiple threads on PVM, which fails to isolate local properties 
for each thread on PVM.
       > To work around this, you can set PYSPARK_PIN_THREAD to true (see 
SPARK-22340). However, note that it cannot inherit the local properties from 
the parent thread although it isolates each thread on PVM and JVM with its own 
local properties.
       > To work around this, you should manually copy and set the local 
properties from the parent thread to the child thread when you create another 
thread.
   
   ### Why are the changes needed?
   
   Currently, it shows the same warning message regardless of 
PYSPARK_PIN_THREAD being set. In the warning message it says "you can set 
PYSPARK_PIN_THREAD to true ..." which is confusing.
   
   Also, I received a feedback about how to write the workaround class to mimic 
thread local inheritance. 
   
   ### Does this PR introduce any user-facing change?
   
   Documentation and warning message as shown above.
   
   ### How was this patch tested?
   
   Manually tested.
   
   For workaround,
   
   ```bash
   $ PYSPARK_PIN_THREAD=true ./bin/pyspark
   ```
   
   ```python
   class CustomThread(threading.Thread):
       def __init__(self, sc, target, *args, **kwargs):
           properties = sc._jsc.sc().getLocalProperties()
           def copy_local_properties(*a, **k),
               sc._jsc.sc().setLocalProperties(properties)
               return target(*a, **k)
           super(CustomThread, self).__init__(
               target=copy_local_properties, *args, **kwargs)
   
   sc.setLocalProperty("a", "b")
   CustomThread(sc, target=lambda: print(sc.getLocalProperty("a") == 
"b")).start()
   ```
   
   For warning messages:
   
   ```bash
   $ PYSPARK_PIN_THREAD=true ./bin/pyspark
   ```
   
   ```python
   sc.setJobGroup("a", "b")
   ```
   
   ```
   .../pyspark/util.py:141: UserWarning: PYSPARK_PIN_THREAD feature is enabled. 
However, note that it cannot inherit the local properties from the parent 
thread although it isolates each thread on PVM and JVM with its own local 
properties.
   To work around this, you should manually copy and set the local properties 
from the parent thread to the child thread when you create another thread.
     warnings.warn(msg, UserWarning)
   ```
   
   
   ```bash
   $ ./bin/pyspark
   ```
   
   ```python
   sc.setJobGroup("a", "b")
   ```
   
   ```
   .../pyspark/util.py:141: UserWarning: Currently, 'setJobGroup' (set to local 
properties) with multiple threads does not properly work.
   Internally threads on PVM and JVM are not synced, and JVM thread can be 
reused for multiple threads on PVM, which fails to isolate local properties for 
each thread on PVM.
   To work around this, you can set PYSPARK_PIN_THREAD to true (see 
SPARK-22340). However, note that it cannot inherit the local properties from 
the parent thread although it isolates each thread on PVM and JVM with its own 
local properties.
   To work around this, you should manually copy and set the local properties 
from the parent thread to the child thread when you create another thread.
     warnings.warn(msg, UserWarning)
   ```
   

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to