Make power-loss replay verification use a causal cut boundary

This commit is contained in:
pingqiu committed 2026-11-30 02:20:11 -08:00
1 parent 992f702ffe
commit 5c679df1cf
1 file changed
+53 -49
@@ -4,12 +4,16 @@ package main_test
import (
"bytes"
"context"
"fmt"
"os"
"path"
"path/filepath"
"syscall"
"testing"
"time"
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
)
// Power loss on a chunk volume (INV-FE-FLUSH-SYNC-001),
@@ -27,7 +31,7 @@ import (
// 2. image B over the same LBAs, flushed, one more write without a flush,
// then a power cut (image A in HEAD must not show through);
// 3. a new image flushed, a cut started, a power cut while it uploads (the
// delay adapts until chunk objects reached the filer but HEAD did not move).
// first applied object-create reply is held until the holder is killed).
//
// Environment: SWBLOCK_REPLAY_ROOT and SWBLOCK_REPLAY_CUT as for
// TestPowerLossReplayAckedWritesSurvive; CHUNKSTORE_WEED; optional
@@ -40,7 +44,19 @@ func TestPowerLossReplayChunkVolume(t *testing.T) {
if root == "" || cutTool == "" {
t.Fatalf("SWBLOCK_REPLAY_ROOT=%q and SWBLOCK_REPLAY_CUT=%q must be set together", root, cutTool)
}
env := startHandoffEnvWith(t, 1, []string{"v2"})
if testListenHost() != "127.0.0.1" {
t.Fatal("causal filer fixture requires loopback")
}
var boundary *snapshotFilerBoundary
cluster := startWorkspaceWeedWith(t, t.TempDir(), w4WeedOpts{VolumeSizeLimitMB: 256, VolumeMax: 32,
FilerBind: "127.0.0.2", FilerFront: func(c *w4WeedCluster) { boundary = newSnapshotFilerBoundary(t, c) }})
env := startHandoffEnvOnCluster(t, 1, []string{"v2"}, procedureTLSConfig(), false, cluster)
id, err := layout.ParseVolumeID(env.layoutID("v1"))
if err != nil {
t.Fatal(err)
}
boundary.id = id
boundary.dir = path.Join(env.chunkPath, layout.VolumeDir("v1", id))
if d := os.Getenv("SWBLOCK_REPLAY_LOGS"); d != "" {
t.Cleanup(func() { copyLogs(t, env.h.dir, d) })
}
@@ -49,7 +65,7 @@ func TestPowerLossReplayChunkVolume(t *testing.T) {
env.hosts["s1"] = h
env.volExtra = []string{"--chunk-capture-interval", "0", "--chunk-snapshot-schedule", "off"}
cut := []string{"ops", "chunk", "cut", "--volume", "v1", "--status-addr", env.hosts["s1"].status}
verify := []string{"ops", "chunk", "verify", "--volume", "v1", "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath, "--depth", "data", "--timeout", "5m"}
verify := []string{"ops", "chunk", "verify", "--master", env.h.masterAddr, "--volume", "v1", "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath, "--depth", "data", "--timeout", "5m"}
headGen := func() uint64 {
out, _ := env.h.command(t, env.chunkShow()...)
hd, err := parseLayoutHead(out)
@@ -67,13 +83,16 @@ func TestPowerLossReplayChunkVolume(t *testing.T) {
// a new generation (a cut that uploads nothing would leave it). frozenGen,
// when not 0, is the HEAD generation seen while frozen: it must still hold
// after the kill, so no HEAD write from before the freeze landed later.
powerCut := func(phase string, img byte, frozenGen uint64, advance bool) {
powerCut := func(phase string, img byte, frozenGen uint64, advance bool, afterKill func()) {
t.Helper()
if err := holder.proc.Signal(syscall.SIGSTOP); err != nil {
t.Fatalf("%s: freeze the holder: %v", phase, err)
}
runReplayCut(t, cutTool, "mark")
holder.kill(t)
if afterKill != nil {
afterKill()
}
if frozenGen != 0 {
if g := headGen(); g != frozenGen {
t.Fatalf("%s: HEAD moved from gen %d to %d after the freeze: the cut was not in progress at the mark", phase, frozenGen, g)
@@ -99,10 +118,8 @@ func TestPowerLossReplayChunkVolume(t *testing.T) {
// Phase 1: a committed cut before the power cut.
env.writeCutCrashImage(t, 'A')
cutStart := time.Now()
env.h.command(t, cut...)
cutTook := time.Since(cutStart)
powerCut("phase 1 (after a committed cut)", 'A', 0, false)
powerCut("phase 1 (after a committed cut)", 'A', 0, false, nil)
// Phase 2: overwrite with B; then a write the guest never flushed.
env.writeCutCrashImage(t, 'B')
@@ -117,7 +134,7 @@ func TestPowerLossReplayChunkVolume(t *testing.T) {
defer c.close()
c.write(t, extra, cutCrashBlock('x', extra))
}()
powerCut("phase 2 (between a write and its flush)", 'B', 0, true)
powerCut("phase 2 (between a write and its flush)", 'B', 0, true, nil)
func() {
c := dialNVMe(t, env.hosts["s1"].nvme, plannedHandoffNQN)
defer c.close()
@@ -127,50 +144,37 @@ func TestPowerLossReplayChunkVolume(t *testing.T) {
}
}()
// Phase 3: a power cut while a cut uploads.
delay := cutTook / 2
img, landed := byte('B'), false
for attempt := 1; attempt <= cutCrashAttempts && !landed; attempt++ {
img++
env.writeCutCrashImage(t, img)
gen, entries := headGen(), f1FilerEntries(t, env)
done := make(chan int, 1)
go func() {
_, _, code := env.h.run(t, cut...)
done <- code
}()
time.Sleep(delay)
if err := holder.proc.Signal(syscall.SIGSTOP); err != nil {
t.Fatalf("phase 3: freeze the holder: %v", err)
}
time.Sleep(time.Second) // a HEAD write already on the wire lands before the reading
uploaded, after := f1FilerEntries(t, env)-entries, headGen()
switch {
case after != gen:
t.Logf("phase 3 attempt %d: the cut committed before %s (HEAD gen %d -> %d); shorter", attempt, delay, gen, after)
delay /= 2
case uploaded <= 0:
t.Logf("phase 3 attempt %d: nothing uploaded by %s; longer", attempt, delay)
delay *= 2
default:
landed = true
t.Logf("phase 3 attempt %d: frozen at %s mid-upload: %d new filer entries, HEAD still gen %d", attempt, delay, uploaded, gen)
}
if landed {
// Still frozen mid-upload: the power cut starts from here (its
// SIGSTOP is a no-op on a stopped process).
powerCut(fmt.Sprintf("phase 3 attempt %d (cut in progress)", attempt), img, gen, true)
<-done
break
}
if err := holder.proc.Signal(syscall.SIGCONT); err != nil {
t.Fatalf("phase 3: resume: %v", err)
}
// Phase 3: hold the stock filer's first applied object-create answer.
// Begin itself advances HEAD; generation movement is not proof of Commit.
// Observe an open session and unchanged Current at the actual object boundary.
img := byte('C')
env.writeCutCrashImage(t, img)
beforeHead := boundary.head(t)
reached := boundary.arm("OBJECT", "")
ctx, cancel := context.WithTimeout(context.Background(), 150*time.Second)
defer cancel()
done := make(chan struct{})
go func() { defer close(done); _, _, _, _ = runCLI(ctx, env.h, cut...) }()
t.Cleanup(func() { cancel(); boundary.unblock(); <-done })
select {
case <-reached:
case <-time.After(30 * time.Second):
cancel()
boundary.unblock()
<-done
t.Fatal("inconclusive: cut never reached applied object boundary")
}
if !landed {
t.Fatalf("no power cut landed while a cut uploaded, in %d attempts", cutCrashAttempts)
if err := holder.proc.Signal(syscall.SIGSTOP); err != nil {
t.Fatal(err)
}
frozen := boundary.head(t)
if frozen.Activity.Kind != layout.ActSnap || frozen.Activity.Session == "" || frozen.Current == nil || beforeHead.Current == nil || *frozen.Current != *beforeHead.Current {
t.Fatalf("object boundary is not an open unpublished cut: before=%+v frozen=%+v", beforeHead, frozen)
}
t.Logf("phase 3: applied object held, HEAD gen=%d open session=%s Current unchanged", frozen.Gen, frozen.Activity.Session)
// Mark and kill while the holder is stopped, then release the old RPC before
// checking the exact frozen HEAD and restarting from the replayed device.
powerCut("phase 3 (cut in progress)", img, frozen.Gen, true, func() { boundary.unblock(); <-done })
// What the filer holds is the image: a snapshot restored into a new volume.
env.snapshot(t, "s1", "v1", "after-power-cuts")
env.restoreInto(t, "v1", "after-power-cuts", "v2")