Class: MysqlManager::Utilities
- Inherits:
-
Object
- Object
- MysqlManager::Utilities
- Defined in:
- lib/mysql_manager/utilities.rb
Instance Attribute Summary collapse
-
#dbh ⇒ Object
Returns the value of attribute dbh.
-
#dry_run ⇒ Object
Returns the value of attribute dry_run.
-
#log ⇒ Object
Returns the value of attribute log.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Utilities
constructor
A new instance of Utilities.
- #kill_queries(options = {}) ⇒ Object
- #reload_my_cnf(options = {}) ⇒ Object
- #skip_replication_errors(options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Utilities
Returns a new instance of Utilities.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mysql_manager/utilities.rb', line 30 def initialize( = {}) [:dsn] ||= "DBI:Mysql:mysql:localhost" [:username] ||= "root" [:password] ||= "" @log = Logger.new(STDERR) # connect to the MySQL server @dbh = DBI.connect([:dsn], [:username], [:password]) end |
Instance Attribute Details
#dbh ⇒ Object
Returns the value of attribute dbh.
28 29 30 |
# File 'lib/mysql_manager/utilities.rb', line 28 def dbh @dbh end |
#dry_run ⇒ Object
Returns the value of attribute dry_run.
28 29 30 |
# File 'lib/mysql_manager/utilities.rb', line 28 def dry_run @dry_run end |
#log ⇒ Object
Returns the value of attribute log.
28 29 30 |
# File 'lib/mysql_manager/utilities.rb', line 28 def log @log end |
Instance Method Details
#kill_queries(options = {}) ⇒ Object
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 98 |
# File 'lib/mysql_manager/utilities.rb', line 41 def kill_queries( = {}) [:max_query_time] ||= -1 [:user] ||= [] [:host] ||= [] [:query] ||= [] [:command] ||= [] [:state] ||= [] [:db] ||= [] @dbh.execute("SHOW FULL PROCESSLIST") do |sth| sth.fetch_hash() do |row| next if row['Command'] == 'Binlog Dump' next if row['User'] == 'system user' results = [] .each_pair do |field,criteria| case field when :max_query_time if criteria >= 0 if row['Time'].to_i > criteria results << true else results << false end end when :user, :host, :query, :command, :state, :db col = field == :query ? 'Info' : field.to_s.capitalize if criteria.length > 0 matched = false criteria.each do |pattern| if pattern.match(row[col]) matched = true break end end #puts "#{row[col]} #{criteria.inspect} == #{matched}" results << matched end end end # Some conditions need to apply #puts results.inspect if results.length > 0 # None of them may be false unless results.include?(false) begin @log.info("Killing id:#{row['Id']} db:#{row['db']} user:#{row['User']} command:#{row['Command']} state:#{row['State']} time:#{row['Time']} host:#{row['Host']} query:#{row['Info']} rows_sent:#{row['Rows_sent']} rows_examined:#{row['Rows_examined']} rows_read:#{row['Rows_read']}") @dbh.do("KILL #{row['Id']}") unless @dry_run rescue DBI::DatabaseError => e @log.warn(e.) end end end end end end |
#reload_my_cnf(options = {}) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/mysql_manager/utilities.rb', line 100 def reload_my_cnf( = {}) [:config] ||= '/etc/my.cnf' [:groups] ||= ['mysqld', 'mysqld_safe', 'mysql.server', 'mysql_server', 'server', 'mysql'] variables = {} @dbh.execute("SHOW VARIABLES") do |sth| sth.fetch_hash() do |row| variables[row['Variable_name']] = row['Value'] end end unless File.exists?([:config]) raise FileNotFoundException.new("Unable to open file #{[:config]}") end my_cnf = ParseConfig.new([:config]) my_cnf.groups.each do |group| if [:groups].include?(group) @log.debug("loading values from [#{group}]") my_cnf[group].each_pair do |k,v| next if v.nil? || v.empty? if variables.has_key?(k) begin v = v.to_s v = v.to_i if v =~ /^(\d+)$/ v = $1.to_i * (1024) if v =~ /^(\d+)K$/ v = $1.to_i * (1024*1024) if v =~ /^(\d+)M$/ v = $1.to_i * (1024*1024*1024) if v =~ /^(\d+)G$/ if v.instance_of?(Integer) || v.instance_of?(Fixnum) sql = "SET GLOBAL #{k} = #{v}" else sql = "SET GLOBAL #{k} = '#{v}'" end @dbh.do(sql) unless @dry_run @log.info("set #{k}=#{v}") rescue DBI::DatabaseError => e @log.debug(@dbh.last_statement) @log.warn(e.) end end end end end end |
#skip_replication_errors(options = {}) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/mysql_manager/utilities.rb', line 144 def skip_replication_errors( = {}) [:max_errors] ||= -1 [:max_error_duration] ||= -1 [:min_healthy_duration] ||= -1 [:log_frequency] ||= 10 begin # get server version string and display it max_seconds_behind = 0 t_start = Time.now.to_f t_last_error = nil t_last_error_elapsed = 0 t_last_log = 0 t_elapsed = 0 t_recovered = 0 errors = 0 while ([:max_errors] < 0 || errors < [:max_errors]) && ([:max_error_duration] < 0 || t_last_error_elapsed < [:max_error_duration]) @dbh.execute("SHOW SLAVE STATUS") do |sth| t_now = Time.now.to_f sth.fetch_hash() do |row| seconds_behind = row['Seconds_Behind_Master'] if seconds_behind.nil? @log.info("replication broken") @log.info("last error: #{row['Last_Error'].gsub(/\r?\n/, '')}") @dbh.do("SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1") unless @dry_run @dbh.do("START SLAVE") errors += 1 max_seconds_behind = 0 t_last_error = t_now elsif seconds_behind == 0 t_recovered = t_now if t_recovered == 0 if t_last_log == 0 || t_now - t_last_log > [:log_frequency] @log.info("fully caught up with master") t_last_log = t_now end if (t_min_healthy_duration >=0) && (t_now - t_recovered > t_min_healthy_duration) @log.info("satisfied health duration window") break end else seconds_behind = seconds_behind.to_f t_recovered = 0 t_last_error_elapsed = t_last_error.nil? ? 0 : t_now - t_last_error t_elapsed = t_now - t_start max_seconds_behind = [max_seconds_behind, seconds_behind].max t_catchup = max_seconds_behind - seconds_behind t_rate = t_elapsed == 0 ? 1 : t_catchup/t_elapsed t_left = t_rate > 0 ? seconds_behind/t_rate : 0 if t_left > 60*60 t_left_str = sprintf('%.2f hours', t_left/(60*60)) elsif t_left > 60 t_left_str = sprintf('%.2f mins', t_left/60) else t_left_str = sprintf('%.2f secs', t_left) end if t_last_log == 0 || t_now.to_f - t_last_log.to_f > [:log_frequency] @log.info("#{seconds_behind} seconds behind master; #{t_catchup} seconds caught up; #{sprintf('%.2f', t_rate)} seconds/second; #{t_left_str} left; #{errors} errors; last error #{sprintf('%.2f', t_last_error_elapsed)} seconds ago") t_last_log = t_now end sleep 0.100 end end end end rescue Interrupt @log.info("Aborted by user") rescue DBI::DatabaseError => e @log.error("Error code: #{e.err}: #{e.errstr}") end end |