diff --git a/seaweed-block/core/frontend/chunkstore/layout/commit.go b/seaweed-block/core/frontend/chunkstore/layout/commit.go index 4b83d48f97..3afbc378a1 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/commit.go +++ b/seaweed-block/core/frontend/chunkstore/layout/commit.go @@ -23,8 +23,9 @@ var ( // CommitRequest is what a snapshot session commits: the image at // Position, as a manifest, and optionally a snapshot name for it. Prev is -// the manifest of HEAD's current root (empty if none) that the session -// computed what is new against. +// the manifest of HEAD's current root that the session computed what is +// new against: empty when HEAD has no current root, and empty also when +// the current root is that of an empty image. type CommitRequest struct { Session string Prev map[uint64]Ref @@ -113,16 +114,22 @@ func RunCommit(ctx context.Context, st HeadStore, look Lookup, volRoot, vol stri if next, changed, err := h.Commit(me, req.Session, root, req.Position, req.Snapshot); err != nil || !changed { return next, changed, err // refused, or already applied: no lookups } - var base *RootName - if len(req.Prev) > 0 { - rn, _, err := rootOf(req.Prev) + // The base is HEAD's current root. Before any commit there is none + // and the previous manifest must be empty; after one (even of an + // empty image, which is a real root) the previous manifest must + // build to it. + if h.Current == nil { + if len(req.Prev) > 0 { + return Head{}, false, ErrStaleBase + } + } else { + base, _, err := rootOf(req.Prev) if err != nil { return Head{}, false, err } - base = &rn - } - if (base == nil) != (h.Current == nil) || base != nil && *base != *h.Current { - return Head{}, false, ErrStaleBase + if base != *h.Current { + return Head{}, false, ErrStaleBase + } } missing, err := MissingOwn(ctx, look, volRoot, vol, newOwn(vol, req.Prev, req.Next)) if err != nil { diff --git a/seaweed-block/core/frontend/chunkstore/layout/commit_test.go b/seaweed-block/core/frontend/chunkstore/layout/commit_test.go index be58e6d328..5a2119b002 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/commit_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/commit_test.go @@ -200,3 +200,69 @@ func TestRunCommit_InterruptAtEveryStep(t *testing.T) { } }) } + +// An empty image is a real root: after it is published, a commit whose +// previous manifest is empty builds on it (empty -> written, and empty -> +// empty); a previous manifest that is not HEAD's current root is still +// refused. +func TestRunCommit_AfterAnEmptyPublishedRoot(t *testing.T) { + ctx := context.Background() + f := &fakeStore{have: map[string]bool{}} + prepared(t, f, nil) + h := idleHead() + st := &memHead{h: &h} + if _, err := RunBegin(ctx, st, me, "s0"); err != nil { + t.Fatal(err) + } + if _, err := runCommit(st, f, CommitRequest{Session: "s0", Snapshot: "empty"}); err != nil { + t.Fatal(err) + } + if st.h.Current == nil { + t.Fatal("the empty snapshot did not publish its root") + } + if _, err := RunBegin(ctx, st, me, "s1"); err != nil { + t.Fatal(err) + } + if _, err := runCommit(st, f, CommitRequest{Session: "s1", Prev: map[uint64]Ref{}, Position: 1}); err != nil { + t.Fatalf("empty -> empty: %v", err) + } + next := manifest(ref("v1", 1, "x")) + prepared(t, f, next) + if _, err := RunBegin(ctx, st, me, "s2"); err != nil { + t.Fatal(err) + } + stale := manifest(ref("v1", 9, "z")) + if _, err := runCommit(st, f, CommitRequest{Session: "s2", Prev: stale, Next: next, Position: 2}); !errors.Is(err, ErrStaleBase) { + t.Fatalf("a non-empty previous manifest over the empty root: %v; want ErrStaleBase", err) + } + if _, err := runCommit(st, f, CommitRequest{Session: "s2", Next: next, Position: 2, Snapshot: "written"}); err != nil { + t.Fatalf("empty -> written: %v", err) + } + if want, _, _ := rootOf(next); *st.h.Current != want { + t.Fatalf("HEAD current %s; want %s", st.h.Current, want) + } + // Before any commit there is no current root: a non-empty previous + // manifest is stale there too. + h0 := Head{Gen: 1, Epoch: 3, Holder: "srv-a", Activity: Activity{Kind: ActSnap, Session: "s9"}} + if _, err := runCommit(&memHead{h: &h0}, f, CommitRequest{Session: "s9", Prev: next, Next: next, Position: 3}); !errors.Is(err, ErrStaleBase) { + t.Fatalf("a previous manifest before any commit: %v; want ErrStaleBase", err) + } +} + +// Abort after the session committed succeeds without a write and leaves +// the commit in place: its success says only that no activity remains. +func TestAbort_AfterCommitLeavesTheCommit(t *testing.T) { + f, h, req := commitFixture(t) + st := &memHead{h: &h} + if _, err := runCommit(st, f, req); err != nil { + t.Fatal(err) + } + committed := *st.h.Current + before := st.updates + if _, err := RunAbort(context.Background(), st, me, "s1"); err != nil { + t.Fatalf("Abort after the commit: %v", err) + } + if st.updates != before || *st.h.Current != committed || st.h.Snapshots["hourly"].Root != committed { + t.Fatalf("Abort after the commit wrote or changed HEAD: %+v", st.h) + } +} diff --git a/seaweed-block/core/frontend/chunkstore/layout/session_test.go b/seaweed-block/core/frontend/chunkstore/layout/session_test.go index 13d18ced25..c8e89926de 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/session_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/session_test.go @@ -156,14 +156,12 @@ func sessionFixture(t *testing.T) (func() *memVolume, map[uint64][]byte, CommitR data := map[uint64][]byte{0: []byte("a"), 3: []byte("b2"), 10004: []byte("c")} prev := manifest(ref("v1", 0, "a"), ref("v1", 3, "b")) next := manifest(ref("v1", 0, "a"), ref("v1", 3, "b2"), ref("v1", 10004, "c"), ref("src", 7, "d")) + prevData := map[uint64][]byte{0: []byte("a"), 3: []byte("b")} fresh := func() *memVolume { - f := &fakeStore{have: map[string]bool{}} - prevRoot := prepared(t, f, prev) objs := map[string][]byte{} - for p := range f.have { - objs[p] = []byte("x") - } - h := Head{Gen: 7, Epoch: 3, Holder: "srv-a", Activity: Activity{Kind: ActIdle}, Current: &prevRoot, Position: 40} + prevRoot := writeImage(t, objs, prev, prevData) + h := Head{Gen: 7, Epoch: 3, Holder: "srv-a", Activity: Activity{Kind: ActIdle}, Current: &prevRoot, Position: 40, + Snapshots: map[string]Snapshot{"daily": {Root: prevRoot}}} return &memVolume{head: &memHead{h: &h}, objects: objs} } return fresh, data, CommitRequest{Prev: prev, Next: next, Position: 50, Snapshot: "hourly"} @@ -199,6 +197,9 @@ func requireCommitted(t *testing.T, v *memVolume, data map[uint64][]byte, req Co if _, ok := v.objects[path.Join(volRoot, rn.Path())]; !ok { t.Fatal("the committed root is missing") } + if err := reachableMissing(h, v.objects); err != nil { + t.Fatalf("after the session HEAD reaches a missing object: %v", err) + } } // Interrupt at every step of the snapshot transaction: killed at each of @@ -239,15 +240,123 @@ func TestSnapshotSession_InterruptAtEveryStep(t *testing.T) { } } -// requireReachablePresent: every own object HEAD's current root reaches -// exists (checked after a kill, before the restart). +// writeImage stores a manifest's own chunks (with their bytes), its +// segments and its root, and returns the root's name. +func writeImage(t *testing.T, objs map[string][]byte, m map[uint64]Ref, data map[uint64][]byte) RootName { + t.Helper() + root, segs, err := Build(m) + if err != nil { + t.Fatal(err) + } + for i, r := range m { + if r.Owner == "v1" { + objs[path.Join(volRoot, r.Chunk.Path())] = data[i] + } + } + for n, b := range segs { + objs[path.Join(volRoot, n.Path())] = b + } + rn, rb, err := NameRoot(root) + if err != nil { + t.Fatal(err) + } + objs[path.Join(volRoot, rn.Path())] = rb + return rn +} + +// reachableMissing walks every root HEAD names (current and every +// snapshot) through its segments to this volume's chunks, reading the +// stored bytes, and reports the first object that is missing or does not +// hash to its name. +func reachableMissing(h *Head, objs map[string][]byte) error { + roots := map[RootName]bool{} + if h.Current != nil { + roots[*h.Current] = true + } + for _, s := range h.Snapshots { + roots[s.Root] = true + } + get := func(rel string, want Hash) ([]byte, error) { + b, ok := objs[path.Join(volRoot, rel)] + if !ok { + return nil, fmt.Errorf("%s is missing", rel) + } + if Sum(b) != want { + return nil, fmt.Errorf("%s does not hash to its name", rel) + } + return b, nil + } + for rn := range roots { + rb, err := get(rn.Path(), rn.Hash) + if err != nil { + return err + } + root, err := DecodeRoot(rb) + if err != nil { + return err + } + for _, sn := range root.Segments { + sb, err := get(sn.Path(), sn.Hash) + if err != nil { + return err + } + seg, err := DecodeSegment(sb) + if err != nil { + return err + } + for _, r := range seg.Chunks { + if r.Owner != "v1" { + continue // the source's pool holds it + } + if _, err := get(r.Chunk.Path(), r.Chunk.Hash); err != nil { + return err + } + } + } + } + return nil +} + +// requireReachablePresent: every object any root in HEAD reaches in this +// volume exists with its bytes (checked after a kill, before the restart). func requireReachablePresent(t *testing.T, v *memVolume) { t.Helper() - h := v.head.h - if h.Current == nil { - return + if err := reachableMissing(v.head.h, v.objects); err != nil { + t.Fatalf("after the kill HEAD reaches a missing object: %v", err) } - if _, ok := v.objects[path.Join(volRoot, h.Current.Path())]; !ok { - t.Fatalf("HEAD names root %s, which is missing", h.Current) +} + +// The reachability observer's negative controls: a reachable own chunk, +// segment or snapshot root deleted (the current root kept) is reported. +func TestReachableMissing_Controls(t *testing.T) { + fresh, _, req := sessionFixture(t) + if err := reachableMissing(fresh().head.h, fresh().objects); err != nil { + t.Fatalf("control: the fixture itself: %v", err) + } + _, segs, _ := Build(req.Prev) + var seg0 SegName + for n := range segs { + seg0 = n + } + for name, rel := range map[string]string{ + "own chunk": req.Prev[3].Chunk.Path(), + "segment": seg0.Path(), + } { + v := fresh() + delete(v.objects, path.Join(volRoot, rel)) + if err := reachableMissing(v.head.h, v.objects); err == nil { + t.Errorf("%s deleted: not reported", name) + } + } + v := fresh() + other := RootName{Hash: Sum([]byte("missing root"))} + v.head.h.Snapshots["weekly"] = Snapshot{Root: other} + if err := reachableMissing(v.head.h, v.objects); err == nil { + t.Error("a snapshot root that is missing: not reported") + } + v = fresh() + v.objects[path.Join(volRoot, req.Prev[0].Chunk.Path())] = []byte("tampered") + if err := reachableMissing(v.head.h, v.objects); err == nil { + t.Error("a chunk whose bytes do not match its name: not reported") } }