diff --git a/include/CachePool.h b/include/CachePool.h new file mode 100644 index 0000000000..c8530b0c13 --- /dev/null +++ b/include/CachePool.h @@ -0,0 +1,105 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2017, OpenNebula Project, OpenNebula Systems */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#ifndef CACHE_POOL_H_ +#define CACHE_POOL_H_ + +#include +#include +#include + +#include + +#include + +using namespace std; + +/** + * The Cache Pool class. This class is used to store volatile pool data. + */ +template class CachePool +{ +public: + CachePool() + { + pthread_mutex_init(&resource_lock, 0); + } + + ~CachePool() + { + typename std::map::iterator it; + + pthread_mutex_lock(&resource_lock); + + for (it=resources.begin(); it != resources.end() ; ++it) + { + delete it->second; + } + + pthread_mutex_unlock(&resource_lock); + + pthread_mutex_destroy(&resource_lock); + }; + + 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); + } + +private: + + pthread_mutex_t resource_lock; + + std::map resources; +}; + +#endif /*CACHE_POOL_H_*/ \ No newline at end of file diff --git a/include/HostPool.h b/include/HostPool.h index 2128274480..75d5f6a9c5 100644 --- a/include/HostPool.h +++ b/include/HostPool.h @@ -19,6 +19,7 @@ #include "PoolSQL.h" #include "Host.h" +#include "CachePool.h" #include #include @@ -39,7 +40,7 @@ public: const string& hook_location, const string& remotes_location, time_t expire_time); - ~HostPool(); + ~HostPool(){}; /** * Function to allocate a new Host object @@ -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; @@ -318,13 +320,17 @@ private: set prev_rediscovered_vms; }; - pthread_mutex_t host_vm_lock; + CachePool cache; - map host_vms; + HostVM * get_host_vm(int oid) + { + return cache.get_resource(oid); + } - HostVM * get_host_vm(int 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..e998077727 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; }; @@ -286,7 +286,7 @@ private: // ************************************************************************* // Authentication session used to cache authentication calls // ************************************************************************* - SessionToken session; + SessionToken * session; // ************************************************************************* // DataBase implementation (Private) @@ -371,7 +371,8 @@ protected: password(_password), auth_driver(_auth_driver), enabled(_enabled), - groups("GROUPS") + groups("GROUPS"), + session(0) { obj_template = new UserTemplate; }; diff --git a/include/UserPool.h b/include/UserPool.h index 8d557646d9..363359ec7e 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->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->session = get_session_token(u->oid); + } + + return u; }; /** @@ -221,6 +237,18 @@ 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 +301,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..4174fc4b86 100644 --- a/src/host/HostPool.cc +++ b/src/host/HostPool.cc @@ -140,26 +140,11 @@ HostPool::HostPool(SqlDB* db, add_hook(hook); } - - pthread_mutex_init(&host_vm_lock,0); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -HostPool::~HostPool() -{ - map::iterator it; - - for (it=host_vms.begin(); it != host_vms.end() ; ++it) - { - delete it->second; - } -}; - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - int HostPool::allocate ( int * oid, const string& hostname, @@ -337,47 +322,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..01c0c89105 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -497,7 +497,15 @@ int UserPool::drop(PoolObjectSQL * objsql, string& error_msg) return -1; } - return PoolSQL::drop(objsql, error_msg); + int oid = (static_cast(objsql))->oid; + int rc = PoolSQL::drop(objsql, error_msg); + + if ( rc == 0 ) + { + delete_session_token(oid); + } + + return rc; } /* -------------------------------------------------------------------------- */ @@ -680,7 +688,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 +752,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 +945,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 +979,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 +1349,3 @@ string UserPool::get_token_password(int oid, int bck_oid){ } return token_password; } - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */