Module: Repctl::Helpers

Includes:
Config, Commands, Servers
Defined in:
lib/repctl/helpers.rb

Instance Method Summary collapse

Methods included from Servers

#all_instances, #all_live_instances, #all_live_servers, #all_servers, #get_mysqld_pid, #instance_for, #live?, #mysqld_running?, #server_for_instance

Methods included from Commands

#do_admin, #do_change_master, #do_cluster_user, #do_config, #do_crash, #do_create_widgets, #do_dump, #do_remove_slave, #do_repl_user, #do_restore, #do_secure_accounts, #do_start, #do_switch_master, #get_coordinates, #get_slave_status, #run_mysql_query

Instance Method Details

#do_add_slave(master, slave, options = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/repctl/helpers.rb', line 100

def do_add_slave(master, slave, options = {})
  puts options.to_yaml
  sync = options.has_key?(:sync) ? options[:sync] : false
  dumpfile = options.has_key?(:dumpfile) ? options[:dumpfile] : DEFAULT_DUMPFILE
  if sync
    do_reset(slave)
    coordinates = do_dump(master, dumpfile)
    do_restore(slave, dumpfile)
  end
  coordinates = get_coordinates(master)
  do_change_master(master, slave, coordinates)
  do_start_slave(slave)
  do_cluster_user(slave)
  do_repl_user(slave)
end

#do_repl_pair(master, slave) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/repctl/helpers.rb', line 116

def do_repl_pair(master, slave)
  do_reset(master)
  do_reset(slave)
  do_cluster_user(master)
  do_repl_user(master)
  coordinates = get_coordinates(master)
  file = coordinates[:file]
  position = coordinates[:position]
  do_change_master(master, slave, :file => file, :position => position)
  do_start_slave(slave)
end

#do_repl_trio(master, slave1, slave2, options = {}) ⇒ Object



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
# File 'lib/repctl/helpers.rb', line 128

def do_repl_trio(master, slave1, slave2, options = {})
  reset = options.has_key?(:reset) ? options[:reset] : true
  if reset
    do_reset(master)
    do_reset(slave1)
    do_reset(slave2)
  else
    do_restart(master)
    do_restart(slave1)
    do_restart(slave2)
  end
  # Set up the replication accounts for all servers, in case we
  # decide to switch masters later.
  [master, slave1, slave2].each do |s|
    do_cluster_user(s)
    do_repl_user(s)
  end
  coordinates = get_coordinates(master)
  file = coordinates[:file]
  position = coordinates[:position]
  do_change_master(master, slave1, :file => file, :position => position)
  do_start_slave(slave1)
  do_change_master(master, slave2, :file => file, :position => position)
  do_start_slave(slave2)
end

#do_reset(instance) ⇒ Object



12
13
14
15
16
17
# File 'lib/repctl/helpers.rb', line 12

def do_reset(instance)
  do_stop(instance)
  do_config(instance)
  do_start(instance)
  do_secure_accounts(instance)
end

#do_restart(instance) ⇒ Object



27
28
29
30
# File 'lib/repctl/helpers.rb', line 27

def do_restart(instance)
  do_admin(instance, "shutdown")
  do_start(instance)
end

#do_start_slave(instance) ⇒ Object



19
20
21
# File 'lib/repctl/helpers.rb', line 19

def do_start_slave(instance)
  run_mysql_query(instance, "START SLAVE")
end

#do_stop(instance) ⇒ Object



8
9
10
# File 'lib/repctl/helpers.rb', line 8

def do_stop(instance)
  do_admin(instance, "shutdown")
end

#do_stop_slave(instance) ⇒ Object



23
24
25
# File 'lib/repctl/helpers.rb', line 23

def do_stop_slave(instance)
  run_mysql_query(instance, "STOP SLAVE")
end

#formatted_status(options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/repctl/helpers.rb', line 68

def formatted_status(options = {})
  output = []
  header = sprintf("%-5s%-27s%-27s%-27s%-8s",
    "inst", "master", "received", "applied", "lag")
  output << header.colorize(:green)
  todos = repl_status(options)
  todos.each do |server|
    instance = server[:instance]
    gen_binlog = server[:generated_binlog]
    if server[:master]
      server[:master].match(/.*:(\d*)$/)
      master_instance = $1
      recv_binlog = server[:received_binlog]
      app_binlog = server[:applied_binlog]
      lag = server[:lag]
      if lag == nil
        lag = "-"
      else
        lag = lag.to_s
      end
      format = "%1d%-4s%-27s%-27s%-27s%-8s"
      str = sprintf(format, instance, "(#{master_instance})",
        gen_binlog, recv_binlog, app_binlog, lag)
    else
      format = "%-5d%-26s"
      str = sprintf(format, instance, gen_binlog)
    end
    output << str.colorize(:yellow)
  end
  output
end

#repl_status(options = {}) ⇒ Object

Generate an array of hashes, one hash per fabric-wide instance.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/repctl/helpers.rb', line 33

def repl_status(options = {})
  todos = options[:servers] || all_live_instances
  return [] unless todos.any?
  status_array = []
  todos.each do |i|
    coordinates = get_coordinates(i)
    next unless coordinates
    master_file = coordinates[:file]
    master_pos =  coordinates[:position]

    fields = {}
    fields[:instance] = i.to_s
    fields[:server] = "#{server_for_instance(i)['hostname']}:#{i}"
    fields[:generated_binlog] = "#{master_file}:#{master_pos}"
    if is_slave?(i)
      slave_status = get_slave_status(i)
      recv_file = slave_status["Master_Log_File"]
      recv_pos = slave_status["Read_Master_Log_Pos"]
      apply_file = slave_status["Relay_Master_Log_File"]
      apply_pos = slave_status["Exec_Master_Log_Pos"]
      lag = slave_status["Seconds_Behind_Master"]
      master_host = slave_status["Master_Host"]
      master_port = slave_status["Master_Port"]
      master_instance = instance_for(master_host, master_port)

      fields[:applied_binlog] = "#{apply_file}:#{apply_pos}"
      fields[:received_binlog] = "#{recv_file}:#{recv_pos}"
      fields[:master] = "#{master_host}:#{master_instance}"
      fields[:lag] = lag
    end
    status_array << fields
  end
  status_array
end