MySQL  8.0.17
Source Code Documentation
persisted_variable.h
Go to the documentation of this file.
1 /* Copyright (c) 2016, 2018, 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, 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 <vector>
30 
31 #include "my_alloc.h"
32 #include "my_inttypes.h"
33 #include "my_psi_config.h"
35 #include "mysql/psi/mysql_mutex.h"
36 #include "mysql/psi/psi_base.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 };
64 
65 /**
66  CLASS Persisted_variables_cache
67  Holds <name,value> pair of all options which needs to be persisted
68  to a file.
69 
70  OVERVIEW
71  --------
72  When first SET PERSIST statement is executed we instantiate
73  Persisted_variables_cache which loads the config file if present into
74  m_persist_variables vector. This is a singleton operation. m_persist_variables
75  is an in-memory copy of config file itself. If the SET statement passes then
76  this in-memory is updated and flushed to file as an atomic operation.
77 
78  Next SET PERSIST statement would only update the in-memory copy and sync
79  to config file instead of loading the file again.
80 */
81 
82 #ifdef HAVE_PSI_INTERFACE
83 void my_init_persist_psi_keys(void);
84 #endif /* HAVE_PSI_INTERFACE */
85 
87  public:
88  int init(int *argc, char ***argv);
90  /**
91  Update in-memory copy for every SET PERSIST statement
92  */
93  void set_variable(THD *thd, set_var *system_var);
94  /**
95  Flush in-memory copy to persistent file
96  */
97  bool flush_to_file();
98  /**
99  Read options from persistent file
100  */
101  int read_persist_file();
102  /**
103  Search for persisted config file and if found read persistent options
104  */
105  bool load_persist_file();
106  /**
107  Set persisted options
108  */
109  bool set_persist_options(bool plugin_options = false);
110  /**
111  Reset persisted options
112  */
113  bool reset_persisted_variables(THD *thd, const char *name, bool if_exists);
114  /**
115  Get persisted variables
116  */
117  std::vector<st_persist_var> *get_persisted_variables();
118  /**
119  Get persisted static variables
120  */
121  std::map<std::string, st_persist_var> *get_persist_ro_variables();
122  /**
123  append read only persisted variables to command line options with a
124  separator.
125  */
126  bool append_read_only_variables(int *argc, char ***argv,
127  bool plugin_options = false);
128  void cleanup();
129 
130  /**
131  Acquire lock on m_persist_variables/m_persist_ro_variables
132  */
134  /**
135  Release lock on m_persist_variables/m_persist_ro_variables
136  */
138  /**
139  Assert caller that owns lock on m_persist_variables/m_persist_ro_variables
140  */
143  }
144 
145  private:
146  /* Helper function to get variable value */
147  static String *get_variable_value(THD *thd, sys_var *system_var, String *str,
148  bool *is_null);
149  /* Helper function to get variable name */
150  static const char *get_variable_name(sys_var *system_var);
151  /* Helper function to construct json formatted string */
152  static String *construct_json_string(std::string name, std::string value,
153  ulonglong timestamp, std::string user,
154  std::string host, bool is_null,
155  String *dest);
156  /* Helper function to extract variables from json formatted string */
157  bool extract_variables_from_json(const Json_dom *dom,
158  bool is_read_only = false);
159 
160  private:
161  /* Helper functions for file IO */
162  bool open_persist_file(int flag);
163  void close_persist_file();
164 
165  private:
166  /* In memory copy of persistent config file */
167  std::vector<st_persist_var> m_persist_variables;
168  /* copy of plugin variables whose plugin is not yet installed */
169  std::vector<st_persist_var> m_persist_plugin_variables;
170  /* In memory copy of read only persistent variables */
171  std::map<std::string, st_persist_var> m_persist_ro_variables;
172 
175 
176  /* File handler members */
178  std::string m_persist_filename;
180  /* Memory for read only persisted options */
182  /* Memory for read only persisted plugin options */
184 };
185 
186 #endif /* PERSISTED_VARIABLE_H_INCLUDED */
unsigned long long int ulonglong
Definition: my_inttypes.h:74
Instrumentation helpers for mutexes.
Our own string classes, used pervasively throughout the executor.
bool is_null
Definition: persisted_variable.h:57
std::vector< st_persist_var > * get_persisted_variables()
Get persisted variables.
Definition: persisted_variable.cc:1185
get_options & argc
Definition: do_ctype.cc:51
MYSQL_FILE * m_fd
Definition: persisted_variable.h:177
char * user
Definition: mysqladmin.cc:58
mysql_mutex_t m_LOCK_persist_variables
Definition: persisted_variable.h:173
static Persisted_variables_cache * m_instance
Definition: persisted_variable.h:174
MEM_ROOT ro_persisted_argv_alloc
Definition: persisted_variable.h:181
void lock()
Acquire lock on m_persist_variables/m_persist_ro_variables.
Definition: persisted_variable.h:133
const string name("\ame\)
static Persisted_variables_cache * get_instance()
Return a singleton object.
Definition: persisted_variable.cc:268
Some integer typedefs for easier portability.
int read_persist_file()
Read options from persistent file.
Definition: persisted_variable.cc:956
std::string key
Definition: persisted_variable.h:52
bool flush_to_file()
Flush in-memory copy to persistent file.
Definition: persisted_variable.cc:500
const char * host
Definition: mysqladmin.cc:57
st_persist_var()
Definition: persisted_variable.cc:162
bool load_persist_file()
Search for persisted config file and if found read persistent options.
Definition: persisted_variable.cc:596
#define PSI_NOT_INSTRUMENTED
Definition: validate_password_imp.cc:38
int init(int *argc, char ***argv)
Initialize class members.
Definition: persisted_variable.cc:203
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:159
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:372
Performance schema instrumentation interface.
set_var_base descendant for assignments to the system variables.
Definition: set_var.h:386
ulonglong timestamp
Definition: persisted_variable.h:54
std::string user
Definition: persisted_variable.h:55
A class representing one system variable - that is something that can be accessed as @global...
Definition: set_var.h:102
void assert_lock_owner()
Assert caller that owns lock on m_persist_variables/m_persist_ro_variables.
Definition: persisted_variable.h:141
Definition: persisted_variable.h:86
void unlock()
Release lock on m_persist_variables/m_persist_ro_variables.
Definition: persisted_variable.h:137
std::string m_persist_filename
Definition: persisted_variable.h:178
char * argv[]
Definition: example1.cc:2
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:437
void set_variable(THD *thd, set_var *system_var)
Update in-memory copy for every SET PERSIST statement.
Definition: persisted_variable.cc:283
bool open_persist_file(int flag)
Open persisted config file.
Definition: persisted_variable.cc:574
std::map< std::string, st_persist_var > * get_persist_ro_variables()
Get persisted static variables.
Definition: persisted_variable.cc:1193
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
mysql_mutex_t m_LOCK_persist_file
Definition: persisted_variable.h:179
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:130
std::vector< st_persist_var > m_persist_variables
Definition: persisted_variable.h:167
std::string value
Definition: persisted_variable.h:53
An instrumented FILE structure.
Definition: mysql_file.h:475
void close_persist_file()
Close persisted config file.
Definition: persisted_variable.cc:584
std::string host
Definition: persisted_variable.h:56
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:844
void cleanup()
Definition: persisted_variable.cc:1197
static int flag
Definition: hp_test1.cc:39
std::vector< st_persist_var > m_persist_plugin_variables
Definition: persisted_variable.h:169
bool set_persist_options(bool plugin_options=false)
Set persisted options.
Definition: persisted_variable.cc:615
bool reset_persisted_variables(THD *thd, const char *name, bool if_exists)
Reset persisted options.
Definition: persisted_variable.cc:1098
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
ABI for instrumented mutexes.
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:1026
#define mysql_mutex_assert_owner(M)
Wrapper, to use safe_mutex_assert_owner with instrumented mutexes.
Definition: mysql_mutex.h:109
#define mysql_mutex_unlock(M)
Definition: mysql_mutex.h:53
#define mysql_mutex_lock(M)
Definition: mysql_mutex.h:44
const string value("\alue\)
MEM_ROOT ro_persisted_plugin_argv_alloc
Definition: persisted_variable.h:183
const string timestamp("\imestamp\)
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
JSON DOM abstract base class.
Definition: json_dom.h:169
std::map< std::string, st_persist_var > m_persist_ro_variables
Definition: persisted_variable.h:171
static const char * get_variable_name(sys_var *system_var)
Retrieve variables name from sys_var.
Definition: persisted_variable.cc:406
STRUCT st_persist_var.
Definition: persisted_variable.h:51
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:777