1 /*
2    Copyright (c) 2003, 2016, 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, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include <ndb_global.h>
26 
27 #include <NdbOut.hpp>
28 
29 #include <NdbApi.hpp>
30 #include <NDBT.hpp>
31 #include <NdbSleep.h>
32 #include <getarg.h>
33 
34 #include <HugoTransactions.hpp>
35 
main(int argc,const char ** argv)36 int main(int argc, const char** argv){
37   ndb_init();
38 
39   int _records = 0;
40   int _loops = 1;
41   int _parallelism = 1;
42   int _ver2 = 0;
43   const char* _tabname = NULL, *db = 0;
44   int _help = 0;
45   int abort= 0;
46 
47   struct getargs args[] = {
48     { "loops", 'l', arg_integer, &_loops, "number of times to run this program(0=infinite loop)", "loops" },
49     { "parallelism", 'p', arg_integer, &_parallelism, "parallelism(1-240)", "para" },
50     { "records", 'r', arg_integer, &_records, "Number of records", "recs" },
51     { "ver2", '2', arg_flag, &_ver2, "Use version 2 of scanUpdateRecords", "" },
52     { "ver2", '1', arg_negative_flag, &_ver2, "Use version 1 of scanUpdateRecords (default)", "" },
53     { "abort", 'a', arg_integer, &abort, "Abort probability", "" },
54     { "usage", '?', arg_flag, &_help, "Print help", "" },
55     { "database", 'd', arg_string, &db, "Database", "" }
56   };
57   int num_args = sizeof(args) / sizeof(args[0]);
58   int optind = 0;
59   char desc[] =
60     "tabname\n"\
61     "This program will scan update all records in one table in Ndb\n";
62 
63   if(getarg(args, num_args, argc, argv, &optind) ||
64      argv[optind] == NULL || _help) {
65     arg_printusage(args, num_args, argv[0], desc);
66     return NDBT_ProgramExit(NDBT_WRONGARGS);
67   }
68   _tabname = argv[optind];
69 
70   // Connect to Ndb
71   Ndb_cluster_connection con;
72   if(con.connect(12, 5, 1) != 0)
73   {
74     return NDBT_ProgramExit(NDBT_FAILED);
75   }
76 
77   if (con.wait_until_ready(30,0) < 0)
78   {
79     ndbout << "Cluster nodes not ready in 30 seconds." << endl;
80     return NDBT_ProgramExit(NDBT_FAILED);
81   }
82 
83   Ndb MyNdb( &con, db ? db : "TEST_DB" );
84 
85   if(MyNdb.init() != 0){
86     NDB_ERR(MyNdb.getNdbError());
87     return NDBT_ProgramExit(NDBT_FAILED);
88   }
89 
90   // Check if table exists in db
91   const NdbDictionary::Table * pTab = NDBT_Table::discoverTableFromDb(&MyNdb, _tabname);
92   if(pTab == NULL){
93     ndbout << " Table " << _tabname << " does not exist!" << endl;
94     return NDBT_ProgramExit(NDBT_WRONGARGS);
95   }
96 
97   HugoTransactions hugoTrans(*pTab);
98   int i = 0;
99   int res = NDBT_FAILED;
100   while (i<_loops || _loops==0) {
101     ndbout << i << ": ";
102     if (_ver2 == 0){
103       res = hugoTrans.scanUpdateRecords(&MyNdb,
104 					_records,
105 					abort % 101,
106 					_parallelism);
107     } else{
108       res = hugoTrans.scanUpdateRecords2(&MyNdb,
109 					 _records,
110 					 abort % 101,
111 					 _parallelism);
112     }
113     if (res != NDBT_OK ){
114       return NDBT_ProgramExit(NDBT_FAILED);
115     }
116     i++;
117     //NdbSleep_MilliSleep(300);
118   }
119 
120   return NDBT_ProgramExit(NDBT_OK);
121 }
122