@@ -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+ */
4467void 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+ */
66113void 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+ */
169233bool 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+ */
415502bool addGtidInterval (gtid_set_t & gtid_executed, std::string server_uuid, int64_t txid_start, int64_t txid_end) {
416503 bool updated = true ;
417504
0 commit comments