Skip to content

Commit b727012

Browse files
authored
fix(storage): resume Umami standby replacement after claim retention (#6718)
1 parent fd58295 commit b727012

11 files changed

Lines changed: 428 additions & 59 deletions

‎.github/workflows/prepare-longhorn-v2-node.yaml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ name: Prepare Longhorn V2 Migration
5050
resume-umami-archive-data,
5151
observe-umami-v2-standby,
5252
replace-umami-v2-standby,
53+
resume-retained-umami-v2-standby,
5354
register-temporary-v2-storage,
5455
repair-v1-reservation,
5556
reserve-v1-scratch,
@@ -200,6 +201,10 @@ jobs:
200201
[[ -z "${LONGHORN_V2_NODE}" && "${LONGHORN_V2_DATABASE}" == umami-db &&
201202
"${LONGHORN_V2_CONFIRM}" =~ ^replace-umami-v2-standby:umami-db-[1-9][0-9]*:umami-db-[1-9][0-9]*$ ]]
202203
;;
204+
resume-retained-umami-v2-standby)
205+
[[ -z "${LONGHORN_V2_NODE}" && "${LONGHORN_V2_DATABASE}" == umami-db &&
206+
"${LONGHORN_V2_CONFIRM}" =~ ^resume-retained-umami-v2-standby:umami-db-[1-9][0-9]*:[1-9][0-9]{0,18}:[0-9a-f]{40}:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ ]]
207+
;;
203208
prepare-worker)
204209
[[ "${LONGHORN_V2_NODE}" =~ ^prod-worker-[123]$ && "${LONGHORN_V2_CONFIRM}" == "prepare-longhorn-v2-${LONGHORN_V2_NODE}" ]]
205210
;;

