Skip to content

Commit 4fe7ff4

Browse files
committed
feat userver: use RegisterScope() API for metrics & cache
commit_hash:aac3515679e852a16316374728b845c64a522aa5
1 parent 5793856 commit 4fe7ff4

File tree

32 files changed

+147
-177
lines changed

32 files changed

+147
-177
lines changed

core/functional_tests/cache_update/service.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@ class AlertCache final : public components::CachingComponentBase<Data> {
2323
public:
2424
static constexpr std::string_view kName = "alert-cache";
2525

26-
AlertCache(const components::ComponentConfig& config, const components::ComponentContext& context)
27-
: CachingComponentBase(config, context)
28-
{
29-
CacheUpdateTrait::StartPeriodicUpdates();
30-
}
31-
32-
~AlertCache() override { CacheUpdateTrait::StopPeriodicUpdates(); }
26+
using components::CachingComponentBase<Data>::CachingComponentBase;
3327

3428
void Update(
3529
cache::UpdateType type,
@@ -53,11 +47,7 @@ class CacheSample final : public components::CachingComponentBase<Data> {
5347

5448
CacheSample(const components::ComponentConfig& config, const components::ComponentContext& context)
5549
: CachingComponentBase(config, context)
56-
{
57-
CacheUpdateTrait::StartPeriodicUpdates();
58-
}
59-
60-
~CacheSample() override { CacheUpdateTrait::StopPeriodicUpdates(); }
50+
{}
6151

6252
void Update(
6353
cache::UpdateType /*type*/,

core/functional_tests/dump_coroutines/service.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ class CachedTranslations final : public components::CachingComponentBase<KeyTran
2424

2525
CachedTranslations(const components::ComponentConfig& config, const components::ComponentContext& context)
2626
: CachingComponentBase(config, context)
27-
{
28-
CacheUpdateTrait::StartPeriodicUpdates();
29-
}
30-
31-
~CachedTranslations() override { CacheUpdateTrait::StopPeriodicUpdates(); }
27+
{}
3228

3329
void Update(
3430
cache::UpdateType /*type*/,

core/functional_tests/metrics/service.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ class CachedTranslations final : public components::CachingComponentBase<KeyTran
2323

2424
CachedTranslations(const components::ComponentConfig& config, const components::ComponentContext& context)
2525
: CachingComponentBase(config, context)
26-
{
27-
CacheUpdateTrait::StartPeriodicUpdates();
28-
}
29-
30-
~CachedTranslations() override { CacheUpdateTrait::StopPeriodicUpdates(); }
26+
{}
3127

3228
void Update(
3329
cache::UpdateType /*type*/,

core/include/userver/cache/cache_update_trait.hpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,6 @@ class CacheUpdateTrait {
6666
kNoFirstUpdate = 1 << 0,
6767
};
6868

69-
/// Starts periodic updates
70-
void StartPeriodicUpdates(utils::Flags<Flag> flags = {});
71-
72-
/// @brief Stops periodic updates
73-
/// @warning Should be called in destructor of derived class.
74-
void StopPeriodicUpdates();
75-
76-
void AssertPeriodicUpdateStarted();
77-
78-
void AssertPeriodicUpdateStopped();
79-
8069
/// Called in `CachingComponentBase::Set` during update to indicate
8170
/// that the cached data has been modified
8271
void OnCacheModified();
@@ -145,7 +134,23 @@ class CacheUpdateTrait {
145134
UpdateStatisticsScope& stats_scope
146135
) = 0;
147136

137+
/// @brief Returns flags for cache start.
138+
virtual utils::Flags<Flag> GetStartFlags() const;
139+
140+
/// @brief Call this to start periodic updates just now,
141+
/// not after the constuctor.
142+
void EarlyStartPeriodicUpdates(utils::Flags<Flag> flags);
143+
144+
/// @cond
145+
// For internal use only
146+
void EarlyStopPeriodicUpdates();
147+
/// @endcond
148+
148149
private:
150+
void StartPeriodicUpdates();
151+
152+
void StopPeriodicUpdates();
153+
149154
virtual void Cleanup() = 0;
150155

151156
virtual void MarkAsExpired();

core/include/userver/cache/caching_component_base.hpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <userver/cache/exceptions.hpp>
1515
#include <userver/compiler/demangle.hpp>
1616
#include <userver/components/component_base.hpp>
17+
#include <userver/components/component_context.hpp>
1718
#include <userver/components/component_fwd.hpp>
1819
#include <userver/concurrent/async_event_channel.hpp>
1920
#include <userver/dump/helpers.hpp>
@@ -36,9 +37,7 @@ namespace components {
3637
/// @brief Base class for caching components
3738
///
3839
/// Provides facilities for creating periodically updated caches.
39-
/// You need to override cache::CacheUpdateTrait::Update
40-
/// then call cache::CacheUpdateTrait::StartPeriodicUpdates after setup
41-
/// and cache::CacheUpdateTrait::StopPeriodicUpdates before teardown.
40+
/// You need to override cache::CacheUpdateTrait::Update.
4241
/// You can also override cache::CachingComponentBase::PreAssignCheck and set
4342
/// has-pre-assign-check: true in the static config to enable check.
4443
///
@@ -208,8 +207,6 @@ class CachingComponentBase : public ComponentBase, public cache::DataProvider<T>
208207
virtual void PreAssignCheck(const T* old_value_ptr, const T* new_value_ptr) const;
209208

210209
private:
211-
void OnAllComponentsLoaded() final;
212-
213210
void Cleanup() final;
214211

215212
void MarkAsExpired() final;
@@ -237,9 +234,7 @@ CachingComponentBase<T>::CachingComponentBase(const ComponentConfig& config, con
237234
}
238235
}
239236
)
240-
{
241-
const auto initial_config = GetConfig();
242-
}
237+
{}
243238

244239
template <typename T>
245240
CachingComponentBase<T>::~CachingComponentBase() {
@@ -356,11 +351,6 @@ std::unique_ptr<const T> CachingComponentBase<T>::ReadContents(dump::Reader& rea
356351
}
357352
}
358353

359-
template <typename T>
360-
void CachingComponentBase<T>::OnAllComponentsLoaded() {
361-
AssertPeriodicUpdateStarted();
362-
}
363-
364354
template <typename T>
365355
void CachingComponentBase<T>::Cleanup() {
366356
cache_.Cleanup();

core/include/userver/components/statistics_storage.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,21 @@ inline constexpr auto kConfigFileMode<StatisticsStorage> = ConfigFileMode::kNotR
6868

6969
} // namespace components
7070

71+
namespace utils::statistics {
72+
73+
/// @brief Add a writer function to @ref Storage from @ref StatisticsStorage.
74+
/// It automatically calls @ref Storage::RegisterWriter just after the component
75+
/// construction and @ref Entry::Unregister just before the component
76+
/// destructor.
77+
///
78+
/// @see @ref Storage::RegisterWriter.
79+
void RegisterWriterScope(
80+
const components::ComponentContext&,
81+
std::string common_prefix,
82+
WriterFunc func,
83+
std::vector<Label> add_labels = {}
84+
);
85+
86+
} // namespace utils::statistics
87+
7188
USERVER_NAMESPACE_END

core/include/userver/dynamic_config/updater/component.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ class DynamicConfigClientUpdater final : public CachingComponentBase<dynamic_con
7575

7676
DynamicConfigClientUpdater(const ComponentConfig&, const ComponentContext&);
7777

78-
~DynamicConfigClientUpdater() override;
79-
8078
// After calling this method, `Get()` will return a dynamic_config containing
8179
// the specified keys while the token that this method returned is alive.
8280
dynamic_config::AdditionalKeysToken SetAdditionalKeys(std::vector<std::string> keys);

core/include/userver/server/component.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ class Server final : public ComponentBase {
5353

5454
Server(const components::ComponentConfig& component_config, const components::ComponentContext& component_context);
5555

56-
~Server() override;
57-
5856
void OnAllComponentsLoaded() override;
5957

6058
void OnAllComponentsAreStopping() override;
@@ -71,8 +69,6 @@ class Server final : public ComponentBase {
7169
void WriteStatistics(utils::statistics::Writer& writer);
7270

7371
std::unique_ptr<server::Server> server_;
74-
utils::statistics::Entry server_statistics_holder_;
75-
utils::statistics::Entry handler_statistics_holder_;
7672
};
7773

7874
template <>

core/include/userver/utils/statistics/storage.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ class Storage final {
146146

147147
/// @brief Add a writer function. Note that `func` is called concurrently with
148148
/// other code, so it should be thread-safe.
149+
///
150+
/// @note Prefer using @ref RegisterWriterScope instead.
149151
Entry RegisterWriter(std::string common_prefix, WriterFunc func, std::vector<Label> add_labels = {});
150152

151153
/// @deprecated Use RegisterWriter instead.

core/src/cache/cache_metrics_initialization_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class ToyCache final : public components::CachingComponentBase<int> {
3939
ToyCache(const components::ComponentConfig& config, const components::ComponentContext& context)
4040
: CachingComponentBase(config, context)
4141
{
42-
StartPeriodicUpdates();
42+
EarlyStartPeriodicUpdates({});
4343
}
4444

45-
~ToyCache() override { StopPeriodicUpdates(); }
45+
~ToyCache() override { EarlyStopPeriodicUpdates(); }
4646

4747
void Update(
4848
cache::UpdateType /*type*/,

0 commit comments

Comments
 (0)