Line data Source code
1 : #pragma once 2 : 3 : #include <string> 4 : 5 : #include "envoy/common/pure.h" 6 : #include "envoy/event/scaled_range_timer_manager.h" 7 : #include "envoy/event/timer.h" 8 : #include "envoy/server/proactive_resource_monitor.h" 9 : #include "envoy/thread_local/thread_local_object.h" 10 : 11 : #include "source/common/common/interval_value.h" 12 : #include "source/common/singleton/const_singleton.h" 13 : 14 : namespace Envoy { 15 : namespace Server { 16 : 17 : enum class OverloadProactiveResourceName { 18 : GlobalDownstreamMaxConnections, 19 : }; 20 : 21 : class OverloadProactiveResourceNameValues { 22 : public: 23 : // Overload action to stop accepting new HTTP requests. 24 : const std::string GlobalDownstreamMaxConnections = 25 : "envoy.resource_monitors.global_downstream_max_connections"; 26 : 27 : absl::flat_hash_map<std::string, OverloadProactiveResourceName> 28 : proactive_action_name_to_resource_ = { 29 : {GlobalDownstreamMaxConnections, 30 : OverloadProactiveResourceName::GlobalDownstreamMaxConnections}}; 31 : }; 32 : 33 : using OverloadProactiveResources = ConstSingleton<OverloadProactiveResourceNameValues>; 34 : 35 : /** 36 : * Tracks the state of an overload action. The state is a number between 0 and 1 that represents the 37 : * level of saturation. The values are categorized in two groups: 38 : * - Saturated (value = 1): indicates that an overload action is active because at least one of its 39 : * triggers has reached saturation. 40 : * - Scaling (0 <= value < 1): indicates that an overload action is not saturated. 41 : */ 42 : class OverloadActionState { 43 : public: 44 2155 : static constexpr OverloadActionState inactive() { return OverloadActionState(UnitFloat::min()); } 45 : 46 0 : static constexpr OverloadActionState saturated() { return OverloadActionState(UnitFloat::max()); } 47 : 48 2347 : explicit constexpr OverloadActionState(UnitFloat value) : action_value_(value) {} 49 : 50 1094 : UnitFloat value() const { return action_value_; } 51 0 : bool isSaturated() const { return action_value_.value() == UnitFloat::max().value(); } 52 : 53 : private: 54 : UnitFloat action_value_; 55 : }; 56 : 57 : /** 58 : * Callback invoked when an overload action changes state. 59 : */ 60 : using OverloadActionCb = std::function<void(OverloadActionState)>; 61 : 62 : /** 63 : * Thread-local copy of the state of each configured overload action. 64 : */ 65 : class ThreadLocalOverloadState : public ThreadLocal::ThreadLocalObject { 66 : public: 67 : // Get a thread-local reference to the value for the given action key. 68 : virtual const OverloadActionState& getState(const std::string& action) PURE; 69 : /** 70 : * Invokes the corresponding resource monitor to allocate resource for given resource monitor in 71 : * a thread safe manner. Returns true if there is enough resource quota available and allocation 72 : * has succeeded, false if allocation failed or resource is not registered. 73 : * @param name of corresponding resource monitor. 74 : * @param increment to add to current resource usage value within monitor. 75 : */ 76 : virtual bool tryAllocateResource(OverloadProactiveResourceName resource_name, 77 : int64_t increment) PURE; 78 : /** 79 : * Invokes the corresponding resource monitor to deallocate resource for given resource monitor in 80 : * a thread safe manner. Returns true if there is enough resource quota available and deallocation 81 : * has succeeded, false if deallocation failed or resource is not registered. 82 : * @param name of corresponding resource monitor. 83 : * @param decrement to subtract from current resource usage value within monitor. 84 : */ 85 : virtual bool tryDeallocateResource(OverloadProactiveResourceName resource_name, 86 : int64_t decrement) PURE; 87 : 88 : /** 89 : * Checks if resource monitor is registered and resource usage tracking is 90 : * enabled in overload manager. Returns true if resource monitor is registered, false otherwise. 91 : * @param name of resource monitor to check. 92 : */ 93 : virtual bool isResourceMonitorEnabled(OverloadProactiveResourceName resource_name) PURE; 94 : 95 : /** 96 : * Returns the proactive resource owned by the overload manager. 97 : * @param name of the proactive resource to retrieve. 98 : */ 99 : virtual ProactiveResourceMonitorOptRef 100 : getProactiveResourceMonitorForTest(OverloadProactiveResourceName resource_name) PURE; 101 : }; 102 : 103 : using ThreadLocalOverloadStateOptRef = OptRef<ThreadLocalOverloadState>; 104 : 105 : } // namespace Server 106 : } // namespace Envoy