2012-05-27 23:15:05 +02:00
/* -------------------------------------------------------------------------- */
2024-07-29 14:25:20 +02:00
/* Copyright 2002-2024, OpenNebula Project, OpenNebula Systems */
2012-05-27 23:15:05 +02:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef QUOTA_H_
# define QUOTA_H_
# include "Template.h"
2012-11-19 16:38:39 +01:00
// Forward declaration to avoid include cycle
class Quotas ;
2012-05-27 23:15:05 +02:00
/**
2016-02-03 10:28:54 +01:00
* This class defines the public interface ( pure abstract ) for Quota .
2012-05-27 23:15:05 +02:00
*/
2016-02-03 10:28:54 +01:00
class QuotaInterface
2012-05-27 23:15:05 +02:00
{
public :
2016-02-03 10:28:54 +01:00
/**
* Check if the resource allocation will exceed the quota limits . If not
* the usage counters are updated
* @ param tmpl template for the resource
* @ param default_quotas Quotas that contain the default limits
* @ param error string
* @ return true if the operation can be performed
*/
2020-07-02 22:42:10 +02:00
virtual bool check ( Template * tmpl , Quotas & default_quotas , std : : string & error ) = 0 ;
2012-11-30 00:16:52 +01:00
2016-02-03 10:28:54 +01:00
/**
2023-12-12 16:19:20 +01:00
* Decrement usage counters
2016-02-03 10:28:54 +01:00
* @ param tmpl template for the resource
*/
virtual void del ( Template * tmpl ) = 0 ;
2023-12-12 16:19:20 +01:00
2012-05-27 23:15:05 +02:00
/**
* Set the quotas . If the quota previously exists its limit is updated .
* @ param quota_str the quota template in ASCII or XML formats
* @ param error describe the error in case of error
2012-11-26 12:48:10 +01:00
*
2012-05-27 23:15:05 +02:00
* @ return 0 on success - 1 otherwise
*/
2020-07-02 22:42:10 +02:00
virtual int set ( std : : vector < VectorAttribute * > * quotas , std : : string & error ) = 0 ;
2012-05-27 23:15:05 +02:00
2012-05-29 00:36:13 +02:00
/**
2016-02-03 10:28:54 +01:00
* 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
2012-11-19 16:38:39 +01:00
* @ param default_quotas Quotas that contain the default limits
2012-11-30 00:16:52 +01:00
* @ param error string
2012-05-29 00:36:13 +02:00
* @ return true if the operation can be performed
*/
2020-07-02 22:42:10 +02:00
virtual bool update ( Template * tmpl , Quotas & default_quotas , std : : string & error ) = 0 ;
2016-02-03 10:28:54 +01:00
/**
* Returns the name that identifies the quota in a template
*/
2024-06-03 11:40:24 +02:00
virtual const char * get_quota_name ( ) const = 0 ;
2016-02-03 10:28:54 +01:00
2024-06-03 11:40:24 +02:00
/**
* Gets a quota identified by its ID .
* @ param id of the quota
* @ param va The quota , if it is found
* @ return 0 on success , - 1 if not found
*/
virtual int get_quota ( const std : : string & id , VectorAttribute * * va ) = 0 ;
2016-02-03 10:28:54 +01:00
protected :
2024-06-03 11:40:24 +02:00
QuotaInterface ( ) { } ;
2016-02-03 10:28:54 +01:00
2024-06-03 11:40:24 +02:00
virtual ~ QuotaInterface ( ) { } ;
2016-02-03 10:28:54 +01:00
} ;
/**
* The QuotaDecorator class decorates the quota interface with specific
* behavior over the same quota object and counters . Decorators must be derived
* from this class and pass in its constructor the decorated object .
*/
class QuotaDecorator : public QuotaInterface
{
2023-02-07 08:50:30 +01:00
bool check ( Template * tmpl , Quotas & default_quotas , std : : string & error ) override
2016-02-03 10:28:54 +01:00
{
return quota - > check ( tmpl , default_quotas , error ) ;
}
2023-02-07 08:50:30 +01:00
void del ( Template * tmpl ) override
2016-02-03 10:28:54 +01:00
{
return quota - > del ( tmpl ) ;
}
2023-02-07 08:50:30 +01:00
int set ( std : : vector < VectorAttribute * > * quotas , std : : string & error ) override
2016-02-03 10:28:54 +01:00
{
return quota - > set ( quotas , error ) ;
}
2023-02-07 08:50:30 +01:00
bool update ( Template * tmpl , Quotas & default_quotas , std : : string & error ) override
2016-02-03 10:28:54 +01:00
{
return quota - > update ( tmpl , default_quotas , error ) ;
}
2023-02-07 08:50:30 +01:00
const char * get_quota_name ( ) const override
2016-02-03 10:28:54 +01:00
{
return quota - > get_quota_name ( ) ;
}
2023-02-07 08:50:30 +01:00
int get_quota ( const std : : string & id , VectorAttribute * * va ) override
2016-02-03 10:28:54 +01:00
{
return quota - > get_quota ( id , va ) ;
}
protected :
2024-06-03 11:40:24 +02:00
QuotaDecorator ( QuotaInterface * _quota ) : quota ( _quota ) { } ;
2016-02-03 10:28:54 +01:00
2024-06-03 11:40:24 +02:00
virtual ~ QuotaDecorator ( ) { } ;
2016-02-03 10:28:54 +01:00
QuotaInterface * quota ;
} ;
/**
* Base class for resource quotas , it provides basic storage and management of
* the quotas . Each resource MUST inherit from it to implement check and
* update methods . Quotas are stored in a template form , each class store the
* limits and usage in a resource specific format .
*/
class Quota : public Template , public QuotaInterface
{
public :
/**
* Set the quotas . If the quota previously exists its limit is updated .
* @ param quota_str the quota template in ASCII or XML formats
* @ param error describe the error in case of error
*
* @ return 0 on success - 1 otherwise
*/
2023-02-07 08:50:30 +01:00
int set ( std : : vector < VectorAttribute * > * quotas , std : : string & error ) override ;
2012-05-29 00:36:13 +02:00
2013-07-22 22:08:01 +02:00
/**
* 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
*/
2023-02-07 08:50:30 +01:00
bool update ( Template * tmpl , Quotas & default_quotas , std : : string & error ) override
2013-07-22 22:08:01 +02:00
{
error = " Update operation for quotas not supported. " ;
return false ;
} ;
2012-06-07 00:04:08 +02:00
/**
* Returns the name that identifies the quota in a template
*/
2024-06-03 11:40:24 +02:00
const char * get_quota_name ( ) const override
{
2012-06-07 00:04:08 +02:00
return template_name ;
2024-06-03 11:40:24 +02:00
}
/**
* Gets a quota identified by its ID .
* @ param id of the quota
* @ param va The quota , if it is found
* @ return 0 on success , - 1 if not found
*/
int get_quota ( const std : : string & id , VectorAttribute * * va ) override
{
std : : map < std : : string , Attribute * > : : iterator it ;
return get_quota ( id , va , it ) ;
}
/**
* Value for limit default
*/
static const int DEFAULT ;
static const std : : string DEFAULT_STR ;
/**
* Value for " unlimited " limit
*/
static const int UNLIMITED ;
2014-07-03 12:16:29 +02:00
2012-05-27 23:15:05 +02:00
protected :
2012-06-05 13:34:28 +02:00
Quota ( const char * quota_name ,
const char * _template_name ,
2024-01-08 13:56:40 +01:00
const std : : vector < std : : string > & _metrics ,
2012-11-30 00:16:52 +01:00
bool _is_default )
2012-06-05 13:34:28 +02:00
: Template ( false , ' = ' , quota_name ) ,
template_name ( _template_name ) ,
metrics ( _metrics ) ,
2024-06-03 11:40:24 +02:00
is_default ( _is_default ) { } ;
2012-11-30 00:16:52 +01:00
2024-06-03 11:40:24 +02:00
virtual ~ Quota ( ) { } ;
2012-05-27 23:15:05 +02:00
2012-06-05 01:58:37 +02:00
/**
2012-11-30 00:16:52 +01:00
* Generic Quota Names
2012-06-05 01:58:37 +02:00
*
* template_name = [
* ID = " ID to identify the resource " ,
* metrics [ 0 ] = " Limit for the first metric "
* metrics [ 0 ] _USED = " Usage for metric "
* ]
*
* ID & counter fields are optional
*/
/**
* Name of the quota used in the templates
*/
2012-06-05 13:34:28 +02:00
const char * template_name ;
2012-06-05 01:58:37 +02:00
/**
* The name of the quota metrics
*/
2024-01-08 13:56:40 +01:00
const std : : vector < std : : string > & metrics ;
2012-06-05 01:58:37 +02:00
2012-11-30 00:16:52 +01:00
/**
* Whether or not this is a default quota . Default quotas do not have usage ,
* and can ' t have a limit of - 1
*/
bool is_default ;
/**
* Check a given quota for an usage request and update counters if the
2012-06-05 01:58:37 +02:00
* request does not exceed quota limits
* @ param qid id that identifies the quota , to be used by get_quota
* @ param usage_req usage for each metric
2012-11-19 16:38:39 +01:00
* @ param default_quotas Quotas that contain the default limits
* @ param error string describing the error
2012-06-05 01:58:37 +02:00
* @ return true if the request does not exceed current limits
*/
2020-07-02 22:42:10 +02:00
bool check_quota ( const std : : string & qid ,
std : : map < std : : string , float > & usage_req ,
2012-11-19 16:38:39 +01:00
Quotas & default_quotas ,
2020-07-02 22:42:10 +02:00
std : : string & error ) ;
2012-06-05 01:58:37 +02:00
2023-12-12 16:19:20 +01:00
/**
* 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 ) ;
2012-06-05 01:58:37 +02:00
/**
* Reduce usage from a given quota based on the current consumption
* @ param qid id that identifies the quota , to be used by get_quota
* @ param usage_req usage for each metric
2012-05-27 23:15:05 +02:00
*/
2020-07-02 22:42:10 +02:00
void del_quota ( const std : : string & qid ,
std : : map < std : : string , float > & usage_req ) ;
2012-05-27 23:15:05 +02:00
2012-06-05 13:34:28 +02:00
/**
2012-11-19 15:19:09 +01:00
* Gets the default quota identified by its ID .
*
2012-06-05 13:34:28 +02:00
* @ param id of the quota
2012-11-19 16:38:39 +01:00
* @ param default_quotas Quotas that contain the default limits
2012-06-19 17:41:42 +02:00
* @ param va The quota , if it is found
2012-11-19 15:19:09 +01:00
*
2012-06-19 17:41:42 +02:00
* @ return 0 on success , - 1 if not found
2012-06-05 13:34:28 +02:00
*/
2020-07-02 22:42:10 +02:00
virtual int get_default_quota ( const std : : string & id ,
2024-06-03 11:40:24 +02:00
Quotas & default_quotas ,
VectorAttribute * * va ) = 0 ;
2012-06-19 17:41:42 +02:00
/**
* Gets a quota identified by its ID .
*
* @ param id of the quota
* @ param va The quota , if it is found
* @ param it The quota iterator , if it is found
*
* @ return 0 on success , - 1 if not found
*/
virtual int get_quota (
2020-07-02 22:42:10 +02:00
const std : : string & id ,
2012-06-19 17:41:42 +02:00
VectorAttribute * * va ,
2020-07-02 22:42:10 +02:00
std : : map < std : : string , Attribute * > : : iterator & it ) ;
2012-06-19 17:41:42 +02:00
2012-06-21 18:12:50 +02:00
/**
* Checks if a quota has 0 limit and usage , and deletes it
*
* @ param qid id of the quota
*/
2020-07-02 22:42:10 +02:00
void cleanup_quota ( const std : : string & qid ) ;
2012-06-21 18:12:50 +02:00
2012-06-05 13:34:28 +02:00
private :
2012-05-27 23:15:05 +02:00
/**
* Creates an empty quota based on the given attribute . The attribute va
* contains the limits for the quota .
* @ param va limits for the new quota if 0 limits will be 0
2012-11-26 12:48:10 +01:00
*
2012-11-29 12:49:29 +01:00
* @ return a new attribute representing the quota , 0 on error
2012-05-27 23:15:05 +02:00
*/
2023-03-08 15:52:20 +01:00
VectorAttribute * new_quota ( const VectorAttribute * va ) ;
2012-06-02 02:58:46 +02:00
2012-05-27 23:15:05 +02:00
/**
2012-06-04 00:45:56 +02:00
* Adds a new quota , it also updates an internal index for fast accessing
* the quotas
* @ param quota is the new quota , allocated in the HEAP
2012-05-27 23:15:05 +02:00
*/
2012-06-05 13:34:28 +02:00
void add ( VectorAttribute * nq )
{
2024-06-03 11:40:24 +02:00
attributes . insert ( make_pair ( nq - > name ( ) , nq ) ) ;
2012-06-05 13:34:28 +02:00
}
2012-05-27 23:15:05 +02:00
/**
* Adds a given value to the current quota ( vector )
* @ param attr the quota ;
* @ param va_name name of the quota in the vector attribute
* @ param num value to add to the current quota ;
*/
2020-07-02 22:42:10 +02:00
void add_to_quota ( VectorAttribute * attr ,
const std : : string & va_name ,
float num ) ;
2012-06-05 01:58:37 +02:00
2012-11-30 00:16:52 +01:00
/**
2012-06-05 01:58:37 +02:00
* Sets new limit values for the quota
* @ param quota to be updated
* @ param va attribute with the new limits
2012-11-26 12:48:10 +01:00
*
2012-06-05 01:58:37 +02:00
* @ return 0 on success or - 1 if wrong limits
*/
2012-11-26 12:48:10 +01:00
int update_limits ( VectorAttribute * quota ,
2024-06-03 11:40:24 +02:00
const VectorAttribute * va ) ;
2012-06-05 01:58:37 +02:00
/**
2012-06-05 13:34:28 +02:00
* Extract the limits for the defined quota metrics from a given attribute
2012-06-05 01:58:37 +02:00
* @ param va the attribute with the limits
2012-06-05 13:34:28 +02:00
* @ param limits stores the known limits
* @ return 0 on success
2012-06-05 01:58:37 +02:00
*/
2020-07-02 22:42:10 +02:00
int get_limits ( const VectorAttribute * va ,
2020-07-05 22:01:32 +02:00
std : : map < std : : string , std : : string > & limits ) const ;
2012-05-27 23:15:05 +02:00
} ;
# endif /*QUOTA_H_*/