1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-24 21:34:01 +03:00

Removed unneeded locks and updated Pool tests

This commit is contained in:
Ruben S. Montero 2010-06-03 11:47:03 +02:00
parent 4be783e9be
commit ad57e2b2c6
6 changed files with 80 additions and 49 deletions

View File

@ -140,22 +140,6 @@ protected:
*/
SqlDB * db;
/**
* Function to lock the pool
*/
void lock()
{
pthread_mutex_lock(&mutex);
};
/**
* Function to unlock the pool
*/
void unlock()
{
pthread_mutex_unlock(&mutex);
};
private:
pthread_mutex_t mutex;
@ -177,7 +161,7 @@ private:
* The pool is implemented with a Map of SQL object pointers, using the
* OID as key.
*/
map<int,PoolObjectSQL *> pool;
map<int,PoolObjectSQL *> pool;
/**
* Factory method, must return an ObjectSQL pointer to an allocated pool
@ -189,7 +173,23 @@ private:
* OID queue to implement a FIFO-like replacement policy for the pool
* cache.
*/
queue<int> oid_queue;
queue<int> oid_queue;
/**
* Function to lock the pool
*/
void lock()
{
pthread_mutex_lock(&mutex);
};
/**
* Function to unlock the pool
*/
void unlock()
{
pthread_mutex_unlock(&mutex);
};
/**
* FIFO-like replacement policy function. Before removing an object (pop)

View File

@ -96,7 +96,7 @@ public:
{
if (mysql)
{
db = new MySqlDB("localhost","oneadmin","onepass",NULL);
db = new MySqlDB("localhost","oneadmin","oneadmin",NULL);
ostringstream oss1;
oss1 << "DROP DATABASE IF EXISTS " << db_name;
@ -135,9 +135,9 @@ public:
{
unlink(db_name.c_str());
}
if ( pool != 0 )
{
{
delete pool;
}

View File

@ -75,8 +75,6 @@ int HostPool::discover(map<int, string> * discovered_hosts)
ostringstream sql;
int rc;
lock();
set_callback(static_cast<Callbackable::Callback>(&HostPool::discover_cb),
static_cast<void *>(discovered_hosts));
@ -86,8 +84,6 @@ int HostPool::discover(map<int, string> * discovered_hosts)
rc = db->exec(sql,this);
unlock();
return rc;
}

View File

@ -233,10 +233,13 @@ void PoolSQL::replace()
}
else
{
delete index->second;
PoolObjectSQL * tmp_ptr;
tmp_ptr = index->second;
pool.erase(index);
delete tmp_ptr;
oid_queue.pop();
removed = true;
}
@ -292,8 +295,6 @@ int PoolSQL::search(
ostringstream sql;
int rc;
lock();
set_callback(static_cast<Callbackable::Callback>(&PoolSQL::search_cb),
static_cast<void *>(&oids));
@ -301,7 +302,5 @@ int PoolSQL::search(
rc = db->exec(sql, this);
unlock();
return rc;
}
}

View File

@ -85,7 +85,21 @@ int TestObjectSQL::select(SqlDB *db)
int TestObjectSQL::insert(SqlDB *db)
{
return update(db);
int rc;
char * sql_text;
sql_text = db->escape_str(text.c_str());
oss << "INSERT INTO " << table << " "<< db_names <<" VALUES ("
<< oid << ","
<< number << ","
<< "'" << sql_text << "')";
rc = db->exec(oss);
db->free_str(sql_text);
return rc;
}
/* -------------------------------------------------------------------------- */
@ -99,13 +113,13 @@ int TestObjectSQL::update(SqlDB *db)
sql_text = db->escape_str(text.c_str());
oss << "INSERT OR REPLACE INTO " << table << " "<< db_names <<" VALUES ("
oss << "REPLACE INTO " << table << " "<< db_names <<" VALUES ("
<< oid << ","
<< number << ","
<< "'" << sql_text << "')";
rc = db->exec(oss);
db->free_str(sql_text);
return rc;

View File

@ -28,6 +28,8 @@
#include "PoolSQL.h"
#include "TestPoolSQL.h"
#include "SqliteDB.h"
#include "MySqlDB.h"
#include "SqlDB.h"
using namespace std;
@ -45,7 +47,7 @@ class PoolTest : public CppUnit::TestFixture
private:
TestPool * pool;
SqliteDB * db;
SqlDB * db;
int create_allocate(int n, string st)
{
@ -58,16 +60,36 @@ public:
PoolTest(){};
~PoolTest(){};
void setUp()
{
string db_name = "test.db";
unlink("test.db");
db = new SqliteDB(db_name);
string db_name = "testdb";
if (true)
{
db = new MySqlDB("localhost","oneadmin","oneadmin",NULL);
ostringstream oss1;
oss1 << "DROP DATABASE IF EXISTS " << db_name;
db->exec(oss1);
ostringstream oss;
oss << "CREATE DATABASE " << db_name;
db->exec(oss);
ostringstream oss2;
oss2 << "use " << db_name;
db->exec(oss2);
}
else
{
unlink(db_name.c_str());
db = new SqliteDB(db_name);
}
TestObjectSQL::bootstrap(db);
pool = new TestPool(db);
};
@ -136,7 +158,7 @@ public:
pool->clean();
obj = pool->get(oid,true);
CPPUNIT_ASSERT(obj == 0);
CPPUNIT_ASSERT(obj == 0);
};
void search()
@ -153,14 +175,14 @@ public:
const char * table = "test_pool";
string where = "text = '" + stB + "'";
int ret;
ret = pool->search(results, table, where);
CPPUNIT_ASSERT(ret == 0);
CPPUNIT_ASSERT(results.size() == 1);
CPPUNIT_ASSERT(results.at(0) == oidB);
results.erase(results.begin(), results.end());
where = "number < 18";
ret = pool->search(results, table, where);
@ -175,15 +197,15 @@ public:
TestObjectSQL *obj;
TestObjectSQL *obj_lock;
//pin object in the cache, it can't be removed -
//pin object in the cache, it can't be removed -
for (int i=0 ; i < 499 ; i++)
{
create_allocate(i,"A Test object");
obj_lock = pool->get(i, true);
CPPUNIT_ASSERT(obj_lock != 0);
}
for (int i=499 ; i < 2000 ; i++)
{
create_allocate(i,"A Test object");
@ -200,7 +222,7 @@ public:
}
for (int i=0 ; i < 499 ; i++)
{
{
obj_lock = pool->get(i, false);//pin object in the cache, it can't be removed
obj_lock->unlock();
}
@ -219,6 +241,6 @@ int main(int argc, char ** argv)
runner.addTest( PoolTest::suite() );
runner.run();
NebulaLog::finalize_log_system();
return 0;
}