Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
b1175e4
[WIP] Poc.
sahnib Jan 22, 2024
0a98ed8
Introduce Protobuf.
sahnib Feb 6, 2024
8e2b193
Fixing things.
sahnib Feb 6, 2024
16e4c17
support timeMode for python state v2 API
bogao007 Jun 20, 2024
92ef716
Add protobuf for serde
sahnib Feb 13, 2024
c3eaf38
protobuf change
bogao007 Jun 20, 2024
609d94e
Initial commit
bogao007 Jun 27, 2024
a27f9d9
better error handling, support value state with different types
bogao007 Jun 28, 2024
684939b
addressed comments
bogao007 Jul 3, 2024
7f65fbd
fix
bogao007 Jul 3, 2024
c25d7da
Added support for unix domain socket
bogao007 Jul 11, 2024
9c8c616
removed unrelated log lines, addressed part of the comments
bogao007 Jul 17, 2024
c641192
fix
bogao007 Jul 17, 2024
8d3da4e
Addressed comments
bogao007 Jul 19, 2024
cc9bf95
removed unnecessary print
bogao007 Jul 19, 2024
f7df2dc
rename
bogao007 Jul 19, 2024
27cd169
fix
bogao007 Jul 19, 2024
3b5b3e5
removed duplicate proto file
bogao007 Jul 20, 2024
5d910d8
revert unrelated changes
bogao007 Jul 20, 2024
df859ab
fix
bogao007 Jul 20, 2024
654f2f6
Added unit tests for transformWithStateInPandas
bogao007 Jul 24, 2024
38832a6
Merge branch 'master' into state-v2-initial
bogao007 Jul 24, 2024
0585ac0
fix and rename
bogao007 Jul 24, 2024
0ee5029
update test
bogao007 Jul 24, 2024
6232c81
Added lisences
bogao007 Jul 25, 2024
41f8234
fixed format issues
bogao007 Jul 25, 2024
d57633f
fix
bogao007 Jul 25, 2024
df9ea9e
fix format
bogao007 Jul 25, 2024
68f7a7e
doc
bogao007 Jul 25, 2024
ca5216b
addressed comments
bogao007 Jul 26, 2024
c9e3a7c
structured log
bogao007 Jul 26, 2024
2320805
suppress auto generated proto file
bogao007 Jul 29, 2024
6e5de2e
fix linter
bogao007 Jul 29, 2024
200ec5e
fixed dependency issue
bogao007 Jul 29, 2024
dd3e46b
make protobuf as local dependency
bogao007 Jul 30, 2024
e8360d4
fix dependency issue
bogao007 Jul 30, 2024
82983af
fix
bogao007 Jul 30, 2024
49dbc16
fix lint
bogao007 Jul 30, 2024
d4e04ea
fix
bogao007 Jul 30, 2024
e108f60
updated fix
bogao007 Jul 30, 2024
bae26c2
reformat
bogao007 Jul 30, 2024
d96fa9e
addressed comments
bogao007 Jul 31, 2024
92531db
fix linter
bogao007 Jul 31, 2024
d507793
linter
bogao007 Jul 31, 2024
5dcb4c8
addressed comments
bogao007 Aug 2, 2024
37be02a
address comment
bogao007 Aug 2, 2024
f63687f
addressed comments
bogao007 Aug 9, 2024
263c087
Merge branch 'master' into state-v2-initial
bogao007 Aug 10, 2024
c7b0a4f
address comments
bogao007 Aug 12, 2024
c80b292
address comments
bogao007 Aug 12, 2024
81276f3
address comments
bogao007 Aug 14, 2024
5886b5c
fix lint
bogao007 Aug 14, 2024
23e54b4
fix lint
bogao007 Aug 14, 2024
2ba4fd0
address comments
bogao007 Aug 14, 2024
2a9c20b
fix test
bogao007 Aug 14, 2024
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
Prev Previous commit
Next Next commit
updated fix
  • Loading branch information
bogao007 committed Jul 30, 2024
commit e108f606b97466e63ca4414e8a5d5667005be6e1
70 changes: 26 additions & 44 deletions python/pyspark/sql/streaming/stateful_processor_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class StatefulProcessorHandleState(Enum):


class StatefulProcessorApiClient:
import pyspark.sql.streaming.StateMessage_pb2 as stateMessage

def __init__(self, state_server_port: int, key_schema: StructType) -> None:
self.key_schema = key_schema
self._client_socket = socket.socket()
Expand All @@ -52,21 +50,26 @@ def __init__(self, state_server_port: int, key_schema: StructType) -> None:
def set_handle_state(self, state: StatefulProcessorHandleState) -> None:
import pyspark.sql.streaming.StateMessage_pb2 as stateMessage

