tree: 34cb659b215268a8aff977afa45174906b88d2f0 [search]
  1. data/
  2. runtime/
  3. sql/
  4. src/
  5. .dockerignore
  6. .gitignore
  7. compose.yaml
  8. prepare.sh
  9. README.md
  10. run.py
flink-multi-window-aggregation/README.md

Hotel demand windows with Flink and Fluss

This directory runs an isolated, synthetic Flink SQL example with Apache Flink 1.20.3, Apache Fluss 1.0.0 and the Fluss Flink 1.20 connector 1.0.0. Flink SQL Client executes the pipeline via sql/transform.sql and sql/write.sql (EXECUTE STATEMENT SET), with sql/catalog.sql and sql/setup.sql for catalog configuration and DDL; only src/AssignWindows.java (blog.AssignWindows) is Java. It compares a Flink GroupAggregate reference with Fluss SUM accumulation against an independent current-order snapshot oracle.

Prerequisites

  • Docker Engine with Docker Compose v2 or newer, at least 6 GiB available to Docker, and approximately 8 GiB of free disk for images, the connector cache and the small cluster.
  • Python 3.11 or newer and curl. No host JDK, Maven, tar or Python packages are required; src/AssignWindows.java is compiled inside the Docker build.
  • Network access to Docker Hub and Maven Central for initial preparation.
  • TCP port 18096 available on localhost. Do not run multiple copies with the same Compose project name.

Run

From this directory:

./prepare.sh
python3 run.py verify
python3 run.py query

prepare.sh downloads the Fluss Flink 1.20 connector 1.0.0 into .runtime/ (re-verifying its SHA-256 against runtime/artifacts.json before reusing any cached copy), then runs Docker Compose to build the custom Flink image and start five containers. Flink REST is available at http://127.0.0.1:18096.

Container images follow official upstream bases pinned for reproducibility:

  • Fluss Coordinator and TabletServer: official apache/fluss:1.0.0 (Java 17), pinned by multi-platform manifest digest.
  • ZooKeeper: official zookeeper:3.9.2, pinned by version tag.
  • Flink JobManager and TaskManager: built by runtime/Dockerfile.flink from official flink:1.20.3-scala_2.12-java11 (pinned by multi-platform manifest digest, retaining the default official entrypoint). The build stage uses a digest-pinned Eclipse Temurin Java 11 JDK to compile src/AssignWindows.java (blog.AssignWindows) against the libraries bundled in that Flink image and the verified connector JAR (runtime/connector.sha1 records the upstream checksum source); the runtime stage layers the connector JAR and /opt/flink/assign-windows.jar onto the same Flink Java 11 JRE base.

The current Flink SQL implementation has passed all seven verification phases—including both real TaskManager SIGKILL recoveries from checkpoints 1 and 2, matching all 10 final window keys with unchanged results—using the pinned Flink 1.20.3 and Fluss 1.0.0 binaries in local archive-built containers. Cold-start verification against the official container images defined in compose.yaml and runtime/Dockerfile.flink is separately pending.

verify requires a fresh tutorial cluster. Python (run.py) drives Flink SQL Client (sql-client.sh) for catalog/DDL setup (sql/catalog.sql and sql/setup.sql, which create the hotel_blog database and four tables—orders, channels, window_totals, and baseline—and seed channels), streaming job submission (sql/transform.sql and sql/write.sql), staged fixture writes into orders, and bounded batch snapshot queries against window_totals and baseline. It controls checkpoints and failure injection, and prints progress only after complete snapshots from both result tables match the independent Python oracle.

The script deliberately kills this project‘s TaskManager twice: after a successful write beyond a completed checkpoint, and after a subsequent completed checkpoint. Other Docker projects are not touched. Checkpoints and Fluss data are retained in this project’s named volumes across TaskManager restarts. JobManager or storage-server loss, rescaling, mutable dimensions and online migration are outside this experiment.

The verification stages are:

  1. Insert orders A and B into orders; reconcile all four metrics for every window key.
  2. Complete checkpoint 1, recording the orders$changelog source offset and window_totals sink undo recovery state.
  3. Update A's amount in orders from 10000 to 12000 cents and verify the uncheckpointed write.
  4. Kill/restart TaskManager, verify restored checkpoint 1, and reconcile the replayed update.
  5. Cancel A in orders; verify business cancellation and changelog _change_type retraction are separate.
  6. Delete B from orders; check retained zero rows.
  7. Insert C into orders with a missing dimension key; check default channel flag and continued accumulation into previously zero windows.
  8. Complete checkpoint 2 and kill/restart TaskManager again; reconcile the committed state across all 10 final keys.

Evidence is written to evidence/: the actual physical plan (job-graph.json, which shows GroupAggregate only in the baseline path), stage snapshots, checkpoint metadata, recovery records and result.json. A PASS is written only after every stage succeeds. run.py paces stages by writing batches of changes into orders via SQL Client; it does not supply or change business timestamps.

