Skip to content
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

server: split thread_local_object.h and overload_manager.h #13999

Merged
merged 2 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/envoy/runtime/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ envoy_cc_library(
],
deps = [
"//include/envoy/stats:stats_interface",
"//include/envoy/thread_local:thread_local_interface",
"//include/envoy/thread_local:thread_local_object",
"//source/common/common:assert_lib",
"//source/common/singleton:threadsafe_singleton",
"@envoy_api//envoy/type/v3:pkg_cc_proto",
Expand Down
2 changes: 1 addition & 1 deletion include/envoy/runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "envoy/common/pure.h"
#include "envoy/stats/store.h"
#include "envoy/thread_local/thread_local.h"
#include "envoy/thread_local/thread_local_object.h"
#include "envoy/type/v3/percent.pb.h"

#include "common/common/assert.h"
Expand Down
18 changes: 4 additions & 14 deletions include/envoy/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ envoy_cc_library(
"//include/envoy/local_info:local_info_interface",
"//include/envoy/runtime:runtime_interface",
"//include/envoy/secret:secret_manager_interface",
"//include/envoy/server:overload_manager_interface",
"//include/envoy/server/overload:overload_manager_interface",
"//include/envoy/ssl:context_manager_interface",
"//include/envoy/thread_local:thread_local_interface",
"//include/envoy/tracing:http_tracer_interface",
Expand All @@ -151,8 +151,8 @@ envoy_cc_library(
name = "worker_interface",
hdrs = ["worker.h"],
deps = [
":overload_manager_interface",
"//include/envoy/server:guarddog_interface",
"//include/envoy/server/overload:overload_manager_interface",
],
)

Expand Down Expand Up @@ -183,7 +183,7 @@ envoy_cc_library(
"//include/envoy/local_info:local_info_interface",
"//include/envoy/network:drain_decision_interface",
"//include/envoy/runtime:runtime_interface",
"//include/envoy/server:overload_manager_interface",
"//include/envoy/server/overload:overload_manager_interface",
"//include/envoy/singleton:manager_interface",
"//include/envoy/thread_local:thread_local_interface",
"//include/envoy/tracing:http_tracer_interface",
Expand All @@ -207,8 +207,8 @@ envoy_cc_library(
"//include/envoy/config:typed_config_interface",
"//include/envoy/http:codes_interface",
"//include/envoy/http:filter_interface",
"//include/envoy/server:overload_manager_interface",
"//include/envoy/server:transport_socket_config_interface",
"//include/envoy/server/overload:overload_manager_interface",
"//include/envoy/singleton:manager_interface",
"//include/envoy/thread_local:thread_local_interface",
"//include/envoy/tracing:http_tracer_interface",
Expand Down Expand Up @@ -302,16 +302,6 @@ envoy_cc_library(
],
)

envoy_cc_library(
name = "overload_manager_interface",
hdrs = ["overload_manager.h"],
deps = [
"//include/envoy/event:timer_interface",
"//include/envoy/thread_local:thread_local_interface",
"//source/common/singleton:const_singleton",
],
)

envoy_cc_library(
name = "tracer_config_interface",
hdrs = ["tracer_config.h"],
Expand Down
2 changes: 1 addition & 1 deletion include/envoy/server/factory_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "envoy/server/admin.h"
#include "envoy/server/drain_manager.h"
#include "envoy/server/lifecycle_notifier.h"
#include "envoy/server/overload_manager.h"
#include "envoy/server/overload/overload_manager.h"
#include "envoy/server/process_context.h"
#include "envoy/singleton/manager.h"
#include "envoy/stats/scope.h"
Expand Down
2 changes: 1 addition & 1 deletion include/envoy/server/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "envoy/server/lifecycle_notifier.h"
#include "envoy/server/listener_manager.h"
#include "envoy/server/options.h"
#include "envoy/server/overload_manager.h"
#include "envoy/server/overload/overload_manager.h"
#include "envoy/ssl/context_manager.h"
#include "envoy/thread_local/thread_local.h"
#include "envoy/tracing/http_tracer.h"
Expand Down
29 changes: 29 additions & 0 deletions include/envoy/server/overload/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load(
"//bazel:envoy_build_system.bzl",
"envoy_cc_library",
"envoy_package",
)

licenses(["notice"]) # Apache 2

envoy_package()

envoy_cc_library(
name = "overload_manager_interface",
hdrs = ["overload_manager.h"],
deps = [
":thread_local_overload_state",
"//include/envoy/event:dispatcher_interface",
"//include/envoy/thread_local:thread_local_interface",
"//source/common/singleton:const_singleton",
],
)

envoy_cc_library(
name = "thread_local_overload_state",
hdrs = ["thread_local_overload_state.h"],
deps = [
"//include/envoy/event:timer_interface",
"//include/envoy/thread_local:thread_local_object",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,13 @@
#include <string>

#include "envoy/common/pure.h"
#include "envoy/event/timer.h"
#include "envoy/thread_local/thread_local.h"
#include "envoy/event/dispatcher.h"
#include "envoy/server/overload/thread_local_overload_state.h"

#include "common/common/macros.h"
#include "common/singleton/const_singleton.h"

namespace Envoy {
namespace Server {

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

static constexpr OverloadActionState saturated() { return OverloadActionState(1.0); }

explicit constexpr OverloadActionState(float value)
: action_value_(std::min(1.0f, std::max(0.0f, value))) {}

float value() const { return action_value_; }
bool isSaturated() const { return action_value_ == 1; }

private:
float action_value_;
};

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

enum class OverloadTimerType {
// Timers created with this type will never be scaled. This should only be used for testing.
UnscaledRealTimerForTest,
// The amount of time an HTTP connection to a downstream client can remain idle (no streams). This
// corresponds to the HTTP_DOWNSTREAM_CONNECTION_IDLE TimerType in overload.proto.
HttpDownstreamIdleConnectionTimeout,
};

/**
* Thread-local copy of the state of each configured overload action.
*/
class ThreadLocalOverloadState : public ThreadLocal::ThreadLocalObject {
public:
// Get a thread-local reference to the value for the given action key.
virtual const OverloadActionState& getState(const std::string& action) PURE;

// Get a scaled timer whose minimum corresponds to the configured value for the given timer type.
virtual Event::TimerPtr createScaledTimer(OverloadTimerType timer_type,
Event::TimerCb callback) PURE;
};

/**
* Well-known overload action names.
*/
Expand Down
63 changes: 63 additions & 0 deletions include/envoy/server/overload/thread_local_overload_state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

#pragma once

#include <string>

#include "envoy/common/pure.h"
#include "envoy/event/timer.h"
#include "envoy/thread_local/thread_local_object.h"

namespace Envoy {
namespace Server {

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

static constexpr OverloadActionState saturated() { return OverloadActionState(1.0); }

explicit constexpr OverloadActionState(float value)
: action_value_(std::min(1.0f, std::max(0.0f, value))) {}

float value() const { return action_value_; }
bool isSaturated() const { return action_value_ == 1; }

private:
float action_value_;
};

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

enum class OverloadTimerType {
// Timers created with this type will never be scaled. This should only be used for testing.
UnscaledRealTimerForTest,
// The amount of time an HTTP connection to a downstream client can remain idle (no streams). This
// corresponds to the HTTP_DOWNSTREAM_CONNECTION_IDLE TimerType in overload.proto.
HttpDownstreamIdleConnectionTimeout,
};

/**
* Thread-local copy of the state of each configured overload action.
*/
class ThreadLocalOverloadState : public ThreadLocal::ThreadLocalObject {
public:
// Get a thread-local reference to the value for the given action key.
virtual const OverloadActionState& getState(const std::string& action) PURE;

// Get a scaled timer whose minimum corresponds to the configured value for the given timer type.
virtual Event::TimerPtr createScaledTimer(OverloadTimerType timer_type,
Event::TimerCb callback) PURE;
};

} // namespace Server
} // namespace Envoy
2 changes: 1 addition & 1 deletion include/envoy/server/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <functional>

#include "envoy/server/guarddog.h"
#include "envoy/server/overload_manager.h"
#include "envoy/server/overload/overload_manager.h"

namespace Envoy {
namespace Server {
Expand Down
12 changes: 11 additions & 1 deletion include/envoy/thread_local/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,15 @@ envoy_package()
envoy_cc_library(
name = "thread_local_interface",
hdrs = ["thread_local.h"],
deps = ["//include/envoy/event:dispatcher_interface"],
deps = [
":thread_local_object",
"//include/envoy/event:dispatcher_interface",
"//source/common/common:assert_lib",
],
)

envoy_cc_library(
name = "thread_local_object",
hdrs = ["thread_local_object.h"],
deps = ["//source/common/common:assert_lib"],
)
23 changes: 3 additions & 20 deletions include/envoy/thread_local/thread_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,13 @@
#include "envoy/common/optref.h"
#include "envoy/common/pure.h"
#include "envoy/event/dispatcher.h"
#include "envoy/thread_local/thread_local_object.h"

#include "common/common/assert.h"

namespace Envoy {
namespace ThreadLocal {

/**
* All objects that are stored via the ThreadLocal interface must derive from this type.
*/
class ThreadLocalObject {
public:
virtual ~ThreadLocalObject() = default;

/**
* Return the object casted to a concrete type. See getTyped() below for comments on the casts.
*/
template <class T> T& asType() {
ASSERT(dynamic_cast<T*>(this) != nullptr);
return *static_cast<T*>(this);
}
};

using ThreadLocalObjectSharedPtr = std::shared_ptr<ThreadLocalObject>;

template <class T = ThreadLocalObject> class TypedSlot;

/**
* An individual allocated TLS slot. When the slot is destroyed the stored thread local will
* be freed on each thread.
Expand Down
33 changes: 33 additions & 0 deletions include/envoy/thread_local/thread_local_object.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <cstdint>
#include <functional>
#include <memory>

#include "common/common/assert.h"

namespace Envoy {
namespace ThreadLocal {

/**
* All objects that are stored via the ThreadLocal interface must derive from this type.
*/
class ThreadLocalObject {
public:
virtual ~ThreadLocalObject() = default;

/**
* Return the object casted to a concrete type. See getTyped() below for comments on the casts.
*/
template <class T> T& asType() {
ASSERT(dynamic_cast<T*>(this) != nullptr);
return *static_cast<T*>(this);
}
};

using ThreadLocalObjectSharedPtr = std::shared_ptr<ThreadLocalObject>;

template <class T = ThreadLocalObject> class TypedSlot;

} // namespace ThreadLocal
} // namespace Envoy
2 changes: 1 addition & 1 deletion source/common/grpc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ envoy_cc_library(
"//include/envoy/api:api_interface",
"//include/envoy/grpc:google_grpc_creds_interface",
"//include/envoy/thread:thread_interface",
"//include/envoy/thread_local:thread_local_interface",
"//include/envoy/thread_local:thread_local_object",
"//source/common/common:base64_lib",
"//source/common/common:empty_string",
"//source/common/common:linked_object",
Expand Down
2 changes: 1 addition & 1 deletion source/common/grpc/google_async_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "envoy/grpc/async_client.h"
#include "envoy/stats/scope.h"
#include "envoy/thread/thread.h"
#include "envoy/thread_local/thread_local.h"
#include "envoy/thread_local/thread_local_object.h"
#include "envoy/tracing/http_tracer.h"

#include "common/common/linked_object.h"
Expand Down
2 changes: 1 addition & 1 deletion source/common/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ envoy_cc_library(
"//include/envoy/router:rds_interface",
"//include/envoy/router:scopes_interface",
"//include/envoy/runtime:runtime_interface",
"//include/envoy/server:overload_manager_interface",
"//include/envoy/server/overload:overload_manager_interface",
"//include/envoy/ssl:connection_interface",
"//include/envoy/stats:stats_interface",
"//include/envoy/stats:stats_macros",
Expand Down
2 changes: 1 addition & 1 deletion source/common/http/conn_manager_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "envoy/router/rds.h"
#include "envoy/router/scopes.h"
#include "envoy/runtime/runtime.h"
#include "envoy/server/overload_manager.h"
#include "envoy/server/overload/overload_manager.h"
#include "envoy/ssl/connection.h"
#include "envoy/stats/scope.h"
#include "envoy/stats/stats_macros.h"
Expand Down
2 changes: 1 addition & 1 deletion source/common/memory/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ envoy_cc_library(
deps = [
":utils_lib",
"//include/envoy/event:dispatcher_interface",
"//include/envoy/server:overload_manager_interface",
"//include/envoy/server/overload:overload_manager_interface",
"//include/envoy/stats:stats_interface",
"//source/common/stats:symbol_table_lib",
],
Expand Down
2 changes: 1 addition & 1 deletion source/common/memory/heap_shrinker.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "envoy/event/dispatcher.h"
#include "envoy/server/overload_manager.h"
#include "envoy/server/overload/overload_manager.h"
#include "envoy/stats/scope.h"
#include "envoy/stats/stats.h"

Expand Down
Loading