userver: userver/dump/dumper.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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/fast_pimpl.hpp>
17#include <userver/yaml_config/fwd.hpp>
18
19USERVER_NAMESPACE_BEGIN
20
21namespace utils::statistics {
22class Storage;
23} // namespace utils::statistics
24
25namespace testsuite {
26class DumpControl;
27} // namespace testsuite
28
29/// Dumping of cache-like components
30namespace dump {
31
32struct Config;
34extern const std::string_view kDump;
35
36/// A dynamically dispatched equivalent of `kDumpable` "concept". Unlike
37/// with ADL-found `Write`/`Read`, the methods are guaranteed not to be called
38/// in parallel.
40 public:
41 virtual ~DumpableEntity();
42
43 virtual void GetAndWrite(dump::Writer& writer) const = 0;
44
45 virtual void ReadAndSet(dump::Reader& reader) = 0;
46};
47
48enum class UpdateType {
49 /// Some new data has appeared since the last update. `Dumper` will write it
50 /// on the next `WriteDumpAsync` call, or as specified by the config.
52
53 /// There is no new data, but we have verified that the old data is
54 /// up-to-date. `Dumper` will bump the dump modification time to `now`.
56};
57
58// clang-format off
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/// ## Dynamic config
75/// * @ref USERVER_DUMPS
76///
77/// ## Static config
78/// Name | Type | Description | Default value
79/// ---- | ---- | ----------- | -------------
80/// `enable` | `boolean` | Whether this `Dumper` should actually read and write dumps | (required)
81/// `world-readable` | `boolean` | If `true`, dumps are created with access `0444`, otherwise with access `0400` | (required)
82/// `format-version` | `integer` | Allows to ignore dumps written with an obsolete `format-version` | (required)
83/// `max-age` | optional `string` (duration) | Overdue dumps are ignored | null
84/// `max-count` | optional `integer` | Old dumps over the limit are removed from disk | `1`
85/// `min-interval` | `string` (duration) | `WriteDumpAsync` calls performed in a fast succession are ignored | `0s`
86/// `fs-task-processor` | `string` | `TaskProcessor` for blocking disk IO | `fs-task-processor`
87/// `encrypted` | `boolean` | Whether to encrypt the dump | `false`
88/// `first-update-mode` | `string` | specifies whether required or best-effort first update will be used | skip
89/// `first-update-type` | `string` | specifies whether incremental and/or full first update will be used | full
90///
91/// ## Sample usage
92/// @snippet core/src/dump/dumper_test.cpp Sample Dumper usage
93///
94/// @see components::DumpConfigurator
95// clang-format on
96class Dumper final {
97 public:
98 /// @brief The primary constructor for when `Dumper` is stored in a component
99 /// @note `dumpable` must outlive this `Dumper`
100 Dumper(const components::ComponentConfig& config,
101 const components::ComponentContext& context, DumpableEntity& dumpable);
102
103 /// For internal use only
104 Dumper(const Config& initial_config,
105 std::unique_ptr<OperationsFactory> rw_factory,
106 engine::TaskProcessor& fs_task_processor,
107 dynamic_config::Source config_source,
108 utils::statistics::Storage& statistics_storage,
109 testsuite::DumpControl& dump_control, DumpableEntity& dumpable);
110
111 Dumper(Dumper&&) = delete;
112 Dumper& operator=(Dumper&&) = delete;
113 ~Dumper();
114
115 const std::string& Name() const;
116
117 /// @brief Read data from a dump, if any
118 /// @note Catches and logs any exceptions related to read operation failure
119 /// @returns `update_time` of the loaded dump on success, `null` otherwise
120 std::optional<TimePoint> ReadDump();
121
122 /// @brief Forces the `Dumper` to write a dump synchronously
123 /// @throws std::exception if the `Dumper` failed to write a dump
125
126 /// @brief Forces the `Dumper` to read from a dump synchronously
127 /// @throws std::exception if the `Dumper` failed to read a dump
129
130 /// @brief Notifies the `Dumper` of an update in the `DumpableEntity`
131 ///
132 /// A dump will be written asynchronously as soon as:
133 ///
134 /// 1. data update has been reported via `OnUpdateCompleted` since the last
135 /// written dump,
136 /// 2. dumps are `enabled` in the dynamic config, and
137 /// 3. `min-interval` time has passed
138 ///
139 /// @note This overload is more performant. The time written on the dump will
140 /// be taken from the dump writing time.
142
143 /// @overload void OnUpdateCompleted()
144 /// @param update_time The time at which the data has been guaranteed to be
145 /// up-to-date
146 /// @param update_type Whether the update modified the data or confirmed its
147 /// actuality, UpdateType::kModified by default
148 /// @note This overload locks mutexes and should not be used in tight loops.
149 /// On the other hand, it allows to exactly control the dump expiration.
150 void OnUpdateCompleted(TimePoint update_time, UpdateType update_type);
151
152 /// @brief Cancel and wait for the task running background writes. Also
153 /// disables operations via testsuite dump control.
154 ///
155 /// CancelWriteTaskAndWait is automatically called in the destructor. This
156 /// method must be called explicitly if the `DumpableEntity` may start its
157 /// destruction before the `Dumper` is destroyed.
158 ///
159 /// After calling this method, OnUpdateCompleted calls have no effect.
161
162 /// @brief Returns the static config schema for a
163 /// components::LoggableComponentBase with an added `dump` sub-section.
165
166 private:
167 Dumper(const Config& initial_config,
168 const components::ComponentContext& context, DumpableEntity& dumpable);
169
170 class Impl;
171 utils::FastPimpl<Impl, 1056, 16> impl_;
172};
173
174} // namespace dump
175
176USERVER_NAMESPACE_END