Class: MysqlIsolatedServer

Inherits:
Object
  • Object
show all
Includes:
DBConnection, Socket::Constants
Defined in:
lib/mysql_isolated_server.rb,
lib/mysql_isolated_server/version.rb,
lib/mysql_isolated_server/jdbc_connection.rb,
lib/mysql_isolated_server/mysql2_connection.rb

Defined Under Namespace

Modules: DBConnection Classes: WrappedJDBCConnection

Constant Summary collapse

MYSQL_BASE_DIR =
"/usr"
VERSION =
"0.5.4"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DBConnection

#connection

Constructor Details

#initialize(options = {}) ⇒ MysqlIsolatedServer

Returns a new instance of MysqlIsolatedServer.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mysql_isolated_server.rb', line 16

def initialize(options = {})
  @base = options[:base] || Dir.mktmpdir("mysql_isolated", "/tmp")
  @mysql_data_dir="#{@base}/mysqld"
  @mysql_socket="#{@mysql_data_dir}/mysqld.sock"
  @params = options[:params]
  @load_data_path = options[:data_path]
  @port = options[:port]
  @allow_output = options[:allow_output]
  @log_bin = options[:log_bin] || "--log-bin"
  @parent_pid = options[:pid]
  @server_id = rand(2**31)
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



12
13
14
# File 'lib/mysql_isolated_server.rb', line 12

def base
  @base
end

#initial_binlog_fileObject (readonly)

Returns the value of attribute initial_binlog_file.



12
13
14
# File 'lib/mysql_isolated_server.rb', line 12

def initial_binlog_file
  @initial_binlog_file
end

#initial_binlog_posObject (readonly)

Returns the value of attribute initial_binlog_pos.



12
13
14
# File 'lib/mysql_isolated_server.rb', line 12

def initial_binlog_pos
  @initial_binlog_pos
end

#paramsObject

Returns the value of attribute params.



13
14
15
# File 'lib/mysql_isolated_server.rb', line 13

def params
  @params
end

#pidObject (readonly)

Returns the value of attribute pid.



12
13
14
# File 'lib/mysql_isolated_server.rb', line 12

def pid
  @pid
end

#portObject (readonly)

Returns the value of attribute port.



12
13
14
# File 'lib/mysql_isolated_server.rb', line 12

def port
  @port
end

Class Method Details

.exec_wait(cmd, options = {}) ⇒ Object



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
219
220
221
222
223
224
# File 'lib/mysql_isolated_server.rb', line 171

def self.exec_wait(cmd, options = {})
  allow_output = options[:allow_output] # default false
  parent_pid = options[:parent_pid] || $$

  fork do
    exec_pid = fork do
      if !allow_output
        devnull = File.open("/dev/null", "w")
        STDOUT.reopen(devnull)
        STDERR.reopen(devnull)
      end

      exec(cmd)
    end

    # begin waiting for the parent (or mysql) to die; at_exit is hard to control when interacting with test/unit
    # we can also be killed by our parent with down! and up!
    #
    ["TERM", "INT"].each do |sig|
      trap(sig) do
        if block_given?
          yield(exec_pid)
        else
          Process.kill("KILL", exec_pid)
        end

        exit!
      end
    end

    # HUP == don't cleanup.
    trap("HUP") do
      Process.kill("KILL", exec_pid)
      exit!
    end

    while true
      begin
        Process.kill(0, parent_pid)
        Process.kill(0, exec_pid)
      rescue Exception => e
        if block_given?
          yield(exec_pid)
        else
          Process.kill("KILL", exec_pid)
        end

        exit!
      end

      sleep 1
    end
  end
end

.thread_boot(*params) ⇒ Object



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
56
# File 'lib/mysql_isolated_server.rb', line 29

def self.thread_boot(*params)
  bin = [File.dirname(__FILE__) + "/../bin/boot_isolated_server"]
  mysql_dir, mysql_port = nil, nil
  restore_env = {}

  if `which ruby` =~ (/rvm/)
    bin = ["rvm", "1.8.7", "do", "ruby"] + bin
  end

  params = ["--pid", $$.to_s] + params

  Thread.abort_on_exception = true
  Thread.new do
    ENV.keys.grep(/GEM|BUNDLE|RUBYOPT/).each do |k|
      restore_env[k] = ENV.delete(k)
    end
    pipe = IO.popen(bin + params, "r") do |pipe|
      mysql_dir = pipe.readline.split(' ').last
      mysql_port = pipe.readline.split(' ').last.to_i
      sleep
    end
  end

  while mysql_port.nil?
    sleep 1
  end
  new(:port => mysql_port, :base => mysql_dir)
end

Instance Method Details

#boot!Object



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
145
146
147
148
149
150
151
152
153
# File 'lib/mysql_isolated_server.rb', line 109

