2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2019-01-16 13:27:59 +03:00
/* Copyright 2002-2019, 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"
2017-10-19 17:41:42 +03:00
# include "CachePool.h"
2008-06-17 20:27:32 +04:00
# include <time.h>
# include <sstream>
# include <iostream>
# include <vector>
using namespace std ;
/**
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 :
2016-02-04 15:10:42 +03:00
HostPool ( SqlDB * db , vector < const VectorAttribute * > hook_mads ,
const string & hook_location , const string & remotes_location ,
time_t expire_time ) ;
2008-06-17 20:27:32 +04:00
2017-10-20 15:17:55 +03:00
~ HostPool ( ) { } ;
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 (
int * oid ,
2010-08-05 21:28:28 +04:00
const string & hostname ,
const string & im_mad_name ,
const string & vmm_mad_name ,
2012-02-24 21:53:18 +04:00
int cluster_id ,
const string & cluster_name ,
2010-08-05 21:28:28 +04:00
string & error_str ) ;
2008-06-17 20:27:32 +04:00
/**
* Function to get a Host from the pool , if the object is not in memory
* it is loaded from the DB
* @ param oid Host unique id
* @ param lock locks the Host mutex
* @ return a pointer to the Host , 0 if the Host could not be loaded
*/
Host * get (
2018-03-18 01:31:52 +03:00
int oid )
2008-06-17 20:27:32 +04:00
{
2018-03-18 01:31:52 +03:00
Host * h = static_cast < Host * > ( PoolSQL : : get ( oid ) ) ;
2017-06-23 00:37:08 +03:00
if ( h ! = 0 )
{
HostVM * hv = get_host_vm ( oid ) ;
h - > tmp_lost_vms = & ( hv - > tmp_lost_vms ) ;
h - > tmp_zombie_vms = & ( hv - > tmp_zombie_vms ) ;
h - > prev_rediscovered_vms = & ( hv - > prev_rediscovered_vms ) ;
}
return h ;
2008-06-17 20:27:32 +04:00
} ;
2010-04-04 03:12:52 +04:00
2018-10-09 12:05:08 +03:00
void update_prev_rediscovered_vms ( int hoid ,
const set < int > & prev_rediscovered_vms )
{
if ( hoid < 0 )
{
return ;
}
HostVM * hv = get_host_vm ( hoid ) ;
hv - > prev_rediscovered_vms = prev_rediscovered_vms ;
}
/**
* Function to get a read only Host from the pool , if the object is not in memory
* it is loaded from the DB
* @ param oid Host unique id
* @ return a pointer to the Host , 0 if the Host could not be loaded
*/
Host * get_ro (
int oid )
{
Host * h = static_cast < Host * > ( PoolSQL : : get_ro ( oid ) ) ;
if ( h ! = 0 )
{
HostVM * hv = get_host_vm ( oid ) ;
h - > tmp_lost_vms = & ( hv - > tmp_lost_vms ) ;
h - > tmp_zombie_vms = & ( hv - > tmp_zombie_vms ) ;
h - > prev_rediscovered_vms = & ( hv - > prev_rediscovered_vms ) ;
}
return h ;
} ;
2011-03-21 18:16:30 +03:00
/**
* Function to get a Host from the pool , if the object is not in memory
* it is loaded from the DB
* @ param hostname
* @ param lock locks the Host mutex
* @ return a pointer to the Host , 0 if the Host could not be loaded
*/
2018-03-18 01:31:52 +03:00
Host * get ( 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
2018-03-18 01:31:52 +03:00
Host * h = static_cast < Host * > ( PoolSQL : : get ( name , - 1 ) ) ;
2017-06-23 00:37:08 +03:00
if ( h ! = 0 )
{
HostVM * hv = get_host_vm ( h - > oid ) ;
h - > tmp_lost_vms = & ( hv - > tmp_lost_vms ) ;
h - > tmp_zombie_vms = & ( hv - > tmp_zombie_vms ) ;
h - > prev_rediscovered_vms = & ( hv - > prev_rediscovered_vms ) ;
}
return h ;
2011-03-21 18:16:30 +03:00
} ;
2018-10-09 12:05:08 +03:00
/**
* Function to get a Host from the pool , if the object is not in memory
* it is loaded from the DB
* @ param hostname
* @ param lock locks the Host mutex
* @ return a pointer to the Host , 0 if the Host could not be loaded
*/
Host * get_ro ( string name )
{
// The owner is set to -1, because it is not used in the key() method
Host * h = static_cast < Host * > ( PoolSQL : : get_ro ( name , - 1 ) ) ;
if ( h ! = 0 )
{
HostVM * hv = get_host_vm ( h - > oid ) ;
h - > tmp_lost_vms = & ( hv - > tmp_lost_vms ) ;
h - > tmp_zombie_vms = & ( hv - > tmp_zombie_vms ) ;
h - > prev_rediscovered_vms = & ( hv - > prev_rediscovered_vms ) ;
}
return h ;
} ;
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
*/
string key ( const string & name , int uid )
{
// 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
{
2011-10-10 17:14:46 +04:00
return Host : : bootstrap ( _db ) ;
2008-06-17 20:27:32 +04:00
} ;
2010-04-04 03:12:52 +04:00
2008-06-17 20:27:32 +04:00
/**
2010-07-11 21:45:10 +04:00
* Get the least monitored hosts
2013-02-13 16:20:37 +04:00
* @ param discovered hosts
2010-06-18 02:20:01 +04:00
* @ param host_limit max . number of hosts to monitor at a time
2013-02-14 17:51:16 +04:00
* @ param target_time Filters hosts with last_mon_time < = target_time
2009-07-09 18:34:34 +04:00
* @ return int 0 if success
2008-06-17 20:27:32 +04:00
*/
2013-02-14 17:51:16 +04:00
int discover ( set < int > * discovered_hosts , int host_limit , time_t target_time ) ;
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
*/
2015-08-21 02:08:28 +03:00
int add_capacity ( int oid , int vm_id , int cpu , int mem , int disk ,
2016-02-04 15:10:42 +03:00
vector < VectorAttribute * > pci )
2008-06-22 05:51:49 +04:00
{
2014-07-23 14:14:36 +04:00
int rc = 0 ;
2018-03-18 01:31:52 +03:00
Host * host = get ( oid ) ;
2010-04-04 03:12:52 +04:00
if ( host ! = 0 )
{
2015-08-21 02:08:28 +03:00
host - > add_capacity ( vm_id , cpu , mem , disk , pci ) ;
2010-04-04 03:12:52 +04:00
update ( host ) ;
host - > unlock ( ) ;
}
2014-07-23 14:14:36 +04:00
else
{
rc = - 1 ;
}
return rc ;
2008-06-22 05:51:49 +04: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
*/
2015-08-21 13:00:19 +03:00
void del_capacity ( int oid , int vm_id , int cpu , int mem , int disk ,
2016-02-04 15:10:42 +03:00
vector < VectorAttribute * > pci )
2008-06-22 05:51:49 +04:00
{
2018-03-18 01:31:52 +03:00
Host * host = get ( oid ) ;
2010-04-04 03:12:52 +04:00
if ( host ! = 0 )
{
2015-08-21 13:00:19 +03:00
host - > del_capacity ( vm_id , cpu , mem , disk , pci ) ;
2010-04-04 03:12:52 +04:00
update ( host ) ;
host - > unlock ( ) ;
}
2008-06-22 05:51:49 +04:00
} ;
2009-07-09 18:34:34 +04:00
2011-06-10 18:58:28 +04:00
int drop ( PoolObjectSQL * objsql , string & error_msg )
{
Host * host = static_cast < Host * > ( objsql ) ;
2017-10-19 17:41:42 +03:00
int oid = host - > oid ;
2011-06-10 18:58:28 +04:00
if ( host - > get_share_running_vms ( ) > 0 )
{
error_msg = " Can not remove a host with running VMs " ;
return - 1 ;
}
2017-06-23 00:37:08 +03:00
int rc = PoolSQL : : drop ( objsql , error_msg ) ;
if ( rc = = 0 )
{
2017-10-19 17:41:42 +03:00
delete_host_vm ( oid ) ;
2017-06-23 00:37:08 +03:00
}
return rc ;
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
2014-01-13 19:30:43 +04:00
* @ param limit parameters used for pagination
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
*/
2018-10-09 12:05:08 +03:00
int dump ( string & oss , const string & where , const string & limit ,
2018-07-24 12:41:41 +03:00
bool desc )
2010-07-08 19:26:25 +04:00
{
2018-10-09 12:05:08 +03:00
return PoolSQL : : dump ( oss , " HOST_POOL " , " body " , Host : : table , where , limit , 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
*/
2011-03-15 19:06:08 +03:00
int search ( vector < int > & oids , const string & where )
2010-07-08 19:26:25 +04:00
{
2011-03-15 19:06:08 +03:00
return PoolSQL : : search ( oids , 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
*
* @ return 0 on success
*/
2018-10-09 12:05:08 +03:00
int dump_monitoring ( string & oss , const string & where ) ;
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
*/
2018-10-09 12:05:08 +03:00
int dump_monitoring ( string & oss , int hostid )
2012-05-17 03:24:39 +04:00
{
ostringstream filter ;
filter < < " oid = " < < hostid ;
return dump_monitoring ( oss , filter . str ( ) ) ;
}
2012-05-16 20:00:31 +04:00
/**
* Inserts the last monitoring , and deletes old monitoring entries for this
* host
*
* @ param host pointer to the host object
* @ return 0 on success
*/
int update_monitoring ( Host * host )
{
2012-05-17 03:24:39 +04:00
if ( _monitor_expiration < = 0 )
2012-05-16 20:00:31 +04:00
{
return 0 ;
}
return host - > update_monitoring ( db ) ;
} ;
/**
2012-05-18 14:05:18 +04:00
* Deletes the expired monitoring entries for all hosts
2012-05-16 20:00:31 +04:00
*
* @ return 0 on success
*/
2012-05-18 14:05:18 +04:00
int clean_expired_monitoring ( ) ;
2012-05-16 20:00:31 +04:00
2008-06-17 20:27:32 +04:00
private :
2017-06-23 00:37:08 +03:00
/**
* Stores several Host counters to give VMs one monitor grace cycle before
* moving them to another state
*/
struct HostVM
{
/**
* Tmp set of lost VM IDs .
*/
set < int > tmp_lost_vms ;
/**
* Tmp set of zombie VM IDs .
*/
set < int > tmp_zombie_vms ;
/**
* VMs reported as found from the poweroff state .
*/
set < int > prev_rediscovered_vms ;
} ;
2017-10-19 17:41:42 +03:00
CachePool < HostVM > cache ;
2017-06-23 00:37:08 +03:00
2017-10-20 15:17:55 +03:00
HostVM * get_host_vm ( int oid )
{
2017-10-19 17:41:42 +03:00
return cache . get_resource ( oid ) ;
}
2017-06-23 00:37:08 +03:00
2017-10-20 15:17:55 +03:00
void delete_host_vm ( int oid )
{
2017-10-19 17:41:42 +03:00
cache . delete_resource ( oid ) ;
}
2010-07-08 19:26:25 +04:00
2008-06-17 20:27:32 +04:00
/**
* 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-04-04 03:12:52 +04:00
/**
* Callback function to get the IDs of the hosts to be monitored
* ( Host : : discover )
2013-02-13 16:20:37 +04:00
*
* @ param _set the set < int > * of host ids
2010-04-04 03:12:52 +04:00
* @ param num the number of columns read from the DB
2013-02-13 16:20:37 +04:00
* @ param values the column values
2010-04-04 03:12:52 +04:00
* @ param names the column names
2013-02-13 16:20:37 +04:00
*
2010-04-04 03:12:52 +04:00
* @ return 0 on success
*/
2013-02-13 16:20:37 +04:00
int discover_cb ( void * _set , int num , char * * values , char * * names ) ;
2012-05-16 20:00:31 +04:00
2012-05-17 16:58:28 +04:00
/**
* Deletes all monitoring entries for all hosts
*
* @ return 0 on success
*/
2012-05-18 14:05:18 +04:00
int clean_all_monitoring ( ) ;
2012-05-17 16:58:28 +04:00
2012-05-16 20:00:31 +04:00
/**
* Size , in seconds , of the historical monitoring information
*/
2012-05-17 03:24:39 +04:00
static time_t _monitor_expiration ;
2008-06-17 20:27:32 +04:00
} ;
2010-07-08 19:26:25 +04:00
# endif /*HOST_POOL_H_*/