# -*- coding: utf-8 -*- require 'isono' module Dcmgr module NodeModules class InstanceHA < Isono::NodeModules::Base include Dcmgr::Logger initialize_hook do @thread_pool = Isono::ThreadPool.new(1, 'InstanceHA') event = Isono::NodeModules::EventChannel.new(node) event.subscribe('hva/fault_instance', '#') { |args| @thread_pool.pass { inst_id = args[0] inst = Models::Instance[inst_id] # check if the instance has HA enable. next if inst.ha_enabled == 0 myinstance.restart_instance(inst) } } end terminate_hook do @thread_pool.shutdown end def restart_instance(inst) # terminate and cleanup begin jobrpc.run("kvm-handle.#{inst.host_pool.node_id}", 'cleanup', inst.canonical_uuid) rescue => e # termination may fail end # TODO: pick a new host node Isono::NodeModules::DataStore.barrier { inst.state = :failingover inst.save } # start a new backup instance case inst.image.boot_dev_type when Models::Image::BOOT_DEV_SAN boot_vol = inst.volume.find {|v| v.boot_dev == 1 } res = jobrpc.submit("kvm-handle.#{inst.host_pool.node_id}", 'run_vol_store', inst.canonical_uuid, boot_vol.canonical_uuid) when Models::Image::BOOT_DEV_LOCAL res = jobrpc.submit("kvm-handle.#{inst.host_pool.node_id}", 'run_local_store', inst.canonical_uuid) else raise "Unknown boot type" end logger.info("#{inst.canonical_uuid} has been restarted") end private def event @event ||= Isono::NodeModules::EventChannel.new(node) end def jobrpc @jobrpc ||= Isono::NodeModules::JobChannel.new(node) end end end end