1 /* Copyright (c) 2014, 2021, 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 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 SEMISYNC_MASTER_ACK_RECEIVER_DEFINED
24 #define SEMISYNC_MASTER_ACK_RECEIVER_DEFINED
25 
26 #include <vector>
27 #include "my_global.h"
28 #include "my_thread.h"
29 #include "sql_class.h"
30 
31 struct Slave
32 {
33   uint32_t thread_id;
34   Vio vio;
35   uint server_id;
36   bool net_compress;
37 
sock_fdSlave38   my_socket sock_fd() const { return vio.mysql_socket.fd; }
39 };
40 
41 typedef std::vector<Slave> Slave_vector;
42 typedef Slave_vector::iterator Slave_vector_it;
43 
44 /**
45   Ack_receiver is responsible to control ack receive thread and maintain
46   slave information used by ack receive thread.
47 
48   There are mainly four operations on ack receive thread:
49   start: start ack receive thread
50   stop: stop ack receive thread
51   add_slave: maintain a new semisync slave's information
52   remove_slave: remove a semisync slave's information
53  */
54 class Ack_receiver : public ReplSemiSyncBase
55 {
56 public:
57   Ack_receiver();
58   ~Ack_receiver();
59 
60   /**
61      Notify ack receiver to receive acks on the dump session.
62 
63      It adds the given dump thread into the slave list and wakes
64      up ack thread if it is waiting for any slave coming.
65 
66      @param[in] thd  THD of a dump thread.
67 
68      @return it return false if succeeds, otherwise true is returned.
69   */
70   bool add_slave(THD *thd);
71 
72   /**
73     Notify ack receiver not to receive ack on the dump session.
74 
75     it removes the given dump thread from slave list.
76 
77     @param[in] thd  THD of a dump thread.
78   */
79   void remove_slave(THD *thd);
80 
81   /**
82     Start ack receive thread
83 
84     @return it return false if succeeds, otherwise true is returned.
85   */
86   bool start();
87 
88   /**
89      Stop ack receive thread
90   */
91   void stop();
92 
93   /**
94      The core of ack receive thread.
95 
96      It monitors all slaves' sockets and receives acks when they come.
97   */
98   void run();
99 
setTraceLevel(unsigned long trace_level)100   void setTraceLevel(unsigned long trace_level)
101   {
102     trace_level_= trace_level;
103   }
104 
init()105   bool init()
106   {
107     setTraceLevel(rpl_semi_sync_master_trace_level);
108     if (rpl_semi_sync_master_enabled)
109       return start();
110     return false;
111   }
112 private:
113   enum status {ST_UP, ST_DOWN, ST_STOPPING};
114   uint8 m_status;
115   /*
116     Protect m_status, m_slaves_changed and m_slaves. ack thread and other
117     session may access the variables at the same time.
118   */
119   mysql_mutex_t m_mutex;
120   mysql_cond_t m_cond;
121   /* If slave list is updated(add or remove). */
122   bool m_slaves_changed;
123   Slave_vector m_slaves;
124   my_thread_handle m_pid;
125 
126 /* Declare them private, so no one can copy the object. */
127   Ack_receiver(const Ack_receiver &ack_receiver);
128   Ack_receiver& operator=(const Ack_receiver &ack_receiver);
129 
130   void set_stage_info(const PSI_stage_info &stage);
131   void wait_for_slave_connection();
132 };
133 
134 extern Ack_receiver ack_receiver;
135 #endif
136