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:
127  sentinel& add_sentinel(const std::string& host, std::size_t port);
128 
132  void clear_sentinels(void);
133 
134 public:
140  void disconnect(bool wait_for_removal = false);
141 
145  bool is_connected(void);
146 
151  typedef std::function<void(sentinel&)> sentinel_disconnect_handler_t;
152 
159  void connect_sentinel(
160  std::uint32_t timeout_msecs = 0,
161  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 private:
211  class sentinel_def {
212  public:
214  sentinel_def(const std::string& host, std::size_t port)
215  : m_host(host), m_port(port) {}
216 
218  ~sentinel_def(void) = default;
219 
220  public:
224  const std::string&
225  get_host(void) const { return m_host; }
226 
230  size_t
231  get_port(void) const { return m_port; }
232 
233  private:
237  std::string m_host;
238 
242  std::size_t m_port;
243  };
244 
245 private:
252  void connection_receive_handler(network::redis_connection& connection, reply& reply);
253 
259  void connection_disconnect_handler(network::redis_connection& connection);
260 
264  void call_disconnect_handler(void);
265 
269  void clear_callbacks(void);
270 
275  void try_commit(void);
276 
277 private:
281  std::vector<sentinel_def> m_sentinels;
282 
286  network::redis_connection m_client;
287 
291  std::queue<reply_callback_t> m_callbacks;
292 
296  sentinel_disconnect_handler_t m_disconnect_handler;
297 
301  std::mutex m_callbacks_mutex;
302 
306  std::condition_variable m_sync_condvar;
307 
311  std::atomic<unsigned int> m_callbacks_running;
312 };
313 
314 }; // namespace cpp_redis
sentinel & add_sentinel(const std::string &host, std::size_t port)
Definition: redis_connection.hpp:45
~sentinel(void)
dtor
sentinel & operator=(const sentinel &)=delete
assignment operator
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)
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
void connect_sentinel(std::uint32_t timeout_msecs=0, const sentinel_disconnect_handler_t &disconnect_handler=nullptr)
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:151
bool get_master_addr_by_name(const std::string &name, std::string &host, std::size_t &port, bool autoconnect=true)
void clear_sentinels(void)
bool is_connected(void)
sentinel(void)
ctor & dtor
Definition: array_builder.hpp:29