1 /*
2    Copyright (c) 2003, 2020, 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, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #ifndef ConfigInfo_H
26 #define ConfigInfo_H
27 
28 #include <kernel_types.h>
29 #include <Properties.hpp>
30 #include <ndb_limits.h>
31 #include <NdbOut.hpp>
32 #include "InitConfigFileParser.hpp"
33 
34 // Parameter must be specified in config file
35 #define MANDATORY ((char*)~(UintPtr)0)
36 
37 /**
38  * @class  ConfigInfo
39  * @brief  Metainformation about ALL cluster configuration parameters
40  *
41  * Use the getters to find out metainformation about parameters.
42  */
43 class ConfigInfo {
44 public:
45   enum Type        { CI_BOOL,
46                      CI_INT,
47                      CI_INT64,
48                      CI_STRING,
49                      CI_ENUM, // String externaly, int internally
50                      CI_BITMASK, // String both externally and internally
51                      CI_SECTION
52   };
53   enum Status      { CI_USED,            ///< Active
54                      CI_EXPERIMENTAL,    ///< Active but experimental
55                      CI_DEPRECATED,      ///< Can be used, but shouldn't
56                      CI_NOTIMPLEMENTED,  ///< Is ignored.
57                      CI_INTERNAL         ///< Not configurable by the user
58   };
59 
60   enum Flags {
61     CI_ONLINE_UPDATEABLE  = 1, // Parameter can be updated online
62     CI_CHECK_WRITABLE = 2, // Path given by parameter should be writable
63 
64     /*
65       Flags  telling how the system must be restarted for a changed
66       parameter to take effect
67 
68       Default is none of these flags set, which means node restart
69       of one node at a time for the setting to take effect
70 
71       CS_RESTART_INITIAL
72       Each data node need to be restarted one at a time with --initial
73 
74       CS_RESTART_SYSTEM
75       The whole system need to be stopped and then started up again
76 
77       CS_RESTART_SYSTEM + CS_RESTART_INITIAL
78       The whole system need to be stopped and then restarted with --initial
79       thus destroying any data in the cluster
80 
81       These flags can not be combined with CI_ONLINE_UPDATABLE flag which
82       indicates that the parameter can be changed online without
83       restarting anything
84     */
85     CI_RESTART_SYSTEM = 4, // System restart is necessary to apply setting
86     CI_RESTART_INITIAL = 8 // Initial restart is necessary to apply setting
87   };
88 
89   struct Typelib {
90     const char* name;
91     Uint32 value;
92   };
93 
94   /**
95    *   Entry for one configuration parameter
96    */
97   struct ParamInfo {
98     /**
99      * Internal id used to identify configuration parameter when accessing
100      * config.
101      */
102     Uint32         _paramId;
103     /* External name, as given in text in config file. */
104     const char*    _fname;
105     /**
106      * Name (as it appears in config file text) of section that this extry
107      * belongs to.
108      *
109      * Each section alsa has one entry with the section name stored in both
110      * _fname and _section.
111      */
112     const char*    _section;
113     /* Short textual description/documentation for entry. */
114     const char*    _description;
115     Status         _status;
116     Uint32         _flags;
117     Type           _type;
118     /**
119      * Default value, minimum value (if any), and maximum value (if any).
120      *
121      * Stored as pointers to char * representation of default (eg "10k").
122      *
123      * For section entries, instead the _default member gives the internal id
124      * of that kind of section (CONNECTION_TYPE_TCP, NODE_TYPE_MGM, etc.)
125      */
126     const char*  _default;
127     const char* _min;
128     const char* _max;
129   };
130 
131   /**
132    * section type is stored in _default
133    */
getSectionType(const ParamInfo & p)134   static Uint32 getSectionType(const ParamInfo& p) {
135     assert(p._type == CI_SECTION);
136     return Uint32(reinterpret_cast<UintPtr>(p._default));
137   }
138 
139   /**
140    * typelib ptr is stored in _min
141    */
getTypelibPtr(const ParamInfo & p)142   static const Typelib* getTypelibPtr(const ParamInfo& p) {
143     assert(p._type == CI_ENUM);
144     return reinterpret_cast<const Typelib*>(p._min);
145   }
146 
147   class ParamInfoIter {
148     const ConfigInfo& m_info;
149     const char* m_section_name;
150     int m_curr_param;
151   public:
152     ParamInfoIter(const ConfigInfo& info,
153                   Uint32 section,
154                   Uint32 section_type = ~0);
155 
156     const ParamInfo* next(void);
157   };
158 
159   struct AliasPair{
160     const char * name;
161     const char * alias;
162   };
163 
164   /**
165    * Entry for one section rule
166    */
167   struct SectionRule {
168     const char * m_section;
169     bool (* m_sectionRule)(struct InitConfigFileParser::Context &,
170 			   const char * m_ruleData);
171     const char * m_ruleData;
172   };
173 
174   /**
175    * Entry for config rule
176    */
177   struct ConfigRuleSection {
178     BaseString m_sectionType;
179     Properties * m_sectionData;
180   };
181 
182   struct ConfigRule {
183     bool (* m_configRule)(Vector<ConfigRuleSection>&,
184 			  struct InitConfigFileParser::Context &,
185 			  const char * m_ruleData);
186     const char * m_ruleData;
187   };
188 
189   ConfigInfo();
190 
191   /**
192    *   Checks if the suggested value is valid for the suggested parameter
193    *   (i.e. if it is >= than min and <= than max).
194    *
195    *   @param  section  Init Config file section name
196    *   @param  fname    Name of parameter
197    *   @param  value    Value to check
198    *   @return true if parameter value is valid.
199    *
200    *   @note Result is not defined if section/name are wrong!
201    */
202   bool verify(const Properties* section, const char* fname, Uint64 value) const;
203   bool verify_enum(const Properties * section, const char* fname,
204                    const char* value, Uint32& value_int) const;
205   void get_enum_values(const Properties * section, const char* fname,
206                        BaseString& err) const;
207   static const char* nameToAlias(const char*);
208   static const char* getAlias(const char*);
209   bool isSection(const char*) const;
210 
211   const char*  getDescription(const Properties * sec, const char* fname) const;
212   Type         getType(const Properties * section, const char* fname) const;
213   Status       getStatus(const Properties* section, const char* fname) const;
214   Uint64       getMin(const Properties * section, const char* fname) const;
215   Uint64       getMax(const Properties * section, const char* fname) const;
216   Uint64 getDefault(const Properties * section, const char* fname) const;
217   Uint32 getFlags(const Properties* section, const char* fname) const;
218   const char* getDefaultString(const Properties * section,
219                                const char* fname) const;
220   bool getMandatory(const Properties * section, const char* fname) const;
221   bool hasDefault(const Properties * section, const char* fname) const;
222 
223   const Properties * getInfo(const char * section) const;
224   const Properties * getDefaults(const char * section) const;
225 
226   const char* sectionName(Uint32 section_type, Uint32 type) const;
227 
228   void print(const char* section= NULL) const;
229   void print_xml(const char* section= NULL) const;
230 private:
231   bool is_internal_section(const Properties* sec) const;
232   void print_impl(const char* section,
233                   class ConfigPrinter& printer) const;
234 private:
235   Properties               m_info;
236   Properties               m_systemDefaults;
237 
238   static const AliasPair   m_sectionNameAliases[];
239   static const char*       m_sectionNames[];
240   static const int         m_noOfSectionNames;
241 
242 public:
243   static const ParamInfo   m_ParamInfo[];
244   static const int         m_NoOfParams;
245 
246   static const SectionRule m_SectionRules[];
247   static const ConfigRule  m_ConfigRules[];
248   static const int         m_NoOfRules;
249 };
250 
251 #endif // ConfigInfo_H
252