mariadb/ndb/ndbapi-examples/ndbapi_async_example/ndbapi_async.cpp

477 lines
12 KiB
C++
Raw Normal View History

2004-04-14 10:53:21 +02:00
/* Copyright (C) 2003 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/**
* ndbapi_async.cpp:
* Illustrates how to use callbacks and error handling using the asynchronous
* part of the NDBAPI.
*
* Classes and methods in NDBAPI used in this example:
*
* Ndb_cluster_connection
* connect()
* wait_until_ready()
*
2004-04-14 10:53:21 +02:00
* Ndb
* init()
* startTransaction()
* closeTransaction()
* sendPollNdb()
* getNdbError()
*
* NdbConnection
* getNdbOperation()
* executeAsynchPrepare()
* getNdbError()
*
* NdbOperation
* insertTuple()
* equal()
* setValue()
*
*/
#include <mysql.h>
#include <mysqld_error.h>
2004-04-14 10:53:21 +02:00
#include <NdbApi.hpp>
2004-04-14 10:53:21 +02:00
#include <iostream> // Used for cout
/**
* Helper sleep function
*/
static void
2004-04-14 10:53:21 +02:00
milliSleep(int milliseconds){
struct timeval sleeptime;
2004-04-14 10:53:21 +02:00
sleeptime.tv_sec = milliseconds / 1000;
sleeptime.tv_usec = (milliseconds - (sleeptime.tv_sec * 1000)) * 1000000;
select(0, 0, 0, 0, &sleeptime);
2004-04-14 10:53:21 +02:00
}
2004-04-14 10:53:21 +02:00
/**
* error printout macro
*/
#define PRINT_ERROR(code,msg) \
std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
<< ", code: " << code \
<< ", msg: " << msg << "." << std::endl
#define MYSQLERROR(mysql) { \
PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
exit(-1); }
#define APIERROR(error) { \
PRINT_ERROR(error.code,error.message); \
exit(-1); }
2004-04-14 10:53:21 +02:00
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
/**
* callback struct.
* transaction : index of the transaction in transaction[] array below
* data : the data that the transaction was modifying.
* retries : counter for how many times the trans. has been retried
*/
typedef struct {
Ndb * ndb;
int transaction;
int data;
int retries;
} async_callback_t;
/**
* Structure used in "free list" to a NdbTransaction
2004-04-14 10:53:21 +02:00
*/
typedef struct {
NdbTransaction* conn;
2004-04-14 10:53:21 +02:00
int used;
} transaction_t;
/**
* Free list holding transactions
*/
transaction_t transaction[1024]; //1024 - max number of outstanding
//transaction in one Ndb object
#endif
/**
* prototypes
*/
/**
* Prepare and send transaction
*/
int populate(Ndb * myNdb, int data, async_callback_t * cbData);
/**
* Error handler.
*/
bool asynchErrorHandler(NdbTransaction * trans, Ndb* ndb);
2004-04-14 10:53:21 +02:00
/**
* Exit function
*/
void asynchExitHandler(Ndb * m_ndb) ;
/**
* Helper function used in callback(...)
*/
void closeTransaction(Ndb * ndb , async_callback_t * cb);
/**
* Function to create table
*/
int create_table(Ndb * myNdb);
/**
* stat. variables
*/
int tempErrors = 0;
int permErrors = 0;
void
closeTransaction(Ndb * ndb , async_callback_t * cb)
{
ndb->closeTransaction(transaction[cb->transaction].conn);
transaction[cb->transaction].conn = 0;
transaction[cb->transaction].used = 0;
cb->retries++;
}
/**
* Callback executed when transaction has return from NDB
*/
static void
callback(int result, NdbTransaction* trans, void* aObject)
2004-04-14 10:53:21 +02:00
{
async_callback_t * cbData = (async_callback_t *)aObject;
if (result<0)
{
/**
* Error: Temporary or permanent?
*/
if (asynchErrorHandler(trans, (Ndb*)cbData->ndb))
{
closeTransaction((Ndb*)cbData->ndb, cbData);
while(populate((Ndb*)cbData->ndb, cbData->data, cbData) < 0)
milliSleep(10);
}
else
{
std::cout << "Restore: Failed to restore data "
<< "due to a unrecoverable error. Exiting..." << std::endl;
delete cbData;
asynchExitHandler((Ndb*)cbData->ndb);
}
}
else
{
/**
* OK! close transaction
*/
closeTransaction((Ndb*)cbData->ndb, cbData);
delete cbData;
}
}
/**
* Create table "GARAGE"
*/
int create_table(MYSQL &mysql)
2004-04-14 10:53:21 +02:00
{
while (mysql_query(&mysql,
"CREATE TABLE"
" GARAGE"
" (REG_NO INT UNSIGNED NOT NULL,"
" BRAND CHAR(20) NOT NULL,"
" COLOR CHAR(20) NOT NULL,"
" PRIMARY KEY USING HASH (REG_NO))"
" ENGINE=NDB"))
2004-04-14 10:53:21 +02:00
{
if (mysql_errno(&mysql) != ER_TABLE_EXISTS_ERROR)
MYSQLERROR(mysql);
std::cout << "MySQL Cluster already has example table: GARAGE. "
2004-04-14 10:53:21 +02:00
<< "Dropping it..." << std::endl;
/**************
* Drop table *
**************/
if (mysql_query(&mysql, "DROP TABLE GARAGE"))
MYSQLERROR(mysql);
2004-04-14 10:53:21 +02:00
}
return 1;
}
void asynchExitHandler(Ndb * m_ndb)
{
if (m_ndb != NULL)
delete m_ndb;
exit(-1);
}
/* returns true if is recoverable (temporary),
* false if it is an error that is permanent.
*/
bool asynchErrorHandler(NdbTransaction * trans, Ndb* ndb)
2004-04-14 10:53:21 +02:00
{
NdbError error = trans->getNdbError();
switch(error.status)
{
case NdbError::Success:
return false;
break;
case NdbError::TemporaryError:
/**
* The error code indicates a temporary error.
* The application should typically retry.
* (Includes classifications: NdbError::InsufficientSpace,
* NdbError::TemporaryResourceError, NdbError::NodeRecoveryError,
* NdbError::OverloadError, NdbError::NodeShutdown
* and NdbError::TimeoutExpired.)
*
* We should sleep for a while and retry, except for insufficient space
*/
if(error.classification == NdbError::InsufficientSpace)
return false;
milliSleep(10);
tempErrors++;
return true;
break;
case NdbError::UnknownResult:
std::cout << error.message << std::endl;
return false;
break;
default:
case NdbError::PermanentError:
switch (error.code)
{
case 499:
case 250:
milliSleep(10);
return true; // SCAN errors that can be retried. Requires restart of scan.
default:
break;
}
//ERROR
std::cout << error.message << std::endl;
return false;
break;
}
return false;
}
static int nPreparedTransactions = 0;
static int MAX_RETRIES = 10;
static int parallelism = 100;
/************************************************************************
* populate()
* 1. Prepare 'parallelism' number of insert transactions.
* 2. Send transactions to NDB and wait for callbacks to execute
*/
int populate(Ndb * myNdb, int data, async_callback_t * cbData)
{
NdbOperation* myNdbOperation; // For operations
const NdbDictionary::Dictionary* myDict= myNdb->getDictionary();
const NdbDictionary::Table *myTable= myDict->getTable("GARAGE");
if (myTable == NULL)
APIERROR(myDict->getNdbError());
2004-04-14 10:53:21 +02:00
async_callback_t * cb;
Merge NDB patches into 4.1-clone, below is list of changesets # -------------------------------------------- # 04/04/13 joreland@mysql.com 1.1858.1.1 # Removal of NDBT_Table # -------------------------------------------- # 04/04/13 joreland@mysql.com 1.1858.1.2 # Removal of NDBT_Table # -------------------------------------------- # 04/04/13 joreland@mysql.com 1.1858.1.3 # Fix for crashing AT # -------------------------------------------- # 04/04/13 joreland@mysql.com 1.1858.1.4 # Bug fixes in testIndex # -------------------------------------------- # 04/04/13 joreland@mysql.com 1.1866.1.5 # Merge joreland@bk-internal.mysql.com:/home/bk/mysql-4.1-ndb # into mysql.com:/home/jonas/src/tmp # -------------------------------------------- # 04/04/14 joreland@mysql.com 1.1858.1.5 # Fix for takeover when accessing indexes. # -------------------------------------------- # 04/04/14 joreland@mysql.com 1.1866.1.6 # Merge joreland@bk-internal.mysql.com:/home/bk/mysql-4.1-ndb # into mysql.com:/home/jonas/src/tmp # -------------------------------------------- # 04/04/14 joreland@mysql.com 1.1858.2.1 # Fixed error handling of fire_trigger_ord + lqhkeyref w.r.t indexes # -------------------------------------------- # 04/04/14 joreland@mysql.com 1.1858.2.2 # Simple retry mechanism in UTIL # Helps testIndex -n BuildDuring _a lot_ # -------------------------------------------- # 04/04/14 joreland@mysql.com 1.1858.2.3 # Misc fixes to test prg. # -------------------------------------------- # 04/04/14 joreland@mysql.com 1.1858.2.4 # Allow more retires on application errors # -------------------------------------------- # 04/04/14 joreland@mysql.com 1.1858.2.5 # Pass no of rows fetched in SUB_SYNC_CONTINUE_REQ # -------------------------------------------- # 04/04/14 joreland@mysql.com 1.1869 # wl1714 # Improve parallellism # -------------------------------------------- # 04/04/14 joreland@mysql.com 1.1870 # Merge joreland@bk-internal.mysql.com:/home/bk/mysql-4.1-ndb # into mysql.com:/home/jonas/src/wl1714 # -------------------------------------------- # 04/04/14 joreland@mysql.com 1.1866.1.7 # Merge # -------------------------------------------- # 04/04/14 joreland@mysql.com 1.1871 # Merge mysql.com:/home/jonas/src/wl1714 # into mysql.com:/home/jonas/src/mysql-4.1-ndb # -------------------------------------------- # 04/04/14 joreland@mysql.com 1.1872 # Fix for testScan -n ScanReadError5030 # -------------------------------------------- # 04/04/14 joreland@mysql.com 1.1873 # fix testDict -n FragmentType* # -------------------------------------------- # 04/04/14 johan@stingray.(none) 1.1866.2.1 # small fixes # -------------------------------------------- # 04/04/14 johan@stingray.(none) 1.1874 # Merge jandersson@bk-internal.mysql.com:/home/bk/mysql-4.1-ndb # into stingray.(none):/space/bk/rep/mysql-4.1-ndb # -------------------------------------------- # 04/04/14 johan@stingray.(none) 1.1875 # handle rep node as an api node # -------------------------------------------- # 04/04/14 pekka@mysql.com 1.1873.1.1 # Fix for NDBT_Table removal # -------------------------------------------- # 04/04/15 joreland@mysql.com 1.1873.1.2 # Uninitialized var. # -------------------------------------------- # 04/04/15 johan@stingray.(none) 1.1876 # Merge jandersson@bk-internal.mysql.com:/home/bk/mysql-4.1-ndb # into stingray.(none):/space/bk/rep/mysql-4.1-ndb # -------------------------------------------- # 04/04/15 joreland@mysql.com 1.1873.1.3 # Write pid before changing user # (as runas user probably doesn't have permissions on pid-dir) # -------------------------------------------- # -------------------------------------------- # 04/04/15 ndb@mc03.ndb.mysql.com 1.1862.1.3 # Fix printout of varchars # -------------------------------------------- # 04/04/15 ndb@mc03.ndb.mysql.com 1.1877 # Merge joreland@bk-internal.mysql.com:/home/bk/mysql-4.1-ndb # into mc03.ndb.mysql.com:/space/ndb/tmp # -------------------------------------------- # 04/04/15 ndb@mc03.ndb.mysql.com 1.1862.1.4 # Hmm.. don't print attr as many times as length of array # -------------------------------------------- # 04/04/15 ndb@mc03.ndb.mysql.com 1.1878 # Merge mc03.ndb.mysql.com:/space/ndb/mysql-4.1-ndb # into mc03.ndb.mysql.com:/space/ndb/tmp # -------------------------------------------- # 04/04/15 joreland@mysql.com 1.1876.1.1 # Merge joreland@bk-internal.mysql.com:/home/bk/mysql-4.1-ndb # into mysql.com:/home/jonas/src/mysql-4.1-ndb # -------------------------------------------- # 04/04/15 joreland@mysql.com 1.1877.1.1 # Merge joreland@bk-internal.mysql.com:/home/bk/mysql-4.1-ndb # into mysql.com:/home/jonas/src/mysql-4.1-ndb # -------------------------------------------- # 04/04/15 ndb@mc03.ndb.mysql.com 1.1879 # Merge joreland@bk-internal.mysql.com:/home/bk/mysql-4.1-ndb # into mc03.ndb.mysql.com:/space/ndb/tmp # -------------------------------------------- # 04/04/17 joreland@mysql.com 1.1880 # LCP bug when restarting ops take's longer than DIH sending a new lcp ord # Solution: # Send LCP_FRAG_REP after restarting all ops # -------------------------------------------- # 04/04/18 joreland@mysql.com 1.1881 # Missspelled ERROR_INSERT # -------------------------------------------- # 04/04/18 joreland@mysql.com 1.1882 # Added possibility to log only distributed signals # -------------------------------------------- # 04/04/22 joreland@mysql.com 1.1883 # Removed dependancy to libstdc++ # * pure virtual functions "__cxa_pure_virtual" is defined in libstd++ -> # remove all pure virtual functions # -------------------------------------------- # 04/04/23 joreland@mysql.com 1.1884 # Various minor bug fixes for problems found while compiling # with icc # -------------------------------------------- # -------------------------------------------- # 04/04/25 joreland@mysql.com 1.1885 # Removed debug code (joreland:1.1875) # -------------------------------------------- # 04/04/26 ejonore@mc03.ndb.mysql.com 1.1862.1.5 # Lock pages in memory _after_ daemon-mode (exec/fork) # -------------------------------------------- # 04/04/27 ejonore@mc03.ndb.mysql.com 1.1862.1.6 # 1) Trap abort() # 2) StopStart/StopAborted events # 3) warning if memlock fails # 4) use g_logger more often (instead of ndbout) # # -------------------------------------------- # 04/04/27 joreland@mysql.com 1.1886 # bug fix for a bug in wl1714 + software upgrade # -------------------------------------------- # 04/04/27 joreland@mysql.com 1.1887 # Cset exclude: joreland@mysql.com|ChangeSet|20040422160720|05374 # -------------------------------------------- # 04/04/27 joreland@mysql.com 1.1888 # removed libstc++ second try... # -------------------------------------------- # 04/04/28 ejonore@mc03.ndb.mysql.com 1.1862.1.7 # 1) Don't lock FUTURE pages but only current # 2) Do this when SIZEALT has completed # -------------------------------------------- # 04/04/30 joreland@mysql.com 1.1889 # Merge jonas@orca:/home/ndb/releases/mysql-4.1-ndb-3.4.x # into mysql.com:/home/jonas/src/mysql-4.1-ndb # -------------------------------------------- # 04/05/03 ndb@mc03.ndb.mysql.com 1.1862.1.8 # Version update # -------------------------------------------- # 04/05/03 joreland@mysql.com 1.1862.3.1 # Changed restart behavior. # Use "angel" process which monitor "real" process # -------------------------------------------- # 04/05/03 joreland@mysql.com 1.1862.1.9 # Merge jonas@orca:/home/ndb/releases/mysql-4.1-ndb-3.4.x # into mysql.com:/home/jonas/src/mysql-4.1-ndb-3.4.x # -------------------------------------------- # 04/05/03 joreland@mysql.com 1.1890 # Merge B2 fixes # -------------------------------------------- # -------------------------------------------- # 04/05/03 joreland@mysql.com 1.1862.1.10 # Better handling of children terminations # -------------------------------------------- # 04/05/03 joreland@mysql.com 1.1862.1.11 # Fixes for angel-restarting # -------------------------------------------- # 04/05/03 joreland@mysql.com 1.1891 # Merge mysql.com:/home/jonas/src/mysql-4.1-ndb-3.4.x # into mysql.com:/home/jonas/src/mysql-4.1-ndb # -------------------------------------------- #
2004-05-04 11:19:19 +02:00
int retries = 0;
2004-04-14 10:53:21 +02:00
int current = 0;
for(int i=0; i<1024; i++)
{
if(transaction[i].used == 0)
{
current = i;
if (cbData == 0)
{
/**
* We already have a callback
* This is an absolutely new transaction
*/
cb = new async_callback_t;
cb->retries = 0;
}
else
{
/**
* We already have a callback
*/
cb =cbData;
retries = cbData->retries;
}
/**
* Set data used by the callback
*/
cb->ndb = myNdb; //handle to Ndb object so that we can close transaction
// in the callback (alt. make myNdb global).
cb->data = data; //this is the data we want to insert
cb->transaction = current; //This is the number (id) of this transaction
transaction[current].used = 1 ; //Mark the transaction as used
break;
}
}
if(!current)
return -1;
while(retries < MAX_RETRIES)
{
transaction[current].conn = myNdb->startTransaction();
if (transaction[current].conn == NULL) {
if (asynchErrorHandler(transaction[current].conn, myNdb))
{
/**
* no transaction to close since conn == null
*/
milliSleep(10);
retries++;
continue;
}
asynchExitHandler(myNdb);
}
myNdbOperation = transaction[current].conn->getNdbOperation(myTable);
2004-04-14 10:53:21 +02:00
if (myNdbOperation == NULL)
{
if (asynchErrorHandler(transaction[current].conn, myNdb))
{
myNdb->closeTransaction(transaction[current].conn);
transaction[current].conn = 0;
milliSleep(10);
retries++;
continue;
}
asynchExitHandler(myNdb);
} // if
if(myNdbOperation->insertTuple() < 0 ||
myNdbOperation->equal("REG_NO", data) < 0 ||
myNdbOperation->setValue("BRAND", "Mercedes") <0 ||
myNdbOperation->setValue("COLOR", "Blue") < 0)
{
if (asynchErrorHandler(transaction[current].conn, myNdb))
{
myNdb->closeTransaction(transaction[current].conn);
transaction[current].conn = 0;
retries++;
milliSleep(10);
continue;
}
asynchExitHandler(myNdb);
}
/*Prepare transaction (the transaction is NOT yet sent to NDB)*/
transaction[current].conn->executeAsynchPrepare(NdbTransaction::Commit,
2004-04-14 10:53:21 +02:00
&callback,
cb);
/**
* When we have prepared parallelism number of transactions ->
* send the transaction to ndb.
* Next time we will deal with the transactions are in the
* callback. There we will see which ones that were successful
* and which ones to retry.
*/
if (nPreparedTransactions == parallelism-1)
{
// send-poll all transactions
// close transaction is done in callback
myNdb->sendPollNdb(3000, parallelism );
nPreparedTransactions=0;
}
else
nPreparedTransactions++;
return 1;
}
std::cout << "Unable to recover from errors. Exiting..." << std::endl;
asynchExitHandler(myNdb);
return -1;
}
int main()
{
2004-09-15 11:49:18 +02:00
ndb_init();
MYSQL mysql;
/**************************************************************
* Connect to mysql server and create table *
**************************************************************/
{
if ( !mysql_init(&mysql) ) {
std::cout << "mysql_init failed\n";
exit(-1);
}
if ( !mysql_real_connect(&mysql, "localhost", "root", "", "",
3306, "/tmp/mysql.sock", 0) )
MYSQLERROR(mysql);
mysql_query(&mysql, "CREATE DATABASE TEST_DB");
if (mysql_query(&mysql, "USE TEST_DB") != 0) MYSQLERROR(mysql);
create_table(mysql);
2004-04-14 10:53:21 +02:00
}
/**************************************************************
* Connect to ndb cluster *
**************************************************************/
Ndb_cluster_connection cluster_connection;
if (cluster_connection.connect(4, 5, 1))
{
std::cout << "Unable to connect to cluster within 30 secs." << std::endl;
exit(-1);
}
// Optionally connect and wait for the storage nodes (ndbd's)
if (cluster_connection.wait_until_ready(30,0) < 0)
{
std::cout << "Cluster was not ready within 30 secs.\n";
2004-04-14 10:53:21 +02:00
exit(-1);
}
Ndb* myNdb = new Ndb( &cluster_connection,
"TEST_DB" ); // Object representing the database
if (myNdb->init(1024) == -1) { // Set max 1024 parallel transactions
APIERROR(myNdb->getNdbError());
}
2004-04-14 10:53:21 +02:00
/**
* Initialise transaction array
*/
for(int i = 0 ; i < 1024 ; i++)
{
transaction[i].used = 0;
transaction[i].conn = 0;
}
int i=0;
/**
* Do 20000 insert transactions.
*/
while(i < 20000)
{
while(populate(myNdb,i,0)<0) // <0, no space on free list. Sleep and try again.
milliSleep(10);
i++;
}
std::cout << "Number of temporary errors: " << tempErrors << std::endl;
delete myNdb;
}