diff --git a/README.md b/README.md index f4cb2cd47a..d498ad162e 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ Run AWS-compatible services locally without an AWS account, auth token, or paid
Real Docker where fidelity matters -Lambda, RDS, Neptune, ElastiCache, MSK, ECS, EC2, EKS, OpenSearch, and CodeBuild use real Docker-backed execution instead of shallow mocks. +Lambda, RDS, Neptune, ElastiCache, MSK, ECS, EC2, EKS, OpenSearch, CodeBuild, and Managed Service for Apache Flink use real Docker-backed execution instead of shallow mocks.
@@ -198,7 +198,7 @@ flowchart LR end subgraph Containers ["Container Services"] - C["Lambda\nElastiCache\nRDS\nNeptune\nECS\nEC2\nMSK\nEKS\nOpenSearch\nCodeBuild"] + C["Lambda\nElastiCache\nRDS\nNeptune\nECS\nEC2\nMSK\nEKS\nOpenSearch\nCodeBuild\nManaged Flink"] D["Athena -> floci-duck\nDuckDB sidecar"] end @@ -223,7 +223,7 @@ Floci supports local emulation for application services, data services, eventing | Events and workflows | EventBridge, EventBridge Pipes, EventBridge Scheduler, Step Functions, CloudWatch Logs, CloudWatch Metrics | | API and identity | API Gateway REST, API Gateway v2, AppSync, Cognito, ACM, Route53, Cloud Map | | Containers and compute | ECS, EC2, Lightsail, EKS, MWAA, ECR, CodeBuild, CodeDeploy, CodePipeline, AWS Batch, Auto Scaling, Application Auto Scaling, Elastic Beanstalk, ELB v2 | -| Data, analytics, and AI | Athena, Glue, EMR, Firehose, OpenSearch, S3 Vectors, Textract, Transcribe, Bedrock Runtime | +| Data, analytics, and AI | Athena, Glue, EMR, Firehose, Managed Service for Apache Flink, OpenSearch, S3 Vectors, Textract, Transcribe, Bedrock Runtime | | Databases and caching | RDS, RDS Data API, Neptune, DocumentDB, MemoryDB, ElastiCache | | Messaging and transfer | SES, Kinesis, MSK, Amazon MQ, Transfer Family, IoT Core | | Security and governance | WAF v2, CloudTrail, CloudFront, Resource Groups Tagging API | @@ -271,6 +271,7 @@ For operation-level compatibility, see the [Services Overview](https://floci.io/ | Glue | In-process | Data Catalog, Schema Registry, tables consumed by Athena | | EMR | In-process | Cluster (job flow) lifecycle, instance groups and fleets, steps, security configurations, tagging | | Data Firehose | In-process | Streaming delivery, NDJSON flush to S3 | +| Managed Service for Apache Flink | Real Docker | Kinesis Analytics V2 control plane; StartApplication runs a real Flink cluster (JobManager + TaskManager, image per RuntimeEnvironment), pulls the application JAR from local S3, and submits the job | | ECS | Real Docker | Clusters, task definitions, tasks, services, capacity providers, task sets | | EC2 | Real Docker | RunInstances launches containers, SSH key injection, UserData, IMDS, VPC resources | | Lightsail | In-process | Instances, disks, static IPs, key pairs, ports, tags, regions, blueprints, bundles, operations | @@ -325,6 +326,7 @@ Floci uses real Docker containers when in-process emulation would reduce fidelit | DocumentDB | `mongo:7.0` | MongoDB engine; MongoDB wire protocol on port 27017 | | MSK | `redpandadata/redpanda:latest` | Kafka-compatible broker via Redpanda | | Amazon MQ | `rabbitmq:3-management` | RabbitMQ broker; AMQP on port 5672, management console on 15672 | +| Managed Service for Apache Flink | `apache/flink:` | Apache Flink JobManager; image chosen per RuntimeEnvironment (FLINK-1_15 … FLINK-1_20, FLINK-2_0 … FLINK-2_3) | | EC2 | AMI-mapped Linux images | Linux containers, SSH key injection, UserData, IMDS, IAM credentials | | ECS | User-specified task image | Container lifecycle, start, stop, health checks | | EKS | `rancher/k3s:latest` | Kubernetes API server via k3s | @@ -353,6 +355,7 @@ docker run -d --name floci \ | `FLOCI_SERVICES_RDS_DEFAULT_MARIADB_IMAGE` | `mariadb:11` | | `FLOCI_SERVICES_MSK_DEFAULT_IMAGE` | `redpandadata/redpanda:latest` | | `FLOCI_SERVICES_OPENSEARCH_DEFAULT_IMAGE` | `opensearchproject/opensearch:2` | +| `FLOCI_SERVICES_KINESIS_ANALYTICS_DEFAULT_IMAGE` | _(unset; chosen per RuntimeEnvironment)_ | | `FLOCI_SERVICES_NEPTUNE_DEFAULT_IMAGE` | `tinkerpop/gremlin-server:3.7.3` | | `FLOCI_SERVICES_NEPTUNE_DEFAULT_NEO4J_IMAGE` | `neo4j:5-community` | | `FLOCI_SERVICES_DOCDB_DEFAULT_IMAGE` | `mongo:7.0` | diff --git a/compatibility-tests/sdk-test-java/pom.xml b/compatibility-tests/sdk-test-java/pom.xml index 01402a4657..b91b7c3e7f 100644 --- a/compatibility-tests/sdk-test-java/pom.xml +++ b/compatibility-tests/sdk-test-java/pom.xml @@ -144,6 +144,10 @@ software.amazon.awssdk mq + + software.amazon.awssdk + kinesisanalyticsv2 + software.amazon.awssdk athena diff --git a/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java b/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java index 3e49bbcf44..c894a2406c 100644 --- a/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java +++ b/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java @@ -39,6 +39,7 @@ import software.amazon.awssdk.services.ssm.SsmClient; import software.amazon.awssdk.services.sts.StsClient; import software.amazon.awssdk.services.kafka.KafkaClient; import software.amazon.awssdk.services.mq.MqClient; +import software.amazon.awssdk.services.kinesisanalyticsv2.KinesisAnalyticsV2Client; import software.amazon.awssdk.services.athena.AthenaClient; import software.amazon.awssdk.services.athena.model.GetQueryExecutionRequest; import software.amazon.awssdk.services.athena.model.QueryExecutionState; @@ -397,6 +398,14 @@ public final class TestFixtures { .build(); } + public static KinesisAnalyticsV2Client kinesisAnalyticsV2Client() { + return KinesisAnalyticsV2Client.builder() + .endpointOverride(ENDPOINT) + .region(REGION) + .credentialsProvider(CREDENTIALS) + .build(); + } + public static AthenaClient athenaClient() { return AthenaClient.builder() .endpointOverride(ENDPOINT) diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AbstractKinesisAnalyticsV2LifecycleTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AbstractKinesisAnalyticsV2LifecycleTest.java new file mode 100644 index 0000000000..0a4246a42d --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AbstractKinesisAnalyticsV2LifecycleTest.java @@ -0,0 +1,227 @@ +package com.floci.test; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestMethodOrder; +import software.amazon.awssdk.services.kinesisanalyticsv2.KinesisAnalyticsV2Client; +import software.amazon.awssdk.services.kinesisanalyticsv2.model.ApplicationDetail; +import software.amazon.awssdk.services.kinesisanalyticsv2.model.ApplicationStatus; +import software.amazon.awssdk.services.kinesisanalyticsv2.model.CreateApplicationRequest; +import software.amazon.awssdk.services.kinesisanalyticsv2.model.CreateApplicationResponse; +import software.amazon.awssdk.services.kinesisanalyticsv2.model.DeleteApplicationRequest; +import software.amazon.awssdk.services.kinesisanalyticsv2.model.DescribeApplicationRequest; +import software.amazon.awssdk.services.kinesisanalyticsv2.model.KinesisAnalyticsV2Exception; +import software.amazon.awssdk.services.kinesisanalyticsv2.model.ListApplicationsRequest; +import software.amazon.awssdk.services.kinesisanalyticsv2.model.RuntimeEnvironment; +import software.amazon.awssdk.services.kinesisanalyticsv2.model.StartApplicationRequest; +import software.amazon.awssdk.services.kinesisanalyticsv2.model.StopApplicationRequest; + +import java.time.Duration; +import java.time.Instant; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * Ordered lifecycle for Managed Service for Apache Flink (Kinesis Analytics V2), exercised against a + * specific Flink runtime supplied by {@link #runtime()}. Concrete subclasses pin a runtime so the suite + * guards both the Flink 1.x and 2.x lines (Flink 2.0 changed the config system and boots slower): + * CreateApplication → READY, StartApplication provisions a real {@code apache/flink} cluster and the + * application transitions to RUNNING, ListApplications, StopApplication → READY, unsupported-runtime + * rejection, DeleteApplication. + * + *

Uses {@code PER_CLASS} instance lifecycle so each concrete subclass gets its own state (static + * fields would collide across subclasses). Requires a running Floci with + * {@code kinesis-analytics.mock=false} and a working Docker daemon; when the application never reaches + * RUNNING (Docker/Flink unavailable in CI), the dependent assertions skip rather than fail, mirroring + * {@code AmazonMqTest} / {@code MskTest}. + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +abstract class AbstractKinesisAnalyticsV2LifecycleTest { + + private static final String ROLE = "arn:aws:iam::000000000000:role/flink-exec"; + + /** The Flink {@code RuntimeEnvironment} under test, e.g. {@code FLINK-1_18} or {@code FLINK-2_3}. */ + protected abstract String runtime(); + + private KinesisAnalyticsV2Client kda; + private String applicationName = TestFixtures.uniqueName("flink-app"); + private Instant createTimestamp; + private boolean reachedRunning; + + @BeforeAll + void setup() { + kda = TestFixtures.kinesisAnalyticsV2Client(); + } + + @AfterAll + void cleanup() { + if (kda != null) { + if (applicationName != null && createTimestamp != null) { + try { + ensureStopped(); + kda.deleteApplication(DeleteApplicationRequest.builder() + .applicationName(applicationName) + .createTimestamp(createTimestamp) + .build()); + } catch (Exception e) { + // Tolerated (best-effort teardown) but logged with context per AGENTS.md. + System.err.println("[WARN] AfterAll cleanup failed for application " + + applicationName + ": " + e.getMessage()); + } + } + kda.close(); + } + } + + @Test + @Order(1) + void createApplicationLandsReady() { + // runtime() is passed as a String: FLINK-2_3 is newer than this SDK's RuntimeEnvironment enum + // (2.44.14). The generated builder supports the String overload; assert via + // runtimeEnvironmentAsString() so both the enum-known (1.18) and newer (2.3) values work. + CreateApplicationResponse response = kda.createApplication(CreateApplicationRequest.builder() + .applicationName(applicationName) + .runtimeEnvironment(runtime()) + .serviceExecutionRole(ROLE) + .build()); + + ApplicationDetail detail = response.applicationDetail(); + assertThat(detail.applicationName()).isEqualTo(applicationName); + assertThat(detail.applicationARN()).contains(":kinesisanalytics:"); + // AWS-faithful: a freshly created application is READY, not RUNNING. + assertThat(detail.applicationStatus()).isEqualTo(ApplicationStatus.READY); + assertThat(detail.runtimeEnvironmentAsString()).isEqualTo(runtime()); + createTimestamp = detail.createTimestamp(); + } + + @Test + @Order(2) + void startApplicationReachesRunning() { + requireApp(); + + boolean started; + try { + kda.startApplication(StartApplicationRequest.builder() + .applicationName(applicationName) + .build()); + started = true; + } catch (KinesisAnalyticsV2Exception e) { + // Container provisioning failed (Docker likely unavailable) — skip rather than fail. + started = false; + } + Assumptions.assumeTrue(started, + "StartApplication failed (Docker likely unavailable); skipping RUNNING checks"); + + // Flink 2.x boots noticeably slower than 1.x (multi-pass JVM config parser), so allow more time. + ApplicationDetail detail = waitForStatus(ApplicationStatus.RUNNING, Duration.ofSeconds(300)); + Assumptions.assumeTrue(detail != null, + "Application did not reach RUNNING (Docker/Flink likely unavailable); skipping"); + + reachedRunning = true; + assertThat(detail.applicationStatus()).isEqualTo(ApplicationStatus.RUNNING); + assertThat(detail.runtimeEnvironmentAsString()).isEqualTo(runtime()); + } + + @Test + @Order(3) + void listApplicationsIncludesCreated() { + requireApp(); + + var response = kda.listApplications(ListApplicationsRequest.builder().build()); + assertThat(response.applicationSummaries()) + .anyMatch(s -> s.applicationName().equals(applicationName)); + } + + @Test + @Order(4) + void stopApplicationReturnsToReady() { + requireApp(); + Assumptions.assumeTrue(reachedRunning, + "Application never reached RUNNING; skipping stop"); + + kda.stopApplication(StopApplicationRequest.builder() + .applicationName(applicationName) + .build()); + + ApplicationDetail detail = waitForStatus(ApplicationStatus.READY, Duration.ofSeconds(60)); + assertThat(detail).isNotNull(); + assertThat(detail.applicationStatus()).isEqualTo(ApplicationStatus.READY); + } + + @Test + @Order(5) + void unsupportedRuntimeRejected() { + // Floci only backs FLINK-x_y runtimes with a plain Flink image; the SQL and Studio (Zeppelin) + // runtimes are rejected with InvalidArgumentException. + assertThatThrownBy(() -> kda.createApplication(CreateApplicationRequest.builder() + .applicationName(TestFixtures.uniqueName("flink-bad")) + .runtimeEnvironment(RuntimeEnvironment.ZEPPELIN_FLINK_3_0) + .serviceExecutionRole(ROLE) + .build())) + .isInstanceOf(KinesisAnalyticsV2Exception.class); + } + + @Test + @Order(6) + void deleteApplication() { + requireApp(); + + // AWS rejects DeleteApplication on a non-stopped application (ResourceInUseException), so + // ensure it is READY first (it may still be STARTING if it never reached RUNNING in time). + ensureStopped(); + kda.deleteApplication(DeleteApplicationRequest.builder() + .applicationName(applicationName) + .createTimestamp(createTimestamp) + .build()); + applicationName = null; + } + + /** Best-effort: stop the application if it is RUNNING/STARTING so it can be deleted from READY. */ + private void ensureStopped() { + try { + ApplicationStatus status = kda.describeApplication(DescribeApplicationRequest.builder() + .applicationName(applicationName) + .build()).applicationDetail().applicationStatus(); + if (status == ApplicationStatus.RUNNING || status == ApplicationStatus.STARTING) { + kda.stopApplication(StopApplicationRequest.builder() + .applicationName(applicationName) + .build()); + waitForStatus(ApplicationStatus.READY, Duration.ofSeconds(60)); + } + } catch (Exception e) { + System.err.println("[WARN] ensureStopped failed for application " + + applicationName + ": " + e.getMessage()); + } + } + + private ApplicationDetail waitForStatus(ApplicationStatus target, Duration timeout) { + Instant deadline = Instant.now().plus(timeout); + while (Instant.now().isBefore(deadline)) { + ApplicationDetail detail = kda.describeApplication(DescribeApplicationRequest.builder() + .applicationName(applicationName) + .build()).applicationDetail(); + if (detail.applicationStatus() == target) { + return detail; + } + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + break; + } + } + return null; + } + + private void requireApp() { + Assumptions.assumeTrue(applicationName != null, + "Application must exist from earlier ordered test"); + } +} diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/KinesisAnalyticsV2Flink118Test.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/KinesisAnalyticsV2Flink118Test.java new file mode 100644 index 0000000000..b8aeeab145 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/KinesisAnalyticsV2Flink118Test.java @@ -0,0 +1,13 @@ +package com.floci.test; + +import org.junit.jupiter.api.DisplayName; + +/** Runs the Managed Flink lifecycle against the Flink 1.x line (FLINK-1_18). */ +@DisplayName("Managed Service for Apache Flink — Flink 1.18") +class KinesisAnalyticsV2Flink118Test extends AbstractKinesisAnalyticsV2LifecycleTest { + + @Override + protected String runtime() { + return "FLINK-1_18"; + } +} diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/KinesisAnalyticsV2Flink23Test.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/KinesisAnalyticsV2Flink23Test.java new file mode 100644 index 0000000000..83a58084b2 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/KinesisAnalyticsV2Flink23Test.java @@ -0,0 +1,13 @@ +package com.floci.test; + +import org.junit.jupiter.api.DisplayName; + +/** Runs the Managed Flink lifecycle against the Flink 2.x line (FLINK-2_3). */ +@DisplayName("Managed Service for Apache Flink — Flink 2.3") +class KinesisAnalyticsV2Flink23Test extends AbstractKinesisAnalyticsV2LifecycleTest { + + @Override + protected String runtime() { + return "FLINK-2_3"; + } +} diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md index 708fc5e0b1..1d63f6a1bf 100644 --- a/docs/configuration/environment-variables.md +++ b/docs/configuration/environment-variables.md @@ -360,6 +360,14 @@ These services spawn Docker containers. They require access to the Docker socket | `FLOCI_SERVICES_MSK_MOCK` | `false` | When `true`, clusters are created instantly without a real Redpanda container | | `FLOCI_SERVICES_MSK_DEFAULT_IMAGE` | `redpandadata/redpanda:latest` | Docker image for Kafka/Redpanda brokers | +### Managed Service for Apache Flink (Kinesis Analytics V2) + +| Variable | Default | Description | +|---|---|---| +| `FLOCI_SERVICES_KINESIS_ANALYTICS_ENABLED` | `true` | Enable the Managed Flink (Kinesis Analytics V2) service | +| `FLOCI_SERVICES_KINESIS_ANALYTICS_MOCK` | `false` | When `true`, applications start instantly without a real Flink container | +| `FLOCI_SERVICES_KINESIS_ANALYTICS_DEFAULT_IMAGE` | _(unset)_ | Optional image override; when unset, the image is chosen from the requested `RuntimeEnvironment` (e.g. `FLINK-1_19` → `apache/flink:1.19`) | + ### ECR (Elastic Container Registry) | Variable | Default | Description | diff --git a/docs/index.md b/docs/index.md index b40ab49ce9..9bd266b971 100644 --- a/docs/index.md +++ b/docs/index.md @@ -44,6 +44,7 @@ Floci emulates 69 AWS services. See the [Services Overview](services/index.md) f | Athena | JSON 1.1 | | Glue Data Catalog + Schema Registry | JSON 1.1 | | Data Firehose | JSON 1.1 | +| Managed Service for Apache Flink | JSON 1.1 | | ECS | JSON 1.1 | | EC2 | EC2 Query | | Lightsail | JSON 1.1 | diff --git a/docs/services/index.md b/docs/services/index.md index f8b6969bfc..00d8e489a8 100644 --- a/docs/services/index.md +++ b/docs/services/index.md @@ -25,6 +25,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac | [Cognito](cognito.md) | `POST /` + `X-Amz-Target: AWSCognitoIdentityProviderService.*` | JSON 1.1 | 43 | | [KMS](kms.md) | `POST /` + `X-Amz-Target: TrentService.*` | JSON 1.1 | 34 | | [Kinesis](kinesis.md) | `POST /` + `X-Amz-Target: Kinesis_20131202.*` | JSON 1.1 | 24 | +| [Managed Service for Apache Flink](kinesisanalytics.md) | `POST /` + `X-Amz-Target: KinesisAnalytics_20180523.*` | JSON 1.1 | 7 | | [Secrets Manager](secrets-manager.md) | `POST /` + `X-Amz-Target: secretsmanager.*` | JSON 1.1 | 16 | | [Step Functions](step-functions.md) | `POST /` + `X-Amz-Target: AmazonStatesService.*` | JSON 1.1 | 19 | | [CloudFormation](cloudformation.md) | `POST /` with `Action=` param | Query | 19 | diff --git a/docs/services/kinesisanalytics.md b/docs/services/kinesisanalytics.md new file mode 100644 index 0000000000..5e4cf83ff3 --- /dev/null +++ b/docs/services/kinesisanalytics.md @@ -0,0 +1,227 @@ +# Managed Service for Apache Flink + +**Protocol:** JSON 1.1 +**Endpoint:** `http://localhost:4566/` + +Floci emulates Amazon Managed Service for Apache Flink (the Kinesis Analytics V2 API). Unlike a +pure mock, Floci runs a **real Apache Flink cluster** as Docker sidecars when an application is +started and **submits the application's JAR** to it, so the application reaches a genuine `RUNNING` +state backed by a live Flink job on Floci's Docker network. + +## Supported Actions + + +| Action | Description | +| --- | --- | +| `CreateApplication` | Creates a Flink application in the READY state | +| `CreateApplicationPresignedUrl` | Returns a URL to the application's Flink Dashboard | +| `DescribeApplication` | Returns details about an application | +| `ListApplications` | Lists all applications | +| `StartApplication` | Starts an application, provisioning a Flink JobManager container | +| `StopApplication` | Stops a running application and tears down its container | +| `UpdateApplication` | Updates an application (code, parallelism, execution role) and bumps its version; redeploys code in place on a running job | +| `DeleteApplication` | Deletes an application | +| `TagResource` | Assigns one or more tags to an application | +| `UntagResource` | Removes one or more tags from an application | +| `ListTagsForResource` | Lists the tags assigned to an application | +| `CreateApplicationSnapshot` | Triggers a Flink savepoint of the application's running job | +| `DescribeApplicationSnapshot` | Returns details about an application snapshot | +| `ListApplicationSnapshots` | Lists the snapshots for an application | +| `DeleteApplicationSnapshot` | Deletes an application snapshot | + + +## How it works + +1. **CreateApplication**: registers the application in the `READY` state and stores its + `ApplicationConfiguration` (the S3 location of the Flink JAR and the parallelism). No container is + started yet — this mirrors AWS, where a freshly created application is not running. +2. **StartApplication**: reads the application JAR from Floci's local S3, launches an `apache/flink` + **JobManager** container (plus a **TaskManager** for task slots) on Floci's Docker network, uploads + the JAR to the cluster and runs it. The application transitions `STARTING → RUNNING` once the Flink + job itself is `RUNNING`. An application created **without** code comes up `RUNNING` as a bare + cluster (no job). +3. **StopApplication**: cancels the Flink job, tears down the JobManager and TaskManager, and returns + the application to `READY`. +4. **DeleteApplication**: requires the application be stopped (`READY`); tears down any cluster and + removes the application. + +Because the Flink containers join Floci's Docker network, the job can reach `http://floci:4566` to +consume local Kinesis or MSK data streams. + +### Deploying application code (a Flink JAR) + +Pass an `ApplicationConfiguration` with an `ApplicationCodeConfiguration` pointing at a JAR in Floci's +local S3, exactly as with real AWS: + +```bash +export AWS_ENDPOINT_URL=http://localhost:4566 + +# 1. Upload your Flink job JAR to (local) S3 +aws s3 mb s3://flink-code +aws s3 cp my-flink-app.jar s3://flink-code/app.jar + +# 2. Create the application pointing at that JAR +aws kinesisanalyticsv2 create-application \ + --application-name jobdemo --runtime-environment FLINK-1_18 \ + --service-execution-role arn:aws:iam::000000000000:role/x \ + --application-configuration '{ + "ApplicationCodeConfiguration": { + "CodeContent": { "S3ContentLocation": { + "BucketARN": "arn:aws:s3:::flink-code", "FileKey": "app.jar" } }, + "CodeContentType": "ZIPFILE" }, + "FlinkApplicationConfiguration": { + "ParallelismConfiguration": { "ConfigurationType": "CUSTOM", "Parallelism": 1 } } + }' + +# 3. Start it — Floci pulls the JAR, runs it on the cluster, and reaches RUNNING +aws kinesisanalyticsv2 start-application --application-name jobdemo --run-configuration '{}' +aws kinesisanalyticsv2 describe-application --application-name jobdemo # ApplicationStatus: RUNNING +``` + +The job's main class is taken from the JAR's manifest (as in AWS Managed Flink). + +### Updating application code in place + +`UpdateApplication` with a new `ApplicationConfigurationUpdate.ApplicationCodeConfigurationUpdate` +redeploys a new JAR onto an **already-running** application without tearing down its JobManager/ +TaskManager containers: the current Flink job is cancelled and the new JAR is uploaded and run on the +same cluster, exactly the way `StartApplication` submits a job — the application briefly transitions +`RUNNING → STARTING → RUNNING` while the new job comes up. Calling `UpdateApplication` on a `READY` +application just stores the new code location for the next `StartApplication`. +`FlinkApplicationConfigurationUpdate.ParallelismConfigurationUpdate.ParallelismUpdate` is also applied. + +```bash +aws kinesisanalyticsv2 update-application \ + --application-name jobdemo --current-application-version-id 1 \ + --application-configuration-update '{ + "ApplicationCodeConfigurationUpdate": { "CodeContentUpdate": { "S3ContentLocationUpdate": { + "BucketARNUpdate": "arn:aws:s3:::flink-code", "FileKeyUpdate": "app-v2.jar" } } } }' +``` + +Not yet emulated: attaching code to a **bare** running cluster (one started without code) via +`UpdateApplication` — stop and start the application instead. + +### Runtime properties (`EnvironmentProperties`) + +Pass `ApplicationConfiguration.EnvironmentProperties.PropertyGroups` on `CreateApplication` to configure +your application without recompiling it — exactly as with real AWS. Floci writes them to +`/etc/flink/application_properties.json` inside **both** the JobManager and TaskManager containers +before the job runs, the same well-known path real Managed Service for Apache Flink uses, so a JAR +written against `KinesisAnalyticsRuntime.getApplicationProperties()` picks them up unmodified: + +```bash +aws kinesisanalyticsv2 create-application \ + --application-name jobdemo --runtime-environment FLINK-1_18 \ + --service-execution-role arn:aws:iam::000000000000:role/x \ + --application-configuration '{ + "ApplicationCodeConfiguration": { + "CodeContent": { "S3ContentLocation": { + "BucketARN": "arn:aws:s3:::flink-code", "FileKey": "app.jar" } }, + "CodeContentType": "ZIPFILE" }, + "EnvironmentProperties": { + "PropertyGroups": [ + { "PropertyGroupId": "ProducerConfigProperties", + "PropertyMap": { "aws.region": "us-west-2", "flink.stream.initpos": "LATEST" } } + ] } + }' +``` + +`DescribeApplication` echoes them back under +`ApplicationConfigurationDescription.EnvironmentPropertyDescriptions.PropertyGroupDescriptions`. + +### Snapshots (savepoints) + +`CreateApplicationSnapshot` triggers a real Flink [savepoint](https://nightlies.apache.org/flink/flink-docs-stable/docs/ops/state/savepoints/) +of the application's running job — the application must be `RUNNING` with a deployed job. The +snapshot starts `CREATING` and transitions to `READY` (or `FAILED`) once Flink's savepoint completes, +polled the same way `StartApplication` polls job readiness. Snapshot files land on a **named Docker +volume** mounted at `/opt/flink/savepoints` in the JobManager container, not the container's ephemeral +filesystem, so they survive a `StopApplication`/`StartApplication` cycle; the volume itself is only +removed on `DeleteApplication`. + +```bash +aws kinesisanalyticsv2 create-application-snapshot \ + --application-name jobdemo --snapshot-name before-upgrade + +aws kinesisanalyticsv2 describe-application-snapshot \ + --application-name jobdemo --snapshot-name before-upgrade # SnapshotStatus: CREATING -> READY + +aws kinesisanalyticsv2 list-application-snapshots --application-name jobdemo + +aws kinesisanalyticsv2 delete-application-snapshot \ + --application-name jobdemo --snapshot-name before-upgrade \ + --snapshot-creation-timestamp +``` + +Restoring a job *from* a snapshot (`CreateApplication`/`UpdateApplication` with a +`RestoreConfiguration` pointing at one) is not yet emulated. + +Pass `ApplicationConfiguration.ApplicationSnapshotConfiguration.SnapshotsEnabled: false` on +`CreateApplication` (or `ApplicationSnapshotConfigurationUpdate.SnapshotsEnabledUpdate` on +`UpdateApplication`) to disable snapshots for an application — real AWS defaults this to `true`. +`CreateApplicationSnapshot` rejects the request with `InvalidRequestException` while disabled. + +### Flink Dashboard access + +`CreateApplicationPresignedUrl` (with `UrlType: FLINK_DASHBOARD_URL`) returns a URL to the running +application's Flink Dashboard — the application must be `RUNNING`: + +```bash +aws kinesisanalyticsv2 create-application-presigned-url \ + --application-name jobdemo --url-type FLINK_DASHBOARD_URL +``` + +Real AWS returns a session-authorized, time-limited URL through its own proxy; Floci has no +equivalent session/proxy layer, so it returns the JobManager container's own REST/dashboard URL +directly — genuinely live and browsable, but not actually session-scoped or time-limited the way +`SessionExpirationDurationInSeconds` implies (the parameter is still validated to AWS's documented +1800–43200 second range). `UrlType: ZEPPELIN_UI_URL` is rejected, since Zeppelin/Studio applications +aren't supported at all (see below). + +## Supported runtimes + +The `RuntimeEnvironment` requested on `CreateApplication` selects the Flink image, so the running +container matches the requested version: + +| RuntimeEnvironment | Image | +|---|---| +| `FLINK-2_3` | `apache/flink:2.3` | +| `FLINK-2_2` | `apache/flink:2.2` | +| `FLINK-2_1` | `apache/flink:2.1` | +| `FLINK-2_0` | `apache/flink:2.0` | +| `FLINK-1_20` | `apache/flink:1.20` | +| `FLINK-1_19` | `apache/flink:1.19` | +| `FLINK-1_18` | `apache/flink:1.18` | +| `FLINK-1_15` | `apache/flink:1.15` | + +The set mirrors the runtimes AWS Managed Service for Apache Flink offers; `FLINK-1_16` and `FLINK-1_17` +are not offered by AWS and are rejected. The SQL (`SQL-1_0`) and Studio (`ZEPPELIN-FLINK-*`) runtimes +have no plain Flink image and are also rejected with `InvalidArgumentException`. + +## Configuration + +| Variable | Default | Description | +|---|---|---| +| `FLOCI_SERVICES_KINESIS_ANALYTICS_ENABLED` | `true` | Enable or disable the service | +| `FLOCI_SERVICES_KINESIS_ANALYTICS_MOCK` | `false` | Skip the Flink container; `StartApplication` comes up `RUNNING` immediately (useful without a Docker daemon) | +| `FLOCI_SERVICES_KINESIS_ANALYTICS_DEFAULT_IMAGE` | _(unset)_ | Optional override pinning **every** application to one image regardless of `RuntimeEnvironment`. When unset, the image is chosen from the runtime (see above) | + +## Example + +```bash +export AWS_ENDPOINT_URL=http://localhost:4566 + +# Create an application (lands in READY) +aws kinesisanalyticsv2 create-application \ + --application-name demo \ + --runtime-environment FLINK-1_18 \ + --service-execution-role arn:aws:iam::000000000000:role/x \ + --endpoint-url $AWS_ENDPOINT_URL + +# Start it — spins up a real Flink JobManager container (STARTING -> RUNNING) +aws kinesisanalyticsv2 start-application --application-name demo \ + --run-configuration '{}' --endpoint-url $AWS_ENDPOINT_URL + +# Check status +aws kinesisanalyticsv2 describe-application --application-name demo --endpoint-url $AWS_ENDPOINT_URL +``` diff --git a/mkdocs.yml b/mkdocs.yml index 0a7d377b01..1b78a0e8c9 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -85,6 +85,7 @@ nav: - Cognito: services/cognito.md - KMS: services/kms.md - Kinesis: services/kinesis.md + - Managed Service for Apache Flink: services/kinesisanalytics.md - Secrets Manager: services/secrets-manager.md - CloudFormation: services/cloudformation.md - Step Functions: services/step-functions.md 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 d1c7771781..3409b2cd21 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -531,6 +531,7 @@ public interface EmulatorConfig { IamServiceConfig iam(); MskServiceConfig msk(); AmazonMqServiceConfig amazonmq(); + KinesisAnalyticsServiceConfig kinesisAnalytics(); ElastiCacheServiceConfig elasticache(); MemoryDbServiceConfig memorydb(); RdsServiceConfig rds(); @@ -818,6 +819,23 @@ public interface EmulatorConfig { String defaultImage(); } + interface KinesisAnalyticsServiceConfig { + @WithDefault("true") + boolean enabled(); + + /** When true, StartApplication comes up RUNNING immediately with no backing Flink + * container. Useful for tests and hosts without a Docker daemon. */ + @WithDefault("false") + boolean mock(); + + /** + * Optional fixed image used for every application regardless of the requested + * {@code RuntimeEnvironment} (private registry mirror, pinned patch). When unset, the image + * is chosen from the runtime via {@code KinesisAnalyticsRuntimes.imageFor(runtimeEnvironment)}. + */ + Optional defaultImage(); + } + interface ElastiCacheServiceConfig { @WithDefault("true") boolean enabled(); diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java index 2bc6ffbcb5..598f0fb018 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java @@ -36,6 +36,7 @@ import io.github.hectorvent.floci.services.emr.EmrHandler; import io.github.hectorvent.floci.services.memorydb.MemoryDbHandler; import io.github.hectorvent.floci.services.wafv2.WafV2Handler; import io.github.hectorvent.floci.services.kinesis.KinesisJsonHandler; +import io.github.hectorvent.floci.services.kinesisanalytics.KinesisAnalyticsV2JsonHandler; import io.github.hectorvent.floci.services.kms.KmsJsonHandler; import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerJsonHandler; import io.github.hectorvent.floci.services.ssm.Ec2MessagesJsonHandler; @@ -75,6 +76,7 @@ public class AwsJson11Controller { private final CloudWatchLogsHandler cloudWatchLogsHandler; private final SecretsManagerJsonHandler secretsManagerJsonHandler; private final KinesisJsonHandler kinesisJsonHandler; + private final KinesisAnalyticsV2JsonHandler kinesisAnalyticsV2JsonHandler; private final ApiGatewayV2JsonHandler apigwV2JsonHandler; private final KmsJsonHandler kmsJsonHandler; private final CognitoJsonHandler cognitoJsonHandler; @@ -113,6 +115,7 @@ public class AwsJson11Controller { CloudWatchLogsHandler cloudWatchLogsHandler, SecretsManagerJsonHandler secretsManagerJsonHandler, KinesisJsonHandler kinesisJsonHandler, + KinesisAnalyticsV2JsonHandler kinesisAnalyticsV2JsonHandler, ApiGatewayV2JsonHandler apigwV2JsonHandler, KmsJsonHandler kmsJsonHandler, CognitoJsonHandler cognitoJsonHandler, AcmJsonHandler acmJsonHandler, EcsJsonHandler ecsJsonHandler, @@ -149,6 +152,7 @@ public class AwsJson11Controller { this.cloudWatchLogsHandler = cloudWatchLogsHandler; this.secretsManagerJsonHandler = secretsManagerJsonHandler; this.kinesisJsonHandler = kinesisJsonHandler; + this.kinesisAnalyticsV2JsonHandler = kinesisAnalyticsV2JsonHandler; this.apigwV2JsonHandler = apigwV2JsonHandler; this.kmsJsonHandler = kmsJsonHandler; this.cognitoJsonHandler = cognitoJsonHandler; @@ -218,6 +222,7 @@ public class AwsJson11Controller { case "logs" -> cloudWatchLogsHandler.handle(action, request, region); case "secretsmanager" -> secretsManagerJsonHandler.handle(action, request, region); case "kinesis" -> kinesisJsonHandler.handle(action, request, region); + case "kinesisanalytics" -> kinesisAnalyticsV2JsonHandler.handle(action, request, region); case "apigatewayv2" -> apigwV2JsonHandler.handle(action, request, region); case "kms" -> kmsJsonHandler.handle(action, request, region); case "cognito-idp" -> cognitoJsonHandler.handle(action, request, region); diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java index 0bf6858f9b..d2fd226ee7 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java @@ -178,6 +178,11 @@ public class ResolvedServiceCatalog { "kinesis", config.storage().mode(), 5000L, null, ServiceProtocol.JSON, protocols(ServiceProtocol.JSON, ServiceProtocol.CBOR), Set.of("Kinesis_20131202."), Set.of("kinesis"), Set.of(), Set.of()), + descriptor("kinesisanalytics", "kinesisanalytics", + config.services().kinesisAnalytics().enabled(), true, + "kinesisanalytics", config.storage().mode(), 5000L, null, ServiceProtocol.JSON, + protocols(ServiceProtocol.JSON), + Set.of("KinesisAnalytics_20180523."), Set.of("kinesisanalytics"), Set.of(), Set.of()), descriptor("kms", "kms", config.services().kms().enabled(), true, "kms", config.storage().mode(), 5000L, null, ServiceProtocol.JSON, protocols(ServiceProtocol.JSON), diff --git a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java index 58af767a5c..5580dc0b26 100644 --- a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java +++ b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java @@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.ec2.Ec2MetadataServer; import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager; import io.github.hectorvent.floci.services.floci.ui.FlociUiManager; import io.github.hectorvent.floci.services.amazonmq.container.RabbitMqManager; +import io.github.hectorvent.floci.services.kinesisanalytics.container.FlinkContainerManager; import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager; import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheMemcachedContainerManager; import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager; @@ -75,6 +76,7 @@ public class EmulatorLifecycle { private final NeptuneContainerManager neptuneContainerManager; private final NeptuneProxyManager neptuneProxyManager; private final RabbitMqManager rabbitMqManager; + private final FlinkContainerManager flinkContainerManager; private final RdsService rdsService; private final ElbV2Service elbV2Service; private final InitializationHooksRunner initializationHooksRunner; @@ -104,6 +106,7 @@ public class EmulatorLifecycle { NeptuneContainerManager neptuneContainerManager, NeptuneProxyManager neptuneProxyManager, RabbitMqManager rabbitMqManager, + FlinkContainerManager flinkContainerManager, RdsService rdsService, ElbV2Service elbV2Service, InitializationHooksRunner initializationHooksRunner, @@ -132,6 +135,7 @@ public class EmulatorLifecycle { this.neptuneContainerManager = neptuneContainerManager; this.neptuneProxyManager = neptuneProxyManager; this.rabbitMqManager = rabbitMqManager; + this.flinkContainerManager = flinkContainerManager; this.rdsService = rdsService; this.elbV2Service = elbV2Service; this.initializationHooksRunner = initializationHooksRunner; @@ -279,6 +283,7 @@ public class EmulatorLifecycle { docDbContainerManager.stopAll(); neptuneContainerManager.stopAll(); rabbitMqManager.stopAll(); + flinkContainerManager.stopAll(); ecrRegistryManager.shutdown(); flociUiManager.shutdown(); // Centralized teardown for process-bound containers (Lambda warm pool, ECS tasks, diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsRuntimes.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsRuntimes.java new file mode 100644 index 0000000000..f63db509d0 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsRuntimes.java @@ -0,0 +1,97 @@ +package io.github.hectorvent.floci.services.kinesisanalytics; + +import io.github.hectorvent.floci.core.common.AwsException; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +/** + * Catalog of Managed Service for Apache Flink runtime environments Floci supports, mapping each + * AWS-shaped {@code RuntimeEnvironment} (e.g. {@code FLINK-1_18}) to the Docker image tag that + * {@link container.FlinkContainerManager} launches for the JobManager. + * + *

Modelled on {@code services/opensearch/OpenSearchVersions}: without this map every application + * would boot whatever {@code apache/flink:1.18} resolved to regardless of the requested + * {@code RuntimeEnvironment} — a footgun for anyone testing version-specific Flink behaviour. Images + * are pinned to the {@code major.minor} tag (guaranteed to exist on Docker Hub; e.g. + * {@code apache/flink:1.18} resolves to a concrete 1.18.x). + * + *

Only the {@code FLINK-1_x} runtimes are in scope — they map to a plain Flink JobManager image. + * The {@code SQL-1_0} and {@code ZEPPELIN-FLINK-*} (Studio) runtimes have no equivalent stock image + * and are rejected by {@link #validate(String)}. + */ +public final class KinesisAnalyticsRuntimes { + + /** Default runtime when {@code CreateApplication} omits {@code RuntimeEnvironment}. AWS actually + * requires the field, so this is only a defensive fallback for {@link #imageFor(String)}. */ + public static final String DEFAULT_RUNTIME = "FLINK-1_20"; + + private static final Map RUNTIME_TO_IMAGE = new LinkedHashMap<>(); + + static { + // Pinned to major.minor tags — reproducible and guaranteed present on Docker Hub. The set + // mirrors the runtimes AWS Managed Service for Apache Flink offers: the 2.x line, plus the + // supported 1.x versions. AWS does not offer FLINK-1_16 / FLINK-1_17, so they are omitted. + RUNTIME_TO_IMAGE.put("FLINK-2_3", "apache/flink:2.3"); + RUNTIME_TO_IMAGE.put("FLINK-2_2", "apache/flink:2.2"); + RUNTIME_TO_IMAGE.put("FLINK-2_1", "apache/flink:2.1"); + RUNTIME_TO_IMAGE.put("FLINK-2_0", "apache/flink:2.0"); + RUNTIME_TO_IMAGE.put("FLINK-1_20", "apache/flink:1.20"); + RUNTIME_TO_IMAGE.put("FLINK-1_19", "apache/flink:1.19"); + RUNTIME_TO_IMAGE.put("FLINK-1_18", "apache/flink:1.18"); + RUNTIME_TO_IMAGE.put("FLINK-1_15", "apache/flink:1.15"); + } + + private KinesisAnalyticsRuntimes() { + } + + /** Runtimes Floci supports, newest-first. */ + public static List supportedRuntimes() { + return List.copyOf(RUNTIME_TO_IMAGE.keySet()); + } + + /** + * Pick the image for an application, honouring an optional operator-supplied override that + * applies to every application regardless of {@code runtimeEnvironment} (private registry mirror, + * pinned patch). When the override is absent this delegates to {@link #imageFor(String)}. + */ + public static String resolveImage(Optional override, String runtimeEnvironment) { + return override.filter(o -> !o.isBlank()).orElseGet(() -> imageFor(runtimeEnvironment)); + } + + /** + * Docker image to launch for {@code runtimeEnvironment}. Falls back to the default-runtime image + * only when {@code runtimeEnvironment} is null/blank; unsupported runtimes throw — call + * {@link #validate(String)} first. + */ + public static String imageFor(String runtimeEnvironment) { + if (runtimeEnvironment == null || runtimeEnvironment.isBlank()) { + return RUNTIME_TO_IMAGE.get(DEFAULT_RUNTIME); + } + String image = RUNTIME_TO_IMAGE.get(runtimeEnvironment); + if (image == null) { + throw new AwsException("InvalidArgumentException", + "Unsupported RuntimeEnvironment: " + runtimeEnvironment + + ". Supported: " + RUNTIME_TO_IMAGE.keySet(), 400); + } + return image; + } + + /** + * Throws {@code InvalidArgumentException} when {@code runtimeEnvironment} is set but not supported. + * A null/blank value is allowed — callers that accept an omitted value fall back to + * {@link #DEFAULT_RUNTIME}. (Floci's service requires the field, so blanks are rejected upstream.) + */ + public static void validate(String runtimeEnvironment) { + if (runtimeEnvironment == null || runtimeEnvironment.isBlank()) { + return; + } + if (!RUNTIME_TO_IMAGE.containsKey(runtimeEnvironment)) { + throw new AwsException("InvalidArgumentException", + "Unsupported RuntimeEnvironment: " + runtimeEnvironment + + ". Supported: " + RUNTIME_TO_IMAGE.keySet(), 400); + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandler.java new file mode 100644 index 0000000000..fa25a245a9 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandler.java @@ -0,0 +1,379 @@ +package io.github.hectorvent.floci.services.kinesisanalytics; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.AwsErrorResponse; +import io.github.hectorvent.floci.services.kinesisanalytics.model.FlinkApplication; +import io.github.hectorvent.floci.services.kinesisanalytics.model.Snapshot; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import jakarta.ws.rs.core.Response; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Dispatches Kinesis Analytics V2 (Managed Service for Apache Flink) actions for the + * {@code application/x-amz-json-1.1} protocol, routed here by {@code AwsJson11Controller} + * on the {@code KinesisAnalytics_20180523.} target prefix. + */ +@ApplicationScoped +public class KinesisAnalyticsV2JsonHandler { + + private final KinesisAnalyticsV2Service service; + private final ObjectMapper objectMapper; + + @Inject + public KinesisAnalyticsV2JsonHandler(KinesisAnalyticsV2Service service, ObjectMapper objectMapper) { + this.service = service; + this.objectMapper = objectMapper; + } + + public Response handle(String action, JsonNode request, String region) { + return switch (action) { + case "CreateApplication" -> handleCreateApplication(request); + case "CreateApplicationPresignedUrl" -> handleCreateApplicationPresignedUrl(request); + case "DescribeApplication" -> handleDescribeApplication(request); + case "ListApplications" -> handleListApplications(request); + case "StartApplication" -> handleStartApplication(request); + case "StopApplication" -> handleStopApplication(request); + case "UpdateApplication" -> handleUpdateApplication(request); + case "DeleteApplication" -> handleDeleteApplication(request); + case "TagResource" -> handleTagResource(request); + case "UntagResource" -> handleUntagResource(request); + case "ListTagsForResource" -> handleListTagsForResource(request); + case "CreateApplicationSnapshot" -> handleCreateApplicationSnapshot(request); + case "DescribeApplicationSnapshot" -> handleDescribeApplicationSnapshot(request); + case "ListApplicationSnapshots" -> handleListApplicationSnapshots(request); + case "DeleteApplicationSnapshot" -> handleDeleteApplicationSnapshot(request); + default -> Response.status(400) + .entity(new AwsErrorResponse("UnsupportedOperation", + "Operation " + action + " is not supported.")) + .build(); + }; + } + + private Response handleCreateApplication(JsonNode request) { + String applicationName = request.path("ApplicationName").asText(null); + String runtimeEnvironment = request.path("RuntimeEnvironment").asText(null); + String serviceExecutionRole = request.path("ServiceExecutionRole").asText(null); + String applicationDescription = request.path("ApplicationDescription").asText(null); + String applicationMode = request.path("ApplicationMode").asText(null); + + // Application code (the Flink JAR in S3) + parallelism, from ApplicationConfiguration. + JsonNode appConfig = request.path("ApplicationConfiguration"); + JsonNode s3 = appConfig.path("ApplicationCodeConfiguration").path("CodeContent") + .path("S3ContentLocation"); + String codeBucket = bucketFromArn(s3.path("BucketARN").asText(null)); + String codeKey = s3.path("FileKey").asText(null); + String codeVersion = s3.path("ObjectVersion").asText(null); + int parallelism = appConfig.path("FlinkApplicationConfiguration") + .path("ParallelismConfiguration").path("Parallelism").asInt(1); + Map> environmentProperties = + parsePropertyGroups(appConfig.path("EnvironmentProperties").path("PropertyGroups")); + JsonNode snapshotsEnabledNode = appConfig.path("ApplicationSnapshotConfiguration").path("SnapshotsEnabled"); + Boolean snapshotsEnabled = snapshotsEnabledNode.isMissingNode() || snapshotsEnabledNode.isNull() + ? null : snapshotsEnabledNode.asBoolean(); + + FlinkApplication app = service.createApplication(applicationName, runtimeEnvironment, + serviceExecutionRole, applicationDescription, applicationMode, + codeBucket, codeKey, codeVersion, parallelism, parseTags(request.path("Tags")), + environmentProperties, snapshotsEnabled); + return applicationDetailResponse(app); + } + + private Response handleCreateApplicationPresignedUrl(JsonNode request) { + String applicationName = request.path("ApplicationName").asText(null); + String urlType = request.path("UrlType").asText(null); + Long sessionExpirationDurationInSeconds = request.hasNonNull("SessionExpirationDurationInSeconds") + ? request.path("SessionExpirationDurationInSeconds").asLong() : null; + String url = service.createApplicationPresignedUrl(applicationName, urlType, + sessionExpirationDurationInSeconds); + ObjectNode response = objectMapper.createObjectNode(); + response.put("AuthorizedUrl", url); + return Response.ok(response).build(); + } + + /** Extracts the bucket name from an S3 bucket ARN ({@code arn:aws:s3:::bucket}). */ + private static String bucketFromArn(String bucketArn) { + if (bucketArn == null) { + return null; + } + String prefix = "arn:aws:s3:::"; + return bucketArn.startsWith(prefix) ? bucketArn.substring(prefix.length()) : bucketArn; + } + + private Response handleDescribeApplication(JsonNode request) { + String applicationName = request.path("ApplicationName").asText(null); + return applicationDetailResponse(service.describeApplication(applicationName)); + } + + private Response handleListApplications(JsonNode request) { + ObjectNode response = objectMapper.createObjectNode(); + ArrayNode summaries = response.putArray("ApplicationSummaries"); + for (FlinkApplication app : service.listApplications()) { + ObjectNode summary = summaries.addObject(); + summary.put("ApplicationName", app.getApplicationName()); + summary.put("ApplicationARN", app.getApplicationArn()); + summary.put("ApplicationStatus", app.getApplicationStatus().name()); + summary.put("ApplicationVersionId", app.getApplicationVersionId()); + summary.put("RuntimeEnvironment", app.getRuntimeEnvironment()); + if (app.getApplicationMode() != null) { + summary.put("ApplicationMode", app.getApplicationMode()); + } + } + return Response.ok(response).build(); + } + + private Response handleStartApplication(JsonNode request) { + String applicationName = request.path("ApplicationName").asText(null); + service.startApplication(applicationName); + // AWS StartApplication returns an empty body. + return Response.ok(objectMapper.createObjectNode()).build(); + } + + private Response handleStopApplication(JsonNode request) { + String applicationName = request.path("ApplicationName").asText(null); + service.stopApplication(applicationName); + // AWS StopApplication returns an empty body. + return Response.ok(objectMapper.createObjectNode()).build(); + } + + private Response handleUpdateApplication(JsonNode request) { + String applicationName = request.path("ApplicationName").asText(null); + // CurrentApplicationVersionId gates the update (optimistic concurrency); the service rejects a + // stale or missing value. + Long currentVersionId = request.hasNonNull("CurrentApplicationVersionId") + ? request.path("CurrentApplicationVersionId").asLong() + : null; + String serviceExecutionRole = request.path("ServiceExecutionRoleUpdate").asText(null); + + // ApplicationConfigurationUpdate.ApplicationCodeConfigurationUpdate.CodeContentUpdate + // .S3ContentLocationUpdate: a new JAR location to redeploy in place. + JsonNode appCfgUpdate = request.path("ApplicationConfigurationUpdate"); + JsonNode s3Update = appCfgUpdate.path("ApplicationCodeConfigurationUpdate") + .path("CodeContentUpdate").path("S3ContentLocationUpdate"); + String codeBucket = bucketFromArn(s3Update.path("BucketARNUpdate").asText(null)); + String codeKey = s3Update.path("FileKeyUpdate").asText(null); + String codeVersion = s3Update.path("ObjectVersionUpdate").asText(null); + JsonNode parallelismUpdate = appCfgUpdate.path("FlinkApplicationConfigurationUpdate") + .path("ParallelismConfigurationUpdate").path("ParallelismUpdate"); + Integer parallelism = parallelismUpdate.isMissingNode() || parallelismUpdate.isNull() + ? null : parallelismUpdate.asInt(); + JsonNode snapshotsEnabledUpdate = appCfgUpdate.path("ApplicationSnapshotConfigurationUpdate") + .path("SnapshotsEnabledUpdate"); + Boolean snapshotsEnabled = snapshotsEnabledUpdate.isMissingNode() || snapshotsEnabledUpdate.isNull() + ? null : snapshotsEnabledUpdate.asBoolean(); + + return applicationDetailResponse(service.updateApplication(applicationName, currentVersionId, + serviceExecutionRole, codeBucket, codeKey, codeVersion, parallelism, snapshotsEnabled)); + } + + private Response handleDeleteApplication(JsonNode request) { + String applicationName = request.path("ApplicationName").asText(null); + // CreateTimestamp is epoch seconds on the wire (possibly fractional); the service validates it + // against the stored value. + Instant createTimestamp = request.hasNonNull("CreateTimestamp") + ? Instant.ofEpochMilli(Math.round(request.path("CreateTimestamp").asDouble() * 1000)) + : null; + service.deleteApplication(applicationName, createTimestamp); + // AWS DeleteApplication returns an empty body. + return Response.ok(objectMapper.createObjectNode()).build(); + } + + private Response handleCreateApplicationSnapshot(JsonNode request) { + String applicationName = request.path("ApplicationName").asText(null); + String snapshotName = request.path("SnapshotName").asText(null); + service.createApplicationSnapshot(applicationName, snapshotName); + // AWS CreateApplicationSnapshot returns an empty body. + return Response.ok(objectMapper.createObjectNode()).build(); + } + + private Response handleDescribeApplicationSnapshot(JsonNode request) { + String applicationName = request.path("ApplicationName").asText(null); + String snapshotName = request.path("SnapshotName").asText(null); + Snapshot snapshot = service.describeApplicationSnapshot(applicationName, snapshotName); + ObjectNode response = objectMapper.createObjectNode(); + response.set("SnapshotDetails", snapshotDetailNode(snapshot)); + return Response.ok(response).build(); + } + + private Response handleListApplicationSnapshots(JsonNode request) { + String applicationName = request.path("ApplicationName").asText(null); + ObjectNode response = objectMapper.createObjectNode(); + ArrayNode summaries = response.putArray("SnapshotSummaries"); + for (Snapshot snapshot : service.listApplicationSnapshots(applicationName)) { + summaries.add(snapshotDetailNode(snapshot)); + } + return Response.ok(response).build(); + } + + private Response handleDeleteApplicationSnapshot(JsonNode request) { + String applicationName = request.path("ApplicationName").asText(null); + String snapshotName = request.path("SnapshotName").asText(null); + Instant snapshotCreationTimestamp = request.hasNonNull("SnapshotCreationTimestamp") + ? Instant.ofEpochMilli(Math.round(request.path("SnapshotCreationTimestamp").asDouble() * 1000)) + : null; + service.deleteApplicationSnapshot(applicationName, snapshotName, snapshotCreationTimestamp); + // AWS DeleteApplicationSnapshot returns an empty body. + return Response.ok(objectMapper.createObjectNode()).build(); + } + + private ObjectNode snapshotDetailNode(Snapshot snapshot) { + ObjectNode node = objectMapper.createObjectNode(); + node.put("SnapshotName", snapshot.getSnapshotName()); + node.put("SnapshotStatus", snapshot.getSnapshotStatus().name()); + node.put("ApplicationVersionId", snapshot.getApplicationVersionId()); + if (snapshot.getSnapshotCreationTimestamp() != null) { + node.put("SnapshotCreationTimestamp", snapshot.getSnapshotCreationTimestamp().toEpochMilli() / 1000.0); + } + if (snapshot.getRuntimeEnvironment() != null) { + node.put("RuntimeEnvironment", snapshot.getRuntimeEnvironment()); + } + return node; + } + + private Response handleTagResource(JsonNode request) { + String resourceArn = request.path("ResourceARN").asText(null); + service.tagResource(resourceArn, parseTags(request.path("Tags"))); + // AWS TagResource returns an empty body. + return Response.ok(objectMapper.createObjectNode()).build(); + } + + private Response handleUntagResource(JsonNode request) { + String resourceArn = request.path("ResourceARN").asText(null); + List tagKeys = new ArrayList<>(); + request.path("TagKeys").forEach(k -> tagKeys.add(k.asText())); + service.untagResource(resourceArn, tagKeys); + // AWS UntagResource returns an empty body. + return Response.ok(objectMapper.createObjectNode()).build(); + } + + private Response handleListTagsForResource(JsonNode request) { + String resourceArn = request.path("ResourceARN").asText(null); + Map tags = service.listTagsForResource(resourceArn); + ObjectNode response = objectMapper.createObjectNode(); + response.set("Tags", tagsNode(tags)); + return Response.ok(response).build(); + } + + /** {@code Tags} is a list of {@code {Key, Value}} objects on this API, not a string map. */ + private Map parseTags(JsonNode tagsNode) { + Map tags = new LinkedHashMap<>(); + if (tagsNode != null && tagsNode.isArray()) { + for (JsonNode tag : tagsNode) { + String key = tag.path("Key").asText(null); + if (key != null) { + tags.put(key, tag.path("Value").asText(null)); + } + } + } + return tags; + } + + private ArrayNode tagsNode(Map tags) { + ArrayNode arr = objectMapper.createArrayNode(); + tags.forEach((k, v) -> { + ObjectNode tag = arr.addObject(); + tag.put("Key", k); + if (v != null) { + tag.put("Value", v); + } + }); + return arr; + } + + private Response applicationDetailResponse(FlinkApplication app) { + ObjectNode response = objectMapper.createObjectNode(); + response.set("ApplicationDetail", applicationDetailNode(app)); + return Response.ok(response).build(); + } + + private ObjectNode applicationDetailNode(FlinkApplication app) { + ObjectNode detail = objectMapper.createObjectNode(); + detail.put("ApplicationARN", app.getApplicationArn()); + detail.put("ApplicationName", app.getApplicationName()); + if (app.getApplicationDescription() != null) { + detail.put("ApplicationDescription", app.getApplicationDescription()); + } + detail.put("RuntimeEnvironment", app.getRuntimeEnvironment()); + if (app.getServiceExecutionRole() != null) { + detail.put("ServiceExecutionRole", app.getServiceExecutionRole()); + } + detail.put("ApplicationStatus", app.getApplicationStatus().name()); + detail.put("ApplicationVersionId", app.getApplicationVersionId()); + if (app.getApplicationMode() != null) { + detail.put("ApplicationMode", app.getApplicationMode()); + } + if (app.getCreateTimestamp() != null) { + detail.put("CreateTimestamp", app.getCreateTimestamp().toEpochMilli() / 1000.0); + } + if (app.getLastUpdateTimestamp() != null) { + detail.put("LastUpdateTimestamp", app.getLastUpdateTimestamp().toEpochMilli() / 1000.0); + } + if (app.hasCode()) { + detail.set("ApplicationConfigurationDescription", applicationConfigurationNode(app)); + } + return detail; + } + + private ObjectNode applicationConfigurationNode(FlinkApplication app) { + ObjectNode config = objectMapper.createObjectNode(); + + ObjectNode codeDesc = config.putObject("ApplicationCodeConfigurationDescription"); + codeDesc.put("CodeContentType", "ZIPFILE"); + ObjectNode s3Desc = codeDesc.putObject("CodeContentDescription") + .putObject("S3ApplicationCodeLocationDescription"); + s3Desc.put("BucketARN", "arn:aws:s3:::" + app.getCodeS3Bucket()); + s3Desc.put("FileKey", app.getCodeS3Key()); + if (app.getCodeS3ObjectVersion() != null) { + s3Desc.put("ObjectVersion", app.getCodeS3ObjectVersion()); + } + + config.putObject("FlinkApplicationConfigurationDescription") + .putObject("ParallelismConfigurationDescription") + .put("Parallelism", app.getParallelism()) + .put("CurrentParallelism", app.getParallelism()); + + if (!app.getEnvironmentProperties().isEmpty()) { + ArrayNode groups = config.putObject("EnvironmentPropertyDescriptions") + .putArray("PropertyGroupDescriptions"); + app.getEnvironmentProperties().forEach((groupId, properties) -> { + ObjectNode group = groups.addObject(); + group.put("PropertyGroupId", groupId); + ObjectNode map = group.putObject("PropertyMap"); + properties.forEach(map::put); + }); + } + + config.putObject("ApplicationSnapshotConfigurationDescription") + .put("SnapshotsEnabled", app.isSnapshotsEnabled()); + + return config; + } + + /** {@code ApplicationConfiguration.EnvironmentProperties.PropertyGroups}: a list of + * {@code {PropertyGroupId, PropertyMap}} objects, keyed by PropertyGroupId internally since + * that's how a Flink app looks a group up via {@code KinesisAnalyticsRuntime.getApplicationProperties()}. */ + private Map> parsePropertyGroups(JsonNode propertyGroupsNode) { + Map> groups = new LinkedHashMap<>(); + if (propertyGroupsNode != null && propertyGroupsNode.isArray()) { + for (JsonNode group : propertyGroupsNode) { + String groupId = group.path("PropertyGroupId").asText(null); + if (groupId == null) { + continue; + } + Map properties = new LinkedHashMap<>(); + group.path("PropertyMap").fields().forEachRemaining( + entry -> properties.put(entry.getKey(), entry.getValue().asText(null))); + groups.put(groupId, properties); + } + } + return groups; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2Service.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2Service.java new file mode 100644 index 0000000000..0d09c98a64 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2Service.java @@ -0,0 +1,579 @@ +package io.github.hectorvent.floci.services.kinesisanalytics; + +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.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.kinesisanalytics.container.FlinkContainerManager; +import io.github.hectorvent.floci.services.kinesisanalytics.model.ApplicationStatus; +import io.github.hectorvent.floci.services.kinesisanalytics.model.FlinkApplication; +import io.github.hectorvent.floci.services.kinesisanalytics.model.Snapshot; +import io.github.hectorvent.floci.services.kinesisanalytics.model.SnapshotStatus; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import org.jboss.logging.Logger; + +import java.time.Instant; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * Control plane for Managed Service for Apache Flink (Kinesis Analytics V2). Holds the + * application state machine and delegates the real Flink cluster to {@link FlinkContainerManager}. + * + *

Modelled on {@code AmazonMqService}: {@code CreateApplication} lands in {@code READY} with no + * container (AWS-faithful — a created application is not running); {@code StartApplication} spins up + * a Flink JobManager container and the readiness poller flips {@code STARTING → RUNNING} once the + * JobManager REST API answers; {@code StopApplication} tears the container down and returns to + * {@code READY}. + */ +@ApplicationScoped +public class KinesisAnalyticsV2Service { + + private static final Logger LOG = Logger.getLogger(KinesisAnalyticsV2Service.class); + + // AWS: "the maximum number of user-defined application tags is 50" (the stated 200-tag ceiling + // on the Tags/TagKeys shapes includes AWS-managed system tags, which floci does not model). + private static final int MAX_USER_TAGS = 50; + + // CreateApplicationPresignedUrl's SessionExpirationDurationInSeconds valid range (30 min - 12 hr). + private static final long MIN_SESSION_EXPIRATION_SECONDS = 1800; + private static final long MAX_SESSION_EXPIRATION_SECONDS = 43200; + + private final StorageBackend storage; + private final EmulatorConfig config; + private final RegionResolver regionResolver; + private final FlinkContainerManager containerManager; + private final ScheduledExecutorService poller = Executors.newSingleThreadScheduledExecutor(); + + @Inject + public KinesisAnalyticsV2Service(StorageFactory storageFactory, EmulatorConfig config, + RegionResolver regionResolver, + FlinkContainerManager containerManager) { + this.storage = storageFactory.create("kinesisanalytics", "kinesisanalytics-applications.json", + new TypeReference>() {}); + this.config = config; + this.regionResolver = regionResolver; + this.containerManager = containerManager; + } + + @PostConstruct + public void init() { + startReadinessPoller(); + } + + @PreDestroy + public void shutdown() { + // Container teardown is wired into EmulatorLifecycle.onStop() via + // FlinkContainerManager.stopAll() (ordered with the other container managers); + // here we only stop the readiness poller. Wait briefly for an in-flight tick so a + // mid-flight putApplication cannot race the storage flush in onStop(). + poller.shutdown(); + try { + if (!poller.awaitTermination(5, TimeUnit.SECONDS)) { + poller.shutdownNow(); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + poller.shutdownNow(); + } + } + + public FlinkApplication createApplication(String applicationName, String runtimeEnvironment, + String serviceExecutionRole, String applicationDescription, + String applicationMode) { + return createApplication(applicationName, runtimeEnvironment, serviceExecutionRole, + applicationDescription, applicationMode, null, null, null, 1); + } + + public FlinkApplication createApplication(String applicationName, String runtimeEnvironment, + String serviceExecutionRole, String applicationDescription, + String applicationMode, String codeS3Bucket, String codeS3Key, + String codeS3ObjectVersion, int parallelism) { + return createApplication(applicationName, runtimeEnvironment, serviceExecutionRole, + applicationDescription, applicationMode, codeS3Bucket, codeS3Key, codeS3ObjectVersion, + parallelism, null); + } + + public FlinkApplication createApplication(String applicationName, String runtimeEnvironment, + String serviceExecutionRole, String applicationDescription, + String applicationMode, String codeS3Bucket, String codeS3Key, + String codeS3ObjectVersion, int parallelism, + Map tags) { + return createApplication(applicationName, runtimeEnvironment, serviceExecutionRole, + applicationDescription, applicationMode, codeS3Bucket, codeS3Key, codeS3ObjectVersion, + parallelism, tags, null); + } + + public FlinkApplication createApplication(String applicationName, String runtimeEnvironment, + String serviceExecutionRole, String applicationDescription, + String applicationMode, String codeS3Bucket, String codeS3Key, + String codeS3ObjectVersion, int parallelism, + Map tags, + Map> environmentProperties) { + return createApplication(applicationName, runtimeEnvironment, serviceExecutionRole, + applicationDescription, applicationMode, codeS3Bucket, codeS3Key, codeS3ObjectVersion, + parallelism, tags, environmentProperties, null); + } + + public FlinkApplication createApplication(String applicationName, String runtimeEnvironment, + String serviceExecutionRole, String applicationDescription, + String applicationMode, String codeS3Bucket, String codeS3Key, + String codeS3ObjectVersion, int parallelism, + Map tags, + Map> environmentProperties, + Boolean snapshotsEnabled) { + if (applicationName == null || applicationName.isBlank()) { + throw new AwsException("InvalidArgumentException", "ApplicationName is required", 400); + } + if (runtimeEnvironment == null || runtimeEnvironment.isBlank()) { + throw new AwsException("InvalidArgumentException", "RuntimeEnvironment is required", 400); + } + // Reject a runtime we cannot back with a Flink image up front (AWS-faithful + // InvalidArgumentException), rather than failing later at StartApplication. + KinesisAnalyticsRuntimes.validate(runtimeEnvironment); + if (serviceExecutionRole == null || serviceExecutionRole.isBlank()) { + throw new AwsException("InvalidArgumentException", "ServiceExecutionRole is required", 400); + } + if (storage.get(applicationName).isPresent()) { + throw new AwsException("ResourceInUseException", + "Application already exists: " + applicationName, 400); + } + if (tags != null && tags.size() > MAX_USER_TAGS) { + throw new AwsException("TooManyTagsException", + "The maximum number of user-defined application tags is " + MAX_USER_TAGS, 400); + } + + String accountId = regionResolver.getAccountId(); + String arn = AwsArnUtils.Arn.of("kinesisanalytics", config.defaultRegion(), accountId, + "application/" + applicationName).toString(); + String mode = (applicationMode == null || applicationMode.isBlank()) ? "STREAMING" : applicationMode; + + FlinkApplication app = new FlinkApplication(applicationName, arn, runtimeEnvironment, + serviceExecutionRole, mode); + app.setAccountId(accountId); + app.setApplicationDescription(applicationDescription); + // AWS-faithful: a freshly created application is READY (not RUNNING); no container yet. + app.setApplicationStatus(ApplicationStatus.READY); + app.setCodeS3Bucket(codeS3Bucket); + app.setCodeS3Key(codeS3Key); + app.setCodeS3ObjectVersion(codeS3ObjectVersion); + app.setParallelism(parallelism < 1 ? 1 : parallelism); + if (tags != null) { + app.setTags(new LinkedHashMap<>(tags)); + } + if (environmentProperties != null) { + app.setEnvironmentProperties(new LinkedHashMap<>(environmentProperties)); + } + if (snapshotsEnabled != null) { + app.setSnapshotsEnabled(snapshotsEnabled); + } + + storage.put(applicationName, app); + LOG.infov("Created Kinesis Analytics V2 application {0}", applicationName); + return app; + } + + public FlinkApplication describeApplication(String applicationName) { + return storage.get(applicationName) + .orElseThrow(() -> new AwsException("ResourceNotFoundException", + "Application not found: " + applicationName, 400)); + } + + public List listApplications() { + return storage.scan(k -> true); + } + + public FlinkApplication startApplication(String applicationName) { + FlinkApplication app = describeApplication(applicationName); + if (app.getApplicationStatus() != ApplicationStatus.READY) { + throw new AwsException("ResourceInUseException", + "Application " + applicationName + " cannot be started while in state " + + app.getApplicationStatus() + "; it must be READY", 400); + } + + if (config.services().kinesisAnalytics().mock()) { + // No backing container: come up immediately. + app.setApplicationStatus(ApplicationStatus.RUNNING); + } else { + try { + // Start the container; the application stays STARTING until the readiness poller + // observes the JobManager REST API answering. + containerManager.startCluster(app); + app.setApplicationStatus(ApplicationStatus.STARTING); + } catch (AwsException e) { + // Already an AWS-shaped client error (e.g. a missing/empty/unreadable application-code + // S3 object from readJar); preserve its error code and message rather than masking it + // as a 500. + throw e; + } catch (RuntimeException e) { + // Unexpected provisioning failure (e.g. Docker unavailable). Keep the cause in the logs; + // don't leak internal details into the AWS envelope. "InternalFailure" (no "Exception" + // suffix, HTTP 500) is AWS's documented generic error common to every service's API + // (e.g. https://docs.aws.amazon.com/comprehend/latest/APIReference/CommonErrors.html) — + // it isn't declared per-operation in kinesisanalyticsv2's model (no operation here + // declares any 5xx shape at all), so this isn't a code this specific API's model dictates, + // it's the AWS-wide platform fallback, same as JsonErrorResponseUtils' own default. + LOG.errorv(e, "Failed to start application {0}", applicationName); + throw new AwsException("InternalFailure", + "Failed to start application " + applicationName, 500); + } + } + app.setLastUpdateTimestamp(Instant.now()); + putApplication(app); + LOG.infov("Starting Kinesis Analytics V2 application {0}", applicationName); + return app; + } + + public FlinkApplication stopApplication(String applicationName) { + FlinkApplication app = describeApplication(applicationName); + if (app.getApplicationStatus() != ApplicationStatus.RUNNING + && app.getApplicationStatus() != ApplicationStatus.STARTING) { + throw new AwsException("ResourceInUseException", + "Application " + applicationName + " cannot be stopped while in state " + + app.getApplicationStatus() + "; it must be RUNNING or STARTING", 400); + } + if (!config.services().kinesisAnalytics().mock()) { + containerManager.stopCluster(app); + } + // Stopping the JobManager is synchronous, so the application returns to READY directly. + app.setApplicationStatus(ApplicationStatus.READY); + app.setLastUpdateTimestamp(Instant.now()); + putApplication(app); + LOG.infov("Stopped Kinesis Analytics V2 application {0}", applicationName); + return app; + } + + public FlinkApplication updateApplication(String applicationName, Long currentApplicationVersionId, + String serviceExecutionRole) { + return updateApplication(applicationName, currentApplicationVersionId, serviceExecutionRole, + null, null, null, null); + } + + public FlinkApplication updateApplication(String applicationName, Long currentApplicationVersionId, + String serviceExecutionRole, String codeS3Bucket, + String codeS3Key, String codeS3ObjectVersion, + Integer parallelism) { + return updateApplication(applicationName, currentApplicationVersionId, serviceExecutionRole, + codeS3Bucket, codeS3Key, codeS3ObjectVersion, parallelism, null); + } + + public FlinkApplication updateApplication(String applicationName, Long currentApplicationVersionId, + String serviceExecutionRole, String codeS3Bucket, + String codeS3Key, String codeS3ObjectVersion, + Integer parallelism, Boolean snapshotsEnabled) { + FlinkApplication app = describeApplication(applicationName); + // AWS requires CurrentApplicationVersionId and rejects a stale value with + // ConcurrentModificationException (optimistic concurrency on the application version). + if (currentApplicationVersionId == null) { + throw new AwsException("InvalidArgumentException", + "CurrentApplicationVersionId is required", 400); + } + if (currentApplicationVersionId != app.getApplicationVersionId()) { + throw new AwsException("ConcurrentModificationException", + "Provided CurrentApplicationVersionId " + currentApplicationVersionId + + " does not match the current version " + app.getApplicationVersionId(), 400); + } + if (serviceExecutionRole != null && !serviceExecutionRole.isBlank()) { + app.setServiceExecutionRole(serviceExecutionRole); + } + boolean codeChanged = codeS3Bucket != null && !codeS3Bucket.isBlank() + && codeS3Key != null && !codeS3Key.isBlank(); + if (codeChanged) { + app.setCodeS3Bucket(codeS3Bucket); + app.setCodeS3Key(codeS3Key); + app.setCodeS3ObjectVersion(codeS3ObjectVersion); + } + if (parallelism != null && parallelism > 0) { + app.setParallelism(parallelism); + } + if (snapshotsEnabled != null) { + app.setSnapshotsEnabled(snapshotsEnabled); + } + + if (codeChanged && app.getApplicationStatus() == ApplicationStatus.RUNNING + && !config.services().kinesisAnalytics().mock()) { + if (app.getTaskManagerContainerId() == null) { + // The application is running as a bare cluster (no code yet); attaching code to an + // already-running cluster for the first time is not yet emulated. + throw new AwsException("InvalidRequestException", + "Adding code to a running bare-cluster application is not supported; " + + "stop and start " + applicationName + " instead", 400); + } + try { + // In-place redeploy: cancel the current job and swap in the new JAR without tearing + // down the JobManager/TaskManager containers. The readiness poller resubmits it, the + // same way it submits a fresh job on StartApplication. + containerManager.redeployCode(app); + app.setApplicationStatus(ApplicationStatus.STARTING); + } catch (AwsException e) { + throw e; + } catch (RuntimeException e) { + // Same AWS-wide "InternalFailure" generic 500 as startApplication's catch above. + LOG.errorv(e, "Failed to redeploy code for application {0}", applicationName); + throw new AwsException("InternalFailure", + "Failed to redeploy application code for " + applicationName, 500); + } + } + + app.setApplicationVersionId(app.getApplicationVersionId() + 1); + app.setLastUpdateTimestamp(Instant.now()); + putApplication(app); + LOG.infov("Updated Kinesis Analytics V2 application {0} to version {1}", + applicationName, app.getApplicationVersionId()); + return app; + } + + public void deleteApplication(String applicationName, Instant createTimestamp) { + FlinkApplication app = describeApplication(applicationName); + // AWS requires CreateTimestamp and rejects a value that does not match the stored one. The + // wire value is epoch seconds, so compare at second granularity. + if (createTimestamp == null) { + throw new AwsException("InvalidArgumentException", "CreateTimestamp is required", 400); + } + if (app.getCreateTimestamp() == null + || app.getCreateTimestamp().getEpochSecond() != createTimestamp.getEpochSecond()) { + throw new AwsException("InvalidArgumentException", + "Provided CreateTimestamp does not match application " + applicationName, 400); + } + // AWS rejects deletion of a non-stopped application (RUNNING/STARTING) with + // ResourceInUseException; the caller must StopApplication first. + if (app.getApplicationStatus() != ApplicationStatus.READY) { + throw new AwsException("ResourceInUseException", + "Application " + applicationName + " cannot be deleted while in state " + + app.getApplicationStatus() + "; stop the application first", 400); + } + if (!config.services().kinesisAnalytics().mock()) { + // No-op for a READY app with no container; also clears any stale container left from a + // previous run (containerId is not persisted across an emulator restart). + containerManager.stopCluster(app); + // Only on delete, never on a plain StopApplication (stopCluster above) — the savepoints + // volume must survive a stop/restart cycle so snapshots remain describable/listable. + containerManager.removeSavepointsVolume(app); + } + storage.delete(applicationName); + LOG.infov("Deleted Kinesis Analytics V2 application {0}", applicationName); + } + + public Map listTagsForResource(String resourceArn) { + return findByArn(resourceArn).getTags(); + } + + public Map tagResource(String resourceArn, Map tags) { + FlinkApplication app = findByArn(resourceArn); + Map merged = new LinkedHashMap<>(app.getTags()); + if (tags != null) { + merged.putAll(tags); + } + if (merged.size() > MAX_USER_TAGS) { + throw new AwsException("TooManyTagsException", + "The maximum number of user-defined application tags is " + MAX_USER_TAGS, 400); + } + app.setTags(merged); + putApplication(app); + return app.getTags(); + } + + public Map untagResource(String resourceArn, List tagKeys) { + FlinkApplication app = findByArn(resourceArn); + if (tagKeys != null) { + tagKeys.forEach(app.getTags()::remove); + } + putApplication(app); + return app.getTags(); + } + + public Snapshot createApplicationSnapshot(String applicationName, String snapshotName) { + FlinkApplication app = describeApplication(applicationName); + if (snapshotName == null || snapshotName.isBlank()) { + throw new AwsException("InvalidArgumentException", "SnapshotName is required", 400); + } + // Real AWS requires a live, running job to snapshot — a bare cluster (no code) never has one. + if (app.getApplicationStatus() != ApplicationStatus.RUNNING || !app.hasCode()) { + throw new AwsException("InvalidRequestException", + "Application " + applicationName + " must be RUNNING with a deployed job to " + + "create a snapshot", 400); + } + if (!app.isSnapshotsEnabled()) { + throw new AwsException("InvalidRequestException", + "Snapshots are not enabled for application " + applicationName, 400); + } + if (app.getSnapshots().containsKey(snapshotName)) { + throw new AwsException("ResourceInUseException", + "Snapshot already exists: " + snapshotName, 400); + } + + Snapshot snapshot = new Snapshot(snapshotName, app.getApplicationVersionId(), app.getRuntimeEnvironment()); + if (config.services().kinesisAnalytics().mock()) { + // No backing job to snapshot: come up READY immediately, same as StartApplication's + // mock-mode shortcut. + snapshot.setSnapshotStatus(SnapshotStatus.READY); + } else { + try { + containerManager.createSnapshot(app, snapshot); + } catch (RuntimeException e) { + LOG.errorv(e, "Failed to trigger snapshot {0} for application {1}", + snapshotName, applicationName); + snapshot.setSnapshotStatus(SnapshotStatus.FAILED); + } + } + app.getSnapshots().put(snapshotName, snapshot); + putApplication(app); + LOG.infov("Creating Kinesis Analytics V2 snapshot {0} for application {1}", + snapshotName, applicationName); + return snapshot; + } + + public Snapshot describeApplicationSnapshot(String applicationName, String snapshotName) { + FlinkApplication app = describeApplication(applicationName); + Snapshot snapshot = app.getSnapshots().get(snapshotName); + if (snapshot == null) { + throw new AwsException("ResourceNotFoundException", + "Snapshot not found: " + snapshotName, 400); + } + return snapshot; + } + + public List listApplicationSnapshots(String applicationName) { + return List.copyOf(describeApplication(applicationName).getSnapshots().values()); + } + + public void deleteApplicationSnapshot(String applicationName, String snapshotName, + Instant snapshotCreationTimestamp) { + FlinkApplication app = describeApplication(applicationName); + Snapshot snapshot = app.getSnapshots().get(snapshotName); + if (snapshot == null) { + throw new AwsException("ResourceNotFoundException", + "Snapshot not found: " + snapshotName, 400); + } + if (snapshotCreationTimestamp == null || snapshot.getSnapshotCreationTimestamp() == null + || snapshot.getSnapshotCreationTimestamp().getEpochSecond() + != snapshotCreationTimestamp.getEpochSecond()) { + throw new AwsException("InvalidArgumentException", + "Provided SnapshotCreationTimestamp does not match snapshot " + snapshotName, 400); + } + if (snapshot.getSnapshotStatus() == SnapshotStatus.CREATING) { + throw new AwsException("ResourceInUseException", + "Snapshot " + snapshotName + " cannot be deleted while still being created", 400); + } + if (!config.services().kinesisAnalytics().mock()) { + containerManager.deleteSnapshotFiles(app, snapshot); + } + app.getSnapshots().remove(snapshotName); + putApplication(app); + LOG.infov("Deleted Kinesis Analytics V2 snapshot {0} for application {1}", + snapshotName, applicationName); + } + + /** + * Real AWS returns a session-authorized URL to the application's Flink Dashboard (or, for a + * Zeppelin Studio notebook, its UI — not supported here since floci already rejects Zeppelin + * runtimes at CreateApplication). Since floci has no real IAM-backed session/proxy layer for this, + * it returns the JobManager's own REST/dashboard URL directly rather than a genuinely time-limited, + * signed one — the same "real behavior, stubbed authorization" tradeoff MWAA's CreateWebLoginToken + * makes for its own SSO handshake. + */ + public String createApplicationPresignedUrl(String applicationName, String urlType, + Long sessionExpirationDurationInSeconds) { + FlinkApplication app = describeApplication(applicationName); + if (!"FLINK_DASHBOARD_URL".equals(urlType)) { + throw new AwsException("InvalidArgumentException", + "Unsupported UrlType: " + urlType + "; only FLINK_DASHBOARD_URL is supported", 400); + } + if (sessionExpirationDurationInSeconds != null + && (sessionExpirationDurationInSeconds < MIN_SESSION_EXPIRATION_SECONDS + || sessionExpirationDurationInSeconds > MAX_SESSION_EXPIRATION_SECONDS)) { + throw new AwsException("InvalidArgumentException", + "SessionExpirationDurationInSeconds must be between " + MIN_SESSION_EXPIRATION_SECONDS + + " and " + MAX_SESSION_EXPIRATION_SECONDS, 400); + } + if (app.getApplicationStatus() != ApplicationStatus.RUNNING || app.getRestEndpoint() == null) { + throw new AwsException("ResourceInUseException", + "Application " + applicationName + " is not available for this operation", 400); + } + return app.getRestEndpoint(); + } + + private FlinkApplication findByArn(String resourceArn) { + if (resourceArn == null || resourceArn.isBlank()) { + throw new AwsException("InvalidArgumentException", "ResourceARN is required", 400); + } + // Resource format is "application/"; application names never contain '/'. + int slash = resourceArn.lastIndexOf('/'); + if (slash < 0 || slash == resourceArn.length() - 1) { + throw new AwsException("InvalidArgumentException", "Invalid resource ARN: " + resourceArn, 400); + } + String applicationName = resourceArn.substring(slash + 1); + return storage.get(applicationName) + .orElseThrow(() -> new AwsException("ResourceNotFoundException", + "No application found for ARN: " + resourceArn, 400)); + } + + private void startReadinessPoller() { + poller.scheduleAtFixedRate(() -> { + try { + if (config.services().kinesisAnalytics().mock()) { + return; + } + for (FlinkApplication app : allApplications()) { + boolean changed = false; + if (app.getApplicationStatus() == ApplicationStatus.STARTING) { + String previousJobId = app.getFlinkJobId(); + if (containerManager.advanceToRunning(app)) { + LOG.infov("Kinesis Analytics V2 application {0} is now RUNNING", + app.getApplicationName()); + app.setApplicationStatus(ApplicationStatus.RUNNING); + app.setLastUpdateTimestamp(Instant.now()); + changed = true; + } else if (!Objects.equals(previousJobId, app.getFlinkJobId())) { + // The Flink job was just submitted (flinkJobId newly assigned) but hasn't + // reached RUNNING yet. Persist now rather than waiting for that: pendingJars + // (an in-process-only cache) is already cleared at this point, so if the + // emulator restarts before the next RUNNING-triggered persist, an unpersisted + // flinkJobId would leave the application stuck — it could never re-fetch the + // JAR to resubmit, nor know a job was already running to poll instead. + changed = true; + } + } + for (Snapshot snapshot : app.getSnapshots().values()) { + if (snapshot.getSnapshotStatus() == SnapshotStatus.CREATING + && containerManager.advanceSnapshot(app, snapshot)) { + changed = true; + } + } + if (changed) { + putApplication(app); + } + } + } catch (Exception e) { + LOG.error("Error in Kinesis Analytics V2 readiness poller", e); + } + }, 1, 2, TimeUnit.SECONDS); + } + + private List allApplications() { + if (storage instanceof AccountAwareStorageBackend aware) { + return aware.scanAllAccounts(); + } + return storage.scan(k -> true); + } + + private void putApplication(FlinkApplication app) { + if (app.getAccountId() != null && storage instanceof AccountAwareStorageBackend aware) { + aware.putForAccount(app.getAccountId(), app.getApplicationName(), app); + } else { + storage.put(app.getApplicationName(), app); + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java new file mode 100644 index 0000000000..b873e5e52a --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java @@ -0,0 +1,526 @@ +package io.github.hectorvent.floci.services.kinesisanalytics.container; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.docker.ContainerBuilder; +import io.github.hectorvent.floci.core.common.docker.ContainerDetector; +import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager; +import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo; +import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo; +import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer; +import io.github.hectorvent.floci.core.common.docker.ContainerSpec; +import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper; +import io.github.hectorvent.floci.services.kinesisanalytics.KinesisAnalyticsRuntimes; +import io.github.hectorvent.floci.services.kinesisanalytics.model.FlinkApplication; +import io.github.hectorvent.floci.services.kinesisanalytics.model.Snapshot; +import io.github.hectorvent.floci.services.kinesisanalytics.model.SnapshotStatus; +import io.github.hectorvent.floci.services.s3.S3Service; +import io.github.hectorvent.floci.services.s3.model.S3Object; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import com.github.dockerjava.api.async.ResultCallback; +import com.github.dockerjava.api.model.Frame; +import com.github.dockerjava.api.model.StreamType; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; +import org.jboss.logging.Logger; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.Closeable; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; + +/** + * Manages the backing Apache Flink cluster for a Managed Service for Apache Flink application. + * + *

The cluster is a standalone session cluster: a **JobManager** container (REST/UI on 8081) plus, + * when the application has a code artifact to run, a **TaskManager** container that shares the + * JobManager's network namespace ({@code --network container:}) so the two communicate over + * {@code localhost} without a dedicated Docker network. The container joins Floci's Docker network so + * the JobManager can look up {@code http://floci:4566} to consume local Kinesis or MSK streams. + * + *

Job deployment: {@code StartApplication} reads the application JAR from Floci's local S3 (on the + * request thread, so account context is available) and stashes the bytes; the readiness poller then + * uploads and runs it against the cluster via {@link FlinkRestClient} once task slots are available, and + * flips the application to RUNNING when the Flink job reaches {@code RUNNING}. An application without a + * code artifact comes up RUNNING as a bare cluster (no job). + */ +@ApplicationScoped +public class FlinkContainerManager { + + private static final Logger LOG = Logger.getLogger(FlinkContainerManager.class); + private static final int JOBMANAGER_REST_PORT = 8081; + private static final String SAVEPOINTS_MOUNT = "/opt/flink/savepoints"; + + private final ContainerBuilder containerBuilder; + private final ContainerLifecycleManager lifecycleManager; + private final ContainerLogStreamer logStreamer; + private final ContainerDetector containerDetector; + private final EmulatorConfig config; + private final RegionResolver regionResolver; + private final S3Service s3Service; + private final FlinkRestClient flinkRest; + private final ObjectMapper objectMapper; + + private final Map logStreams = new ConcurrentHashMap<>(); + private final Map containerIds = new ConcurrentHashMap<>(); + private final Map taskManagerIds = new ConcurrentHashMap<>(); + // Application JAR bytes read from S3 at StartApplication, pending upload by the readiness poller. + private final Map pendingJars = new ConcurrentHashMap<>(); + // Applications whose job submission hard-failed (e.g. a bad JAR) — not retried by the poller. + private final Set submissionFailed = ConcurrentHashMap.newKeySet(); + + @Inject + public FlinkContainerManager(ContainerBuilder containerBuilder, + ContainerLifecycleManager lifecycleManager, + ContainerLogStreamer logStreamer, + ContainerDetector containerDetector, + EmulatorConfig config, + RegionResolver regionResolver, + S3Service s3Service, + FlinkRestClient flinkRest, + ObjectMapper objectMapper) { + this.containerBuilder = containerBuilder; + this.lifecycleManager = lifecycleManager; + this.logStreamer = logStreamer; + this.containerDetector = containerDetector; + this.config = config; + this.regionResolver = regionResolver; + this.objectMapper = objectMapper; + this.s3Service = s3Service; + this.flinkRest = flinkRest; + } + + /** + * Deterministic JobManager container name for an application, stable across emulator restarts. + * Follows the shared {@code floci--} convention via + * {@link ContainerStorageHelper#resourceName} so a configured {@code resourceNamespace} scopes the + * name (letting multiple Floci instances share a Docker host without collisions). + * + *

Like OpenSearch's domain names, the name is scoped by application name, not by account — two + * accounts using the same application name would map to the same container. This mirrors AWS, where + * an application name is account-and-region unique, and matches the accepted OpenSearch trade-off. + */ + private String containerName(String applicationName) { + return ContainerStorageHelper.resourceName(config, "kinesisanalytics", null, applicationName); + } + + /** + * Starts the JobManager (and, when the application has a code artifact, a TaskManager) for the + * application. Reads the JAR from S3 up front (on the request thread) so a missing artifact fails + * StartApplication fast; the JAR is uploaded/run asynchronously by the readiness poller. + */ + public void startCluster(FlinkApplication app) { + String image = KinesisAnalyticsRuntimes.resolveImage( + config.services().kinesisAnalytics().defaultImage(), app.getRuntimeEnvironment()); + String jmName = containerName(app.getApplicationName()); + String tmName = jmName + "-tm"; + + // Read the JAR before starting anything so a missing/empty artifact fails fast, before any + // container is created. (S3 read runs on the request thread → account context is available.) + byte[] jarBytes = app.hasCode() ? readJar(app) : null; + + LOG.infov("Starting Flink cluster for application {0} using image {1}{2}", + app.getApplicationName(), image, app.hasCode() ? " (with TaskManager)" : ""); + lifecycleManager.removeIfExists(jmName); + lifecycleManager.removeIfExists(tmName); + + // rpc.address=localhost so a same-netns TaskManager reaches the JobManager over loopback; + // rest.bind-address=0.0.0.0 so the REST/UI is reachable via the mapped host port / container IP. + String jmProps = "jobmanager.rpc.address: localhost\nrest.bind-address: 0.0.0.0"; + ContainerBuilder.Builder jmSpec = containerBuilder.newContainer(image) + .withName(jmName) + .withCmd("jobmanager") + .withEnv("FLINK_PROPERTIES", jmProps) + .withDockerNetwork(config.services().dockerNetwork()) + .withLogRotation(); + // Named volume (not the container's ephemeral filesystem) so snapshots survive a + // Stop/StartApplication cycle — stopCluster() removes the JobManager container, but this + // volume is only removed on DeleteApplication (removeSavepointsVolume), mirroring how other + // Docker-backed services keep persistent data outside the container lifecycle. + ContainerStorageHelper.applyStorage(jmSpec, lifecycleManager, config, "kinesisanalytics", + app.getApplicationName() + "-savepoints", app.getApplicationName() + "-savepoints", + SAVEPOINTS_MOUNT); + if (!containerDetector.isRunningInContainer()) { + jmSpec.withDynamicPort(JOBMANAGER_REST_PORT); + } else { + jmSpec.withExposedPort(JOBMANAGER_REST_PORT); + } + + ContainerInfo jm; + try { + jm = lifecycleManager.createAndStart(jmSpec.build()); + } catch (RuntimeException e) { + lifecycleManager.removeIfExists(jmName); + throw e; + } + app.setContainerId(jm.containerId()); + containerIds.put(app.getApplicationName(), jm.containerId()); + EndpointInfo rest = jm.getEndpoint(JOBMANAGER_REST_PORT); + app.setRestEndpoint("http://" + rest.host() + ":" + rest.port()); + attachLogs(app, jm.containerId()); + LOG.infov("Flink JobManager {0} started for application {1}: rest={2}", + jm.containerId(), app.getApplicationName(), app.getRestEndpoint()); + + if (app.hasCode()) { + // TaskManager shares the JobManager's network namespace so it registers over localhost and + // provides the task slots the job needs to run. + String tmProps = "jobmanager.rpc.address: localhost\n" + + "taskmanager.numberOfTaskSlots: " + Math.max(1, app.getParallelism()); + ContainerSpec tmSpec = containerBuilder.newContainer(image) + .withName(tmName) + .withCmd("taskmanager") + .withEnv("FLINK_PROPERTIES", tmProps) + .withNetworkMode("container:" + jm.containerId()) + .withLogRotation() + .build(); + try { + ContainerInfo tm = lifecycleManager.createAndStart(tmSpec); + app.setTaskManagerContainerId(tm.containerId()); + taskManagerIds.put(app.getApplicationName(), tm.containerId()); + } catch (RuntimeException e) { + // Roll back the whole cluster so a failed start leaves nothing behind. + lifecycleManager.removeIfExists(tmName); + stopCluster(app); + throw e; + } + // A real MSF environment always provides this file to every Flink process (JobManager and + // TaskManager) so KinesisAnalyticsRuntime.getApplicationProperties() never has to handle a + // missing file, even when no EnvironmentProperties were configured (then it's an empty + // array). User code's main() runs on the JobManager during job submission; a sink/source's + // open() can run on either, so both containers get it. + // /etc/flink does not exist in the stock apache/flink image (confirmed against the real + // image) and the container's non-root "flink" user can't create it — but naming the tar + // entry "flink/application_properties.json" and extracting into the existing /etc lets the + // archive extraction itself create the subdirectory, which the daemon-level copy API can do + // even though the in-container user couldn't (verified live: root:root 775, world-readable). + byte[] propertiesJson = applicationPropertiesJson(app); + copyFileIntoContainer(jm.containerId(), "/etc", "flink/application_properties.json", propertiesJson); + copyFileIntoContainer(app.getTaskManagerContainerId(), "/etc", "flink/application_properties.json", + propertiesJson); + pendingJars.put(app.getApplicationName(), jarBytes); + submissionFailed.remove(app.getApplicationName()); + } + } + + /** Builds {@code /etc/flink/application_properties.json} exactly matching the real MSF/KDA runtime + * file shape: a JSON array of {@code {PropertyGroupId, PropertyMap}} objects, the same shape as + * the AWS API's own {@code EnvironmentProperties.PropertyGroups}. Package-private (not private) + * so FlinkContainerManagerTest can assert on the JSON shape directly. */ + byte[] applicationPropertiesJson(FlinkApplication app) { + ArrayNode root = objectMapper.createArrayNode(); + app.getEnvironmentProperties().forEach((groupId, properties) -> { + ObjectNode group = root.addObject(); + group.put("PropertyGroupId", groupId); + ObjectNode map = group.putObject("PropertyMap"); + properties.forEach(map::put); + }); + try { + return objectMapper.writeValueAsBytes(root); + } catch (IOException e) { + throw new IllegalStateException("Could not serialize application_properties.json", e); + } + } + + private void copyFileIntoContainer(String containerId, String remoteDir, String relativePath, byte[] content) { + if (containerId == null) { + return; + } + try { + lifecycleManager.getDockerClient() + .copyArchiveToContainerCmd(containerId) + .withTarInputStream(new ByteArrayInputStream(tarSingleFile(relativePath, content))) + .withRemotePath(remoteDir) + .exec(); + } catch (Exception e) { + LOG.warnv("Could not copy {0} into Flink container {1}: {2}", relativePath, containerId, e.getMessage()); + } + } + + private static byte[] tarSingleFile(String entryName, byte[] content) { + try { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (TarArchiveOutputStream tar = new TarArchiveOutputStream(out)) { + TarArchiveEntry entry = new TarArchiveEntry(entryName); + entry.setSize(content.length); + entry.setMode(0644); + tar.putArchiveEntry(entry); + tar.write(content); + tar.closeArchiveEntry(); + } + return out.toByteArray(); + } catch (IOException e) { + throw new IllegalStateException("Could not build in-memory tar for " + entryName, e); + } + } + + private byte[] readJar(FlinkApplication app) { + try { + S3Object obj = s3Service.getObject(app.getCodeS3Bucket(), app.getCodeS3Key(), + app.getCodeS3ObjectVersion()); + byte[] data = obj != null ? obj.getData() : null; + if (data == null || data.length == 0) { + throw new AwsException("InvalidArgumentException", + "Application code object is empty: s3://" + app.getCodeS3Bucket() + "/" + + app.getCodeS3Key(), 400); + } + return data; + } catch (AwsException e) { + throw e; + } catch (Exception e) { + throw new AwsException("InvalidArgumentException", + "Unable to fetch application code from s3://" + app.getCodeS3Bucket() + "/" + + app.getCodeS3Key() + ": " + e.getMessage(), 400); + } + } + + /** + * Drives the application toward RUNNING and reports whether it has reached it. For a bare cluster + * (no code), RUNNING once the JobManager REST answers. For an application with code, this uploads + * and runs the stashed JAR once task slots are available, then reports RUNNING when the Flink job + * reaches {@code RUNNING}. Safe to call repeatedly from the readiness poller. + */ + public boolean advanceToRunning(FlinkApplication app) { + String rest = app.getRestEndpoint(); + if (rest == null || !flinkRest.isRestUp(rest)) { + return false; + } + if (!app.hasCode()) { + return true; + } + if (app.getFlinkJobId() == null) { + if (submissionFailed.contains(app.getApplicationName())) { + return false; + } + if (flinkRest.totalSlots(rest) < Math.max(1, app.getParallelism())) { + return false; // TaskManager not registered yet + } + byte[] jar = pendingJars.get(app.getApplicationName()); + if (jar == null) { + return false; // stashed at StartApplication; absent only after an emulator restart + } + try { + String jarId = flinkRest.uploadJar(rest, jar); + String jobId = flinkRest.runJob(rest, jarId, app.getParallelism()); + app.setFlinkJobId(jobId); + pendingJars.remove(app.getApplicationName()); + LOG.infov("Submitted Flink job {0} for application {1}", jobId, app.getApplicationName()); + } catch (Exception e) { + // Hard failure (e.g. a JAR with no main class) — do not resubmit every tick. + submissionFailed.add(app.getApplicationName()); + LOG.errorv(e, "Failed to submit Flink job for application {0}", app.getApplicationName()); + } + return false; + } + return "RUNNING".equals(flinkRest.jobState(rest, app.getFlinkJobId())); + } + + /** + * Swaps in a new application JAR on an already-running cluster ({@code UpdateApplication} with a + * new {@code ApplicationCodeConfigurationUpdate}), without tearing down the JobManager/TaskManager + * containers: cancels the current job (if any) and refreshes {@code application_properties.json}, + * then stashes the new JAR the same way {@link #startCluster} does so the readiness poller + * ({@link #advanceToRunning}) picks it up and resubmits it as soon as task slots free up. Callers + * must only invoke this when the application already has a TaskManager (a running job) — attaching + * code to a bare cluster for the first time is not supported here. + */ + public void redeployCode(FlinkApplication app) { + String rest = app.getRestEndpoint(); + if (rest != null && app.getFlinkJobId() != null) { + flinkRest.cancelJob(rest, app.getFlinkJobId()); + app.setFlinkJobId(null); + } + byte[] propertiesJson = applicationPropertiesJson(app); + copyFileIntoContainer(app.getContainerId(), "/etc", "flink/application_properties.json", propertiesJson); + copyFileIntoContainer(app.getTaskManagerContainerId(), "/etc", "flink/application_properties.json", + propertiesJson); + pendingJars.put(app.getApplicationName(), readJar(app)); + submissionFailed.remove(app.getApplicationName()); + LOG.infov("Redeployed code for Kinesis Analytics V2 application {0}", app.getApplicationName()); + } + + public void stopCluster(FlinkApplication app) { + String rest = app.getRestEndpoint(); + if (rest != null && app.getFlinkJobId() != null) { + flinkRest.cancelJob(rest, app.getFlinkJobId()); + } + pendingJars.remove(app.getApplicationName()); + submissionFailed.remove(app.getApplicationName()); + + // Stop the TaskManager first, then the JobManager (whose netns it shares). + String tmId = taskManagerIds.remove(app.getApplicationName()); + if (tmId == null) { + tmId = app.getTaskManagerContainerId(); + } + if (tmId != null) { + lifecycleManager.stopAndRemove(tmId, null); + } else { + lifecycleManager.removeIfExists(containerName(app.getApplicationName()) + "-tm"); + } + + containerIds.remove(app.getApplicationName()); + Closeable logHandle = logStreams.remove(app.getApplicationName()); + String jmId = app.getContainerId(); + if (jmId != null) { + lifecycleManager.stopAndRemove(jmId, logHandle); + LOG.infov("Flink cluster for application {0} stopped and removed", app.getApplicationName()); + } else { + lifecycleManager.removeIfExists(containerName(app.getApplicationName())); + } + app.setContainerId(null); + app.setRestEndpoint(null); + app.setTaskManagerContainerId(null); + app.setFlinkJobId(null); + } + + /** + * Triggers a Flink savepoint for the application's running job. Requires the application to + * actually be RUNNING (a live {@code flinkJobId} and REST endpoint) — the caller + * ({@link io.github.hectorvent.floci.services.kinesisanalytics.KinesisAnalyticsV2Service}) is + * responsible for that AWS-shaped precondition check. Stores the async operation's + * {@code request-id} on the snapshot for {@link #advanceSnapshot} to poll; throws on a failure to + * even start the trigger (e.g. JobManager unreachable) so the caller can mark the snapshot FAILED. + */ + public void createSnapshot(FlinkApplication app, Snapshot snapshot) { + try { + String requestId = flinkRest.triggerSavepoint(app.getRestEndpoint(), app.getFlinkJobId(), + SAVEPOINTS_MOUNT); + snapshot.setFlinkRequestId(requestId); + LOG.infov("Triggered Flink savepoint for application {0} snapshot {1}: request {2}", + app.getApplicationName(), snapshot.getSnapshotName(), requestId); + } catch (Exception e) { + throw new RuntimeException("Failed to trigger Flink savepoint: " + e.getMessage(), e); + } + } + + /** + * Polls a CREATING snapshot toward a terminal state, mutating {@code snapshot}'s status in place. + * Returns {@code true} once terminal (READY or FAILED) so the caller stops polling; {@code false} + * while still in progress, including when the JobManager probe itself fails (transient — the + * caller should keep retrying, same as {@code advanceToRunning}'s job-state polling). + */ + public boolean advanceSnapshot(FlinkApplication app, Snapshot snapshot) { + String rest = app.getRestEndpoint(); + if (rest == null || snapshot.getFlinkRequestId() == null) { + return false; + } + FlinkRestClient.SavepointStatus status = flinkRest.savepointStatus(rest, app.getFlinkJobId(), + snapshot.getFlinkRequestId()); + if (status == null || !"COMPLETED".equals(status.statusId())) { + return false; // still IN_PROGRESS, or a transient probe failure — keep polling + } + if (status.failed()) { + snapshot.setSnapshotStatus(SnapshotStatus.FAILED); + LOG.warnv("Flink savepoint failed for application {0} snapshot {1}", + app.getApplicationName(), snapshot.getSnapshotName()); + } else { + snapshot.setSnapshotStatus(SnapshotStatus.READY); + snapshot.setFlinkLocation(status.location()); + LOG.infov("Flink savepoint ready for application {0} snapshot {1}: {2}", + app.getApplicationName(), snapshot.getSnapshotName(), status.location()); + } + return true; + } + + /** Best-effort removal of a snapshot's files. A no-op if the JobManager isn't currently running — + * the files remain in the savepoints volume until {@link #removeSavepointsVolume} on delete. */ + public void deleteSnapshotFiles(FlinkApplication app, Snapshot snapshot) { + String containerId = app.getContainerId(); + if (containerId == null || snapshot.getFlinkLocation() == null) { + return; + } + try { + execInContainer(containerId, new String[]{"rm", "-rf", snapshot.getFlinkLocation()}); + } catch (Exception e) { + LOG.warnv("Could not remove savepoint files at {0} for application {1}: {2}", + snapshot.getFlinkLocation(), app.getApplicationName(), e.getMessage()); + } + } + + /** Removes the persistent savepoints volume. Called only on DeleteApplication, never on a plain + * StopApplication (stopCluster), so snapshots survive a stop/restart cycle. */ + public void removeSavepointsVolume(FlinkApplication app) { + ContainerStorageHelper.removeStorage(config, lifecycleManager, "kinesisanalytics", + app.getApplicationName() + "-savepoints", app.getApplicationName() + "-savepoints"); + } + + /** + * Stops and removes every running Flink container (JobManagers and TaskManagers). Wired into + * {@code EmulatorLifecycle.onStop()} so containers are torn down on shutdown alongside the other + * container managers. + */ + public void stopAll() { + if (!containerIds.isEmpty()) { + LOG.infov("Stopping {0} Flink cluster(s) on shutdown", containerIds.size()); + } + for (String applicationName : new ArrayList<>(taskManagerIds.keySet())) { + String tmId = taskManagerIds.remove(applicationName); + if (tmId != null) { + lifecycleManager.stopAndRemove(tmId, null); + } + } + for (String applicationName : new ArrayList<>(containerIds.keySet())) { + String jmId = containerIds.remove(applicationName); + if (jmId == null) { + continue; + } + Closeable logHandle = logStreams.remove(applicationName); + lifecycleManager.stopAndRemove(jmId, logHandle); + } + } + + private void attachLogs(FlinkApplication app, String containerId) { + String shortId = containerId.length() >= 8 ? containerId.substring(0, 8) : containerId; + String logGroup = "/aws/kinesis-analytics/" + app.getApplicationName(); + String logStream = logStreamer.generateLogStreamName(shortId); + String region = regionResolver.getDefaultRegion(); + Closeable logHandle = logStreamer.attach(containerId, logGroup, logStream, region, + "kinesisanalytics:" + app.getApplicationName()); + if (logHandle != null) { + logStreams.put(app.getApplicationName(), logHandle); + } + } + + private ExecResult execInContainer(String containerId, String[] cmd) throws Exception { + var dockerClient = lifecycleManager.getDockerClient(); + var exec = dockerClient.execCreateCmd(containerId) + .withCmd(cmd) + .withAttachStdout(true) + .withAttachStderr(true) + .exec(); + + ByteArrayOutputStream stdout = new ByteArrayOutputStream(); + ByteArrayOutputStream stderr = new ByteArrayOutputStream(); + boolean completed = dockerClient.execStartCmd(exec.getId()) + .exec(new ResultCallback.Adapter() { + @Override + public void onNext(Frame frame) { + if (frame.getStreamType() == StreamType.STDERR) { + stderr.writeBytes(frame.getPayload()); + } else { + stdout.writeBytes(frame.getPayload()); + } + } + }) + .awaitCompletion(15, TimeUnit.SECONDS); + + if (!completed) { + throw new RuntimeException("exec timed out in container " + containerId); + } + Long exitCode = dockerClient.inspectExecCmd(exec.getId()).exec().getExitCodeLong(); + return new ExecResult(exitCode != null ? exitCode : -1, + stdout.toString(StandardCharsets.UTF_8), stderr.toString(StandardCharsets.UTF_8)); + } + + private record ExecResult(long exitCode, String stdout, String stderr) {} +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkRestClient.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkRestClient.java new file mode 100644 index 0000000000..ca0de4e169 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkRestClient.java @@ -0,0 +1,197 @@ +package io.github.hectorvent.floci.services.kinesisanalytics.container; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.enterprise.context.ApplicationScoped; +import org.jboss.logging.Logger; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Duration; + +/** + * Thin client for the Apache Flink JobManager REST API, used to deploy an application's JAR and drive + * its job lifecycle: upload the JAR ({@code POST /jars/upload}), run it ({@code POST /jars/:id/run}), + * poll job state ({@code GET /jobs/:id}) and cancel it ({@code PATCH /jobs/:id?mode=cancel}). + * + *

Uses the JDK {@link HttpClient} (as the rest of Floci does). Read-only probes swallow errors and + * degrade to "not ready" so the readiness poller can retry; the deploy calls surface failures. + */ +@ApplicationScoped +public class FlinkRestClient { + + private static final Logger LOG = Logger.getLogger(FlinkRestClient.class); + + private final HttpClient http = HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(3)) + .build(); + private final ObjectMapper mapper = new ObjectMapper(); + + /** True once the JobManager REST API answers on {@code /config} (the cluster is up). */ + public boolean isRestUp(String restBase) { + try { + HttpResponse resp = http.send( + HttpRequest.newBuilder(URI.create(restBase + "/config")) + .timeout(Duration.ofSeconds(2)).GET().build(), + HttpResponse.BodyHandlers.discarding()); + return resp.statusCode() == 200; + } catch (Exception e) { + // Expected while the JobManager is still booting. + LOG.debugf("Flink REST %s not up yet: %s", restBase, e.toString()); + return false; + } + } + + /** Total task slots the cluster advertises (TaskManagers registered); 0 if the probe fails. */ + public int totalSlots(String restBase) { + try { + HttpResponse resp = http.send( + HttpRequest.newBuilder(URI.create(restBase + "/overview")) + .timeout(Duration.ofSeconds(3)).GET().build(), + HttpResponse.BodyHandlers.ofString()); + if (resp.statusCode() != 200) { + return 0; + } + return mapper.readTree(resp.body()).path("slots-total").asInt(0); + } catch (Exception e) { + LOG.debugf("Flink /overview probe on %s failed: %s", restBase, e.toString()); + return 0; + } + } + + /** + * Uploads a JAR via {@code POST /jars/upload} (multipart form field {@code jarfile}) and returns + * the jar id (the basename of the returned {@code filename}), used by {@link #runJob}. + */ + public String uploadJar(String restBase, byte[] jar) throws IOException, InterruptedException { + String boundary = "flociFlinkBoundary" + System.nanoTime(); + String head = "--" + boundary + "\r\n" + + "Content-Disposition: form-data; name=\"jarfile\"; filename=\"application.jar\"\r\n" + + "Content-Type: application/x-java-archive\r\n\r\n"; + String tail = "\r\n--" + boundary + "--\r\n"; + ByteArrayOutputStream body = new ByteArrayOutputStream(); + body.write(head.getBytes(StandardCharsets.UTF_8)); + body.write(jar); + body.write(tail.getBytes(StandardCharsets.UTF_8)); + + HttpResponse resp = http.send( + HttpRequest.newBuilder(URI.create(restBase + "/jars/upload")) + .timeout(Duration.ofSeconds(60)) + .header("Content-Type", "multipart/form-data; boundary=" + boundary) + .POST(HttpRequest.BodyPublishers.ofByteArray(body.toByteArray())) + .build(), + HttpResponse.BodyHandlers.ofString()); + if (resp.statusCode() != 200) { + throw new IOException("Flink /jars/upload failed: HTTP " + resp.statusCode() + " " + resp.body()); + } + String filename = mapper.readTree(resp.body()).path("filename").asText(""); + int slash = filename.lastIndexOf('/'); + return slash >= 0 ? filename.substring(slash + 1) : filename; + } + + /** Runs an uploaded jar via {@code POST /jars/:id/run} and returns the assigned Flink job id. */ + public String runJob(String restBase, String jarId, int parallelism) throws IOException, InterruptedException { + String requestBody = "{\"parallelism\":" + Math.max(1, parallelism) + "}"; + HttpResponse resp = http.send( + HttpRequest.newBuilder(URI.create(restBase + "/jars/" + jarId + "/run")) + .timeout(Duration.ofSeconds(30)) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(requestBody)) + .build(), + HttpResponse.BodyHandlers.ofString()); + if (resp.statusCode() != 200) { + throw new IOException("Flink /jars/" + jarId + "/run failed: HTTP " + resp.statusCode() + + " " + resp.body()); + } + return mapper.readTree(resp.body()).path("jobid").asText(null); + } + + /** Current job state (e.g. {@code RUNNING}, {@code FINISHED}, {@code FAILED}); null if unknown. */ + public String jobState(String restBase, String jobId) { + try { + HttpResponse resp = http.send( + HttpRequest.newBuilder(URI.create(restBase + "/jobs/" + jobId)) + .timeout(Duration.ofSeconds(5)).GET().build(), + HttpResponse.BodyHandlers.ofString()); + if (resp.statusCode() != 200) { + return null; + } + return mapper.readTree(resp.body()).path("state").asText(null); + } catch (Exception e) { + LOG.debugf("Flink /jobs/%s probe on %s failed: %s", jobId, restBase, e.toString()); + return null; + } + } + + /** Best-effort cancel via {@code PATCH /jobs/:id?mode=cancel}. */ + public void cancelJob(String restBase, String jobId) { + try { + http.send( + HttpRequest.newBuilder(URI.create(restBase + "/jobs/" + jobId + "?mode=cancel")) + .timeout(Duration.ofSeconds(10)) + .method("PATCH", HttpRequest.BodyPublishers.noBody()).build(), + HttpResponse.BodyHandlers.discarding()); + } catch (Exception e) { + // Best-effort: the container is about to be removed anyway. + LOG.debugf("Flink cancel of job %s on %s failed: %s", jobId, restBase, e.toString()); + } + } + + /** + * Triggers a savepoint via {@code POST /jobs/:id/savepoints} and returns the async operation's + * {@code request-id}, used to poll completion via {@link #savepointStatus}. + */ + public String triggerSavepoint(String restBase, String jobId, String targetDirectory) + throws IOException, InterruptedException { + String requestBody = "{\"target-directory\":\"" + targetDirectory + "\",\"cancel-job\":false}"; + HttpResponse resp = http.send( + HttpRequest.newBuilder(URI.create(restBase + "/jobs/" + jobId + "/savepoints")) + .timeout(Duration.ofSeconds(10)) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(requestBody)) + .build(), + HttpResponse.BodyHandlers.ofString()); + if (resp.statusCode() != 202 && resp.statusCode() != 200) { + throw new IOException("Flink /jobs/" + jobId + "/savepoints trigger failed: HTTP " + + resp.statusCode() + " " + resp.body()); + } + return mapper.readTree(resp.body()).path("request-id").asText(null); + } + + /** + * Polls {@code GET /jobs/:id/savepoints/:requestId}. {@code statusId} is Flink's async-operation + * status ({@code IN_PROGRESS}/{@code COMPLETED}); null if the probe itself failed (network/parse + * error, e.g. the JobManager isn't reachable — different from a Flink-reported failure). + */ + public SavepointStatus savepointStatus(String restBase, String jobId, String requestId) { + try { + HttpResponse resp = http.send( + HttpRequest.newBuilder(URI.create(restBase + "/jobs/" + jobId + "/savepoints/" + requestId)) + .timeout(Duration.ofSeconds(5)).GET().build(), + HttpResponse.BodyHandlers.ofString()); + if (resp.statusCode() != 200) { + return null; + } + var root = mapper.readTree(resp.body()); + String statusId = root.path("status").path("id").asText(null); + var operation = root.path("operation"); + boolean failed = operation.has("failure-cause"); + String location = operation.path("location").asText(null); + return new SavepointStatus(statusId, location, failed); + } catch (Exception e) { + LOG.debugf("Flink savepoint status probe (job %s, request %s) on %s failed: %s", + jobId, requestId, restBase, e.toString()); + return null; + } + } + + /** {@code statusId}: Flink's async-operation status ({@code IN_PROGRESS}/{@code COMPLETED}). + * {@code location}: the savepoint's path once completed successfully. {@code failed}: true when + * Flink reports a {@code failure-cause} (the savepoint itself failed, as opposed to still running). */ + public record SavepointStatus(String statusId, String location, boolean failed) {} +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/ApplicationStatus.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/ApplicationStatus.java new file mode 100644 index 0000000000..e27eac3d7d --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/ApplicationStatus.java @@ -0,0 +1,24 @@ +package io.github.hectorvent.floci.services.kinesisanalytics.model; + +import io.quarkus.runtime.annotations.RegisterForReflection; + +/** + * Lifecycle states for a Managed Service for Apache Flink application, mirroring the + * {@code ApplicationStatus} enum of the AWS Kinesis Analytics V2 API. Serialized by name + * (e.g. {@code "READY"}) to match the wire shape. + * + *

Only the states this emulator transitions through are modelled: + * {@code CreateApplication} lands in {@link #READY}; {@code StartApplication} moves + * {@link #STARTING} → {@link #RUNNING} (the backing Flink container coming up); + * {@code StopApplication} returns to {@link #READY}; {@code DeleteApplication} passes + * through {@link #DELETING}. + */ +@RegisterForReflection +public enum ApplicationStatus { + READY, + STARTING, + RUNNING, + STOPPING, + UPDATING, + DELETING +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/FlinkApplication.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/FlinkApplication.java new file mode 100644 index 0000000000..7480b4518f --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/FlinkApplication.java @@ -0,0 +1,197 @@ +package io.github.hectorvent.floci.services.kinesisanalytics.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import io.quarkus.runtime.annotations.RegisterForReflection; + +import java.time.Instant; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * In-memory representation of a Managed Service for Apache Flink application + * (Kinesis Analytics V2). Mirrors the {@code amazonmq/model/Broker} shape: a mutable + * POJO whose {@link ApplicationStatus} transitions in place (READY → STARTING → RUNNING) + * as the backing Flink container comes up. + * + *

Wire keys are PascalCase to match the Kinesis Analytics V2 (application/x-amz-json-1.1) + * protocol (e.g. {@code ApplicationName}, {@code ApplicationARN}). + */ +@RegisterForReflection +@JsonIgnoreProperties(ignoreUnknown = true) +public class FlinkApplication { + + @JsonProperty("ApplicationName") + private String applicationName; + + @JsonProperty("ApplicationARN") + private String applicationArn; + + @JsonProperty("ApplicationDescription") + private String applicationDescription; + + @JsonProperty("RuntimeEnvironment") + private String runtimeEnvironment; + + @JsonProperty("ServiceExecutionRole") + private String serviceExecutionRole; + + @JsonProperty("ApplicationStatus") + private ApplicationStatus applicationStatus; + + @JsonProperty("ApplicationVersionId") + private long applicationVersionId; + + @JsonProperty("ApplicationMode") + private String applicationMode; + + @JsonProperty("CreateTimestamp") + private Instant createTimestamp; + + @JsonProperty("LastUpdateTimestamp") + private Instant lastUpdateTimestamp; + + // Internal bookkeeping — NOT part of the AWS response shape, but persisted so the + // application stays manageable after an emulator restart in persistent mode (container + // teardown, account-aware storage routing). The handler builds the ApplicationDetail + // response explicitly, so these are never exposed to clients despite being stored. + private String containerId; + + private String accountId; + + // JobManager REST endpoint (host:port) resolved when the container starts; used by the + // readiness probe. Re-resolved on every StartApplication, so it is transient bookkeeping. + private String restEndpoint; + + // TaskManager container id (only present when a job is deployed; the JobManager alone has no + // task slots). Bookkeeping, not part of the AWS shape. + private String taskManagerContainerId; + + // Flink job id assigned once the application JAR is submitted to the cluster; used to poll job + // state and to cancel on stop. Bookkeeping, not part of the AWS shape. + private String flinkJobId; + + // ApplicationCodeConfiguration.CodeContent.S3ContentLocation — the S3 object holding the Flink + // application JAR. These ARE echoed back on DescribeApplication (built explicitly by the handler). + private String codeS3Bucket; + private String codeS3Key; + private String codeS3ObjectVersion; + + // FlinkApplicationConfiguration.ParallelismConfiguration.Parallelism (defaults to 1). + private int parallelism = 1; + + // ApplicationSnapshotConfiguration.SnapshotsEnabled — real AWS defaults this to true when the + // application is created without specifying it. + private boolean snapshotsEnabled = true; + + // Resource tags. Real AWS never echoes these in ApplicationDetail (CreateApplication / + // DescribeApplication) — only ListTagsForResource returns them — so this is bookkeeping only, + // like containerId/accountId above, not part of the wire response the handler builds. + private Map tags = new LinkedHashMap<>(); + + // ApplicationConfiguration.EnvironmentProperties.PropertyGroups, keyed by PropertyGroupId (the + // key a Flink app looks it up by via KinesisAnalyticsRuntime.getApplicationProperties()). Echoed + // back on DescribeApplication (unlike tags) as ApplicationConfigurationDescription + // .EnvironmentPropertyDescriptions.PropertyGroupDescriptions, built explicitly by the handler. + private Map> environmentProperties = new LinkedHashMap<>(); + + // Application snapshots (Flink savepoints), keyed by SnapshotName. Bookkeeping — snapshots are + // only ever returned via CreateApplicationSnapshot/DescribeApplicationSnapshot/ + // ListApplicationSnapshots, never embedded in ApplicationDetail. + private Map snapshots = new LinkedHashMap<>(); + + public FlinkApplication() {} + + public FlinkApplication(String applicationName, String applicationArn, + String runtimeEnvironment, String serviceExecutionRole, + String applicationMode) { + this.applicationName = applicationName; + this.applicationArn = applicationArn; + this.runtimeEnvironment = runtimeEnvironment; + this.serviceExecutionRole = serviceExecutionRole; + this.applicationMode = applicationMode; + this.applicationStatus = ApplicationStatus.READY; + this.applicationVersionId = 1L; + this.createTimestamp = Instant.now(); + this.lastUpdateTimestamp = this.createTimestamp; + } + + public String getApplicationName() { return applicationName; } + public void setApplicationName(String applicationName) { this.applicationName = applicationName; } + + public String getApplicationArn() { return applicationArn; } + public void setApplicationArn(String applicationArn) { this.applicationArn = applicationArn; } + + public String getApplicationDescription() { return applicationDescription; } + public void setApplicationDescription(String applicationDescription) { this.applicationDescription = applicationDescription; } + + public String getRuntimeEnvironment() { return runtimeEnvironment; } + public void setRuntimeEnvironment(String runtimeEnvironment) { this.runtimeEnvironment = runtimeEnvironment; } + + public String getServiceExecutionRole() { return serviceExecutionRole; } + public void setServiceExecutionRole(String serviceExecutionRole) { this.serviceExecutionRole = serviceExecutionRole; } + + public ApplicationStatus getApplicationStatus() { return applicationStatus; } + public void setApplicationStatus(ApplicationStatus applicationStatus) { this.applicationStatus = applicationStatus; } + + public long getApplicationVersionId() { return applicationVersionId; } + public void setApplicationVersionId(long applicationVersionId) { this.applicationVersionId = applicationVersionId; } + + public String getApplicationMode() { return applicationMode; } + public void setApplicationMode(String applicationMode) { this.applicationMode = applicationMode; } + + public Instant getCreateTimestamp() { return createTimestamp; } + public void setCreateTimestamp(Instant createTimestamp) { this.createTimestamp = createTimestamp; } + + public Instant getLastUpdateTimestamp() { return lastUpdateTimestamp; } + public void setLastUpdateTimestamp(Instant lastUpdateTimestamp) { this.lastUpdateTimestamp = lastUpdateTimestamp; } + + public String getContainerId() { return containerId; } + public void setContainerId(String containerId) { this.containerId = containerId; } + + public String getAccountId() { return accountId; } + public void setAccountId(String accountId) { this.accountId = accountId; } + + public String getRestEndpoint() { return restEndpoint; } + public void setRestEndpoint(String restEndpoint) { this.restEndpoint = restEndpoint; } + + public String getTaskManagerContainerId() { return taskManagerContainerId; } + public void setTaskManagerContainerId(String taskManagerContainerId) { this.taskManagerContainerId = taskManagerContainerId; } + + public String getFlinkJobId() { return flinkJobId; } + public void setFlinkJobId(String flinkJobId) { this.flinkJobId = flinkJobId; } + + public String getCodeS3Bucket() { return codeS3Bucket; } + public void setCodeS3Bucket(String codeS3Bucket) { this.codeS3Bucket = codeS3Bucket; } + + public String getCodeS3Key() { return codeS3Key; } + public void setCodeS3Key(String codeS3Key) { this.codeS3Key = codeS3Key; } + + public String getCodeS3ObjectVersion() { return codeS3ObjectVersion; } + public void setCodeS3ObjectVersion(String codeS3ObjectVersion) { this.codeS3ObjectVersion = codeS3ObjectVersion; } + + public int getParallelism() { return parallelism; } + public void setParallelism(int parallelism) { this.parallelism = parallelism; } + + public boolean isSnapshotsEnabled() { return snapshotsEnabled; } + public void setSnapshotsEnabled(boolean snapshotsEnabled) { this.snapshotsEnabled = snapshotsEnabled; } + + public Map getTags() { return tags; } + public void setTags(Map tags) { this.tags = tags != null ? tags : new LinkedHashMap<>(); } + + public Map> getEnvironmentProperties() { return environmentProperties; } + public void setEnvironmentProperties(Map> environmentProperties) { + this.environmentProperties = environmentProperties != null ? environmentProperties : new LinkedHashMap<>(); + } + + public Map getSnapshots() { return snapshots; } + public void setSnapshots(Map snapshots) { + this.snapshots = snapshots != null ? snapshots : new LinkedHashMap<>(); + } + + /** True when the application has a code artifact to deploy (S3 JAR), i.e. a job should run. */ + public boolean hasCode() { + return codeS3Bucket != null && !codeS3Bucket.isBlank() + && codeS3Key != null && !codeS3Key.isBlank(); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/Snapshot.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/Snapshot.java new file mode 100644 index 0000000000..1d4b9d9a80 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/Snapshot.java @@ -0,0 +1,74 @@ +package io.github.hectorvent.floci.services.kinesisanalytics.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import io.quarkus.runtime.annotations.RegisterForReflection; + +import java.time.Instant; + +/** + * An application snapshot (a Flink savepoint under the covers), mirroring AWS's + * {@code SnapshotDetails} shape. Stored embedded on {@link FlinkApplication}, not in its own + * top-level storage — mirrors how tags/environment properties are embedded rather than tracked + * separately, since a snapshot has no independent lifecycle outside its owning application. + */ +@RegisterForReflection +@JsonIgnoreProperties(ignoreUnknown = true) +public class Snapshot { + + @JsonProperty("SnapshotName") + private String snapshotName; + + @JsonProperty("SnapshotStatus") + private SnapshotStatus snapshotStatus; + + @JsonProperty("ApplicationVersionId") + private long applicationVersionId; + + @JsonProperty("SnapshotCreationTimestamp") + private Instant snapshotCreationTimestamp; + + @JsonProperty("RuntimeEnvironment") + private String runtimeEnvironment; + + // Internal bookkeeping — NOT part of the AWS response shape, mirroring how FlinkApplication keeps + // containerId/restEndpoint/flinkJobId out of its own wire-facing fields. + + // The Flink REST /jobs/:id/savepoints trigger's request-id, used to poll completion status. + private String flinkRequestId; + + // The savepoint's location once Flink reports COMPLETED (e.g. + // /opt/flink/savepoints/savepoint--), used to remove the files on delete. + private String flinkLocation; + + public Snapshot() {} + + public Snapshot(String snapshotName, long applicationVersionId, String runtimeEnvironment) { + this.snapshotName = snapshotName; + this.snapshotStatus = SnapshotStatus.CREATING; + this.applicationVersionId = applicationVersionId; + this.snapshotCreationTimestamp = Instant.now(); + this.runtimeEnvironment = runtimeEnvironment; + } + + public String getSnapshotName() { return snapshotName; } + public void setSnapshotName(String snapshotName) { this.snapshotName = snapshotName; } + + public SnapshotStatus getSnapshotStatus() { return snapshotStatus; } + public void setSnapshotStatus(SnapshotStatus snapshotStatus) { this.snapshotStatus = snapshotStatus; } + + public long getApplicationVersionId() { return applicationVersionId; } + public void setApplicationVersionId(long applicationVersionId) { this.applicationVersionId = applicationVersionId; } + + public Instant getSnapshotCreationTimestamp() { return snapshotCreationTimestamp; } + public void setSnapshotCreationTimestamp(Instant snapshotCreationTimestamp) { this.snapshotCreationTimestamp = snapshotCreationTimestamp; } + + public String getRuntimeEnvironment() { return runtimeEnvironment; } + public void setRuntimeEnvironment(String runtimeEnvironment) { this.runtimeEnvironment = runtimeEnvironment; } + + public String getFlinkRequestId() { return flinkRequestId; } + public void setFlinkRequestId(String flinkRequestId) { this.flinkRequestId = flinkRequestId; } + + public String getFlinkLocation() { return flinkLocation; } + public void setFlinkLocation(String flinkLocation) { this.flinkLocation = flinkLocation; } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/SnapshotStatus.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/SnapshotStatus.java new file mode 100644 index 0000000000..1cb3d8d6e3 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/SnapshotStatus.java @@ -0,0 +1,16 @@ +package io.github.hectorvent.floci.services.kinesisanalytics.model; + +import io.quarkus.runtime.annotations.RegisterForReflection; + +/** + * Lifecycle states for an application snapshot, mirroring the {@code SnapshotStatus} enum of the AWS + * Kinesis Analytics V2 API exactly ({@code CREATING}, {@code READY}, {@code DELETING}, {@code FAILED}). + * Serialized by name to match the wire shape. + */ +@RegisterForReflection +public enum SnapshotStatus { + CREATING, + READY, + DELETING, + FAILED +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ae57efdc76..8e1819fe96 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -241,6 +241,13 @@ floci: enabled: true mock: false default-image: "rabbitmq:3-management" + kinesis-analytics: + enabled: true + mock: false + # default-image: optional override; uncomment to pin every application to a single image + # (e.g. a private registry mirror). When unset, the image is chosen from the requested + # RuntimeEnvironment (KinesisAnalyticsRuntimes.imageFor, e.g. FLINK-1_19 -> apache/flink:1.19). + # default-image: "apache/flink:1.18" elasticache: enabled: true proxy-base-port: 6379 diff --git a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java index 9193228408..6386460862 100644 --- a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java +++ b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java @@ -69,6 +69,7 @@ class EmulatorLifecycleTest { @Mock private NeptuneContainerManager neptuneContainerManager; @Mock private NeptuneProxyManager neptuneProxyManager; @Mock private io.github.hectorvent.floci.services.amazonmq.container.RabbitMqManager rabbitMqManager; + @Mock private io.github.hectorvent.floci.services.kinesisanalytics.container.FlinkContainerManager flinkContainerManager; @Mock private RdsService rdsService; @Mock private io.github.hectorvent.floci.services.elbv2.ElbV2Service elbV2Service; @Mock private InitializationHooksRunner initializationHooksRunner; @@ -103,7 +104,7 @@ class EmulatorLifecycleTest { elastiCacheProxyManager, rdsContainerManager, rdsProxyManager, memoryDbContainerManager, memoryDbProxyManager, docDbContainerManager, neptuneContainerManager, neptuneProxyManager, - rabbitMqManager, rdsService, elbV2Service, + rabbitMqManager, flinkContainerManager, rdsService, elbV2Service, initializationHooksRunner, sqsPoller, kinesisPoller, dynamodbStreamsPoller, pipesService, ec2MetadataServer, ecrRegistryManager, flociUiManager, initLifecycleState, schemaCreationWorker, containerTeardowns, persistentPathValidator); diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsRuntimesTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsRuntimesTest.java new file mode 100644 index 0000000000..484cd5b1b9 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsRuntimesTest.java @@ -0,0 +1,67 @@ +package io.github.hectorvent.floci.services.kinesisanalytics; + +import io.github.hectorvent.floci.core.common.AwsException; +import org.junit.jupiter.api.Test; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class KinesisAnalyticsRuntimesTest { + + @Test + void imageForMapsRuntimeToFlinkTag() { + assertEquals("apache/flink:1.18", KinesisAnalyticsRuntimes.imageFor("FLINK-1_18")); + assertEquals("apache/flink:1.19", KinesisAnalyticsRuntimes.imageFor("FLINK-1_19")); + assertEquals("apache/flink:1.20", KinesisAnalyticsRuntimes.imageFor("FLINK-1_20")); + // Flink 2.x line + assertEquals("apache/flink:2.0", KinesisAnalyticsRuntimes.imageFor("FLINK-2_0")); + assertEquals("apache/flink:2.2", KinesisAnalyticsRuntimes.imageFor("FLINK-2_2")); + assertEquals("apache/flink:2.3", KinesisAnalyticsRuntimes.imageFor("FLINK-2_3")); + } + + @Test + void imageForFallsBackToDefaultWhenBlank() { + assertEquals(KinesisAnalyticsRuntimes.imageFor(KinesisAnalyticsRuntimes.DEFAULT_RUNTIME), + KinesisAnalyticsRuntimes.imageFor(null)); + assertEquals(KinesisAnalyticsRuntimes.imageFor(KinesisAnalyticsRuntimes.DEFAULT_RUNTIME), + KinesisAnalyticsRuntimes.imageFor(" ")); + } + + @Test + void imageForRejectsUnsupportedRuntime() { + // SQL / ZEPPELIN studio runtimes and bogus values have no plain Flink image. + assertThrows(AwsException.class, () -> KinesisAnalyticsRuntimes.imageFor("SQL-1_0")); + assertThrows(AwsException.class, () -> KinesisAnalyticsRuntimes.imageFor("FLINK-9_9")); + // AWS does not offer 1.16 / 1.17, so Floci rejects them too. + assertThrows(AwsException.class, () -> KinesisAnalyticsRuntimes.imageFor("FLINK-1_16")); + assertThrows(AwsException.class, () -> KinesisAnalyticsRuntimes.imageFor("FLINK-1_17")); + } + + @Test + void validateAllowsSupportedAndBlankButRejectsUnknown() { + KinesisAnalyticsRuntimes.validate("FLINK-1_20"); + KinesisAnalyticsRuntimes.validate(null); + assertThrows(AwsException.class, () -> KinesisAnalyticsRuntimes.validate("ZEPPELIN-FLINK-3_0")); + } + + @Test + void resolveImageHonoursOperatorOverride() { + // A configured override wins regardless of the requested runtime. + assertEquals("registry.example.com/flink:pinned", + KinesisAnalyticsRuntimes.resolveImage( + Optional.of("registry.example.com/flink:pinned"), "FLINK-1_18")); + // Blank/absent override falls back to the runtime map. + assertEquals("apache/flink:1.19", + KinesisAnalyticsRuntimes.resolveImage(Optional.empty(), "FLINK-1_19")); + assertEquals("apache/flink:1.19", + KinesisAnalyticsRuntimes.resolveImage(Optional.of(" "), "FLINK-1_19")); + } + + @Test + void supportedRuntimesListsFlinkVersions() { + assertTrue(KinesisAnalyticsRuntimes.supportedRuntimes().contains("FLINK-1_18")); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2IntegrationTest.java new file mode 100644 index 0000000000..e43d341649 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2IntegrationTest.java @@ -0,0 +1,399 @@ +package io.github.hectorvent.floci.services.kinesisanalytics; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.path.json.config.JsonPathConfig; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.startsWith; + +/** + * Drives the full JSON-1.1 dispatch path (AwsJson11Controller → ResolvedServiceCatalog → + * KinesisAnalyticsV2JsonHandler) via the {@code KinesisAnalytics_20180523.} target prefix. + * Runs with {@code kinesis-analytics.mock=true} (see src/test/resources/application.yml), so no + * Docker daemon is needed — StartApplication comes up RUNNING immediately. + */ +@QuarkusTest +class KinesisAnalyticsV2IntegrationTest { + + private static final String CONTENT_TYPE = "application/x-amz-json-1.1"; + private static final String ROLE = "arn:aws:iam::000000000000:role/x"; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + private void createApplication(String name) { + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.CreateApplication") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "%s", "RuntimeEnvironment": "FLINK-1_18", + "ServiceExecutionRole": "%s"} + """.formatted(name, ROLE)) + .when() + .post("/") + .then() + .statusCode(200) + .body("ApplicationDetail.ApplicationName", equalTo(name)) + .body("ApplicationDetail.ApplicationStatus", equalTo("READY")) + .body("ApplicationDetail.ApplicationARN", startsWith("arn:aws:kinesisanalytics:")); + } + + @Test + void createThenDescribeApplication() { + createApplication("it-describe"); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.DescribeApplication") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-describe"} + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("ApplicationDetail.ApplicationName", equalTo("it-describe")) + .body("ApplicationDetail.RuntimeEnvironment", equalTo("FLINK-1_18")); + } + + @Test + void startApplicationTransitionsToRunning() { + createApplication("it-start"); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.StartApplication") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-start", "RunConfiguration": {}} + """) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.DescribeApplication") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-start"} + """) + .when() + .post("/") + .then() + .statusCode(200) + // mock mode: RUNNING immediately + .body("ApplicationDetail.ApplicationStatus", equalTo("RUNNING")); + } + + @Test + void listApplicationsIncludesCreated() { + createApplication("it-list"); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.ListApplications") + .contentType(CONTENT_TYPE) + .body("{}") + .when() + .post("/") + .then() + .statusCode(200) + .body("ApplicationSummaries.ApplicationName", hasItem("it-list")); + } + + @Test + void deleteApplicationSucceeds() { + createApplication("it-delete"); + + // DeleteApplication validates CreateTimestamp against the stored value — fetch the real one + // (epoch seconds). Use BigDecimal number handling: the default float extraction truncates the + // ~10-digit epoch to ~7 significant figures, which would shift it by seconds and fail the match. + String createTimestamp = given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.DescribeApplication") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-delete"} + """) + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath(new JsonPathConfig(JsonPathConfig.NumberReturnType.BIG_DECIMAL)) + .getString("ApplicationDetail.CreateTimestamp"); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.DeleteApplication") + .contentType(CONTENT_TYPE) + .body("{\"ApplicationName\": \"it-delete\", \"CreateTimestamp\": " + createTimestamp + "}") + .when() + .post("/") + .then() + .statusCode(200); + } + + @Test + void tagLifecycleRoundTripsThroughTheWireProtocol() { + String arn = given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.CreateApplication") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-tags", "RuntimeEnvironment": "FLINK-1_18", + "ServiceExecutionRole": "%s", + "Tags": [{"Key": "env", "Value": "dev"}]} + """.formatted(ROLE)) + .when() + .post("/") + .then() + .statusCode(200) + // Tags is a list of {Key, Value} objects on this API, never echoed on ApplicationDetail. + .body("ApplicationDetail.Tags", equalTo(null)) + .extract().path("ApplicationDetail.ApplicationARN"); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.ListTagsForResource") + .contentType(CONTENT_TYPE) + .body("{\"ResourceARN\": \"" + arn + "\"}") + .when() + .post("/") + .then() + .statusCode(200) + .body("Tags.Key", hasItem("env")) + .body("Tags.Value", hasItem("dev")); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.TagResource") + .contentType(CONTENT_TYPE) + .body("{\"ResourceARN\": \"" + arn + "\", \"Tags\": [{\"Key\": \"team\", \"Value\": \"platform\"}]}") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.UntagResource") + .contentType(CONTENT_TYPE) + .body("{\"ResourceARN\": \"" + arn + "\", \"TagKeys\": [\"env\"]}") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.ListTagsForResource") + .contentType(CONTENT_TYPE) + .body("{\"ResourceARN\": \"" + arn + "\"}") + .when() + .post("/") + .then() + .statusCode(200) + .body("Tags.Key", hasItem("team")) + .body("Tags.Key", not(hasItem("env"))); + } + + @Test + void updateApplicationWithNewCodeLocationRoundTripsThroughTheWireProtocol() { + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.CreateApplication") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-update-code", "RuntimeEnvironment": "FLINK-1_18", + "ServiceExecutionRole": "%s", + "ApplicationConfiguration": {"ApplicationCodeConfiguration": { + "CodeContent": {"S3ContentLocation": { + "BucketARN": "arn:aws:s3:::flink-code", "FileKey": "app.jar"}}, + "CodeContentType": "ZIPFILE"}}} + """.formatted(ROLE)) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.UpdateApplication") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-update-code", "CurrentApplicationVersionId": 1, + "ApplicationConfigurationUpdate": {"ApplicationCodeConfigurationUpdate": { + "CodeContentUpdate": {"S3ContentLocationUpdate": { + "BucketARNUpdate": "arn:aws:s3:::flink-code-v2", + "FileKeyUpdate": "app-v2.jar"}}}}} + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("ApplicationDetail.ApplicationVersionId", equalTo(2)) + .body("ApplicationDetail.ApplicationConfigurationDescription" + + ".ApplicationCodeConfigurationDescription.CodeContentDescription" + + ".S3ApplicationCodeLocationDescription.BucketARN", equalTo("arn:aws:s3:::flink-code-v2")) + .body("ApplicationDetail.ApplicationConfigurationDescription" + + ".ApplicationCodeConfigurationDescription.CodeContentDescription" + + ".S3ApplicationCodeLocationDescription.FileKey", equalTo("app-v2.jar")); + } + + @Test + void snapshotLifecycleRoundTripsThroughTheWireProtocol() { + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.CreateApplication") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-snapshot", "RuntimeEnvironment": "FLINK-1_18", + "ServiceExecutionRole": "%s", + "ApplicationConfiguration": {"ApplicationCodeConfiguration": { + "CodeContent": {"S3ContentLocation": { + "BucketARN": "arn:aws:s3:::flink-code", "FileKey": "app.jar"}}, + "CodeContentType": "ZIPFILE"}}} + """.formatted(ROLE)) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.StartApplication") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-snapshot", "RunConfiguration": {}} + """) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.CreateApplicationSnapshot") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-snapshot", "SnapshotName": "before-upgrade"} + """) + .when() + .post("/") + .then() + .statusCode(200); + + String creationTimestamp = given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.DescribeApplicationSnapshot") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-snapshot", "SnapshotName": "before-upgrade"} + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("SnapshotDetails.SnapshotName", equalTo("before-upgrade")) + // mock mode: the snapshot completes immediately, same as the application itself. + .body("SnapshotDetails.SnapshotStatus", equalTo("READY")) + .extract().jsonPath(new JsonPathConfig(JsonPathConfig.NumberReturnType.BIG_DECIMAL)) + .getString("SnapshotDetails.SnapshotCreationTimestamp"); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.ListApplicationSnapshots") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-snapshot"} + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("SnapshotSummaries.SnapshotName", hasItem("before-upgrade")); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.DeleteApplicationSnapshot") + .contentType(CONTENT_TYPE) + .body("{\"ApplicationName\": \"it-snapshot\", \"SnapshotName\": \"before-upgrade\", " + + "\"SnapshotCreationTimestamp\": " + creationTimestamp + "}") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.ListApplicationSnapshots") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-snapshot"} + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("SnapshotSummaries", equalTo(java.util.List.of())); + } + + @Test + void createApplicationWithSnapshotsDisabledRejectsCreateApplicationSnapshot() { + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.CreateApplication") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-nosnaps", "RuntimeEnvironment": "FLINK-1_18", + "ServiceExecutionRole": "%s", + "ApplicationConfiguration": {"ApplicationCodeConfiguration": { + "CodeContent": {"S3ContentLocation": { + "BucketARN": "arn:aws:s3:::flink-code", "FileKey": "app.jar"}}, + "CodeContentType": "ZIPFILE"}, + "ApplicationSnapshotConfiguration": {"SnapshotsEnabled": false}}} + """.formatted(ROLE)) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.StartApplication") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-nosnaps", "RunConfiguration": {}} + """) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.CreateApplicationSnapshot") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-nosnaps", "SnapshotName": "attempt"} + """) + .when() + .post("/") + .then() + .statusCode(400); + } + + @Test + void createApplicationPresignedUrlRejectsWhenNotRunning() { + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.CreateApplication") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-presigned", "RuntimeEnvironment": "FLINK-1_18", + "ServiceExecutionRole": "%s"} + """.formatted(ROLE)) + .when() + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "KinesisAnalytics_20180523.CreateApplicationPresignedUrl") + .contentType(CONTENT_TYPE) + .body(""" + {"ApplicationName": "it-presigned", "UrlType": "FLINK_DASHBOARD_URL"} + """) + .when() + .post("/") + .then() + .statusCode(400); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandlerTest.java new file mode 100644 index 0000000000..96fd91540f --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandlerTest.java @@ -0,0 +1,430 @@ +package io.github.hectorvent.floci.services.kinesisanalytics; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.kinesisanalytics.container.FlinkContainerManager; +import jakarta.ws.rs.core.Response; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.when; + +class KinesisAnalyticsV2JsonHandlerTest { + + private static final String REGION = "us-east-1"; + private static final String ROLE = "arn:aws:iam::000000000000:role/x"; + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private KinesisAnalyticsV2JsonHandler handler; + + @BeforeEach + void setUp() { + handler = new KinesisAnalyticsV2JsonHandler(mockModeService(), MAPPER); + } + + private ObjectNode entity(Response response) { + return (ObjectNode) response.getEntity(); + } + + private void createApplication(String name) { + ObjectNode req = MAPPER.createObjectNode(); + req.put("ApplicationName", name); + req.put("RuntimeEnvironment", "FLINK-1_18"); + req.put("ServiceExecutionRole", ROLE); + assertThat(handler.handle("CreateApplication", req, REGION).getStatus(), is(200)); + } + + @Test + void createApplicationReturnsReadyDetail() { + ObjectNode req = MAPPER.createObjectNode(); + req.put("ApplicationName", "demo"); + req.put("RuntimeEnvironment", "FLINK-1_18"); + req.put("ServiceExecutionRole", ROLE); + + Response resp = handler.handle("CreateApplication", req, REGION); + assertThat(resp.getStatus(), is(200)); + ObjectNode detail = (ObjectNode) entity(resp).get("ApplicationDetail"); + assertEquals("demo", detail.get("ApplicationName").asText()); + assertEquals("READY", detail.get("ApplicationStatus").asText()); + assertEquals(1, detail.get("ApplicationVersionId").asLong()); + assertThat(detail.get("ApplicationARN").asText().contains(":kinesisanalytics:"), is(true)); + } + + @Test + void describeApplicationReturnsDetail() { + createApplication("demo"); + + ObjectNode req = MAPPER.createObjectNode(); + req.put("ApplicationName", "demo"); + Response resp = handler.handle("DescribeApplication", req, REGION); + assertThat(resp.getStatus(), is(200)); + ObjectNode detail = (ObjectNode) entity(resp).get("ApplicationDetail"); + assertEquals("demo", detail.get("ApplicationName").asText()); + } + + @Test + void startThenDescribeShowsRunning() { + createApplication("demo"); + + ObjectNode start = MAPPER.createObjectNode(); + start.put("ApplicationName", "demo"); + assertThat(handler.handle("StartApplication", start, REGION).getStatus(), is(200)); + + ObjectNode describe = MAPPER.createObjectNode(); + describe.put("ApplicationName", "demo"); + ObjectNode detail = (ObjectNode) entity(handler.handle("DescribeApplication", describe, REGION)) + .get("ApplicationDetail"); + // Mock mode: the application comes up RUNNING immediately. + assertEquals("RUNNING", detail.get("ApplicationStatus").asText()); + } + + @Test + void listApplicationsIncludesCreated() { + createApplication("demo-a"); + createApplication("demo-b"); + + Response resp = handler.handle("ListApplications", MAPPER.createObjectNode(), REGION); + assertThat(resp.getStatus(), is(200)); + var summaries = entity(resp).get("ApplicationSummaries"); + assertEquals(2, summaries.size()); + } + + @Test + void deleteApplicationRemovesIt() { + createApplication("demo"); + + // DeleteApplication requires the app's CreateTimestamp (epoch seconds). + ObjectNode describe = MAPPER.createObjectNode(); + describe.put("ApplicationName", "demo"); + double createTs = ((ObjectNode) entity(handler.handle("DescribeApplication", describe, REGION)) + .get("ApplicationDetail")).get("CreateTimestamp").asDouble(); + + ObjectNode del = MAPPER.createObjectNode(); + del.put("ApplicationName", "demo"); + del.put("CreateTimestamp", createTs); + assertThat(handler.handle("DeleteApplication", del, REGION).getStatus(), is(200)); + + var summaries = entity(handler.handle("ListApplications", MAPPER.createObjectNode(), REGION)) + .get("ApplicationSummaries"); + assertEquals(0, summaries.size()); + } + + @Test + void createWithCodeConfigEchoesItOnDescribe() { + ObjectNode s3 = MAPPER.createObjectNode(); + s3.put("BucketARN", "arn:aws:s3:::flink-code"); + s3.put("FileKey", "app.jar"); + ObjectNode codeCfg = MAPPER.createObjectNode(); + codeCfg.set("CodeContent", MAPPER.createObjectNode().set("S3ContentLocation", s3)); + codeCfg.put("CodeContentType", "ZIPFILE"); + ObjectNode flinkCfg = MAPPER.createObjectNode(); + flinkCfg.set("ParallelismConfiguration", MAPPER.createObjectNode().put("Parallelism", 3)); + ObjectNode appCfg = MAPPER.createObjectNode(); + appCfg.set("ApplicationCodeConfiguration", codeCfg); + appCfg.set("FlinkApplicationConfiguration", flinkCfg); + + ObjectNode req = MAPPER.createObjectNode(); + req.put("ApplicationName", "coded"); + req.put("RuntimeEnvironment", "FLINK-1_18"); + req.put("ServiceExecutionRole", ROLE); + req.set("ApplicationConfiguration", appCfg); + assertThat(handler.handle("CreateApplication", req, REGION).getStatus(), is(200)); + + ObjectNode describe = MAPPER.createObjectNode(); + describe.put("ApplicationName", "coded"); + ObjectNode detail = (ObjectNode) entity(handler.handle("DescribeApplication", describe, REGION)) + .get("ApplicationDetail"); + var loc = detail.get("ApplicationConfigurationDescription") + .get("ApplicationCodeConfigurationDescription") + .get("CodeContentDescription").get("S3ApplicationCodeLocationDescription"); + assertEquals("arn:aws:s3:::flink-code", loc.get("BucketARN").asText()); + assertEquals("app.jar", loc.get("FileKey").asText()); + assertEquals(3, detail.get("ApplicationConfigurationDescription") + .get("FlinkApplicationConfigurationDescription") + .get("ParallelismConfigurationDescription").get("Parallelism").asInt()); + } + + @Test + void createWithEnvironmentPropertiesEchoesThemOnDescribe() { + ObjectNode s3 = MAPPER.createObjectNode(); + s3.put("BucketARN", "arn:aws:s3:::flink-code"); + s3.put("FileKey", "app.jar"); + ObjectNode codeCfg = MAPPER.createObjectNode(); + codeCfg.set("CodeContent", MAPPER.createObjectNode().set("S3ContentLocation", s3)); + ObjectNode propertyGroup = MAPPER.createObjectNode(); + propertyGroup.put("PropertyGroupId", "ProducerConfigProperties"); + propertyGroup.set("PropertyMap", MAPPER.createObjectNode().put("aws.region", "us-west-2")); + ObjectNode envProps = MAPPER.createObjectNode(); + envProps.putArray("PropertyGroups").add(propertyGroup); + ObjectNode appCfg = MAPPER.createObjectNode(); + appCfg.set("ApplicationCodeConfiguration", codeCfg); + appCfg.set("EnvironmentProperties", envProps); + + ObjectNode req = MAPPER.createObjectNode(); + req.put("ApplicationName", "envprops"); + req.put("RuntimeEnvironment", "FLINK-1_18"); + req.put("ServiceExecutionRole", ROLE); + req.set("ApplicationConfiguration", appCfg); + assertThat(handler.handle("CreateApplication", req, REGION).getStatus(), is(200)); + + ObjectNode describe = MAPPER.createObjectNode(); + describe.put("ApplicationName", "envprops"); + ObjectNode detail = (ObjectNode) entity(handler.handle("DescribeApplication", describe, REGION)) + .get("ApplicationDetail"); + var groups = detail.get("ApplicationConfigurationDescription") + .get("EnvironmentPropertyDescriptions").get("PropertyGroupDescriptions"); + assertEquals(1, groups.size()); + assertEquals("ProducerConfigProperties", groups.get(0).get("PropertyGroupId").asText()); + assertEquals("us-west-2", groups.get(0).get("PropertyMap").get("aws.region").asText()); + } + + @Test + void createApplicationDefaultsSnapshotsEnabledToTrueOnDescribe() { + createApplication("demo"); + + ObjectNode describe = MAPPER.createObjectNode(); + describe.put("ApplicationName", "demo"); + ObjectNode detail = (ObjectNode) entity(handler.handle("DescribeApplication", describe, REGION)) + .get("ApplicationDetail"); + // No code configured, so ApplicationConfigurationDescription isn't built at all — matches how + // EnvironmentPropertyDescriptions is also only ever echoed when the app has code. + assertThat(detail.has("ApplicationConfigurationDescription"), is(false)); + } + + @Test + void createApplicationWithSnapshotsDisabledEchoesItOnDescribe() { + ObjectNode s3 = MAPPER.createObjectNode(); + s3.put("BucketARN", "arn:aws:s3:::flink-code"); + s3.put("FileKey", "app.jar"); + ObjectNode codeCfg = MAPPER.createObjectNode(); + codeCfg.set("CodeContent", MAPPER.createObjectNode().set("S3ContentLocation", s3)); + ObjectNode appCfg = MAPPER.createObjectNode(); + appCfg.set("ApplicationCodeConfiguration", codeCfg); + appCfg.set("ApplicationSnapshotConfiguration", + MAPPER.createObjectNode().put("SnapshotsEnabled", false)); + + ObjectNode req = MAPPER.createObjectNode(); + req.put("ApplicationName", "nosnaps"); + req.put("RuntimeEnvironment", "FLINK-1_18"); + req.put("ServiceExecutionRole", ROLE); + req.set("ApplicationConfiguration", appCfg); + assertThat(handler.handle("CreateApplication", req, REGION).getStatus(), is(200)); + + ObjectNode describe = MAPPER.createObjectNode(); + describe.put("ApplicationName", "nosnaps"); + ObjectNode detail = (ObjectNode) entity(handler.handle("DescribeApplication", describe, REGION)) + .get("ApplicationDetail"); + assertThat(detail.get("ApplicationConfigurationDescription") + .get("ApplicationSnapshotConfigurationDescription").get("SnapshotsEnabled").asBoolean(), is(false)); + + ObjectNode createSnap = MAPPER.createObjectNode(); + createSnap.put("ApplicationName", "nosnaps"); + createSnap.put("SnapshotName", "attempt"); + assertThrows(AwsException.class, () -> handler.handle("CreateApplicationSnapshot", createSnap, REGION)); + } + + @Test + void createApplicationPresignedUrlRejectsUnsupportedUrlType() { + createApplication("demo"); + + ObjectNode req = MAPPER.createObjectNode(); + req.put("ApplicationName", "demo"); + req.put("UrlType", "ZEPPELIN_UI_URL"); + assertThrows(AwsException.class, () -> handler.handle("CreateApplicationPresignedUrl", req, REGION)); + } + + @Test + void createApplicationPresignedUrlRejectsWhenNotRunning() { + createApplication("demo"); // READY, never started + + ObjectNode req = MAPPER.createObjectNode(); + req.put("ApplicationName", "demo"); + req.put("UrlType", "FLINK_DASHBOARD_URL"); + assertThrows(AwsException.class, () -> handler.handle("CreateApplicationPresignedUrl", req, REGION)); + } + + @Test + void updateApplicationWithNewCodeLocationEchoesItOnDescribe() { + createRunningCodedApplication("redeploy-target"); + + ObjectNode s3Update = MAPPER.createObjectNode(); + s3Update.put("BucketARNUpdate", "arn:aws:s3:::flink-code-v2"); + s3Update.put("FileKeyUpdate", "app-v2.jar"); + ObjectNode codeCfgUpdate = MAPPER.createObjectNode(); + codeCfgUpdate.set("CodeContentUpdate", MAPPER.createObjectNode().set("S3ContentLocationUpdate", s3Update)); + ObjectNode appCfgUpdate = MAPPER.createObjectNode(); + appCfgUpdate.set("ApplicationCodeConfigurationUpdate", codeCfgUpdate); + + ObjectNode updateReq = MAPPER.createObjectNode(); + updateReq.put("ApplicationName", "redeploy-target"); + updateReq.put("CurrentApplicationVersionId", 1L); + updateReq.set("ApplicationConfigurationUpdate", appCfgUpdate); + Response updated = handler.handle("UpdateApplication", updateReq, REGION); + assertThat(updated.getStatus(), is(200)); + ObjectNode detail = (ObjectNode) entity(updated).get("ApplicationDetail"); + assertEquals(2L, detail.get("ApplicationVersionId").asLong()); + + var loc = detail.get("ApplicationConfigurationDescription") + .get("ApplicationCodeConfigurationDescription") + .get("CodeContentDescription").get("S3ApplicationCodeLocationDescription"); + assertEquals("arn:aws:s3:::flink-code-v2", loc.get("BucketARN").asText()); + assertEquals("app-v2.jar", loc.get("FileKey").asText()); + } + + @Test + void unsupportedActionReturns400() { + Response resp = handler.handle("BogusAction", MAPPER.createObjectNode(), REGION); + assertThat(resp.getStatus(), is(400)); + } + + @Test + void createApplicationWithTagsThenListTagsForResourceRoundTrips() { + ObjectNode req = MAPPER.createObjectNode(); + req.put("ApplicationName", "tagged"); + req.put("RuntimeEnvironment", "FLINK-1_18"); + req.put("ServiceExecutionRole", ROLE); + req.putArray("Tags") + .addObject().put("Key", "env").put("Value", "dev"); + Response created = handler.handle("CreateApplication", req, REGION); + assertThat(created.getStatus(), is(200)); + // Tags is a list of {Key, Value} objects on this API, never echoed on ApplicationDetail. + assertThat(entity(created).get("ApplicationDetail").has("Tags"), is(false)); + + String arn = entity(created).get("ApplicationDetail").get("ApplicationARN").asText(); + ObjectNode listReq = MAPPER.createObjectNode(); + listReq.put("ResourceARN", arn); + Response listed = handler.handle("ListTagsForResource", listReq, REGION); + assertThat(listed.getStatus(), is(200)); + var tags = entity(listed).get("Tags"); + assertEquals(1, tags.size()); + assertEquals("env", tags.get(0).get("Key").asText()); + assertEquals("dev", tags.get(0).get("Value").asText()); + } + + @Test + void tagResourceThenUntagResourceRoundTrips() { + createApplication("demo"); + String arn = describeArn("demo"); + + ObjectNode tagReq = MAPPER.createObjectNode(); + tagReq.put("ResourceARN", arn); + tagReq.putArray("Tags").addObject().put("Key", "team").put("Value", "platform"); + assertThat(handler.handle("TagResource", tagReq, REGION).getStatus(), is(200)); + + ObjectNode listReq = MAPPER.createObjectNode(); + listReq.put("ResourceARN", arn); + assertEquals(1, entity(handler.handle("ListTagsForResource", listReq, REGION)).get("Tags").size()); + + ObjectNode untagReq = MAPPER.createObjectNode(); + untagReq.put("ResourceARN", arn); + untagReq.putArray("TagKeys").add("team"); + assertThat(handler.handle("UntagResource", untagReq, REGION).getStatus(), is(200)); + + assertEquals(0, entity(handler.handle("ListTagsForResource", listReq, REGION)).get("Tags").size()); + } + + @Test + void createApplicationSnapshotThenDescribeAndListThenDeleteRoundTrips() { + createRunningCodedApplication("streaming"); + + ObjectNode createSnap = MAPPER.createObjectNode(); + createSnap.put("ApplicationName", "streaming"); + createSnap.put("SnapshotName", "before-upgrade"); + assertThat(handler.handle("CreateApplicationSnapshot", createSnap, REGION).getStatus(), is(200)); + + ObjectNode describeSnap = MAPPER.createObjectNode(); + describeSnap.put("ApplicationName", "streaming"); + describeSnap.put("SnapshotName", "before-upgrade"); + Response described = handler.handle("DescribeApplicationSnapshot", describeSnap, REGION); + assertThat(described.getStatus(), is(200)); + ObjectNode details = (ObjectNode) entity(described).get("SnapshotDetails"); + assertEquals("before-upgrade", details.get("SnapshotName").asText()); + // Mock mode: the snapshot completes immediately, same as the application itself. + assertEquals("READY", details.get("SnapshotStatus").asText()); + double creationTimestamp = details.get("SnapshotCreationTimestamp").asDouble(); + + Response listed = handler.handle("ListApplicationSnapshots", + MAPPER.createObjectNode().put("ApplicationName", "streaming"), REGION); + assertThat(listed.getStatus(), is(200)); + var summaries = entity(listed).get("SnapshotSummaries"); + assertEquals(1, summaries.size()); + assertEquals("before-upgrade", summaries.get(0).get("SnapshotName").asText()); + + ObjectNode deleteSnap = MAPPER.createObjectNode(); + deleteSnap.put("ApplicationName", "streaming"); + deleteSnap.put("SnapshotName", "before-upgrade"); + deleteSnap.put("SnapshotCreationTimestamp", creationTimestamp); + assertThat(handler.handle("DeleteApplicationSnapshot", deleteSnap, REGION).getStatus(), is(200)); + + var afterDelete = entity(handler.handle("ListApplicationSnapshots", + MAPPER.createObjectNode().put("ApplicationName", "streaming"), REGION)).get("SnapshotSummaries"); + assertEquals(0, afterDelete.size()); + } + + @Test + void createApplicationSnapshotFailsWhenApplicationNotRunning() { + createApplication("demo"); + + ObjectNode createSnap = MAPPER.createObjectNode(); + createSnap.put("ApplicationName", "demo"); + createSnap.put("SnapshotName", "before-upgrade"); + assertThrows(AwsException.class, () -> handler.handle("CreateApplicationSnapshot", createSnap, REGION)); + } + + private void createRunningCodedApplication(String name) { + ObjectNode s3 = MAPPER.createObjectNode(); + s3.put("BucketARN", "arn:aws:s3:::flink-code"); + s3.put("FileKey", "app.jar"); + ObjectNode codeCfg = MAPPER.createObjectNode(); + codeCfg.set("CodeContent", MAPPER.createObjectNode().set("S3ContentLocation", s3)); + codeCfg.put("CodeContentType", "ZIPFILE"); + ObjectNode appCfg = MAPPER.createObjectNode(); + appCfg.set("ApplicationCodeConfiguration", codeCfg); + + ObjectNode req = MAPPER.createObjectNode(); + req.put("ApplicationName", name); + req.put("RuntimeEnvironment", "FLINK-1_18"); + req.put("ServiceExecutionRole", ROLE); + req.set("ApplicationConfiguration", appCfg); + assertThat(handler.handle("CreateApplication", req, REGION).getStatus(), is(200)); + + ObjectNode start = MAPPER.createObjectNode(); + start.put("ApplicationName", name); + assertThat(handler.handle("StartApplication", start, REGION).getStatus(), is(200)); + } + + private String describeArn(String name) { + ObjectNode describe = MAPPER.createObjectNode(); + describe.put("ApplicationName", name); + return entity(handler.handle("DescribeApplication", describe, REGION)) + .get("ApplicationDetail").get("ApplicationARN").asText(); + } + + private KinesisAnalyticsV2Service mockModeService() { + StorageFactory storageFactory = Mockito.mock(StorageFactory.class); + when(storageFactory.create(Mockito.anyString(), Mockito.anyString(), Mockito.any())) + .thenReturn(new InMemoryStorage<>()); + + EmulatorConfig config = Mockito.mock(EmulatorConfig.class); + var servicesConfig = Mockito.mock(EmulatorConfig.ServicesConfig.class); + var kaConfig = Mockito.mock(EmulatorConfig.KinesisAnalyticsServiceConfig.class); + when(config.services()).thenReturn(servicesConfig); + when(servicesConfig.kinesisAnalytics()).thenReturn(kaConfig); + when(kaConfig.mock()).thenReturn(true); + when(config.defaultRegion()).thenReturn("us-east-1"); + + RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000"); + return new KinesisAnalyticsV2Service(storageFactory, config, regionResolver, + Mockito.mock(FlinkContainerManager.class)); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2ServiceTest.java new file mode 100644 index 0000000000..bf620c094d --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2ServiceTest.java @@ -0,0 +1,625 @@ +package io.github.hectorvent.floci.services.kinesisanalytics; + +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.kinesisanalytics.container.FlinkContainerManager; +import io.github.hectorvent.floci.services.kinesisanalytics.model.ApplicationStatus; +import io.github.hectorvent.floci.services.kinesisanalytics.model.FlinkApplication; +import io.github.hectorvent.floci.services.kinesisanalytics.model.Snapshot; +import io.github.hectorvent.floci.services.kinesisanalytics.model.SnapshotStatus; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + +class KinesisAnalyticsV2ServiceTest { + + private static final String ROLE = "arn:aws:iam::000000000000:role/x"; + + private KinesisAnalyticsV2Service service; + + @BeforeEach + void setUp() { + service = mockModeService(); + } + + private FlinkApplication create(String name) { + return service.createApplication(name, "FLINK-1_18", ROLE, "desc", "STREAMING"); + } + + @Test + void createApplicationLandsInReady() { + // AWS-faithful: a freshly created application is READY, not RUNNING. + FlinkApplication app = create("demo"); + assertEquals("demo", app.getApplicationName()); + assertEquals("FLINK-1_18", app.getRuntimeEnvironment()); + assertEquals(ApplicationStatus.READY, app.getApplicationStatus()); + assertEquals(1L, app.getApplicationVersionId()); + assertTrue(app.getApplicationArn().contains(":kinesisanalytics:")); + assertTrue(app.getApplicationArn().contains("application/demo")); + } + + @Test + void createApplicationRequiresName() { + assertThrows(AwsException.class, + () -> service.createApplication(" ", "FLINK-1_18", ROLE, null, null)); + } + + @Test + void createApplicationRequiresRuntimeEnvironment() { + assertThrows(AwsException.class, + () -> service.createApplication("demo", null, ROLE, null, null)); + } + + @Test + void createApplicationRequiresServiceExecutionRole() { + assertThrows(AwsException.class, + () -> service.createApplication("demo", "FLINK-1_18", null, null, null)); + } + + @Test + void createApplicationRejectsUnsupportedRuntime() { + // SQL/ZEPPELIN studio runtimes and bogus values are not backable with a Flink image. + assertThrows(AwsException.class, + () -> service.createApplication("demo", "ZEPPELIN-FLINK-3_0", ROLE, null, null)); + } + + @Test + void createApplicationRejectsDuplicateName() { + create("demo"); + assertThrows(AwsException.class, () -> create("demo")); + } + + @Test + void describeApplicationThrowsWhenMissing() { + assertThrows(AwsException.class, () -> service.describeApplication("nope")); + } + + @Test + void startApplicationRunsInMockMode() { + create("demo"); + FlinkApplication started = service.startApplication("demo"); + // In mock mode there is no container: the application comes up RUNNING immediately. + assertEquals(ApplicationStatus.RUNNING, started.getApplicationStatus()); + } + + @Test + void startApplicationRejectedWhenNotReady() { + create("demo"); + service.startApplication("demo"); + // Already RUNNING → cannot start again. + assertThrows(AwsException.class, () -> service.startApplication("demo")); + } + + @Test + void stopApplicationReturnsToReady() { + create("demo"); + service.startApplication("demo"); + FlinkApplication stopped = service.stopApplication("demo"); + assertEquals(ApplicationStatus.READY, stopped.getApplicationStatus()); + } + + @Test + void stopApplicationRejectedWhenNotRunning() { + create("demo"); + // Still READY (never started) → cannot stop. + assertThrows(AwsException.class, () -> service.stopApplication("demo")); + } + + @Test + void updateApplicationBumpsVersion() { + create("demo"); + FlinkApplication updated = service.updateApplication("demo", 1L, "arn:aws:iam::000000000000:role/y"); + assertEquals(2L, updated.getApplicationVersionId()); + assertEquals("arn:aws:iam::000000000000:role/y", updated.getServiceExecutionRole()); + } + + @Test + void updateApplicationRejectsStaleVersion() { + create("demo"); // version 1 + // Optimistic concurrency: a mismatched CurrentApplicationVersionId is rejected. + assertThrows(AwsException.class, () -> service.updateApplication("demo", 5L, null)); + } + + @Test + void deleteApplicationRemovesIt() { + FlinkApplication app = create("demo"); + service.deleteApplication("demo", app.getCreateTimestamp()); + assertTrue(service.listApplications().isEmpty()); + } + + @Test + void deleteApplicationRejectsWrongTimestamp() { + create("demo"); + assertThrows(AwsException.class, + () -> service.deleteApplication("demo", java.time.Instant.ofEpochSecond(1))); + } + + @Test + void deleteApplicationRejectedWhileRunning() { + FlinkApplication app = create("demo"); + service.startApplication("demo"); // mock mode → RUNNING + // AWS rejects delete of a running application; it must be stopped first. + assertThrows(AwsException.class, + () -> service.deleteApplication("demo", app.getCreateTimestamp())); + } + + @Test + void startApplicationWrapsProvisioningFailure() { + // Real mode with a container manager that fails: the AWS envelope must not leak the + // internal cause, but the operation must throw. "InternalFailure" (no "Exception" suffix) is + // AWS's documented generic 500 common to every service's API, not a kinesisanalyticsv2-specific + // shape (this service's model declares no 5xx shape at all). + KinesisAnalyticsV2Service realMode = realModeServiceWithFailingManager(); + realMode.createApplication("demo", "FLINK-1_18", ROLE, null, null); + AwsException ex = assertThrows(AwsException.class, () -> realMode.startApplication("demo")); + assertEquals("InternalFailure", ex.getErrorCode()); + assertEquals(500, ex.getHttpStatus()); + } + + @Test + void startApplicationPreservesAwsExceptionFromCodeFetch() { + // A bad application-code S3 location makes startCluster throw AwsException(InvalidArgumentException). + // It must propagate as-is (400 client error), not be masked as InternalFailure (500). + FlinkContainerManager failing = Mockito.mock(FlinkContainerManager.class); + Mockito.doThrow(new AwsException("InvalidArgumentException", + "Unable to fetch application code from s3://b/missing.jar", 400)) + .when(failing).startCluster(Mockito.any()); + KinesisAnalyticsV2Service realMode = buildService(false, failing); + realMode.createApplication("demo", "FLINK-1_18", ROLE, null, null); + + AwsException ex = assertThrows(AwsException.class, () -> realMode.startApplication("demo")); + assertEquals("InvalidArgumentException", ex.getErrorCode()); + } + + @Test + void createApplicationStoresCodeConfig() { + FlinkApplication app = service.createApplication("coded", "FLINK-1_18", ROLE, null, null, + "flink-code", "app.jar", "v2", 4); + assertTrue(app.hasCode()); + assertEquals("flink-code", app.getCodeS3Bucket()); + assertEquals("app.jar", app.getCodeS3Key()); + assertEquals("v2", app.getCodeS3ObjectVersion()); + assertEquals(4, app.getParallelism()); + } + + @Test + void createApplicationWithoutCodeHasNoCode() { + FlinkApplication app = create("bare"); + assertFalse(app.hasCode()); + assertEquals(1, app.getParallelism()); + } + + @Test + void createApplicationStoresTags() { + FlinkApplication app = service.createApplication("tagged", "FLINK-1_18", ROLE, null, null, + null, null, null, 1, Map.of("env", "dev")); + assertEquals("dev", app.getTags().get("env")); + } + + @Test + void createApplicationStoresEnvironmentProperties() { + Map> groups = Map.of( + "ProducerConfigProperties", Map.of("aws.region", "us-west-2")); + FlinkApplication app = service.createApplication("props", "FLINK-1_18", ROLE, null, null, + null, null, null, 1, null, groups); + assertEquals("us-west-2", + app.getEnvironmentProperties().get("ProducerConfigProperties").get("aws.region")); + } + + @Test + void createApplicationDefaultsSnapshotsEnabledToTrue() { + FlinkApplication app = create("demo"); + assertTrue(app.isSnapshotsEnabled()); + } + + @Test + void createApplicationWithSnapshotsDisabled() { + FlinkApplication app = service.createApplication("demo", "FLINK-1_18", ROLE, null, null, + null, null, null, 1, null, null, false); + assertFalse(app.isSnapshotsEnabled()); + } + + @Test + void createApplicationRejectsTooManyTags() { + Map tooMany = new HashMap<>(); + for (int i = 0; i < 51; i++) { + tooMany.put("k" + i, "v"); + } + AwsException ex = assertThrows(AwsException.class, () -> service.createApplication( + "tagged", "FLINK-1_18", ROLE, null, null, null, null, null, 1, tooMany)); + assertEquals("TooManyTagsException", ex.getErrorCode()); + } + + @Test + void tagResourceThenListTagsForResourceRoundTrips() { + FlinkApplication app = create("demo"); + + Map after = service.tagResource(app.getApplicationArn(), Map.of("team", "platform")); + assertEquals("platform", after.get("team")); + assertEquals("platform", service.listTagsForResource(app.getApplicationArn()).get("team")); + } + + @Test + void untagResourceRemovesKeys() { + FlinkApplication app = create("demo"); + service.tagResource(app.getApplicationArn(), Map.of("team", "platform", "env", "dev")); + + Map after = service.untagResource(app.getApplicationArn(), List.of("team")); + assertFalse(after.containsKey("team")); + assertEquals("dev", after.get("env")); + } + + @Test + void tagResourceRejectsUnknownArn() { + assertThrows(AwsException.class, () -> service.tagResource( + "arn:aws:kinesisanalytics:us-east-1:000000000000:application/nope", Map.of("k", "v"))); + } + + @Test + void tagResourceRejectsExceedingMaxTags() { + FlinkApplication app = create("demo"); + Map tooMany = new HashMap<>(); + for (int i = 0; i < 51; i++) { + tooMany.put("k" + i, "v"); + } + AwsException ex = assertThrows(AwsException.class, + () -> service.tagResource(app.getApplicationArn(), tooMany)); + assertEquals("TooManyTagsException", ex.getErrorCode()); + } + + private FlinkApplication createRunningWithCode(String name) { + service.createApplication(name, "FLINK-1_18", ROLE, null, null, "bucket", "app.jar", null, 1); + return service.startApplication(name); // mock mode: RUNNING immediately + } + + @Test + void createApplicationSnapshotRequiresRunningApplication() { + create("demo"); // READY, never started + AwsException ex = assertThrows(AwsException.class, + () -> service.createApplicationSnapshot("demo", "snap1")); + assertEquals("InvalidRequestException", ex.getErrorCode()); + } + + @Test + void createApplicationSnapshotRejectsWhenSnapshotsDisabled() { + service.createApplication("demo", "FLINK-1_18", ROLE, null, null, + "bucket", "app.jar", null, 1, null, null, false); + service.startApplication("demo"); // mock mode: RUNNING immediately + + AwsException ex = assertThrows(AwsException.class, + () -> service.createApplicationSnapshot("demo", "snap1")); + assertEquals("InvalidRequestException", ex.getErrorCode()); + } + + @Test + void updateApplicationTogglesSnapshotsEnabled() { + FlinkApplication app = createRunningWithCode("demo"); + assertTrue(app.isSnapshotsEnabled()); + + service.updateApplication("demo", 1L, null, null, null, null, null, false); + + AwsException ex = assertThrows(AwsException.class, + () -> service.createApplicationSnapshot("demo", "snap1")); + assertEquals("InvalidRequestException", ex.getErrorCode()); + } + + @Test + void createApplicationSnapshotRequiresCode() { + create("demo"); + service.startApplication("demo"); // mock mode: RUNNING even with no code + AwsException ex = assertThrows(AwsException.class, + () -> service.createApplicationSnapshot("demo", "snap1")); + assertEquals("InvalidRequestException", ex.getErrorCode()); + } + + @Test + void createApplicationSnapshotSucceedsInMockMode() { + createRunningWithCode("demo"); + Snapshot snapshot = service.createApplicationSnapshot("demo", "snap1"); + assertEquals(SnapshotStatus.READY, snapshot.getSnapshotStatus()); + assertEquals(1L, snapshot.getApplicationVersionId()); + } + + @Test + void createApplicationSnapshotRejectsDuplicateName() { + createRunningWithCode("demo"); + service.createApplicationSnapshot("demo", "snap1"); + AwsException ex = assertThrows(AwsException.class, + () -> service.createApplicationSnapshot("demo", "snap1")); + assertEquals("ResourceInUseException", ex.getErrorCode()); + } + + @Test + void describeApplicationSnapshotThrowsWhenMissing() { + createRunningWithCode("demo"); + assertThrows(AwsException.class, () -> service.describeApplicationSnapshot("demo", "nope")); + } + + @Test + void listApplicationSnapshotsIncludesCreated() { + createRunningWithCode("demo"); + service.createApplicationSnapshot("demo", "snap1"); + service.createApplicationSnapshot("demo", "snap2"); + assertEquals(2, service.listApplicationSnapshots("demo").size()); + } + + @Test + void deleteApplicationSnapshotRemovesIt() { + createRunningWithCode("demo"); + Snapshot snapshot = service.createApplicationSnapshot("demo", "snap1"); + service.deleteApplicationSnapshot("demo", "snap1", snapshot.getSnapshotCreationTimestamp()); + assertTrue(service.listApplicationSnapshots("demo").isEmpty()); + } + + @Test + void deleteApplicationSnapshotRejectsWrongTimestamp() { + createRunningWithCode("demo"); + service.createApplicationSnapshot("demo", "snap1"); + assertThrows(AwsException.class, () -> service.deleteApplicationSnapshot( + "demo", "snap1", java.time.Instant.ofEpochSecond(1))); + } + + @Test + void deleteApplicationSnapshotRejectsWhileStillCreating() { + // Real mode, but the mocked FlinkContainerManager.createSnapshot never actually advances the + // snapshot to READY/FAILED, so it stays CREATING — matching a real Flink savepoint still + // in flight when a delete is attempted. + FlinkContainerManager manager = Mockito.mock(FlinkContainerManager.class); + KinesisAnalyticsV2Service realMode = buildService(false, manager); + realMode.createApplication("demo", "FLINK-1_18", ROLE, null, null, "bucket", "app.jar", null, 1); + // Real mode's startApplication only marks STARTING; force RUNNING directly to isolate the + // snapshot precondition from the readiness-poller mechanics already covered elsewhere. + FlinkApplication app = realMode.describeApplication("demo"); + app.setApplicationStatus(ApplicationStatus.RUNNING); + Snapshot snapshot = realMode.createApplicationSnapshot("demo", "snap1"); + assertEquals(SnapshotStatus.CREATING, snapshot.getSnapshotStatus()); + + AwsException ex = assertThrows(AwsException.class, () -> realMode.deleteApplicationSnapshot( + "demo", "snap1", snapshot.getSnapshotCreationTimestamp())); + assertEquals("ResourceInUseException", ex.getErrorCode()); + } + + @Test + void createApplicationSnapshotMarksFailedWhenTriggerThrows() { + FlinkContainerManager manager = Mockito.mock(FlinkContainerManager.class); + Mockito.doThrow(new RuntimeException("jobmanager unreachable")) + .when(manager).createSnapshot(Mockito.any(), Mockito.any()); + KinesisAnalyticsV2Service realMode = buildService(false, manager); + realMode.createApplication("demo", "FLINK-1_18", ROLE, null, null, "bucket", "app.jar", null, 1); + FlinkApplication app = realMode.describeApplication("demo"); + app.setApplicationStatus(ApplicationStatus.RUNNING); + + // The trigger failure marks the snapshot FAILED rather than failing CreateApplicationSnapshot + // itself, the same async-follow-up-failure shape job submission already uses. + Snapshot snapshot = realMode.createApplicationSnapshot("demo", "snap1"); + assertEquals(SnapshotStatus.FAILED, snapshot.getSnapshotStatus()); + } + + @Test + void updateApplicationWithNewCodeWhileReadyJustUpdatesStoredCode() { + service.createApplication("demo", "FLINK-1_18", ROLE, null, null, "bucket", "app.jar", null, 1); + + FlinkApplication updated = service.updateApplication("demo", 1L, null, + "flink-code", "v2.jar", null, null); + + assertEquals("flink-code", updated.getCodeS3Bucket()); + assertEquals("v2.jar", updated.getCodeS3Key()); + assertEquals(ApplicationStatus.READY, updated.getApplicationStatus()); + assertEquals(2L, updated.getApplicationVersionId()); + } + + @Test + void updateApplicationUpdatesParallelism() { + create("demo"); + FlinkApplication updated = service.updateApplication("demo", 1L, null, null, null, null, 5); + assertEquals(5, updated.getParallelism()); + } + + @Test + void updateApplicationRedeploysCodeInPlaceWhenRunningInRealMode() { + FlinkContainerManager manager = Mockito.mock(FlinkContainerManager.class); + KinesisAnalyticsV2Service realMode = buildService(false, manager); + realMode.createApplication("demo", "FLINK-1_18", ROLE, null, null, "bucket", "app.jar", null, 1); + FlinkApplication app = realMode.describeApplication("demo"); + app.setApplicationStatus(ApplicationStatus.RUNNING); + app.setTaskManagerContainerId("tm-1"); + app.setFlinkJobId("job-1"); + + FlinkApplication updated = realMode.updateApplication("demo", 1L, null, + "bucket", "v2.jar", null, null); + + Mockito.verify(manager).redeployCode(app); + assertEquals("v2.jar", updated.getCodeS3Key()); + assertEquals(ApplicationStatus.STARTING, updated.getApplicationStatus()); + } + + @Test + void updateApplicationWrapsRedeployFailureAsInternalFailure() { + FlinkContainerManager manager = Mockito.mock(FlinkContainerManager.class); + Mockito.doThrow(new RuntimeException("docker unavailable")) + .when(manager).redeployCode(Mockito.any()); + KinesisAnalyticsV2Service realMode = buildService(false, manager); + realMode.createApplication("demo", "FLINK-1_18", ROLE, null, null, "bucket", "app.jar", null, 1); + FlinkApplication app = realMode.describeApplication("demo"); + app.setApplicationStatus(ApplicationStatus.RUNNING); + app.setTaskManagerContainerId("tm-1"); + + AwsException ex = assertThrows(AwsException.class, () -> realMode.updateApplication( + "demo", 1L, null, "bucket", "v2.jar", null, null)); + assertEquals("InternalFailure", ex.getErrorCode()); + assertEquals(500, ex.getHttpStatus()); + } + + @Test + void updateApplicationRejectsCodeChangeOnRunningBareCluster() { + FlinkContainerManager manager = Mockito.mock(FlinkContainerManager.class); + KinesisAnalyticsV2Service realMode = buildService(false, manager); + realMode.createApplication("demo", "FLINK-1_18", ROLE, null, null); + FlinkApplication app = realMode.describeApplication("demo"); + app.setApplicationStatus(ApplicationStatus.RUNNING); + + AwsException ex = assertThrows(AwsException.class, () -> realMode.updateApplication( + "demo", 1L, null, "bucket", "app.jar", null, null)); + assertEquals("InvalidRequestException", ex.getErrorCode()); + Mockito.verify(manager, Mockito.never()).redeployCode(Mockito.any()); + } + + @Test + void updateApplicationWithCodeChangeInMockModeSkipsContainerRedeploy() { + service.createApplication("demo", "FLINK-1_18", ROLE, null, null, "bucket", "app.jar", null, 1); + service.startApplication("demo"); // mock mode: RUNNING immediately, no container + + FlinkApplication updated = service.updateApplication("demo", 1L, null, + "bucket", "v2.jar", null, null); + + assertEquals("v2.jar", updated.getCodeS3Key()); + // Mock mode has no container to redeploy against, so the application simply stays RUNNING. + assertEquals(ApplicationStatus.RUNNING, updated.getApplicationStatus()); + } + + @Test + void createApplicationPresignedUrlReturnsRestEndpointWhenRunning() { + FlinkContainerManager manager = Mockito.mock(FlinkContainerManager.class); + KinesisAnalyticsV2Service realMode = buildService(false, manager); + realMode.createApplication("demo", "FLINK-1_18", ROLE, null, null); + FlinkApplication app = realMode.describeApplication("demo"); + app.setApplicationStatus(ApplicationStatus.RUNNING); + app.setRestEndpoint("http://localhost:41000"); + + String url = realMode.createApplicationPresignedUrl("demo", "FLINK_DASHBOARD_URL", 1800L); + assertEquals("http://localhost:41000", url); + } + + @Test + void createApplicationPresignedUrlRejectsWhenNotRunning() { + create("demo"); // READY, never started + AwsException ex = assertThrows(AwsException.class, + () -> service.createApplicationPresignedUrl("demo", "FLINK_DASHBOARD_URL", null)); + assertEquals("ResourceInUseException", ex.getErrorCode()); + } + + @Test + void createApplicationPresignedUrlRejectsUnsupportedUrlType() { + FlinkContainerManager manager = Mockito.mock(FlinkContainerManager.class); + KinesisAnalyticsV2Service realMode = buildService(false, manager); + realMode.createApplication("demo", "FLINK-1_18", ROLE, null, null); + FlinkApplication app = realMode.describeApplication("demo"); + app.setApplicationStatus(ApplicationStatus.RUNNING); + app.setRestEndpoint("http://localhost:41000"); + + AwsException ex = assertThrows(AwsException.class, + () -> realMode.createApplicationPresignedUrl("demo", "ZEPPELIN_UI_URL", null)); + assertEquals("InvalidArgumentException", ex.getErrorCode()); + } + + @Test + void createApplicationPresignedUrlRejectsOutOfRangeExpiration() { + FlinkContainerManager manager = Mockito.mock(FlinkContainerManager.class); + KinesisAnalyticsV2Service realMode = buildService(false, manager); + realMode.createApplication("demo", "FLINK-1_18", ROLE, null, null); + FlinkApplication app = realMode.describeApplication("demo"); + app.setApplicationStatus(ApplicationStatus.RUNNING); + app.setRestEndpoint("http://localhost:41000"); + + AwsException ex = assertThrows(AwsException.class, + () -> realMode.createApplicationPresignedUrl("demo", "FLINK_DASHBOARD_URL", 100L)); + assertEquals("InvalidArgumentException", ex.getErrorCode()); + } + + @Test + void createApplicationPresignedUrlRejectsUnknownApplication() { + assertThrows(AwsException.class, + () -> service.createApplicationPresignedUrl("nope", "FLINK_DASHBOARD_URL", null)); + } + + @Test + void readinessPollerPersistsFlinkJobIdAssignedBeforeReachingRunning() throws InterruptedException { + // Regression test: advanceToRunning can assign flinkJobId on a tick where the Flink job has + // been submitted but hasn't reached RUNNING yet (it returns false in that case). That + // intermediate state must still be persisted — not just the final RUNNING transition — since + // pendingJars (a separate in-process-only cache) is already cleared by then; an emulator + // restart before the next persist would otherwise leave the application permanently stuck. + InMemoryStorage backing = Mockito.spy(new InMemoryStorage<>()); + StorageFactory storageFactory = Mockito.mock(StorageFactory.class); + Mockito.doReturn(backing).when(storageFactory) + .create(Mockito.anyString(), Mockito.anyString(), Mockito.any()); + + EmulatorConfig config = Mockito.mock(EmulatorConfig.class); + var servicesConfig = Mockito.mock(EmulatorConfig.ServicesConfig.class); + var kaConfig = Mockito.mock(EmulatorConfig.KinesisAnalyticsServiceConfig.class); + when(config.services()).thenReturn(servicesConfig); + when(servicesConfig.kinesisAnalytics()).thenReturn(kaConfig); + when(kaConfig.mock()).thenReturn(false); + when(config.defaultRegion()).thenReturn("us-east-1"); + + FlinkContainerManager manager = Mockito.mock(FlinkContainerManager.class); + when(manager.advanceToRunning(Mockito.any())).thenAnswer(invocation -> { + FlinkApplication app = invocation.getArgument(0); + if (app.getFlinkJobId() == null) { + app.setFlinkJobId("job-1"); // job just submitted; not yet RUNNING + } + return false; + }); + + RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000"); + KinesisAnalyticsV2Service realMode = + new KinesisAnalyticsV2Service(storageFactory, config, regionResolver, manager); + realMode.createApplication("demo", "FLINK-1_18", ROLE, null, null, "bucket", "app.jar", null, 1); + FlinkApplication app = realMode.describeApplication("demo"); + app.setApplicationStatus(ApplicationStatus.STARTING); + Mockito.clearInvocations(backing); // ignore createApplication's own put() + + try { + // @PostConstruct isn't wired by a plain `new` in this unit test; start the poller manually. + realMode.init(); + // The poller's first tick fires ~1s after scheduling; give it margin. + Thread.sleep(1500); + + Mockito.verify(backing, Mockito.atLeastOnce()).put(Mockito.eq("demo"), Mockito.any()); + assertEquals("job-1", realMode.describeApplication("demo").getFlinkJobId()); + } finally { + realMode.shutdown(); + } + } + + private KinesisAnalyticsV2Service mockModeService() { + return buildService(true, Mockito.mock(FlinkContainerManager.class)); + } + + private KinesisAnalyticsV2Service realModeServiceWithFailingManager() { + FlinkContainerManager failing = Mockito.mock(FlinkContainerManager.class); + Mockito.doThrow(new RuntimeException("docker unavailable")) + .when(failing).startCluster(Mockito.any()); + return buildService(false, failing); + } + + private KinesisAnalyticsV2Service buildService(boolean mock, FlinkContainerManager manager) { + StorageFactory storageFactory = Mockito.mock(StorageFactory.class); + when(storageFactory.create(Mockito.anyString(), Mockito.anyString(), Mockito.any())) + .thenReturn(new InMemoryStorage<>()); + + EmulatorConfig config = Mockito.mock(EmulatorConfig.class); + var servicesConfig = Mockito.mock(EmulatorConfig.ServicesConfig.class); + var kaConfig = Mockito.mock(EmulatorConfig.KinesisAnalyticsServiceConfig.class); + when(config.services()).thenReturn(servicesConfig); + when(servicesConfig.kinesisAnalytics()).thenReturn(kaConfig); + when(kaConfig.mock()).thenReturn(mock); + when(config.defaultRegion()).thenReturn("us-east-1"); + + RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000"); + return new KinesisAnalyticsV2Service(storageFactory, config, regionResolver, manager); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java new file mode 100644 index 0000000000..99c1d5093d --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java @@ -0,0 +1,81 @@ +package io.github.hectorvent.floci.services.kinesisanalytics.container; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.docker.ContainerBuilder; +import io.github.hectorvent.floci.core.common.docker.ContainerDetector; +import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager; +import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer; +import io.github.hectorvent.floci.services.kinesisanalytics.model.FlinkApplication; +import io.github.hectorvent.floci.services.s3.S3Service; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Unit coverage for {@link FlinkContainerManager}'s {@code application_properties.json} construction — + * the JSON shape must exactly match real MSF/KDA's runtime file so a real + * {@code KinesisAnalyticsRuntime.getApplicationProperties()} call in a user's JAR finds it. The + * container-injection path itself (tar-entry-prefix trick to create the non-existent {@code /etc/flink} + * directory via the daemon-level copy) was verified live against a real {@code apache/flink} image + * rather than re-implemented here with a mocked DockerClient — see the design notes on the injection + * call sites for what was checked and why. + */ +class FlinkContainerManagerTest { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private FlinkContainerManager manager() { + return new FlinkContainerManager( + Mockito.mock(ContainerBuilder.class), + Mockito.mock(ContainerLifecycleManager.class), + Mockito.mock(ContainerLogStreamer.class), + Mockito.mock(ContainerDetector.class), + Mockito.mock(EmulatorConfig.class), + Mockito.mock(RegionResolver.class), + Mockito.mock(S3Service.class), + Mockito.mock(FlinkRestClient.class), + MAPPER); + } + + @Test + void applicationPropertiesJsonMatchesTheRealMsfFileShape() throws Exception { + FlinkApplication app = new FlinkApplication("demo", "arn:aws:kinesisanalytics:us-east-1:000000000000:application/demo", + "FLINK-1_18", "arn:aws:iam::000000000000:role/x", "STREAMING"); + Map> groups = new LinkedHashMap<>(); + groups.put("ProducerConfigProperties", Map.of("flink.stream.initpos", "LATEST", "aws.region", "us-west-2")); + groups.put("ConsumerConfigProperties", Map.of("aws.region", "us-west-2")); + app.setEnvironmentProperties(groups); + + byte[] json = manager().applicationPropertiesJson(app); + JsonNode root = MAPPER.readTree(json); + + assertTrue(root.isArray()); + assertEquals(2, root.size()); + assertEquals("ProducerConfigProperties", root.get(0).get("PropertyGroupId").asText()); + assertEquals("LATEST", root.get(0).get("PropertyMap").get("flink.stream.initpos").asText()); + assertEquals("us-west-2", root.get(0).get("PropertyMap").get("aws.region").asText()); + assertEquals("ConsumerConfigProperties", root.get(1).get("PropertyGroupId").asText()); + } + + @Test + void applicationPropertiesJsonIsAnEmptyArrayWhenNoPropertiesConfigured() throws Exception { + FlinkApplication app = new FlinkApplication("bare", "arn:aws:kinesisanalytics:us-east-1:000000000000:application/bare", + "FLINK-1_18", "arn:aws:iam::000000000000:role/x", "STREAMING"); + + byte[] json = manager().applicationPropertiesJson(app); + JsonNode root = MAPPER.readTree(json); + + // Real MSF always provides the file, even with zero property groups configured, so + // KinesisAnalyticsRuntime.getApplicationProperties() never has to handle a missing file. + assertTrue(root.isArray()); + assertEquals(0, root.size()); + } +} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index ca5359a04d..eb2664beb4 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -125,6 +125,9 @@ floci: amazonmq: enabled: true mock: true + kinesis-analytics: + enabled: true + mock: true elasticache: enabled: true proxy-base-port: 6379 diff --git a/tools/docs/services.yaml b/tools/docs/services.yaml index 7ff1db5f34..3054bdcae5 100644 --- a/tools/docs/services.yaml +++ b/tools/docs/services.yaml @@ -54,6 +54,10 @@ services: doc: docs/services/kinesis.md sources: - { path: src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisJsonHandler.java, mode: switch } + - service: kinesisanalytics + doc: docs/services/kinesisanalytics.md + sources: + - { path: src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandler.java, mode: switch } - service: kms doc: docs/services/kms.md sources: