Class: Wakame::Service::MySQL_Slave

Inherits:
Property
  • Object
show all
Defined in:
lib/wakame/service.rb

Constant Summary

Constants included from AttributeHelper

AttributeHelper::CONVERT_CLASSES, AttributeHelper::PRIMITIVE_CLASSES

Instance Attribute Summary collapse

Attributes inherited from Property

#check_time, #vm_spec

Instance Method Summary collapse

Methods inherited from Property

#dump_status, #on_child_changed, #on_parent_changed, #reload, #render_config

Methods included from AttributeHelper

#dump_attrs

Constructor Details

#initializeMySQL_Slave

Returns a new instance of MySQL_Slave.



749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
# File 'lib/wakame/service.rb', line 749

def initialize
  super()
  @template = ConfigurationTemplate::MySQLSlaveTemplate.new()
  @basedir = '/home/wakame/mysql'

  @mysqld_server_id = 2 # dynamic
  @mysqld_port = 3307
  @mysqld_datadir = File.expand_path('data-slave', @basedir)

  @ebs_volume = 'vol-38bc5f51'  # master volume_id
  @ebs_device = '/dev/sdn'      # slave mount point
  @ebs_mount_option = 'noatime'

  @mysqld_master_host = '10.249.2.115'
  @mysqld_master_user = 'wakame-repl'
  @mysqld_master_pass = 'wakame-slave'
  @mysqld_master_port = 3306
  @mysqld_master_datadir = File.expand_path('data', @basedir)

  @duplicable = false
end

Instance Attribute Details

#basedirObject (readonly)

Returns the value of attribute basedir.



747
748
749
# File 'lib/wakame/service.rb', line 747

def basedir
  @basedir
end

#ebs_deviceObject (readonly)

Returns the value of attribute ebs_device.



747
748
749
# File 'lib/wakame/service.rb', line 747

def ebs_device
  @ebs_device
end

#ebs_volumeObject (readonly)

Returns the value of attribute ebs_volume.



747
748
749
# File 'lib/wakame/service.rb', line 747

def ebs_volume
  @ebs_volume
end

#mysqld_datadirObject (readonly)

Returns the value of attribute mysqld_datadir.



747
748
749
# File 'lib/wakame/service.rb', line 747

def mysqld_datadir
  @mysqld_datadir
end

#mysqld_log_binObject (readonly)

Returns the value of attribute mysqld_log_bin.



747
748
749
# File 'lib/wakame/service.rb', line 747

def mysqld_log_bin
  @mysqld_log_bin
end

#mysqld_portObject (readonly)

Returns the value of attribute mysqld_port.



747
748
749
# File 'lib/wakame/service.rb', line 747

def mysqld_port
  @mysqld_port
end

#mysqld_server_idObject (readonly)

Returns the value of attribute mysqld_server_id.



747
748
749
# File 'lib/wakame/service.rb', line 747

def mysqld_server_id
  @mysqld_server_id
end

Instance Method Details

#before_start(svc, action) ⇒ Object



771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
# File 'lib/wakame/service.rb', line 771

def before_start(svc, action)
  vm_manipulator = VmManipulator.create

  Wakame.log.debug("mkdir #{@mysqld_datadir}")
  system("[ -d #{@mysqld_datadir} ] || mkdir -p #{@mysqld_datadir}")
  Wakame.log.debug("[ -b #{@ebs_device} ]")
  system("[ -b #{@ebs_device} ]")
  if $? == 0
    Wakame.log.debug("The EBS volume(slave) device is not ready to attach: #{@ebs_device}")
    return
  end

  volume_map = vm_manipulator.describe_volume(@ebs_volume)
  Wakame.log.debug("describe_volume(#{@ebs_volume}): #{volume_map.inspect}")
  if volume_map['status'] == 'in-use'
    # Nothin to be done
  else
    Wakame.log.debug("The EBS volume(slave) is not ready to attach: #{@ebs_volume}")
    return
  end

  system("echo show master status | /usr/bin/mysql -h#{@mysqld_master_host} -P#{@mysqld_master_port} -u#{@mysqld_master_user}  -p#{@mysqld_master_pass}")
  if $? != 0
    raise "Can't connect mysql master: #{@mysqld_master_host}:#{@mysqld_master_port}"
  end

  system("echo 'FLUSH TABLES WITH READ LOCK;' | /usr/bin/mysql -h#{@mysqld_master_host} -P#{@mysqld_master_port} -u#{@mysqld_master_user}  -p#{@mysqld_master_pass} -s")
  master_status = `echo show master status | /usr/bin/mysql -h#{@mysqld_master_host} -P#{@mysqld_master_port} -u#{@mysqld_master_user}  -p#{@mysqld_master_pass} -s`.to_s.split(/\t/)[0..1]
