From b0c27c74fd2fed880d05993df12cdfb3e85587e5 Mon Sep 17 00:00:00 2001 From: William Yang Date: Mon, 11 Oct 2021 20:17:19 +0200 Subject: [PATCH] feat(olp): bump lc to 0.1.1 --- apps/emqx/rebar.config | 2 +- apps/emqx/src/emqx_olp.erl | 13 ++++++++++-- apps/emqx/test/emqx_olp_SUITE.erl | 35 ++++++++++++++++++++++++------- rebar.config | 2 +- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/apps/emqx/rebar.config b/apps/emqx/rebar.config index 5c21b80e5..d388e6803 100644 --- a/apps/emqx/rebar.config +++ b/apps/emqx/rebar.config @@ -9,7 +9,7 @@ %% This rebar.config is necessary because the app may be used as a %% `git_subdir` dependency in other projects. {deps, - [ {lc, {git, "https://github.com/qzhuyan/lc.git", {tag, "0.1.0"}}} + [ {lc, {git, "https://github.com/qzhuyan/lc.git", {tag, "0.1.1"}}} , {gproc, {git, "https://github.com/uwiger/gproc", {tag, "0.8.0"}}} , {typerefl, {git, "https://github.com/k32/typerefl", {tag, "0.8.5"}}} , {jiffy, {git, "https://github.com/emqx/jiffy", {tag, "1.0.5"}}} diff --git a/apps/emqx/src/emqx_olp.erl b/apps/emqx/src/emqx_olp.erl index d3f93f455..101064bb6 100644 --- a/apps/emqx/src/emqx_olp.erl +++ b/apps/emqx/src/emqx_olp.erl @@ -15,6 +15,8 @@ %%-------------------------------------------------------------------- -module(emqx_olp). +-include_lib("lc/include/lc.hrl"). + -export([ is_overloaded/0 , backoff/1 , backoff_gc/1 @@ -100,9 +102,16 @@ off() -> load_ctl:stop_runq_flagman(5000). %% @doc turn on backgroud runq check. --spec on() -> {ok, pid()} | {error, running | restarting}. +-spec on() -> {ok, pid()} | {error, running | restarting | disabled}. on() -> - load_ctl:restart_runq_flagman(). + case load_ctl:restart_runq_flagman() of + {error, disabled} -> + OldCfg = load_ctl:get_config(), + ok = load_ctl:put_config(OldCfg#{ ?RUNQ_MON_F0 => true }), + load_ctl:restart_runq_flagman(); + Other -> + Other + end. %%% Internals -spec do_check(Zone::atom(), cfg_key(), cnt_name()) -> boolean(). diff --git a/apps/emqx/test/emqx_olp_SUITE.erl b/apps/emqx/test/emqx_olp_SUITE.erl index 58777c2e2..0096765b9 100644 --- a/apps/emqx/test/emqx_olp_SUITE.erl +++ b/apps/emqx/test/emqx_olp_SUITE.erl @@ -35,11 +35,20 @@ end_per_suite(_Config) -> init_per_testcase(_, Config) -> emqx_olp:on(), - ok = load_ctl:put_config(#{ ?RUNQ_MON_T1 => 200 - , ?RUNQ_MON_T2 => 50 - , ?RUNQ_MON_C1 => 3 - }), - Config. + case wait_for(fun() -> lc_sup:whereis_runq_flagman() end, 10) of + true -> ok; + false -> + ct:fail("runq_flagman is not up") + end, + ok = load_ctl:put_config(#{ ?RUNQ_MON_F0 => true + , ?RUNQ_MON_F1 => 5 + , ?RUNQ_MON_F2 => 1 + , ?RUNQ_MON_T1 => 200 + , ?RUNQ_MON_T2 => 50 + , ?RUNQ_MON_C1 => 2 + , ?RUNQ_MON_F5 => -1 + }), + Config. %% Test that olp could be enabled/disabled globally t_off_on(_Config) -> @@ -47,15 +56,16 @@ t_off_on(_Config) -> ok = emqx_olp:off(), ?assert(not is_process_alive(Old)), {ok, Pid} = emqx_olp:on(), + timer:sleep(1000), ?assert(is_process_alive(Pid)). %% Test that overload detection works t_is_overloaded(_Config) -> P = burst_runq(), - timer:sleep(2000), + timer:sleep(3000), ?assert(emqx_olp:is_overloaded()), exit(P, kill), - timer:sleep(2000), + timer:sleep(3000), ?assert(not emqx_olp:is_overloaded()). %% Test that new conn is rejected when olp is enabled @@ -95,3 +105,14 @@ worker_parent(N, {M, F, A}) -> busy_loop() -> erlang:yield(), busy_loop(). + +wait_for(_Fun, 0) -> + false; +wait_for(Fun, Retry) -> + case is_pid(Fun()) of + true -> + true; + false -> + timer:sleep(10), + wait_for(Fun, Retry - 1) + end. diff --git a/rebar.config b/rebar.config index 200b892fe..2829095b0 100644 --- a/rebar.config +++ b/rebar.config @@ -42,7 +42,7 @@ {post_hooks,[]}. {deps, - [ {lc, {git, "https://github.com/qzhuyan/lc.git", {tag, "0.1.0"}}} + [ {lc, {git, "https://github.com/qzhuyan/lc.git", {tag, "0.1.1"}}} , {gpb, "4.11.2"} %% gpb only used to build, but not for release, pin it here to avoid fetching a wrong version due to rebar plugins scattered in all the deps , {typerefl, {git, "https://github.com/k32/typerefl", {tag, "0.8.5"}}} , {ehttpc, {git, "https://github.com/emqx/ehttpc", {tag, "0.1.9"}}}