diff --git a/seaweed-block/core/frontend/chunkstore/layout/commit.go b/seaweed-block/core/frontend/chunkstore/layout/commit.go index d202b3783d..0fc572afb6 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/commit.go +++ b/seaweed-block/core/frontend/chunkstore/layout/commit.go @@ -55,6 +55,19 @@ type CommitRequest struct { // lookups unless ObjectsChecked), so no attempt of the commit rebuilds // it. The caller keys it to exactly this request. Plan *CommitPlan + // Delta, when set, is the commit by change: Prev and Next are unused, + // and the plan builds and checks only the changed shards and the root. + Delta *CommitDelta +} + +// CommitDelta is a commit by change (cost table K3): HEAD's current root, +// the previous segment of every changed shard it names, and the changes. +// Base is nil only when HEAD has no current root (BaseRoot is then empty). +type CommitDelta struct { + Base *RootName + BaseRoot Root + Prev map[uint64]Segment // by shard; only the changed shards Base names + Changes map[uint64]*Ref // by chunk index; nil = the index reads as zeros } // CommitRoot is the root a commit of next names. @@ -179,6 +192,9 @@ type changedOwner struct { // the objects to look up (RunCommitPlanned refuses an unchecked request // without them). func PlanCommit(vol string, req CommitRequest, lookups bool) (CommitPlan, error) { + if req.Delta != nil { + return planDelta(vol, *req.Delta, lookups) + } root, segs, err := rootOf(req.Next) if err != nil { return CommitPlan{}, err @@ -229,9 +245,90 @@ func PlanCommit(vol string, req CommitRequest, lookups bool) (CommitPlan, error) return p, nil } +// planDelta is PlanCommit of a commit by change. Its work grows with the +// change: it builds the changed shards' segments and the root +// (BuildDelta), and its lookups are the new chunks of this volume, the new +// segments and the root. An unchanged segment is not looked up: its name is +// in HEAD's current root, as an unchanged chunk's is (newOwn). +func planDelta(vol string, d CommitDelta, lookups bool) (CommitPlan, error) { + if d.Base == nil && len(d.BaseRoot.Segments) > 0 { + return CommitPlan{}, fmt.Errorf("%w: a base root without its name", ErrMalformed) + } + if d.Base != nil { + bn, _, err := NameRoot(d.BaseRoot) + if err != nil { + return CommitPlan{}, err + } + if bn != *d.Base { + return CommitPlan{}, fmt.Errorf("%w: base root %s does not hash to its name %s", ErrMalformed, bn, *d.Base) + } + } + root, segs, err := BuildDelta(d.BaseRoot, d.Prev, d.Changes) + if err != nil { + return CommitPlan{}, err + } + rn, _, err := NameRoot(root) + if err != nil { + return CommitPlan{}, err + } + p := CommitPlan{root: rn, segs: segs, prevEmpty: d.Base == nil, lookups: lookups} + if d.Base != nil { + p.base = *d.Base + } + type key struct { + owner string + id VolumeID + } + first := map[key]uint64{} + var own []uint64 + for i, r := range d.Changes { + if r == nil { + continue + } + if was, ok := d.Prev[Shard(i)].Chunks[i]; ok && was == *r { + continue + } + k := key{r.Owner, r.OwnerID} + if at, ok := first[k]; !ok || i < at { + first[k] = i + } + if r.Owner == vol { + own = append(own, i) + } + } + for k, at := range first { + p.owners = append(p.owners, changedOwner{owner: k.owner, id: k.id, first: at}) + } + sort.Slice(p.owners, func(a, b int) bool { return p.owners[a].first < p.owners[b].first }) + if lookups { + sort.Slice(own, func(a, b int) bool { return own[a] < own[b] }) + for _, i := range own { + p.rels = append(p.rels, d.Changes[i].Chunk.Path()) + } + names := make([]SegName, 0, len(segs)) + for n := range segs { + names = append(names, n) + } + sort.Slice(names, func(a, b int) bool { return names[a].Shard < names[b].Shard }) + for _, n := range names { + p.rels = append(p.rels, n.Path()) + } + p.rels = append(p.rels, rn.Path()) + } + return p, nil +} + // Root is the root the planned commit names. func (p CommitPlan) Root() RootName { return p.root } +// Segments are the segments the planned commit writes before its root, by +// name: every segment of the new root (a full commit), or only the changed +// shards' new ones (a commit by change). +func (p CommitPlan) Segments() map[SegName][]byte { return p.segs } + +// Lookups are the objects the planned commit looks up, in order. +func (p CommitPlan) Lookups() []string { return p.rels } + // foreignAt returns an index of next holding another volume's chunk that // prev does not hold at that index (the smallest, for a stable refusal), // from the plan's changed owners. Another volume is told by identity, name @@ -248,6 +345,16 @@ func (p CommitPlan) foreignAt(vol string, id VolumeID) (uint64, bool) { return 0, false } +// ownerAt is the owner of the changed entries whose smallest index is i. +func (p CommitPlan) ownerAt(i uint64) string { + for _, o := range p.owners { + if o.first == i { + return o.owner + } + } + return "" +} + // RunCommit checks that every object the new root reaches is written by // this session and unmarked (the new chunks of this volume against HEAD's // current root, every segment, the root: see EnsureObject), then commits @@ -323,7 +430,7 @@ func commitChecked(ctx context.Context, h Head, os ObjectStore, vol string, me H return Head{}, false, ErrStaleBase } if i, ok := plan.foreignAt(vol, h.VolumeID); ok { - return Head{}, false, fmt.Errorf("%w: index %d names %s's chunk", ErrForeignRefAdded, i, req.Next[i].Owner) + return Head{}, false, fmt.Errorf("%w: index %d names %s's chunk", ErrForeignRefAdded, i, plan.ownerAt(i)) } if lookups { // A commit that was not checked first looks up every new chunk here, diff --git a/seaweed-block/core/frontend/chunkstore/layout/delta_test.go b/seaweed-block/core/frontend/chunkstore/layout/delta_test.go index 439dc87c19..88656306fc 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/delta_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/delta_test.go @@ -160,3 +160,95 @@ func TestBuildDeltaRefusesAWrongPreviousSegment(t *testing.T) { t.Fatalf("the base's own segment: %v", err) } } + +// A commit by change plans what the full commit plans, and looks up less: +// the same root, base and changed owners, and the full plan's lookups +// without the unchanged segments (the new chunks of this volume, the new +// segments, the root, in the same order). +func TestPlanDeltaMatchesTheFullPlan(t *testing.T) { + rng := rand.New(rand.NewSource(2)) + for round := 0; round < 200; round++ { + prev := map[uint64]Ref{} + for k := 0; k < 1+rng.Intn(30); k++ { + i := uint64(rng.Intn(5))*ShardSize + uint64(rng.Intn(40)) + prev[i] = deltaRef("v", i, 0) + } + changes := map[uint64]*Ref{} + for k := 0; k < 1+rng.Intn(6); k++ { + i := uint64(rng.Intn(5))*ShardSize + uint64(rng.Intn(40)) + switch rng.Intn(4) { + case 0: + changes[i] = nil + case 1: + r := deltaRef("src", i, round) + changes[i] = &r + case 2: + if r, ok := prev[i]; ok { + changes[i] = &r // rewritten with the same reference + continue + } + fallthrough + default: + r := deltaRef("v", i, 1+round) + changes[i] = &r + } + } + next := applyAll(prev, changes) + full, err := PlanCommit("v", CommitRequest{Prev: prev, Next: next}, true) + if err != nil { + t.Fatal(err) + } + base, baseSegs, err := Build(prev) + if err != nil { + t.Fatal(err) + } + bn, _, err := NameRoot(base) + if err != nil { + t.Fatal(err) + } + d := CommitDelta{Base: &bn, BaseRoot: base, Prev: prevSegsOf(t, base, baseSegs, changes), Changes: changes} + got, err := PlanCommit("v", CommitRequest{Delta: &d}, true) + if err != nil { + t.Fatalf("round %d: %v", round, err) + } + if got.root != full.root || got.base != full.base || got.prevEmpty != full.prevEmpty { + t.Fatalf("round %d: delta plan root/base %s/%s, full %s/%s", round, got.root, got.base, full.root, full.base) + } + if fmt.Sprint(got.owners) != fmt.Sprint(full.owners) { + t.Fatalf("round %d: owners %v, full %v", round, got.owners, full.owners) + } + unchanged := map[string]bool{} + for _, n := range base.Segments { + unchanged[n.Path()] = true + } + var want []string + for _, rel := range full.rels { + if !unchanged[rel] { + want = append(want, rel) + } + } + if fmt.Sprint(got.rels) != fmt.Sprint(want) { + t.Fatalf("round %d: lookups %v, want %v", round, got.rels, want) + } + } +} + +// A delta whose base root does not hash to its name, or a base root with no +// name, is refused before anything is built. +func TestPlanDeltaRefusesAnUnnamedOrMisnamedBase(t *testing.T) { + a := deltaRef("v", 1, 0) + base, _, err := Build(map[uint64]Ref{1: a}) + if err != nil { + t.Fatal(err) + } + wrong := RootName{Hash: Sum([]byte("another root"))} + r := deltaRef("v", 2, 1) + for name, d := range map[string]CommitDelta{ + "misnamed": {Base: &wrong, BaseRoot: base, Changes: map[uint64]*Ref{ShardSize: &r}}, + "unnamed": {BaseRoot: base, Changes: map[uint64]*Ref{ShardSize: &r}}, + } { + if _, err := PlanCommit("v", CommitRequest{Delta: &d}, true); !errors.Is(err, ErrMalformed) { + t.Fatalf("%s: %v, want ErrMalformed", name, err) + } + } +}