From a2c5a4cbaaa3290ca208ad2954c5967d0a504d65 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 21 Jun 2017 03:22:56 +0200 Subject: [PATCH] F #4809. Fix index in PoolSQL. Update interface in all classes --- include/DocumentPool.h | 2 +- include/MarketPlaceAppPool.h | 5 +- include/PoolSQL.h | 17 +----- include/VMGroupPool.h | 2 +- include/VMTemplatePool.h | 2 +- include/VirtualRouterPool.h | 2 +- src/cluster/ClusterPool.cc | 2 +- src/datastore/DatastorePool.cc | 2 +- src/group/GroupPool.cc | 2 +- src/host/HostPool.cc | 2 +- src/image/ImagePool.cc | 2 +- src/market/MarketPlacePool.cc | 6 +-- src/nebula/Nebula.cc | 2 +- src/pool/PoolSQL.cc | 89 +++++++++---------------------- src/secgroup/SecurityGroupPool.cc | 3 +- src/um/UserPool.cc | 2 +- src/vdc/VdcPool.cc | 3 +- src/vm/VirtualMachinePool.cc | 2 +- src/vnm/VirtualNetworkPool.cc | 2 +- src/zone/ZonePool.cc | 3 +- 20 files changed, 46 insertions(+), 106 deletions(-) diff --git a/include/DocumentPool.h b/include/DocumentPool.h index cf673d2b6f..0ea34d99b1 100644 --- a/include/DocumentPool.h +++ b/include/DocumentPool.h @@ -27,7 +27,7 @@ class DocumentPool : public PoolSQL { public: - DocumentPool(SqlDB * db) : PoolSQL(db, Document::table, true, false){}; + DocumentPool(SqlDB * db) : PoolSQL(db, Document::table){}; ~DocumentPool(){}; diff --git a/include/MarketPlaceAppPool.h b/include/MarketPlaceAppPool.h index a8baf229b2..bb39389057 100644 --- a/include/MarketPlaceAppPool.h +++ b/include/MarketPlaceAppPool.h @@ -24,10 +24,7 @@ class SqlDB; class MarketPlaceAppPool : public PoolSQL { public: - MarketPlaceAppPool( - SqlDB * db, - bool is_federation_slave) - :PoolSQL(db,MarketPlaceApp::table,!is_federation_slave,true){}; + MarketPlaceAppPool(SqlDB * db):PoolSQL(db, MarketPlaceApp::table){}; ~MarketPlaceAppPool(){}; diff --git a/include/PoolSQL.h b/include/PoolSQL.h index 3e97bdc243..2e7a0b1d61 100644 --- a/include/PoolSQL.h +++ b/include/PoolSQL.h @@ -45,10 +45,8 @@ public: * @param _db a pointer to the database * @param _table the name of the table supporting the pool (to set the oid * 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(); @@ -353,18 +351,7 @@ private: * The pool is implemented with a Map of SQL object pointers, using the * OID as key. */ - map 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 name_pool; + vector pool; /** * Factory method, must return an ObjectSQL pointer to an allocated pool diff --git a/include/VMGroupPool.h b/include/VMGroupPool.h index bfeb6fbba0..150720d07d 100644 --- a/include/VMGroupPool.h +++ b/include/VMGroupPool.h @@ -25,7 +25,7 @@ class AuthRequest; class VMGroupPool : public PoolSQL { public: - VMGroupPool(SqlDB * db):PoolSQL(db, VMGroup::table, true, true){}; + VMGroupPool(SqlDB * db):PoolSQL(db, VMGroup::table){}; ~VMGroupPool(){}; diff --git a/include/VMTemplatePool.h b/include/VMTemplatePool.h index 58e9c89815..ad8a0c5b7a 100644 --- a/include/VMTemplatePool.h +++ b/include/VMTemplatePool.h @@ -27,7 +27,7 @@ class VMTemplatePool : public PoolSQL { public: - VMTemplatePool(SqlDB * db) : PoolSQL(db, VMTemplate::table, true, true){}; + VMTemplatePool(SqlDB * db) : PoolSQL(db, VMTemplate::table){}; ~VMTemplatePool(){}; diff --git a/include/VirtualRouterPool.h b/include/VirtualRouterPool.h index 040035098b..a16c64a7c2 100644 --- a/include/VirtualRouterPool.h +++ b/include/VirtualRouterPool.h @@ -28,7 +28,7 @@ class VirtualRouterPool : public PoolSQL public: VirtualRouterPool(SqlDB * db, vector 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); }; diff --git a/src/cluster/ClusterPool.cc b/src/cluster/ClusterPool.cc index c23276934c..e34bc4e333 100644 --- a/src/cluster/ClusterPool.cc +++ b/src/cluster/ClusterPool.cc @@ -36,7 +36,7 @@ const int ClusterPool::DEFAULT_CLUSTER_ID = 0; /* -------------------------------------------------------------------------- */ 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; string error_str; diff --git a/src/datastore/DatastorePool.cc b/src/datastore/DatastorePool.cc index 353d250df8..85ef24459a 100644 --- a/src/datastore/DatastorePool.cc +++ b/src/datastore/DatastorePool.cc @@ -41,7 +41,7 @@ const int DatastorePool::FILE_DS_ID = 2; DatastorePool::DatastorePool( SqlDB * db, const vector& _inherit_attrs) : - PoolSQL(db, Datastore::table, true, true) + PoolSQL(db, Datastore::table) { ostringstream oss; diff --git a/src/group/GroupPool.cc b/src/group/GroupPool.cc index bada8ddcf9..4e8679a7b8 100644 --- a/src/group/GroupPool.cc +++ b/src/group/GroupPool.cc @@ -39,7 +39,7 @@ const int GroupPool::USERS_ID = 1; GroupPool::GroupPool(SqlDB * db, vector hook_mads, const string& remotes_location, bool is_federation_slave) : - PoolSQL(db, Group::table, !is_federation_slave, true) + PoolSQL(db, Group::table) { ostringstream oss; string error_str; diff --git a/src/host/HostPool.cc b/src/host/HostPool.cc index d6127ba564..4174fc4b86 100644 --- a/src/host/HostPool.cc +++ b/src/host/HostPool.cc @@ -41,7 +41,7 @@ HostPool::HostPool(SqlDB* db, const string& hook_location, const string& remotes_location, time_t expire_time) - : PoolSQL(db, Host::table, true, true) + : PoolSQL(db, Host::table) { _monitor_expiration = expire_time; diff --git a/src/image/ImagePool.cc b/src/image/ImagePool.cc index 5b8edc8888..f57f3eab53 100644 --- a/src/image/ImagePool.cc +++ b/src/image/ImagePool.cc @@ -42,7 +42,7 @@ ImagePool::ImagePool( vector& hook_mads, const string& remotes_location, const vector& _inherit_attrs) - :PoolSQL(db, Image::table, true, true) + :PoolSQL(db, Image::table) { // Init static defaults _default_type = __default_type; diff --git a/src/market/MarketPlacePool.cc b/src/market/MarketPlacePool.cc index f118a60cc3..0864394224 100644 --- a/src/market/MarketPlacePool.cc +++ b/src/market/MarketPlacePool.cc @@ -21,10 +21,8 @@ /* -------------------------------------------------------------------------- */ -MarketPlacePool::MarketPlacePool( - SqlDB * db, - bool is_federation_slave) - :PoolSQL(db, MarketPlace::table, !is_federation_slave, true) +MarketPlacePool::MarketPlacePool(SqlDB * db, bool is_federation_slave) + :PoolSQL(db, MarketPlace::table) { //Federation slaves do not need to init the pool if (is_federation_slave) diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc index 0e69fbe131..77db5129c1 100644 --- a/src/nebula/Nebula.cc +++ b/src/nebula/Nebula.cc @@ -663,7 +663,7 @@ void Nebula::start(bool bootstrap_only) secgrouppool = new SecurityGroupPool(logdb); 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); diff --git a/src/pool/PoolSQL.cc b/src/pool/PoolSQL.cc index ca7dcae3ed..8730e64f6b 100644 --- a/src/pool/PoolSQL.cc +++ b/src/pool/PoolSQL.cc @@ -87,8 +87,8 @@ void PoolSQL::set_lastOID(int _last_oid) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -PoolSQL::PoolSQL(SqlDB * _db, const char * _table, bool _cache, bool by_name): - db(_db), table(_table), uses_name_pool(by_name) +PoolSQL::PoolSQL(SqlDB * _db, const char * _table): + db(_db), table(_table) { pthread_mutex_init(&mutex,0); }; @@ -98,15 +98,15 @@ PoolSQL::PoolSQL(SqlDB * _db, const char * _table, bool _cache, bool by_name): PoolSQL::~PoolSQL() { - map::iterator it; + vector::iterator it; 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); @@ -192,14 +192,7 @@ PoolObjectSQL * PoolSQL::get(int oid, bool olock) return 0; } - pool.insert(make_pair(objectsql->oid, objectsql)); - - if ( uses_name_pool ) - { - string okey = key(objectsql->name, objectsql->uid); - - name_pool.insert(make_pair(okey, objectsql)); - } + pool.push_back(objectsql); if ( olock == true ) { @@ -216,11 +209,6 @@ PoolObjectSQL * PoolSQL::get(int oid, bool olock) PoolObjectSQL * PoolSQL::get(const string& name, int ouid, bool olock) { - if ( uses_name_pool == false ) - { - return 0; - } - lock(); string name_key = key(name, ouid); @@ -242,9 +230,7 @@ PoolObjectSQL * PoolSQL::get(const string& name, int ouid, bool olock) return 0; } - pool.insert(make_pair(objectsql->oid, objectsql)); - - name_pool.insert(make_pair(name_key, objectsql)); + pool.push_back(objectsql); if ( olock == true ) { @@ -261,22 +247,17 @@ PoolObjectSQL * PoolSQL::get(const string& name, int ouid, bool olock) void PoolSQL::flush_cache(int oid) { - int rc; - PoolObjectSQL * tmp_ptr; - - map::iterator it; - - for (it = pool.begin(); it != pool.end(); ) + for (vector::iterator it = pool.begin(); it != pool.end();) { // 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 { // 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 { @@ -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); - - 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) { - int rc; - PoolObjectSQL * tmp_ptr; - - map::iterator it; - - for (it = name_pool.begin(); it != name_pool.end(); ) + for (vector::iterator it = pool.begin(); it != pool.end();) { + string okey = key((*it)->name, (*it)->uid); + // 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 { // 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 { @@ -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 = name_pool.erase(it); - - pool.erase(tmp_ptr->oid); - - delete tmp_ptr; + it = pool.erase(it); } } @@ -346,21 +309,19 @@ void PoolSQL::flush_cache(const string& name_key) void PoolSQL::clean() { - map::iterator it; + vector::iterator it; 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(); - name_pool.clear(); - unlock(); } diff --git a/src/secgroup/SecurityGroupPool.cc b/src/secgroup/SecurityGroupPool.cc index ae85fbe999..d3b01bdd17 100644 --- a/src/secgroup/SecurityGroupPool.cc +++ b/src/secgroup/SecurityGroupPool.cc @@ -21,8 +21,7 @@ /* -------------------------------------------------------------------------- */ -SecurityGroupPool::SecurityGroupPool(SqlDB * db) - :PoolSQL(db, SecurityGroup::table, true, true) +SecurityGroupPool::SecurityGroupPool(SqlDB * db):PoolSQL(db,SecurityGroup::table) { //lastOID is set in PoolSQL::init_cb if (get_lastOID() == -1) diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index 3199f89bfe..7294fa7231 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -56,7 +56,7 @@ UserPool::UserPool(SqlDB * db, vector hook_mads, const string& remotes_location, bool is_federation_slave): - PoolSQL(db, User::table, !is_federation_slave, true) + PoolSQL(db, User::table) { int one_uid = -1; int server_uid = -1; diff --git a/src/vdc/VdcPool.cc b/src/vdc/VdcPool.cc index 21aed45177..d221507917 100644 --- a/src/vdc/VdcPool.cc +++ b/src/vdc/VdcPool.cc @@ -25,8 +25,7 @@ const int VdcPool::DEFAULT_ID = 0; /* -------------------------------------------------------------------------- */ -VdcPool::VdcPool(SqlDB * db, bool is_federation_slave) - :PoolSQL(db, Vdc::table, !is_federation_slave, true) +VdcPool::VdcPool(SqlDB * db, bool is_federation_slave): PoolSQL(db, Vdc::table) { string error_str; diff --git a/src/vm/VirtualMachinePool.cc b/src/vm/VirtualMachinePool.cc index 0afdc6d7f9..6cb0660de8 100644 --- a/src/vm/VirtualMachinePool.cc +++ b/src/vm/VirtualMachinePool.cc @@ -47,7 +47,7 @@ VirtualMachinePool::VirtualMachinePool( float default_cpu_cost, float default_mem_cost, float default_disk_cost) - : PoolSQL(db, VirtualMachine::table, true, false), + : PoolSQL(db, VirtualMachine::table), _monitor_expiration(expire_time), _submit_on_hold(on_hold), _default_cpu_cost(default_cpu_cost), _default_mem_cost(default_mem_cost), _default_disk_cost(default_disk_cost) diff --git a/src/vnm/VirtualNetworkPool.cc b/src/vnm/VirtualNetworkPool.cc index b0ffa4c291..34d4ca3e51 100644 --- a/src/vnm/VirtualNetworkPool.cc +++ b/src/vnm/VirtualNetworkPool.cc @@ -48,7 +48,7 @@ VirtualNetworkPool::VirtualNetworkPool( const vector& _inherit_attrs, const VectorAttribute * _vlan_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), vxlan_conf(_vxlan_conf) { diff --git a/src/zone/ZonePool.cc b/src/zone/ZonePool.cc index 692d567ae6..69a0af4190 100644 --- a/src/zone/ZonePool.cc +++ b/src/zone/ZonePool.cc @@ -28,8 +28,7 @@ const int ZonePool::STANDALONE_ZONE_ID = 0; /* -------------------------------------------------------------------------- */ -ZonePool::ZonePool(SqlDB * db, bool is_federation_slave) - :PoolSQL(db, Zone::table, !is_federation_slave, true) +ZonePool::ZonePool(SqlDB * db, bool is_federation_slave):PoolSQL(db,Zone::table) { string error_str;