Skip to content

Commit

Permalink
B #6355: Fix VM quotas (#2861)
Browse files Browse the repository at this point in the history
* Remove useless default values from VM quotas, adapt chown and chgrp to it
* Fix VM transition from poweroff, stopped, suspended, undeployed to done when quotas are tmp exceeded
* Fix VM transition from poweroff to undeployed
* Fix VM transition from suspended to stopped

(cherry picked from commit 1e25904)
  • Loading branch information
paczerny authored and rsmontero committed Dec 12, 2023
1 parent c5f9292 commit 725337e
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 72 deletions.
11 changes: 10 additions & 1 deletion include/Quota.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ class QuotaInterface
virtual bool check(Template* tmpl, Quotas& default_quotas, std::string& error) = 0;

/**
* Decrement usage counters when deallocating image
* Decrement usage counters
* @param tmpl template for the resource
*/
virtual void del(Template* tmpl) = 0;

/**
* Set the quotas. If the quota previously exists its limit is updated.
* @param quota_str the quota template in ASCII or XML formats
Expand Down Expand Up @@ -251,6 +252,14 @@ class Quota: public Template, public QuotaInterface
Quotas& default_quotas,
std::string& error);

/**
* Add usage for a given quota without checking the limits
* @param qid id that identifies the quota, to be used by get_quota
* @param usage_req usage for each metric
*/
void add_quota(const std::string& qid,
std::map<std::string, float>& usage_req);

/**
* Reduce usage from a given quota based on the current consumption
* @param qid id that identifies the quota, to be used by get_quota
Expand Down
6 changes: 6 additions & 0 deletions include/QuotaVirtualMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class QuotaVirtualMachine : public Quota
*/
bool update(Template * tmpl, Quotas& default_quotas, std::string& error) override;

/**
* Add usage counters. Use carefully this method does not care about exceeding the quota
* @param tmpl Template with the quota usage
*/
void add(Template* tmpl);

/**
* Decrement usage counters when deallocating image
* @param tmpl template for the resource
Expand Down
22 changes: 20 additions & 2 deletions include/Quotas.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Quotas

/**
* Delete usage from quota counters.
* @param tmpl template for the image, with usage
* @param tmpl Template with the quota usage
*/
void ds_del(Template * tmpl)
{
Expand All @@ -70,6 +70,15 @@ class Quotas
return datastore_quota.get_quota(id, va);
}

/**
* Add VM quota usage, without checking limits
* @param tmpl Template with the quota usage
*/
void vm_add(Template * tmpl)
{
vm_quota.add(tmpl);
}

/**
* Gets a VM quota identified by its ID.
*
Expand Down Expand Up @@ -157,6 +166,15 @@ class Quotas
*/
int from_xml(ObjectXML * object_xml);

/**
* Add VM quota usage, without checking the limits
* @param uid of the user
* @param gid of the group
* @param tmpl template with quota usage
* @note Use carefully as this method may exceed quota limits
*/
static void vm_add(int uid, int gid, Template * tmpl);

/**
* Delete VM related usage (network, image and compute) from quota counters.
* for the given user and group
Expand Down Expand Up @@ -244,7 +262,7 @@ class Quotas
* @param type the quota to work with
* @param uid of the user
* @param gid of the group
* @param tmpl template for the image, with usage
* @param tmpl template with quota usage
*/
static void quota_del(QuotaType type, int uid, int gid, Template * tmpl);

Expand Down
54 changes: 45 additions & 9 deletions src/lcm/LifeCycleActions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ void LifeCycleManager::trigger_stop(int vid, const RequestAttributes& ra)
//----------------------------------------------------
// Bypass SAVE_STOP
//----------------------------------------------------
VirtualMachineTemplate quota_tmpl;

std::string memory, cpu;

int quota_uid = vm->get_uid();
int quota_gid = vm->get_gid();

vm->get_template_attribute("MEMORY", memory);
vm->get_template_attribute("CPU", cpu);

quota_tmpl.add("RUNNING_MEMORY", memory);
quota_tmpl.add("RUNNING_CPU", cpu);
quota_tmpl.add("RUNNING_VMS", 1);

vm->set_state(VirtualMachine::ACTIVE);
vm->set_state(VirtualMachine::EPILOG_STOP);

Expand All @@ -206,6 +220,10 @@ void LifeCycleManager::trigger_stop(int vid, const RequestAttributes& ra)
//----------------------------------------------------

tm->trigger_epilog_stop(vm.get());

