userver: userver/cache/base_mongo_cache.hpp Source File
Loading...
Searching...
No Matches
base_mongo_cache.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/cache/base_mongo_cache.hpp
4/// @brief @copybrief components::MongoCache
5
6#include <chrono>
7
8#include <fmt/format.h>
9
10#include <userver/cache/cache_statistics.hpp>
11#include <userver/cache/caching_component_base.hpp>
12#include <userver/cache/mongo_cache_type_traits.hpp>
13#include <userver/components/component_context.hpp>
14#include <userver/formats/bson/document.hpp>
15#include <userver/formats/bson/inline.hpp>
16#include <userver/formats/bson/value_builder.hpp>
17#include <userver/storages/mongo/collection.hpp>
18#include <userver/storages/mongo/operations.hpp>
19#include <userver/storages/mongo/options.hpp>
20#include <userver/tracing/span.hpp>
21#include <userver/utils/cpu_relax.hpp>
22#include <userver/yaml_config/merge_schemas.hpp>
23
24USERVER_NAMESPACE_BEGIN
25
26namespace components {
27
28inline const std::string kFetchAndParseStage = "fetch_and_parse";
29
30inline constexpr std::chrono::milliseconds kCpuRelaxThreshold{10};
31inline constexpr std::chrono::milliseconds kCpuRelaxInterval{2};
32
33namespace impl {
34
35std::chrono::milliseconds GetMongoCacheUpdateCorrection(const ComponentConfig&);
36
37}
38
39/// @ingroup userver_components
40///
41/// @brief %Base class for all caches polling mongo collection
42///
43/// You have to provide a traits class in order to use this.
44///
45/// For avoiding "memory leaks", see the respective section
46/// in @ref components::CachingComponentBase.
47///
48/// ## Static options of components::MongoCache :
49///
50/// @include{doc} scripts/docs/en/components_schema/mongo/src/cache/base_mongo_cache.md
51///
52/// Options inherited from @ref components::CachingComponentBase :
53/// @include{doc} scripts/docs/en/components_schema/core/src/cache/caching_component_base.md
54///
55/// Options inherited from @ref components::ComponentBase :
56/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
57///
58/// ## Traits example:
59/// All fields below (except for function overrides) are mandatory.
60///
61/// ```
62/// struct MongoCacheTraitsExample {
63/// // Component name for component
64/// static constexpr std::string_view kName = "mongo-dynamic-config";
65///
66/// // Collection to read from
67/// static constexpr auto kMongoCollectionsField =
68/// &storages::mongo::Collections::config;
69/// // Update field name to use for incremental update (optional).
70/// // When missing, incremental update is disabled.
71/// // Please use reference here to avoid global variables
72/// // initialization order issues.
73/// static constexpr const std::string& kMongoUpdateFieldName =
74/// mongo::db::taxi::config::kUpdated;
75///
76/// // Cache element type
77/// using ObjectType = CachedObject;
78/// // Cache element field name that is used as an index in the cache map
79/// static constexpr auto kKeyField = &CachedObject::name;
80/// // Type of kKeyField
81/// using KeyType = std::string;
82/// // Type of cache map, e.g. unordered_map, map, bimap
83/// using DataType = std::unordered_map<KeyType, ObjectType>;
84///
85/// // Whether the cache prefers to read from replica (if true, you might get stale data)
86/// static constexpr bool kIsSecondaryPreferred = true;
87///
88/// // Optional function that overrides BSON to ObjectType conversion
89/// static constexpr auto DeserializeObject = &CachedObject::FromBson;
90/// // or
91/// static ObjectType DeserializeObject(const formats::bson::Document& doc) {
92/// return doc["value"].As<ObjectType>();
93/// }
94/// // (default implementation calls doc.As<ObjectType>())
95/// // For using default implementation
96/// static constexpr bool kUseDefaultDeserializeObject = true;
97///
98/// // Optional function that overrides data retrieval operation
99/// static storages::mongo::operations::Find GetFindOperation(
100/// cache::UpdateType type,
101/// const std::chrono::system_clock::time_point& last_update,
102/// const std::chrono::system_clock::time_point& now,
103/// const std::chrono::system_clock::duration& correction) {
104/// mongo::operations::Find find_op({});
105/// find_op.SetOption(mongo::options::Projection{"key", "value"});
106/// return find_op;
107/// }
108/// // (default implementation queries kMongoUpdateFieldName: {$gt: last_update}
109/// // for incremental updates, and {} for full updates)
110/// // For using default implementation
111/// static constexpr bool kUseDefaultFindOperation = true;
112///
113/// // Whether update part of the cache even if failed to parse some documents
114/// static constexpr bool kAreInvalidDocumentsSkipped = false;
115///
116/// // Component to get the collections
117/// using MongoCollectionsComponent = components::MongoCollections;
118/// };
119/// ```
120template <class MongoCacheTraits>
121class MongoCache : public CachingComponentBase<typename MongoCacheTraits::DataType> {
122 using CollectionsType = mongo_cache::impl::CollectionsType<decltype(MongoCacheTraits::kMongoCollectionsField)>;
123
124public:
125 static constexpr std::string_view kName = MongoCacheTraits::kName;
126
127 MongoCache(const ComponentConfig&, const ComponentContext&);
128
129 ~MongoCache();
130
131 static yaml_config::Schema GetStaticConfigSchema();
132
133private:
134 void Update(
135 cache::UpdateType type,
136 const std::chrono::system_clock::time_point& last_update,
137 const std::chrono::system_clock::time_point& now,
138 cache::UpdateStatisticsScope& stats_scope
139 ) override;
140
141 typename MongoCacheTraits::ObjectType DeserializeObject(const formats::bson::Document& doc) const;
142
143 storages::mongo::operations::Find GetFindOperation(
144 cache::UpdateType type,
145 const std::chrono::system_clock::time_point& last_update,
146 const std::chrono::system_clock::time_point& now,
147 const std::chrono::system_clock::duration& correction
148 );
149
150 std::unique_ptr<typename MongoCacheTraits::DataType> GetData(cache::UpdateType type);
151
152 const std::shared_ptr<CollectionsType> mongo_collections_;
153 const storages::mongo::Collection* const mongo_collection_;
154 const std::chrono::system_clock::duration correction_;
155 std::size_t cpu_relax_iterations_{0};
156};
157
158template <class MongoCacheTraits>
159inline constexpr bool kHasValidate<MongoCache<MongoCacheTraits>> = true;
160
161template <class MongoCacheTraits>
162MongoCache<MongoCacheTraits>::MongoCache(const ComponentConfig& config, const ComponentContext& context)
163 : CachingComponentBase<typename MongoCacheTraits::DataType>(config, context),
164 mongo_collections_(context.FindComponent<typename MongoCacheTraits::MongoCollectionsComponent>()
165 .template GetCollectionForLibrary<CollectionsType>()),
166 mongo_collection_(std::addressof(mongo_collections_.get()->*MongoCacheTraits::kMongoCollectionsField)),
167 correction_(impl::GetMongoCacheUpdateCorrection(config))
168{
169 [[maybe_unused]] mongo_cache::impl::CheckTraits<MongoCacheTraits> check_traits;
170
171 if (CachingComponentBase<typename MongoCacheTraits::DataType>::GetAllowedUpdateTypes() ==
172 cache::AllowedUpdateTypes::kFullAndIncremental &&
173 !mongo_cache::impl::kHasUpdateFieldName<MongoCacheTraits> &&
174 !mongo_cache::impl::kHasFindOperation<MongoCacheTraits>)
175 {
176 throw std::logic_error(fmt::format(
177 "Incremental update support is requested in config but no update field "
178 "name is specified in traits of '{}' cache",
180 ));
181 }
182 if (correction_.count() < 0) {
183 throw std::logic_error(fmt::format(
184 "Refusing to set forward (negative) update correction requested in "
185 "config for '{}' cache",
187 ));
188 }
189
190 this->StartPeriodicUpdates();
191}
192
193template <class MongoCacheTraits>
194MongoCache<MongoCacheTraits>::~MongoCache() {
195 this->StopPeriodicUpdates();
196}
197
198template <class MongoCacheTraits>
199void MongoCache<MongoCacheTraits>::Update(
200 cache::UpdateType type,
201 const std::chrono::system_clock::time_point& last_update,
202 const std::chrono::system_clock::time_point& now,
203 cache::UpdateStatisticsScope& stats_scope
204) {
205 namespace sm = storages::mongo;
206
207 const auto* collection = mongo_collection_;
208 auto find_op = GetFindOperation(type, last_update, now, correction_);
209 auto cursor = collection->Execute(find_op);
210 if (type == cache::UpdateType::kIncremental && !cursor) {
211 // Don't touch the cache at all
212 LOG_INFO() << "No changes in cache " << MongoCacheTraits::kName;
213 stats_scope.FinishNoChanges();
214 return;
215 }
216
217 auto scope = tracing::Span::CurrentSpan().CreateScopeTime("copy_data");
218 auto new_cache = GetData(type);
219
220 // No good way to identify whether cursor accesses DB or reads buffed data
221 scope.Reset(kFetchAndParseStage);
222
223 utils::CpuRelax relax{cpu_relax_iterations_, &scope};
224 std::size_t doc_count = 0;
225
226 for (const auto& doc : cursor) {
227 ++doc_count;
228
229 relax.Relax();
230
232
233 try {
234 auto object = DeserializeObject(doc);
235 auto key = (object.*MongoCacheTraits::kKeyField);
236
237 if (type == cache::UpdateType::kIncremental || new_cache->count(key) == 0) {
238 (*new_cache)[key] = std::move(object);
239 } else {
241 << "Found duplicate key for 2 items in cache " << MongoCacheTraits::kName << ", key=" << key;
242 }
243 } catch (const std::exception& e) {
245 << "Failed to deserialize cache item of cache " << MongoCacheTraits::kName
246 << ", _id=" << doc["_id"].template ConvertTo<std::string>() << ", what(): " << e;
248
249 if (!MongoCacheTraits::kAreInvalidDocumentsSkipped) {
250 throw;
251 }
252 }
253 }
254
255 const auto elapsed_time = scope.ElapsedTotal(kFetchAndParseStage);
256 if (elapsed_time > kCpuRelaxThreshold) {
257 cpu_relax_iterations_ = static_cast<
258 std::size_t>(static_cast<double>(doc_count) / (elapsed_time / kCpuRelaxInterval));
259 LOG_TRACE() << fmt::format(
260 "Elapsed time for updating {} {} for {} data items is over threshold. "
261 "Will relax CPU every {} iterations",
262 kName,
263 elapsed_time.count(),
264 doc_count,
265 cpu_relax_iterations_
266 );
267 }
268
269 scope.Reset();
270
271 const auto size = new_cache->size();
272 this->Set(std::move(new_cache));
273 stats_scope.Finish(size);
274}
275
276template <class MongoCacheTraits>
277typename MongoCacheTraits::ObjectType MongoCache<MongoCacheTraits>::DeserializeObject(const formats::bson::Document& doc
278) const {
279 if constexpr (mongo_cache::impl::kHasDeserializeObject<MongoCacheTraits>) {
280 return MongoCacheTraits::DeserializeObject(doc);
281 }
282 if constexpr (mongo_cache::impl::kHasDefaultDeserializeObject<MongoCacheTraits>) {
283 return doc.As<typename MongoCacheTraits::ObjectType>();
284 }
285 UASSERT_MSG(false, "No deserialize operation defined but DeserializeObject invoked");
286}
287
288template <class MongoCacheTraits>
289storages::mongo::operations::Find MongoCache<MongoCacheTraits>::GetFindOperation(
290 cache::UpdateType type,
291 const std::chrono::system_clock::time_point& last_update,
292 const std::chrono::system_clock::time_point& now,
293 const std::chrono::system_clock::duration& correction
294) {
295 namespace bson = formats::bson;
296 namespace sm = storages::mongo;
297
298 auto find_op = [&]() -> sm::operations::Find {
299 if constexpr (mongo_cache::impl::kHasFindOperation<MongoCacheTraits>) {
300 return MongoCacheTraits::GetFindOperation(type, last_update, now, correction);
301 }
302 if constexpr (mongo_cache::impl::kHasDefaultFindOperation<MongoCacheTraits>) {
303 bson::ValueBuilder query_builder(bson::ValueBuilder::Type::kObject);
304 if constexpr (mongo_cache::impl::kHasUpdateFieldName<MongoCacheTraits>) {
306 query_builder
307 [MongoCacheTraits::kMongoUpdateFieldName] = bson::MakeDoc("$gt", last_update - correction);
308 }
309 }
310 return sm::operations::Find(query_builder.ExtractValue());
311 }
312 UASSERT_MSG(false, "No find operation defined but GetFindOperation invoked");
313 }();
314
315 if (MongoCacheTraits::kIsSecondaryPreferred) {
317 }
318 return find_op;
319}
320
321template <class MongoCacheTraits>
322std::unique_ptr<typename MongoCacheTraits::DataType> MongoCache<MongoCacheTraits>::GetData(cache::UpdateType type) {
324 auto ptr = this->Get();
325 return std::make_unique<typename MongoCacheTraits::DataType>(*ptr);
326 } else {
327 return std::make_unique<typename MongoCacheTraits::DataType>();
328 }
329}
330
331namespace impl {
332
333std::string GetMongoCacheSchema();
334
335} // namespace impl
336
337template <class MongoCacheTraits>
338yaml_config::Schema MongoCache<MongoCacheTraits>::GetStaticConfigSchema() {
339 return yaml_config::MergeSchemas<
340 CachingComponentBase<typename MongoCacheTraits::DataType>>(impl::GetMongoCacheSchema());
341}
342
343} // namespace components
344
345USERVER_NAMESPACE_END