Skip to content

Commit 72db39f

Browse files
renecannaowazir-ahmed
authored andcommitted
docs: Add comprehensive Doxygen documentation for GTID refactoring
- Document addGtidInterval() function with parameter details and reconnection behavior - Add documentation for readall() method explaining robust error handling - Document connect_cb() and reader_cb() callbacks with resource management details - Document generate_mysql_gtid_executed_tables() with multi-phase process explanation - Focus on functionality, thread safety, and performance improvements - Provide clear parameter descriptions and return value semantics
1 parent b442db1 commit 72db39f

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

lib/GTID_Server_Data.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ static void gtid_timer_cb (struct ev_loop *loop, struct ev_timer *timer, int rev
4141
return;
4242
}
4343

44+
/**
45+
* @brief Data reception callback for established GTID server connections
46+
*
47+
* This callback handles reading GTID data from established connections to binlog readers.
48+
* It processes incoming GTID information and manages connection failures gracefully.
49+
*
50+
* On successful read:
51+
* - Processes the received GTID data
52+
* - Calls dump() to parse and update GTID sets
53+
*
54+
* On read failure:
55+
* - Marks the server connection as inactive
56+
* - Sets gtid_missing_nodes flag to trigger reconnection
57+
* - Performs proper cleanup of socket and watcher resources
58+
* - Clears the watcher reference to maintain clean state
59+
*
60+
* @param loop The event loop (unused in this implementation)
61+
* @param w The I/O watcher for data reception
62+
* @param revents The events that triggered this callback
63+
*
64+
* @note This function is critical for maintaining GTID synchronization stability
65+
* @note Proper resource cleanup prevents memory leaks and maintains system stability
66+
*/
4467
void reader_cb(struct ev_loop *loop, struct ev_io *w, int revents) {
4568
pthread_mutex_lock(&ev_loop_mutex);
4669
if (revents & EV_READ) {
@@ -63,6 +86,30 @@ void reader_cb(struct ev_loop *loop, struct ev_io *w, int revents) {
6386
pthread_mutex_unlock(&ev_loop_mutex);
6487
}
6588

89+
/**
90+
* @brief Connection establishment callback for GTID server connections
91+
*
92+
* This callback is triggered when a non-blocking connect() operation completes.
93+
* It handles both successful connections and connection failures with proper
94+
* resource cleanup and state management.
95+
*
96+
* On successful connection:
97+
* - Stops and frees the connect watcher
98+
* - Creates a new read watcher for data reception
99+
* - Starts the read watcher to begin GTID data processing
100+
*
101+
* On connection failure:
102+
* - Marks server as inactive
103+
* - Logs appropriate warning messages
104+
* - Performs proper cleanup of socket and watcher resources
105+
*
106+
* @param loop The event loop (unused in this implementation)
107+
* @param w The I/O watcher for the connection
108+
* @param revents The events that triggered this callback
109+
*
110+
* @note This function ensures proper resource management to prevent memory leaks
111+
* @note Takes ev_loop_mutex to ensure thread-safe operations
112+
*/
66113
void connect_cb(EV_P_ ev_io *w, int revents) {
67114
pthread_mutex_lock(&ev_loop_mutex);
68115
if (revents & EV_WRITE) {
@@ -166,6 +213,23 @@ GTID_Server_Data::~GTID_Server_Data() {
166213
free(data);
167214
}
168215

216+
/**
217+
* @brief Reads data from the GTID server connection socket
218+
*
219+
* Reads available data from the socket connection to the binlog reader.
220+
* Handles different read conditions to provide robust connection management:
221+
* - Successful read: Data is appended to internal buffer
222+
* - EOF (rc == 0): Connection was gracefully closed by peer
223+
* - Error conditions: Distinguishes between transient (EINTR/EAGAIN) and fatal errors
224+
*
225+
* This function is critical for maintaining stable GTID synchronization and
226+
* properly detecting connection failures for reconnection handling.
227+
*
228+
* @return bool True if read was successful or should be retried, false on fatal errors
229+
*
230+
* @note Expands buffer automatically when full to prevent data loss
231+
* @note EINTR and EAGAIN are not treated as errors for non-blocking sockets
232+
*/
169233
bool GTID_Server_Data::readall() {
170234
if (size == len) {
171235
// buffer is full, expand
@@ -412,6 +476,29 @@ void addGtid(const gtid_t& gtid, gtid_set_t& gtid_executed) {
412476
}
413477
}
414478

479+
/**
480+
* @brief Adds or updates a GTID interval in the executed set
481+
*
482+
* This function intelligently merges GTID intervals to prevent events_count reset
483+
* when a binlog reader reconnects and provides updated GTID sets. It handles
484+
* reconnection scenarios where the server provides updated transaction ID ranges.
485+
*
486+
* For example, during reconnection:
487+
* - Before disconnection: server_UUID:1-10
488+
* - After reconnection: server_UUID:1-19
489+
*
490+
* This function will update the existing interval rather than replacing it,
491+
* preserving the events_count metric accuracy.
492+
*
493+
* @param gtid_executed Reference to the GTID set to update
494+
* @param server_uuid The server UUID string
495+
* @param txid_start Starting transaction ID of the interval
496+
* @param txid_end Ending transaction ID of the interval
497+
* @return bool True if the GTID set was updated, false if interval already existed
498+
*
499+
* @note This function is critical for maintaining accurate GTID metrics across
500+
* binlog reader reconnections and preventing events_count resets.
501+
*/
415502
bool addGtidInterval(gtid_set_t& gtid_executed, std::string server_uuid, int64_t txid_start, int64_t txid_end) {
416503
bool updated = true;
417504

lib/MySQL_HostGroups_Manager.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,30 @@ bool MySQL_HostGroups_Manager::gtid_exists(MySrvC *mysrvc, char * gtid_uuid, uin
16801680
return ret;
16811681
}
16821682

1683+
/**
1684+
* @brief Generates and manages GTID connection tables for all MySQL servers
1685+
*
1686+
* This function synchronizes the GTID server connections with the current MySQL server
1687+
* configuration. It handles server additions, removals, and reconnections with improved
1688+
* lifecycle management for stable operation.
1689+
*
1690+
* The function operates in several phases:
1691+
* 1. Mark all existing GTID connections as potentially stale
1692+
* 2. Iterate through configured MySQL servers to validate existing connections
1693+
* 3. Establish new connections for servers that don't have active GTID connections
1694+
* 4. Clean up stale connections for servers that are no longer configured
1695+
*
1696+
* Key improvements in this implementation:
1697+
* - Uses stale_server tracking for efficient cleanup
1698+
* - Maintains proper lock ordering (gtid_rwlock → wrlock)
1699+
* - Reuses existing GTID_Server_Data objects when possible
1700+
* - Proper cleanup of ev_io watchers and socket resources
1701+
*
1702+
* @note This function must be called with appropriate locking to prevent race conditions
1703+
* @note Servers with OFFLINE_HARD status are skipped for connection establishment
1704+
*
1705+
* @thread_safety Thread-safe when called with proper external synchronization
1706+
*/
16831707
void MySQL_HostGroups_Manager::generate_mysql_gtid_executed_tables() {
16841708
// NOTE: We are required to lock while iterating over 'MyHostGroups'. Otherwise race conditions could take place,
16851709
// e.g. servers could be purged by 'purge_mysql_servers_table' and invalid memory be accessed.

0 commit comments

Comments
 (0)