Skip to content

Commit ba2ec12

Browse files
author
Rares Polenciuc
committed
feat: complete sqlite store and function handler implementation
- Add SQLiteExecutionStore with database persistence and indexing - Implement query system with pagination support - Add BaseExecutionStore with shared query processing logic - Update Executor to use new query system for efficient operations - Complete ListDurableExecutionsByFunctionHandler with proper filtering - Add function name validation and error handling - Add comprehensive test coverage for all implementations - Support concurrent access patterns with proper database handling
1 parent 27084b1 commit ba2ec12

File tree

18 files changed

+2563
-302
lines changed

18 files changed

+2563
-302
lines changed

src/aws_durable_execution_sdk_python_testing/checkpoint/processors/execution.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def process(
4545
"There is no error details but EXECUTION checkpoint action is not SUCCEED."
4646
)
4747
)
48+
# All EXECUTION failures go through normal fail path
49+
# Timeout/Stop status is set by executor based on the operation that caused it
4850
notifier.notify_failed(execution_arn=execution_arn, error=error)
4951
# TODO: Svc doesn't actually create checkpoint for EXECUTION. might have to for localrunner though.
5052
return None

src/aws_durable_execution_sdk_python_testing/execution.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
from dataclasses import replace
55
from datetime import UTC, datetime
6+
from enum import Enum
67
from threading import Lock
78
from typing import Any
89
from uuid import uuid4
@@ -20,17 +21,37 @@
2021
OperationUpdate,
2122
)
2223

23-
# Import AWS exceptions
2424
from aws_durable_execution_sdk_python_testing.exceptions import (
2525
IllegalStateException,
2626
InvalidParameterValueException,
2727
)
28+
29+
# Import AWS exceptions
2830
from aws_durable_execution_sdk_python_testing.model import (
2931
StartDurableExecutionInput,
3032
)
3133
from aws_durable_execution_sdk_python_testing.token import CheckpointToken
3234

3335

36+
class CloseStatus(Enum):
37+
"""Close status for completed executions (mimics backend SWF CloseStatus)."""
38+
39+
COMPLETED = "COMPLETED"
40+
FAILED = "FAILED"
41+
TERMINATED = "TERMINATED"
42+
TIMED_OUT = "TIMED_OUT"
43+
44+
45+
class ExecutionStatus(Enum):
46+
"""Execution status for API responses (mimics backend ExecutionStatus)."""
47+
48+
RUNNING = "RUNNING"
49+
SUCCEEDED = "SUCCEEDED"
50+
FAILED = "FAILED"
51+
STOPPED = "STOPPED"
52+
TIMED_OUT = "TIMED_OUT"
53+
54+
3455
class Execution:
3556
"""Execution state."""
3657

@@ -52,12 +73,39 @@ def __init__(
5273
self.is_complete: bool = False
5374
self.result: DurableExecutionInvocationOutput | None = None
5475
self.consecutive_failed_invocation_attempts: int = 0
76+
self.close_status: CloseStatus | None = (
77+
None # Track close status like backend SWF
78+
)
5579

5680
@property
5781
def token_sequence(self) -> int:
5882
"""Get current token sequence value."""
5983
return self._token_sequence
6084

85+
@property
86+
def status(self) -> str:
87+
"""Get execution status string (mimics backend ExecutionStatusConverter)."""
88+
if not self.is_complete:
89+
return ExecutionStatus.RUNNING.value
90+
91+
if not self.close_status:
92+
msg: str = "close_status cannot be None"
93+
raise IllegalStateException(msg)
94+
95+
# Convert CloseStatus to ExecutionStatus (like backend ExecutionStatusConverter)
96+
match self.close_status:
97+
case CloseStatus.COMPLETED:
98+
return ExecutionStatus.SUCCEEDED.value
99+
case CloseStatus.FAILED:
100+
return ExecutionStatus.FAILED.value
101+
case CloseStatus.TERMINATED:
102+
return ExecutionStatus.STOPPED.value
103+
case CloseStatus.TIMED_OUT:
104+
return ExecutionStatus.TIMED_OUT.value
105+
case _:
106+
error_msg: str = f"Unexpected close status: {self.close_status}"
107+
raise InvalidParameterValueException(error_msg)
108+
61109
@staticmethod
62110
def new(input: StartDurableExecutionInput) -> Execution: # noqa: A002
63111
# make a nicer arn
@@ -79,6 +127,7 @@ def to_dict(self) -> dict[str, Any]:
79127
"IsComplete": self.is_complete,
80128
"Result": self.result.to_dict() if self.result else None,
81129
"ConsecutiveFailedInvocationAttempts": self.consecutive_failed_invocation_attempts,
130+
"CloseStatus": self.close_status.value if self.close_status else None,
82131
}
83132

