From 20a93fc09c98a13521c7b45995a37f956eb45a60 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 25 Nov 2026 17:38:32 -0800 Subject: [PATCH] feat(sw-block): ops failover for standalone; the alpha stack deploys the manual failover sw-block ops failover is the operator's failover of a standalone deployment: one call to blockmaster's admin-only FailbackService over mutual TLS with the expected current line, after the operator confirms the old node is off or cut from its clients. blockmaster decides the move. The alpha stack now deploys what its notes describe: blockmaster serves the failback RPC, and the failback executor (admin client Secret, execution on) runs in kube-system; install applies the CRD and executor, uninstall removes both. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/cmd/sw-block/failover.go | 125 ++++++++++++++++++ seaweed-block/cmd/sw-block/main.go | 5 +- .../deploy/k8s/alpha/block-stack.yaml | 1 + .../deploy/k8s/alpha/failback-executor.yaml | 82 ++++++++++++ seaweed-block/scripts/install-k8s-alpha.sh | 2 + seaweed-block/scripts/run-alpha-app-demo.sh | 2 + seaweed-block/scripts/uninstall-k8s-alpha.sh | 2 + 7 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 seaweed-block/cmd/sw-block/failover.go create mode 100644 seaweed-block/deploy/k8s/alpha/failback-executor.yaml diff --git a/seaweed-block/cmd/sw-block/failover.go b/seaweed-block/cmd/sw-block/failover.go new file mode 100644 index 0000000000..453a344795 --- /dev/null +++ b/seaweed-block/cmd/sw-block/failover.go @@ -0,0 +1,125 @@ +package main + +import ( + "context" + "flag" + "fmt" + "io" + "strconv" + "strings" + "time" + + "github.com/seaweedfs/seaweed-block/core/ops" +) + +const opsFailoverName = "sw-block ops failover" + +// failoverRuntime is blockmaster's admin-only FailbackService, the one call +// the operator's failover makes. +type failoverRuntime interface { + ExecuteFailback(context.Context, ops.FailbackRuntimeRequest) (ops.FailbackRuntimeResult, error) +} + +// opsFailoverRuntimeFactory dials blockmaster with the mutual-TLS client +// identity from SW_BLOCK_TLS_CERT / SW_BLOCK_TLS_KEY / SW_BLOCK_TLS_CA. +var opsFailoverRuntimeFactory = func(master string) failoverRuntime { + return ops.NewGRPCFailbackRuntime(master) +} + +const failoverUsage = `usage: sw-block ops failover --master ADDR --volume V --target R + --target-data A --target-ctrl A --expect-current R@E --confirm-old-node-off V + +The operator's failover of volume V's primary to replica R, for a standalone +(systemd) deployment. blockmaster decides: when the current primary is +unreachable it runs the fencing round (every other replica fenced at E+1) and +moves the line only to a ready replica holding the highest fenced end; a +reachable primary's move is a planned move under its guard. A refusal changes +nothing. + +The fencing round stops the old primary's writes, not its reads: power the +old node off or cut it from its clients first, then confirm with +--confirm-old-node-off V. --expect-current is the line you saw (sw-block ops +volumes); a line that moved since is refused. + +Needs an admin client certificate. Exit: 0 moved; 1 refused or failed; 2 +bad arguments. +` + +func runOpsFailover(args []string, stdout, stderr io.Writer) int { + fs := flag.NewFlagSet(opsFailoverName, flag.ContinueOnError) + fs.SetOutput(stderr) + fs.Usage = func() { fmt.Fprint(stderr, failoverUsage) } + var master, volume, target, data, ctrl, expect, confirm string + var timeout time.Duration + fs.StringVar(&master, "master", "", "blockmaster gRPC address host:port") + fs.StringVar(&volume, "volume", "", "volume id") + fs.StringVar(&target, "target", "", "replica id to promote") + fs.StringVar(&data, "target-data", "", "the target replica's data address host:port") + fs.StringVar(&ctrl, "target-ctrl", "", "the target replica's control address host:port") + fs.StringVar(&expect, "expect-current", "", "the current line, exactly @") + fs.StringVar(&confirm, "confirm-old-node-off", "", "exactly the volume id: the old primary's node is off or cut from its clients") + fs.DurationVar(&timeout, "timeout", 30*time.Second, "bound for the call (the fencing round runs inside it)") + if err := fs.Parse(args); err != nil { + return 2 + } + if fs.NArg() != 0 || master == "" || volume == "" || target == "" || data == "" || ctrl == "" || timeout <= 0 { + fmt.Fprint(stderr, failoverUsage) + return 2 + } + curReplica, curEpoch, ok := parseReplicaAtEpoch(expect) + if !ok { + fmt.Fprintf(stderr, "%s: --expect-current %q is not @\n", opsFailoverName, expect) + return 2 + } + if curReplica == target { + fmt.Fprintf(stderr, "%s: the target %s is the current primary\n", opsFailoverName, target) + return 2 + } + if confirm != volume { + fmt.Fprintf(stderr, "%s: --confirm-old-node-off must be exactly %q (the old node is off or cut from its clients)\n", opsFailoverName, volume) + return 2 + } + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + res, err := opsFailoverRuntimeFactory(master).ExecuteFailback(ctx, ops.FailbackRuntimeRequest{ + VolumeID: volume, + ReplicaID: target, + TargetDataAddr: data, + TargetCtrlAddr: ctrl, + ExpectedCurrentReplicaID: curReplica, + ExpectedCurrentEpoch: curEpoch, + // The operator's assertions the runtime requires; blockmaster's gate + // (the fencing round for an unreachable primary) decides the move. + AckEligible: true, + FrontendFencedBeforeFailback: true, // --confirm-old-node-off + DurableFrontierCovered: true, + NoCrossVolumeIdentityChange: true, + EvidenceRefs: []string{"sw-block ops failover", "operator_confirmed_old_node_off=" + volume}, + }) + if err != nil { + fmt.Fprintf(stdout, "failover volume=%s target=%s expected=%s@%d result=refused\n", volume, target, curReplica, curEpoch) + fmt.Fprintf(stderr, "%s: %v\n", opsFailoverName, err) + return 1 + } + moved := res.FailbackStarted && res.AuthorityEpochAdvanced && res.SinglePrimaryAfterFailback + fmt.Fprintf(stdout, "failover volume=%s target=%s expected=%s@%d result=%s epoch_advanced=%t single_primary=%t publish_target_swapped=%t\n", + volume, target, curReplica, curEpoch, map[bool]string{true: "moved", false: "not_moved"}[moved], + res.AuthorityEpochAdvanced, res.SinglePrimaryAfterFailback, res.PublishTargetSwappedAfterFailback) + if !moved { + return 1 + } + return 0 +} + +// parseReplicaAtEpoch parses "@" with a nonzero epoch. +func parseReplicaAtEpoch(s string) (string, uint64, bool) { + i := strings.LastIndexByte(s, '@') + if i <= 0 || i == len(s)-1 { + return "", 0, false + } + e, err := strconv.ParseUint(s[i+1:], 10, 64) + if err != nil || e == 0 { + return "", 0, false + } + return s[:i], e, true +} diff --git a/seaweed-block/cmd/sw-block/main.go b/seaweed-block/cmd/sw-block/main.go index 81bfc7e5dd..8b4ca9f27f 100644 --- a/seaweed-block/cmd/sw-block/main.go +++ b/seaweed-block/cmd/sw-block/main.go @@ -82,7 +82,7 @@ func run(args []string, stdout, stderr io.Writer) int { return ops.VolumeStatusExitInvalid } if len(args) < 2 { - fmt.Fprintln(stderr, "sw-block: expected subcommand ops status|inventory|list|cluster|volumes|describe|timeline|explain|report|dashboard|generate-helm-values|operator-status|lifecycle-owner|authority-executor|rebuild-target-owner|failback-target-owner|failback-executor|frontend-publication-target-owner|frontend-publication-executor|chunk|volume-unheld|workspace") + fmt.Fprintln(stderr, "sw-block: expected subcommand ops status|inventory|list|cluster|volumes|describe|timeline|explain|report|dashboard|generate-helm-values|operator-status|lifecycle-owner|authority-executor|rebuild-target-owner|failback-target-owner|failback-executor|frontend-publication-target-owner|frontend-publication-executor|chunk|volume-unheld|workspace|failover") usage(stderr) return ops.VolumeStatusExitInvalid } @@ -129,6 +129,8 @@ func run(args []string, stdout, stderr io.Writer) int { return runOpsVolumeUnheld(args[2:], stdout, stderr) case "workspace": return runOpsWorkspace(args[2:], stdout, stderr) + case "failover": + return runOpsFailover(args[2:], stdout, stderr) default: fmt.Fprintf(stderr, "sw-block: unknown ops subcommand %q\n", args[1]) usage(stderr) @@ -2297,6 +2299,7 @@ func usage(w io.Writer) { fmt.Fprintln(w, " sw-block ops chunk verify --volume --chunk-filer [--chunk-path /swblock/chunks] [--depth meta|bindings|data] [--orphans] [--timeout ]") fmt.Fprintln(w, " (read-only; every root HEAD names is checked; exit 0 clean, 3 findings, 1 incomplete; --depth data needs --timeout)") fmt.Fprintln(w, " sw-block ops volume-unheld --path (exit 0: no process on this host holds it; local filesystems only)") + fmt.Fprintln(w, " sw-block ops failover --master --volume --target --target-data --target-ctrl --expect-current @ --confirm-old-node-off ") fmt.Fprintln(w, " sw-block ops workspace force-move --master --volume --target-host --target-data --target-ctrl --request-id --confirm-forced @ [--dry-run]") fmt.Fprintln(w, " sw-block ops workspace lost-range --master --volume [--request-id ] (authority only; no bypass: see `sw-block ops workspace help`)") } diff --git a/seaweed-block/deploy/k8s/alpha/block-stack.yaml b/seaweed-block/deploy/k8s/alpha/block-stack.yaml index 1ea5da978e..dabcb62954 100644 --- a/seaweed-block/deploy/k8s/alpha/block-stack.yaml +++ b/seaweed-block/deploy/k8s/alpha/block-stack.yaml @@ -53,6 +53,7 @@ spec: - "--audit-log=/var/lib/sw-block/audit.log" - "--launcher-master-tls-secret-prefix=sw-block-node-tls" - "--expected-slots-per-volume=__EXPECTED_SLOTS_PER_VOLUME__" + - "--failback-runtime-rpc" ports: - name: grpc containerPort: 9333 diff --git a/seaweed-block/deploy/k8s/alpha/failback-executor.yaml b/seaweed-block/deploy/k8s/alpha/failback-executor.yaml new file mode 100644 index 0000000000..ff5b9a7946 --- /dev/null +++ b/seaweed-block/deploy/k8s/alpha/failback-executor.yaml @@ -0,0 +1,82 @@ +# The admin's manual failover in the alpha stack: the failback executor reads +# SwBlockReplicaFailback objects in kube-system and calls blockmaster's +# admin-only ExecuteFailback over mutual TLS (client Secret +# sw-block-failback-executor-tls from scripts/k8s-alpha-certs.sh). The CRD is +# charts/seaweed-block/crds/swblockreplicafailbacks.block.seaweedfs.com.yaml. +apiVersion: v1 +kind: ServiceAccount +metadata: + name: sw-block-failback-executor + namespace: kube-system +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: sw-block-failback-executor +rules: + - apiGroups: ["block.seaweedfs.com"] + resources: ["swblockreplicafailbacks"] + verbs: ["get", "list", "watch"] + - apiGroups: ["block.seaweedfs.com"] + resources: ["swblockreplicafailbacks/status"] + verbs: ["get", "update", "patch"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: sw-block-failback-executor +subjects: + - kind: ServiceAccount + name: sw-block-failback-executor + namespace: kube-system +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: sw-block-failback-executor +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: sw-block-failback-executor + namespace: kube-system + labels: + app: sw-block-failback-executor +spec: + replicas: 1 + selector: + matchLabels: + app: sw-block-failback-executor + template: + metadata: + labels: + app: sw-block-failback-executor + spec: + serviceAccountName: sw-block-failback-executor + containers: + - name: failback-executor + image: sw-block:local + imagePullPolicy: Never + command: ["/usr/local/bin/sw-block"] + args: + - "ops" + - "failback-executor" + - "--namespace=kube-system" + - "--enable-execution" + - "--execution-policy" + - "--failback-runtime-grpc-addr=blockmaster.kube-system.svc.cluster.local:9333" + - "--interval=5s" + env: + - name: SW_BLOCK_TLS_CERT + value: /etc/sw-block/master-tls/tls.crt + - name: SW_BLOCK_TLS_KEY + value: /etc/sw-block/master-tls/tls.key + - name: SW_BLOCK_TLS_CA + value: /etc/sw-block/master-tls/ca.crt + volumeMounts: + - name: master-tls + mountPath: /etc/sw-block/master-tls + readOnly: true + volumes: + - name: master-tls + secret: + secretName: sw-block-failback-executor-tls diff --git a/seaweed-block/scripts/install-k8s-alpha.sh b/seaweed-block/scripts/install-k8s-alpha.sh index 7e3bbaeba2..bb92c41e96 100755 --- a/seaweed-block/scripts/install-k8s-alpha.sh +++ b/seaweed-block/scripts/install-k8s-alpha.sh @@ -268,6 +268,8 @@ kubectl apply -f "$STACK_RENDERED" | tee "$ARTIFACT_DIR/apply-block-stack.log" kubectl -n kube-system wait --for=condition=available deploy/sw-blockmaster --timeout=120s log "apply CSI components" +kubectl apply -f "$ROOT/charts/seaweed-block/crds/swblockreplicafailbacks.block.seaweedfs.com.yaml" | tee "$ARTIFACT_DIR/apply-failback-crd.log" +kubectl apply -f "$ROOT/deploy/k8s/alpha/failback-executor.yaml" | tee "$ARTIFACT_DIR/apply-failback-executor.log" kubectl apply -f "$ROOT/deploy/k8s/alpha/csi-driver.yaml" | tee "$ARTIFACT_DIR/apply-csidriver.log" kubectl apply -f "$CSI_CONTROLLER_RENDERED" | tee "$ARTIFACT_DIR/apply-csi-controller.log" kubectl apply -f "$CSI_NODE_RENDERED" | tee "$ARTIFACT_DIR/apply-csi-node.log" diff --git a/seaweed-block/scripts/run-alpha-app-demo.sh b/seaweed-block/scripts/run-alpha-app-demo.sh index 082143dca9..967c8624c1 100755 --- a/seaweed-block/scripts/run-alpha-app-demo.sh +++ b/seaweed-block/scripts/run-alpha-app-demo.sh @@ -1545,6 +1545,8 @@ kubectl apply -f "$STACK_RENDERED" | tee "$ARTIFACT_DIR/apply-block-stack.log" kubectl -n kube-system wait --for=condition=available deploy/sw-blockmaster --timeout=120s log "apply CSI manifests" +kubectl apply -f "$ROOT/charts/seaweed-block/crds/swblockreplicafailbacks.block.seaweedfs.com.yaml" | tee "$ARTIFACT_DIR/apply-failback-crd.log" +kubectl apply -f "$ROOT/deploy/k8s/alpha/failback-executor.yaml" | tee "$ARTIFACT_DIR/apply-failback-executor.log" kubectl apply -f "$ROOT/deploy/k8s/alpha/csi-driver.yaml" | tee "$ARTIFACT_DIR/apply-csidriver.log" kubectl apply -f "$CSI_CONTROLLER_RENDERED" | tee "$ARTIFACT_DIR/apply-csi-controller.log" kubectl apply -f "$CSI_NODE_RENDERED" | tee "$ARTIFACT_DIR/apply-csi-node.log" diff --git a/seaweed-block/scripts/uninstall-k8s-alpha.sh b/seaweed-block/scripts/uninstall-k8s-alpha.sh index a30bfe2a4e..ec26e226a1 100755 --- a/seaweed-block/scripts/uninstall-k8s-alpha.sh +++ b/seaweed-block/scripts/uninstall-k8s-alpha.sh @@ -65,6 +65,8 @@ log "delete CSI components" kubectl delete -f "$ROOT/deploy/k8s/alpha/csi-node.yaml" --ignore-not-found=true --wait=true --timeout=120s | tee "$ARTIFACT_DIR/delete-csi-node.log" kubectl delete -f "$ROOT/deploy/k8s/alpha/csi-controller.yaml" --ignore-not-found=true --wait=true --timeout=120s | tee "$ARTIFACT_DIR/delete-csi-controller.log" kubectl delete -f "$ROOT/deploy/k8s/alpha/csi-driver.yaml" --ignore-not-found=true --wait=true --timeout=120s | tee "$ARTIFACT_DIR/delete-csidriver.log" +kubectl delete -f "$ROOT/deploy/k8s/alpha/failback-executor.yaml" --ignore-not-found=true --wait=true --timeout=120s | tee "$ARTIFACT_DIR/delete-failback-executor.log" +kubectl delete -f "$ROOT/charts/seaweed-block/crds/swblockreplicafailbacks.block.seaweedfs.com.yaml" --ignore-not-found=true --wait=true --timeout=120s | tee "$ARTIFACT_DIR/delete-failback-crd.log" kubectl delete -f "$ROOT/deploy/k8s/alpha/rbac.yaml" --ignore-not-found=true --wait=true --timeout=120s | tee "$ARTIFACT_DIR/delete-rbac.log" log "delete alpha StorageClass"