diff --git a/seaweed-block/cmd/blockvolume/chunk_maintenance.go b/seaweed-block/cmd/blockvolume/chunk_maintenance.go index 1a763263d1..a6225ac880 100644 --- a/seaweed-block/cmd/blockvolume/chunk_maintenance.go +++ b/seaweed-block/cmd/blockvolume/chunk_maintenance.go @@ -49,13 +49,21 @@ func newChunkMaintenance(cat *chunkstore.BCatalog, ref chunkstore.VolumeRef, cle err = remote.Clean(ctx, func(ctx context.Context, me layout.HolderID, s string) error { session = s // This generation is diagnostic only; it does not authorize removal. - head, _, _ := cat.ReadHead(ctx, ref) - defer func() { durable.ReportMaintenance(ctx, int64(res.ChunkPlanned), int64(res.ChunkRemoved), head.Gen) }() + head, found, readErr := cat.ReadHead(ctx, ref) + diagnostic := "" + if readErr != nil { + diagnostic = fmt.Sprintf("HEAD generation not read: %v", readErr) + } else if !found { + diagnostic = "HEAD generation not read: HEAD absent" + } + defer func() { + durable.ReportMaintenance(ctx, durable.MaintenancePassStatus{Planned: int64(res.Planned), Removed: int64(res.Removed), RemovedChunks: int64(res.Removed - res.Roots - res.Segments), RemovedRoots: int64(res.Roots), RemovedSegments: int64(res.Segments), HeadGen: head.Gen, Error: diagnostic}) + }() var rerr error res, rerr = cat.RemoveUnreachableAll(ctx, ref, me, s) return rerr }) - log.Printf("blockvolume: maintenance volume=%s clean=%s shards=%d planned=%d removed=%d", vol, session, res.Shards, res.Planned, res.Removed) + log.Printf("blockvolume: maintenance volume=%s clean=%s shards=%d planned=%d removed=%d removedChunks=%d removedRoots=%d removedSegments=%d", vol, session, res.Shards, res.Planned, res.Removed, res.Removed-res.Roots-res.Segments, res.Roots, res.Segments) // A cleaning refused because HEAD moved away from this holder is // the same fence a cut would see: classified so the provider latches. return chunkstore.LayoutErr(err) diff --git a/seaweed-block/cmd/blockvolume/maintenance_status_test.go b/seaweed-block/cmd/blockvolume/maintenance_status_test.go index e8fed2307a..68931feaf3 100644 --- a/seaweed-block/cmd/blockvolume/maintenance_status_test.go +++ b/seaweed-block/cmd/blockvolume/maintenance_status_test.go @@ -4,15 +4,43 @@ import ( "bytes" "context" "encoding/json" + "errors" + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore" "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/filerpb" "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout" "github.com/seaweedfs/seaweed-block/core/frontend/durable" "github.com/seaweedfs/seaweed-block/core/frontend/durable/testutil" "path" + "strings" + "sync/atomic" "testing" "time" ) +type maintenanceDiagnosticFiler struct { + chunkstore.Filer + fail atomic.Bool +} + +func (f *maintenanceDiagnosticFiler) LookupEntry(ctx context.Context, dir, name string) (*filerpb.Entry, error) { + if name == layout.HeadName && f.fail.Swap(false) { + return nil, errors.New("diagnostic read unavailable") + } + return f.Filer.LookupEntry(ctx, dir, name) +} + +type maintenanceDiagnosticCleaner struct { + layoutCleaner + filer *maintenanceDiagnosticFiler +} + +func (c maintenanceDiagnosticCleaner) Clean(ctx context.Context, run func(context.Context, layout.HolderID, string) error) error { + return c.layoutCleaner.Clean(ctx, func(ctx context.Context, me layout.HolderID, s string) error { + c.filer.fail.Store(true) + return run(ctx, me, s) + }) +} + func maintenanceJSON(t *testing.T, p *durable.DurableProvider) map[string]any { t.Helper() b, err := json.Marshal(p.DurableStatuses()) @@ -32,19 +60,32 @@ func maintenanceJSON(t *testing.T, p *durable.DurableProvider) map[string]any { } func TestMaintenanceStatusRealPassAndTimeout(t *testing.T) { - for _, timeout := range []bool{false, true} { - t.Run(map[bool]string{false: "completed", true: "timeout"}[timeout], func(t *testing.T) { + for _, kind := range []string{"completed", "metadata-only", "head-unread", "timeout"} { + t.Run(kind, func(t *testing.T) { + timeout := kind == "timeout" ctx := context.Background() fixture := testutil.NewLayoutFixture(t, "v1", 256, 16) cfg := durable.ProviderConfig{Impl: durable.ImplWALStoreChunk, StorageRoot: t.TempDir(), BlockSize: 4096, NumBlocks: 256, ChunkBlocks: 16, ChunkMaintenanceInterval: time.Hour, ChunkMaintenanceTimeout: 100 * time.Millisecond, ChunkStagger: true} fixture.Configure(&cfg) var p *durable.DurableProvider + diagnosticFiler := &maintenanceDiagnosticFiler{Filer: fixture.Filer} + maintenanceCatalog := fixture.Catalog + if kind == "head-unread" { + var e error + maintenanceCatalog, e = chunkstore.NewBCatalog(diagnosticFiler, fixture.Blobs, "/provider-test") + if e != nil { + t.Fatal(e) + } + } entered, release := make(chan struct{}), make(chan struct{}) - pass := newChunkMaintenance(fixture.Catalog, fixture.Ref, func(ctx context.Context) (layoutCleaner, error) { + pass := newChunkMaintenance(maintenanceCatalog, fixture.Ref, func(ctx context.Context) (layoutCleaner, error) { r, e := p.LayoutRemote(ctx, "v1") if e != nil { return nil, e } + if kind == "head-unread" { + return maintenanceDiagnosticCleaner{layoutCleaner: r.(layoutCleaner), filer: diagnosticFiler}, nil + } return r.(layoutCleaner), nil }, true, time.Now) cfg.ChunkMaintenance = func(ctx context.Context, v string) error { @@ -85,7 +126,17 @@ func TestMaintenanceStatusRealPassAndTimeout(t *testing.T) { if e = fixture.Filer.CreateEntry(ctx, path.Join("/provider-test", fixture.Ref.Dir(), layout.PoolDir), &filerpb.Entry{Name: "0", IsDirectory: true}, false); e != nil { t.Fatal(e) } - if e = layout.EnsureObject(ctx, st, layout.NameChunk(0, data).Path(), data, "ended-session"); e != nil { + chunk := layout.NameChunk(0, data) + if kind != "metadata-only" { + if e = layout.EnsureObject(ctx, st, chunk.Path(), data, "ended-session"); e != nil { + t.Fatal(e) + } + } + root, segs, e := layout.Build(map[uint64]layout.Ref{0: {Owner: fixture.Ref.Name, OwnerID: fixture.Ref.ID, Chunk: chunk}}) + if e != nil { + t.Fatal(e) + } + if _, e = layout.PrepareRoot(ctx, st, root, segs, "ended-session"); e != nil { t.Fatal(e) } } @@ -133,11 +184,24 @@ func TestMaintenanceStatusRealPassAndTimeout(t *testing.T) { if err != nil { t.Fatal(err) } - if after.Gen != before.Gen+2 || m["passesOk"] != float64(1) || last["error"] != "" || last["headGen"].(float64) != float64(before.Gen+1) { + expectedGen := before.Gen + 1 + if kind == "head-unread" { + expectedGen = 0 + if !strings.Contains(last["error"].(string), "HEAD generation not read") { + t.Fatalf("missing diagnostic: %v", last) + } + } else if last["error"] != "" { + t.Fatalf("unexpected error: %v", last) + } + if after.Gen != before.Gen+2 || m["passesOk"] != float64(1) || last["headGen"].(float64) != float64(expectedGen) { t.Fatalf("before=%d after=%d status=%v", before.Gen, after.Gen, m) } - if last["planned"] != float64(1) || last["removed"] != float64(1) { - t.Fatalf("chunk counts=%v", last) + total, chunks := float64(3), float64(1) + if kind == "metadata-only" { + total, chunks = 2, 0 + } + if last["planned"] != total || last["removed"] != total || last["removedChunks"] != chunks || last["removedRoots"] != float64(1) || last["removedSegments"] != float64(1) { + t.Fatalf("object counts=%v", last) } if _, ok := last["planned"]; !ok { t.Fatal("planned absent") diff --git a/seaweed-block/core/frontend/chunkstore/clean_b.go b/seaweed-block/core/frontend/chunkstore/clean_b.go index 1261fae7f7..5011cf7eb2 100644 --- a/seaweed-block/core/frontend/chunkstore/clean_b.go +++ b/seaweed-block/core/frontend/chunkstore/clean_b.go @@ -15,13 +15,11 @@ var cleanPage uint32 = 1000 // CleanResult counts names examined/deleted by this bounded pass, all kinds. type CleanResult struct { - ChunkPlanned int - ChunkRemoved int - Shards int - Planned int - Removed int - Roots int - Segments int + Shards int + Planned int + Removed int + Roots int + Segments int } // The cursor is scheduling only: after a timeout a new session re-plans tags @@ -144,10 +142,8 @@ func (c *BCatalog) RemoveUnreachableAll(ctx context.Context, v VolumeRef, me lay names[i] = n.String() } res.Planned += len(page) - res.ChunkPlanned += len(page) out, err := cleaner.Page(ctx, layout.PoolDir, shard, names) res.Removed += len(out.Names) - res.ChunkRemoved += len(out.Names) if out.After != "" { cur.poolAfter = path.Base(out.After) } diff --git a/seaweed-block/core/frontend/durable/maintenance_status.go b/seaweed-block/core/frontend/durable/maintenance_status.go index d4fc6e7cba..cc27e68b87 100644 --- a/seaweed-block/core/frontend/durable/maintenance_status.go +++ b/seaweed-block/core/frontend/durable/maintenance_status.go @@ -17,11 +17,14 @@ type MaintenanceStatus struct { // MaintenancePassStatus describes the last completed or failed pass. type MaintenancePassStatus struct { - AtNs int64 `json:"atNs"` // unix ns when the pass ended - Planned int64 `json:"planned"` // chunk objects considered for removal - Removed int64 `json:"removed"` // chunk objects removed - Error string `json:"error"` // one-line error; empty when completed - HeadGen uint64 `json:"headGen"` // observed cleaning HEAD generation; diagnostic only + AtNs int64 `json:"atNs"` // unix ns when the pass ended + Planned int64 `json:"planned"` // all objects considered for removal + Removed int64 `json:"removed"` // all objects removed + RemovedChunks int64 `json:"removedChunks"` // chunk objects removed + RemovedRoots int64 `json:"removedRoots"` // root objects removed + RemovedSegments int64 `json:"removedSegments"` // segment objects removed + Error string `json:"error"` // one-line pass/observation error; empty when both succeeded + HeadGen uint64 `json:"headGen"` // observed cleaning HEAD generation; diagnostic only } type maintenanceSlot struct { @@ -33,14 +36,14 @@ type maintenanceContextKey struct{} // ReportMaintenance supplies counts from the existing cleaning callback. It does // not read or write storage, and cannot affect the pass's safety decisions. -func ReportMaintenance(ctx context.Context, planned, removed int64, headGen uint64) { +func ReportMaintenance(ctx context.Context, result MaintenancePassStatus) { slot, _ := ctx.Value(maintenanceContextKey{}).(*maintenanceSlot) if slot == nil { return } slot.mu.Lock() defer slot.mu.Unlock() - slot.result.Planned, slot.result.Removed, slot.result.HeadGen = planned, removed, headGen + slot.result = result } func (m *maintenanceSlot) begin() { m.mu.Lock() @@ -53,10 +56,12 @@ func (m *maintenanceSlot) end(err error) { defer m.mu.Unlock() m.result.AtNs = time.Now().UnixNano() if err != nil { - m.result.Error = strings.Join(strings.Fields(err.Error()), " ") + m.result.Error = strings.TrimSpace(m.result.Error + "; " + err.Error()) + m.result.Error = strings.TrimPrefix(m.result.Error, "; ") } else { m.s.PassesOK++ } + m.result.Error = strings.Join(strings.Fields(m.result.Error), " ") last := m.result m.s.LastPass, m.s.InProgress, m.s.CurrentStartNs = &last, false, 0 } diff --git a/seaweed-block/core/frontend/durable/maintenance_status_test.go b/seaweed-block/core/frontend/durable/maintenance_status_test.go index 75c19f27b4..a907d2aec8 100644 --- a/seaweed-block/core/frontend/durable/maintenance_status_test.go +++ b/seaweed-block/core/frontend/durable/maintenance_status_test.go @@ -9,10 +9,10 @@ import ( func TestMaintenanceStatusSnapshotIsIndependent(t *testing.T) { var s maintenanceSlot s.begin() - ReportMaintenance(context.WithValue(context.Background(), maintenanceContextKey{}, &s), 9, 4, 7) + ReportMaintenance(context.WithValue(context.Background(), maintenanceContextKey{}, &s), MaintenancePassStatus{Planned: 9, Removed: 4, RemovedChunks: 1, RemovedRoots: 2, RemovedSegments: 1, HeadGen: 7}) s.end(errors.New("first line\nsecond line")) a := s.status() - if a.LastPass.Error != "first line second line" || a.PassesOK != 0 || a.LastPass.Planned != 9 || a.LastPass.Removed != 4 || a.LastPass.HeadGen != 7 { + if a.LastPass.Error != "first line second line" || a.PassesOK != 0 || a.LastPass.Planned != 9 || a.LastPass.Removed != 4 || a.LastPass.HeadGen != 7 || a.LastPass.RemovedChunks != 1 || a.LastPass.RemovedRoots != 2 || a.LastPass.RemovedSegments != 1 { t.Fatalf("failed=%+v", a) } a.LastPass.Removed = 99 diff --git a/seaweed-block/docs/releases/cleaning-convergence.md b/seaweed-block/docs/releases/cleaning-convergence.md index 1b8d84a8a8..ad749f98dc 100644 --- a/seaweed-block/docs/releases/cleaning-convergence.md +++ b/seaweed-block/docs/releases/cleaning-convergence.md @@ -31,9 +31,12 @@ is loopback-only: an off-host checker reports "not checked here". `passesOk` counts completed passes in this holder process. `inProgress` and `currentStartNs` show a running pass (unix nanoseconds; zero start when idle). `lastPass` is absent until a pass ends, then records `atNs` (unix nanosecond end), -`planned` and `removed` chunk-object counts, a one-line `error` (empty on success), -and `headGen`, the generation observed after opening the cleaning. Metadata -objects are excluded from the chunk counts. A pass that fails before entering +`planned` and `removed` total object counts, a one-line `error` (empty on success), +and `headGen`, the generation observed after opening the cleaning. `removedChunks`, `removedRoots`, and `removedSegments` give the deletion +breakdown; metadata is included in the total. A metadata-only pass therefore +never reports `removed: 0` when it reclaimed roots or segments. A pass that fails before entering cleaning has zero counts and generation; these zeros do not prove a clean store. -A timed-out pass is recorded as an error and does not increment `passesOk`. +An unreadable HEAD generation is named as "not read" in `error`; its zero +generation is unavailable, not a storage fact. This diagnostic does not fail the +cleaning callback or suppress its success count. A timed-out pass is recorded as an error and does not increment `passesOk`. These are process-local observations, not authority or a safety decision. diff --git a/seaweed-block/docs/status-schema.md b/seaweed-block/docs/status-schema.md index d5d41fbc4f..d85092879c 100644 --- a/seaweed-block/docs/status-schema.md +++ b/seaweed-block/docs/status-schema.md @@ -224,9 +224,12 @@ Durable store state of the served volume. Go type `volume.durableStatusBody`. | `Volumes[].Chunk.Maintenance.currentStartNs` | int64 | unix ns start of the running pass; zero when idle | | `Volumes[].Chunk.Maintenance.lastPass` | object | absent until a pass ends (omitted when empty) | | `Volumes[].Chunk.Maintenance.lastPass.atNs` | int64 | unix ns when the pass ended | -| `Volumes[].Chunk.Maintenance.lastPass.planned` | int64 | chunk objects considered for removal | -| `Volumes[].Chunk.Maintenance.lastPass.removed` | int64 | chunk objects removed | -| `Volumes[].Chunk.Maintenance.lastPass.error` | string | one-line error; empty when completed | +| `Volumes[].Chunk.Maintenance.lastPass.planned` | int64 | all objects considered for removal | +| `Volumes[].Chunk.Maintenance.lastPass.removed` | int64 | all objects removed | +| `Volumes[].Chunk.Maintenance.lastPass.removedChunks` | int64 | chunk objects removed | +| `Volumes[].Chunk.Maintenance.lastPass.removedRoots` | int64 | root objects removed | +| `Volumes[].Chunk.Maintenance.lastPass.removedSegments` | int64 | segment objects removed | +| `Volumes[].Chunk.Maintenance.lastPass.error` | string | one-line pass/observation error; empty when both succeeded | | `Volumes[].Chunk.Maintenance.lastPass.headGen` | uint64 | observed cleaning HEAD generation; diagnostic only | | `Volumes[].Chunk.Attached` | bool | the chunk layer is attached to the remote | | `Volumes[].Chunk.AwaitingAuthority` | bool | recovered; the layer waits for master authority to attach |