Skip to content

Conversation

@WweiL
Copy link
Contributor

@WweiL WweiL commented Jul 24, 2023

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

Copy link
Contributor

@bogao007 bogao007 left a 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?

Copy link
Contributor

@bogao007 bogao007 left a 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
Copy link
Contributor

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!

@WweiL
Copy link
Contributor Author

WweiL commented Jul 28, 2023

@HyukjinKwon @ueshin guys can you take another look? Thanks! This also needs to goto 3.5 sorry for the trouble!

@WweiL
Copy link
Contributor Author

WweiL commented Jul 31, 2023

Hi Takuya @ueshin, could you check if this could be merged? Thanks!

@ueshin
Copy link
Member

ueshin commented Jul 31, 2023

Thanks! merging to master/3.5.

@ueshin ueshin closed this in 799ab87 Jul 31, 2023
@ueshin
Copy link
Member

ueshin commented Jul 31, 2023

@WweiL There was a conflict with 3.5. Could you submit another PR to backport this? Thanks.

bogao007 pushed a commit to bogao007/spark that referenced this pull request Jul 31, 2023
### 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]>
@bogao007
Copy link
Contributor

bogao007 commented Jul 31, 2023

@WweiL There was a conflict with 3.5. Could you submit another PR to backport this? Thanks.

@ueshin Created a backport PR to 3.5 branch #42250, could you help take a look? Thanks!

bogao007 pushed a commit to bogao007/spark that referenced this pull request Jul 31, 2023
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]>
HyukjinKwon pushed a commit that referenced this pull request Aug 4, 2023
…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]>
ueshin pushed a commit that referenced this pull request Aug 5, 2023
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants