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