mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(lambda): bind-mount hot-reload via S3Bucket=hot-reload (#601)
* feat(lambda): add bind-mount hot-reload mode via S3Bucket=hot-reload Functions created with S3Bucket=hot-reload and S3Key=/absolute/path have their host directory bind-mounted into /var/task rather than packaged as a ZIP. Each invocation runs an ephemeral container so file changes on disk are picked up immediately without calling UpdateFunctionCode or re-uploading code. Feature is disabled by default; opt in with: FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ENABLED=true An optional path allowlist (FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS) restricts which host directories may be bind-mounted. * fix: missing hot reload enable env
This commit is contained in:
1 parent
7dbd0225d4
commit
404b42bdc0
14 files changed
+462
-14
No files matched your search
@@ -632,6 +632,31 @@ public interface EmulatorConfig {
|
||||
*/
|
||||
@WithDefault("100")
|
||||
int unreservedConcurrencyMin();
|
||||
|
||||
HotReload hotReload();
|
||||
|
||||
interface HotReload {
|
||||
/**
|
||||
* When true, the magic bucket name {@code hot-reload} triggers a bind-mount of the
|
||||
* S3Key path (a Docker-host absolute path) into the Lambda container instead of
|
||||
* extracting a ZIP. Changes on disk are visible on the next invocation without
|
||||
* re-deploying. Disabled by default — when false, {@code hot-reload} is an
|
||||
* ordinary (non-existent) bucket and returns NoSuchBucket as usual.
|
||||
*
|
||||
* Env var: FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ENABLED
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean enabled();
|
||||
|
||||
/**
|
||||
* Optional allow-list of absolute path prefixes. When non-empty, the S3Key supplied
|
||||
* to a hot-reload CreateFunction/UpdateFunctionCode must start with one of these
|
||||
* prefixes. Empty = all absolute paths are accepted.
|
||||
*
|
||||
* Env var: FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS
|
||||
*/
|
||||
Optional<List<String>> allowedPaths();
|
||||
}
|
||||
}
|
||||
|
||||
interface Ec2ServiceConfig {
|
||||
|
||||
@@ -88,13 +88,23 @@ public class LambdaService {
|
||||
CodeStore codeStore,
|
||||
ZipExtractor zipExtractor,
|
||||
RegionResolver regionResolver) {
|
||||
this(functionStore, warmPool, codeStore, zipExtractor, null, regionResolver);
|
||||
}
|
||||
|
||||
/** Package-private constructor for testing with a supplied config (e.g. for hot-reload tests). */
|
||||
LambdaService(LambdaFunctionStore functionStore,
|
||||
WarmPool warmPool,
|
||||
CodeStore codeStore,
|
||||
ZipExtractor zipExtractor,
|
||||
EmulatorConfig config,
|
||||
RegionResolver regionResolver) {
|
||||
this.functionStore = functionStore;
|
||||
this.executorService = null;
|
||||
this.concurrencyLimiter = new LambdaConcurrencyLimiter();
|
||||
this.warmPool = warmPool;
|
||||
this.codeStore = codeStore;
|
||||
this.zipExtractor = zipExtractor;
|
||||
this.config = null;
|
||||
this.config = config;
|
||||
this.regionResolver = regionResolver;
|
||||
this.esmStore = null;
|
||||
this.aliasStore = null;
|
||||
@@ -291,7 +301,11 @@ public class LambdaService {
|
||||
String s3Bucket = (String) code.get("S3Bucket");
|
||||
String s3Key = (String) code.get("S3Key");
|
||||
if (s3Bucket != null && s3Key != null) {
|
||||
extractZipCodeFromS3(fn, s3Bucket, s3Key);
|
||||
if ("hot-reload".equals(s3Bucket)) {
|
||||
applyHotReload(fn, s3Key);
|
||||
} else {
|
||||
extractZipCodeFromS3(fn, s3Bucket, s3Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -358,7 +372,11 @@ public class LambdaService {
|
||||
fn.setImageUri(imageUri);
|
||||
}
|
||||
if (s3Bucket != null && s3Key != null) {
|
||||
extractZipCodeFromS3(fn, s3Bucket, s3Key);
|
||||
if ("hot-reload".equals(s3Bucket)) {
|
||||
applyHotReload(fn, s3Key);
|
||||
} else {
|
||||
extractZipCodeFromS3(fn, s3Bucket, s3Key);
|
||||
}
|
||||
}
|
||||
|
||||
fn.setLastModified(System.currentTimeMillis());
|
||||
@@ -1090,6 +1108,30 @@ public class LambdaService {
|
||||
return modulePath;
|
||||
}
|
||||
|
||||
private void applyHotReload(LambdaFunction fn, String hostPath) {
|
||||
if (config == null || !config.services().lambda().hotReload().enabled()) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Hot-reload is disabled. Set FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ENABLED=true to enable it.", 400);
|
||||
}
|
||||
if (hostPath == null || !hostPath.startsWith("/")) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Hot-reload S3Key must be an absolute path on the Docker host, got: " + hostPath, 400);
|
||||
}
|
||||
config.services().lambda().hotReload().allowedPaths().ifPresent(allowed -> {
|
||||
if (allowed.stream().noneMatch(hostPath::startsWith)) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Path '" + hostPath + "' is not under an allowed hot-reload mount prefix.", 400);
|
||||
}
|
||||
});
|
||||
fn.setHotReloadHostPath(hostPath);
|
||||
fn.setCodeLocalPath(null);
|
||||
fn.setS3Bucket(null);
|
||||
fn.setS3Key(null);
|
||||
fn.setCodeSizeBytes(0);
|
||||
fn.setCodeSha256("");
|
||||
LOG.infov("Hot-reload configured for function {0}: bind-mounting {1}", fn.getFunctionName(), hostPath);
|
||||
}
|
||||
|
||||
// ──────────────────────────── Permissions (Policy) ────────────────────────────
|
||||
|
||||
public Map<String, Object> addPermission(String region, String functionName, Map<String, Object> request) {
|
||||
@@ -1228,6 +1270,9 @@ public class LambdaService {
|
||||
String region = regionResolver.getDefaultRegion();
|
||||
List<LambdaFunction> functions = functionStore.list(region);
|
||||
for (LambdaFunction fn : functions) {
|
||||
if (fn.isHotReload()) {
|
||||
continue;
|
||||
}
|
||||
if (event.bucketName().equals(fn.getS3Bucket()) && event.key().equals(fn.getS3Key())) {
|
||||
LOG.infov("Reactive S3 Sync: updating function {0} from s3://{1}/{2}",
|
||||
fn.getFunctionName(), event.bucketName(), event.key());
|
||||
|
||||
@@ -126,8 +126,9 @@ public class WarmPool {
|
||||
*/
|
||||
public void release(ContainerHandle handle) {
|
||||
boolean ephemeral = config != null && config.services().lambda().ephemeral();
|
||||
if (ephemeral) {
|
||||
LOG.debugv("Ephemeral: stopping container {0} after invocation", handle.getContainerId());
|
||||
if (ephemeral || handle.isHotReload()) {
|
||||
LOG.debugv("{0}: stopping container {1} after invocation",
|
||||
handle.isHotReload() ? "Hot-reload" : "Ephemeral", handle.getContainerId());
|
||||
stopQuietly(handle);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -14,22 +14,30 @@ public class ContainerHandle {
|
||||
private final String functionName;
|
||||
private final RuntimeApiServer runtimeApiServer;
|
||||
private final long createdAt;
|
||||
private final boolean hotReload;
|
||||
private volatile ContainerState state;
|
||||
private volatile long lastUsedMs;
|
||||
private Closeable logStream;
|
||||
|
||||
public ContainerHandle(String containerId, String functionName,
|
||||
RuntimeApiServer runtimeApiServer, ContainerState state) {
|
||||
this(containerId, functionName, runtimeApiServer, state, false);
|
||||
}
|
||||
|
||||
public ContainerHandle(String containerId, String functionName,
|
||||
RuntimeApiServer runtimeApiServer, ContainerState state, boolean hotReload) {
|
||||
this.containerId = containerId;
|
||||
this.functionName = functionName;
|
||||
this.runtimeApiServer = runtimeApiServer;
|
||||
this.state = state;
|
||||
this.hotReload = hotReload;
|
||||
this.createdAt = System.currentTimeMillis();
|
||||
this.lastUsedMs = this.createdAt;
|
||||
}
|
||||
|
||||
public String getContainerId() { return containerId; }
|
||||
public String getFunctionName() { return functionName; }
|
||||
public boolean isHotReload() { return hotReload; }
|
||||
public RuntimeApiServer getRuntimeApiServer() { return runtimeApiServer; }
|
||||
public long getCreatedAt() { return createdAt; }
|
||||
public long getLastUsedMs() { return lastUsedMs; }
|
||||
|
||||
+17
-9
@@ -105,12 +105,15 @@ public class ContainerLauncher {
|
||||
LOG.infov("Launching container for function: {0}", fn.getFunctionName());
|
||||
|
||||
// For Zip functions, verify code exists before allocating any resources.
|
||||
if (fn.getCodeLocalPath() != null) {
|
||||
Path codePath = Path.of(fn.getCodeLocalPath());
|
||||
if (!Files.exists(codePath)) {
|
||||
throw new RuntimeException("Code directory not found for function '"
|
||||
+ fn.getFunctionName() + "': " + fn.getCodeLocalPath()
|
||||
+ " (function may have been deleted or updated)");
|
||||
// Hot-reload functions use a bind-mount; the Docker daemon validates the path at start.
|
||||
if (!fn.isHotReload()) {
|
||||
if (fn.getCodeLocalPath() != null) {
|
||||
Path codePath = Path.of(fn.getCodeLocalPath());
|
||||
if (!Files.exists(codePath)) {
|
||||
throw new RuntimeException("Code directory not found for function '"
|
||||
+ fn.getFunctionName() + "': " + fn.getCodeLocalPath()
|
||||
+ " (function may have been deleted or updated)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,6 +162,10 @@ public class ContainerLauncher {
|
||||
|
||||
specBuilder.withEmbeddedDns();
|
||||
|
||||
if (fn.isHotReload()) {
|
||||
specBuilder.withBind(fn.getHotReloadHostPath(), TASK_DIR);
|
||||
}
|
||||
|
||||
// For Image package type without an explicit handler, omit CMD so the image's own CMD is used
|
||||
if (fn.getHandler() != null && !fn.getHandler().isBlank()) {
|
||||
specBuilder.withCmd(fn.getHandler());
|
||||
@@ -171,9 +178,10 @@ public class ContainerLauncher {
|
||||
String containerId = lifecycleManager.create(spec);
|
||||
LOG.infov("Created container {0} for function {1}", containerId, fn.getFunctionName());
|
||||
|
||||
// Copy code into container via Docker API tar stream (works inside Docker too)
|
||||
// Copy code into container via Docker API tar stream (works inside Docker too).
|
||||
// Hot-reload functions skip the tar-copy — the bind-mount already wires the host path.
|
||||
DockerClient dockerClient = lifecycleManager.getDockerClient();
|
||||
if (fn.getCodeLocalPath() != null) {
|
||||
if (!fn.isHotReload() && fn.getCodeLocalPath() != null) {
|
||||
Path codePath = Path.of(fn.getCodeLocalPath());
|
||||
|
||||
// 1. Always copy all code to /var/task (TASK_DIR)
|
||||
@@ -194,7 +202,7 @@ public class ContainerLauncher {
|
||||
// Now start the container with code in place
|
||||
lifecycleManager.startCreated(containerId, spec);
|
||||
|
||||
ContainerHandle handle = new ContainerHandle(containerId, fn.getFunctionName(), runtimeApiServer, ContainerState.WARM);
|
||||
ContainerHandle handle = new ContainerHandle(containerId, fn.getFunctionName(), runtimeApiServer, ContainerState.WARM, fn.isHotReload());
|
||||
|
||||
// Determine CloudWatch Logs destination for this container instance
|
||||
String cwLogGroup = "/aws/lambda/" + fn.getFunctionName();
|
||||
|
||||
@@ -47,6 +47,9 @@ public class LambdaFunction {
|
||||
private Map<String, Object> vpcConfig;
|
||||
private String codeSha256;
|
||||
|
||||
/** Non-null only for hot-reload functions. Holds the Docker-host path bind-mounted into /var/task. */
|
||||
private String hotReloadHostPath;
|
||||
|
||||
@JsonIgnore
|
||||
private volatile ContainerState containerState = ContainerState.COLD;
|
||||
|
||||
@@ -152,6 +155,12 @@ public class LambdaFunction {
|
||||
public String getCodeSha256() { return codeSha256; }
|
||||
public void setCodeSha256(String codeSha256) { this.codeSha256 = codeSha256; }
|
||||
|
||||
public String getHotReloadHostPath() { return hotReloadHostPath; }
|
||||
public void setHotReloadHostPath(String hotReloadHostPath) { this.hotReloadHostPath = hotReloadHostPath; }
|
||||
|
||||
@JsonIgnore
|
||||
public boolean isHotReload() { return hotReloadHostPath != null; }
|
||||
|
||||
@JsonIgnore
|
||||
public ContainerState getContainerState() { return containerState; }
|
||||
@JsonIgnore
|
||||
|
||||
Reference in new issue
Block a user