2010-04-27 19:42:37 +04:00
/* ------------------------------------------------------------------------ */
2020-04-30 16:00:02 +03:00
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
2010-04-27 19:42:37 +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. */
/* -------------------------------------------------------------------------*/
2008-06-17 20:27:32 +04:00
# ifndef HOST_H_
# define HOST_H_
2019-12-10 13:45:15 +03:00
# include "PoolObjectSQL.h"
2010-08-04 17:14:53 +04:00
# include "HostTemplate.h"
2020-03-04 18:05:57 +03:00
# include "HostMonitoringTemplate.h"
2008-06-17 20:27:32 +04:00
# include "HostShare.h"
2016-03-15 18:37:36 +03:00
# include "ClusterableSingle.h"
2012-10-25 03:32:35 +04:00
# include "ObjectCollection.h"
# include "NebulaLog.h"
2013-10-04 21:05:55 +04:00
# include "NebulaUtil.h"
2008-06-17 20:27:32 +04:00
/**
2010-04-04 03:12:52 +04:00
* The Host class .
2008-06-17 20:27:32 +04:00
*/
2016-03-15 18:37:36 +03:00
class Host : public PoolObjectSQL , public ClusterableSingle
2008-06-17 20:27:32 +04:00
{
public :
2010-04-04 03:12:52 +04:00
2016-04-12 01:33:09 +03:00
// HOST STATES +----------------+
// | VM DEPLOYMENT |
// +----------------+------------+--------+-------+
// | STATE | MONITORING | MANUAL | SCHED |
// +----------------+------------+--------+-------+
// | INIT/MONITORED | Yes | Yes |
// +----------------+------------+--------+-------+
// | DISABLED | Yes | Yes | No |
// +----------------+------------+----------------+
// | OFFLINE | No | No |
// +----------------+-----------------------------+
2008-06-17 20:27:32 +04:00
enum HostState
{
2012-05-18 02:14:18 +04:00
INIT = 0 , /**< Initial state for enabled hosts. */
2020-03-04 18:05:57 +03:00
//MONITORING_MONITORED = 1, /**< Monitoring the host (from monitored). */
2016-04-12 01:33:09 +03:00
MONITORED = 2 , /**< The host has been monitored. */
ERROR = 3 , /**< An error ocurrer in host monitoring. */
DISABLED = 4 , /**< The host is disabled see above. */
2020-03-04 18:05:57 +03:00
//MONITORING_ERROR = 5, /**< Monitoring the host (from error). */
//MONITORING_INIT = 6, /**< Monitoring the host (from init). */
//MONITORING_DISABLED = 7, /**< Monitoring the host (from disabled). */
2016-04-12 01:33:09 +03:00
OFFLINE = 8 /**< The host is set offline, see above */
2008-06-17 20:27:32 +04:00
} ;
2019-09-09 15:43:51 +03:00
/**
* Functions to convert to / from string the Host states
*/
static int str_to_state ( std : : string & st , HostState & state )
{
one_util : : toupper ( st ) ;
state = INIT ;
if ( st = = " INIT " ) {
state = INIT ;
} else if ( st = = " MONITORED " ) {
state = MONITORED ;
} else if ( st = = " ERROR " ) {
state = ERROR ;
} else if ( st = = " DISABLED " ) {
state = DISABLED ;
} else if ( st = = " OFFLINE " ) {
state = OFFLINE ;
}
else
{
return - 1 ;
}
return 0 ;
}
2020-03-04 18:05:57 +03:00
static string state_to_str ( HostState state )
2019-09-09 15:43:51 +03:00
{
2020-03-04 18:05:57 +03:00
string st = " " ;
2019-09-09 15:43:51 +03:00
switch ( state )
{
case INIT :
st = " INIT " ;
break ;
case MONITORED :
st = " MONITORED " ;
break ;
case ERROR :
st = " ERROR " ;
break ;
case DISABLED :
st = " DISABLED " ;
break ;
case OFFLINE :
st = " OFFLINE " ;
break ;
}
return st ;
}
2011-02-24 20:12:26 +03:00
/**
* Function to print the Host object into a string in XML format
* @ param xml the resulting XML string
* @ return a reference to the generated string
*/
2020-03-04 18:05:57 +03:00
std : : string & to_xml ( string & xml ) const override ;
2009-07-09 18:34:34 +04:00
2008-06-17 20:27:32 +04:00
/**
2011-02-24 20:12:26 +03:00
* Rebuilds the object from an xml formatted string
* @ param xml_str The xml - formatted string
*
* @ return 0 on success , - 1 otherwise
2008-06-17 20:27:32 +04:00
*/
2020-03-04 18:05:57 +03:00
int from_xml ( const std : : string & xml_str ) override ;
2008-06-17 20:27:32 +04:00
2013-11-21 14:25:53 +04:00
/**
* Checks if the host is a remote public cloud
* @ return true if the host is a remote public cloud
*/
bool is_public_cloud ( ) const ;
2013-10-04 21:05:55 +04:00
2008-06-17 20:27:32 +04:00
/**
2016-04-12 01:33:09 +03:00
* Sets the current host offline , it will not be monitored nor used by the
* scheduler , manual VM deployment is also restricted
*/
void offline ( ) ;
/**
* Sets the current host disable , it will receive monitor updates , manual
* deployment of VMs is allowed and the scheduler will not consider this
* host .
2010-04-04 03:12:52 +04:00
*/
2013-04-24 03:12:22 +04:00
void disable ( ) ;
2008-06-17 20:27:32 +04:00
/**
2010-04-04 03:12:52 +04:00
* Enables the current host , it will be monitored and could be used by
2008-06-17 20:27:32 +04:00
* the scheduler
2010-04-04 03:12:52 +04:00
*/
2016-04-12 01:33:09 +03:00
void enable ( ) ;
2010-04-04 03:12:52 +04:00
2013-04-24 03:12:22 +04:00
/**
2020-03-04 18:05:57 +03:00
* Sets the host in error state ( if not disabled / offline ) and update
* template with the associated error message
* @ param message associated error message
2013-04-24 03:12:22 +04:00
*/
2020-03-04 18:05:57 +03:00
void error ( const std : : string & message ) ;
2013-04-24 03:12:22 +04:00
2019-09-09 15:43:51 +03:00
/**
* Test if the Host has changed state since last time prev state was set
* @ return true if Host changed state
*/
2020-03-04 18:05:57 +03:00
bool has_changed_state ( )
{
return prev_state ! = state ;
}
2019-09-09 15:43:51 +03:00
/**
* Sets the previous state to the current one
*/
void set_prev_state ( )
{
prev_state = state ;
} ;
2013-02-16 05:55:17 +04:00
/**
* Update host after a successful monitor . It modifies counters , state
* and template attributes
2008-06-17 20:27:32 +04:00
* @ param parse_str string with values to be parsed
* @ return 0 on success
* */
2020-03-04 18:05:57 +03:00
int update_info ( Template & tmpl ) ;
2008-06-17 20:27:32 +04:00
2012-05-16 20:00:31 +04:00
/**
2020-03-04 18:05:57 +03:00
* Retrieves host state
* @ return HostState code number
2012-05-16 20:00:31 +04:00
*/
2020-03-04 18:05:57 +03:00
HostState get_state ( ) const
{
return state ;
} ;
2012-05-16 20:00:31 +04:00
2008-06-17 20:27:32 +04:00
/**
2013-02-12 20:15:28 +04:00
* Retrieves host state
2010-04-27 19:42:37 +04:00
* @ return HostState code number
2008-06-17 20:27:32 +04:00
*/
2020-03-04 18:05:57 +03:00
void set_state ( HostState new_state )
2008-06-17 20:27:32 +04:00
{
2020-03-04 18:05:57 +03:00
state = new_state ;
2008-06-17 20:27:32 +04:00
} ;
/**
2013-02-12 20:15:28 +04:00
* Retrieves VMM mad name
2010-04-27 19:42:37 +04:00
* @ return string vmm mad name
2008-06-17 20:27:32 +04:00
*/
const string & get_vmm_mad ( ) const
{
return vmm_mad_name ;
} ;
2010-04-04 03:12:52 +04:00
2008-06-17 20:27:32 +04:00
/**
2013-02-12 20:15:28 +04:00
* Retrieves IM mad name
2010-04-27 19:42:37 +04:00
* @ return string im mad name
2008-06-17 20:27:32 +04:00
*/
const string & get_im_mad ( ) const
{
return im_mad_name ;
} ;
2010-04-04 03:12:52 +04:00
2014-03-04 14:42:44 +04:00
// -------------------------------------------------------------------------
2016-11-05 23:14:38 +03:00
// Share functions.
2014-03-04 14:42:44 +04:00
// -------------------------------------------------------------------------
2009-07-14 20:40:33 +04:00
2016-11-05 23:14:38 +03:00
long long get_share_running_vms ( )
2009-07-14 20:40:33 +04:00
{
2016-11-05 23:14:38 +03:00
return host_share . get_running_vms ( ) ;
2009-07-14 20:40:33 +04:00
}
2008-06-17 20:27:32 +04:00
/**
2019-07-01 18:52:47 +03:00
* Adds a new VM to the host share by incrementing usage counters
* @ param sr the capacity request of the VM
2008-06-17 20:27:32 +04:00
* @ return 0 on success
*/
2019-07-01 18:52:47 +03:00
void add_capacity ( HostShareCapacity & sr )
2008-06-17 20:27:32 +04:00
{
2019-07-01 18:52:47 +03:00
if ( vm_collection . add ( sr . vmid ) = = 0 )
2012-10-25 03:32:35 +04:00
{
2019-07-01 18:52:47 +03:00
host_share . add ( sr ) ;
2012-10-25 03:32:35 +04:00
}
else
{
2020-03-04 18:05:57 +03:00
std : : ostringstream oss ;
2019-07-01 18:52:47 +03:00
oss < < " VM " < < sr . vmid < < " is already in host " < < oid < < " . " ;
2012-10-25 03:32:35 +04:00
NebulaLog : : log ( " ONE " , Log : : ERROR , oss ) ;
}
2008-06-17 20:27:32 +04:00
} ;
2010-04-04 03:12:52 +04:00
2008-06-17 20:27:32 +04:00
/**
2019-07-01 18:52:47 +03:00
* Deletes a new VM to the host share by incrementing usage counters
* @ param sr the capacity request of the VM
2008-06-17 20:27:32 +04:00
* @ return 0 on success
*/
2019-07-01 18:52:47 +03:00
void del_capacity ( HostShareCapacity & sr )
2010-04-04 03:12:52 +04:00
{
2019-07-01 18:52:47 +03:00
if ( vm_collection . del ( sr . vmid ) = = 0 )
2012-10-25 03:32:35 +04:00
{
2019-07-01 18:52:47 +03:00
host_share . del ( sr ) ;
2012-10-25 03:32:35 +04:00
}
else
{
2020-03-04 18:05:57 +03:00
std : : ostringstream oss ;
2019-07-01 18:52:47 +03:00
oss < < " VM " < < sr . vmid < < " is not in host " < < oid < < " . " ;
2012-10-25 03:32:35 +04:00
NebulaLog : : log ( " ONE " , Log : : ERROR , oss ) ;
}
2008-06-17 20:27:32 +04:00
} ;
2013-02-23 22:49:06 +04:00
/**
2019-07-01 18:52:47 +03:00
* Tests whether a VM device capacity can be allocated in the host
* @ param sr capacity requested by the VM
* @ param error returns the error reason , if any
*
2015-09-04 14:22:11 +03:00
* @ return true if the share can host the VM
*/
2019-07-01 18:52:47 +03:00
bool test_capacity ( HostShareCapacity & sr , string & error )
2015-09-04 14:22:11 +03:00
{
2019-07-01 18:52:47 +03:00
return host_share . test ( sr , error ) ;
2015-09-04 14:22:11 +03:00
}
2020-03-04 18:05:57 +03:00
/**
* Update reserved capacity according to cluster reservations
* @ param ccpu cluster reserved cpu
* @ param cmem cluster reserved mem
*
* @ return true capacity was updated ,
* false if host has its own reservations
*/
bool update_reserved_capacity ( const string & ccpu , const string & cmem ) ;
2013-09-02 14:53:54 +04:00
/**
* Returns a copy of the VM IDs set
*/
set < int > get_vm_ids ( )
{
2016-03-02 01:31:31 +03:00
return vm_collection . clone ( ) ;
2013-09-02 14:53:54 +04:00
}
2011-06-02 01:53:09 +04:00
/**
* Factory method for host templates
*/
2019-09-03 17:31:51 +03:00
Template * get_new_template ( ) const override
2011-06-02 01:53:09 +04:00
{
return new HostTemplate ;
}
2014-11-18 01:17:22 +03:00
/**
* Executed after an update operation to process the new template
2019-09-03 17:31:51 +03:00
* - encrypt secret attributes .
2014-11-18 01:17:22 +03:00
*/
2020-03-04 18:05:57 +03:00
int post_update_template ( std : : string & error ) override ;
2014-11-18 01:17:22 +03:00
2015-05-05 13:14:35 +03:00
/**
2020-03-04 18:05:57 +03:00
* Read monitoring from DB
2015-05-05 13:14:35 +03:00
*/
2020-03-04 18:05:57 +03:00
void load_monitoring ( ) ;
2015-05-05 13:14:35 +03:00
2020-03-13 17:04:31 +03:00
void update_zombies ( const std : : set < int > & ids ) ;
2008-06-17 20:27:32 +04:00
private :
2010-04-04 03:12:52 +04:00
friend class HostPool ;
2008-06-17 20:27:32 +04:00
/**
* The state of the Host
*/
2020-03-04 18:05:57 +03:00
HostState state ;
HostState prev_state ;
2010-04-04 03:12:52 +04:00
2015-07-01 22:15:40 +03:00
/**
2020-03-04 18:05:57 +03:00
* Name of the IM and VMM drivers
2010-04-04 03:12:52 +04:00
*/
2020-03-04 18:05:57 +03:00
string im_mad_name ;
string vmm_mad_name ;
2010-04-04 03:12:52 +04:00
2008-06-17 20:27:32 +04:00
/**
2009-07-09 18:34:34 +04:00
* The Share represents the logical capacity associated with the host
2008-06-17 20:27:32 +04:00
*/
2017-06-23 00:37:08 +03:00
HostShare host_share ;
2008-06-17 20:27:32 +04:00
2012-10-25 03:32:35 +04:00
/**
* Stores a collection with the VMs running in the host
*/
ObjectCollection vm_collection ;
2020-03-04 18:05:57 +03:00
/**
* Stores monitor information for the host
*/
HostMonitoringTemplate monitoring ;
2013-10-04 21:05:55 +04:00
2008-06-17 20:27:32 +04:00
// *************************************************************************
// Constructor
// *************************************************************************
2020-03-04 18:05:57 +03:00
Host ( int id , const std : : string & hostname , const std : : string & im_mad ,
const std : : string & vmm_mad , int clusterid , const std : : string & cluster ) ;
2008-06-17 20:27:32 +04:00
2019-09-03 17:31:51 +03:00
virtual ~ Host ( ) = default ;
2010-04-04 03:12:52 +04:00
2020-03-04 18:05:57 +03:00
2008-06-17 20:27:32 +04:00
// *************************************************************************
2020-03-04 18:05:57 +03:00
// Helper functions
2008-06-17 20:27:32 +04:00
// *************************************************************************
2020-03-04 18:05:57 +03:00
/**
* Gets the reserved capacity of this host , if not defined it will be used
* the cluster one ( if any )
* @ param rcpu reserved cpu
* @ param rmem reserved mem
*/
void reserved_capacity ( std : : string & rcpu , std : : string & rmem ) const ;
2010-04-04 03:12:52 +04:00
2020-03-04 18:05:57 +03:00
void update_wilds ( ) ;
2012-05-16 20:00:31 +04:00
2020-03-04 18:05:57 +03:00
// *************************************************************************
// DataBase implementation (Private)
// *************************************************************************
2012-05-16 20:00:31 +04:00
2008-06-17 20:27:32 +04:00
/**
2011-02-25 01:30:39 +03:00
* Execute an INSERT or REPLACE Sql query .
* @ param db The SQL DB
* @ param replace Execute an INSERT or a REPLACE
2011-12-19 20:07:32 +04:00
* @ param error_str Returns the error reason , if any
2011-02-25 01:30:39 +03:00
* @ return 0 one success
2011-12-19 20:07:32 +04:00
*/
2020-03-04 18:05:57 +03:00
int insert_replace ( SqlDB * db , bool replace , std : : string & error_str ) ;
2010-04-04 03:12:52 +04:00
2008-06-17 20:27:32 +04:00
/**
* Writes the Host and its associated HostShares in the database .
* @ param db pointer to the db
* @ return 0 on success
*/
2020-03-04 18:05:57 +03:00
int insert ( SqlDB * db , std : : string & error_str ) override
2011-12-19 20:07:32 +04:00
{
return insert_replace ( db , false , error_str ) ;
} ;
2009-07-09 18:34:34 +04:00
/**
2008-06-17 20:27:32 +04:00
* Writes / updates the Hosts data fields in the database .
* @ param db pointer to the db
2009-07-09 18:34:34 +04:00
* @ return 0 on success
*/
2019-09-03 17:31:51 +03:00
int update ( SqlDB * db ) override
2011-12-19 20:07:32 +04:00
{
2020-03-04 18:05:57 +03:00
std : : string error_str ;
2011-12-19 20:07:32 +04:00
return insert_replace ( db , true , error_str ) ;
} ;
2008-06-17 20:27:32 +04:00
} ;
# endif /*HOST_H_*/