-
Notifications
You must be signed in to change notification settings - Fork 23
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
Interarrival as throttle #186
Open
NelsonVides
wants to merge
5
commits into
master
Choose a base branch
from
interarrival_as_throttle
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2ee8986
Pattern match on the right return value for start_scenario
NelsonVides 54549af
Set user interarrival as a user_rate throttle
NelsonVides d865393
Redistribute user_rate between user sup and user workers
NelsonVides 8886183
Unify duplicated function
NelsonVides acfdabc
Review
NelsonVides File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,40 +8,46 @@ | |
-behaviour(gen_server). | ||
|
||
-define(SERVER, ?MODULE). | ||
-define(DEFAULT_USER_RATE, 1200). | ||
|
||
-required_variable(#{name => interarrival, default_value => 50, | ||
verification => {?MODULE, non_neg_integer, 1}, | ||
description => "a delay between creating the processes for two " | ||
"consecutive users (ms, def: 50ms)", | ||
update => {?MODULE, maybe_update_interarrival_timer, 2}}). | ||
-required_variable(#{name => user_rate, default_value => ?DEFAULT_USER_RATE, | ||
verification => {?MODULE, verify_user_rate, 1}, | ||
description => "Throttle rate for the Scenario:start/1,2 callback", | ||
update => {?MODULE, update_user_rate, 2}}). | ||
|
||
-record(state, {scenario :: amoc:scenario() | undefined, | ||
status = idle :: status(), | ||
last_user_id = 0 :: last_user_id(), | ||
status = idle :: idle | running | terminating | finished | | ||
{error, any()} | disabled, | ||
scenario_state :: any(), %% state returned from Scenario:init/0 | ||
create_users = [] :: [amoc_scenario:user_id()], | ||
tref :: timer:tref() | undefined}). | ||
scenario_state :: amoc_scenario:state() %% state returned from Scenario:init/0 | ||
}). | ||
|
||
-type status() :: idle | running | terminating | finished | {error, any()} | disabled. | ||
%% Scenario status. | ||
|
||
-type state() :: #state{}. | ||
%% Internal state of the node's controller | ||
|
||
-type handle_call_res() :: ok | {ok, term()} | {error, term()}. | ||
-type running_status() :: #{scenario := amoc:scenario(), | ||
currently_running_users := user_count(), | ||
highest_user_id := last_user_id()}. | ||
%% Details about the scenario currently running | ||
|
||
-type amoc_status() :: idle | | ||
{running, running_status()} | | ||
{terminating, amoc:scenario()} | | ||
{finished, amoc:scenario()} | | ||
{error, any()} | | ||
disabled. | ||
%% Status of the node, note that amoc_controller is disabled for the master node | ||
|
||
-type user_count() :: non_neg_integer(). | ||
%% Number of users currently running in the node | ||
|
||
-type last_user_id() :: non_neg_integer(). | ||
%% Highest user id registered in the node | ||
-type interarrival() :: non_neg_integer(). | ||
|
||
-type user_rate() :: amoc_throttle:rate(). | ||
%% Time to wait in between spawning new users | ||
|
||
%% ------------------------------------------------------------------ | ||
|
@@ -65,14 +71,14 @@ | |
%% ------------------------------------------------------------------ | ||
%% Parameters verification functions | ||
%% ------------------------------------------------------------------ | ||
-export([maybe_update_interarrival_timer/2, non_neg_integer/1]). | ||
-export([update_user_rate/2, verify_user_rate/1]). | ||
|
||
-export([zero_users_running/0]). | ||
-export([wait_user_rate/0, zero_users_running/0]). | ||
|
||
%% ------------------------------------------------------------------ | ||
%% gen_server Function Exports | ||
%% ------------------------------------------------------------------ | ||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2]). | ||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]). | ||
|
||
%% ------------------------------------------------------------------ | ||
%% API Function Definitions | ||
|
@@ -122,14 +128,16 @@ disable() -> | |
gen_server:call(?SERVER, disable). | ||
|
||
%% @private | ||
-spec non_neg_integer(any()) -> boolean(). | ||
non_neg_integer(Interarrival) -> | ||
is_integer(Interarrival) andalso Interarrival >= 0. | ||
-spec verify_user_rate(any()) -> boolean(). | ||
verify_user_rate(UserRate) -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use braces to make expression more readable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
(infinity =:= UserRate) | ||
orelse is_integer(UserRate) | ||
andalso (0 =< UserRate). | ||
|
||
%% @private | ||
-spec maybe_update_interarrival_timer(interarrival, term()) -> ok. | ||
maybe_update_interarrival_timer(interarrival, _) -> | ||
gen_server:cast(?SERVER, maybe_update_interarrival_timer). | ||
-spec update_user_rate(user_rate, user_rate()) -> ok. | ||
update_user_rate(user_rate, UserRate) -> | ||
ok = amoc_throttle:change_rate(user_rate, #{rate => UserRate}). | ||
|
||
%% @private | ||
-spec zero_users_running() -> ok. | ||
|
@@ -180,8 +188,6 @@ handle_call(_Request, _From, State) -> | |
|
||
%% @private | ||
-spec handle_cast(any(), state()) -> {noreply, state()}. | ||
handle_cast(maybe_update_interarrival_timer, State) -> | ||
{noreply, maybe_update_interarrival_timer(State)}; | ||
handle_cast(zero_users_running, State) -> | ||
NewSate = handle_zero_users_running(State), | ||
{noreply, NewSate}; | ||
|
@@ -190,15 +196,14 @@ handle_cast(_Msg, State) -> | |
|
||
%% @private | ||
-spec handle_info(any(), state()) -> {noreply, state()}. | ||
handle_info(start_user, State) -> | ||
NewSate = handle_start_user(State), | ||
{noreply, NewSate}; | ||
handle_info(start_all_users, State) -> | ||
NewSate = handle_start_all_users(State), | ||
{noreply, NewSate}; | ||
handle_info(_Msg, State) -> | ||
{noreply, State}. | ||
|
||
%% @private | ||
-spec terminate(term(), state()) -> any(). | ||
terminate(_Reason, _State) -> | ||
amoc_users_sup:terminate_all_children(). | ||
|
||
%% ------------------------------------------------------------------ | ||
%% internal functions | ||
%% ------------------------------------------------------------------ | ||
|
@@ -243,18 +248,15 @@ handle_update_settings(_Settings, #state{status = Status}) -> | |
|
||
-spec handle_add(amoc_scenario:user_id(), amoc_scenario:user_id(), state()) -> | ||
{handle_call_res(), state()}. | ||
handle_add(StartId, EndId, #state{last_user_id = LastId, | ||
create_users = ScheduledUsers, | ||
status = running, | ||
handle_add(StartId, EndId, #state{status = running, | ||
last_user_id = LastId, | ||
scenario = Scenario, | ||
tref = TRef} = State) when StartId =< EndId, | ||
LastId < StartId -> | ||
scenario_state = ScenarioState} = State) | ||
when StartId =< EndId, LastId < StartId -> | ||
amoc_telemetry:execute([controller, users], #{count => EndId - StartId + 1}, | ||
#{scenario => Scenario, type => add}), | ||
NewUsers = lists:seq(StartId, EndId), | ||
NewScheduledUsers = lists:append(ScheduledUsers, NewUsers), | ||
NewTRef = maybe_start_timer(TRef), | ||
{ok, State#state{create_users = NewScheduledUsers, tref = NewTRef, last_user_id = EndId}}; | ||
amoc_users_sup:start_children(Scenario, {StartId, EndId}, ScenarioState), | ||
{ok, State#state{last_user_id = EndId}}; | ||
handle_add(_StartId, _EndId, #state{status = running} = State) -> | ||
{{error, invalid_range}, State}; | ||
handle_add(_StartId, _EndId, #state{status = Status} = State) -> | ||
|
@@ -287,23 +289,6 @@ handle_disable(#state{status = idle} = State) -> | |
handle_disable(#state{status = Status} = State) -> | ||
{{error, {invalid_status, Status}}, State}. | ||
|
||
-spec handle_start_user(state()) -> state(). | ||
handle_start_user(#state{create_users = [UserId | T], | ||
scenario = Scenario, | ||
scenario_state = ScenarioState} = State) -> | ||
amoc_users_sup:start_child(Scenario, UserId, ScenarioState), | ||
State#state{create_users = T}; | ||
handle_start_user(#state{create_users = [], tref = TRef} = State) -> | ||
State#state{tref = maybe_stop_timer(TRef)}. | ||
|
||
-spec handle_start_all_users(state()) -> state(). | ||
handle_start_all_users(#state{create_users = AllUsers, | ||
scenario = Scenario, | ||
scenario_state = ScenarioState, | ||
tref = TRef} = State) -> | ||
amoc_users_sup:start_children(Scenario, AllUsers, ScenarioState), | ||
State#state{create_users = [], tref = maybe_stop_timer(TRef)}. | ||
|
||
%% ------------------------------------------------------------------ | ||
%% helpers | ||
%% ------------------------------------------------------------------ | ||
|
@@ -316,12 +301,15 @@ start_tables() -> %% ETS creation | |
{ok | error, any()}. | ||
init_scenario(Scenario, Settings) -> | ||
case amoc_config_scenario:parse_scenario_settings(Scenario, Settings) of | ||
ok -> amoc_scenario:init(Scenario); | ||
ok -> | ||
start_user_rate(), | ||
amoc_scenario:init(Scenario); | ||
{error, Type, Reason} -> {error, {Type, Reason}} | ||
end. | ||
|
||
-spec terminate_scenario(state()) -> ok | {ok, any()} | {error, any()}. | ||
terminate_scenario(#state{scenario = Scenario, scenario_state = ScenarioState}) -> | ||
stop_user_rate(), | ||
amoc_scenario:terminate(Scenario, ScenarioState). | ||
|
||
-spec handle_zero_users_running(state()) -> state(). | ||
|
@@ -331,35 +319,20 @@ handle_zero_users_running(#state{status = terminating} = State) -> | |
handle_zero_users_running(State) -> | ||
State. | ||
|
||
-spec maybe_stop_timer(timer:tref() | undefined) -> undefined. | ||
maybe_stop_timer(undefined) -> | ||
undefined; | ||
maybe_stop_timer(TRef) -> | ||
{ok, cancel} = timer:cancel(TRef), | ||
undefined. | ||
|
||
-spec get_interarrival() -> interarrival(). | ||
get_interarrival() -> | ||
amoc_config:get(interarrival). | ||
|
||
-spec maybe_update_interarrival_timer(state()) -> state(). | ||
maybe_update_interarrival_timer(#state{tref = undefined} = State) -> | ||
State; | ||
maybe_update_interarrival_timer(#state{tref = TRef} = State) -> | ||
{ok, cancel} = timer:cancel(TRef), | ||
Value = get_interarrival(), | ||
NewTRef = do_interarrival(Value), | ||
State#state{tref = NewTRef}. | ||
|
||
-spec maybe_start_timer(timer:tref() | undefined) -> timer:tref(). | ||
maybe_start_timer(undefined) -> | ||
Value = get_interarrival(), | ||
do_interarrival(Value); | ||
maybe_start_timer(TRef) -> TRef. | ||
|
||
do_interarrival(0) -> | ||
self() ! start_all_users, | ||
undefined; | ||
do_interarrival(Value) -> | ||
{ok, NewTRef} = timer:send_interval(Value, start_user), | ||
NewTRef. | ||
-spec get_user_rate() -> user_rate(). | ||
get_user_rate() -> | ||
amoc_config:get(user_rate, ?DEFAULT_USER_RATE). | ||
|
||
-spec wait_user_rate() -> boolean(). | ||
wait_user_rate() -> | ||
0 =:= get_user_rate() | ||
orelse ok =:= amoc_throttle:wait(user_rate). | ||
|
||
-spec start_user_rate() -> any(). | ||
start_user_rate() -> | ||
UserRate = get_user_rate(), | ||
amoc_throttle:start(user_rate, #{rate => UserRate}). | ||
|
||
-spec stop_user_rate() -> any(). | ||
stop_user_rate() -> | ||
amoc_throttle:stop(user_rate). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is
t:amoc_throttle:rate/0
?