Skip to content

feat(pubsub): add CancellationSharer and HedgingScheduler - #9464

Open
feywind wants to merge 1 commit into
pubsub/hedging-options-and-token-bucketfrom
pubsub/hedging-coordinator-and-scheduler
Open

feywind wants to merge 1 commit into
pubsub/hedging-options-and-token-bucketfrom
pubsub/hedging-coordinator-and-scheduler

Conversation

@feywind

@feywind feywind commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Overview

This PR is Layer 3 of 7 in Stack #9467, which implements Publish Hedging for @google-cloud/pubsub to mitigate high-percentile (p99/p99.9) publish tail latencies by sending backup (hedged) publish RPCs when an in-flight batch takes longer than a configurable hedgeDelay threshold.

Stack Structure

  1. feat(gax): support cancelling API calls via CallOptions.signal #9462 - feat(gax): support AbortSignal cancellation in CallOptions and createApiCall
  2. feat(pubsub): add HedgingOptions validation and token bucket #9463 - feat(pubsub): add HedgingOptions validation and HedgingTokenBucket
  3. feat(pubsub): add CancellationSharer and HedgingScheduler #9464 - feat(pubsub): add CancellationSharer and HedgingScheduler (this PR)
  4. feat(pubsub): add OpenTelemetry events and logging for hedged publish #9465 - feat(pubsub): add OpenTelemetry events and logging for hedged publish
  5. feat(pubsub): integrate publish hedging into MessageQueue #9466 - feat(pubsub): integrate publish hedging into MessageQueue
  6. test(pubsub): add unit tests for publish hedging queue integration #9635 - test(pubsub): add unit tests for publish hedging queue integration
  7. test(pubsub): add publish hedging system tests #9636 - test(pubsub): add publish hedging system tests

Summary of Changes in This PR

This layer implements the core hedging state machine (CancellationSharer) and the event-driven single-timer FIFO hedging queue (HedgingScheduler) using Duration arithmetic:

  • CancellationSharer (handwritten/pubsub/src/publisher/hedging.ts):
    • Coordinates the original attempt (attempt 0) and any hedged attempts (attempt 1..N) for a single publish batch.
    • First Success Wins: Whichever attempt resolves first marks the batch as done, refills the token bucket once (onSuccess()), aborts all other in-flight attempts via their AbortControllers, and resolves coordinator.promise with {response, wasHedged, successfulAttempt}.
    • Failure Handling:
      • If a hedged attempt (attempt > 0) fails while the original attempt (attempt 0 with GAX retries) is still running, the error is discarded so the batch can still succeed.
      • If the original attempt (attempt 0) fails terminally (permanent error or after exhausting GAX retries), all remaining attempts are aborted and coordinator.promise rejects immediately.
  • HedgedRequest & HedgingScheduler (handwritten/pubsub/src/publisher/hedging.ts):
    • Maintains a chronological FIFO queue of HedgedRequest items and a single setTimeout handle (queueProcessingTimer) scheduled for the head item's sendAfter Duration.
    • When an item becomes due and the batch is still in flight with positive remainingTimeout:
      • Computes attemptTimeout = atMost(remainingTimeout, MAX_HEDGE_ATTEMPT_TIMEOUT) (capped at 10s).
      • Attempts tokenBucket.tryAcquireHedgeToken(). If acquired, enqueues the next hedge check (attemptNumber + 1 at now.add(hedgeDelay)) for multiple hedging and invokes startHedgedAttempt(attemptNumber, attemptTimeout).
      • If no token is available, invokes onRateLimited and does not re-enqueue the batch.
  • Unit Tests (handwritten/pubsub/test/publisher/hedging.ts):
    • Tests for CancellationSharer (original wins, hedged wins, hedged failure ignored while original runs, original failure aborts hedged attempts) and HedgingScheduler (multiple hedging at hedgeDelay intervals, 10s/remainingTimeout capping, early completion skipping).

@product-auto-label product-auto-label Bot added the api: pubsub Issues related to the Pub/Sub API. label Sep 29, 2026
@feywind
feywind added this pull request to stack #9467 September 29, 2026 02:09

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces publish hedging orchestration and scheduling for the Pub/Sub publisher, adding the CancellationSharer to coordinate attempts and the HedgingScheduler to manage the timing of hedged requests. The review feedback highlights a critical issue where the coordinator prematurely aborts hedged attempts and rejects the promise if the original attempt fails, rather than waiting for all attempts to fail. Additionally, the reviewer recommends rejecting the promise with a cancellation error when cancel() is called to prevent hangs, adding an early return in scheduleFirstHedge if the coordinator is already done, optimizing processQueue by using the pre-calculated now duration, and updating the unit tests to reflect the corrected failure behavior.

Comment thread handwritten/pubsub/src/publisher/hedging.ts
Comment thread handwritten/pubsub/src/publisher/hedging.ts
Comment thread handwritten/pubsub/src/publisher/hedging.ts
Comment thread handwritten/pubsub/src/publisher/hedging.ts Outdated
Comment thread handwritten/pubsub/test/publisher/hedging.ts
Comment thread handwritten/pubsub/src/publisher/hedging.ts Outdated
Comment thread handwritten/pubsub/src/publisher/hedging.ts Outdated
Comment thread handwritten/pubsub/src/publisher/hedging.ts Outdated
Comment thread handwritten/pubsub/src/publisher/hedging.ts Outdated
@feywind
feywind force-pushed the pubsub/hedging-coordinator-and-scheduler branch 2 times, most recently from 96e021d to 3ed8373 Compare October 13, 2026 22:09
@feywind
feywind marked this pull request as ready for review October 15, 2026 21:33
@feywind
feywind requested a review from a team as a code owner October 15, 2026 21:33
@github-actions
github-actions Bot requested a review from shivanee-p October 15, 2026 21:35
@feywind
feywind force-pushed the pubsub/hedging-coordinator-and-scheduler branch from 3ed8373 to 9e130b3 Compare October 19, 2026 21:57

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: pubsub Issues related to the Pub/Sub API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant