Files
floci/docs/services/kinesisanalytics.md
T
Priamo Germosen aaae99282f feat(kinesisanalytics): emit MSF-style JSON CloudWatch logs from Flink (#2656)
* feat(kinesisanalytics): emit MSF-style JSON CloudWatch logs from Flink

The JobManager/TaskManager now start with a log4j-console.properties
that formats every log line as a single JSON object matching real
Managed Service for Apache Flink's CloudWatch Logs schema
(applicationARN, applicationVersionId, locationInformation, logger,
message, messageSchemaVersion, messageType, threadName,
throwableInformation), instead of the stock image's default plain-text
pattern. Applies to Flink's own framework logs and anything a deployed
JAR logs through SLF4J/Log4j2, so a JAR that expects its logs to reach
CloudWatch (or asserts on this schema) sees the same thing here as on
real MSF.

Written into the container before the JVM starts -- log4j2 reads its
config at process boot, so copying the file in after start (the way
application_properties.json already is) would be too late. Required
splitting the JobManager/TaskManager container startup from
createAndStart into create + copyFileIntoContainer + startCreated,
mirroring the create-then-inject-then-start pattern
ContainerLifecycleManager already exposed for exactly this purpose.

* fix(kinesisanalytics): escape log4j2 pattern injection and fail loudly on log4j copy failure

ApplicationName flows unvalidated into the ARN baked into the JSON log
pattern, so a name containing '%' could be interpreted as a log4j2
conversion specifier and a '"' or '\' could break the emitted JSON.
Escape both before embedding, reusing the existing %enc{}{JSON}
wrapper to JSON-encode the value at log time.

Also stop silently swallowing a failed log4j-console.properties copy:
startCluster previously reported success and ran with the stock
(non-JSON) log format if the copy failed. The copy is now required,
so a failure rolls back the cluster the same way a create() failure
already does.

* fix(kinesisanalytics): validate ApplicationName, fix NPE, and block log4j2 Lookup injection

CI failure: startClusterLabelsJobManagerContainerWithResourceIdentity
built a FlinkApplication with no ARN set, and literalForLog4j2Pattern
NPE'd on the null value. Wrap both baked-in values with
String.valueOf() so a null degrades to the literal text "null"
instead of throwing.

Also validate ApplicationName against AWS's real CreateApplication
constraint (Pattern: [a-zA-Z0-9_.-]+, 1-128 chars) instead of only
requiring it non-blank -- this is both a genuine AWS-parity fix and
what keeps '%', '"', '\', or '$' out of the generated log4j2 pattern
in the first place.

Finally, close a gap the previous escaping missed: '$' wasn't
neutralized, so a name containing a log4j2 Lookup ('${env:...}',
'${sys:...}') would have it resolved against the process environment
at config-load time and baked into every emitted log line. Doubling
'$' the way '%' is doubled only defers the lookup to render time
rather than neutralizing it, so literalForLog4j2Pattern now drops '$'
outright.

* fix(kinesisanalytics): reject legacy invalid ApplicationName at StartApplication too

Greptile follow-up: an application persisted before the
ApplicationName validation existed (e.g. one with '$' in its name)
would bypass CreateApplication's new check entirely, and
StartApplication would silently generate a CloudWatch-log
applicationARN with the '$' dropped -- mismatching the real
ApplicationARN.

I verified empirically (a standalone harness against the actual
log4j-core jar) that there's no safe way to preserve '$' here: doubling
it or backslash-escaping it both still let log4j2 resolve the Lookup
and leak the referenced environment variable, so dropping it was the
only safe option. Given that, StartApplication now re-validates the
name and fails loudly for such legacy state instead of quietly
emitting a mismatched ARN -- real AWS could never have had this state
to begin with, so refusing to start it is the more AWS-faithful
outcome anyway.

* fix(kinesisanalytics): fix test compile error from Mockito generic inference

when(storageFactory.create(...)).thenReturn(store) failed to compile:
create()'s type parameter defaults to Object with no target-type
context to unify against the pre-declared, concretely-typed 'store'
variable, so thenReturn(store) rejected it as a type mismatch.
Switch to doReturn(store).when(storageFactory).create(...), matching
the same pattern already used for this exact scenario elsewhere in
this file (readinessPollerPersistsFlinkJobIdAssignedBeforeReachingRunning).
2026-08-29 09:15:52 -05:00

253 lines
13 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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
<!-- floci:actions:start -->
| 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 |
<!-- floci:actions:end -->
## 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 <SnapshotCreationTimestamp from describe>
```
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.
### CloudWatch Logs format
The JobManager and TaskManager are started with a `log4j-console.properties` that makes every log
line a single JSON object, matching the shape real Managed Service for Apache Flink writes to
CloudWatch Logs: `applicationARN`, `applicationVersionId`, `locationInformation`, `logger`,
`message`, `messageSchemaVersion`, `messageType`, `threadName`, `throwableInformation`. This applies
to Flink's own framework logs as well as anything a deployed JAR logs through SLF4J/Log4j2 — a JAR
whose own tests assert on this exact schema (or that simply expects its logs to reach CloudWatch
rather than being swallowed by a competing logging framework it bundles) sees the same thing here as
on real MSF. The config is written into the container before the JVM starts (config placed at
creation time, before `StartApplication`'s container start), overwriting the stock image's default,
non-JSON config entirely. `throwableInformation` is always present (an empty string when the log
event has no exception), unlike real AWS which omits the key entirely in that case. Not re-applied
on `UpdateApplication` — `applicationVersionId` in already-emitted log lines does not advance across
an in-place code update, since the JobManager/TaskManager JVMs (and therefore log4j2) are not
restarted for that, matching how real MSF also keeps the same processes running across an
`UpdateApplication`.
`applicationARN` and `applicationVersionId` are baked into the generated config as literal text, so
`CreateApplication` validates `ApplicationName` against AWS's own constraints (`[a-zA-Z0-9_.-]+`,
1-128 characters) rather than only requiring it non-blank — matching real AWS, and also keeping a
name containing `%`, `"`, `\`, or `$` out of the log4j2 pattern in the first place. `StartApplication`
re-checks the same pattern, so an application persisted by an older Floci build (before this
validation existed) fails loudly there rather than getting a corrupted `applicationARN` in its logs.
### 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
```