MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ndbapi_retries.cpp
1 /*
2  Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 //
19 // ndbapi_retries.cpp: Error handling and transaction retries
20 //
21 // There are many ways to program using the NDB API. In this example
22 // we execute two inserts in the same transaction using
23 // NdbConnection::execute(NoCommit).
24 //
25 // Transaction failing is handled by re-executing the transaction
26 // in case of non-permanent transaction errors.
27 // Application errors (i.e. errors at points marked with APIERROR)
28 // should be handled by the application programmer.
29 
30 #include <mysql.h>
31 #include <mysqld_error.h>
32 #include <NdbApi.hpp>
33 
34 // Used for cout
35 #include <iostream>
36 
37 // Used for sleep (use your own version of sleep)
38 #include <unistd.h>
39 #define TIME_TO_SLEEP_BETWEEN_TRANSACTION_RETRIES 1
40 
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)); \
47  exit(-1); }
48 
49 //
50 // APIERROR prints an NdbError object
51 //
52 #define APIERROR(error) \
53  { std::cout << "API ERROR: " << error.code << " " << error.message \
54  << std::endl \
55  << " " << "Status: " << error.status \
56  << ", Classification: " << error.classification << std::endl\
57  << " " << "File: " << __FILE__ \
58  << " (Line: " << __LINE__ << ")" << std::endl \
59  ; \
60  }
61 
62 //
63 // TRANSERROR prints all error info regarding an NdbTransaction
64 //
65 #define TRANSERROR(ndbTransaction) \
66  { NdbError error = ndbTransaction->getNdbError(); \
67  std::cout << "TRANS ERROR: " << error.code << " " << error.message \
68  << std::endl \
69  << " " << "Status: " << error.status \
70  << ", Classification: " << error.classification << std::endl \
71  << " " << "File: " << __FILE__ \
72  << " (Line: " << __LINE__ << ")" << std::endl \
73  ; \
74  printTransactionError(ndbTransaction); \
75  }
76 
77 void printTransactionError(NdbTransaction *ndbTransaction) {
78  const NdbOperation *ndbOp = NULL;
79  int i=0;
80 
81  /****************************************************************
82  * Print NdbError object of every operations in the transaction *
83  ****************************************************************/
84  while ((ndbOp = ndbTransaction->getNextCompletedOperation(ndbOp)) != NULL) {
85  NdbError error = ndbOp->getNdbError();
86  std::cout << " OPERATION " << i+1 << ": "
87  << error.code << " " << error.message << std::endl
88  << " Status: " << error.status
89  << ", Classification: " << error.classification << std::endl;
90  i++;
91  }
92 }
93 
94 
95 //
96 // Example insert
97 // @param myNdb Ndb object representing NDB Cluster
98 // @param myTransaction NdbTransaction used for transaction
99 // @param myTable Table to insert into
100 // @param error NdbError object returned in case of errors
101 // @return -1 in case of failures, 0 otherwise
102 //
103 int insert(int transactionId, NdbTransaction* myTransaction,
104  const NdbDictionary::Table *myTable) {
105  NdbOperation *myOperation; // For other operations
106 
107  myOperation = myTransaction->getNdbOperation(myTable);
108  if (myOperation == NULL) return -1;
109 
110  if (myOperation->insertTuple() ||
111  myOperation->equal("ATTR1", transactionId) ||
112  myOperation->setValue("ATTR2", transactionId)) {
113  APIERROR(myOperation->getNdbError());
114  exit(-1);
115  }
116 
117  return myTransaction->execute(NdbTransaction::NoCommit);
118 }
119 
120 
121 //
122 // Execute function which re-executes (tries 10 times) the transaction
123 // if there are temporary errors (e.g. the NDB Cluster is overloaded).
124 // @return -1 failure, 1 success
125 //
126 int executeInsertTransaction(int transactionId, Ndb* myNdb,
127  const NdbDictionary::Table *myTable) {
128  int result = 0; // No result yet
129  int noOfRetriesLeft = 10;
130  NdbTransaction *myTransaction; // For other transactions
131  NdbError ndberror;
132 
133  while (noOfRetriesLeft > 0 && !result) {
134 
135  /*********************************
136  * Start and execute transaction *
137  *********************************/
138  myTransaction = myNdb->startTransaction();
139  if (myTransaction == NULL) {
140  APIERROR(myNdb->getNdbError());
141  ndberror = myNdb->getNdbError();
142  result = -1; // Failure
143  } else if (insert(transactionId, myTransaction, myTable) ||
144  insert(10000+transactionId, myTransaction, myTable) ||
145  myTransaction->execute(NdbTransaction::Commit)) {
146  TRANSERROR(myTransaction);
147  ndberror = myTransaction->getNdbError();
148  result = -1; // Failure
149  } else {
150  result = 1; // Success
151  }
152 
153  /**********************************
154  * If failure, then analyze error *
155  **********************************/
156  if (result == -1) {
157  switch (ndberror.status) {
158  case NdbError::Success:
159  break;
161  std::cout << "Retrying transaction..." << std::endl;
162  sleep(TIME_TO_SLEEP_BETWEEN_TRANSACTION_RETRIES);
163  --noOfRetriesLeft;
164  result = 0; // No completed transaction yet
165  break;
166 
169  std::cout << "No retry of transaction..." << std::endl;
170  result = -1; // Permanent failure
171  break;
172  }
173  }
174 
175  /*********************
176  * Close transaction *
177  *********************/
178  if (myTransaction != NULL) {
179  myNdb->closeTransaction(myTransaction);
180  }
181  }
182 
183  if (result != 1) exit(-1);
184  return result;
185 }
186 
187 /*********************************************************
188  * Create a table named api_retries if it does not exist *
189  *********************************************************/
190 static void create_table(MYSQL &mysql)
191 {
192  while(mysql_query(&mysql,
193  "CREATE TABLE "
194  " api_retries"
195  " (ATTR1 INT UNSIGNED NOT NULL PRIMARY KEY,"
196  " ATTR2 INT UNSIGNED NOT NULL)"
197  " ENGINE=NDB"))
198  {
199  if (mysql_errno(&mysql) == ER_TABLE_EXISTS_ERROR)
200  {
201  std::cout << "MySQL Cluster already has example table: api_scan. "
202  << "Dropping it..." << std::endl;
203  mysql_query(&mysql, "DROP TABLE api_retries");
204  }
205  else MYSQLERROR(mysql);
206  }
207 }
208 
209 
210 int main(int argc, char** argv)
211 {
212  if (argc != 3)
213  {
214  std::cout << "Arguments are <socket mysqld> <connect_string cluster>.\n";
215  exit(-1);
216  }
217  char * mysqld_sock = argv[1];
218  const char *connectstring = argv[2];
219  ndb_init();
220 
221  Ndb_cluster_connection *cluster_connection=
222  new Ndb_cluster_connection(connectstring); // Object representing the cluster
223 
224  int r= cluster_connection->connect(5 /* retries */,
225  3 /* delay between retries */,
226  1 /* verbose */);
227  if (r > 0)
228  {
229  std::cout
230  << "Cluster connect failed, possibly resolved with more retries.\n";
231  exit(-1);
232  }
233  else if (r < 0)
234  {
235  std::cout
236  << "Cluster connect failed.\n";
237  exit(-1);
238  }
239 
240  if (cluster_connection->wait_until_ready(30,30))
241  {
242  std::cout << "Cluster was not ready within 30 secs." << std::endl;
243  exit(-1);
244  }
245  // connect to mysql server
246  MYSQL mysql;
247  if ( !mysql_init(&mysql) ) {
248  std::cout << "mysql_init failed\n";
249  exit(-1);
250  }
251  if ( !mysql_real_connect(&mysql, "localhost", "root", "", "",
252  0, mysqld_sock, 0) )
253  MYSQLERROR(mysql);
254 
255  /********************************************
256  * Connect to database via mysql-c *
257  ********************************************/
258  mysql_query(&mysql, "CREATE DATABASE ndb_examples");
259  if (mysql_query(&mysql, "USE ndb_examples") != 0) MYSQLERROR(mysql);
260  create_table(mysql);
261 
262  Ndb* myNdb= new Ndb( cluster_connection,
263  "ndb_examples" ); // Object representing the database
264 
265  if (myNdb->init() == -1) {
266  APIERROR(myNdb->getNdbError());
267  exit(-1);
268  }
269 
270  const NdbDictionary::Dictionary* myDict= myNdb->getDictionary();
271  const NdbDictionary::Table *myTable= myDict->getTable("api_retries");
272  if (myTable == NULL)
273  {
274  APIERROR(myDict->getNdbError());
275  return -1;
276  }
277  /************************************
278  * Execute some insert transactions *
279  ************************************/
280 
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);
285  }
286  std::cout << "Done.\n";
287 
288  delete myNdb;
289  delete cluster_connection;
290 
291  ndb_end(0);
292  return 0;
293 }