// Add running quota, it will be removed in DM::stop_success
vm.reset();
Quotas::vm_add(quota_uid, quota_gid, &quota_tmpl);
}
else
{
Expand Down Expand Up @@ -428,7 +446,6 @@ void LifeCycleManager::trigger_shutdown(int vid, bool hard,
trigger([this, hard, vid, uid, gid, req_id] {
auto vm = vmpool->get(vid);
VirtualMachineTemplate quota_tmpl;
string error;

if ( vm == nullptr )
{
Expand All @@ -451,10 +468,6 @@ void LifeCycleManager::trigger_shutdown(int vid, bool hard,
quota_tmpl.add("RUNNING_MEMORY", memory);
quota_tmpl.add("RUNNING_CPU", cpu);
quota_tmpl.add("RUNNING_VMS", 1);

quota_tmpl.add("MEMORY", 0);
quota_tmpl.add("CPU", 0);
quota_tmpl.add("VMS", 0);
}

auto lcm_state = vm->get_lcm_state();
Expand Down Expand Up @@ -505,8 +518,6 @@ void LifeCycleManager::trigger_shutdown(int vid, bool hard,
vm->set_state(VirtualMachine::ACTIVE);
vm->set_state(VirtualMachine::EPILOG);

Quotas::vm_check(uid, gid, &quota_tmpl, error);

vm->set_action(VMActions::TERMINATE_ACTION, uid, gid, req_id);

vm->set_epilog_stime(time(0));
Expand All @@ -518,15 +529,17 @@ void LifeCycleManager::trigger_shutdown(int vid, bool hard,
//----------------------------------------------------

tm->trigger_epilog(false, vm.get());

// Add running quota, it will be removed in DM::done
vm.reset();
Quotas::vm_add(uid, gid, &quota_tmpl);
}
else if (vm->get_state() == VirtualMachine::STOPPED ||
vm->get_state() == VirtualMachine::UNDEPLOYED)
{
vm->set_state(VirtualMachine::ACTIVE);
vm->set_state(VirtualMachine::EPILOG);

Quotas::vm_check(uid, gid, &quota_tmpl, error);

vm->set_action(VMActions::TERMINATE_ACTION, uid, gid, req_id);

vm->set_epilog_stime(time(0));
Expand All @@ -538,6 +551,10 @@ void LifeCycleManager::trigger_shutdown(int vid, bool hard,
//----------------------------------------------------

tm->trigger_epilog(true, vm.get());

// Add running quota, it will be removed in DM::done
vm.reset();
Quotas::vm_add(uid, gid, &quota_tmpl);
}
else
{
Expand Down Expand Up @@ -612,6 +629,20 @@ void LifeCycleManager::trigger_undeploy(int vid, bool hard,
// Bypass SHUTDOWN_UNDEPLOY
//----------------------------------------------------

VirtualMachineTemplate quota_tmpl;

std::string memory, cpu;

int quota_uid = vm->get_uid();
int quota_gid = vm->get_gid();

vm->get_template_attribute("MEMORY", memory);
vm->get_template_attribute("CPU", cpu);

quota_tmpl.add("RUNNING_MEMORY", memory);
quota_tmpl.add("RUNNING_CPU", cpu);
quota_tmpl.add("RUNNING_VMS", 1);

vm->set_state(VirtualMachine::ACTIVE);
vm->set_state(VirtualMachine::EPILOG_UNDEPLOY);

Expand All @@ -626,6 +657,11 @@ void LifeCycleManager::trigger_undeploy(int vid, bool hard,
//----------------------------------------------------

tm->trigger_epilog_stop(vm.get());

vm.reset();

// Add running quota, it will be removed in DM:undeploy_success
Quotas::vm_add(quota_uid, quota_gid, &quota_tmpl);
}
else
{
Expand Down
1 change: 0 additions & 1 deletion src/lcm/LifeCycleStates.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1601,7 +1601,6 @@ void LifeCycleManager::trigger_snapshot_delete_success(int vid)
Template quota_tmpl;

quota_tmpl.set(snap);
quota_tmpl.replace("VMS", 0);

Quotas::quota_del(Quotas::VM, vm_uid, vm_gid, &quota_tmpl);
}
Expand Down
2 changes: 2 additions & 0 deletions src/rm/RequestManagerChown.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ unique_ptr<PoolObjectSQL> RequestManagerChown::get_and_quota(
tmpl->add("RUNNING_VMS", 1);
}

tmpl->add("VMS", 1);

VirtualMachineDisks::image_ds_quotas(tmpl.get(), ds_quotas);

quota_map.insert(make_pair(Quotas::VIRTUALMACHINE, move(tmpl)));
Expand Down
7 changes: 0 additions & 7 deletions src/rm/RequestManagerVirtualMachine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,6 @@ Request::ErrorCode VirtualMachineAction::request_execute(RequestAttributes& att,
quota_tmpl.add("RUNNING_CPU", cpu);
quota_tmpl.add("RUNNING_VMS", 1);

quota_tmpl.add("VMS", 0);
quota_tmpl.add("MEMORY", 0);
quota_tmpl.add("CPU", 0);

att_aux.uid = vm->get_uid();
att_aux.gid = vm->get_gid();

Expand Down Expand Up @@ -2170,7 +2166,6 @@ void VirtualMachineResize::request_execute(xmlrpc_c::paramList const& paramList,

deltas.add("MEMORY", dmemory);
deltas.add("CPU", dcpu);
deltas.add("VMS", 0);

if (update_running_quota)
{
Expand Down Expand Up @@ -2347,8 +2342,6 @@ Request::ErrorCode VirtualMachineSnapshotCreate::request_execute(RequestAttribut
Template quota_tmpl;

quota_tmpl.set(snap);
quota_tmpl.add("MEMORY", 0);
quota_tmpl.add("CPU", 0);
quota_tmpl.add("VMS", 0);

RequestAttributes att_quota(vm_perms.uid, vm_perms.gid, att);
Expand Down
34 changes: 34 additions & 0 deletions src/um/Quota.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,40 @@ bool Quota::check_quota(const string& qid,
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

void Quota::add_quota(const string& qid, map<string, float>& usage_req)
{
VectorAttribute * q;

if ( get_quota(qid, &q) == -1)
{
return;
}

if ( q == 0 )
{
return;
}

for (int i=0; i < num_metrics; i++)
{
string metrics_used = metrics[i];

metrics_used += "_USED";

auto it = usage_req.find(metrics[i]);

if (it == usage_req.end())
{
continue;
}

add_to_quota(q, metrics_used, it->second);
}
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

void Quota::del_quota(const string& qid, map<string, float>& usage_req)
{
VectorAttribute * q;
Expand Down
Loading

0 comments on commit 725337e

Please sign in to comment.