Merge pull request #653 from seaweedfs/test/store-down-causal-cut

test: the store goes down inside the cut by construction
This commit is contained in:
Ping Qiu authored and GitHub committed 2026-11-30 22:43:48 -08:00
commit 406a2375fd
2 files changed
+144 -20

No files matched your search

@@ -7,11 +7,15 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
)
// The SeaweedFS side down during a cut, then back,
@@ -21,6 +25,13 @@ import (
// records how the cut answers and how much copy-on-write side data the
// stalled cut keeps on the local root. Then the store comes back.
//
// The store goes down inside the cut by construction: a filer front holds
// the reply to the cut's first applied chunk-object create, the store is
// stopped while that reply is held, then the reply is released. The cut
// cannot commit while one of its uploads is unanswered. With
// E1_TEST_SKIP_BOUNDARY=1 the front holds nothing and the test fails as
// inconclusive (the control).
//
// Asserted, in every variant: the store goes down mid-cut (uploads begun,
// HEAD unmoved); the guest keeps writing with no error and every command
// answers within storeDownBound; the cut the outage hit fails; after the
@@ -48,7 +59,13 @@ func TestProcedureStoreDownDuringACut(t *testing.T) {
}
func storeDownDuringACut(t *testing.T, down string, cowBytes int64, cowWindow bool) {
env := startHandoffEnvWith(t, 1, []string{"v2"})
if testListenHost() != "127.0.0.1" {
t.Fatal("the causal filer front 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)
env.volExtra = []string{"--chunk-capture-interval", "0", "--chunk-snapshot-schedule", "off"}
if cowBytes > 0 {
// Not under a temporary directory: the host-limit launch check refuses one.
@@ -65,8 +82,14 @@ func storeDownDuringACut(t *testing.T, down string, cowBytes int64, cowWindow bo
}
holder := env.start(t, "blockvolume-v1", "s1")
env.adopt(t, holder, "s1")
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))
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)
@@ -75,13 +98,46 @@ func storeDownDuringACut(t *testing.T, down string, cowBytes int64, cowWindow bo
}
return hd.Gen
}
// headImage is HEAD's committed image (its current root and position).
// A cut's session begin also writes HEAD (a new generation) before its
// first upload, so the generation alone does not tell a commit apart.
headImage := func() string {
out, _ := env.h.command(t, env.chunkShow()...)
hd, err := parseLayoutHead(out)
if err != nil {
t.Fatal(err)
}
return fmt.Sprintf("%s@%d", hd.Current, hd.Position)
}
// sideBytes sums the copy-on-write side files anywhere under the host's
// root: a store lives in its volume's own directory (<name>-<id8>), and
// its side file is created next to it.
// A census it cannot complete fails the test: only a file removed while
// it is walked (the window closing) is skipped.
sideBytes := func() int64 {
files, _ := filepath.Glob(filepath.Join(env.hosts["s1"].root, "*.cut-cow-*"))
var n int64
for _, f := range files {
if st, err := os.Stat(f); err == nil {
n += st.Size()
err := filepath.WalkDir(env.hosts["s1"].root, func(p string, d fs.DirEntry, err error) error {
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil
}
return err
}
if d.IsDir() || !strings.Contains(d.Name(), ".cut-cow-") {
return nil
}
st, err := os.Stat(p)
if errors.Is(err, fs.ErrNotExist) {
return nil
}
if err != nil {
return err
}
n += st.Size()
return nil
})
if err != nil {
t.Fatalf("copy-on-write side-file census: %v", err)
}
return n
}
@@ -97,8 +153,10 @@ func storeDownDuringACut(t *testing.T, down string, cowBytes int64, cowWindow bo
}
}
// A cut starts; the store goes down once its uploads have begun.
gen, entries := headGen(), f1FilerEntries(t, env)
// A cut starts; the store goes down while the reply to its first applied
// chunk-object create is held, so before the cut can commit.
image := headImage()
reached := boundary.arm("OBJECT", "")
type cutAnswer struct {
rc int
out string
@@ -118,16 +176,32 @@ func storeDownDuringACut(t *testing.T, down string, cowBytes int64, cowWindow bo
}
answered <- cutAnswer{rc, out + errOut, time.Since(cutStart)}
}()
t.Cleanup(func() { cutCancel(); <-cutDone })
for end := time.Now().Add(10 * time.Second); f1FilerEntries(t, env) <= entries; time.Sleep(20 * time.Millisecond) {
if time.Now().After(end) {
t.Fatalf("%s: the cut uploaded nothing within 10s: the store would not go down mid-cut", down)
t.Cleanup(func() { boundary.unblock(); cutCancel(); <-cutDone })
select {
case <-reached:
case <-time.After(30 * time.Second):
t.Fatalf("%s: inconclusive: the cut applied no chunk-object create within 30s, so the store would not go down mid-cut", down)
}
if got := headImage(); got != image {
t.Fatalf("%s: the cut committed (HEAD image %s -> %s) while one of its uploads was unanswered", down, image, got)
}
if d := os.Getenv("E1_TEST_DELAY_KILL"); d != "" {
// Control: a kill delayed past the upload's own timeout, so the cut
// gives up while the store is healthy; the check below must call
// the run inconclusive.
if wait, err := time.ParseDuration(d); err == nil {
time.Sleep(wait)
}
}
if g := headGen(); g != gen {
t.Fatalf("%s: the cut committed (HEAD gen %d -> %d) before the store went down: not mid-cut", down, gen, g)
}
env.cluster.stopProc(t, down)
// The outage is inside the cut only if the held upload was still
// waiting when the store went down: a held call whose caller gave up
// (the upload's 30 s context) means the cut failed with the store
// healthy.
if held, why := boundary.stillHeld(); !held {
t.Fatalf("%s: inconclusive: the held upload was not waiting when the store went down (%s)", down, why)
}
boundary.unblock()
downAt := time.Now()
t.Logf("%s killed %s after the cut started", down, downAt.Sub(cutStart).Round(time.Millisecond))
@@ -239,8 +313,18 @@ func storeDownDuringACut(t *testing.T, down string, cowBytes int64, cowWindow bo
// The store comes back.
env.cluster.restartProc(t, down)
backAt := time.Now()
if down == "filer" {
w4WaitTCP(t, env.cluster.filerGRPC, 30*time.Second)
// The store is back when HEAD reads through it again: the filer front
// accepts at once, while the real filer behind it may still be starting.
readable := func(end time.Time) bool { // one read, bounded by the whole wait's deadline
ctx, cancel := context.WithDeadline(context.Background(), end)
defer cancel()
_, _, rc, err := runCLI(ctx, env.h, env.chunkShow()...)
return err == nil && rc == 0
}
for end := time.Now().Add(time.Minute); !readable(end); time.Sleep(time.Second) {
if time.Now().After(end) {
t.Fatalf("%s back: HEAD not readable within 1m of the restart", down)
}
}
stalled := cutWhileDown
if stalled == nil {
@@ -257,7 +341,7 @@ func storeDownDuringACut(t *testing.T, down string, cowBytes int64, cowWindow bo
if stalled.rc == 0 {
t.Fatalf("%s: the cut the outage hit reported success: %s", down, firstLine(stalled.out))
}
gen = headGen()
gen := headGen()
var g uint64
for end := time.Now().Add(2 * time.Minute); ; time.Sleep(2 * time.Second) {
_, _, rc := env.h.run(t, cut...)
@@ -4,6 +4,7 @@ package main_test
import (
"context"
"fmt"
"io"
"net"
"net/http"
@@ -37,7 +38,14 @@ type snapshotFilerBoundary struct {
kind, name string
reached chan struct{}
release chan struct{}
hit bool
// left is closed when the held call returns, by the test's release or
// by its caller giving up (its context done; leftByCaller says which).
left chan struct{}
leftByCaller bool
// heldCtx is the held call's own context: its caller giving up is read
// from it directly, not from the handler having run since.
heldCtx context.Context
hit bool
}
func newSnapshotFilerBoundary(t *testing.T, cluster *w4WeedCluster) *snapshotFilerBoundary {
@@ -88,9 +96,34 @@ func (p *snapshotFilerBoundary) arm(kind, name string) <-chan struct{} {
p.kind, p.name = kind, name
p.reached = make(chan struct{})
p.release = make(chan struct{})
p.left = make(chan struct{})
p.leftByCaller = false
p.heldCtx = nil
p.hit = false
return p.reached
}
// stillHeld reports whether the held call is still waiting (its caller has
// not given up), with why it left otherwise.
func (p *snapshotFilerBoundary) stillHeld() (bool, string) {
p.mu.Lock()
left, ctx := p.left, p.heldCtx
p.mu.Unlock()
if left == nil || ctx == nil {
return false, "nothing was held"
}
// The call's own context first: its caller can give up before the
// handler is scheduled to notice, which would leave left open.
if err := ctx.Err(); err != nil {
return false, fmt.Sprintf("its caller gave up (%v)", err)
}
select {
case <-left:
return false, "it was released"
default:
return true, ""
}
}
func (p *snapshotFilerBoundary) unblock() {
p.mu.Lock()
defer p.mu.Unlock()
@@ -118,13 +151,20 @@ func (p *snapshotFilerBoundary) after(ctx context.Context, dir string, entry *fi
return
}
p.hit = true
p.heldCtx = ctx
close(p.reached)
release := p.release
release, left := p.release, p.left
p.mu.Unlock()
byCaller := false
select {
case <-release:
case <-ctx.Done():
byCaller = true
}
p.mu.Lock()
p.leftByCaller = byCaller
p.mu.Unlock()
close(left)
}
func (p *snapshotFilerBoundary) head(t *testing.T) layout.Head {
t.Helper()