Skip to content

Cherry pick #109107 to 26.6: Stream .backup metadata with SAX instead of building a DOM tree - #112810

Merged
jkartseva merged 18 commits into
backport/26.6/109107from
cherrypick/26.6/109107
Aug 1, 2026
Merged

jkartseva merged 18 commits into
backport/26.6/109107from
cherrypick/26.6/109107

Conversation

@robot-clickhouse

Copy link
Copy Markdown
Member

Original pull-request #109107

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

jkartseva and others added 17 commits July 1, 2026 23:19
`BackupImpl::readBackupMetadata` parsed the whole `.backup` manifest into an
in-memory `Poco::XML` DOM tree. For large incremental backups the manifest
lists millions of files, so the DOM tree alone costs multiple gigabytes.

Replace it with a streaming `SAXParser`: header elements are applied when
`<contents>` opens, and each `<file>` is folded into the existing maps as its
closing tag is seen, instead of being retained in a tree. The on-disk format
is unchanged. Callback exceptions are captured and rethrown after parsing,
since they must not propagate through the underlying expat callbacks.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Backs up a table (full, incremental on top of the full one, and into an
archive), restores each into a separate table, and checks the restored data
matches. This exercises `readBackupMetadata` across the disk and archive read
paths and the base-backup dedup fields, so a mis-parse of the `.backup`
manifest would fail the RESTORE or restore wrong data.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Move the streaming SAX `ContentHandler` out of the anonymous namespace of
`BackupImpl.cpp` into `Backups/BackupMetadataHandler.{h,cpp}`. The handler was
already fully decoupled from `BackupImpl` (it communicates only through the
`on_header`/`on_file` callbacks), so this is a pure code move that makes it a
named, directly unit-testable unit. No behavior change.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Covers the cases the SQL round-trip test cannot easily construct: header and
per-file field extraction, header-applied-before-any-file ordering, empty
`<contents>`, per-file map reset, capture-and-short-circuit of callback
exceptions (which must not propagate through the expat parser), and a
malformed manifest reported by the parser itself.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The Fast test build is compiled without the `minizip` library, so backing up
to / restoring from a `.zip` archive fails with `minizip library is disabled`
(`SUPPORT_IS_DISABLED`). The archive step differs from the disk path only in
where the `ReadBuffer` for `.backup` comes from - the metadata parsing is
identical - and archive round-trips are already covered by other tests, so drop
it here and keep the full + incremental disk round-trip, which runs everywhere.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109107&sha=0f61bc54919a89fa269b20d968906d5c9fc561d7&name_0=PR&name_1=Fast%20test

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Parses a synthetic manifest with both the old DOM parse and the streaming SAX
handler and reports peak live-allocated bytes (jemalloc stats.allocated). The
file count is BACKUP_METADATA_BENCH_FILES-tunable for a worst-case measurement.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This reverts commit e9496e4.

The benchmark allocates a full DOM tree, which is too heavy to run on every
unit-test invocation. It was a one-off harness for the before/after memory
numbers; it stays reachable at e9496e4 for anyone who wants to reproduce them.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Reject a manifest without <contents> (BACKUP_DAMAGED), gate the SAX callbacks
by exact path so misplaced <file>/<contents> elements are ignored, parse
numbers and booleans strictly, and prefer a captured callback exception over a
secondary parse error. Also drops the now-unused <Common/XMLUtils.h> include.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Follow-up hardening for the SAX metadata reader:
- Range-check the parsed version as UInt64 before narrowing to int, so a value
  that fits in UInt64 but not int (e.g. 4294967298 -> 2) is rejected instead of
  wrapping past the supported-range check.
- Reject a second top-level <contents> (writeBackupMetadata never emits two).
- Keep the first value for duplicate header/file fields (try_emplace), matching
  the old DOM getNodeByPath behavior instead of last-value-wins.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Cover missing/misplaced <contents>, a <file> outside <contents>, a duplicate
top-level <contents>, and first-value-wins for duplicate header/file fields.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Enable FEATURE_NAMESPACE_PREFIXES on the SAX parser so a prefixed element keeps
its prefix in the name (as the old DOM parser did). Without it a declared
<x:contents> arrives as local name "contents" and is accepted as an ordinary
element; with it the handler sees "x:contents", never matches, and the manifest
is rejected as having no <contents>. Confirmed by running against Poco XML.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Tampers a backup manifest to <version>4294967298</version> (fits in UInt64,
narrows to 2 as int) and asserts RESTORE fails with BACKUP_VERSION_NOT_SUPPORTED
instead of wrapping past the supported-version check.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…-streaming

# Conflicts:
#	src/Backups/BackupImpl.cpp
A scalar field containing a child element (e.g. `<version>4294967298<x/>2</version>` or
`<encrypted_by_disk>true<x/>false</encrypted_by_disk>`) collapsed to the last text run in the
SAX path, turning a damaged value into a valid one. Reject a child element opening inside a
header/file scalar leaf, matching the old DOM `innerText` behavior. Adds gtests for both cases.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Stream .backup metadata with SAX instead of building a DOM tree
@robot-clickhouse robot-clickhouse added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Jul 31, 2026
…ypick/26.6/109107

# Conflicts:
#	src/Backups/BackupImpl.cpp
@jkartseva
jkartseva merged commit 52fc485 into backport/26.6/109107 Aug 1, 2026
1 check passed
@jkartseva
jkartseva deleted the cherrypick/26.6/109107 branch August 1, 2026 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants