-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathSlave.h
192 lines (120 loc) · 5.05 KB
/
Slave.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* Copyright 2011 ZAO "Begun".
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/*
*
* Guaranteed to work with MySQL5.1.23
*
*/
#ifndef __SLAVE_SLAVE_H_
#define __SLAVE_SLAVE_H_
#include <string>
#include <vector>
#include <map>
#include <set>
#include <mysql/my_global.h>
#undef min
#undef max
#include <mysql/mysql.h>
#include <mysql/m_ctype.h>
// MySQL headers are broken and cause errors when included. Hacks follow.
//#include <mysql/sql_common.h>
extern "C" {
my_bool cli_advanced_command(MYSQL *mysql, enum enum_server_command command,
const unsigned char *header, unsigned long header_length,
const unsigned char *arg, unsigned long arg_length,
my_bool skip_check, MYSQL_STMT *stmt);
#define simple_command(mysql, command, arg, length, skip_check) \
cli_advanced_command(mysql, command, 0, 0, arg, length, skip_check, NULL)
unsigned long cli_safe_read(MYSQL *mysql);
void end_server(MYSQL *mysql);
}
// End of hacks.
#include "slave_log_event.h"
#include "SlaveStats.h"
#define packet_end_data 1
#define ER_NET_PACKET_TOO_LARGE 1153
#define ER_MASTER_FATAL_ERROR_READING_BINLOG 1236
#define BIN_LOG_HEADER_SIZE 4
namespace slave
{
unsigned char *net_store_length_fast(unsigned char *pkg, unsigned int length);
class Slave
{
public:
typedef std::vector<std::pair<std::string, std::string> > table_order_t;
typedef std::map<std::pair<std::string, std::string>, callback> callbacks_t;
private:
static inline bool falseFunction() { return false; };
MYSQL mysql;
int m_server_id;
MasterInfo m_master_info;
ExtStateIface &ext_state;
table_order_t m_table_order;
callbacks_t m_callbacks;
typedef boost::function<void (unsigned int)> xid_callback_t;
xid_callback_t m_xid_callback;
RelayLogInfo m_rli;
void createDatabaseStructure_(table_order_t& tabs, RelayLogInfo& rli) const;
public:
Slave(ExtStateIface &state) : ext_state(state) {}
Slave(MasterInfo& _master_info, ExtStateIface &state) : m_master_info(_master_info), ext_state(state) {}
void setCallback(const std::string& _db_name, const std::string& _tbl_name, callback _callback) {
m_table_order.push_back(std::make_pair(_db_name, _tbl_name));
m_callbacks[std::make_pair(_db_name, _tbl_name)] = _callback;
ext_state.initTableCount(_db_name + "." + _tbl_name);
}
void setXidCallback(xid_callback_t _callback) {
m_xid_callback = _callback;
}
void get_remote_binlog( const boost::function< bool() >& _interruptFlag = &Slave::falseFunction );
void createDatabaseStructure() {
m_rli.clear();
createDatabaseStructure_(m_table_order, m_rli);
for (RelayLogInfo::name_to_table_t::iterator i = m_rli.m_table_map.begin(); i != m_rli.m_table_map.end(); ++i) {
i->second->m_callback = m_callbacks[i->first];
}
}
RelayLogInfo getRli() const {
return m_rli;
}
table_order_t getTableOrder() const {
return m_table_order;
}
void init();
int getServerOid() const { return m_server_id; }
// Closes connection, opened in get_remotee_binlog. Should be called if your have get_remote_binlog
// blocked on reading data from mysql server in the separate thread and you want to stop this thread.
// You should take care that interruptFlag will return 'true' after connection is closed.
void close_connection();
protected:
int connect_to_master(int reconnect = 0);
int safe_reconnect();
int safe_connect();
void check_master_version();
void check_master_binlog_format();
int process_event(const slave::Basic_event_info& bei, RelayLogInfo &rli, unsigned long long pos);
void request_dump(const std::string& logname, unsigned long start_position, MYSQL* mysql);
ulong read_event(MYSQL* mysql);
std::map<std::string,std::string> getRowType(const std::string& db_name,
const std::set<std::string>& tbl_names) const;
std::pair<std::string,unsigned int> getLastBinlog();
void createTable(RelayLogInfo& rli,
const std::string& db_name, const std::string& tbl_name,
const collate_map_t& collate_map, nanomysql::Connection& conn) const;
void register_slave_on_master(MYSQL* mysql);
void deregister_slave_on_master(MYSQL* mysql);
void generateSlaveId();
};
}
#endif