def boot!
  @port ||= grab_free_port
  system("rm -Rf #{@mysql_data_dir}")
  system("mkdir #{@mysql_data_dir}")
  if @load_data_path
    system("cp -a #{@load_data_path}/* #{@mysql_data_dir}")
    system("rm -f #{@mysql_data_dir}/relay-log.info")
  else
    mysql_install_db = locate_executable("mysql_install_db")

    idb_path = File.dirname(mysql_install_db)
    system("(cd #{idb_path}/..; mysql_install_db --datadir=#{@mysql_data_dir} --user=`whoami`) >/dev/null 2>&1")
    system("cp #{File.expand_path(File.dirname(__FILE__))}/tables/user.* #{@mysql_data_dir}/mysql")
  end

  if !@log_bin
    @log_bin = "--log-bin"
  else
    if @log_bin[0] != '/'
      binlog_dir = "#{@mysql_data_dir}/#{@log_bin}"
    else
      binlog_dir = @log_bin
    end

    system("mkdir -p #{binlog_dir}")
    @log_bin = "--log-bin=#{binlog_dir}"
  end

  up!

  master_binlog_info = connection.query("show master status").first
  @initial_binlog_file, @initial_binlog_pos = master_binlog_info.values_at('File', 'Position')

  tzinfo_to_sql = locate_executable("mysql_tzinfo_to_sql5", "mysql_tzinfo_to_sql")
  raise "could not find mysql_tzinfo_to_sql" unless tzinfo_to_sql
  system("#{tzinfo_to_sql} /usr/share/zoneinfo 2>/dev/null| mysql -h127.0.0.1 --database=mysql --port=#{@port} -u root mysql ")

  begin
    connection.query("SET GLOBAL time_zone='UTC'")
  rescue Mysql2::Error
    connection.query("SET GLOBAL time_zone='UTC'")
  end

  connection.query("SET GLOBAL server_id=#{@server_id}")
end

#cleanup!Object



245
246
247
# File 'lib/mysql_isolated_server.rb', line 245

def cleanup!
  system("rm -Rf #{base}")
end

#down!Object



100
101
102
103
104
105
106
107
# File 'lib/mysql_isolated_server.rb', line 100

def down!
  Process.kill("HUP", @pid)
  while (Process.kill 0, @pid rescue false)
    Process.waitpid(@pid)
    sleep 1
  end
  @cx = nil
end

#exec_server(cmd) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/mysql_isolated_server.rb', line 226

def exec_server(cmd)
  cmd.strip!
  cmd.gsub!(/\\\n/, ' ')
  system("mkdir -p #{base}/tmp")
  system("chmod 0777 #{base}/tmp")

  parent_pid = @parent_pid || $$
  mysql_pid = nil

  ENV["TMPDIR"] = "#{base}/tmp"
  @pid = MysqlIsolatedServer.exec_wait(cmd, allow_output: @allow_output, parent_pid: @parent_pid) do |mysql_pid|
    Process.kill("KILL", mysql_pid)
    cleanup!
  end
end

#grab_free_portObject



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/mysql_isolated_server.rb', line 156

def grab_free_port
  while true
    candidate=9000 + rand(50_000)

    begin
      socket = Socket.new(AF_INET, SOCK_STREAM, 0)
      socket.bind(Socket.pack_sockaddr_in(candidate, '127.0.0.1'))
      socket.close
      return candidate
    rescue Exception => e
    end
  end
end

#kill!Object



249
250
251
252
# File 'lib/mysql_isolated_server.rb', line 249

def kill!
  return unless @pid
  system("kill -TERM #{@pid}")
end

#locate_executable(*candidates) ⇒ Object



80
81
82
83
84
# File 'lib/mysql_isolated_server.rb', line 80

def locate_executable(*candidates)
  output = `which #{candidates.join(' ')}`
  raise "I couldn't find any of these: #{candidates.join(',')} in $PATH" if output.chomp.empty?
  output.split("\n").first
end

#make_slave_of(master) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mysql_isolated_server.rb', line 58

def make_slave_of(master)
  binlog_file = master.initial_binlog_file || (@log_bin.split('/').last + ".000001")
  binlog_pos = master.initial_binlog_pos || 4

  connection.query(<<-EOL
    change master to master_host='127.0.0.1',
                     master_port=#{master.port},
                     master_user='root', master_password='',
                     master_log_file='#{binlog_file}',
                     master_log_pos=#{binlog_pos}
    EOL
  )
  connection.query("START SLAVE")
  connection.query("SET GLOBAL READ_ONLY=1")
end

#mysql_shellObject



242
243
244
# File 'lib/mysql_isolated_server.rb', line 242

def mysql_shell
  system("mysql -uroot --port #{@port} mysql --host 127.0.0.1")
end

#reconnect!Object



254
255
256
257
# File 'lib/mysql_isolated_server.rb', line 254

def reconnect!
  @cx = nil
  connection
end

#set_rw(rw) ⇒ Object



74
75
76
77
# File 'lib/mysql_isolated_server.rb', line 74

def set_rw(rw)
  ro = rw ? 0 : 1
  connection.query("SET GLOBAL READ_ONLY=#{ro}")
end

#up!Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mysql_isolated_server.rb', line 86

def up!
  mysqld = locate_executable("mysqld")

  exec_server <<-EOL
      #{mysqld} --no-defaults --default-storage-engine=innodb \
              --datadir=#{@mysql_data_dir} --pid-file=#{@base}/mysqld.pid --port=#{@port} \
              #{@params} --socket=#{@mysql_data_dir}/mysql.sock #{@log_bin} --log-slave-updates
  EOL

  while !system("mysql -h127.0.0.1 --port=#{@port} --database=mysql -u root -e 'select 1' >/dev/null 2>&1")
    sleep(0.1)
  end
end