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
4
  const std::string& resourceToName(OverloadProactiveResourceName resource) const {
33
4
    switch (resource) {
34
4
    case OverloadProactiveResourceName::GlobalDownstreamMaxConnections:
35
4
      return GlobalDownstreamMaxConnections;
36
4
    }
37
    PANIC_DUE_TO_CORRUPT_ENUM;
38
  }
39
};
40

            
41
using OverloadProactiveResources = ConstSingleton<OverloadProactiveResourceNameValues>;
42

            
43
/**
44
 * Tracks the state of an overload action. The state is a number between 0 and 1 that represents the
45
 * level of saturation. The values are categorized in two groups:
46
 * - Saturated (value = 1): indicates that an overload action is active because at least one of its
47
 *   triggers has reached saturation.
48
 * - Scaling (0 <= value < 1): indicates that an overload action is not saturated.
49
 */
50
class OverloadActionState {
51
public:
52
144930
  static constexpr OverloadActionState inactive() { return OverloadActionState(UnitFloat::min()); }
53

            
54
47572
  static constexpr OverloadActionState saturated() { return OverloadActionState(UnitFloat::max()); }
55

            
56
280727
  explicit constexpr OverloadActionState(UnitFloat value) : action_value_(value) {}
57

            
58
392806
  UnitFloat value() const { return action_value_; }
59
686
  bool isSaturated() const { return action_value_.value() == UnitFloat::max().value(); }
60

            
61
private:
62
  UnitFloat action_value_;
63
};
64

            
65
/**
66
 * Callback invoked when an overload action changes state.
67
 */
68
using OverloadActionCb = std::function<void(OverloadActionState)>;
69

            
70
/**
71
 * Thread-local copy of the state of each configured overload action.
72
 */
73
class ThreadLocalOverloadState : public ThreadLocal::ThreadLocalObject {
74
public:
75
  // Get a thread-local reference to the value for the given action key.
76
  virtual const OverloadActionState& getState(const std::string& action) PURE;
77
  /**
78
   * Invokes the corresponding resource monitor to allocate resource for given resource monitor in
79
   * a thread safe manner. Returns true if there is enough resource quota available and allocation
80
   * has succeeded, false if allocation failed or resource is not registered.
81
   * @param name of corresponding resource monitor.
82
   * @param increment to add to current resource usage value within monitor.
83
   */
84
  virtual bool tryAllocateResource(OverloadProactiveResourceName resource_name,
85
                                   int64_t increment) PURE;
86
  /**
87
   * Invokes the corresponding resource monitor to deallocate resource for given resource monitor in
88
   * a thread safe manner. Returns true if there is enough resource quota available and deallocation
89
   * has succeeded, false if deallocation failed or resource is not registered.
90
   * @param name of corresponding resource monitor.
91
   * @param decrement to subtract from current resource usage value within monitor.
92
   */
93
  virtual bool tryDeallocateResource(OverloadProactiveResourceName resource_name,
94
                                     int64_t decrement) PURE;
95

            
96
  /**
97
   * Checks if resource monitor is registered and resource usage tracking is
98
   * enabled in overload manager. Returns true if resource monitor is registered, false otherwise.
99
   * @param name of resource monitor to check.
100
   */
101
  virtual bool isResourceMonitorEnabled(OverloadProactiveResourceName resource_name) PURE;
102

            
103
  /**
104
   * Returns the proactive resource owned by the overload manager.
105
   * @param name of the proactive resource to retrieve.
106
   */
107
  virtual ProactiveResourceMonitorOptRef
108
  getProactiveResourceMonitorForTest(OverloadProactiveResourceName resource_name) PURE;
109
};
110

            
111
using ThreadLocalOverloadStateOptRef = OptRef<ThreadLocalOverloadState>;
112

            
113
} // namespace Server
114
} // namespace Envoy