Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,17 @@ private[spark] abstract class BasePythonRunner[IN, OUT](
*/
def writeNextInputToStream(dataOut: DataOutputStream): Boolean

def open(dataOut: DataOutputStream): Unit = Utils.logUncaughtExceptions {
def open(outputStream: DataOutputStream): Unit = Utils.logUncaughtExceptions {
val isUnixDomainSock = authHelper.conf.get(PYTHON_UNIX_DOMAIN_SOCKET_ENABLED)
lazy val sockPath = new File(
authHelper.conf.get(PYTHON_UNIX_DOMAIN_SOCKET_DIR)
.getOrElse(System.getProperty("java.io.tmpdir")),
s".${UUID.randomUUID()}.sock")
try {
// Buffer the initialization message, and send it together with its length.
val buffer = new ByteArrayOutputStream()
val dataOut = new DataOutputStream(buffer)

// Partition index
dataOut.writeInt(partitionIndex)

Expand Down Expand Up @@ -522,6 +526,13 @@ private[spark] abstract class BasePythonRunner[IN, OUT](
writeCommand(dataOut)

dataOut.flush()

// The initialization message is complete, write it to the stream with its length.
val messageBytes = buffer.toByteArray
outputStream.writeInt(SpecialLengths.START_OF_INIT_MESSAGE)
outputStream.writeInt(messageBytes.length)
outputStream.write(messageBytes)
outputStream.flush()
} catch {
case t: Throwable if NonFatal(t) || t.isInstanceOf[Exception] =>
if (context.isCompleted() || context.isInterrupted()) {
Expand Down Expand Up @@ -1085,6 +1096,7 @@ private[spark] object SpecialLengths {
val NULL = -5
val START_ARROW_STREAM = -6
val END_OF_MICRO_BATCH = -7
val START_OF_INIT_MESSAGE = -8
}

private[spark] object BarrierTaskContextMessageProtocol {
Expand Down
1 change: 1 addition & 0 deletions dev/sparktestsupport/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ def __hash__(self):
"pyspark.tests.test_readwrite",
"pyspark.tests.test_serializers",
"pyspark.tests.test_shuffle",
"pyspark.tests.test_spark_message_receiver",
"pyspark.tests.test_statcounter",
"pyspark.tests.test_taskcontext",
"pyspark.tests.test_util",
Expand Down
64 changes: 45 additions & 19 deletions python/benchmarks/bench_eval_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import pyarrow as pa

from pyspark.cloudpickle import dumps as cloudpickle_dumps
from pyspark.serializers import write_int, write_long
from pyspark.serializers import write_int, write_long, SpecialLengths
from pyspark.sql.types import (
BinaryType,
BooleanType,
Expand Down Expand Up @@ -127,6 +127,46 @@ def write_preamble(cls, buf: io.BytesIO) -> None:
cls.write_bool(False, buf) # needs_broadcast_decryption_server
write_int(0, buf) # num_broadcast_variables

@classmethod
def write_init_message(
cls,
eval_type: int,
write_udf: Callable[[io.BufferedIOBase], None],
target_buffer: io.BytesIO,
runner_conf: dict[str, str] | None = None,
eval_conf: dict[str, str] | None = None,
) -> None:
"""Write the initial message with header, length + its data."""

# Write everything to a seperate buffer so we can
# determine the length of the initial message.
buf = io.BytesIO()
cls.write_preamble(buf)
write_int(eval_type, buf)
if runner_conf:
write_int(len(runner_conf), buf)
for k, v in runner_conf.items():
cls.write_utf8(k, buf)
cls.write_utf8(v, buf)
else:
write_int(0, buf) # RunnerConf (0 key-value pairs)
if eval_conf:
write_int(len(eval_conf), buf)
for k, v in eval_conf.items():
cls.write_utf8(k, buf)
cls.write_utf8(v, buf)
else:
write_int(0, buf) # EvalConf (0 key-value pairs)
write_udf(buf)

# Write the actual data
# header...
write_int(SpecialLengths.START_OF_INIT_MESSAGE, target_buffer)
write_int(buf.getbuffer().nbytes, target_buffer)
# ... + previously buffered data
buf.seek(0)
target_buffer.write(buf.read())

@classmethod
def write_udf_payload(
cls,
Expand Down Expand Up @@ -165,25 +205,11 @@ def write_worker_input(
eval_conf: dict[str, str] | None = None,
) -> None:
"""Write the full worker binary stream: preamble + command + data + end."""
cls.write_preamble(buf)
write_int(eval_type, buf)
if runner_conf:
write_int(len(runner_conf), buf)
for k, v in runner_conf.items():
cls.write_utf8(k, buf)
cls.write_utf8(v, buf)
else:
write_int(0, buf) # RunnerConf (0 key-value pairs)
if eval_conf:
write_int(len(eval_conf), buf)
for k, v in eval_conf.items():
cls.write_utf8(k, buf)
cls.write_utf8(v, buf)
else:
write_int(0, buf) # EvalConf (0 key-value pairs)
write_udf(buf)
cls.write_init_message(
eval_type, write_udf, buf, runner_conf=runner_conf, eval_conf=eval_conf
)
write_data(buf)
write_int(-4, buf) # SpecialLengths.END_OF_STREAM
write_int(SpecialLengths.END_OF_STREAM, buf)

@classmethod
def write_arrow_udtf_payload(
Expand Down
2 changes: 2 additions & 0 deletions python/packaging/classic/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ def run(self):
"pyspark",
"pyspark.core",
"pyspark.cloudpickle",
"pyspark.messages",
"pyspark.messages.socket",
"pyspark.mllib",
"pyspark.mllib.linalg",
"pyspark.mllib.stat",
Expand Down
2 changes: 2 additions & 0 deletions python/packaging/client/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@
connect_packages = [
"pyspark",
"pyspark.cloudpickle",
"pyspark.messages",
"pyspark.messages.socket",
"pyspark.mllib",
"pyspark.mllib.linalg",
"pyspark.mllib.stat",
Expand Down
4 changes: 4 additions & 0 deletions python/pyspark/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
# limitations under the License.
#

from pyspark.messages.spark_message_receiver import SparkMessageReceiver
from pyspark.messages.zero_copy_byte_stream import ZeroCopyByteStream
from pyspark.messages.socket.spark_socket_message_receiver import SparkSocketMessageReceiver

__all__ = [
"SparkMessageReceiver",
"SparkSocketMessageReceiver",
"ZeroCopyByteStream",
]
16 changes: 16 additions & 0 deletions python/pyspark/messages/socket/__init__.py
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.
#
62 changes: 62 additions & 0 deletions python/pyspark/messages/socket/spark_socket_message_receiver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#
# 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} 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_get_finish_signal_from_stream(self) -> None:
# If everything finished properly, we should read END_OF_STREAM.
# Anything else means something went wrong during processing.
message_id = read_int(self._infile)
_assert_message_id(message_id, SpecialLengths.END_OF_STREAM)
122 changes: 122 additions & 0 deletions python/pyspark/messages/spark_message_receiver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#
# 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:
Comment thread
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 get_finish_signal_from_stream(self) -> None:
"""
Consumes the finish message from the JVM and transitions to the DONE state.
The finish message marks the end of the stream. Raises an `AssertionError` if
the finish signal was not received correctly.
"""
self._do_get_finish_signal_from_stream()

@abstractmethod
def _do_get_init_message(self) -> ZeroCopyByteStream:
"""
Returns the contents of the init message
as a 'ZeroCopyByteStream'.

To be implemented by child classes.
"""
...

@abstractmethod
def _do_get_data_stream(self) -> BinaryIO:
"""
Returns the Spark data stream.

To be implemented by child classes.
"""
...

@abstractmethod
def _do_get_finish_signal_from_stream(self) -> None:
"""
Consumes the finish signal from the stream.
Raises an `AssertionError` if the signal is not received correctly.

To be implemented by child classes.
"""
...
4 changes: 3 additions & 1 deletion python/pyspark/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import zlib
import itertools
import pickle
import codecs

pickle_protocol = pickle.HIGHEST_PROTOCOL

Expand All @@ -84,6 +85,7 @@ class SpecialLengths:
END_OF_STREAM = -4
NULL = -5
START_ARROW_STREAM = -6
START_OF_INIT_MESSAGE = -8


class Serializer:
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Contributor Author

@sven-weber-db sven-weber-db May 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is required because we use the ZeroCopyByteStream class now to read the initial message. ZeroCopyByteStream returns memoryview objects instead of bytes or bytearray. s.decode is only implemented for s{bytes, bytearray}.

According to the Python documentation, for bytes.decode and codecs.decode the later implementation may behave different in case of errors.

bytes.decode states that

errors controls how decoding errors are handled. If 'strict' (the default), a UnicodeError exception is raised.

codecs.decode states that

The default error handler is 'strict' meaning that decoding errors raise ValueError (or a more codec specific subclass, such as UnicodeDecodeError)

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 s

This alternative implementation will invoke a memory copy to copy the memoryview contents into a bytes object before decoding. Given that this call is only invoked on deserialization of the initial message, this memory copy is probably acceptable as it is a one time cost.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine I think.


def load_stream(self, stream):
try:
Expand Down
Loading