-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathexecution.py
More file actions
449 lines (378 loc) · 17.2 KB
/
execution.py
File metadata and controls
449 lines (378 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
from __future__ import annotations
import contextlib
import functools
import json
import logging
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from enum import Enum
from typing import TYPE_CHECKING, Any
from aws_durable_execution_sdk_python.context import DurableContext, ExecutionState
from aws_durable_execution_sdk_python.exceptions import (
BackgroundThreadError,
BotoClientError,
CheckpointError,
DurableExecutionsError,
ExecutionError,
InvocationError,
SuspendExecution,
)
from aws_durable_execution_sdk_python.lambda_service import (
DurableServiceClient,
ErrorObject,
LambdaClient,
Operation,
OperationType,
OperationUpdate,
)
from aws_durable_execution_sdk_python.serdes import (
JsonSerDes,
SerDes,
deserialize,
serialize,
)
if TYPE_CHECKING:
from collections.abc import Callable, MutableMapping
import boto3 # type: ignore
from aws_durable_execution_sdk_python.types import LambdaContext
logger = logging.getLogger(__name__)
# 6MB in bytes, minus 50 bytes for envelope
LAMBDA_RESPONSE_SIZE_LIMIT = 6 * 1024 * 1024 - 50
# region Invocation models
@dataclass(frozen=True)
class InitialExecutionState:
operations: list[Operation]
next_marker: str
@staticmethod
def from_dict(input_dict: MutableMapping[str, Any]) -> InitialExecutionState:
operations = []
if input_operations := input_dict.get("Operations"):
operations = [Operation.from_dict(op) for op in input_operations]
return InitialExecutionState(
operations=operations,
next_marker=input_dict.get("NextMarker", ""),
)
def get_execution_operation(self) -> Operation | None:
if not self.operations:
# Due to payload size limitations we may have an empty operations list.
# This will only happen when loading the initial page of results and is
# expected behaviour. We don't fail, but instead return None
# as the execution operation does not exist
msg: str = "No durable operations found in initial execution state."
logger.debug(msg)
return None
candidate = self.operations[0]
if candidate.operation_type is not OperationType.EXECUTION:
msg = f"First operation in initial execution state is not an execution operation: {candidate.operation_type}"
raise DurableExecutionsError(msg)
return candidate
def get_input_payload(self) -> str | None:
# It is possible that backend will not provide an execution operation
# for the initial page of results.
if not (operations := self.get_execution_operation()):
return None
if not (execution_details := operations.execution_details):
return None
return execution_details.input_payload
def to_dict(self) -> MutableMapping[str, Any]:
return {
"Operations": [op.to_dict() for op in self.operations],
"NextMarker": self.next_marker,
}
@dataclass(frozen=True)
class DurableExecutionInvocationInput:
durable_execution_arn: str
checkpoint_token: str
initial_execution_state: InitialExecutionState
is_local_runner: bool
@staticmethod
def from_dict(
input_dict: MutableMapping[str, Any],
) -> DurableExecutionInvocationInput:
return DurableExecutionInvocationInput(
durable_execution_arn=input_dict["DurableExecutionArn"],
checkpoint_token=input_dict["CheckpointToken"],
initial_execution_state=InitialExecutionState.from_dict(
input_dict.get("InitialExecutionState", {})
),
is_local_runner=input_dict.get("LocalRunner", False),
)
def to_dict(self) -> MutableMapping[str, Any]:
return {
"DurableExecutionArn": self.durable_execution_arn,
"CheckpointToken": self.checkpoint_token,
"InitialExecutionState": self.initial_execution_state.to_dict(),
"LocalRunner": self.is_local_runner,
}
@dataclass(frozen=True)
class DurableExecutionInvocationInputWithClient(DurableExecutionInvocationInput):
"""Invocation input with Lambda boto client injected.
This is useful for testing scenarios where you want to inject a mock client.
"""
service_client: DurableServiceClient
@staticmethod
def from_durable_execution_invocation_input(
invocation_input: DurableExecutionInvocationInput,
service_client: DurableServiceClient,
):
return DurableExecutionInvocationInputWithClient(
durable_execution_arn=invocation_input.durable_execution_arn,
checkpoint_token=invocation_input.checkpoint_token,
initial_execution_state=invocation_input.initial_execution_state,
is_local_runner=invocation_input.is_local_runner,
service_client=service_client,
)
class InvocationStatus(Enum):
SUCCEEDED = "SUCCEEDED"
FAILED = "FAILED"
PENDING = "PENDING"
@dataclass(frozen=True)
class DurableExecutionInvocationOutput:
"""Representation the DurableExecutionInvocationOutput. This is what the Durable lambda handler returns.
If the execution has been already completed via an update to the EXECUTION operation via CheckpointDurableExecution,
payload must be empty for SUCCEEDED/FAILED status.
"""
status: InvocationStatus
result: str | None = None
error: ErrorObject | None = None
@classmethod
def from_dict(
cls, data: MutableMapping[str, Any]
) -> DurableExecutionInvocationOutput:
"""Create an instance from a dictionary.
Args:
data: Dictionary with camelCase keys matching the original structure
Returns:
A DurableExecutionInvocationOutput instance
"""
status = InvocationStatus(data.get("Status"))
error = ErrorObject.from_dict(data["Error"]) if data.get("Error") else None
return cls(status=status, result=data.get("Result"), error=error)
def to_dict(self) -> MutableMapping[str, Any]:
"""Convert to a dictionary with the original field names.
Returns:
Dictionary with the original camelCase keys
"""
result: MutableMapping[str, Any] = {"Status": self.status.value}
if self.result is not None:
# large payloads return "", because checkpointed already
result["Result"] = self.result
if self.error:
result["Error"] = self.error.to_dict()
return result
@classmethod
def create_succeeded(cls, result: str) -> DurableExecutionInvocationOutput:
"""Create a succeeded invocation output."""
return cls(status=InvocationStatus.SUCCEEDED, result=result)
# endregion Invocation models
def durable_execution(
func: Callable[[Any, DurableContext], Any] | None = None,
*,
boto3_client: boto3.client | None = None,
input_serdes: SerDes | None = None,
output_serdes: SerDes | None = None,
) -> Callable[[Any, LambdaContext], Any]:
# Decorator called with parameters
if func is None:
logger.debug("Decorator called with parameters")
return functools.partial(
durable_execution,
boto3_client=boto3_client,
input_serdes=input_serdes,
output_serdes=output_serdes,
)
logger.debug("Starting durable execution handler...")
input_serdes = input_serdes or JsonSerDes()
output_serdes = output_serdes or JsonSerDes()
def wrapper(event: Any, context: LambdaContext) -> MutableMapping[str, Any]:
invocation_input: DurableExecutionInvocationInput
service_client: DurableServiceClient
# event likely only to be DurableExecutionInvocationInputWithClient when directly injected by test framework
if isinstance(event, DurableExecutionInvocationInputWithClient):
logger.debug("durableExecutionArn: %s", event.durable_execution_arn)
invocation_input = event
service_client = invocation_input.service_client
else:
try:
logger.debug(
"durableExecutionArn: %s", event.get("DurableExecutionArn")
)
invocation_input = DurableExecutionInvocationInput.from_dict(event)
except (KeyError, TypeError, AttributeError) as e:
msg = (
"The payload is not the correct Durable Function input. "
"Please set DurableConfig on the AWS Lambda to invoke it as a Durable Function."
)
raise ExecutionError(msg) from e
# Local runner always uses its own client, otherwise use custom or default
if invocation_input.is_local_runner:
service_client = LambdaClient.initialize_local_runner_client()
else:
service_client = (
LambdaClient(client=boto3_client)
if boto3_client is not None
else LambdaClient.initialize_from_env()
)
raw_input_payload: str | None = (
invocation_input.initial_execution_state.get_input_payload()
)
input_event: MutableMapping[str, Any] = {}
if raw_input_payload and raw_input_payload.strip():
input_event = deserialize(
serdes=input_serdes,
data=raw_input_payload,
operation_id="EXECUTION",
durable_execution_arn=invocation_input.durable_execution_arn,
)
execution_state: ExecutionState = ExecutionState(
durable_execution_arn=invocation_input.durable_execution_arn,
initial_checkpoint_token=invocation_input.checkpoint_token,
operations={},
service_client=service_client,
)
execution_state.fetch_paginated_operations(
invocation_input.initial_execution_state.operations,
invocation_input.checkpoint_token,
invocation_input.initial_execution_state.next_marker,
)
durable_context: DurableContext = DurableContext.from_lambda_context(
state=execution_state, lambda_context=context
)
# Use ThreadPoolExecutor for concurrent execution of user code and background checkpoint processing
with (
ThreadPoolExecutor(
max_workers=2, thread_name_prefix="dex-handler"
) as executor,
contextlib.closing(execution_state) as execution_state,
):
# Thread 1: Run background checkpoint processing
executor.submit(execution_state.checkpoint_batches_forever)
# Thread 2: Execute user function
logger.debug(
"%s entering user-space...", invocation_input.durable_execution_arn
)
user_future = executor.submit(func, input_event, durable_context)
logger.debug(
"%s waiting for user code completion...",
invocation_input.durable_execution_arn,
)
try:
# Background checkpointing errors will propagate through CompletionEvent.wait() as BackgroundThreadError
result = user_future.result()
# done with userland
logger.debug(
"%s exiting user-space...",
invocation_input.durable_execution_arn,
)
# Serialize result using output_serdes if provided
serialized_result = serialize(
serdes=output_serdes,
value=result,
operation_id="EXECUTION",
durable_execution_arn=invocation_input.durable_execution_arn,
)
# large response handling here. Remember if checkpointing to complete, NOT to include
# payload in response
if (
serialized_result
and len(serialized_result) > LAMBDA_RESPONSE_SIZE_LIMIT
):
logger.debug(
"Response size (%s bytes) exceeds Lambda limit (%s) bytes). Checkpointing result.",
len(serialized_result),
LAMBDA_RESPONSE_SIZE_LIMIT,
)
success_operation = OperationUpdate.create_execution_succeed(
payload=serialized_result
)
# Checkpoint large result with blocking (is_sync=True, default).
# Must ensure the result is persisted before returning to Lambda.
# Large results exceed Lambda response limits and must be stored durably
# before the execution completes.
try:
execution_state.create_checkpoint(
success_operation, is_sync=True
)
except CheckpointError as e:
return handle_checkpoint_error(e).to_dict()
return DurableExecutionInvocationOutput.create_succeeded(
result=""
).to_dict()
return DurableExecutionInvocationOutput.create_succeeded(
result=serialized_result
).to_dict()
except BackgroundThreadError as bg_error:
# Background checkpoint system failed - propagated through CompletionEvent
# Do not attempt to checkpoint anything, just terminate immediately
if isinstance(bg_error.source_exception, BotoClientError):
logger.exception(
"Checkpoint processing failed",
extra=bg_error.source_exception.build_logger_extras(),
)
else:
logger.exception("Checkpoint processing failed")
# handle the original exception
if isinstance(bg_error.source_exception, CheckpointError):
return handle_checkpoint_error(bg_error.source_exception).to_dict()
raise bg_error.source_exception from bg_error
except SuspendExecution:
# User code suspended - stop background checkpointing thread
logger.debug("Suspending execution...")
return DurableExecutionInvocationOutput(
status=InvocationStatus.PENDING
).to_dict()
except CheckpointError as e:
# Checkpoint system is broken - stop background thread and exit immediately
logger.exception(
"Checkpoint system failed",
extra=e.build_logger_extras(),
)
return handle_checkpoint_error(e).to_dict()
except InvocationError:
logger.exception("Invocation error. Must terminate.")
# Throw the error to trigger Lambda retry
raise
except ExecutionError as e:
logger.exception("Execution error. Must terminate without retry.")
return DurableExecutionInvocationOutput(
status=InvocationStatus.FAILED,
error=ErrorObject.from_exception(e),
).to_dict()
except Exception as e:
# all user-space errors go here
logger.exception("Execution failed")
result = DurableExecutionInvocationOutput(
status=InvocationStatus.FAILED, error=ErrorObject.from_exception(e)
).to_dict()
serialized_result = json.dumps(result)
if (
serialized_result
and len(serialized_result) > LAMBDA_RESPONSE_SIZE_LIMIT
):
logger.debug(
"Response size (%s bytes) exceeds Lambda limit (%s) bytes). Checkpointing result.",
len(serialized_result),
LAMBDA_RESPONSE_SIZE_LIMIT,
)
failed_operation = OperationUpdate.create_execution_fail(
error=ErrorObject.from_exception(e)
)
# Checkpoint large result with blocking (is_sync=True, default).
# Must ensure the result is persisted before returning to Lambda.
# Large results exceed Lambda response limits and must be stored durably
# before the execution completes.
try:
execution_state.create_checkpoint_sync(failed_operation)
except CheckpointError as e:
return handle_checkpoint_error(e).to_dict()
return DurableExecutionInvocationOutput(
status=InvocationStatus.FAILED
).to_dict()
return result
return wrapper
def handle_checkpoint_error(error: CheckpointError) -> DurableExecutionInvocationOutput:
if error.is_retriable():
raise error from None # Terminate Lambda immediately and have it be retried
return DurableExecutionInvocationOutput(
status=InvocationStatus.FAILED, error=ErrorObject.from_exception(error)
)