mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
chunkstore: export GC's per-key reclaim step as ChunkRemote.ReclaimKey
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
29dab5e1d7
commit
b657923019
3 files changed
+102
-17
No files matched your search
@@ -218,6 +218,32 @@ func (r *ChunkRemote) Get(ctx context.Context, key string) ([]byte, error) {
|
||||
return r.b.Get(ctx, fid)
|
||||
}
|
||||
|
||||
// ReclaimKey deletes the needle bound to one of this volume's own keys
|
||||
// with GC's step (reclaimBinding): the binding is tombstoned, the exact fid
|
||||
// deleted, the binding kept for ever and marked done. It decides nothing:
|
||||
// the caller must know that no HEAD, snapshot, fork or intent references
|
||||
// key. An owner-qualified key is refused.
|
||||
func (r *ChunkRemote) ReclaimKey(ctx context.Context, key string, blobs BlobDeleter) error {
|
||||
if err := storage.ValidChunkKey(key); err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrInvalidKey, err)
|
||||
}
|
||||
if strings.HasPrefix(key, "@") {
|
||||
return fmt.Errorf("%w: %q is owner-qualified; a volume reclaims only its own keys", ErrInvalidKey, key)
|
||||
}
|
||||
name, err := keyName(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e, err := r.f.LookupEntry(ctx, r.keyDir(), name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("chunkstore: chunk key %q: %w", key, err)
|
||||
}
|
||||
if len(e.GetChunks()) != 0 || string(e.Extended[extKind]) != kindChunkKey || len(e.Extended[extFid]) == 0 {
|
||||
return fmt.Errorf("chunkstore: chunk key %q: entry is not a chunk key binding", key)
|
||||
}
|
||||
return reclaimBinding(ctx, r.f, blobs, r.keyDir(), e)
|
||||
}
|
||||
|
||||
// Delete is not supported; see ErrDeleteUnsupported.
|
||||
func (r *ChunkRemote) Delete(context.Context, string) error { return ErrDeleteUnsupported }
|
||||
|
||||
|
||||
@@ -331,23 +331,7 @@ func (g *Collector) run(ctx context.Context, apply bool) (result GCResult, err e
|
||||
if err = ctx.Err(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
// Preserve the immutable binding forever: removing it would allow Put
|
||||
// to rebind a key that once existed. The tombstone also permits retry
|
||||
// after a failed physical delete without losing the fid.
|
||||
if string(b.entry.Extended[extGCDeleted]) != "1" {
|
||||
e := proto.Clone(b.entry).(*filerpb.Entry)
|
||||
e.Extended[extGCDeleted] = []byte("1")
|
||||
expected := map[string][]byte{extKind: []byte(kindChunkKey), extFid: b.entry.Extended[extFid], extGCDeleted: nil}
|
||||
if err = g.cat.f.UpdateEntry(ctx, path.Dir(p), e, expected); err != nil {
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
if err = g.blobs.DeleteBlob(ctx, string(b.entry.Extended[extFid])); err != nil {
|
||||
return result, err
|
||||
}
|
||||
e := proto.Clone(b.entry).(*filerpb.Entry)
|
||||
e.Extended[extGCDeleted] = []byte("done")
|
||||
if err = g.cat.f.UpdateEntry(ctx, path.Dir(p), e, map[string][]byte{extFid: b.entry.Extended[extFid], extGCDeleted: []byte("1")}); err != nil {
|
||||
if err = reclaimBinding(ctx, g.cat.f, g.blobs, path.Dir(p), b.entry); err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Reclaimed++
|
||||
@@ -388,3 +372,31 @@ func (b gcBlobReader) Get(ctx context.Context, fid string) ([]byte, error) {
|
||||
}
|
||||
return reader.Get(ctx, fid)
|
||||
}
|
||||
|
||||
// reclaimBinding deletes the needle a key binding names, keeping the binding
|
||||
// for ever: removing it would allow Put to rebind a key that once existed.
|
||||
// The binding first gets a conditional swb.gc.deleted=1 tombstone (which
|
||||
// also lets a failed physical delete be retried without losing the fid),
|
||||
// then the exact fid is deleted, then the tombstone becomes done. A binding
|
||||
// already done is left as it is. The caller decides that nothing references
|
||||
// the key.
|
||||
func reclaimBinding(ctx context.Context, f Filer, blobs BlobDeleter, dir string, entry *filerpb.Entry) error {
|
||||
switch string(entry.Extended[extGCDeleted]) {
|
||||
case "done":
|
||||
return nil
|
||||
case "1":
|
||||
default:
|
||||
e := proto.Clone(entry).(*filerpb.Entry)
|
||||
e.Extended[extGCDeleted] = []byte("1")
|
||||
expected := map[string][]byte{extKind: []byte(kindChunkKey), extFid: entry.Extended[extFid], extGCDeleted: nil}
|
||||
if err := f.UpdateEntry(ctx, dir, e, expected); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := blobs.DeleteBlob(ctx, string(entry.Extended[extFid])); err != nil {
|
||||
return err
|
||||
}
|
||||
e := proto.Clone(entry).(*filerpb.Entry)
|
||||
e.Extended[extGCDeleted] = []byte("done")
|
||||
return f.UpdateEntry(ctx, dir, e, map[string][]byte{extFid: entry.Extended[extFid], extGCDeleted: []byte("1")})
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package chunkstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/storage"
|
||||
)
|
||||
|
||||
// ReclaimKey is GC's step for one own key: the binding is tombstoned and
|
||||
// kept, the needle deleted; repeating it is a no-op; a qualified key is
|
||||
// refused.
|
||||
func TestChunkRemote_ReclaimKey(t *testing.T) {
|
||||
f, b := newFakeFiler(), newFakeBlobs()
|
||||
r, err := NewChunkRemote(f, b, catBase+"/v1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
if err := r.Put(ctx, "k1", []byte("payload")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fid := r.fids["k1"]
|
||||
if err := r.ReclaimKey(ctx, "k1", gcBlobs{b}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e, err := f.LookupEntry(ctx, r.keyDir(), "k1")
|
||||
if err != nil {
|
||||
t.Fatalf("binding removed: %v", err)
|
||||
}
|
||||
if string(e.Extended[extGCDeleted]) != "done" || string(e.Extended[extFid]) != fid {
|
||||
t.Fatalf("binding after reclaim: %q fid %q; want done, same fid", e.Extended[extGCDeleted], e.Extended[extFid])
|
||||
}
|
||||
if _, err := b.Get(ctx, fid); err == nil {
|
||||
t.Fatal("needle still readable after reclaim")
|
||||
}
|
||||
if err := r.ReclaimKey(ctx, "k1", gcBlobs{b}); err != nil {
|
||||
t.Fatalf("repeated reclaim: %v", err)
|
||||
}
|
||||
if err := r.Put(ctx, "k1", []byte("again")); !errors.Is(err, storage.ErrChunkKeyExists) {
|
||||
t.Fatalf("rebinding a reclaimed key: %v", err)
|
||||
}
|
||||
if err := r.ReclaimKey(ctx, "@v0/k1", gcBlobs{b}); err == nil {
|
||||
t.Fatal("reclaim of an owner-qualified key accepted")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user