| cpp_redis 4.0.0 cpp_redis is a C++11 Asynchronous Multi-Platform Lightweight Redis Client, with support for synchronous operations and pipelining.

sentinel.hpp
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22
23 #pragma once
24
25 #include <atomic>
26 #include <condition_variable>
27 #include <queue>
28 #include <vector>
29
30 #include <cpp_redis/misc/logger.hpp>
31 #include <cpp_redis/network/redis_connection.hpp>
32
33 namespace cpp_redis {
34
40 class sentinel {
41 public:
43 #ifndef __CPP_REDIS_USE_CUSTOM_TCP_CLIENT
44 sentinel(void);
46 #endif /* __CPP_REDIS_USE_CUSTOM_TCP_CLIENT */
47
53 explicit sentinel(const std::shared_ptr<network::tcp_client_iface>& tcp_client);
54
56 ~sentinel(void);
57
59 sentinel(const sentinel&) = delete;
61 sentinel& operator=(const sentinel&) = delete;
62
63 public:
67 typedef std::function<void(reply&)> reply_callback_t;
68
78 sentinel& send(const std::vector<std::string>& sentinel_cmd, const reply_callback_t& callback = nullptr);
79
86 sentinel& commit(void);
87
94 sentinel& sync_commit(void);
95
102 template <class Rep, class Period>
103 sentinel&
104 sync_commit(const std::chrono::duration<Rep, Period>& timeout) {
105 try_commit();
106
107 std::unique_lock<std::mutex> lock_callback(m_callbacks_mutex);
108 __CPP_REDIS_LOG(debug, "cpp_redis::sentinel waiting for callbacks to complete");
109 if (!m_sync_condvar.wait_for(lock_callback, timeout, [=] {
110 return m_callbacks_running == 0 && m_callbacks.empty();
111 })) {
112 __CPP_REDIS_LOG(debug, "cpp_redis::sentinel finished waiting for callback");
113 }
114 else {
115 __CPP_REDIS_LOG(debug, "cpp_redis::sentinel timed out waiting for callback");
116 }
117 return *this;
118 }
119
120 public:
129 sentinel& add_sentinel(const std::string& host, std::size_t port, std::uint32_t timeout_msecs = 0);
130
134 void clear_sentinels(void);
135
136 public:
142 void disconnect(bool wait_for_removal = false);
143
147 bool is_connected(void);
148
153 typedef std::function<void(sentinel&)> sentinel_disconnect_handler_t;
154
161 void connect_sentinel(const sentinel_disconnect_handler_t& disconnect_handler = nullptr);
162
171 void connect(
172 const std::string& host,
173 std::size_t port,
174 const sentinel_disconnect_handler_t& disconnect_handler = nullptr,
175 std::uint32_t timeout_msecs = 0);
176
190 bool get_master_addr_by_name(
191 const std::string& name,
192 std::string& host,
193 std::size_t& port,
194 bool autoconnect = true);
195
196 public:
197 sentinel& ckquorum(const std::string& name, const reply_callback_t& reply_callback = nullptr);
198 sentinel& failover(const std::string& name, const reply_callback_t& reply_callback = nullptr);
199 sentinel& flushconfig(const reply_callback_t& reply_callback = nullptr);
200 sentinel& master(const std::string& name, const reply_callback_t& reply_callback = nullptr);
201 sentinel& masters(const reply_callback_t& reply_callback = nullptr);
202 sentinel& monitor(const std::string& name, const std::string& ip, std::size_t port, std::size_t quorum, const reply_callback_t& reply_callback = nullptr);
203 sentinel& ping(const reply_callback_t& reply_callback = nullptr);
204 sentinel& remove(const std::string& name, const reply_callback_t& reply_callback = nullptr);
205 sentinel& reset(const std::string& pattern, const reply_callback_t& reply_callback = nullptr);
206 sentinel& sentinels(const std::string& name, const reply_callback_t& reply_callback = nullptr);
207 sentinel& set(const std::string& name, const std::string& option, const std::string& value, const reply_callback_t& reply_callback = nullptr);
208 sentinel& slaves(const std::string& name, const reply_callback_t& reply_callback = nullptr);
209
210 public:
215 class sentinel_def {
216 public:
218 sentinel_def(const std::string& host, std::size_t port, std::uint32_t timeout_msecs)
219 : m_host(host), m_port(port), m_timeout_msecs(timeout_msecs) {}
220
222 ~sentinel_def(void) = default;
223
224 public:
228 const std::string&
229 get_host(void) const { return m_host; }
230
234 size_t
235 get_port(void) const { return m_port; }
236
240 std::uint32_t
241 get_timeout_msecs(void) const { return m_timeout_msecs; }
242
247 void
248 set_timeout_msecs(std::uint32_t timeout_msecs) { m_timeout_msecs = timeout_msecs; }
249
250 private:
254 std::string m_host;
255
259 std::size_t m_port;
260
264 std::uint32_t m_timeout_msecs;
265 };
266
267 public:
271 const std::vector<sentinel_def>& get_sentinels(void) const;
272
276 std::vector<sentinel_def>& get_sentinels(void);
277
278 private:
285 void connection_receive_handler(network::redis_connection& connection, reply& reply);
286
292 void connection_disconnect_handler(network::redis_connection& connection);
293
297 void call_disconnect_handler(void);
298
302 void clear_callbacks(void);
303
308 void try_commit(void);
309
310 private:
314 std::vector<sentinel_def> m_sentinels;
315
319 network::redis_connection m_client;
320
324 std::queue<reply_callback_t> m_callbacks;
325
329 sentinel_disconnect_handler_t m_disconnect_handler;
330
334 std::mutex m_callbacks_mutex;
335
339 std::condition_variable m_sync_condvar;
340
344 std::atomic<unsigned int> m_callbacks_running;
345 };
346
347 }; // namespace cpp_redis
cpp_redis::sentinel::add_sentinel
sentinel & add_sentinel(const std::string &host, std::size_t port, std::uint32_t timeout_msecs=0)
cpp_redis::network::redis_connection
Definition: redis_connection.hpp:45
cpp_redis::sentinel::sentinel_def
Definition: sentinel.hpp:215
cpp_redis::sentinel::sentinel_def::get_port
size_t get_port(void) const
Definition: sentinel.hpp:235
cpp_redis::sentinel::sentinel_def::set_timeout_msecs
void set_timeout_msecs(std::uint32_t timeout_msecs)
Definition: sentinel.hpp:248
cpp_redis::sentinel::~sentinel
~sentinel(void)
dtor
cpp_redis::sentinel::get_sentinels
const std::vector< sentinel_def > & get_sentinels(void) const
cpp_redis::sentinel::operator=
sentinel & operator=(const sentinel &)=delete
assignment operator
cpp_redis::sentinel::sentinel_def::get_timeout_msecs
std::uint32_t get_timeout_msecs(void) const
Definition: sentinel.hpp:241
cpp_redis::reply
Definition: reply.hpp:37
cpp_redis::sentinel::connect
void connect(const std::string &host, std::size_t port, const sentinel_disconnect_handler_t &disconnect_handler=nullptr, std::uint32_t timeout_msecs=0)
cpp_redis::sentinel::sentinel_def::get_host
const std::string & get_host(void) const
Definition: sentinel.hpp:229
cpp_redis::sentinel::disconnect
void disconnect(bool wait_for_removal=false)
cpp_redis::sentinel::send
sentinel & send(const std::vector< std::string > &sentinel_cmd, const reply_callback_t &callback=nullptr)
cpp_redis::sentinel::reply_callback_t
std::function< void(reply &)> reply_callback_t
Definition: sentinel.hpp:67
cpp_redis::sentinel::commit
sentinel & commit(void)
cpp_redis::sentinel
Definition: sentinel.hpp:40
cpp_redis::sentinel::sentinel_def::~sentinel_def
~sentinel_def(void)=default
dtor
cpp_redis::sentinel::sync_commit
sentinel & sync_commit(const std::chrono::duration< Rep, Period > &timeout)
Definition: sentinel.hpp:104
cpp_redis::sentinel::sync_commit
sentinel & sync_commit(void)
cpp_redis::sentinel::sentinel_disconnect_handler_t
std::function< void(sentinel &)> sentinel_disconnect_handler_t
Definition: sentinel.hpp:153
cpp_redis::sentinel::get_master_addr_by_name
bool get_master_addr_by_name(const std::string &name, std::string &host, std::size_t &port, bool autoconnect=true)
cpp_redis::sentinel::clear_sentinels
void clear_sentinels(void)
cpp_redis::sentinel::connect_sentinel
void connect_sentinel(const sentinel_disconnect_handler_t &disconnect_handler=nullptr)
cpp_redis::sentinel::is_connected
bool is_connected(void)
cpp_redis::sentinel::sentinel_def::sentinel_def
sentinel_def(const std::string &host, std::size_t port, std::uint32_t timeout_msecs)
ctor
Definition: sentinel.hpp:218
cpp_redis::sentinel::sentinel
sentinel(void)
ctor & dtor
cpp_redis
Definition: array_builder.hpp:29