bool do_command(atrt_config& config){ #ifdef _WIN32 return true; #endif MYSQL* mysql= find_atrtdb_client(config); if (!mysql) return true; AtrtClient atrtdb(mysql); SqlResultSet command; if (!atrtdb.doQuery("SELECT * FROM command " \ "WHERE state = 'new' ORDER BY id LIMIT 1", command)){ g_logger.critical("query failed"); return false; } if (command.numRows() == 0) return true; uint id= command.columnAsInt("id"); uint cmd= command.columnAsInt("cmd"); g_logger.info("Got command, id: %d, cmd: %d", id, cmd); // command.print(); // Set state of command to running if (!ack_command(atrtdb, id, "running")) return false; switch (cmd){ case AtrtClient::ATCT_CHANGE_VERSION: if (!do_change_version(config, command, atrtdb)) return false; break; case AtrtClient::ATCT_RESET_PROC: if (!do_reset_proc(config, command, atrtdb)) return false; break; default: command.print(); g_logger.error("got unknown command: %d", cmd); return false; } // Set state of command to done if (!ack_command(atrtdb, id, "done")) return false; g_logger.info("done!"); return true; }
bool AtrtClient::doCommand(AtrtCommandType type, const Properties& args){ int running_timeout= 10; int total_timeout= 120; int commandId= writeCommand(type, args); if (commandId == -1){ g_err << "Failed to write command" << endl; return false; } while (true){ SqlResultSet result; if (!readCommand(commandId, result)) { result.print(); g_err << "Failed to read command "<< commandId << endl; return false; } // Get first row result.next(); // Check if command has completed BaseString state(result.column("state")); if (state == "done") { return true; } if (state == "new"){ if (!running_timeout--){ g_err << "Timeout while waiting for command " << commandId << " to start run" << endl; return false; } } else if (!total_timeout--){ g_err << "Timeout while waiting for result of command " << commandId << endl; return false; } NdbSleep_SecSleep(1); } return false; }
int runTestDbUtil(NDBT_Context* ctx, NDBT_Step* step){ DbUtil sql; { // Select all rows from mysql.user SqlResultSet result; if (!sql.doQuery("SELECT * FROM mysql.user", result)) return NDBT_FAILED; // result.print(); while(result.next()) { ndbout << result.column("host") << ", " << result.column("uSer") << ", " << result.columnAsInt("max_updates") << ", " << endl; } result.reset(); while(result.next()) { ndbout << result.column("host") << endl; } } { // No column name, query should fail Properties args; SqlResultSet result; if (sql.doQuery("SELECT * FROM mysql.user WHERE name=?", args, result)) return NDBT_FAILED; result.print(); } { // Select nonexisiting rows from mysql.user Properties args; SqlResultSet result; args.put("0", "no_such_host"); if (!sql.doQuery("SELECT * FROM mysql.user WHERE host=?", args, result)) return NDBT_FAILED; ndbout << "no rows" << endl; result.print(); // Change args to an find one row args.clear(); args.put("0", "localhost"); if (!sql.doQuery("SELECT host, user FROM mysql.user WHERE host=?", args, result)) return NDBT_FAILED; result.print(); } { if (!sql.doQuery("CREATE TABLE sql_client_test (a int, b varchar(255))")) return NDBT_FAILED; if (!sql.doQuery("INSERT INTO sql_client_test VALUES(1, 'hello'), (2, 'bye')")) return NDBT_FAILED; // Select all rows from sql_client_test SqlResultSet result; if (!sql.doQuery("SELECT * FROM sql_client_test", result)) return NDBT_FAILED; // result.print(); while(result.next()) { } // Select second row from sql_client_test Properties args; args.put("0", 2); if (!sql.doQuery("SELECT * FROM sql_client_test WHERE a=?", args,result)) return NDBT_FAILED; result.print(); result.reset(); while(result.next()) { ndbout << "a: " << result.columnAsInt("a") << endl; ndbout << "b: " << result.column("b") << endl; if (result.columnAsInt("a") != 2){ ndbout << "hepp1" << endl; return NDBT_FAILED; } if (strcmp(result.column("b"), "bye")){ ndbout << "hepp2" << endl; return NDBT_FAILED; } } if (sql.selectCountTable("sql_client_test") != 2) { ndbout << "Got wrong count" << endl; return NDBT_FAILED; } if (!sql.doQuery("DROP TABLE sql_client_test")) return NDBT_FAILED; } return NDBT_OK; }
bool syncSlaveWithMaster() { /* We need to look at the MAX epoch of the mysql.ndb_binlog_index table so we will know when the slave has caught up */ SqlResultSet result; unsigned long long masterEpoch = 0; unsigned long long slaveEpoch = 0; unsigned long long slaveEpochOld = 0; int maxLoops = 100; int loopCnt = 0; //Create a DbUtil object for the master DbUtil master("mysql"); //Login to Master if (!master.connect()) { g_err << "sync connect to master failed" << endl; return false; } //Get max epoch from master if(!master.doQuery("SELECT MAX(epoch) FROM mysql.ndb_binlog_index", result)) { g_err << "Select max(epoch) SQL failed" << endl; return false; } masterEpoch = result.columnAsLong("epoch"); /* Now we will pull current epoch from slave. If not the same as master, we will continue to retrieve the epoch and compare until it matches or we reach the max loops allowed. */ //Create a dbutil object for the slave DbUtil slave("mysql", ".1.slave"); //Login to slave if (!slave.connect()) { g_err << "sync connect to slave failed" << endl; return false; } while(slaveEpoch != masterEpoch && loopCnt < maxLoops) { if(!slave.doQuery("SELECT epoch FROM mysql.ndb_apply_status",result)) { g_err << "Select epoch SQL on slave failed" << endl; return false; } result.print(); if (result.numRows() > 0) slaveEpoch = result.columnAsLong("epoch"); if(slaveEpoch != slaveEpochOld) { slaveEpochOld = slaveEpoch; if(loopCnt > 0) loopCnt--; sleep(3); } else { sleep(1); loopCnt++; } } if(slaveEpoch != masterEpoch) { g_err << "Slave not in sync with master!" << endl; return false; } return true; }