| 1 | /* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
| 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 as published by |
| 5 | the Free Software Foundation; version 2 of the License. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU General Public License |
| 13 | along with this program; if not, write to the Free Software |
| 14 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 15 | |
| 16 | #ifndef SEMISYNC_MASTER_ACK_RECEIVER_DEFINED |
| 17 | #define SEMISYNC_MASTER_ACK_RECEIVER_DEFINED |
| 18 | |
| 19 | #include "my_global.h" |
| 20 | #include "my_pthread.h" |
| 21 | #include "sql_class.h" |
| 22 | #include "semisync.h" |
| 23 | /** |
| 24 | Ack_receiver is responsible to control ack receive thread and maintain |
| 25 | slave information used by ack receive thread. |
| 26 | |
| 27 | There are mainly four operations on ack receive thread: |
| 28 | start: start ack receive thread |
| 29 | stop: stop ack receive thread |
| 30 | add_slave: maintain a new semisync slave's information |
| 31 | remove_slave: remove a semisync slave's information |
| 32 | */ |
| 33 | class Ack_receiver : public Repl_semi_sync_base |
| 34 | { |
| 35 | public: |
| 36 | Ack_receiver(); |
| 37 | ~Ack_receiver() {} |
| 38 | void cleanup(); |
| 39 | /** |
| 40 | Notify ack receiver to receive acks on the dump session. |
| 41 | |
| 42 | It adds the given dump thread into the slave list and wakes |
| 43 | up ack thread if it is waiting for any slave coming. |
| 44 | |
| 45 | @param[in] thd THD of a dump thread. |
| 46 | |
| 47 | @return it return false if succeeds, otherwise true is returned. |
| 48 | */ |
| 49 | bool add_slave(THD *thd); |
| 50 | |
| 51 | /** |
| 52 | Notify ack receiver not to receive ack on the dump session. |
| 53 | |
| 54 | it removes the given dump thread from slave list. |
| 55 | |
| 56 | @param[in] thd THD of a dump thread. |
| 57 | */ |
| 58 | void remove_slave(THD *thd); |
| 59 | |
| 60 | /** |
| 61 | Start ack receive thread |
| 62 | |
| 63 | @return it return false if succeeds, otherwise true is returned. |
| 64 | */ |
| 65 | bool start(); |
| 66 | |
| 67 | /** |
| 68 | Stop ack receive thread |
| 69 | */ |
| 70 | void stop(); |
| 71 | |
| 72 | /** |
| 73 | The core of ack receive thread. |
| 74 | |
| 75 | It monitors all slaves' sockets and receives acks when they come. |
| 76 | */ |
| 77 | void run(); |
| 78 | |
| 79 | void set_trace_level(unsigned long trace_level) |
| 80 | { |
| 81 | m_trace_level= trace_level; |
| 82 | } |
| 83 | private: |
| 84 | enum status {ST_UP, ST_DOWN, ST_STOPPING}; |
| 85 | uint8 m_status; |
| 86 | /* |
| 87 | Protect m_status, m_slaves_changed and m_slaves. ack thread and other |
| 88 | session may access the variables at the same time. |
| 89 | */ |
| 90 | mysql_mutex_t m_mutex; |
| 91 | mysql_cond_t m_cond; |
| 92 | /* If slave list is updated(add or remove). */ |
| 93 | bool m_slaves_changed; |
| 94 | |
| 95 | class Slave :public ilink |
| 96 | { |
| 97 | public: |
| 98 | THD *thd; |
| 99 | Vio vio; |
| 100 | |
| 101 | my_socket sock_fd() { return vio.mysql_socket.fd; } |
| 102 | uint server_id() { return thd->variables.server_id; } |
| 103 | }; |
| 104 | |
| 105 | I_List<Slave> m_slaves; |
| 106 | |
| 107 | pthread_t m_pid; |
| 108 | |
| 109 | /* Declare them private, so no one can copy the object. */ |
| 110 | Ack_receiver(const Ack_receiver &ack_receiver); |
| 111 | Ack_receiver& operator=(const Ack_receiver &ack_receiver); |
| 112 | |
| 113 | void set_stage_info(const PSI_stage_info &stage); |
| 114 | void wait_for_slave_connection(); |
| 115 | my_socket get_slave_sockets(fd_set *fds, uint *count); |
| 116 | }; |
| 117 | |
| 118 | extern Ack_receiver ack_receiver; |
| 119 | #endif |
| 120 | |