Skip to content

download: don't leave half-extracted parts when a download is interrupted, fix #1633 - #1675

Merged
Slach merged 8 commits into
masterfrom
fix/download-extract-parts-atomically
Nov 26, 2026
Merged

Slach merged 8 commits into
masterfrom
fix/download-extract-parts-atomically

Conversation

@Slach

@Slach Slach commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Reopened from #1665 by @RyoKusnadi (original fork was deleted); commit cherry-picked onto current master with authorship preserved.

Problem

After an interrupted download of an incremental backup, running download again can finish without an error, but restore --data then fails with code: 226 ... Marks file ... doesn't exist on some parts. Those parts were required parts of the full backup, and they were only half downloaded.

How it happens: download stopped (killed container, cancelled run) while DownloadCompressedStream was still extracting a required part into backup/<required>/shadow/<db>/<table>/<disk>/<part>. The half-extracted directory stays. On the next run, downloadDiffParts (pkg/backup/download.go, the else branch after os.Stat(existsPath)) sees the directory, takes it as complete, and hard-links it into the incremental backup. This also happens without --resume, for example after delete local <incremental> and a new download.

Cause

DownloadCompressedStream in pkg/storage/general.go extracted straight into the final directory and did not remove the files it had already written when it failed or was cancelled.

Fix

DownloadCompressedStream now extracts into a temporary directory inside localPath (.tmp_extract_*) and moves the top-level entries into place only after the whole archive was read. On any error the temporary directory is removed. So an existing part directory is complete. An entry already in localPath (a leftover from an older version or an earlier attempt) is replaced. This is step 1 of the proposal in the issue; it fixes all callers (required parts, own parts, backup-related dirs) at one place.

Scope

  • Not changed: step 2 of the issue (in downloadDiffParts, remove an existing existsPath that is not in the resumable state). Without --resume that state is empty, so it would re-download every part of an already complete required backup. Leftovers written by older versions are still trusted. Happy to do it as a follow-up if you prefer.
  • If the process is killed hard (not an error return), a .tmp_extract_* directory can stay inside the table directory. It is not a part directory, so it is not linked or restored.

Testing

New tests in pkg/storage/download_atomic_test.go (temp dirs, in-memory tar, no ClickHouse):

  • TestDownloadCompressedStreamInterruptedLeavesNoPartDir: stream cut inside data.bin. Before the fix: Should be empty, but was [20261001_1_1_0]. After: passes.
  • TestDownloadCompressedStreamMovesCompletePartIntoPlace: complete archive replaces an old stale part directory. Before the fix: elements differ (stale.bin stayed). After: passes.
  • go test ./pkg/storage/ passes, go vet ./pkg/storage/ and gofmt -l are clean.
  • Not run: integration/testflows tests (need a live ClickHouse) and the full suite.

Risk

One extra rename per top-level entry after each archive, on the same filesystem. No config or format change. Revert the commit to roll back.

Fixes #1633

🤖 Generated with Claude Code

RyoKusnadi and others added 8 commits November 9, 2026 11:45
…1633

Signed-off-by: Ryo Kusnadi <ryokusnadi@gmail.com>
…1633

moveExtractedIntoPlace replaced every top-level entry with RemoveAll+Rename.
With upload_by_part=false splitFilesBySize can split one part across several
archives, and all archives of a table are extracted concurrently into the same
table dir, so the second archive wiped the files of the first one (or failed
with ENOTEMPTY when racing). Publish now renames a whole directory only when the
target is missing and otherwise merges it file by file, RemoveAll only on a
file/dir type conflict. A cancelled context is checked before publishing.

A killed download (SIGKILL, OOM) skips the deferred cleanup and leaves
.tmp_extract_* dirs, which restore copies into access/configs. They are removed
once in downloadTablesMetadata (download and restore_remote --streaming), under
the backup pid lock and before any download goroutine starts, depth-limited
and scoped to the backup being downloaded. The prefix is the shared
storage.ExtractTempDirPrefix constant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Integration coverage for the merge-into-place fix: upload_by_part=false with a
small max_file_size splits wide parts over several archives (asserted via more
archives than parts on MinIO), an incremental --diff-from-remote backup is
downloaded with download_by_part=true and download_concurrency=4 so the diff
flow extracts all required-backup archives into one table dir, then restore
must reproduce count() and sum(cityHash64(*)) and no .tmp_extract_* dirs may
remain under /var/lib/clickhouse/backup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ix/download-extract-parts-atomically

# Conflicts:
#	ChangeLog.md
…nd verify split parts via tar listings

Calibrate rows per part so each part is ~70% of max_file_size (above the
CalculateMaxSize floor), read archive tar headers to assert at least one part
spans several archives, use upload_by_part=true for the --diff-from-remote
increment, hash explicit columns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ouse before 19.15

upload_by_part=false grouped system.parts by disk_name, which doesn't exist
before multiple disks support, create_remote/upload failed with
code: 47, Unknown identifier: disk_name on 1.1.54394. Fall back to max over
all active parts when the column is absent, there is only one disk there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 45810596740

Coverage increased (+0.3%) to 70.831%

Details

  • Coverage increased (+0.3%) from the base build.
  • Patch coverage: 32 uncovered changes across 3 files (62 of 94 lines covered, 65.96%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
pkg/storage/general.go 56 33 58.93%
pkg/backup/download.go 33 26 78.79%
pkg/clickhouse/clickhouse.go 5 3 60.0%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 30214
Covered Lines: 21401
Line Coverage: 70.83%
Coverage Strength: 35955.19 hits per line

💛 - Coveralls

@Slach
Slach merged commit f74066c into master Nov 26, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants