mirror of
https://github.com/floci-io/floci.git
synced 2026-11-25 14:57:54 -05:00
feat(lambda): durable function chained invokes (#5164)
* feat(lambda): durable function chained invokes
A CHAINED_INVOKE operation runs another function. A durable target
needs a version or alias and runs as its own durable execution with a
random name; its close completes the operation as SUCCEEDED, FAILED,
TIMED_OUT ("CHAINED_INVOKE timed out after N seconds.") or STOPPED. Any
other function is invoked once, and an output over 1 MB fails the
operation. A missing function or an unqualified durable one fails the
operation at once. Stopping the parent leaves the child running.
A plain chained invoke lost with the process runs again after a restart,
and a durable child that closed before its report reaches the parent
reports then.
* fix(lambda): close the gaps in durable chained invokes
A durable child that closes during a restart reports to its parent
under its own lock, and recovery triggers an execution once. A durable
target invoked without Payload starts its child with no input, while a
plain target gets {}. A target started in the batch that closes the
execution still runs and its result is ignored, as on AWS.
* fix(lambda): reject a chained invoke of another account or region
AWS rejects the checkpoint with InvalidParameterValueException "Cannot
start a CHAINED_INVOKE on a function in another account." or "... in
another region." Cross-account chained invocations are not supported.
* fix(lambda): keep durable chained invokes across a crash
The parent is saved before the child it starts, so a crash in between
leaves no child the parent does not know. On restart a child the parent
recorded but the store lost is started again under the same ARN, and a
plain target started by the checkpoint that closed the execution runs
again until its result is recorded. A child reports only to the
operation that started it.
* fix(lambda): recreate a lost durable child on the version its ARN names
An alias the chained invoke named may point to another version after a
restart, and a child recreated there would not match the ARN the parent
saved.
This commit is contained in:
1 parent
be668acefd
commit
6e6ce61eea
13 files changed
+921
-37
No files matched your search
+44
-1
@@ -23,6 +23,8 @@ 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 final String CHAIN_FN = TestFixtures.uniqueName("fn-durable-chain");
|
||||
private static final String CHAIN_TARGET_FN = TestFixtures.uniqueName("fn-durable-chain-target");
|
||||
|
||||
private static LambdaClient lambda;
|
||||
|
||||
@@ -34,7 +36,7 @@ class LambdaDurableFunctionsTest {
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (lambda != null) {
|
||||
for (String name : new String[] {FN, CALLBACK_FN}) {
|
||||
for (String name : new String[] {FN, CALLBACK_FN, CHAIN_FN, CHAIN_TARGET_FN}) {
|
||||
try {
|
||||
lambda.deleteFunction(DeleteFunctionRequest.builder().functionName(name).build());
|
||||
} catch (Exception ignored) {
|
||||
@@ -179,4 +181,45 @@ class LambdaDurableFunctionsTest {
|
||||
SendDurableExecutionCallbackSuccessRequest.builder().callbackId(id).build()))
|
||||
.isInstanceOf(CallbackTimeoutException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("a durable function invokes another function and receives its result")
|
||||
void durableChainedInvokeReturnsTheTargetResult() {
|
||||
Assumptions.assumeTrue(TestFixtures.isLambdaDispatchAvailable(),
|
||||
"skipping: Lambda dispatch (Docker) not available in this environment");
|
||||
|
||||
lambda.createFunction(CreateFunctionRequest.builder()
|
||||
.functionName(CHAIN_TARGET_FN)
|
||||
.runtime(Runtime.NODEJS20_X)
|
||||
.role(ROLE)
|
||||
.handler("index.handler")
|
||||
.code(FunctionCode.builder().zipFile(SdkBytes.fromByteArray(LambdaUtils.handlerZip())).build())
|
||||
.build());
|
||||
lambda.createFunction(CreateFunctionRequest.builder()
|
||||
.functionName(CHAIN_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());
|
||||
|
||||
InvokeResponse invoked = lambda.invoke(InvokeRequest.builder()
|
||||
.functionName(CHAIN_FN + ":$LATEST")
|
||||
.payload(SdkBytes.fromUtf8String("{\"chain\": \"" + CHAIN_TARGET_FN + "\"}"))
|
||||
.build());
|
||||
|
||||
assertThat(invoked.functionError()).isNull();
|
||||
assertThat(invoked.payload().asUtf8String()).contains("Hello, Durable!");
|
||||
GetDurableExecutionHistoryResponse history = lambda.getDurableExecutionHistory(
|
||||
GetDurableExecutionHistoryRequest.builder().durableExecutionArn(invoked.durableExecutionArn()).build());
|
||||
assertThat(history.events()).extracting(Event::eventType)
|
||||
.contains(EventType.CHAINED_INVOKE_STARTED, EventType.CHAINED_INVOKE_SUCCEEDED);
|
||||
Event started = history.events().stream()
|
||||
.filter(event -> event.eventType() == EventType.CHAINED_INVOKE_STARTED)
|
||||
.findFirst().orElseThrow();
|
||||
assertThat(started.chainedInvokeStartedDetails().functionName()).isEqualTo(CHAIN_TARGET_FN);
|
||||
assertThat(started.chainedInvokeStartedDetails().executedVersion()).isEqualTo("$LATEST");
|
||||
}
|
||||
}
|
||||
@@ -284,6 +284,22 @@ public final class LambdaUtils {
|
||||
if request.get("fail"):
|
||||
return {"Status": "FAILED",
|
||||
"Error": {"ErrorMessage": "asked to fail", "ErrorType": "TestFailure"}}
|
||||
if request.get("chain"):
|
||||
if "chain" not in operations:
|
||||
client.checkpoint_durable_execution(
|
||||
DurableExecutionArn=event["DurableExecutionArn"],
|
||||
CheckpointToken=event["CheckpointToken"],
|
||||
Updates=[{"Id": "chain", "Name": "greet", "Type": "CHAINED_INVOKE",
|
||||
"SubType": "ChainedInvoke", "Action": "START",
|
||||
"Payload": json.dumps({"name": "Durable"}),
|
||||
"ChainedInvokeOptions": {"FunctionName": request["chain"]}}])
|
||||
return {"Status": "PENDING"}
|
||||
chain = operations["chain"]
|
||||
if chain["Status"] == "STARTED":
|
||||
return {"Status": "PENDING"}
|
||||
details = chain.get("ChainedInvokeDetails", {})
|
||||
return {"Status": "SUCCEEDED",
|
||||
"Result": details.get("Result") or json.dumps(details.get("Error"))}
|
||||
if request.get("callback"):
|
||||
if "callback" not in operations:
|
||||
client.checkpoint_durable_execution(
|
||||
|
||||
Reference in new issue
Block a user