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
- #hotcopy(options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Utilities
constructor
A new instance of Utilities.
- #kill_queries(options = {}) ⇒ Object
- #reload_my_cnf(options = {}) ⇒ Object
- #run(cmd) ⇒ Object
- #skip_replication_errors(options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Utilities
Returns a new instance of Utilities.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mysql_manager/utilities.rb', line 32 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.
30 31 32 |
# File 'lib/mysql_manager/utilities.rb', line 30 def dbh @dbh end |
#dry_run ⇒ Object
Returns the value of attribute dry_run.
30 31 32 |
# File 'lib/mysql_manager/utilities.rb', line 30 def dry_run @dry_run end |
#log ⇒ Object
Returns the value of attribute log.
30 31 32 |
# File 'lib/mysql_manager/utilities.rb', line 30 def log @log end |
Instance Method Details
#hotcopy(options = {}) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/mysql_manager/utilities.rb', line 229 def hotcopy( = {}) begin t_start = Time.now.to_f # ensure mysql directory exists unless File.directory?([:data_dir]) raise FileNotFoundException.new("Unable to open mysql data dir #{[:data_dir]}") end # test connection @log.info("Testing connection...") @dbh.do("SELECT 1") unless @dry_run rsync = [] rsync << [:rsync_bin] rsync << [:rsync_args] rsync << [:data_dir] + '/' rsync << [:backup_dir] + '/' # create target directory if it does not already exist (unless ofcourse we're syncing to a remote target denoted by rsync:// or an rsync module denoted by ::) if [:backup_dir] !~ /^rsync:\/\// && [:backup_dir] !~ /::/ unless File.directory?([:backup_dir]) @log.info("Creating #{[:backup_dir]}") run ['mkdir', '-p', [:backup_dir]] unless @dry_run end end rsync.flatten! t_rsync = Time.now.to_f t_rsync_elapsed = Time.now.to_f while t_rsync_elapsed > [:rsync_ttl] # first do a raw rsync without table locks @log.info("Performing first-pass rsync without table locks") run rsync unless @dry_run t_rsync_elapsed = Time.now.to_f - t_rsync @log.info("Rsync took #{t_rsync_elapsed} seconds") sleep 1 t_rsync = Time.now.to_f end # then flush tables with read lock @log.info("Locking tables") t_lock = Time.now.to_f @dbh.do("FLUSH TABLES WITH READ LOCK") unless @dry_run # re-sync the delats @log.info("Performing second-pass rsync with table locks") run rsync unless @dry_run # release locks @log.info("Unlocking tables") @dbh.do('UNLOCK TABLES') unless @dry_run @log.info("Total lock time #{Time.now.to_f - t_lock} seconds") @log.info("Total elapsed time #{Time.now.to_f - t_start} seconds") rescue DBI::DatabaseError => e @log.debug(@dbh.last_statement) @log.warn(e.) exit(1) end end |
#kill_queries(options = {}) ⇒ Object
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 99 100 |
# File 'lib/mysql_manager/utilities.rb', line 43 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
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 143 144 |
# File 'lib/mysql_manager/utilities.rb', line 102 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 |
#run(cmd) ⇒ Object
220 221 222 223 224 225 226 227 |
# File 'lib/mysql_manager/utilities.rb', line 220 def run(cmd) stdout, stderr, status = Open3.capture3(Shellwords.join(cmd)) if status != 0 puts stderr raise Exception.new("execution of command failed") end return status end |
#skip_replication_errors(options = {}) ⇒ Object
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 217 218 |
# File 'lib/mysql_manager/utilities.rb', line 146 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 |