proto_state = self._get_proto_state(state)
if state == StatefulProcessorHandleState.CREATED:
proto_state = stateMessage.CREATED
elif state == StatefulProcessorHandleState.INITIALIZED:
proto_state = stateMessage.INITIALIZED
elif state == StatefulProcessorHandleState.DATA_PROCESSED:
proto_state = stateMessage.DATA_PROCESSED
else:
proto_state = stateMessage.CLOSED
set_handle_state = stateMessage.SetHandleState(state=proto_state)
handle_call = stateMessage.StatefulProcessorCall(setHandleState=set_handle_state)
message = stateMessage.StateRequest(statefulProcessorCall=handle_call)

self._send_proto_message(message)
self._send_proto_message(message.SerializeToString())

response_message = self._receive_proto_message()
status = response_message.statusCode
status = response_message[0]
if status == 0:
self.handle_state = state
else:
raise PySparkRuntimeError(
f"Error setting handle state: " f"{response_message.errorMessage}"
)
raise PySparkRuntimeError(f"Error setting handle state: " f"{response_message[1]}")
Copy link
Contributor

Choose a reason for hiding this comment

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

I see we just match all errors here to PySparkRuntimeError with error message (no classification) - shall we revisit the Scala codebase and ensure we give the same error class for the same error?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also there are internal requests vs user side requests. For example, I don't expect users to call set_implicit_key by themselves (so errors from them are internal errors), but expect users to call get_value_state (so error could be either user facing and internal). The classification of error class has to be different for these cases.


def set_implicit_key(self, key: Tuple) -> None:
import pyspark.sql.streaming.StateMessage_pb2 as stateMessage
Expand All @@ -76,13 +79,11 @@ def set_implicit_key(self, key: Tuple) -> None:
request = stateMessage.ImplicitGroupingKeyRequest(setImplicitKey=set_implicit_key)
message = stateMessage.StateRequest(implicitGroupingKeyRequest=request)

self._send_proto_message(message)
self._send_proto_message(message.SerializeToString())
response_message = self._receive_proto_message()
status = response_message.statusCode
status = response_message[0]
if status != 0:
raise PySparkRuntimeError(
f"Error setting implicit key: " f"{response_message.errorMessage}"
)
raise PySparkRuntimeError(f"Error setting implicit key: " f"{response_message[1]}")

def remove_implicit_key(self) -> None:
import pyspark.sql.streaming.StateMessage_pb2 as stateMessage
Expand All @@ -92,13 +93,11 @@ def remove_implicit_key(self) -> None:
request = stateMessage.ImplicitGroupingKeyRequest(removeImplicitKey=remove_implicit_key)
message = stateMessage.StateRequest(implicitGroupingKeyRequest=request)

self._send_proto_message(message)
self._send_proto_message(message.SerializeToString())
response_message = self._receive_proto_message()
status = response_message.statusCode
status = response_message[0]
if status != 0:
raise PySparkRuntimeError(
f"Error removing implicit key: " f"{response_message.errorMessage}"
)
raise PySparkRuntimeError(f"Error removing implicit key: " f"{response_message[1]}")

def get_value_state(self, state_name: str, schema: Union[StructType, str]) -> None:
import pyspark.sql.streaming.StateMessage_pb2 as stateMessage
Expand All @@ -112,47 +111,30 @@ def get_value_state(self, state_name: str, schema: Union[StructType, str]) -> No
call = stateMessage.StatefulProcessorCall(getValueState=state_call_command)
message = stateMessage.StateRequest(statefulProcessorCall=call)

self._send_proto_message(message)
self._send_proto_message(message.SerializeToString())
response_message = self._receive_proto_message()
status = response_message.statusCode
status = response_message[0]
if status != 0:
raise PySparkRuntimeError(
f"Error initializing value state: " f"{response_message.errorMessage}"
)

def _get_proto_state(
self, state: StatefulProcessorHandleState
) -> stateMessage.HandleState.ValueType:
import pyspark.sql.streaming.StateMessage_pb2 as stateMessage

if state == StatefulProcessorHandleState.CREATED:
return stateMessage.CREATED
elif state == StatefulProcessorHandleState.INITIALIZED:
return stateMessage.INITIALIZED
elif state == StatefulProcessorHandleState.DATA_PROCESSED:
return stateMessage.DATA_PROCESSED
else:
return stateMessage.CLOSED
raise PySparkRuntimeError(f"Error initializing value state: " f"{response_message[1]}")