To rerun from a fresh cluster, first clean up and run preparation again. Starting a new job against previously accumulated SUM tables would count the input again.

Input and contract

data/images.csv contains eight ordered, complete changelog images, without a header. Columns are:

RowKind,hotel_id,order_id,stay_date,observed_at,created_at,updated_at,cancelled,amount_cents,rate_code

The logical order key is (hotel_id, order_id, stay_date), which is also the primary key of the Fluss orders table. When staging fixture changes via SQL Client, run.py issues INSERT INTO orders for INSERT and UPDATE_AFTER rows (skipping UPDATE_BEFORE because the orders primary-key table generates the old image automatically) and DELETE FROM orders for DELETE rows. The streaming SQL job reads the resulting full changelog from the Fluss 1.0.0 orders$changelog virtual table. All timestamps are naive UTC values; SQL Client sets table.local-time-zone = 'UTC'. observed_at is the fixed first-observation timestamp for an order and is preserved in each image. This is explicitly different from the original processing-time architecture.

Two window specifications (length 2 and 4 minutes, both step 1 minute) are enumerated by blog.AssignWindows (src/AssignWindows.java, registered as ASSIGN_WINDOWS), a stateless Java table function. Membership is [start,end). The creation and cancellation expressions retain inclusive BETWEEN endpoints. Filters use updated_at >= observed_at - window_length and stay_date >= DATE(observed_at). There is no watermark, window finalization or TTL; old windows remain correctable.

Amounts are non-null integer cents. The small fixture stays safely within BIGINT range. NULL amounts and arithmetic overflow are outside this example's input contract. The static channels table maps OTA to 1, DIRECT to 0, and a missing key to 0. Dimension values must stay fixed for this run: a live, changing lookup dimension would require a separate contract for the enriched before-image.

sql/transform.sql defines images over orders$changelog with PROCTIME() AS lookup_pt for the static channels lookup, and maps _change_type in SQL CASE ('insert' and 'update_after' -> +1; 'update_before' and 'delete' -> -1) to row_sign. Both statements in sql/write.sql (EXECUTE STATEMENT SET) consume contributions and multiply each metric by row_sign as append-only signed INSERT rows. The business cancellation flag is evaluated inside each image's factor before applying row_sign. The Fluss path (window_totals) does not send business DELETE operations to the aggregation merge engine.

window_totals is the published output. Its full key is (hotel_id,stay_date,window_minutes,window_start,window_end). Net-zero rows remain present. There is no wide-table projection or implicit choice of a “latest” window.

Flink checkpoint recovery pairs the orders$changelog source offset with the connector's undo state. The SUM table does not deduplicate arbitrary business events or business IDs. Starting a new job to re-read orders$changelog against the same table is not a valid recovery procedure.

Files

  • src/AssignWindows.java: the only Java source file; top-level stateless blog.AssignWindows UDTF enumerating the 2- and 4-minute windows.
  • sql/catalog.sql: Fluss catalog configuration (fluss catalog pointing at coordinator:9123).
  • sql/setup.sql: hotel_blog database, orders primary-key input table, static channels lookup table (seeded with DIRECT and OTA), window_totals aggregation table (with sum explicitly configured on every metric), and baseline standard PK reference table.
  • sql/transform.sql: loads /opt/flink/assign-windows.jar, registers ASSIGN_WINDOWS, defines the images view over orders$changelog with PROCTIME() AS lookup_pt, maps _change_type to row_sign via SQL CASE, performs static dimension lookup, stateless window expansion, deterministic filtering, and per-image contributions.
  • sql/write.sql: EXECUTE STATEMENT SET submitting direct Fluss accumulation into window_totals and the Flink GroupAggregate reference into baseline from contributions.
  • run.py: runs Flink SQL Client for setup, staged orders fixture writes, job submission, and bounded batch snapshot queries; controls checkpoints and real TaskManager SIGKILL recovery; and independently reconciles both tables against the Python oracle.
  • runtime/: Dockerfile.flink (multi-stage Flink image build compiling src/AssignWindows.java into assign-windows.jar), artifacts.json (pinned connector SHA-256) and connector.sha1 (upstream Maven Central checksum source).

The oracle retains the current complete order image and recomputes its windows from scratch; it does not add the candidate's signed deltas. Historical touched keys are filled with zero to match the agreed zero-row retention contract. Comparing both paths alone would miss a shared expansion or sign bug.

Cleanup

python3 run.py cleanup

This removes only the hotel-window-blog Compose project's containers, network and named volumes, including its checkpoints and table data. Evidence and source files remain on disk. The connector cache in .runtime/ and local Docker images can be removed separately when no longer needed.