Skip to content
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

[SPARK-49744][SS][PYTHON] Implement TTL support for ListState in TransformWithStateInPandas #48253

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
13 changes: 10 additions & 3 deletions python/pyspark/sql/streaming/stateful_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get(self) -> Optional[Tuple]:
"""
return self._value_state_client.get(self._state_name)

def update(self, new_value: Any) -> None:
def update(self, new_value: Tuple) -> None:
"""
Update the value of the state.
"""
Expand Down Expand Up @@ -156,7 +156,9 @@ def getValueState(
self.stateful_processor_api_client.get_value_state(state_name, schema, ttl_duration_ms)
return ValueState(ValueStateClient(self.stateful_processor_api_client), state_name, schema)

def getListState(self, state_name: str, schema: Union[StructType, str]) -> ListState:
def getListState(
self, state_name: str, schema: Union[StructType, str], ttl_duration_ms: Optional[int] = None
) -> ListState:
"""
Function to create new or return existing single value state variable of given type.
The user must ensure to call this function only within the `init()` method of the
Expand All @@ -169,8 +171,13 @@ def getListState(self, state_name: str, schema: Union[StructType, str]) -> ListS
schema : :class:`pyspark.sql.types.DataType` or str
The schema of the state variable. The value can be either a
:class:`pyspark.sql.types.DataType` object or a DDL-formatted type string.
ttlDurationMs: int
Time to live duration of the state in milliseconds. State values will not be returned
past ttlDuration and will be eventually removed from the state store. Any state update
resets the expiration time to current processing time plus ttlDuration.
If ttl is not specified the state will never expire.
"""
self.stateful_processor_api_client.get_list_state(state_name, schema)
self.stateful_processor_api_client.get_list_state(state_name, schema, ttl_duration_ms)
return ListState(ListStateClient(self.stateful_processor_api_client), state_name, schema)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ def get_value_state(
# TODO(SPARK-49233): Classify user facing errors.
raise PySparkRuntimeError(f"Error initializing value state: " f"{response_message[1]}")

def get_list_state(self, state_name: str, schema: Union[StructType, str]) -> None:
def get_list_state(
self, state_name: str, schema: Union[StructType, str], ttl_duration_ms: Optional[int]
) -> None:
import pyspark.sql.streaming.StateMessage_pb2 as stateMessage

if isinstance(schema, str):
Expand All @@ -140,6 +142,8 @@ def get_list_state(self, state_name: str, schema: Union[StructType, str]) -> Non
state_call_command = stateMessage.StateCallCommand()
state_call_command.stateName = state_name
state_call_command.schema = schema.json()
if ttl_duration_ms is not None:
state_call_command.ttl.durationMs = ttl_duration_ms
call = stateMessage.StatefulProcessorCall(getListState=state_call_command)
message = stateMessage.StateRequest(statefulProcessorCall=call)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,18 @@ def check_results(batch_df, _):

self._test_transform_with_state_in_pandas_basic(ListStateProcessor(), check_results, True)

# test list state with ttl has the same behavior as list state when state doesn't expire.
def test_transform_with_state_in_pandas_list_state_large_ttl(self):
def check_results(batch_df, _):
assert set(batch_df.sort("id").collect()) == {
Row(id="0", countAsString="2"),
Row(id="1", countAsString="2"),
}

self._test_transform_with_state_in_pandas_basic(
ListStateLargeTTLProcessor(), check_results, True, "processingTime"
)

# test value state with ttl has the same behavior as value state when
# state doesn't expire.
def test_value_state_ttl_basic(self):
Expand Down Expand Up @@ -248,8 +260,10 @@ def check_results(batch_df, batch_id):
[
Row(id="ttl-count-0", count=1),
Row(id="count-0", count=1),
Row(id="ttl-list-state-count-0", count=1),
Row(id="ttl-count-1", count=1),
Row(id="count-1", count=1),
Row(id="ttl-list-state-count-1", count=1),
],
)
elif batch_id == 1:
Expand All @@ -258,21 +272,27 @@ def check_results(batch_df, batch_id):
[
Row(id="ttl-count-0", count=2),
Row(id="count-0", count=2),
Row(id="ttl-list-state-count-0", count=3),
Row(id="ttl-count-1", count=2),
Row(id="count-1", count=2),
Row(id="ttl-list-state-count-1", count=3),
],
)
elif batch_id == 2:
# ttl-count-0 expire and restart from count 0.
# ttl-count-1 get reset in batch 1 and keep the state
# ttl-count-1 get reset in batch 1 and keep the state.
Copy link
Contributor

@HeartSaVioR HeartSaVioR Oct 4, 2024

Choose a reason for hiding this comment

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

nit: it may not be very clear what "reset" means here - now I get what you mean, "TTL reset", but I was puzzled and had to think a bit. Same for ttl-list-state-count-1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the comment with more details, thanks!

# ttl-list-state-count-0 expire and restart from count 0.
# ttl-list-state-count-1 get reset in batch 1 and keep the state.
# non-ttl state never expires
assertDataFrameEqual(
batch_df,
[
Row(id="ttl-count-0", count=1),
Row(id="count-0", count=3),
Row(id="ttl-list-state-count-0", count=1),
Row(id="ttl-count-1", count=3),
Row(id="count-1", count=3),
Row(id="ttl-list-state-count-1", count=7),
],
)
if batch_id == 0 or batch_id == 1:
Expand Down Expand Up @@ -362,25 +382,38 @@ def init(self, handle: StatefulProcessorHandle) -> None:
state_schema = StructType([StructField("value", IntegerType(), True)])
self.ttl_count_state = handle.getValueState("ttl-state", state_schema, 10000)
self.count_state = handle.getValueState("state", state_schema)
self.ttl_list_state = handle.getListState("ttl-list-state", state_schema, 10000)

def handleInputRows(self, key, rows) -> Iterator[pd.DataFrame]:
count = 0
ttl_count = 0
ttl_list_state_count = 0
id = key[0]
if self.count_state.exists():
count = self.count_state.get()[0]
if self.ttl_count_state.exists():
ttl_count = self.ttl_count_state.get()[0]
if self.ttl_list_state.exists():
iter = self.ttl_list_state.get()
for s in iter:
ttl_list_state_count += s[0]
for pdf in rows:
pdf_count = pdf.count().get("temperature")
count += pdf_count
ttl_count += pdf_count
ttl_list_state_count += pdf_count

self.count_state.update((count,))
# skip updating state for the 2nd batch so that ttl state expire
if not (ttl_count == 2 and id == "0"):
self.ttl_count_state.update((ttl_count,))
yield pd.DataFrame({"id": [f"ttl-count-{id}", f"count-{id}"], "count": [ttl_count, count]})
self.ttl_list_state.put([(ttl_list_state_count,), (ttl_list_state_count,)])
yield pd.DataFrame(
{
"id": [f"ttl-count-{id}", f"count-{id}", f"ttl-list-state-count-{id}"],
"count": [ttl_count, count, ttl_list_state_count],
}
)

def close(self) -> None:
pass
Expand Down Expand Up @@ -457,6 +490,15 @@ def close(self) -> None:
pass


# A stateful processor that inherit all behavior of ListStateProcessor except that it use
# ttl state with a large timeout.
class ListStateLargeTTLProcessor(ListStateProcessor):
def init(self, handle: StatefulProcessorHandle) -> None:
state_schema = StructType([StructField("temperature", IntegerType(), True)])
self.list_state1 = handle.getListState("listState1", state_schema, 30000)
self.list_state2 = handle.getListState("listState2", state_schema, 30000)


class TransformWithStateInPandasTests(TransformWithStateInPandasTestsMixin, ReusedSQLTestCase):
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,12 @@ class TransformWithStateInPandasStateServer(
case StatefulProcessorCall.MethodCase.GETLISTSTATE =>
val stateName = message.getGetListState.getStateName
val schema = message.getGetListState.getSchema
// TODO(SPARK-49744): Add ttl support for list state.
initializeStateVariable(stateName, schema, StateVariableType.ListState, None)
val ttlDurationMs = if (message.getGetListState.hasTtl) {
Some(message.getGetListState.getTtl.getDurationMs)
} else {
None
}
initializeStateVariable(stateName, schema, StateVariableType.ListState, ttlDurationMs)
case _ =>
throw new IllegalArgumentException("Invalid method call")
}
Expand Down Expand Up @@ -373,9 +377,14 @@ class TransformWithStateInPandasStateServer(
}
case StateVariableType.ListState => if (!listStates.contains(stateName)) {
// TODO(SPARK-49744): Add ttl support for list state.
val state = if (ttlDurationMs.isEmpty) {
statefulProcessorHandle.getListState[Row](stateName, Encoders.row(schema))
} else {
statefulProcessorHandle.getListState(
stateName, Encoders.row(schema), TTLConfig(Duration.ofMillis(ttlDurationMs.get)))
}
listStates.put(stateName,
ListStateInfo(statefulProcessorHandle.getListState[Row](stateName,
Encoders.row(schema)), schema, expressionEncoder.createDeserializer(),
ListStateInfo(state, schema, expressionEncoder.createDeserializer(),
expressionEncoder.createSerializer()))
sendResponse(0)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,29 @@ class TransformWithStateInPandasStateServerSuite extends SparkFunSuite with Befo
}
}

Seq(true, false).foreach { useTTL =>
test(s"get list state, useTTL=$useTTL") {
val stateCallCommandBuilder = StateCallCommand.newBuilder()
.setStateName("newName")
.setSchema("StructType(List(StructField(value,IntegerType,true)))")
if (useTTL) {
stateCallCommandBuilder.setTtl(StateMessage.TTLConfig.newBuilder().setDurationMs(1000))
}
val message = StatefulProcessorCall
.newBuilder()
.setGetListState(stateCallCommandBuilder.build())
.build()
stateServer.handleStatefulProcessorCall(message)
if (useTTL) {
verify(statefulProcessorHandle)
.getListState[Row](any[String], any[Encoder[Row]], any[TTLConfig])
} else {
verify(statefulProcessorHandle).getListState[Row](any[String], any[Encoder[Row]])
}
verify(outputStream).writeInt(0)
}
}

test("value state exists") {
val message = ValueStateCall.newBuilder().setStateName(stateName)
.setExists(Exists.newBuilder().build()).build()
Expand Down