fluss icon indicating copy to clipboard operation
fluss copied to clipboard

[flink] DataStream FlussSource shares one KV snapshot lease ID per JVM, so one job can drop or overwrite another job's snapshot lease

Open ymkim1006 opened this issue 1 month ago • 0 comments

Search before asking

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

Fluss version

1.0.0 (latest release). The relevant code is unchanged on main (dc3eb23); links below point to that commit.

Please describe the bug 🐞

The DataStream FlussSource always uses one KV snapshot lease ID per JVM, so in a Flink session cluster one source job can drop or overwrite the snapshot lease of another source job. The other job's snapshot is then deleted while that job may still be reading it.

Why the lease ID is shared

  • LeaseContext.DEFAULT is a static created once per JVM: new LeaseContext(UUID.randomUUID().toString(), Duration.ofDays(1).toMillis()) (LeaseContext.java#L34-L35).
  • FlussSource always passes it to FlinkSource (FlussSource.java#L165).
  • FlussSourceBuilder has no option to set a lease ID or duration. Only the SQL connector reads scan.kv.snapshot.lease.*.
  • With the connector jar in the Flink lib/ directory, as the docs recommend for a session cluster, every FlussSource job submitted to the same cluster uses the same lease ID.

How one job affects another

The server keys a lease only by its ID. It holds one snapshot ID per (table, bucket), and it does not record which job acquired it. Two paths unpin another job's snapshot:

  1. Drop on close. For a PK table with full startup, FlinkSourceEnumerator#maybeDropKvSnapshotLease drops the lease on close() if no checkpoint was ever triggered (FlinkSourceEnumerator.java#L1775). This is always the case for a batch job, and also for a streaming job that fails or is cancelled before its first checkpoint. The code comment says this is safe because the lease ID was not persisted to state. That only holds if the ID belongs to this source alone. KvSnapshotLeaseManager#dropLease removes every table and bucket under the ID (KvSnapshotLeaseManager.java#L302), including snapshots pinned by other running jobs.
  2. Overwrite on acquire. When a second job acquires a snapshot with the same ID on the same bucket, KvSnapshotLeaseHandler#acquireBucket replaces the earlier snapshot ID (KvSnapshotLeaseHandler.java#L124). KvSnapshotLeaseManager then decrements the ref count of the original snapshot (KvSnapshotLeaseManager.java#L237-L238). The first job's snapshot is unpinned without any drop.

notifyCheckpointComplete → releaseSnapshots also uses the shared ID (FlinkSourceEnumerator.java#L1703).

Once a snapshot is no longer pinned and falls outside kv.snapshot.num-retained, the next snapshot makes CompletedSnapshotStore delete it (CompletedSnapshotStore.java#L185, #L218). This can happen while the other job is still reading that snapshot. With a 10-minute snapshot interval and 2 retained snapshots, that can happen about 20 minutes after the lease is lost.

Reproduction

We reproduced this in a JUnit 5 test with FlussClusterExtension and one Flink MiniClusterExtension, so both sources run in one JVM like jobs in a session cluster. The test used a PK table, kv.snapshot.num-retained=2, and snapshots triggered manually.

  1. Take snapshot S0.
  2. Start job A: a streaming FlussSource with OffsetsInitializer.full() and no checkpointing. It acquires S0.
  3. Run job B to completion: a batch FlussSource (setBatch(), full) on the same table. Its enumerator close() calls dropLease() with the shared ID.
  4. A is still RUNNING, but the lease no longer exists. Take two more snapshots: S0 is deleted (getKvSnapshotMetadata throws KvSnapshotNotExistException).
  5. Variant without any drop: A holds S0. Take S1. Start a second streaming full job B on the same table; it acquires S1 with the same ID, so the lease now holds S1. Take S2: S0 is deleted while both jobs are RUNNING.
  6. Control: give each source a distinct LeaseContext (we set FlinkSource.leaseContext by reflection in the test). A's lease and S0 survive B finishing and two more snapshots.

We observed the snapshot being removed from the server metadata. We did not go on to make the reading job fail on missing files.

Expected: a source's snapshot lease is isolated from other sources and jobs. One job's drop, release or acquire never unpins a snapshot that another job holds.

Actual: all FlussSource instances in a JVM share one lease, so one job can unpin another job's snapshot, and the server then deletes it.

Solution

  • Generate the lease ID per source instance instead of using the JVM-wide LeaseContext.DEFAULT, for example in FlussSourceBuilder#build(). The enumerator already persists the lease ID in its state, so restore keeps working.
  • Add setKvSnapshotLeaseId(String) and setKvSnapshotLeaseDuration(Duration) to FlussSourceBuilder, mirroring the SQL options scan.kv.snapshot.lease.id and scan.kv.snapshot.lease.duration.
  • Optionally, scope server-side drop and release to the caller. Otherwise any client that reuses an ID can clear another client's lease.

A possible workaround until then is to construct FlinkSource directly through its public constructor with a distinct LeaseContext, bypassing FlussSourceBuilder.

Are you willing to submit a PR?

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

ymkim1006 avatar Oct 29 '26 04:10 ymkim1006