jingz-db commented on code in PR #49156:
URL: https://github.com/apache/spark/pull/49156#discussion_r1887728579


##########
python/pyspark/sql/streaming/transform_with_state_driver_worker.py:
##########
@@ -0,0 +1,89 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import os
+import json
+
+from pyspark.util import local_connect_and_auth
+from pyspark.serializers import (
+    write_int,
+    read_int,
+    UTF8Deserializer,
+    CPickleSerializer,
+)
+from pyspark import worker
+from pyspark.util import handle_worker_exception
+from typing import IO
+from pyspark.worker_util import check_python_version
+from pyspark.sql.streaming.stateful_processor_api_client import 
StatefulProcessorApiClient
+from pyspark.sql.streaming.stateful_processor_util import 
TransformWithStateInPandasFuncMode
+from pyspark.sql.types import StructType
+
+pickle_ser = CPickleSerializer()
+utf8_deserializer = UTF8Deserializer()
+
+
+def main(infile: IO, outfile: IO) -> None:
+    check_python_version(infile)
+
+    log_name = "Streaming TransformWithStateInPandas Python worker"
+
+    def process(processor, mode, key, input):
+        print(f"{log_name} Started execution of UDF: {func}.\n")
+        func(processor, mode, key, input)
+        print(f"{log_name} Completed execution of UDF: {func}.\n")
+
+    try:
+        func, return_type = worker.read_command(pickle_ser, infile)
+        # send signal for getting args
+        write_int(0, outfile)
+        outfile.flush()
+
+        while True:
+            state_server_port = read_int(infile)
+            key_schema = 
StructType.fromJson(json.loads(utf8_deserializer.loads(infile)))
+            print(
+                f"{log_name} received parameters for UDF. State server port: 
{state_server_port}, "
+                f"key schema: {key_schema}.\n"
+            )
+
+            stateful_processor_api_client = StatefulProcessorApiClient(
+                state_server_port, key_schema
+            )
+            process(
+                stateful_processor_api_client,
+                TransformWithStateInPandasFuncMode.PRE_INIT,
+                None,
+                iter([]),
+            )
+            write_int(0, outfile)
+            outfile.flush()
+    except Exception as e:
+        handle_worker_exception(e, outfile)
+        outfile.flush()
+
+
+if __name__ == "__main__":
+    # Read information about how to connect back to the JVM from the 
environment.
+    java_port = int(os.environ["PYTHON_WORKER_FACTORY_PORT"])
+    auth_secret = os.environ["PYTHON_WORKER_FACTORY_SECRET"]
+    (sock_file, sock) = local_connect_and_auth(java_port, auth_secret)
+    # There could be a long time between each micro batch.
+    sock.settimeout(None)

Review Comment:
   I am a bit hesitate what is a good number to set here. The timeout = 
overhead set up and tear down time + user defined init() function execution 
time + network delay. This is quite indefinite as we can not predict how long 
it will take to finish `init()` UDF. What do you think? Do you think we should 
set this a config, or shall we just keep as indefinite?



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