feat(pubsub): implement publish hedging to reduce tail latency - #13735
feat(pubsub): implement publish hedging to reduce tail latency#13735tonyyyycui wants to merge 45 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a hedging mechanism for the Pub/Sub Publisher, adding HedgeSettings for configuration and a thread-safe HedgeTokenBucket to limit hedged requests. The feedback suggests replacing the synchronized methods in HedgeTokenBucket with explicit ReentrantLock to reduce lock contention and improve performance on the critical path.
michaelpri10
left a comment
There was a problem hiding this comment.
At a higher level, I think the full implementation should be one PR (instead of the , given any submitted changes become public. It would be unexpected for customers to be able to set HedgingSettings without anything actually happening, so the final PR should have everything needed for the implementation included.
| * | ||
| * @return the hedging delay. | ||
| */ | ||
| public Duration getHedgeDelay() { |
There was a problem hiding this comment.
nit: Does this method need to be public? I think leaving it without an access modifier (i.e., making it package-private) should be sufficient.
There was a problem hiding this comment.
I've removed it for now, but I think it would also make sense if the user could see what they see the hedge delay is set to since it's a configurable field. Leaving this conversation unresolved.
There was a problem hiding this comment.
Thought more on this and I actually think having this public makes sense and matches what we do for SubscriberShutdownSettings. All of the getters for HedgingSettings can be public.
…and maxtokens, updated tests.
…r for hedgeTokenBucket. Also added HEDGE_TOKEN_SCALE.
…CancellationSharer.java
d9a8655 to
0b88e33
Compare
…le-cloud-java into publish-hedging-settings # Please enter a commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit.
|
|
||
| private final HedgeSettings hedgeSettings; | ||
| private final HedgeTokenBucket hedgeTokenBucket; | ||
| private final ApiClock clock; |
There was a problem hiding this comment.
Sounds good, I think it is fine to use this then.
| * | ||
| * @return the hedging delay. | ||
| */ | ||
| public Duration getHedgeDelay() { |
There was a problem hiding this comment.
Thought more on this and I actually think having this public makes sense and matches what we do for SubscriberShutdownSettings. All of the getters for HedgingSettings can be public.
| } | ||
|
|
||
| if (runningAttempts.isEmpty() || !isRetryable) { | ||
| if (done.compareAndSet(false, true)) { |
There was a problem hiding this comment.
I believe the lock added in CancellationSharer prevents this race condition.
This PR implements publish hedging in the Java Cloud Pub/Sub Publisher. Specifically, it adds support for scheduling "hedged" publish attempts when a publish call is slow to respond. A token-based method is utilized to rate-limit hedged requests and a coordinator is used to manage/cancel concurrent requests to prevent duplicate publishes.