def _send_proto_message(self, message: stateMessage.StateRequest) -> None:
serialized_msg = message.SerializeToString()
def _send_proto_message(self, message: str) -> None:
# Writing zero here to indicate message version. This allows us to evolve the message
# format or even changing the message protocol in the future.
write_int(0, self.sockfile)
write_int(len(serialized_msg), self.sockfile)
self.sockfile.write(serialized_msg)
write_int(len(message), self.sockfile)
self.sockfile.write(message)
self.sockfile.flush()

def _receive_proto_message(self) -> stateMessage.StateResponse:
def _receive_proto_message(self) -> Tuple[int, str]:
import pyspark.sql.streaming.StateMessage_pb2 as stateMessage

serialized_msg = self._receive_str()
# proto3 will not serialize the message if the value is default, in this case 0
if len(serialized_msg) == 0:
return stateMessage.StateResponse(statusCode=0)
return 0, ""
message = stateMessage.StateResponse()
message.ParseFromString(serialized_msg.encode("utf-8"))
return message
return message.statusCode, message.errorMessage

def _receive_str(self) -> str:
return self.utf8_deserializer.loads(self.sockfile)
Expand Down
28 changes: 12 additions & 16 deletions python/pyspark/sql/streaming/value_state_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ def exists(self, state_name: str) -> bool:
state_variable_request = stateMessage.StateVariableRequest(valueStateCall=value_state_call)
message = stateMessage.StateRequest(stateVariableRequest=state_variable_request)

self._stateful_processor_api_client._send_proto_message(message)
self._stateful_processor_api_client._send_proto_message(message.SerializeToString())
response_message = self._stateful_processor_api_client._receive_proto_message()
status = response_message.statusCode
status = response_message[0]
if status == 0:
return True
elif status == 1:
Copy link
Contributor

Choose a reason for hiding this comment

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

As I commented, please distinguish two different cases.

# server returns 1 if the state does not exist
return False
else:
raise PySparkRuntimeError(
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto for this class

f"Error checking value state exists: " f"{response_message.errorMessage}"
f"Error checking value state exists: " f"{response_message[1]}"
)

def get(self, state_name: str) -> Any:
Expand All @@ -56,13 +56,13 @@ def get(self, state_name: str) -> Any:
state_variable_request = stateMessage.StateVariableRequest(valueStateCall=value_state_call)
message = stateMessage.StateRequest(stateVariableRequest=state_variable_request)

self._stateful_processor_api_client._send_proto_message(message)
self._stateful_processor_api_client._send_proto_message(message.SerializeToString())
response_message = self._stateful_processor_api_client._receive_proto_message()
status = response_message.statusCode
status = response_message[0]
if status == 0:
return self._stateful_processor_api_client._receive_and_deserialize()
else:
raise PySparkRuntimeError(f"Error getting value state: {response_message.errorMessage}")
raise PySparkRuntimeError(f"Error getting value state: {response_message[1]}")

def update(self, state_name: str, schema: Union[StructType, str], value: Tuple) -> None:
import pyspark.sql.streaming.StateMessage_pb2 as stateMessage
Expand All @@ -77,13 +77,11 @@ def update(self, state_name: str, schema: Union[StructType, str], value: Tuple)
state_variable_request = stateMessage.StateVariableRequest(valueStateCall=value_state_call)
message = stateMessage.StateRequest(stateVariableRequest=state_variable_request)

self._stateful_processor_api_client._send_proto_message(message)
self._stateful_processor_api_client._send_proto_message(message.SerializeToString())
response_message = self._stateful_processor_api_client._receive_proto_message()
status = response_message.statusCode
status = response_message[0]
if status != 0:
raise PySparkRuntimeError(
f"Error updating value state: " f"{response_message.errorMessage}"
)
raise PySparkRuntimeError(f"Error updating value state: " f"{response_message[1]}")

def clear(self, state_name: str) -> None:
import pyspark.sql.streaming.StateMessage_pb2 as stateMessage
Expand All @@ -93,10 +91,8 @@ def clear(self, state_name: str) -> None:
state_variable_request = stateMessage.StateVariableRequest(valueStateCall=value_state_call)
message = stateMessage.StateRequest(stateVariableRequest=state_variable_request)

self._stateful_processor_api_client._send_proto_message(message)
self._stateful_processor_api_client._send_proto_message(message.SerializeToString())
response_message = self._stateful_processor_api_client._receive_proto_message()
status = response_message.statusCode
status = response_message[0]
if status != 0:
raise PySparkRuntimeError(
f"Error clearing value state: " f"{response_message.errorMessage}"
)
raise PySparkRuntimeError(f"Error clearing value state: " f"{response_message[1]}")