Line data Source code
1 : #pragma once 2 : 3 : #include <string> 4 : 5 : #include "envoy/common/optref.h" 6 : #include "envoy/common/pure.h" 7 : #include "envoy/stats/scope.h" 8 : #include "envoy/stats/stats.h" 9 : 10 : #include "source/common/common/assert.h" 11 : #include "source/common/stats/symbol_table.h" 12 : 13 : namespace Envoy { 14 : namespace Server { 15 : 16 : class ProactiveResourceMonitor { 17 : public: 18 0 : ProactiveResourceMonitor() = default; 19 0 : virtual ~ProactiveResourceMonitor() = default; 20 : /** 21 : * Tries to allocate resource for given resource monitor in thread safe manner. 22 : * Returns true if there is enough resource quota available and allocation has succeeded, false 23 : * otherwise. 24 : * @param increment to add to current resource usage value and compare against configured max 25 : * threshold. 26 : */ 27 : virtual bool tryAllocateResource(int64_t increment) PURE; 28 : /** 29 : * Tries to deallocate resource for given resource monitor in thread safe manner. 30 : * Returns true if there is enough resource quota available and deallocation has succeeded, false 31 : * otherwise. 32 : * @param decrement to subtract from current resource usage value. 33 : */ 34 : virtual bool tryDeallocateResource(int64_t decrement) PURE; 35 : /** 36 : * Returns current resource usage (most recent read) tracked by monitor. 37 : */ 38 : virtual int64_t currentResourceUsage() const PURE; 39 : /** 40 : * Returns max resource usage configured in monitor. 41 : */ 42 : virtual int64_t maxResourceUsage() const PURE; 43 : }; 44 : 45 : using ProactiveResourceMonitorPtr = std::unique_ptr<ProactiveResourceMonitor>; 46 : using ProactiveResourceMonitorOptRef = OptRef<ProactiveResourceMonitor>; 47 : 48 : class ProactiveResource { 49 : public: 50 : ProactiveResource(const std::string& name, ProactiveResourceMonitorPtr monitor, 51 : Stats::Scope& stats_scope) 52 : : name_(name), monitor_(std::move(monitor)), 53 0 : failed_updates_counter_(makeCounter(stats_scope, name, "failed_updates")) {} 54 : 55 0 : bool tryAllocateResource(int64_t increment) { 56 0 : if (monitor_->tryAllocateResource(increment)) { 57 0 : return true; 58 0 : } else { 59 0 : failed_updates_counter_.inc(); 60 0 : return false; 61 0 : } 62 0 : } 63 : 64 0 : bool tryDeallocateResource(int64_t decrement) { 65 0 : if (monitor_->tryDeallocateResource(decrement)) { 66 0 : return true; 67 0 : } else { 68 0 : failed_updates_counter_.inc(); 69 0 : return false; 70 0 : } 71 0 : } 72 : 73 0 : ProactiveResourceMonitorOptRef getProactiveResourceMonitorForTest() { 74 0 : return makeOptRefFromPtr<ProactiveResourceMonitor>(monitor_.get()); 75 0 : }; 76 : 77 : private: 78 : const std::string name_; 79 : ProactiveResourceMonitorPtr monitor_; 80 : Stats::Counter& failed_updates_counter_; 81 : 82 0 : Stats::Counter& makeCounter(Stats::Scope& scope, absl::string_view a, absl::string_view b) { 83 0 : Stats::StatNameManagedStorage stat_name(absl::StrCat("overload.", a, ".", b), 84 0 : scope.symbolTable()); 85 0 : return scope.counterFromStatName(stat_name.statName()); 86 0 : } 87 : }; 88 : 89 : } // namespace Server 90 : } // namespace Envoy