From f947bd40637a16b2ca4e2eb762bdedfa2e60a45b Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 06:12:20 -0800 Subject: [PATCH] volume, chunkstore: lsn_behind, closing 409 after detach, invalid_label, no cut stats Contract revisions from integration (B9 W2 appendix, 2026-11-15 14:30Z, and the rulings of 14:09Z): - a cut behind HEAD's remote_lsn answers lsn_behind (409, not retryable); - a detached layer answers closing with 409 (it stays closed until a new attach); a closed layer (process shutdown) stays closing 503; - an invalid label is invalid_label (400), and SnapshotReplay validates labels and consistency with the name and request id, so a bad request is refused before any cut; - the /chunk/cut body omits the cut statistics until W3 fills them. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/chunkstore/catalog.go | 16 +++++-- .../core/host/volume/chunk_control.go | 44 +++++++++---------- .../host/volume/chunk_control_order_test.go | 10 +++++ .../core/host/volume/chunk_control_test.go | 8 ++-- 4 files changed, 47 insertions(+), 31 deletions(-) diff --git a/seaweed-block/core/frontend/chunkstore/catalog.go b/seaweed-block/core/frontend/chunkstore/catalog.go index 9e0706b0fc..309dc8e6b2 100644 --- a/seaweed-block/core/frontend/chunkstore/catalog.go +++ b/seaweed-block/core/frontend/chunkstore/catalog.go @@ -93,6 +93,7 @@ const ( CodeDeadlineExceeded = "deadline_exceeded" CodeRemoteUnavailable = "remote_unavailable" CodeOutcomeUnknown = "outcome_unknown" + CodeInvalidLabel = "invalid_label" ) // CatalogError is a typed control-plane failure. @@ -311,15 +312,15 @@ func CheckSnapshotName(name string) error { func checkLabels(labels map[string]string) error { if len(labels) > maxLabels { - return catErr(CodeInvalidName, false, nil, "%d labels, at most %d", len(labels), maxLabels) + return catErr(CodeInvalidLabel, false, nil, "%d labels, at most %d", len(labels), maxLabels) } for k, v := range labels { if k == "" || len(k) > maxLabelKey || len(v) > maxLabelValue { - return catErr(CodeInvalidName, false, nil, "label %q: key 1-%d characters, value at most %d bytes", k, maxLabelKey, maxLabelValue) + return catErr(CodeInvalidLabel, false, nil, "label %q: key 1-%d characters, value at most %d bytes", k, maxLabelKey, maxLabelValue) } for _, r := range k { if !(r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '.' || r == '_' || r == '-') { - return catErr(CodeInvalidName, false, nil, "label key %q: only [a-z0-9._-]", k) + return catErr(CodeInvalidLabel, false, nil, "label key %q: only [a-z0-9._-]", k) } } } @@ -672,7 +673,8 @@ func (c *Catalog) CreateSnapshot(ctx context.Context, vol string, req SnapshotRe // the caller cuts (B9 W2 §3.2 step 1): when snaps/ exists with // req's request id it returns that snapshot (Replayed, true); with another // request id, snapshot_exists; when the name is free, (zero, false, nil). -// It validates the name and the request id first. +// It validates the whole request first (name, request id, labels, +// consistency), so a caller can refuse a bad request before it cuts. func (c *Catalog) SnapshotReplay(ctx context.Context, vol string, req SnapshotRequest) (SnapshotInfo, bool, error) { if _, err := c.volRoot(vol); err != nil { return SnapshotInfo{}, false, err @@ -683,6 +685,12 @@ func (c *Catalog) SnapshotReplay(ctx context.Context, vol string, req SnapshotRe if req.RequestID == "" { return SnapshotInfo{}, false, catErr(CodeInvalidName, false, nil, "snapshot %s/%s: a request id is required", vol, req.Name) } + if err := checkLabels(req.Labels); err != nil { + return SnapshotInfo{}, false, err + } + if cs := req.Consistency; cs != "" && cs != "crash" && cs != "app" { + return SnapshotInfo{}, false, catErr(CodeInvalidName, false, nil, "consistency %q: crash or app", cs) + } existing, prev, _, err := c.readSnap(ctx, vol, req.Name) switch { case err == nil && prev == req.RequestID: diff --git a/seaweed-block/core/host/volume/chunk_control.go b/seaweed-block/core/host/volume/chunk_control.go index f1095e15c4..0c5785ea0d 100644 --- a/seaweed-block/core/host/volume/chunk_control.go +++ b/seaweed-block/core/host/volume/chunk_control.go @@ -35,21 +35,15 @@ type ChunkControl interface { // ChunkCutResponse is the body of a successful POST /chunk/cut (§3.1). type ChunkCutResponse struct { - VolumeID string `json:"volumeID"` - CutLSN uint64 `json:"cutLSN"` - HeadGen uint64 `json:"headGen"` - HeadEpoch uint64 `json:"headEpoch"` - Holder string `json:"holder"` - Noop bool `json:"noop"` - FullHash bool `json:"fullHash"` - CandidateChunks int `json:"candidateChunks"` - UploadedChunks int `json:"uploadedChunks"` - UploadedBytes int64 `json:"uploadedBytes"` - ZeroedChunks int `json:"zeroedChunks"` - BarrierPauseNs int64 `json:"barrierPauseNs"` - UploadNs int64 `json:"uploadNs"` - CasNs int64 `json:"casNs"` - TotalNs int64 `json:"totalNs"` + VolumeID string `json:"volumeID"` + CutLSN uint64 `json:"cutLSN"` + HeadGen uint64 `json:"headGen"` + HeadEpoch uint64 `json:"headEpoch"` + Holder string `json:"holder"` + Noop bool `json:"noop"` + FullHash bool `json:"fullHash"` + // The cut statistics (§3.1) are added when W3 fills them; until then + // the body omits them rather than report zeros (appendix 14:30Z). } // ChunkSnapshotRequest is POST /chunk/snapshot's request (§3.2), without @@ -93,8 +87,8 @@ type chunkErrorBody struct { } // The codes the routes add to the catalog's (B9 §2.2 plus the 2026-11-15 -// rulings: awaiting_assignment, internal, and the transport errors -// bad_request, method_not_allowed, forbidden). +// rulings: awaiting_assignment, internal, lsn_behind, and the transport +// errors bad_request, method_not_allowed, forbidden). const ( codeVolumeNotServed = "volume_not_served" codeNotPrimary = "not_primary" @@ -104,6 +98,7 @@ const ( codeFenced = "fenced" codeHeadConflict = "head_conflict" codeClosing = "closing" + codeLSNBehind = "lsn_behind" codeInternal = "internal" codeBadRequest = "bad_request" codeMethodNotAllowed = "method_not_allowed" @@ -170,6 +165,8 @@ func chunkErrorCode(err error) (int, string, bool) { // An earlier CAS may still land: report that even when this // attempt was also refused (the re-proposal re-checks admission). return http.StatusServiceUnavailable, chunkstore.CodeOutcomeUnknown, true + case errors.Is(err, storage.ErrChunkLSNBehind): + return http.StatusConflict, codeLSNBehind, false case errors.Is(err, durable.ErrChunkAwaitingAuthority): return http.StatusConflict, codeAwaitingAssignment, true case errors.Is(err, durable.ErrChunkNotPrimary): @@ -182,8 +179,10 @@ func chunkErrorCode(err error) (int, string, bool) { return http.StatusConflict, codeHeadConflict, true case errors.Is(err, storage.ErrChunkRemoteUnavailable): return http.StatusServiceUnavailable, chunkstore.CodeRemoteUnavailable, true - case errors.Is(err, storage.ErrChunkLayerClosed), errors.Is(err, storage.ErrChunkLayerDetached): - return http.StatusServiceUnavailable, codeClosing, false + case errors.Is(err, storage.ErrChunkLayerDetached): + return http.StatusConflict, codeClosing, false // stays closed until a new attach + case errors.Is(err, storage.ErrChunkLayerClosed): + return http.StatusServiceUnavailable, codeClosing, false // the process is shutting down case errors.Is(err, durable.ErrNotChunkVolume), errors.Is(err, durable.ErrVolumeNotOpen): return http.StatusConflict, chunkstore.CodeNotChunkVolume, false case errors.Is(err, context.DeadlineExceeded): @@ -195,7 +194,7 @@ func chunkErrorCode(err error) (int, string, bool) { func catalogStatus(code string) int { switch code { - case chunkstore.CodeInvalidName: + case chunkstore.CodeInvalidName, chunkstore.CodeInvalidLabel: return http.StatusBadRequest case chunkstore.CodeSnapshotNotFound: return http.StatusNotFound @@ -434,11 +433,8 @@ func (c *chunkControl) Cut(ctx context.Context, volumeID string, minLSN uint64) if err != nil { return ChunkCutResponse{}, err } - st := r.Stats return ChunkCutResponse{VolumeID: volumeID, CutLSN: r.H, HeadGen: r.Head.Gen, HeadEpoch: r.Head.Epoch, Holder: r.Head.Holder, - Noop: r.Noop, FullHash: r.FullHash, CandidateChunks: st.CandidateChunks, UploadedChunks: st.UploadedChunks, - UploadedBytes: st.UploadedBytes, ZeroedChunks: st.ZeroedChunks, BarrierPauseNs: st.BarrierPauseNs, - UploadNs: st.UploadNs, CasNs: st.CASNs, TotalNs: st.TotalNs}, nil + Noop: r.Noop, FullHash: r.FullHash}, nil } // Snapshot resolves the request against the catalog first (§3.2 step 1): diff --git a/seaweed-block/core/host/volume/chunk_control_order_test.go b/seaweed-block/core/host/volume/chunk_control_order_test.go index c7b0c52fe0..87f19228ea 100644 --- a/seaweed-block/core/host/volume/chunk_control_order_test.go +++ b/seaweed-block/core/host/volume/chunk_control_order_test.go @@ -86,3 +86,13 @@ func TestChunkControl_CutToUnwrittenMinLSNPublishesNothing(t *testing.T) { t.Fatalf("cut to the last written LSN: %+v %v", r, err) } } + +// A snapshot request with invalid labels is refused with invalid_label before +// any cut. +func TestChunkControl_InvalidLabelRefusedBeforeCutting(t *testing.T) { + fp, ctl := adapterFixture(t) + _, err := ctl.Snapshot(context.Background(), "v1", ChunkSnapshotRequest{Name: "s1", RequestID: "r", Labels: map[string]string{"Bad Key": "x"}}) + if chunkstore.CodeOf(err) != chunkstore.CodeInvalidLabel || fp.cuts != 0 { + t.Fatalf("snapshot with an invalid label: %v (cuts %d); want invalid_label without a cut", err, fp.cuts) + } +} diff --git a/seaweed-block/core/host/volume/chunk_control_test.go b/seaweed-block/core/host/volume/chunk_control_test.go index e1535208cf..d2700ce698 100644 --- a/seaweed-block/core/host/volume/chunk_control_test.go +++ b/seaweed-block/core/host/volume/chunk_control_test.go @@ -152,8 +152,8 @@ func TestStatusServer_ChunkSuccessBodiesUseSpecFields(t *testing.T) { ctl.deadline < chunkstore.DefaultOpDeadline-time.Minute || ctl.deadline > chunkstore.DefaultOpDeadline { t.Fatalf("cut with a huge timeoutMs: %d (deadline %v), want OpDeadline", w.Code, ctl.deadline) } - wantKeys(t, "cut", jsonKeys(t, w.Body.Bytes()), "volumeID", "cutLSN", "headGen", "headEpoch", "holder", "noop", "fullHash", - "candidateChunks", "uploadedChunks", "uploadedBytes", "zeroedChunks", "barrierPauseNs", "uploadNs", "casNs", "totalNs") + // The cut statistics are omitted until W3 fills them (appendix 14:30Z). + wantKeys(t, "cut", jsonKeys(t, w.Body.Bytes()), "volumeID", "cutLSN", "headGen", "headEpoch", "holder", "noop", "fullHash") w = serve(t, s, http.MethodPost, "/chunk/snapshot", `{"volumeID":"v1","name":"s1","requestID":"r","labels":{"agent.step":"x"},"consistency":"app","expiresAtNs":9}`, "") if w.Code != 200 || ctl.lastSnap.Name != "s1" || ctl.lastSnap.RequestID != "r" || ctl.lastSnap.Consistency != "app" || ctl.lastSnap.Labels["agent.step"] != "x" || ctl.lastSnap.ExpiresAtNs != 9 { @@ -211,7 +211,9 @@ func TestStatusServer_ChunkErrorBodyIsTyped(t *testing.T) { {fmt.Errorf("x: %w", storage.ErrChunkHeadPrecondition), "head_conflict", 409, true}, {fmt.Errorf("x: %w", storage.ErrChunkRemoteUnavailable), "remote_unavailable", 503, true}, {fmt.Errorf("x: %w", storage.ErrChunkLayerClosed), "closing", 503, false}, - {fmt.Errorf("x: %w", storage.ErrChunkLayerDetached), "closing", 503, false}, + {fmt.Errorf("x: %w", storage.ErrChunkLayerDetached), "closing", 409, false}, + {fmt.Errorf("%w: %w: cut LSN 1, HEAD remote_lsn 10", storage.ErrChunkCaptureRefused, storage.ErrChunkLSNBehind), "lsn_behind", 409, false}, + {&chunkstore.CatalogError{Code: chunkstore.CodeInvalidLabel, Message: "m"}, "invalid_label", 400, false}, {fmt.Errorf("x: %w", durable.ErrNotChunkVolume), "not_chunk_volume", 409, false}, {&chunkstore.CatalogError{Code: chunkstore.CodeSnapshotExists, Message: "m"}, "snapshot_exists", 409, false}, {&chunkstore.CatalogError{Code: chunkstore.CodeSnapshotNotFound, Message: "m"}, "snapshot_not_found", 404, false},