MySQL 8.4.0
Source Code Documentation
Primary_election_action Class Reference

The group action class to do migration to primary mode or elect a primary. More...

#include <primary_election_action.h>

Inheritance diagram for Primary_election_action:
[legend]

Public Types

enum  enum_action_execution_mode { PRIMARY_ELECTION_ACTION_PRIMARY_SWITCH = 0 , PRIMARY_ELECTION_ACTION_MODE_SWITCH = 1 , PRIMARY_ELECTION_ACTION_END = 2 }
 Enum for type of primary election executed. More...
 
enum  enum_primary_election_phase {
  PRIMARY_NO_PHASE = 0 , PRIMARY_VALIDATION_PHASE = 1 , PRIMARY_SAFETY_CHECK_PHASE = 2 , PRIMARY_ELECTION_PHASE = 3 ,
  PRIMARY_ELECTED_PHASE = 4
}
 Enum for the phases on the primary action. More...
 
enum  enum_primary_election_status { PRIMARY_ELECTION_INIT = 0 , PRIMARY_ELECTION_END_ELECTION = 1 , PRIMARY_ELECTION_END_ERROR = 2 }
 Enum for the phases on the primary action. More...
 
- Public Types inherited from Group_action
enum  enum_action_execution_result {
  GROUP_ACTION_RESULT_TERMINATED = 0 , GROUP_ACTION_RESULT_ERROR = 1 , GROUP_ACTION_RESULT_RESTART = 2 , GROUP_ACTION_RESULT_ABORTED = 3 ,
  GROUP_ACTION_RESULT_KILLED = 4 , GROUP_ACTION_RESULT_END = 5
}
 Enum for the end results of a action execution. More...
 

Public Member Functions

 Primary_election_action ()
 Create a new primary election action. More...
 
 Primary_election_action (std::string primary_uuid, my_thread_id thread_id, int32 transaction_wait_timeout=-1)
 Create a new primary election action with a given uuid. More...
 
 ~Primary_election_action () override
 
void get_action_message (Group_action_message **message) override
 Get the message with parameters to this action. More...
 
int process_action_message (Group_action_message &message, const std::string &message_origin) override
 
Group_action::enum_action_execution_result execute_action (bool invoking_member, Plugin_stage_monitor_handler *stage_handler, Notification_context *) override
 Execute the action. More...
 
bool stop_action_execution (bool killed) override
 
Group_action_diagnosticsget_execution_info () override
 Gets the info about execution, be it success or failure. More...
 
PSI_stage_key get_action_stage_termination_key () override
 For this action, what is the PSI key for the last stage when the action is terminating. More...
 
- Public Member Functions inherited from Group_action
virtual ~Group_action ()=0
 

Private Member Functions

bool persist_variable_values ()
 Persist the value of the variables changed in the action. More...
 
void log_result_execution (bool error, bool aborted, bool mode_changed, std::string &error_message)
 Log the result of the execution. More...
 
int after_view_change (const std::vector< Gcs_member_identifier > &joining, const std::vector< Gcs_member_identifier > &leaving, const std::vector< Gcs_member_identifier > &group, bool is_leaving, bool *skip_election, enum_primary_election_mode *election_mode, std::string &suggested_primary) override
 Executed after view install and before primary election. More...
 
int after_primary_election (std::string primary_uuid, enum_primary_election_primary_change_status primary_change_status, enum_primary_election_mode election_mode, int error) override
 Executed after primary election. More...
 
int before_message_handling (const Plugin_gcs_message &message, const std::string &message_origin, bool *skip_message) override
 Executed before the message is processed. More...
 
bool stop_transaction_monitor_thread ()
 Stop the transaction_monitor_thread if running. More...
 
void change_action_phase (enum_primary_election_phase phase)
 Changes the phase where the action is currently. More...
 
- Private Member Functions inherited from Group_event_observer
virtual ~Group_event_observer ()=0
 

Private Attributes

enum_action_execution_mode action_execution_mode
 Is this an primary change or mode change. More...
 
