diff --git a/include/CachePool.h b/include/CachePool.h new file mode 100644 index 0000000000..a51e0dac68 --- /dev/null +++ b/include/CachePool.h @@ -0,0 +1,82 @@ +#ifndef CACHE_POOL_H_ +#define CACHE_POOL_H_ + +#include +#include +#include + +#include + +#include + +using namespace std; + +/** + * The Cache Pool class. + */ +template class CachePool +{ +private: + + pthread_mutex_t resource_lock; + + std::map resources; + +public: + CachePool(){ + pthread_mutex_init(&resource_lock,0); + } + + ~CachePool() + { + typename std::map::iterator it; + + for (it=resources.begin(); it != resources.end() ; ++it) + { + delete it->second; + } + }; + + T * get_resource(int oid) + { + T * res; + + pthread_mutex_lock(&resource_lock); + + typename std::map::iterator it = resources.find(oid); + + if ( it == resources.end() ) + { + res = new T; + + resources.insert(std::make_pair(oid, res)); + } + else + { + res = it->second; + } + + pthread_mutex_unlock(&resource_lock); + + return res; + } + + + void delete_resource(int oid) + { + pthread_mutex_lock(&resource_lock); + + typename std::map::iterator it = resources.find(oid); + + if ( it != resources.end() ) + { + delete it->second; + + resources.erase(it); + } + + pthread_mutex_unlock(&resource_lock); + } +}; + +#endif /*CACHE_POOL_H_*/ \ No newline at end of file diff --git a/include/HostPool.h b/include/HostPool.h index 2128274480..31b3ae5046 100644 --- a/include/HostPool.h +++ b/include/HostPool.h @@ -19,6 +19,7 @@ #include "PoolSQL.h" #include "Host.h" +#include "CachePool.h" #include #include @@ -197,6 +198,7 @@ public: int drop(PoolObjectSQL * objsql, string& error_msg) { Host * host = static_cast(objsql); + int oid = host->oid; if ( host->get_share_running_vms() > 0 ) { @@ -208,7 +210,7 @@ public: if ( rc == 0 ) { - delete_host_vm(host->oid); + delete_host_vm(oid); } return rc; @@ -320,11 +322,15 @@ private: pthread_mutex_t host_vm_lock; - map host_vms; + CachePool cache; - HostVM * get_host_vm(int oid); + HostVM * get_host_vm(int oid){ + return cache.get_resource(oid); + } - void delete_host_vm(int oid); + void delete_host_vm(int oid){ + cache.delete_resource(oid); + } /** * Factory method to produce Host objects diff --git a/include/User.h b/include/User.h index d3ae755b3c..1f6892e50c 100644 --- a/include/User.h +++ b/include/User.h @@ -95,7 +95,7 @@ public: { enabled = false; - session.reset(); + session->reset(); login_tokens.reset(); }; @@ -137,7 +137,7 @@ public: int set_auth_driver(const string& _auth_driver, string& error_str) { auth_driver = _auth_driver; - session.reset(); + session->reset(); return 0; }; @@ -224,6 +224,16 @@ public: return groups.contains(_group_id); } + /** + * Sets session token + * + * @param session_token the new session token + */ + void set_session(SessionToken * session_token) + { + session = session_token; + } + // ************************************************************************* // Quotas // ************************************************************************* @@ -286,7 +296,7 @@ private: // ************************************************************************* // Authentication session used to cache authentication calls // ************************************************************************* - SessionToken session; + SessionToken * session; // ************************************************************************* // DataBase implementation (Private) diff --git a/include/UserPool.h b/include/UserPool.h index 8d557646d9..5e10ad620f 100644 --- a/include/UserPool.h +++ b/include/UserPool.h @@ -20,6 +20,8 @@ #include "PoolSQL.h" #include "User.h" #include "GroupPool.h" +#include "CachePool.h" +#include "LoginToken.h" #include #include @@ -80,7 +82,14 @@ public: */ User * get(int oid, bool lock) { - return static_cast(PoolSQL::get(oid,lock)); + User * u = static_cast(PoolSQL::get(oid,lock)); + + if ( u != 0 ) + { + u->set_session(get_session_token(oid)); + } + + return u; }; /** @@ -93,7 +102,14 @@ public: User * get(string name, bool lock) { // The owner is set to -1, because it is not used in the key() method - return static_cast(PoolSQL::get(name,-1,lock)); + User * u = static_cast(PoolSQL::get(name,-1,lock)); + + if ( u != 0 ) + { + u->set_session(get_session_token(u->oid)); + } + + return u; }; /** @@ -221,6 +237,16 @@ private: **/ static time_t _session_expiration_time; + + CachePool cache; + SessionToken * get_session_token(int oid){ + return cache.get_resource(oid); + } + + void delete_session_token(int oid){ + cache.delete_resource(oid); + } + /** * Function to authenticate internal (known) users */ @@ -273,7 +299,7 @@ private: //-------------------------------------------------------------------------- /** - * Callback function to get output in XML format + * Callback function to output in XML format * @param num the number of columns read from the DB * @param names the column names * @param vaues the column values diff --git a/src/host/HostPool.cc b/src/host/HostPool.cc index cba28bbc1f..31b06cb2f9 100644 --- a/src/host/HostPool.cc +++ b/src/host/HostPool.cc @@ -147,15 +147,7 @@ HostPool::HostPool(SqlDB* db, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -HostPool::~HostPool() -{ - map::iterator it; - - for (it=host_vms.begin(); it != host_vms.end() ; ++it) - { - delete it->second; - } -}; +HostPool::~HostPool(){}; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -337,47 +329,3 @@ int HostPool::clean_all_monitoring() return rc; } - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -HostPool::HostVM * HostPool::get_host_vm(int oid) -{ - HostVM * hvm; - - pthread_mutex_lock(&host_vm_lock); - - map::iterator it = host_vms.find(oid); - - if ( it == host_vms.end() ) - { - hvm = new HostVM; - - host_vms.insert(make_pair(oid, new HostVM)); - } - else - { - hvm = it->second; - } - - pthread_mutex_unlock(&host_vm_lock); - - return hvm; -} - -void HostPool::delete_host_vm(int oid) -{ - pthread_mutex_lock(&host_vm_lock); - - map::iterator it = host_vms.find(oid); - - if ( it != host_vms.end() ) - { - delete it->second; - - host_vms.erase(it); - } - - pthread_mutex_unlock(&host_vm_lock); -} - diff --git a/src/um/User.cc b/src/um/User.cc index 9b6b1242e7..826bea1636 100644 --- a/src/um/User.cc +++ b/src/um/User.cc @@ -353,7 +353,7 @@ int User::set_password(const string& passwd, string& error_str) password = passwd; } - session.reset(); + session->reset(); login_tokens.reset(); } diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index c9912b42cb..aa497e25f5 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -497,7 +497,17 @@ int UserPool::drop(PoolObjectSQL * objsql, string& error_msg) return -1; } - return PoolSQL::drop(objsql, error_msg); + User * user = static_cast(objsql); + int oid = user->oid; + + int rc = PoolSQL::drop(objsql, error_msg); + + if ( rc == 0 ) + { + delete_session_token(oid); + } + + return rc; } /* -------------------------------------------------------------------------- */ @@ -680,7 +690,7 @@ bool UserPool::authenticate_internal(User * user, return true; } - else if (user->session.is_valid(token)) + else if (user->session->is_valid(token)) { user->unlock(); return true; @@ -744,7 +754,7 @@ bool UserPool::authenticate_internal(User * user, return false; } - user->session.set(token, _session_expiration_time); + user->session->set(token, _session_expiration_time); if ( !driver_managed_groups || new_gid == -1 || new_group_ids == group_ids ) { @@ -937,7 +947,7 @@ bool UserPool::authenticate_server(User * user, uname = user->name; gname = user->gname; - result = user->session.is_valid(second_token); + result = user->session->is_valid(second_token); umask = user->get_umask(); @@ -971,7 +981,7 @@ bool UserPool::authenticate_server(User * user, if (user != 0) { - user->session.set(second_token, _session_expiration_time); + user->session->set(second_token, _session_expiration_time); user->unlock(); } @@ -1341,6 +1351,3 @@ string UserPool::get_token_password(int oid, int bck_oid){ } return token_password; } - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */