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
Go to the documentation of this file.
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 
30 #include <tacopie/network/io_service.hpp>
31 
32 namespace cpp_redis {
33 
34 class sentinel {
35 public:
37 #ifndef __CPP_REDIS_USE_CUSTOM_TCP_CLIENT
38  sentinel(void);
40 #endif /* __CPP_REDIS_USE_CUSTOM_TCP_CLIENT */
41 
47  explicit sentinel(const std::shared_ptr<network::tcp_client_iface>& tcp_client);
48 
50  ~sentinel(void);
51 
53  sentinel(const sentinel&) = delete;
55  sentinel& operator=(const sentinel&) = delete;
56 
57 public:
61  typedef std::function<void(reply&)> reply_callback_t;
62 
72  sentinel& send(const std::vector<std::string>& sentinel_cmd, const reply_callback_t& callback = nullptr);
73 
80  sentinel& commit(void);
81 
88  sentinel& sync_commit(void);
89 
96  template <class Rep, class Period>
97  sentinel&
98  sync_commit(const std::chrono::duration<Rep, Period>& timeout) {
99  try_commit();
100 
101  std::unique_lock<std::mutex> lock_callback(m_callbacks_mutex);
102  __CPP_REDIS_LOG(debug, "cpp_redis::sentinel waiting for callbacks to complete");
103  if (!m_sync_condvar.wait_for(lock_callback, timeout, [=] {
104  return m_callbacks_running == 0 && m_callbacks.empty();
105  })) {
106  __CPP_REDIS_LOG(debug, "cpp_redis::sentinel finished waiting for callback");
107  }
108  else {
109  __CPP_REDIS_LOG(debug, "cpp_redis::sentinel timed out waiting for callback");
110  }
111  return *this;
112  }
113 
114 public:
122  sentinel& add_sentinel(const std::string& host, std::size_t port);
123 
127  void clear_sentinels(void);
128 
129 public:
135  void disconnect(bool wait_for_removal = false);
136 
140  bool is_connected(void);
141 
146  typedef std::function<void(sentinel&)> sentinel_disconnect_handler_t;
147 
154  void connect_sentinel(
155  std::uint32_t timeout_msecs = 0,
156  const sentinel_disconnect_handler_t& disconnect_handler = nullptr);
157 
166  void connect(
167  const std::string& host,
168  std::size_t port,
169  const sentinel_disconnect_handler_t& disconnect_handler = nullptr,
170  std::uint32_t timeout_msecs = 0);
171 
186  const std::string& name,
187  std::string& host,
188  std::size_t& port,
189  bool autoconnect = true);
190 
191 public:
192  sentinel& ckquorum(const std::string& name, const reply_callback_t& reply_callback = nullptr);
193  sentinel& failover(const std::string& name, const reply_callback_t& reply_callback = nullptr);
194  sentinel& flushconfig(const reply_callback_t& reply_callback = nullptr);
195  sentinel& master(const std::string& name, const reply_callback_t& reply_callback = nullptr);
196  sentinel& masters(const reply_callback_t& reply_callback = nullptr);
197  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);
198  sentinel& ping(const reply_callback_t& reply_callback = nullptr);
199  sentinel& remove(const std::string& name, const reply_callback_t& reply_callback = nullptr);
200  sentinel& reset(const std::string& pattern, const reply_callback_t& reply_callback = nullptr);
201  sentinel& sentinels(const std::string& name, const reply_callback_t& reply_callback = nullptr);
202  sentinel& set(const std::string& name, const std::string& option, const std::string& value, const reply_callback_t& reply_callback = nullptr);
203  sentinel& slaves(const std::string& name, const reply_callback_t& reply_callback = nullptr);
204 
205 private:
206  class sentinel_def {
207  public:
209  sentinel_def(const std::string& host, std::size_t port)
210  : m_host(host), m_port(port) {}
211 
213  ~sentinel_def(void) = default;
214 
215  public:
219  const std::string&
220  get_host(void) const { return m_host; }
221 
225  size_t
226  get_port(void) const { return m_port; }
227 
228  private:
232  std::string m_host;
233 
237  std::size_t m_port;
238  };
239 
240 private:
248 
255 
259  void call_disconnect_handler(void);
260 
264  void clear_callbacks(void);
265 
270  void try_commit(void);
271 
272 private:
276  std::vector<sentinel_def> m_sentinels;
277 
282 
286  std::queue<reply_callback_t> m_callbacks;
287 
291  sentinel_disconnect_handler_t m_disconnect_handler;
292 
296  std::mutex m_callbacks_mutex;
297 
301  std::condition_variable m_sync_condvar;
302 
306  std::atomic<unsigned int> m_callbacks_running = ATOMIC_VAR_INIT(0);
307 };
308 
309 }; // namespace cpp_redis
sentinel & add_sentinel(const std::string &host, std::size_t port)
Definition: redis_connection.hpp:45
std::queue< reply_callback_t > m_callbacks
Definition: sentinel.hpp:286
Definition: sentinel.hpp:206
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)
sentinel & reset(const std::string &pattern, const reply_callback_t &reply_callback=nullptr)
sentinel_def(const std::string &host, std::size_t port)
ctor
Definition: sentinel.hpp:209
size_t get_port(void) const
Definition: sentinel.hpp:226
~sentinel(void)
dtor
sentinel & ping(const reply_callback_t &reply_callback=nullptr)
sentinel & operator=(const sentinel &)=delete
assignment operator
network::redis_connection m_client
Definition: sentinel.hpp:281
sentinel & ckquorum(const std::string &name, const reply_callback_t &reply_callback=nullptr)
Definition: reply.hpp:33
void clear_callbacks(void)
void connect(const std::string &host, std::size_t port, const sentinel_disconnect_handler_t &disconnect_handler=nullptr, std::uint32_t timeout_msecs=0)
sentinel & flushconfig(const reply_callback_t &reply_callback=nullptr)
const std::string & get_host(void) const
Definition: sentinel.hpp:220
std::atomic< unsigned int > m_callbacks_running
Definition: sentinel.hpp:306
sentinel_disconnect_handler_t m_disconnect_handler
Definition: sentinel.hpp:291
std::vector< sentinel_def > m_sentinels
Definition: sentinel.hpp:276
sentinel & masters(const reply_callback_t &reply_callback=nullptr)
void disconnect(bool wait_for_removal=false)
std::condition_variable m_sync_condvar
Definition: sentinel.hpp:301
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:61
sentinel & commit(void)
Definition: sentinel.hpp:34
std::size_t m_port
Definition: sentinel.hpp:237
void try_commit(void)
sentinel & slaves(const std::string &name, const reply_callback_t &reply_callback=nullptr)
void call_disconnect_handler(void)
void connection_disconnect_handler(network::redis_connection &connection)
void connect_sentinel(std::uint32_t timeout_msecs=0, const sentinel_disconnect_handler_t &disconnect_handler=nullptr)
std::mutex m_callbacks_mutex
Definition: sentinel.hpp:296
~sentinel_def(void)=default
dtor
#define __CPP_REDIS_LOG(level, msg)
Definition: logger.hpp:212
sentinel & sync_commit(const std::chrono::duration< Rep, Period > &timeout)
Definition: sentinel.hpp:98
sentinel & sync_commit(void)
sentinel & sentinels(const std::string &name, const reply_callback_t &reply_callback=nullptr)
std::function< void(sentinel &)> sentinel_disconnect_handler_t
Definition: sentinel.hpp:146
bool get_master_addr_by_name(const std::string &name, std::string &host, std::size_t &port, bool autoconnect=true)
std::string m_host
Definition: sentinel.hpp:232
void debug(const std::string &msg, const std::string &file, std::size_t line)
void clear_sentinels(void)
bool is_connected(void)
sentinel & failover(const std::string &name, const reply_callback_t &reply_callback=nullptr)
sentinel(void)
ctor & dtor
void connection_receive_handler(network::redis_connection &connection, reply &reply)
Definition: array_builder.hpp:29
sentinel & master(const std::string &name, const reply_callback_t &reply_callback=nullptr)