enum_primary_election_phase current_action_phase
 The current phase. More...
 
mysql_mutex_t phase_lock
 Lock for the phase change. More...
 
bool single_election_action_aborted
 Is this action aborted. More...
 
bool error_on_primary_election
 Was there an error in the election. More...
 
bool action_killed
 Was this action order to terminate by a kill signal. More...
 
std::string appointed_primary_uuid
 The primary to elect. More...
 
std::string appointed_primary_gcs_id
 The id of the primary to elect. More...
 
std::string invoking_member_gcs_id
 The id of the invoking primary. More...
 
std::string old_primary_uuid
 The uuid of the original master at action invocation. More...
 
bool is_primary
 If this member is old primary. More...
 
my_thread_id invoking_thread_id
 The thread that invoked this action - if applicable, 0 otherwise. More...
 
bool is_primary_election_invoked
 Is the primary election invoked. More...
 
enum_primary_election_status m_execution_status {PRIMARY_ELECTION_INIT}
 primary election status More...
 
bool is_transaction_queue_applied
 Is the transaction back log consumed. More...
 
mysql_mutex_t notification_lock
 The lock for notifications. More...
 
mysql_cond_t notification_cond
 The condition for notifications. More...
 
Primary_election_validation_handler validation_handler
 The handler for primary election validations. More...
 
Group_action_diagnostics execution_message_area
 Place to store result messages. More...
 
int32 m_transaction_wait_timeout = {-1}
 The number of seconds to wait before setting the THD::KILL_CONNECTION flag for the transactions that did not reach commit stage. More...
 
Transaction_monitor_threadtransaction_monitor_thread {nullptr}
 Used to monitor transactions, this stops the new transactions and sets the THD::KILL_CONNECTION flag for the transactions that did not reach commit stage post timeout expire. More...
 

Detailed Description

The group action class to do migration to primary mode or elect a primary.

Member Enumeration Documentation

◆ enum_action_execution_mode

Enum for type of primary election executed.

Enumerator
PRIMARY_ELECTION_ACTION_PRIMARY_SWITCH 
PRIMARY_ELECTION_ACTION_MODE_SWITCH 
PRIMARY_ELECTION_ACTION_END 

◆ enum_primary_election_phase

Enum for the phases on the primary action.

Enumerator
PRIMARY_NO_PHASE 
PRIMARY_VALIDATION_PHASE 
PRIMARY_SAFETY_CHECK_PHASE 
PRIMARY_ELECTION_PHASE 
PRIMARY_ELECTED_PHASE 

◆ enum_primary_election_status

Enum for the phases on the primary action.

Enumerator
PRIMARY_ELECTION_INIT 
PRIMARY_ELECTION_END_ELECTION 
PRIMARY_ELECTION_END_ERROR 

Constructor & Destructor Documentation

◆ Primary_election_action() [1/2]

Primary_election_action::Primary_election_action ( )

Create a new primary election action.

◆ Primary_election_action() [2/2]

Primary_election_action::Primary_election_action ( std::string  primary_uuid,
my_thread_id  thread_id,
int32  transaction_wait_timeout = -1 
)

Create a new primary election action with a given uuid.

Parameters
primary_uuidthe primary uuid to elect, can be empty
thread_idthe local thread id that is invoking this action
transaction_wait_timeoutThe number of seconds to wait before setting the THD::KILL_CONNECTION flag for the transactions that did not reach commit stage.

◆ ~Primary_election_action()

Primary_election_action::~Primary_election_action ( )
override

Assert has been used since object has not been destroyed at the exit point code. If this ever occurs, the scenario needs to be analyzed to understand why object was not deleted on the exit points of the primary election.

delete transaction_monitor_thread is present at exit points when a change of primary is finishing. However if delete is not triggered at those exit points then object should be destroyed here. This code is added for safety.

Member Function Documentation

◆ after_primary_election()

int Primary_election_action::after_primary_election ( std::string  primary_uuid,
enum_primary_election_primary_change_status  primary_change_status,
enum_primary_election_mode  election_mode,
int  error 
)
overrideprivatevirtual

