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

F #4809. Fix index in PoolSQL. Update interface in all classes

This commit is contained in:
Ruben S. Montero 2017-06-21 03:22:56 +02:00
parent 826cf76016
commit a2c5a4cbaa
20 changed files with 46 additions and 106 deletions

View File

@ -27,7 +27,7 @@ class DocumentPool : public PoolSQL
{ {
public: public:
DocumentPool(SqlDB * db) : PoolSQL(db, Document::table, true, false){}; DocumentPool(SqlDB * db) : PoolSQL(db, Document::table){};
~DocumentPool(){}; ~DocumentPool(){};

View File

@ -24,10 +24,7 @@ class SqlDB;
class MarketPlaceAppPool : public PoolSQL class MarketPlaceAppPool : public PoolSQL
{ {
public: public:
MarketPlaceAppPool( MarketPlaceAppPool(SqlDB * db):PoolSQL(db, MarketPlaceApp::table){};
SqlDB * db,
bool is_federation_slave)
:PoolSQL(db,MarketPlaceApp::table,!is_federation_slave,true){};
~MarketPlaceAppPool(){}; ~MarketPlaceAppPool(){};

View File

@ -45,10 +45,8 @@ public:
* @param _db a pointer to the database * @param _db a pointer to the database
* @param _table the name of the table supporting the pool (to set the oid * @param _table the name of the table supporting the pool (to set the oid
* counter). If null the OID counter is not updated. * counter). If null the OID counter is not updated.
* @param cache True to enable the cache
* @param cache_by_name True if the objects can be retrieved by name
*/ */
PoolSQL(SqlDB * _db, const char * _table, bool cache, bool cache_by_name); PoolSQL(SqlDB * _db, const char * _table);
virtual ~PoolSQL(); virtual ~PoolSQL();
@ -353,18 +351,7 @@ private:
* The pool is implemented with a Map of SQL object pointers, using the * The pool is implemented with a Map of SQL object pointers, using the
* OID as key. * OID as key.
*/ */
map<int,PoolObjectSQL *> pool; vector<PoolObjectSQL *> pool;
/**
* Whether or not this pool uses the name_pool index
*/
bool uses_name_pool;
/**
* This is a name index for the pool map. The key is the name of the object
* , that may be combained with the owner id.
*/
map<string,PoolObjectSQL *> name_pool;
/** /**
* Factory method, must return an ObjectSQL pointer to an allocated pool * Factory method, must return an ObjectSQL pointer to an allocated pool

View File

@ -25,7 +25,7 @@ class AuthRequest;
class VMGroupPool : public PoolSQL class VMGroupPool : public PoolSQL
{ {
public: public:
VMGroupPool(SqlDB * db):PoolSQL(db, VMGroup::table, true, true){}; VMGroupPool(SqlDB * db):PoolSQL(db, VMGroup::table){};
~VMGroupPool(){}; ~VMGroupPool(){};

View File

@ -27,7 +27,7 @@ class VMTemplatePool : public PoolSQL
{ {
public: public:
VMTemplatePool(SqlDB * db) : PoolSQL(db, VMTemplate::table, true, true){}; VMTemplatePool(SqlDB * db) : PoolSQL(db, VMTemplate::table){};
~VMTemplatePool(){}; ~VMTemplatePool(){};

View File

@ -28,7 +28,7 @@ class VirtualRouterPool : public PoolSQL
public: public:
VirtualRouterPool(SqlDB * db, vector<const VectorAttribute *> hook_mads, VirtualRouterPool(SqlDB * db, vector<const VectorAttribute *> hook_mads,
const string& remotes_location) : PoolSQL(db, VirtualRouter::table, true, true) const string& remotes_location) : PoolSQL(db, VirtualRouter::table)
{ {
register_hooks(hook_mads, remotes_location); register_hooks(hook_mads, remotes_location);
}; };

View File

@ -36,7 +36,7 @@ const int ClusterPool::DEFAULT_CLUSTER_ID = 0;
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
ClusterPool::ClusterPool(SqlDB * db, const VectorAttribute * _vnc_conf): ClusterPool::ClusterPool(SqlDB * db, const VectorAttribute * _vnc_conf):
PoolSQL(db, Cluster::table, true, true), vnc_conf(_vnc_conf) PoolSQL(db, Cluster::table), vnc_conf(_vnc_conf)
{ {
ostringstream oss; ostringstream oss;
string error_str; string error_str;

View File

@ -41,7 +41,7 @@ const int DatastorePool::FILE_DS_ID = 2;
DatastorePool::DatastorePool( DatastorePool::DatastorePool(
SqlDB * db, SqlDB * db,
const vector<const SingleAttribute *>& _inherit_attrs) : const vector<const SingleAttribute *>& _inherit_attrs) :
PoolSQL(db, Datastore::table, true, true) PoolSQL(db, Datastore::table)
{ {
ostringstream oss; ostringstream oss;

View File

@ -39,7 +39,7 @@ const int GroupPool::USERS_ID = 1;
GroupPool::GroupPool(SqlDB * db, vector<const VectorAttribute *> hook_mads, GroupPool::GroupPool(SqlDB * db, vector<const VectorAttribute *> hook_mads,
const string& remotes_location, bool is_federation_slave) : const string& remotes_location, bool is_federation_slave) :
PoolSQL(db, Group::table, !is_federation_slave, true) PoolSQL(db, Group::table)
{ {
ostringstream oss; ostringstream oss;
string error_str; string error_str;

View File

@ -41,7 +41,7 @@ HostPool::HostPool(SqlDB* db,
const string& hook_location, const string& hook_location,
const string& remotes_location, const string& remotes_location,
time_t expire_time) time_t expire_time)
: PoolSQL(db, Host::table, true, true) : PoolSQL(db, Host::table)
{ {
_monitor_expiration = expire_time; _monitor_expiration = expire_time;

View File

@ -42,7 +42,7 @@ ImagePool::ImagePool(
vector<const VectorAttribute *>& hook_mads, vector<const VectorAttribute *>& hook_mads,
const string& remotes_location, const string& remotes_location,
const vector<const SingleAttribute *>& _inherit_attrs) const vector<const SingleAttribute *>& _inherit_attrs)
:PoolSQL(db, Image::table, true, true) :PoolSQL(db, Image::table)
{ {
// Init static defaults // Init static defaults
_default_type = __default_type; _default_type = __default_type;

View File

@ -21,10 +21,8 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
MarketPlacePool::MarketPlacePool( MarketPlacePool::MarketPlacePool(SqlDB * db, bool is_federation_slave)
SqlDB * db, :PoolSQL(db, MarketPlace::table)
bool is_federation_slave)
:PoolSQL(db, MarketPlace::table, !is_federation_slave, true)
{ {
//Federation slaves do not need to init the pool //Federation slaves do not need to init the pool
if (is_federation_slave) if (is_federation_slave)

View File

@ -663,7 +663,7 @@ void Nebula::start(bool bootstrap_only)
secgrouppool = new SecurityGroupPool(logdb); secgrouppool = new SecurityGroupPool(logdb);
marketpool = new MarketPlacePool(db_ptr, is_federation_slave()); marketpool = new MarketPlacePool(db_ptr, is_federation_slave());
apppool = new MarketPlaceAppPool(db_ptr, is_federation_slave()); apppool = new MarketPlaceAppPool(db_ptr);
vmgrouppool = new VMGroupPool(logdb); vmgrouppool = new VMGroupPool(logdb);

View File

@ -87,8 +87,8 @@ void PoolSQL::set_lastOID(int _last_oid)
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
PoolSQL::PoolSQL(SqlDB * _db, const char * _table, bool _cache, bool by_name): PoolSQL::PoolSQL(SqlDB * _db, const char * _table):
db(_db), table(_table), uses_name_pool(by_name) db(_db), table(_table)
{ {
pthread_mutex_init(&mutex,0); pthread_mutex_init(&mutex,0);
}; };
@ -98,15 +98,15 @@ PoolSQL::PoolSQL(SqlDB * _db, const char * _table, bool _cache, bool by_name):
PoolSQL::~PoolSQL() PoolSQL::~PoolSQL()
{ {
map<int,PoolObjectSQL *>::iterator it; vector<PoolObjectSQL *>::iterator it;
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
for ( it = pool.begin(); it != pool.end(); it++) for ( it = pool.begin(); it != pool.end(); ++it)
{ {
it->second->lock(); (*it)->lock();
delete it->second; delete *it;
} }
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
@ -192,14 +192,7 @@ PoolObjectSQL * PoolSQL::get(int oid, bool olock)
return 0; return 0;
} }
pool.insert(make_pair(objectsql->oid, objectsql)); pool.push_back(objectsql);
if ( uses_name_pool )
{
string okey = key(objectsql->name, objectsql->uid);
name_pool.insert(make_pair(okey, objectsql));
}
if ( olock == true ) if ( olock == true )
{ {
@ -216,11 +209,6 @@ PoolObjectSQL * PoolSQL::get(int oid, bool olock)
PoolObjectSQL * PoolSQL::get(const string& name, int ouid, bool olock) PoolObjectSQL * PoolSQL::get(const string& name, int ouid, bool olock)
{ {
if ( uses_name_pool == false )
{
return 0;
}
lock(); lock();
string name_key = key(name, ouid); string name_key = key(name, ouid);
@ -242,9 +230,7 @@ PoolObjectSQL * PoolSQL::get(const string& name, int ouid, bool olock)
return 0; return 0;
} }
pool.insert(make_pair(objectsql->oid, objectsql)); pool.push_back(objectsql);
name_pool.insert(make_pair(name_key, objectsql));
if ( olock == true ) if ( olock == true )
{ {
@ -261,22 +247,17 @@ PoolObjectSQL * PoolSQL::get(const string& name, int ouid, bool olock)
void PoolSQL::flush_cache(int oid) void PoolSQL::flush_cache(int oid)
{ {
int rc; for (vector<PoolObjectSQL *>::iterator it = pool.begin(); it != pool.end();)
PoolObjectSQL * tmp_ptr;
map<int,PoolObjectSQL *>::iterator it;
for (it = pool.begin(); it != pool.end(); )
{ {
// The object we are looking for in ::get(). Wait until it is unlocked() // The object we are looking for in ::get(). Wait until it is unlocked()
if (it->first == oid) if ((*it)->oid == oid)
{ {
it->second->lock(); (*it)->lock();
} }
else else
{ {
// Any other locked object is just ignored // Any other locked object is just ignored
rc = pthread_mutex_trylock(&(it->second->mutex)); int rc = pthread_mutex_trylock(&((*it)->mutex));
if ( rc == EBUSY ) // In use by other thread if ( rc == EBUSY ) // In use by other thread
{ {
@ -285,19 +266,9 @@ void PoolSQL::flush_cache(int oid)
} }
} }
tmp_ptr = it->second; delete *it;
// map::erase does not invalidate iterator, except for the current one
it = pool.erase(it); it = pool.erase(it);
if ( uses_name_pool )
{
string okey = key(tmp_ptr->name, tmp_ptr->uid);
name_pool.erase(okey);
}
delete tmp_ptr;
} }
} }
@ -306,22 +277,19 @@ void PoolSQL::flush_cache(int oid)
void PoolSQL::flush_cache(const string& name_key) void PoolSQL::flush_cache(const string& name_key)
{ {
int rc; for (vector<PoolObjectSQL *>::iterator it = pool.begin(); it != pool.end();)
PoolObjectSQL * tmp_ptr;
map<string,PoolObjectSQL *>::iterator it;
for (it = name_pool.begin(); it != name_pool.end(); )
{ {
string okey = key((*it)->name, (*it)->uid);
// The object we are looking for in ::get(). Wait until it is unlocked() // The object we are looking for in ::get(). Wait until it is unlocked()
if (name_key == it->first) if ( name_key == okey)
{ {
it->second->lock(); (*it)->lock();
} }
else else
{ {
// Any other locked object is just ignored // Any other locked object is just ignored
rc = pthread_mutex_trylock(&(it->second->mutex)); int rc = pthread_mutex_trylock(&((*it)->mutex));
if ( rc == EBUSY ) // In use by other thread if ( rc == EBUSY ) // In use by other thread
{ {
@ -330,14 +298,9 @@ void PoolSQL::flush_cache(const string& name_key)
} }
} }
tmp_ptr = it->second; delete *it;
// map::erase does not invalidate iterator, except for the current one it = pool.erase(it);
it = name_pool.erase(it);
pool.erase(tmp_ptr->oid);
delete tmp_ptr;
} }
} }
@ -346,21 +309,19 @@ void PoolSQL::flush_cache(const string& name_key)
void PoolSQL::clean() void PoolSQL::clean()
{ {
map<int,PoolObjectSQL *>::iterator it; vector<PoolObjectSQL *>::iterator it;
lock(); lock();
for ( it = pool.begin(); it != pool.end(); it++) for (it = pool.begin(); it != pool.end(); ++it)
{ {
it->second->lock(); (*it)->lock();
delete it->second; delete *it;
} }
pool.clear(); pool.clear();
name_pool.clear();
unlock(); unlock();
} }

View File

@ -21,8 +21,7 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
SecurityGroupPool::SecurityGroupPool(SqlDB * db) SecurityGroupPool::SecurityGroupPool(SqlDB * db):PoolSQL(db,SecurityGroup::table)
:PoolSQL(db, SecurityGroup::table, true, true)
{ {
//lastOID is set in PoolSQL::init_cb //lastOID is set in PoolSQL::init_cb
if (get_lastOID() == -1) if (get_lastOID() == -1)

View File

@ -56,7 +56,7 @@ UserPool::UserPool(SqlDB * db,
vector<const VectorAttribute *> hook_mads, vector<const VectorAttribute *> hook_mads,
const string& remotes_location, const string& remotes_location,
bool is_federation_slave): bool is_federation_slave):
PoolSQL(db, User::table, !is_federation_slave, true) PoolSQL(db, User::table)
{ {
int one_uid = -1; int one_uid = -1;
int server_uid = -1; int server_uid = -1;

View File

@ -25,8 +25,7 @@ const int VdcPool::DEFAULT_ID = 0;
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
VdcPool::VdcPool(SqlDB * db, bool is_federation_slave) VdcPool::VdcPool(SqlDB * db, bool is_federation_slave): PoolSQL(db, Vdc::table)
:PoolSQL(db, Vdc::table, !is_federation_slave, true)
{ {
string error_str; string error_str;

View File

@ -47,7 +47,7 @@ VirtualMachinePool::VirtualMachinePool(
float default_cpu_cost, float default_cpu_cost,
float default_mem_cost, float default_mem_cost,
float default_disk_cost) float default_disk_cost)
: PoolSQL(db, VirtualMachine::table, true, false), : PoolSQL(db, VirtualMachine::table),
_monitor_expiration(expire_time), _submit_on_hold(on_hold), _monitor_expiration(expire_time), _submit_on_hold(on_hold),
_default_cpu_cost(default_cpu_cost), _default_mem_cost(default_mem_cost), _default_cpu_cost(default_cpu_cost), _default_mem_cost(default_mem_cost),
_default_disk_cost(default_disk_cost) _default_disk_cost(default_disk_cost)

View File

@ -48,7 +48,7 @@ VirtualNetworkPool::VirtualNetworkPool(
const vector<const SingleAttribute *>& _inherit_attrs, const vector<const SingleAttribute *>& _inherit_attrs,
const VectorAttribute * _vlan_conf, const VectorAttribute * _vlan_conf,
const VectorAttribute * _vxlan_conf): const VectorAttribute * _vxlan_conf):
PoolSQL(db, VirtualNetwork::table, true, true), vlan_conf(_vlan_conf), PoolSQL(db, VirtualNetwork::table), vlan_conf(_vlan_conf),
vlan_id_bitmap(vlan_conf, VLAN_BITMAP_ID, vlan_table), vlan_id_bitmap(vlan_conf, VLAN_BITMAP_ID, vlan_table),
vxlan_conf(_vxlan_conf) vxlan_conf(_vxlan_conf)
{ {

View File

@ -28,8 +28,7 @@ const int ZonePool::STANDALONE_ZONE_ID = 0;
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
ZonePool::ZonePool(SqlDB * db, bool is_federation_slave) ZonePool::ZonePool(SqlDB * db, bool is_federation_slave):PoolSQL(db,Zone::table)
:PoolSQL(db, Zone::table, !is_federation_slave, true)
{ {
string error_str; string error_str;