mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
sw-block: snapshot-delete routes on the HEAD version and the cluster level
At cluster format level 0 a delete by name of a version-0 HEAD's snapshot is v1.0's name-only delete, chosen before any operation record is written. On a version-1 HEAD, or at level 1, it is an operation delete under --request-id or an id made once per invocation and printed for resume. The level is the master's answer (--master), 0 without one or when it cannot be read, and every HEAD write runs at it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9c2722ace7
commit
53befc1a78
4 files changed
+241
-12
No files matched your search
@@ -338,7 +338,8 @@ func TestSnapshotDeletePublicAfterV11CommitStampsEveryIncarnation(t *testing.T)
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
committed, err := layout.RunCommit(ctx, st, st, v.Name, me, layout.CommitRequest{Session: "modern-cut", Prev: entries, Next: entries, Position: 3, Snapshot: snapshot, V11: true}, layout.CauseCommit)
|
||||
// A v1.1 holder commits at its attach level: a new cluster's 1.
|
||||
committed, err := layout.RunCommit(layout.WithFormatLevel(ctx, 1), st, st, v.Name, me, layout.CommitRequest{Session: "modern-cut", Prev: entries, Next: entries, Position: 3, Snapshot: snapshot, V11: true}, layout.CauseCommit)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
@@ -200,16 +201,55 @@ func chunkSnapshots(args []string, stdout, stderr io.Writer) int {
|
||||
})
|
||||
}
|
||||
|
||||
// readClusterFormatLevel reads the cluster's format level from the master
|
||||
// (a test replaces it).
|
||||
var readClusterFormatLevel = clusterFormatLevel
|
||||
|
||||
// snapshotDeleteFormatLevel is the cluster format level a snapshot delete
|
||||
// runs at: the master's answer, or 0 (v1.0 semantics, fail closed) without a
|
||||
// master or when it cannot be read.
|
||||
func snapshotDeleteFormatLevel(ctx context.Context, master string, stderr io.Writer) uint8 {
|
||||
if master == "" {
|
||||
return 0
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
level, err := readClusterFormatLevel(ctx, master)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "snapshot-delete: cluster format level unavailable (%v); running at level 0\n", err)
|
||||
return 0
|
||||
}
|
||||
if level > 1 {
|
||||
level = 1
|
||||
}
|
||||
return uint8(level)
|
||||
}
|
||||
|
||||
// newSnapshotDeleteRequestID is a fresh operation id, made once per
|
||||
// invocation of snapshot-delete without --request-id.
|
||||
func newSnapshotDeleteRequestID() (string, error) {
|
||||
var b [16]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "cli-" + hex.EncodeToString(b[:]), nil
|
||||
}
|
||||
|
||||
// chunkSnapshotDelete removes a named snapshot; refused while a restore
|
||||
// target depends on it.
|
||||
// target depends on it. At cluster format level 0 on a version-0 HEAD it is
|
||||
// v1.0's name-only delete, chosen before any operation record is written
|
||||
// (and as retry-unsafe as in v1.0). On a version-1 HEAD, or at level 1, it
|
||||
// is an operation delete: under --request-id, or else under an id made once
|
||||
// for this invocation and printed for resume. Every HEAD write runs at the
|
||||
// cluster's level (layout.WithFormatLevel).
|
||||
func chunkSnapshotDelete(args []string, stdout, stderr io.Writer) int {
|
||||
c := newBCommand("snapshot-delete", 30*time.Second, stdout, stderr)
|
||||
var vol volumeFlags
|
||||
var name, requestID string
|
||||
var name, requestID, autoID string
|
||||
var noWait bool
|
||||
c.bindVolume(&vol, "volume", "volume")
|
||||
c.fs.StringVar(&name, "name", "", "snapshot name")
|
||||
c.fs.StringVar(&requestID, "request-id", "", "stable identity for operation-based snapshot deletion; reuse on retry")
|
||||
c.fs.StringVar(&requestID, "request-id", "", "operation id for snapshot deletion; reuse on retry (made once per invocation when omitted)")
|
||||
c.fs.BoolVar(&noWait, "no-wait", false, "persist the delete operation without executing it; requires --request-id")
|
||||
var ref chunkstore.VolumeRef
|
||||
return c.run(args, true, func() (err error) {
|
||||
@@ -219,16 +259,35 @@ func chunkSnapshotDelete(args []string, stdout, stderr io.Writer) int {
|
||||
if noWait && requestID == "" {
|
||||
return errors.New("--no-wait requires --request-id")
|
||||
}
|
||||
if requestID == "" {
|
||||
if autoID, err = newSnapshotDeleteRequestID(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
ref, err = vol.ref("volume")
|
||||
return err
|
||||
}, func(ctx context.Context, cat *chunkstore.BCatalog) (any, error) {
|
||||
if requestID != "" {
|
||||
return chunkAdmitCatalogSnapshotDelete(ctx, cat, ref, requestID, name, noWait, stderr)
|
||||
level := snapshotDeleteFormatLevel(ctx, c.master, stderr)
|
||||
ctx = layout.WithFormatLevel(ctx, level)
|
||||
id := requestID
|
||||
if id == "" {
|
||||
st, err := cat.Store(ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
h, found, err := st.ReadHead(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !found || (h.Version < layout.HeadVersion1 && level < 1) {
|
||||
if _, err := cat.DeleteSnapshot(ctx, ref, name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]string{"volume": ref.Dir(), "name": name, "state": "absent"}, nil
|
||||
}
|
||||
id = autoID
|
||||
}
|
||||
if _, err := cat.DeleteSnapshot(ctx, ref, name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]string{"volume": ref.Dir(), "name": name, "state": "absent"}, nil
|
||||
return chunkAdmitCatalogSnapshotDelete(ctx, cat, ref, id, name, noWait, stderr)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,8 @@ var testIDs sync.Map
|
||||
func useMemChunkStore(t *testing.T, store *memChunkStore) {
|
||||
t.Helper()
|
||||
assumeSingleFilerFixture(t)
|
||||
prev, prevResolve, prevRestore := dialChunkFiler, resolveVolumeID, restoreCreateRPC
|
||||
prev, prevResolve, prevRestore, prevLevel := dialChunkFiler, resolveVolumeID, restoreCreateRPC, readClusterFormatLevel
|
||||
readClusterFormatLevel = func(context.Context, string) (uint32, error) { return 0, nil } // a test sets its own level
|
||||
dialChunkFiler = func(string) (chunkstore.Filer, func() error, error) { return store, func() error { return nil }, nil }
|
||||
resolveVolumeID = testResolve
|
||||
restoreCreateRPC = func(ctx context.Context, _ string, req *control.CreateVolumeRequest) (*control.CreateVolumeResponse, error) {
|
||||
@@ -80,7 +81,9 @@ func useMemChunkStore(t *testing.T, store *memChunkStore) {
|
||||
}
|
||||
return &control.CreateVolumeResponse{VolumeId: req.GetVolumeId(), LayoutId: tid.String(), StoreReady: true}, nil
|
||||
}
|
||||
t.Cleanup(func() { dialChunkFiler, resolveVolumeID, restoreCreateRPC = prev, prevResolve, prevRestore })
|
||||
t.Cleanup(func() {
|
||||
dialChunkFiler, resolveVolumeID, restoreCreateRPC, readClusterFormatLevel = prev, prevResolve, prevRestore, prevLevel
|
||||
})
|
||||
}
|
||||
|
||||
func testResolve(_ context.Context, _ string, name string) (layout.VolumeID, error) {
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore"
|
||||
filerpb "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/filerpb"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
|
||||
)
|
||||
|
||||
// opCountingStore counts every write attempt (a failed or torn one too)
|
||||
// under the snapshot-operation tree: its directory, INDEX and receipts.
|
||||
type opCountingStore struct {
|
||||
*memChunkStore
|
||||
writes atomic.Int64
|
||||
}
|
||||
|
||||
func (s *opCountingStore) count(dir, name string) {
|
||||
if strings.Contains(dir+"/", "/operations/") || name == "operations" {
|
||||
s.writes.Add(1)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *opCountingStore) CreateEntry(ctx context.Context, dir string, e *filerpb.Entry, oExcl bool) error {
|
||||
s.count(dir, e.GetName())
|
||||
return s.memChunkStore.CreateEntry(ctx, dir, e, oExcl)
|
||||
}
|
||||
|
||||
func (s *opCountingStore) UpdateEntry(ctx context.Context, dir string, e *filerpb.Entry, expected map[string][]byte) error {
|
||||
s.count(dir, e.GetName())
|
||||
return s.memChunkStore.UpdateEntry(ctx, dir, e, expected)
|
||||
}
|
||||
|
||||
func (s *opCountingStore) DeleteEntry(ctx context.Context, dir, name string, expected map[string][]byte) error {
|
||||
s.count(dir, name)
|
||||
return s.memChunkStore.DeleteEntry(ctx, dir, name, expected)
|
||||
}
|
||||
|
||||
// useCountingStore routes the CLI's filer through a write counter over mem
|
||||
// and sets the cluster format level the CLI reads.
|
||||
func useCountingStore(t *testing.T, mem *memChunkStore, level uint32) *opCountingStore {
|
||||
t.Helper()
|
||||
useMemChunkStore(t, mem)
|
||||
counting := &opCountingStore{memChunkStore: mem}
|
||||
dialChunkFiler = func(string) (chunkstore.Filer, func() error, error) {
|
||||
return counting, func() error { return nil }, nil
|
||||
}
|
||||
readClusterFormatLevel = func(context.Context, string) (uint32, error) { return level, nil }
|
||||
return counting
|
||||
}
|
||||
|
||||
// toVersion1 makes v's HEAD a version-1 HEAD whose snapshots are stamped, as
|
||||
// a v1.1 holder's first write leaves it.
|
||||
func toVersion1(t *testing.T, st *chunkstore.LayoutStore) layout.Head {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
h, _, err := st.ReadHead(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, s := range h.Snapshots {
|
||||
s.Gen = h.Gen + 1
|
||||
h.Snapshots[name] = s
|
||||
}
|
||||
h.Version = layout.HeadVersion1
|
||||
if err := st.UpdateHead(ctx, h.Gen, h, layout.CauseCommit); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h, _, err = st.ReadHead(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
// At cluster format level 0 a delete by name of a version-0 HEAD's snapshot
|
||||
// is v1.0's name-only delete: it succeeds, the HEAD stays version 0, and not
|
||||
// one write is attempted under the snapshot-operation tree.
|
||||
func TestSnapshotDeleteByNameAtLevel0OnAV0HeadIsTheV10Delete(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
mem := newMemChunkStore()
|
||||
v := cliVolume(t, mem, "v1", "daily")
|
||||
counting := useCountingStore(t, mem, 0)
|
||||
code, out, logs := runChunk(cliArgs("snapshot-delete", volArgs("volume", v), []string{"--name", "daily"})...)
|
||||
if code != 0 || !strings.Contains(out, `"state":"absent"`) {
|
||||
t.Fatalf("v1.0 delete by name: code=%d out=%s logs=%s", code, out, logs)
|
||||
}
|
||||
cat, _ := chunkstore.NewBCatalog(mem, mem, defaultChunkPathCLI)
|
||||
st, _ := cat.Store(v)
|
||||
h, _, err := st.ReadHead(ctx)
|
||||
if err != nil || h.Version != 0 || len(h.Snapshots) != 0 {
|
||||
t.Fatalf("after the v1.0 delete: %+v %v; want a version-0 HEAD without daily", h, err)
|
||||
}
|
||||
if n := counting.writes.Load(); n != 0 {
|
||||
t.Fatalf("the v1.0 delete attempted %d writes under the operation tree, want 0", n)
|
||||
}
|
||||
}
|
||||
|
||||
// On a version-1 HEAD a delete by name is an operation delete under an id the
|
||||
// CLI makes once for this invocation and prints, at level 0 as at level 1;
|
||||
// the HEAD stays version 1. At level 1 a version-0 HEAD takes the operation
|
||||
// route too, and its admission refuses typed and retryable until a v1.1
|
||||
// holder's first write stamps the generations; the HEAD is unchanged.
|
||||
func TestSnapshotDeleteByNameRoutesOnTheHeadAndTheLevel(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
for _, level := range []uint32{0, 1} {
|
||||
t.Run(map[uint32]string{0: "v1 head at level 0", 1: "v1 head at level 1"}[level], func(t *testing.T) {
|
||||
mem := newMemChunkStore()
|
||||
v := cliVolume(t, mem, "v1", "daily")
|
||||
cat, _ := chunkstore.NewBCatalog(mem, mem, defaultChunkPathCLI)
|
||||
st, _ := cat.Store(v)
|
||||
toVersion1(t, st)
|
||||
useCountingStore(t, mem, level)
|
||||
code, out, logs := runChunk(cliArgs("snapshot-delete", volArgs("volume", v), []string{"--name", "daily"})...)
|
||||
if code != 0 || !strings.Contains(logs, "request id cli-") {
|
||||
t.Fatalf("operation delete by name: code=%d out=%s logs=%s; want success under a printed cli- id", code, out, logs)
|
||||
}
|
||||
h, _, err := st.ReadHead(ctx)
|
||||
if err != nil || h.Version != layout.HeadVersion1 || len(h.Snapshots) != 0 {
|
||||
t.Fatalf("after the operation delete: %+v %v; want a version-1 HEAD without daily", h, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
t.Run("v0 head at level 1", func(t *testing.T) {
|
||||
mem := newMemChunkStore()
|
||||
v := cliVolume(t, mem, "v1", "daily")
|
||||
cat, _ := chunkstore.NewBCatalog(mem, mem, defaultChunkPathCLI)
|
||||
st, _ := cat.Store(v)
|
||||
before, _, err := st.ReadHead(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
useCountingStore(t, mem, 1)
|
||||
code, out, _ := runChunk(cliArgs("snapshot-delete", volArgs("volume", v), []string{"--name", "daily"})...)
|
||||
failure := chunkErrorOut(t, out)
|
||||
if code == 0 || failure.Code != "snapshot_generation_unavailable" || !failure.Retryable {
|
||||
t.Fatalf("level 1, version-0 HEAD: code=%d out=%s; want snapshot_generation_unavailable, retryable", code, out)
|
||||
}
|
||||
after, _, err := st.ReadHead(ctx)
|
||||
if err != nil || after.Gen != before.Gen || after.Version != 0 {
|
||||
t.Fatalf("the refused delete changed the HEAD: before %+v after %+v %v", before, after, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// An old client's name-only delete (the v1.0 call) of a version-1 HEAD's
|
||||
// snapshot refuses typed and writes nothing.
|
||||
func TestSnapshotDeleteOldClientNameOnlyRefusesAV1Head(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
mem := newMemChunkStore()
|
||||
v := cliVolume(t, mem, "v1", "daily")
|
||||
cat, _ := chunkstore.NewBCatalog(mem, mem, defaultChunkPathCLI)
|
||||
st, _ := cat.Store(v)
|
||||
before := toVersion1(t, st)
|
||||
if _, err := cat.DeleteSnapshot(ctx, v, "daily"); !errors.Is(err, layout.ErrSnapshotDeleteNeedsOperation) {
|
||||
t.Fatalf("an old client's name-only delete of a version-1 HEAD: %v; want snapshot_delete_requires_operation", err)
|
||||
}
|
||||
after, _, err := st.ReadHead(ctx)
|
||||
if err != nil || after.Gen != before.Gen {
|
||||
t.Fatalf("the refused delete wrote the HEAD: before gen %d after %+v %v", before.Gen, after, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user