From 1845e472e0d2e2d852b42b901606edd9ffd84836 Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Fri, 4 Nov 2022 15:53:36 +0800 Subject: [PATCH 1/3] fix: create trace sometime failed by end_at time has already passed --- apps/emqx/src/emqx_trace/emqx_trace.erl | 15 +++++++++------ apps/emqx/src/emqx_trace/emqx_trace_dl.erl | 4 ++-- changes/v5.0.10-en.md | 2 ++ changes/v5.0.10-zh.md | 2 ++ 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/apps/emqx/src/emqx_trace/emqx_trace.erl b/apps/emqx/src/emqx_trace/emqx_trace.erl index 65756fc2f..d86a0af33 100644 --- a/apps/emqx/src/emqx_trace/emqx_trace.erl +++ b/apps/emqx/src/emqx_trace/emqx_trace.erl @@ -38,7 +38,8 @@ delete/1, clear/0, update/2, - check/0 + check/0, + now_second/0 ]). -export([ @@ -287,7 +288,7 @@ insert_new_trace(Trace) -> transaction(fun emqx_trace_dl:insert_new_trace/1, [Trace]). update_trace(Traces) -> - Now = erlang:system_time(second), + Now = now_second(), {_Waiting, Running, Finished} = classify_by_time(Traces, Now), disable_finished(Finished), Started = emqx_trace_handler:running(), @@ -455,7 +456,7 @@ ensure_map(Trace) when is_list(Trace) -> ). fill_default(Trace = #?TRACE{start_at = undefined}) -> - fill_default(Trace#?TRACE{start_at = erlang:system_time(second)}); + fill_default(Trace#?TRACE{start_at = now_second()}); fill_default(Trace = #?TRACE{end_at = undefined, start_at = StartAt}) -> fill_default(Trace#?TRACE{end_at = StartAt + 10 * 60}); fill_default(Trace) -> @@ -493,7 +494,7 @@ to_trace(#{start_at := StartAt} = Trace, Rec) -> {ok, Sec} = to_system_second(StartAt), to_trace(maps:remove(start_at, Trace), Rec#?TRACE{start_at = Sec}); to_trace(#{end_at := EndAt} = Trace, Rec) -> - Now = erlang:system_time(second), + Now = now_second(), case to_system_second(EndAt) of {ok, Sec} when Sec > Now -> to_trace(maps:remove(end_at, Trace), Rec#?TRACE{end_at = Sec}); @@ -517,8 +518,7 @@ validate_ip_address(IP) -> end. to_system_second(Sec) -> - Now = erlang:system_time(second), - {ok, erlang:max(Now, Sec)}. + {ok, erlang:max(now_second(), Sec)}. zip_dir() -> filename:join([trace_dir(), "zip"]). @@ -570,3 +570,6 @@ filter_cli_handler(Names) -> end, Names ). + +now_second() -> + os:system_time(second). diff --git a/apps/emqx/src/emqx_trace/emqx_trace_dl.erl b/apps/emqx/src/emqx_trace/emqx_trace_dl.erl index 3f96e1531..dd546f793 100644 --- a/apps/emqx/src/emqx_trace/emqx_trace_dl.erl +++ b/apps/emqx/src/emqx_trace/emqx_trace_dl.erl @@ -30,7 +30,7 @@ -include("emqx_trace.hrl"). %%================================================================================ -%% API funcions +%% API functions %%================================================================================ %% Introduced in 5.0 @@ -43,7 +43,7 @@ update(Name, Enable) -> [#?TRACE{enable = Enable}] -> ok; [Rec] -> - case erlang:system_time(second) >= Rec#?TRACE.end_at of + case emqx_trace:now_second() >= Rec#?TRACE.end_at of false -> mnesia:write(?TRACE, Rec#?TRACE{enable = Enable}, write); true -> mnesia:abort(finished) end diff --git a/changes/v5.0.10-en.md b/changes/v5.0.10-en.md index 124e07063..9b04ec4fe 100644 --- a/changes/v5.0.10-en.md +++ b/changes/v5.0.10-en.md @@ -26,6 +26,8 @@ ## Bug fixes +- Fix create trace sometime failed by end_at time has already passed. [#9302](https://github.com/emqx/emqx/pull/9302) + - Fix error log message when `mechanism` is missing in authentication config [#8924](https://github.com/emqx/emqx/pull/8924). - Fix HTTP 500 issue when unknown `status` parameter is used in `/gateway` API call [#9225](https://github.com/emqx/emqx/pull/9225). diff --git a/changes/v5.0.10-zh.md b/changes/v5.0.10-zh.md index 89f345bcf..357bcdf42 100644 --- a/changes/v5.0.10-zh.md +++ b/changes/v5.0.10-zh.md @@ -25,6 +25,8 @@ ## Bug fixes +- 修复创建追踪日志时偶尔会报`end_at time has already passed`错误,导致创建失败。[#9302](https://github.com/emqx/emqx/pull/9302) + - 优化认认证配置中 `mechanism` 字段缺失情况下的错误日志 [#8924](https://github.com/emqx/emqx/pull/8924)。 - 修复未知 `status` 参数导致 `/gateway` API 发生 HTTP 500 错误的问题 [#9225](https://github.com/emqx/emqx/pull/9225)。 From 6f6ed1b4f80dd848f0819ce54f292b09faa92b74 Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Mon, 21 Nov 2022 10:12:43 +0800 Subject: [PATCH 2/3] chore: update 5.0.11 changelog --- changes/v5.0.10-en.md | 2 -- changes/v5.0.10-zh.md | 2 -- changes/v5.0.11-en.md | 2 ++ changes/v5.0.11-zh.md | 2 ++ 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/changes/v5.0.10-en.md b/changes/v5.0.10-en.md index 9b04ec4fe..124e07063 100644 --- a/changes/v5.0.10-en.md +++ b/changes/v5.0.10-en.md @@ -26,8 +26,6 @@ ## Bug fixes -- Fix create trace sometime failed by end_at time has already passed. [#9302](https://github.com/emqx/emqx/pull/9302) - - Fix error log message when `mechanism` is missing in authentication config [#8924](https://github.com/emqx/emqx/pull/8924). - Fix HTTP 500 issue when unknown `status` parameter is used in `/gateway` API call [#9225](https://github.com/emqx/emqx/pull/9225). diff --git a/changes/v5.0.10-zh.md b/changes/v5.0.10-zh.md index 357bcdf42..89f345bcf 100644 --- a/changes/v5.0.10-zh.md +++ b/changes/v5.0.10-zh.md @@ -25,8 +25,6 @@ ## Bug fixes -- 修复创建追踪日志时偶尔会报`end_at time has already passed`错误,导致创建失败。[#9302](https://github.com/emqx/emqx/pull/9302) - - 优化认认证配置中 `mechanism` 字段缺失情况下的错误日志 [#8924](https://github.com/emqx/emqx/pull/8924)。 - 修复未知 `status` 参数导致 `/gateway` API 发生 HTTP 500 错误的问题 [#9225](https://github.com/emqx/emqx/pull/9225)。 diff --git a/changes/v5.0.11-en.md b/changes/v5.0.11-en.md index 0a5314e1d..d4c92da6f 100644 --- a/changes/v5.0.11-en.md +++ b/changes/v5.0.11-en.md @@ -19,6 +19,8 @@ ## Bug fixes +- Fix create trace sometime failed by end_at time has already passed. [#9302](https://github.com/emqx/emqx/pull/9302) + - Return 404 for status of unknown authenticator in `/authenticator/{id}/status` [#9328](https://github.com/emqx/emqx/pull/9328). - Fix that JWT ACL rules are only applied if an `exp` claim is set [#9368](https://github.com/emqx/emqx/pull/9368). diff --git a/changes/v5.0.11-zh.md b/changes/v5.0.11-zh.md index 6bc968008..ae42aa0ae 100644 --- a/changes/v5.0.11-zh.md +++ b/changes/v5.0.11-zh.md @@ -17,6 +17,8 @@ ## 修复 +- 修复创建追踪日志时偶尔会报`end_at time has already passed`错误,导致创建失败。[#9302](https://github.com/emqx/emqx/pull/9302) + - 通过 `/authenticator/{id}/status` 请求未知认证器的状态时,将会返回 404。 - 修复 JWT ACL 规则只在设置了超期时间时才生效的问题 [#9368](https://github.com/emqx/emqx/pull/9368)。 From 4e97344149aeae0ba46281dc68e7f1d2aa440ea4 Mon Sep 17 00:00:00 2001 From: zhongwencool Date: Mon, 21 Nov 2022 16:16:40 +0800 Subject: [PATCH 3/3] chore: apply suggestions from code review Co-authored-by: JianBo He --- changes/v5.0.11-en.md | 2 +- changes/v5.0.11-zh.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/changes/v5.0.11-en.md b/changes/v5.0.11-en.md index d4c92da6f..45547e1eb 100644 --- a/changes/v5.0.11-en.md +++ b/changes/v5.0.11-en.md @@ -19,7 +19,7 @@ ## Bug fixes -- Fix create trace sometime failed by end_at time has already passed. [#9302](https://github.com/emqx/emqx/pull/9302) +- Fix create trace sometime failed by end_at time has already passed. [#9303](https://github.com/emqx/emqx/pull/9303) - Return 404 for status of unknown authenticator in `/authenticator/{id}/status` [#9328](https://github.com/emqx/emqx/pull/9328). diff --git a/changes/v5.0.11-zh.md b/changes/v5.0.11-zh.md index ae42aa0ae..d372c15cb 100644 --- a/changes/v5.0.11-zh.md +++ b/changes/v5.0.11-zh.md @@ -17,7 +17,7 @@ ## 修复 -- 修复创建追踪日志时偶尔会报`end_at time has already passed`错误,导致创建失败。[#9302](https://github.com/emqx/emqx/pull/9302) +- 修复创建追踪日志时偶尔会报`end_at time has already passed`错误,导致创建失败。[#9303](https://github.com/emqx/emqx/pull/9303) - 通过 `/authenticator/{id}/status` 请求未知认证器的状态时,将会返回 404。