mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
Brings seaweed-block main (v1.1.1 plus the docs and a test fix after it) into seaweed-block/ as one commit. Only files under seaweed-block/ change, and the subtree equals seaweed-block main exactly. The module's full history stays in the seaweed-block repository. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
340 lines
11 KiB
Go
340 lines
11 KiB
Go
package main
|
|
|
|
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"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type maintenanceDiagnosticFiler struct {
|
|
chunkstore.Filer
|
|
fail atomic.Bool
|
|
absent bool
|
|
}
|
|
|
|
func (f *maintenanceDiagnosticFiler) LookupEntry(ctx context.Context, dir, name string) (*filerpb.Entry, error) {
|
|
if name == layout.HeadName && f.fail.Swap(false) {
|
|
if f.absent {
|
|
return nil, chunkstore.ErrNotFound
|
|
}
|
|
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())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var v []map[string]any
|
|
if err = json.Unmarshal(b, &v); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c := v[0]["Chunk"].(map[string]any)
|
|
m, ok := c["Maintenance"].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("maintenance loop has no status: %s", b)
|
|
}
|
|
return m
|
|
}
|
|
|
|
func TestMaintenanceStatusRealPassAndTimeout(t *testing.T) {
|
|
for _, kind := range []string{"completed", "reachable", "metadata-only", "head-unread", "head-absent", "retention-only", "newer-pending-work", "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, absent: kind == "head-absent"}
|
|
maintenanceCatalog := fixture.Catalog
|
|
if kind == "head-unread" || kind == "head-absent" {
|
|
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{})
|
|
var cleanerRequests atomic.Int32
|
|
var orphanPaths []string
|
|
pass := newChunkMaintenance(maintenanceCatalog, fixture.Ref, func(ctx context.Context) (layoutCleaner, error) {
|
|
cleanerRequests.Add(1)
|
|
r, e := p.LayoutRemote(ctx, "v1")
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
if kind == "head-unread" || kind == "head-absent" {
|
|
return maintenanceDiagnosticCleaner{layoutCleaner: r.(layoutCleaner), filer: diagnosticFiler}, nil
|
|
}
|
|
return r.(layoutCleaner), nil
|
|
}, kind != "retention-only", time.Now)
|
|
cfg.ChunkMaintenance = func(ctx context.Context, v string) error {
|
|
close(entered)
|
|
if timeout {
|
|
<-ctx.Done()
|
|
return ctx.Err()
|
|
}
|
|
select {
|
|
case <-release:
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
return pass(ctx, v)
|
|
}
|
|
var err error
|
|
p, err = durable.NewDurableProvider(cfg, cleaningView{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer p.Close()
|
|
if _, err = p.EnsureStorage("v1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = p.RecoverVolume(ctx, "v1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m := maintenanceJSON(t, p)
|
|
if m["passesOk"] != float64(0) || m["inProgress"] != false || m["currentStartNs"] != float64(0) || m["lastPass"] != nil {
|
|
t.Fatalf("initial=%v", m)
|
|
}
|
|
if !timeout {
|
|
st, e := fixture.Catalog.Store(fixture.Ref)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
data := bytes.Repeat([]byte{99}, 4096)
|
|
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)
|
|
}
|
|
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)
|
|
}
|
|
orphanPaths = append(orphanPaths, chunk.Path())
|
|
}
|
|
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)
|
|
}
|
|
rn, e := layout.PrepareRoot(ctx, st, root, segs, "ended-session")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
orphanPaths = append(orphanPaths, rn.Path())
|
|
for sn := range segs {
|
|
orphanPaths = append(orphanPaths, sn.Path())
|
|
}
|
|
}
|
|
if kind == "reachable" {
|
|
local, e := p.EnsureStorage("v1")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = local.Write(0, bytes.Repeat([]byte{42}, 4096)); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = local.Sync(); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = p.CaptureVolume(ctx, "v1"); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
before, _, err := fixture.Catalog.ReadHead(ctx, fixture.Ref)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if kind == "newer-pending-work" {
|
|
// Simulate bytes persisted by a future producer, not a v1.1 producer path.
|
|
st, e := fixture.Catalog.Store(fixture.Ref)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
before.Version = layout.HeadVersion1
|
|
before.Lists = []layout.ListCursor{{Ref: layout.ListRef{Hash: layout.Sum([]byte("future pending page")).String(), Pages: 1}, Kind: layout.ListUploadIntent, Producer: "future", Generation: 1}}
|
|
if e = st.UpdateHead(ctx, before.Gen, before, layout.CauseCommit); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
before, _, err = fixture.Catalog.ReadHead(ctx, fixture.Ref)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if !p.KickMaintenance("v1") {
|
|
t.Fatal("kick refused")
|
|
}
|
|
select {
|
|
case <-entered:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("not entered")
|
|
}
|
|
m = maintenanceJSON(t, p)
|
|
if m["inProgress"] != true || m["currentStartNs"].(float64) <= 0 {
|
|
t.Fatalf("running=%v", m)
|
|
}
|
|
if !timeout {
|
|
close(release)
|
|
}
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
m = maintenanceJSON(t, p)
|
|
if m["lastPass"] != nil {
|
|
break
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
last, ok := m["lastPass"].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("no completion: %v", m)
|
|
}
|
|
if m["inProgress"] != false || m["currentStartNs"] != float64(0) || last["atNs"].(float64) <= 0 {
|
|
t.Fatalf("ended=%v", m)
|
|
}
|
|
if timeout {
|
|
if m["passesOk"] != float64(0) || last["error"] == "" {
|
|
t.Fatalf("timeout=%v", m)
|
|
}
|
|
return
|
|
}
|
|
after, _, err := fixture.Catalog.ReadHead(ctx, fixture.Ref)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if kind == "retention-only" {
|
|
if cleanerRequests.Load() != 0 || !reflect.DeepEqual(before, after) || m["passesOk"] != float64(1) || last["headGen"] != float64(0) || last["error"] != "" {
|
|
t.Fatalf("retention reported a clean: requests=%d status=%v", cleanerRequests.Load(), m)
|
|
}
|
|
st, e := fixture.Catalog.Store(fixture.Ref)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for _, name := range orphanPaths {
|
|
_, found, e := st.Stat(ctx, name)
|
|
if e != nil || !found {
|
|
t.Fatalf("retention-only removed %s: found=%t err=%v", name, found, e)
|
|
}
|
|
}
|
|
// The shared fixture is produced by the real callback/status projection;
|
|
// normalize only its already-checked wall-clock end timestamp.
|
|
last["atNs"] = float64(1)
|
|
raw, e := os.ReadFile(filepath.Join("testdata", "maintenance-retention-only.json"))
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
var want map[string]any
|
|
if e = json.Unmarshal(raw, &want); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if !reflect.DeepEqual(m, want) {
|
|
t.Fatalf("retention-only contract changed: got=%v want=%v", m, want)
|
|
}
|
|
return
|
|
}
|
|
if kind == "newer-pending-work" {
|
|
if last["skipReason"] != "newer_pending_work" || !reflect.DeepEqual(before.Lists, after.Lists) {
|
|
t.Fatalf("reserved work silently skipped or consumed: status=%v lists=%v", last, after.Lists)
|
|
}
|
|
} else if _, present := last["skipReason"]; present {
|
|
t.Fatalf("unexpected skip reason: %v", last)
|
|
}
|
|
expectedGen := before.Gen + 1
|
|
if kind == "head-unread" || kind == "head-absent" {
|
|
expectedGen = 0
|
|
if last["error"] != "" || !strings.Contains(last["headGenError"].(string), "HEAD generation not read") {
|
|
t.Fatalf("missing diagnostic: %v", last)
|
|
}
|
|
if kind == "head-absent" && last["headGenError"] != "HEAD generation not read: HEAD absent" {
|
|
t.Fatalf("missing absent-HEAD diagnostic: %v", last)
|
|
}
|
|
} else if last["error"] != "" || last["headGenError"] != "" {
|
|
t.Fatalf("unexpected error: %v", last)
|
|
}
|
|
// Initial admission closes before enumeration, then roots, segments
|
|
// and (unless metadata-only) chunks each have their own begin/end.
|
|
pages := uint64(3)
|
|
if kind == "metadata-only" {
|
|
pages = 2
|
|
}
|
|
if after.Gen != before.Gen+2+2*pages || m["passesOk"] != float64(1) || last["headGen"].(float64) != float64(expectedGen) {
|
|
t.Fatalf("before=%d after=%d status=%v", before.Gen, after.Gen, m)
|
|
}
|
|
total, chunks := float64(3), float64(1)
|
|
if kind == "metadata-only" {
|
|
total, chunks = 2, 0
|
|
}
|
|
plannedTotal, plannedChunks, plannedRoots, plannedSegments := total, chunks, float64(1), float64(1)
|
|
if kind == "reachable" {
|
|
plannedTotal, plannedChunks, plannedRoots, plannedSegments = 6, 2, 2, 2
|
|
}
|
|
if last["plannedChunks"] != plannedChunks || last["plannedRoots"] != plannedRoots || last["plannedSegments"] != plannedSegments || last["planned"] != plannedTotal || 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")
|
|
}
|
|
if _, ok := last["removed"]; !ok {
|
|
t.Fatal("removed absent")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMaintenanceStatusAbsentWithoutLoop(t *testing.T) {
|
|
fixture := testutil.NewLayoutFixture(t, "v1", 256, 16)
|
|
cfg := durable.ProviderConfig{Impl: durable.ImplWALStoreChunk, StorageRoot: t.TempDir(), BlockSize: 4096, NumBlocks: 256, ChunkBlocks: 16}
|
|
fixture.Configure(&cfg)
|
|
p, err := durable.NewDurableProvider(cfg, cleaningView{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer p.Close()
|
|
if _, err = p.EnsureStorage("v1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = p.RecoverVolume(context.Background(), "v1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := json.Marshal(p.DurableStatuses())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var v []map[string]any
|
|
if err = json.Unmarshal(b, &v); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, exists := v[0]["Chunk"].(map[string]any)["Maintenance"]; exists {
|
|
t.Fatalf("disabled loop exposed Maintenance")
|
|
}
|
|
}
|