1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

Merge branch 'juanmont-bug-5425'

This commit is contained in:
Ruben S. Montero 2017-10-20 14:19:03 +02:00
commit 998e2f35dd
7 changed files with 168 additions and 82 deletions

105
include/CachePool.h Normal file
View File

@ -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 <time.h>
#include <sstream>
#include <pthread.h>
#include <iostream>
#include <vector>
using namespace std;
/**
* The Cache Pool class. This class is used to store volatile pool data.
*/
template<typename T> class CachePool
{
public:
CachePool()
{
pthread_mutex_init(&resource_lock, 0);
}
~CachePool()
{
typename std::map<int, T *>::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<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);
}
private:
pthread_mutex_t resource_lock;
std::map<int, T *> resources;
};
#endif /*CACHE_POOL_H_*/

View File

@ -19,6 +19,7 @@
#include "PoolSQL.h"
#include "Host.h"
#include "CachePool.h"
#include <time.h>
#include <sstream>
@ -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<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;
@ -318,13 +320,17 @@ private:
set<int> prev_rediscovered_vms;
};
pthread_mutex_t host_vm_lock;
CachePool<HostVM> cache;
map<int, HostVM *> 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

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;
};
@ -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;
};

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->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->session = get_session_token(u->oid);
}
return u;
};
/**
@ -221,6 +237,18 @@ 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 +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

View File

@ -140,26 +140,11 @@ HostPool::HostPool(SqlDB* db,
add_hook(hook);
}
pthread_mutex_init(&host_vm_lock,0);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
HostPool::~HostPool()
{
map<int, HostVM *>::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<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);
}

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();
}

View File

@ -497,7 +497,15 @@ int UserPool::drop(PoolObjectSQL * objsql, string& error_msg)
return -1;
}
return PoolSQL::drop(objsql, error_msg);
int oid = (static_cast<User *>(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;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */