Class: Reptile::DeltaMonitor
- Inherits:
-
Object
- Object
- Reptile::DeltaMonitor
- Defined in:
- lib/reptile/delta_monitor.rb
Overview
This monitor compares the row counts for each table for each master and slave.
Class Method Summary collapse
-
.connection ⇒ Object
Retrieve the active database connection.
-
.diff(db_name, master_configs, slave_configs) ⇒ Object
Compares the row counts for master tables and slave tables Returns a hash of TABLE_NAME => ROW COUNT DELTAs.
-
.get_table_counts ⇒ Object
Returns a hash of TABLE_NAME => # Rows for all tables in current db.
-
.get_tables ⇒ Object
Returns an array of strings containing the table names for the current database.
-
.print_deltas(db_name, deltas, configs) ⇒ Object
Prints stats about the differences in number of rows between the master and slave.
-
.user ⇒ Object
The user settings for a user that has global select privilidgess.
-
.user=(user_settings) ⇒ Object
Set the user settings for a user that has global SELECT privilidgess.
Class Method Details
.connection ⇒ Object
Retrieve the active database connection. Nil of none exists.
16 17 18 |
# File 'lib/reptile/delta_monitor.rb', line 16 def self.connection ActiveRecord::Base.connection end |
.diff(db_name, master_configs, slave_configs) ⇒ Object
Compares the row counts for master tables and slave tables Returns a hash of TABLE_NAME => ROW COUNT DELTAs
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/reptile/delta_monitor.rb', line 22 def self.diff(db_name, master_configs, slave_configs) ActiveRecord::Base.establish_connection(slave_configs.merge(user)) slave_counts = get_table_counts ActiveRecord::Base.establish_connection(master_configs.merge(user)) master_counts = get_table_counts deltas= {} master_counts.each do |table, master_count| if slave_counts[table].nil? Log.error "Table '#{table}' exists on master but not on slave." next end delta = master_count.first.to_i - slave_counts[table].first.to_i deltas[table] = delta end print_deltas(db_name, deltas, master_configs) deltas rescue Exception => e Log.error "Error: Caught #{e}" Log.error "DB Name: #{db_name}" Log.error "Master Configs: #{master_configs.inspect}" Log.error "Slave Configs: #{slave_configs.inspect}" raise e end |
.get_table_counts ⇒ Object
Returns a hash of TABLE_NAME => # Rows for all tables in current db
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/reptile/delta_monitor.rb', line 73 def self.get_table_counts tables = get_tables tables_w_count = {} tables.each do |table| connection.execute("SELECT COUNT(*) FROM #{table}").each do |table_count| tables_w_count["#{table}"] = table_count end end tables_w_count end |
.get_tables ⇒ Object
Returns an array of strings containing the table names for the current database.
66 67 68 69 70 |
# File 'lib/reptile/delta_monitor.rb', line 66 def self.get_tables tables = [] connection.execute('SHOW TABLES').each { |row| tables << row } tables end |
.print_deltas(db_name, deltas, configs) ⇒ Object
Prints stats about the differences in number of rows between the master and slave
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/reptile/delta_monitor.rb', line 51 def self.print_deltas(db_name, deltas, configs) non_zero_deltas = deltas.select{|table, delta| not delta.zero?} if non_zero_deltas.size.zero? Log.info "Replication counts A-OK for #{db_name} on #{configs['host']} @ #{Time.now}" else Log.info "Replication Row Count Deltas for #{db_name} on #{configs['host']} @ #{Time.now}" Log.info "There #{non_zero_deltas.size > 1 ? 'are' : 'is'} #{non_zero_deltas.size} #{non_zero_deltas.size > 1 ? 'deltas' : 'delta'}" non_zero_deltas.each do |table, delta| Log.info " #{table} table: #{delta}" unless delta.zero? end end end |
.user ⇒ Object
The user settings for a user that has global select privilidgess
10 11 12 13 |
# File 'lib/reptile/delta_monitor.rb', line 10 def self.user raise "You need to specify a user!" if @user.nil? @user end |
.user=(user_settings) ⇒ Object
Set the user settings for a user that has global SELECT privilidgess
5 6 7 |
# File 'lib/reptile/delta_monitor.rb', line 5 def self.user=(user_settings) @user = user_settings end |