userver: userver/cache/cache_statistics.hpp Source File
Loading...
Searching...
No Matches
cache_statistics.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <atomic>
7#include <chrono>
8#include <cstddef>
9
11#include <userver/utils/impl/internal_tag.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace cache {
18
19namespace impl {
20
21struct UpdateStatistics final {
22 utils::statistics::RateCounter update_attempt_count{0};
23 utils::statistics::RateCounter update_no_changes_count{0};
24 utils::statistics::RateCounter update_failures_count{0};
25
26 utils::statistics::RateCounter documents_read_count{0};
27 utils::statistics::RateCounter documents_parse_failures{0};
28
29 std::atomic<std::chrono::steady_clock::time_point> last_update_start_time{{}};
30 std::atomic<std::chrono::steady_clock::time_point> last_successful_update_start_time{{}};
31 std::atomic<std::chrono::milliseconds> last_update_duration{{}};
32};
33
34void DumpMetric(utils::statistics::Writer& writer, const UpdateStatistics& stats);
35
36struct Statistics final {
37 UpdateStatistics full_update;
38 UpdateStatistics incremental_update;
39 std::atomic<std::size_t> documents_current_count{0};
40 std::atomic<bool> is_first_sync_update_complete{false}; // Success or failure does not matter here.
41};
42
43void DumpMetric(utils::statistics::Writer& writer, const Statistics& stats);
44
45enum class UpdateState { kNotFinished, kSuccess, kNoChanges, kFailure };
46
47} // namespace impl
48
57public:
59 // Note: DO NOT try to create `UpdateStatisticsScope` manually for manual `Update` calls.
60 // CachingComponentBase's internals WILL be corrupted.
61 // Call `InvalidateAsync` or `UpdateSyncDebug` instead.
62 // For internal use only.
63 UpdateStatisticsScope(utils::impl::InternalTag, impl::Statistics& stats, cache::UpdateType type);
64
66
67 // For internal use only.
68 impl::UpdateState GetState(utils::impl::InternalTag) const noexcept;
70
73 void Finish(std::size_t total_documents_count);
74
77
80
85 void IncreaseDocumentsReadCount(std::size_t add);
86
91 void IncreaseDocumentsParseFailures(std::size_t add);
92
93private:
94 void DoFinish(impl::UpdateState new_state);
95
96 impl::Statistics& stats_;
97 impl::UpdateStatistics& update_stats_;
98 impl::UpdateState state_{impl::UpdateState::kNotFinished};
99 const std::chrono::steady_clock::time_point update_start_time_;
100};
101
102} // namespace cache
103
104USERVER_NAMESPACE_END