Class: Maatkit::DeadlockLogger
- Inherits:
-
Object
- Object
- Maatkit::DeadlockLogger
- Defined in:
- lib/maatkit-ruby/mk-deadlock-logger.rb
Overview
Extract and log MySQL deadlock information.
Maatkit::DeadlockLogger.new( array, str, array)
Instance Attribute Summary collapse
-
#ask_pass ⇒ Object
Prompt for a password when connecting to MySQL.
-
#charset ⇒ Object
short form: -A; type: string Default character set.
-
#clear_deadlocks ⇒ Object
type: string Use this table to create a small deadlock.
-
#collapse ⇒ Object
Collapse whitespace in queries to a single space.
-
#columns ⇒ Object
type: hash Output only this comma-separated list of columns.
-
#config ⇒ Object
type: Array Read this comma-separated list of config files; if specified, this must be the first option on the command line.
-
#create_dest_table ⇒ Object
Create the table specified by –dest.
-
#daemonize ⇒ Object
Fork to the background and detach from the shell.
-
#defaults_file ⇒ Object
short form: -F; type: string Only read mysql options from the given file.
-
#dest ⇒ Object
type: DSN DSN for where to store deadlocks; specify at least a database (D) and table (t).
-
#help ⇒ Object
Show help and exit.
-
#host ⇒ Object
short form: -h; type: string Connect to host.
-
#interval ⇒ Object
type: time How often to check for deadlocks.
-
#log ⇒ Object
type: string Print all output to this file when daemonized.
-
#numeric_ip ⇒ Object
Express IP addresses as integers.
-
#password ⇒ Object
short form: -p; type: string Password to use when connecting.
-
#path_to_mk_deadlock_logger ⇒ Object
Sets the executable path, otherwise the environment path will be used.
-
#pid ⇒ Object
type: string Create the given PID file when daemonized.
-
#port ⇒ Object
short form: -P; type: int Port number to use for connection.
-
#print ⇒ Object
Print results on standard output.
-
#run_time ⇒ Object
type: time How long to run before exiting.
-
#set_vars ⇒ Object
type: string; default: wait_timeout=10000 Set these MySQL variables.
-
#socket ⇒ Object
short form: -S; type: string Socket file to use for connection.
-
#tab ⇒ Object
Print tab-separated columns, instead of aligned.
-
#user ⇒ Object
short form: -u; type: string User for login if not current user.
-
#version ⇒ Object
Show version and exit.
Instance Method Summary collapse
-
#initialize ⇒ DeadlockLogger
constructor
Returns a new DeadlockLogger Object.
-
#start(options = nil) ⇒ Object
Execute the command.
Constructor Details
#initialize ⇒ DeadlockLogger
Returns a new DeadlockLogger Object
166 167 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 166 def initialize() end |
Instance Attribute Details
#ask_pass ⇒ Object
Prompt for a password when connecting to MySQL.
17 18 19 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 17 def ask_pass @ask_pass end |
#charset ⇒ Object
short form: -A; type: string Default character set. If the value is utf8, sets Perl’s binmode on STDOUT to utf8, passes the mysql_enable_utf8 option to DBD::mysql, and runs SET NAMES UTF8 after connecting to MySQL. Any other value sets binmode on STDOUT without the utf8 layer, and runs SET NAMES after connecting to MySQL.
22 23 24 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 22 def charset @charset end |
#clear_deadlocks ⇒ Object
type: string Use this table to create a small deadlock. This usually has the effect of clearing out a huge deadlock, which otherwise consumes the entire output of SHOW INNODB STATUS. The table must not exist. mk-deadlock-logger will create it with the following MAGIC_clear_deadlocks structure:
CREATE TABLE test.deadlock_maker(a INT PRIMARY KEY) ENGINE=InnoDB;
After creating the table and causing a small deadlock, the tool will drop the table again.
29 30 31 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 29 def clear_deadlocks @clear_deadlocks end |
#collapse ⇒ Object
Collapse whitespace in queries to a single space. This might make it easier to inspect on the command line or in a query. By default, whitespace is collapsed when printing with –print, but not modified when storing to –dest. (That is, the default is different for each action).
33 34 35 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 33 def collapse @collapse end |
#columns ⇒ Object
type: hash Output only this comma-separated list of columns. See OUTPUT for more details on columns.
38 39 40 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 38 def columns @columns end |
#config ⇒ Object
type: Array Read this comma-separated list of config files; if specified, this must be the first option on the command line.
43 44 45 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 43 def config @config end |
#create_dest_table ⇒ Object
Create the table specified by –dest. Normally the –dest table is expected to exist already. This option causes mk-deadlock-logger to create the table automatically using the suggested table structure.
48 49 50 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 48 def create_dest_table @create_dest_table end |
#daemonize ⇒ Object
Fork to the background and detach from the shell. POSIX operating systems only.
52 53 54 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 52 def daemonize @daemonize end |
#defaults_file ⇒ Object
short form: -F; type: string Only read mysql options from the given file. You must give an absolute pathname.
57 58 59 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 57 def defaults_file @defaults_file end |
#dest ⇒ Object
type: DSN DSN for where to store deadlocks; specify at least a database (D) and table (t). Missing values are filled in with the same values from the source host, so you can usually omit most parts of this argument if you’re storing deadlocks on the same server on which they happen. By default, whitespace in the query column is left intact; use –[no]collapse if you want whitespace collapsed. The following MAGIC_dest_table is suggested if you want to store all the information mk-deadlock-logger can extract about deadlocks:
CREATE TABLE deadlocks (
server char(20) NOT NULL,
ts datetime NOT NULL,
thread int unsigned NOT NULL,
txn_id bigint unsigned NOT NULL,
txn_time smallint unsigned NOT NULL,
user char(16) NOT NULL,
hostname char(20) NOT NULL,
ip char(15) NOT NULL, -- alternatively, ip int unsigned NOT NULL
db char(64) NOT NULL,
tbl char(64) NOT NULL,
idx char(64) NOT NULL,
lock_type char(16) NOT NULL,
lock_mode char(1) NOT NULL,
wait_hold char(1) NOT NULL,
victim tinyint unsigned NOT NULL,
query text NOT NULL,
PRIMARY KEY (server,ts,thread)
) ENGINE=InnoDB
If you use –columns, you can omit whichever columns you don’t want to store.
85 86 87 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 85 def dest @dest end |
#help ⇒ Object
Show help and exit.
89 90 91 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 89 def help @help end |
#host ⇒ Object
short form: -h; type: string Connect to host.
94 95 96 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 94 def host @host end |
#interval ⇒ Object
type: time How often to check for deadlocks. If no –run-time is specified, mk-deadlock-logger runs forever, checking for deadlocks at every interval. See also –run-time.
99 100 101 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 99 def interval @interval end |
#log ⇒ Object
type: string Print all output to this file when daemonized.
104 105 106 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 104 def log @log end |
#numeric_ip ⇒ Object
Express IP addresses as integers.
108 109 110 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 108 def numeric_ip @numeric_ip end |
#password ⇒ Object
short form: -p; type: string Password to use when connecting.
113 114 115 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 113 def password @password end |
#path_to_mk_deadlock_logger ⇒ Object
Sets the executable path, otherwise the environment path will be used.
161 162 163 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 161 def path_to_mk_deadlock_logger @path_to_mk_deadlock_logger end |
#pid ⇒ Object
type: string Create the given PID file when daemonized. The file contains the process ID of the daemonized instance. The PID file is removed when the daemonized instance exits. The program checks for the existence of the PID file when starting; if it exists and the process with the matching PID exists, the program exits.
118 119 120 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 118 def pid @pid end |
#port ⇒ Object
short form: -P; type: int Port number to use for connection.
123 124 125 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 123 def port @port end |
#print ⇒ Object
Print results on standard output. See OUTPUT for more. By default, enables –[no]collapse unless you explicitly disable it. If –interval or –run-time is specified, only new deadlocks are printed at each interval. A fingerprint for each deadlock is created using –columns server, ts and thread (even if those columns were not specified by –columns) and if the current deadlock’s fingerprint is different from the last deadlock’s fingerprint, then it is printed.
128 129 130 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 128 def print @print end |
#run_time ⇒ Object
type: time How long to run before exiting. By default mk-deadlock-logger runs once, checks for deadlocks, and exits. If –run-time is specified but no –interval is specified, a default 1 second interval will be used.
133 134 135 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 133 def run_time @run_time end |
#set_vars ⇒ Object
type: string; default: wait_timeout=10000 Set these MySQL variables. Immediately after connecting to MySQL, this string will be appended to SET and executed.
138 139 140 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 138 def set_vars @set_vars end |
#socket ⇒ Object
short form: -S; type: string Socket file to use for connection.
143 144 145 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 143 def socket @socket end |
#tab ⇒ Object
Print tab-separated columns, instead of aligned.
147 148 149 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 147 def tab @tab end |
#user ⇒ Object
short form: -u; type: string User for login if not current user.
152 153 154 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 152 def user @user end |
#version ⇒ Object
Show version and exit.
156 157 158 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 156 def version @version end |
Instance Method Details
#start(options = nil) ⇒ Object
Execute the command
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/maatkit-ruby/mk-deadlock-logger.rb', line 172 def start( = nil) tmp = Tempfile.new('tmp') command = option_string() + .to_s + " 2> " + tmp.path success = system(command) if success begin while (line = tmp.readline) line.chomp selected_string = line end rescue EOFError tmp.close end return selected_string else tmp.close! return success end end |