Class: TungstenScriptMySQLDatasource
Instance Method Summary
collapse
#can_sql?, #initialize, #sql_result, #sql_results, #start, #stop
Instance Method Details
#_start_server ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/tungsten/datasources/mysql.rb', line 53
def _start_server
begin
start_command = @ti.setting(TI.setting_key(REPL_SERVICES, @service, "repl_datasource_service_start"))
TU.cmd_result("#{@ti.sudo_prefix()}#{start_command}")
rescue CommandError
end
begin
Timeout.timeout(30) {
while true
if is_running?()
break
else
sleep 1
end
end
}
rescue Timeout::Error
raise "The MySQL server has taken too long to start"
end
end
|
#_stop_server ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
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
49
50
51
|
# File 'lib/tungsten/datasources/mysql.rb', line 11
def _stop_server
begin
pid_file = get_variable("pid_file")
pid = TU.cmd_result("#{@ti.sudo_prefix()}cat #{pid_file}")
rescue CommandError
pid = ""
end
begin
stop_command = @ti.setting(TI.setting_key(REPL_SERVICES, @service, "repl_datasource_service_stop"))
TU.cmd_result("#{@ti.sudo_prefix()}#{stop_command}")
rescue CommandError
end
if is_running?() == true
raise "Unable to properly shutdown the MySQL service"
end
unless pid.to_s() == ""
begin
TU.debug("Verify that the MySQL pid has gone away")
Timeout.timeout(30) {
pid_missing = false
while pid_missing == false do
begin
TU.cmd_result("#{@ti.sudo_prefix()}ps -p #{pid}")
sleep 5
rescue CommandError
pid_missing = true
end
end
}
rescue Timeout::Error
raise "Unable to verify that MySQL has fully shutdown"
end
end
end
|
#can_lock_tables? ⇒ Boolean
110
111
112
|
# File 'lib/tungsten/datasources/mysql.rb', line 110
def can_lock_tables?
true
end
|
#can_manage_service? ⇒ Boolean
173
174
175
|
# File 'lib/tungsten/datasources/mysql.rb', line 173
def can_manage_service?
true
end
|
#get_mysql_command ⇒ Object
177
178
179
180
181
182
183
|
# File 'lib/tungsten/datasources/mysql.rb', line 177
def get_mysql_command
host = @ti.setting(@ti.setting_key(REPL_SERVICES, @service, "repl_datasource_host"))
port = @ti.setting(@ti.setting_key(REPL_SERVICES, @service, "repl_datasource_port"))
my_cnf = @ti.setting(@ti.setting_key(REPL_SERVICES, @service, "repl_datasource_mysql_service_conf"))
"mysql --defaults-file=#{my_cnf} -h#{host} --port=#{port}"
end
|
#get_option(opt) ⇒ Object
Read the configured value for a mysql variable
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/tungsten/datasources/mysql.rb', line 78
def get_option(opt)
begin
cnf = @ti.setting(@ti.setting_key(REPL_SERVICES, @service, "repl_datasource_mysql_service_conf"))
val = TU.cmd_result("my_print_defaults --config-file=#{cnf} mysqld | grep -e'^--#{opt.gsub(/[\-\_]/, "[-_]")}='")
rescue CommandError => ce
return nil
end
return val.split("\n")[0].split("=")[1]
end
|
#get_system_user ⇒ Object
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/tungsten/datasources/mysql.rb', line 99
def get_system_user
if @mysql_user == nil
@mysql_user = get_option("user")
if @mysql_user.to_s() == ""
@mysql_user = "mysql"
end
end
@mysql_user
end
|
#get_variable(var) ⇒ Object
Read the current value for a mysql variable
90
91
92
93
94
95
96
97
|
# File 'lib/tungsten/datasources/mysql.rb', line 90
def get_variable(var)
begin
sql_result("SHOW VARIABLES LIKE '#{var}'")[0]["Value"]
rescue => e
TU.debug(e)
return nil
end
end
|
#is_running? ⇒ Boolean
2
3
4
5
6
7
8
9
|
# File 'lib/tungsten/datasources/mysql.rb', line 2
def is_running?
begin
sql_result("SELECT 1")
return true
rescue
return false
end
end
|
#lock_tables ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/tungsten/datasources/mysql.rb', line 114
def lock_tables
if @lock_thread != nil
TU.debug("Unable to lock tables because they are already locked")
return
end
TU.debug("Run FLUSH TABLES WITH READ LOCK")
@lock_thread = Thread.new(get_mysql_command()) {
|mysql_command|
status = Open4::popen4("export LANG=en_US; #{mysql_command}") do |pid, stdin, stdout, stderr|
stdin.puts("SET wait_timeout=30;\n")
stdin.puts("SET lock_wait_timeout=30;\n")
stdin.puts("FLUSH TABLES WITH READ LOCK;\n")
while true
sleep 60
end
end
}
end
|
#snapshot_paths ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/tungsten/datasources/mysql.rb', line 148
def snapshot_paths
paths = []
val = get_option("datadir")
if val == nil
val = get_variable("datadir")
end
paths << val
val = get_option("innodb_data_home_dir")
if val != nil
paths << val
end
val = get_option("innodb_log_group_home_dir")
if val != nil
paths << val
end
paths.uniq()
end
|
#unlock_tables ⇒ Object
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/tungsten/datasources/mysql.rb', line 137
def unlock_tables
if @lock_thread != nil
begin
Thread.kill(@lock_thread)
rescue TypeError
end
@lock_thread = nil
end
end
|