Executed after primary election.

Parameters
primary_uuidthe elected primary
primary_change_statusif the primary changed after the election
election_modewhat was the election mode
errorif there was and error on the process

Implements Group_event_observer.

◆ after_view_change()

int Primary_election_action::after_view_change ( const std::vector< Gcs_member_identifier > &  joining,
const std::vector< Gcs_member_identifier > &  leaving,
const std::vector< Gcs_member_identifier > &  group,
bool  is_leaving,
bool *  skip_election,
enum_primary_election_mode election_mode,
std::string &  suggested_primary 
)
overrideprivatevirtual

Executed after view install and before primary election.

Parameters
joiningmembers joining the group
leavingmembers leaving the group
groupmembers in the group
is_leavingis the member leaving
[out]skip_electionskip primary election on view
[out]election_modeelection mode
[out]suggested_primarywhat should be the next primary to elect

Either: -This is a change from SP and the the old primary died, so there is no invoking member, nor a member that is waiting on local transactions. -On changes from multi primary, and the invoking member left

For all the old secondaries that remain this means:

  1. If election was already called, then we don't care
  2. If election was not yet called then, either 2.1 we are still validating the appointed primary, so we just change what the invoking member is 2.2 we have already validate it, so if this is the new invoker it must invoke the new primary election

Note that this state change from validation to the next state is not in a group logical moment so different members can be at different stages. Still all that members care is the moment the election is called, so the result will be the same at all members. If the primary is not valid, the stage never changed from validation so member will invoke the election here. If valid, members will either update the invoking member or invoke the election according to the stage (hence the phase lock)

We also make the failover primary election process to be skipped, because we want a specific primary to be elected.

There is an election and the primary being elected dies.

Case 1: We are moving to another primary, and it failed. The process will fail abort but we still try to invoke an election to go back to the old primary.

Case 2: We are moving to single primary mode and the appointed primary died, then we move to another primary and emit a warning

Note 1: We pass election params instead of invoking them because of the hook mechanism that is invoking this method and is also used in the invoked election methods.

Check if we can change to the old primary. If this variable is empty, it is not present.

Implements Group_event_observer.

◆ before_message_handling()

int Primary_election_action::before_message_handling ( const Plugin_gcs_message message,
const std::string &  message_origin,
bool *  skip_message 
)
overrideprivatevirtual

Executed before the message is processed.

Parameters
messageThe GCS message
message_originThe member that sent this message (address)
[out]skip_messageskip message handling if true

Implements Group_event_observer.

◆ change_action_phase()

void Primary_election_action::change_action_phase ( enum_primary_election_phase  phase)
private

Changes the phase where the action is currently.

Parameters
phasethe new election primary execution phase

◆ execute_action()

Group_action::enum_action_execution_result Primary_election_action::execute_action ( bool  invoking_member,
Plugin_stage_monitor_handler stage_handler,
Notification_context  
)
overridevirtual

Execute the action.

Parameters
invoking_memberis the member that invoked it
stage_handlerthe stage handler to report progress
Returns
the execution result

Observers have been de-registered so transaction_monitor_thread should have been destroyed by now in after_view_change or after_primary_election. If not, then destroy the object now(during UDF failure it has been observed intermittently object was not destroyed in after_view_change or after_primary_election).

Implements Group_action.

◆ get_action_message()

void Primary_election_action::get_action_message ( Group_action_message **  message)
overridevirtual

Get the message with parameters to this action.

Parameters
[out]messagethe message to start the action

Implements Group_action.

◆ get_action_stage_termination_key()

PSI_stage_key Primary_election_action::get_action_stage_termination_key ( )
overridevirtual

For this action, what is the PSI key for the last stage when the action is terminating.

Returns
The stage key for this class

Reimplemented from Group_action.

◆ get_execution_info()

Group_action_diagnostics * Primary_election_action::get_execution_info ( )
overridevirtual

Gets the info about execution, be it success or failure.

Returns
the execution diagnostics object that was the message and its level

Implements Group_action.

