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 
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
sentinel & add_sentinel(const std::string &host, std::size_t port, std::uint32_t timeout_msecs=0)
Definition: redis_connection.hpp:45
Definition: sentinel.hpp:215
size_t get_port(void) const
Definition: sentinel.hpp:235
void set_timeout_msecs(std::uint32_t timeout_msecs)
Definition: sentinel.hpp:248
~sentinel(void)
dtor
const std::vector< sentinel_def > & get_sentinels(void) const
sentinel & operator=(const sentinel &)=delete
assignment operator
std::uint32_t get_timeout_msecs(void) const
Definition: sentinel.hpp:241
Definition: reply.hpp:37
void connect(const std::string &host, std::size_t port, const sentinel_disconnect_handler_t &disconnect_handler=nullptr, std::uint32_t timeout_msecs=0)
const std::string & get_host(void) const
Definition: sentinel.hpp:229
void disconnect(bool wait_for_removal=false)
sentinel & send(const std::vector< std::string > &sentinel_cmd, const reply_callback_t &callback=nullptr)
std::function< void(reply &)> reply_callback_t
Definition: sentinel.hpp:67
sentinel & commit(void)
Definition: sentinel.hpp:40
~sentinel_def(void)=default
dtor
sentinel & sync_commit(const std::chrono::duration< Rep, Period > &timeout)
Definition: sentinel.hpp:104
sentinel & sync_commit(void)
std::function< void(sentinel &)> sentinel_disconnect_handler_t
Definition: sentinel.hpp:153
bool get_master_addr_by_name(const std::string &name, std::string &host, std::size_t &port, bool autoconnect=true)
void clear_sentinels(void)
void connect_sentinel(const sentinel_disconnect_handler_t &disconnect_handler=nullptr)
bool is_connected(void)
sentinel_def(const std::string &host, std::size_t port, std::uint32_t timeout_msecs)
ctor
Definition: sentinel.hpp:218
sentinel(void)
ctor & dtor
Definition: array_builder.hpp:29