Skip to content

[client] Stop KV snapshot downloads when scanner closes - #5289

Open
pbanakar wants to merge 3 commits into
apache:mainfrom
pbanakar:fix-kv-snapshot-scanner-download
Open

pbanakar wants to merge 3 commits into
apache:mainfrom
pbanakar:fix-kv-snapshot-scanner-download

Conversation

@pbanakar

@pbanakar pbanakar commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Linked issue: close #5239

When KvSnapshotBatchScanner was closed during a KV snapshot download, background download workers could continue writing to disk after the scanner deleted the local snapshot directory. On shared TaskManagers, this could cause disk usage to grow after jobs were stopped or cancelled.

This PR closes registered download streams and interrupts the scanner’s initialization task when the scanner is closed. close() waits for initialization for a bounded time; if a worker has not stopped by then, the initialization task performs the final directory cleanup after the workers finish. This prevents cleanup from racing with workers that may still create files.

The shared download utility is also used by tablet-server KV recovery. On download failure, it now closes registered streams and waits for in-flight workers to finish before deleting their target directories.

Brief change log

  • Track active download streams in a scanner-owned CloseableRegistry so close() can abort them.
  • Move snapshot download I/O outside the scanner lock.
  • Bound the time close() waits for initialization; defer final directory cleanup to the initialization task if it is still running.
  • Propagate initialization failures to a waiting pollBatch() instead of allowing a null-reader failure.
  • Make the shared download utility wait for in-flight workers before failure cleanup.

Tests

  • Added scanner tests covering close during a blocked download, eventual directory cleanup, and download failure while pollBatch() is waiting.
  • Ran KvSnapshotBatchScannerTest and the tablet-server KvSnapshotDataDownloaderTest.

API and Format

Does not change any public API, wire protocol, or storage format.

Documentation

Does not introduce new user-facing features or configuration changes.

@shihchengwei-lab

Copy link
Copy Markdown

Could close() return before a download worker has finished creating its local file? In FileDownloadUtils.downloadFile, a worker can register the input stream at line 141 and pause before Files.createDirectories at line 143. If cleanup finishes during that pause, the worker can then recreate the directory and open the target file. Registration at line 145 would close the output stream and throw before any bytes are copied, but the empty file could remain.

I haven’t reproduced this interleaving. The new test blocks inside an overridden transferAllToDirectory, so it covers interruption of the initialization thread. A test using the real download worker, paused between input registration and directory creation, could check that the directory is still absent after both close() and the worker have finished.

@pbanakar

pbanakar commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Hey @shihchengwei-lab Thanks for the review, Addressed your concern by waiting for the real download worker to finish before deleting the snapshot directory, and also Added a regression test covering the stream-registration and cleanup race. PTAL another look when you have some time, Thanks again

@pbanakar
pbanakar force-pushed the fix-kv-snapshot-scanner-download branch from 532182a to 0022952 Compare November 20, 2026 14:47
@pbanakar

Copy link
Copy Markdown
Contributor Author

The CI failure is in FlinkSourceEnumeratorTest.testBatchPartitionFilterOnlyGeneratesMatchingPartitionSplits, failing with UnknownTableOrBucketException while listing offsets. This test seems unrelated to the KV snapshot scanner changes, which only modify download cancellation and cleanup

@shihchengwei-lab

Copy link
Copy Markdown

Thanks for updating this. Suppose worker B has registered its input stream and pauses just before creating the local directory. Worker A then fails, and the initialization thread starts waiting in completeAll(futures).get(). If scanner.close() interrupts that wait, what keeps cleanup from deleting the directory before B resumes and creates it again? Cleanup path · Worker path

I traced this sequence in the code but haven’t reproduced it in a test.

@pbanakar
pbanakar force-pushed the fix-kv-snapshot-scanner-download branch from 0022952 to fa8a949 Compare November 20, 2026 17:26
@pbanakar

Copy link
Copy Markdown
Contributor Author

@shihchengwei-lab Thanks for tracing it further. I changed the failure path to wait uninterruptibly for every download future before cleanup, preserving the interrupt for the caller afterward. Combined with the cancellation checks around directory/file creation, the directory cannot be deleted until the workers have finished. The focused scanner tests pass.

@pbanakar

Copy link
Copy Markdown
Contributor Author

@pbanakar

pbanakar commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @swuferhong Thanks for Re trigerring the CI multiple times, Still it is failing, since we Merged the fix yesterday, Let me rebase the PR

@pbanakar
pbanakar force-pushed the fix-kv-snapshot-scanner-download branch from fa8a949 to cc29404 Compare November 24, 2026 11:17

@polyzos polyzos left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pbanakar for picking this up, the overall approach looks right.

Two asks beyond the inline comments:

  • The description says close() no longer waits for the download. It now does, so please update it.
  • The change to the shared download utility also affects tablet-server KV recovery: on failure it now waits for in-flight downloads before cleanup. Could you mention this in the description, and confirm the server download tests still pass?

() -> {
if (!closed.get()) {
initSnapshotFilesReaderException = e;
readerIsReady.signalAll();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! This signal wakes a waiting pollBatch while snapshotFilesReader is still null and closed is false, so line 168 throws an NPE and the real download error is lost. Could pollBatch call ensureNoException() after the await, and return NO_DATA_AVAILABLE if the reader is still null? A test where the download fails while pollBatch is waiting would cover it.

boolean interrupted = false;
while (!initExecutor.isTerminated()) {
try {
initExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes close() wait with no limit, and ignore interrupts, until every download worker reacts to its stream being closed. How fast that happens depends on the filesystem's stream close(), and close() runs on the Flink fetcher thread during cancellation. Could we bound this wait? Or could the init thread own the final directory delete instead? It already deletes when it sees closed in the catch path. Only the success path at line 256 skips it.

// Cancel background downloading tasks and abort active download streams.
IOUtils.closeQuietly(downloadCloseableRegistry);
if (initFuture != null) {
initFuture.cancel(true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CompletableFuture.cancel doesn't interrupt the running task. shutdownNow() below does. Could we drop this call and the initFuture field?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed shutdownNow()

FSDataInputStream inputStream = fileSystem.open(remoteFilePath);
closeableRegistry.registerCloseable(inputStream);

if (closeableRegistry.isClosed()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registerCloseable already closes the argument and throws if the registry is closed, so this check and the one after the output stream is registered can go. Only the check before newOutputStream adds anything.

assertThat(openStarted.await(10, TimeUnit.SECONDS)).isTrue();
closeThread.start();
assertThat(inputCloseStarted.await(10, TimeUnit.SECONDS)).isTrue();
releaseInputClose.countDown();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because read() returns -1 at once, the worker may finish and pause in its own close before scanner.close() runs, so this passes for either ordering. Could the worker block in open() instead, and could we assert that closeThread is still alive before releasing it?

@pbanakar

Copy link
Copy Markdown
Contributor Author

Thanks @polyzos for the review. I’ve addressed the inline comments and updated the PR description to clarify the bounded close() wait and the shared download utility’s effect on tablet-server KV recovery. The scanner and tablet-server download tests pass.

@pbanakar
pbanakar requested a review from polyzos November 29, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[client] Closing a KV snapshot scanner does not stop its download

3 participants