B #5425: Changed UserPool and HostPool for to work with CachePool

(cherry picked from commit 3c46ff3366)
This commit is contained in:
juanmont authored and Ruben S. Montero committed 2017-10-20 14:20:08 +02:00
1 parent a39d152981
commit be1f6cf457
7 files changed
+151 -72

No files matched your search

+82
View File
@@ -0,0 +1,82 @@
#ifndef CACHE_POOL_H_
#define CACHE_POOL_H_
#include <time.h>
#include <sstream>
#include <pthread.h>
#include <iostream>
#include <vector>
using namespace std;
/**
* The Cache Pool class.
*/
template<typename T> class CachePool
{
private:
pthread_mutex_t resource_lock;
std::map<int, T *> resources;
public:
CachePool(){
pthread_mutex_init(&resource_lock,0);
}
~CachePool()
{
typename std::map<int, T *>::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<int, T *>::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<int, T *>::iterator it = resources.find(oid);
if ( it != resources.end() )
{
delete it->second;
resources.erase(it);
}
pthread_mutex_unlock(&resource_lock);
}
};
#endif /*CACHE_POOL_H_*/
+10 -4
View File
@@ -19,6 +19,7 @@
#include "PoolSQL.h"
#include "Host.h"
#include "CachePool.h"
#include <time.h>
#include <sstream>
@@ -197,6 +198,7 @@ public:
int drop(PoolObjectSQL * objsql, string& error_msg)
{
Host * host = static_cast<Host *>(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<int, HostVM *> host_vms;
CachePool<HostVM> 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
+13 -3
View File
@@ -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)
+29 -3
View File
@@ -20,6 +20,8 @@
#include "PoolSQL.h"
#include "User.h"
#include "GroupPool.h"
#include "CachePool.h"
#include "LoginToken.h"
#include <time.h>
#include <sstream>
@@ -80,7 +82,14 @@ public:
*/
User * get(int oid, bool lock)
{
return static_cast<User *>(PoolSQL::get(oid,lock));
User * u = static_cast<User *>(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<User *>(PoolSQL::get(name,-1,lock));
User * u = static_cast<User *>(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<SessionToken> 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
+1 -53
View File
@@ -147,15 +147,7 @@ HostPool::HostPool(SqlDB* db,
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
HostPool::~HostPool()
{
map<int, HostVM *>::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<int, HostVM *>::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<int, HostVM *>::iterator it = host_vms.find(oid);
if ( it != host_vms.end() )
{
delete it->second;
host_vms.erase(it);
}
pthread_mutex_unlock(&host_vm_lock);
}
+1 -1
View File
@@ -353,7 +353,7 @@ int User::set_password(const string& passwd, string& error_str)
password = passwd;
}
session.reset();
session->reset();
login_tokens.reset();
}
+15 -8
View File
@@ -497,7 +497,17 @@ int UserPool::drop(PoolObjectSQL * objsql, string& error_msg)
return -1;
}
return PoolSQL::drop(objsql, error_msg);
User * user = static_cast<User *>(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;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */