MySQL 9.3.0
Source Code Documentation
primary_election_invocation_handler.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 2025, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef PRIMARY_ELECTION_INVOCATION_HANDLER_INCLUDED
25#define PRIMARY_ELECTION_INVOCATION_HANDLER_INCLUDED
26
27#include <string>
28#include <vector>
29
36
38 std::string uuid;
39 uint64_t delta;
40};
41
42/**
43 @class Primary_election_handler
44 The base class to request and execute an election
45*/
47 public:
48 /**
49 Instantiate a new election handler
50 @param[in] components_stop_timeout the timeout when waiting on shutdown
51 */
52 Primary_election_handler(ulong components_stop_timeout);
53
54 /** Class destructor */
56
57 /**
58 Send a message to all members requesting an election
59
60 @param primary_uuid the primary member to elect
61 @param mode the election mode to use
62 */
63 int request_group_primary_election(std::string primary_uuid,
65
66 /**
67 Handle new received primary message of type SINGLE_PRIMARY_PRIMARY_ELECTION
68 @param message The received primary message
69 @param notification_ctx the notification object to report changes
70 @return !=0 in case of error
71 */
73 Notification_context *notification_ctx);
74
75 /**
76 Execute the primary member selection if needed and the election algorithm
77 invocation.
78
79 @param primary_uuid the primary member to elect
80 @param mode the election mode to use
81 @param notification_ctx the notification object to report changes
82
83 @return !=0 in case of error
84 */
85 int execute_primary_election(std::string &primary_uuid,
87 Notification_context *notification_ctx);
88
89 /**
90 Print server executed GTID and applier retrieved GTID in logs.
91 */
93
94 /**
95 Is an election process running?
96 @return true if yes, false if no
97 */
99
100 /**
101 Sets if the election process is running or not
102 @param election_running is the election running or not
103 */
104 void set_election_running(bool election_running);
105
106 /**
107 End any running election process.
108 @return !=0 in case of error
109 */
111
112 // Consistency transaction manager notifiers
113 /**
114 Notify transaction consistency manager that election is running
115 */
117
118 /**
119 Notify transaction consistency manager that election ended
120 */
121 void notify_election_end();
122
123 /**
124 Sets the component stop timeout.
125
126 @param[in] timeout the timeout
127 */
128 void set_stop_wait_timeout(ulong timeout);
129
130 private:
131 /**
132 Get the member to elect from all group members.
133 This method returns the current primary if one exists
134 If no primary exists this method returns one of the lowest version present
135 in the group according to a weight or uuid criteria.
136
137 @param[out] primary_uuid the primary member to elect
138 @param[in] all_members_info The members currently in the group
139 @param[in] mode The primary election mode
140
141 @return true if a primary is found, false otherwise
142 */
143 bool pick_primary_member(std::string &primary_uuid,
144 Group_member_info_list *all_members_info,
146
147 /**
148 Execute the standard primary election algorithm (that supports primary
149 appointments)
150
151 @param primary_uuid the primary member to elect
152 @param mode the election mode to use
153 */
154 int internal_primary_election(std::string &primary_uuid,
156
157 /**
158 Execute the legacy (<8.0.12) primary election algorithm
159
160 @param primary_uuid the primary member to elect
161 */
162 int legacy_primary_election(std::string &primary_uuid);
163
164 /**
165 Order list of members until lowest_version_end position.
166
167 @param[out] all_members_info list of members to select primary
168 @param[in] lowest_version_end position last member to select primary
169 @param[out] members delta members transactions applied
170
171 @return true if a primary is found, false otherwise
172 */
174 Group_member_info_list *all_members_info,
175 Group_member_info_list_iterator lowest_version_end,
176 std::vector<Gr_primary_election_member> &members);
177
178 /** The handler to handle the election on the primary member */
180
181 /** The handler to handle the election in the secondary members */
183
184 /** Is an election running? */
186
187 /** The lock for the running flag*/
189};
190
191/**
192 Sort lower version members based on member weight if member version
193 is greater than equal to PRIMARY_ELECTION_MEMBER_WEIGHT_VERSION or uuid.
194
195 @param all_members_info the vector with members info
196 @param lowest_version_end first iterator position where members version
197 increases.
198*/
200 Group_member_info_list *all_members_info,
201 Group_member_info_list_iterator lowest_version_end);
202
203/**
204 Sort members based on member_version and get first iterator position
205 where member version differs.
206
207 @param all_members_info the vector with members info
208
209 @return the first iterator position where members version increase.
210
211 @note from the start of the list to the returned iterator, all members have
212 the lowest version in the group.
213 */
215 Group_member_info_list *all_members_info);
216
217#endif /* PRIMARY_ELECTION_INVOCATION_HANDLER_INCLUDED */
A convenience context class used to share information between the event handlers and the notifier.
Definition: notification.h:35
The base class to request and execute an election.
Definition: primary_election_invocation_handler.h:46
~Primary_election_handler()
Class destructor.
Definition: primary_election_invocation_handler.cc:43
Primary_election_primary_process primary_election_handler
The handler to handle the election on the primary member.
Definition: primary_election_invocation_handler.h:179
int execute_primary_election(std::string &primary_uuid, enum_primary_election_mode mode, Notification_context *notification_ctx)
Execute the primary member selection if needed and the election algorithm invocation.
Definition: primary_election_invocation_handler.cc:92
bool election_process_running
Is an election running?
Definition: primary_election_invocation_handler.h:185
int internal_primary_election(std::string &primary_uuid, enum_primary_election_mode mode)
Execute the standard primary election algorithm (that supports primary appointments)
Definition: primary_election_invocation_handler.cc:257
void notify_election_end()
Notify transaction consistency manager that election ended.
Definition: primary_election_invocation_handler.cc:637
Primary_election_secondary_process secondary_election_handler
The handler to handle the election in the secondary members.
Definition: primary_election_invocation_handler.h:182
Primary_election_handler(ulong components_stop_timeout)
Instantiate a new election handler.
Definition: primary_election_invocation_handler.cc:34
mysql_mutex_t flag_lock
The lock for the running flag.
Definition: primary_election_invocation_handler.h:188
int handle_primary_election_message(Single_primary_message *message, Notification_context *notification_ctx)
Handle new received primary message of type SINGLE_PRIMARY_PRIMARY_ELECTION.
Definition: primary_election_invocation_handler.cc:72
bool is_an_election_running()
Is an election process running?
Definition: primary_election_invocation_handler.cc:52
int terminate_election_process()
End any running election process.
Definition: primary_election_invocation_handler.cc:79
bool pick_primary_member(std::string &primary_uuid, Group_member_info_list *all_members_info, enum_primary_election_mode mode)
Get the member to elect from all group members.
Definition: primary_election_invocation_handler.cc:352
int sort_member_by_most_up_to_date(Group_member_info_list *all_members_info, Group_member_info_list_iterator lowest_version_end, std::vector< Gr_primary_election_member > &members)
Order list of members until lowest_version_end position.
Definition: primary_election_invocation_handler.cc:506
void print_gtid_info_in_log()
Print server executed GTID and applier retrieved GTID in logs.
Definition: primary_election_invocation_handler.cc:229
int legacy_primary_election(std::string &primary_uuid)
Execute the legacy (<8.0.12) primary election algorithm.
Definition: primary_election_invocation_handler.cc:294
int request_group_primary_election(std::string primary_uuid, enum_primary_election_mode mode)
Send a message to all members requesting an election.
Definition: primary_election_invocation_handler.cc:65
void set_election_running(bool election_running)
Sets if the election process is running or not.
Definition: primary_election_invocation_handler.cc:59
void notify_election_running()
Notify transaction consistency manager that election is running.
Definition: primary_election_invocation_handler.cc:633
void set_stop_wait_timeout(ulong timeout)
Sets the component stop timeout.
Definition: primary_election_invocation_handler.cc:47
Class that contains the primary election process logic for the elected primary.
Definition: primary_election_primary_process.h:41
Class that contains the primary election process logic for secondary members.
Definition: primary_election_secondary_process.h:40
Definition: single_primary_message.h:35
Group_member_info_list::iterator Group_member_info_list_iterator
Definition: member_info.h:793
std::vector< Group_member_info *, Malloc_allocator< Group_member_info * > > Group_member_info_list
Definition: member_info.h:792
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:498
mode
Definition: file_handle.h:61
enum_primary_election_mode
Enum for election types.
Definition: primary_election_include.h:33
void sort_members_for_election(Group_member_info_list *all_members_info, Group_member_info_list_iterator lowest_version_end)
Sort lower version members based on member weight if member version is greater than equal to PRIMARY_...
Definition: primary_election_invocation_handler.cc:617
Group_member_info_list_iterator sort_and_get_lowest_version_member_position(Group_member_info_list *all_members_info)
Sort members based on member_version and get first iterator position where member version differs.
Definition: primary_election_invocation_handler.cc:577
Definition: primary_election_invocation_handler.h:37
uint64_t delta
Definition: primary_election_invocation_handler.h:39
std::string uuid
Definition: primary_election_invocation_handler.h:38
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50