userver: userver/dump/dumper.hpp Source File
Loading...
Searching...
No Matches
dumper.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/dump/dumper.hpp
4/// @brief @copybrief dump::Dumper
5
6#include <chrono>
7#include <memory>
8#include <optional>
9#include <string>
10
11#include <userver/components/component_fwd.hpp>
12#include <userver/dump/helpers.hpp>
13#include <userver/dump/operations.hpp>
14#include <userver/dynamic_config/fwd.hpp>
15#include <userver/engine/task/task_processor_fwd.hpp>
16#include <userver/utils/box.hpp>
17#include <userver/utils/resource_scopes_fwd.hpp>
18#include <userver/yaml_config/fwd.hpp>
19
20USERVER_NAMESPACE_BEGIN
21
22namespace utils::statistics {
23class Storage;
24} // namespace utils::statistics
25
26namespace testsuite {
27class DumpControl;
28} // namespace testsuite
29
30/// Dumping of cache-like components
31namespace dump {
32
33struct Config;
35extern const std::string_view kDump;
36
37/// A dynamically dispatched equivalent of `kDumpable` "concept". Unlike
38/// with ADL-found `Write`/`Read`, the methods are guaranteed not to be called
39/// in parallel.
41public:
42 virtual ~DumpableEntity();
43
44 virtual void GetAndWrite(dump::Writer& writer) const = 0;
45
46 virtual void ReadAndSet(dump::Reader& reader) = 0;
47};
48
49enum class UpdateType {
50 /// Some new data has appeared since the last update. `Dumper` will write it
51 /// on the next `WriteDumpAsync` call, or as specified by the config.
53
54 /// There is no new data, but we have verified that the old data is
55 /// up-to-date. `Dumper` will bump the dump modification time to `now`.
57};
58
59/// @brief Manages dumps of a cache-like component
60///
61/// The class is thread-safe.
62///
63/// Used in `components::CachingComponentBase`.
64///
65/// Automatically subscribes to:
66/// - dynamic config updates from `USERVER_DUMPS` under `dumper_name`
67/// - statistics under `cache.{dumper_name}.dump`
68///
69/// Dumps will be stored in `{dump-root}/{dumper_name}`, where `dump-root` is
70/// taken from `components::DumpConfigurator`.
71///
72/// Here, `dumper_name` is the name of the parent component.
73///
74/// ## Dumper Dynamic config
75/// * @ref USERVER_DUMPS
76///
77/// ## Static config of dump::Dumper :
78/// @include{doc} scripts/docs/en/components_schema/core/src/dump/dumper.md
79///
80/// Options inherited from @ref components::ComponentBase :
81/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
82///
83/// ## Sample usage
84/// @snippet core/src/dump/dumper_test.cpp Sample Dumper usage
85///
86/// @see components::DumpConfigurator
87class Dumper final {
88public:
89 /// @brief The primary constructor for when `Dumper` is stored in a component
90 /// @note `dumpable` must outlive this `Dumper`
92 const components::ComponentConfig& config,
93 const components::ComponentContext& context,
94 DumpableEntity& dumpable
95 );
96
97 /// For internal use only
99 const Config& initial_config,
100 std::unique_ptr<OperationsFactory> rw_factory,
101 engine::TaskProcessor& fs_task_processor,
102 dynamic_config::Source config_source,
103 utils::statistics::Storage& statistics_storage,
104 testsuite::DumpControl& dump_control,
105 DumpableEntity& dumpable
106 );
107
108 Dumper(Dumper&&) = delete;
109 Dumper& operator=(Dumper&&) = delete;
110 ~Dumper();
111
112 const std::string& Name() const;
113
114 /// @brief Read data from a dump, if any
115 /// @note Catches and logs any exceptions related to read operation failure
116 /// @returns `update_time` of the loaded dump on success, `null` otherwise
117 std::optional<TimePoint> ReadDump();
118
119 /// @brief Forces the `Dumper` to write a dump synchronously
120 /// @throws std::exception if the `Dumper` failed to write a dump
122
123 /// @brief Forces the `Dumper` to read from a dump synchronously
124 /// @throws std::exception if the `Dumper` failed to read a dump
126
127 /// @brief Notifies the `Dumper` of an update in the `DumpableEntity`
128 ///
129 /// A dump will be written asynchronously as soon as:
130 ///
131 /// 1. data update has been reported via `OnUpdateCompleted` since the last
132 /// written dump,
133 /// 2. dumps are `enabled` in the dynamic config, and
134 /// 3. `min-interval` time has passed
135 ///
136 /// @note This overload is more performant. The time written on the dump will
137 /// be taken from the dump writing time.
139
140 /// @see void OnUpdateCompleted()
141 /// @param update_time The time at which the data has been guaranteed to be
142 /// up-to-date
143 /// @param update_type Whether the update modified the data or confirmed its
144 /// actuality, UpdateType::kModified by default
145 /// @note This overload locks mutexes and should not be used in tight loops.
146 /// On the other hand, it allows you to exactly control the dump expiration.
147 void OnUpdateCompleted(TimePoint update_time, UpdateType update_type);
148
149 /// @brief Cancel and wait for the task running background writes.
150 ///
151 /// CancelWriteTaskAndWait is automatically called in the destructor. This
152 /// method must be called explicitly if the `DumpableEntity` may start its
153 /// destruction before the `Dumper` is destroyed.
154 ///
155 /// After calling this method, OnUpdateCompleted calls have no effect.
157
158 /// @brief Returns the static config schema for a
159 /// components::ComponentBase with an added `dump` sub-section.
161
162private:
163 Dumper(const Config& initial_config, const components::ComponentContext& context, DumpableEntity& dumpable);
164
165 class Impl;
166 utils::Box<utils::WithResourceScopes<Impl>> impl_;
167};
168
169} // namespace dump
170
171USERVER_NAMESPACE_END