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/logging.h>
28 #include <core/threading/thread.h>
29 #include <interfaces/MongoDBManagedReplicaSetInterface.h>
30 #include <mongo/bson/bson.h>
31 #include <mongo/client/dbclient.h>
32 
33 #include <memory>
34 #include <string>
35 #include <vector>
36 
37 namespace fawkes {
38 class Configuration;
39 class TimeWait;
40 } // namespace fawkes
41 
42 namespace mongo {
43 class DBClientBase;
44 }
45 
47  public fawkes::LoggingAspect,
48  public fawkes::ClockAspect,
50 {
51 public:
53  std::string cfgname,
54  std::string prefix,
55  std::string bootstrap_database);
56  void bootstrap(std::shared_ptr<mongo::DBClientBase> bootstrap_client);
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  bool leader_elect(bool force = false);
73  void leader_resign();
74 
75  struct ReplicaSetStatus
76  {
77  fawkes::MongoDBManagedReplicaSetInterface::ReplicaSetMemberStatus member_status;
78  fawkes::MongoDBManagedReplicaSetInterface::ReplicaSetPrimaryStatus primary_status;
79  std::string error_msg;
80 
81  bool
82  operator!=(const ReplicaSetStatus &other) const
83  {
84  return member_status != other.member_status || primary_status != other.primary_status
85  || error_msg != other.error_msg;
86  }
87  };
88 
89  ReplicaSetStatus rs_status(mongo::BSONObj &reply);
90  void rs_init();
91  void rs_monitor(const mongo::BSONObj &reply);
92  bool check_alive(const std::string &h);
93  bool rs_get_config(mongo::BSONObj &rs_config);
94 
95  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
96 protected:
97  virtual void
98  run()
99  {
100  Thread::run();
101  }
102 
103 private:
104  bool enabled_;
105 
106  std::string config_name_;
107 
108  std::shared_ptr<mongo::DBClientBase> bootstrap_client_;
109  mongo::BSONObj leader_elec_query_;
110  mongo::BSONObj leader_elec_query_force_;
111  mongo::BSONObj leader_elec_update_;
112  std::string bootstrap_database_;
113  std::string bootstrap_collection_;
114  std::string bootstrap_ns_;
115 
116  std::string local_client_cfg_;
117  std::shared_ptr<mongo::DBClientBase> local_client_;
118  std::string local_hostport_;
119  std::set<std::string> hosts_;
120 
121  bool is_leader_;
122  float loop_interval_;
123  int leader_expiration_;
124  fawkes::TimeWait *timewait_;
125 
126  ReplicaSetStatus last_status_;
127 
128  fawkes::MongoDBManagedReplicaSetInterface *rs_status_if_;
129 };
130 
131 #endif
Thread aspect to access to BlackBoard.
Definition: blackboard.h:33
Thread aspect that allows to obtain the current time from the clock.
Definition: clock.h:33
Fawkes library namespace.
Thread class encapsulation of pthreads.
Definition: thread.h:45
MongoDBReplicaSetConfig(fawkes::Configuration *config, std::string cfgname, std::string prefix, std::string bootstrap_database)
Constructor.
bool is_enabled() const
Check if configuration is enabled.
virtual void loop()
Code to execute in the thread.
MongoDB replica set configuration.
Thread aspect to log output.
Definition: logging.h:32
void bootstrap(std::shared_ptr< mongo::DBClientBase > bootstrap_client)
Setup replicaset bootstrap client.
virtual void run()
Stub to see name in backtrace for easier debugging.
virtual void init()
Initialize the thread.
Interface for configuration handling.
Definition: config.h:64
Time wait utility.
Definition: wait.h:32
virtual void finalize()
Finalize the thread.