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

Feature: add seconds limiter with unitest #168

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build --copt=-O2
#build --copt=-g --strip=never
build --jobs 16
#test --cache_test_results=no --test_output=errors
build --define trpc_include_overload_control=true
Binary file added docs/images/seconds_limiter_pic1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/seconds_limiter_pic2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/seconds_limiter_pic3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/helloworld/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cc_binary(
":helloworld_proto",
"@com_github_fmtlib_fmt//:fmtlib",
"@trpc_cpp//trpc/common:trpc_app",
"@trpc_cpp//trpc/overload_control/seconds_limiter:seconds_limiter_server_filter",
],
)

Expand Down
63 changes: 43 additions & 20 deletions examples/helloworld/conf/trpc_cpp_fiber.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,52 @@
#Global configuration (required)
global:
local_ip: 0.0.0.0 #Local IP, used for: not affecting the normal operation of the framework, used to obtain the local IP from the framework configuration.
threadmodel:
fiber: # Use Fiber(m:n coroutine) threadmodel
- instance_name: fiber_instance # Need to be unique if you config mutiple fiber threadmodel instances
# Fiber worker thread num
# If not specified, will use number of cores on the machine.
# In a Numa architecture, the workers will be automatically grouped (8-15 cores per group),
# while in Uma architecture, they will not be grouped.
concurrency_hint: 8
default:
# Separate model
- instance_name: default_instance
io_handle_type: separate
io_thread_num: 2 #Number of network I/O threads.
handle_thread_num: 2 #Number of business processing handle threads.

#Server configuration
server:
app: test
server: helloworld
admin_port: 8888 # Start server with admin service which can manage service
admin_ip: 0.0.0.0
service:
- name: trpc.test.helloworld.Greeter
protocol: trpc # Application layer protocol, eg: trpc/http/...
network: tcp # Network type, Support two types: tcp/udp
ip: 0.0.0.0 # Service bind ip
port: 12345 # Service bind port
app: test #Business name, such as: COS, CDB.
server: helloworld #Module name of the business
admin_port: 21111 # Admin port
admin_ip: 0.0.0.0 # Admin ip
service: #Business service, can have multiple.
- name: trpc.test.helloworld.Greeter #Service name, needs to be filled in according to the format, the first field is default to trpc, the second and third fields are the app and server configurations above, and the fourth field is the user-defined service_name.
network: tcp #Network listening type: for example: TCP, UDP.
ip: 0.0.0.0 #Listen ip
port: 12345 #Listen port
protocol: trpc #Service application layer protocol, for example: trpc, http.
accept_thread_num: 1 #Number of threads for binding ports.
filter:
- seconds_limiter

# Plugin configuration.
plugins:
log:
log: # Log configuration
default:
- name: default
min_level: 2 # 0-trace, 1-debug, 2-info, 3-warn, 4-error, 5-critical
format: "[%Y-%m-%d %H:%M:%S.%e] [thread %t] [%l] [%@] %v" # Output of all sinks in the log instance.
mode: 2 # 1-sync, 2-async, 3-fast
sinks:
local_file:
filename: helloworld_fiber.log
stdout: # Console output
min_level: 2
format: "[%Y-%m-%d %H:%M:%S.%e] [%l] %v"
mode: 1
local_file: # Local log file
filename: /trpc/test.log # The name of log file
overload_control:
seconds_limiter:
- service_name: trpc.test.helloworld.Greeter #service name.
is_report: true # Whether to report monitoring data.
service_limiter: default(20) #Service-level flow control limiter, standard format: name (maximum limit per second), empty for no limit.
window_size: 10
func_limiter: # Interface-level flow control.
- name: SayHello # Method name
limiter: seconds(10) # Interface-level flow control limiter, standard format: name (maximum limit per second), empty for no limit.
window_size: 10
10 changes: 10 additions & 0 deletions examples/helloworld/helloworld_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
#include "fmt/format.h"

#include "trpc/common/trpc_app.h"
#include "trpc/common/trpc_plugin.h"

#include "examples/helloworld/greeter_service.h"

