Module: BootMysqlCluster

Defined in:
lib/isolated_database_service/boot_mysql_cluster.rb

Overview

Class Method Summary collapse

Class Method Details

.boot!(initial_sql = []) ⇒ Object



8
9
10
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
52
# File 'lib/isolated_database_service/boot_mysql_cluster.rb', line 8

def self.boot!(initial_sql = [])
  mysql_master = nil
  mysql_slave = nil
  mysql_slave_2 = nil

  threads = []
  threads << Thread.new do
    mysql_master = IsolatedServer::Mysql.new(allow_output: false)
    mysql_master.boot!
  end

  threads << Thread.new do
    mysql_slave = IsolatedServer::Mysql.new
    mysql_slave.boot!
  end

  threads << Thread.new do
    mysql_slave_2 = IsolatedServer::Mysql.new
    mysql_slave_2.boot!
  end

  threads.each(&:join)

  mysql_master.connection.query("CHANGE MASTER TO master_host='127.0.0.1', master_user='root', master_password=''")
  mysql_slave.make_slave_of(mysql_master)
  mysql_slave_2.make_slave_of(mysql_slave)

  initial_sql.each do |sql|
    mysql_master.connection.query(sql)
  end
  mysql_slave.set_rw(false)
  mysql_slave_2.set_rw(false)

  # let replication for the grants and such flow down.  bleh.
  repl_sync = false
  while !repl_sync
    repl_sync = [[mysql_master, mysql_slave], [mysql_slave, mysql_slave_2]].all? do |master, slave|
      master_pos = master.connection.query("show master status").to_a.first["Position"]
      slave.connection.query("show slave status").to_a.first["Exec_Master_Log_Pos"] == master_pos
    end
    sleep 1
  end

  [mysql_master, mysql_slave, mysql_slave_2]
end