‎scripts/migrate-longhorn-v2/cnpg_health.go‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,25 @@ func cnpgInstanceID(name, cluster string) (uint64, error) {
5555
// healthyCNPGPods binds the required stable owned instances from a complete Pod
5656
// inventory. It establishes neither maintenance exclusivity nor storage clearance.
5757
func healthyCNPGPods(cluster object, allPods []object) ([]object, error) {
58+
return healthyCNPGPodsWithRetention(cluster, allPods, "")
59+
}
60+
61+
func healthyCNPGPodsWithRetention(cluster object, allPods []object, retainedStandby string) ([]object, error) {
5862
primary := text(cluster, "status", "currentPrimary")
5963
name, ns := text(cluster, "metadata", "name"), text(cluster, "metadata", "namespace")
6064
expectedInstances := hostDatabases[ns+"/"+name].instances
6165
if umamiV2Transition(cluster) {
6266
expectedInstances = 4
6367
}
68+
expectedClaimCount := expectedInstances
69+
if retainedStandby != "" {
70+
if !umamiV2Transition(cluster) || retainedStandby == primary {
71+
return nil, errors.New("the retained claim must belong to an Umami standby")
72+
}
73+
expectedClaimCount--
74+
}
6475
if expectedInstances == 0 || text(cluster, "metadata", "uid") == "" || text(cluster, "metadata", "resourceVersion") == "" || value(cluster, "metadata", "deletionTimestamp") != nil ||
65-
value(cluster, "spec", "instances") != expectedInstances || value(cluster, "status", "instances") != expectedInstances || value(cluster, "status", "readyInstances") != expectedInstances || !cnpgCondition(cluster, "Ready") || !cnpgCondition(cluster, "ContinuousArchiving") || primary == "" || primary != text(cluster, "status", "targetPrimary") {
76+
value(cluster, "spec", "instances") != expectedInstances || value(cluster, "status", "instances") != expectedClaimCount || value(cluster, "status", "readyInstances") != expectedInstances || !cnpgCondition(cluster, "Ready") || !cnpgCondition(cluster, "ContinuousArchiving") || primary == "" || primary != text(cluster, "status", "targetPrimary") {
6677
return nil, errors.New("database instance health, operator conditions or primary identity are unverified")
6778
}
6879
if fence := text(cluster, "metadata", "annotations", "cnpg.io/fencedInstances"); fence != "" && fence != "[]" {
@@ -83,6 +94,9 @@ func healthyCNPGPods(cluster object, allPods []object) ([]object, error) {
8394
if len(known) != int(expectedInstances) || !known[primary] {
8495
return nil, errors.New("all reviewed healthy database instances are required")
8596
}
97+
if retainedStandby != "" && !umamiHealthyClaimsExcludeOnly(cluster, known, retainedStandby) {
98+
return nil, errors.New("the retained standby must be the only missing healthy claim")
99+
}
86100
pods := []object{}
87101
for _, pod := range allPods {
88102
if text(pod, "metadata", "namespace") == ns && (text(pod, "metadata", "labels", "cnpg.io/cluster") == name || cnpgOwnerReference(pod, cluster)) {
@@ -135,6 +149,24 @@ func healthyCNPGPods(cluster object, allPods []object) ([]object, error) {
135149
return ordered, nil
136150
}
137151

152+
// CNPG stops counting a detached claim immediately, even while its exact
153+
// standby still runs. All other claims and all four processes must be healthy.
154+
func umamiHealthyClaimsExcludeOnly(cluster object, instances map[string]bool, retainedStandby string) bool {
155+
claims, _ := value(cluster, "status", "healthyPVC").([]any)
156+
if !instances[retainedStandby] || len(claims) != len(instances)-1 {
157+
return false
158+
}
159+
seen := map[string]bool{}
160+
for _, entry := range claims {
161+
name, ok := entry.(string)
162+
if !ok || !instances[name] || name == retainedStandby || seen[name] {
163+
return false
164+
}
165+
seen[name] = true
166+
}
167+
return true
168+
}
169+
138170
// Requiring the four-instance V2 shape lets the physical observer verify the
139171
// spare during migration. Host maintenance still uses the steady-state roster.
140172
func umamiV2Transition(cluster object) bool {

‎scripts/migrate-longhorn-v2/cnpg_observation.go‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ type cnpgObservation struct {
99
Cluster object
1010
Pods []object
1111
States map[string]postgresState
12+
13+
retainedStandby string
1214
}
1315

1416
// validateCNPGObservation requires physical replay and stable process identities.
1517
func validateCNPGObservation(before, after cnpgObservation) error {
1618
refused := errors.New("database identity, process or physical replication changed during observation")
17-
original, err := healthyCNPGPods(before.Cluster, before.Pods)
19+
if before.retainedStandby != after.retainedStandby {
20+
return refused
21+
}
22+
original, err := healthyCNPGPodsWithRetention(before.Cluster, before.Pods, before.retainedStandby)
1823
if err != nil {
1924
return err
2025
}
21-
current, err := healthyCNPGPods(after.Cluster, after.Pods)
26+
current, err := healthyCNPGPodsWithRetention(after.Cluster, after.Pods, after.retainedStandby)
2227
if err != nil {
2328
return err
2429
}

‎scripts/migrate-longhorn-v2/cnpg_observe.go‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77

88
// observeCNPGMigration is a protected read-only step, not cutover clearance.
99
func observeCNPGMigration(ctx context.Context, c client, target, source string) (cnpgObservation, error) {
10+
return observeCNPGMigrationWithRetention(ctx, c, target, source, "")
11+
}
12+
13+
func observeCNPGMigrationWithRetention(ctx context.Context, c client, target, source, retainedStandby string) (cnpgObservation, error) {
1014
var selected cnpgSource
1115
for _, candidate := range cnpgSources() {
1216
if candidate.Name == target {
@@ -19,14 +23,14 @@ func observeCNPGMigration(ctx context.Context, c client, target, source string)
1923
if err := sourceCurrent(ctx, c.run, source); err != nil {
2024
return cnpgObservation{}, err
2125
}
22-
before, err := c.readCNPGObservation(ctx, selected)
26+
before, err := c.readCNPGObservationWithRetention(ctx, selected, retainedStandby)
2327
if err != nil {
2428
return cnpgObservation{}, err
2529
}
2630
if err = c.readCNPGPhysicalState(ctx, &before); err != nil {
2731
return cnpgObservation{}, err
2832
}
29-
after, err := c.readCNPGObservation(ctx, selected)
33+
after, err := c.readCNPGObservationWithRetention(ctx, selected, retainedStandby)
3034
if err != nil {
3135
return cnpgObservation{}, err
3236
}
@@ -35,7 +39,7 @@ func observeCNPGMigration(ctx context.Context, c client, target, source string)
3539
}
3640
// The final whole-cluster read follows the final SQL process bracket. A
3741
// switchover during the last command must not escape through an older read.
38-
final, err := c.readCNPGObservation(ctx, selected)
42+
final, err := c.readCNPGObservationWithRetention(ctx, selected, retainedStandby)
3943
if err != nil {
4044
return cnpgObservation{}, err
4145
}
@@ -51,6 +55,10 @@ func observeCNPGMigration(ctx context.Context, c client, target, source string)
5155

5256
// readCNPGObservation obtains an unfiltered complete namespace Pod inventory.
5357
func (c client) readCNPGObservation(ctx context.Context, selected cnpgSource) (cnpgObservation, error) {
58+
return c.readCNPGObservationWithRetention(ctx, selected, "")
59+
}
60+
61+
func (c client) readCNPGObservationWithRetention(ctx context.Context, selected cnpgSource, retainedStandby string) (cnpgObservation, error) {
5462
cluster, err := c.get(ctx, "cluster.postgresql.cnpg.io", selected.Namespace, selected.Name, false)
5563
if err != nil {
5664
return cnpgObservation{}, err
@@ -63,11 +71,11 @@ func (c client) readCNPGObservation(ctx context.Context, selected cnpgSource) (c
6371
if err != nil {
6472
return cnpgObservation{}, err
6573
}
66-
owned, err := healthyCNPGPods(cluster, pods)
74+
owned, err := healthyCNPGPodsWithRetention(cluster, pods, retainedStandby)
6775
if err != nil {
6876
return cnpgObservation{}, err
6977
}
70-
return cnpgObservation{Cluster: cluster, Pods: owned, States: map[string]postgresState{}}, nil
78+
return cnpgObservation{Cluster: cluster, Pods: owned, States: map[string]postgresState{}, retainedStandby: retainedStandby}, nil
7179
}
7280

7381
// readCNPGPhysicalState captures the primary barrier before the standbys.

‎scripts/migrate-longhorn-v2/cnpg_standby_claim_retention.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,23 @@ func verifyUmamiStandbyClaimRetention(original, retained object) error {
5858
return nil
5959
}
6060

61+
func umamiRetainedClaimMetadata(claim object, name string) bool {
62+
owners := value(claim, "metadata", "ownerReferences")
63+
return text(claim, "metadata", "labels", "cnpg.io/instanceName") == name &&
64+
text(claim, "metadata", "annotations", "cnpg.io/pvcStatus") == "detached" &&
65+
(owners == nil || reflect.DeepEqual(owners, []any{}))
66+
}
67+
6168
// verifyUmamiStandbyDetached accepts the old backend only when its original
6269
// claim still protects it and neither a Pod nor a CSI attachment can use it.
6370
func verifyUmamiStandbyDetached(witness cnpgStandbyCutover, claim, pv, volume object, pods, attachments []object) error {
6471
refused := errors.New("the retained standby storage is not demonstrably detached")
6572
name := witness.Consumer.Name
66-
owners := value(claim, "metadata", "ownerReferences")
6773
robustness := text(volume, "status", "robustness")
6874
if pods == nil || attachments == nil ||
6975
benchmarkIdentity(claim) != witness.Claim || benchmarkIdentity(pv) != witness.PV ||
7076
benchmarkIdentity(volume) != witness.Volume || !verifyUmamiOriginalClaimBinding(claim, pv, volume) ||
71-
text(claim, "metadata", "labels", "cnpg.io/instanceName") != name ||
72-
text(claim, "metadata", "annotations", "cnpg.io/pvcStatus") != "detached" ||
73-
(owners != nil && !reflect.DeepEqual(owners, []any{})) ||
77+
!umamiRetainedClaimMetadata(claim, name) ||
7478
text(volume, "status", "state") != "detached" || text(volume, "status", "currentNodeID") != "" ||
7579
text(volume, "spec", "nodeID") != "" || (robustness != "unknown" && robustness != "healthy") {
7680
return refused

‎scripts/migrate-longhorn-v2/cnpg_standby_replacement.go‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,28 @@ package main
33
import (
44
"context"
55
"errors"
6+
"os"
67
"time"
78
)
89

910
const umamiStandbyReplacementOperation = "replace-umami-v2-standby"
1011

12+
func executeUmamiStandbyReplacement(ctx context.Context, c client, operation string) error {
13+
executor := umamiReplacementOwner{Source: os.Getenv("GITHUB_SHA"), Run: os.Getenv("GITHUB_RUN_ID")}
14+
if operation == resumeUmamiStandbyOperation {
15+
prior, err := authorizeUmamiStandbyResume(os.Getenv)
16+
if err != nil {
17+
return err
18+
}
19+
return resumeUmamiStandbyReplacement(ctx, c, executor, prior)
20+
}
21+
target, spare, err := authorizeUmamiStandbyTarget(os.Getenv, operation)
22+
if err != nil {
23+
return err
24+
}
25+
return runUmamiStandbyReplacement(ctx, c, executor.Source, executor.Run, target, spare)
26+
}
27+
1128
func runUmamiStandbyReplacement(ctx context.Context, c client, source, run, target, spare string) error {
1229
witness, err := observeUmamiStandbyCutover(ctx, c, source, target, spare)
1330
if err != nil {
@@ -36,11 +53,10 @@ func runUmamiStandbyReplacement(ctx context.Context, c client, source, run, targ
3653
if err != nil {
3754
return err
3855
}
39-
original, err := retainUmamiOriginalStandby(ctx, &journal)
40-
if err != nil {
56+
if err = retainUmamiOriginalStandby(ctx, &journal); err != nil {
4157
return err
4258
}
43-
if err = evictUmamiRetainedStandby(ctx, &journal, original); err != nil {
59+
if err = evictUmamiRetainedStandby(ctx, &journal); err != nil {
4460
return err
4561
}
4662
return observeUmamiStandbyRebuild(ctx, &journal)
@@ -119,7 +135,8 @@ func observeUmamiOriginalDetached(ctx context.Context, journal *umamiStandbyRepl
119135
func readUmamiReplacementStorage(ctx context.Context, journal *umamiStandbyReplacementJournal) (cnpgRecoveredStorage, error) {
120136
const target = "umami-db"
121137
c, record := journal.c, journal.record
122-
observation, err := observeCNPGMigration(ctx, c, target, record.Source)
138+
executor := journal.executor()
139+
observation, err := observeCNPGMigration(ctx, c, target, executor.Source)
123140
if err != nil {
124141
return cnpgRecoveredStorage{}, err
125142
}
@@ -147,7 +164,7 @@ func readUmamiReplacementStorage(ctx context.Context, journal *umamiStandbyRepla
147164
if err != nil {
148165
return cnpgRecoveredStorage{}, err
149166
}
150-
activation := temporaryV2Activation{c: c, source: record.Source, owner: record.Run, pools: c.inventoryPools}
167+
activation := temporaryV2Activation{c: c, source: executor.Source, owner: executor.Run, pools: c.inventoryPools}
151168
qualified, err := activation.recoveryStorage(ctx, archive.Plan.Source)
152169
if err != nil || !recoveryJSONEqual(qualified, *archive.Storage) {
153170
return cnpgRecoveredStorage{}, errors.New("the new standby raw storage is unverified")

‎scripts/migrate-longhorn-v2/cnpg_standby_replacement_record.go‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,34 @@ type umamiStandbyReplacementRecord struct {
2222
Primary benchmarkResource
2323
Original cnpgStandbyCutover
2424
OtherPods map[string]benchmarkResource
25-
Replacement *cnpgRecoveredStorage `json:",omitempty"`
25+
Replacement *cnpgRecoveredStorage `json:",omitempty"`
26+
Continuation *umamiReplacementOwner `json:",omitempty"`
2627
}
2728

2829
const umamiStandbyJournalNamespace = "longhorn-system"
2930

31+
type umamiReplacementOwner struct {
32+
Source, Run string
33+
}
34+
3035
type umamiStandbyReplacementJournal struct {
3136
c client
3237
uid string
3338
record umamiStandbyReplacementRecord
39+
resume *umamiReplacementOwner
40+
}
41+
42+
func (j umamiStandbyReplacementJournal) executor() umamiReplacementOwner {
43+
if j.resume != nil {
44+
return *j.resume
45+
}
46+
return umamiReplacementOwner{Source: j.record.Source, Run: j.record.Run}
3447
}
3548

3649
func beginUmamiStandbyReplacement(ctx context.Context, c client, record umamiStandbyReplacementRecord) (umamiStandbyReplacementJournal, error) {
3750
var journal umamiStandbyReplacementJournal
38-
if c.context != "admin@prod" || record.Phase != umamiReplacementPlanned || record.Replacement != nil || !validUmamiOriginalReplacement(record) {
51+
if c.context != "admin@prod" || record.Phase != umamiReplacementPlanned || record.Replacement != nil ||
52+
record.Continuation != nil || !validUmamiOriginalReplacement(record) {
3953
return journal, errors.New("the original Umami standby replacement witness is incomplete")
4054
}
4155
name := umamiStandbyJournalName(record)
@@ -67,6 +81,9 @@ func beginUmamiStandbyReplacement(ctx context.Context, c client, record umamiSta
6781
}
6882

6983
func (j *umamiStandbyReplacementJournal) transition(ctx context.Context, next umamiStandbyReplacementRecord) error {
84+
if next.Phase == umamiReplacementEvictionPending {
85+
next.Continuation = j.resume
86+
}
7087
if err := verifyUmamiReplacementTransition(j.record, next); err != nil {
7188
return err
7289
}
@@ -78,7 +95,7 @@ func (j *umamiStandbyReplacementJournal) transition(ctx context.Context, next um
7895
if err != nil {
7996
return err
8097
}
81-
if err = sourceCurrent(ctx, j.c.run, j.record.Source); err != nil {
98+
if err = sourceCurrent(ctx, j.c.run, j.executor().Source); err != nil {
8299
return err
83100
}
84101
if err = j.c.patch(ctx, "configmap", umamiStandbyJournalNamespace, current, object{
@@ -179,6 +196,15 @@ func verifyUmamiReplacementTransition(previous, next umamiStandbyReplacementReco
179196
if next.Phase != expected {
180197
return refused
181198
}
199+
if next.Continuation != nil && previous.Phase == umamiReplacementClaimRetained {
200+
owner := next.Continuation
201+
if previous.Continuation != nil || !validBenchmarkOwner(owner.Run, owner.Source) ||
202+
owner.Run == previous.Run || owner.Source == previous.Source {
203+
return refused
204+
}
205+
} else if !reflect.DeepEqual(next.Continuation, previous.Continuation) {
206+
return refused
207+
}
182208
if next.Phase == umamiReplacementStandbyRebuilt {
183209
if next.Replacement == nil || !independentUmamiReplacement(next.Original, *next.Replacement) {
184210
return refused

0 commit comments

Comments
 (0)