1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-10 01:17:40 +03:00

feature #206: VirtualMachine uses SqlDB

This commit is contained in:
Ruben S. Montero 2010-04-10 22:16:47 +02:00
parent aeb27c1004
commit ce7d0681e3
6 changed files with 277 additions and 365 deletions

View File

@ -17,17 +17,10 @@
#ifndef HISTORY_H_ #ifndef HISTORY_H_
#define HISTORY_H_ #define HISTORY_H_
#include <sqlite3.h>
#include "ObjectSQL.h" #include "ObjectSQL.h"
using namespace std; using namespace std;
extern "C" int history_select_cb (
void * _history,
int num,
char ** values,
char ** names);
/** /**
* The History class, it represents an execution record of a Virtual Machine. * The History class, it represents an execution record of a Virtual Machine.
*/ */
@ -80,6 +73,7 @@ public:
private: private:
friend class VirtualMachine; friend class VirtualMachine;
friend class VirtualMachinePool;
// ---------------------------------------- // ----------------------------------------
// DataBase implementation variables // DataBase implementation variables
@ -166,25 +160,19 @@ private:
string checkpoint_file; string checkpoint_file;
string rdeployment_file; string rdeployment_file;
friend int history_select_cb (
void * _history,
int num,
char ** values,
char ** names);
/** /**
* Writes the history record in the DB * Writes the history record in the DB
* @param db pointer to the database. * @param db pointer to the database.
* @return 0 on success. * @return 0 on success.
*/ */
int insert(SqliteDB * db); int insert(SqlDB * db);
/** /**
* Reads the history record from the DB * Reads the history record from the DB
* @param db pointer to the database. * @param db pointer to the database.
* @return 0 on success. * @return 0 on success.
*/ */
int select(SqliteDB * db); int select(SqlDB * db);
/** /**
* Removes the all history records from the DB * Removes the all history records from the DB
@ -192,26 +180,26 @@ private:
* @return 0 on success. * @return 0 on success.
*/ */
int drop(SqliteDB * db); int drop(SqlDB * db);
/** /**
* Updates the history record * Updates the history record
* @param db pointer to the database. * @param db pointer to the database.
* @return 0 on success. * @return 0 on success.
*/ */
int update(SqliteDB * db) int update(SqlDB * db)
{ {
return insert(db); return insert(db);
} }
/** /**
* Function to unmarshall a history object * Callback function to unmarshall a history object (History::select)
* @param num the number of columns read from the DB * @param num the number of columns read from the DB
* @para names the column names * @para names the column names
* @para vaues the column values * @para vaues the column values
* @return 0 on success * @return 0 on success
*/ */
int unmarshall(int num, char **names, char ** values); int select_cb(void *nil, int num, char **values, char **names);
/** /**
* Function to unmarshall a History object into an output stream with XML * Function to unmarshall a History object into an output stream with XML
@ -222,10 +210,7 @@ private:
* @param vaues the column values * @param vaues the column values
* @return 0 on success * @return 0 on success
*/ */
static int unmarshall(ostringstream& oss, static int dump(ostringstream& oss, int num, char **names, char **values);
int num,
char ** names,
char ** values);
}; };
#endif /*HISTORY_H_*/ #endif /*HISTORY_H_*/

View File

