2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2022-04-07 20:49:58 +03:00
/* Copyright 2002-2022, OpenNebula Project, OpenNebula Systems */
2008-06-17 20:27:32 +04: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 HOST_POOL_H_
# define HOST_POOL_H_
# include "PoolSQL.h"
# include "Host.h"
2020-03-04 18:05:57 +03:00
# include "HostMonitoringTemplate.h"
# include "OneDB.h"
2008-06-17 20:27:32 +04:00
# include <sstream>
# include <vector>
/**
2010-04-04 03:12:52 +04:00
* The Host Pool class .
2008-06-17 20:27:32 +04:00
*/
class HostPool : public PoolSQL
{
public :
2020-03-04 18:05:57 +03:00
HostPool ( SqlDB * db , std : : vector < const SingleAttribute * > & secrets ) ;
2008-06-17 20:27:32 +04:00
2020-03-04 18:05:57 +03:00
~ HostPool ( ) = default ;
2008-06-17 20:27:32 +04:00
/**
* Function to allocate a new Host object
* @ param oid the id assigned to the Host
2010-04-28 20:25:43 +04:00
* @ return the oid assigned to the object or - 1 in case of failure
2008-06-17 20:27:32 +04:00
*/
int allocate (
2020-07-02 23:42:10 +03:00
int * oid ,
const std : : string & hostname ,
const std : : string & im_mad_name ,
const std : : string & vmm_mad_name ,
int cluster_id ,
const std : : string & cluster_name ,
std : : string & error_str ) ;
2008-06-17 20:27:32 +04:00
2019-09-09 15:43:51 +03:00
/**
2019-12-10 13:45:15 +03:00
* Updates a Host in the data base . It also updates the previous state
2019-09-09 15:43:51 +03:00
* after executing the hooks .
* @ param objsql a pointer to the Host
*
* @ return 0 on success .
*/
2022-04-09 15:48:15 +03:00
int update ( PoolObjectSQL * objsql ) override ;
2019-09-09 15:43:51 +03:00
2008-06-17 20:27:32 +04:00
/**
2020-09-10 10:08:29 +03:00
* Gets an object from the pool ( if needed the object is loaded from the
* database ) . The object is locked , other threads can ' t access the same
* object . The lock is released by destructor .
* @ param oid the Host unique identifier
* @ return a pointer to the Host , nullptr in case of failure
2008-06-17 20:27:32 +04:00
*/
2020-09-10 10:08:29 +03:00
std : : unique_ptr < Host > get ( int oid )
2008-06-17 20:27:32 +04:00
{
2020-09-10 10:08:29 +03:00
return PoolSQL : : get < Host > ( oid ) ;
}
2010-04-04 03:12:52 +04:00
2018-10-09 12:05:08 +03:00
/**
2020-09-10 10:08:29 +03:00
* Gets a read only object from the pool ( if needed the object is loaded from the
* database ) . No object lock , other threads may work with the same object .
* @ param oid the Host unique identifier
* @ return a pointer to the Host , nullptr in case of failure
2018-10-09 12:05:08 +03:00
*/
2020-09-10 10:08:29 +03:00
std : : unique_ptr < Host > get_ro ( int oid )
2018-10-09 12:05:08 +03:00
{
2020-09-10 10:08:29 +03:00
return PoolSQL : : get_ro < Host > ( oid ) ;
}
2018-10-09 12:05:08 +03:00
2011-03-21 18:16:30 +03:00
/**
2020-09-10 10:08:29 +03:00
* Gets an object from the pool ( if needed the object is loaded from the
* database ) . The object is locked , other threads can ' t access the same
* object . The lock is released by destructor .
2011-03-21 18:16:30 +03:00
* @ param hostname
2020-09-10 10:08:29 +03:00
* @ return a pointer to the Host , nullptr if the Host could not be loaded
2011-03-21 18:16:30 +03:00
*/
2020-09-10 10:08:29 +03:00
std : : unique_ptr < Host > get ( std : : string name )
2011-03-21 18:16:30 +03:00
{
2012-01-25 15:26:46 +04:00
// The owner is set to -1, because it is not used in the key() method
2020-09-10 10:08:29 +03:00
return PoolSQL : : get < Host > ( name , - 1 ) ;
}
2011-03-21 18:16:30 +03:00
2018-10-09 12:05:08 +03:00
/**
2020-09-10 10:08:29 +03:00
* Gets a read only object from the pool ( if needed the object is loaded from the
* database ) . No object lock , other threads may work with the same object .
2018-10-09 12:05:08 +03:00
* @ param hostname
* @ return a pointer to the Host , 0 if the Host could not be loaded
*/
2020-09-10 10:08:29 +03:00
std : : unique_ptr < Host > get_ro ( std : : string name )
2018-10-09 12:05:08 +03:00
{
// The owner is set to -1, because it is not used in the key() method
2020-09-10 10:08:29 +03:00
return PoolSQL : : get_ro < Host > ( name , - 1 ) ;
}
2018-10-09 12:05:08 +03:00
2012-01-25 15:26:46 +04:00
/**
* Generate an index key for the object
* @ param name of the object
* @ param uid owner of the object , only used if needed
*
* @ return the key , a string
*/
2020-07-02 23:42:10 +03:00
std : : string key ( const std : : string & name , int uid )
2012-01-25 15:26:46 +04:00
{
// Name is enough key because Hosts can't repeat names.
return name ;
} ;
2008-06-17 20:27:32 +04:00
/**
* Bootstraps the database table ( s ) associated to the Host pool
2011-10-10 17:14:46 +04:00
* @ return 0 on success
2008-06-17 20:27:32 +04:00
*/
2011-10-10 17:14:46 +04:00
static int bootstrap ( SqlDB * _db )
2008-06-17 20:27:32 +04:00
{
2020-03-04 18:05:57 +03:00
int rc ;
2010-04-04 03:12:52 +04:00
2020-07-02 23:42:10 +03:00
std : : ostringstream oss_host ( one_db : : host_db_bootstrap ) ;
std : : ostringstream oss_monitor ( one_db : : host_monitor_db_bootstrap ) ;
2020-03-04 18:05:57 +03:00
rc = _db - > exec_local_wr ( oss_host ) ;
rc + = _db - > exec_local_wr ( oss_monitor ) ;
return rc ;
} ;
2008-06-22 05:51:49 +04:00
2009-07-09 18:34:34 +04:00
/**
* Allocates a given capacity to the host
* @ param oid the id of the host to allocate the capacity
2012-10-25 03:32:35 +04:00
* @ param vm_id id of the vm to add to the host
2012-07-12 14:48:27 +04:00
* @ param cpu amount of CPU , in percentage
* @ param mem amount of main memory , in KB
2009-07-09 18:34:34 +04:00
* @ param disk amount of disk
2015-08-21 02:08:28 +03:00
* @ param pci devices requested by the VM
2014-07-23 14:14:36 +04:00
*
* @ return 0 on success - 1 in case of failure
2009-07-09 18:34:34 +04:00
*/
2019-07-01 18:52:47 +03:00
int add_capacity ( int oid , HostShareCapacity & sr )
2008-06-22 05:51:49 +04:00
{
2014-07-23 14:14:36 +04:00
int rc = 0 ;
2010-04-04 03:12:52 +04:00
2020-09-10 10:08:29 +03:00
if ( auto host = get ( oid ) )
2010-04-04 03:12:52 +04:00
{
2019-07-01 18:52:47 +03:00
host - > add_capacity ( sr ) ;
2010-04-04 03:12:52 +04:00
2020-09-10 10:08:29 +03:00
update ( host . get ( ) ) ;
2010-04-04 03:12:52 +04:00
}
2014-07-23 14:14:36 +04:00
else
{
rc = - 1 ;
}
return rc ;
2020-09-10 10:08:29 +03:00
}
2010-04-04 03:12:52 +04:00
2009-07-09 18:34:34 +04:00
/**
* De - Allocates a given capacity to the host
* @ param oid the id of the host to allocate the capacity
2012-10-25 03:32:35 +04:00
* @ param vm_id id of the vm to add to the host
2009-07-09 18:34:34 +04:00
* @ param cpu amount of CPU
* @ param mem amount of main memory
* @ param disk amount of disk
2015-08-21 13:00:19 +03:00
* @ param pci devices requested by the VM
2010-04-04 03:12:52 +04:00
*/
2019-07-01 18:52:47 +03:00
void del_capacity ( int oid , HostShareCapacity & sr )
2008-06-22 05:51:49 +04:00
{
2020-09-10 10:08:29 +03:00
if ( auto host = get ( oid ) )
2010-04-04 03:12:52 +04:00
{
2019-07-01 18:52:47 +03:00
host - > del_capacity ( sr ) ;
2010-04-04 03:12:52 +04:00
2020-09-10 10:08:29 +03:00
update ( host . get ( ) ) ;
2010-04-04 03:12:52 +04:00
}
2020-09-10 10:08:29 +03:00
}
2009-07-09 18:34:34 +04:00
2020-07-02 23:42:10 +03:00
int drop ( PoolObjectSQL * objsql , std : : string & error_msg )
2011-06-10 18:58:28 +04:00
{
Host * host = static_cast < Host * > ( objsql ) ;
if ( host - > get_share_running_vms ( ) > 0 )
{
error_msg = " Can not remove a host with running VMs " ;
return - 1 ;
}
2020-03-04 18:05:57 +03:00
return PoolSQL : : drop ( objsql , error_msg ) ;
2011-06-10 18:58:28 +04:00
} ;
2009-07-09 18:34:34 +04:00
/**
* Dumps the HOST pool in XML format . A filter can be also added to the
* query
* @ param oss the output stream to dump the pool contents
* @ param where filter for the objects , defaults to all
2020-04-13 18:32:21 +03:00
* @ param sid first element used for pagination
* @ param eid last element used for pagination , - 1 to disable
2018-07-24 12:41:41 +03:00
* @ param desc descending order of pool elements
2009-07-09 18:34:34 +04:00
*
* @ return 0 on success
*/
2020-04-13 18:32:21 +03:00
int dump ( std : : string & oss , const std : : string & where , int sid , int eid ,
2018-07-24 12:41:41 +03:00
bool desc )
2010-07-08 19:26:25 +04:00
{
2020-03-04 18:05:57 +03:00
return PoolSQL : : dump ( oss , " HOST_POOL " , " body " , one_db : : host_table ,
2020-04-13 18:32:21 +03:00
where , sid , eid , desc ) ;
2010-07-08 19:26:25 +04:00
} ;
/**
2011-03-15 19:06:08 +03:00
* Finds a set objects that satisfies a given condition
* @ param oids a vector with the oids of the objects .
* @ param the name of the DB table .
* @ param where condition in SQL format .
2010-07-08 19:26:25 +04:00
*
2011-03-15 19:06:08 +03:00
* @ return 0 on success
2010-07-08 19:26:25 +04:00
*/
2020-07-02 23:42:10 +03:00
int search ( std : : vector < int > & oids , const std : : string & where )
2010-07-08 19:26:25 +04:00
{
2020-03-04 18:05:57 +03:00
return PoolSQL : : search ( oids , one_db : : host_table , where ) ;
2010-07-08 19:26:25 +04:00
} ;
2012-05-16 20:00:31 +04:00
/**
* Dumps the host monitoring information entries in XML format . A filter
* can be also added to the query .
*
* @ param oss the output stream to dump the pool contents
* @ param where filter for the objects , defaults to all
2020-10-28 19:12:27 +03:00
* @ param seconds Retrieve monitor records in the last seconds
2012-05-16 20:00:31 +04:00
*
* @ return 0 on success
*/
2020-10-28 19:12:27 +03:00
int dump_monitoring ( std : : string & oss , const std : : string & where , const int seconds ) ;
2012-05-16 20:00:31 +04:00
2012-05-17 03:24:39 +04:00
/**
* Dumps the HOST monitoring information for a single HOST
*
* @ param oss the output stream to dump the pool contents
* @ param hostid id of the target HOST
*
* @ return 0 on success
*/
2020-07-02 23:42:10 +03:00
int dump_monitoring ( std : : string & oss , int hostid )
2012-05-17 03:24:39 +04:00
{
2020-07-02 23:42:10 +03:00
std : : ostringstream filter ;
2012-05-17 03:24:39 +04:00
filter < < " oid = " < < hostid ;
2020-10-28 19:12:27 +03:00
return dump_monitoring ( oss , filter . str ( ) , - 1 ) ;
2012-05-17 03:24:39 +04:00
}
2012-05-16 20:00:31 +04:00
/**
2020-03-04 18:05:57 +03:00
* Returns last monitoring info for a host
* @ param hid host id
2012-05-16 20:00:31 +04:00
*/
2020-03-04 18:05:57 +03:00
HostMonitoringTemplate get_monitoring ( int hid ) ;
2012-05-16 20:00:31 +04:00
2008-06-17 20:27:32 +04:00
private :
/**
* Factory method to produce Host objects
* @ return a pointer to the new Host
*/
PoolObjectSQL * create ( )
{
2016-04-07 12:06:43 +03:00
return new Host ( - 1 , " " , " " , " " , - 1 , " " ) ;
2008-06-17 20:27:32 +04:00
} ;
} ;
2010-07-08 19:26:25 +04:00
# endif /*HOST_POOL_H_*/