Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makara doesn't switch to slave when using AR.connection directly #148

Open
bogdan opened this issue Feb 17, 2017 · 2 comments
Open

Makara doesn't switch to slave when using AR.connection directly #148

bogdan opened this issue Feb 17, 2017 · 2 comments

Comments

@bogdan
Copy link

bogdan commented Feb 17, 2017

Here you can see that reads are from slave when using model methods... but not connection itself:

production >> SlaveUtils.read_from_slave { User.first }
  [slave/1] User Load (0.6ms)  SELECT  `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
production >> SlaveUtils.read_from_slave { User.connection.select_all('show processlist') }
  [master/1] (1.4ms)  show processlist
production >> 
``
@jeremy
Copy link
Contributor

jeremy commented Mar 2, 2017

This is unrelated to using model methods vs connection methods. You're comparing two different SQL statements. Makara decides which queries to send where based on whether they need the primary db or can use a replica. The SHOW query falls back to the primary connection since it doesn't match any of the regexps.

To verify: If you do a SELECT on the connection first, it'll pick a replica connection and a subsequent SHOW PROCESSLIST should stick to the same connection.

@bogdan
Copy link
Author

bogdan commented Mar 3, 2017

Thanks for clarification. I am not sure I got it at 100%.

Should I do it like this:

production >> SlaveUtils.read_from_slave { c = User.connection; c.select_all('SELECT  `users`.* FROM `users` LIMIT 1'); c.select_all('show processlist'); nil }
  [slave/1] (0.7ms)  SELECT  `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
  [master/1] (1.3ms)  show processlist
=> nil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants