MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MgmtSrvr.hpp
1 /*
2  Copyright (c) 2003, 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 #ifndef MgmtSrvr_H
19 #define MgmtSrvr_H
20 
21 #include "Config.hpp"
22 #include "ConfigSubscriber.hpp"
23 
24 #include <mgmapi.h>
25 #include <Vector.hpp>
26 #include <NodeBitmask.hpp>
27 #include <ndb_version.h>
28 #include <EventLogger.hpp>
29 
30 #include <SignalSender.hpp>
31 
32 #define MGM_ERROR_MAX_INJECT_SESSION_ONLY 10000
33 
34 class SetLogLevelOrd;
35 
37 {
38  friend class MgmtSrvr;
39 public:
40  struct Event_listener : public EventLoggerBase {
41  Event_listener() {}
42  NDB_SOCKET_TYPE m_socket;
43  Uint32 m_parsable;
44  };
45 
46 private:
47  class MgmtSrvr * m_mgmsrv;
49 public:
50  Ndb_mgmd_event_service(class MgmtSrvr * m) : m_clients(5) {
51  m_mgmsrv = m;
52  }
53 
54  void add_listener(const Event_listener&);
55  void check_listeners();
56  void update_max_log_level(const LogLevel&);
57  void update_log_level(const LogLevel&);
58 
59  void log(int eventType, const Uint32* theData, Uint32 len, NodeId nodeId);
60 
61  void stop_sessions();
62 
63  Event_listener& operator[](unsigned i) { return m_clients[i]; }
64  const Event_listener& operator[](unsigned i) const { return m_clients[i]; }
65  void lock() { m_clients.lock(); }
66  void unlock(){ m_clients.unlock(); }
67 };
68 
69 
70 
75 class MgmtSrvr : private ConfigSubscriber, public trp_client {
76 
77 public:
78  // some compilers need all of this
79  class Allocated_resources;
80  friend class Allocated_resources;
82  public:
83  Allocated_resources(class MgmtSrvr &m);
85  // methods to reserve/allocate resources which
86  // will be freed when running destructor
87  void reserve_node(NodeId id, NDB_TICKS timeout);
88  bool is_timed_out(NDB_TICKS tick);
89  bool is_reserved(NodeId nodeId) { return m_reserved_nodes.get(nodeId); }
90  bool is_reserved(NodeBitmask mask) { return !mask.bitAND(m_reserved_nodes).isclear(); }
91  bool isclear() { return m_reserved_nodes.isclear(); }
92  NodeId get_nodeid() const;
93  private:
94  MgmtSrvr &m_mgmsrv;
95  NodeBitmask m_reserved_nodes;
96  NDB_TICKS m_alloc_timeout;
97  };
98 
103  enum LogMode {In, Out, InOut, Off};
104 
110  struct MgmtOpts {
111  int daemon;
112  int non_interactive;
113  int interactive;
114  const char* config_filename;
115  int mycnf;
116  int config_cache;
117  const char* bind_address;
118  int no_nodeid_checks;
119  int print_full_config;
120  const char* configdir;
121  int verbose;
122  MgmtOpts() : configdir(MYSQLCLUSTERDIR) {};
123  int reload;
124  int initial;
125  NodeBitmask nowait_nodes;
126  };
127 
128  MgmtSrvr(); // Not implemented
129  MgmtSrvr(const MgmtSrvr&); // Not implemented
130  MgmtSrvr(const MgmtOpts&);
131 
132  ~MgmtSrvr();
133 
134 private:
135  /* Function used from 'init' */
136  const char* check_configdir() const;
137 
138 public:
139  /*
140  To be called after constructor.
141  */
142  bool init();
143 
144  /*
145  To be called after 'init', starts up the services
146  this server will expose
147  */
148  bool start(void);
149 private:
150  /* Functions used from 'start' */
151  bool start_transporter(const Config*);
152  bool start_mgm_service(const Config*);
153  bool connect_to_self(void);
154 
155 public:
156 
157  NodeId getOwnNodeId() const {return _ownNodeId;};
158 
165  int status(int nodeId,
167  Uint32 * version,
168  Uint32 * mysql_version,
169  Uint32 * phase,
170  bool * systemShutdown,
171  Uint32 * dynamicId,
172  Uint32 * nodeGroup,
173  Uint32 * connectCount,
174  const char **address);
175 
179  int stopNodes(const Vector<NodeId> &node_ids, int *stopCount, bool abort,
180  bool force, int *stopSelf);
181 
182  int shutdownMGM(int *stopCount, bool abort, int *stopSelf);
183 
187  int shutdownDB(int * cnt = 0, bool abort = false);
188 
192  int enterSingleUser(int * cnt = 0, Uint32 singleuserNodeId = 0);
193 
194 
198  int exitSingleUser(int * cnt = 0, bool abort = false);
199 
205  int start(int processId);
206 
210  int restartNodes(const Vector<NodeId> &node_ids,
211  int *stopCount, bool nostart,
212  bool initialStart, bool abort, bool force,
213  int *stopSelf);
214 
218  int restartDB(bool nostart, bool initialStart,
219  bool abort = false,
220  int * stopCount = 0);
221 
225  int startBackup(Uint32& backupId, int waitCompleted= 2, Uint32 input_backupId= 0, Uint32 backuppoint= 0);
226  int abortBackup(Uint32 backupId);
227  int performBackup(Uint32* backupId);
228 
229  //**************************************************************************
230  // Description: Set event report level for a DB process
231  // Parameters:
232  // processId: Id of the DB process
233  // level: Event report level
234  // isResend: Flag to indicate for resending log levels during node restart
235  // Returns: 0 if succeeded, otherwise: as stated above, plus:
236  // INVALID_LEVEL
237  //**************************************************************************
238 
239  int setEventReportingLevelImpl(int processId, const EventSubscribeReq& ll);
240  int setNodeLogLevelImpl(int processId, const SetLogLevelOrd & ll);
241 
249  int insertError(int processId, int errorNo);
250 
251 
252 
253  int setTraceNo(int processId, int traceNo);
254  //**************************************************************************
255  // Description: Set trace number in a DB process.
256  // Parameters:
257  // processId: Id of the DB process
258  // trace: Trace number
259  // Returns: 0 if succeeded, otherwise: as stated above, plus:
260  // INVALID_TRACE_NUMBER
261  //**************************************************************************
262 
263 
264  int setSignalLoggingMode(int processId, LogMode mode,
265  const Vector<BaseString> &blocks);
266 
267  int setSignalLoggingMode(int processId, LogMode mode,
268  BaseString &block) {
270  v.push_back(block);
271  return setSignalLoggingMode(processId, mode, v);
272  }
273  //**************************************************************************
274  // Description: Set signal logging mode for blocks in a DB process.
275  // Parameters:
276  // processId: Id of the DB process
277  // mode: The log mode
278  // blocks: Which blocks to be affected (container of strings)
279  // Returns: 0 if succeeded, otherwise: as stated above, plus:
280  // INVALID_BLOCK_NAME
281  //**************************************************************************
282 
283 
284  int startSignalTracing(int processId);
285  //**************************************************************************
286  // Description: Start signal tracing for a DB process.
287  // Parameters:
288  // processId: Id of the DB process
289  // Returns: 0 if succeeded, otherwise: as stated above.
290  //**************************************************************************
291 
292 
293  int stopSignalTracing(int processId);
294  //**************************************************************************
295  // Description: Stop signal tracing for a DB process.
296  // Parameters:
297  // processId: Id of the DB process
298  // Returns: 0 if succeeded, otherwise: as stated above.
299  //**************************************************************************
300 
304  int dumpState(int processId, const Uint32 args[], Uint32 argNo);
305  int dumpState(int processId, const char* args);
306 
313  bool getNextNodeId(NodeId * _nodeId, enum ndb_mgm_node_type type) const ;
314  bool alloc_node_id(NodeId * _nodeId, enum ndb_mgm_node_type type,
315  const struct sockaddr *client_addr,
316  SOCKET_SIZE_TYPE *client_addr_len,
317  int &error_code, BaseString &error_string,
318  int log_event = 1,
319  int timeout_s = 20);
320 
321  bool change_config(Config& new_config, BaseString& msg);
322 
329  const char* getErrorText(int errorCode, char *buf, int buf_sz);
330 
331 private:
332  void config_changed(NodeId, const Config*);
333  void setClusterLog(const Config* conf);
334 public:
335 
340  int getPort() const { return m_port; };
341 
342  int setDbParameter(int node, int parameter, const char * value, BaseString&);
343  int setConnectionDbParameter(int node1, int node2, int param, int value,
344  BaseString& msg);
345  int getConnectionDbParameter(int node1, int node2, int param,
346  int *value, BaseString& msg);
347 
348  bool transporter_connect(NDB_SOCKET_TYPE sockfd, BaseString& errormsg);
349 
350  const char *get_connect_address(Uint32 node_id);
351  void get_connected_nodes(NodeBitmask &connected_nodes) const;
352  SocketServer *get_socket_server() { return &m_socket_server; }
353 
354  void updateStatus();
355 
356  int createNodegroup(int *nodes, int count, int *ng);
357  int dropNodegroup(int ng);
358 
359  int startSchemaTrans(SignalSender& ss, NodeId & out_nodeId,
360  Uint32 transId, Uint32 & out_transKey);
361  int endSchemaTrans(SignalSender& ss, NodeId nodeId,
362  Uint32 transId, Uint32 transKey, Uint32 flags);
363 
364 private:
365  int guess_master_node(SignalSender&);
366 
367  void status_api(int nodeId,
368  ndb_mgm_node_status& node_status,
369  Uint32& version, Uint32& mysql_version,
370  const char **address);
371  void status_mgmd(NodeId node_id,
372  ndb_mgm_node_status& node_status,
373  Uint32& version, Uint32& mysql_version,
374  const char **address);
375 
376  int sendVersionReq(int processId, Uint32 &version,
377  Uint32& mysql_version, const char **address);
378 
379  int sendStopMgmd(NodeId nodeId,
380  bool abort,
381  bool stop,
382  bool restart,
383  bool nostart,
384  bool initialStart);
385 
386  int sendall_STOP_REQ(NodeBitmask &stoppedNodes,
387  bool abort,
388  bool stop,
389  bool restart,
390  bool nostart,
391  bool initialStart);
392 
393  int sendSTOP_REQ(const Vector<NodeId> &node_ids,
394  NodeBitmask &stoppedNodes,
395  bool abort,
396  bool stop,
397  bool restart,
398  bool nostart,
399  bool initialStart,
400  int *stopSelf);
401 
408  int okToSendTo(NodeId nodeId, bool unCond = false);
409 
416  int getBlockNumber(const BaseString &blockName);
417 
418  int alloc_node_id_req(NodeId free_node_id,
419  enum ndb_mgm_node_type type,
420  Uint32 timeout_ms);
421 
422  bool is_any_node_starting(void);
423  bool is_any_node_stopping(void);
424  bool is_cluster_single_user(void);
425 
426  //**************************************************************************
427 
428  const MgmtOpts& m_opts;
429  int _blockNumber;
430  NodeId _ownNodeId;
431  Uint32 m_port;
432  SocketServer m_socket_server;
433 
434  NdbMutex* m_local_config_mutex;
435  const Config* m_local_config;
436 
437  NdbMutex *m_node_id_mutex;
438 
439  BlockReference _ownReference;
440 
441  class ConfigManager* m_config_manager;
442 
443  bool m_need_restart;
444 
445  NodeBitmask m_reserved_nodes;
446  struct in_addr m_connect_address[MAX_NODES];
447 
451  virtual void trp_deliver_signal(const NdbApiSignal* signal,
452  const struct LinearSectionPtr ptr[3]);
453  virtual void trp_node_status(Uint32 nodeId, Uint32 event);
454 
458  void eventReport(const Uint32 * theData, Uint32 len);
459 
460  class TransporterFacade * theFacade;
461 
462  bool _isStopThread;
463  int _logLevelThreadSleep;
464  MutexVector<NodeId> m_started_nodes;
465  MutexVector<EventSubscribeReq> m_log_level_requests;
466  LogLevel m_nodeLogLevel[MAX_NODES];
467  enum ndb_mgm_node_type nodeTypes[MAX_NODES];
468  friend class MgmApiSession;
469  friend class Ndb_mgmd_event_service;
470  Ndb_mgmd_event_service m_event_listner;
471 
472  NodeId m_master_node;
473 
474  ndb_mgm_node_type getNodeType(NodeId) const;
475 
480  struct NdbThread* _logLevelThread;
481  static void *logLevelThread_C(void *);
482  void logLevelThreadRun();
483  void report_unknown_signal(SimpleSignal *signal);
484 
485  void make_sync_req(SignalSender& ss, Uint32 nodeId);
486 public:
487  /* Get copy of configuration packed with base64 */
488  bool get_packed_config(ndb_mgm_node_type nodetype,
489  BaseString& buf64, BaseString& error);
490 
491  /* Get copy of configuration packed with base64 from node nodeid */
492  bool get_packed_config_from_node(NodeId nodeid,
493  BaseString& buf64, BaseString& error);
494 
495  void print_config(const char* section_filter = NULL,
496  NodeId nodeid_filter = 0,
497  const char* param_filter = NULL,
498  NdbOut& out = ndbout);
499 
500  bool reload_config(const char* config_filename,
501  bool mycnf, BaseString& msg);
502 
503  void show_variables(NdbOut& out = ndbout);
504 
506  {
507  unsigned id;
508  BaseString host;
509  };
510  int find_node_type(unsigned node_id, enum ndb_mgm_node_type type,
511  const struct sockaddr *client_addr,
512  NodeBitmask &nodes,
513  NodeBitmask &exact_nodes,
514  Vector<nodeid_and_host> &nodes_info,
515  int &error_code, BaseString &error_string);
516  int try_alloc(unsigned id, const char *, enum ndb_mgm_node_type type,
517  const struct sockaddr *client_addr, Uint32 timeout_ms);
518 
519  BaseString m_version_string;
520  const char* get_version_string(void) const {
521  return m_version_string.c_str();
522  }
523 
524  bool request_events(NdbNodeBitmask nodes, Uint32 reports_per_node,
525  Uint32 dump_type,
526  Vector<SimpleSignal>& events);
527 };
528 
529 #endif // MgmtSrvr_H