@ -27,9 +27,6 @@
using namespace std; using namespace std;
extern "C" int vm_select_cb (void * _vm, int num,char ** values, char ** names);
extern "C" int vm_dump_cb (void * _oss, int num,char ** values, char ** names);
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -747,18 +744,6 @@ private:
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
friend class VirtualMachinePool; friend class VirtualMachinePool;
friend int vm_select_cb (
void * _vm,
int num,
char ** values,
char ** names);
friend int vm_dump_cb (
void * _vm,
int num,
char ** values,
char ** names);
// ************************************************************************* // *************************************************************************
// Virtual Machine Attributes // Virtual Machine Attributes
// ************************************************************************* // *************************************************************************
@ -870,44 +855,33 @@ private:
/** /**
* Bootstraps the database table(s) associated to the VirtualMachine * Bootstraps the database table(s) associated to the VirtualMachine
*/ */
static void bootstrap(SqliteDB * db) static void bootstrap(SqlDB * db)
{ {
db->exec(VirtualMachine::db_bootstrap); ostringstream oss_vm(VirtualMachine::db_bootstrap);
ostringstream oss_tmpl(VirtualMachineTemplate::db_bootstrap);
ostringstream oss_hist(History::db_bootstrap);
db->exec(VirtualMachineTemplate::db_bootstrap); db->exec(oss_vm);
db->exec(oss_tmpl);
db->exec(History::db_bootstrap); db->exec(oss_hist);
}; };
/** /**
* Function to unmarshall a VM object, an associated classes. * Callback function to unmarshall a VirtualMachine object
* (VirtualMachine::select)
* @param num the number of columns read from the DB * @param num the number of columns read from the DB
* @param names the column names * @param names the column names
* @param vaues the column values * @param vaues the column values
* @return 0 on success * @return 0 on success
*/ */
int unmarshall(int num, char **names, char ** values); int select_cb(void *nil, int num, char **names, char ** values);
/**
* Function to unmarshall a VM object into an output stream with XML
* format.
* @param oss the output stream
* @param num the number of columns read from the DB
* @param names the column names
* @param vaues the column values
* @return 0 on success
*/
static int unmarshall(ostringstream& oss,
int num,
char ** names,
char ** values);
/** /**
* Updates the VM history record * Updates the VM history record
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return 0 on success
*/ */
int update_history(SqliteDB * db) int update_history(SqlDB * db)
{ {
if ( history != 0 ) if ( history != 0 )
{ {
@ -922,7 +896,7 @@ private:
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return 0 on success
*/ */
int update_previous_history(SqliteDB * db) int update_previous_history(SqlDB * db)
{ {
if ( previous_history != 0 ) if ( previous_history != 0 )
{ {
@ -941,7 +915,7 @@ private:
* @return 0 on success * @return 0 on success
*/ */
int update_template_attribute( int update_template_attribute(
SqliteDB * db, SqlDB * db,
string& name, string& name,
string& value) string& value)
{ {
@ -966,7 +940,7 @@ private:
* @param attribute the new attribute for the template * @param attribute the new attribute for the template
* @return 0 on success * @return 0 on success
*/ */
int insert_template_attribute(SqliteDB * db, Attribute * attribute) int insert_template_attribute(SqlDB * db, Attribute * attribute)
{ {
return vm_template.insert_attribute(db,attribute); return vm_template.insert_attribute(db,attribute);
} }
@ -1042,21 +1016,21 @@ protected:
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return 0 on success
*/ */
int select(SqliteDB * db); int select(SqlDB * db);
/** /**
* Writes the Virtual Machine and its associated template in the database. * Writes the Virtual Machine and its associated template in the database.
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return 0 on success
*/ */
virtual int insert(SqliteDB * db); virtual int insert(SqlDB * db);
/** /**
* Writes/updates the Virtual Machine data fields in the database. * Writes/updates the Virtual Machine data fields in the database.
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return 0 on success
*/ */
virtual int update(SqliteDB * db); virtual int update(SqlDB * db);
/** /**
* Deletes a VM from the database and all its associated information: * Deletes a VM from the database and all its associated information:
@ -1065,7 +1039,7 @@ protected:
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return 0 on success
*/ */
virtual int drop(SqliteDB * db) virtual int drop(SqlDB * db)
{ {
int rc; int rc;
@ -1082,12 +1056,13 @@ protected:
/** /**
* Dumps the contect of a set of VirtualMachine objects in the given stream * Dumps the contect of a set of VirtualMachine objects in the given stream
* using XML format * using XML format
* @param db pointer to the db
* @param oss the output stream * @param oss the output stream
* @param where string to filter the VirtualMachine objects * @param num the number of columns read from the DB
* @param names the column names
* @param vaues the column values
* @return 0 on success * @return 0 on success
*/ */
static int dump(SqliteDB * db, ostringstream& oss, const string& where); static int dump(ostringstream& oss, int num, char ** values, char ** names);
}; };
#endif /*VIRTUAL_MACHINE_H_*/ #endif /*VIRTUAL_MACHINE_H_*/

View File

@ -32,7 +32,7 @@ class VirtualMachinePool : public PoolSQL
{ {
public: public:
VirtualMachinePool(SqliteDB * db, vector<const Attribute *> hook_mads); VirtualMachinePool(SqlDB * db, vector<const Attribute *> hook_mads);
~VirtualMachinePool(){}; ~VirtualMachinePool(){};
@ -126,7 +126,7 @@ public:
/** /**
* Bootstraps the database table(s) associated to the VirtualMachine pool * Bootstraps the database table(s) associated to the VirtualMachine pool
*/ */
static void bootstrap(SqliteDB * _db) static void bootstrap(SqlDB * _db)
{ {
VirtualMachine::bootstrap(_db); VirtualMachine::bootstrap(_db);
}; };
@ -140,18 +140,7 @@ public:
* *
* @return 0 on success * @return 0 on success
*/ */
int dump(ostringstream& oss, const string& where) int dump(ostringstream& oss, const string& where);
{
int rc;
oss << "<VM_POOL>";
rc = VirtualMachine::dump(db,oss,where);
oss << "</VM_POOL>";
return rc;
}
private: private:
/** /**
@ -169,6 +158,16 @@ private:
{ {
return new VirtualMachine; return new VirtualMachine;
}; };
/**
* Callback function to get output the vm pool in XML format
* (VirtualMachinePool::dump)
* @param num the number of columns read from the DB
* @param names the column names
* @param vaues the column values
* @return 0 on success
*/
int dump_cb(void * _oss, int num, char **values, char **names);
}; };
#endif /*VIRTUAL_MACHINE_POOL_H_*/ #endif /*VIRTUAL_MACHINE_POOL_H_*/

View File

@ -134,7 +134,7 @@ void History::non_persistent_data()
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int History::insert(SqliteDB * db) int History::insert(SqlDB * db)
{ {
ostringstream oss; ostringstream oss;
@ -150,28 +150,28 @@ int History::insert(SqliteDB * db)
return 0; return 0;
} }
sql_hostname = sqlite3_mprintf("%q",hostname.c_str()); sql_hostname = db->escape_str(hostname.c_str());
if ( sql_hostname == 0 ) if ( sql_hostname == 0 )
{ {
goto error_hostname; goto error_hostname;
} }
sql_vm_dir = sqlite3_mprintf("%q",vm_dir.c_str()); sql_vm_dir = db->escape_str(vm_dir.c_str());
if ( sql_vm_dir == 0 ) if ( sql_vm_dir == 0 )
{ {
goto error_vm_dir; goto error_vm_dir;
} }
sql_vmm_mad_name = sqlite3_mprintf("%q",vmm_mad_name.c_str()); sql_vmm_mad_name = db->escape_str(vmm_mad_name.c_str());
if ( sql_vmm_mad_name == 0 ) if ( sql_vmm_mad_name == 0 )
{ {
goto error_vmm; goto error_vmm;
} }
sql_tm_mad_name = sqlite3_mprintf("%q",tm_mad_name.c_str()); sql_tm_mad_name = db->escape_str(tm_mad_name.c_str());
if ( sql_tm_mad_name == 0 ) if ( sql_tm_mad_name == 0 )
{ {
@ -198,19 +198,19 @@ int History::insert(SqliteDB * db)
rc = db->exec(oss); rc = db->exec(oss);
sqlite3_free(sql_hostname); db->free_str(sql_hostname);
sqlite3_free(sql_vm_dir); db->free_str(sql_vm_dir);
sqlite3_free(sql_vmm_mad_name); db->free_str(sql_vmm_mad_name);
sqlite3_free(sql_tm_mad_name); db->free_str(sql_tm_mad_name);
return rc; return rc;
error_tm: error_tm:
sqlite3_free(sql_vmm_mad_name); db->free_str(sql_vmm_mad_name);
error_vmm: error_vmm:
sqlite3_free(sql_vm_dir); db->free_str(sql_vm_dir);
error_vm_dir: error_vm_dir:
sqlite3_free(sql_hostname); db->free_str(sql_hostname);
error_hostname: error_hostname:
return -1; return -1;
} }
@ -218,7 +218,7 @@ error_hostname:
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int History::unmarshall(int num, char **names, char ** values) int History::select_cb(void *nil, int num, char **values, char **names)
{ {
if ((!values[VID]) || if ((!values[VID]) ||
(!values[SEQ]) || (!values[SEQ]) ||
@ -272,10 +272,7 @@ int History::unmarshall(int num, char **names, char ** values)
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int History::unmarshall(ostringstream& oss, int History::dump(ostringstream& oss, int num, char **values, char **names)
int num,
char ** names,
char ** values)
{ {
if ((!values[VID])|| if ((!values[VID])||
(!values[SEQ])|| (!values[SEQ])||
@ -314,30 +311,9 @@ int History::unmarshall(ostringstream& oss,
return 0; return 0;
} }
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
extern "C" int history_select_cb ( int History::select(SqlDB * db)
void * _history,
int num,
char ** values,
char ** names)
{
History * history;
history = static_cast<History *>(_history);
if (history == 0)
{
return -1;
}
return history->unmarshall(num,names,values);
};
/* -------------------------------------------------------------------------- */
int History::select(SqliteDB * db)
{ {
ostringstream oss; ostringstream oss;
int rc; int rc;
@ -357,7 +333,9 @@ int History::select(SqliteDB * db)
oss << "SELECT * FROM history WHERE vid = "<< oid <<" AND seq = "<< seq; oss << "SELECT * FROM history WHERE vid = "<< oid <<" AND seq = "<< seq;
} }
rc = db->exec(oss,history_select_cb,(void *) this); set_callback(static_cast<Callbackable::Callback>(&History::select_cb));
rc = db->exec(oss,this);
if ( rc == 0 ) // Regenerate non-persistent data if ( rc == 0 ) // Regenerate non-persistent data
{ {
@ -370,7 +348,7 @@ int History::select(SqliteDB * db)
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int History::drop(SqliteDB * db) int History::drop(SqlDB * db)
{ {
ostringstream oss; ostringstream oss;

View File

@ -87,7 +87,7 @@ const char * VirtualMachine::db_bootstrap = "CREATE TABLE vm_pool ("
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int VirtualMachine::unmarshall(int num, char **names, char ** values) int VirtualMachine::select_cb(void *nil, int num, char **values, char **names)
{ {
if ((values[OID] == 0) || if ((values[OID] == 0) ||
(values[UID] == 0) || (values[UID] == 0) ||
@ -134,30 +134,9 @@ int VirtualMachine::unmarshall(int num, char **names, char ** values)
return 0; return 0;
} }
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
extern "C" int vm_select_cb ( int VirtualMachine::select(SqlDB * db)
void * _vm,
int num,
char ** values,
char ** names)
{
VirtualMachine * vm;
vm = static_cast<VirtualMachine *>(_vm);
if (vm == 0)
{
return -1;
}
return vm->unmarshall(num,names,values);
};
/* -------------------------------------------------------------------------- */
int VirtualMachine::select(SqliteDB * db)
{ {
ostringstream oss; ostringstream oss;
ostringstream ose; ostringstream ose;
@ -168,13 +147,15 @@ int VirtualMachine::select(SqliteDB * db)
string filename; string filename;
Nebula& nd = Nebula::instance(); Nebula& nd = Nebula::instance();
set_callback(
static_cast<Callbackable::Callback>(&VirtualMachine::select_cb));
oss << "SELECT * FROM " << table << " WHERE oid = " << oid; oss << "SELECT * FROM " << table << " WHERE oid = " << oid;
boid = oid; boid = oid;
oid = -1; oid = -1;
rc = db->exec(oss,vm_select_cb,(void *) this); rc = db->exec(oss,this);
if ((rc != 0) || (oid != boid )) if ((rc != 0) || (oid != boid ))
{ {
@ -267,7 +248,7 @@ error_previous_history:
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int VirtualMachine::insert(SqliteDB * db) int VirtualMachine::insert(SqlDB * db)
{ {
int rc; int rc;
string name; string name;
@ -351,7 +332,7 @@ error_leases:
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int VirtualMachine::update(SqliteDB * db) int VirtualMachine::update(SqlDB * db)
{ {
ostringstream oss; ostringstream oss;
int rc; int rc;
@ -359,18 +340,18 @@ int VirtualMachine::update(SqliteDB * db)
char * sql_deploy_id; char * sql_deploy_id;
char * sql_name; char * sql_name;
sql_deploy_id = sqlite3_mprintf("%q",deploy_id.c_str()); sql_deploy_id = db->escape_str(deploy_id.c_str());
if ( sql_deploy_id == 0 ) if ( sql_deploy_id == 0 )
{ {
return -1; return -1;
} }
sql_name = sqlite3_mprintf("%q",name.c_str()); sql_name = db->escape_str(name.c_str());
if ( sql_name == 0 ) if ( sql_name == 0 )
{ {
sqlite3_free(sql_deploy_id); db->free_str(sql_deploy_id);
return -1; return -1;
} }
@ -390,8 +371,8 @@ int VirtualMachine::update(SqliteDB * db)
net_tx << "," << net_tx << "," <<
net_rx << ")"; net_rx << ")";
sqlite3_free(sql_deploy_id); db->free_str(sql_deploy_id);
sqlite3_free(sql_name); db->free_str(sql_name);
rc = db->exec(oss); rc = db->exec(oss);
@ -401,10 +382,7 @@ int VirtualMachine::update(SqliteDB * db)
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int VirtualMachine::unmarshall(ostringstream& oss, int VirtualMachine::dump(ostringstream& oss,int num,char **values,char **names)
int num,
char ** names,
char ** values)
{ {
if ((!values[OID])|| if ((!values[OID])||
(!values[UID])|| (!values[UID])||
@ -441,62 +419,13 @@ int VirtualMachine::unmarshall(ostringstream& oss,
"<NET_TX>" << values[NET_TX] << "</NET_TX>" << "<NET_TX>" << values[NET_TX] << "</NET_TX>" <<
"<NET_RX>" << values[NET_RX] << "</NET_RX>"; "<NET_RX>" << values[NET_RX] << "</NET_RX>";
History::unmarshall(oss, num-LIMIT-2, names+LIMIT+1, values+LIMIT+1); History::dump(oss, num-LIMIT-2, values+LIMIT+1, names+LIMIT+1);
oss << "</VM>"; oss << "</VM>";
return 0; return 0;
} }
/* -------------------------------------------------------------------------- */
extern "C" int vm_dump_cb (
void * _oss,
int num,
char ** values,
char ** names)
{
ostringstream * oss;
ostringstream dbg;
oss = static_cast<ostringstream *>(_oss);
if (oss == 0)
{
return -1;
}
return VirtualMachine::unmarshall(*oss,num,names,values);
};
/* -------------------------------------------------------------------------- */
int VirtualMachine::dump(SqliteDB * db, ostringstream& oss, const string& where)
{
int rc;
ostringstream cmd;
cmd << "SELECT " << VirtualMachine::table << ".*, "
<< "user_pool.user_name, " << History::table << ".* FROM "
<< VirtualMachine::table
<< " LEFT OUTER JOIN (SELECT *,MAX(seq) FROM "
<< History::table << " GROUP BY vid) AS " << History::table
<< " ON " << VirtualMachine::table << ".oid = "
<< History::table << ".vid LEFT OUTER JOIN (SELECT oid,user_name FROM "
<< "user_pool) AS user_pool ON "
<< VirtualMachine::table << ".uid = user_pool.oid WHERE "
<< VirtualMachine::table << ".state != " << VirtualMachine::DONE;
if ( !where.empty() )
{
cmd << " AND " << where;
}
rc = db->exec(cmd,vm_dump_cb,(void *) &oss);
return rc;
}
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */

View File

@ -22,7 +22,7 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
VirtualMachinePool::VirtualMachinePool(SqliteDB * db, VirtualMachinePool::VirtualMachinePool(SqlDB * db,
vector<const Attribute *> hook_mads) vector<const Attribute *> hook_mads)
: PoolSQL(db,VirtualMachine::table) : PoolSQL(db,VirtualMachine::table)
{ {
@ -331,4 +331,50 @@ void VirtualMachinePool::generate_context(int vm_id, Attribute * attr)
vm->unlock(); vm->unlock();
} }
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachinePool::dump_cb(void * _oss,int num,char **values,char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
return VirtualMachine::dump(*oss, num, values, names);
}
/* -------------------------------------------------------------------------- */
int VirtualMachinePool::dump(ostringstream& oss, const string& where)
{
int rc;
ostringstream cmd;
oss << "<VM_POOL>";
set_callback(
static_cast<Callbackable::Callback>(&VirtualMachinePool::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT " << VirtualMachine::table << ".*, "
<< "user_pool.user_name, " << History::table << ".* FROM "
<< VirtualMachine::table
<< " LEFT OUTER JOIN (SELECT *,MAX(seq) FROM "
<< History::table << " GROUP BY vid) AS " << History::table
<< " ON " << VirtualMachine::table << ".oid = "
<< History::table << ".vid LEFT OUTER JOIN (SELECT oid,user_name FROM "
<< "user_pool) AS user_pool ON "
<< VirtualMachine::table << ".uid = user_pool.oid WHERE "
<< VirtualMachine::table << ".state != " << VirtualMachine::DONE;
if ( !where.empty() )
{
cmd << " AND " << where;
}
rc = db->exec(cmd,this);
oss << "</VM_POOL>";
return rc;
}