From efc446b90e7c27dcb33cd7db8f0f6e6a2eec601b Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 28 Nov 2026 15:36:29 -0800 Subject: [PATCH] feat, status: count acknowledged HEAD writes by census cause --- .../core/frontend/chunkstore/catalog_b.go | 19 ++-- .../core/frontend/chunkstore/layout_create.go | 1 + .../frontend/chunkstore/layout_head_counts.go | 37 +++++++ .../chunkstore/layout_head_counts_test.go | 101 ++++++++++++++++++ .../core/frontend/chunkstore/layout_store.go | 12 ++- .../core/frontend/durable/provider.go | 9 +- .../durable/provider_current_cut_test.go | 8 ++ seaweed-block/docs/operator-guide.md | 11 ++ seaweed-block/docs/status-schema.md | 1 + 9 files changed, 187 insertions(+), 12 deletions(-) create mode 100644 seaweed-block/core/frontend/chunkstore/layout_head_counts.go create mode 100644 seaweed-block/core/frontend/chunkstore/layout_head_counts_test.go diff --git a/seaweed-block/core/frontend/chunkstore/catalog_b.go b/seaweed-block/core/frontend/chunkstore/catalog_b.go index 9f14ffe2b0..1b493e7998 100644 --- a/seaweed-block/core/frontend/chunkstore/catalog_b.go +++ b/seaweed-block/core/frontend/chunkstore/catalog_b.go @@ -25,11 +25,12 @@ func (v VolumeRef) Dir() string { return layout.VolumeDir(v.Name, v.ID) } // BCatalog is the volume catalog on the content-named layout: every // operation is a layout kernel step over the volume's filer store. type BCatalog struct { - f Filer - b Blobs - base string - cleanMu sync.Mutex - cursors map[VolumeRef]*cleanCursor + headWrites sync.Map // volume directory -> *headWriteCounters; catalog/process lifetime + f Filer + b Blobs + base string + cleanMu sync.Mutex + cursors map[VolumeRef]*cleanCursor } // NewBCatalog returns the catalog of the volumes under base. @@ -42,7 +43,13 @@ func NewBCatalog(f Filer, b Blobs, base string) (*BCatalog, error) { // Store returns the volume's filer store. func (c *BCatalog) Store(v VolumeRef) (*LayoutStore, error) { - return NewLayoutStore(c.f, c.b, path.Join(c.base, v.Dir()), v.ID) + s, err := NewLayoutStore(c.f, c.b, path.Join(c.base, v.Dir()), v.ID) + if err != nil { + return nil, err + } + counts, _ := c.headWrites.LoadOrStore(v.Dir(), s.headWrites) + s.headWrites = counts.(*headWriteCounters) + return s, nil } func (c *BCatalog) open(v VolumeRef) (*LayoutStore, *layout.Reader, error) { diff --git a/seaweed-block/core/frontend/chunkstore/layout_create.go b/seaweed-block/core/frontend/chunkstore/layout_create.go index c0e5753ea6..5c8b3b08a4 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_create.go +++ b/seaweed-block/core/frontend/chunkstore/layout_create.go @@ -50,6 +50,7 @@ func (s *LayoutStore) createHead(ctx context.Context, h layout.Head, cause layou e.Extended = map[string][]byte{layoutKind: []byte("head"), layoutGen: []byte(strconv.FormatUint(1, 10)), layoutVolume: []byte(s.id.String())} err = s.f.CreateEntry(ctx, s.root, e, true) if err == nil { + s.headWrites.record(cause) s.logHeadWrite(ctx, 0, h, cause, started) } else { s.logUnknownHeadWrite(ctx, 0, h, cause, started, err) diff --git a/seaweed-block/core/frontend/chunkstore/layout_head_counts.go b/seaweed-block/core/frontend/chunkstore/layout_head_counts.go new file mode 100644 index 0000000000..426e9717fb --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout_head_counts.go @@ -0,0 +1,37 @@ +package chunkstore + +import ( + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout" + "sync" +) + +// One counter owner per catalog/volume, shared by its temporary store handles. +// Only a successful store reply is acknowledged; an unknown reply is not proof. +type headWriteCounters struct { + mu sync.Mutex + counts map[layout.HeadWriteCause]uint64 +} + +func (c *headWriteCounters) record(cause layout.HeadWriteCause) { + c.mu.Lock() + defer c.mu.Unlock() + if c.counts == nil { + c.counts = make(map[layout.HeadWriteCause]uint64) + } + c.counts[cause]++ +} + +// HeadWrites returns acknowledged HEAD-write counts by census cause since this +// catalog started. Missing causes are zero. Returned maps are independent. +func (s *LayoutStore) HeadWrites() map[layout.HeadWriteCause]uint64 { + if s.headWrites == nil { + return nil + } + s.headWrites.mu.Lock() + defer s.headWrites.mu.Unlock() + out := make(map[layout.HeadWriteCause]uint64, len(s.headWrites.counts)) + for cause, n := range s.headWrites.counts { + out[cause] = n + } + return out +} diff --git a/seaweed-block/core/frontend/chunkstore/layout_head_counts_test.go b/seaweed-block/core/frontend/chunkstore/layout_head_counts_test.go new file mode 100644 index 0000000000..d76a20e9a9 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout_head_counts_test.go @@ -0,0 +1,101 @@ +package chunkstore + +import ( + "context" + "errors" + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout" + "testing" +) + +type headCountSource interface { + HeadWrites() map[layout.HeadWriteCause]uint64 +} + +func readHeadCounts(t *testing.T, s *LayoutStore) map[layout.HeadWriteCause]uint64 { + t.Helper() + source, ok := any(s).(headCountSource) + if !ok { + t.Fatal("store has no typed HEAD write counters") + } + return source.HeadWrites() +} +func TestHeadWriteCountersCountAcknowledgedCensusRowsNotRefusals(t *testing.T) { + for _, cause := range layout.HeadWriteCauses { + t.Run(string(cause), func(t *testing.T) { + s, _ := causeStore(t) + ctx := context.Background() + h := layout.Unclaimed(s.id, nil) + before := readHeadCounts(t, s) + if cause == layout.CauseTombstone { + h.State = layout.StateDeleted + if err := s.CreateDeletedHead(ctx, h, cause); err != nil { + t.Fatal(err) + } + } else { + if err := s.CreateHead(ctx, h, layout.CauseCreate); err != nil { + t.Fatal(err) + } + if cause != layout.CauseCreate { + if err := s.UpdateHead(ctx, 1, h, cause); err != nil { + t.Fatal(err) + } + } + } + got := readHeadCounts(t, s) + if got[cause] != before[cause]+1 { + t.Fatalf("cause %s counts=%v before=%v", cause, got, before) + } + if err := s.UpdateHead(ctx, 99, h, cause); err == nil { + t.Fatal("stale write accepted") + } + if after := readHeadCounts(t, s); after[cause] != got[cause] { + t.Fatalf("refusal counted %v", after) + } + got[cause] = 999 + if readHeadCounts(t, s)[cause] == 999 { + t.Fatal("mutable counter snapshot") + } + }) + } +} +func TestHeadWriteCountersShareTheCatalogLifetimeButNotAnotherProcess(t *testing.T) { + f, b := newFakeFiler(), newFakeBlobs() + cat, err := NewBCatalog(f, b, "/blocks") + if err != nil { + t.Fatal(err) + } + id, _ := layout.NewVolumeID() + ref := VolumeRef{Name: "v", ID: id} + s, err := cat.Store(ref) + if err != nil { + t.Fatal(err) + } + other, err := cat.Store(ref) + if err != nil { + t.Fatal(err) + } + if err := s.CreateHead(context.Background(), layout.Unclaimed(id, nil), layout.CauseCreate); err != nil { + t.Fatal(err) + } + if readHeadCounts(t, other)[layout.CauseCreate] != 1 { + t.Fatal("new store handle lost process counts") + } + freshCat, _ := NewBCatalog(f, b, "/blocks") + freshStore, _ := freshCat.Store(ref) + if readHeadCounts(t, freshStore)[layout.CauseCreate] != 0 { + t.Fatal("another catalog inherited process counts") + } +} +func TestHeadWriteCountersDoNotCallAnUnknownAnswerAcknowledged(t *testing.T) { + for _, applied := range []bool{false, true} { + s, _ := causeStore(t) + fault := errors.New("answer lost") + s.f = lostHeadReplyFiler{Filer: s.f, applied: applied, fault: fault} + if err := s.CreateHead(context.Background(), layout.Unclaimed(s.id, nil), layout.CauseCreate); !errors.Is(err, fault) { + t.Fatal(err) + } + if readHeadCounts(t, s)[layout.CauseCreate] != 0 { + t.Fatal("unknown answer counted as acknowledged") + } + } +} diff --git a/seaweed-block/core/frontend/chunkstore/layout_store.go b/seaweed-block/core/frontend/chunkstore/layout_store.go index 0096941f9f..592ed16393 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_store.go +++ b/seaweed-block/core/frontend/chunkstore/layout_store.go @@ -29,17 +29,18 @@ var ErrLegacyStore = errors.New("chunkstore: legacy store format; recreate the v // Objects carry their immutable bytes as file chunks; metadata updates preserve // those chunks, and deletion delegates their reclamation to the filer. type LayoutStore struct { - f Filer - b Blobs - root string - id layout.VolumeID + headWrites *headWriteCounters + f Filer + b Blobs + root string + id layout.VolumeID } func NewLayoutStore(f Filer, b Blobs, root string, id layout.VolumeID) (*LayoutStore, error) { if f == nil || b == nil || id.IsZero() || !path.IsAbs(root) || path.Clean(root) != root || root == "/" { return nil, errors.New("chunkstore: layout store needs filer, blobs, a clean volume root and volume id") } - return &LayoutStore{f: f, b: b, root: root, id: id}, nil + return &LayoutStore{f: f, b: b, root: root, id: id, headWrites: &headWriteCounters{}}, nil } var _ layout.HeadStore = (*LayoutStore)(nil) @@ -119,6 +120,7 @@ func (s *LayoutStore) UpdateHead(ctx context.Context, expectedGen uint64, next l err = s.f.UpdateEntry(ctx, s.root, e, map[string][]byte{layoutGen: []byte(strconv.FormatUint(expectedGen, 10)), layoutVolume: []byte(s.id.String())}) } if err == nil { + s.headWrites.record(cause) s.logHeadWrite(ctx, expectedGen, next, cause, started) } else { s.logUnknownHeadWrite(ctx, expectedGen, next, cause, started, err) diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index eec9cf1b08..8cd43de44d 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -359,6 +359,7 @@ type volHandle struct { // handle. It intentionally exposes lineage and readiness facts without giving // callers a control surface. type VolumeStatus struct { + HeadWrites map[layout.HeadWriteCause]uint64 `json:",omitempty"` // acknowledged HEAD writes by cause since holder catalog start; unknown replies excluded VolumeID string // the volume Path string // its store path under --durable-root Impl string // the durable implementation (walstore, smartwal, walstore-chunk, ...) @@ -770,6 +771,7 @@ func durableIdentityConflict(got, want frontend.Identity) error { // durable lineage latching without scraping logs. func (p *DurableProvider) DurableStatuses() []VolumeStatus { type chunkView struct { + head layout.HeadStore layer storage.ChunkLayer awaiting bool snapshots *snapshotSlot @@ -781,7 +783,7 @@ func (p *DurableProvider) DurableStatuses() []VolumeStatus { for volumeID, h := range p.volumes { handles[volumeID] = h if p.cfg.Impl == ImplWALStoreChunk { - chunks[volumeID] = chunkView{layer: h.chunk, awaiting: h.waitGen != 0, snapshots: h.snapshots, cutWait: h.cutWait} + chunks[volumeID] = chunkView{head: h.layoutHead, layer: h.chunk, awaiting: h.waitGen != 0, snapshots: h.snapshots, cutWait: h.cutWait} } } p.mu.Unlock() @@ -791,6 +793,11 @@ func (p *DurableProvider) DurableStatuses() []VolumeStatus { st := h.backend.durableStatus(volumeID, h.path, string(h.impl)) if cv, ok := chunks[volumeID]; ok { cs := &ChunkStatus{AwaitingAuthority: cv.awaiting} + if counts, ok := cv.head.(interface { + HeadWrites() map[layout.HeadWriteCause]uint64 + }); ok { + st.HeadWrites = counts.HeadWrites() + } if cv.layer != nil { cs.Attached, cs.Fenced, cs.RemoteLSN, cs.Stats = true, cv.layer.Fenced(), cv.layer.RemoteLSN(), cv.layer.Stats() cs.CurrentCut = cs.Stats.CurrentCut diff --git a/seaweed-block/core/frontend/durable/provider_current_cut_test.go b/seaweed-block/core/frontend/durable/provider_current_cut_test.go index d4f1905394..15d0da53a0 100644 --- a/seaweed-block/core/frontend/durable/provider_current_cut_test.go +++ b/seaweed-block/core/frontend/durable/provider_current_cut_test.go @@ -58,6 +58,7 @@ func TestDurableStatusExposesTheCurrentCutWithoutWaitingForCommit(t *testing.T) if _, err = store.Write(1, block(0x41)); err != nil { t.Fatal(err) } + beforeCounts := p.DurableStatuses()[0].HeadWrites remote.held = true ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -74,6 +75,9 @@ func TestDurableStatusExposesTheCurrentCutWithoutWaitingForCommit(t *testing.T) if len(status) != 1 || status[0].Chunk == nil || status[0].Chunk.CurrentCut == nil || status[0].Chunk.CurrentCut.Phase != "commit" { t.Fatalf("status=%+v", status) } + if status[0].HeadWrites[layout.CauseSessionBegin] != beforeCounts[layout.CauseSessionBegin]+1 || status[0].HeadWrites[layout.CauseCommit] != beforeCounts[layout.CauseCommit] { + t.Fatalf("held commit counts=%v before=%v", status[0].HeadWrites, beforeCounts) + } wire, err := json.Marshal(status) if err != nil { t.Fatal(err) @@ -91,6 +95,10 @@ func TestDurableStatusExposesTheCurrentCutWithoutWaitingForCommit(t *testing.T) case <-ctx.Done(): t.Fatal("capture did not return") } + afterCounts := p.DurableStatuses()[0].HeadWrites + if afterCounts[layout.CauseCommit] != beforeCounts[layout.CauseCommit]+1 || status[0].HeadWrites[layout.CauseCommit] != beforeCounts[layout.CauseCommit] { + t.Fatalf("ack count or immutable snapshot: after=%v held=%v", afterCounts, status[0].HeadWrites) + } if p.DurableStatuses()[0].Chunk.CurrentCut != nil { t.Fatal("idle CurrentCut still present") } diff --git a/seaweed-block/docs/operator-guide.md b/seaweed-block/docs/operator-guide.md index 5225dbd425..ba66d5e3bf 100644 --- a/seaweed-block/docs/operator-guide.md +++ b/seaweed-block/docs/operator-guide.md @@ -58,6 +58,17 @@ attempt measurement up to logging; synchronous log output and subsequent cleanup can extend caller latency beyond it. Logging releases the statistics mutex first but remains within the cut's capture lock and in-flight lifetime. +The holder's `/status/durable` response also exposes `Volumes[].HeadWrites`, +a snapshot of acknowledged HEAD writes by the census cause name since this +holder's catalog started. Missing causes are zero. Counts survive temporary +store handles and attachment retries within the same catalog, but reset with +a new process/catalog. Definite refusals and ambiguous write answers do not +count as acknowledged writes. An unacknowledged write may nevertheless have +reached HEAD; these counters alone never prove that every persisted generation +was observed. Runner comparisons must use the same live holder process and +retain their own issued-operation history. A KILL window is checked against +the whole-HEAD field-change census, not inferred from missing counters. + The holder's `/status/durable` response exposes `Volumes[].Chunk.CurrentCut` while a cut is active, and omits it when idle. Its typed `phase` is one of `admit`, `recover`, `stage`, `barrier`, `candidates`, `compare`, `upload`, diff --git a/seaweed-block/docs/status-schema.md b/seaweed-block/docs/status-schema.md index 795fee635e..f623f7a17a 100644 --- a/seaweed-block/docs/status-schema.md +++ b/seaweed-block/docs/status-schema.md @@ -74,6 +74,7 @@ Durable store state of the served volume. Go type `volume.durableStatusBody`. | `ReplicaID` | string | this host's replica | | `VolumeCount` | int | number of entries in Volumes | | `Volumes[]` | object | the durable store state of the served volume | +| `Volumes[].HeadWrites{}` | uint64 | acknowledged HEAD writes by cause since holder catalog start; unknown replies excluded (omitted when empty) | | `Volumes[].VolumeID` | string | the volume | | `Volumes[].Path` | string | its store path under --durable-root | | `Volumes[].Impl` | string | the durable implementation (walstore, smartwal, walstore-chunk, ...) |