1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-02 09:47:00 +03:00

Bug #2200: VM Resize uses the same quota mechanism as all the other methods

(cherry picked from commit 886346606d7ff1d4738987babc907c8ef0e59276)
This commit is contained in:
Carlos Martín 2013-07-22 15:43:35 +02:00
parent d533591ef3
commit 7658e9cd94
6 changed files with 31 additions and 161 deletions

View File

@ -51,20 +51,6 @@ public:
*/
virtual bool check(Template* tmpl, Quotas& default_quotas, string& error) = 0;
/**
* Check if a resource update in usage counters will exceed the
* quota limits. If not the usage counters are updated for that resource
* @param tmpl with increments in MEMORY and CPU
* @param default_quotas Quotas that contain the default limits
* @param error string
* @return true if the operation can be performed
*/
virtual bool update(Template * tmpl, Quotas& default_quotas, string& error)
{
error = "Update operation for quotas not supported.";
return false;
};
/**
* Decrement usage counters when deallocating image
* @param tmpl template for the resource

View File

@ -57,16 +57,6 @@ public:
*/
bool check(Template* tmpl, Quotas& default_quotas, string& error);
/**
* Check if the resource update (change in MEMORY or CPU) will exceed the
* quota limits. If not the usage counters are updated
* @param tmpl with increments in MEMORY and CPU
* @param default_quotas Quotas that contain the default limits
* @param error string
* @return true if the operation can be performed
*/
bool update(Template * tmpl, Quotas& default_quotas, string& error);
/**
* Decrement usage counters when deallocating image
* @param tmpl template for the resource

View File

@ -148,20 +148,6 @@ public:
Quotas& default_quotas,
string& error_str);
/**
* Update usage of an existing quota (e.g. size of an image), it updates
* the usage counters if quotas are not exceeded.
* @param type the quota to work with
* @param tmpl template for the VirtualMachine
* @param default_quotas Quotas that contain the default limits
* @param error_str string describing the error
* @return true if resource can be updated, false otherwise
*/
bool quota_update(QuotaType type,
Template *tmpl,
Quotas& default_quotas,
string& error_str);
/**
* Delete usage from the given quota counters.
* @param type the quota to work with

View File

@ -1187,9 +1187,6 @@ void VirtualMachineResize::request_execute(xmlrpc_c::paramList const& paramList,
int nvcpu, ovcpu;
Nebula& nd = Nebula::instance();
UserPool* upool = nd.get_upool();
GroupPool* gpool = nd.get_gpool();
Quotas dquotas = nd.get_default_user_quota();
HostPool * hpool = nd.get_hpool();
Host * host;
@ -1296,6 +1293,10 @@ void VirtualMachineResize::request_execute(xmlrpc_c::paramList const& paramList,
deltas.add("MEMORY", dmemory);
deltas.add("CPU", dcpu);
// This attribute tells the quotas that we are not allocating a new
// VM, just updating the CPU & MEMORY usage
deltas.add("UPDATE", "YES");
switch (vm->get_state())
{
case VirtualMachine::POWEROFF: //Only check host capacity in POWEROFF
@ -1340,69 +1341,9 @@ void VirtualMachineResize::request_execute(xmlrpc_c::paramList const& paramList,
/* Check quotas */
/* ---------------------------------------------------------------------- */
if (vm_perms.uid != UserPool::ONEADMIN_ID)
if ( quota_authorization(&deltas, Quotas::VIRTUALMACHINE, att) == false )
{
User * user = upool->get(vm_perms.uid, true);
if ( user != 0 )
{
rc = user->quota.quota_update(Quotas::VM, &deltas, dquotas, error_str);
if (rc == false)
{
ostringstream oss;
oss << object_name(PoolObjectSQL::USER)
<< " [" << vm_perms.uid << "] "
<< error_str;
failure_response(AUTHORIZATION,
request_error(oss.str(), ""),
att);
user->unlock();
return;
}
upool->update(user);
user->unlock();
}
}
if (vm_perms.gid != GroupPool::ONEADMIN_ID)
{
Group * group = gpool->get(vm_perms.gid, true);
if ( group != 0 )
{
rc = group->quota.quota_update(Quotas::VM, &deltas, dquotas, error_str);
if (rc == false)
{
ostringstream oss;
RequestAttributes att_tmp(vm_perms.uid, -1, att);
oss << object_name(PoolObjectSQL::GROUP)
<< " [" << vm_perms.gid << "] "
<< error_str;
failure_response(AUTHORIZATION,
request_error(oss.str(), ""),
att);
group->unlock();
quota_rollback(&deltas, Quotas::VM, att_tmp);
return;
}
gpool->update(group);
group->unlock();
}
return;
}
/* ---------------------------------------------------------------------- */

