Skip to content

Commit 79f1b02

Browse files
author
Rares Polenciuc
committed
feat: execution state pagination and token validation
- Add paging logic in checkpoint processor with next_marker support - Implement checkpoint token validation - Add token expiration checking with error responses - Handle missing token cases with context-appropriate validation - Add pagination metadata to responses with configurable max_items - Add test coverage for all validation scenarios
1 parent fea8882 commit 79f1b02

File tree

8 files changed

+252
-61
lines changed

8 files changed

+252
-61
lines changed

src/aws_durable_execution_sdk_python_testing/checkpoint/processor.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
CheckpointUpdatedExecutionState,
1010
OperationUpdate,
1111
StateOutput,
12+
Operation,
1213
)
1314

1415
from aws_durable_execution_sdk_python_testing.checkpoint.transformer import (
@@ -88,14 +89,37 @@ def process_checkpoint(
8889
def get_execution_state(
8990
self,
9091
checkpoint_token: str,
91-
next_marker: str, # noqa: ARG002
92-
max_items: int = 1000, # noqa: ARG002
92+
next_marker: str | None = None,
93+
max_items: int = 1000,
9394
) -> StateOutput:
94-
"""Get current execution state."""
95+
"""Get current execution state with batched checkpoint token validation and pagination."""
96+
if not checkpoint_token:
97+
msg: str = "Checkpoint token is required"
98+
raise InvalidParameterValueException(msg)
99+
95100
token: CheckpointToken = CheckpointToken.from_str(checkpoint_token)
96101
execution: Execution = self._store.load(token.execution_arn)
102+
execution.validate_checkpoint_token(checkpoint_token)
103+
104+
# Get all operations
105+
all_operations: list[Operation] = execution.get_navigable_operations()
106+
107+
# Apply pagination
108+
start_index: int = 0
109+
if next_marker:
110+
try:
111+
start_index = int(next_marker)
112+
except ValueError:
113+
start_index = 0
114+
115+
end_index: int = start_index + max_items
116+
paginated_operations: list[Operation] = all_operations[start_index:end_index]
117+
118+
# Determine next marker
119+
next_marker_result: str | None = (
120+
str(end_index) if end_index < len(all_operations) else None
121+
)
97122

98-
# TODO: paging when size or max
99123
return StateOutput(
100-
operations=execution.get_navigable_operations(), next_marker=None
124+
operations=paginated_operations, next_marker=next_marker_result
101125
)

src/aws_durable_execution_sdk_python_testing/checkpoint/processors/execution.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,8 @@ def process(
3838
)
3939
case _:
4040
# intentional. actual service will fail any EXECUTION update that is not SUCCEED.
41-
error = (
42-
update.error
43-
if update.error
44-
else ErrorObject.from_message(
45-
"There is no error details but EXECUTION checkpoint action is not SUCCEED."
46-
)
41+
error = update.error or ErrorObject.from_message(
42+
"There is no error details but EXECUTION checkpoint action is not SUCCEED."
4743
)
4844
# All EXECUTION failures go through normal fail path
4945
# Timeout/Stop status is set by executor based on the operation that caused it

src/aws_durable_execution_sdk_python_testing/checkpoint/transformer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __init__(
5555
self,
5656
processors: MutableMapping[OperationType, OperationProcessor] | None = None,
5757
):
58-
self.processors = processors if processors else self._DEFAULT_PROCESSORS
58+
self.processors = processors or self._DEFAULT_PROCESSORS
5959

6060
def process_updates(
6161
self,

src/aws_durable_execution_sdk_python_testing/execution.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __init__(
6060
self.start_input: StartDurableExecutionInput = start_input
6161
self.operations: list[Operation] = operations
6262
self.updates: list[OperationUpdate] = []
63-
self.used_tokens: set[str] = set()
63+
self.generated_tokens: set[str] = set()
6464
# TODO: this will need to persist/rehydrate depending on inmemory vs sqllite store
6565
self._token_sequence: int = 0
6666
self._state_lock: Lock = Lock()
@@ -101,7 +101,7 @@ def to_dict(self) -> dict[str, Any]:
101101
"StartInput": self.start_input.to_dict(),
102102
"Operations": [op.to_dict() for op in self.operations],
103103
"Updates": [update.to_dict() for update in self.updates],
104-
"UsedTokens": list(self.used_tokens),
104+
"GeneratedTokens": list(self.generated_tokens),
105105
"TokenSequence": self._token_sequence,
106106
"IsComplete": self.is_complete,
107107
"Result": self.result.to_dict() if self.result else None,
@@ -129,7 +129,7 @@ def from_dict(cls, data: dict[str, Any]) -> Execution:
129129
execution.updates = [
130130
OperationUpdate.from_dict(update_data) for update_data in data["Updates"]
131131
]
132-
execution.used_tokens = set(data["UsedTokens"])
132+
execution.generated_tokens = set(data["GeneratedTokens"])
133133
execution._token_sequence = data["TokenSequence"] # noqa: SLF001
134134
execution.is_complete = data["IsComplete"]
135135
execution.result = (
@@ -184,13 +184,38 @@ def get_new_checkpoint_token(self) -> str:
184184
token_sequence=new_token_sequence,
185185
)
186186
token_str = token.to_str()
187-
self.used_tokens.add(token_str)
187+
self.generated_tokens.add(token_str)
188188
return token_str
189189

190190
def get_navigable_operations(self) -> list[Operation]:
191191
"""Get list of operations, but exclude child operations where the parent has already completed."""
192192
return self.operations
193193

194+
def validate_checkpoint_token(
195+
self,
196+
token: str | None,
197+
checkpoint_required_msg: str | None = None,
198+
) -> None:
199+
"""Validate checkpoint token against this execution."""
200+
if not token:
201+
msg: str = checkpoint_required_msg or "Checkpoint token is required"
202+
raise InvalidParameterValueException(msg)
203+
204+
checkpoint_token: CheckpointToken = CheckpointToken.from_str(token)
205+
if checkpoint_token.execution_arn != self.durable_execution_arn:
206+
msg = "Checkpoint token does not match execution ARN"
207+
raise InvalidParameterValueException(msg)
208+
209+
if self.is_complete or checkpoint_token.token_sequence > self.token_sequence:
210+
msg = "Invalid or expired checkpoint token"
211+
raise InvalidParameterValueException(msg)
212+
213+
# Check if token has been generated
214+
token_str: str = checkpoint_token.to_str()
215+
if token_str not in self.generated_tokens:
216+
msg = f"Invalid checkpoint token: {token_str}"
217+
raise InvalidParameterValueException(msg)
218+
194219
def get_assertable_operations(self) -> list[Operation]:
195220
"""Get list of operations, but exclude the EXECUTION operations"""
196221
# TODO: this excludes EXECUTION at start, but can there be an EXECUTION at the end if there was a checkpoint with large payload?

src/aws_durable_execution_sdk_python_testing/executor.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
if TYPE_CHECKING:
5959
from collections.abc import Awaitable, Callable
6060

61+
from aws_durable_execution_sdk_python.lambda_service import Operation
62+
6163
from aws_durable_execution_sdk_python_testing.checkpoint.processor import (
6264
CheckpointProcessor,
6365
)
@@ -347,32 +349,33 @@ def get_execution_state(
347349
ResourceNotFoundException: If execution does not exist
348350
InvalidParameterValueException: If checkpoint token is invalid
349351
"""
350-
execution = self.get_execution(execution_arn)
352+
execution: Execution = self.get_execution(execution_arn)
353+
is_checkpoint_required: bool = not execution.is_complete and marker is not None
351354

352-
# TODO: Validate checkpoint token if provided
353-
if checkpoint_token and checkpoint_token not in execution.used_tokens:
354-
msg: str = f"Invalid checkpoint token: {checkpoint_token}"
355-
raise InvalidParameterValueException(msg)
355+
if is_checkpoint_required or checkpoint_token:
356+
checkpoint_required_msg: str = "Checkpoint token is required for paginated requests on active executions"
357+
execution.validate_checkpoint_token(
358+
checkpoint_token, checkpoint_required_msg
359+
)
356360

357361
# Get operations (excluding the initial EXECUTION operation for state)
358-
operations = execution.get_assertable_operations()
362+
operations: list[Operation] = execution.get_assertable_operations()
359363

360364
# Apply pagination
361365
if max_items is None:
362366
max_items = 100
363367

364-
# Simple pagination - in real implementation would need proper marker handling
365-
start_index = 0
368+
start_index: int = 0
366369
if marker:
367370
try:
368371
start_index = int(marker)
369372
except ValueError:
370373
start_index = 0
371374

372-
end_index = start_index + max_items
373-
paginated_operations = operations[start_index:end_index]
375+
end_index: int = start_index + max_items
376+
paginated_operations: list[Operation] = operations[start_index:end_index]
374377

375-
next_marker = None
378+
next_marker: str | None = None
376379
if end_index < len(operations):
377380
next_marker = str(end_index)
378381

@@ -541,11 +544,10 @@ def checkpoint_execution(
541544
InvalidParameterValueException: If checkpoint token is invalid
542545
"""
543546
execution = self.get_execution(execution_arn)
544-
545-
# Validate checkpoint token
546-
if checkpoint_token not in execution.used_tokens:
547-
msg: str = f"Invalid checkpoint token: {checkpoint_token}"
548-
raise InvalidParameterValueException(msg)
547+
execution.validate_checkpoint_token(
548+
checkpoint_token,
549+
checkpoint_required_msg="Checkpoint token is required for checkpoint operations",
550+
)
549551

550552
if updates:
551553
checkpoint_output = self._checkpoint_processor.process_checkpoint(

tests/execution_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_execution_init():
4343
assert execution.start_input == start_input
4444
assert execution.operations == operations
4545
assert execution.updates == []
46-
assert execution.used_tokens == set()
46+
assert execution.generated_tokens == set()
4747
assert execution.token_sequence == 0
4848
assert execution.is_complete is False
4949
assert execution.consecutive_failed_invocation_attempts == 0
@@ -154,8 +154,8 @@ def test_get_new_checkpoint_token():
154154
token2 = execution.get_new_checkpoint_token()
155155

156156
assert execution.token_sequence == 2
157-
assert token1 in execution.used_tokens
158-
assert token2 in execution.used_tokens
157+
assert token1 in execution.generated_tokens
158+
assert token2 in execution.generated_tokens
159159
assert token1 != token2
160160

161161

@@ -801,7 +801,7 @@ def test_from_dict_with_none_result():
801801
"StartInput": {"function_name": "test"},
802802
"Operations": [],
803803
"Updates": [],
804-
"UsedTokens": [],
804+
"GeneratedTokens": [],
805805
"TokenSequence": 0,
806806
"IsComplete": False,
807807
"Result": None, # None result

0 commit comments

Comments
 (0)