Repository navigation
Bugfix: Route advisory-lock queries to the master - #185
Open
ErikBrendel wants to merge 1 commit into
Open
ErikBrendel wants to merge 1 commit into
ErikBrendel wants to merge 1 commit into
Conversation
MySQL's advisory lock functions (GET_LOCK, RELEASE_LOCK, RELEASE_ALL_LOCKS, IS_FREE_LOCK, IS_USED_LOCK) are issued as SELECTs, so is_write_query() treats them as reads and db_connect() routes them to a replica. Because these locks are connection scoped, this defeats their purpose: the lock is held on a different connection than the master where the guarded writes happen, and replica load balancing can place concurrent callers on different servers, so they never contend — the named lock provides no mutual exclusion at all. Add is_lock_query() and force such queries to the master. It is kept separate from is_write_query() on purpose: a lock alters no data, and the is_write_query path also sets srtm[$table]=true — for a tableless lock query $table is '', and srtm[''] would make the later stripos($query, '') match every subsequent read and wrongly pin them all to the master.
|
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.



MySQL's advisory lock functions (GET_LOCK, RELEASE_LOCK, RELEASE_ALL_LOCKS, IS_FREE_LOCK, IS_USED_LOCK) are currently issued as SELECTs, so is_write_query() treats them as reads and db_connect() routes them to a replica. Because these locks are connection scoped, this defeats their purpose: the lock is held on a different connection than the master where the guarded writes happen, and replica load balancing can place concurrent callers on different servers, so they never contend: the named lock provides no mutual exclusion at all.
Add is_lock_query() and force such queries to the master. It is kept separate from is_write_query() on purpose: a lock alters no data, and the is_write_query path also sets srtm[$table]=true. For a tableless lock query $table is '', and srtm[''] would make the later stripos($query, '') match every subsequent read and wrongly pin them all to the master.