MySQL  8.0.18
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 
26 #include "my_dbug.h"
29 
30 const char *const member_offline_or_minority_str =
31  "Member must be ONLINE and in the majority partition.";
33  "All members in the group must be reachable.";
34 const char *const recovering_member_on_group_str =
35  "A member is joining the group, wait for it to be ONLINE.";
36 const char *const server_uuid_not_present_str =
37  "Wrong arguments: You need to specify a server uuid.";
38 const char *const server_uuid_not_valid_str =
39  "Wrong arguments: The server uuid is not valid.";
40 const char *const server_uuid_not_on_group_str =
41  "The requested uuid is not a member of the group.";
42 
43 /**
44  * Result data type for user_has_gr_admin_privilege.
45  * There are three cases:
46  *
47  * error: There was an error fetching the user's privileges
48  * ok: The user has the required privileges
49  * no_privilege: The user does *not* have the required privileges
50  *
51  * In the no_privilege case, the result contains the user's name and host for
52  * the caller to create an helpful error message.
53  */
56  public:
58  char const *get_user() const {
60  "get_user() can only be called if status == no_privilege");
61  return user;
62  }
63  char const *get_host() const {
65  "get_host() can only be called if status == no_privilege");
66  return host;
67  }
70  }
73  }
74  static privilege_result no_privilege(char const *user, char const *host) {
75  return privilege_result(user, host);
76  }
77 
78  private:
79  char const *user;
80  char const *host;
82  : status(status), user(nullptr), host(nullptr) {
84  "privilege_result(status) can only be called if status != "
85  "no_privilege");
86  }
87  privilege_result(char const *user, char const *host)
89 };
90 
91 /**
92  @class UDF_counter
93  Class used to increase an atomic value when UDF functions are being
94  initialized. If initialization fails the value will be decreased.
95 
96  number_udfs_running works together with plugin_is_stopping so when group
97  replication is stopping, all new udf will fail to start and server will
98  wait for the running ones to finish.
99 */
100 
101 class UDF_counter {
102  public:
103  static std::atomic<int> number_udfs_running;
104  static void terminated() { number_udfs_running--; }
105  static bool is_zero() { return number_udfs_running == 0; }
106 
110  }
111 
112  void succeeded() { success = true; }
113 
114  private:
115  bool success;
116 };
117 
118 /**
119  * Checks whether the user has GROUP_REPLICATION_ADMIN privilege.
120  *
121  * @retval privilege_result::error if there was an error fetching the user's
122  * privileges
123  * @retval privilege_result::no_privilege if the user does not have the
124  * privilege
125  * @retval privilege_result::success if the user has the privilege
126  */
128 
129 /**
130  * Logs the privilege status of @c privilege into @c message.
131  *
132  * @param privilege the result of @c user_has_gr_admin_privilege()
133  * @param[out] message the buffer where the log message will be written
134  */
135 void log_privilege_status_result(privilege_result const &privilege,
136  char *message);
137 
138 /**
139  * Checks whether the server is ONLINE and belongs to the majority partition.
140  *
141  * @retval true if the member is online and in the majority partition
142  * @retval false otherwise
143  */
145 
146 /**
147  * Checks if an unreachable member exists in the group
148  *
149  * @retval true if an unreachable member exists
150  * @retval false otherwise
151  */
153 
154 /**
155  * Checks if a member in recovery exists in the group
156  *
157  * @retval true if a recovering member exists
158  * @retval false otherwise
159  */
161 
162 /**
163  * Checks if the uuid is valid to use in a function
164  * It checks:
165  * 1. It is not empty
166  * 2. It is a valid uuid
167  * 3. It belongs to the group
168  *
169  * @param uuid the uuid string
170  * @param ulength the length of the uuid string
171  * @param[out] error_message the returned error message
172  *
173  * @retval true if uuid is not valid
174  * @retval false otherwise
175  */
176 bool validate_uuid_parameter(std::string &uuid, size_t ulength,
177  const char **error_message);
178 
179 /**
180  * Throw a error on a UDF function with mysql_error_service_printf
181  *
182  * @param action_name the action name when the error occurred
183  * @param error_message the error message to print
184  * @param log_error should the error also go to the log (default = false)
185  *
186  * @retval true the function failed to use the mysql_runtime_error service to
187  * throw the error
188  * @retval false everything went OK
189  */
190 bool throw_udf_error(const char *action_name, const char *error_message,
191  bool log_error = false);
192 
193 /**
194  * Logs the group action @c action_name result from @c result_area into
195  * @c result_message.
196  *
197  * @param result_area describes the log message level
198  * @param action_name group action name
199  * @param[out] result_message buffer where the log message will be written
200  * @param[out] length size of the log message written to @c result_message
201  *
202  * @retval true the group action failed and this function threw/logged the group
203  * action's error
204  * @retval false everything went OK
205  */
207  const char *action_name,
208  char *result_message,
209  unsigned long *length);
210 
211 /**
212  * Checks if tables are locked, and logs to @c message if so.
213  *
214  * @param[out] message buffer where the log message will be written to
215  * @retval true if tables are not locked
216  * @retval false if tables are locked (@c message is written to)
217  */
218 bool check_locked_tables(char *message);
219 
220 /**
221  * Checks whether the group contains a member older than the specified version.
222  *
223  * @param min_required_version Minimum version required
224  * @returns true if there is some older member, false otherwise
225  */
227  Member_version const &min_required_version);
228 
229 #endif /* PLUGIN_GR_INCLUDE_UDF_UTILS_H */
char const * get_host() const
Definition: udf_utils.h:63
const char *const server_uuid_not_present_str
Definition: udf_utils.h:36
char const * host
Definition: udf_utils.h:80
UDF_counter()
Definition: udf_utils.h:107
privilege_result(privilege_status status)
Definition: udf_utils.h:81
void succeeded()
Definition: udf_utils.h:112
Class used to increase an atomic value when UDF functions are being initialized.
Definition: udf_utils.h:101
char const * user
Definition: udf_utils.h:79
The parent class for group wide operations.
Definition: group_action.h:33
static privilege_result error()
Definition: udf_utils.h:71
const char *const server_uuid_not_on_group_str
Definition: udf_utils.h:40
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
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:103
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
privilege_result(char const *user, char const *host)
Definition: udf_utils.h:87
const char *const server_uuid_not_valid_str
Definition: udf_utils.h:38
static privilege_result success()
Definition: udf_utils.h:68
bool success
Definition: udf_utils.h:115
privilege_status status
Definition: udf_utils.h:57
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:34
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:104
Definition: udf_utils.h:55
const char *const unreachable_member_on_group_str
Definition: udf_utils.h:32
char const * get_user() const
Definition: udf_utils.h:58
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
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:30
static privilege_result no_privilege(char const *user, char const *host)
Definition: udf_utils.h:74
#define false
Definition: config_static.h:43
~UDF_counter()
Definition: udf_utils.h:108
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:105
#define log_error
Definition: log_client.h:110
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
privilege_status
Result data type for user_has_gr_admin_privilege.
Definition: udf_utils.h:54