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 */
|
|
|
|
|
2004-05-07 08:39:42 +02:00
|
|
|
#include <ndb_global.h>
|
2004-11-01 14:55:43 +01:00
|
|
|
#include <ndb_opts.h>
|
2004-04-14 10:53:21 +02:00
|
|
|
|
|
|
|
#include <NdbOut.hpp>
|
|
|
|
#include <NdbApi.hpp>
|
|
|
|
#include <NdbSleep.h>
|
|
|
|
#include <NDBT.hpp>
|
|
|
|
|
2004-06-03 20:25:46 +02:00
|
|
|
static int clear_table(Ndb* pNdb, const NdbDictionary::Table* pTab, int parallelism=240);
|
2004-04-14 10:53:21 +02:00
|
|
|
|
2004-12-20 12:36:14 +01:00
|
|
|
NDB_STD_OPTS_VARS;
|
|
|
|
|
2004-11-01 14:55:43 +01:00
|
|
|
static const char* _dbname = "TEST_DB";
|
|
|
|
static struct my_option my_long_options[] =
|
|
|
|
{
|
|
|
|
NDB_STD_OPTS("ndb_desc"),
|
|
|
|
{ "database", 'd', "Name of database table is in",
|
|
|
|
(gptr*) &_dbname, (gptr*) &_dbname, 0,
|
|
|
|
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
|
|
|
|
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
static void usage()
|
|
|
|
{
|
2004-04-14 10:53:21 +02:00
|
|
|
char desc[] =
|
|
|
|
"tabname\n"\
|
|
|
|
"This program will delete all records in the specified table using scan delete.\n";
|
2005-01-28 00:42:41 +01:00
|
|
|
ndb_std_print_version();
|
2004-11-01 14:55:43 +01:00
|
|
|
my_print_help(my_long_options);
|
|
|
|
my_print_variables(my_long_options);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv){
|
|
|
|
NDB_INIT(argv[0]);
|
2004-11-22 09:06:39 +01:00
|
|
|
const char *load_default_groups[]= { "mysql_cluster",0 };
|
2004-11-01 14:55:43 +01:00
|
|
|
load_defaults("my",load_default_groups,&argc,&argv);
|
|
|
|
int ho_error;
|
2005-02-21 23:15:30 +01:00
|
|
|
#ifndef DBUG_OFF
|
|
|
|
opt_debug= "d:t:O,/tmp/ndb_delete_all.trace";
|
|
|
|
#endif
|
|
|
|
if ((ho_error=handle_options(&argc, &argv, my_long_options,
|
|
|
|
ndb_std_get_one_option)))
|
2004-11-01 14:55:43 +01:00
|
|
|
return NDBT_ProgramExit(NDBT_WRONGARGS);
|
2004-04-14 10:53:21 +02:00
|
|
|
|
2005-01-01 19:25:31 +01:00
|
|
|
Ndb_cluster_connection con(opt_connect_str);
|
|
|
|
if(con.connect(12, 5, 1) != 0)
|
|
|
|
{
|
2005-01-05 17:59:24 +01:00
|
|
|
ndbout << "Unable to connect to management server." << endl;
|
|
|
|
return NDBT_ProgramExit(NDBT_FAILED);
|
|
|
|
}
|
|
|
|
if (con.wait_until_ready(30,0) < 0)
|
|
|
|
{
|
|
|
|
ndbout << "Cluster nodes not ready in 30 seconds." << endl;
|
2005-01-01 19:25:31 +01:00
|
|
|
return NDBT_ProgramExit(NDBT_FAILED);
|
|
|
|
}
|
2004-04-14 10:53:21 +02:00
|
|
|
|
2005-01-05 17:59:24 +01:00
|
|
|
Ndb MyNdb(&con, _dbname );
|
2004-04-14 10:53:21 +02:00
|
|
|
if(MyNdb.init() != 0){
|
|
|
|
ERR(MyNdb.getNdbError());
|
|
|
|
return NDBT_ProgramExit(NDBT_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if table exists in db
|
|
|
|
int res = NDBT_OK;
|
2004-11-01 14:55:43 +01:00
|
|
|
for(int i = 0; i<argc; i++){
|
2004-04-14 10:53:21 +02:00
|
|
|
const NdbDictionary::Table * pTab = NDBT_Table::discoverTableFromDb(&MyNdb, argv[i]);
|
|
|
|
if(pTab == NULL){
|
2004-11-01 14:55:43 +01:00
|
|
|
ndbout << " Table " << argv[i] << " does not exist!" << endl;
|
2004-04-14 10:53:21 +02:00
|
|
|
return NDBT_ProgramExit(NDBT_WRONGARGS);
|
|
|
|
}
|
|
|
|
ndbout << "Deleting all from " << argv[i] << "...";
|
2004-06-03 20:25:46 +02:00
|
|
|
if(clear_table(&MyNdb, pTab) == NDBT_FAILED){
|
|
|
|
res = NDBT_FAILED;
|
2004-04-14 10:53:21 +02:00
|
|
|
ndbout << "FAILED" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NDBT_ProgramExit(res);
|
|
|
|
}
|
|
|
|
|
2004-06-03 20:25:46 +02:00
|
|
|
|
|
|
|
int clear_table(Ndb* pNdb, const NdbDictionary::Table* pTab, int parallelism)
|
|
|
|
{
|
|
|
|
// Scan all records exclusive and delete
|
|
|
|
// them one by one
|
|
|
|
int retryAttempt = 0;
|
|
|
|
const int retryMax = 10;
|
|
|
|
int deletedRows = 0;
|
|
|
|
int check;
|
2005-01-04 08:30:48 +01:00
|
|
|
NdbTransaction *pTrans;
|
2004-06-03 20:25:46 +02:00
|
|
|
NdbScanOperation *pOp;
|
|
|
|
NdbError err;
|
|
|
|
|
|
|
|
int par = parallelism;
|
|
|
|
while (true){
|
|
|
|
restart:
|
|
|
|
if (retryAttempt++ >= retryMax){
|
|
|
|
g_info << "ERROR: has retried this operation " << retryAttempt
|
|
|
|
<< " times, failing!" << endl;
|
|
|
|
return NDBT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
pTrans = pNdb->startTransaction();
|
|
|
|
if (pTrans == NULL) {
|
|
|
|
err = pNdb->getNdbError();
|
|
|
|
if (err.status == NdbError::TemporaryError){
|
|
|
|
ERR(err);
|
|
|
|
NdbSleep_MilliSleep(50);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
pOp = pTrans->getNdbScanOperation(pTab->getName());
|
|
|
|
if (pOp == NULL) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2005-01-05 17:59:24 +01:00
|
|
|
if( pOp->readTuples(NdbOperation::LM_Exclusive,par) ) {
|
2004-06-03 20:25:46 +02:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2005-01-05 17:59:24 +01:00
|
|
|
if(pTrans->execute(NdbTransaction::NoCommit) != 0){
|
2004-06-03 20:25:46 +02:00
|
|
|
err = pTrans->getNdbError();
|
|
|
|
if(err.status == NdbError::TemporaryError){
|
|
|
|
ERR(err);
|
|
|
|
pNdb->closeTransaction(pTrans);
|
|
|
|
NdbSleep_MilliSleep(50);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2004-12-07 15:15:49 +01:00
|
|
|
while((check = pOp->nextResult(true)) == 0){
|
2004-06-03 20:25:46 +02:00
|
|
|
do {
|
2004-12-07 15:15:49 +01:00
|
|
|
if (pOp->deleteCurrentTuple() != 0){
|
2004-06-03 20:25:46 +02:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
deletedRows++;
|
2004-12-07 15:15:49 +01:00
|
|
|
} while((check = pOp->nextResult(false)) == 0);
|
2004-06-03 20:25:46 +02:00
|
|
|
|
|
|
|
if(check != -1){
|
2005-01-05 17:59:24 +01:00
|
|
|
check = pTrans->execute(NdbTransaction::Commit);
|
2004-06-16 13:27:35 +02:00
|
|
|
pTrans->restart();
|
2004-06-03 20:25:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = pTrans->getNdbError();
|
|
|
|
if(check == -1){
|
|
|
|
if(err.status == NdbError::TemporaryError){
|
|
|
|
ERR(err);
|
|
|
|
pNdb->closeTransaction(pTrans);
|
|
|
|
NdbSleep_MilliSleep(50);
|
|
|
|
par = 1;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(check == -1){
|
|
|
|
err = pTrans->getNdbError();
|
|
|
|
if(err.status == NdbError::TemporaryError){
|
|
|
|
ERR(err);
|
|
|
|
pNdb->closeTransaction(pTrans);
|
|
|
|
NdbSleep_MilliSleep(50);
|
|
|
|
par = 1;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
pNdb->closeTransaction(pTrans);
|
|
|
|
return NDBT_OK;
|
|
|
|
}
|
|
|
|
return NDBT_FAILED;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
if(pTrans != 0) pNdb->closeTransaction(pTrans);
|
|
|
|
ERR(err);
|
|
|
|
return (err.code != 0 ? err.code : NDBT_FAILED);
|
|
|
|
}
|