From 4f3f1a5cadab5e99c0193efd496b5d5aac673098 Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Wed, 4 Nov 2026 19:26:23 +0300 Subject: [PATCH 01/13] fix(clickhouse): stop the backup sidecar from publishing failed attempts create_remote keeps its local backup when the upload fails, and with the upstream defaults the sidecar never prunes local backups and resumes every unfinished upload when it starts. A strategy attempt that fails mid-upload therefore lands in the bucket on the next sidecar restart as an archive no Backup refers to, and restore can pick it as the newest one. The resumed upload also holds the API lock, so the strategy Pods racing it get 423 and burn the Job's retries. Turning the resume off keeps a failed attempt out of the bucket, and a negative local retention leaves at most the newest local backup and drops it once its upload succeeds, so leftovers no longer accumulate on the data volume. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- .../apps/clickhouse/templates/clickhouse.yaml | 12 ++++++ .../apps/clickhouse/tests/backup_test.yaml | 39 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/packages/apps/clickhouse/templates/clickhouse.yaml b/packages/apps/clickhouse/templates/clickhouse.yaml index 827b5d579..571469bfe 100644 --- a/packages/apps/clickhouse/templates/clickhouse.yaml +++ b/packages/apps/clickhouse/templates/clickhouse.yaml @@ -222,6 +222,18 @@ spec: key: backup - name: ALLOW_EMPTY_BACKUPS value: "true" + {{- /* + create_remote keeps the local backup when its upload fails, + and the upstream defaults never prune local backups (0) and + resume unfinished uploads at start, which pushes a failed + attempt to the bucket as an archive no Backup refers to and + holds the API lock (423) while it does. -1 keeps only the + newest local backup and removes it once its upload succeeds. + */}} + - name: BACKUPS_TO_KEEP_LOCAL + value: "-1" + - name: API_COMPLETE_RESUMABLE_AFTER_RESTART + value: "false" - name: REMOTE_STORAGE value: s3 {{- /* diff --git a/packages/apps/clickhouse/tests/backup_test.yaml b/packages/apps/clickhouse/tests/backup_test.yaml index 66eda4dc8..90bcd1066 100644 --- a/packages/apps/clickhouse/tests/backup_test.yaml +++ b/packages/apps/clickhouse/tests/backup_test.yaml @@ -292,3 +292,42 @@ tests: - notMatchRegex: path: stringData["backup.sh"] pattern: 'echo DB_LIST=' + + # A strategy attempt that fails mid-upload leaves its local backup behind, + # and with the upstream defaults the sidecar would keep it forever and push + # it to the bucket on its next start as an archive no Backup refers to. The + # sidecar must neither resume uploads at start nor keep local copies once an + # upload succeeds, on either S3 flow. + - it: "sidecar drops local backups after upload and does not resume uploads at start" + template: templates/clickhouse.yaml + documentSelector: + path: kind + value: ClickHouseInstallation + set: + backup: + enabled: true + useSystemBucket: true + asserts: + - equal: + path: spec.templates.podTemplates[0].spec.containers[?(@.name=="clickhouse-backup")].env[?(@.name=="BACKUPS_TO_KEEP_LOCAL")].value + value: "-1" + - equal: + path: spec.templates.podTemplates[0].spec.containers[?(@.name=="clickhouse-backup")].env[?(@.name=="API_COMPLETE_RESUMABLE_AFTER_RESTART")].value + value: "false" + + - it: "legacy flow: sidecar drops local backups after upload and does not resume uploads at start" + template: templates/clickhouse.yaml + documentSelector: + path: kind + value: ClickHouseInstallation + set: + backup: + enabled: true + useSystemBucket: false + asserts: + - equal: + path: spec.templates.podTemplates[0].spec.containers[?(@.name=="clickhouse-backup")].env[?(@.name=="BACKUPS_TO_KEEP_LOCAL")].value + value: "-1" + - equal: + path: spec.templates.podTemplates[0].spec.containers[?(@.name=="clickhouse-backup")].env[?(@.name=="API_COMPLETE_RESUMABLE_AFTER_RESTART")].value + value: "false" From a25db5aeb6469d1fe5df473479108dd2a84e6911 Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Wed, 4 Nov 2026 19:26:23 +0300 Subject: [PATCH 02/13] fix(backups): wait for an idle ClickHouse backup sidecar before starting The sidecar refuses a new operation with 423 while another one is in progress, but the strategy only waited for the API to answer, and went on to start its operation even when it never did. An operation already running in the sidecar therefore failed the attempt within seconds. Poll the action log until nothing is in progress, and fail the Pod instead of proceeding when the sidecar stays busy or unreachable, so the failure names the cause rather than surfacing as a 423 or a connection error from the operation itself. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- hack/altinity-backup-strategy.bats | 149 ++++++++++++++++++ .../templates/strategy-altinity-default.yaml | 13 +- 2 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 hack/altinity-backup-strategy.bats diff --git a/hack/altinity-backup-strategy.bats b/hack/altinity-backup-strategy.bats new file mode 100644 index 000000000..61729bf00 --- /dev/null +++ b/hack/altinity-backup-strategy.bats @@ -0,0 +1,149 @@ +#!/usr/bin/env bats +# Behavioural tests for the shipped ClickHouse (Altinity) strategy script, +# extracted from the rendered cozy-default-altinity Strategy CR and EXECUTED +# against a stubbed clickhouse-backup sidecar API. +# +# The contract pinned here is the start gate: the sidecar serialises its +# operations and answers any new one with HTTP 423 while another is "in +# progress", so a strategy Pod that starts its operation without waiting for +# an idle sidecar burns a Job attempt in seconds. The gate has to fail the Pod +# rather than proceed when the sidecar never becomes idle. +# +# Run from the repo root with make bats-unit-tests. The rendered script is run +# with `bash`, not `sh`: it opens `set -euo pipefail`, which the production +# image's busybox ash supports but the CI runner's /bin/sh (dash) rejects. + +load test_helper + +CHART=packages/system/backupstrategy-controller + +# Render the shipped strategy's in-Pod script into $1 with the controller-render +# placeholders substituted ($2 mode, $3 application name). An empty render, or +# a placeholder left unsubstituted, is an early failure: these tests EXECUTE it. +render_script() { + out=$1; mode=$2; app=$3 + helm template t "$CHART" -n cozy-backup-controller \ + --set backupStorage.bucketNameOverride=test-bucket 2>/dev/null \ + | yq eval 'select(.kind == "Altinity") | .spec.template.spec.containers[0].args[0]' - \ + | sed -e "s#{{ .Mode }}#${mode}#g" \ + -e "s#{{ .Release.Name }}#${app}#g" \ + -e "s#{{ .Release.Namespace }}#tenant-x#g" \ + -e "s#{{ .Backup.ApplicationRef.Name }}#${app}#g" > "$out" + [ -s "$out" ] || return 1 + if grep -q '{{' "$out"; then + echo "rendered script still holds a controller placeholder" >&2 + return 1 + fi +} + +# A curl stub standing in for the sidecar API, put first on PATH next to a +# no-op sleep. State lives in $1: +# unreachable present: every request fails as a refused connection (exit 7) +# busy a number: that many GET /backup/actions answers still list an +# operation "in progress"; while it is positive a POST is +# refused the way the sidecar refuses it (423, curl -f exit 22) +# posted the operation the script started, once it started one +# requests one line per request: method and URL +make_stubs() { + bin=$1; state=$2 + mkdir -p "$bin" "$state" + cat > "$bin/curl" <> "\$state/requests" +if [ -e "\$state/unreachable" ]; then exit 7; fi +busy=\$(cat "\$state/busy" 2>/dev/null || echo 0) +case "\$method \$url" in + "POST "*) + if [ "\$busy" -gt 0 ]; then exit 22; fi + echo "\${url##*/}" > "\$state/posted" + ;; + "GET "*/backup/actions) + echo '{"command":"create_remote clickhouse-myapp-20260101T000000Z","status":"error","start":"2026-01-01 00:00:00","error":"old"}' + if [ "\$busy" -gt 0 ]; then + echo \$((busy - 1)) > "\$state/busy" + echo '{"command":"upload --resumable=1 clickhouse-myapp-20260101T000000Z","status":"in progress","start":"2026-01-01 00:01:00"}' + elif [ -s "\$state/posted" ]; then + echo "{\"command\":\"\$(cat "\$state/posted")\",\"status\":\"success\",\"start\":\"2026-01-01 00:02:00\"}" + fi + ;; + "GET "*/backup/list/remote) + echo '{"name":"clickhouse-myapp-20260101T000000Z","created":"2026-01-01 00:00:00"}' + ;; +esac +exit 0 +STUB + cat > "$bin/sleep" <<'STUB' +#!/bin/sh +exit 0 +STUB + chmod 0755 "$bin/curl" "$bin/sleep" +} + +run_script() { + tmp=$1 + API_USERNAME=u API_PASSWORD=p PATH="$tmp/bin:$PATH" bash "$tmp/s.sh" +} + +@test "an empty render fails the fixture instead of passing as a no-op" { + tmp=$(mktemp -d) + CHART=$tmp/no-such-chart + if render_script "$tmp/s.sh" backup myapp 2>/dev/null; then + echo "FAIL: render_script reported success with no chart" + false + fi +} + +@test "backup on an idle sidecar starts create_remote and waits for its success" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + render_script "$tmp/s.sh" backup myapp + run_script "$tmp" + grep -q '^POST .*/backup/create_remote?name=clickhouse-myapp-' "$tmp/state/requests" +} + +@test "backup waits until the running operation finishes before create_remote" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + echo 3 > "$tmp/state/busy" + render_script "$tmp/s.sh" backup myapp + run_script "$tmp" + grep -q '^POST .*/backup/create_remote?name=clickhouse-myapp-' "$tmp/state/requests" + # All three busy answers were consumed before the operation was started. + [ "$(cat "$tmp/state/busy")" = 0 ] +} + +@test "backup fails without starting create_remote when the sidecar stays busy" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + echo 1000 > "$tmp/state/busy" + render_script "$tmp/s.sh" backup myapp + if run_script "$tmp"; then echo "FAIL: a busy sidecar must fail the Pod"; false; fi + if grep -q '^POST ' "$tmp/state/requests"; then echo "FAIL: no operation may start on a busy sidecar"; false; fi +} + +@test "backup fails without starting create_remote when the sidecar never answers" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + touch "$tmp/state/unreachable" + render_script "$tmp/s.sh" backup myapp + if run_script "$tmp"; then echo "FAIL: an unreachable sidecar must fail the Pod"; false; fi + if grep -q '^POST ' "$tmp/state/requests"; then echo "FAIL: no operation may start on an unreachable sidecar"; false; fi +} + +@test "restore fails without starting restore_remote when the sidecar stays busy" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + echo 1000 > "$tmp/state/busy" + render_script "$tmp/s.sh" restore myapp + if run_script "$tmp"; then echo "FAIL: a busy sidecar must fail the Pod"; false; fi + if grep -q '^POST ' "$tmp/state/requests"; then echo "FAIL: no restore may start on a busy sidecar"; false; fi +} diff --git a/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml b/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml index 3397d6d0d..560b1e597 100644 --- a/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml +++ b/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml @@ -51,10 +51,21 @@ spec: host="chi-clickhouse-{{ printf "{{ .Release.Name }}" }}-clickhouse-0-0.{{ printf "{{ .Release.Namespace }}" }}.svc" api="http://${host}:7171" auth="-u ${API_USERNAME}:${API_PASSWORD}" + # The sidecar answers 423 to a new operation while any other is + # in progress, so starting before it is idle burns the attempt. + idle= for _ in $(seq 1 60); do - if curl -fsS ${auth} --max-time 2 "${api}/backup/status" >/dev/null 2>&1; then break; fi + if actions=$(curl -fsS ${auth} --max-time 5 "${api}/backup/actions") \ + && [ "$(printf '%s\n' "${actions}" | jq -rs '[.[] | select(.status == "in progress")] | length')" = 0 ]; then + idle=1 + break + fi sleep 5 done + if [ -z "${idle}" ]; then + echo "clickhouse-backup sidecar at ${api} did not become idle" >&2 + exit 1 + fi if [ "{{ printf "{{ .Mode }}" }}" = "backup" ]; then prefix="clickhouse-{{ printf "{{ .Release.Name }}" }}-" target="${prefix}$(date -u +%Y%m%dT%H%M%SZ)" From 3a10f956ccefac3990c3319cdf5c6889ee63dcde Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Wed, 4 Nov 2026 19:30:54 +0300 Subject: [PATCH 03/13] test(backups): cover a restore that starts on an idle ClickHouse sidecar The strategy suite only exercised restore against a busy sidecar, so a gate that wrongly blocked restore on an idle one would have stayed green. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- hack/altinity-backup-strategy.bats | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/hack/altinity-backup-strategy.bats b/hack/altinity-backup-strategy.bats index 61729bf00..4703a861c 100644 --- a/hack/altinity-backup-strategy.bats +++ b/hack/altinity-backup-strategy.bats @@ -117,10 +117,17 @@ run_script() { render_script "$tmp/s.sh" backup myapp run_script "$tmp" grep -q '^POST .*/backup/create_remote?name=clickhouse-myapp-' "$tmp/state/requests" - # All three busy answers were consumed before the operation was started. [ "$(cat "$tmp/state/busy")" = 0 ] } +@test "restore on an idle sidecar starts restore_remote of the listed archive" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + render_script "$tmp/s.sh" restore myapp + run_script "$tmp" + grep -q '^POST .*/backup/restore_remote/clickhouse-myapp-20260101T000000Z$' "$tmp/state/requests" +} + @test "backup fails without starting create_remote when the sidecar stays busy" { tmp=$(mktemp -d) make_stubs "$tmp/bin" "$tmp/state" From d0a73302d9a74b0aa70480d17271ee19b5a99abf Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Thu, 5 Nov 2026 21:09:24 +0300 Subject: [PATCH 04/13] fix(backups): fail a ClickHouse backup whose operation the sidecar lost The sidecar keeps its action log only in memory, so a sidecar restart after create_remote was accepted drops the operation's row, and the strategy kept polling for it forever under a Job with no deadline. Until now the resumed upload re-added a row ending in the archive name and ended the loop, but resume is now off so a failed attempt never reaches the bucket, which leaves nothing to end it. The row is logged before the POST is acknowledged, so its absence means the operation is gone and the attempt has to fail. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- hack/altinity-backup-strategy.bats | 22 +++++++++++++++++-- .../templates/strategy-altinity-default.yaml | 7 ++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/hack/altinity-backup-strategy.bats b/hack/altinity-backup-strategy.bats index 4703a861c..7c5fdb476 100644 --- a/hack/altinity-backup-strategy.bats +++ b/hack/altinity-backup-strategy.bats @@ -7,7 +7,9 @@ # operations and answers any new one with HTTP 423 while another is "in # progress", so a strategy Pod that starts its operation without waiting for # an idle sidecar burns a Job attempt in seconds. The gate has to fail the Pod -# rather than proceed when the sidecar never becomes idle. +# rather than proceed when the sidecar never becomes idle. Once started, an +# operation the sidecar no longer lists (its action log lives in memory and a +# restart empties it) has to fail the Pod too, since the Job has no deadline. # # Run from the repo root with make bats-unit-tests. The rendered script is run # with `bash`, not `sh`: it opens `set -euo pipefail`, which the production @@ -43,7 +45,12 @@ render_script() { # operation "in progress"; while it is positive a POST is # refused the way the sidecar refuses it (423, curl -f exit 22) # posted the operation the script started, once it started one +# restarted present: the action log no longer lists the started operation, +# as after the sidecar restarted and lost its in-memory log # requests one line per request: method and URL +# runaway created when the script exceeds 200 requests; that request +# and every later one fails, so a polling loop that never ends +# stops the test instead of hanging it make_stubs() { bin=$1; state=$2 mkdir -p "$bin" "$state" @@ -59,6 +66,7 @@ for a in "\$@"; do prev=\$a done echo "\$method \$url" >> "\$state/requests" +if [ "\$(wc -l < "\$state/requests")" -gt 200 ]; then touch "\$state/runaway"; exit 7; fi if [ -e "\$state/unreachable" ]; then exit 7; fi busy=\$(cat "\$state/busy" 2>/dev/null || echo 0) case "\$method \$url" in @@ -71,7 +79,7 @@ case "\$method \$url" in if [ "\$busy" -gt 0 ]; then echo \$((busy - 1)) > "\$state/busy" echo '{"command":"upload --resumable=1 clickhouse-myapp-20260101T000000Z","status":"in progress","start":"2026-01-01 00:01:00"}' - elif [ -s "\$state/posted" ]; then + elif [ -s "\$state/posted" ] && [ ! -e "\$state/restarted" ]; then echo "{\"command\":\"\$(cat "\$state/posted")\",\"status\":\"success\",\"start\":\"2026-01-01 00:02:00\"}" fi ;; @@ -154,3 +162,13 @@ run_script() { if run_script "$tmp"; then echo "FAIL: a busy sidecar must fail the Pod"; false; fi if grep -q '^POST ' "$tmp/state/requests"; then echo "FAIL: no restore may start on a busy sidecar"; false; fi } + +@test "backup fails instead of polling forever when the sidecar restarts after create_remote" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + touch "$tmp/state/restarted" + render_script "$tmp/s.sh" backup myapp + if run_script "$tmp"; then echo "FAIL: an operation lost from the action log must fail the Pod"; false; fi + grep -q '^POST .*/backup/create_remote?name=clickhouse-myapp-' "$tmp/state/requests" + if [ -e "$tmp/state/runaway" ]; then echo "FAIL: the script kept polling for an operation the sidecar no longer lists"; false; fi +} diff --git a/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml b/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml index 560b1e597..cc7275f30 100644 --- a/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml +++ b/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml @@ -87,6 +87,13 @@ spec: row=$(curl -fsS ${auth} "${api}/backup/actions" \ | jq -rs --arg suffix "${target}" \ '[.[] | select((.command // "") | endswith($suffix))] | sort_by(.start) | last') + # The sidecar logs the operation before acknowledging the POST + # and keeps that log only in memory, so a missing row means it + # restarted and the operation is gone; nothing will resume it. + if [ "${row}" = "null" ]; then + echo "operation ${target} is no longer in the sidecar action log; the sidecar restarted and dropped it" >&2 + exit 1 + fi status=$(echo "${row}" | jq -r '.status // ""') case "${status}" in success) echo "operation succeeded" ; exit 0 ;; From 993b79bb370c20969c45fbcb51c01271f452fac6 Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Fri, 6 Nov 2026 11:23:28 +0300 Subject: [PATCH 05/13] fix(backups): take over a running ClickHouse restore on Job retry restore_remote drops every table it restores before recreating it, so running it twice discards whatever was written after the first run finished. The strategy Job retries a failed Pod up to twice, and with the start gate waiting for an idle sidecar instead of hitting 423, a retry whose predecessor died after its POST waited for that restore to finish and then started a second one. A retry now takes over a restore_remote of the same archive that is still running. A finished row is not taken over: the sidecar never trims its action log, so a success row of the archive may belong to an earlier RestoreJob, and treating it as this one would report a restore that never ran. A retry that arrives after the restore finished therefore still runs it again. Once the operation is accepted, one failed poll no longer ends the attempt under errexit; six in a row do. An operation that ends cancelled now fails the Pod instead of being polled forever. A retried backup still starts a new create_remote, because its archive name changes on every attempt; taking it over needs a name fixed for the whole BackupJob. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- hack/altinity-backup-strategy.bats | 174 ++++++++++++++++-- .../templates/strategy-altinity-default.yaml | 82 +++++++-- 2 files changed, 218 insertions(+), 38 deletions(-) diff --git a/hack/altinity-backup-strategy.bats b/hack/altinity-backup-strategy.bats index 7c5fdb476..2db1f33c6 100644 --- a/hack/altinity-backup-strategy.bats +++ b/hack/altinity-backup-strategy.bats @@ -10,6 +10,9 @@ # rather than proceed when the sidecar never becomes idle. Once started, an # operation the sidecar no longer lists (its action log lives in memory and a # restart empties it) has to fail the Pod too, since the Job has no deadline. +# A Job retry must not run a restore twice: it takes over the restore_remote +# the attempt before it left running, and a few lost polls do not end an +# attempt in the first place. # # Run from the repo root with make bats-unit-tests. The rendered script is run # with `bash`, not `sh`: it opens `set -euo pipefail`, which the production @@ -40,17 +43,31 @@ render_script() { # A curl stub standing in for the sidecar API, put first on PATH next to a # no-op sleep. State lives in $1: -# unreachable present: every request fails as a refused connection (exit 7) -# busy a number: that many GET /backup/actions answers still list an -# operation "in progress"; while it is positive a POST is -# refused the way the sidecar refuses it (423, curl -f exit 22) -# posted the operation the script started, once it started one -# restarted present: the action log no longer lists the started operation, -# as after the sidecar restarted and lost its in-memory log -# requests one line per request: method and URL -# runaway created when the script exceeds 200 requests; that request -# and every later one fails, so a polling loop that never ends -# stops the test instead of hanging it +# unreachable present: every request fails as a refused connection (7) +# down a number: that many next requests fail the same way, as +# before the sidecar has started +# busy a number: that many GET /backup/actions answers still list +# an operation of someone else "in progress" +# op the command line of the sidecar's own operation, written +# by an accepted POST or seeded by a test as the operation +# an earlier attempt of the same Job started +# op_left how many more GET /backup/actions answers list op as +# "in progress" before it ends with op_result +# op_duration op_left given to an operation a POST starts (default 0) +# op_result the status op ends with (default success) +# prior extra action-log rows, verbatim, older than op +# restarted present: the action log no longer lists op, as after the +# sidecar restarted and lost its in-memory log +# fail_on_post a POST that is accepted makes that many following +# GET /backup/actions requests fail (curl exit 7) +# idle created by an answer that lists nothing in progress; a +# POST is refused (423, curl -f exit 22) unless one was seen +# since the last accepted POST, and while anything runs +# accepted one line per POST the sidecar accepted +# requests one line per request: method and URL +# runaway created when the script exceeds 200 requests; that request +# and every later one fails, so a polling loop that never ends +# stops the test instead of hanging it make_stubs() { bin=$1; state=$2 mkdir -p "$bin" "$state" @@ -68,20 +85,49 @@ done echo "\$method \$url" >> "\$state/requests" if [ "\$(wc -l < "\$state/requests")" -gt 200 ]; then touch "\$state/runaway"; exit 7; fi if [ -e "\$state/unreachable" ]; then exit 7; fi -busy=\$(cat "\$state/busy" 2>/dev/null || echo 0) +num() { cat "\$state/\$1" 2>/dev/null || echo 0; } +down=\$(num down) +if [ "\$down" -gt 0 ]; then echo \$((down - 1)) > "\$state/down"; exit 7; fi +running=0 +if [ -s "\$state/op" ] && [ ! -e "\$state/restarted" ] && [ "\$(num op_left)" -gt 0 ]; then running=1; fi case "\$method \$url" in "POST "*) - if [ "\$busy" -gt 0 ]; then exit 22; fi - echo "\${url##*/}" > "\$state/posted" + if [ "\$(num busy)" -gt 0 ] || [ "\$running" = 1 ] || [ ! -e "\$state/idle" ]; then exit 22; fi + case "\$url" in + */create_remote\?name=*) cmd="create_remote \${url##*=}" ;; + */restore_remote/*) cmd="restore_remote \${url##*/}" ;; + *) exit 22 ;; + esac + echo "\$cmd" > "\$state/op" + echo "\$cmd" >> "\$state/accepted" + num op_duration > "\$state/op_left" + num fail_on_post > "\$state/fail_polls" + rm -f "\$state/idle" ;; "GET "*/backup/actions) + fail=\$(num fail_polls) + if [ "\$fail" -gt 0 ]; then echo \$((fail - 1)) > "\$state/fail_polls"; exit 7; fi echo '{"command":"create_remote clickhouse-myapp-20260101T000000Z","status":"error","start":"2026-01-01 00:00:00","error":"old"}' + cat "\$state/prior" 2>/dev/null || true + inprog=0 + busy=\$(num busy) if [ "\$busy" -gt 0 ]; then echo \$((busy - 1)) > "\$state/busy" echo '{"command":"upload --resumable=1 clickhouse-myapp-20260101T000000Z","status":"in progress","start":"2026-01-01 00:01:00"}' - elif [ -s "\$state/posted" ] && [ ! -e "\$state/restarted" ]; then - echo "{\"command\":\"\$(cat "\$state/posted")\",\"status\":\"success\",\"start\":\"2026-01-01 00:02:00\"}" + inprog=1 fi + if [ -s "\$state/op" ] && [ ! -e "\$state/restarted" ]; then + left=\$(num op_left) + if [ "\$left" -gt 0 ]; then + echo \$((left - 1)) > "\$state/op_left" + status="in progress" + inprog=1 + else + status=\$(cat "\$state/op_result" 2>/dev/null || echo success) + fi + echo "{\"command\":\"\$(cat "\$state/op")\",\"status\":\"\$status\",\"start\":\"2026-01-01 00:02:00\",\"error\":\"boom\"}" + fi + if [ "\$inprog" = 0 ]; then touch "\$state/idle"; fi ;; "GET "*/backup/list/remote) echo '{"name":"clickhouse-myapp-20260101T000000Z","created":"2026-01-01 00:00:00"}' @@ -96,6 +142,11 @@ STUB chmod 0755 "$bin/curl" "$bin/sleep" } +# The number of operations the sidecar accepted. +accepted() { + if [ -e "$1/state/accepted" ]; then wc -l < "$1/state/accepted" | tr -d ' '; else echo 0; fi +} + run_script() { tmp=$1 API_USERNAME=u API_PASSWORD=p PATH="$tmp/bin:$PATH" bash "$tmp/s.sh" @@ -124,7 +175,7 @@ run_script() { echo 3 > "$tmp/state/busy" render_script "$tmp/s.sh" backup myapp run_script "$tmp" - grep -q '^POST .*/backup/create_remote?name=clickhouse-myapp-' "$tmp/state/requests" + grep -q '^create_remote clickhouse-myapp-' "$tmp/state/accepted" [ "$(cat "$tmp/state/busy")" = 0 ] } @@ -168,7 +219,94 @@ run_script() { make_stubs "$tmp/bin" "$tmp/state" touch "$tmp/state/restarted" render_script "$tmp/s.sh" backup myapp - if run_script "$tmp"; then echo "FAIL: an operation lost from the action log must fail the Pod"; false; fi + if run_script "$tmp" 2> "$tmp/err"; then echo "FAIL: an operation lost from the action log must fail the Pod"; false; fi + grep -q 'no longer in the sidecar action log' "$tmp/err" grep -q '^POST .*/backup/create_remote?name=clickhouse-myapp-' "$tmp/state/requests" if [ -e "$tmp/state/runaway" ]; then echo "FAIL: the script kept polling for an operation the sidecar no longer lists"; false; fi } + +@test "restore waits for a sidecar that has not started yet" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + echo 10 > "$tmp/state/down" + render_script "$tmp/s.sh" restore myapp + run_script "$tmp" + [ "$(accepted "$tmp")" = 1 ] +} + +@test "restore waits until another operation finishes, then starts its own" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + echo 3 > "$tmp/state/busy" + render_script "$tmp/s.sh" restore myapp + run_script "$tmp" + [ "$(accepted "$tmp")" = 1 ] + grep -qx 'restore_remote clickhouse-myapp-20260101T000000Z' "$tmp/state/accepted" +} + +@test "failed polls after restore_remote, short of six in a row, do not fail the attempt" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + echo 3 > "$tmp/state/op_duration" + echo 5 > "$tmp/state/fail_on_post" + render_script "$tmp/s.sh" restore myapp + run_script "$tmp" + [ "$(accepted "$tmp")" = 1 ] +} + +@test "a retry after an attempt lost its polls joins the running restore instead of starting another" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + echo 20 > "$tmp/state/op_duration" + echo 6 > "$tmp/state/fail_on_post" + render_script "$tmp/s.sh" restore myapp + if run_script "$tmp"; then echo "FAIL: six failed polls in a row must fail the attempt"; false; fi + [ "$(accepted "$tmp")" = 1 ] + echo 0 > "$tmp/state/fail_polls" + run_script "$tmp" + if [ "$(accepted "$tmp")" != 1 ]; then echo "FAIL: the retry started restore_remote a second time"; false; fi + [ "$(cat "$tmp/state/op_left")" = 0 ] +} + +@test "restore does not join an operation running on another archive" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + echo 'restore_remote clickhouse-myapp-20251231T000000Z' > "$tmp/state/op" + echo 3 > "$tmp/state/op_left" + render_script "$tmp/s.sh" restore myapp + run_script "$tmp" + [ "$(accepted "$tmp")" = 1 ] + grep -qx 'restore_remote clickhouse-myapp-20260101T000000Z' "$tmp/state/accepted" +} + +# The sidecar never trims its action log, so a finished restore_remote row of +# the same archive may belong to an earlier RestoreJob; only a running one may +# be taken over. +@test "restore starts restore_remote again when the log holds a finished one of the same archive" { + for status in success error cancel; do + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + echo "{\"command\":\"restore_remote clickhouse-myapp-20260101T000000Z\",\"status\":\"$status\",\"start\":\"2026-01-01 00:01:30\"}" > "$tmp/state/prior" + render_script "$tmp/s.sh" restore myapp + run_script "$tmp" + if [ "$(accepted "$tmp")" != 1 ]; then echo "FAIL: a $status row of an earlier restore must not stand in for this one"; false; fi + done +} + +@test "a restore that ends in error fails the Pod" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + echo error > "$tmp/state/op_result" + render_script "$tmp/s.sh" restore myapp + if run_script "$tmp"; then echo "FAIL: a failed restore must fail the Pod"; false; fi + [ "$(accepted "$tmp")" = 1 ] +} + +@test "a cancelled backup fails the Pod instead of polling forever" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + echo cancel > "$tmp/state/op_result" + render_script "$tmp/s.sh" backup myapp + if run_script "$tmp"; then echo "FAIL: a cancelled operation must fail the Pod"; false; fi + if [ -e "$tmp/state/runaway" ]; then echo "FAIL: the script kept polling a cancelled operation"; false; fi +} diff --git a/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml b/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml index cc7275f30..bbae43349 100644 --- a/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml +++ b/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml @@ -51,42 +51,83 @@ spec: host="chi-clickhouse-{{ printf "{{ .Release.Name }}" }}-clickhouse-0-0.{{ printf "{{ .Release.Namespace }}" }}.svc" api="http://${host}:7171" auth="-u ${API_USERNAME}:${API_PASSWORD}" + # Every request but the POST is retried on failure, every 5s, + # up to $2 times: once the operation is accepted, a dropped poll + # must not fail the attempt, since the Job retry it causes cannot + # tell a lost poll from a lost operation. + api_get() { + n=0 + while true; do + if out=$(curl -fsS ${auth} --max-time 10 "${api}$1"); then + printf '%s\n' "${out}" + return 0 + fi + n=$((n + 1)) + if [ "${n}" -ge "$2" ]; then + echo "GET ${api}$1 failed ${n} times in a row" >&2 + return 1 + fi + sleep 5 + done + } # The sidecar answers 423 to a new operation while any other is # in progress, so starting before it is idle burns the attempt. - idle= - for _ in $(seq 1 60); do - if actions=$(curl -fsS ${auth} --max-time 5 "${api}/backup/actions") \ - && [ "$(printf '%s\n' "${actions}" | jq -rs '[.[] | select(.status == "in progress")] | length')" = 0 ]; then - idle=1 - break - fi - sleep 5 - done - if [ -z "${idle}" ]; then + # The exception is the operation $1 itself already running: an + # earlier attempt of this Job started it and died, and the retry + # takes it over rather than starting it twice. + wait_turn() { + for _ in $(seq 1 60); do + if actions=$(curl -fsS ${auth} --max-time 10 "${api}/backup/actions"); then + turn=$(printf '%s\n' "${actions}" | jq -rs --arg cmd "$1" ' + if any(.[]; .status == "in progress" and .command == $cmd) then "join" + elif any(.[]; .status == "in progress") then "busy" + else "idle" end') || turn= + case "${turn}" in join|idle) return 0 ;; esac + fi + sleep 5 + done echo "clickhouse-backup sidecar at ${api} did not become idle" >&2 exit 1 - fi + } if [ "{{ printf "{{ .Mode }}" }}" = "backup" ]; then prefix="clickhouse-{{ printf "{{ .Release.Name }}" }}-" + # The name is new on every attempt, so a retry never takes over + # the create_remote of the attempt it replaces; that needs a + # name fixed for the whole BackupJob. target="${prefix}$(date -u +%Y%m%dT%H%M%SZ)" - echo "create_remote ${target}" + cmd="create_remote ${target}" + wait_turn "${cmd}" + echo "${cmd}" curl -fsS ${auth} -X POST "${api}/backup/create_remote?name=${target}" >/dev/null else prefix="clickhouse-{{ printf "{{ .Backup.ApplicationRef.Name }}" }}-" - target=$(curl -fsS ${auth} "${api}/backup/list/remote" \ + listing=$(api_get /backup/list/remote 60) + target=$(printf '%s\n' "${listing}" \ | jq -rs --arg prefix "${prefix}" \ '[.[] | select((.name // "") | startswith($prefix))] | sort_by(.created // "") | last.name // ""') if [ -z "${target}" ] || [ "${target}" = "null" ]; then echo "no remote backup found" >&2 exit 1 fi - echo "restore_remote ${target}" - curl -fsS ${auth} -X POST "${api}/backup/restore_remote/${target}" >/dev/null + # restore_remote drops every table it restores before + # recreating it, so a second run discards whatever was written + # after the first one finished. Only a running one is taken + # over: the action log is never trimmed, and a finished row of + # this archive may be left by an earlier RestoreJob. + cmd="restore_remote ${target}" + wait_turn "${cmd}" + if [ "${turn}" = "join" ]; then + echo "${cmd} is already running; waiting for it" + else + echo "${cmd}" + curl -fsS ${auth} -X POST "${api}/backup/restore_remote/${target}" >/dev/null + fi fi while true; do - row=$(curl -fsS ${auth} "${api}/backup/actions" \ - | jq -rs --arg suffix "${target}" \ - '[.[] | select((.command // "") | endswith($suffix))] | sort_by(.start) | last') + actions=$(api_get /backup/actions 6) + row=$(printf '%s\n' "${actions}" \ + | jq -rs --arg cmd "${cmd}" \ + '[.[] | select(.command == $cmd)] | sort_by(.start) | last') # The sidecar logs the operation before acknowledging the POST # and keeps that log only in memory, so a missing row means it # restarted and the operation is gone; nothing will resume it. @@ -97,9 +138,10 @@ spec: status=$(echo "${row}" | jq -r '.status // ""') case "${status}" in success) echo "operation succeeded" ; exit 0 ;; - error) + "in progress") ;; + *) err=$(echo "${row}" | jq -r '.error // "unknown"') - echo "operation failed: ${err}" >&2 + echo "operation ended with status ${status}: ${err}" >&2 exit 1 ;; esac From ae1e1830ce697bc454866a874ccc92b91e416e03 Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Fri, 6 Nov 2026 11:29:52 +0300 Subject: [PATCH 06/13] test(backups): cover a ClickHouse restore against an unreachable sidecar The restore path resolves its archive from list/remote before the start gate runs, so it carries its own wait for a sidecar that is not up, and only the backup path had a test for a sidecar that never answers. The retry comment also miscounted: the limit is attempts, not retries. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- hack/altinity-backup-strategy.bats | 11 +++++++++++ .../templates/strategy-altinity-default.yaml | 12 ++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/hack/altinity-backup-strategy.bats b/hack/altinity-backup-strategy.bats index 2db1f33c6..9b31158d9 100644 --- a/hack/altinity-backup-strategy.bats +++ b/hack/altinity-backup-strategy.bats @@ -205,6 +205,17 @@ run_script() { if grep -q '^POST ' "$tmp/state/requests"; then echo "FAIL: no operation may start on an unreachable sidecar"; false; fi } +@test "restore fails without starting restore_remote when the sidecar never answers" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + touch "$tmp/state/unreachable" + render_script "$tmp/s.sh" restore myapp + if run_script "$tmp" 2> "$tmp/err"; then echo "FAIL: an unreachable sidecar must fail the Pod"; false; fi + if grep -q '^POST ' "$tmp/state/requests"; then echo "FAIL: no restore may start on an unreachable sidecar"; false; fi + grep -q 'list/remote failed 60 times in a row' "$tmp/err" + [ "$(grep -c '^GET .*/backup/list/remote$' "$tmp/state/requests")" = 60 ] +} + @test "restore fails without starting restore_remote when the sidecar stays busy" { tmp=$(mktemp -d) make_stubs "$tmp/bin" "$tmp/state" diff --git a/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml b/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml index bbae43349..a176044d6 100644 --- a/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml +++ b/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml @@ -52,9 +52,9 @@ spec: api="http://${host}:7171" auth="-u ${API_USERNAME}:${API_PASSWORD}" # Every request but the POST is retried on failure, every 5s, - # up to $2 times: once the operation is accepted, a dropped poll - # must not fail the attempt, since the Job retry it causes cannot - # tell a lost poll from a lost operation. + # for $2 attempts in all: once the operation is accepted, a + # dropped poll must not fail the attempt, since the Job retry it + # causes cannot tell a lost poll from a lost operation. api_get() { n=0 while true; do @@ -72,9 +72,9 @@ spec: } # The sidecar answers 423 to a new operation while any other is # in progress, so starting before it is idle burns the attempt. - # The exception is the operation $1 itself already running: an - # earlier attempt of this Job started it and died, and the retry - # takes it over rather than starting it twice. + # The exception is the operation $1 itself already running, as + # left by an earlier attempt of this Job that died: it is taken + # over rather than started twice. wait_turn() { for _ in $(seq 1 60); do if actions=$(curl -fsS ${auth} --max-time 10 "${api}/backup/actions"); then From d530ca8cf534eb4387934eccae6a94402ddfe01b Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Fri, 6 Nov 2026 17:51:39 +0300 Subject: [PATCH 07/13] fix(backups): keep a ClickHouse archive an earlier attempt finished A backup Pod that loses sight of its upload, by dying or by losing six polls in a row, is retried while the sidecar finishes that upload. The retry found it in the action log as a success, took the sidecar for idle and asked for create_remote with resume again. clickhouse-backup then rebuilds the local backup and uploads it once more over the archive already in the bucket, which is left broken if that second upload dies halfway. The archive name belongs to one BackupJob, so a success row of exactly this upload can only be an earlier attempt's, and the retry now takes it as its result. An error or cancel row still leads to a resumed upload, and a restore still runs again, since a restore is not named per RestoreJob. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- hack/altinity-backup-strategy.bats | 44 ++++++++++++++++--- .../templates/strategy-altinity-default.yaml | 9 +++- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/hack/altinity-backup-strategy.bats b/hack/altinity-backup-strategy.bats index 2597338a2..46cf62d9a 100644 --- a/hack/altinity-backup-strategy.bats +++ b/hack/altinity-backup-strategy.bats @@ -483,15 +483,49 @@ run_script() { grep -qx 'create_remote --resume clickhouse-myapp-20260304T050607Z' "$tmp/state/accepted" } -# The action log is never trimmed, so a finished row of the same archive may -# be an earlier run's and is not taken for this attempt's result. -@test "backup starts create_remote again when the log holds a finished one of the same archive" { - for status in success error cancel; do +# The archive is named per BackupJob, so a success row of exactly this +# upload is an earlier attempt of the same BackupJob that finished while it +# could not see it. Uploading again with resume would re-upload over the +# finished archive and break it if that second upload dies halfway. +@test "a retried backup takes the upload of its archive an earlier attempt finished as its result" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + echo '{"command":"create_remote --resume clickhouse-myapp-20260304T050607Z","status":"success","start":"2026-01-01 00:01:30"}' > "$tmp/state/prior" + render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z + run_script "$tmp" + if grep -q '^POST ' "$tmp/state/requests"; then + echo "FAIL: uploaded again over a finished archive: $(cat "$tmp/state/requests")" + false + fi +} + +@test "a retried backup resumes an upload of its archive that ended in error or was cancelled" { + for status in error cancel; do tmp=$(mktemp -d) make_stubs "$tmp/bin" "$tmp/state" echo "{\"command\":\"create_remote --resume clickhouse-myapp-20260304T050607Z\",\"status\":\"$status\",\"start\":\"2026-01-01 00:01:30\"}" > "$tmp/state/prior" render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z run_script "$tmp" - if [ "$(accepted "$tmp")" != 1 ]; then echo "FAIL: a $status row of an earlier upload must not stand in for this one"; false; fi + if [ "$(accepted "$tmp")" != 1 ]; then echo "FAIL: a $status row must lead to a resumed upload"; false; fi done } + +@test "a retried backup resumes when its latest upload failed after an earlier one succeeded" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + printf '%s\n' \ + '{"command":"create_remote --resume clickhouse-myapp-20260304T050607Z","status":"success","start":"2026-01-01 00:01:00"}' \ + '{"command":"create_remote --resume clickhouse-myapp-20260304T050607Z","status":"error","start":"2026-01-01 00:01:30"}' > "$tmp/state/prior" + render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z + run_script "$tmp" + [ "$(accepted "$tmp")" = 1 ] +} + +@test "backup does not take another archive's finished upload as its own" { + tmp=$(mktemp -d) + make_stubs "$tmp/bin" "$tmp/state" + echo '{"command":"create_remote --resume clickhouse-myapp-20260304T040000Z","status":"success","start":"2026-01-01 00:01:30"}' > "$tmp/state/prior" + render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z + run_script "$tmp" + [ "$(accepted "$tmp")" = 1 ] +} diff --git a/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml b/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml index 18174d61a..331a89be8 100644 --- a/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml +++ b/packages/system/backupstrategy-controller/templates/strategy-altinity-default.yaml @@ -104,11 +104,18 @@ spec: # so a local or partial remote backup an earlier attempt left is # resumed rather than refused as existing, whatever # use_resumable_state the sidecar runs with. The sidecar logs - # the request as "create_remote --resume ". + # the request as "create_remote --resume ". The name is + # this BackupJob's alone, so an upload of it that last ended in + # success is an earlier attempt's finished result: resuming + # over it would upload the archive again and leave it broken if + # that upload died halfway. cmd="create_remote --resume ${target}" wait_turn "${cmd}" if [ "${turn}" = "join" ]; then echo "${cmd} is already running; waiting for it" + elif printf '%s\n' "${actions}" | jq -es --arg cmd "${cmd}" \ + '[.[] | select(.command == $cmd)] | sort_by(.start) | last | .status == "success"' >/dev/null; then + echo "${cmd} already succeeded in an earlier attempt" else echo "${cmd}" curl -fsS ${auth} -X POST "${api}/backup/create_remote?name=${target}&resume=true" >/dev/null From 1291e616c700def517907d072289e9970b8e694c Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Mon, 9 Nov 2026 16:39:02 +0300 Subject: [PATCH 08/13] fix(clickhouse): give every backup run an archive of its own An archive was named from the application and the BackupJob's start time, which has one-second resolution. Two runs of one application started in the same second, as two Plans on the same schedule are, wrote the same archive. The strategy takes a finished upload of its archive as an earlier attempt of its own run, so the later run could succeed on the other run's upload and record an archive missing any table created since. The BackupJob's UID now ends the name. It goes after the timestamp so names keep sorting chronologically, and the restore of a Backup that records no archive, which accepts only a bare timestamp after the release prefix, never picks one of these. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- hack/altinity-backup-strategy.bats | 1 + .../altinitystrategy_controller.go | 14 +++--- .../altinitystrategy_controller_test.go | 44 +++++++++++++++---- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/hack/altinity-backup-strategy.bats b/hack/altinity-backup-strategy.bats index 46cf62d9a..d26b22ee4 100644 --- a/hack/altinity-backup-strategy.bats +++ b/hack/altinity-backup-strategy.bats @@ -393,6 +393,7 @@ run_script() { remote_rows clickhouse-myapp-20260304T040000Z clickhouse-myapp-20260304T050607Z \ "clickhouse-myapp-20260304T050700Z|broken (can't stat metadata.json)" \ clickhouse-myapp-20260304T050710Z-copy \ + clickhouse-myapp-20260304T050720Z-0f3c9a1e-5b7d-4c2a-9e8f-1a2b3c4d5e6f \ clickhouse-myapp-20260304T060000Z > "$tmp/state/remote" out=$(run_script "$tmp" 2>&1) grep -qxF 'POST http://chi-clickhouse-myapp-clickhouse-0-0.tenant-x.svc:7171/backup/restore_remote/clickhouse-myapp-20260304T050607Z' "$tmp/state/requests" diff --git a/internal/backupcontroller/altinitystrategy_controller.go b/internal/backupcontroller/altinitystrategy_controller.go index a0868ec38..1aec3a585 100644 --- a/internal/backupcontroller/altinitystrategy_controller.go +++ b/internal/backupcontroller/altinitystrategy_controller.go @@ -117,12 +117,16 @@ func backupParameters(b *backupsv1alpha1.Backup) map[string]string { } // altinityArchiveName mints the archive name for one BackupJob from its -// persisted StartedAt, so every reconcile and every Pod retry of the Job -// writes the same archive. It keeps the clickhouse-- -// shape, because a Backup that records no archive is restored by the -// timestamp in that shape. +// persisted StartedAt and its UID, so every reconcile and every Pod retry of +// the Job writes the same archive and no other run writes it. StartedAt has +// one-second resolution, and two Plans on one schedule start their runs in +// the same second; the strategy takes a finished upload of its archive as an +// earlier attempt of its own run, so a shared name would let one run report +// the other's archive. The UID goes after the timestamp so names still sort +// chronologically, and the restore of a Backup that records no archive, which +// accepts only clickhouse--, never picks one of these. func altinityArchiveName(j *backupsv1alpha1.BackupJob) string { - return fmt.Sprintf("clickhouse-%s-%s", j.Spec.ApplicationRef.Name, j.Status.StartedAt.UTC().Format(altinityArchiveTimeLayout)) + return fmt.Sprintf("clickhouse-%s-%s-%s", j.Spec.ApplicationRef.Name, j.Status.StartedAt.UTC().Format(altinityArchiveTimeLayout), j.UID) } // altinityJobCarriesArchive reports whether the Job's Pod was rendered with diff --git a/internal/backupcontroller/altinitystrategy_controller_test.go b/internal/backupcontroller/altinitystrategy_controller_test.go index e5f1e1695..18cead060 100644 --- a/internal/backupcontroller/altinitystrategy_controller_test.go +++ b/internal/backupcontroller/altinitystrategy_controller_test.go @@ -14,6 +14,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/types" dynamicfake "k8s.io/client-go/dynamic/fake" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/record" @@ -730,14 +731,41 @@ func newAltinityArchiveStrategy(name string) *strategyv1alpha1.Altinity { func TestAltinityArchiveName_FromStartedAtInUTC(t *testing.T) { startedAt := metav1.NewTime(time.Date(2026, 3, 4, 8, 6, 7, 890000000, time.FixedZone("UTC+3", 3*3600))) j := &backupsv1alpha1.BackupJob{ - Spec: backupsv1alpha1.BackupJobSpec{ApplicationRef: newAltinityClickHouseRef("ch-test")}, - Status: backupsv1alpha1.BackupJobStatus{StartedAt: &startedAt}, + ObjectMeta: metav1.ObjectMeta{UID: "0f3c9a1e-5b7d-4c2a-9e8f-1a2b3c4d5e6f"}, + Spec: backupsv1alpha1.BackupJobSpec{ApplicationRef: newAltinityClickHouseRef("ch-test")}, + Status: backupsv1alpha1.BackupJobStatus{StartedAt: &startedAt}, } - if got, want := altinityArchiveName(j), "clickhouse-ch-test-20260304T050607Z"; got != want { + if got, want := altinityArchiveName(j), "clickhouse-ch-test-20260304T050607Z-0f3c9a1e-5b7d-4c2a-9e8f-1a2b3c4d5e6f"; got != want { t.Errorf("altinityArchiveName = %q, want %q", got, want) } } +// TestAltinityArchiveName_DistinctForRunsStartedInTheSameSecond pins that two +// BackupJobs of one application started within the same second, as two Plans +// on the same schedule are, write different archives. With a shared name, the +// strategy takes the other run's finished upload for an earlier attempt of its +// own and records an archive missing what changed since that upload. +func TestAltinityArchiveName_DistinctForRunsStartedInTheSameSecond(t *testing.T) { + startedAt := metav1.NewTime(time.Date(2026, 3, 4, 5, 6, 7, 0, time.UTC)) + run := func(name, uid string) *backupsv1alpha1.BackupJob { + return &backupsv1alpha1.BackupJob{ + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "tenant-test", UID: types.UID(uid)}, + Spec: backupsv1alpha1.BackupJobSpec{ApplicationRef: newAltinityClickHouseRef("ch-test")}, + Status: backupsv1alpha1.BackupJobStatus{StartedAt: &startedAt}, + } + } + daily := altinityArchiveName(run("daily-1", "11111111-1111-1111-1111-111111111111")) + weekly := altinityArchiveName(run("weekly-1", "22222222-2222-2222-2222-222222222222")) + if daily == weekly { + t.Fatalf("two runs started in the same second share archive %q", daily) + } + for _, name := range []string{daily, weekly} { + if !strings.HasPrefix(name, "clickhouse-ch-test-20260304T050607Z-") { + t.Errorf("archive %q does not start with the application and start time, so names stop sorting chronologically", name) + } + } +} + // TestReconcileAltinity_RecordsTheArchiveTheJobWasGiven drives one BackupJob // through Job creation and completion in separate reconciles. The archive // name has to come out of the persisted StartedAt both times: the Pod is @@ -748,7 +776,7 @@ func TestReconcileAltinity_RecordsTheArchiveTheJobWasGiven(t *testing.T) { strategy := newAltinityArchiveStrategy("clickhouse-strategy") startedAt := metav1.NewTime(time.Date(2026, 3, 4, 5, 6, 7, 0, time.UTC)) backupJob := &backupsv1alpha1.BackupJob{ - ObjectMeta: metav1.ObjectMeta{Name: "test-bj", Namespace: "tenant-test"}, + ObjectMeta: metav1.ObjectMeta{Name: "test-bj", Namespace: "tenant-test", UID: "test-bj-uid"}, Spec: backupsv1alpha1.BackupJobSpec{ ApplicationRef: newAltinityClickHouseRef("ch-test"), BackupClassName: "clickhouse-backup", @@ -765,7 +793,7 @@ func TestReconcileAltinity_RecordsTheArchiveTheJobWasGiven(t *testing.T) { } r, _, _ := newAltinityTestEnv(t, app, clientfake.NewClientBuilder().WithObjects(backupJob, strategy)) ctx := context.Background() - const want = "clickhouse-ch-test-20260304T050607Z" + const want = "clickhouse-ch-test-20260304T050607Z-test-bj-uid" if _, err := r.reconcileAltinity(ctx, backupJob, resolved); err != nil { t.Fatalf("reconcileAltinity() creating the Job: %v", err) @@ -869,13 +897,13 @@ func TestReconcileAltinity_RecordsNoArchiveTheJobWasNotGiven(t *testing.T) { // A Backup recording this run's own archive is the one an earlier reconcile // created before its status write was lost, and is adopted. func TestReconcileAltinity_BackupNameAlreadyTaken(t *testing.T) { - const thisRun = "clickhouse-ch-test-20260305T050607Z" + const thisRun = "clickhouse-ch-test-20260305T050607Z-test-bj-uid" cases := []struct { name string existing string // archive the existing Backup records; empty for none wantSucceeded bool }{ - {name: "earlier run's Backup records another archive", existing: "clickhouse-ch-test-20260304T050607Z"}, + {name: "earlier run's Backup records another archive", existing: "clickhouse-ch-test-20260304T050607Z-earlier-bj-uid"}, {name: "earlier run's Backup records no archive", existing: ""}, {name: "this run's Backup from a reconcile whose status write was lost", existing: thisRun, wantSucceeded: true}, } @@ -885,7 +913,7 @@ func TestReconcileAltinity_BackupNameAlreadyTaken(t *testing.T) { strategy := newAltinityArchiveStrategy("clickhouse-strategy") startedAt := metav1.NewTime(time.Date(2026, 3, 5, 5, 6, 7, 0, time.UTC)) backupJob := &backupsv1alpha1.BackupJob{ - ObjectMeta: metav1.ObjectMeta{Name: "test-bj", Namespace: "tenant-test"}, + ObjectMeta: metav1.ObjectMeta{Name: "test-bj", Namespace: "tenant-test", UID: "test-bj-uid"}, Spec: backupsv1alpha1.BackupJobSpec{ ApplicationRef: newAltinityClickHouseRef("ch-test"), BackupClassName: "clickhouse-backup", From 0f336e99c51999117712d6a67e074dcbaaec6bad Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Mon, 9 Nov 2026 16:39:33 +0300 Subject: [PATCH 09/13] test(backups): drop the comment restating what accepted() returns Assisted-by: LLM Signed-off-by: Alexey Artamonov --- hack/altinity-backup-strategy.bats | 1 - 1 file changed, 1 deletion(-) diff --git a/hack/altinity-backup-strategy.bats b/hack/altinity-backup-strategy.bats index d26b22ee4..97ec869cc 100644 --- a/hack/altinity-backup-strategy.bats +++ b/hack/altinity-backup-strategy.bats @@ -175,7 +175,6 @@ remote_rows() { done } -# The number of operations the sidecar accepted. accepted() { if [ -e "$1/state/accepted" ]; then wc -l < "$1/state/accepted" | tr -d ' '; else echo 0; fi } From fc15ed7171322c1e67bd008e2a59a3102740f107 Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Mon, 9 Nov 2026 16:42:13 +0300 Subject: [PATCH 10/13] fix(clickhouse): keep the archive name of a backup running across the upgrade A backup Job created before archive names carried the BackupJob's UID writes its archive under the name without it, and the Job is not re-rendered when the controller is upgraded. Matching only the new name left that Backup with no archive recorded, so its restore fell back to guessing by timestamp. The name without the UID is matched second, because it is a prefix of the one with it. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- .../altinitystrategy_controller.go | 16 ++++- .../altinitystrategy_controller_test.go | 59 +++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/internal/backupcontroller/altinitystrategy_controller.go b/internal/backupcontroller/altinitystrategy_controller.go index 1aec3a585..cd03b1d46 100644 --- a/internal/backupcontroller/altinitystrategy_controller.go +++ b/internal/backupcontroller/altinitystrategy_controller.go @@ -126,7 +126,14 @@ func backupParameters(b *backupsv1alpha1.Backup) map[string]string { // chronologically, and the restore of a Backup that records no archive, which // accepts only clickhouse--, never picks one of these. func altinityArchiveName(j *backupsv1alpha1.BackupJob) string { - return fmt.Sprintf("clickhouse-%s-%s-%s", j.Spec.ApplicationRef.Name, j.Status.StartedAt.UTC().Format(altinityArchiveTimeLayout), j.UID) + return altinityArchiveNameBeforeUIDs(j) + "-" + string(j.UID) +} + +// altinityArchiveNameBeforeUIDs is the name a controller that did not append +// the UID gave the same BackupJob, which a Job it created before an upgrade +// still writes. +func altinityArchiveNameBeforeUIDs(j *backupsv1alpha1.BackupJob) string { + return fmt.Sprintf("clickhouse-%s-%s", j.Spec.ApplicationRef.Name, j.Status.StartedAt.UTC().Format(altinityArchiveTimeLayout)) } // altinityJobCarriesArchive reports whether the Job's Pod was rendered with @@ -286,8 +293,13 @@ func (r *BackupJobReconciler) reconcileAltinity(ctx context.Context, j *backupsv if j.Status.BackupRef != nil { return ctrl.Result{}, nil } + // The name without the UID is a prefix of the one with it, so it is + // tried only after the full name is not found. if !altinityJobCarriesArchive(batchJob, archive) { - archive = "" + archive = altinityArchiveNameBeforeUIDs(j) + if !altinityJobCarriesArchive(batchJob, archive) { + archive = "" + } } artifact, err := r.createAltinityBackupArtifact(ctx, j, resolved, archive) if err != nil { diff --git a/internal/backupcontroller/altinitystrategy_controller_test.go b/internal/backupcontroller/altinitystrategy_controller_test.go index 18cead060..08c6e5466 100644 --- a/internal/backupcontroller/altinitystrategy_controller_test.go +++ b/internal/backupcontroller/altinitystrategy_controller_test.go @@ -829,6 +829,65 @@ func TestReconcileAltinity_RecordsTheArchiveTheJobWasGiven(t *testing.T) { } } +// TestReconcileAltinity_RecordsTheArchiveOfAJobNamedBeforeUIDs covers a backup +// Job created by a controller that named archives without the BackupJob's UID +// and still running when the controller is upgraded: the Job is not +// re-rendered, so its archive carries the old name, and the Backup has to +// record that name rather than none. +func TestReconcileAltinity_RecordsTheArchiveOfAJobNamedBeforeUIDs(t *testing.T) { + app := newAltinityClickHouseApp("ch-test", "tenant-test") + strategy := newAltinityArchiveStrategy("clickhouse-strategy") + startedAt := metav1.NewTime(time.Date(2026, 3, 4, 5, 6, 7, 0, time.UTC)) + backupJob := &backupsv1alpha1.BackupJob{ + ObjectMeta: metav1.ObjectMeta{Name: "test-bj", Namespace: "tenant-test", UID: "test-bj-uid"}, + Spec: backupsv1alpha1.BackupJobSpec{ + ApplicationRef: newAltinityClickHouseRef("ch-test"), + BackupClassName: "clickhouse-backup", + }, + Status: backupsv1alpha1.BackupJobStatus{StartedAt: &startedAt, Phase: backupsv1alpha1.BackupJobPhaseRunning}, + } + resolved := &ResolvedBackupConfig{ + StrategyRef: corev1.TypedLocalObjectReference{ + APIGroup: new(strategyv1alpha1.GroupVersion.Group), + Kind: strategyv1alpha1.AltinityStrategyKind, + Name: "clickhouse-strategy", + }, + } + r, _, _ := newAltinityTestEnv(t, app, clientfake.NewClientBuilder().WithObjects(backupJob, strategy)) + ctx := context.Background() + const legacy = "clickhouse-ch-test-20260304T050607Z" + + if _, err := r.reconcileAltinity(ctx, backupJob, resolved); err != nil { + t.Fatalf("reconcileAltinity() creating the Job: %v", err) + } + job := &batchv1.Job{} + if err := r.Get(ctx, client.ObjectKey{Namespace: "tenant-test", Name: jobNameForBackupJob(backupJob)}, job); err != nil { + t.Fatalf("get backup Job: %v", err) + } + job.Spec.Template.Spec.Containers[0].Args[1] = "--archive=" + legacy + if err := r.Update(ctx, job); err != nil { + t.Fatalf("give the backup Job the name without the UID: %v", err) + } + job.Status.Conditions = []batchv1.JobCondition{{Type: batchv1.JobComplete, Status: corev1.ConditionTrue}} + if err := r.Status().Update(ctx, job); err != nil { + t.Fatalf("mark backup Job complete: %v", err) + } + if err := r.Get(ctx, client.ObjectKeyFromObject(backupJob), backupJob); err != nil { + t.Fatalf("refresh BackupJob: %v", err) + } + if _, err := r.reconcileAltinity(ctx, backupJob, resolved); err != nil { + t.Fatalf("reconcileAltinity() on the completed Job: %v", err) + } + + created := &backupsv1alpha1.Backup{} + if err := r.Get(ctx, client.ObjectKey{Namespace: "tenant-test", Name: backupJob.Name}, created); err != nil { + t.Fatalf("get Backup: %v", err) + } + if got := created.Spec.DriverMetadata[altinityArchiveMetaKey]; got != legacy { + t.Errorf("Backup records archive %q, want %q", got, legacy) + } +} + // TestReconcileAltinity_RecordsNoArchiveTheJobWasNotGiven covers a Job whose // Pod never carried the driver's archive name: one created before the // upgrade that introduced the names, or rendered from a strategy that names From 2046649b4cbcf064df9478698b348f18f05517eb Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Mon, 9 Nov 2026 17:53:43 +0300 Subject: [PATCH 11/13] fix(clickhouse): match a backup Job's archive by its whole name The archive a backup Job carries was found as a substring of its Pod spec. The name without the BackupJob's UID starts the name with it, and a strategy may append to either, so a Job that wrote a longer archive could get the shorter name recorded on its Backup, and a restore of that Backup would read another archive. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- .../altinitystrategy_controller.go | 11 ++++--- .../altinitystrategy_controller_test.go | 33 +++++++++++++++---- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/internal/backupcontroller/altinitystrategy_controller.go b/internal/backupcontroller/altinitystrategy_controller.go index cd03b1d46..c29162474 100644 --- a/internal/backupcontroller/altinitystrategy_controller.go +++ b/internal/backupcontroller/altinitystrategy_controller.go @@ -2,11 +2,11 @@ package backupcontroller import ( - "bytes" "context" "encoding/json" "fmt" "maps" + "regexp" "strings" "time" @@ -140,13 +140,16 @@ func altinityArchiveNameBeforeUIDs(j *backupsv1alpha1.BackupJob) string { // the archive name. A Job created by a controller that predates archive // names, or rendered from a strategy that names its own archives, wrote an // archive under another name, and recording this one would point the Backup -// at an archive that does not exist. +// at an archive that does not exist. The name has to stand on its own: the +// name without the UID starts the name with it, and a strategy may append to +// either. func altinityJobCarriesArchive(job *batchv1.Job, archive string) bool { spec, err := json.Marshal(job.Spec.Template.Spec) if err != nil { return false } - return bytes.Contains(spec, []byte(archive)) + whole := regexp.MustCompile(`(^|[^A-Za-z0-9._-])` + regexp.QuoteMeta(archive) + `($|[^A-Za-z0-9._-])`) + return whole.Match(spec) } // renderAltinityTemplate runs the strategy's PodTemplateSpec through the @@ -293,8 +296,6 @@ func (r *BackupJobReconciler) reconcileAltinity(ctx context.Context, j *backupsv if j.Status.BackupRef != nil { return ctrl.Result{}, nil } - // The name without the UID is a prefix of the one with it, so it is - // tried only after the full name is not found. if !altinityJobCarriesArchive(batchJob, archive) { archive = altinityArchiveNameBeforeUIDs(j) if !altinityJobCarriesArchive(batchJob, archive) { diff --git a/internal/backupcontroller/altinitystrategy_controller_test.go b/internal/backupcontroller/altinitystrategy_controller_test.go index 08c6e5466..15a450e59 100644 --- a/internal/backupcontroller/altinitystrategy_controller_test.go +++ b/internal/backupcontroller/altinitystrategy_controller_test.go @@ -835,6 +835,30 @@ func TestReconcileAltinity_RecordsTheArchiveTheJobWasGiven(t *testing.T) { // re-rendered, so its archive carries the old name, and the Backup has to // record that name rather than none. func TestReconcileAltinity_RecordsTheArchiveOfAJobNamedBeforeUIDs(t *testing.T) { + const legacy = "clickhouse-ch-test-20260304T050607Z" + cases := []struct { + name string + archive string // archive the Job's Pod was rendered with + want string + }{ + {name: "the name without the UID", archive: legacy, want: legacy}, + {name: "a longer name that starts with it", archive: legacy + "-copy", want: ""}, + {name: "a longer name that ends with it", archive: "copy-" + legacy, want: ""}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := recordAltinityArchiveOfJobRenderedWith(t, tc.archive); got != tc.want { + t.Errorf("Backup records archive %q, want %q", got, tc.want) + } + }) + } +} + +// recordAltinityArchiveOfJobRenderedWith completes a backup Job whose Pod +// carries the given archive in place of the one the controller rendered, and +// returns the archive the resulting Backup records. +func recordAltinityArchiveOfJobRenderedWith(t *testing.T, archive string) string { + t.Helper() app := newAltinityClickHouseApp("ch-test", "tenant-test") strategy := newAltinityArchiveStrategy("clickhouse-strategy") startedAt := metav1.NewTime(time.Date(2026, 3, 4, 5, 6, 7, 0, time.UTC)) @@ -855,7 +879,6 @@ func TestReconcileAltinity_RecordsTheArchiveOfAJobNamedBeforeUIDs(t *testing.T) } r, _, _ := newAltinityTestEnv(t, app, clientfake.NewClientBuilder().WithObjects(backupJob, strategy)) ctx := context.Background() - const legacy = "clickhouse-ch-test-20260304T050607Z" if _, err := r.reconcileAltinity(ctx, backupJob, resolved); err != nil { t.Fatalf("reconcileAltinity() creating the Job: %v", err) @@ -864,9 +887,9 @@ func TestReconcileAltinity_RecordsTheArchiveOfAJobNamedBeforeUIDs(t *testing.T) if err := r.Get(ctx, client.ObjectKey{Namespace: "tenant-test", Name: jobNameForBackupJob(backupJob)}, job); err != nil { t.Fatalf("get backup Job: %v", err) } - job.Spec.Template.Spec.Containers[0].Args[1] = "--archive=" + legacy + job.Spec.Template.Spec.Containers[0].Args[1] = "--archive=" + archive if err := r.Update(ctx, job); err != nil { - t.Fatalf("give the backup Job the name without the UID: %v", err) + t.Fatalf("render the backup Job with archive %q: %v", archive, err) } job.Status.Conditions = []batchv1.JobCondition{{Type: batchv1.JobComplete, Status: corev1.ConditionTrue}} if err := r.Status().Update(ctx, job); err != nil { @@ -883,9 +906,7 @@ func TestReconcileAltinity_RecordsTheArchiveOfAJobNamedBeforeUIDs(t *testing.T) if err := r.Get(ctx, client.ObjectKey{Namespace: "tenant-test", Name: backupJob.Name}, created); err != nil { t.Fatalf("get Backup: %v", err) } - if got := created.Spec.DriverMetadata[altinityArchiveMetaKey]; got != legacy { - t.Errorf("Backup records archive %q, want %q", got, legacy) - } + return created.Spec.DriverMetadata[altinityArchiveMetaKey] } // TestReconcileAltinity_RecordsNoArchiveTheJobWasNotGiven covers a Job whose From 5a95bcdd64e042701bb448f35d33536cfbd1ef9c Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Thu, 12 Nov 2026 11:10:43 +0300 Subject: [PATCH 12/13] fix(clickhouse): keep a run's archive name within the old name's length The archive name is part of every object key, a key is capped at 1024 bytes, and the legacy s3PathOverride prefix has no length limit. The full UID made the name 37 characters longer than before, so a prefix that fit the old name pushed the manifest key past the cap and failed the backup. The run's name now drops "clickhouse-" and the "T" and "Z" of the timestamp and spends the room on the first 12 hex digits of the UID, so it is exactly as long as the old name for every application. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- hack/altinity-backup-strategy.bats | 2 +- .../altinitystrategy_controller.go | 22 +++++++++-- .../altinitystrategy_controller_test.go | 39 ++++++++++++++++--- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/hack/altinity-backup-strategy.bats b/hack/altinity-backup-strategy.bats index 97ec869cc..070b6bad5 100644 --- a/hack/altinity-backup-strategy.bats +++ b/hack/altinity-backup-strategy.bats @@ -392,7 +392,7 @@ run_script() { remote_rows clickhouse-myapp-20260304T040000Z clickhouse-myapp-20260304T050607Z \ "clickhouse-myapp-20260304T050700Z|broken (can't stat metadata.json)" \ clickhouse-myapp-20260304T050710Z-copy \ - clickhouse-myapp-20260304T050720Z-0f3c9a1e-5b7d-4c2a-9e8f-1a2b3c4d5e6f \ + myapp-20260304050720-0f3c9a1e5b7d \ clickhouse-myapp-20260304T060000Z > "$tmp/state/remote" out=$(run_script "$tmp" 2>&1) grep -qxF 'POST http://chi-clickhouse-myapp-clickhouse-0-0.tenant-x.svc:7171/backup/restore_remote/clickhouse-myapp-20260304T050607Z' "$tmp/state/requests" diff --git a/internal/backupcontroller/altinitystrategy_controller.go b/internal/backupcontroller/altinitystrategy_controller.go index c29162474..ef91dc177 100644 --- a/internal/backupcontroller/altinitystrategy_controller.go +++ b/internal/backupcontroller/altinitystrategy_controller.go @@ -57,6 +57,12 @@ const ( // archive relies on. altinityArchiveTimeLayout = "20060102T150405Z" + // altinityArchiveRunTimeLayout and altinityArchiveUIDLength shape the + // archive name of a run; see altinityArchiveName for why they are this + // short. + altinityArchiveRunTimeLayout = "20060102150405" + altinityArchiveUIDLength = 12 + // Polling cadence for the Altinity Job lifecycle. Mirrors the CNPG / // Velero strategy cadence so behaviour is uniform across drivers. altinityPollInterval = 5 * time.Second @@ -122,11 +128,19 @@ func backupParameters(b *backupsv1alpha1.Backup) map[string]string { // one-second resolution, and two Plans on one schedule start their runs in // the same second; the strategy takes a finished upload of its archive as an // earlier attempt of its own run, so a shared name would let one run report -// the other's archive. The UID goes after the timestamp so names still sort -// chronologically, and the restore of a Backup that records no archive, which -// accepts only clickhouse--, never picks one of these. +// the other's archive. +// +// The name is no longer than altinityArchiveNameBeforeUIDs, because it sits in +// every object key, a key is capped at 1024 bytes, and the legacy +// s3PathOverride prefix has no length limit of its own: the UID takes the room +// that dropping "clickhouse-", "T" and "Z" frees. It goes after the timestamp +// so names still sort chronologically, and the restore of a Backup that +// records no archive, which accepts only clickhouse--, +// never picks one of these. func altinityArchiveName(j *backupsv1alpha1.BackupJob) string { - return altinityArchiveNameBeforeUIDs(j) + "-" + string(j.UID) + uid := strings.ReplaceAll(string(j.UID), "-", "") + uid = uid[:min(len(uid), altinityArchiveUIDLength)] + return fmt.Sprintf("%s-%s-%s", j.Spec.ApplicationRef.Name, j.Status.StartedAt.UTC().Format(altinityArchiveRunTimeLayout), uid) } // altinityArchiveNameBeforeUIDs is the name a controller that did not append diff --git a/internal/backupcontroller/altinitystrategy_controller_test.go b/internal/backupcontroller/altinitystrategy_controller_test.go index 15a450e59..7f2c906aa 100644 --- a/internal/backupcontroller/altinitystrategy_controller_test.go +++ b/internal/backupcontroller/altinitystrategy_controller_test.go @@ -735,11 +735,36 @@ func TestAltinityArchiveName_FromStartedAtInUTC(t *testing.T) { Spec: backupsv1alpha1.BackupJobSpec{ApplicationRef: newAltinityClickHouseRef("ch-test")}, Status: backupsv1alpha1.BackupJobStatus{StartedAt: &startedAt}, } - if got, want := altinityArchiveName(j), "clickhouse-ch-test-20260304T050607Z-0f3c9a1e-5b7d-4c2a-9e8f-1a2b3c4d5e6f"; got != want { + if got, want := altinityArchiveName(j), "ch-test-20260304050607-0f3c9a1e5b7d"; got != want { t.Errorf("altinityArchiveName = %q, want %q", got, want) } } +// TestAltinityArchiveName_NoLongerThanTheNameWithoutUID pins the S3 key +// budget: the archive name is part of every object key, a key may not exceed +// 1024 bytes, and the legacy s3PathOverride prefix is unbounded, so a prefix +// that fit the name without the UID must still fit this one. +func TestAltinityArchiveName_NoLongerThanTheNameWithoutUID(t *testing.T) { + startedAt := metav1.NewTime(time.Date(2026, 3, 4, 5, 6, 7, 0, time.UTC)) + for n := 1; n <= 63; n++ { + j := &backupsv1alpha1.BackupJob{ + ObjectMeta: metav1.ObjectMeta{UID: "0f3c9a1e-5b7d-4c2a-9e8f-1a2b3c4d5e6f"}, + Spec: backupsv1alpha1.BackupJobSpec{ApplicationRef: newAltinityClickHouseRef(strings.Repeat("a", n))}, + Status: backupsv1alpha1.BackupJobStatus{StartedAt: &startedAt}, + } + name, legacy := altinityArchiveName(j), altinityArchiveNameBeforeUIDs(j) + if len(name) > len(legacy) { + t.Errorf("application name of %d characters: archive %q is longer than %q", n, name, legacy) + } + // The manifest key of a prefix that left the name without the UID + // exactly at the limit. + prefix := strings.Repeat("p", 1024-len("/"+legacy+"/metadata.json")) + if key := prefix + "/" + name + "/metadata.json"; len(key) > 1024 { + t.Errorf("application name of %d characters: manifest key is %d bytes", n, len(key)) + } + } +} + // TestAltinityArchiveName_DistinctForRunsStartedInTheSameSecond pins that two // BackupJobs of one application started within the same second, as two Plans // on the same schedule are, write different archives. With a shared name, the @@ -760,7 +785,7 @@ func TestAltinityArchiveName_DistinctForRunsStartedInTheSameSecond(t *testing.T) t.Fatalf("two runs started in the same second share archive %q", daily) } for _, name := range []string{daily, weekly} { - if !strings.HasPrefix(name, "clickhouse-ch-test-20260304T050607Z-") { + if !strings.HasPrefix(name, "ch-test-20260304050607-") { t.Errorf("archive %q does not start with the application and start time, so names stop sorting chronologically", name) } } @@ -793,7 +818,7 @@ func TestReconcileAltinity_RecordsTheArchiveTheJobWasGiven(t *testing.T) { } r, _, _ := newAltinityTestEnv(t, app, clientfake.NewClientBuilder().WithObjects(backupJob, strategy)) ctx := context.Background() - const want = "clickhouse-ch-test-20260304T050607Z-test-bj-uid" + const want = "ch-test-20260304050607-testbjuid" if _, err := r.reconcileAltinity(ctx, backupJob, resolved); err != nil { t.Fatalf("reconcileAltinity() creating the Job: %v", err) @@ -844,6 +869,10 @@ func TestReconcileAltinity_RecordsTheArchiveOfAJobNamedBeforeUIDs(t *testing.T) {name: "the name without the UID", archive: legacy, want: legacy}, {name: "a longer name that starts with it", archive: legacy + "-copy", want: ""}, {name: "a longer name that ends with it", archive: "copy-" + legacy, want: ""}, + {name: "a longer name that continues it after a period", archive: legacy + ".copy", want: ""}, + {name: "a longer name that leads into it with a period", archive: "copy." + legacy, want: ""}, + {name: "a longer name that continues it after an underscore", archive: legacy + "_copy", want: ""}, + {name: "a longer name that leads into it with an underscore", archive: "copy_" + legacy, want: ""}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { @@ -977,13 +1006,13 @@ func TestReconcileAltinity_RecordsNoArchiveTheJobWasNotGiven(t *testing.T) { // A Backup recording this run's own archive is the one an earlier reconcile // created before its status write was lost, and is adopted. func TestReconcileAltinity_BackupNameAlreadyTaken(t *testing.T) { - const thisRun = "clickhouse-ch-test-20260305T050607Z-test-bj-uid" + const thisRun = "ch-test-20260305050607-testbjuid" cases := []struct { name string existing string // archive the existing Backup records; empty for none wantSucceeded bool }{ - {name: "earlier run's Backup records another archive", existing: "clickhouse-ch-test-20260304T050607Z-earlier-bj-uid"}, + {name: "earlier run's Backup records another archive", existing: "ch-test-20260304050607-earlierbjuid"}, {name: "earlier run's Backup records no archive", existing: ""}, {name: "this run's Backup from a reconcile whose status write was lost", existing: thisRun, wantSucceeded: true}, } From 583f18437d8f78dbf65fca767c779fe25bb7a53e Mon Sep 17 00:00:00 2001 From: Alexey Artamonov Date: Thu, 12 Nov 2026 17:21:51 +0300 Subject: [PATCH 13/13] test(clickhouse): give the credentials-wait BackupJob a UID The test pins that a backup held for late bucket credentials keeps its archive name, and a run's archive name now carries part of the BackupJob's UID, so the fixture needs one for the name it expects to be the name the controller mints. Assisted-by: LLM Signed-off-by: Alexey Artamonov --- internal/backupcontroller/altinitystrategy_controller_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/backupcontroller/altinitystrategy_controller_test.go b/internal/backupcontroller/altinitystrategy_controller_test.go index 49da21fa0..a9022ceb6 100644 --- a/internal/backupcontroller/altinitystrategy_controller_test.go +++ b/internal/backupcontroller/altinitystrategy_controller_test.go @@ -1640,7 +1640,7 @@ func TestReconcileAltinityRestore_UnreadableHoldStateDoesNotStartTheJob(t *testi func TestReconcileAltinity_CredentialsWaitKeepsTheArchiveName(t *testing.T) { startedAt := metav1.NewTime(time.Date(2026, 3, 4, 5, 6, 7, 0, time.UTC)) backupJob := &backupsv1alpha1.BackupJob{ - ObjectMeta: metav1.ObjectMeta{Name: "test-bj", Namespace: "tenant-test"}, + ObjectMeta: metav1.ObjectMeta{Name: "test-bj", Namespace: "tenant-test", UID: "0f3c9a1e-5b7d-4c2a-9e8f-1a2b3c4d5e6f"}, Spec: backupsv1alpha1.BackupJobSpec{ ApplicationRef: newAltinityClickHouseRef("ch-test"), BackupClassName: "clickhouse-backup", @@ -1659,7 +1659,7 @@ func TestReconcileAltinity_CredentialsWaitKeepsTheArchiveName(t *testing.T) { }, } ctx := context.Background() - const want = "clickhouse-ch-test-20260304T050607Z" + const want = "ch-test-20260304050607-0f3c9a1e5b7d" reconcile := func(step string) { t.Helper() j := &backupsv1alpha1.BackupJob{}