From 1a77477892bcdcdd12ea47e3e9c475bb89b36a4c Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 23 Nov 2026 01:04:10 -0800 Subject: [PATCH] fix(hostlimit): uploads go unpaced only while every live lease is a fallback lease; wrapper test A healthy lease admitted beside an urgent fallback lease kept no pacing (review). The pacing error wrapper now has a test: prefix, cause, no put. Co-Authored-By: Claude Opus 5.5 --- .../durable/chunk_upload_pacing_error_test.go | 25 ++++++++++++++++ seaweed-block/core/hostlimit/limiter.go | 12 ++++++-- seaweed-block/core/hostlimit/limiter_test.go | 30 +++++++++++++++++++ seaweed-block/docs/design/invariants.md | 2 +- 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 seaweed-block/core/frontend/durable/chunk_upload_pacing_error_test.go diff --git a/seaweed-block/core/frontend/durable/chunk_upload_pacing_error_test.go b/seaweed-block/core/frontend/durable/chunk_upload_pacing_error_test.go new file mode 100644 index 0000000000..72f02b39bd --- /dev/null +++ b/seaweed-block/core/frontend/durable/chunk_upload_pacing_error_test.go @@ -0,0 +1,25 @@ +package durable + +import ( + "context" + "errors" + "strings" + "testing" + + "github.com/seaweedfs/seaweed-block/core/storage" +) + +// A pacing refusal before a chunk put names the host limiter, keeps its cause +// and sends no put (issue 486: it was reported as the chunk remote). +func TestChunkUploadPacingErrorNamesTheLimiter(t *testing.T) { + inner := storage.NewMemChunkRemote() + bound := &closeBoundRemote{ChunkRemote: inner, done: context.Background(), + waitUpload: func(context.Context, int) error { return context.DeadlineExceeded }} + err := bound.Put(context.Background(), "chunk-1", []byte("x")) + if !errors.Is(err, context.DeadlineExceeded) || !strings.HasPrefix(err.Error(), "hostlimit: upload pacing before put chunk-1: ") { + t.Fatalf("pacing error: %v", err) + } + if _, err := inner.Get(context.Background(), "chunk-1"); err == nil { + t.Fatal("a chunk was put although pacing refused it") + } +} diff --git a/seaweed-block/core/hostlimit/limiter.go b/seaweed-block/core/hostlimit/limiter.go index 494d293d60..1861fdb34b 100644 --- a/seaweed-block/core/hostlimit/limiter.go +++ b/seaweed-block/core/hostlimit/limiter.go @@ -469,9 +469,15 @@ func (l *Limiter) WaitUpload(ctx context.Context, n int) error { l.mu.Unlock() return ErrRevoked } - if l.fallback { - // Only an urgent cut runs on the fallback, and with the shared - // pool unusable there is no host cap to protect: not paced. + // Only urgent cuts run on the fallback, and with the shared pool + // unusable there is no host cap to protect: while every live lease + // is a fallback lease, uploads are not paced. A healthy lease beside + // one keeps its pacing. + onlyFallback := l.fallback && len(l.live) > 0 + for _, held := range l.live { + onlyFallback = onlyFallback && held.fallback + } + if onlyFallback { l.mu.Unlock() return nil } diff --git a/seaweed-block/core/hostlimit/limiter_test.go b/seaweed-block/core/hostlimit/limiter_test.go index 8e43876c9b..05b23eb02a 100644 --- a/seaweed-block/core/hostlimit/limiter_test.go +++ b/seaweed-block/core/hostlimit/limiter_test.go @@ -656,3 +656,33 @@ func TestUrgentFallbackUploadIsNotPaced(t *testing.T) { } } } + +// While an urgent cut is on the fallback, a healthy cut admitted after the +// pool came back is still paced: only fallback leases alone go unpaced. +func TestFallbackDoesNotUnpaceAHealthyLease(t *testing.T) { + c := testConfig(t) + c.BytesPerSecond = 16 << 20 + c.Directory += "-missing" + l := newTest(t, c) + urgent, err := l.Acquire(context.Background(), Urgent, nil) + if err != nil || !l.Snapshot().(Stats).FallbackActive { + t.Fatalf("urgent fallback: id=%d err=%v", urgent, err) + } + defer l.Release(urgent) + if err := os.Mkdir(c.Directory, 0o700); err != nil { + t.Fatal(err) + } + normal, err := l.Admit(context.Background(), 1, nil) + if err != nil { + t.Fatalf("healthy normal admission after the pool came back: %v", err) + } + defer l.Release(normal) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + if err := l.WaitUpload(ctx, 16<<20); err != nil { + t.Fatal(err) + } + if err := l.WaitUpload(ctx, 16<<20); !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("a healthy lease's second 16 MiB upload was not paced beside a fallback lease: %v", err) + } +} diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index d2da1bc5ef..441f35e996 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -839,7 +839,7 @@ Notes: | `INV-HOST-CUT-BUDGET-RESERVE-001` | Each admitted slot, urgent included, carries at most PayloadBytes of cut payload and at most COWBytes of COW side bytes; a reservation beyond COWBytes revokes the slot and aborts its cut. | M7 admitted | `core/hostlimit/limiter.go` `Admit`, `ReserveCOW`; `core/storage/chunk_cut_budget.go` | `TestHostCOWCapAbortsCutAndRetainsPermitUntilJoin` (`core/storage/chunk_host_budget_test.go`); `TestSlotsUrgentAndJoinedRelease` (`core/hostlimit/limiter_test.go`) | `HostBudget` PayloadBound/SpillBound | ACTIVE | | `INV-HOST-CUT-BUDGET-SETTLE-001` | A slot's OS lock is released only after its cut's workers settle and Release is called; cancellation, hold expiry and limiter Close abort the cut but keep the slot until then. | M7 draining → joined → released | `core/hostlimit/limiter.go` `Release`, `releaseLoop`, `revoke`, `Close`; `core/storage/chunk_layer.go` `captureLocked` | `TestCOWAbortHoldsSlotUntilWorkerSettlement`, `TestHoldExpiryAndCloseDoNotSteal`, `TestKilledProcessReleasesButLiveHolderDoesNot` (`core/hostlimit/limiter_test.go`); `TestHostCOWCapAbortsCutAndRetainsPermitUntilJoin` (`core/storage/chunk_host_budget_test.go`) | `HostBudget` HeldUntilJoined | ACTIVE | | `INV-HOST-CUT-BUDGET-CONFIG-001` | A busy configuration lock or a mismatched pool configuration refuses every class, urgent included, with no fallback. | M7 free → admitted (refused) | `core/hostlimit/limiter.go` `pool`, `Admit` | `TestConfigurationLockContentionRefusesAllClasses` (`core/hostlimit/limiter_test.go`) | — | ACTIVE | -| `INV-HOST-CUT-BUDGET-FALLBACK-001` | Only urgent admissions (urgent move-final cuts and named seals) proceed while the pool is unavailable, on a per-process fallback that claims no host-wide bound, does not pace their uploads (there is no shared cap to protect), and is reported as fallback while any such lease lives. | M7 free → admitted (pool unavailable) | `core/hostlimit/limiter.go` `Admit`, `releaseLoop`, `WaitUpload` | `TestUnusablePoolClassPolicy`, `TestLastFallbackReleaseClearsActiveStatus`, `TestUrgentFallbackUploadIsNotPaced`, `TestConfigurationLockFailurePreservesPoolPolicy` (`core/hostlimit/limiter_test.go`); `TestChunkControl_NamedSnapshotUsesUrgentFallback` (`core/host/volume/chunk_control_adapter_test.go`) | `HostBudget` (fallback excluded) | ACTIVE | +| `INV-HOST-CUT-BUDGET-FALLBACK-001` | Only urgent admissions (urgent move-final cuts and named seals) proceed while the pool is unavailable, on a per-process fallback that claims no host-wide bound, does not pace their uploads while only fallback leases live (there is no shared cap to protect; a healthy lease beside one stays paced), and is reported as fallback while any such lease lives. | M7 free → admitted (pool unavailable) | `core/hostlimit/limiter.go` `Admit`, `releaseLoop`, `WaitUpload` | `TestUnusablePoolClassPolicy`, `TestLastFallbackReleaseClearsActiveStatus`, `TestUrgentFallbackUploadIsNotPaced`, `TestFallbackDoesNotUnpaceAHealthyLease`, `TestConfigurationLockFailurePreservesPoolPolicy` (`core/hostlimit/limiter_test.go`); `TestChunkControl_NamedSnapshotUsesUrgentFallback` (`core/host/volume/chunk_control_adapter_test.go`) | `HostBudget` (fallback excluded) | ACTIVE | | `INV-HOST-CUT-BUDGET-PACING-001` | A process's upload pacing debt carries across its own leases, and a new process starts with no debt. | M7 admitted (upload pacing) | `core/hostlimit/limiter.go` `WaitUpload` | `TestPacingDebtSurvivesOwnLeaseTurnover`, `TestPacingFreshProcessesDoNotShareDebt` (`core/hostlimit/limiter_test.go`) | — | ACTIVE | | `INV-HOST-POOL-CONFIG-001` | A pool configuration is invisible until its complete synced temporary file is atomically renamed under the OS configuration lock. A killed initializer cannot leave a partial visible configuration that strands successors. | M7 configuration preparation/publication | `core/hostlimit` `publishConfig` | `TestKilledInitializerDoesNotPublishPartialConfiguration` | `HostBudget` NoPartialConfig/ConfigBeforeAdmission (filesystem atomicity checked natively) | ACTIVE | | `INV-HOST-URGENT-PENDING-001` | While an urgent cut holds its slot, or a caller holds HoldUrgentPending, every Normal admission on the host is refused with ErrUrgentPending (a held-slot wait); urgent admissions still run. | M7 host cut admission (Normal before a slot) | `core/hostlimit/limiter.go` `pool`, `Admit`; `core/hostlimit/pending.go` `HoldUrgentPending` | `TestNormalWaitsWhileUrgentPendingIsHeld`, `TestUrgentLeaseHoldsThePendingSignal` (`core/hostlimit/pending_test.go`); `TestSlotsUrgentAndJoinedRelease` (`core/hostlimit/limiter_test.go`) | — | ACTIVE |