MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConfigManager.hpp
1 /* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 #ifndef ConfigManager_H
17 #define ConfigManager_H
18 
19 #include "MgmtThread.hpp"
20 #include "Config.hpp"
21 #include "ConfigSubscriber.hpp"
22 #include "MgmtSrvr.hpp"
23 #include "Defragger.hpp"
24 
25 #include <ConfigRetriever.hpp>
26 
27 #include <SignalSender.hpp>
28 #include <NodeBitmask.hpp>
29 #include <HashMap.hpp>
30 
31 #include <signaldata/ConfigChange.hpp>
32 
33 class ConfigManager : public MgmtThread {
34  const MgmtSrvr::MgmtOpts& m_opts;
35  TransporterFacade * m_facade;
36  SignalSender * m_ss;
37 
38  NdbMutex *m_config_mutex;
39  const Config * m_config;
40  BaseString m_packed_config; // base64 packed
41 
42  ConfigRetriever m_config_retriever;
43 
44  struct ConfigChangeState {
45  enum States {
46  IDLE = 0,
47  PREPARING = 1,
48  COMITTING = 2,
49  ABORT = 3,
50  ABORTING = 4
51  } m_current_state;
52 
53  ConfigChangeState() :
54  m_current_state(IDLE) {}
55 
56  operator int() const { return m_current_state; }
57  };
58 
59  void set_config_change_state(ConfigChangeState::States state);
60 
61  enum ConfigState {
62  CS_UNINITIALIZED = 0,
63 
64  CS_INITIAL = 1, // Initial config.ini, ie. no config.bin.X found
65 
66  CS_CONFIRMED = 2, // Started and all agreed
67  CS_FORCED = 3 // Forced start
68  };
69 
70  ConfigState m_config_state;
71  ConfigState m_previous_state;
72 
73  struct ConfigChange
74  {
75  ConfigChange() :
76  m_client_ref(RNIL),
77  m_error(ConfigChangeRef::OK),
78  m_new_config(0),
79  m_loaded_config(0),
80  m_initial_config(0)
81  {}
82 
83  void release() {
84  if (m_new_config)
85  delete m_new_config;
86  if (m_loaded_config)
87  delete m_loaded_config;
88  if (m_initial_config)
89  delete m_initial_config;
90  m_new_config = 0;
91  m_loaded_config = 0;
92  m_initial_config = 0;
93  }
94 
95  virtual ~ConfigChange() {
96  release();
97  }
98 
99  ConfigChangeState m_state;
100  BlockReference m_client_ref;
101  /* The original error that caused config change to be aborted */
102  ConfigChangeRef::ErrorCode m_error;
103  const Config * m_new_config;
104 
105  bool config_loaded(Config* config);
106  Config* m_loaded_config;
107  Config* m_initial_config;
108  NodeBitmask m_contacted_nodes;
109  } m_config_change;
110 
111  BaseString m_config_name;
112  Config* m_prepared_config;
113 
114  NodeBitmask m_all_mgm;
115  NodeBitmask m_started;
116  NodeBitmask m_waiting_for;
117  NodeBitmask m_checked;
118 
119  NodeId m_node_id;
120 
121  const char* m_configdir;
122 
123  Defragger m_defragger;
124 
125  /* Functions used from 'init' */
126  static Config* load_init_config(const char*);
127  static Config* load_init_mycnf(void);
128  Config* load_config(void) const;
129  Config* fetch_config(void);
130  bool save_config(const Config* conf);
131  bool save_config(void);
132  bool saved_config_exists(BaseString& config_name) const;
133  bool delete_saved_configs(void) const;
134  bool failed_config_change_exists(void) const;
135  Config* load_saved_config(const BaseString& config_name);
136  NodeId find_nodeid_from_configdir(void);
137  NodeId find_nodeid_from_config(void);
138  bool init_nodeid(void);
139 
140  /* Set the new config and inform subscribers */
141  void set_config(Config* config);
142  Vector<ConfigSubscriber*> m_subscribers;
143 
144  /* Check config is ok */
145  bool config_ok(const Config* conf);
146 
147  /* Prepare loaded config */
148  Config* prepareLoadedConfig(Config* config);
149 
150  /* Functions for writing config.bin to disk */
151  bool prepareConfigChange(const Config* config);
152  void commitConfigChange();
153  void abortConfigChange();
154 
155  /* Functions for starting config change from ConfigManager */
156  void startConfigChange(SignalSender& ss, Uint32 ref);
157  void startAbortConfigChange(SignalSender&);
158 
159  /* CONFIG_CHANGE - controlling config change from other node */
160  void execCONFIG_CHANGE_REQ(SignalSender& ss, SimpleSignal* sig);
161  void execCONFIG_CHANGE_REF(SignalSender& ss, SimpleSignal* sig);
162  void sendConfigChangeRef(SignalSender& ss, BlockReference,
163  ConfigChangeRef::ErrorCode) const;
164  void sendConfigChangeConf(SignalSender& ss, BlockReference) const;
165 
166  /*
167  CONFIG_CHANGE_IMPL - protocol for starting config change
168  between nodes
169  */
170  void execCONFIG_CHANGE_IMPL_REQ(SignalSender& ss, SimpleSignal* sig);
171  void execCONFIG_CHANGE_IMPL_REF(SignalSender& ss, SimpleSignal* sig);
172  void execCONFIG_CHANGE_IMPL_CONF(SignalSender& ss, SimpleSignal* sig);
173  void sendConfigChangeImplRef(SignalSender& ss, NodeId nodeId,
174  ConfigChangeRef::ErrorCode) const;
175  int sendConfigChangeImplReq(SignalSender& ss, const Config* conf);
176 
177  /*
178  CONFIG_CHECK - protocol for exchanging and checking config state
179  between nodes
180  */
181  void execCONFIG_CHECK_REQ(SignalSender& ss, SimpleSignal* sig);
182  void execCONFIG_CHECK_CONF(SignalSender& ss, SimpleSignal* sig);
183  void execCONFIG_CHECK_REF(SignalSender& ss, SimpleSignal* sig);
184  void sendConfigCheckReq(SignalSender& ss, NodeBitmask to);
185  void sendConfigCheckRef(SignalSender& ss, BlockReference to,
186  ConfigCheckRef::ErrorCode,
187  Uint32, Uint32, ConfigState, ConfigState) const;
188  void sendConfigCheckConf(SignalSender& ss, BlockReference to) const;
189 
190  /*
191  ConfigChecker - for connecting to other mgm nodes without
192  transporter
193  */
194  class ConfigChecker : public MgmtThread {
195  ConfigManager& m_manager;
196  ConfigRetriever m_config_retriever;
197  BaseString m_connect_string;
198  NodeId m_nodeid;
199  public:
200  ConfigChecker(); // Not implemented
201  ConfigChecker(const ConfigChecker&); // Not implemented
202  ConfigChecker(ConfigManager& manager,
203  const char* connect_string,
204  const char* bind_address,
205  NodeId nodeid);
206  bool init();
207  virtual void run();
208  };
209  bool init_checkers(const Config* config);
210  void start_checkers();
211  void stop_checkers();
212  Vector<ConfigChecker*> m_checkers;
213  MutexVector<NodeId> m_exclude_nodes;
214  void handle_exclude_nodes(void);
215 
216  class DynamicPorts {
217  struct NodePair {
218  int node1;
219  int node2;
220  NodePair(int n1, int n2) : node1(n1), node2(n2) {};
221  };
222  HashMap<NodePair, int> m_ports;
223  bool check(int& node1, int& node2) const;
224  public:
225 
226  bool set(int node1, int node2, int value);
227  bool get(int node1, int node2, int* value) const;
228 
229  // Write all known dynamic ports into the config
230  bool set_in_config(Config* config);
231 
232  } m_dynamic_ports;
233 
234 public:
236  const char* configdir);
237  virtual ~ConfigManager();
238  bool init();
239  void set_facade(TransporterFacade* facade);
240  virtual void run();
241 
242 
243  /*
244  Installs subscriber that will be notified when
245  config has changed
246  */
247  int add_config_change_subscriber(ConfigSubscriber*);
248 
249  /*
250  Retrieve the current configuration in base64 packed format
251  */
252  bool get_packed_config(ndb_mgm_node_type nodetype,
253  BaseString * buf64, BaseString& error);
254 
255  static Config* load_config(const char* config_filename, bool mycnf,
256  BaseString& msg);
257 
258  bool set_dynamic_port(int node1, int node2, int value, BaseString& msg);
259  bool get_dynamic_port(int node1, int node2, int *value,
260  BaseString& msg) const;
261 
262 };
263 
264 
265 
266 #endif