mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
Merge pull request #167 from seaweedfs/fix/workspace-moveback-claim
durable: refuse to attach an old local root under another holder's HEAD (P0, #165)
This commit is contained in:
commit
cc887bae37
2 files changed
+295
No files matched your search
@@ -43,6 +43,15 @@ var (
|
||||
ErrChunkEpochStale = errors.New("durable: chunk layer lineage moved since attach")
|
||||
)
|
||||
|
||||
// ErrChunkStaleLocalRoot refuses to attach an existing local volume whose
|
||||
// HEAD another holder has published: the local image predates that HEAD
|
||||
// (a workspace that moved away and back), so serving or cutting it would
|
||||
// lose the other holder's acknowledged writes. It wraps
|
||||
// storage.ErrChunkLayerFenced: the volume is not served and the attach is
|
||||
// not retried. Removing the local volume lets the next attach start fresh
|
||||
// from HEAD.
|
||||
var ErrChunkStaleLocalRoot = errors.New("durable: local volume predates a HEAD published by another holder")
|
||||
|
||||
var (
|
||||
errProviderClosed = errors.New("durable: provider closed")
|
||||
errChunkNoAuthority = errors.New("durable: projection carries no master authority for the chunk layer")
|
||||
@@ -171,6 +180,13 @@ func (p *DurableProvider) chunkAuthorityWait(volumeID string, h *volHandle, gen
|
||||
return
|
||||
case errors.Is(err, storage.ErrChunkLayerFenced):
|
||||
log.Printf("durable: chunk layer for volume=%s: %v (not serving)", volumeID, err)
|
||||
if errors.Is(err, ErrChunkStaleLocalRoot) {
|
||||
h.attachMu.Lock()
|
||||
if p.current(h, gen) && h.chunk == nil {
|
||||
h.backend.SetOperational(false, "chunk layer attach refused: "+err.Error())
|
||||
}
|
||||
h.attachMu.Unlock()
|
||||
}
|
||||
return
|
||||
case errors.Is(err, errChunkNoAuthority):
|
||||
continue
|
||||
@@ -283,6 +299,23 @@ func (p *DurableProvider) newChunkLayer(ctx context.Context, volumeID string, h
|
||||
return nil
|
||||
},
|
||||
}
|
||||
if !h.fresh {
|
||||
// An existing local volume is this holder's image only while HEAD is
|
||||
// still this holder's publication. Another holder's HEAD means the
|
||||
// workspace moved away and back: refuse rather than serve, or cut
|
||||
// over, the newer image with the stale one.
|
||||
head, err := remote.GetHead(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("durable: read HEAD before attaching existing volume %s: %w", volumeID, err)
|
||||
}
|
||||
if head.Gen != 0 && head.Holder != holder {
|
||||
err := fmt.Errorf("%w: %w: volume %s local root %s, HEAD holder %q gen %d epoch %d, local holder %q; remove or quarantine the local root to attach fresh from HEAD",
|
||||
ErrChunkStaleLocalRoot, storage.ErrChunkLayerFenced, volumeID, h.path, head.Holder, head.Gen, head.Epoch, holder)
|
||||
log.Printf("durable: refusing to attach volume=%s root=%s: HEAD holder=%q gen=%d epoch=%d is not this holder %q (remove or quarantine the local root)",
|
||||
volumeID, h.path, head.Holder, head.Gen, head.Epoch, holder)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if h.fresh {
|
||||
// Attach preparation is gated. Seed the exact observed HEAD before
|
||||
// its takeover CAS; only then finalize lazy attachment and expose I/O.
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
package durable_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend"
|
||||
"github.com/seaweedfs/seaweed-block/core/frontend/durable"
|
||||
"github.com/seaweedfs/seaweed-block/core/storage"
|
||||
)
|
||||
|
||||
func viewAs(replica string, epoch uint64) *stubView {
|
||||
return newStubView(healthyProj(frontend.Identity{VolumeID: "v1", ReplicaID: replica, Epoch: epoch, EndpointVersion: 1}))
|
||||
}
|
||||
|
||||
func writeSync(t *testing.T, s storage.LogicalStorage, lba uint32, fill byte) {
|
||||
t.Helper()
|
||||
if _, err := s.Write(lba, block(fill)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func headHolder(t *testing.T, remote *storage.MemChunkRemote) storage.ChunkHead {
|
||||
t.Helper()
|
||||
h, err := remote.GetHead(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
// movedAwayAndBack runs A (epoch 3) cut then uncut synced writes, A lost;
|
||||
// B (epoch 4, fresh) claims, writes LBA 0 = 0xB1, syncs, cuts, closes. It
|
||||
// returns the remote and A's surviving root.
|
||||
func movedAwayAndBack(t *testing.T) (*storage.MemChunkRemote, string) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
remote := storage.NewMemChunkRemote()
|
||||
rootA, rootB := t.TempDir(), t.TempDir()
|
||||
|
||||
// 1. A (epoch 3) cuts LBA 0 and LBA 16, then keeps writing without a cut.
|
||||
pA := chunkProvider(t, rootA, remote, viewAs("rA", 3), 0)
|
||||
sA := openRecovered(t, pA)
|
||||
writeSync(t, sA, 0, 0xA1)
|
||||
writeSync(t, sA, 16, 0xA1)
|
||||
if _, err := pA.CaptureVolume(ctx, "v1"); err != nil {
|
||||
t.Fatalf("A capture: %v", err)
|
||||
}
|
||||
writeSync(t, sA, 0, 0xA2) // never cut
|
||||
if err := pA.Close(); err != nil { // A is lost; its root stays on disk
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 2. A forced move to B (epoch 4, fresh root): B claims HEAD, writes,
|
||||
// syncs and cuts.
|
||||
pB := chunkProvider(t, rootB, remote, viewAs("rB", 4), 0)
|
||||
sB := openRecovered(t, pB)
|
||||
writeSync(t, sB, 0, 0xB1)
|
||||
if _, err := pB.CaptureVolume(ctx, "v1"); err != nil {
|
||||
t.Fatalf("B capture: %v", err)
|
||||
}
|
||||
if err := pB.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return remote, rootA
|
||||
}
|
||||
|
||||
// A workspace moves A -> B -> A, and A's old durable root is still on disk.
|
||||
// A must not serve its stale image or cut it over B's acknowledged, synced
|
||||
// and published writes: the existing root is refused (typed, visible, not
|
||||
// served, HEAD unchanged). Once the old root is removed, A attaches fresh
|
||||
// from HEAD and reads B's data.
|
||||
func TestWALStoreChunk_MoveBackOntoAnOldRootIsRefused(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
remote, rootA := movedAwayAndBack(t)
|
||||
before := headHolder(t, remote)
|
||||
|
||||
// 3. Back to A (epoch 5) over its old root: refused.
|
||||
pA2 := chunkProvider(t, rootA, remote, viewAs("rA", 5), 0)
|
||||
if _, err := pA2.EnsureStorage("v1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := pA2.RecoverVolume(ctx, "v1")
|
||||
if !errors.Is(err, durable.ErrChunkStaleLocalRoot) || !errors.Is(err, storage.ErrChunkLayerFenced) {
|
||||
t.Fatalf("move back onto the old root: %v; want ErrChunkStaleLocalRoot (fenced)", err)
|
||||
}
|
||||
b, err := pA2.Open(ctx, "v1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := b.Read(ctx, 0, make([]byte, 4096)); !errors.Is(err, frontend.ErrNotReady) {
|
||||
t.Errorf("read after a refused move back: %v; want ErrNotReady", err)
|
||||
}
|
||||
if _, err := b.Write(ctx, 0, block(0xA3)); !errors.Is(err, frontend.ErrNotReady) {
|
||||
t.Errorf("write after a refused move back: %v; want ErrNotReady", err)
|
||||
}
|
||||
if _, err := pA2.CaptureVolume(ctx, "v1"); err == nil {
|
||||
t.Error("a refused move back still cuts")
|
||||
}
|
||||
var evidence string
|
||||
for _, st := range pA2.DurableStatuses() {
|
||||
if st.VolumeID == "v1" {
|
||||
evidence = st.Evidence
|
||||
}
|
||||
}
|
||||
if !strings.Contains(evidence, "remove or quarantine the local root") || !strings.Contains(evidence, rootA) {
|
||||
t.Errorf("status evidence %q does not tell the operator what to do with root %s", evidence, rootA)
|
||||
}
|
||||
if after := headHolder(t, remote); after.Gen != before.Gen || after.Holder != before.Holder || after.Epoch != before.Epoch {
|
||||
t.Errorf("HEAD changed by a refused move back: %+v -> %+v", before, after)
|
||||
}
|
||||
if h := headBlock(t, remote, 0); !bytes.Equal(h, block(0xB1)) {
|
||||
t.Errorf("HEAD LBA 0 = %02x after a refused move back; want B's %02x", h[0], 0xB1)
|
||||
}
|
||||
if err := pA2.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 4. The operator removes the old root: A attaches fresh and serves B's
|
||||
// image (and A's own earlier published chunk).
|
||||
entries, err := os.ReadDir(rootA)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, e := range entries {
|
||||
if err := os.RemoveAll(filepath.Join(rootA, e.Name())); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
pA3 := chunkProvider(t, rootA, remote, viewAs("rA", 5), 0)
|
||||
sA3 := openRecovered(t, pA3)
|
||||
if got, err := sA3.Read(0); err != nil || !bytes.Equal(got, block(0xB1)) {
|
||||
t.Fatalf("fresh attach after removing the root: LBA 0 err %v; want B's %02x", err, 0xB1)
|
||||
}
|
||||
if got, err := sA3.Read(16); err != nil || !bytes.Equal(got, block(0xA1)) {
|
||||
t.Fatalf("fresh attach after removing the root: LBA 16 err %v; want the published %02x", err, 0xA1)
|
||||
}
|
||||
}
|
||||
|
||||
// The same holder reattaching its own root at a newer epoch is not a move
|
||||
// back: it attaches, serves its data and cuts.
|
||||
func TestWALStoreChunk_SameHolderReattachAtNewerEpoch(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
remote := storage.NewMemChunkRemote()
|
||||
root := t.TempDir()
|
||||
p := chunkProvider(t, root, remote, viewAs("rA", 3), 0)
|
||||
s := openRecovered(t, p)
|
||||
writeSync(t, s, 0, 0xC1)
|
||||
if _, err := p.CaptureVolume(ctx, "v1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeSync(t, s, 32, 0xC2) // local only
|
||||
if err := p.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p2 := chunkProvider(t, root, remote, viewAs("rA", 4), 0)
|
||||
s2 := openRecovered(t, p2)
|
||||
if got, _ := s2.Read(32); !bytes.Equal(got, block(0xC2)) {
|
||||
t.Fatal("same-holder reattach lost its local write")
|
||||
}
|
||||
if _, err := p2.CaptureVolume(ctx, "v1"); err != nil {
|
||||
t.Fatalf("same-holder reattach cannot cut: %v", err)
|
||||
}
|
||||
if !bytes.Equal(headBlock(t, remote, 32), block(0xC2)) {
|
||||
t.Fatal("HEAD lacks the reattached holder's write")
|
||||
}
|
||||
}
|
||||
|
||||
// An existing root with no HEAD at all (generation 0) is this holder's own
|
||||
// image: it attaches.
|
||||
func TestWALStoreChunk_ExistingRootWithoutHeadAttaches(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
p := chunkProvider(t, root, storage.NewMemChunkRemote(), viewAs("rA", 3), 0)
|
||||
s := openRecovered(t, p)
|
||||
writeSync(t, s, 0, 0xD1)
|
||||
if err := p.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
empty := storage.NewMemChunkRemote() // no HEAD
|
||||
if h := headHolder(t, empty); h.Gen != 0 {
|
||||
t.Fatalf("empty remote HEAD gen %d", h.Gen)
|
||||
}
|
||||
p2 := chunkProvider(t, root, empty, viewAs("rA", 3), 0)
|
||||
s2 := openRecovered(t, p2)
|
||||
if got, _ := s2.Read(0); !bytes.Equal(got, block(0xD1)) {
|
||||
t.Fatal("existing root without a HEAD lost its data")
|
||||
}
|
||||
}
|
||||
|
||||
// A holder whose HEAD a newer epoch has taken over never cuts over it.
|
||||
func TestWALStoreChunk_CutRefusedAfterANewerEpochTakesOver(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
remote := storage.NewMemChunkRemote()
|
||||
pA := chunkProvider(t, t.TempDir(), remote, viewAs("rA", 3), 0)
|
||||
sA := openRecovered(t, pA)
|
||||
writeSync(t, sA, 0, 0xE1)
|
||||
if _, err := pA.CaptureVolume(ctx, "v1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pB := chunkProvider(t, t.TempDir(), remote, viewAs("rB", 4), 0)
|
||||
openRecovered(t, pB) // takeover raises HEAD to epoch 4
|
||||
before := headHolder(t, remote)
|
||||
if before.Epoch != 4 || before.Holder != "rB" {
|
||||
t.Fatalf("takeover HEAD %+v", before)
|
||||
}
|
||||
writeSync(t, sA, 0, 0xE2)
|
||||
if _, err := pA.CaptureVolume(ctx, "v1"); err == nil {
|
||||
t.Fatal("the epoch-3 holder cut over the epoch-4 HEAD")
|
||||
}
|
||||
if after := headHolder(t, remote); after.Gen != before.Gen || after.Holder != "rB" {
|
||||
t.Fatalf("HEAD moved after the stale holder's cut: %+v -> %+v", before, after)
|
||||
}
|
||||
if !bytes.Equal(headBlock(t, remote, 0), block(0xE1)) {
|
||||
t.Fatal("HEAD lost the published image")
|
||||
}
|
||||
}
|
||||
|
||||
// The refusal is visible on the deferred path too: A recovers before the
|
||||
// master's assignment, and the attach that runs when it arrives is refused
|
||||
// with the same evidence.
|
||||
func TestWALStoreChunk_MoveBackRefusedOnDeferredAttach(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
remote, rootA := movedAwayAndBack(t)
|
||||
before := headHolder(t, remote)
|
||||
view := newStubView(frontend.Projection{})
|
||||
pA2 := chunkProvider(t, rootA, remote, view, 0)
|
||||
if _, err := pA2.EnsureStorage("v1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pA2.RecoverVolume(ctx, "v1"); err != nil {
|
||||
t.Fatalf("recovery before the assignment: %v", err)
|
||||
}
|
||||
view.set(healthyProj(frontend.Identity{VolumeID: "v1", ReplicaID: "rA", Epoch: 5, EndpointVersion: 1}))
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
var st durable.VolumeStatus
|
||||
for time.Now().Before(deadline) {
|
||||
for _, s := range pA2.DurableStatuses() {
|
||||
if s.VolumeID == "v1" {
|
||||
st = s
|
||||
}
|
||||
}
|
||||
if strings.Contains(st.Evidence, "remove or quarantine the local root") {
|
||||
break
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
if st.Operational || !strings.Contains(st.Evidence, "remove or quarantine the local root") {
|
||||
t.Fatalf("deferred attach of the old root: operational %t evidence %q", st.Operational, st.Evidence)
|
||||
}
|
||||
if after := headHolder(t, remote); after.Gen != before.Gen || after.Holder != before.Holder {
|
||||
t.Fatalf("HEAD changed by a refused deferred attach: %+v -> %+v", before, after)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user