Class: TungstenScriptMySQLDatasource

Inherits:
TungstenScriptDatasource show all
Defined in:
lib/tungsten/datasources/mysql.rb

Instance Method Summary collapse

Methods inherited from TungstenScriptDatasource

#can_sql?, #host, #initialize, #password, #port, #sql_result, #sql_results, #start, #stop, #url, #user

Constructor Details

This class inherits a constructor from TungstenScriptDatasource

Instance Method Details

#_start_serverObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tungsten/datasources/mysql.rb', line 57

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
  
  # Wait 30 seconds for the MySQL service to be responsive
  begin
    Timeout.timeout(30) {
      while true
        if is_running?()
          break
        else
          # Pause for a second before running again
          sleep 1
        end
      end
    }
  rescue Timeout::Error
    raise "The MySQL server has taken too long to start"
  end
end

#_stop_serverObject



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
52
53
54
55
# File 'lib/tungsten/datasources/mysql.rb', line 15

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
  
  # Raise an error if we got a response to the previous command
  if is_running?() == true
    raise "Unable to properly shutdown the MySQL service"
  end
  
  # We saw issues where MySQL would not close completely. This will
  # watch the PID and make sure it does not appear
  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

Returns:

  • (Boolean)


114
115
116
# File 'lib/tungsten/datasources/mysql.rb', line 114

def can_lock_tables?
  true
end

#can_manage_service?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/tungsten/datasources/mysql.rb', line 177

def can_manage_service?
  true
end

#get_mysql_commandObject



181
182
183
184
185
186
187
# File 'lib/tungsten/datasources/mysql.rb', line 181

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



82
83
84
85
86
87
88
89
90
91
# File 'lib/tungsten/datasources/mysql.rb', line 82

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_userObject



103
104
105
106
107
108
109
110
111
112
# File 'lib/tungsten/datasources/mysql.rb', line 103

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



94
95
96
97
98
99
100
101
# File 'lib/tungsten/datasources/mysql.rb', line 94

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

Returns:

  • (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_tablesObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/tungsten/datasources/mysql.rb', line 118

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")
      
      # Infinite loop to keep this thread alive until it is killed by the 
      # unlock_tables method
      while true
        sleep 60
      end
    end
  }
end

#snapshot_pathsObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/tungsten/datasources/mysql.rb', line 152

def snapshot_paths
  paths = []
  
  # The datadir may not exist in my.cnf but we need the value
  # If we don't see it in my.cnf get the value from the dbms
  val = get_option("datadir")
  if val == nil
    val = get_variable("datadir")
  end
  paths << val
  
  # These values must appear in my.cnf if they are to be used
  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
  
  # Only return a unique set of paths
  paths.uniq()
end

#titleObject



11
12
13
# File 'lib/tungsten/datasources/mysql.rb', line 11

def title
  "MySQL"
end

#unlock_tablesObject



141
142
143
144
145
146
147
148
149
150
# File 'lib/tungsten/datasources/mysql.rb', line 141

def unlock_tables
  if @lock_thread != nil
    begin
      Thread.kill(@lock_thread)
    rescue TypeError
    end

    @lock_thread = nil
  end
end