mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-01 20:56:02 +09:00
fix(postgres): run the collation repair outside the Helm hook
The collation repair ran at the end of the post-upgrade init-job and, when the image moved, waited for the primary to run it. Helm starts that hook as soon as the Cluster is patched; CNPG then rolls the replicas first and the primary last, and every instance whose clients hold sessions sits in smart shutdown for smartShutdownTimeout (180s on existing clusters). With three instances the wait took about eight of the Job's nine minutes, the Job printed "the init-job is out of time, the next run of it carries on" and the HelmRelease went UpgradeFailed until Flux retried. When the Job's psql met the primary restart instead, it failed with "FATAL: the database system is shutting down" and failed the upgrade the same way. A rebuild of a large index does not fit the hook budget either. Moving the repair into an ordinary manifest does not help: helm-controller waits for every Job of a release to complete within the same upgrade timeout. So a short post-upgrade hook, run after the init-job, creates the repair Job through the API, outside the release, and returns. The repair Job has an hour, keeps the wait for the new image and the resume marks it had, and is owned by the Cluster. It has a fixed name, so two repairs never run against one Cluster: the hook leaves one still running on the same image alone and deletes any other, waiting for its pod to go, before creating the new one, which also retries a repair that failed. The hook's Role reaches only that Job and the release's Cluster by name, apart from create, which RBAC cannot narrow, and is deleted once the hooks succeed. psql exits with status 2 both when an instance refuses connections while shutting down and when it drops a session, so both Jobs now rerun their script after a growing pause on that status instead of failing; the scripts are idempotent and their elapsed-time bounds keep counting across attempts. A connection lost during an index rebuild is reported the same way. smartShutdownTimeout stays as it is: the repair no longer waits inside the hook, so the rollout's length does not matter. The PostGIS extension update waits for the same rollout, so it moves into the repair Job with the collation pass, and the messages of both name the next upgrade of the release as what starts them again. A lost connection in the PostGIS pass is reported with the same status as in the collation pass, so the Job reconnects instead of giving up. The repair runs after install as well. The init-job used to record there which Debian release each database started on, and without that record the first image move is detected only if the repair reaches the primary before it restarts. The Job sets PGCONNECT_TIMEOUT, so a primary that does not answer is retried instead of using up the Job's deadline. A failed index rebuild no longer stops the PostGIS update that follows it: a unique index that duplicates keep from being rebuilt would otherwise hold the extensions back until an operator removes them. The Job still fails at its end. With an hour to spend, the repair now waits for the new image whenever the image changes, not only when an index already depends on a versioned collation. Otherwise it planned on the previous primary, missed an index created meanwhile, and the release it recorded at the end claimed that index repaired. The repair Job carries the WorkloadMonitor's selector labels, opts in to having its failure reported with workloads.cozystack.io/report-failure, and has no ttlSecondsAfterFinished, so a failed repair stays visible until the next upgrade replaces it instead of vanishing while the indexes stay wrong. Refs: #5459 Assisted-By: LLM Signed-off-by: Andrey Kolkov <androndo@gmail.com>
This commit is contained in:
1 parent
222c31fcf8
commit
66bba3bf1e
10 files changed
+574
-79
No files matched your search
@@ -250,9 +250,9 @@ spec:
|
||||
check:
|
||||
($error == null): true
|
||||
|
||||
# The init Job rebuilds an index whose collation changed library version.
|
||||
# The pass is SQL run by a Helm hook, so only a real server can say whether
|
||||
# it works. A collation created with an explicit version reads as stale
|
||||
# The collation repair Job rebuilds an index whose collation changed library
|
||||
# version. The pass is SQL run by a Job a Helm hook starts, so only a real
|
||||
# server can say whether it works. A collation created with an explicit version reads as stale
|
||||
# without swapping the operand image, and so does a C.<codeset> collation
|
||||
# once the database records another Debian release as the one it was
|
||||
# repaired for, which makes one more upgrade of the release enough to run
|
||||
@@ -293,6 +293,15 @@ spec:
|
||||
EOF
|
||||
check:
|
||||
($error == null): true
|
||||
# A repair Job left by the install has completed already; it goes
|
||||
# first, so that the Job awaited below is the one this upgrade starts.
|
||||
- script:
|
||||
timeout: 2m
|
||||
content: |
|
||||
set -eu
|
||||
kubectl -n "$NAMESPACE" delete job postgres-test-collation-repair --ignore-not-found --wait=true
|
||||
check:
|
||||
($error == null): true
|
||||
# A declared user is the smallest change that makes Helm upgrade the
|
||||
# release, and so run the hook, without restarting PostgreSQL.
|
||||
- patch:
|
||||
@@ -317,6 +326,58 @@ spec:
|
||||
status:
|
||||
(conditions[?type == 'Complete']):
|
||||
- status: "True"
|
||||
# The repair runs in a Job a post-upgrade hook starts outside the
|
||||
# release, after the init Job. The label is what makes the
|
||||
# WorkloadMonitor report a failed repair on the Application.
|
||||
- assert:
|
||||
timeout: 15m
|
||||
resource:
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: postgres-test-collation-repair
|
||||
labels:
|
||||
app.kubernetes.io/name: postgres.apps.cozystack.io
|
||||
app.kubernetes.io/instance: postgres-test
|
||||
workloads.cozystack.io/report-failure: "true"
|
||||
status:
|
||||
(conditions[?type == 'Complete']):
|
||||
- status: "True"
|
||||
# The next upgrade finds that Job complete on the same image and replaces
|
||||
# it with a new run under the same name.
|
||||
- script:
|
||||
timeout: 1m
|
||||
content: |
|
||||
set -eu
|
||||
# A failed repair must stay visible until the next upgrade replaces it.
|
||||
ttl=$(kubectl -n "$NAMESPACE" get job postgres-test-collation-repair -o jsonpath='{.spec.ttlSecondsAfterFinished}')
|
||||
if [ -n "$ttl" ]; then
|
||||
echo "the collation repair Job has ttlSecondsAfterFinished=$ttl, so a failed repair would disappear" >&2
|
||||
exit 1
|
||||
fi
|
||||
kubectl -n "$NAMESPACE" annotate job postgres-test-collation-repair e2e.cozystack.io/previous-run=true
|
||||
check:
|
||||
($error == null): true
|
||||
- patch:
|
||||
resource:
|
||||
apiVersion: apps.cozystack.io/v1alpha1
|
||||
kind: Postgres
|
||||
metadata:
|
||||
name: test
|
||||
spec:
|
||||
users:
|
||||
collationprobe: {}
|
||||
collationprobe2: {}
|
||||
- script:
|
||||
timeout: 15m
|
||||
content: |
|
||||
set -eu
|
||||
until [ "$(kubectl -n "$NAMESPACE" get job postgres-test-collation-repair --ignore-not-found \
|
||||
-o jsonpath='{.metadata.annotations.e2e\.cozystack\.io/previous-run}|{.status.conditions[?(@.type=="Complete")].status}')" = "|True" ]; do
|
||||
sleep 5
|
||||
done
|
||||
check:
|
||||
($error == null): true
|
||||
- script:
|
||||
timeout: 2m
|
||||
content: |
|
||||
@@ -330,14 +391,14 @@ spec:
|
||||
}
|
||||
refreshed=$(q "SELECT collversion = pg_collation_actual_version(oid) FROM pg_collation WHERE collname = 'stale_icu'")
|
||||
if [ "$refreshed" != t ]; then
|
||||
echo "the init Job left the collation version stale" >&2
|
||||
echo "the collation repair Job left the collation version stale" >&2
|
||||
exit 1
|
||||
fi
|
||||
# REINDEX CONCURRENTLY gives the index a new OID, so an unchanged
|
||||
# one means the version was refreshed over an index nobody rebuilt.
|
||||
rebuilt=$(q "SELECT 'collation_probe_s'::regclass::oid <> index FROM collation_probe_before")
|
||||
if [ "$rebuilt" != t ]; then
|
||||
echo "the init Job refreshed the collation version without rebuilding the index" >&2
|
||||
echo "the collation repair Job refreshed the collation version without rebuilding the index" >&2
|
||||
exit 1
|
||||
fi
|
||||
q "CREATE EXTENSION IF NOT EXISTS amcheck"
|
||||
@@ -346,18 +407,18 @@ spec:
|
||||
# OID and gets a new storage file.
|
||||
rebuilt_in_place=$(q "SELECT c.relfilenode <> b.in_place_file FROM pg_class c, collation_probe_before b WHERE c.oid = 'collation_probe_excl_s_excl'::regclass")
|
||||
if [ "$rebuilt_in_place" != t ]; then
|
||||
echo "the init Job did not rebuild the exclusion constraint's index" >&2
|
||||
echo "the collation repair Job did not rebuild the exclusion constraint's index" >&2
|
||||
exit 1
|
||||
fi
|
||||
rebuilt_c=$(q "SELECT 'collation_probe_c_s_key'::regclass::oid <> c_index FROM collation_probe_before")
|
||||
if [ "$rebuilt_c" != t ]; then
|
||||
echo "the init Job did not rebuild the index on a C.<codeset> collation after the Debian release changed" >&2
|
||||
echo "the collation repair Job did not rebuild the index on a C.<codeset> collation after the Debian release changed" >&2
|
||||
exit 1
|
||||
fi
|
||||
q "SELECT bt_index_check('collation_probe_c_s_key', true)"
|
||||
release=$(q "SELECT current_setting('cozystack.collation_release', true) = substring(current_setting('server_version') from 'pgdg[0-9]+')")
|
||||
if [ "$release" != t ]; then
|
||||
echo "the init Job did not record the Debian release it repaired the database for" >&2
|
||||
echo "the collation repair Job did not record the Debian release it repaired the database for" >&2
|
||||
exit 1
|
||||
fi
|
||||
qu() {
|
||||
@@ -366,21 +427,21 @@ spec:
|
||||
}
|
||||
rebuilt_unrecorded=$(qu "SELECT 'collation_probe_c_s_key'::regclass::oid <> c_index FROM collation_probe_before")
|
||||
if [ "$rebuilt_unrecorded" != t ]; then
|
||||
echo "the init Job did not rebuild the index on a C.<codeset> collation in a database that recorded no release and holds a stale libc collation" >&2
|
||||
echo "the collation repair Job did not rebuild the index on a C.<codeset> collation in a database that recorded no release and holds a stale libc collation" >&2
|
||||
exit 1
|
||||
fi
|
||||
qu "CREATE EXTENSION IF NOT EXISTS amcheck"
|
||||
qu "SELECT bt_index_check('collation_probe_c_s_key', true)"
|
||||
release=$(qu "SELECT current_setting('cozystack.collation_release', true) = substring(current_setting('server_version') from 'pgdg[0-9]+')")
|
||||
if [ "$release" != t ]; then
|
||||
echo "the init Job did not record the Debian release it repaired the unrecorded database for" >&2
|
||||
echo "the collation repair Job did not record the Debian release it repaired the unrecorded database for" >&2
|
||||
exit 1
|
||||
fi
|
||||
# The pass keeps its progress in a setting of the database and
|
||||
# removes it once nothing is left to repair.
|
||||
progress=$(q "SELECT count(*) FROM pg_db_role_setting s JOIN pg_database d ON d.oid = s.setdatabase, unnest(s.setconfig) AS c WHERE d.datname = current_database() AND c LIKE 'cozystack.collation_repair=%'")
|
||||
if [ "$progress" != 0 ]; then
|
||||
echo "the init Job left its repair setting on a database it finished repairing" >&2
|
||||
echo "the collation repair Job left its repair setting on a database it finished repairing" >&2
|
||||
exit 1
|
||||
fi
|
||||
leftovers=$(q "SELECT count(*) FROM pg_index WHERE NOT indisvalid")
|
||||
@@ -407,6 +468,11 @@ spec:
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
name: postgres-test-init-job
|
||||
- delete:
|
||||
ref:
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
name: postgres-test-collation-repair
|
||||
# The Postgres CR itself was applied by chainsaw and is deleted (and waited
|
||||
# on) automatically during cleanup — no explicit teardown step needed.
|
||||
---
|
||||
|
||||
Reference in new issue
Block a user