-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-56324] Introducing message-based communication to Spark -> PySpark communication channel #55716
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
base: master
Are you sure you want to change the base?
[SPARK-56324] Introducing message-based communication to Spark -> PySpark communication channel #55716
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # | ||
| # 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. | ||
| # |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # | ||
| # 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. | ||
| # | ||
|
|
||
| from typing import BinaryIO | ||
|
|
||
| from pyspark.serializers import read_int, SpecialLengths | ||
| from pyspark.messages.zero_copy_byte_stream import ZeroCopyByteStream | ||
| from pyspark.messages.spark_message_receiver import ( | ||
| SparkMessageReceiver, | ||
| ) | ||
|
|
||
|
|
||
| def _assert_message_id(message_id: int, expected: int) -> None: | ||
| assert message_id == expected, ( | ||
| f"Expected message with id {expected} " + f"but got message with id {message_id} instead." | ||
| ) | ||
|
|
||
|
|
||
| class SparkSocketMessageReceiver(SparkMessageReceiver): | ||
| def __init__(self, infile: BinaryIO): | ||
| super().__init__() | ||
| self._infile = infile | ||
|
|
||
| def _do_get_init_message(self) -> ZeroCopyByteStream: | ||
| message_id = read_int(self._infile) | ||
| _assert_message_id(message_id, SpecialLengths.START_OF_INIT_MESSAGE) | ||
|
|
||
| # Read the length and init content | ||
| message_length = read_int(self._infile) | ||
| message_content = self._infile.read(message_length) | ||
|
|
||
| return ZeroCopyByteStream(memoryview(message_content)) | ||
|
|
||
| def _do_get_data_stream(self) -> BinaryIO: | ||
| # For socket communication, we just pass along the underlying socket | ||
| # for the data channel. We already stripped the initialization data | ||
| # at this state. Therefore, any bytes following this are data bytes. | ||
| # | ||
| # Note: We deliberately did not introduce a message header for | ||
| # data messages to reduce the overhead, especially for small | ||
| # batch sizes and real-time-mode (RTM). | ||
| return self._infile | ||
|
|
||
| def _do_is_stream_finished(self) -> bool: | ||
| # Check if the stream is finished. | ||
| # If everything finished properly, we should read a | ||
| # 'END_OF_STREAM'. If we read somethign else this means | ||
| # the stream has unread data and something went wrong | ||
| # during processing. | ||
| return read_int(self._infile) == SpecialLengths.END_OF_STREAM | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # | ||
| # 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. | ||
| # | ||
|
|
||
| from enum import Enum | ||
| from functools import wraps | ||
| from typing import BinaryIO, Callable, TypeVar | ||
| from abc import ABC, abstractmethod | ||
|
|
||
| from pyspark.messages.zero_copy_byte_stream import ZeroCopyByteStream | ||
|
|
||
|
|
||
| T = TypeVar("T", bound="SparkMessageReceiver") | ||
| R = TypeVar("R") | ||
|
|
||
|
|
||
| class MessageState(Enum): | ||
| WAITING_FOR_INIT = 1 | ||
| WAITING_FOR_DATA = 2 | ||
| WAITING_FOR_FINISH = 3 | ||
| DONE = 4 | ||
|
|
||
|
|
||
| class SparkMessageReceiver(ABC): | ||
| """ | ||
| Generic class that implements receiving messages from Spark. | ||
| Caution: This class is STATEFUL. It is expected, that the | ||
| methods of this class are called in the following order: | ||
|
sven-weber-db marked this conversation as resolved.
|
||
|
|
||
| 1. Init -> 2. Data stream -> 3. Finish | ||
|
|
||
| This order is verified using assertions in the class. Each function | ||
| can be called EXACTLY ONCE in the specified order. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| self._state = MessageState.WAITING_FOR_INIT | ||
|
|
||
| @staticmethod | ||
| def _state_transition( | ||
| required_state: MessageState, next_state: MessageState | ||
| ) -> Callable[[Callable[[T], R]], Callable[[T], R]]: | ||
| """Decorator to enforce state transitions.""" | ||
|
|
||
| def decorator(func: Callable[[T], R]) -> Callable[[T], R]: | ||
| @wraps(func) | ||
| def wrapper(self: T) -> R: | ||
| assert self._state == required_state | ||
| result = func(self) | ||
| self._state = next_state | ||
| return result | ||
|
|
||
| return wrapper | ||
|
|
||
| return decorator | ||
|
|
||
| @_state_transition(MessageState.WAITING_FOR_INIT, MessageState.WAITING_FOR_DATA) | ||
| def get_init_message(self) -> ZeroCopyByteStream: | ||
| """ | ||
| Returns: | ||
| the binary contents of the initial message as a ZeroCopyByteStream. | ||
| """ | ||
| return self._do_get_init_message() | ||
|
|
||
| @_state_transition(MessageState.WAITING_FOR_DATA, MessageState.WAITING_FOR_FINISH) | ||
| def get_data_stream(self) -> BinaryIO: | ||
| """ | ||
| Returns: | ||
| A binary stream containing the data to invoke the UDF on. | ||
| """ | ||
| return self._do_get_data_stream() | ||
|
|
||
| @_state_transition(MessageState.WAITING_FOR_FINISH, MessageState.DONE) | ||
| def is_stream_finished(self) -> bool: | ||
| """ | ||
| Checks if a finish message was received | ||
| from the JVM. The finish message itself only | ||
| has a message id and marks the end of the stream. | ||
| If bytes different from the finish id are read | ||
| this means something went wrong while consuming the stream. | ||
| """ | ||
| return self._do_is_stream_finished() | ||
|
|
||
| @abstractmethod | ||
| def _do_get_init_message(self) -> ZeroCopyByteStream: | ||
| """ | ||
| Returns the contents of the init message | ||
| as a 'ZeroCopyByteStream'. | ||
|
|
||
| To be implemented by child classes. | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def _do_get_data_stream(self) -> BinaryIO: | ||
| """ | ||
| Returns the Spark data stream. | ||
|
|
||
| To be implemented by child classes. | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def _do_is_stream_finished(self) -> bool: | ||
| """ | ||
| Blocking call that returns whether | ||
| the data stream from the JVM is finished. | ||
| This is implemented differently, depending | ||
| on the transport channel. | ||
|
|
||
| To be implemented by child classes. | ||
| """ | ||
| pass | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,7 @@ | |
| import zlib | ||
| import itertools | ||
| import pickle | ||
| import codecs | ||
|
|
||
| pickle_protocol = pickle.HIGHEST_PROTOCOL | ||
|
|
||
|
|
@@ -84,6 +85,7 @@ class SpecialLengths: | |
| END_OF_STREAM = -4 | ||
| NULL = -5 | ||
| START_ARROW_STREAM = -6 | ||
| START_OF_INIT_MESSAGE = -8 | ||
|
|
||
|
|
||
| class Serializer: | ||
|
|
@@ -539,7 +541,7 @@ def loads(self, stream): | |
| elif length == SpecialLengths.NULL: | ||
| return None | ||
| s = stream.read(length) | ||
| return s.decode("utf-8") if self.use_unicode else s | ||
| return codecs.decode(s, "utf-8") if self.use_unicode else s | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is required because we use the According to the Python documentation, for
As decoding errors are unexpected and the specific Exception type that is thrown should not matter, I believe this change is safe. However, if there are concerns we can change this implementation to the following: return bytes(s).decode("utf-8") if self.use_unicode else sThis alternative implementation will invoke a memory copy to copy the |
||
|
|
||
| def load_stream(self, stream): | ||
| try: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,7 +161,7 @@ def _getOrCreate(cls: Type["TaskContext"]) -> "TaskContext": | |
| return cls._taskContext | ||
|
|
||
| @classmethod | ||
| def _setTaskContext(cls: Type["TaskContext"], taskContext: "TaskContext") -> None: | ||
| def _setTaskContext(cls: Type["TaskContext"], taskContext: Optional["TaskContext"]) -> None: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is required to make # Cleanup
# Reset task context to None. This is a guard code to avoid residual context when worker
# reuse.
TaskContext._setTaskContext(None)
BarrierTaskContext._setTaskContext(None)It is unclear to my why this type check if failing only now since this code has been in |
||
| cls._taskContext = taskContext | ||
|
|
||
| @classmethod | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.