#        p master_status

  # mysql/data/master.info
  master_infos = []
  master_infos << 14
  master_infos << master_status[0]
  master_infos << master_status[1]
  master_infos << @mysqld_master_host
  master_infos << @mysqld_master_user
  master_infos << @mysqld_master_pass
  master_infos << @mysqld_master_port
  master_infos << 60
  master_infos << 0
  master_infos << ""
  master_infos << ""
  master_infos << ""
  master_infos << ""
  master_infos << ""
  master_infos << ""

  tmp_output_basedir = File.expand_path(Wakame.gen_id, "/tmp")
  FileUtils.mkdir_p tmp_output_basedir
  master_info = File.expand_path('master.info', tmp_output_basedir)
  file = File.new(master_info, "w")
  file.puts(master_infos.join("\n"))
  file.chmod(0664)
  file.close

  3.times do |i|
    system("/bin/sync")
    sleep 1.0
  end

  Wakame.log.debug("scp -i #{Wakame.config.ssh_private_key} -o \"UserKnownHostsFile #{Wakame.config.ssh_known_hosts}\" #{master_info} root@#{@mysqld_master_host}:#{@mysqld_master_datadir}/" )
  system("scp -i #{Wakame.config.ssh_private_key} -o \"UserKnownHostsFile #{Wakame.config.ssh_known_hosts}\" #{master_info} root@#{@mysqld_master_host}:#{@mysqld_master_datadir}/" )
  Wakame.log.debug("ssh -i #{Wakame.config.ssh_private_key} -o \"UserKnownHostsFile #{Wakame.config.ssh_known_hosts}\" root@#{@mysqld_master_host} chown mysql:mysql #{@mysqld_master_datadir}/master.info" )
  system("ssh -i #{Wakame.config.ssh_private_key} -o \"UserKnownHostsFile #{Wakame.config.ssh_known_hosts}\" root@#{@mysqld_master_host} chown mysql:mysql #{@mysqld_master_datadir}/master.info" )

  3.times do |i|
    system("/bin/sync")
    sleep 1.0
  end

  FileUtils.rm_rf tmp_output_basedir

  # 2. snapshot
  Wakame.log.debug("create_snapshot (#{@ebs_volume})")
  snapshot_map = vm_manipulator.create_snapshot(@ebs_volume)
  16.times do |i|
    Wakame.log.debug("describe_snapshot(#{snapshot_map.snapshotId}) ... #{i}")
    snapshot_map = vm_manipulator.describe_snapshot(snapshot_map["snapshotId"])
    if snapshot_map["status"] == "completed"
      break
    end
    sleep 1.0
  end
  if snapshot_map["status"] != "completed"
    raise "#{snapshot_map.snapshotId} status is #{snapshot_map.status}"
  end

  # 3. unlock mysql-master
  system("echo 'UNLOCK TABLES;' | /usr/bin/mysql -h#{@mysqld_master_host} -P#{@mysqld_master_port} -u#{@mysqld_master_user}  -p#{@mysqld_master_pass}")

  # create volume /dev/xxxx
  Wakame.log.debug("create_volume_from_snapshot(#{volume_map.availabilityZone}, #{snapshot_map.snapshotId})")
  created_volume_from_snapshot_map = vm_manipulator.create_volume_from_snapshot(volume_map["availabilityZone"], snapshot_map["snapshotId"])
  volume_from_snapshot_map = created_volume_from_snapshot_map
  16.times do |i|
    Wakame.log.debug("describe_snapshot(#{snapshot_map.snapshotId}) ... #{i}")
    volume_from_snapshot_map = vm_manipulator.describe_snapshot(snapshot_map["snapshotId"])
    if volume_from_snapshot_map["status"] == "completed"
      break
    end
    sleep 1.0
  end
  if volume_from_snapshot_map["status"] != "completed"
    raise "#{volume_from_snapshot_map.snapshotId} status is #{volume_from_snapshot_map.status}"
  end

  # attach volume
  attach_volume_map = vm_manipulator.attach_volume(svc.agent.agent_id, created_volume_from_snapshot_map["volumeId"], @ebs_device)
  16.times do |i|
    Wakame.log.debug("describe_volume(#{attach_volume_map.volumeId}) ... #{i}")
    attach_volume_map = vm_manipulator.describe_volume(created_volume_from_snapshot_map["volumeId"])
    if attach_volume_map["status"] == "in-use"
      break
    end
    sleep 1.0
  end
  if attach_volume_map["status"] != "in-use"
    raise "#{attach_volume_map.volumeId} status is #{attach_volume_map.status}"
  end
end

#checkObject



902
903
904
905
906
# File 'lib/wakame/service.rb', line 902

def check
  system("/usr/bin/mysqladmin --defaults-file=/home/wakame/config/mysql-slave/my-slave.cnf ping > /dev/null")
  return false if $? != 0
  true
end

#startObject



893
894
895
896
897
898
899
900
# File 'lib/wakame/service.rb', line 893

def start
  mount_point_dev=`df "#{@mysqld_datadir}" | awk 'NR==2 {print $1}'`
  if mount_point_dev != @ebs_device
    Wakame.log.debug("Mounting EBS volume: #{@ebs_device} as #{@mysqld_datadir} (with option: #{@ebs_mount_option})")
    system("/bin/mount -o #{@ebs_mount_option} #{@ebs_device} #{@mysqld_datadir}")
  end
  system(Wakame.config.root + "/config/init.d/mysql-slave start")
end

#stopObject



908
909
910
# File 'lib/wakame/service.rb', line 908

def stop
  system(Wakame.config.root + "/config/init.d/mysql-slave stop")
end