[flink] DataStream FlussSource shares one KV snapshot lease ID per JVM, so one job can drop or overwrite another job's snapshot lease
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.DEFAULTis a static created once per JVM:new LeaseContext(UUID.randomUUID().toString(), Duration.ofDays(1).toMillis())(LeaseContext.java#L34-L35).FlussSourcealways passes it toFlinkSource(FlussSource.java#L165).FlussSourceBuilderhas no option to set a lease ID or duration. Only the SQL connector readsscan.kv.snapshot.lease.*.- With the connector jar in the Flink
lib/directory, as the docs recommend for a session cluster, everyFlussSourcejob 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:
- Drop on close. For a PK table with
fullstartup,FlinkSourceEnumerator#maybeDropKvSnapshotLeasedrops the lease onclose()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#dropLeaseremoves every table and bucket under the ID (KvSnapshotLeaseManager.java#L302), including snapshots pinned by other running jobs. - Overwrite on acquire. When a second job acquires a snapshot with the same ID on the same bucket,
KvSnapshotLeaseHandler#acquireBucketreplaces the earlier snapshot ID (KvSnapshotLeaseHandler.java#L124).KvSnapshotLeaseManagerthen 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.
- Take snapshot S0.
- Start job A: a streaming
FlussSourcewithOffsetsInitializer.full()and no checkpointing. It acquires S0. - Run job B to completion: a batch
FlussSource(setBatch(),full) on the same table. Its enumeratorclose()callsdropLease()with the shared ID. - A is still RUNNING, but the lease no longer exists. Take two more snapshots: S0 is deleted (
getKvSnapshotMetadatathrowsKvSnapshotNotExistException). - Variant without any drop: A holds S0. Take S1. Start a second streaming
fulljob 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. - Control: give each source a distinct
LeaseContext(we setFlinkSource.leaseContextby 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 inFlussSourceBuilder#build(). The enumerator already persists the lease ID in its state, so restore keeps working. - Add
setKvSnapshotLeaseId(String)andsetKvSnapshotLeaseDuration(Duration)toFlussSourceBuilder, mirroring the SQL optionsscan.kv.snapshot.lease.idandscan.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!