MySQL  8.0.28
Source Code Documentation
udf_utils.h
Go to the documentation of this file.
1 /* Copyright (c) 2018, 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 PLUGIN_GR_INCLUDE_UDF_UTILS_H
24 #define PLUGIN_GR_INCLUDE_UDF_UTILS_H
25 
26 #include <assert.h>
30 
33 
34 const char *const member_offline_or_minority_str =
35  "Member must be ONLINE and in the majority partition.";
37  "All members in the group must be reachable.";
38 const char *const recovering_member_on_group_str =
39  "A member is joining the group, wait for it to be ONLINE.";
40 const char *const server_uuid_not_present_str =
41  "Wrong arguments: You need to specify a server uuid.";
42 const char *const server_uuid_not_valid_str =
43  "Wrong arguments: The server uuid is not valid.";
44 const char *const server_uuid_not_on_group_str =
45  "The requested uuid is not a member of the group.";
46 
47 /**
48  * Result data type for user_has_gr_admin_privilege.
49  * There are three cases:
50  *
51  * error: There was an error fetching the user's privileges
52  * ok: The user has the required privileges
53  * no_privilege: The user does *not* have the required privileges
54  *
55  * In the no_privilege case, the result contains the user's name and host for
56  * the caller to create an helpful error message.
57  */
60  public:
62  char const *get_user() const {
64  "get_user() can only be called if status == no_privilege");
65  return user;
66  }
67  char const *get_host() const {
69  "get_host() can only be called if status == no_privilege");
70  return host;
71  }
74  }
77  }
78  static privilege_result no_privilege(char const *user, char const *host) {
79  return privilege_result(user, host);
80  }
81 
82  private:
83  char const *user;
84  char const *host;
88  "privilege_result(status) can only be called if status != "
89  "no_privilege");
90  }
91  privilege_result(char const *user, char const *host)
93 };
94 
95 /**
96  @class UDF_counter
97  Class used to increase an atomic value when UDF functions are being
98  initialized. If initialization fails the value will be decreased.
99 
100  number_udfs_running works together with plugin_is_stopping so when group
101  replication is stopping, all new udf will fail to start and server will
102  wait for the running ones to finish.
103 */
104 
105 class UDF_counter {
106  public:
107  static std::atomic<int> number_udfs_running;
108  static void terminated() { number_udfs_running--; }
109  static bool is_zero() { return number_udfs_running == 0; }
110 
114  }
115 
116  void succeeded() { success = true; }
117 
118  private:
119  bool success;
120 };
121 
122 /**
123  * Checks whether the user has GROUP_REPLICATION_ADMIN privilege.
124  *
125  * @retval privilege_result::error if there was an error fetching the user's
126  * privileges
127  * @retval privilege_result::no_privilege if the user does not have the
128  * privilege
129  * @retval privilege_result::success if the user has the privilege
130  */
132 
133 /**
134  * Logs the privilege status of @c privilege into @c message.
135  *
136  * @param privilege the result of @c user_has_gr_admin_privilege()
137  * @param[out] message the buffer where the log message will be written
138  */
139 void log_privilege_status_result(privilege_result const &privilege,
140  char *message);
141 /**
142  * Checks that `super_read_only` is disabled on the server.
143  *
144  * @returns std::pair<bool, std::string> where each element has the
145  * following meaning:
146  * first element of the pair is the function error value:
147  * false Successful
148  * true Error
149  * second element of the pair is the error message.
150  */
151 std::pair<bool, std::string> check_super_read_only_is_disabled();
152 
153 /**
154  * Checks whether the server is ONLINE and belongs to the majority partition.
155  *
156  * @retval true if the member is online and in the majority partition
157  * @retval false otherwise
158  */
160 
161 /**
162  * Checks if an unreachable member exists in the group
163  *
164  * @retval true if an unreachable member exists
165  * @retval false otherwise
166  */
168 
169 /**
170  * Checks if a member in recovery exists in the group
171  *
172  * @retval true if a recovering member exists
173  * @retval false otherwise
174  */
176 
177 /**
178  * Checks if the uuid is valid to use in a function
179  * It checks:
180  * 1. It is not empty
181  * 2. It is a valid uuid
182  * 3. It belongs to the group
183  *
184  * @param uuid the uuid string
185  * @param ulength the length of the uuid string
186  * @param[out] error_message the returned error message
187  *
188  * @retval true if uuid is not valid
189  * @retval false otherwise
190  */
191 bool validate_uuid_parameter(std::string &uuid, size_t ulength,
192  const char **error_message);
193 
194 /**
195  * Throw a error on a UDF function with mysql_error_service_printf
196  *
197  * @param action_name the action name when the error occurred
198  * @param error_message the error message to print
199  * @param log_error should the error also go to the log (default = false)
200  *
201  * @retval true the function failed to use the mysql_runtime_error service to
202  * throw the error
203  * @retval false everything went OK
204  */
205 bool throw_udf_error(const char *action_name, const char *error_message,
206  bool log_error = false);
207 
208 /**
209  * Logs the group action @c action_name result from @c result_area into
210  * @c result_message.
211  *
212  * @param result_area describes the log message level
213  * @param action_name group action name
214  * @param[out] result_message buffer where the log message will be written
215  * @param[out] length size of the log message written to @c result_message
216  *
217  * @retval true the group action failed and this function threw/logged the group
218  * action's error
219  * @retval false everything went OK
220  */
222  const char *action_name,
223  char *result_message,
224  unsigned long *length);
225 
226 /**
227  * Checks if tables are locked, and logs to @c message if so.
228  *
229  * @param[out] message buffer where the log message will be written to
230  * @retval true if tables are not locked
231  * @retval false if tables are locked (@c message is written to)
232  */
233 bool check_locked_tables(char *message);
234 
235 /**
236  * Checks whether the group contains a member older than the specified version.
237  *
238  * @param min_required_version Minimum version required
239  * @returns true if there is some older member, false otherwise
240  */
242  Member_version const &min_required_version);
243 /**
244  @class Charset_service
245 
246  Class that acquire/release the udf_metadata_service from registry service.
247  It provides the APIs to set the character set of return value and arguments
248  of UDFs using the udf_metadata service.
249 */
251  public:
252  /**
253  Acquires the udf_metadata_service from the registry service.
254  @param[in] reg_srv Registry service from which udf_metadata service
255  will be acquired
256 
257  @retval true if service could not be acquired
258  @retval false Otherwise
259  */
260  static bool init(SERVICE_TYPE(registry) * reg_srv);
261 
262  /**
263  Release the udf_metadata service
264 
265  @param[in] reg_srv Registry service from which the udf_metadata
266  service will be released.
267 
268  @retval true if service could not be released
269  @retval false Otherwise
270  */
271  static bool deinit(SERVICE_TYPE(registry) * reg_srv);
272 
273  /**
274  Set the specified character set of UDF return value
275 
276  @param[in] initid UDF_INIT structure
277  @param[in] charset_name Character set that has to be set.
278  The default charset is set to 'latin1'
279 
280  @retval true Could not set the character set of return value
281  @retval false Otherwise
282  */
283  static bool set_return_value_charset(
284  UDF_INIT *initid, const std::string &charset_name = "latin1");
285  /**
286  Set the specified character set of all UDF arguments
287 
288  @param[in] args UDF_ARGS structure
289  @param[in] charset_name Character set that has to be set.
290  The default charset is set to 'latin1'
291 
292  @retval true Could not set the character set of any of the argument
293  @retval false Otherwise
294  */
295  static bool set_args_charset(UDF_ARGS *args,
296  const std::string &charset_name = "latin1");
297 
298  private:
299  /* Argument type to specify in the metadata service methods */
300  static const char *arg_type;
301  /* udf_metadata service name */
302  static const char *service_name;
303  /* Handle of udf_metadata_service */
304  static SERVICE_TYPE(mysql_udf_metadata) * udf_metadata_service;
305 };
306 
307 #endif /* PLUGIN_GR_INCLUDE_UDF_UTILS_H */
#define log_error
Definition: log_client.h:110
Class that acquire/release the udf_metadata_service from registry service.
Definition: udf_utils.h:250
static bool init(const mysql_service_registry_t *reg_srv)
Acquires the udf_metadata_service from the registry service.
Definition: udf_utils.cc:289
static bool deinit(const mysql_service_registry_t *reg_srv)
Release the udf_metadata service.
Definition: udf_utils.cc:298
static bool set_args_charset(UDF_ARGS *args, const std::string &charset_name="latin1")
Set the specified character set of all UDF arguments.
Definition: udf_utils.cc:318
static const mysql_service_mysql_udf_metadata_t * udf_metadata_service
Definition: udf_utils.h:304
static bool set_return_value_charset(UDF_INIT *initid, const std::string &charset_name="latin1")
Set the specified character set of UDF return value.
Definition: udf_utils.cc:308
static const char * arg_type
Definition: udf_utils.h:300
static const char * service_name
Definition: udf_utils.h:302
The parent class for group wide operations.
Definition: group_action.h:34
represent the MySQL version of a Member within the Group Replication group.
Definition: member_version.h:34
Class used to increase an atomic value when UDF functions are being initialized.
Definition: udf_utils.h:105
bool success
Definition: udf_utils.h:119
static bool is_zero()
Definition: udf_utils.h:109
UDF_counter()
Definition: udf_utils.h:111
static void terminated()
Definition: udf_utils.h:108
void succeeded()
Definition: udf_utils.h:116
~UDF_counter()
Definition: udf_utils.h:112
static std::atomic< int > number_udfs_running
Definition: udf_utils.h:107
Definition: udf_utils.h:59
privilege_result(privilege_status status)
Definition: udf_utils.h:85
privilege_result(char const *user, char const *host)
Definition: udf_utils.h:91
char const * get_user() const
Definition: udf_utils.h:62
char const * user
Definition: udf_utils.h:83
static privilege_result error()
Definition: udf_utils.h:75
char const * host
Definition: udf_utils.h:84
char const * get_host() const
Definition: udf_utils.h:67
privilege_status status
Definition: udf_utils.h:61
static privilege_result success()
Definition: udf_utils.h:72
static privilege_result no_privilege(char const *user, char const *host)
Definition: udf_utils.h:78
Dialog Client Authentication nullptr
Definition: dialog.cc:352
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:75
#define SERVICE_TYPE(name)
Generates the standard Service type name.
Definition: service.h:75
Definition: udf_registration_types.h:47
Information about the result of a user defined function.
Definition: udf_registration_types.h:65
static const mysql_service_registry_t * reg_srv
Initialize parameters required for error logging.
Definition: test_plugin.cc:61
const char *const server_uuid_not_on_group_str
Definition: udf_utils.h:44
const char *const server_uuid_not_present_str
Definition: udf_utils.h:40
bool check_locked_tables(char *message)
Checks if tables are locked, and logs to message if so.
Definition: udf_utils.cc:246
bool group_contains_recovering_member()
Checks if a member in recovery exists in the group.
Definition: udf_utils.cc:157
bool log_group_action_result_message(Group_action_diagnostics *result_area, const char *action_name, char *result_message, unsigned long *length)
Logs the group action action_name result from result_area into result_message.
Definition: udf_utils.cc:211
const char *const recovering_member_on_group_str
Definition: udf_utils.h:38
std::pair< bool, std::string > check_super_read_only_is_disabled()
Checks that super_read_only is disabled on the server.
Definition: udf_utils.cc:107
void log_privilege_status_result(privilege_result const &privilege, char *message)
Logs the privilege status of privilege into message.
Definition: udf_utils.cc:86
bool throw_udf_error(const char *action_name, const char *error_message, bool log_error=false)
Throw a error on a UDF function with mysql_error_service_printf.
Definition: udf_utils.cc:190
bool validate_uuid_parameter(std::string &uuid, size_t ulength, const char **error_message)
Checks if the uuid is valid to use in a function It checks:
Definition: udf_utils.cc:166
bool member_online_with_majority()
Checks whether the server is ONLINE and belongs to the majority partition.
Definition: udf_utils.cc:134
bool group_contains_unreachable_member()
Checks if an unreachable member exists in the group.
Definition: udf_utils.cc:148
privilege_status
Result data type for user_has_gr_admin_privilege.
Definition: udf_utils.h:58
privilege_result user_has_gr_admin_privilege()
Checks whether the user has GROUP_REPLICATION_ADMIN privilege.
Definition: udf_utils.cc:32
const char *const unreachable_member_on_group_str
Definition: udf_utils.h:36
const char *const member_offline_or_minority_str
Definition: udf_utils.h:34
const char *const server_uuid_not_valid_str
Definition: udf_utils.h:42
bool group_contains_member_older_than(Member_version const &min_required_version)
Checks whether the group contains a member older than the specified version.
Definition: udf_utils.cc:261