#include "trpc/overload_control/seconds_limiter/seconds_limiter_server_filter.h"

namespace test {

namespace helloworld {
Expand All @@ -39,6 +42,13 @@ class HelloWorldServer : public ::trpc::TrpcApp {
}

void Destroy() override {}

int RegisterPlugins() {
// register server-side filter
auto server_filter = std::make_shared<trpc::overload_control::SecondsLimiterServerFilter>();
trpc::TrpcPlugin::GetInstance()->RegisterServerFilter(server_filter);
return 0;
}
};

} // namespace helloworld
Expand Down
14 changes: 8 additions & 6 deletions examples/helloworld/test/fiber_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,26 @@
DEFINE_string(client_config, "trpc_cpp.yaml", "framework client_config file, --client_config=trpc_cpp.yaml");
DEFINE_string(service_name, "trpc.test.helloworld.Greeter", "callee service name");

int DoRpcCall(const std::shared_ptr<::trpc::test::helloworld::GreeterServiceProxy>& proxy) {
int DoRpcCall(const std::shared_ptr<::trpc::test::helloworld::GreeterServiceProxy>& proxy, int idx) {
::trpc::ClientContextPtr client_ctx = ::trpc::MakeClientContext(proxy);
::trpc::test::helloworld::HelloRequest req;
req.set_msg("fiber");
req.set_msg("fiber,idx=" + std::to_string(idx));
::trpc::test::helloworld::HelloReply rsp;
::trpc::Status status = proxy->SayHello(client_ctx, req, &rsp);
if (!status.OK()) {
std::cerr << "get rpc error: " << status.ErrorMessage() << std::endl;
std::cerr <<idx<< " get rpc error: " << status.ErrorMessage() << std::endl;
return -1;
}
std::cout << "get rsp msg: " << rsp.msg() << std::endl;
std::cout <<idx<< " get rsp msg: " << rsp.msg() << std::endl;
return 0;
}

int Run() {
auto proxy = ::trpc::GetTrpcClient()->GetProxy<::trpc::test::helloworld::GreeterServiceProxy>(FLAGS_service_name);

return DoRpcCall(proxy);
for(int i = 1; i <= 30; i++){
DoRpcCall(proxy, i);
}
return 0;
}

void ParseClientConfig(int argc, char* argv[]) {
Expand Down
5 changes: 5 additions & 0 deletions test_seconds_limiter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
. build.sh
bazel build //examples/helloworld/...
./bazel-bin/examples/helloworld/helloworld_svr --config=./examples/helloworld/conf/trpc_cpp_fiber.yaml
#Start the client in a new terminal with this command:
#./bazel-bin/examples/helloworld/test/fiber_client --client_config=./examples/helloworld/test/conf/trpc_cpp_fiber.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void HighPercentilePriorityImpl::OnWindowExpires() {
curr_infos.tags[fmt::format("{}/quotient", strategies_[i]->Name())] = measured / expected;
}

curr_infos.tags["max_measured"] = std::max_element(measureds.begin(), measureds.end());
curr_infos.tags["max_measured"] = *std::max_element(measureds.begin(), measureds.end());
curr_infos.tags["max_concurrency"] = max_concurrency_;
curr_infos.tags["cur_concurrency"] = cur_concurrency_;

Expand Down
4 changes: 4 additions & 0 deletions trpc/overload_control/overload_control_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ constexpr char kOverloadctrlThrottler[] = "overloadctrl_throttler";
/// @brief Key for request priority in trpc framework.
constexpr char kTransinfoKeyTrpcPriority[] = "trpc-priority";

/// @brief Seconds limiter name.
constexpr char kSecondsLimiterName[] = "seconds_limiter";


} // namespace trpc::overload_control

#endif
148 changes: 148 additions & 0 deletions trpc/overload_control/seconds_limiter/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
licenses(["notice"])

package(default_visibility = ["//visibility:public"])

