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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keep percentage type's range is 0%~100% #11271

Merged
merged 2 commits into from
Jul 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions apps/emqx/src/emqx_schema.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2496,7 +2496,11 @@ to_integer(Str) ->
end.

to_percent(Str) ->
{ok, hocon_postprocess:percent(Str)}.
Percent = hocon_postprocess:percent(Str),
case is_number(Percent) andalso Percent >= 0.0 andalso Percent =< 1.0 of
true -> {ok, Percent};
false -> {error, Str}
end.

to_comma_separated_list(Str) ->
{ok, string:tokens(Str, ", ")}.
Expand Down Expand Up @@ -2724,15 +2728,17 @@ check_cpu_watermark(Conf) ->
check_watermark("sysmon.os.cpu_low_watermark", "sysmon.os.cpu_high_watermark", Conf).

check_watermark(LowKey, HighKey, Conf) ->
case hocon_maps:get(LowKey, Conf) of
undefined ->
case to_percent(hocon_maps:get(LowKey, Conf)) of
{error, undefined} ->
true;
Low ->
High = hocon_maps:get(HighKey, Conf),
case Low < High of
true -> true;
false -> {bad_watermark, #{LowKey => Low, HighKey => High}}
end
{ok, Low} ->
case to_percent(hocon_maps:get(HighKey, Conf)) of
{ok, High} when High > Low -> true;
{ok, High} -> {bad_watermark, #{LowKey => Low, HighKey => High}};
{error, HighVal} -> {bad_watermark, #{HighKey => HighVal}}
end;
{error, Low} ->
{bad_watermark, #{LowKey => Low}}
end.

str(A) when is_atom(A) ->
Expand Down
2 changes: 1 addition & 1 deletion apps/emqx_conf/src/emqx_conf_cli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ load_etc_config_file() ->

filter_readonly_config(Raw) ->
SchemaMod = emqx_conf:schema_module(),
RawDefault = fill_defaults(Raw),
try
RawDefault = fill_defaults(Raw),
_ = emqx_config:check_config(SchemaMod, RawDefault),
ReadOnlyKeys = [atom_to_binary(K) || K <- ?READONLY_KEYS],
{ok, maps:without(ReadOnlyKeys, Raw)}
Expand Down
1 change: 1 addition & 0 deletions changes/ce/fix-11271.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure that the range of percentage type is from 0% to 100%.