MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
connect.cpp
1 /*
2  Copyright (c) 2007, 2010, 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 as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include <ndb_global.h>
19 #include <ndb_opts.h>
20 #include <NDBT.hpp>
21 #include <NdbApi.hpp>
22 #include <NdbSleep.h>
23 
24 static int _loop = 25;
25 static int _sleep = 25;
26 static int _drop = 1;
27 static int _subloop = 5;
28 static int _wait_all = 0;
29 
30 typedef uchar* gptr;
31 
32 static struct my_option my_long_options[] =
33 {
34  NDB_STD_OPTS("ndb_desc"),
35  { "loop", 'l', "loops",
36  (gptr*) &_loop, (gptr*) &_loop, 0,
37  GET_INT, REQUIRED_ARG, _loop, 0, 0, 0, 0, 0 },
38  { "sleep", 's', "Sleep (ms) between connection attempt",
39  (gptr*) &_sleep, (gptr*) &_sleep, 0,
40  GET_INT, REQUIRED_ARG, _sleep, 0, 0, 0, 0, 0 },
41  { "drop", 'd',
42  "Drop event operations before disconnect (0 = no, 1 = yes, else rand",
43  (gptr*) &_drop, (gptr*) &_drop, 0,
44  GET_INT, REQUIRED_ARG, _drop, 0, 0, 0, 0, 0 },
45  { "subscribe-loop", NDB_OPT_NOSHORT,
46  "Loop in subscribe/unsubscribe",
47  (uchar**) &_subloop, (uchar**) &_subloop, 0,
48  GET_INT, REQUIRED_ARG, _subloop, 0, 0, 0, 0, 0 },
49  { "wait-all", NDB_OPT_NOSHORT,
50  "Wait for all ndb-nodes (i.e not only some)",
51  (uchar**) &_wait_all, (uchar**) &_wait_all, 0,
52  GET_INT, REQUIRED_ARG, _wait_all, 0, 0, 0, 0, 0 },
53  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
54 };
55 
56 int main(int argc, char** argv){
57  NDB_INIT(argv[0]);
58 
59  const char *load_default_groups[]= { "mysql_cluster",0 };
60  load_defaults("my",load_default_groups,&argc,&argv);
61  int ho_error;
62 #ifndef DBUG_OFF
63  opt_debug= "d:t:O,/tmp/ndb_desc.trace";
64 #endif
65  if ((ho_error=handle_options(&argc, &argv, my_long_options,
66  ndb_std_get_one_option)))
67  return NDBT_ProgramExit(NDBT_WRONGARGS);
68 
69  for (int i = 0; i<_loop; i++)
70  {
71  Ndb_cluster_connection con(opt_ndb_connectstring, opt_ndb_nodeid);
72  if(con.connect(12, 5, 1) != 0)
73  {
74  ndbout << "Unable to connect to management server." << endl;
75  return NDBT_ProgramExit(NDBT_FAILED);
76  }
77 
78  int res = con.wait_until_ready(30,30);
79  if (res < 0 || (_wait_all && res != 0))
80  {
81  ndbout << "Cluster nodes not ready in 30 seconds." << endl;
82  return NDBT_ProgramExit(NDBT_FAILED);
83  }
84 
85  Ndb MyNdb(&con, "TEST_DB");
86  if(MyNdb.init() != 0){
87  ERR(MyNdb.getNdbError());
88  return NDBT_ProgramExit(NDBT_FAILED);
89  }
90 
91  for (int k = _subloop; k >= 1; k--)
92  {
93  if (k > 1 && ((k % 25) == 0))
94  {
95  ndbout_c("subscribe/unsubscribe: %u", _subloop - k);
96  }
98  const NdbDictionary::Dictionary * dict= MyNdb.getDictionary();
99  for (int j = 0; j < argc; j++)
100  {
101  const NdbDictionary::Table * pTab = dict->getTable(argv[j]);
102  if (pTab == 0)
103  {
104  ndbout_c("Failed to retreive table: \"%s\"", argv[j]);
105  }
106 
107  BaseString tmp;
108  tmp.appfmt("EV-%s", argv[j]);
109  NdbEventOperation* pOp = MyNdb.createEventOperation(tmp.c_str());
110  if ( pOp == NULL )
111  {
112  ndbout << "Event operation creation failed: " <<
113  MyNdb.getNdbError() << endl;
114  return NDBT_ProgramExit(NDBT_FAILED);
115  }
116 
117  for (int a = 0; a < pTab->getNoOfColumns(); a++)
118  {
119  pOp->getValue(pTab->getColumn(a)->getName());
120  pOp->getPreValue(pTab->getColumn(a)->getName());
121  }
122 
123  ops.push_back(pOp);
124  if (pOp->execute())
125  {
126  ndbout << "operation execution failed: " << pOp->getNdbError()
127  << endl;
128  k = 1;
129  }
130  }
131 
132  if (_sleep)
133  {
134  NdbSleep_MilliSleep(10 + rand() % _sleep);
135  }
136  else
137  {
138  ndbout_c("NDBT_ProgramExit: SLEEPING OK");
139  while(true) NdbSleep_SecSleep(5);
140  }
141 
142  for (Uint32 i = 0; i<ops.size(); i++)
143  {
144  switch(k == 1 ? _drop : 1){
145  case 0:
146  break;
147  do_drop:
148  case 1:
149  if (MyNdb.dropEventOperation(ops[i]))
150  {
151  ndbout << "drop event operation failed "
152  << MyNdb.getNdbError() << endl;
153  return NDBT_ProgramExit(NDBT_FAILED);
154  }
155  break;
156  default:
157  if ((rand() % 100) > 50)
158  goto do_drop;
159  }
160  }
161  }
162  }
163 
164  return NDBT_ProgramExit(NDBT_OK);
165 }
166 
167 template class Vector<NdbEventOperation*>;