Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/development/pod-management-architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ The `createMissingResources` function handles pod creation:

Scale-down uses the [drain state machine](#5-drain-state-machine). Extra pods are ranked across all pools and cells in the shard, preferring replicas over the primary. Before starting another planned drain, an uncached API read checks every shard pod for drain annotations or termination, including extra and surge pods. Desired pods must be Ready; an unhealthy extra does not block its own removal.

Scale-down, rolling updates, and filesystem-resize restarts also require a fresh data-plane preflight: an active committed primary, recovered cohort members on the same rule, and connected surviving followers. The remaining cohort must satisfy the committed durability policy and Multigres's recruitment quorum. Deleting a pod does not by itself establish recovery: if its membership is still committed, the next removal waits. Missing or inconsistent observations fail closed, emit `DisruptionBlocked` for a failed data-plane check, and retry after five seconds. The operator observes consensus; it does not appoint leaders or rewrite cohort membership. If Multigres cannot safely shrink the cohort, scale-down remains paused.
Scale-down, rolling updates, and filesystem-resize restarts also require a fresh data-plane preflight: an active committed primary, recovered cohort members on the same rule, and connected surviving followers. The remaining cohort must satisfy the committed durability policy and Multigres's recruitment quorum. Deleting a pod does not by itself establish recovery: if its membership is still committed, the next removal waits. The preflight is taken once per reconcile pass and every candidate is checked against it: a refusal is about the pod being removed, so a refused candidate does not stop the ones after it, and the first candidate allowed is the pass's one disruption. Missing or inconsistent observations fail closed. When no candidate is allowed, the first refusal is reported in the `RollingUpdate` condition with reason `RolloutBlocked` and as a `DisruptionBlocked` Event when it changes, and the shard is polled on a backoff from five seconds up to a minute (plus up to 20% jitter), measured from when that refusal began, because only a pooler RPC can show that it has ended. Waits that end in a Pod or PVC watch event (a drain in progress, a pod or PVC being deleted, a pod not yet Ready) are not polled: the event starts the next pass, and the pass asks only for a backstop requeue six to nine minutes out. The operator observes consensus; it does not appoint leaders or rewrite cohort membership. If Multigres cannot safely shrink the cohort, scale-down remains paused.

Healthy primaries omit `leadershipStatus` in the pinned Multigres implementation. The gate requires observed PostgreSQL primary readiness and committed identity, not an `ACTIVE` leadership signal. A current-term resignation blocks disruption; a stale resignation from an earlier term does not. Cohort ineligibility blocks independently of the resignation term. These Status observations are best-effort, not an atomic availability reservation, and connected followers alone do not establish WAL catch-up.

Expand Down Expand Up @@ -208,7 +208,7 @@ The drain state machine coordinates pod removal within the shard controller. The

When choosing which pod to remove during scale-down:

1. **Strongly disfavor the primary (score penalty −1000).** The pod role is read from `shard.Status.PodRoles`, which the shard controller populates by reading etcd topology. The primary is never the *preferred* target, but it is **not hard-excluded**: if the primary is the only extra pod remaining (e.g. after all replicas have already been removed), it can still be selected to avoid deadlocking the scale-down. Rolling updates handle the primary separately via `handleRollingUpdates` (replicas first, primary last with switchover).
1. **Strongly disfavor the primary (score penalty −1000).** The pod role is read from `shard.Status.PodRoles`, which the shard controller populates by reading etcd topology. The primary is never the *preferred* target, but it is **not hard-excluded**: if the primary is the only extra pod remaining (e.g. after all replicas have already been removed), it can still be selected to avoid deadlocking the scale-down. Rolling updates handle the primary separately in the disruption walk, `startDisruption` (replicas first, then the primary by switchover once no other pod in the shard is drifted).
2. **Prefer non-ready pods.** Pods that are already failing are better candidates.
3. **Among ready non-primary pods, select the highest index.** This makes order deterministic and predictable.
4. **If no suitable pod is found, defer.** Requeue and try again.
Expand Down Expand Up @@ -440,7 +440,8 @@ All pod management code lives in `pkg/resource-handler/controller/shard/`:
| File | Purpose |
|---|---|
| `shard_controller.go` | Main reconciler. Orchestrates pool reconciliation, sets up watches (including ConfigMap watches via `enqueueFromPostgresConfigMap` for `postgresConfigRef` changes), PVC owner-reference management. |
| `reconcile_pool_pods.go` | Core pod lifecycle: `reconcilePoolPods`, `createMissingResources`, `handleScaleDown`, `handleRollingUpdates`, `selectPodToDrain`, `cleanupDrainedPod`, `podNeedsUpdate`. |
| `reconcile_pool_pods.go` | Pool/cell maintenance: `reconcilePoolPods`, `createMissingResources`, `handleScaleDown`, `handleRollingUpdates`, `rankPodsToDrain`, `cleanupDrainedPod`, `podNeedsUpdate`. It records which pool/cells ask for a disruption; it starts none. |
| `disruption_walk.go` | `startDisruption`: the one place a pool pod's removal is decided, once per pass for the whole shard. It observes the data plane once, checks every candidate (resize restart, extra, drifted replica, primary) against that observation and requests the drain of the first one allowed. Every gate, count and target, here and in pool maintenance, the maintenance surge and the drain state machine, is over the pods the shard manages (`isPoolPod`: controlled by the shard, named as its pool/cell labels and ordinal give); any other pod carrying the labels is ignored and named in the `RollingUpdate` condition while anything is waiting. |
| `reconcile_deletion.go` | Shard deletion: `handleDeletion`, topology unregistration, child resource cleanup. |
| `reconcile_multiorch.go` | MultiOrch deployment and service reconciliation. |
| `reconcile_shared_infra.go` | Shared infrastructure: pgBackRest TLS certs, pg_hba ConfigMap, postgres password Secret, shared backup PVC, PDB, headless service. |
Expand Down
2 changes: 1 addition & 1 deletion pkg/ownership/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ var Fields = []Field{
{Kind: kindShard, Path: ".status.readyReplicas", Manager: ManagerShard, Class: Observation},

// Pod
// reconcile_pool_pods.go podTopoDrifted and disruption.go canStartDisruption
// reconcile_pool_pods.go podTopoDrifted and disruption.go observeDisruption
// read this through Pod readiness. A missing value blocks disruption and permits
// replacement of a drifted pod; reconcilePoolerReadiness refreshes it from pooler status.
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestBackupStatus(t *testing.T) {
return store, nil
},
}
_, err := rr.reconcileDataPlane(ctx, get(), renderedConfig{})
_, err := rr.reconcileDataPlane(ctx, get(), renderedConfig{}, &shardRolloutTracker{})
require.NoError(t, err)
cond := meta.FindStatusCondition(get().Status.Conditions, backuphealth.ConditionHealthy)
require.NotNil(t, cond)
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestBackupStatus(t *testing.T) {
client: rpcclient.NewFakeClient(),
err: errors.New("certificate not issued"),
})
_, err := rr.reconcileDataPlane(t.Context(), get(), renderedConfig{})
_, err := rr.reconcileDataPlane(t.Context(), get(), renderedConfig{}, &shardRolloutTracker{})
require.NoError(t, err)
got := get()
handedOver(t, got)
Expand All @@ -303,7 +303,7 @@ func TestBackupStatus(t *testing.T) {
t.Run("without a primary to list backups from", func(t *testing.T) {
get := legacy(t, "upgrade-no-primary")
rr := reconciler(t, poolerclient.Static(rpcclient.NewFakeClient()))
_, err := rr.reconcileDataPlane(t.Context(), get(), renderedConfig{})
_, err := rr.reconcileDataPlane(t.Context(), get(), renderedConfig{}, &shardRolloutTracker{})
require.NoError(t, err)
got := get()
handedOver(t, got)
Expand Down
Loading