Skip to content

fix: persist an honest resync terminal status about object counts - #138

Merged
Vonng merged 1 commit into
mainfrom
fix/issue-136-resync-counter-flush
Sep 6, 2026
Merged

Vonng merged 1 commit into
mainfrom
fix/issue-136-resync-counter-flush

Conversation

@Vonng

@Vonng Vonng commented Sep 6, 2026

Copy link
Copy Markdown
Member

Description

resyncBucket could persist a resync terminal status that misrepresents the work actually accounted for. Fixes #136 — scoped to counter integrity only by maintainer ruling; the inherited resync cancellation defects (dispatch deadlock, walker leak, one-token-vs-N-buckets routing, blocking-phase cancellation) are tracked separately in #137.

Two counter-integrity problems:

  1. The goroutine that folds each worker outcome into the status via incStats was not joined before the deferred markStatus persisted, so Completed could be persisted with an under-count (objects and failed objects) until the periodic ~1m flush; the status API reads from disk.
  2. A run could persist Completed even when work was dropped: the parent context cancelled mid-drain (workers return without sending their result), or a worker consumed the resync-cancel token and returned without submitting its computed result.

Fix (minimal; cmd/bucket-replication.go, +100/-18)

  • Drain before persist. A small resyncResults helper centralises finalization ordering: close worker inputs → join workers → close the result channel → join the consumer, then persist. Registered as defer results.finish(workers, &wg) after the markStatus finalizer, so LIFO runs the drain first. This also runs on early-return paths (no send-on-closed), replacing the trailing inline close+wait.
  • Honest terminal status. finalResyncStatus(status, ctxErr, workerAborted) downgrades a tentative Completed to Failed when the parent context was cancelled or a worker aborted; called in the finalizer (after the drain, before markStatus, which persists under its own background context). sendResyncResult records workerAborted when a worker drops its result on the resync-cancel token; a ctx.Done() return is covered by the parent-context check.

Deliberately unchanged (→ #137): Walk(ctx) inline, the default: blocking dispatch send, the worker resyncCancelCh branches, and cmd/site-replication.go. This change does not depend on those for its own correctness.

Adversarial review

Codex Astra 6 (Max) recommended exactly this minimal set for the narrowed scope and then reviewed the implementation: VERDICT PASS. It confirmed the ordering, the parent-context recheck, the worker-abort safeguard, behavior-preserving helper extractions, and that #137's mechanisms are byte-identical with no refactor residue.

How to test

GOWORK=off go test -tags kqueue,dev ./cmd -count=1 -run '^(TestResyncBucketFinalize|TestResyncFinishDrainsResults|TestResyncFinishWaitsForInflightWorker)$'

TestResyncBucketFinalize drives a real erasure ObjectLayer and asserts the on-disk terminal status: complete counts stay Completed (2 replicated + 1 failed); parent-cancel-during-drain → Failed; worker-abort → Failed. The finish-order tests use testing/synctest. Red-on-revert confirmed for each wait and each downgrade term. gofmt/go vet/go build clean; -race clean; full -run 'Resync' -count=1 passes.

Compatibility impact

No wire or API change. A resync that lost work now reports Failed instead of a misleading Completed, and the persisted counts are complete at the moment Completed is written. There is one intentional conservative downgrade: a fully-processed run whose parent is cancelled between finishing and the final check is recorded Failed.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

🤖 Generated with Claude Code

https://claude.ai/code/session_01L7qJqWwy8oFA6aCXWRzXQe

…unts

resyncBucket could publish and persist a Completed resync status that did
not actually cover every object, in two ways:

1. It joined only the producer workers before the deferred markStatus ran,
   not the goroutine that folds each worker result into the status, so a
   Completed status could omit the last object (or a failed object) until the
   periodic ~1m flush (issue #136). The same finalization also closed the
   result channel on early-return paths while workers were still in flight,
   risking a send-on-closed-channel panic and a lost result.
2. markStatus persists under its own background context, so if the parent
   context was cancelled during the drain - workers then return without
   sending their computed result - or a worker dropped a result on the
   resync-cancel signal, a bare Completed was still recorded with counts that
   no longer matched the objects seen.

Fixes (count integrity only; the inherited cancellation deadlock, walker leak,
and single-token routing are tracked as separate follow-ups):

- Centralize shutdown in a resyncResults helper whose finish() stops the
  workers (closes inputs, waits for them to exit) before closing the result
  channel and waiting for the consumer to drain, then lets the deferred
  markStatus persist the final counts. finish() now runs on every exit path.
- Record a dropped result via sendResyncResult (a worker consuming the
  resync-cancel token returns without sending), and in the finalizer downgrade
  a Completed status to Failed via finalResyncStatus when the parent context
  was cancelled or a worker aborted - so a persisted Completed never
  misrepresents an incomplete resync.

Deterministic tests: an on-disk round-trip of the terminal status (complete
counts stay Completed; parent-cancel-during-drain and worker-abort each
downgrade to Failed), and testing/synctest drain/worker-order assertions that
fail deterministically if a finish() wait is removed. The inherited
cancellation structure (inline Walk, the dispatch send, the worker cancel
branches) is left unchanged for the follow-ups.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01L7qJqWwy8oFA6aCXWRzXQe
Signed-off-by: Feng Ruohang <[email protected]>
@Vonng

Vonng commented Sep 6, 2026

Copy link
Copy Markdown
Member Author

Independent round-2 verification: the #136 drain-before-persist fix passes; complete resync outcome correctness still needs #139.

Tested 35d9f1824d3de0d368d7f157be4decf445ab645f in the combined candidate. The refreshed f8ca4a8656217165a78f3cea09a3c247d483e455 changes only comments/test-message spelling and fixes the initial misspell lint failure; product behavior is identical.

  • Two real healthy object versions are both present in the first persisted Completed response (~0.2 seconds), with 8,917,248 replicated ciphertext bytes.
  • A real missing-destination upload rejected by a 100 KiB quota reports 1 successful / 1 failed object immediately, and the exact status survives source restart.
  • The drain/producer-ordering and parent-cancel/worker-abort classification tests pass under -race.
  • The integrated cmd/internal suite passes 5,701 tests/subtests (166 skipped).

P2 found in the actual producer: when those versions already exist at the destination, the same quota-rejected updates yield Completed / 2 successes / 0 failures, while source status is FAILED and destination tags remain old. Restart preserves the false result. Clearing only the quota makes the same updates converge. Full reproduction, source path and bounded acceptance are in #139.

The current sink tests inject already-classified outcomes, so they cannot catch that defect. Keep the minimal drain fix; make the real worker use the outcome for the selected target ARN and retain an existing-version failure regression. There is no need to absorb #137's cancellation architecture or #133's multi-pool work into this fix. #136's original four acceptance checks are now marked verified, with #139 explicitly linked as the remaining release-accounting condition.

@Vonng
Vonng merged commit 8a1f594 into main Sep 6, 2026
8 checks passed
@Vonng
Vonng deleted the fix/issue-136-resync-counter-flush branch September 6, 2026 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[P2] Resync publishes Completed before the final object counters are persisted

1 participant