53 #include <mysqld_error.h>
62 milliSleep(
int milliseconds){
64 sleeptime.tv_sec = milliseconds / 1000;
65 sleeptime.tv_usec = (milliseconds - (sleeptime.tv_sec * 1000)) * 1000000;
66 select(0, 0, 0, 0, &sleeptime);
73 #define PRINT_ERROR(code,msg) \
74 std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
75 << ", code: " << code \
76 << ", msg: " << msg << "." << std::endl
77 #define MYSQLERROR(mysql) { \
78 PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
80 #define APIERROR(error) { \
81 PRINT_ERROR(error.code,error.message); \
84 #ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
130 void asynchExitHandler(
Ndb * m_ndb) ;
145 void drop_table(
MYSQL &mysql);
157 transaction[cb->transaction].conn = 0;
158 transaction[cb->transaction].used = 0;
174 if (asynchErrorHandler(trans, (
Ndb*)cbData->ndb))
176 closeTransaction((
Ndb*)cbData->ndb, cbData);
177 while(populate((
Ndb*)cbData->ndb, cbData->data, cbData) < 0)
182 std::cout <<
"Restore: Failed to restore data "
183 <<
"due to a unrecoverable error. Exiting..." << std::endl;
185 asynchExitHandler((
Ndb*)cbData->ndb);
193 closeTransaction((
Ndb*)cbData->ndb, cbData);
204 while (mysql_query(&mysql,
207 " (REG_NO INT UNSIGNED NOT NULL,"
208 " BRAND CHAR(20) NOT NULL,"
209 " COLOR CHAR(20) NOT NULL,"
210 " PRIMARY KEY USING HASH (REG_NO))"
213 if (mysql_errno(&mysql) != ER_TABLE_EXISTS_ERROR)
215 std::cout <<
"MySQL Cluster already has example table: api_async. "
216 <<
"Dropping it..." << std::endl;
224 void drop_table(
MYSQL &mysql)
226 if (mysql_query(&mysql,
"DROP TABLE api_async"))
231 void asynchExitHandler(
Ndb * m_ndb)
268 std::cout << error.
message << std::endl;
283 std::cout << error.
message << std::endl;
290 static int nPreparedTransactions = 0;
291 static int MAX_RETRIES = 10;
292 static int parallelism = 100;
312 for(
int i=0;
i<1024;
i++)
314 if(transaction[
i].used == 0)
332 retries = cbData->retries;
341 cb->transaction = current;
342 transaction[current].used = 1 ;
349 while(retries < MAX_RETRIES)
352 if (transaction[current].
conn == NULL) {
361 if (myNdbOperation == NULL)
363 if (asynchErrorHandler(transaction[current].
conn, myNdb))
366 transaction[current].conn = 0;
371 asynchExitHandler(myNdb);
374 myNdbOperation->
equal(
"REG_NO", data) < 0 ||
375 myNdbOperation->
setValue(
"BRAND",
"Mercedes") <0 ||
376 myNdbOperation->
setValue(
"COLOR",
"Blue") < 0)
378 if (asynchErrorHandler(transaction[current].conn, myNdb))
381 transaction[current].conn = 0;
386 asynchExitHandler(myNdb);
400 if (nPreparedTransactions == parallelism-1)
405 nPreparedTransactions=0;
408 nPreparedTransactions++;
411 std::cout <<
"Unable to recover from errors. Exiting..." << std::endl;
412 asynchExitHandler(myNdb);
416 int main(
int argc,
char** argv)
420 std::cout <<
"Arguments are <socket mysqld> <connect_string cluster>.\n";
423 char * mysqld_sock = argv[1];
424 const char *connectstring = argv[2];
432 if ( !mysql_init(&mysql) ) {
433 std::cout <<
"mysql_init failed\n";
436 if ( !mysql_real_connect(&mysql,
"localhost",
"root",
"",
"",
440 mysql_query(&mysql,
"CREATE DATABASE ndb_examples");
441 if (mysql_query(&mysql,
"USE ndb_examples") != 0) MYSQLERROR(mysql);
450 if (cluster_connection.connect(4, 5, 1))
452 std::cout <<
"Unable to connect to cluster within 30 secs." << std::endl;
456 if (cluster_connection.wait_until_ready(30,0) < 0)
458 std::cout <<
"Cluster was not ready within 30 secs.\n";
462 Ndb* myNdb =
new Ndb( &cluster_connection,
464 if (myNdb->
init(1024) == -1) {
471 for(
int i = 0 ;
i < 10 ;
i++)
473 transaction[
i].used = 0;
474 transaction[
i].conn = 0;
483 while(populate(myNdb,i,0)<0)
488 std::cout <<
"Number of temporary errors: " << tempErrors << std::endl;