select_count.cpp 5.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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 */


18
#include <ndb_global.h>
19
#include <ndb_opts.h>
20

21 22 23 24 25 26 27 28
#include <NdbOut.hpp>

#include <NdbApi.hpp>
#include <NdbMain.h>
#include <NDBT.hpp> 
#include <NdbSleep.h>
#include <UtilTransactions.hpp>
 
29 30 31 32
static int 
select_count(Ndb* pNdb, const NdbDictionary::Table* pTab,
	     int parallelism,
	     int* count_rows,
joreland@mysql.com's avatar
joreland@mysql.com committed
33
	     UtilTransactions::ScanLock lock);
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
static const char* opt_connect_str= 0;
static const char* _dbname = "TEST_DB";
static int _parallelism = 240;
static int _lock = 0;
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 },
  { "parallelism", 'p', "parallelism",
    (gptr*) &_parallelism, (gptr*) &_parallelism, 0,
    GET_INT, REQUIRED_ARG, 240, 0, 0, 0, 0, 0 }, 
  { "lock", 'l', "Read(0), Read-hold(1), Exclusive(2)",
    (gptr*) &_lock, (gptr*) &_lock, 0,
    GET_INT, 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 print_version()
{
  printf("MySQL distrib %s, for %s (%s)\n",MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
}
static void usage()
{
59 60 61
  char desc[] = 
    "tabname1 ... tabnameN\n"\
    "This program will count the number of records in tables\n";
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  print_version();
  my_print_help(my_long_options);
  my_print_variables(my_long_options);
}
static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
	       char *argument)
{
  switch (optid) {
  case '#':
    DBUG_PUSH(argument ? argument : "d:t:O,/tmp/ndb_select_count.trace");
    break;
  case 'V':
    print_version();
    exit(0);
  case '?':
    usage();
    exit(0);
  }
  return 0;
}

int main(int argc, char** argv){
  NDB_INIT(argv[0]);
86
  const char *load_default_groups[]= { "mysql_cluster",0 };
87 88 89 90 91 92
  load_defaults("my",load_default_groups,&argc,&argv);
  int ho_error;
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
    return NDBT_ProgramExit(NDBT_WRONGARGS);
  if (argc < 1) {
    usage();
93 94 95
    return NDBT_ProgramExit(NDBT_WRONGARGS);
  }

96
  Ndb::setConnectString(opt_connect_str);
97 98 99 100 101 102 103 104 105 106 107 108
  // Connect to Ndb
  Ndb MyNdb(_dbname);

  if(MyNdb.init() != 0){
    ERR(MyNdb.getNdbError());
    return NDBT_ProgramExit(NDBT_FAILED);
  }

  // Connect to Ndb and wait for it to become ready
  while(MyNdb.waitUntilReady() != 0)
    ndbout << "Waiting for ndb to become ready..." << endl;
   
109
  for(int i = 0; i<argc; i++){
110 111 112 113 114 115 116 117
    // Check if table exists in db
    const NdbDictionary::Table * pTab = NDBT_Table::discoverTableFromDb(&MyNdb, argv[i]);
    if(pTab == NULL){
      ndbout << " Table " << argv[i] << " does not exist!" << endl;
      continue;
    }

    int rows = 0;
118 119
    if (select_count(&MyNdb, pTab, _parallelism, &rows, 
		     (UtilTransactions::ScanLock)_lock) != 0){
120 121 122 123 124 125 126 127
      return NDBT_ProgramExit(NDBT_FAILED);
    }
    
    ndbout << rows << " records in table " << argv[i] << endl;
  }
  return NDBT_ProgramExit(NDBT_OK);
}

128 129 130 131
int 
select_count(Ndb* pNdb, const NdbDictionary::Table* pTab,
	     int parallelism,
	     int* count_rows,
joreland@mysql.com's avatar
joreland@mysql.com committed
132
	     UtilTransactions::ScanLock lock){
133 134 135 136 137
  
  int                  retryAttempt = 0;
  const int            retryMax = 100;
  int                  check;
  NdbConnection	       *pTrans;
joreland@mysql.com's avatar
joreland@mysql.com committed
138
  NdbScanOperation	       *pOp;
139 140 141 142 143 144 145 146 147

  while (true){

    if (retryAttempt >= retryMax){
      g_info << "ERROR: has retried this operation " << retryAttempt 
	     << " times, failing!" << endl;
      return NDBT_FAILED;
    }

joreland@mysql.com's avatar
joreland@mysql.com committed
148
    pTrans = pNdb->startTransaction();
149 150 151 152 153 154 155 156 157 158 159
    if (pTrans == NULL) {
      const NdbError err = pNdb->getNdbError();

      if (err.status == NdbError::TemporaryError){
	NdbSleep_MilliSleep(50);
	retryAttempt++;
	continue;
      }
      ERR(err);
      return NDBT_FAILED;
    }
joreland@mysql.com's avatar
joreland@mysql.com committed
160
    pOp = pTrans->getNdbScanOperation(pTab->getName());	
161 162 163 164 165 166
    if (pOp == NULL) {
      ERR(pTrans->getNdbError());
      pNdb->closeTransaction(pTrans);
      return NDBT_FAILED;
    }

joreland@mysql.com's avatar
joreland@mysql.com committed
167
    NdbResultSet * rs = pOp->readTuples(NdbScanOperation::LM_Dirty); 
joreland@mysql.com's avatar
joreland@mysql.com committed
168
    if( rs == 0 ) {
169 170 171 172 173
      ERR(pTrans->getNdbError());
      pNdb->closeTransaction(pTrans);
      return NDBT_FAILED;
    }

joreland@mysql.com's avatar
joreland@mysql.com committed
174 175

    check = pOp->interpret_exit_last_row();
176 177 178 179 180 181
    if( check == -1 ) {
      ERR(pTrans->getNdbError());
      pNdb->closeTransaction(pTrans);
      return NDBT_FAILED;
    }
  
182
    Uint64 tmp;
joreland@mysql.com's avatar
joreland@mysql.com committed
183 184
    pOp->getValue(NdbDictionary::Column::ROW_COUNT, (char*)&tmp);
    
joreland@mysql.com's avatar
joreland@mysql.com committed
185
    check = pTrans->execute(NoCommit);
186 187 188 189 190
    if( check == -1 ) {
      ERR(pTrans->getNdbError());
      pNdb->closeTransaction(pTrans);
      return NDBT_FAILED;
    }
joreland@mysql.com's avatar
joreland@mysql.com committed
191 192
    
    Uint64 row_count = 0;
193
    int eof;
joreland@mysql.com's avatar
joreland@mysql.com committed
194 195
    while((eof = rs->nextResult(true)) == 0){
      row_count += tmp;
196
    }
joreland@mysql.com's avatar
joreland@mysql.com committed
197
    
198 199
    if (eof == -1) {
      const NdbError err = pTrans->getNdbError();
joreland@mysql.com's avatar
joreland@mysql.com committed
200
      
201 202 203 204 205 206 207 208 209 210
      if (err.status == NdbError::TemporaryError){
	pNdb->closeTransaction(pTrans);
	NdbSleep_MilliSleep(50);
	retryAttempt++;
	continue;
      }
      ERR(err);
      pNdb->closeTransaction(pTrans);
      return NDBT_FAILED;
    }
joreland@mysql.com's avatar
joreland@mysql.com committed
211
    
212 213 214
    pNdb->closeTransaction(pTrans);
    
    if (count_rows != NULL){
joreland@mysql.com's avatar
joreland@mysql.com committed
215
      *count_rows = row_count;
216 217 218 219 220 221
    }
    
    return NDBT_OK;
  }
  return NDBT_FAILED;
}
222 223