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 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-25 17:38:32 -08:00
1 parent a43c23fe1b
commit 20a93fc09c
7 files changed
+218 -1

No files matched your search

+125
View File
@@ -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 <replica>@<epoch>")
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 <replica>@<epoch>\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 "<replica>@<epoch>" 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
}
+4 -1
View File
@@ -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 <id> --chunk-filer <host:port> [--chunk-path /swblock/chunks] [--depth meta|bindings|data] [--orphans] [--timeout <d>]")
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 <volume-file> (exit 0: no process on this host holds it; local filesystems only)")
fmt.Fprintln(w, " sw-block ops failover --master <addr> --volume <id> --target <replica> --target-data <addr> --target-ctrl <addr> --expect-current <replica>@<epoch> --confirm-old-node-off <volume>")
fmt.Fprintln(w, " sw-block ops workspace force-move --master <addr> --volume <id> --target-host <server-id> --target-data <addr> --target-ctrl <addr> --request-id <id> --confirm-forced <volume>@<epoch> [--dry-run]")
fmt.Fprintln(w, " sw-block ops workspace lost-range --master <addr> --volume <id> [--request-id <id>] (authority only; no bypass: see `sw-block ops workspace help`)")
}
@@ -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
@@ -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
@@ -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"
@@ -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"
@@ -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"