forked from Cylix/cpp_redis
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathsentinel.hpp
More file actions
450 lines (387 loc) · 11.7 KB
/
sentinel.hpp
File metadata and controls
450 lines (387 loc) · 11.7 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// The MIT License (MIT)
//
// Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef CPP_REDIS_CORE_SENTINEL_HPP_
#define CPP_REDIS_CORE_SENTINEL_HPP_
#include <atomic>
#include <condition_variable>
#include <queue>
#include <utility>
#include <vector>
#include <cpp_redis/misc/logger.hpp>
#include <cpp_redis/network/redis_connection.hpp>
namespace cpp_redis {
//!
//! cpp_redis::sentinel is the class providing sentinel configuration.
//! It is meant to be used for sending sentinel-related commands to the remote
//! server and receiving its replies. It is also meant to be used with
//! cpp_redis::client and cpp_redis::subscriber for high availability
//! (automatic failover if reconnection is enabled).
//!
//!
class sentinel {
public:
//!
//! ctor & dtor
//!
//!
#ifndef __CPP_REDIS_USE_CUSTOM_TCP_CLIENT
//!
//! default ctor
//!
//!
sentinel();
#endif // __CPP_REDIS_USE_CUSTOM_TCP_CLIENT
//!
//! custom ctor to specify custom tcp_client
//!
//! @param tcp_client tcp client to be used for network communications
//!
//!
explicit sentinel(
const shared_ptr<tcp_client_iface_t> &tcp_client);
//!
//! dtor
//!
//!
~sentinel();
//!
//! copy ctor
//!
//!
sentinel(const sentinel &) = delete;
//!
//! assignment operator
//!
//!
sentinel &operator=(const sentinel &) = delete;
public:
//!
//! callback to be called whenever a reply has been received
//!
//!
using reply_callback_t = std::function<void(reply_t &)>;
//!
//! send the given command
//! the command is actually pipelined and only buffered, so nothing is sent
//! to the network please call commit() to flush the buffer
//!
//! @param sentinel_cmd command to be sent
//! @param callback callback to be called when reply is received for this
//! command
//! @return current instance
//!
//!
sentinel &send(const vector<string_t> &sentinel_cmd,
const reply_callback_t &callback = nullptr);
//!
//! commit pipelined transaction
//! that is, send to the network all commands pipelined by calling send()
//!
//! @return current instance
//!
//!
sentinel &commit();
//!
//! same as commit(), but synchronous
//! will block until all pending commands have been sent and that a reply has
//! been received for each of them and all underlying callbacks completed
//!
//! @return current instance
//!
//!
sentinel &sync_commit();
//!
//! same as sync_commit, but with a timeout
//! will simply block until it completes or timeout expires
//!
//! @return current instance
//!
//!
template <class Rep, class Period>
sentinel &sync_commit(const std::chrono::duration<Rep, Period> &timeout) {
try_commit();
std::unique_lock<mutex_t> lock_callback(m_callbacks_mutex);
__CPP_REDIS_LOG(debug,
"cpp_redis::sentinel waiting for callbacks to complete");
if (!m_sync_condvar.wait_for(lock_callback, timeout, [=] {
return m_callbacks_running == 0 && m_callbacks.empty();
})) {
__CPP_REDIS_LOG(debug,
"cpp_redis::sentinel finished waiting for callback");
} else {
__CPP_REDIS_LOG(debug,
"cpp_redis::sentinel timed out waiting for callback");
}
return *this;
}
public:
//!
//! add a sentinel definition. Required for connect() or
//! get_master_addr_by_name() when autoconnect is enabled.
//!
//! @param host sentinel host
//! @param port sentinel port
//! @param timeout_ms maximum time to connect
//! @return current instance
//!
//!
sentinel &add_sentinel(const string_t &host, size_t port,
uint_t timeout_ms = 0);
//!
//! clear all existing sentinels.
//!
//!
void clear_sentinels();
public:
//!
//! disconnect from redis server
//!
//! @param wait_for_removal when sets to true, disconnect blocks until the
//! underlying TCP client has been effectively removed from the io_service
//! and that all the underlying callbacks have completed.
//!
//!
void disconnect(bool wait_for_removal = false);
//!
//! @return whether we are connected to the redis server or not
//!
//!
bool is_connected();
//!
//! handlers called whenever disconnection occurred
//! function takes the sentinel current instance as parameter
//!
//!
using sentinel_disconnect_handler_t = std::function<void(sentinel &)>;
//!
//! Connect to 1st active sentinel we find. Requires add_sentinel() to be
//! called first will use timeout set for each added sentinel independently
//!
//! @param disconnect_handler handler to be called whenever disconnection
//! occurs
//!
//!
void connect_sentinel(
const sentinel_disconnect_handler_t &disconnect_handler = nullptr);
//!
//! Connect to named sentinel
//!
//! @param host host to be connected to
//! @param port port to be connected to
//! @param timeout_ms maximum time to connect
//! @param disconnect_handler handler to be called whenever disconnection
//! occurs
//!
//!
void
connect(const string_t &host, size_t port,
const sentinel_disconnect_handler_t &disconnect_handler = nullptr,
uint_t timeout_ms = 0);
//!
//! Used to find the current redis master by asking one or more sentinels.
//! Use high availability. Handles connect() and disconnect() automatically
//! when autoconnect=true This method is synchronous. No need to call
//! sync_commit() or process a reply callback. Call add_sentinel() before
//! using when autoconnect==true
//!
//! @param name sentinel name
//! @param host sentinel host
//! @param port sentinel port
//! @param autoconnect autoconnect we loop through and connect/disconnect as
//! necessary to sentinels that were added using add_sentinel().
//! Otherwise we rely on the call to connect to a sentinel before calling
//! this method.
//! @return true if a master was found and fills in host and port output
//! parameters, false otherwise
//!
bool get_master_addr_by_name(const string_t &name, string_t &host,
size_t &port, bool autoconnect = true);
public:
sentinel &ckquorum(const string_t &name,
const reply_callback_t &reply_callback = nullptr);
sentinel &failover(const string_t &name,
const reply_callback_t &reply_callback = nullptr);
sentinel &flushconfig(const reply_callback_t &reply_callback = nullptr);
sentinel &master(const string_t &name,
const reply_callback_t &reply_callback = nullptr);
sentinel &masters(const reply_callback_t &reply_callback = nullptr);
sentinel &monitor(const string_t &name, const string_t &ip, size_t port,
size_t quorum,
const reply_callback_t &reply_callback = nullptr);
sentinel &ping(const reply_callback_t &reply_callback = nullptr);
sentinel &remove(const string_t &name,
const reply_callback_t &reply_callback = nullptr);
sentinel &reset(const string_t &pattern,
const reply_callback_t &reply_callback = nullptr);
sentinel &sentinels(const string_t &name,
const reply_callback_t &reply_callback = nullptr);
sentinel &set(const string_t &name, const string_t &option,
const string_t &value,
const reply_callback_t &reply_callback = nullptr);
sentinel &slaves(const string_t &name,
const reply_callback_t &reply_callback = nullptr);
public:
//!
//! store informations related to a sentinel
//! typically, host, port and connection timeout
//!
//!
class sentinel_def {
public:
//!
//! ctor
//!
//!
sentinel_def(string_t host, size_t port, uint_t timeout_ms)
: m_host(std::move(host)), m_port(port), m_timeout_ms(timeout_ms) {}
//!
//! dtor
//!
//!
~sentinel_def() = default;
public:
//!
//! @return sentinel host
//!
//!
const string_t &get_host() const { return m_host; }
//!
//! @return sentinel port
//!
//!
size_t get_port() const { return m_port; }
//!
//! @return timeout for sentinel
//!
//!
uint_t get_timeout_ms() const { return m_timeout_ms; }
//!
//! set connect timeout for sentinel in ms
//! @param timeout_ms new value
//!
//!
void set_timeout_ms(uint_t timeout_ms) { m_timeout_ms = timeout_ms; }
private:
//!
//! sentinel host
//!
//!
string_t m_host;
//!
//! sentinel port
//!
//!
size_t m_port;
//!
//! connect timeout config
//!
//!
uint_t m_timeout_ms;
};
public:
//!
//! @return sentinels
//!
//!
const vector<sentinel_def> &get_sentinels() const;
//!
//! @return sentinels (non-const version)
//!
//!
vector<sentinel_def> &get_sentinels();
private:
//!
//! redis connection receive handler, triggered whenever a reply has been
//! read by the redis connection
//!
//! @param connection redis_connection instance
//! @param reply parsed reply
//!
//!
void connection_receive_handler(redis_connection_t &connection,
reply_t &reply);
//!
//! redis_connection disconnection handler, triggered whenever a
//! disconnection occurred
//!
//! @param connection redis_connection instance
//!
//!
void connection_disconnect_handler(redis_connection_t &connection);
//!
//! Call the user-defined disconnection handler
//!
//!
void call_disconnect_handler();
//!
//! reset the queue of pending callbacks
//!
//!
void clear_callbacks();
//!
//! try to commit the pending pipelined
//! if client is disconnected, will throw an exception and clear all pending
//! callbacks (call clear_callbacks())
//!
//!
void try_commit();
private:
//!
//! A pool of 1 or more sentinels we ask to determine which redis server is
//! the master.
//!
//!
vector<sentinel_def> m_sentinels;
//!
//! tcp client for redis sentinel connection
//!
//!
redis_connection_t m_client;
//!
//! queue of callback to process
//!
//!
queue<reply_callback_t> m_callbacks;
//!
//! user defined disconnection handler to be called on disconnection
//!
//!
sentinel_disconnect_handler_t m_disconnect_handler;
//!
//! callbacks thread safety
//!
//!
mutex_t m_callbacks_mutex;
//!
//! condvar for callbacks updates
//!
//!
condition_variable_t m_sync_condvar;
//!
//! number of callbacks currently being running
//!
//!
atomic_uint m_callbacks_running;
};
using sentinel_t = sentinel;
} // namespace cpp_redis
#endif // !CPP_REDIS_CORE_SENTINEL_HPP_