-
Notifications
You must be signed in to change notification settings - Fork 5.5k
add deferred_creation util into stats #27899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
6b8b489
8f3119a
17cfb10
f8ba76a
68d3c6b
19d75d8
2bbace4
7d69cbf
8318935
91e47e3
cfc9859
f6bd430
97c2b86
8e9dfe5
bca128a
d1ee36c
448e474
1fa16e7
6e9a861
c1d2aa4
4e72f1e
35e7cb1
0a11cb5
8a80f36
448074d
28db9d3
8134314
c22db02
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -218,5 +218,44 @@ using SizeFn = std::function<void(std::size_t)>; | |
| */ | ||
| template <typename Stat> using StatFn = std::function<void(Stat&)>; | ||
|
|
||
| /** | ||
| * Interface for stats lazy initialization. | ||
| * To save memory and CPU consumption from unused stats, Envoy can enable the bootstrap config | ||
| * :ref:`enable_deferred_creation_stats | ||
| * <envoy_v3_api_field_config.bootstrap.v3.Bootstrap.enable_deferred_creation_stats>`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. put the API update in this PR
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bootstrap.proto is in this pR |
||
| * A 'StatsStructType' is only created when any of its field is referenced. | ||
| * See more context: https://github.com/envoyproxy/envoy/issues/23575 | ||
| */ | ||
| template <typename StatsStructType> class DeferredCreationCompatibleInterface { | ||
| public: | ||
| // Helper function to get-or-create and return the StatsStructType object. | ||
| virtual StatsStructType& instantiate() PURE; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As noted in the comment, I think
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| virtual ~DeferredCreationCompatibleInterface() = default; | ||
| }; | ||
|
|
||
| // Template that lazily initializes a StatsStruct. | ||
| // The bootstrap config :ref:`enable_deferred_creation_stats | ||
| // <envoy_v3_api_field_config.bootstrap.v3.Bootstrap.enable_deferred_creation_stats>` decides if | ||
| // stats lazy initialzation is enabled or not. | ||
| template <typename StatsStructType> class DeferredCreation; | ||
| template <typename StatsStructType> class DirectStats; | ||
|
|
||
| // A helper class for a lazy compatible stats struct type. | ||
| template <typename StatsStructType> class DeferredCreationCompatibleStats { | ||
| public: | ||
| DeferredCreationCompatibleStats( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| std::unique_ptr<DeferredCreationCompatibleInterface<StatsStructType>> d) | ||
| : data_(std::move(d)) {} | ||
| // Allows move construct and assign. | ||
| DeferredCreationCompatibleStats& operator=(DeferredCreationCompatibleStats&&) = default; | ||
| DeferredCreationCompatibleStats(DeferredCreationCompatibleStats&&) = default; | ||
|
|
||
| inline StatsStructType* operator->() { return &data_->instantiate(); }; | ||
| inline StatsStructType& operator*() { return data_->instantiate(); }; | ||
|
|
||
| private: | ||
| std::unique_ptr<DeferredCreationCompatibleInterface<StatsStructType>> data_; | ||
| }; | ||
| } // namespace Stats | ||
| } // namespace Envoy | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -158,16 +158,17 @@ static inline std::string statPrefixJoin(absl::string_view prefix, absl::string_ | |
| */ | ||
| #define MAKE_STATS_STRUCT(StatsStruct, StatNamesStruct, ALL_STATS) \ | ||
| struct StatsStruct { \ | ||
| using StatNameType = StatNamesStruct; \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I understand why you need to create this alias. But can you add a comment explaining that?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, added a line comment. |
||
| static const absl::string_view typeName() { return #StatsStruct; } \ | ||
| StatsStruct(const StatNamesStruct& stat_names, Envoy::Stats::Scope& scope, \ | ||
| Envoy::Stats::StatName prefix = Envoy::Stats::StatName()) \ | ||
| : stat_names_(stat_names) \ | ||
| ALL_STATS(MAKE_STATS_STRUCT_COUNTER_HELPER_, MAKE_STATS_STRUCT_GAUGE_HELPER_, \ | ||
| MAKE_STATS_STRUCT_HISTOGRAM_HELPER_, \ | ||
| MAKE_STATS_STRUCT_TEXT_READOUT_HELPER_, \ | ||
| MAKE_STATS_STRUCT_STATNAME_HELPER_) {} \ | ||
| const StatNamesStruct& stat_names_; \ | ||
| const StatNameType& stat_names_; \ | ||
| ALL_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT, GENERATE_HISTOGRAM_STRUCT, \ | ||
| GENERATE_TEXT_READOUT_STRUCT, GENERATE_STATNAME_STRUCT) \ | ||
| } | ||
|
|
||
| } // namespace Envoy | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| #pragma once | ||
|
|
||
| #include "envoy/common/pure.h" | ||
| #include "envoy/stats/scope.h" | ||
| #include "envoy/stats/stats.h" | ||
|
|
||
| #include "source/common/common/cleanup.h" | ||
| #include "source/common/common/thread.h" | ||
| #include "source/common/stats/symbol_table.h" | ||
| #include "source/common/stats/utility.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Stats { | ||
|
|
||
| /** | ||
| * Lazy-initialization wrapper for StatsStructType, intended for deferred instantiation of a block | ||
| * of stats that might not be needed in a given Envoy process. | ||
| * | ||
| * This class is thread-safe -- instantiations can occur on multiple concurrent threads. | ||
| * This is used when | ||
| * :ref:`enable_deferred_creation_stats | ||
| * <envoy_v3_api_field_config.bootstrap.v3.Bootstrap.enable_deferred_creation_stats>` is enabled. | ||
| */ | ||
| template <typename StatsStructType> | ||
| class DeferredCreation : public DeferredCreationCompatibleInterface<StatsStructType> { | ||
| public: | ||
| // Capture the stat names object and the scope with a ctor, that can be used to instantiate a | ||
| // StatsStructType object later. | ||
| // Caller should make sure scope and stat_names outlive this object. | ||
| DeferredCreation(const typename StatsStructType::StatNameType& stat_names, | ||
| Stats::ScopeSharedPtr scope) | ||
| : initialized_([&scope]() -> Gauge& { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to capture scope by value here? That this works makes me suspect we don't have a test that removes the scope first and then instantiates the stats.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The constructor accepts the scope by value, so this should be fine, right?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually come to think of it, line 41 will cause the scope originally passed in to the constructor to be deleted. Shouldn't we keep the scope alive for the lifetime of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the constructor will be out of scope before the lambda gets run, so no help there :( I think maybe this is all moot because StatsStructType does not hold a reference-count for the scope, but holds it by So what will happen in the test scenario I just thought of: I think the code as is will reference freed memory, but with my suggestion it will work, but not be useful :) It will successfully instantiate the stats in the scope, and then the Cleanup thing will remove the scope. So you'll be left with struct full of invalid stats. So if you then do: that will then crash. This is beyond the scope of this PR to resolve (sic) but I'd still vote for capturing scope by value on line 32.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The scope is used first in initialize the gauge, and then moved into the ctor_, could you help me to understand the life cycle issue there? the scope is always supposed to outlive the stats, otherwise the stats will be deleted IIUC.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I articulated the lifecycle issue above. In practice it doesn't matter but my recommendation stands -- just change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not following where this "reference freed memory" is from. Scope is held in ctor_ until it's called. but further references to its member I am not sure, since the last copy of "scope" is deleted. I have the understanding that scope always outlive the individual StatStruct, if that not true, then the previous suggestion of do not capture scope in DeferredCreation need to be revisited.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The lambda in line 32 is not stored anywhere, so capturing scope by value there does not change anything. It's a temporary that is invoked and immediately destroyed. The lambda that is stored is the one on line 38, and the scope there is (effectively) captured by value (moved from the passed in by value object for the constructor of However storing the scope in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the code as is, the call to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added comment as asked. |
||
| Stats::StatNamePool pool(scope->symbolTable()); | ||
| return Stats::Utility::gaugeFromElements( | ||
| *scope, {pool.add(StatsStructType::typeName()), pool.add("initialized")}, | ||
| Stats::Gauge::ImportMode::HiddenAccumulate); | ||
| }()), | ||
| ctor_([&, stats_scope = std::move(scope)]() -> StatsStructType* { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/&/this/ ? does that work?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| initialized_.inc(); | ||
| // Reset ctor_ to save some RAM. | ||
| Cleanup reset_ctor([&] { ctor_ = nullptr; }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/&/this/ ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| return new StatsStructType(stat_names, *stats_scope); | ||
| }) { | ||
| if (initialized_.value() > 0) { | ||
| instantiate(); | ||
| } | ||
| } | ||
| ~DeferredCreation() { | ||
| if (ctor_ == nullptr) { | ||
| initialized_.dec(); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| inline StatsStructType& instantiate() override { return *internal_stats_.get(ctor_); } | ||
|
|
||
| // In order to preserve stat value continuity across a config reload, we need to automatically | ||
| // re-instantiate lazy stats when they are constructed, if there is already a live instantiation | ||
| // to the same stats. Consider the following alternate scenarios: | ||
|
|
||
| // Scenario 1: a cluster is instantiated but receives no requests, so its traffic-related stats | ||
| // are never instantiated. When this cluster gets reloaded on a config update, a new lazy-init | ||
| // block is created, but the stats should again not be instantiated. | ||
|
|
||
| // Scenario 2: a cluster is instantiated and receives traffic, so its traffic-related stats are | ||
| // instantiated. We must ensure that a new instance for the same cluster gets its lazy-stats | ||
| // instantiated before the previous cluster of the same name is destructed. | ||
|
|
||
| // To do that we keep an "initialized" gauge in the cluster's scope, which will be associated by | ||
| // name to the previous generation's cluster's lazy-init block. We use the value in this shared | ||
| // gauge to determine whether to instantiate the lazy block on construction. | ||
| // TODO(#26106): See #14610. The initialized_ gauge could be disabled in a | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not possible thanks to @DiazAlan's PR: https://github.com/envoyproxy/envoy/blob/main/source/common/stats/thread_local_store.cc#L602
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahh, right, thanks!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You cannot disable hidden stats per @DiazAlan so you can remove the TODO starting line 72 also.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| // corner case where a user disables stats with suffix "initialized". In which case, the | ||
| // initialized_ will be a NullGauge, which breaks the above scenario 2. | ||
| // TODO(#26106): Consider hiding this Gauge from being exported, through using the | ||
| // stats flags mask. | ||
| Gauge& initialized_; | ||
| // TODO(#26957): Clean up this ctor_ by moving its ownership to AtomicPtr, and drop | ||
| // the setter lambda when the nested object is created. | ||
| std::function<StatsStructType*()> ctor_; | ||
| Thread::AtomicPtr<StatsStructType, Thread::AtomicPtrAllocMode::DeleteOnDestruct> internal_stats_; | ||
| }; | ||
|
|
||
| // Non-DeferredCreation wrapper over StatsStructType. This is used when | ||
| // :ref:`enable_deferred_creation_stats | ||
| // <envoy_v3_api_field_config.bootstrap.v3.Bootstrap.enable_deferred_creation_stats>` is not | ||
| // enabled. | ||
| template <typename StatsStructType> | ||
| class DirectStats : public DeferredCreationCompatibleInterface<StatsStructType> { | ||
| public: | ||
| DirectStats(const typename StatsStructType::StatNameType& stat_names, Stats::Scope& scope) | ||
| : stats_(stat_names, scope) {} | ||
|
|
||
| private: | ||
| inline StatsStructType& instantiate() override { return stats_; } | ||
| StatsStructType stats_; | ||
| }; | ||
|
|
||
| template <typename StatsStructType> | ||
| DeferredCreationCompatibleStats<StatsStructType> | ||
| createDeferredCompatibleStats(Stats::ScopeSharedPtr scope, | ||
| const typename StatsStructType::StatNameType& stat_names, | ||
| bool deferred_creation) { | ||
| if (deferred_creation) { | ||
| return {std::make_unique<DeferredCreation<StatsStructType>>(stat_names, scope)}; | ||
| } else { | ||
| return {std::make_unique<DirectStats<StatsStructType>>(stat_names, *scope)}; | ||
| } | ||
| } | ||
|
|
||
| } // namespace Stats | ||
| } // namespace Envoy | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest slightly more detail:
To save memory and CPU consumption on blocks of stats that are never referenced throughout the process lifetime, they can be encapsulated in a DeferredCreationCompatibleInterface. Then the Envoy bootstrap configuration can be set to defer the instantiation of those block. Note that when the blocks of stats are created, they carry an extra ~160 byte overhead (depending on worker thread count) due to internal bookkeeping data structures. The overhead when deferred stats are disabled is just 8 bytes.
WDYT? You should confirm the 160 bytes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated.
did you see this? #23921 (comment)
"The last column shows the overhead is around 5MB for 100K clusters."