View File

@ -55,22 +55,32 @@ bool QuotaVirtualMachine::check(Template * tmpl,
{
map<string, float> vm_request;
int memory;
float cpu;
int memory;
float cpu;
bool update;
if ( tmpl->get("MEMORY", memory) == false || memory <= 0 )
// For an update (change in MEMORY or CPU), negative values are allowed,
// and VMS is not updated since a new VM is not being allocated.
tmpl->get("UPDATE", update);
if ( tmpl->get("MEMORY", memory) == false || (!update && memory <= 0) )
{
error = "MEMORY attribute must be a positive integer value";
return false;
}
if ( tmpl->get("CPU", cpu) == false || cpu <= 0 )
if ( tmpl->get("CPU", cpu) == false || (!update && cpu <= 0) )
{
error = "CPU attribute must be a positive float or integer value";
return false;
}
vm_request.insert(make_pair("VMS",1));
if ( !update )
{
vm_request.insert(make_pair("VMS",1));
}
vm_request.insert(make_pair("MEMORY", memory));
vm_request.insert(make_pair("CPU", cpu));
@ -84,8 +94,11 @@ void QuotaVirtualMachine::del(Template * tmpl)
{
map<string, float> vm_request;
int memory;
float cpu;
int memory;
float cpu;
bool update;
tmpl->get("UPDATE", update);
if ( tmpl->get("MEMORY", memory) == false )
{
@ -97,7 +110,11 @@ void QuotaVirtualMachine::del(Template * tmpl)
cpu = 0;
}
vm_request.insert(make_pair("VMS",1));
if (!update)
{
vm_request.insert(make_pair("VMS",1));
}
vm_request.insert(make_pair("MEMORY", memory));
vm_request.insert(make_pair("CPU", cpu));
@ -114,28 +131,3 @@ int QuotaVirtualMachine::get_default_quota(
{
return default_quotas.vm_get(id, va);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
bool QuotaVirtualMachine::update(Template * tmpl,
Quotas& default_quotas,
string& error)
{
map<string, float> vm_request;
int delta_memory;
float delta_cpu;
if ( tmpl->get("MEMORY", delta_memory) == true )
{
vm_request.insert(make_pair("MEMORY", delta_memory));
}
if ( tmpl->get("CPU", delta_cpu) == true )
{
vm_request.insert(make_pair("CPU", delta_cpu));
}
return check_quota("", vm_request, default_quotas, error);
}

View File

@ -219,31 +219,6 @@ bool Quotas::quota_check(QuotaType type,
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
bool Quotas::quota_update(QuotaType type,
Template *tmpl,
Quotas &default_quotas,
string &error_str)
{
switch (type)
{
// This is an internal check, should never get in here.
case DATASTORE:
case NETWORK:
case IMAGE:
case VIRTUALMACHINE:
error_str = "Cannot update quota. Not implemented";
return false;
case VM:
return vm_quota.update(tmpl, default_quotas, error_str);
}
return false;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void Quotas::quota_del(QuotaType type, int uid, int gid, Template * tmpl)
{
Nebula& nd = Nebula::instance();