fluss icon indicating copy to clipboard operation
fluss copied to clipboard

[flink] Undo recovery deletes or reverts rows written by other clients to an aggregation table

Open mcgrj opened this issue 1 week ago • 1 comments

Search before asking

  • [x] I searched in the issues and found nothing similar.

Fluss version

1.0.0 (latest release)

Please describe the bug 🐞

When a Flink sink on an aggregation table restores, undo recovery reverts every change in the bucket log after the sink's restored offset, whichever client wrote it. Rows that any other client wrote in that window are deleted or rolled back. Suggested fix: skip, or fail on, other writers' batches during undo (details under Solution).

Reproduce (1.0.0, Flink 1.20.3 with fluss-flink-1.20-1.0.0.jar):

  1. In Flink SQL:
    CREATE TABLE agg_core (id INT, v BIGINT, PRIMARY KEY (id) NOT ENFORCED)
      WITH ('table.merge-engine' = 'aggregation', 'fields.v.agg' = 'sum', 'bucket.num' = '1');
    CREATE TABLE src_core (id INT, dt STRING, v BIGINT) WITH ('bucket.num' = '1');
    SET 'execution.checkpointing.interval' = '1 h';
    INSERT INTO agg_core SELECT id, v FROM src_core;
    
  2. Run INSERT INTO src_core VALUES (1, 'x', 10), (2, 'x', 10), (3, 'x', 10), (4, 'x', 10), (5, 'x', 10);, then trigger a checkpoint. The log end offset is 5.
  3. From a separate Java client:
    Configuration conf = new Configuration();
    conf.setString("bootstrap.servers", "localhost:9123");
    try (Connection conn = ConnectionFactory.createConnection(conf);
         Table table = conn.getTable(TablePath.of("fluss", "agg_core"))) {
        UpsertWriter w = table.newUpsert().createWriter();
        for (int id = 1001; id <= 1010; id++) w.upsert(GenericRow.of(id, 7L));
        w.upsert(GenericRow.of(1, 100L)); // SUM, id 1 becomes 110
        w.flush();
    }
    
  4. kill -9 the TaskManager and start a new one.

Result: ids 1001..1010 are gone and id 1 is back to 10.

RecoveryOffsetManager - Subtask 0: bucket=TableBucket{tableId=40, bucket=0}, baseline=5 (explicit=true), current=17
UndoRecoveryManager   - Completed undo recovery for 1 bucket(s) with 11 undo operation(s) on subtask 0/1

Also reproduced:

  • Rows another client writes before the checkpoint are also deleted. The restored baseline is the sink's own last offset, not the log end at checkpoint time (baseline=2, current=7 after 2 sink rows and 5 rows from the other client).
  • Restoring from a flink stop --savepointPath savepoint gives the same result.
  • A partition created after the job starts and written only by another client is wiped: baseline=0 (explicit=false), current=1000, and all 1000 rows are gone.
  • A new job with no state but the same sink.producer-id gets ALREADY_EXISTS, runs PRODUCER_OFFSET_RECOVERY, and deletes the other client's rows.

Cause: UndoComputer.java#L129-L143 reverts every record in [baseline, current) with no writer check. RecoveryOffsetManager.java#L286-L287 uses baseline 0 for buckets missing from state. FIP-21 assumes "single-job writes or non-overlapping column writes", but the aggregation merge engine docs don't say so and nothing enforces it.

Solution

  1. Register each writer id against the sink's producer id before the first write, for example an optional producer_id on InitWriterRequest, stored with the producer offsets TTL.
  2. Pass the batch writer id to the undo scan filter, and undo only batches from writer ids registered to that producer.
  3. Keep today's behaviour, with a WARN, when idempotence is off (no writer id). Warn or fail when another writer changed the same key after the sink. A key changed by both the sink and another client after the checkpoint cannot be undone safely.
  4. Until then, document on the aggregation merge engine page that a Flink job writing an aggregation table must be the only writer (#5331).

Are you willing to submit a PR?

  • [x] I'm willing to submit a PR!

mcgrj avatar Nov 21 '26 14:11 mcgrj

This still reproduces on main (c2448838d).

I'm happy to raise a PR for the writer-id approach in the issue description: an optional producer_id on InitWriterRequest so the server records which writer ids belong to the sink, then undo only reverts batches from those ids. It's a fairly big change across rpc, server, client and flink though, so I'd like to get maintainers' thoughts before going ahead. Does this approach sound reasonable?

mcgrj avatar Nov 21 '26 15:11 mcgrj