Use MassInsertBuilder for PutMessageSecrets, PutManySessions, PutManyLIDMappings#1101
Open
AlejoGarat wants to merge 1 commit into
Open
Use MassInsertBuilder for PutMessageSecrets, PutManySessions, PutManyLIDMappings#1101AlejoGarat wants to merge 1 commit into
AlejoGarat wants to merge 1 commit into
Conversation
PutMessageSecrets, PutManySessions, and PutManyLIDMappings were doing one db.Exec per row inside a transaction, resulting in N sequential round-trips to the database. This becomes a bottleneck during pairing (initial key/session sync) and mass reconnects, causing the DoTxn slow-transaction monitor to fire warnings at 1s and 5s intervals. Replace the per-row loops with MassInsertBuilder (already used by PutAllContactNames and PutManyRedactedPhones), reducing N round-trips to a single bulk INSERT per chunk of 500 rows. - Add GetMassInsertValues() to MessageSecretInsert and addressSessionTuple - LIDMapping already had GetMassInsertValues(); add builder and refactor - PutManyLIDMappings keeps the per-row DELETE (stale PN cleanup) and batches only the INSERT, which is where the bottleneck lies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
PutMessageSecrets,PutManySessions, andPutManyLIDMappingsexecute onedb.Execper row inside aDoTxn, resulting in N sequential round-trips to the database per call.This is particularly noticeable during:
The
DoTxnslow-transaction monitor (ingo.mau.fi/util/dbutil) logs"Transaction still running"every 5s and"Transaction took long"on close for transactions exceeding 1s. These warnings appear frequently.Solution
Use
MassInsertBuilder(already available ingo.mau.fi/util/dbutiland already used byPutAllContactNamesandPutManyRedactedPhones) to reduce N round-trips to a single bulkINSERTper chunk of 500 rows.Changes:
GetMassInsertValues() [4]anytoMessageSecretInsert(instore/store.go)GetMassInsertValues() [2]anytoaddressSessionTuple(insqlstore/store.go)LIDMappingalready hadGetMassInsertValues(); add the builder and refactorPutManyLIDMappingsPutManyLIDMappingskeeps the per-rowDELETE(needed for stale PN→LID cleanup) and batches only theINSERTBefore / After
Before (
PutMessageSecretswith 200 inserts):db.Exec(putMsgSecret, ...)inside a single transactionAfter:
db.ExecwithINSERT INTO whatsmeow_message_secrets VALUES ($1,$2,...), ($1,$6,...), ...