diff --git a/README.md b/README.md index a33333420e..e987d4f1ed 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ All settings are overridable via environment variables (`FLOCI_` prefix). | `FLOCI_DEFAULT_ACCOUNT_ID` | `000000000000` | Default AWS account ID | | `FLOCI_BASE_URL` | `http://localhost:4566` | Base URL used in API responses (e.g. SQS QueueUrl) | | `FLOCI_HOSTNAME` | *(unset)* | Override hostname in response URLs (for Docker Compose) | -| `FLOCI_STORAGE_MODE` | `hybrid` | `memory` · `persistent` · `hybrid` · `wal` | +| `FLOCI_STORAGE_MODE` | `memory` | `memory` · `persistent` · `hybrid` · `wal` | | `FLOCI_STORAGE_PERSISTENT_PATH` | `./data` | Data directory | → Full reference: [configuration docs](https://hectorvent.dev/floci/configuration/application-yml/) diff --git a/docs/configuration/application-yml.md b/docs/configuration/application-yml.md index 2885f12c77..0cd9f58453 100644 --- a/docs/configuration/application-yml.md +++ b/docs/configuration/application-yml.md @@ -21,29 +21,18 @@ floci: compaction-interval-ms: 30000 services: ssm: - mode: memory flush-interval-ms: 5000 - sqs: - mode: memory - s3: - mode: memory dynamodb: - mode: memory flush-interval-ms: 5000 sns: - mode: memory flush-interval-ms: 5000 lambda: - mode: memory flush-interval-ms: 5000 cloudwatchlogs: - mode: memory flush-interval-ms: 5000 cloudwatchmetrics: - mode: memory flush-interval-ms: 5000 secretsmanager: - mode: memory flush-interval-ms: 5000 services: diff --git a/docs/configuration/storage.md b/docs/configuration/storage.md index ba3755569e..8b9a54c8f4 100644 --- a/docs/configuration/storage.md +++ b/docs/configuration/storage.md @@ -24,54 +24,44 @@ floci: ## Per-Service Override +When `mode` is omitted for a service, it inherits the global `storage.mode`. Only set a per-service mode when you need a different behaviour for that service. + ```yaml title="application.yml" floci: storage: - mode: memory + mode: memory # default for all services services: dynamodb: - mode: persistent + mode: persistent # DynamoDB uses persistent; everything else uses memory flush-interval-ms: 5000 s3: - mode: hybrid - sqs: - mode: memory + mode: hybrid # S3 uses hybrid; everything else uses memory ``` ## Per-Service Storage Overrides -Override the global mode for individual services via environment variables: +Override the global mode for individual services via environment variables. When not set, the service inherits `FLOCI_STORAGE_MODE`. -| Variable | Default | Description | -|-----------------------------------------------------------------|----------|----------------------------------------| -| `FLOCI_STORAGE_SERVICES_SSM_MODE` | `memory` | SSM storage mode | -| `FLOCI_STORAGE_SERVICES_SSM_FLUSH_INTERVAL_MS` | `5000` | SSM flush interval (ms) | -| `FLOCI_STORAGE_SERVICES_SQS_MODE` | `memory` | SQS storage mode | -| `FLOCI_STORAGE_SERVICES_SQS_PERSIST_ON_SHUTDOWN` | `true` | Flush SQS messages to disk on shutdown | -| `FLOCI_STORAGE_SERVICES_S3_MODE` | `hybrid` | S3 storage mode | -| `FLOCI_STORAGE_SERVICES_S3_CACHE_SIZE_MB` | `100` | S3 in-memory cache size (MB) | -| `FLOCI_STORAGE_SERVICES_DYNAMODB_MODE` | `memory` | DynamoDB storage mode | -| `FLOCI_STORAGE_SERVICES_DYNAMODB_FLUSH_INTERVAL_MS` | `5000` | DynamoDB flush interval (ms) | -| `FLOCI_STORAGE_SERVICES_SNS_MODE` | `memory` | SNS storage mode | -| `FLOCI_STORAGE_SERVICES_SNS_FLUSH_INTERVAL_MS` | `5000` | SNS flush interval (ms) | -| `FLOCI_STORAGE_SERVICES_LAMBDA_MODE` | `memory` | Lambda storage mode | -| `FLOCI_STORAGE_SERVICES_LAMBDA_FLUSH_INTERVAL_MS` | `5000` | Lambda flush interval (ms) | -| `FLOCI_STORAGE_SERVICES_APIGATEWAY_MODE` | `memory` | API Gateway (v1) storage mode | -| `FLOCI_STORAGE_SERVICES_APIGATEWAY_FLUSH_INTERVAL_MS` | `5000` | API Gateway (v1) flush interval (ms) | -| `FLOCI_STORAGE_SERVICES_APIGATEWAYV2_MODE` | `memory` | API Gateway (v2) storage mode | -| `FLOCI_STORAGE_SERVICES_APIGATEWAYV2_FLUSH_INTERVAL_MS` | `5000` | API Gateway (v2) flush interval (ms) | -| `FLOCI_STORAGE_SERVICES_IAM_MODE` | `memory` | IAM storage mode | -| `FLOCI_STORAGE_SERVICES_IAM_FLUSH_INTERVAL_MS` | `5000` | IAM flush interval (ms) | -| `FLOCI_STORAGE_SERVICES_RDS_MODE` | `memory` | RDS storage mode | -| `FLOCI_STORAGE_SERVICES_RDS_FLUSH_INTERVAL_MS` | `5000` | RDS flush interval (ms) | -| `FLOCI_STORAGE_SERVICES_EVENTBRIDGE_MODE` | `memory` | EventBridge storage mode | -| `FLOCI_STORAGE_SERVICES_EVENTBRIDGE_FLUSH_INTERVAL_MS` | `5000` | EventBridge flush interval (ms) | -| `FLOCI_STORAGE_SERVICES_CLOUDWATCHLOGS_MODE` | `memory` | CloudWatch Logs storage mode | -| `FLOCI_STORAGE_SERVICES_CLOUDWATCHLOGS_FLUSH_INTERVAL_MS` | `5000` | CloudWatch Logs flush interval (ms) | -| `FLOCI_STORAGE_SERVICES_CLOUDWATCHMETRICS_MODE` | `memory` | CloudWatch Metrics storage mode | -| `FLOCI_STORAGE_SERVICES_CLOUDWATCHMETRICS_FLUSH_INTERVAL_MS` | `5000` | CloudWatch Metrics flush interval (ms) | -| `FLOCI_STORAGE_SERVICES_SECRETSMANAGER_MODE` | `memory` | Secrets Manager storage mode | -| `FLOCI_STORAGE_SERVICES_SECRETSMANAGER_FLUSH_INTERVAL_MS` | `5000` | Secrets Manager flush interval (ms) | +| Variable | Default | Description | +|-----------------------------------------------------------------|----------------|----------------------------------------| +| `FLOCI_STORAGE_SERVICES_SSM_MODE` | global default | SSM storage mode | +| `FLOCI_STORAGE_SERVICES_SSM_FLUSH_INTERVAL_MS` | `5000` | SSM flush interval (ms) | +| `FLOCI_STORAGE_SERVICES_SQS_MODE` | global default | SQS storage mode | +| `FLOCI_STORAGE_SERVICES_S3_MODE` | global default | S3 storage mode | +| `FLOCI_STORAGE_SERVICES_DYNAMODB_MODE` | global default | DynamoDB storage mode | +| `FLOCI_STORAGE_SERVICES_DYNAMODB_FLUSH_INTERVAL_MS` | `5000` | DynamoDB flush interval (ms) | +| `FLOCI_STORAGE_SERVICES_SNS_MODE` | global default | SNS storage mode | +| `FLOCI_STORAGE_SERVICES_SNS_FLUSH_INTERVAL_MS` | `5000` | SNS flush interval (ms) | +| `FLOCI_STORAGE_SERVICES_LAMBDA_MODE` | global default | Lambda storage mode | +| `FLOCI_STORAGE_SERVICES_LAMBDA_FLUSH_INTERVAL_MS` | `5000` | Lambda flush interval (ms) | +| `FLOCI_STORAGE_SERVICES_CLOUDWATCHLOGS_MODE` | global default | CloudWatch Logs storage mode | +| `FLOCI_STORAGE_SERVICES_CLOUDWATCHLOGS_FLUSH_INTERVAL_MS` | `5000` | CloudWatch Logs flush interval (ms) | +| `FLOCI_STORAGE_SERVICES_CLOUDWATCHMETRICS_MODE` | global default | CloudWatch Metrics storage mode | +| `FLOCI_STORAGE_SERVICES_CLOUDWATCHMETRICS_FLUSH_INTERVAL_MS` | `5000` | CloudWatch Metrics flush interval (ms) | +| `FLOCI_STORAGE_SERVICES_SECRETSMANAGER_MODE` | global default | Secrets Manager storage mode | +| `FLOCI_STORAGE_SERVICES_SECRETSMANAGER_FLUSH_INTERVAL_MS` | `5000` | Secrets Manager flush interval (ms) | +| `FLOCI_STORAGE_SERVICES_ACM_MODE` | global default | ACM storage mode | +| `FLOCI_STORAGE_SERVICES_ACM_FLUSH_INTERVAL_MS` | `5000` | ACM flush interval (ms) | ## Environment Variable Override diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java index 88c27d1b4b..9dad2e424e 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -70,74 +70,64 @@ public interface EmulatorConfig { } interface SsmStorageConfig { - @WithDefault("memory") - String mode(); + Optional mode(); @WithDefault("5000") long flushIntervalMs(); } interface SqsStorageConfig { - @WithDefault("memory") - String mode(); + Optional mode(); } interface S3StorageConfig { - @WithDefault("hybrid") - String mode(); + Optional mode(); } interface DynamoDbStorageConfig { - @WithDefault("memory") - String mode(); + Optional mode(); @WithDefault("5000") long flushIntervalMs(); } interface SnsStorageConfig { - @WithDefault("memory") - String mode(); + Optional mode(); @WithDefault("5000") long flushIntervalMs(); } interface LambdaStorageConfig { - @WithDefault("memory") - String mode(); + Optional mode(); @WithDefault("5000") long flushIntervalMs(); } interface CloudWatchLogsStorageConfig { - @WithDefault("memory") - String mode(); + Optional mode(); @WithDefault("5000") long flushIntervalMs(); } interface CloudWatchMetricsStorageConfig { - @WithDefault("memory") - String mode(); + Optional mode(); @WithDefault("5000") long flushIntervalMs(); } interface SecretsManagerStorageConfig { - @WithDefault("memory") - String mode(); + Optional mode(); @WithDefault("5000") long flushIntervalMs(); } interface AcmStorageConfig { - @WithDefault("memory") - String mode(); + Optional mode(); @WithDefault("5000") long flushIntervalMs(); diff --git a/src/main/java/io/github/hectorvent/floci/core/storage/StorageFactory.java b/src/main/java/io/github/hectorvent/floci/core/storage/StorageFactory.java index 1c217f4bbb..c245e3eb01 100644 --- a/src/main/java/io/github/hectorvent/floci/core/storage/StorageFactory.java +++ b/src/main/java/io/github/hectorvent/floci/core/storage/StorageFactory.java @@ -98,17 +98,18 @@ public class StorageFactory { } private String resolveMode(String serviceName) { + String globalMode = config.storage().mode(); return switch (serviceName) { - case "ssm" -> config.storage().services().ssm().mode(); - case "sqs" -> config.storage().services().sqs().mode(); - case "s3" -> config.storage().services().s3().mode(); - case "dynamodb" -> config.storage().services().dynamodb().mode(); - case "sns" -> config.storage().services().sns().mode(); - case "lambda" -> config.storage().services().lambda().mode(); - case "cloudwatchlogs" -> config.storage().services().cloudwatchlogs().mode(); - case "cloudwatchmetrics" -> config.storage().services().cloudwatchmetrics().mode(); - case "secretsmanager" -> config.storage().services().secretsmanager().mode(); - default -> config.storage().mode(); + case "ssm" -> config.storage().services().ssm().mode().orElse(globalMode); + case "sqs" -> config.storage().services().sqs().mode().orElse(globalMode); + case "s3" -> config.storage().services().s3().mode().orElse(globalMode); + case "dynamodb" -> config.storage().services().dynamodb().mode().orElse(globalMode); + case "sns" -> config.storage().services().sns().mode().orElse(globalMode); + case "lambda" -> config.storage().services().lambda().mode().orElse(globalMode); + case "cloudwatchlogs" -> config.storage().services().cloudwatchlogs().mode().orElse(globalMode); + case "cloudwatchmetrics" -> config.storage().services().cloudwatchmetrics().mode().orElse(globalMode); + case "secretsmanager" -> config.storage().services().secretsmanager().mode().orElse(globalMode); + default -> globalMode; }; } diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java index c58bd398bb..6a80b1838b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java @@ -37,6 +37,9 @@ public class S3Service { private final StorageBackend bucketStore; private final StorageBackend objectStore; private final Path dataRoot; + private final boolean inMemory; + private final ConcurrentHashMap memoryDataStore = new ConcurrentHashMap<>(); + private final ConcurrentHashMap> memoryMultipartStore = new ConcurrentHashMap<>(); private final ConcurrentHashMap multipartUploads = new ConcurrentHashMap<>(); private final SqsService sqsService; @@ -57,6 +60,7 @@ public class S3Service { new TypeReference>() { }), Path.of(config.storage().persistentPath()).resolve("s3"), + "memory".equals(config.storage().services().s3().mode().orElse(config.storage().mode())), sqsService, snsService, regionResolver, config.effectiveBaseUrl(), objectMapper ); } @@ -67,26 +71,29 @@ public class S3Service { S3Service(StorageBackend bucketStore, StorageBackend objectStore, Path dataRoot) { - this(bucketStore, objectStore, dataRoot, null, null, null, "http://localhost:4566", + this(bucketStore, objectStore, dataRoot, true, null, null, null, "http://localhost:4566", new ObjectMapper()); } private S3Service(StorageBackend bucketStore, StorageBackend objectStore, - Path dataRoot, SqsService sqsService, SnsService snsService, + Path dataRoot, boolean inMemory, SqsService sqsService, SnsService snsService, RegionResolver regionResolver, String baseUrl, ObjectMapper objectMapper) { this.bucketStore = bucketStore; this.objectStore = objectStore; this.dataRoot = dataRoot; + this.inMemory = inMemory; this.sqsService = sqsService; this.snsService = snsService; this.regionResolver = regionResolver; this.baseUrl = baseUrl; this.objectMapper = objectMapper; - try { - Files.createDirectories(dataRoot); - } catch (IOException e) { - throw new UncheckedIOException("Failed to create S3 data directory: " + dataRoot, e); + if (!inMemory) { + try { + Files.createDirectories(dataRoot); + } catch (IOException e) { + throw new UncheckedIOException("Failed to create S3 data directory: " + dataRoot, e); + } } } @@ -115,7 +122,12 @@ public class S3Service { } bucketStore.delete(bucketName); - deleteDirectory(dataRoot.resolve(bucketName)); + if (inMemory) { + String prefix = bucketName + "/"; + memoryDataStore.keySet().removeIf(k -> k.startsWith(prefix)); + } else { + deleteDirectory(dataRoot.resolve(bucketName)); + } LOG.infov("Deleted bucket: {0}", bucketName); } @@ -729,10 +741,14 @@ public class S3Service { } upload.setStorageClass(ObjectAttributeName.normalizeStorageClass(storageClass)); - try { - Files.createDirectories(dataRoot.resolve(".multipart").resolve(upload.getUploadId())); - } catch (IOException e) { - throw new UncheckedIOException("Failed to create multipart temp directory", e); + if (inMemory) { + memoryMultipartStore.put(upload.getUploadId(), new ConcurrentHashMap<>()); + } else { + try { + Files.createDirectories(dataRoot.resolve(".multipart").resolve(upload.getUploadId())); + } catch (IOException e) { + throw new UncheckedIOException("Failed to create multipart temp directory", e); + } } multipartUploads.put(upload.getUploadId(), upload); @@ -751,12 +767,15 @@ public class S3Service { "Part number must be between 1 and 10000.", 400); } - // Write part to temp directory - Path partPath = dataRoot.resolve(".multipart").resolve(uploadId).resolve(String.valueOf(partNumber)); - try { - Files.write(partPath, data); - } catch (IOException e) { - throw new UncheckedIOException("Failed to write multipart part", e); + if (inMemory) { + memoryMultipartStore.get(uploadId).put(partNumber, data); + } else { + Path partPath = dataRoot.resolve(".multipart").resolve(uploadId).resolve(String.valueOf(partNumber)); + try { + Files.write(partPath, data); + } catch (IOException e) { + throw new UncheckedIOException("Failed to write multipart part", e); + } } String eTag = computeETag(data); @@ -788,8 +807,9 @@ public class S3Service { MessageDigest md = MessageDigest.getInstance("MD5"); for (int num : partNumbers) { - Path partPath = dataRoot.resolve(".multipart").resolve(uploadId).resolve(String.valueOf(num)); - byte[] partData = Files.readAllBytes(partPath); + byte[] partData = inMemory + ? memoryMultipartStore.get(uploadId).get(num) + : Files.readAllBytes(dataRoot.resolve(".multipart").resolve(uploadId).resolve(String.valueOf(num))); combined.write(partData); // For composite ETag: hash each part's MD5 md.update(computeETagBytes(partData)); @@ -1101,7 +1121,11 @@ public class S3Service { private void cleanupMultipart(String uploadId) { multipartUploads.remove(uploadId); - deleteDirectory(dataRoot.resolve(".multipart").resolve(uploadId)); + if (inMemory) { + memoryMultipartStore.remove(uploadId); + } else { + deleteDirectory(dataRoot.resolve(".multipart").resolve(uploadId)); + } } private static S3Checksum buildChecksum(byte[] data, List parts, boolean multipartUpload) { @@ -1216,6 +1240,10 @@ public class S3Service { } private void writeVersionedFile(String bucketName, String key, String versionId, byte[] data) { + if (inMemory) { + memoryDataStore.put(versionedKey(bucketName, key, versionId), data); + return; + } try { Path filePath = resolveVersionedPath(bucketName, key, versionId); Files.createDirectories(filePath.getParent()); @@ -1226,6 +1254,9 @@ public class S3Service { } private byte[] readVersionedFile(String bucketName, String key, String versionId) { + if (inMemory) { + return memoryDataStore.get(versionedKey(bucketName, key, versionId)); + } try { return Files.readAllBytes(resolveVersionedPath(bucketName, key, versionId)); } catch (IOException e) { @@ -1234,6 +1265,10 @@ public class S3Service { } private void writeFile(String bucketName, String key, byte[] data) { + if (inMemory) { + memoryDataStore.put(objectKey(bucketName, key), data); + return; + } try { Path filePath = resolveObjectPath(bucketName, key); Files.createDirectories(filePath.getParent()); @@ -1244,6 +1279,9 @@ public class S3Service { } private byte[] readFile(String bucketName, String key) { + if (inMemory) { + return memoryDataStore.get(objectKey(bucketName, key)); + } try { return Files.readAllBytes(resolveObjectPath(bucketName, key)); } catch (IOException e) { @@ -1252,6 +1290,10 @@ public class S3Service { } private void deleteFile(String bucketName, String key) { + if (inMemory) { + memoryDataStore.remove(objectKey(bucketName, key)); + return; + } try { Files.deleteIfExists(resolveObjectPath(bucketName, key)); } catch (IOException e) { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 2954972dc8..dc4c78b6c7 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -29,32 +29,20 @@ floci: compaction-interval-ms: 30000 services: ssm: - mode: memory flush-interval-ms: 5000 - sqs: - mode: memory - s3: - mode: memory dynamodb: - mode: memory flush-interval-ms: 5000 sns: - mode: memory flush-interval-ms: 5000 lambda: - mode: memory flush-interval-ms: 5000 cloudwatchlogs: - mode: memory flush-interval-ms: 5000 cloudwatchmetrics: - mode: memory flush-interval-ms: 5000 secretsmanager: - mode: memory flush-interval-ms: 5000 acm: - mode: memory flush-interval-ms: 5000 auth: diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index b445106642..59467c3d04 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -14,29 +14,18 @@ floci: compaction-interval-ms: 60000 services: ssm: - mode: memory flush-interval-ms: 60000 - sqs: - mode: memory - s3: - mode: memory dynamodb: - mode: memory flush-interval-ms: 60000 sns: - mode: memory flush-interval-ms: 60000 lambda: - mode: memory flush-interval-ms: 60000 cloudwatchlogs: - mode: memory flush-interval-ms: 60000 cloudwatchmetrics: - mode: memory flush-interval-ms: 60000 secretsmanager: - mode: memory flush-interval-ms: 60000 auth: