mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
fix(layout): plan a commit once; the HEAD update's step does constant work
A 1 TiB commit (262,144 chunks) spent 2.47 s inside the 2 s HEAD update re-deriving the base root and re-scanning the manifests, and its retry did it again. PlanCommit now computes everything a commit needs from the request alone (root, segments, base root, the owners of the changed entries for the foreign-reference check, and the objects to look up) before the update; the update step only checks the replay, the base, the owners and writes. CheckCommit plans on the cut's budget and CASHead reuses that plan and the passed check on every attempt of the same open session (matched by manifest equality); the check is no longer dropped before the commit, so a re-proposal after an unknown outcome looks up nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
de64d1a655
commit
4fdecba380
5 files changed
+388
-67
No files matched your search
@@ -51,6 +51,10 @@ type CommitRequest struct {
|
||||
// session and root, in the same open session: RunCommit then skips the
|
||||
// object lookups. The caller keys it to exactly (session, root).
|
||||
ObjectsChecked bool
|
||||
// Plan, when set, is PlanCommit of this request built beforehand (with
|
||||
// lookups unless ObjectsChecked), so no attempt of the commit rebuilds
|
||||
// it. The caller keys it to exactly this request.
|
||||
Plan *CommitPlan
|
||||
}
|
||||
|
||||
// CommitRoot is the root a commit of next names.
|
||||
@@ -82,22 +86,6 @@ func newOwn(vol string, prev, next map[uint64]Ref) map[uint64]Ref {
|
||||
return out
|
||||
}
|
||||
|
||||
// addedForeign returns an index of next holding another volume's chunk
|
||||
// that prev does not hold at that index (the smallest, for a stable
|
||||
// refusal). Another volume is told by identity, name and id: a volume
|
||||
// re-created under this name is another volume.
|
||||
func addedForeign(vol string, id VolumeID, prev, next map[uint64]Ref) (uint64, bool) {
|
||||
var found bool
|
||||
var at uint64
|
||||
for i, r := range next {
|
||||
own := r.Owner == vol && r.OwnerID == id
|
||||
if !own && prev[i] != r && (!found || i < at) {
|
||||
found, at = true, i
|
||||
}
|
||||
}
|
||||
return at, found
|
||||
}
|
||||
|
||||
// Commit is transition 4 as a pure step on HEAD: for the holder at HEAD's
|
||||
// epoch with snap(session) open, current = root, position, the optional
|
||||
// snapshot added, activity idle, and state live or detached. Detached is the
|
||||
@@ -163,6 +151,102 @@ func (h Head) Commit(me HolderID, session string, root RootName, position uint64
|
||||
return next, true, nil
|
||||
}
|
||||
|
||||
// CommitPlan is what a commit of a request computes from the request
|
||||
// alone: the new root and its segments, the root of Prev, the owners of the
|
||||
// entries that changed (for the foreign-reference check), and, when the
|
||||
// object lookups run, the objects to look up in order. It is built once per
|
||||
// request, outside the conditional HEAD update, so that update's step does no
|
||||
// work that grows with the chunk count, on any attempt.
|
||||
type CommitPlan struct {
|
||||
root RootName
|
||||
segs map[SegName][]byte
|
||||
base RootName
|
||||
prevEmpty bool
|
||||
owners []changedOwner
|
||||
lookups bool
|
||||
rels []string
|
||||
}
|
||||
|
||||
// changedOwner is one owner of the entries of Next that differ from Prev at
|
||||
// the same index, with the smallest such index it holds.
|
||||
type changedOwner struct {
|
||||
owner string
|
||||
id VolumeID
|
||||
first uint64
|
||||
}
|
||||
|
||||
// PlanCommit builds req's plan for vol; lookups says whether the plan carries
|
||||
// the objects to look up (RunCommitPlanned refuses an unchecked request
|
||||
// without them).
|
||||
func PlanCommit(vol string, req CommitRequest, lookups bool) (CommitPlan, error) {
|
||||
root, segs, err := rootOf(req.Next)
|
||||
if err != nil {
|
||||
return CommitPlan{}, err
|
||||
}
|
||||
base, _, err := rootOf(req.Prev)
|
||||
if err != nil {
|
||||
return CommitPlan{}, err
|
||||
}
|
||||
p := CommitPlan{root: root, segs: segs, base: base, prevEmpty: len(req.Prev) == 0, lookups: lookups}
|
||||
type key struct {
|
||||
owner string
|
||||
id VolumeID
|
||||
}
|
||||
first := map[key]uint64{}
|
||||
for i, r := range req.Next {
|
||||
if req.Prev[i] == r {
|
||||
continue
|
||||
}
|
||||
k := key{r.Owner, r.OwnerID}
|
||||
if at, ok := first[k]; !ok || i < at {
|
||||
first[k] = 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 {
|
||||
chunks := newOwn(vol, req.Prev, req.Next)
|
||||
idx := make([]uint64, 0, len(chunks))
|
||||
for i := range chunks {
|
||||
idx = append(idx, i)
|
||||
}
|
||||
sort.Slice(idx, func(a, b int) bool { return idx[a] < idx[b] })
|
||||
for _, i := range idx {
|
||||
p.rels = append(p.rels, chunks[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, root.Path())
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Root is the root the planned commit names.
|
||||
func (p CommitPlan) Root() RootName { return p.root }
|
||||
|
||||
// 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
|
||||
// and id: a volume re-created under this name is another volume.
|
||||
func (p CommitPlan) foreignAt(vol string, id VolumeID) (uint64, bool) {
|
||||
var found bool
|
||||
var at uint64
|
||||
for _, o := range p.owners {
|
||||
if !(o.owner == vol && o.id == id) && (!found || o.first < at) {
|
||||
found, at = true, o.first
|
||||
}
|
||||
}
|
||||
return at, found
|
||||
}
|
||||
|
||||
// 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
|
||||
@@ -173,33 +257,47 @@ func (h Head) Commit(me HolderID, session string, root RootName, position uint64
|
||||
// reads HEAD after an unknown outcome: detached at its claim means stopped;
|
||||
// it never resumes serving without that read.
|
||||
func RunCommit(ctx context.Context, st HeadStore, os ObjectStore, vol string, me HolderID, req CommitRequest, cause HeadWriteCause) (Head, error) {
|
||||
root, segs, err := rootOf(req.Next)
|
||||
plan, err := req.planned(vol, !req.ObjectsChecked)
|
||||
if err != nil {
|
||||
return Head{}, err
|
||||
}
|
||||
return runTransition(ctx, st, cause, func(h Head) (Head, bool, error) {
|
||||
return commitChecked(ctx, h, os, vol, me, req, root, segs, !req.ObjectsChecked)
|
||||
return commitChecked(ctx, h, os, vol, me, req, plan, !req.ObjectsChecked)
|
||||
})
|
||||
}
|
||||
|
||||
// planned returns req's plan: req.Plan when set, else built here. A plan
|
||||
// without its object lookups is refused when lookups must run.
|
||||
func (req CommitRequest) planned(vol string, lookups bool) (CommitPlan, error) {
|
||||
if req.Plan == nil {
|
||||
return PlanCommit(vol, req, lookups)
|
||||
}
|
||||
if lookups && !req.Plan.lookups {
|
||||
return CommitPlan{}, errors.New("layout: an unchecked commit needs a plan with its object lookups")
|
||||
}
|
||||
return *req.Plan, nil
|
||||
}
|
||||
|
||||
// CheckCommitObjects runs RunCommit's checks of req against h, the object
|
||||
// lookups included, without the update. It is run in the open session after
|
||||
// its last upload, with the caller's budget rather than the HEAD update's:
|
||||
// no cleaning can begin while the session is open, so what it found holds
|
||||
// until that session's commit. A commit already applied passes.
|
||||
func CheckCommitObjects(ctx context.Context, h Head, os ObjectStore, vol string, me HolderID, req CommitRequest) error {
|
||||
root, segs, err := rootOf(req.Next)
|
||||
plan, err := req.planned(vol, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, _, err = commitChecked(ctx, h, os, vol, me, req, root, segs, true)
|
||||
_, _, err = commitChecked(ctx, h, os, vol, me, req, plan, true)
|
||||
return err
|
||||
}
|
||||
|
||||
// commitChecked is the commit of req against h after its checks; lookups
|
||||
// says whether the object lookups run.
|
||||
func commitChecked(ctx context.Context, h Head, os ObjectStore, vol string, me HolderID, req CommitRequest, root RootName, segs map[SegName][]byte, lookups bool) (Head, bool, error) {
|
||||
if next, changed, err := h.Commit(me, req.Session, root, req.Position, req.Snapshot, req.Meta, req.State); err != nil || !changed {
|
||||
// says whether the object lookups run. Apart from those lookups, its work
|
||||
// does not grow with the chunk count: everything computed from the request
|
||||
// alone is in plan.
|
||||
func commitChecked(ctx context.Context, h Head, os ObjectStore, vol string, me HolderID, req CommitRequest, plan CommitPlan, lookups bool) (Head, bool, error) {
|
||||
if next, changed, err := h.Commit(me, req.Session, plan.root, req.Position, req.Snapshot, req.Meta, req.State); err != nil || !changed {
|
||||
return next, changed, err // refused, or already applied: no lookups
|
||||
}
|
||||
// The base is HEAD's current root. Before any commit there is none
|
||||
@@ -207,46 +305,21 @@ func commitChecked(ctx context.Context, h Head, os ObjectStore, vol string, me H
|
||||
// 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
|
||||
}
|
||||
if base != *h.Current {
|
||||
if !plan.prevEmpty {
|
||||
return Head{}, false, ErrStaleBase
|
||||
}
|
||||
} else if plan.base != *h.Current {
|
||||
return Head{}, false, ErrStaleBase
|
||||
}
|
||||
if i, ok := addedForeign(vol, h.VolumeID, req.Prev, req.Next); ok {
|
||||
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)
|
||||
}
|
||||
var rels []string
|
||||
chunks := newOwn(vol, req.Prev, req.Next)
|
||||
idx := make([]uint64, 0, len(chunks))
|
||||
for i := range chunks {
|
||||
idx = append(idx, i)
|
||||
}
|
||||
sort.Slice(idx, func(a, b int) bool { return idx[a] < idx[b] })
|
||||
for _, i := range idx {
|
||||
rels = append(rels, chunks[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 {
|
||||
rels = append(rels, n.Path())
|
||||
}
|
||||
rels = append(rels, root.Path())
|
||||
if lookups {
|
||||
if err := allOwnedBySession(ctx, os, rels, req.Session); err != nil {
|
||||
if err := allOwnedBySession(ctx, os, plan.rels, req.Session); err != nil {
|
||||
return Head{}, false, err
|
||||
}
|
||||
}
|
||||
return h.Commit(me, req.Session, root, req.Position, req.Snapshot, req.Meta, req.State)
|
||||
return h.Commit(me, req.Session, plan.root, req.Position, req.Snapshot, req.Meta, req.State)
|
||||
}
|
||||
|
||||
// commitVerifyParallel bounds the object lookups one commit runs at once.
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// oneTiBChunks is the chunk count of a 1 TiB volume (4 MiB chunks).
|
||||
const oneTiBChunks = 262144
|
||||
|
||||
// firstCommitNoObjects is a first commit of n new chunks of volume v1 whose
|
||||
// object check passed (ObjectsChecked): no object is stored, because none is
|
||||
// looked up.
|
||||
func firstCommitNoObjects(n int, session string) (*memHead, CommitRequest) {
|
||||
next := make(map[uint64]Ref, n)
|
||||
for i := 0; i < n; i++ {
|
||||
next[uint64(i)] = ref("v1", uint64(i), fmt.Sprintf("c%d", i))
|
||||
}
|
||||
h := Head{Gen: 1, Epoch: 3, Holder: "srv-a", ClaimID: "k-a", VolumeID: idFor("v1"), Activity: Activity{Kind: ActSnap, Session: session}}
|
||||
return &memHead{h: &h}, CommitRequest{Session: session, Next: next, Position: 10, ObjectsChecked: true}
|
||||
}
|
||||
|
||||
// The conditional HEAD update's step does no work that grows with the chunk
|
||||
// count: everything computed from the request is planned before the HEAD
|
||||
// read. A 1 TiB commit's update step spent 2.47 s re-deriving the base root
|
||||
// and re-scanning the manifests inside the 2 s budget, and its retry did it
|
||||
// again. The test changes both manifests after the HEAD read and before the
|
||||
// update: a step that reads them again sees a different base (refused); a
|
||||
// planned step is unaffected. It also bounds the time between the read and
|
||||
// the update at this size.
|
||||
func TestCommitUpdateStepDoesNotRereadTheManifests(t *testing.T) {
|
||||
st, req := firstCommitNoObjects(oneTiBChunks, "s1")
|
||||
var readAt, updateAt time.Time
|
||||
st.afterRead = func() {
|
||||
readAt = time.Now()
|
||||
req.Prev[1<<40] = ref("v1", 1<<40, "planted after the HEAD read") // Prev was empty when planned
|
||||
for i := range req.Next {
|
||||
delete(req.Next, i)
|
||||
}
|
||||
}
|
||||
st.hook = func() { updateAt = time.Now() }
|
||||
req.Prev = map[uint64]Ref{}
|
||||
got, err := RunCommit(context.Background(), st, nil, "v1", me, req, CauseCommit)
|
||||
if err != nil {
|
||||
t.Fatalf("commit whose manifests changed after the HEAD read: %v (the update step re-read them)", err)
|
||||
}
|
||||
if got.Current == nil {
|
||||
t.Fatal("commit applied no root")
|
||||
}
|
||||
if window := updateAt.Sub(readAt); updateAt.IsZero() || window > 250*time.Millisecond {
|
||||
t.Fatalf("update step of a %d-chunk commit took %v between the HEAD read and the update, want under 250ms", oneTiBChunks, window)
|
||||
}
|
||||
}
|
||||
|
||||
// unknownOnce applies the first HEAD update and then reports an unknown
|
||||
// outcome, as a write whose reply is lost.
|
||||
type unknownOnce struct {
|
||||
*memHead
|
||||
mu sync.Mutex
|
||||
done bool
|
||||
}
|
||||
|
||||
var errUnknownOutcome = errors.New("update outcome unknown: reply lost")
|
||||
|
||||
func (u *unknownOnce) UpdateHead(ctx context.Context, gen uint64, next Head, cause HeadWriteCause) error {
|
||||
if err := u.memHead.UpdateHead(ctx, gen, next, cause); err != nil {
|
||||
return err
|
||||
}
|
||||
u.mu.Lock()
|
||||
defer u.mu.Unlock()
|
||||
if !u.done {
|
||||
u.done = true
|
||||
return errUnknownOutcome
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// countingObjects counts every lookup.
|
||||
type countingObjects struct {
|
||||
*memObjects
|
||||
mu sync.Mutex
|
||||
stats int
|
||||
}
|
||||
|
||||
func (c *countingObjects) Stat(ctx context.Context, rel string) (ObjectMeta, bool, error) {
|
||||
c.mu.Lock()
|
||||
c.stats++
|
||||
c.mu.Unlock()
|
||||
return c.memObjects.Stat(ctx, rel)
|
||||
}
|
||||
|
||||
// An unknown outcome of a 1 TiB commit is resolved by reading HEAD, never by
|
||||
// object lookups: the retry finds the commit applied and returns, even when
|
||||
// it no longer carries the passed check.
|
||||
func TestUnknownCommitOutcomeAtOneTiBResolvesWithoutLookups(t *testing.T) {
|
||||
mh, req := firstCommitNoObjects(oneTiBChunks, "s1")
|
||||
st := &unknownOnce{memHead: mh}
|
||||
objs := &countingObjects{memObjects: newMemObjects(volRoot)}
|
||||
ctx := context.Background()
|
||||
if _, err := RunCommit(ctx, st, objs, "v1", me, req, CauseCommit); !errors.Is(err, errUnknownOutcome) {
|
||||
t.Fatalf("first attempt: %v, want the unknown outcome", err)
|
||||
}
|
||||
req.ObjectsChecked = false // the retry may have lost the check
|
||||
start := time.Now()
|
||||
got, err := RunCommit(ctx, st, objs, "v1", me, req, CauseCommit)
|
||||
if err != nil {
|
||||
t.Fatalf("retry after the unknown outcome: %v", err)
|
||||
}
|
||||
if objs.stats != 0 {
|
||||
t.Fatalf("retry of an applied commit ran %d object lookups, want 0", objs.stats)
|
||||
}
|
||||
if got.Current == nil || got.Activity.Kind != ActIdle {
|
||||
t.Fatalf("retry returned HEAD %+v, want the applied commit", got)
|
||||
}
|
||||
t.Logf("retry of a %d-chunk applied commit took %v", oneTiBChunks, time.Since(start).Round(time.Millisecond))
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package chunkstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/storage"
|
||||
)
|
||||
|
||||
// c1Layer is c1Cut's setup without its cut: a fresh volume with one block
|
||||
// written in every chunk, its chunk layer over a c1Remote (the real
|
||||
// pre-check), and pool lookups made inside CASHead counted.
|
||||
func c1Layer(t *testing.T) (*layoutHost, *c1Remote, storage.ChunkLayer) {
|
||||
t.Helper()
|
||||
lh := newLayoutHost(t)
|
||||
s, err := storage.CreateWALStoreWithOptions(filepath.Join(lh.dir, "a"), lrBlocks, 4096, storage.WALStoreOptions{ChunkBlocks: lrChunkBlocks})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.Recover(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r, err := NewLayoutRemote(lh.cat, lh.v, lh.me)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := &c1Remote{LayoutRemote: r, ff: lh.ff}
|
||||
l, err := storage.AttachChunkLayer(s, w, storage.ChunkLayerConfig{ChunkBlocks: lrChunkBlocks, Epoch: lh.me.Epoch, Holder: lh.me.Holder, FreshFromHead: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = l.Close(); _ = s.Close() })
|
||||
blocks := map[uint32][]byte{}
|
||||
for c := uint32(0); c < lrBlocks/lrChunkBlocks; c++ {
|
||||
blocks[c*lrChunkBlocks] = block(byte(c + 1))
|
||||
}
|
||||
writeSync(t, s, blocks)
|
||||
lh.ff.afterLookup = func(p string) {
|
||||
if strings.Contains(p, "/pool/") && w.inCAS.Load() {
|
||||
w.casGot.Add(1)
|
||||
}
|
||||
}
|
||||
return lh, w, l
|
||||
}
|
||||
|
||||
// A HEAD update whose outcome is unknown is proposed again in the same open
|
||||
// session; that attempt keeps the passed object check and its plan, so it
|
||||
// looks up no chunk. Clearing the check before the commit made every
|
||||
// re-proposal of a 1 TiB cut redo all its lookups inside the HEAD update's
|
||||
// budget, so the commit never finished.
|
||||
func TestReproposalAfterAnUnknownCommitOutcomeLooksUpNothing(t *testing.T) {
|
||||
lh, w, l := c1Layer(t)
|
||||
var lost atomic.Bool
|
||||
lh.ff.failUpdate = func(p string) error { // under ff.mu: the update is lost before it applies
|
||||
if strings.HasSuffix(p, "/HEAD") && w.inCAS.Load() && !lost.Load() {
|
||||
lost.Store(true)
|
||||
return errors.New("connection reset: the update's reply is lost")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if _, err := l.Cut(context.Background()); err == nil {
|
||||
t.Fatal("setup: the cut whose HEAD update was lost reported success")
|
||||
}
|
||||
if !lost.Load() {
|
||||
t.Fatal("setup: no HEAD update was lost")
|
||||
}
|
||||
if n := w.casGot.Load(); n != 0 {
|
||||
t.Fatalf("setup: the first CAS looked up %d chunks after a passed pre-check", n)
|
||||
}
|
||||
if _, err := l.Cut(context.Background()); err != nil {
|
||||
t.Fatalf("the re-proposal after the unknown outcome: %v", err)
|
||||
}
|
||||
if n := w.casGot.Load(); n != 0 {
|
||||
t.Fatalf("the re-proposal in the same session looked up %d chunks; the passed check was dropped", n)
|
||||
}
|
||||
if lh.head().Current == nil {
|
||||
t.Fatal("no commit landed")
|
||||
}
|
||||
}
|
||||
@@ -58,12 +58,16 @@ type LayoutRemote struct {
|
||||
// abandoned is set by the engine when a cut ends without a commit
|
||||
// (Abort): the next call drops the session and the pending name.
|
||||
abandoned atomic.Bool
|
||||
// checked is the (session, root) whose commit's object check passed
|
||||
// (CheckCommit), in that open session: CASHead skips the lookups only for
|
||||
// exactly that pair. A new session has a new id, so it never matches.
|
||||
// checked is the commit whose object check passed (CheckCommit), in
|
||||
// that open session: its session, root, manifests and plan. CASHead
|
||||
// skips the lookups, and reuses the plan, only for a request of exactly
|
||||
// that session and those manifests, on every attempt of that session. A
|
||||
// new session has a new id, so it never matches.
|
||||
checked struct {
|
||||
session string
|
||||
root layout.RootName
|
||||
session string
|
||||
root layout.RootName
|
||||
prev, next map[uint64]layout.Ref
|
||||
plan layout.CommitPlan
|
||||
}
|
||||
pending *pendingSnapshot
|
||||
// snapErr is why the last commit could not name its snapshot (the cut
|
||||
@@ -375,15 +379,16 @@ func (l *LayoutRemote) prepareCommitLocked(ctx context.Context, expected storage
|
||||
func (l *LayoutRemote) CheckCommit(ctx context.Context, expected storage.ChunkHeadVersion, next storage.ChunkHead) error {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
l.checked.session, l.checked.root = "", layout.RootName{}
|
||||
l.clearCheckedLocked()
|
||||
h, req, err := l.prepareCommitLocked(ctx, expected, next)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
root, err := layout.CommitRoot(req.Next)
|
||||
plan, err := layout.PlanCommit(l.vol.Name, req, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Plan = &plan
|
||||
if err := layout.CheckCommitObjects(ctx, h, l.st, l.vol.Name, l.me, req); err != nil {
|
||||
if errors.Is(err, layout.ErrNotPrepared) {
|
||||
l.forceView = true // as CASHead: the re-read drops what the ended session staged
|
||||
@@ -391,10 +396,41 @@ func (l *LayoutRemote) CheckCommit(ctx context.Context, expected storage.ChunkHe
|
||||
}
|
||||
return layoutRemoteErr(err)
|
||||
}
|
||||
l.checked.session, l.checked.root = req.Session, root
|
||||
l.checked.session, l.checked.root = req.Session, plan.Root()
|
||||
l.checked.prev, l.checked.next, l.checked.plan = req.Prev, req.Next, plan
|
||||
return nil
|
||||
}
|
||||
|
||||
// clearCheckedLocked forgets the checked commit.
|
||||
func (l *LayoutRemote) clearCheckedLocked() {
|
||||
l.checked.session, l.checked.root = "", layout.RootName{}
|
||||
l.checked.prev, l.checked.next, l.checked.plan = nil, nil, layout.CommitPlan{}
|
||||
}
|
||||
|
||||
// checkedPlanLocked returns the checked commit's plan when req is exactly
|
||||
// that commit: the same open session and the same manifests (so the same
|
||||
// root and the same base).
|
||||
func (l *LayoutRemote) checkedPlanLocked(req layout.CommitRequest) (layout.CommitPlan, bool) {
|
||||
c := &l.checked
|
||||
if c.session == "" || c.session != req.Session || !sameRefs(c.prev, req.Prev) || !sameRefs(c.next, req.Next) {
|
||||
return layout.CommitPlan{}, false
|
||||
}
|
||||
return c.plan, true
|
||||
}
|
||||
|
||||
// sameRefs reports whether two manifests hold the same references.
|
||||
func sameRefs(a, b map[uint64]layout.Ref) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i, r := range a {
|
||||
if o, ok := b[i]; !ok || o != r {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (l *LayoutRemote) CASHead(ctx context.Context, expected storage.ChunkHeadVersion, next storage.ChunkHead) (uint64, error) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
@@ -402,10 +438,16 @@ func (l *LayoutRemote) CASHead(ctx context.Context, expected storage.ChunkHeadVe
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if root, rerr := layout.CommitRoot(req.Next); rerr == nil && l.checked.session != "" && l.checked.session == req.Session && l.checked.root == root {
|
||||
// The plan of the checked commit is reused on every attempt of this
|
||||
// session, so no attempt rebuilds the root or redoes the lookups inside
|
||||
// the HEAD update's budget; any other request is planned with lookups.
|
||||
plan, ok := l.checkedPlanLocked(req)
|
||||
if ok {
|
||||
req.ObjectsChecked = true
|
||||
} else if plan, err = layout.PlanCommit(l.vol.Name, req, true); err != nil {
|
||||
return 0, layoutRemoteErr(err)
|
||||
}
|
||||
l.checked.session, l.checked.root = "", layout.RootName{}
|
||||
req.Plan = &plan
|
||||
if l.pending != nil {
|
||||
if err := layout.CheckSnapshot(l.pending.name, l.pending.meta); err != nil {
|
||||
// A name or facts the commit would refuse: the cut commits
|
||||
@@ -471,6 +513,7 @@ func (l *LayoutRemote) CASHead(ctx context.Context, expected storage.ChunkHeadVe
|
||||
}
|
||||
l.session, l.pending, l.lastCommit = "", nil, got
|
||||
l.ownGen, l.visible, l.seen = got.Gen, got.Gen, imageOf(got)
|
||||
l.clearCheckedLocked() // the session is closed: its check covers nothing more
|
||||
return got.Gen, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -28,11 +28,13 @@ func TestLayoutRemoteSessionFieldSites(t *testing.T) {
|
||||
})
|
||||
}
|
||||
// Begin returns the session it opened; CASHead checks that it survived
|
||||
// a concurrent operator HEAD write before retrying, and keys its skip of
|
||||
// the object check to the session CheckCommit checked (checked.session).
|
||||
// a concurrent operator HEAD write before retrying. CheckCommit records
|
||||
// the checked session, checkedPlanLocked matches a request against it
|
||||
// (the skip of the object check and the plan's reuse), and
|
||||
// clearCheckedLocked forgets it.
|
||||
// ProbeSnapshots skips open activity
|
||||
// (TestLayoutSnapshotProbeSkipsOpenActivityWithoutReadingHEAD).
|
||||
if !reflect.DeepEqual(fields, map[string]int{"openSessionLocked": 2, "rollBackLocked": 1, "Begin": 1, "PutPaced": 1, "CASHead": 5, "CheckCommit": 2, "prepareCommitLocked": 1, "CommitPendingSnapshot": 2, "writeRootLocked": 1, "Clean": 1, "syncSessionLocked": 3, "takeAbandonLocked": 1, "ProbeSnapshots": 1}) {
|
||||
if !reflect.DeepEqual(fields, map[string]int{"openSessionLocked": 2, "rollBackLocked": 1, "Begin": 1, "PutPaced": 1, "CASHead": 2, "CheckCommit": 1, "checkedPlanLocked": 2, "clearCheckedLocked": 1, "prepareCommitLocked": 1, "CommitPendingSnapshot": 2, "writeRootLocked": 1, "Clean": 1, "syncSessionLocked": 3, "takeAbandonLocked": 1, "ProbeSnapshots": 1}) {
|
||||
t.Fatal("session field sites changed; enumerate and probe them", fields)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user