diff --git a/include/DispatchManager.h b/include/DispatchManager.h index 7bf870f885..43e91271b1 100644 --- a/include/DispatchManager.h +++ b/include/DispatchManager.h @@ -486,14 +486,37 @@ private: */ void free_vm_resources(VirtualMachine * vm, bool check_images); + //-------------------------------------------------------------------------- + // DM Actions associated with a VM state transition + //-------------------------------------------------------------------------- + + void suspend_success_action(int vid); + + void stop_success_action(int vid); + + void undeploy_success_action(int vid); + + void poweroff_success_action(int vid); + + void done_action(int vid); + + void resubmit_action(int vid); + /** - * Fill a template only with the necessary attributes to update the quotas - * @param vm with the attributes - * @param template that will be filled - * @param only_running true to not add CPU, MEMORY and VMS counters - */ - void get_quota_template(VirtualMachine * vm, - VirtualMachineTemplate& quota_tmpl, bool only_running) const; + * Function to execute the Manager action loop method within a new pthread + * (requires C linkage) + */ + friend void * dm_action_loop(void *arg); + + // ------------------------------------------------------------------------- + // Action Listener interface + // ------------------------------------------------------------------------- + void finalize_action(const ActionRequest& ar) + { + NebulaLog::log("DiM",Log::INFO,"Stopping Dispatch Manager..."); + }; + + void user_action(const ActionRequest& ar); }; #endif /*DISPATCH_MANAGER_H*/ diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 412d177e38..4628dcc20c 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -1020,6 +1020,13 @@ public: */ bool is_pinned() const; + /** + * Fill a template only with the necessary attributes to update the quotas + * @param qtmpl template that will be filled + * @param only_running true to not add CPU, MEMORY and VMS counters + */ + void get_quota_template(VirtualMachineTemplate& qtmpl, bool only_running); + // ------------------------------------------------------------------------ // Virtual Machine Disks // ------------------------------------------------------------------------ diff --git a/src/dm/DispatchManager.cc b/src/dm/DispatchManager.cc index 8ae23e2fb0..72dab0a449 100644 --- a/src/dm/DispatchManager.cc +++ b/src/dm/DispatchManager.cc @@ -55,39 +55,3 @@ void DispatchManager::init_managers() /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void DispatchManager::get_quota_template(VirtualMachine * vm, - VirtualMachineTemplate& quota_tmpl, bool only_running) const -{ - std::string memory, cpu; - - vm->get_template_attribute("MEMORY", memory); - vm->get_template_attribute("CPU", cpu); - - if ( (vm->get_state() == VirtualMachine::ACTIVE) || - (vm->get_state() == VirtualMachine::PENDING) || - (vm->get_state() == VirtualMachine::CLONING) || - (vm->get_state() == VirtualMachine::CLONING_FAILURE) || - (vm->get_state() == VirtualMachine::HOLD) ) - { - quota_tmpl.add("RUNNING_MEMORY", memory); - quota_tmpl.add("RUNNING_CPU", cpu); - quota_tmpl.add("RUNNING_VMS", 1); - - if (only_running) - { - quota_tmpl.add("MEMORY", 0); - quota_tmpl.add("CPU", 0); - quota_tmpl.add("VMS", 0); - } - else - { - quota_tmpl.add("MEMORY", memory); - quota_tmpl.add("CPU", cpu); - quota_tmpl.add("VMS", 1); - } - } -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - diff --git a/src/dm/DispatchManagerActions.cc b/src/dm/DispatchManagerActions.cc index 1f92f2b85f..d48c98dfea 100644 --- a/src/dm/DispatchManagerActions.cc +++ b/src/dm/DispatchManagerActions.cc @@ -73,7 +73,7 @@ int DispatchManager::deploy(VirtualMachine * vm, const RequestAttributes& ra) uid = vm->get_uid(); gid = vm->get_gid(); - get_quota_template(vm, quota_tmpl, true); + vm->get_quota_template(quota_tmpl, true); } lcm->trigger_deploy(vid); @@ -156,7 +156,7 @@ int DispatchManager::import(VirtualMachine * vm, const RequestAttributes& ra) uid = vm->get_uid(); gid = vm->get_gid(); - get_quota_template(vm, quota_tmpl, true); + vm->get_quota_template(quota_tmpl, true); do_quotas = true; } @@ -1248,7 +1248,7 @@ int DispatchManager::delete_recreate(VirtualMachine * vm, if ( do_quotas ) { - get_quota_template(vm, quota_tmpl, true); + vm->get_quota_template(quota_tmpl, true); } break; diff --git a/src/dm/DispatchManagerStates.cc b/src/dm/DispatchManagerStates.cc index 412a57b06d..3df60a54d0 100644 --- a/src/dm/DispatchManagerStates.cc +++ b/src/dm/DispatchManagerStates.cc @@ -26,11 +26,6 @@ using namespace std; void DispatchManager::trigger_suspend_success(int vid) { trigger([this, vid] { - VirtualMachineTemplate quota_tmpl; - string error_str; - - int uid, gid; - VirtualMachine * vm = vmpool->get(vid); if (vm == nullptr) @@ -46,13 +41,22 @@ void DispatchManager::trigger_suspend_success(int vid) vm->get_lcm_state() == VirtualMachine::DISK_SNAPSHOT_REVERT_SUSPENDED|| vm->get_lcm_state() == VirtualMachine::DISK_SNAPSHOT_DELETE_SUSPENDED)) { - get_quota_template(vm, quota_tmpl, true); + VirtualMachineTemplate quota_tmpl; + + vm->get_quota_template(quota_tmpl, true); vm->set_state(VirtualMachine::SUSPENDED); vm->set_state(VirtualMachine::LCM_INIT); vmpool->update(vm); + + int uid = vm->get_uid(); + int gid = vm->get_gid(); + + vm->unlock(); + + Quotas::vm_del(uid, gid, "a_tmpl); } else { @@ -65,13 +69,6 @@ void DispatchManager::trigger_suspend_success(int vid) vm->unlock(); return; } - - uid = vm->get_uid(); - gid = vm->get_gid(); - - vm->unlock(); - - Quotas::vm_del(uid, gid, "a_tmpl); }); } @@ -81,11 +78,6 @@ void DispatchManager::trigger_suspend_success(int vid) void DispatchManager::trigger_stop_success(int vid) { trigger([this, vid] { - VirtualMachineTemplate quota_tmpl; - string error_str; - - int uid, gid; - VirtualMachine * vm = vmpool->get(vid); if (vm == nullptr) @@ -97,7 +89,9 @@ void DispatchManager::trigger_stop_success(int vid) (vm->get_lcm_state() == VirtualMachine::EPILOG_STOP || vm->get_lcm_state() == VirtualMachine::PROLOG_RESUME)) { - get_quota_template(vm, quota_tmpl, true); + VirtualMachineTemplate quota_tmpl; + + vm->get_quota_template(quota_tmpl, true); vm->set_state(VirtualMachine::STOPPED); @@ -112,6 +106,13 @@ void DispatchManager::trigger_stop_success(int vid) } vmpool->update(vm); + + int uid = vm->get_uid(); + int gid = vm->get_gid(); + + vm->unlock(); + + Quotas::vm_del(uid, gid, "a_tmpl); } else { @@ -123,13 +124,6 @@ void DispatchManager::trigger_stop_success(int vid) vm->unlock(); return; } - - uid = vm->get_uid(); - gid = vm->get_gid(); - - vm->unlock(); - - Quotas::vm_del(uid, gid, "a_tmpl); }); } @@ -139,11 +133,6 @@ void DispatchManager::trigger_stop_success(int vid) void DispatchManager::trigger_undeploy_success(int vid) { trigger([this, vid] { - VirtualMachineTemplate quota_tmpl; - string error_str; - - int uid, gid; - VirtualMachine * vm = vmpool->get(vid); if (vm == nullptr) @@ -156,7 +145,9 @@ void DispatchManager::trigger_undeploy_success(int vid) vm->get_lcm_state() == VirtualMachine::DISK_RESIZE_UNDEPLOYED || vm->get_lcm_state() == VirtualMachine::PROLOG_UNDEPLOY)) { - get_quota_template(vm, quota_tmpl, true); + VirtualMachineTemplate quota_tmpl; + + vm->get_quota_template(quota_tmpl, true); vm->set_state(VirtualMachine::UNDEPLOYED); @@ -171,6 +162,13 @@ void DispatchManager::trigger_undeploy_success(int vid) } vmpool->update(vm); + + int uid = vm->get_uid(); + int gid = vm->get_gid(); + + vm->unlock(); + + Quotas::vm_del(uid, gid, "a_tmpl); } else { @@ -183,13 +181,6 @@ void DispatchManager::trigger_undeploy_success(int vid) vm->unlock(); return; } - - uid = vm->get_uid(); - gid = vm->get_gid(); - - vm->unlock(); - - Quotas::vm_del(uid, gid, "a_tmpl); }); } @@ -199,11 +190,6 @@ void DispatchManager::trigger_undeploy_success(int vid) void DispatchManager::trigger_poweroff_success(int vid) { trigger([this, vid] { - VirtualMachineTemplate quota_tmpl; - string error_str; - - int uid, gid; - VirtualMachine * vm = vmpool->get(vid); if (vm == nullptr) @@ -225,13 +211,27 @@ void DispatchManager::trigger_poweroff_success(int vid) vm->get_lcm_state() == VirtualMachine::HOTPLUG_NIC_POWEROFF || vm->get_lcm_state() == VirtualMachine::PROLOG_MIGRATE_POWEROFF_FAILURE)) { - get_quota_template(vm, quota_tmpl, true); + VirtualMachineTemplate quota_tmpl; + + vm->get_quota_template(quota_tmpl, true); vm->set_state(VirtualMachine::POWEROFF); vm->set_state(VirtualMachine::LCM_INIT); vmpool->update(vm); + + int uid = vm->get_uid(); + int gid = vm->get_gid(); + + vm->unlock(); + + if (prev_state != VirtualMachine::DISK_SNAPSHOT_POWEROFF && + prev_state != VirtualMachine::DISK_SNAPSHOT_REVERT_POWEROFF && + prev_state != VirtualMachine::DISK_SNAPSHOT_DELETE_POWEROFF) + { + Quotas::vm_del(uid, gid, "a_tmpl); + } } else { @@ -244,18 +244,6 @@ void DispatchManager::trigger_poweroff_success(int vid) vm->unlock(); return; } - - uid = vm->get_uid(); - gid = vm->get_gid(); - - vm->unlock(); - - if (prev_state != VirtualMachine::DISK_SNAPSHOT_POWEROFF && - prev_state != VirtualMachine::DISK_SNAPSHOT_REVERT_POWEROFF && - prev_state != VirtualMachine::DISK_SNAPSHOT_DELETE_POWEROFF) - { - Quotas::vm_del(uid, gid, "a_tmpl); - } }); } @@ -265,11 +253,6 @@ void DispatchManager::trigger_poweroff_success(int vid) void DispatchManager::trigger_done(int vid) { trigger([this, vid] { - string error_str; - - VirtualMachine::LcmState lcm_state; - VirtualMachine::VmState dm_state; - VirtualMachine * vm = vmpool->get(vid); if (vm == nullptr) @@ -277,8 +260,8 @@ void DispatchManager::trigger_done(int vid) return; } - lcm_state = vm->get_lcm_state(); - dm_state = vm->get_state(); + VirtualMachine::LcmState lcm_state = vm->get_lcm_state(); + VirtualMachine::VmState dm_state = vm->get_state(); if ((dm_state == VirtualMachine::ACTIVE) && (lcm_state == VirtualMachine::EPILOG || diff --git a/src/lcm/LifeCycleStates.cc b/src/lcm/LifeCycleStates.cc index 7df912f88a..9c74b66292 100644 --- a/src/lcm/LifeCycleStates.cc +++ b/src/lcm/LifeCycleStates.cc @@ -1150,9 +1150,7 @@ void LifeCycleManager::trigger_monitor_poweroff(int vid) void LifeCycleManager::trigger_monitor_poweron(int vid) { trigger([this, vid] { - VirtualMachine * vm; - - vm = vmpool->get(vid); + VirtualMachine * vm = vmpool->get(vid); if ( vm == nullptr ) { @@ -1162,6 +1160,12 @@ void LifeCycleManager::trigger_monitor_poweron(int vid) if ( vm->get_state() == VirtualMachine::POWEROFF || vm->get_state() == VirtualMachine::SUSPENDED ) { + VirtualMachineTemplate quota_tmpl; + string error; + + int uid = vm->get_uid(); + int gid = vm->get_gid(); + vm->log("VMM",Log::INFO,"VM found again by the drivers"); time_t the_time = time(0); @@ -1183,6 +1187,12 @@ void LifeCycleManager::trigger_monitor_poweron(int vid) vmpool->insert_history(vm); vmpool->update(vm); + + vm->get_quota_template(quota_tmpl, true); + + vm->unlock(); + + Quotas::vm_check(uid, gid, "a_tmpl, error); } else if ( vm->get_state() == VirtualMachine::ACTIVE ) { @@ -1213,9 +1223,9 @@ void LifeCycleManager::trigger_monitor_poweron(int vid) default: break; } - } - vm->unlock(); + vm->unlock(); + } }); } diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index b679635262..cfa89169d8 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -3623,3 +3623,39 @@ void VirtualMachine::decrypt() /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ +void VirtualMachine::get_quota_template(VirtualMachineTemplate& quota_tmpl, + bool only_running) +{ + std::string memory, cpu; + + get_template_attribute("MEMORY", memory); + get_template_attribute("CPU", cpu); + + if ((state == VirtualMachine::ACTIVE) || + (state == VirtualMachine::PENDING) || + (state == VirtualMachine::CLONING) || + (state == VirtualMachine::CLONING_FAILURE) || + (state == VirtualMachine::HOLD) ) + { + quota_tmpl.add("RUNNING_MEMORY", memory); + quota_tmpl.add("RUNNING_CPU", cpu); + quota_tmpl.add("RUNNING_VMS", 1); + + if (only_running) + { + quota_tmpl.add("MEMORY", 0); + quota_tmpl.add("CPU", 0); + quota_tmpl.add("VMS", 0); + } + else + { + quota_tmpl.add("MEMORY", memory); + quota_tmpl.add("CPU", cpu); + quota_tmpl.add("VMS", 1); + } + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ +