mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
feat(lambda): durable function callbacks (#5100)
* feat(lambda): durable function callbacks
A CALLBACK operation started at checkpoint gets a callback id that
SendDurableExecutionCallbackSuccess, Failure and Heartbeat complete or
keep alive, after which the function is invoked again to see it.
TimeoutSeconds and HeartbeatTimeoutSeconds time it out with
Callback.Timeout or Callback.Heartbeat. A completed, timed out or
stopped callback answers CallbackTimeoutException, an unreadable id
"Invalid callback id". The callback id carries the execution ARN, which
IAM authorizes the callback actions against.
Option range errors now name the update, as AWS does
("updates.1.member.waitOptions.waitSeconds").
* fix(lambda): name a repeated durable update as AWS does
AWS rejects two updates of one operation in a batch, other than a START
closed by its STEP or CONTEXT action, with "Cannot update the same
operation twice in a single request."
* fix(lambda): leave a durable execution waiting after a body-less callback failure
AWS marks a callback failed with an empty request body as FAILED but does
not invoke the function again, so the execution keeps waiting. The SDKs
send {} when Error is omitted, which does invoke it.
* fix(lambda): bound a durable callback failure's error at 1 MB
AWS rejects an Error over 1048576 bytes, measured as compact JSON, with
InvalidParameterValueException "Error object size must be less than or
equal to 1048576 bytes."
* test(lambda): keep a pending durable callback across a restart
* fix(lambda): reject a durable callback failure body that is not a JSON object
AWS treats only an empty body as a failure with no Error. Whitespace,
null or broken JSON get 400 SerializationException and leave the
callback open.
* docs(lambda): note the durable callback result limit AWS enforces
This commit is contained in:
1 parent
a6df1dbb12
commit
a1ed65c51d
20 files changed
+809
-50
No files matched your search
+66
-4
@@ -22,6 +22,7 @@ class LambdaDurableFunctionsTest {
|
||||
|
||||
private static final String ROLE = "arn:aws:iam::000000000000:role/lambda-role";
|
||||
private static final String FN = TestFixtures.uniqueName("fn-durable-exec");
|
||||
private static final String CALLBACK_FN = TestFixtures.uniqueName("fn-durable-callback");
|
||||
|
||||
private static LambdaClient lambda;
|
||||
|
||||
@@ -33,10 +34,12 @@ class LambdaDurableFunctionsTest {
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (lambda != null) {
|
||||
try {
|
||||
lambda.deleteFunction(DeleteFunctionRequest.builder().functionName(FN).build());
|
||||
} catch (Exception ignored) {
|
||||
// Cleanup only. A failed delete must not hide the test result.
|
||||
for (String name : new String[] {FN, CALLBACK_FN}) {
|
||||
try {
|
||||
lambda.deleteFunction(DeleteFunctionRequest.builder().functionName(name).build());
|
||||
} catch (Exception ignored) {
|
||||
// Cleanup only. A failed delete must not hide the test result.
|
||||
}
|
||||
}
|
||||
lambda.close();
|
||||
}
|
||||
@@ -117,4 +120,63 @@ class LambdaDurableFunctionsTest {
|
||||
.isInstanceOf(InvalidParameterValueException.class)
|
||||
.hasMessageContaining("You cannot invoke a durable function using an unqualified ARN.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("a durable callback is completed through the callback APIs and resumes the execution")
|
||||
void durableCallbackResumesTheExecution() throws InterruptedException {
|
||||
Assumptions.assumeTrue(TestFixtures.isLambdaDispatchAvailable(),
|
||||
"skipping: Lambda dispatch (Docker) not available in this environment");
|
||||
|
||||
lambda.createFunction(CreateFunctionRequest.builder()
|
||||
.functionName(CALLBACK_FN)
|
||||
.runtime(Runtime.PYTHON3_14)
|
||||
.role(ROLE)
|
||||
.handler("lambda_function.handler")
|
||||
.timeout(30)
|
||||
.durableConfig(DurableConfig.builder().executionTimeout(120).retentionPeriodInDays(1).build())
|
||||
.code(FunctionCode.builder().zipFile(SdkBytes.fromByteArray(LambdaUtils.durablePythonZip())).build())
|
||||
.build());
|
||||
|
||||
String arn = lambda.invoke(InvokeRequest.builder()
|
||||
.functionName(CALLBACK_FN + ":$LATEST")
|
||||
.invocationType(InvocationType.EVENT)
|
||||
.payload(SdkBytes.fromUtf8String("{\"callback\": true}"))
|
||||
.build()).durableExecutionArn();
|
||||
|
||||
String callbackId = null;
|
||||
for (int i = 0; i < 60 && callbackId == null; i++) {
|
||||
callbackId = lambda.getDurableExecutionHistory(GetDurableExecutionHistoryRequest.builder()
|
||||
.durableExecutionArn(arn).build())
|
||||
.events().stream()
|
||||
.filter(event -> event.eventType() == EventType.CALLBACK_STARTED)
|
||||
.map(event -> event.callbackStartedDetails().callbackId())
|
||||
.findFirst().orElse(null);
|
||||
if (callbackId == null) {
|
||||
Thread.sleep(500);
|
||||
}
|
||||
}
|
||||
assertThat(callbackId).as("the function started a callback").isNotNull();
|
||||
String id = callbackId;
|
||||
|
||||
lambda.sendDurableExecutionCallbackHeartbeat(SendDurableExecutionCallbackHeartbeatRequest.builder()
|
||||
.callbackId(id).build());
|
||||
lambda.sendDurableExecutionCallbackSuccess(SendDurableExecutionCallbackSuccessRequest.builder()
|
||||
.callbackId(id).result(SdkBytes.fromUtf8String("{\"approved\": true}")).build());
|
||||
|
||||
GetDurableExecutionResponse execution = null;
|
||||
for (int i = 0; i < 60; i++) {
|
||||
execution = lambda.getDurableExecution(GetDurableExecutionRequest.builder()
|
||||
.durableExecutionArn(arn).includeExecutionData(true).build());
|
||||
if (execution.status() != ExecutionStatus.RUNNING) {
|
||||
break;
|
||||
}
|
||||
Thread.sleep(500);
|
||||
}
|
||||
assertThat(execution.status()).isEqualTo(ExecutionStatus.SUCCEEDED);
|
||||
assertThat(execution.result()).isEqualTo("{\"approved\": true}");
|
||||
|
||||
assertThatThrownBy(() -> lambda.sendDurableExecutionCallbackSuccess(
|
||||
SendDurableExecutionCallbackSuccessRequest.builder().callbackId(id).build()))
|
||||
.isInstanceOf(CallbackTimeoutException.class);
|
||||
}
|
||||
}
|
||||
@@ -284,6 +284,19 @@ public final class LambdaUtils {
|
||||
if request.get("fail"):
|
||||
return {"Status": "FAILED",
|
||||
"Error": {"ErrorMessage": "asked to fail", "ErrorType": "TestFailure"}}
|
||||
if request.get("callback"):
|
||||
if "callback" not in operations:
|
||||
client.checkpoint_durable_execution(
|
||||
DurableExecutionArn=event["DurableExecutionArn"],
|
||||
CheckpointToken=event["CheckpointToken"],
|
||||
Updates=[{"Id": "callback", "Name": "approval", "Type": "CALLBACK",
|
||||
"SubType": "Callback", "Action": "START",
|
||||
"CallbackOptions": {"HeartbeatTimeoutSeconds": 60}}])
|
||||
return {"Status": "PENDING"}
|
||||
callback = operations["callback"]
|
||||
if callback["Status"] == "STARTED":
|
||||
return {"Status": "PENDING"}
|
||||
return {"Status": "SUCCEEDED", "Result": callback["CallbackDetails"].get("Result", "")}
|
||||
if "step" not in operations:
|
||||
client.checkpoint_durable_execution(
|
||||
DurableExecutionArn=event["DurableExecutionArn"],
|
||||
|
||||
Reference in new issue
Block a user