Repository navigation
fix(pubsub): respect batch limits when acks are added concurrently - #9771
abhiramshaji-cyber wants to merge 2 commits into
Conversation
MessageQueue.add() checked the batch count and byte limits, awaited flush(), and only then pushed the message. Every concurrent caller passed the check against the same batch, waited on its own flush, and then all of them appended to the fresh batch, so one ack or modAck RPC could carry more ack IDs than maxMessages, or more bytes than MAX_BATCH_BYTES. flush() already takes the current batch synchronously, so add() now starts the flush, pushes the message into the new batch in the same tick, and waits for both the flush and its own response afterwards. The check and the push can no longer be split by another caller, and the sequential path sends the same batches as before. Fixes googleapis#9303
There was a problem hiding this comment.
Code Review
This pull request resolves a concurrency issue in the MessageQueue class where concurrent add() calls could overfill a batch. By capturing the flush promise and awaiting it alongside the response promise at the end of the method, the queue avoids blocking during the addition phase. Unit tests have also been added to verify that batch sizes and byte limits are respected under concurrent load. No review comments were provided, and the implementation appears correct and complete.
|
Hi, hope you're doing well! I know reviews here take time with so many packages, so no rush. Whenever someone has a moment, could the pubsub system tests be started and the change get a look? It keeps each ack and modAck request within the batch limit when several are added at once. Happy to change anything you'd prefer. |
|
Hi @abhiramshaji-cyber, I'm glad my issue/gist helped, thank you for putting up this PR! |
|
Hey, hope you're all doing well! And thank you @vienneraphael, your issue and gist made this so much easier to track down. I know the review queue is long right now, so no pressure at all. If anything would make this easier to look at, like more tests, a smaller diff, or a different take on the flush ordering, I'm happy to change it. The pubsub system tests are still waiting on a maintainer to start them whenever someone has a spare minute. |
|
Hi everyone, hope you're doing well! Hey, it's me again from my note here on the 22nd. The earlier workflow runs had expired, so I merged the latest main into the branch and fresh runs are now waiting for approval. I know there's a lot on everyone's plate, so whenever a maintainer has a spare minute, could they approve them? And if anything else would help this along, I'm happy to do it. |
When a subscriber acks or modAcks several messages at the same moment, one request could carry more messages than the configured batch limit, and now each request stays within it.
What this code is for
MessageQueueinhandwritten/pubsub/src/message-queues.tscollects acks (AckQueue) and modAcks (ModAckQueue) and sends them to the server in batches. Every ack or modAck goes throughadd(), which is supposed to send the current batch first withflush()when adding one more message would reachmaxMessagesorMAX_BATCH_BYTES.flush()takesthis._requestsand replaces it with an empty array before its firstawait, then sends the batch it took.Before this PR
An app sets
maxMessages: 1and modAcks three messages at the same moment (the gist from @vienneraphael in #9303 does exactly this).add()seesthis._requests.length + 1 >= maxMessages, callsflush(), and stops atawait this.flush(reason).add()run while the first is waiting. They see the same emptythis._requests, pass the sameif, and also stop atawait this.flush(reason).this._requests.push(...). Nobody checks the limit again.this._requestsas one batch.modifyAckDeadlinerequest with 3 ack IDs whilemaxMessagesis 1. WithmaxMessages: 2and six messages the batches are[1, 1, 1, 3], and the same thing happens to the byte limit.After this PR
Same app, same three modAcks at the same moment.
add()sees the limit is reached and callsflush()without waiting for it.flush()has already taken the old batch by the time it returns its promise.add()pushes its message into the new emptythis._requestsstraight away, and only then waits for the flush and for its own response.add()now sees one message inthis._requests, so theifis true and it flushes that batch before pushing its own.add()does the same.add()takes the same path as before, so those batches do not change.The diagram
flowchart LR classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827 classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px subgraph BEFORE["BEFORE: 1 request with 3 ack IDs, maxMessages 1"] direction TB B1["3 modAcks at the same moment"] --> B2["add() checks this._requests"] B2 --> B3["limit reached, call flush()"] B3 --> B4{"await flush, then push"} B4 -->|"all 3 wake up and push"| B5["1 request with 3 ack IDs"] B4 -->|"one call at a time"| B6["1 ack ID per request"] end subgraph AFTER["AFTER: 3 requests with 1 ack ID each"] direction TB A1["3 modAcks at the same moment"] --> A2["add() checks this._requests"] A2 --> A3["limit reached, call flush()"] A3 --> A4{"THE CHANGE: push, then await flush"} A4 -->|"next add() sees 1 message"| A5["3 requests with 1 ack ID"] A4 -->|"one call at a time"| A6["1 ack ID per request, same as before"] end B1 ~~~ A1 class B1,B2,B3,B6 box class A1,A2,A3,A6 box class B4,B5 bad class A4,A5 good linkStyle 3 stroke:#cc0000,stroke-width:5px linkStyle 8 stroke:#008800,stroke-width:5px style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827 style AFTER fill:#f3fff3,stroke:#008800,color:#111827The change
In
MessageQueue.add(),await this.flush(reason)becomesflushed = this.flush(reason), and the method now ends withawait Promise.all([flushed, responsePromise.promise])instead ofreturn responsePromise.promise. The check, the start of the flush, and the push now happen together beforeadd()waits on anything, so no otheradd()can run between them. This coversmaxMessagesandMAX_BATCH_BYTES, because both are checked in that sameif.Promise.allattaches toresponsePromise.promisestraight away, so if the message's own batch fails while the earlier flush is still running, the rejection still has a handler and reaches the caller as before.Three tests in
test/message-queues.tscalladd()six times withPromise.alland check every sent batch:maxMessagesof 1 and 2, and the byte limit. Onmainthey fail with batch sizes0,0,0,0,0,0,6,1,1,1,3, and a batch of783360bytes. With the change, they pass, and so do the other 979 unit tests.What changes for a user
maxMessagesand the byte limit. The reproduction from pubsub: concurrent ACK/modACK queue additions exceed maxMessages in 6.0.1 #9303 now givesrespectsLimit: truefor all six rows.[0, 1, 0, 1, 0, 1]forAckQueueand[1, 1, 1]forModAckQueueatmaxMessages: 1).add()still resolves only after the message is sent, and still waits for the flush it started.AckQueuerequests that @vienneraphael pointed out. They come from the existingthis._requests.length + 1 >= maxMessagescheck, which flushes an empty batch whenmaxMessagesis 1, and existing tests expect that flush. I left it alone so this PR only fixes the limit.Thank you @vienneraphael for the very clear report and the gist. The numbered steps in the issue pointed straight at the
await, and the table made the fix easy to check. I would really value the maintainers' view on two things:awaitthe approach you prefer? The other option I looked at was checking the limit again in a loop after eachawait. With the current+ 1 >=check andmaxMessages: 1that loop never ends, so I went with this one, but I am happy to change it.AckQueuerequests handled here, or in a separate PR? That change would need two existing tests updated, so I wanted to ask first.Fixes #9303 🦕