Fawkes API  Fawkes Development Version
mongodb_replicaset_config.h
1 
2 /***************************************************************************
3  * mongodb_replicaset_config.h - MongoDB replica set configuration
4  *
5  * Created: Thu Jul 13 10:23:33 2017
6  * Copyright 2006-2017 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #ifndef _PLUGINS_MONGODB_MONGODB_REPLICASET_CONFIG_H_
23 #define _PLUGINS_MONGODB_MONGODB_REPLICASET_CONFIG_H_
24 
25 #include <aspect/blackboard.h>
26 #include <aspect/clock.h>
27 #include <aspect/configurable.h>
28 #include <aspect/logging.h>
29 #include <core/threading/thread.h>
30 #include <interfaces/MongoDBManagedReplicaSetInterface.h>
31 
32 #include <bsoncxx/json.hpp>
33 #include <memory>
34 #include <mongocxx/client.hpp>
35 #include <set>
36 #include <string>
37 #include <vector>
38 
39 namespace fawkes {
40 class TimeWait;
41 } // namespace fawkes
42 
43 namespace mongo {
44 class DBClientBase;
45 }
46 
48  public fawkes::LoggingAspect,
50  public fawkes::ClockAspect,
52 {
53 public:
54  MongoDBReplicaSetConfig(const std::string &cfgname,
55  const std::string &prefix,
56  const std::string &bootstrap_prefix);
57 
58  /** Check if configuration is enabled.
59  * @return true if configuration is enabled, false otherwise
60  */
61  bool
62  is_enabled() const
63  {
64  return enabled_;
65  }
66 
67  virtual void init();
68  virtual void loop();
69  virtual void finalize();
70 
71 private:
72  void bootstrap();
73  bool leader_elect(bool force = false);
74  void leader_resign();
75 
76  struct ReplicaSetStatus
77  {
78  fawkes::MongoDBManagedReplicaSetInterface::ReplicaSetMemberStatus member_status;
79  fawkes::MongoDBManagedReplicaSetInterface::ReplicaSetPrimaryStatus primary_status;
80  std::string error_msg;
81 
82  bool
83  operator!=(const ReplicaSetStatus &other) const
84  {
85  return member_status != other.member_status || primary_status != other.primary_status
86  || error_msg != other.error_msg;
87  }
88  };
89 
90  // TODO: update signature
91  ReplicaSetStatus rs_status(bsoncxx::document::value &reply);
92  void rs_init();
93  void rs_monitor(const bsoncxx::document::view &reply);
94  bool check_alive(const std::string &h);
95  bool rs_get_config(bsoncxx::document::value &rs_config);
96 
97  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
98 protected:
99  virtual void
100  run()
101  {
102  Thread::run();
103  }
104 
105 private:
106  bool enabled_;
107 
108  std::string config_name_;
109 
110  std::shared_ptr<mongocxx::client> bootstrap_client_;
111  bsoncxx::document::value leader_elec_query_;
112  bsoncxx::document::value leader_elec_query_force_;
113  bsoncxx::document::value leader_elec_update_;
114  std::string prefix_;
115  std::string bootstrap_prefix_;
116  std::string bootstrap_database_;
117  std::string bootstrap_ns_;
118 
119  std::string local_client_cfg_;
120  std::shared_ptr<mongocxx::client> local_client_;
121  std::string local_hostport_;
122  std::set<std::string> hosts_;
123 
124  bool is_leader_;
125  float loop_interval_;
126  int leader_expiration_;
127  fawkes::TimeWait *timewait_;
128 
129  ReplicaSetStatus last_status_;
130 
131  fawkes::MongoDBManagedReplicaSetInterface *rs_status_if_;
132 };
133 
134 #endif
MongoDB replica set configuration.
virtual void init()
Initialize the thread.
MongoDBReplicaSetConfig(const std::string &cfgname, const std::string &prefix, const std::string &bootstrap_prefix)
Constructor.
virtual void loop()
Code to execute in the thread.
bool is_enabled() const
Check if configuration is enabled.
virtual void finalize()
Finalize the thread.
virtual void run()
Stub to see name in backtrace for easier debugging.
Thread aspect to access to BlackBoard.
Definition: blackboard.h:34
Thread aspect that allows to obtain the current time from the clock.
Definition: clock.h:34
Thread aspect to access configuration data.
Definition: configurable.h:33
Thread aspect to log output.
Definition: logging.h:33
Thread class encapsulation of pthreads.
Definition: thread.h:46
Time wait utility.
Definition: wait.h:33
Fawkes library namespace.