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 
27 #include <NdbOut.hpp>
28 #include <NdbApi.hpp>
29 #include <NdbSleep.h>
30 #include <NDBT.hpp>
31 #include <HugoTransactions.hpp>
32 #include <getarg.h>
33 
34 
main(int argc,const char ** argv)35 int main(int argc, const char** argv){
36   ndb_init();
37 
38   int _records = 0;
39   int _help = 0;
40   int _batch = 512;
41   int _loops = 0;
42   int _rand = 0;
43   int _onetrans = 0;
44   int _abort = 0;
45   const char* db = 0;
46 
47   struct getargs args[] = {
48     { "records", 'r', arg_integer, &_records, "Number of records", "recs" },
49     { "batch", 'b', arg_integer, &_batch, "Number of operations in each transaction", "batch" },
50     { "loops", 'l', arg_integer, &_loops, "Number of loops", "" },
51     { "database", 'd', arg_string, &db, "Database", "" },
52     { "usage", '?', arg_flag, &_help, "Print help", "" },
53     { "rnd-rows", 0, arg_flag, &_rand, "Rand number of records", "recs" },
54     { "one-trans", 0, arg_flag, &_onetrans, "Insert as 1 trans", "" },
55     { "abort", 0, arg_integer, &_abort, "Abort probability", "" }
56   };
57   int num_args = sizeof(args) / sizeof(args[0]);
58   int optind = 0;
59   char desc[] =
60     "tabname\n"\
61     "This program will load one table in Ndb with calculated data. \n"\
62     "This means that it is possible to check the validity of the data \n"\
63     "at a later time. The last column in each table is used as an update \n"\
64     "counter, it's initialised to zero and should be incremented for each \n"\
65     "update of the record. \n";
66 
67   if(getarg(args, num_args, argc, argv, &optind) ||
68      argv[optind] == NULL || _records == 0 || _help) {
69     arg_printusage(args, num_args, argv[0], desc);
70     return NDBT_ProgramExit(NDBT_WRONGARGS);
71   }
72 
73 
74   // Connect to Ndb
75   Ndb_cluster_connection con;
76   if(con.connect(12, 5, 1) != 0)
77   {
78     return NDBT_ProgramExit(NDBT_FAILED);
79   }
80 
81   if (con.wait_until_ready(30,0) < 0)
82   {
83     ndbout << "Cluster nodes not ready in 30 seconds." << endl;
84     return NDBT_ProgramExit(NDBT_FAILED);
85   }
86 
87   Ndb MyNdb( &con, db ? db : "TEST_DB" );
88 
89   if(MyNdb.init() != 0){
90     NDB_ERR(MyNdb.getNdbError());
91     return NDBT_ProgramExit(NDBT_FAILED);
92   }
93 
94   for(int i = optind; i<argc; i++)
95   {
96     const char* _tabname = argv[i];
97     // Check if table exists in db
98     const NdbDictionary::Table* pTab =
99       NDBT_Table::discoverTableFromDb(&MyNdb, _tabname);
100     if(pTab == NULL){
101       ndbout << " Table " << _tabname << " does not exist!" << endl;
102       return NDBT_ProgramExit(NDBT_WRONGARGS);
103     }
104 
105     HugoTransactions hugoTrans(*pTab);
106 loop:
107     int rows = (_rand ? rand() % _records : _records);
108     int abort = (rand() % 100) < _abort ? 1 : 0;
109     if (abort)
110       ndbout << "load+abort" << endl;
111     if (hugoTrans.loadTable(&MyNdb,
112 			    rows,
113 			    _batch,
114 			    true, 0, _onetrans, _loops, abort) != 0){
115       return NDBT_ProgramExit(NDBT_FAILED);
116     }
117 
118     if(_loops > 0)
119     {
120       ndbout << "clearing..." << endl;
121       hugoTrans.clearTable(&MyNdb);
122       //hugoTrans.pkDelRecords(&MyNdb, _records);
123       _loops--;
124       goto loop;
125     }
126   }
127 
128   return NDBT_ProgramExit(NDBT_OK);
129 }
130