Skip to content

Commit

Permalink
stats: add mechanism to create dynamic tokens without locks or sharing (
Browse files Browse the repository at this point in the history
envoyproxy#9355)

Signed-off-by: Joshua Marantz <[email protected]>
  • Loading branch information
jmarantz authored Jan 21, 2020
1 parent c7f25e8 commit 7c78372
Show file tree
Hide file tree
Showing 23 changed files with 440 additions and 317 deletions.
8 changes: 2 additions & 6 deletions include/envoy/stats/symbol_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class SymbolTable {

private:
friend struct HeapStatData;
friend class StatNameDynamicStorage;
friend class StatNameStorage;
friend class StatNameList;
friend class StatNameSet;
Expand Down Expand Up @@ -222,12 +223,7 @@ class SymbolTable {
*/
virtual StoragePtr encode(absl::string_view name) PURE;

/**
* Called by StatNameSet's destructor.
*
* @param stat_name_set the set.
*/
virtual void forgetSet(StatNameSet& stat_name_set) PURE;
virtual StoragePtr makeDynamicStorage(absl::string_view name) PURE;
};

using SymbolTablePtr = std::unique_ptr<SymbolTable>;
Expand Down
9 changes: 1 addition & 8 deletions source/common/http/codes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,6 @@ void CodeStatsImpl::chargeResponseTiming(const ResponseTimingInfo& info) const {
}
}

absl::string_view CodeStatsImpl::stripTrailingDot(absl::string_view str) {
if (absl::EndsWith(str, ".")) {
str.remove_suffix(1);
}
return str;
}

Stats::StatName CodeStatsImpl::upstreamRqGroup(Code response_code) const {
switch (enumToInt(response_code) / 100) {
case 1:
Expand All @@ -187,7 +180,7 @@ Stats::StatName CodeStatsImpl::upstreamRqStatName(Code response_code) const {
if (rc_index >= NumHttpCodes) {
return upstream_rq_unknown_;
}
std::atomic<uint8_t*>& atomic_ref = rc_stat_names_[rc_index];
std::atomic<const uint8_t*>& atomic_ref = rc_stat_names_[rc_index];
if (atomic_ref.load() == nullptr) {
absl::MutexLock lock(&mutex_);

Expand Down
3 changes: 1 addition & 2 deletions source/common/http/codes.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class CodeStatsImpl : public CodeStats {
void recordHistogram(Stats::Scope& scope, const Stats::StatNameVec& names,
Stats::Histogram::Unit unit, uint64_t count) const;

static absl::string_view stripTrailingDot(absl::string_view prefix);
Stats::StatName upstreamRqGroup(Code response_code) const;
Stats::StatName upstreamRqStatName(Code response_code) const;

Expand Down Expand Up @@ -109,7 +108,7 @@ class CodeStatsImpl : public CodeStats {

static constexpr uint32_t NumHttpCodes = 500;
static constexpr uint32_t HttpCodeOffset = 100; // code 100 is at index 0.
mutable std::atomic<uint8_t*> rc_stat_names_[NumHttpCodes];
mutable std::atomic<const uint8_t*> rc_stat_names_[NumHttpCodes];
};

/**
Expand Down
6 changes: 1 addition & 5 deletions source/common/router/router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,7 @@ Http::FilterHeadersStatus Filter::decodeHeaders(Http::HeaderMap& headers, bool e

const Http::HeaderEntry* request_alt_name = headers.EnvoyUpstreamAltStatName();
if (request_alt_name) {
// TODO(#7003): converting this header value into a StatName requires
// taking a global symbol-table lock. This is not a frequently used feature,
// but may not be the only occurrence of this pattern, where it's difficult
// or impossible to pre-compute a StatName for a component of a stat name.
alt_stat_prefix_ = std::make_unique<Stats::StatNameManagedStorage>(
alt_stat_prefix_ = std::make_unique<Stats::StatNameDynamicStorage>(
request_alt_name->value().getStringView(), config_.scope_.symbolTable());
headers.removeEnvoyUpstreamAltStatName();
}
Expand Down
2 changes: 1 addition & 1 deletion source/common/router/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ class Filter : Logger::Loggable<Logger::Id::router>,
RouteConstSharedPtr route_;
const RouteEntry* route_entry_{};
Upstream::ClusterInfoConstSharedPtr cluster_;
std::unique_ptr<Stats::StatNameManagedStorage> alt_stat_prefix_;
std::unique_ptr<Stats::StatNameDynamicStorage> alt_stat_prefix_;
const VirtualCluster* request_vcluster_;
Event::TimerPtr response_timeout_;
FilterUtility::TimeoutData timeout_;
Expand Down
12 changes: 6 additions & 6 deletions source/common/stats/fake_symbol_table_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ class FakeSymbolTableImpl : public SymbolTable {
MemBlockBuilder<uint8_t> mem_block(total_size_bytes);
mem_block.appendOne(num_names);
for (uint32_t i = 0; i < num_names; ++i) {
auto& name = names[i];
size_t sz = name.size();
absl::string_view name = names[i];
const size_t sz = name.size();
SymbolTableImpl::Encoding::appendEncoding(sz, mem_block);
if (!name.empty()) {
mem_block.appendData(
absl::MakeConstSpan(reinterpret_cast<const uint8_t*>(name.data()), sz));
mem_block.appendData(absl::MakeSpan(reinterpret_cast<const uint8_t*>(name.data()), sz));
}
}

Expand All @@ -102,6 +101,7 @@ class FakeSymbolTableImpl : public SymbolTable {
void free(const StatName&) override {}
void incRefCount(const StatName&) override {}
StoragePtr encode(absl::string_view name) override { return encodeHelper(name); }
StoragePtr makeDynamicStorage(absl::string_view name) override { return encodeHelper(name); }
SymbolTable::StoragePtr join(const std::vector<StatName>& names) const override {
std::vector<absl::string_view> strings;
for (StatName name : names) {
Expand All @@ -125,7 +125,6 @@ class FakeSymbolTableImpl : public SymbolTable {
// make_unique does not work with private ctor, even though FakeSymbolTableImpl is a friend.
return StatNameSetPtr(new StatNameSet(*this, name));
}
void forgetSet(StatNameSet&) override {}
uint64_t getRecentLookups(const RecentLookupsFn&) const override { return 0; }
void clearRecentLookups() override {}
void setRecentLookupCapacity(uint64_t) override {}
Expand All @@ -142,7 +141,8 @@ class FakeSymbolTableImpl : public SymbolTable {
MemBlockBuilder<uint8_t> mem_block(SymbolTableImpl::Encoding::totalSizeBytes(name.size()));
SymbolTableImpl::Encoding::appendEncoding(name.size(), mem_block);
mem_block.appendData(
absl::MakeConstSpan(reinterpret_cast<const uint8_t*>(name.data()), name.size()));
absl::MakeSpan(reinterpret_cast<const uint8_t*>(name.data()), name.size()));
ASSERT(mem_block.capacityRemaining() == 0);
return mem_block.release();
}
};
Expand Down
Loading

0 comments on commit 7c78372

Please sign in to comment.