mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(lambda): persist version counters and event-invoke configs across restart (#1730)
* fix(lambda): persist version counters and event-invoke configs across restart versionCounters and eventInvokeConfigs were memory-only while the rest of Lambda state persists, so a restart in persistent mode let PublishVersion re-issue already-used version numbers and dropped PutFunctionEventInvokeConfig data. Wrap both maps through the existing 'lambda' storage key, restructure the counter merge into a synchronized get-increment-put, re-put updated event-invoke configs so mutations reach the backend, and add the LastModified deserialization counterpart so the timestamp survives reload. * fix(lambda): scope version-counter lock per key and align test storage with account prefixing PublishVersion no longer serializes on the service instance monitor: the counter increment locks per counter key, mirroring the existing concurrencyOpLocks pattern, so unrelated functions publish in parallel while same-function publishes still cannot issue duplicate numbers. The persistence test factory now wraps its InMemoryStorage in AccountAwareStorageBackend like the production StorageFactory, so the restart tests exercise the account-prefixed key space.
This commit is contained in:
1 parent
2a6cf1c147
commit
f8e8984c16
3 files changed
+190
-5
No files matched your search
@@ -1,9 +1,12 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackedMap;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.lambda.model.EventSourceMapping;
|
||||
import io.github.hectorvent.floci.services.lambda.model.FunctionEventInvokeConfig;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
@@ -61,8 +64,9 @@ public class LambdaService {
|
||||
private final SqsEventSourcePoller poller;
|
||||
private final KinesisEventSourcePoller kinesisPoller;
|
||||
private final DynamoDbStreamsEventSourcePoller dynamodbStreamsPoller;
|
||||
private final ConcurrentHashMap<String, Integer> versionCounters = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, FunctionEventInvokeConfig> eventInvokeConfigs = new ConcurrentHashMap<>();
|
||||
private final StorageFactory storageFactory;
|
||||
private Map<String, Integer> versionCounters = new ConcurrentHashMap<>();
|
||||
private Map<String, FunctionEventInvokeConfig> eventInvokeConfigs = new ConcurrentHashMap<>();
|
||||
/**
|
||||
* Per-function locks covering PutFunctionConcurrency,
|
||||
* DeleteFunctionConcurrency, and deleteFunction itself. Serializing the
|
||||
@@ -77,6 +81,7 @@ public class LambdaService {
|
||||
* emulator workload.
|
||||
*/
|
||||
private final ConcurrentHashMap<String, Object> concurrencyOpLocks = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Object> versionCounterLocks = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Package-private constructor for testing without CDI. Config defaults
|
||||
@@ -100,6 +105,17 @@ public class LambdaService {
|
||||
ZipExtractor zipExtractor,
|
||||
EmulatorConfig config,
|
||||
RegionResolver regionResolver) {
|
||||
this(functionStore, warmPool, codeStore, zipExtractor, config, regionResolver, null);
|
||||
}
|
||||
|
||||
/** Package-private constructor for persistence tests: supplies a real storage factory. */
|
||||
LambdaService(LambdaFunctionStore functionStore,
|
||||
WarmPool warmPool,
|
||||
CodeStore codeStore,
|
||||
ZipExtractor zipExtractor,
|
||||
EmulatorConfig config,
|
||||
RegionResolver regionResolver,
|
||||
StorageFactory storageFactory) {
|
||||
this.functionStore = functionStore;
|
||||
this.executorService = null;
|
||||
this.concurrencyLimiter = new LambdaConcurrencyLimiter();
|
||||
@@ -115,6 +131,7 @@ public class LambdaService {
|
||||
this.poller = null;
|
||||
this.kinesisPoller = null;
|
||||
this.dynamodbStreamsPoller = null;
|
||||
this.storageFactory = storageFactory;
|
||||
}
|
||||
|
||||
@Inject
|
||||
@@ -132,7 +149,8 @@ public class LambdaService {
|
||||
SqsService sqsService,
|
||||
SqsEventSourcePoller poller,
|
||||
KinesisEventSourcePoller kinesisPoller,
|
||||
DynamoDbStreamsEventSourcePoller dynamodbStreamsPoller) {
|
||||
DynamoDbStreamsEventSourcePoller dynamodbStreamsPoller,
|
||||
StorageFactory storageFactory) {
|
||||
this.functionStore = functionStore;
|
||||
this.executorService = executorService;
|
||||
this.concurrencyLimiter = concurrencyLimiter;
|
||||
@@ -148,6 +166,7 @@ public class LambdaService {
|
||||
this.poller = poller;
|
||||
this.kinesisPoller = kinesisPoller;
|
||||
this.dynamodbStreamsPoller = dynamodbStreamsPoller;
|
||||
this.storageFactory = storageFactory;
|
||||
}
|
||||
|
||||
/** Package-private accessor for tests that want to assert limiter state directly. */
|
||||
@@ -155,12 +174,33 @@ public class LambdaService {
|
||||
return concurrencyLimiter;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
initializeStorage();
|
||||
rehydrateConcurrency();
|
||||
}
|
||||
|
||||
/**
|
||||
* Version counters and event-invoke configs are durable Lambda state: a counter
|
||||
* lost on restart re-issues already-used version numbers from PublishVersion.
|
||||
* Wrapped through the same "lambda" storage key as the function store.
|
||||
*/
|
||||
void initializeStorage() {
|
||||
if (storageFactory == null) {
|
||||
return; // keeps non-CDI unit tests working
|
||||
}
|
||||
this.versionCounters = new StorageBackedMap<>(storageFactory.create("lambda",
|
||||
"lambda-version-counters.json", new TypeReference<Map<String, Integer>>() {}));
|
||||
this.eventInvokeConfigs = new StorageBackedMap<>(storageFactory.create("lambda",
|
||||
"lambda-event-invoke-configs.json",
|
||||
new TypeReference<Map<String, FunctionEventInvokeConfig>>() {}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Rehydrates reserved concurrency into the limiter from persisted function state.
|
||||
* Without this, restarts leave {@code totalReserved()=0} and allow validatePut /
|
||||
* unreserved-pool sizing to drift until each function is re-Put.
|
||||
*/
|
||||
@PostConstruct
|
||||
void rehydrateConcurrency() {
|
||||
if (concurrencyLimiter == null) {
|
||||
return;
|
||||
@@ -810,10 +850,25 @@ public class LambdaService {
|
||||
|
||||
// ──────────────────────────── Versions ────────────────────────────
|
||||
|
||||
/**
|
||||
* Locked get-increment-put: StorageBackedMap has no atomic merge, so the
|
||||
* sequence must not interleave or concurrent PublishVersion calls could issue
|
||||
* duplicate version numbers. The lock is per counter key (same pattern as
|
||||
* {@code concurrencyOpLocks}) so publishes of unrelated functions do not
|
||||
* serialize on the instance monitor for the storage round-trip.
|
||||
*/
|
||||
private int nextVersionNumber(String counterKey) {
|
||||
synchronized (versionCounterLocks.computeIfAbsent(counterKey, k -> new Object())) {
|
||||
int next = versionCounters.getOrDefault(counterKey, 0) + 1;
|
||||
versionCounters.put(counterKey, next);
|
||||
return next;
|
||||
}
|
||||
}
|
||||
|
||||
public LambdaFunction publishVersion(String region, String functionName, String description) {
|
||||
LambdaFunction fn = getFunction(region, functionName);
|
||||
functionName = fn.getFunctionName();
|
||||
int version = versionCounters.merge(region + "::" + functionName, 1, Integer::sum);
|
||||
int version = nextVersionNumber(region + "::" + functionName);
|
||||
LambdaFunction snapshot = new LambdaFunction();
|
||||
snapshot.setFunctionName(fn.getFunctionName());
|
||||
snapshot.setVersion(String.valueOf(version));
|
||||
@@ -1391,6 +1446,8 @@ public class LambdaService {
|
||||
}
|
||||
applyEventInvokeRequest(existing, request, false);
|
||||
existing.setLastModified(System.currentTimeMillis());
|
||||
// Re-put so StorageBackedMap routes the mutation through the backend
|
||||
eventInvokeConfigs.put(key, existing);
|
||||
return existing;
|
||||
}
|
||||
|
||||
|
||||
+7
@@ -33,6 +33,13 @@ public class FunctionEventInvokeConfig {
|
||||
return lastModifiedMillis / 1000.0;
|
||||
}
|
||||
|
||||
// Deserialization counterpart so the timestamp survives a persisted-state reload;
|
||||
// without it "LastModified" falls into ignoreUnknown and resets to 0.
|
||||
@JsonProperty("LastModified")
|
||||
void setLastModifiedSeconds(double seconds) {
|
||||
this.lastModifiedMillis = Math.round(seconds * 1000);
|
||||
}
|
||||
|
||||
public String getFunctionArn() { return functionArn; }
|
||||
public void setFunctionArn(String functionArn) { this.functionArn = functionArn; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user