84133
@classmethod
@@ -112,6 +161,10 @@ def from_dict(cls, data: dict[str, Any]) -> Execution:
112161
execution.consecutive_failed_invocation_attempts = data[
113162
"ConsecutiveFailedInvocationAttempts"
114163
]
164+
close_status_str = data.get("CloseStatus")
165+
execution.close_status = (
166+
CloseStatus(close_status_str) if close_status_str else None
167+
)
115168

116169
return execution
117170

@@ -184,16 +237,36 @@ def has_pending_operations(self, execution: Execution) -> bool:
184237
return False
185238

186239
def complete_success(self, result: str | None) -> None:
240+
"""Complete execution successfully (DecisionType.COMPLETE_WORKFLOW_EXECUTION)."""
187241
self.result = DurableExecutionInvocationOutput(
188242
status=InvocationStatus.SUCCEEDED, result=result
189243
)
190244
self.is_complete = True
245+
self.close_status = CloseStatus.COMPLETED
191246

192247
def complete_fail(self, error: ErrorObject) -> None:
248+
"""Complete execution with failure (DecisionType.FAIL_WORKFLOW_EXECUTION)."""
249+
self.result = DurableExecutionInvocationOutput(
250+
status=InvocationStatus.FAILED, error=error
251+
)
252+
self.is_complete = True
253+
self.close_status = CloseStatus.FAILED
254+
255+
def complete_timeout(self, error: ErrorObject) -> None:
256+
"""Complete execution with timeout (SWF workflow timeout)."""
257+
self.result = DurableExecutionInvocationOutput(
258+
status=InvocationStatus.FAILED, error=error
259+
)
260+
self.is_complete = True
261+
self.close_status = CloseStatus.TIMED_OUT
262+
263+
def complete_stopped(self, error: ErrorObject) -> None:
264+
"""Complete execution as terminated (TerminateWorkflowExecutionV2Request)."""
193265
self.result = DurableExecutionInvocationOutput(
194266
status=InvocationStatus.FAILED, error=error
195267
)
196268
self.is_complete = True
269+
self.close_status = CloseStatus.TERMINATED
197270

198271
def find_operation(self, operation_id: str) -> tuple[int, Operation]:
199272
"""Find operation by ID, return index and operation."""

src/aws_durable_execution_sdk_python_testing/executor.py

Lines changed: 54 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,7 @@ def get_execution_details(self, execution_arn: str) -> GetDurableExecutionRespon
128128

129129
# Extract execution details from the first operation (EXECUTION type)
130130
execution_op = execution.get_operation_execution_started()
131-
132-
# Determine status based on execution state
133-
if execution.is_complete:
134-
if (
135-
execution.result
136-
and execution.result.status == InvocationStatus.SUCCEEDED
137-
):
138-
status = "SUCCEEDED"
139-
else:
140-
status = "FAILED"
141-
else:
142-
status = "RUNNING"
131+
status = execution.status
143132

