MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AtrtClient.cpp
1 /*
2  Copyright (C) 2008 MySQL AB, 2009 Sun Microsystems, Inc.
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 <AtrtClient.hpp>
20 #include <NDBT_Output.hpp>
21 #include <NdbSleep.h>
22 #include <NdbEnv.h>
23 
24 AtrtClient::AtrtClient(const char* _group_suffix)
25  : DbUtil("atrt", _group_suffix)
26 {
27 }
28 
29 
30 AtrtClient::AtrtClient(MYSQL* mysql)
31  : DbUtil(mysql)
32 {
33 }
34 
35 
36 AtrtClient::~AtrtClient(){
37 }
38 
39 
40 int
41 AtrtClient::writeCommand(AtrtCommandType _type,
42  const Properties& args){
43  if (!isConnected())
44  return false;
45 
46  BaseString sql;
47  sql.assfmt("INSERT command ( ");
48 
49  const char* name;
50  {
51  Properties::Iterator iter(&args);
52  while((name= iter.next())){
53  sql.appfmt("%s, ", name);
54  }
55  }
56 
57  sql.appfmt(" state, cmd) VALUES (");
58 
59  {
60  Properties::Iterator iter(&args);
61  while((name= iter.next())){
62  PropertiesType t;
63  Uint32 val_i;
64  BaseString val_s;
65  args.getTypeOf(name, &t);
66  switch(t) {
67  case PropertiesType_Uint32:
68  args.get(name, &val_i);
69  sql.appfmt("%d, ", val_i);
70  break;
71  case PropertiesType_char:
72  args.get(name, val_s);
73  sql.appfmt("'%s', ", val_s.c_str());
74  break;
75  default:
76  assert(false);
77  break;
78  }
79  }
80  }
81 
82  sql.appfmt("'new', %d)", _type);
83  if (!doQuery(sql)){
84  return -1;
85  }
86 
87  return (int)mysql_insert_id(m_mysql);
88 }
89 
90 
91 bool
92 AtrtClient::readCommand(uint command_id,
93  SqlResultSet& result){
94  Properties args;
95  args.put("0", command_id);
96  return runQuery("SELECT * FROM command WHERE id = ?",
97  args,
98  result);
99 }
100 
101 
102 bool
103 AtrtClient::doCommand(AtrtCommandType type,
104  const Properties& args){
105 
106  int running_timeout= 10;
107  int total_timeout= 120;
108  int commandId= writeCommand(type,
109  args);
110  if (commandId == -1){
111  g_err << "Failed to write command" << endl;
112  return false;
113  }
114 
115  while (true){
116 
117  SqlResultSet result;
118  if (!readCommand(commandId, result))
119  {
120  result.print();
121  g_err << "Failed to read command "<< commandId << endl;
122  return false;
123  }
124 
125  // Get first row
126  result.next();
127 
128  // Check if command has completed
129  BaseString state(result.column("state"));
130  if (state == "done") {
131  return true;
132  }
133 
134  if (state == "new"){
135  if (!running_timeout--){
136  g_err << "Timeout while waiting for command "
137  << commandId << " to start run" << endl;
138  return false;
139  }
140  }
141  else if (!total_timeout--){
142  g_err << "Timeout while waiting for result of command "
143  << commandId << endl;
144  return false;
145  }
146 
147 
148  NdbSleep_SecSleep(1);
149  }
150 
151  return false;
152 }
153 
154 
155 bool
156 AtrtClient::changeVersion(int process_id,
157  const char* process_args){
158  Properties args;
159  args.put("process_id", process_id);
160  args.put("process_args", process_args);
161  return doCommand(ATCT_CHANGE_VERSION, args);
162 }
163 
164 
165 bool
166 AtrtClient::resetProc(int process_id){
167  Properties args;
168  args.put("process_id", process_id);
169  return doCommand(ATCT_RESET_PROC, args);
170 }
171 
172 
173 bool
174 AtrtClient::getConnectString(int cluster_id, SqlResultSet& result){
175  Properties args;
176  args.put("0", cluster_id);
177  return doQuery("SELECT value as connectstring " \
178  "FROM cluster c, process p, host h, options o " \
179  "WHERE c.id=p.cluster_id AND p.host_id=h.id AND " \
180  "p.id=o.process_id AND c.id=? AND " \
181  "o.name='--ndb-connectstring=' AND type='ndb_mgmd'",
182  args,
183  result);
184 }
185 
186 
187 bool
188 AtrtClient::getClusters(SqlResultSet& result){
189  Properties args;
190  return runQuery("SELECT id, name FROM cluster WHERE name != '.atrt'",
191  args,
192  result);
193 }
194 
195 
196 bool
197 AtrtClient::getMgmds(int cluster_id, SqlResultSet& result){
198  Properties args;
199  args.put("0", cluster_id);
200  return runQuery("SELECT * FROM process WHERE cluster_id=? and type='ndb_mgmd'",
201  args,
202  result);
203 }
204 
205 bool
206 AtrtClient::getNdbds(int cluster_id, SqlResultSet& result){
207  Properties args;
208  args.put("0", cluster_id);
209  return runQuery("SELECT * FROM process WHERE cluster_id=? and type='ndbd'",
210  args,
211  result);
212 }
213 
214 int
216 {
220  char buf[100];
221  if (NdbEnv_GetEnv("ATRT_PID", buf, sizeof(buf)))
222  {
223  return atoi(buf);
224  }
225  return -1;
226 }