◆ log_result_execution()

void Primary_election_action::log_result_execution ( bool  error,
bool  aborted,
bool  mode_changed,
std::string &  error_message 
)
private

Log the result of the execution.

Parameters
errorDid an error occurred
abortedwas the action aborted?
mode_changedwas the mode changed to single primary?
error_messagedetails of error

◆ persist_variable_values()

bool Primary_election_action::persist_variable_values ( )
private

Persist the value of the variables changed in the action.

Returns
true if a problem was found, false otherwise

◆ process_action_message()

int Primary_election_action::process_action_message ( Group_action_message message,
const std::string &  message_origin 
)
overridevirtual

Implements Group_action.

◆ stop_action_execution()

bool Primary_election_action::stop_action_execution ( bool  killed)
overridevirtual

When a UDF group_replication_set_as_primary is killed using 'KILL connection_id' code reaches here.

Implements Group_action.

◆ stop_transaction_monitor_thread()

bool Primary_election_action::stop_transaction_monitor_thread ( )
private

Stop the transaction_monitor_thread if running.

Returns
status
Return values
truefailed to stop the thread
falsethread stopped successfully.

Member Data Documentation

◆ action_execution_mode

enum_action_execution_mode Primary_election_action::action_execution_mode
private

Is this an primary change or mode change.

◆ action_killed

bool Primary_election_action::action_killed
private

Was this action order to terminate by a kill signal.

◆ appointed_primary_gcs_id

std::string Primary_election_action::appointed_primary_gcs_id
private

The id of the primary to elect.

◆ appointed_primary_uuid

std::string Primary_election_action::appointed_primary_uuid
private

The primary to elect.

◆ current_action_phase

enum_primary_election_phase Primary_election_action::current_action_phase
private

The current phase.

◆ error_on_primary_election

bool Primary_election_action::error_on_primary_election
private

Was there an error in the election.

◆ execution_message_area

Group_action_diagnostics Primary_election_action::execution_message_area
private

Place to store result messages.

◆ invoking_member_gcs_id

std::string Primary_election_action::invoking_member_gcs_id
private

The id of the invoking primary.

◆ invoking_thread_id

my_thread_id Primary_election_action::invoking_thread_id
private

The thread that invoked this action - if applicable, 0 otherwise.

◆ is_primary

bool Primary_election_action::is_primary
private

If this member is old primary.

◆ is_primary_election_invoked

bool Primary_election_action::is_primary_election_invoked
private

Is the primary election invoked.

◆ is_transaction_queue_applied

bool Primary_election_action::is_transaction_queue_applied
private

Is the transaction back log consumed.

◆ m_execution_status

enum_primary_election_status Primary_election_action::m_execution_status {PRIMARY_ELECTION_INIT}
private

primary election status

◆ m_transaction_wait_timeout

int32 Primary_election_action::m_transaction_wait_timeout = {-1}
private

The number of seconds to wait before setting the THD::KILL_CONNECTION flag for the transactions that did not reach commit stage.

Client connection is dropped.

◆ notification_cond

mysql_cond_t Primary_election_action::notification_cond
private

The condition for notifications.

◆ notification_lock

mysql_mutex_t Primary_election_action::notification_lock
private

The lock for notifications.

◆ old_primary_uuid

std::string Primary_election_action::old_primary_uuid
private

The uuid of the original master at action invocation.

◆ phase_lock

mysql_mutex_t Primary_election_action::phase_lock
private

Lock for the phase change.

◆ single_election_action_aborted

bool Primary_election_action::single_election_action_aborted
private

Is this action aborted.

◆ transaction_monitor_thread

Transaction_monitor_thread* Primary_election_action::transaction_monitor_thread {nullptr}
private

Used to monitor transactions, this stops the new transactions and sets the THD::KILL_CONNECTION flag for the transactions that did not reach commit stage post timeout expire.

Client connection is dropped.

◆ validation_handler

Primary_election_validation_handler Primary_election_action::validation_handler
private

The handler for primary election validations.


The documentation for this class was generated from the following files: