31 #include <mysqld_error.h>
39 #define TIME_TO_SLEEP_BETWEEN_TRANSACTION_RETRIES 1
41 #define PRINT_ERROR(code,msg) \
42 std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
43 << ", code: " << code \
44 << ", msg: " << msg << "." << std::endl
45 #define MYSQLERROR(mysql) { \
46 PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
52 #define APIERROR(error) \
53 { std::cout << "API ERROR: " << error.code << " " << error.message \
55 << " " << "Status: " << error.status \
56 << ", Classification: " << error.classification << std::endl\
57 << " " << "File: " << __FILE__ \
58 << " (Line: " << __LINE__ << ")" << std::endl \
65 #define TRANSERROR(ndbTransaction) \
66 { NdbError error = ndbTransaction->getNdbError(); \
67 std::cout << "TRANS ERROR: " << error.code << " " << error.message \
69 << " " << "Status: " << error.status \
70 << ", Classification: " << error.classification << std::endl \
71 << " " << "File: " << __FILE__ \
72 << " (Line: " << __LINE__ << ")" << std::endl \
74 printTransactionError(ndbTransaction); \
86 std::cout <<
" OPERATION " << i+1 <<
": "
88 <<
" Status: " << error.
status
108 if (myOperation == NULL)
return -1;
111 myOperation->
equal(
"ATTR1", transactionId) ||
112 myOperation->
setValue(
"ATTR2", transactionId)) {
126 int executeInsertTransaction(
int transactionId,
Ndb* myNdb,
129 int noOfRetriesLeft = 10;
133 while (noOfRetriesLeft > 0 && !result) {
139 if (myTransaction == NULL) {
143 }
else if (insert(transactionId, myTransaction, myTable) ||
144 insert(10000+transactionId, myTransaction, myTable) ||
146 TRANSERROR(myTransaction);
157 switch (ndberror.status) {
161 std::cout <<
"Retrying transaction..." << std::endl;
162 sleep(TIME_TO_SLEEP_BETWEEN_TRANSACTION_RETRIES);
169 std::cout <<
"No retry of transaction..." << std::endl;
178 if (myTransaction != NULL) {
183 if (result != 1) exit(-1);
192 while(mysql_query(&mysql,
195 " (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY,"
196 " ATTR2 INT UNSIGNED NOT NULL)"
199 if (mysql_errno(&mysql) == ER_TABLE_EXISTS_ERROR)
201 std::cout <<
"MySQL Cluster already has example table: api_scan. "
202 <<
"Dropping it..." << std::endl;
203 mysql_query(&mysql,
"DROP TABLE api_retries");
205 else MYSQLERROR(mysql);
210 int main(
int argc,
char** argv)
214 std::cout <<
"Arguments are <socket mysqld> <connect_string cluster>.\n";
217 char * mysqld_sock = argv[1];
218 const char *connectstring = argv[2];
224 int r= cluster_connection->
connect(5 ,
230 <<
"Cluster connect failed, possibly resolved with more retries.\n";
236 <<
"Cluster connect failed.\n";
242 std::cout <<
"Cluster was not ready within 30 secs." << std::endl;
247 if ( !mysql_init(&mysql) ) {
248 std::cout <<
"mysql_init failed\n";
251 if ( !mysql_real_connect(&mysql,
"localhost",
"root",
"",
"",
258 mysql_query(&mysql,
"CREATE DATABASE ndb_examples");
259 if (mysql_query(&mysql,
"USE ndb_examples") != 0) MYSQLERROR(mysql);
262 Ndb* myNdb=
new Ndb( cluster_connection,
265 if (myNdb->
init() == -1) {
281 std::cout <<
"Ready to insert rows. You will see notices for temporary "
282 "errors, permenant errors, and retries. \n";
283 for (
int i = 10000; i < 20000; i++) {
284 executeInsertTransaction(i, myNdb, myTable);
286 std::cout <<
"Done.\n";
289 delete cluster_connection;