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