diff --git a/docs/services/lambda.md b/docs/services/lambda.md index 83dc78da79..f36baa3cc8 100644 --- a/docs/services/lambda.md +++ b/docs/services/lambda.md @@ -95,13 +95,11 @@ type `Lambda Function Invocation Result - Failure`, a `condition` of `RetriesExh `functionError` member is present only on a failure record. Since retries are not applied, a failure reaches its destination once and `approximateInvokeCount` is always 1. -Three limits are worth knowing: +An alias-specific destination configuration is used when the alias is invoked. If the alias has +none, Floci checks the configuration on the version that the alias resolves to. + +Two limits are worth knowing: -- **A destination configured on an alias does not fire.** The configuration is matched against the - version the invocation actually ran, so one stored for `$LATEST` or for an explicit version is - found, while one stored for an alias (`Qualifier: prod`) is not, and nothing is delivered. The - CDK `onSuccess` and `onFailure` properties store theirs under `$LATEST`, so a destination - declared that way is unaffected. - **An S3 `OnFailure` destination is not delivered.** AWS added an S3 bucket as a fifth destination kind, on failure only. Floci routes on the service in the destination ARN and has no `s3` arm, so such a record is dropped with a warning rather than written to the bucket. The diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/AsyncInvokeDestinationRouter.java b/src/main/java/io/github/hectorvent/floci/services/lambda/AsyncInvokeDestinationRouter.java index 64b9048b16..a49efc9e0f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/AsyncInvokeDestinationRouter.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/AsyncInvokeDestinationRouter.java @@ -108,12 +108,17 @@ public class AsyncInvokeDestinationRouter { * {@link LambdaInvocationChain} instead of running forever */ public void route(LambdaFunction fn, byte[] requestPayload, InvokeResult result, int chainDepth) { + route(fn, requestPayload, result, chainDepth, null); + } + + public void route(LambdaFunction fn, byte[] requestPayload, InvokeResult result, int chainDepth, + String invokedQualifier) { // A runtime that never started, timed out, or crashed is reported as a function error by // the executor, so this one test covers a handler error and a failed runtime alike. boolean failed = result.getFunctionError() != null; FunctionEventInvokeConfig.Destination destination; try { - destination = destinationFor(fn, failed); + destination = destinationFor(fn, failed, invokedQualifier); } catch (Exception e) { LOG.warnv("Could not read the event invoke configuration of {0}: {1}", fn.getFunctionArn(), e.getMessage()); @@ -133,8 +138,11 @@ public class AsyncInvokeDestinationRouter { } } - private FunctionEventInvokeConfig.Destination destinationFor(LambdaFunction fn, boolean failed) { - FunctionEventInvokeConfig config = lambdaService.get().findEventInvokeConfig(fn).orElse(null); + private FunctionEventInvokeConfig.Destination destinationFor(LambdaFunction fn, boolean failed, + String invokedQualifier) { + FunctionEventInvokeConfig config = (invokedQualifier == null + ? lambdaService.get().findEventInvokeConfig(fn) + : lambdaService.get().findEventInvokeConfig(fn, invokedQualifier)).orElse(null); if (config == null || config.getDestinationConfig() == null) { return null; } diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorService.java index 7d1716cf73..64d79bc2c8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorService.java @@ -80,6 +80,11 @@ public class LambdaExecutorService { * one a destination chain can re-enter through. */ InvokeResult invoke(LambdaFunction fn, byte[] payload, InvocationType type, int chainDepth) { + return invoke(fn, payload, type, chainDepth, null); + } + + InvokeResult invoke(LambdaFunction fn, byte[] payload, InvocationType type, int chainDepth, + String invokedQualifier) { String requestId = UUID.randomUUID().toString(); if (type == InvocationType.DryRun) { @@ -105,7 +110,7 @@ public class LambdaExecutorService { permit.close(); } if (destinationRouter != null) { - destinationRouter.route(fn, payload, asyncResult, chainDepth); + destinationRouter.route(fn, payload, asyncResult, chainDepth, invokedQualifier); } }); } catch (RuntimeException e) { diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java index 72bd8d5752..7085e3a41d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java @@ -1047,7 +1047,7 @@ public class LambdaService implements ResourceProvider { } reportCustomResourceLiveness(payload); InvokeResult result = executorService.invoke(fn, payload, type, - LambdaInvocationChain.currentDepth()); + LambdaInvocationChain.currentDepth(), qualifier); result.setExecutedVersion(fn.getVersion()); return result; } @@ -1076,7 +1076,7 @@ public class LambdaService implements ResourceProvider { LambdaArnUtils.ResolvedFunctionRef ref = LambdaArnUtils.resolve(functionArn); LambdaFunction fn = targetResolver.resolveInvokeTargetForAccount( arn.accountId(), arn.region(), ref.name(), ref.qualifier()); - InvokeResult result = executorService.invoke(fn, payload, type, chainDepth); + InvokeResult result = executorService.invoke(fn, payload, type, chainDepth, ref.qualifier()); result.setExecutedVersion(fn.getVersion()); return result; } @@ -3220,36 +3220,38 @@ public class LambdaService implements ResourceProvider { return result; } - /** - * The event invoke configuration that applies to an invocation of {@code fn}, or empty when - * the function has none. Unlike {@link #getEventInvokeConfig} this answers a background - * worker rather than an API caller, so an absent configuration is a result and not a fault. - * - *
The lookup key is the unqualified function ARN plus the version the invocation actually - * ran, which is where a function-level {@code PutFunctionEventInvokeConfig} stores its - * settings: that call names {@code $LATEST}, and so does a resolved unpublished function. - * - *
The read runs as the function's owning account. {@code PutFunctionEventInvokeConfig}
- * stored the configuration in that account's partition of the account-aware backend, and the
- * background worker calling this carries no request context, so without re-establishing the
- * account the read would land in the default partition and a function in any other account
- * would look as though it had no configuration at all.
- */
public Optional>> entries = ArgumentCaptor.forClass(List.class);
+ verify(eventBridgeService, times(2)).putEvents(entries.capture(), eq("us-east-1"), eq(null));
+ JsonNode aliasEntry = MAPPER.valueToTree(entries.getAllValues().get(0).get(0));
+ JsonNode versionEntry = MAPPER.valueToTree(entries.getAllValues().get(1).get(0));
+ assertEquals(BUS_ARN, aliasEntry.path("EventBusName").asText());
+ assertEquals(versionBusArn, versionEntry.path("EventBusName").asText());
+ assertEquals(version.getFunctionArn(), detailOf(aliasEntry)
+ .path("requestContext").path("functionArn").asText());
+ assertEquals(version.getVersion(), detailOf(aliasEntry)
+ .path("responseContext").path("executedVersion").asText());
+ }
+
private void configure(String onSuccess, String onFailure) {
FunctionEventInvokeConfig config = new FunctionEventInvokeConfig();
FunctionEventInvokeConfig.DestinationConfig destinations =
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaArnInvocationAccountTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaArnInvocationAccountTest.java
index 0ae08b667a..2f4c2522ae 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaArnInvocationAccountTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaArnInvocationAccountTest.java
@@ -20,6 +20,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.AdditionalMatchers.aryEq;
import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -48,7 +50,7 @@ class LambdaArnInvocationAccountTest {
LambdaExecutorService executor = mock(LambdaExecutorService.class);
InvokeResult executorResult = new InvokeResult();
- when(executor.invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0)))
+ when(executor.invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), isNull()))
.thenReturn(executorResult);
LambdaService service = new LambdaService(
store,
@@ -76,7 +78,7 @@ class LambdaArnInvocationAccountTest {
InvokeResult result = service.invokeArn(functionArn, "{}".getBytes(), InvocationType.Event);
assertEquals("$LATEST", result.getExecutedVersion());
- verify(executor).invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0));
+ verify(executor).invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), isNull());
}
@Test
@@ -111,7 +113,7 @@ class LambdaArnInvocationAccountTest {
"alias::" + region + "::" + functionName + "::live", alias);
LambdaExecutorService executor = mock(LambdaExecutorService.class);
- when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0)))
+ when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), nullable(String.class)))
.thenAnswer(ignored -> new InvokeResult());
LambdaService service = new LambdaService(
functionStore,
@@ -143,8 +145,8 @@ class LambdaArnInvocationAccountTest {
assertEquals("7", versionResult.getExecutedVersion());
assertEquals("7", aliasResult.getExecutedVersion());
- verify(executor, org.mockito.Mockito.times(2))
- .invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0));
+ verify(executor).invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), eq("7"));
+ verify(executor).invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), eq("live"));
}
@Test
@@ -178,9 +180,9 @@ class LambdaArnInvocationAccountTest {
new AccountAwareStorageBackend<>(rawAliases, null, defaultAccount));
LambdaExecutorService executor = mock(LambdaExecutorService.class);
- when(executor.invoke(eq(latest), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0)))
+ when(executor.invoke(eq(latest), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), isNull()))
.thenAnswer(ignored -> new InvokeResult());
- when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0)))
+ when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), nullable(String.class)))
.thenAnswer(ignored -> new InvokeResult());
LambdaService service = service(functionStore, aliasStore, executor, region, defaultAccount);
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaEventInvokeConfigTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaEventInvokeConfigTest.java
index e6fe3c383c..0445af8a5c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaEventInvokeConfigTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaEventInvokeConfigTest.java
@@ -145,4 +145,26 @@ class LambdaEventInvokeConfigTest {
assertEquals("arn:aws:sqs:us-east-1:000000000000:dlq",
cfg.getDestinationConfig().getOnFailure().getDestination());
}
+
+ @Test
+ void invokedAliasConfigTakesPrecedenceAndFallsBackToExecutedVersion() {
+ String versionDestination = "arn:aws:sqs:us-east-1:000000000000:version-queue";
+ String aliasDestination = "arn:aws:sqs:us-east-1:000000000000:alias-queue";
+ service.putEventInvokeConfig(REGION, "test-fn", "1", Map.of(
+ "DestinationConfig", Map.of("OnSuccess", Map.of("Destination", versionDestination))));
+ service.putEventInvokeConfig(REGION, "test-fn", "prod", Map.of(
+ "DestinationConfig", Map.of("OnSuccess", Map.of("Destination", aliasDestination))));
+
+ LambdaFunction executedVersion = new LambdaFunction();
+ executedVersion.setFunctionArn("arn:aws:lambda:us-east-1:000000000000:function:test-fn:1");
+ executedVersion.setAccountId("000000000000");
+ executedVersion.setVersion("1");
+
+ assertEquals(aliasDestination, service.findEventInvokeConfig(executedVersion, "prod")
+ .orElseThrow().getDestinationConfig().getOnSuccess().getDestination());
+ assertEquals(versionDestination, service.findEventInvokeConfig(executedVersion, "other")
+ .orElseThrow().getDestinationConfig().getOnSuccess().getDestination());
+ assertEquals(versionDestination, service.findEventInvokeConfig(executedVersion)
+ .orElseThrow().getDestinationConfig().getOnSuccess().getDestination());
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorServiceTest.java
index e9f09f2ca0..d4bf6b76d5 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorServiceTest.java
@@ -27,6 +27,8 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -76,6 +78,27 @@ class LambdaExecutorServiceTest {
verify(warmPool, timeout(5000)).acquire(fn);
}
+ @Test
+ void asyncInvocationPassesInvokedAliasToDestinationRouter() {
+ AsyncInvokeDestinationRouter router = mock(AsyncInvokeDestinationRouter.class);
+ executor.shutdown();
+ executor = new LambdaExecutorService(warmPool, new ObjectMapper(), concurrencyLimiter, router);
+ RuntimeApiServer rtas = mock(RuntimeApiServer.class);
+ ContainerHandle handle = new ContainerHandle("cid-alias", "test-fn", rtas, ContainerState.WARM);
+ when(warmPool.acquire(fn)).thenReturn(handle);
+ doAnswer(inv -> {
+ PendingInvocation invocation = inv.getArgument(0);
+ invocation.getResultFuture().complete(
+ new InvokeResult(200, null, "{}".getBytes(), null, invocation.getRequestId()));
+ return invocation.getResultFuture();
+ }).when(rtas).enqueue(any(PendingInvocation.class));
+
+ byte[] payload = "{}".getBytes();
+ executor.invoke(fn, payload, InvocationType.Event, 0, "prod");
+
+ verify(router, timeout(5000)).route(eq(fn), eq(payload), any(InvokeResult.class), eq(0), eq("prod"));
+ }
+
@Test
void syncInvocationPastTheChainBound_isNotAffected() {
// Only the asynchronous path can be re-entered by a destination chain, and refusing a
@@ -234,14 +257,14 @@ class LambdaExecutorServiceTest {
doAnswer(inv -> {
routed.countDown();
return null;
- }).when(router).route(any(), any(), any(), anyInt());
+ }).when(router).route(any(), any(), any(), anyInt(), isNull());
byte[] payload = "{}".getBytes();
InvokeResult result = routingExecutor.invoke(fn, payload, InvocationType.Event);
assertEquals(202, result.getStatusCode());
assertTrue(routed.await(5, TimeUnit.SECONDS), "destination routing never ran");
- verify(router).route(fn, payload, expected, 0);
+ verify(router).route(fn, payload, expected, 0, null);
}
@Test