mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(lambda): store DurableConfig on functions (execution later) (#5007)
* feat(lambda): accept and echo DurableConfig on functions CreateFunction and UpdateFunctionConfiguration take DurableConfig, PublishVersion snapshots it, and every read echoes it. ExecutionTimeout is required on create, RetentionPeriodInDays defaults to 14, a durable function's Timeout defaults to min(ExecutionTimeout, 900) and its logging to JSON. Update merges the members, and a function created without DurableConfig cannot gain one. KMSKeyArn is validated against its pattern and stored, the key itself is not looked up. * chore(lambda): trim the DurableConfig tests to one case per rule * feat(lambda): durable functions log in JSON format only A durable function rejects LogFormat Text on create and update, and a LoggingConfig without LogFormat reads back as JSON. An empty KMSKeyArn means no key. * chore(lambda): let the logging helpers own the durable JSON rule validateLoggingConfig and applyLoggingConfig take the durable flag, so a durable function's default format and the Text rejection live in one place and a LoggingConfig without LogFormat keeps its log levels. applyDurableConfig sets the retention default and merges, and the test uses chainable helpers. * chore(lambda): say why the durable compat test ignores a failed cleanup
This commit is contained in:
1 parent
583b2baf7f
commit
823f8751de
8 files changed
+483
-9
No files matched your search
@@ -769,12 +769,26 @@ public class LambdaController {
|
||||
putSnapStart(node, fn);
|
||||
putLoggingConfig(node, fn);
|
||||
putRuntimeVersionConfig(node, fn);
|
||||
putDurableConfig(node, fn);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> result = objectMapper.convertValue(node, Map.class);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Only durable functions carry a DurableConfig; AWS omits the member for every other function. */
|
||||
private void putDurableConfig(ObjectNode node, LambdaFunction fn) {
|
||||
if (!fn.isDurable()) {
|
||||
return;
|
||||
}
|
||||
ObjectNode durable = node.putObject("DurableConfig");
|
||||
durable.put("ExecutionTimeout", fn.getDurableExecutionTimeout());
|
||||
durable.put("RetentionPeriodInDays", fn.getDurableRetentionPeriodInDays());
|
||||
if (fn.getDurableKmsKeyArn() != null) {
|
||||
durable.put("KMSKeyArn", fn.getDurableKmsKeyArn());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* VpcConfigResponse, not VpcConfig: the response shape adds VpcId, which the request shape
|
||||
* has no member for. Emitted only while the function is actually attached to a VPC — AWS
|
||||
|
||||
@@ -89,6 +89,13 @@ public class LambdaService implements ResourceProvider {
|
||||
private static final int MAX_TAG_KEY_LENGTH = 128;
|
||||
private static final Pattern KMS_KEY_ARN_PATTERN = Pattern.compile(
|
||||
"^(arn:(aws[a-zA-Z-]*)?:[a-z0-9-.]+:.*)?$");
|
||||
/** The DurableConfig member has its own pattern text, which AWS prints in the validation message. */
|
||||
private static final Pattern DURABLE_KMS_KEY_ARN_PATTERN = Pattern.compile(
|
||||
"(arn:(aws[a-zA-Z-]*)?:[a-z0-9-.]+:.*)|()");
|
||||
private static final int MAX_DURABLE_EXECUTION_TIMEOUT_SECONDS = 31622400;
|
||||
private static final int MAX_DURABLE_RETENTION_DAYS = 90;
|
||||
private static final int DEFAULT_DURABLE_RETENTION_DAYS = 14;
|
||||
private static final int MAX_FUNCTION_TIMEOUT_SECONDS = 900;
|
||||
private static final Pattern LAYER_VERSION_ARN_PATTERN = Pattern.compile(
|
||||
"^((arn:(aws[a-zA-Z-]*)?:lambda:(eusc-)?[a-z]{2}((-gov)|(-iso([a-z]?)))?-[a-z]+-\\d{1}:\\d{12}:layer:[a-zA-Z0-9-_]+:[0-9]+)"
|
||||
+ "|(arn:[a-zA-Z0-9-]+:lambda:::awslayer:[a-zA-Z0-9-_]+))$");
|
||||
@@ -124,7 +131,7 @@ public class LambdaService implements ResourceProvider {
|
||||
*/
|
||||
private static final List<String> CONFIG_STRUCTURE_MEMBERS = List.of(
|
||||
"Environment", "EphemeralStorage", "TracingConfig", "DeadLetterConfig",
|
||||
"VpcConfig", "SnapStart", "LoggingConfig", "ImageConfig");
|
||||
"VpcConfig", "SnapStart", "LoggingConfig", "ImageConfig", "DurableConfig");
|
||||
|
||||
private final LambdaFunctionStore functionStore;
|
||||
private final LambdaExecutorService executorService;
|
||||
@@ -404,6 +411,7 @@ public class LambdaService implements ResourceProvider {
|
||||
Map<String, Object> snapStart = structureMember(request, "SnapStart");
|
||||
Map<String, Object> loggingConfig = structureMember(request, "LoggingConfig");
|
||||
Map<String, Object> imageConfig = structureMember(request, "ImageConfig");
|
||||
Map<String, Object> durableConfig = structureMember(request, "DurableConfig");
|
||||
Map<String, Object> code = structureMember(request, "Code");
|
||||
|
||||
String functionName = (String) request.get("FunctionName");
|
||||
@@ -413,7 +421,16 @@ public class LambdaService implements ResourceProvider {
|
||||
validateEnum(request.get("PackageType"), "packageType", List.of("Zip", "Image"));
|
||||
String packageType = request.getOrDefault("PackageType", "Zip").toString();
|
||||
String description = (String) request.get("Description");
|
||||
int timeout = toInt(request.get("Timeout"), config != null ? config.services().lambda().defaultTimeoutSeconds() : 3);
|
||||
DurableConfigRequest durable = durableConfigRequest(durableConfig);
|
||||
int defaultTimeout = config != null ? config.services().lambda().defaultTimeoutSeconds() : 3;
|
||||
if (durable != null) {
|
||||
if (durable.executionTimeout() == null) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"You cannot create a function with a durable configuration without an executionTimeout", 400);
|
||||
}
|
||||
defaultTimeout = Math.min(durable.executionTimeout(), MAX_FUNCTION_TIMEOUT_SECONDS);
|
||||
}
|
||||
int timeout = toInt(request.get("Timeout"), defaultTimeout);
|
||||
int memorySize = toInt(request.get("MemorySize"), config != null ? config.services().lambda().defaultMemoryMb() : 128);
|
||||
|
||||
if (functionName == null || functionName.isBlank()) {
|
||||
@@ -511,6 +528,7 @@ public class LambdaService implements ResourceProvider {
|
||||
}
|
||||
|
||||
applySnapStart(fn, snapStart);
|
||||
applyDurableConfig(fn, durable);
|
||||
applyLoggingConfig(fn, loggingConfig);
|
||||
|
||||
List<LambdaFileSystemConfig> fileSystemConfigs =
|
||||
@@ -772,6 +790,7 @@ public class LambdaService implements ResourceProvider {
|
||||
Map<String, Object> snapStart = structureMember(request, "SnapStart");
|
||||
Map<String, Object> loggingConfig = structureMember(request, "LoggingConfig");
|
||||
Map<String, Object> imageConfig = structureMember(request, "ImageConfig");
|
||||
Map<String, Object> durableConfig = structureMember(request, "DurableConfig");
|
||||
|
||||
// Validated before any field mutation below, not inline where Layers is applied further
|
||||
// down - fn is the live object backing this store entry (InMemoryStorage#get returns the
|
||||
@@ -798,8 +817,14 @@ public class LambdaService implements ResourceProvider {
|
||||
validateSnapStart(snapStart);
|
||||
}
|
||||
if (request.containsKey("LoggingConfig")) {
|
||||
validateLoggingConfig(loggingConfig);
|
||||
validateLoggingConfig(loggingConfig, fn.isDurable());
|
||||
}
|
||||
if (durableConfig != null && !fn.isDurable()) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"You cannot add a durable configuration to a function that was originally created "
|
||||
+ "with no durable configuration", 400);
|
||||
}
|
||||
DurableConfigRequest durable = durableConfigRequest(durableConfig);
|
||||
if (request.containsKey("Role")) {
|
||||
validateRoleArn((String) request.get("Role"));
|
||||
}
|
||||
@@ -904,6 +929,10 @@ public class LambdaService implements ResourceProvider {
|
||||
applyLoggingConfig(fn, loggingConfig);
|
||||
}
|
||||
|
||||
if (durable != null) {
|
||||
mergeDurableConfig(fn, durable);
|
||||
}
|
||||
|
||||
if (request.containsKey("FileSystemConfigs")) {
|
||||
fn.setFileSystemConfigs(requestedFileSystemConfigs);
|
||||
}
|
||||
@@ -2249,6 +2278,9 @@ public class LambdaService implements ResourceProvider {
|
||||
snapshot.setTracingMode(fn.getTracingMode());
|
||||
snapshot.setDeadLetterTargetArn(fn.getDeadLetterTargetArn());
|
||||
snapshot.setKmsKeyArn(fn.getKmsKeyArn());
|
||||
snapshot.setDurableExecutionTimeout(fn.getDurableExecutionTimeout());
|
||||
snapshot.setDurableRetentionPeriodInDays(fn.getDurableRetentionPeriodInDays());
|
||||
snapshot.setDurableKmsKeyArn(fn.getDurableKmsKeyArn());
|
||||
|
||||
functionStore.save(region, snapshot);
|
||||
LOG.infov("Published version {0} for function {1}", version, functionName);
|
||||
@@ -2443,10 +2475,75 @@ public class LambdaService implements ResourceProvider {
|
||||
fn.setSnapStartApplyOn(applyOn instanceof String s && !s.isBlank() ? s : "None");
|
||||
}
|
||||
|
||||
private static void validateLoggingConfig(Object value) {
|
||||
/** The members of a request's DurableConfig after type, range and pattern validation; each may be absent. */
|
||||
private record DurableConfigRequest(Integer executionTimeout, Integer retentionPeriodInDays, String kmsKeyArn) {
|
||||
}
|
||||
|
||||
private static DurableConfigRequest durableConfigRequest(Map<String, Object> durableConfig) {
|
||||
if (durableConfig == null) {
|
||||
return null;
|
||||
}
|
||||
Integer executionTimeout = durableConfigInteger(durableConfig.get("ExecutionTimeout"),
|
||||
"ExecutionTimeout", MAX_DURABLE_EXECUTION_TIMEOUT_SECONDS);
|
||||
Integer retentionPeriodInDays = durableConfigInteger(durableConfig.get("RetentionPeriodInDays"),
|
||||
"RetentionPeriodInDays", MAX_DURABLE_RETENTION_DAYS);
|
||||
Object kmsKeyArn = durableConfig.get("KMSKeyArn");
|
||||
if (kmsKeyArn != null && !(kmsKeyArn instanceof String)) {
|
||||
throw new AwsException("SerializationException", "DurableConfig.KMSKeyArn must be a string", 400);
|
||||
}
|
||||
validatePattern(kmsKeyArn, "durableConfig.kMSKeyArn", DURABLE_KMS_KEY_ARN_PATTERN);
|
||||
return new DurableConfigRequest(executionTimeout, retentionPeriodInDays, (String) kmsKeyArn);
|
||||
}
|
||||
|
||||
private static Integer durableConfigInteger(Object value, String member, int maximum) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (!(value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long)) {
|
||||
throw new AwsException("SerializationException", "DurableConfig." + member + " must be an integer", 400);
|
||||
}
|
||||
long number = ((Number) value).longValue();
|
||||
if (number < 1 || number > maximum) {
|
||||
String field = "durableConfig." + Character.toLowerCase(member.charAt(0)) + member.substring(1);
|
||||
String bound = number < 1 ? "greater than or equal to 1" : "less than or equal to " + maximum;
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value '" + number + "' at '" + field + "' failed to satisfy "
|
||||
+ "constraint: Member must have value " + bound, 400);
|
||||
}
|
||||
return (int) number;
|
||||
}
|
||||
|
||||
private static void applyDurableConfig(LambdaFunction fn, DurableConfigRequest durable) {
|
||||
if (durable == null) {
|
||||
return;
|
||||
}
|
||||
fn.setDurableRetentionPeriodInDays(DEFAULT_DURABLE_RETENTION_DAYS);
|
||||
mergeDurableConfig(fn, durable);
|
||||
fn.setLogFormat("JSON");
|
||||
}
|
||||
|
||||
/** UpdateFunctionConfiguration merges DurableConfig per member; a member left out keeps its stored value. */
|
||||
private static void mergeDurableConfig(LambdaFunction fn, DurableConfigRequest durable) {
|
||||
if (durable.executionTimeout() != null) {
|
||||
fn.setDurableExecutionTimeout(durable.executionTimeout());
|
||||
}
|
||||
if (durable.retentionPeriodInDays() != null) {
|
||||
fn.setDurableRetentionPeriodInDays(durable.retentionPeriodInDays());
|
||||
}
|
||||
if (durable.kmsKeyArn() != null) {
|
||||
fn.setDurableKmsKeyArn(durable.kmsKeyArn().isEmpty() ? null : durable.kmsKeyArn());
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateLoggingConfig(Object value, boolean durable) {
|
||||
if (!(value instanceof Map<?, ?> logging)) {
|
||||
return;
|
||||
}
|
||||
if (durable && "Text".equals(logging.get("LogFormat"))) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"You cannot use plain text logs with a durable function. Only JSON format logs are supported",
|
||||
400);
|
||||
}
|
||||
validateEnum(logging.get("LogFormat"), "loggingConfig.logFormat", List.of("JSON", "Text"));
|
||||
validateEnum(logging.get("ApplicationLogLevel"), "loggingConfig.applicationLogLevel",
|
||||
List.of("TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"));
|
||||
@@ -2614,8 +2711,9 @@ public class LambdaService implements ResourceProvider {
|
||||
if (!(value instanceof Map<?, ?> logging)) {
|
||||
return;
|
||||
}
|
||||
validateLoggingConfig(value);
|
||||
String format = logging.get("LogFormat") instanceof String f && !f.isBlank() ? f : "Text";
|
||||
validateLoggingConfig(value, fn.isDurable());
|
||||
String defaultFormat = fn.isDurable() ? "JSON" : "Text";
|
||||
String format = logging.get("LogFormat") instanceof String f && !f.isBlank() ? f : defaultFormat;
|
||||
boolean json = "JSON".equals(format);
|
||||
fn.setLogFormat(format);
|
||||
fn.setApplicationLogLevel(json && logging.get("ApplicationLogLevel") instanceof String level
|
||||
|
||||
@@ -69,6 +69,10 @@ public class LambdaFunction {
|
||||
private String logGroup;
|
||||
private List<LambdaFileSystemConfig> fileSystemConfigs = new ArrayList<>();
|
||||
private String codeSha256;
|
||||
/** Non-null only for durable functions: AWS requires ExecutionTimeout whenever DurableConfig is given. */
|
||||
private Integer durableExecutionTimeout;
|
||||
private Integer durableRetentionPeriodInDays;
|
||||
private String durableKmsKeyArn;
|
||||
|
||||
/** Non-null only for hot-reload functions. Holds the Docker-host path bind-mounted into /var/task. */
|
||||
private String hotReloadHostPath;
|
||||
@@ -215,6 +219,22 @@ public class LambdaFunction {
|
||||
public String getCodeSha256() { return codeSha256; }
|
||||
public void setCodeSha256(String codeSha256) { this.codeSha256 = codeSha256; }
|
||||
|
||||
public Integer getDurableExecutionTimeout() { return durableExecutionTimeout; }
|
||||
public void setDurableExecutionTimeout(Integer durableExecutionTimeout) {
|
||||
this.durableExecutionTimeout = durableExecutionTimeout;
|
||||
}
|
||||
|
||||
public Integer getDurableRetentionPeriodInDays() { return durableRetentionPeriodInDays; }
|
||||
public void setDurableRetentionPeriodInDays(Integer durableRetentionPeriodInDays) {
|
||||
this.durableRetentionPeriodInDays = durableRetentionPeriodInDays;
|
||||
}
|
||||
|
||||
public String getDurableKmsKeyArn() { return durableKmsKeyArn; }
|
||||
public void setDurableKmsKeyArn(String durableKmsKeyArn) { this.durableKmsKeyArn = durableKmsKeyArn; }
|
||||
|
||||
@JsonIgnore
|
||||
public boolean isDurable() { return durableExecutionTimeout != null; }
|
||||
|
||||
public String getHotReloadHostPath() { return hotReloadHostPath; }
|
||||
public void setHotReloadHostPath(String hotReloadHostPath) { this.hotReloadHostPath = hotReloadHostPath; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user