feat(pubsub): implement publish hedging to reduce tail latency - #13735
feat(pubsub): implement publish hedging to reduce tail latency#13735tonyyyycui wants to merge 35 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.
| final int batchSize = outstandingBatch.outstandingPublishes.size(); | ||
| for (final OutstandingPublish outstanding : outstandingBatch.outstandingPublishes) { | ||
| outstanding.publishResult.addListener( | ||
| new Runnable() { |
There was a problem hiding this comment.
I worried that this approach could have cause additional, unneeded overhead. The cancellation check be done in processQueue instead to avoid needing to create these Runnables, but then we run into an issue of the cancellation not propagating to in-flight hedged requests (i.e., publish at t=0, hedge at t=1000, cancellation at t=1050). This may be okay behavior given that cancellations are best effort. I'll raise this issue with the team.
…and maxtokens, updated tests.
…r for hedgeTokenBucket. Also added HEDGE_TOKEN_SCALE.
…CancellationSharer.java
| } | ||
| this.clock = builder.clock != null ? builder.clock : CurrentMillisClock.getDefaultClock(); | ||
| this.publishContext = GrpcCallContext.createDefault(); | ||
| this.hedgingMetadata = ImmutableMap.of("x-goog-pubsub-hedged", ImmutableList.of("true")); |
There was a problem hiding this comment.
I think we wanted this to be the attempt count, not just true/false. Is it simple to adjust this to something like x-google-pubsub-hedged-count?
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.
| shutdown = new AtomicBoolean(false); | ||
| messagesWaiter = new Waiter(); | ||
| this.hedgeSettings = builder.hedgeSettings; | ||
| if (this.hedgeSettings != null) { |
There was a problem hiding this comment.
Could this be checked in the Publisher.Builder.build() method instead?
| } | ||
|
|
||
| void removeFromHedgingQueue(CancellationSharer coordinator) { | ||
| queueLock.lock(); |
There was a problem hiding this comment.
Is a lock actually needed here (and in processQueue)? hedgingQueue is a concurrent data structure and there are other atomic variables used as well.
| return publishCall(outstandingBatch, 0); | ||
| } | ||
|
|
||
| private ApiFuture<PublishResponse> publishCall( |
There was a problem hiding this comment.
Something I missed from the design:
The timeout of the hedged RPCs should be “total deadline - now”, but capped at 10s overall which is the total time that the server allows for a publish attempt.
I think this will need to be enforced in this method by adding a timeout on the context object we are already modify. We'll need to propagate the absolute deadline here somehow.
|
|
||
| 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.
| * Creates a publish start event that is tied to the publish RPC span time, marking hedged | ||
| * attempts explicitly. | ||
| */ | ||
| void addPublishStartEvent(int attemptNumber) { |
There was a problem hiding this comment.
We should have a publish end (hedged) event as well.
| private void processQueue() { | ||
| queueLock.lock(); | ||
| try { | ||
| isQueueProcessingScheduled.set(false); |
There was a problem hiding this comment.
Going along with this comment, I think we will also need to move the isQueueProcessingScheduled.set(false) call to after the while loop (but before the scheduleQueueProcessing() call). This will prevent a scheduleQueueProcessing call from startHedgedCall from scheduling another processQueue() call without needing the lock in processQueue().
| * @param attemptNumber the 1-based index of the attempt (1 is original, 2+ are hedged) | ||
| * @param future the future representing the gRPC call for this attempt | ||
| */ | ||
| void addAttempt(final int attemptNumber, ApiFuture<PublishResponse> future) { |
There was a problem hiding this comment.
We may need a better synchronization method across done and runningAttempts here using a lock that controls both of them. For example, if checkCompletionOnQueueExit races with handleAttemptFailure, the following can happen:
handleAttemptFailure -> fails with PERMISSION_DENIED
Calls runningAttempts.remove(attemptNumber) // may become empty
// switch threads
In checkCompletionOnQueueExit, if (!done.get() && runningAttempts.isEmpty() && !isInQueue.get()) -> true because runningAttempts just became empty
setException(RuntimeException("Hedging failed with no active attempts"))
// switch threads
setException(PERMISSION_DENIED) // not honored because setException already called
We'd return the wrong error in this case. I believe there are similar issues across all of the methods here, so we should use a lock here to guard the done and runningAttempts members. They likely don't need to be concurrent/atomic data structures anymore.
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.