mirror of
https://github.com/OpenNebula/one.git
synced 2025-02-22 17:57:46 +03:00
feature #407: Minor changes in Host class refactor
This commit is contained in:
parent
ebf66e0fda
commit
0b9955175a
@ -25,7 +25,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
//TODO refactor ClusterPool
|
||||
/**
|
||||
* A cluster helper class. It is not a normal PoolSQL,
|
||||
* but a series of static methods related to clusters.
|
||||
|
@ -61,7 +61,7 @@ public:
|
||||
* @param xml the resulting XML string
|
||||
* @return a reference to the generated string
|
||||
*/
|
||||
virtual string& to_xml(string& xml) const;
|
||||
string& to_xml(string& xml) const;
|
||||
|
||||
/**
|
||||
* Rebuilds the object from an xml formatted string
|
||||
@ -69,7 +69,7 @@ public:
|
||||
*
|
||||
* @return 0 on success, -1 otherwise
|
||||
*/
|
||||
virtual int from_xml(const string &xml_str);
|
||||
int from_xml(const string &xml_str);
|
||||
|
||||
/**
|
||||
* Get the Host unique identifier HID, that matches the OID of the object
|
||||
@ -434,30 +434,6 @@ private:
|
||||
*/
|
||||
HostShare host_share;
|
||||
|
||||
// *************************************************************************
|
||||
// DataBase implementation (Private)
|
||||
// *************************************************************************
|
||||
|
||||
/**
|
||||
* Execute an INSERT or REPLACE Sql query.
|
||||
* @param db The SQL DB
|
||||
* @param replace Execute an INSERT or a REPLACE
|
||||
* @return 0 one success
|
||||
*/
|
||||
int insert_replace(SqlDB *db, bool replace);
|
||||
|
||||
/**
|
||||
* Bootstraps the database table(s) associated to the Host
|
||||
*/
|
||||
static void bootstrap(SqlDB * db)
|
||||
{
|
||||
ostringstream oss_host(Host::db_bootstrap);
|
||||
|
||||
db->exec(oss_host);
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
// *************************************************************************
|
||||
// Constructor
|
||||
// *************************************************************************
|
||||
@ -471,7 +447,7 @@ protected:
|
||||
virtual ~Host();
|
||||
|
||||
// *************************************************************************
|
||||
// DataBase implementation
|
||||
// DataBase implementation (Private)
|
||||
// *************************************************************************
|
||||
|
||||
enum ColNames
|
||||
@ -495,27 +471,36 @@ protected:
|
||||
static const char * table;
|
||||
|
||||
/**
|
||||
* Table name
|
||||
* @return the object's table name
|
||||
* Execute an INSERT or REPLACE Sql query.
|
||||
* @param db The SQL DB
|
||||
* @param replace Execute an INSERT or a REPLACE
|
||||
* @return 0 one success
|
||||
*/
|
||||
int insert_replace(SqlDB *db, bool replace);
|
||||
|
||||
/**
|
||||
* Bootstraps the database table(s) associated to the Host
|
||||
*/
|
||||
virtual const char * table_name()
|
||||
{
|
||||
return table;
|
||||
};
|
||||
static void bootstrap(SqlDB * db)
|
||||
{
|
||||
ostringstream oss_host(Host::db_bootstrap);
|
||||
|
||||
db->exec(oss_host);
|
||||
};
|
||||
|
||||
/**
|
||||
* Writes the Host and its associated HostShares in the database.
|
||||
* @param db pointer to the db
|
||||
* @return 0 on success
|
||||
*/
|
||||
virtual int insert(SqlDB *db, string& error_str);
|
||||
int insert(SqlDB *db, string& error_str);
|
||||
|
||||
/**
|
||||
* Writes/updates the Hosts data fields in the database.
|
||||
* @param db pointer to the db
|
||||
* @return 0 on success
|
||||
*/
|
||||
virtual int update(SqlDB *db);
|
||||
int update(SqlDB *db);
|
||||
};
|
||||
|
||||
#endif /*HOST_H_*/
|
||||
|
@ -146,17 +146,6 @@ public:
|
||||
/* ---------------------------------------------------------------------- */
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Returns true if the clid is an id for an existing cluster
|
||||
* @param clid ID of the cluster
|
||||
*
|
||||
* @return true if the clid is an id for an existing cluster
|
||||
*/
|
||||
/* bool exists_cluster(int clid)
|
||||
{
|
||||
return cluster_pool.exists(clid);
|
||||
};*/
|
||||
|
||||
/**
|
||||
* Allocates a new cluster in the pool
|
||||
* @param clid the id assigned to the cluster
|
||||
|
@ -106,14 +106,6 @@ public:
|
||||
*/
|
||||
string& to_xml(string& xml) const;
|
||||
|
||||
/**
|
||||
* Rebuilds the object from an xml node
|
||||
* @param node The xml node pointer
|
||||
*
|
||||
* @return 0 on success, -1 otherwise
|
||||
*/
|
||||
int from_xml_node(const xmlNodePtr node);
|
||||
|
||||
private:
|
||||
|
||||
int disk_usage; /**< Disk allocated to VMs (in Mb). */
|
||||
@ -132,7 +124,7 @@ private:
|
||||
int used_mem; /**< Used memory from the IM monitor */
|
||||
int used_cpu; /**< Used cpu from the IM monitor */
|
||||
|
||||
int running_vms; /**< Number of running VMs in this Host */
|
||||
int running_vms;/**< Number of running VMs in this Host */
|
||||
|
||||
// ----------------------------------------
|
||||
// Friends
|
||||
@ -141,7 +133,13 @@ private:
|
||||
friend class Host;
|
||||
friend class HostPool;
|
||||
|
||||
/**
|
||||
* Rebuilds the object from an xml node
|
||||
* @param node The xml node pointer
|
||||
*
|
||||
* @return 0 on success, -1 otherwise
|
||||
*/
|
||||
int from_xml_node(const xmlNodePtr node);
|
||||
};
|
||||
|
||||
|
||||
#endif /*HOST_SHARE_H_*/
|
||||
|
@ -37,7 +37,9 @@ class PoolObjectSQL : public ObjectSQL, public ObjectXML
|
||||
{
|
||||
public:
|
||||
|
||||
PoolObjectSQL(int id=-1):oid(id),valid(true)
|
||||
//TODO remove Defaults for Constructor Attributes
|
||||
PoolObjectSQL(int id=-1, const char * _table = 0)
|
||||
:ObjectSQL(),ObjectXML(),oid(id),valid(true),table(_table)
|
||||
{
|
||||
pthread_mutex_init(&mutex,0);
|
||||
};
|
||||
@ -129,8 +131,7 @@ protected:
|
||||
return -1;
|
||||
}
|
||||
|
||||
from_xml( values[0] );
|
||||
return 0;
|
||||
return from_xml(values[0]);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -147,7 +148,7 @@ protected:
|
||||
set_callback(
|
||||
static_cast<Callbackable::Callback>(&PoolObjectSQL::select_cb));
|
||||
|
||||
oss << "SELECT body FROM " << table_name() << " WHERE oid = " << oid;
|
||||
oss << "SELECT body FROM " << table << " WHERE oid = " << oid;
|
||||
|
||||
boid = oid;
|
||||
oid = -1;
|
||||
@ -174,7 +175,7 @@ protected:
|
||||
ostringstream oss;
|
||||
int rc;
|
||||
|
||||
oss << "DELETE FROM " << table_name() << " WHERE oid=" << oid;
|
||||
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
|
||||
|
||||
rc = db->exec(oss);
|
||||
|
||||
@ -215,17 +216,6 @@ protected:
|
||||
*/
|
||||
bool valid;
|
||||
|
||||
/**
|
||||
* Table name
|
||||
* @return the object's table name
|
||||
*/
|
||||
// virtual const char * table_name() = 0;
|
||||
// TODO: change to pure virtual when all child classes implement it
|
||||
virtual const char * table_name()
|
||||
{
|
||||
return "";
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
@ -238,6 +228,11 @@ private:
|
||||
* IS LOCKED when the class destructor is called.
|
||||
*/
|
||||
pthread_mutex_t mutex;
|
||||
|
||||
/**
|
||||
* Pointer to the SQL table for the PoolObjectSQL
|
||||
*/
|
||||
const char * table;
|
||||
};
|
||||
|
||||
#endif /*POOL_OBJECT_SQL_H_*/
|
||||
|
@ -206,12 +206,6 @@ protected:
|
||||
*/
|
||||
Attribute* vector_xml_att(const xmlNode * node);
|
||||
|
||||
/**
|
||||
* Builds the template attribute from the node
|
||||
* @param root_element The xml element to build the template from.
|
||||
*/
|
||||
void rebuild_attributes(const xmlNode * root_element);
|
||||
|
||||
private:
|
||||
|
||||
bool replace_mode;
|
||||
@ -229,6 +223,12 @@ private:
|
||||
* Name of the Root element for the XML document
|
||||
*/
|
||||
string xml_root;
|
||||
|
||||
/**
|
||||
* Builds the template attribute from the node
|
||||
* @param root_element The xml element to build the template from.
|
||||
*/
|
||||
void rebuild_attributes(const xmlNode * root_element);
|
||||
};
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
@ -33,7 +33,7 @@ Host::Host(
|
||||
string _im_mad_name,
|
||||
string _vmm_mad_name,
|
||||
string _tm_mad_name):
|
||||
PoolObjectSQL(id),
|
||||
PoolObjectSQL(id,table),
|
||||
hostname(_hostname),
|
||||
state(INIT),
|
||||
im_mad_name(_im_mad_name),
|
||||
@ -109,8 +109,7 @@ int Host::insert_replace(SqlDB *db, bool replace)
|
||||
goto error_hostname;
|
||||
}
|
||||
|
||||
to_xml(xml_body);
|
||||
sql_xml = db->escape_str(xml_body.c_str());
|
||||
sql_xml = db->escape_str(to_xml(xml_body).c_str());
|
||||
|
||||
if ( sql_xml == 0 )
|
||||
{
|
||||
@ -128,7 +127,7 @@ int Host::insert_replace(SqlDB *db, bool replace)
|
||||
|
||||
// Construct the SQL statement to Insert or Replace
|
||||
|
||||
oss <<" INTO "<< table <<" ("<< db_names <<") VALUES ("
|
||||
oss <<" INTO "<<table <<" ("<< db_names <<") VALUES ("
|
||||
<< oid << ","
|
||||
<< "'" << sql_hostname << "',"
|
||||
<< "'" << sql_xml << "',"
|
||||
|
@ -31,6 +31,7 @@ HostShare::HostShare(
|
||||
int _max_disk,
|
||||
int _max_mem,
|
||||
int _max_cpu):
|
||||
ObjectXML(),
|
||||
disk_usage(0),
|
||||
mem_usage(0),
|
||||
cpu_usage(0),
|
||||
@ -43,9 +44,7 @@ HostShare::HostShare(
|
||||
used_disk(0),
|
||||
used_mem(0),
|
||||
used_cpu(0),
|
||||
running_vms(0)
|
||||
{
|
||||
}
|
||||
running_vms(0){};
|
||||
|
||||
ostream& operator<<(ostream& os, HostShare& hs)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user