Module: DistributeReads
- Defined in:
- lib/distribute_reads.rb,
lib/distribute_reads/version.rb,
lib/distribute_reads/cache_store.rb,
lib/distribute_reads/job_methods.rb,
lib/distribute_reads/global_methods.rb,
lib/distribute_reads/appropriate_pool.rb
Defined Under Namespace
Modules: AppropriatePool, GlobalMethods, JobMethods Classes: CacheStore, Error, NoReplicasAvailable, TooMuchLag
Constant Summary collapse
- VERSION =
"0.3.0"
Class Attribute Summary collapse
-
.by_default ⇒ Object
Returns the value of attribute by_default.
-
.default_options ⇒ Object
Returns the value of attribute default_options.
- .logger ⇒ Object
Class Method Summary collapse
-
.default_to_primary ⇒ Object
legacy.
-
.default_to_primary=(value) ⇒ Object
legacy.
- .log(message) ⇒ Object
-
.makara3? ⇒ Boolean
private.
- .replication_lag(connection: nil) ⇒ Object
-
.with_replica ⇒ Object
private.
Class Attribute Details
.by_default ⇒ Object
Returns the value of attribute by_default.
17 18 19 |
# File 'lib/distribute_reads.rb', line 17 def by_default @by_default end |
.default_options ⇒ Object
Returns the value of attribute default_options.
18 19 20 |
# File 'lib/distribute_reads.rb', line 18 def @default_options end |
.logger ⇒ Object
27 28 29 30 31 32 |
# File 'lib/distribute_reads.rb', line 27 def self.logger unless defined?(@logger) @logger = ActiveRecord::Base.logger end @logger end |
Class Method Details
.default_to_primary ⇒ Object
legacy
123 124 125 |
# File 'lib/distribute_reads.rb', line 123 def self.default_to_primary !by_default end |
.default_to_primary=(value) ⇒ Object
legacy
128 129 130 |
# File 'lib/distribute_reads.rb', line 128 def self.default_to_primary=(value) self.by_default = !value end |
.log(message) ⇒ Object
99 100 101 |
# File 'lib/distribute_reads.rb', line 99 def self.log() logger.info("[distribute_reads] #{}") if logger end |
.makara3? ⇒ Boolean
private
115 116 117 118 119 120 |
# File 'lib/distribute_reads.rb', line 115 def self.makara3? unless defined?(@makara3) @makara3 = Gem::Version.new(Makara::VERSION.to_s) < Gem::Version.new("0.4.0") end @makara3 end |
.replication_lag(connection: nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/distribute_reads.rb', line 34 def self.replication_lag(connection: nil) connection ||= ActiveRecord::Base.connection replica_pool = connection.instance_variable_get(:@slave_pool) if replica_pool && replica_pool.connections.size > 1 log "Multiple replicas available, lag only reported for one" end with_replica do case connection.adapter_name when "PostgreSQL", "PostGIS" # cache the version number @server_version_num ||= {} cache_key = connection.pool.object_id @server_version_num[cache_key] ||= connection.execute("SHOW server_version_num").first["server_version_num"].to_i lag_condition = if @server_version_num[cache_key] >= 100000 "pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn()" else "pg_last_xlog_receive_location() = pg_last_xlog_replay_location()" end connection.execute( "SELECT CASE WHEN NOT pg_is_in_recovery() OR #{lag_condition} THEN 0 ELSE EXTRACT (EPOCH FROM NOW() - pg_last_xact_replay_timestamp()) END AS lag".squish ).first["lag"].to_f when "MySQL", "Mysql2", "Mysql2Spatial", "Mysql2Rgeo" @aurora_mysql ||= {} cache_key = connection.pool.object_id unless @aurora_mysql.key?(cache_key) # makara doesn't send SHOW queries to replica by default @aurora_mysql[cache_key] = connection.exec_query("SHOW VARIABLES LIKE 'aurora_version'").to_hash.any? end if @aurora_mysql[cache_key] status = connection.exec_query("SELECT Replica_lag_in_msec FROM mysql.ro_replica_status WHERE Server_id = @@aurora_server_id").to_hash.first status ? status["Replica_lag_in_msec"].to_f / 1000.0 : 0.0 else status = connection.exec_query("SHOW SLAVE STATUS").to_hash.first if status if status["Seconds_Behind_Master"].nil? # replication stopped # https://dev.mysql.com/doc/refman/8.0/en/show-slave-status.html nil else status["Seconds_Behind_Master"].to_f end else # not a replica 0.0 end end when "SQLite" # never a replica 0.0 else raise DistributeReads::Error, "Option not supported with this adapter" end end end |
.with_replica ⇒ Object
private
104 105 106 107 108 109 110 111 112 |
# File 'lib/distribute_reads.rb', line 104 def self.with_replica previous_value = Thread.current[:distribute_reads] begin Thread.current[:distribute_reads] = {replica: true, failover: false} yield ensure Thread.current[:distribute_reads] = previous_value end end |