MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hugoPkDelete.cpp
1 /*
2  Copyright (C) 2003-2007 MySQL AB
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 as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <ndb_global.h>
20 
21 #include <NdbOut.hpp>
22 
23 #include <NdbApi.hpp>
24 #include <NdbMain.h>
25 #include <NDBT.hpp>
26 #include <NDBT_Thread.hpp>
27 #include <NDBT_Stats.hpp>
28 #include <NdbSleep.h>
29 #include <getarg.h>
30 
31 #include <HugoTransactions.hpp>
32 
33 static NDBT_ThreadFunc hugoPkDelete;
34 
35 struct ThrInput {
36  const NdbDictionary::Table* pTab;
37  int records;
38  int batch;
39  int stats;
40 };
41 
42 struct ThrOutput {
43  NDBT_Stats latency;
44 };
45 
46 int main(int argc, const char** argv){
47  ndb_init();
48 
49  int _records = 0;
50  int _loops = 1;
51  int _threads = 1;
52  int _stats = 0;
53  int _batch = 1;
54  const char* _tabname = NULL;
55  int _help = 0;
56 
57  struct getargs args[] = {
58  { "loops", 'l', arg_integer, &_loops, "number of times to run this program(0=infinite loop)", "loops" },
59  { "threads", 't', arg_integer, &_threads, "number of threads (default 1)", "threads" },
60  { "stats", 's', arg_flag, &_stats, "report latency per batch", "stats" },
61  // { "batch", 'b', arg_integer, &_batch, "batch value", "batch" },
62  { "records", 'r', arg_integer, &_records, "Number of records", "records" },
63  { "usage", '?', arg_flag, &_help, "Print help", "" }
64  };
65  int num_args = sizeof(args) / sizeof(args[0]);
66  int optind = 0;
67  char desc[] =
68  "tabname\n"\
69  "This program will delete all records in a table using PK \n";
70 
71  if(getarg(args, num_args, argc, argv, &optind) ||
72  argv[optind] == NULL || _records == 0 || _help) {
73  arg_printusage(args, num_args, argv[0], desc);
74  return NDBT_ProgramExit(NDBT_WRONGARGS);
75  }
76  _tabname = argv[optind];
77 
78  // Connect to Ndb
80  if(con.connect(12, 5, 1) != 0)
81  {
82  return NDBT_ProgramExit(NDBT_FAILED);
83  }
84 
85  if (con.wait_until_ready(30,0) < 0)
86  {
87  ndbout << "Cluster nodes not ready in 30 seconds." << endl;
88  return NDBT_ProgramExit(NDBT_FAILED);
89  }
90 
91 
92  Ndb MyNdb(&con, "TEST_DB" );
93 
94  if(MyNdb.init() != 0){
95  ERR(MyNdb.getNdbError());
96  return NDBT_ProgramExit(NDBT_FAILED);
97  }
98 
99  // Check if table exists in db
100  const NdbDictionary::Table * pTab = NDBT_Table::discoverTableFromDb(&MyNdb, _tabname);
101  if(pTab == NULL){
102  ndbout << " Table " << _tabname << " does not exist!" << endl;
103  return NDBT_ProgramExit(NDBT_WRONGARGS);
104  }
105 
106  // threads
107  NDBT_ThreadSet ths(_threads);
108 
109  // create Ndb object for each thread
110  if (ths.connect(&con, "TEST_DB") == -1) {
111  ndbout << "connect failed: err=" << ths.get_err() << endl;
112  return NDBT_ProgramExit(NDBT_FAILED);
113  }
114 
115  // input is options
116  ThrInput input;
117  ths.set_input(&input);
118  input.pTab = pTab;
119  input.records = _records;
120  input.batch = _batch;
121  input.stats = _stats;
122 
123  // output is stats
124  ThrOutput output;
125  ths.set_output<ThrOutput>();
126 
127  int i = 0;
128  while (i < _loops || _loops == 0) {
129  ndbout << i << ": ";
130 
131  ths.set_func(hugoPkDelete);
132  ths.start();
133  ths.stop();
134 
135  if (ths.get_err())
136  NDBT_ProgramExit(NDBT_FAILED);
137 
138  if (_stats) {
139  NDBT_Stats latency;
140 
141  // add stats from each thread
142  int n;
143  for (n = 0; n < ths.get_count(); n++) {
144  NDBT_Thread& thr = ths.get_thread(n);
145  ThrOutput* output = (ThrOutput*)thr.get_output();
146  latency += output->latency;
147  }
148 
149  ndbout
150  << "latency per batch (us): "
151  << " samples=" << latency.getCount()
152  << " min=" << (int)latency.getMin()
153  << " max=" << (int)latency.getMax()
154  << " mean=" << (int)latency.getMean()
155  << " stddev=" << (int)latency.getStddev()
156  << endl;
157  }
158  i++;
159  }
160 
161  return NDBT_ProgramExit(NDBT_OK);
162 }
163 
164 static void hugoPkDelete(NDBT_Thread& thr)
165 {
166  const ThrInput* input = (const ThrInput*)thr.get_input();
167  ThrOutput* output = (ThrOutput*)thr.get_output();
168 
169  HugoTransactions hugoTrans(*input->pTab);
170  output->latency.reset();
171  if (input->stats)
172  hugoTrans.setStatsLatency(&output->latency);
173 
174  NDBT_ThreadSet& ths = thr.get_thread_set();
175  hugoTrans.setThrInfo(ths.get_count(), thr.get_thread_no());
176 
177  int ret;
178  ret = hugoTrans.pkDelRecords(thr.get_ndb(),
179  input->records,
180  input->batch);
181  if (ret != 0)
182  thr.set_err(ret);
183 }