-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathSlaveStats.h
178 lines (153 loc) · 6.24 KB
/
SlaveStats.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
/* 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/>.
*/
#ifndef __SLAVE_SLAVESTATS_H_
#define __SLAVE_SLAVESTATS_H_
/*
* WARNING WARNING WARNING
*
* This file is intended as a demonstration only.
* The code here works, but is not thread-safe or production-ready.
* Please provide your own implementation to fill the gaps according
* to your particular project's needs.
*/
#include <sys/time.h>
namespace slave
{
struct MasterInfo {
std::string host;
unsigned int port;
std::string user;
std::string password;
std::string master_log_name;
unsigned long master_log_pos;
unsigned int connect_retry;
MasterInfo() : port(3306), master_log_pos(0), connect_retry(10) {}
MasterInfo(std::string host_, unsigned int port_, std::string user_,
std::string password_, unsigned int connect_retry_) :
host(host_),
port(port_),
user(user_),
password(password_),
master_log_name(),
master_log_pos(0),
connect_retry(connect_retry_)
{}
};
struct State {
time_t connect_time;
time_t last_filtered_update;
time_t last_event_time;
time_t last_update;
std::string master_log_name;
unsigned long master_log_pos;
unsigned long intransaction_pos;
unsigned int connect_count;
bool state_processing;
State() :
connect_time(0),
last_filtered_update(0),
last_event_time(0),
last_update(0),
master_log_pos(0),
intransaction_pos(0),
connect_count(0),
state_processing(false)
{}
};
struct ExtStateIface {
virtual State getState() = 0;
virtual void setConnecting() = 0;
virtual time_t getConnectTime() = 0;
virtual void setLastFilteredUpdateTime() = 0;
virtual time_t getLastFilteredUpdateTime() = 0;
virtual void setLastEventTimePos(time_t t, unsigned long pos) = 0;
virtual time_t getLastUpdateTime() = 0;
virtual time_t getLastEventTime() = 0;
virtual unsigned long getIntransactionPos() = 0;
virtual void setMasterLogNamePos(const std::string& log_name, unsigned long pos) = 0;
virtual unsigned long getMasterLogPos() = 0;
virtual std::string getMasterLogName() = 0;
// Ñîõðàíÿåò master info â persistent õðàíèëèùå, íàïðèìåð, ôàéë èëè ÁÄ.
//  ñëó÷àå êàêîé-íèáóäü îøèáêè, áóäåò ïûòàòüñÿ ñîõðàíèòü master info äî
// òåõ ïîð, ïîêà íå óäàñòñÿ ýòî ñäåëàòü.
virtual void saveMasterInfo() = 0;
// Ôóíêöèÿ ÷èòàåò master info èç persistent õðàíèëèùà.
// Åñëè master info íå áûë ðàíåå ñîõðàí¸í (íàïðèìåð, ïåðâûé çàïóñê äåìîíà),
// òî áèáëèîòåêà ÷èòàåò áèíëîãè ñ òåêóùåé ïîçèöèè.
// Ôóíêöèÿ âîçâðàùàåò true, åñëè ñ÷èòàíà ñîõðàí¸ííàÿ ïîçèöèÿ, è false - åñëè
// íåò ñîõðàí¸ííîé ïîçèöèè (â ýòîì ñëó÷àå ôóíêöèÿ îáíóëÿåò ïîçèöèþ è î÷èùàåò
// èìÿ ëîã-ôàéëà).
//  ñëó÷àå êàêîé-íèáóäü îøèáêè ÷òåíèÿ èç õðàíèëèùà, ôóíêöèÿ áóäåò ïûòàòüñÿ
// ïðî÷èòàòü ñíîâà, ïîêà íå ñìîæåò ïðî÷èòàòü ñîõðàí¸ííóþ ïîçèöèþ èëè âûÿñíèòü
// å¸ îòñóòñòâèå.
virtual bool loadMasterInfo(std::string& logname, unsigned long& pos) = 0;
// Ðàáîòàåò òàê æå, êàê loadMasterInfo(), òîëüêî çàïèñûâàåò â pos ïîñëåäíþþ
// òåêóùóþ ïîçèöèþ âíóòðè òðàíçàêöèè, åñëè òàêàÿ åñòü.
bool getMasterInfo(std::string& logname, unsigned long& pos)
{
unsigned long in_trans_pos = getIntransactionPos();
std::string master_logname = getMasterLogName();
if(in_trans_pos!=0 && !master_logname.empty()) {
logname = master_logname;
pos = in_trans_pos;
return true;
} else
return loadMasterInfo(logname, pos);
}
virtual unsigned int getConnectCount() = 0;
virtual void setStateProcessing(bool _state) = 0;
virtual bool getStateProcessing() = 0;
// Ñòàíäàðòíîãî ôîðìàòà äëÿ ñòàòèñòèêè ðàñïðåäåëåíèÿ ñîáûòèé ïî òàáëèöàì íåò,
// ïîýòîìó íåò ôóíêöèé ïîëó÷åíèÿ ýòîé ñòàòèñòèêè.
virtual void initTableCount(const std::string& t) = 0;
virtual void incTableCount(const std::string& t) = 0;
};
// Îáúåêò-çàãëóøêà äëÿ îòâåòû íà çàïðîñû ñòàòû ÷åðåç StateHolder, êîãäà libslave
// åù¸ íå ïðîèíèöèàëèçèðîâàí
struct EmptyExtState: public ExtStateIface, protected State {
~EmptyExtState() {}
virtual State getState() { return *this; }
virtual void setConnecting() {}
virtual time_t getConnectTime() { return 0; }
virtual void setLastFilteredUpdateTime() {}
virtual time_t getLastFilteredUpdateTime() { return 0; }
virtual void setLastEventTimePos(time_t t, unsigned long pos) {}
virtual time_t getLastUpdateTime() { return 0; }
virtual time_t getLastEventTime() { return 0; }
virtual unsigned long getIntransactionPos() { return 0; }
virtual void setMasterLogNamePos(const std::string& log_name, unsigned long pos) {}
virtual unsigned long getMasterLogPos() { return 0; }
virtual std::string getMasterLogName() { return ""; }
virtual void saveMasterInfo() {}
virtual bool loadMasterInfo(std::string& logname, unsigned long& pos) { while(true); return false; }
virtual unsigned int getConnectCount() { return 0; }
virtual void setStateProcessing(bool _state) {}
virtual bool getStateProcessing() { return false; }
virtual void initTableCount(const std::string& t) {}
virtual void incTableCount(const std::string& t) {}
};
// Ïðåäíàçíà÷åí äëÿ õðàíåíèÿ â ñèíãëòîíå ExtStateIface èëè åãî ïîòîìêà
struct StateHolder {
typedef boost::shared_ptr<ExtStateIface> PExtState;
PExtState ext_state;
StateHolder() :
ext_state(new EmptyExtState)
{}
ExtStateIface& operator()()
{
return *ext_state;
}
};
}
#endif