cc_library(
name = "seconds_limiter_conf",
srcs = ["seconds_limiter_conf.cc"],
hdrs = ["seconds_limiter_conf.h"],
defines = [] +
select({
"//trpc:trpc_include_overload_control": ["TRPC_BUILD_INCLUDE_OVERLOAD_CONTROL"],
"//conditions:default": [],
}),
visibility = ["//visibility:public"],
deps = [
"//trpc/common/config:trpc_config",
"//trpc/log:trpc_log",
"//trpc/overload_control:overload_control_defs",
"@com_github_jbeder_yaml_cpp//:yaml-cpp",
],
)
cc_test(
name = "seconds_limiter_conf_test",
srcs = ["seconds_limiter_conf_test.cc"],
deps = [
":seconds_limiter_conf",
"@com_google_googletest//:gtest_main",
],
)
cc_library(
name = "seconds_overload_controller",
srcs = ["seconds_overload_controller.cc"],
hdrs = ["seconds_overload_controller.h"],
defines = [] +
select({
"//trpc:trpc_include_overload_control": ["TRPC_BUILD_INCLUDE_OVERLOAD_CONTROL"],
"//conditions:default": [],
}),
visibility = [
"//visibility:public",
],
deps = [
"//trpc/overload_control/common:report",
"//trpc/util:time",
"//trpc/util/log:logging",
"//trpc/overload_control:server_overload_controller",
"//trpc/overload_control:overload_control_defs",
"//trpc/overload_control/flow_control:seconds_limiter"

],
)
cc_test(
name = "seconds_overload_controller_test",
srcs = ["seconds_overload_controller_test.cc"],
deps = [
":seconds_overload_controller",
"//trpc/codec/trpc:trpc_protocol",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)
cc_library(
name = "seconds_overload_controller_factory",
hdrs = ["seconds_overload_controller_factory.h"],
defines = [] +
select({
"//trpc:trpc_include_overload_control": ["TRPC_BUILD_INCLUDE_OVERLOAD_CONTROL"],
"//conditions:default": [],
}),
visibility = [
"//visibility:public",
],
deps = [
"//trpc/overload_control/common:overload_control_factory",
"//trpc/overload_control:server_overload_controller",
],
)
cc_library(
name = "seconds_limiter_generator",
srcs = ["seconds_limiter_generator.cc"],
hdrs = ["seconds_limiter_generator.h"],
defines = [] +
select({
"//trpc:trpc_include_overload_control": ["TRPC_BUILD_INCLUDE_OVERLOAD_CONTROL"],
"//conditions:default": [],
}),
visibility = [
"//visibility:public",
],
deps = [
":seconds_overload_controller",
":seconds_limiter_conf",
":seconds_overload_controller_factory",
"//trpc/common/logging:trpc_logging",
"//trpc/util:function",
"//trpc/util:string_util",
],
)
cc_test(
name = "seconds_limiter_generator_test",
srcs = ["seconds_limiter_generator_test.cc"],
deps = [
":seconds_overload_controller",
":seconds_overload_controller_factory",
":seconds_limiter_generator",
":seconds_limiter_conf",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)
cc_library(
name = "seconds_limiter_server_filter",
srcs = ["seconds_limiter_server_filter.cc"],
hdrs = ["seconds_limiter_server_filter.h"],
defines = [] +
select({
"//trpc:trpc_include_overload_control": ["TRPC_BUILD_INCLUDE_OVERLOAD_CONTROL"],
"//conditions:default": [],
}),
visibility = [
"//visibility:public",
],
deps = [
":seconds_limiter_conf",
":seconds_overload_controller_factory",
":seconds_limiter_generator",
"//trpc/filter",
"//trpc/filter:filter_manager",
"//trpc/log:trpc_log",
"//trpc/server:server_context",
"//trpc/util:likely",
],
)
cc_test(
name = "seconds_limiter_server_filter_test",
srcs = ["seconds_limiter_server_filter_test.cc"],
data = [
"//trpc/overload_control/seconds_limiter:filter_test.yaml",
],
deps = [
"//trpc/common:trpc_plugin",
":seconds_overload_controller_factory",
":seconds_limiter_server_filter",
":seconds_overload_controller",
"//trpc/codec/testing:protocol_testing",
"@com_google_googletest//:gtest_main",
],
)
Loading
Loading