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 <queue>
26 #include <vector>
27 
28 #include <cpp_redis/misc/logger.hpp>
29 #include <cpp_redis/network/redis_connection.hpp>
30 #include <tacopie/network/io_service.hpp>
31 
32 namespace cpp_redis {
33 
39 class sentinel {
40 public:
42 #ifndef __CPP_REDIS_USE_CUSTOM_TCP_CLIENT
43  sentinel(void);
45 #endif /* __CPP_REDIS_USE_CUSTOM_TCP_CLIENT */
46 
52  explicit sentinel(const std::shared_ptr<network::tcp_client_iface>& tcp_client);
53 
55  ~sentinel(void);
56 
58  sentinel(const sentinel&) = delete;
60  sentinel& operator=(const sentinel&) = delete;
61 
62 public:
66  typedef std::function<void(reply&)> reply_callback_t;
67 
77  sentinel& send(const std::vector<std::string>& sentinel_cmd, const reply_callback_t& callback = nullptr);
78 
85  sentinel& commit(void);
86 
93  sentinel& sync_commit(void);
94 
101  template <class Rep, class Period>
102  sentinel&
103  sync_commit(const std::chrono::duration<Rep, Period>& timeout) {
104  try_commit();
105 
106  std::unique_lock<std::mutex> lock_callback(m_callbacks_mutex);
107  __CPP_REDIS_LOG(debug, "cpp_redis::sentinel waiting for callbacks to complete");
108  if (!m_sync_condvar.wait_for(lock_callback, timeout, [=] {
109  return m_callbacks_running == 0 && m_callbacks.empty();
110  })) {
111  __CPP_REDIS_LOG(debug, "cpp_redis::sentinel finished waiting for callback");
112  }
113  else {
114  __CPP_REDIS_LOG(debug, "cpp_redis::sentinel timed out waiting for callback");
115  }
116  return *this;
117  }
118 
119 public:
128  sentinel& add_sentinel(const std::string& host, std::size_t port, std::uint32_t timeout_msecs = 0);
129 
133  void clear_sentinels(void);
134 
135 public:
141  void disconnect(bool wait_for_removal = false);
142 
146  bool is_connected(void);
147 
152  typedef std::function<void(sentinel&)> sentinel_disconnect_handler_t;
153 
160  void connect_sentinel(const sentinel_disconnect_handler_t& disconnect_handler = nullptr);
161 
170  void connect(
171  const std::string& host,
172  std::size_t port,
173  const sentinel_disconnect_handler_t& disconnect_handler = nullptr,
174  std::uint32_t timeout_msecs = 0);
175 
190  const std::string& name,
191  std::string& host,
192  std::size_t& port,
193  bool autoconnect = true);
194 
195 public:
196  sentinel& ckquorum(const std::string& name, const reply_callback_t& reply_callback = nullptr);
197  sentinel& failover(const std::string& name, const reply_callback_t& reply_callback = nullptr);
198  sentinel& flushconfig(const reply_callback_t& reply_callback = nullptr);
199  sentinel& master(const std::string& name, const reply_callback_t& reply_callback = nullptr);
200  sentinel& masters(const reply_callback_t& reply_callback = nullptr);
201  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);
202  sentinel& ping(const reply_callback_t& reply_callback = nullptr);
203  sentinel& remove(const std::string& name, const reply_callback_t& reply_callback = nullptr);
204  sentinel& reset(const std::string& pattern, const reply_callback_t& reply_callback = nullptr);
205  sentinel& sentinels(const std::string& name, const reply_callback_t& reply_callback = nullptr);
206  sentinel& set(const std::string& name, const std::string& option, const std::string& value, const reply_callback_t& reply_callback = nullptr);
207  sentinel& slaves(const std::string& name, const reply_callback_t& reply_callback = nullptr);
208 
209 public:
214  class sentinel_def {
215  public:
217  sentinel_def(const std::string& host, std::size_t port, std::uint32_t timeout_msecs)
218  : m_host(host), m_port(port), m_timeout_msecs(timeout_msecs) {}
219 
221  ~sentinel_def(void) = default;
222 
223  public:
227  const std::string&
228  get_host(void) const { return m_host; }
229 
233  size_t
234  get_port(void) const { return m_port; }
235 
239  std::uint32_t
240  get_timeout_msecs(void) const { return m_timeout_msecs; }
241 
246  void
247  set_timeout_msecs(std::uint32_t timeout_msecs) { m_timeout_msecs = timeout_msecs; }
248 
249  private:
253  std::string m_host;
254 
258  std::size_t m_port;
259 
263  std::uint32_t m_timeout_msecs;
264  };
265 
266 public:
270  const std::vector<sentinel_def>& get_sentinels(void) const;
271 
275  std::vector<sentinel_def>& get_sentinels(void);
276 
277 private:
284  void connection_receive_handler(network::redis_connection& connection, reply& reply);
285 
291  void connection_disconnect_handler(network::redis_connection& connection);
292 
296  void call_disconnect_handler(void);
297 
301  void clear_callbacks(void);
302 
307  void try_commit(void);
308 
309 private:
313  std::vector<sentinel_def> m_sentinels;
314 
318  network::redis_connection m_client;
319 
323  std::queue<reply_callback_t> m_callbacks;
324 
328  sentinel_disconnect_handler_t m_disconnect_handler;
329 
333  std::mutex m_callbacks_mutex;
334 
338  std::condition_variable m_sync_condvar;
339 
343  std::atomic<unsigned int> m_callbacks_running;
344 };
345 
346 }; // 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:214
size_t get_port(void) const
Definition: sentinel.hpp:234
void set_timeout_msecs(std::uint32_t timeout_msecs)
Definition: sentinel.hpp:247
~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:240
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:228
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:66
sentinel & commit(void)
Definition: sentinel.hpp:39
~sentinel_def(void)=default
dtor
sentinel & sync_commit(const std::chrono::duration< Rep, Period > &timeout)
Definition: sentinel.hpp:103
sentinel & sync_commit(void)
std::function< void(sentinel &)> sentinel_disconnect_handler_t
Definition: sentinel.hpp:152
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:217
sentinel(void)
ctor & dtor
Definition: array_builder.hpp:29