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