instacart / makara

A Read-Write Proxy for Connections; Also provides an ActiveRecord adapter.
http://tech.taskrabbit.com/
MIT License
929 stars 170 forks source link

It doesn't send a query to master. Why so? 🤔 #205

Open artkirienko opened 6 years ago

artkirienko commented 6 years ago

https://github.com/taskrabbit/makara#common-problems--solutions

database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 10
  host: postgres
  username: postgres

development:
  adapter: 'postgresql_makara'
  encoding: unicode
  pool: 10
  database: jiffyshirts_development
  makara:
    id: postgresql
    blacklist_duration: 5
    master_ttl: 5
    master_strategy: round_robin
    slave_strategy: round_robin
    sticky: false
    connections:
      - role: master
        blacklist_duration: 0
        url: 'postgres://postgres@postgres:5432/jiffyshirts_development'
      - role: slave
        url: 'postgres://postgres@postgres:5432/jiffyshirts_replica'

test:
  database: jiffyshirts_test
  <<: *default

deploy:
  database: jiffyshirts_development
  <<: *default
  1. It uses slave database. It's okay.

    > Spree::User.last
    [slave/1] Spree::User Load (1.5ms)  SELECT  "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL  ORDER BY "spree_users"."id" DESC LIMIT 1
  2. It is still using slave database. Why so? I've even tried to set master_ttl to 600. Same problem.

    > Spree::User.connection.stick_to_master!(true); Spree::User.last
    [slave/1] Spree::User Load (1.2ms)  SELECT  "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL  ORDER BY "spree_users"."id" DESC LIMIT 1
rosa commented 6 years ago

@artkirienko this is working fine for me. For example:

>> User.last # Uses replica
  [replica] User Load (5.5ms)  SELECT  `users`.* FROM `users` ORDER BY `users`.`id` DESC LIMIT 1
>> User.connection.stick_to_master!
=> 10.0
>> User.last # Uses primary
  [primary] User Load (2.9ms)  SELECT  `users`.* FROM `users` ORDER BY `users`.`id` DESC LIMIT 1

Have you tried setting sticky: true in your configuration? I think that's the reason stickiness is being skipped for you after calling stick_to_master!.