-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-42941][SS][CONNECT] Python StreamingQueryListener #42116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
bogao007
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add unit tests as well as tests using spark session inside listener?
...connect/server/src/main/scala/org/apache/spark/sql/connect/planner/SparkConnectPlanner.scala
Outdated
Show resolved
Hide resolved
...erver/src/main/scala/org/apache/spark/sql/connect/planner/StreamingQueryListenerHelper.scala
Show resolved
Hide resolved
…pyspark.sql.tests.connect.streaming.test_parity_listener'
bogao007
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| command.getRemoveListener.getListenerPayload.toByteArray, | ||
| Utils.getContextOrSparkClassLoader) | ||
| .id | ||
| val listenerId = command.getRemoveListener.getId |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing this change!
|
@HyukjinKwon @ueshin guys can you take another look? Thanks! This also needs to goto 3.5 sorry for the trouble! |
core/src/main/scala/org/apache/spark/api/python/StreamingPythonRunner.scala
Outdated
Show resolved
Hide resolved
|
Hi Takuya @ueshin, could you check if this could be merged? Thanks! |
|
Thanks! merging to master/ |
|
@WweiL There was a conflict with 3.5. Could you submit another PR to backport this? Thanks. |
### What changes were proposed in this pull request?
Implement the python streaming query listener and the `addListener` method and `removeListener` method, follow up filed in: SPARK-44516 to actually terminate the query listener process when `removeListener` is called. SPARK-44516 depends on SPARK-44433.
### Why are the changes needed?
SS Connect development
### Does this PR introduce _any_ user-facing change?
Yes now they can use connect listener
### How was this patch tested?
Manual test and added unit test
#### addListener:
```
# Client side:
>>> from pyspark.sql.streaming.listener import StreamingQueryListener;from pyspark.sql.streaming.listener import (QueryStartedEvent, QueryProgressEvent, QueryTerminatedEvent, QueryIdleEvent)
>>> class MyListener(StreamingQueryListener):
... def onQueryStarted(self, event: QueryStartedEvent) -> None: print("hi, event query id is: " + str(event.id)); df=self.spark.createDataFrame(["10","11","13"], "string").toDF("age"); df.write.saveAsTable("tbllistener1")
... def onQueryProgress(self, event: QueryProgressEvent) -> None: pass
... def onQueryIdle(self, event: QueryIdleEvent) -> None: pass
... def onQueryTerminated(self, event: QueryTerminatedEvent) -> None: pass
...
>>> spark.streams.addListener(MyListener())
>>> q = spark.readStream.format("rate").load().writeStream.format("console").start()
>>> q.stop()
>>> spark.read.table("tbllistener1").collect()
[Row(age='13'), Row(age='10'), Row(age='11’)]
# Server side:
##### event_type received from python process is 0
hi, event query id is: dd7ba1c4-6c8f-4369-9c3c-5dede22b8a2f
```
#### removeListener:
```
# Client side:
>>> listener = MyListener(); spark.streams.addListener(listener)
>>> spark.streams.removeListener(listener)
# Server side:
# nothing to print actually, the listener is removed from server side StreamingQueryManager and cache in sessionHolder, but the process still hangs there. Follow up SPARK-44516 filed to stop this process
```
Closes apache#42116 from WweiL/listener-poc-newest.
Lead-authored-by: Wei Liu <[email protected]>
Co-authored-by: pengzhon-db <[email protected]>
Signed-off-by: Takuya UESHIN <[email protected]>
Implement the python streaming query listener and the `addListener` method and `removeListener` method, follow up filed in: SPARK-44516 to actually terminate the query listener process when `removeListener` is called. SPARK-44516 depends on SPARK-44433.
SS Connect development
Yes now they can use connect listener
Manual test and added unit test
```
>>> from pyspark.sql.streaming.listener import StreamingQueryListener;from pyspark.sql.streaming.listener import (QueryStartedEvent, QueryProgressEvent, QueryTerminatedEvent, QueryIdleEvent)
>>> class MyListener(StreamingQueryListener):
... def onQueryStarted(self, event: QueryStartedEvent) -> None: print("hi, event query id is: " + str(event.id)); df=self.spark.createDataFrame(["10","11","13"], "string").toDF("age"); df.write.saveAsTable("tbllistener1")
... def onQueryProgress(self, event: QueryProgressEvent) -> None: pass
... def onQueryIdle(self, event: QueryIdleEvent) -> None: pass
... def onQueryTerminated(self, event: QueryTerminatedEvent) -> None: pass
...
>>> spark.streams.addListener(MyListener())
>>> q = spark.readStream.format("rate").load().writeStream.format("console").start()
>>> q.stop()
>>> spark.read.table("tbllistener1").collect()
[Row(age='13'), Row(age='10'), Row(age='11’)]
hi, event query id is: dd7ba1c4-6c8f-4369-9c3c-5dede22b8a2f
```
```
>>> listener = MyListener(); spark.streams.addListener(listener)
>>> spark.streams.removeListener(listener)
```
Closes apache#42116 from WweiL/listener-poc-newest.
Lead-authored-by: Wei Liu <[email protected]>
Co-authored-by: pengzhon-db <[email protected]>
Signed-off-by: Takuya UESHIN <[email protected]>
…ss with `removeListener` and improvements ### What changes were proposed in this pull request? This is a followup to #42116. It addresses the following issues: 1. When `removeListener` is called upon one listener, before the python process is left running, now it also get stopped. 2. When multiple `removeListener` is called on the same listener, in non-connect mode, subsequent calls will be noop. But before this PR, in connect it actually throws an error, which doesn't align with existing behavior, this PR addresses it. 3. Set the socket timeout to be None (\infty) for `foreachBatch_worker` and `listener_worker`, because there could be a long time between each microbatch. If not setting this, the socket will timeout and won't be able to process new data. ``` scala> Streaming query listener worker is starting with url sc://localhost:15002/;user_id=wei.liu and sessionId 886191f0-2b64-4c44-b067-de511f04b42d. Traceback (most recent call last): File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.9/runpy.py", line 87, in _run_code exec(code, run_globals) File "/home/wei.liu/oss-spark/python/lib/pyspark.zip/pyspark/sql/connect/streaming/worker/listener_worker.py", line 95, in <module> File "/home/wei.liu/oss-spark/python/lib/pyspark.zip/pyspark/sql/connect/streaming/worker/listener_worker.py", line 82, in main File "/home/wei.liu/oss-spark/python/lib/pyspark.zip/pyspark/serializers.py", line 557, in loads File "/home/wei.liu/oss-spark/python/lib/pyspark.zip/pyspark/serializers.py", line 594, in read_int File "/usr/lib/python3.9/socket.py", line 704, in readinto return self._sock.recv_into(b) socket.timeout: timed out ``` ### Why are the changes needed? Necessary improvements ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? Manual test + unit test Closes #42283 from WweiL/SPARK-44433-listener-process-termination. Authored-by: Wei Liu <[email protected]> Signed-off-by: Hyukjin Kwon <[email protected]>
…process with removeListener and improvements ### Master Branch PR: #42283 ### What changes were proposed in this pull request? This is a followup to #42116. It addresses the following issues: 1. When `removeListener` is called upon one listener, before the python process is left running, now it also get stopped. 2. When multiple `removeListener` is called on the same listener, in non-connect mode, subsequent calls will be noop. But before this PR, in connect it actually throws an error, which doesn't align with existing behavior, this PR addresses it. 3. Set the socket timeout to be None (\infty) for `foreachBatch_worker` and `listener_worker`, because there could be a long time between each microbatch. If not setting this, the socket will timeout and won't be able to process new data. ``` scala> Streaming query listener worker is starting with url sc://localhost:15002/;user_id=wei.liu and sessionId 886191f0-2b64-4c44-b067-de511f04b42d. Traceback (most recent call last): File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.9/runpy.py", line 87, in _run_code exec(code, run_globals) File "/home/wei.liu/oss-spark/python/lib/pyspark.zip/pyspark/sql/connect/streaming/worker/listener_worker.py", line 95, in <module> File "/home/wei.liu/oss-spark/python/lib/pyspark.zip/pyspark/sql/connect/streaming/worker/listener_worker.py", line 82, in main File "/home/wei.liu/oss-spark/python/lib/pyspark.zip/pyspark/serializers.py", line 557, in loads File "/home/wei.liu/oss-spark/python/lib/pyspark.zip/pyspark/serializers.py", line 594, in read_int File "/usr/lib/python3.9/socket.py", line 704, in readinto return self._sock.recv_into(b) socket.timeout: timed out ``` ### Why are the changes needed? Necessary improvements ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? Manual test + unit test Closes #42340 from WweiL/SPARK-44433-listener-followup-3.5. Authored-by: Wei Liu <[email protected]> Signed-off-by: Takuya UESHIN <[email protected]>
What changes were proposed in this pull request?
Implement the python streaming query listener and the
addListenermethod andremoveListenermethod, follow up filed in: SPARK-44516 to actually terminate the query listener process whenremoveListeneris called. SPARK-44516 depends on SPARK-44433.Why are the changes needed?
SS Connect development
Does this PR introduce any user-facing change?
Yes now they can use connect listener
How was this patch tested?
Manual test and added unit test
addListener:
removeListener: