instacart / makara

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

If the query result is cached, it will always be marked as primary in the log. #307

Open fukata opened 3 years ago

fukata commented 3 years ago

Hello.

Thanks for the easy to use and awesome gem!

I'm currently verifying it for use in a production environment and found a phenomenon that I'm not sure if it's a bug or a specification, so I'd like to ask a question.

Versions

database.yml

development:
  adapter: mysql2_makara
  database: <%= ENV['DB_NAME'] %>
  username: root
  password: ''
  makara:
    connections:
      - role: master
        name: primary
        host: <%= ENV['DB_PRIMARY_HOST'] || 'db_primary' %>
      - role: slave
        name: replica
        host: <%= ENV['DB_REPLICA_HOST'] || 'db_read' %

Log

log/development.log

[replica] User Load (0.5ms) SELECT users.* FROM users LIMIT 1;
CACHE [primary] User Load (0.0ms) SELECT users.* FROM users LIMIT 1;

Question

The cached results are always output as CACHE [primary] in the log.

I've been monitoring both primary and replica mysql with general-log output, but there is no log output on the primary, only on the replica.

I thought it would be better to show CACHE [replica] since it is a cache of the query results to replica.

nguyenngoc2505 commented 3 years ago

I've a same question.

mintuhouse commented 3 years ago

Add below to your initializer to skip printing primary/replica for queries returned from cache

module MakaraLoggingSubscriber
  # See https://github.com/instacart/makara/blob/e45ba090fce998dad9e9a2759426f4695009cfae/lib/makara/logging/subscriber.rb#L23
  def current_wrapper_name(event)
    return nil if event.payload[:cached]

    super(event)
  end
end

ActiveRecord::LogSubscriber.log_subscribers.each do |subscriber|
  subscriber.extend ::MakaraLoggingSubscriber
end
BambangSinaga commented 2 years ago

Hello,

I have problem related to query log. They're not showing value from adapter._makara_name. versions:

database.yml

default: &default
  adapter: postgresql_makara
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: super_awesome_api_development
  makara:
      id: postgresql
      # list your connections with the override values (they're merged into the top-level config)
      # be sure to provide the role if primary, role is assumed to be a replica if not provided
      connections:
        - role: master
          name: primary
          blacklist_duration: 0
          host: localhost
        - role: replica
          name: replica
          host: localhost

development.log

Started GET "/attendance_office_hours" for ::1 at 2022-05-20 14:02:37 +0700
Processing by AttendanceOfficeHoursController#index as */*
  AttendanceOfficeHour Load (0.8ms)  SELECT "attendance_office_hours".* FROM "attendance_office_hours" WHERE "attendance_office_hours"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/attendance_office_hours_controller.rb:5:in `index'
Completed 200 OK in 30ms (Views: 0.8ms | ActiveRecord: 11.4ms | Allocations: 7800)

I don't know if this query hit primary or replica. Is there any config I should add?