userver: userver/storages/mongo/multi_mongo.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
multi_mongo.hpp
1#pragma once
2
3#include <memory>
4#include <string>
5#include <unordered_map>
6
7#include <userver/dynamic_config/source.hpp>
8#include <userver/rcu/rcu.hpp>
9#include <userver/storages/secdist/fwd.hpp>
10#include <userver/utils/statistics/fwd.hpp>
11
12#include <userver/storages/mongo/pool.hpp>
13#include <userver/storages/mongo/pool_config.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace storages::mongo {
18
20 using PoolMap = std::unordered_map<std::string, storages::mongo::PoolPtr>;
21
22public:
23 /// Database set builder
24 class PoolSet {
25 public:
26 explicit PoolSet(MultiMongo&);
27
28 PoolSet(const PoolSet&);
29 PoolSet(PoolSet&&) noexcept;
30 PoolSet& operator=(const PoolSet&);
31 PoolSet& operator=(PoolSet&&) noexcept;
32
33 /// Adds all currently enabled databases to the set
35
36 /// @brief Adds a database to the set by its name
37 /// @param dbalias name of the database in secdist config
38 void AddPool(std::string dbalias);
39
40 /// @brief Removes the database with the specified name from the set
41 /// @param dbalias name of the database passed to AddPool
42 /// @returns whether the database was in the set
43 bool RemovePool(const std::string& dbalias);
44
45 /// @brief Replaces the working database set
46 void Activate();
47
48 private:
49 MultiMongo* target_{nullptr};
50 std::shared_ptr<PoolMap> pool_map_ptr_;
51 };
52
53 /// @cond
54 MultiMongo(
55 std::string name,
56 storages::secdist::Secdist& secdist,
57 storages::mongo::PoolConfig pool_config,
58 clients::dns::Resolver* dns_resolver,
59 dynamic_config::Source config_source
60 );
61 /// @endcond
62
63 /// @brief Client pool accessor
64 /// @param dbalias name previously passed to `AddPool`
65 /// @throws PoolNotFoundException if no such database is enabled
66 storages::mongo::PoolPtr GetPool(const std::string& dbalias) const;
67
68 /// @brief Adds a database to the working set by its name.
69 /// Equivalent to
70 /// `NewPoolSet()`-`AddExistingPools()`-`AddPool(dbalias)`-`Activate()`
71 /// @param dbalias name of the database in secdist config
72 void AddPool(std::string dbalias);
73
74 /// @brief Removes the database with the specified name from the working set.
75 /// Equivalent to
76 /// `NewPoolSet()`-`AddExistingPools()`-`RemovePool(dbalias)`-`Activate()`
77 /// @param dbalias name of the database passed to AddPool
78 /// @returns whether the database was in the working set
79 bool RemovePool(const std::string& dbalias);
80
81 /// Creates an empty database set bound to the current MultiMongo instance
83
84 /// Writes statistics
85 friend void DumpMetric(utils::statistics::Writer& writer, const MultiMongo& multi_mongo);
86
87 const std::string& GetName() const { return name_; }
88
89private:
90 void OnConfigUpdate(const dynamic_config::Snapshot& config);
91
92 void OnSecdistUpdate(const storages::secdist::SecdistConfig& secdist);
93
94 storages::mongo::PoolPtr FindPool(const std::string& dbalias) const;
95
96 const std::string name_;
97 const storages::secdist::Secdist& secdist_;
98 dynamic_config::Source config_source_;
99 const storages::mongo::PoolConfig pool_config_;
100 clients::dns::Resolver* dns_resolver_;
101 rcu::Variable<PoolMap> pool_map_;
102
103 // Subscriptions (config_subscriber_ and secdist_subscriber_) must be the last fields.
104 concurrent::AsyncEventSubscriberScope config_subscriber_;
105 concurrent::AsyncEventSubscriberScope secdist_subscriber_;
106};
107
108} // namespace storages::mongo
109
110USERVER_NAMESPACE_END