From 69f7b4d1f75569bc540a880dc5c3d9e7c8028e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Czern=C3=BD?= Date: Wed, 12 May 2021 11:21:27 +0200 Subject: [PATCH] B #5289: Better update of sched_message (#1205) Updating whole user_template may cause lost of data due to race conditions. It's better to send update only for modified attribute, in this case only SCHED_MESSAGE --- src/scheduler/include/VirtualMachineXML.h | 7 ++++ .../src/pool/VirtualMachinePoolXML.cc | 9 +++-- src/scheduler/src/pool/VirtualMachineXML.cc | 34 ++++++++++++++++++- src/scheduler/src/sched/Scheduler.cc | 10 ------ 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/scheduler/include/VirtualMachineXML.h b/src/scheduler/include/VirtualMachineXML.h index 31bd08d279..68920d696e 100644 --- a/src/scheduler/include/VirtualMachineXML.h +++ b/src/scheduler/include/VirtualMachineXML.h @@ -457,6 +457,13 @@ protected: void init_storage_usage(); + /** + * Update the VM object in oned + * @param vm_template Object template as xml or ... + * @param append Append mode + */ + bool update(const std::string &vm_template, bool append); + /* ---------------------- SCHEDULER INFORMATION ------------------------- */ ResourceMatch match_hosts; diff --git a/src/scheduler/src/pool/VirtualMachinePoolXML.cc b/src/scheduler/src/pool/VirtualMachinePoolXML.cc index 13ea1e8054..7b378f0b2f 100644 --- a/src/scheduler/src/pool/VirtualMachinePoolXML.cc +++ b/src/scheduler/src/pool/VirtualMachinePoolXML.cc @@ -238,15 +238,14 @@ int VirtualMachinePoolXML::load_info(xmlrpc_c::value &result) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int VirtualMachinePoolXML::dispatch(int vid, int hid, int dsid, bool resched,const string& extra_template) const +int VirtualMachinePoolXML::dispatch(int vid, int hid, int dsid, bool resched, + const string& extra_template) const { xmlrpc_c::value deploy_result; - VirtualMachineXML* vm = get(vid); - - if (vm != 0 && vm->clear_log()) + if (auto vm = get(vid)) { - update(vm); + vm->clear_log(); } try diff --git a/src/scheduler/src/pool/VirtualMachineXML.cc b/src/scheduler/src/pool/VirtualMachineXML.cc index a0c5195c4f..01931d3dbd 100644 --- a/src/scheduler/src/pool/VirtualMachineXML.cc +++ b/src/scheduler/src/pool/VirtualMachineXML.cc @@ -607,7 +607,14 @@ void VirtualMachineXML::log(const string &st) oss << one_util::log_time() << ": " << st; - user_template->replace("SCHED_MESSAGE", oss.str()); + string sched_message = oss.str(); + + user_template->replace("SCHED_MESSAGE", sched_message); + + oss.str(""); + oss << "SCHED_MESSAGE = \"" << sched_message << "\""; + + update(oss.str(), true); // Send the update to oned } /* -------------------------------------------------------------------------- */ @@ -631,6 +638,8 @@ bool VirtualMachineXML::clear_log() user_template->erase("SCHED_MESSAGE"); + update("SCHED_MESSAGE = \"\"", true); // Send update to oned + return true; } @@ -672,3 +681,26 @@ int VirtualMachineXML::parse_action_name(string& action_st) return 0; }; +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +bool VirtualMachineXML::update(const string &vm_template, bool append) +{ + xmlrpc_c::value result; + + try + { + Client::client()->call("one.vm.update", "isi", &result, oid, + vm_template.c_str(), + append ? 1 : 0); + } + catch (exception const& e) + { + return false; + } + + vector values = + xmlrpc_c::value_array(result).vectorValueValue(); + + return xmlrpc_c::value_boolean(values[0]); +} diff --git a/src/scheduler/src/sched/Scheduler.cc b/src/scheduler/src/sched/Scheduler.cc index d1e97534dd..0b813b2d4b 100644 --- a/src/scheduler/src/sched/Scheduler.cc +++ b/src/scheduler/src/sched/Scheduler.cc @@ -895,7 +895,6 @@ void Scheduler::match_schedule() log_match(vm->get_oid(), "Cannot schedule VM. "+ m_error); vm->log("Cannot schedule VM. "+ m_error); - vmpool->update(vm); continue; } @@ -973,8 +972,6 @@ void Scheduler::match_schedule() } } - vmpool->update(vm); - log_match(vm->get_oid(), "Cannot schedule VM, there is no suitable host."); @@ -1090,8 +1087,6 @@ void Scheduler::match_schedule() vm->clear_match_hosts(); - vmpool->update(vm); - log_match(vm->get_oid(), "Cannot schedule VM, there is no suitable " "system ds."); @@ -1194,8 +1189,6 @@ void Scheduler::match_schedule() vm->clear_match_hosts(); vm->clear_match_datastores(); - vmpool->update(vm); - log_match(vm->get_oid(), "Cannot schedule VM, there is no " "suitable network."); @@ -1339,8 +1332,6 @@ void Scheduler::dispatch() { vm->log("Cannot dispatch VM. " + error); - vmpool->update(vm); - continue; } } @@ -1664,7 +1655,6 @@ void Scheduler::dispatch() vm->log("Cannot dispatch VM to any Host. Possible reasons: Not " "enough capacity in Host or System DS, dispatch limit " "reached, or limit of free leases reached."); - vmpool->update(vm); } }