144133
# Extract result and error from execution result
145134
result = None
@@ -175,8 +164,8 @@ def list_executions(
175164
function_version: str | None = None, # noqa: ARG002
176165
execution_name: str | None = None,
177166
status_filter: str | None = None,
178-
time_after: str | None = None, # noqa: ARG002
179-
time_before: str | None = None, # noqa: ARG002
167+
started_after: str | None = None,
168+
started_before: str | None = None,
180169
marker: str | None = None,
181170
max_items: int | None = None,
182171
reverse_order: bool = False, # noqa: FBT001, FBT002
@@ -188,86 +177,43 @@ def list_executions(
188177
function_version: Filter by function version
189178
execution_name: Filter by execution name
190179
status_filter: Filter by status (RUNNING, SUCCEEDED, FAILED)
191-
time_after: Filter executions started after this time
192-
time_before: Filter executions started before this time
180+
started_after: Filter executions started after this time
181+
started_before: Filter executions started before this time
193182
marker: Pagination marker
194183
max_items: Maximum items to return (default 50)
195184
reverse_order: Return results in reverse chronological order
196185
197186
Returns:
198187
ListDurableExecutionsResponse: List of executions with pagination
199188
"""
200-
# Get all executions from store
201-
all_executions = self._store.list_all()
202-
203-
# Apply filters
204-
filtered_executions = []
205-
for execution in all_executions:
206-
# Filter by function name
207-
if function_name and execution.start_input.function_name != function_name:
208-
continue
209-
210-
# Filter by execution name
211-
if (
212-
execution_name
213-
and execution.start_input.execution_name != execution_name
214-
):
215-
continue
216-
217-
# Determine execution status
218-
execution_status = "RUNNING"
219-
if execution.is_complete:
220-
if (
221-
execution.result
222-
and execution.result.status == InvocationStatus.SUCCEEDED
223-
):
224-
execution_status = "SUCCEEDED"
225-
else:
226-
execution_status = "FAILED"
227-
228-
# Filter by status
229-
if status_filter and execution_status != status_filter:
230-
continue
231-
232-
# Convert to ExecutionSummary
233-
execution_op = execution.get_operation_execution_started()
234-
execution_summary = ExecutionSummary(
235-
durable_execution_arn=execution.durable_execution_arn,
236-
durable_execution_name=execution.start_input.execution_name,
237-
function_arn=f"arn:aws:lambda:us-east-1:123456789012:function:{execution.start_input.function_name}",
238-
status=execution_status,
239-
start_timestamp=execution_op.start_timestamp
240-
if execution_op.start_timestamp
241-
else datetime.now(UTC),
242-
end_timestamp=execution_op.end_timestamp
243-
if execution_op.end_timestamp
244-
else None,
245-
)
246-
filtered_executions.append(execution_summary)
247-
248-
# Sort by start date
249-
filtered_executions.sort(key=lambda e: e.start_timestamp, reverse=reverse_order)
250-
251-
# Apply pagination
252-
if max_items is None:
253-
max_items = 50
254-
255-
start_index = 0
189+
# Convert marker to offset
190+
offset: int = 0
256191
if marker:
257192
try:
258-
start_index = int(marker)
193+
offset = int(marker)
259194
except ValueError:
260-
start_index = 0
195+
offset = 0
261196

262-
end_index = start_index + max_items
263-
paginated_executions = filtered_executions[start_index:end_index]
197+
# Query store directly with parameters
198+
executions, next_marker = self._store.query(
199+
function_name=function_name,
200+
execution_name=execution_name,
201+
status_filter=status_filter,
202+
started_after=started_after,
203+
started_before=started_before,
204+
limit=max_items or 50,
205+
offset=offset,
206+
reverse_order=reverse_order,
207+
)
264208

265-
next_marker = None
266-
if end_index < len(filtered_executions):
267-
next_marker = str(end_index)
209+
# Convert to ExecutionSummary objects
210+
execution_summaries: list[ExecutionSummary] = [
211+
ExecutionSummary.from_execution(execution, execution.status)
212+
for execution in executions
213+
]
268214

269215
return ListDurableExecutionsResponse(
270-
durable_executions=paginated_executions, next_marker=next_marker
216+
durable_executions=execution_summaries, next_marker=next_marker
271217
)
272218

273219
def list_executions_by_function(
@@ -276,8 +222,8 @@ def list_executions_by_function(
276222
qualifier: str | None = None, # noqa: ARG002
277223
execution_name: str | None = None,
278224
status_filter: str | None = None,
279-
time_after: str | None = None,
280-
time_before: str | None = None,
225+
started_after: str | None = None,
226+
started_before: str | None = None,
281227
marker: str | None = None,
282228
max_items: int | None = None,
283229
reverse_order: bool = False, # noqa: FBT001, FBT002
@@ -289,8 +235,8 @@ def list_executions_by_function(
289235
qualifier: Function qualifier/version
290236
execution_name: Filter by execution name
291237
status_filter: Filter by status (RUNNING, SUCCEEDED, FAILED)
292-
time_after: Filter executions started after this time
293-
time_before: Filter executions started before this time
238+
started_after: Filter executions started after this time
239+
started_before: Filter executions started before this time
294240
marker: Pagination marker
295241
max_items: Maximum items to return (default 50)
296242
reverse_order: Return results in reverse chronological order
@@ -303,8 +249,8 @@ def list_executions_by_function(
303249
function_name=function_name,
304250
execution_name=execution_name,
305251
status_filter=status_filter,
306-
time_after=time_after,
307-
time_before=time_before,
252+
started_after=started_after,
253+
started_before=started_before,
308254
marker=marker,
309255
max_items=max_items,
310256
reverse_order=reverse_order,
@@ -343,8 +289,11 @@ def stop_execution(
343289
"Execution stopped by user request"
344290
)
345291

346-
# Stop the execution
347-
self.fail_execution(execution_arn, stop_error)
292+
# Stop sets TERMINATED close status (different from fail)
293+
logger.exception("[%s] Stopping execution.", execution_arn)
294+
execution.complete_stopped(error=stop_error) # Sets CloseStatus.TERMINATED
295+
self._store.update(execution)
296+
self._complete_events(execution_arn=execution_arn)
348297

349298
return StopDurableExecutionResponse(stop_timestamp=datetime.now(UTC))
350299

@@ -838,27 +787,24 @@ def wait_until_complete(
838787
raise ResourceNotFoundException(msg)
839788

840789
def complete_execution(self, execution_arn: str, result: str | None = None) -> None:
841-
"""Complete execution successfully."""
790+
"""Complete execution successfully (COMPLETE_WORKFLOW_EXECUTION decision)."""
842791
logger.debug("[%s] Completing execution with result: %s", execution_arn, result)
843792
execution: Execution = self._store.load(execution_arn=execution_arn)
844-
execution.complete_success(result=result)
793+
execution.complete_success(result=result) # Sets CloseStatus.COMPLETED
845794
self._store.update(execution)
846795
if execution.result is None:
847796
msg: str = "Execution result is required"
848-
849797
raise IllegalStateException(msg)
850798
self._complete_events(execution_arn=execution_arn)
851799

852800
def fail_execution(self, execution_arn: str, error: ErrorObject) -> None:
853-
"""Fail execution with error."""
801+
"""Fail execution with error (FAIL_WORKFLOW_EXECUTION decision)."""
854802
logger.exception("[%s] Completing execution with error.", execution_arn)
855803
execution: Execution = self._store.load(execution_arn=execution_arn)
856-
execution.complete_fail(error=error)
804+
execution.complete_fail(error=error) # Sets CloseStatus.FAILED
857805
self._store.update(execution)
858-
# set by complete_fail
859806
if execution.result is None:
860807
msg: str = "Execution result is required"
861-
862808
raise IllegalStateException(msg)
863809
self._complete_events(execution_arn=execution_arn)
864810

@@ -910,6 +856,19 @@ def on_failed(self, execution_arn: str, error: ErrorObject) -> None:
910856
"""Fail execution. Observer method triggered by notifier."""
911857
self.fail_execution(execution_arn, error)
912858

859+
def on_timed_out(self, execution_arn: str, error: ErrorObject) -> None:
860+
"""Handle execution timeout (workflow timeout). Observer method triggered by notifier."""
861+
logger.exception("[%s] Execution timed out.", execution_arn)
862+
execution: Execution = self._store.load(execution_arn=execution_arn)
863+
execution.complete_timeout(error=error) # Sets CloseStatus.TIMED_OUT
864+
self._store.update(execution)
865+
self._complete_events(execution_arn=execution_arn)
866+
867+
def on_stopped(self, execution_arn: str, error: ErrorObject) -> None:
868+
"""Handle execution stop. Observer method triggered by notifier."""
869+
# This should not be called directly - stop_execution handles termination
870+
self.fail_execution(execution_arn, error)
871+
913872
def on_wait_timer_scheduled(
914873
self, execution_arn: str, operation_id: str, delay: float
915874
) -> None:

0 commit comments

Comments
 (0)