feat(pubsub): add publish telemetry headers for publish attempt observability - #14338
tonyyyycui wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces publish hedging for the Google Cloud Pub/Sub Java client, adding configuration settings (HedgingSettings), a coordinator (CancellationSharer), and queue management (HedgedRequest) to manage and rate-limit hedged attempts. Feedback on these changes highlights several critical issues, including a race condition in scheduleQueueProcessing that could leave tasks unscheduled, a logic error in CancellationSharer where a failure of the original attempt prematurely cancels active hedged attempts, a thread-safety issue with queueProcessingFuture lacking volatile visibility, and a hardcoded 10-second timeout cap that ignores the user's configured maximum RPC timeout.
| private void scheduleQueueProcessing() { | ||
| if (isQueueProcessingScheduled.compareAndSet(false, true)) { | ||
| HedgedRequest nextItem = hedgingQueue.peek(); | ||
| if (nextItem == null) { | ||
| isQueueProcessingScheduled.set(false); | ||
| return; | ||
| } | ||
|
|
||
| long delay = Math.max(0, nextItem.getSendAfterMs() - clock.millisTime()); | ||
|
|
||
| queueProcessingFuture = | ||
| executor.schedule( | ||
| new Runnable() { | ||
| @Override | ||
| public void run() { | ||
| processQueue(); | ||
| } | ||
| }, | ||
| delay, | ||
| TimeUnit.MILLISECONDS); | ||
| } | ||
| } |
There was a problem hiding this comment.
There is a race condition in scheduleQueueProcessing when scheduling a new hedged request. If hedgingQueue.peek() returns null (because the queue was empty at that moment), but another thread adds an item to the queue and calls scheduleQueueProcessing right before isQueueProcessingScheduled.set(false) is executed, the compareAndSet in the second thread will fail (since it is still true). The first thread will then set the flag to false and return. As a result, the newly added item will remain in the queue without any scheduled task to process it. Using a loop to re-check the queue after setting the flag to false resolves this issue.
private void scheduleQueueProcessing() {
while (true) {
if (!isQueueProcessingScheduled.compareAndSet(false, true)) {
return;
}
HedgedRequest nextItem = hedgingQueue.peek();
if (nextItem == null) {
isQueueProcessingScheduled.set(false);
if (!hedgingQueue.isEmpty()) {
continue;
}
return;
}
long delay = Math.max(0, nextItem.getSendAfterMs() - clock.millisTime());
queueProcessingFuture =
executor.schedule(
new Runnable() {
@Override
public void run() {
processQueue();
}
},
delay,
TimeUnit.MILLISECONDS);
return;
}
}|
|
||
| private final ConcurrentLinkedQueue<HedgedRequest> hedgingQueue; | ||
| private final AtomicBoolean isQueueProcessingScheduled; | ||
| private ScheduledFuture<?> queueProcessingFuture; |
There was a problem hiding this comment.
The queueProcessingFuture field is written to by both the publisher thread (via startHedgedCall) and the executor thread (via processQueue) without any synchronization or volatile visibility. This can lead to data races and visibility issues, particularly during shutdown where queueProcessingFuture is read and cancelled. Marking this field as volatile ensures proper thread visibility.
| private ScheduledFuture<?> queueProcessingFuture; | |
| private volatile ScheduledFuture<?> queueProcessingFuture; |
| .setPublishOperation( | ||
| PubsubClientTelemetry.PublishOperation.newBuilder() | ||
| .setHedgedAttemptCount(attemptNumber) | ||
| .setPublishStartTime(Timestamps.fromMillis(outstandingBatch.creationTime)) |
There was a problem hiding this comment.
I don't think this is the right publish time. We should use the publish time from the publish() call per the proto comment: "Time at which the publish() call was initiated in the client library". This will likely require a few more changes to plumb that value to here, so I'd be okay with just having the hedged attempt set for now.
There was a problem hiding this comment.
I've removed this from the changes.
8eb6694 to
510bf79
Compare
510bf79 to
a926bdb
Compare
Adds client-side publish telemetry headers to provide observability into publish latency and hedging attempts.
NOTE: Stacked on #13735. Review commit 885792e for the isolated diff.