From 0273c9d8aab6761e0b0b9ee2dd31f9deeea5d77d Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 07:12:27 -0800 Subject: [PATCH] fix: the delete reads a never-claimed initial HEAD as epoch 0 The master's HEAD reader refused the initial HEAD (epoch 0, no holder) as invalid, so the delete saw every never-started line as unreadable. It now returns that HEAD with ErrWorkspaceHeadUnclaimed: every caller that needs an identity still gets an error; the delete evidence reads it as epoch 0. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/core/authority/workspace.go | 7 ++++ seaweed-block/core/host/master/services.go | 3 ++ .../workspace_delete_never_started_test.go | 7 ++-- .../core/host/master/workspace_head.go | 6 +++ .../core/host/master/workspace_head_test.go | 39 +++++++++++++++++++ 5 files changed, 58 insertions(+), 4 deletions(-) diff --git a/seaweed-block/core/authority/workspace.go b/seaweed-block/core/authority/workspace.go index c87f1d031e..03bcabb9a0 100644 --- a/seaweed-block/core/authority/workspace.go +++ b/seaweed-block/core/authority/workspace.go @@ -19,6 +19,13 @@ type WorkspaceHead struct { FilerMtimeNS int64 } +// ErrWorkspaceHeadUnclaimed is a HEAD reader's answer for a volume's initial +// HEAD that no holder ever claimed (epoch 0, no holder, active): the head +// returned with it carries its generation and epoch 0. It names no identity, +// so a caller that needs one treats it as any read error; the delete +// admission reads it as never started (TERM-LIVE-HOLDER-001). +var ErrWorkspaceHeadUnclaimed = errors.New("HEAD never claimed: epoch 0, no holder") + // ErrWorkspaceOutcomeUncertain marks a move whose outcome is unknown: its // issuing write failed after it may have reached the store, or the volume's // workspace persistence is already uncertain. Memory shows it unissued, but diff --git a/seaweed-block/core/host/master/services.go b/seaweed-block/core/host/master/services.go index da59caffab..e094b60936 100644 --- a/seaweed-block/core/host/master/services.go +++ b/seaweed-block/core/host/master/services.go @@ -999,6 +999,9 @@ func (s *services) deleteHolderEvidence(ctx context.Context, vol string) (*autho readCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() head, err := s.host.cfg.WorkspaceHeadReader(readCtx, vol) + if errors.Is(err, authority.ErrWorkspaceHeadUnclaimed) { + return &head, owner // the initial HEAD: epoch 0, never claimed + } if err != nil { s.host.log.Printf("blockmaster: MarkVolumeDeleting volume=%q: HEAD unreadable (%v); a line without an ACK is refused", vol, err) return nil, owner diff --git a/seaweed-block/core/host/master/workspace_delete_never_started_test.go b/seaweed-block/core/host/master/workspace_delete_never_started_test.go index 6e1a836279..ad0413c992 100644 --- a/seaweed-block/core/host/master/workspace_delete_never_started_test.go +++ b/seaweed-block/core/host/master/workspace_delete_never_started_test.go @@ -27,11 +27,10 @@ func TestDeleteAdmitsANeverStartedLine(t *testing.T) { return authority.WorkspaceHead{}, errors.New("filer unreachable") } e := uint64(headEpoch.Load()) - holder := "" - if e > 0 { - holder = "node-a/r1" + if e == 0 { // the initial HEAD, as the filer reader returns it + return authority.WorkspaceHead{Gen: 2}, authority.ErrWorkspaceHeadUnclaimed } - return authority.WorkspaceHead{Gen: 2, Epoch: e, Holder: holder}, nil + return authority.WorkspaceHead{Gen: 2, Epoch: e, Holder: "node-a/r1"}, nil }, nil) ctx, cancel := context.WithTimeout(context.Background(), time.Minute) t.Cleanup(cancel) diff --git a/seaweed-block/core/host/master/workspace_head.go b/seaweed-block/core/host/master/workspace_head.go index 5c471123e2..aaf24eb252 100644 --- a/seaweed-block/core/host/master/workspace_head.go +++ b/seaweed-block/core/host/master/workspace_head.go @@ -83,6 +83,12 @@ func NewWorkspaceHEADReader(base string) (func(context.Context, string) (authori return authority.WorkspaceHead{}, err } // DecodeHeadDocument above owns the supported-format checks. + // The initial HEAD of a volume no holder ever claimed: epoch 0, no + // holder, active. It names no identity, so callers that need one get + // an error as before; the delete reads it as "never started". + if doc.Gen > 0 && doc.Epoch == 0 && doc.Holder == "" && doc.LegacyDetached == nil && doc.DetachedBy == nil && (doc.State == "" || doc.State == "active") { + return authority.WorkspaceHead{Gen: doc.Gen, FilerMtimeNS: headFilerMtimeNS(res.Header)}, authority.ErrWorkspaceHeadUnclaimed + } if doc.Gen == 0 || doc.Epoch == 0 || doc.Holder == "" || doc.LegacyDetached != nil || (doc.State != "" && doc.State != "active" && doc.State != "detached") { return authority.WorkspaceHead{}, fmt.Errorf("invalid HEAD identity/state") } diff --git a/seaweed-block/core/host/master/workspace_head_test.go b/seaweed-block/core/host/master/workspace_head_test.go index f48b8c434b..a59fc0cd89 100644 --- a/seaweed-block/core/host/master/workspace_head_test.go +++ b/seaweed-block/core/host/master/workspace_head_test.go @@ -2,9 +2,12 @@ package master import ( "context" + "errors" "net/http" "net/http/httptest" "testing" + + "github.com/seaweedfs/seaweed-block/core/authority" ) // These are decoder controls, not the W2 Detach/filer integration proof. @@ -43,3 +46,39 @@ func TestWorkspaceHEADCanonicalState(t *testing.T) { }) } } + +// The initial HEAD of a volume no holder ever claimed (epoch 0, no holder, +// active) reads as ErrWorkspaceHeadUnclaimed with its generation and epoch 0: +// still an error to every caller that needs an identity, and the delete's +// evidence of "never started". A claimed or detached HEAD is unchanged, and +// an epoch-0 HEAD with a holder or a detach proof stays invalid. +func TestWorkspaceHEADUnclaimed(t *testing.T) { + for _, tc := range []struct { + name, doc string + unclaimed bool + }{ + {"initial", `{"format":1,"gen":1,"epoch":0,"holder":"","remote_lsn":0}`, true}, + {"initial active", `{"format":1,"gen":1,"epoch":0,"holder":"","remote_lsn":0,"state":"active"}`, true}, + {"epoch 0 with a holder", `{"format":1,"gen":1,"epoch":0,"holder":"x/r1","remote_lsn":0}`, false}, + {"epoch 0 detached", `{"format":1,"gen":1,"epoch":0,"holder":"","remote_lsn":0,"state":"detached","detachedBy":{"holder":"","epoch":0,"gen":1}}`, false}, + } { + t.Run(tc.name, func(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte(tc.doc)) })) + defer s.Close() + read, err := NewWorkspaceHEADReader(s.URL) + if err != nil { + t.Fatal(err) + } + h, err := read(context.Background(), "v") + if got := errors.Is(err, authority.ErrWorkspaceHeadUnclaimed); got != tc.unclaimed { + t.Fatalf("unclaimed=%v (err %v); want %v", got, err, tc.unclaimed) + } + if tc.unclaimed && (h.Gen != 1 || h.Epoch != 0) { + t.Fatalf("unclaimed HEAD %+v; want its generation and epoch 0", h) + } + if !tc.unclaimed && err == nil { + t.Fatalf("HEAD %+v accepted; want an error", h) + } + }) + } +}