Class: TungstenScriptMySQLDatasource

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

Instance Method Summary collapse

Methods inherited from TungstenScriptDatasource

#can_sql?, #initialize, #sql_result, #sql_results, #start, #stop

Constructor Details

This class inherits a constructor from TungstenScriptDatasource

Instance Method Details

#_start_serverObject



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
  
  # 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



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
  
  # 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)


110
111
112
# File 'lib/tungsten/datasources/mysql.rb', line 110

def can_lock_tables?
  true
end

#can_manage_service?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/tungsten/datasources/mysql.rb', line 173

def can_manage_service?
  true
end

#get_mysql_commandObject



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_userObject



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

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



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")
      
      # 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



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 = []
  
  # 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

#unlock_tablesObject



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