mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 20:25:22 +09:00
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 <aleksei.artamonov@aenix.io>
(cherry picked from commit 993b79bb37)
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
This commit is contained in:
1 parent
1e5e3a45df
commit
817b59a45a
1 file changed
+69
-31
+69
-31
@@ -58,43 +58,68 @@ spec:
|
||||
# text/template's missing-key marker, when a controller that
|
||||
# predates archive names renders this template during an upgrade.
|
||||
archive="{{ printf `{{ .ArchiveName | default "" }}` }}"
|
||||
# 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 }}" }}-"
|
||||
# Only that older controller leaves the name empty, and it
|
||||
# records no name on the Backup either.
|
||||
# Only a controller that predates archive names leaves it
|
||||
# empty, and that one records no name on the Backup either.
|
||||
target="${archive:-${prefix}$(date -u +%Y%m%dT%H%M%SZ)}"
|
||||
# A retried Pod gets the same name. Wait on an attempt the
|
||||
# sidecar is still running, which would answer a new one with
|
||||
# 423, and ask for resume, 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.
|
||||
state=$(curl -fsS ${auth} "${api}/backup/actions" \
|
||||
| jq -rs --arg suffix "${target}" \
|
||||
'[.[] | select((.command // "") | endswith($suffix))] | sort_by(.start) | last | .status // ""')
|
||||
if [ "${state}" != "in progress" ]; then
|
||||
echo "create_remote ${target}"
|
||||
# A retried Pod gets the same name, so it takes over an attempt
|
||||
# the sidecar is still running, and otherwise asks for resume,
|
||||
# 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 <name>".
|
||||
cmd="create_remote --resume ${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/create_remote?name=${target}&resume=true" >/dev/null
|
||||
fi
|
||||
else
|
||||
app="{{ printf "{{ .Backup.ApplicationRef.Name }}" }}"
|
||||
backup="{{ printf "{{ .Backup.Name }}" }}"
|
||||
prefix="clickhouse-${app}-"
|
||||
remote=$(curl -fsS ${auth} "${api}/backup/list/remote")
|
||||
remote=$(api_get /backup/list/remote 60)
|
||||
if [ -n "${archive}" ]; then
|
||||
if ! printf '%s\n' "${remote}" | jq -es --arg name "${archive}" 'any(.[]; .name == $name)' >/dev/null; then
|
||||
echo "archive ${archive} recorded on Backup ${backup} is not in remote storage; not restoring any other archive" >&2
|
||||
@@ -115,13 +140,25 @@ spec:
|
||||
exit 1
|
||||
fi
|
||||
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.
|
||||
@@ -132,9 +169,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
|
||||
|
||||
Reference in new issue
Block a user