DbUtil.hpp, DbUtil.cpp, NdbRepStress.cpp:

  More code improvments
parent 4c8a2468
...@@ -90,9 +90,10 @@ public: ...@@ -90,9 +90,10 @@ public:
int connect(); int connect();
int selectDb(); int selectDb();
int selectDb(const char * m_db); int selectDb(const char *);
int doQuery(char * stm); int createDb(BaseString&);
int doQuery(const char * stm); int doQuery(BaseString&);
int doQuery(const char *);
int getErrorNumber(); int getErrorNumber();
unsigned long selectCountTable(const char * table); unsigned long selectCountTable(const char * table);
......
...@@ -17,16 +17,15 @@ ...@@ -17,16 +17,15 @@
#include <NDBT_ReturnCodes.h> #include <NDBT_ReturnCodes.h>
#include <HugoTransactions.hpp> #include <HugoTransactions.hpp>
#include <UtilTransactions.hpp> #include <UtilTransactions.hpp>
#include <TestNdbEventOperation.hpp>
#include <DbUtil.hpp> #include <DbUtil.hpp>
#include <NdbAutoPtr.hpp> #include <mysql.h>
/*
Will include restart testing in future phases
#include <NdbRestarter.hpp> #include <NdbRestarter.hpp>
#include <NdbRestarts.hpp> #include <NdbRestarts.hpp>
#include <mysql.h> */
#include <signaldata/DumpStateOrd.hpp>
static BaseString sqlStm;
static BaseString db;
static int t1_records = 50000; static int t1_records = 50000;
/**** TOOL SECTION ****/ /**** TOOL SECTION ****/
...@@ -42,7 +41,7 @@ static uint ...@@ -42,7 +41,7 @@ static uint
urandom(uint m) urandom(uint m)
{ {
if (m == 0) if (m == 0)
return 0; return NDBT_OK;
uint r = urandom(); uint r = urandom();
r = r % m; r = r % m;
return r; return r;
...@@ -78,12 +77,6 @@ syncSlaveWithMaster() ...@@ -78,12 +77,6 @@ syncSlaveWithMaster()
return NDBT_FAILED; return NDBT_FAILED;
} }
//Set the database we are wanting
if (master.selectDb())
{
return NDBT_FAILED;
}
//Get max epoch from master //Get max epoch from master
if(master.doQuery("SELECT MAX(epoch) FROM mysql.ndb_binlog_index")) if(master.doQuery("SELECT MAX(epoch) FROM mysql.ndb_binlog_index"))
{ {
...@@ -93,7 +86,6 @@ syncSlaveWithMaster() ...@@ -93,7 +86,6 @@ syncSlaveWithMaster()
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
masterEpoch = atoi(row[0]); masterEpoch = atoi(row[0]);
mysql_free_result(result); mysql_free_result(result);
master.databaseLogout();
/* /*
Now we will pull current epoch from slave. If not the Now we will pull current epoch from slave. If not the
...@@ -111,12 +103,6 @@ syncSlaveWithMaster() ...@@ -111,12 +103,6 @@ syncSlaveWithMaster()
return NDBT_FAILED; return NDBT_FAILED;
} }
//Set the database we are wanting
if (slave.selectDb())
{
return NDBT_FAILED;
}
while(slaveEpoch != masterEpoch && loopCnt < maxLoops) while(slaveEpoch != masterEpoch && loopCnt < maxLoops)
{ {
if(slave.doQuery("SELECT epoch FROM mysql.ndb_apply_status")) if(slave.doQuery("SELECT epoch FROM mysql.ndb_apply_status"))
...@@ -153,6 +139,16 @@ syncSlaveWithMaster() ...@@ -153,6 +139,16 @@ syncSlaveWithMaster()
int int
verifySlaveLoad(BaseString *table) verifySlaveLoad(BaseString *table)
{ {
BaseString sqlStm;
BaseString db;
MYSQL_RES * result;
MYSQL_ROW row;
unsigned int masterCount = 0;
unsigned int slaveCount = 0;
db.assign("TEST_DB");
sqlStm.assfmt("SELECT COUNT(*) FROM %s", table);
//First thing to do is sync slave //First thing to do is sync slave
if(syncSlaveWithMaster()) if(syncSlaveWithMaster())
{ {
...@@ -161,13 +157,6 @@ verifySlaveLoad(BaseString *table) ...@@ -161,13 +157,6 @@ verifySlaveLoad(BaseString *table)
} }
//Now that slave is sync we can verify load //Now that slave is sync we can verify load
sqlStm.assfmt("SELECT COUNT(*) FROM %s", table);
MYSQL_RES * result;
MYSQL_ROW row;
unsigned int masterCount = 0;
unsigned int slaveCount = 0;
//Create a DB Object for the master
DbUtil master(db.c_str()," "); DbUtil master(db.c_str()," ");
//Login to Master //Login to Master
...@@ -176,11 +165,6 @@ verifySlaveLoad(BaseString *table) ...@@ -176,11 +165,6 @@ verifySlaveLoad(BaseString *table)
return NDBT_FAILED; return NDBT_FAILED;
} }
if (master.selectDb())
{
return NDBT_FAILED;
}
if(master.doQuery(sqlStm.c_str())) if(master.doQuery(sqlStm.c_str()))
{ {
return NDBT_FAILED; return NDBT_FAILED;
...@@ -189,7 +173,6 @@ verifySlaveLoad(BaseString *table) ...@@ -189,7 +173,6 @@ verifySlaveLoad(BaseString *table)
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
masterCount = atoi(row[0]); masterCount = atoi(row[0]);
mysql_free_result(result); mysql_free_result(result);
master.databaseLogout();
//Create a DB Object for slave //Create a DB Object for slave
DbUtil slave(db.c_str(),".slave"); DbUtil slave(db.c_str(),".slave");
...@@ -200,11 +183,6 @@ verifySlaveLoad(BaseString *table) ...@@ -200,11 +183,6 @@ verifySlaveLoad(BaseString *table)
return NDBT_FAILED; return NDBT_FAILED;
} }
if (slave.selectDb())
{
return NDBT_FAILED;
}
if(slave.doQuery(sqlStm.c_str())) if(slave.doQuery(sqlStm.c_str()))
{ {
return NDBT_FAILED; return NDBT_FAILED;
...@@ -223,45 +201,141 @@ verifySlaveLoad(BaseString *table) ...@@ -223,45 +201,141 @@ verifySlaveLoad(BaseString *table)
return NDBT_OK; return NDBT_OK;
} }
/**** Test Section ****/
int int
createDB(NDBT_Context* ctx, NDBT_Step* step) createTEST_DB(NDBT_Context* ctx, NDBT_Step* step)
{ {
//Setup the BaseString db to use throughout BaseString cdb;
db.assign("TEST_DB"); cdb.assign("TEST_DB");
//Create a dbutil object //Create a dbutil object
DbUtil master("mysql",""); DbUtil master("mysql","");
if (master.connect())
{
if (master.createDb(cdb) == NDBT_OK)
{
return NDBT_OK;
}
}
return NDBT_FAILED;
}
int
dropTEST_DB(NDBT_Context* ctx, NDBT_Step* step)
{
//Create an SQL Object
DbUtil master("mysql","");
//Login to Master //Login to Master
if (!master.connect()) if (!master.connect())
{ {
return NDBT_FAILED; return NDBT_FAILED;
} }
//Check to see if db already there if(master.doQuery("DROP DATABASE TEST_DB") != NDBT_OK)
if (master.selectDb(db.c_str()))
{ {
return NDBT_FAILED; return NDBT_FAILED;
} }
//Create TEST_DB if(syncSlaveWithMaster() != NDBT_OK)
if (master.doQuery("CREATE DATABASE TEST_DB") != 0)
{ {
g_err << "Drop DB -> Syncing with slave failed"
<< endl;
return NDBT_FAILED; return NDBT_FAILED;
} }
return NDBT_OK; return NDBT_OK;
} }
int
verifySlave(BaseString& sqlStm, BaseString& db)
{
MYSQL_RES* resource;
MYSQL_ROW row;
float masterSum;
float slaveSum;
//Create SQL Objects
DbUtil master(db.c_str(),"");
DbUtil slave(db.c_str(),".slave");
if(syncSlaveWithMaster() != NDBT_OK)
{
g_err << "Verify Slave rep1 -> Syncing with slave failed"
<< endl;
return NDBT_FAILED;
}
//Login to Master
if (!master.connect())
{
return NDBT_FAILED;
}
if(master.doQuery(sqlStm.c_str()) != NDBT_OK)
{
return NDBT_FAILED;
}
resource = mysql_use_result(master.getMysql());
row = mysql_fetch_row(resource);
masterSum = atoi(row[0]);
mysql_free_result(resource);
//Login to slave
if (!slave.connect())
{
return NDBT_FAILED;
}
if(slave.doQuery(sqlStm.c_str()) != NDBT_OK)
{
return NDBT_FAILED;
}
resource = mysql_use_result(slave.getMysql());
row = mysql_fetch_row(resource);
slaveSum = atoi(row[0]);
mysql_free_result(resource);
if(masterSum != slaveSum)
{
g_err << "VerifySlave -> masterSum != slaveSum..." << endl;
return NDBT_FAILED;
}
return NDBT_OK;
}
/**** Test Section ****/
int
createDB(NDBT_Context* ctx, NDBT_Step* step)
{
BaseString cdb;
cdb.assign("TEST_DB");
//Create a dbutil object
DbUtil master("mysql","");
if (master.connect())
{
if (master.createDb(cdb) == NDBT_OK)
{
return NDBT_OK;
}
}
return NDBT_FAILED;
}
int int
createTable_rep1(NDBT_Context* ctx, NDBT_Step* step) createTable_rep1(NDBT_Context* ctx, NDBT_Step* step)
{ {
BaseString table; BaseString table;
BaseString db;
table.assign("rep1"); table.assign("rep1");
db.assign("TEST_DB");
//Ensure slave is up and ready //Ensure slave is up and ready
if(syncSlaveWithMaster() != 0) if(syncSlaveWithMaster() != NDBT_OK)
{ {
g_err << "Create Table -> Syncing with slave failed" g_err << "Create Table -> Syncing with slave failed"
<< endl; << endl;
...@@ -277,30 +351,22 @@ createTable_rep1(NDBT_Context* ctx, NDBT_Step* step) ...@@ -277,30 +351,22 @@ createTable_rep1(NDBT_Context* ctx, NDBT_Step* step)
return NDBT_FAILED; return NDBT_FAILED;
} }
// Set the database we want if (master.doQuery("CREATE TABLE rep1 (c1 MEDIUMINT NOT NULL AUTO_INCREMENT,"
if(master.selectDb())
{
return NDBT_FAILED;
}
sqlStm.assign("CREATE TABLE rep1 (c1 MEDIUMINT NOT NULL AUTO_INCREMENT,"
" c2 FLOAT, c3 CHAR(5), c4 bit(8), c5 FLOAT, c6 INT," " c2 FLOAT, c3 CHAR(5), c4 bit(8), c5 FLOAT, c6 INT,"
" c7 INT, PRIMARY KEY (c1))ENGINE=NDB"); " c7 INT, PRIMARY KEY (c1))ENGINE=NDB"))
if (master.doQuery(sqlStm.c_str()))
{ {
return NDBT_FAILED; return NDBT_FAILED;
} }
ctx->setProperty("TABLES",table.c_str()); ctx->setProperty("TABLES",table.c_str());
HugoTransactions hugoTrans(*ctx->getTab()); HugoTransactions hugoTrans(*ctx->getTab());
if (hugoTrans.loadTable(GETNDB(step), t1_records, 1, true, 0) != 0) if (hugoTrans.loadTable(GETNDB(step), t1_records, 1, true, 0) != NDBT_OK)
{ {
g_err << "Create Table -> Load failed!" << endl; g_err << "Create Table -> Load failed!" << endl;
return NDBT_FAILED; return NDBT_FAILED;
} }
if(verifySlaveLoad(&table)!= 0) if(verifySlaveLoad(&table)!= NDBT_OK)
{ {
g_err << "Create Table -> Failed on verify slave load!" g_err << "Create Table -> Failed on verify slave load!"
<< endl; << endl;
...@@ -334,18 +400,14 @@ stressNDB_rep1(NDBT_Context* ctx, NDBT_Step* step) ...@@ -334,18 +400,14 @@ stressNDB_rep1(NDBT_Context* ctx, NDBT_Step* step)
int int
stressSQL_rep1(NDBT_Context* ctx, NDBT_Step* step) stressSQL_rep1(NDBT_Context* ctx, NDBT_Step* step)
{ {
//Create an SQL Object BaseString sqlStm;
DbUtil master(db.c_str(),"");
DbUtil master("TEST_DB","");
int loops = ctx->getNumLoops(); int loops = ctx->getNumLoops();
uint record = 0; uint record = 0;
//Login to Master //Login to Master
if (!master.connect()) if (!master.connect())
{
return NDBT_FAILED;
}
if(master.selectDb())
{ {
ctx->stopTest(); ctx->stopTest();
return NDBT_FAILED; return NDBT_FAILED;
...@@ -367,101 +429,36 @@ stressSQL_rep1(NDBT_Context* ctx, NDBT_Step* step) ...@@ -367,101 +429,36 @@ stressSQL_rep1(NDBT_Context* ctx, NDBT_Step* step)
int int
verifySlave_rep1(NDBT_Context* ctx, NDBT_Step* step) verifySlave_rep1(NDBT_Context* ctx, NDBT_Step* step)
{ {
if(syncSlaveWithMaster() != 0) BaseString sql;
{ BaseString db;
g_err << "Verify Slave rep1 -> Syncing with slave failed"
<< endl;
return NDBT_FAILED;
}
//Create SQL Objects
DbUtil master(db.c_str(),"");
DbUtil slave(db.c_str(),".slave");
MYSQL_RES *resource;
MYSQL_ROW row;
float masterSum;
float slaveSum;
sqlStm.assign("SELECT SUM(c3) FROM rep1");
//Login to Master sql.assign("SELECT SUM(c3) FROM rep1");
if (!master.connect()) db.assign("TEST_DB");
{
return NDBT_FAILED;
}
if(master.selectDb())
{
return NDBT_FAILED;
}
if(master.doQuery(sqlStm.c_str()))
{
return NDBT_FAILED;
}
resource = mysql_use_result(master.getMysql());
row = mysql_fetch_row(resource);
masterSum = atoi(row[0]);
mysql_free_result(resource);
master.databaseLogout();
//Login to slave
if (!slave.connect())
{
return NDBT_FAILED;
}
if(slave.selectDb() != 0)
{
return NDBT_FAILED;
}
if((slave.doQuery(sqlStm.c_str())) != 0)
{
return NDBT_FAILED;
}
resource = mysql_use_result(slave.getMysql());
row = mysql_fetch_row(resource);
slaveSum = atoi(row[0]);
mysql_free_result(resource);
if(masterSum != slaveSum) if (verifySlave(sql,db) != NDBT_OK)
{
g_err << "VerifySlave -> masterSum != slaveSum..." << endl;
return NDBT_FAILED; return NDBT_FAILED;
}
return NDBT_OK; return NDBT_OK;
} }
int /* TOOLS LIST
dropTEST_DB(NDBT_Context* ctx, NDBT_Step* step)
{
//Create an SQL Object
DbUtil master(db.c_str(),"");
//Login to Master syncSlaveWithMaster()
if (!master.connect()) {ensures slave is at same epoch as master}
{
return NDBT_FAILED;
}
if(master.selectDb() != 0) verifySlaveLoad(BaseString *table)
{ {ensures slave table has same record count as master}
return NDBT_FAILED;
}
if(master.doQuery("DROP DATABASE TEST_DB") != 0) createTEST_DB()
{ {Creates TEST_DB database on master}
return NDBT_FAILED;
} dropTEST_DB()
{Drops TEST_DB database on master}
verifySlave(BaseString& sql, BaseSting& db)
{The SQL statement must sum a column and will verify
that the sum of the column is equal on master & slave}
*/
if(syncSlaveWithMaster() != 0)
{
g_err << "Drop DB -> Syncing with slave failed"
<< endl;
return NDBT_FAILED;
}
return NDBT_OK;
}
NDBT_TESTSUITE(NdbRepStress); NDBT_TESTSUITE(NdbRepStress);
TESTCASE("PHASE_I_Stress","Basic Replication Stressing") TESTCASE("PHASE_I_Stress","Basic Replication Stressing")
...@@ -481,7 +478,3 @@ int main(int argc, const char** argv){ ...@@ -481,7 +478,3 @@ int main(int argc, const char** argv){
return NdbRepStress.execute(argc, argv); return NdbRepStress.execute(argc, argv);
} }
template class Vector<HugoOperations *>;
template class Vector<NdbEventOperation *>;
template class Vector<NdbRecAttr*>;
template class Vector<Vector<NdbRecAttr*> >;
...@@ -99,6 +99,7 @@ DbUtil::databaseLogin(const char* system, const char* usr, ...@@ -99,6 +99,7 @@ DbUtil::databaseLogin(const char* system, const char* usr,
mysql_get_server_info(mysql), mysql_get_server_info(mysql),
(unsigned long) mysql_get_server_version(mysql)); (unsigned long) mysql_get_server_version(mysql));
#endif #endif
this->selectDb();
} }
/* Database Connect */ /* Database Connect */
...@@ -132,7 +133,7 @@ DbUtil::connect() ...@@ -132,7 +133,7 @@ DbUtil::connect()
mysql_close(mysql); mysql_close(mysql);
return DBU_FAILED; return DBU_FAILED;
} }
this->selectDb();
m_connected = true; m_connected = true;
return DBU_OK; return DBU_OK;
} }
...@@ -243,15 +244,32 @@ DbUtil::selectDb(const char * m_db) ...@@ -243,15 +244,32 @@ DbUtil::selectDb(const char * m_db)
} }
} }
int
DbUtil::createDb(BaseString& m_db)
{
BaseString stm;
{
if(mysql_select_db(this->getMysql(), m_db.c_str()) == DBU_OK)
{
stm.assfmt("DROP DATABASE %s", m_db.c_str());
if(this->doQuery(m_db.c_str()) == DBU_FAILED)
return DBU_FAILED;
}
stm.assfmt("CREATE DATABASE %s", m_db.c_str());
if(this->doQuery(m_db.c_str()) == DBU_FAILED)
return DBU_FAILED;
return DBU_OK;
}
}
/* Run Simple Queries */ /* Run Simple Queries */
int int
DbUtil::doQuery(char * stm) DbUtil::doQuery(BaseString& str)
{ {
if(mysql_query(this->getMysql(), stm)) if(mysql_query(this->getMysql(), str.c_str()))
{ {
this->printError(stm); this->printError(str.c_str());
return DBU_FAILED; return DBU_FAILED;
} }
return DBU_OK; return DBU_OK;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment