MySQL  8.0.27
Source Code Documentation
persisted_variable.h
Go to the documentation of this file.
1 /* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
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, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef PERSISTED_VARIABLE_H_INCLUDED
24 #define PERSISTED_VARIABLE_H_INCLUDED
25 
26 #include <stddef.h>
27 #include <map>
28 #include <string>
29 #include <unordered_set>
30 
31 #include "my_alloc.h"
32 #include "my_inttypes.h"
33 #include "my_psi_config.h"
36 #include "mysql/psi/mysql_mutex.h"
37 #include "sql_string.h"
38 
39 class Json_dom;
40 class THD;
41 class set_var;
42 class sys_var;
43 struct MYSQL_FILE;
44 
45 /**
46  STRUCT st_persist_var
47 
48  This structure represents information of a variable which is to
49  be persisted in mysql-auto.cnf file.
50 */
52  std::string key;
53  std::string value;
55  std::string user;
56  std::string host;
57  bool is_null;
59  st_persist_var(THD *thd);
60  st_persist_var(const std::string key, const std::string value,
61  const ulonglong timestamp, const std::string user,
62  const std::string host, const bool is_null);
63  /* This is custom comparision function used to make the unordered_set
64  to work with the default std::hash for userdefined types. */
65  bool operator==(const st_persist_var &persist_var) const {
66  return key == persist_var.key;
67  }
68 };
69 
70 /**
71  STRUCT st_persist_var_hash
72 
73  This structure has a custom hasher function used to make the unordered_set
74  to work with the default std::hash for userdefined types.
75 */
77  size_t operator()(const st_persist_var &pv) const { return pv.key.length(); }
78 };
79 
80 /**
81  CLASS Persisted_variables_cache
82  Holds <name,value> pair of all options which needs to be persisted
83  to a file.
84 
85  OVERVIEW
86  --------
87  When first SET PERSIST statement is executed we instantiate
88  Persisted_variables_cache which loads the config file if present into
89  m_persist_variables set. This is a singleton operation. m_persist_variables
90  is an in-memory copy of config file itself. If the SET statement passes then
91  this in-memory is updated and flushed to file as an atomic operation.
92 
93  Next SET PERSIST statement would only update the in-memory copy and sync
94  to config file instead of loading the file again.
95 */
96 
97 #ifdef HAVE_PSI_INTERFACE
98 void my_init_persist_psi_keys(void);
99 #endif /* HAVE_PSI_INTERFACE */
100 
102  public:
103  int init(int *argc, char ***argv);
105  /**
106  Update in-memory copy for every SET PERSIST statement
107  */
108  bool set_variable(THD *thd, set_var *system_var);
109  /**
110  Flush in-memory copy to persistent file
111  */
112  bool flush_to_file();
113  /**
114  Read options from persistent file
115  */
116  int read_persist_file();
117  /**
118  Search for persisted config file and if found read persistent options
119  */
120  bool load_persist_file();
121  bool set_persist_options(bool plugin_options, bool lock_vars);
122  /**
123  Reset persisted options
124  */
125  bool reset_persisted_variables(THD *thd, const char *name, bool if_exists);
126  /**
127  Get persisted variables
128  */
129  std::unordered_set<st_persist_var, st_persist_var_hash>
131  /**
132  Get persisted static variables
133  */
134  std::map<std::string, st_persist_var> *get_persist_ro_variables();
135  /**
136  append read only persisted variables to command line options with a
137  separator.
138  */
139  bool append_read_only_variables(int *argc, char ***argv,
140  bool plugin_options = false);
141  void cleanup();
142 
143  /**
144  Acquire lock on m_persist_variables/m_persist_ro_variables
145  */
147  /**
148  Release lock on m_persist_variables/m_persist_ro_variables
149  */
151  /**
152  Assert caller that owns lock on m_persist_variables/m_persist_ro_variables
153  */
156  }
157 
158  private:
159  /* Helper function to get variable value */
160  static String *get_variable_value(THD *thd, sys_var *system_var, String *str,
161  bool *is_null);
162  /* Helper function to get variable name */
163  static const char *get_variable_name(const sys_var *system_var);
164  /**
165  If the variable has an alias, return the name for the alias.
166  */
167  static const char *get_variable_alias(const sys_var *system_var);
168  static const char *get_variable_alias(const char *system_var);
169  /* Helper function to construct json formatted string */
170  static String *construct_json_string(std::string name, std::string value,
171  ulonglong timestamp, std::string user,
172  std::string host, bool is_null,
173  String *dest);
174  /* Helper function to extract variables from json formatted string */
175  bool extract_variables_from_json(const Json_dom *dom,
176  bool is_read_only = false);
177  /**
178  After extracting the variables from the JSON, we duplicate any
179  variable definition that relates to an alias.
180  */
181  void load_aliases();
182 
183  private:
184  /* Helper functions for file IO */
185  bool open_persist_file(int flag);
186  void close_persist_file();
187 
188  private:
189  /* In memory copy of persistent config file */
190  std::unordered_set<st_persist_var, st_persist_var_hash> m_persist_variables;
191  /* copy of plugin variables whose plugin is not yet installed */
192  std::unordered_set<st_persist_var, st_persist_var_hash>
194  /* In memory copy of read only persistent variables */
195  std::map<std::string, st_persist_var> m_persist_ro_variables;
196 
199 
200  /* File handler members */
202  std::string m_persist_filename;
204  /* Memory for read only persisted options */
206  /* Memory for read only persisted plugin options */
208 };
209 
210 #endif /* PERSISTED_VARIABLE_H_INCLUDED */
JSON DOM abstract base class.
Definition: json_dom.h:171
Definition: persisted_variable.h:101
std::map< std::string, st_persist_var > m_persist_ro_variables
Definition: persisted_variable.h:195
bool reset_persisted_variables(THD *thd, const char *name, bool if_exists)
Reset persisted options.
Definition: persisted_variable.cc:1280
void unlock()
Release lock on m_persist_variables/m_persist_ro_variables.
Definition: persisted_variable.h:150
void load_aliases()
After extracting the variables from the JSON, we duplicate any variable definition that relates to an...
Definition: persisted_variable.cc:1053
void close_persist_file()
Close persisted config file.
Definition: persisted_variable.cc:655
bool load_persist_file()
Search for persisted config file and if found read persistent options.
Definition: persisted_variable.cc:667
static const char * get_variable_name(const sys_var *system_var)
Retrieve variables name from sys_var.
Definition: persisted_variable.cc:464
static Persisted_variables_cache * m_instance
Definition: persisted_variable.h:198
bool set_persist_options(bool plugin_options, bool lock_vars)
set_persist_options() will set the options read from persisted config file
Definition: persisted_variable.cc:687
void cleanup()
Definition: persisted_variable.cc:1393
static Persisted_variables_cache * get_instance()
Return a singleton object.
Definition: persisted_variable.cc:278
MYSQL_FILE * m_fd
Definition: persisted_variable.h:201
MEM_ROOT ro_persisted_argv_alloc
Definition: persisted_variable.h:205
std::string m_persist_filename
Definition: persisted_variable.h:202
std::unordered_set< st_persist_var, st_persist_var_hash > m_persist_variables
Definition: persisted_variable.h:190
std::unordered_set< st_persist_var, st_persist_var_hash > * get_persisted_variables()
Get persisted variables.
Definition: persisted_variable.cc:1381
static const char * get_variable_alias(const sys_var *system_var)
If the variable has an alias, return the name for the alias.
Definition: persisted_variable.cc:469
int read_persist_file()
Read options from persistent file.
Definition: persisted_variable.cc:1136
bool set_variable(THD *thd, set_var *system_var)
Update in-memory copy for every SET PERSIST statement.
Definition: persisted_variable.cc:318
std::unordered_set< st_persist_var, st_persist_var_hash > m_persist_plugin_variables
Definition: persisted_variable.h:193
mysql_mutex_t m_LOCK_persist_file
Definition: persisted_variable.h:203
void lock()
Acquire lock on m_persist_variables/m_persist_ro_variables.
Definition: persisted_variable.h:146
static String * get_variable_value(THD *thd, sys_var *system_var, String *str, bool *is_null)
Retrieve variables value from sys_var.
Definition: persisted_variable.cc:431
std::map< std::string, st_persist_var > * get_persist_ro_variables()
Get persisted static variables.
Definition: persisted_variable.cc:1389
int init(int *argc, char ***argv)
Initialize class members.
Definition: persisted_variable.cc:204
mysql_mutex_t m_LOCK_persist_variables
Definition: persisted_variable.h:197
void assert_lock_owner()
Assert caller that owns lock on m_persist_variables/m_persist_ro_variables.
Definition: persisted_variable.h:154
bool flush_to_file()
Flush in-memory copy to persistent file.
Definition: persisted_variable.cc:572
bool open_persist_file(int flag)
Open persisted config file.
Definition: persisted_variable.cc:646
bool append_read_only_variables(int *argc, char ***argv, bool plugin_options=false)
append read only persisted variables to command line options with a separator.
Definition: persisted_variable.cc:1210
static String * construct_json_string(std::string name, std::string value, ulonglong timestamp, std::string user, std::string host, bool is_null, String *dest)
Given information of variable which needs to be persisted, this function will construct a json foemat...
Definition: persisted_variable.cc:509
bool extract_variables_from_json(const Json_dom *dom, bool is_read_only=false)
extract_variables_from_json() is used to extract all the variable information which is in the form of...
Definition: persisted_variable.cc:958
MEM_ROOT ro_persisted_plugin_argv_alloc
Definition: persisted_variable.h:207
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:165
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
set_var_base descendant for assignments to the system variables.
Definition: set_var.h:453
A class representing one system variable - that is something that can be accessed as @global....
Definition: set_var.h:102
#define mysql_mutex_lock(M)
Definition: mysql_mutex.h:49
#define mysql_mutex_unlock(M)
Definition: mysql_mutex.h:56
char * argv[]
Definition: do_ctype.cc:45
get_options & argc
Definition: do_ctype.cc:51
#define mysql_mutex_assert_owner(M)
Wrapper, to use safe_mutex_assert_owner with instrumented mutexes.
Definition: mysql_mutex.h:111
static int flag
Definition: hp_test1.cc:39
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
ABI for instrumented mutexes.
char * user
Definition: mysqladmin.cc:59
const char * host
Definition: mysqladmin.cc:58
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1056
const string value("\"Value\"")
const string timestamp("\"Timestamp\"")
void my_init_persist_psi_keys(void)
CLASS Persisted_variables_cache Holds <name,value> pair of all options which needs to be persisted to...
Definition: persisted_variable.cc:131
Instrumentation helpers for mutexes.
Performance schema instrumentation interface.
Our own string classes, used pervasively throughout the executor.
case opt name
Definition: sslopt-case.h:32
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:78
An instrumented FILE structure.
Definition: mysql_file.h:482
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
STRUCT st_persist_var_hash.
Definition: persisted_variable.h:76
size_t operator()(const st_persist_var &pv) const
Definition: persisted_variable.h:77
STRUCT st_persist_var.
Definition: persisted_variable.h:51
st_persist_var()
Definition: persisted_variable.cc:163
std::string user
Definition: persisted_variable.h:55
ulonglong timestamp
Definition: persisted_variable.h:54
std::string value
Definition: persisted_variable.h:53
bool is_null
Definition: persisted_variable.h:57
std::string key
Definition: persisted_variable.h:52
std::string host
Definition: persisted_variable.h:56
bool operator==(const st_persist_var &persist_var) const
Definition: persisted_variable.h:65
#define PSI_NOT_INSTRUMENTED
Definition: validate_password_imp.cc:39