1 /*
2    Copyright (c) 2003, 2021, Oracle and/or its affiliates.
3     All rights reserved. Use is subject to license terms.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License, version 2.0,
7    as published by the Free Software Foundation.
8 
9    This program is also distributed with certain software (including
10    but not limited to OpenSSL) that is licensed under separate terms,
11    as designated in a particular file or component or in included license
12    documentation.  The authors of MySQL hereby grant you an additional
13    permission to link the program and your derivative works with the
14    separately licensed software that they have included with MySQL.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License, version 2.0, for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 */
25 
26 #include <ndb_global.h>
27 
28 #include <NdbOut.hpp>
29 
30 #include <NdbApi.hpp>
31 #include <NdbMain.h>
32 #include <NDBT.hpp>
33 #include <NdbSleep.h>
34 #include <getarg.h>
35 
36 #include <HugoTransactions.hpp>
37 
main(int argc,const char ** argv)38 int main(int argc, const char** argv){
39   ndb_init();
40 
41   int _records = 0;
42   int _loops = 1;
43   int _abort = 0;
44   int _parallelism = 1;
45   const char* _tabname = NULL, *db = 0;
46   int _help = 0;
47   int lock = NdbOperation::LM_Read;
48   int sorted = 0;
49 
50   struct getargs args[] = {
51     { "aborts", 'a', arg_integer, &_abort, "percent of transactions that are aborted", "abort%" },
52     { "loops", 'l', arg_integer, &_loops, "number of times to run this program(0=infinite loop)", "loops" },
53     { "parallelism", 'p', arg_integer, &_parallelism, "parallelism(1-240)", "para" },
54     { "records", 'r', arg_integer, &_records, "Number of records", "recs" },
55     { "usage", '?', arg_flag, &_help, "Print help", "" },
56     { "lock", 'm', arg_integer, &lock, "lock mode", "" },
57     { "sorted", 's', arg_flag, &sorted, "sorted", "" },
58     { "database", 'd', arg_string, &db, "Database", "" }
59   };
60   int num_args = sizeof(args) / sizeof(args[0]);
61   int optind = 0;
62   char desc[] =
63     " tabname\n"\
64     "This program will scan read all records in one table in Ndb.\n"\
65     "It will verify every column read by calculating the expected value.\n";
66 
67   if(getarg(args, num_args, argc, argv, &optind) || argv[optind] == NULL || _help) {
68     arg_printusage(args, num_args, argv[0], desc);
69     return NDBT_ProgramExit(NDBT_WRONGARGS);
70   }
71   _tabname = argv[optind];
72 
73   // Connect to Ndb
74   Ndb_cluster_connection con;
75   if(con.connect(12, 5, 1) != 0)
76   {
77     return NDBT_ProgramExit(NDBT_FAILED);
78   }
79   Ndb MyNdb( &con, db ? db : "TEST_DB" );
80 
81   if(MyNdb.init() != 0){
82     NDB_ERR(MyNdb.getNdbError());
83     return NDBT_ProgramExit(NDBT_FAILED);
84   }
85 
86   while(MyNdb.waitUntilReady() != 0)
87     ndbout << "Waiting for ndb to become ready..." << endl;
88 
89   // Check if table exists in db
90   const NdbDictionary::Table * pTab = NDBT_Table::discoverTableFromDb(&MyNdb, _tabname);
91   if(pTab == NULL){
92     ndbout << " Table " << _tabname << " does not exist!" << endl;
93     return NDBT_ProgramExit(NDBT_WRONGARGS);
94   }
95 
96   const NdbDictionary::Index * pIdx = 0;
97   if(optind+1 < argc)
98   {
99     pIdx = MyNdb.getDictionary()->getIndex(argv[optind+1], _tabname);
100     if(!pIdx)
101       ndbout << " Index " << argv[optind+1] << " not found" << endl;
102     else
103       if(pIdx->getType() != NdbDictionary::Index::OrderedIndex)
104       {
105 	ndbout << " Index " << argv[optind+1] << " is not scannable" << endl;
106 	pIdx = 0;
107       }
108   }
109 
110   HugoTransactions hugoTrans(*pTab);
111   int i = 0;
112   while (i<_loops || _loops==0) {
113     ndbout << i << ": ";
114     if(!pIdx)
115     {
116       if(hugoTrans.scanReadRecords(&MyNdb,
117 				   0,
118 				   _abort,
119 				   _parallelism,
120 				   (NdbOperation::LockMode)lock) != 0)
121       {
122 	return NDBT_ProgramExit(NDBT_FAILED);
123       }
124     }
125     else
126     {
127       if(hugoTrans.scanReadRecords(&MyNdb, pIdx,
128 				   0,
129 				   _abort,
130 				   _parallelism,
131 				   (NdbOperation::LockMode)lock,
132 				   sorted) != 0)
133       {
134 	return NDBT_ProgramExit(NDBT_FAILED);
135       }
136     }
137     i++;
138   }
139 
140   return NDBT_ProgramExit(NDBT_OK);
141 }
142