22 #include "mongodb_client_config.h"
24 #include <config/config.h>
25 #include <logging/logger.h>
52 logcomp_ =
"MongoDBClient|" + cfgname;
56 enabled_ = config->
get_bool((prefix +
"enabled").c_str());
60 std::string mode =
"connection";
62 mode = config->
get_string((prefix +
"mode").c_str());
65 "MongoDB config '%s' specifies no client "
66 "mode, assuming 'connection'.",
70 startup_grace_period_ = 10;
72 startup_grace_period_ = config->
get_uint(prefix +
"startup_grace_period");
75 "MongoDB config '%s' specifies no startup grace period "
76 ", assuming 10 seconds.",
81 read_authinfo(config, logger, cfgname, prefix);
83 if (mode ==
"replica_set" || mode ==
"replicaset") {
85 replicaset_name_ = config->
get_string((prefix +
"name").c_str());
87 std::vector<std::string> hosts = config->
get_strings(prefix +
"hosts");
88 std::string uri{
"mongodb://"};
89 for (
auto it = hosts.begin(); it != hosts.end(); it++) {
91 if (std::next(it) != hosts.end()) {
95 uri +=
"/" + auth_dbname;
96 uri +=
"?replicaSet=" + replicaset_name_;
98 uri +=
"&readPreference=" + config->
get_string((prefix +
"read-preference").c_str());
103 uri +=
"&readPreferenceTags=" + config->
get_string((prefix +
"read-preference-tags").c_str());
106 conn_uri_ = mongocxx::uri{uri};
108 }
else if (mode ==
"sync_cluster" || mode ==
"synccluster") {
109 throw Exception(
"sync_cluster connections are no longer supported");
113 conn_uri_ = mongocxx::uri{
"mongodb://" + auth_string_ + config->
get_string(prefix +
"hostport")
114 +
"/" + auth_dbname};
133 auth_dbname = config->
get_string((prefix +
"auth_dbname").c_str());
134 std::string username = config->
get_string((prefix +
"auth_username").c_str());
135 std::string password = config->
get_string((prefix +
"auth_password").c_str());
136 if (!username.empty() && !password.empty()) {
137 auth_string_ = username +
":" + password +
"@";
141 "No default authentication info for "
142 "MongoDB client '%s'",
153 for (
unsigned int startup_tries = 0; startup_tries < startup_grace_period_ * 2; ++startup_tries) {
155 return new mongocxx::client(conn_uri_);
157 using namespace std::chrono_literals;
158 std::this_thread::sleep_for(500ms);
161 throw Exception(
"Failed to create client with uri: '%s'", conn_uri_.to_string().c_str());
174 logger->
log_info(component,
"%smode: replica set", indent);
177 logger->
log_info(component,
"%smode: connection", indent);
180 std::string uri_string = conn_uri_.to_string();
181 if (!auth_string_.empty()) {
182 if (uri_string.find(auth_string_) != std::string::npos) {
183 uri_string.replace(uri_string.find(auth_string_), auth_string_.length(),
"****@****");
186 logger->
log_info(component,
"%suri: %s", indent, uri_string.c_str());
195 auto hosts = conn_uri_.hosts();
200 return hosts[0].name +
":" + std::to_string(hosts[0].port);
mongocxx::client * create_client()
Create MongoDB client for this configuration.
ConnectionMode
Connection mode enumeration.
MongoDBClientConfig(fawkes::Configuration *config, fawkes::Logger *logger, std::string cfgname, std::string prefix)
Constructor.
void log(fawkes::Logger *logger, const char *component, const char *indent)
Write client configuration information to log.
std::string hostport() const
Get host and port of configuration.
ConnectionMode mode() const
Get client configuration mode.
Interface for configuration handling.
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
virtual std::vector< std::string > get_strings(const char *path)=0
Get list of values from configuration which is of type string.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
Base class for exceptions in Fawkes.
virtual void log_info(const char *component, const char *format,...)
Log informational message.
Fawkes library namespace.