mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / test / ndbapi / acrt / NdbRepStress.cpp
blob5151c96f1bad32ef29b3a719718745ee4754da0d
1 /* Copyright (c) 2003, 2008 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 #include <NDBT_Test.hpp>
17 #include <NDBT_ReturnCodes.h>
18 #include <HugoTransactions.hpp>
19 #include <UtilTransactions.hpp>
20 #include <DbUtil.hpp>
21 #include <mysql.h>
24 Will include restart testing in future phases
25 #include <NdbRestarter.hpp>
26 #include <NdbRestarts.hpp>
29 /**** TOOL SECTION ****/
31 static uint
32 urandom()
34 uint r = (uint)random();
35 return r;
38 static uint
39 urandom(uint m)
41 if (m == 0)
42 return NDBT_OK;
43 uint r = urandom();
44 r = r % m;
45 return r;
48 #define GETNDB(ps) ((NDBT_NdbApiStep*)ps)->getNdb()
52 int
53 syncSlaveWithMaster()
55 /*
56 We need to look at the MAX epoch of the
57 mysql.ndb_binlog_index table so we will
58 know when the slave has caught up
61 SqlResultSet result;
62 unsigned int masterEpoch = 0;
63 unsigned int slaveEpoch = 0;
64 unsigned int slaveEpochOld = 0;
65 int maxLoops = 100;
66 int loopCnt = 0;
68 //Create a DbUtil object for the master
69 DbUtil master("mysql","");
71 //Login to Master
72 if (!master.connect())
74 return NDBT_FAILED;
77 //Get max epoch from master
78 if(master.doQuery("SELECT MAX(epoch) FROM mysql.ndb_binlog_index", result))
80 return NDBT_FAILED;
82 masterEpoch = result.columnAsInt("epoch");
85 Now we will pull current epoch from slave. If not the
86 same as master, we will continue to retrieve the epoch
87 and compare until it matches or we reach the max loops
88 allowed.
91 //Create a dbutil object for the slave
92 DbUtil slave("mysql",".slave");
94 //Login to slave
95 if (!slave.connect())
97 return NDBT_FAILED;
100 while(slaveEpoch != masterEpoch && loopCnt < maxLoops)
102 if(slave.doQuery("SELECT epoch FROM mysql.ndb_apply_status",result))
104 return NDBT_FAILED;
106 slaveEpoch = result.columnAsInt("epoch");
108 if(slaveEpoch != slaveEpochOld)
110 slaveEpochOld = slaveEpoch;
111 if(loopCnt > 0)
112 loopCnt--;
113 sleep(3);
115 else
117 sleep(1);
118 loopCnt++;
122 if(slaveEpoch != masterEpoch)
124 g_err << "Slave not in sync with master!" << endl;
125 return NDBT_FAILED;
127 return NDBT_OK;
131 verifySlaveLoad(BaseString &table)
133 //BaseString sqlStm;
134 BaseString db;
135 unsigned int masterCount = 0;
136 unsigned int slaveCount = 0;
138 db.assign("TEST_DB");
139 //sqlStm.assfmt("SELECT COUNT(*) FROM %s", table);
141 //First thing to do is sync slave
142 if(syncSlaveWithMaster())
144 g_err << "Verify Load -> Syncing with slave failed" << endl;
145 return NDBT_FAILED;
148 //Now that slave is sync we can verify load
149 DbUtil master(db.c_str()," ");
151 //Login to Master
152 if (!master.connect())
154 return NDBT_FAILED;
157 if((masterCount = master.selectCountTable(table.c_str())) == 0 )
159 return NDBT_FAILED;
162 //Create a DB Object for slave
163 DbUtil slave(db.c_str(),".slave");
165 //Login to slave
166 if (!slave.connect())
168 return NDBT_FAILED;
171 if((slaveCount = slave.selectCountTable(table.c_str())) == 0 )
173 return NDBT_FAILED;
176 if(slaveCount != masterCount)
178 g_err << "Verify Load -> Slave Count != Master Count "
179 << endl;
180 return NDBT_FAILED;
182 return NDBT_OK;
186 createTEST_DB(NDBT_Context* ctx, NDBT_Step* step)
188 BaseString cdb;
189 cdb.assign("TEST_DB");
191 //Create a dbutil object
192 DbUtil master("mysql","");
194 if (master.connect())
196 if (master.createDb(cdb) == NDBT_OK)
198 return NDBT_OK;
201 return NDBT_FAILED;
205 dropTEST_DB(NDBT_Context* ctx, NDBT_Step* step)
207 //Create an SQL Object
208 DbUtil master("mysql","");
210 //Login to Master
211 if (!master.connect())
213 return NDBT_FAILED;
216 if(master.doQuery("DROP DATABASE TEST_DB") != NDBT_OK)
218 return NDBT_FAILED;
221 if(syncSlaveWithMaster() != NDBT_OK)
223 g_err << "Drop DB -> Syncing with slave failed"
224 << endl;
225 return NDBT_FAILED;
227 return NDBT_OK;
231 verifySlave(BaseString& sqlStm, BaseString& db, BaseString& column)
233 SqlResultSet result;
234 float masterSum;
235 float slaveSum;
237 //Create SQL Objects
238 DbUtil master(db.c_str(),"");
239 DbUtil slave(db.c_str(),".slave");
241 if(syncSlaveWithMaster() != NDBT_OK)
243 g_err << "Verify Slave rep1 -> Syncing with slave failed"
244 << endl;
245 return NDBT_FAILED;
248 //Login to Master
249 if (!master.connect())
251 return NDBT_FAILED;
254 if(master.doQuery(sqlStm.c_str(),result) != NDBT_OK)
256 return NDBT_FAILED;
258 masterSum = result.columnAsInt(column.c_str());
260 //Login to slave
261 if (!slave.connect())
263 return NDBT_FAILED;
266 if(slave.doQuery(sqlStm.c_str(),result) != NDBT_OK)
268 return NDBT_FAILED;
270 slaveSum = result.columnAsInt(column.c_str());
272 if(masterSum != slaveSum)
274 g_err << "VerifySlave -> masterSum != slaveSum..." << endl;
275 return NDBT_FAILED;
277 return NDBT_OK;
281 /**** Test Section ****/
283 int
284 createDB(NDBT_Context* ctx, NDBT_Step* step)
286 BaseString cdb;
287 cdb.assign("TEST_DB");
289 //Create a dbutil object
290 DbUtil master("mysql","");
292 if (master.connect())
294 if (master.createDb(cdb) == NDBT_OK)
296 return NDBT_OK;
299 return NDBT_FAILED;
303 createTable_rep1(NDBT_Context* ctx, NDBT_Step* step)
305 BaseString table;
306 BaseString db;
308 table.assign("rep1");
309 db.assign("TEST_DB");
311 //Ensure slave is up and ready
312 if(syncSlaveWithMaster() != NDBT_OK)
314 g_err << "Create Table -> Syncing with slave failed"
315 << endl;
316 return NDBT_FAILED;
319 //Create an SQL Object
320 DbUtil master(db.c_str(),"");
322 //Login to Master
323 if (!master.connect())
325 return NDBT_FAILED;
328 if (master.doQuery("CREATE TABLE rep1 (c1 MEDIUMINT NOT NULL AUTO_INCREMENT,"
329 " c2 FLOAT, c3 CHAR(5), c4 bit(8), c5 FLOAT, c6 INT,"
330 " c7 INT, PRIMARY KEY (c1))ENGINE=NDB"))
332 return NDBT_FAILED;
334 ctx->setProperty("TABLES",table.c_str());
335 HugoTransactions hugoTrans(*ctx->getTab());
337 if (hugoTrans.loadTable(GETNDB(step), ctx->getNumRecords(), 1, true, 0) != NDBT_OK)
339 g_err << "Create Table -> Load failed!" << endl;
340 return NDBT_FAILED;
343 if(verifySlaveLoad(table)!= NDBT_OK)
345 g_err << "Create Table -> Failed on verify slave load!"
346 << endl;
347 return NDBT_FAILED;
349 //else everything is okay
350 return NDBT_OK;
354 stressNDB_rep1(NDBT_Context* ctx, NDBT_Step* step)
356 const NdbDictionary::Table * table= ctx->getTab();
357 HugoTransactions hugoTrans(* table);
358 while(!ctx->isTestStopped())
360 if (hugoTrans.pkUpdateRecords(GETNDB(step), ctx->getNumRecords(), 1, 30) != 0)
362 g_err << "pkUpdate Failed!" << endl;
363 return NDBT_FAILED;
365 if (hugoTrans.scanUpdateRecords(GETNDB(step), ctx->getNumRecords(), 1, 30) != 0)
367 g_err << "scanUpdate Failed!" << endl;
368 return NDBT_FAILED;
371 return NDBT_OK;
375 stressSQL_rep1(NDBT_Context* ctx, NDBT_Step* step)
377 BaseString sqlStm;
379 DbUtil master("TEST_DB","");
380 int loops = ctx->getNumLoops();
381 uint record = 0;
383 //Login to Master
384 if (!master.connect())
386 ctx->stopTest();
387 return NDBT_FAILED;
390 for (int j= 0; loops == 0 || j < loops; j++)
392 record = urandom(ctx->getNumRecords());
393 sqlStm.assfmt("UPDATE TEST_DB.rep1 SET c2 = 33.3221 where c1 = %u", record);
394 if(master.doQuery(sqlStm.c_str()))
396 return NDBT_FAILED;
399 ctx->stopTest();
400 return NDBT_OK;
404 verifySlave_rep1(NDBT_Context* ctx, NDBT_Step* step)
406 BaseString sql;
407 BaseString db;
408 BaseString column;
410 sql.assign("SELECT SUM(c3) FROM rep1");
411 db.assign("TEST_DB");
412 column.assign("c3");
414 if (verifySlave(sql,db,column) != NDBT_OK)
415 return NDBT_FAILED;
416 return NDBT_OK;
419 /* TOOLS LIST
421 syncSlaveWithMaster()
422 {ensures slave is at same epoch as master}
424 verifySlaveLoad(BaseString *table)
425 {ensures slave table has same record count as master}
427 createTEST_DB()
428 {Creates TEST_DB database on master}
430 dropTEST_DB()
431 {Drops TEST_DB database on master}
433 verifySlave(BaseString& sql, BaseSting& db, BaseSting& column)
434 {The SQL statement must sum a column and will verify
435 that the sum of the column is equal on master & slave}
439 NDBT_TESTSUITE(NdbRepStress);
440 TESTCASE("PHASE_I_Stress","Basic Replication Stressing")
442 INITIALIZER(createDB);
443 INITIALIZER(createTable_rep1);
444 STEP(stressNDB_rep1);
445 STEP(stressSQL_rep1);
446 FINALIZER(verifySlave_rep1);
447 FINALIZER(dropTEST_DB);
449 NDBT_TESTSUITE_END(NdbRepStress);
451 int main(int argc, const char** argv){
452 ndb_init();
453 NdbRepStress.setCreateAllTables(true);
454 return NdbRepStress.execute(argc, argv);