Merge pull request #6380 from zhongwencool/v4.4-trace-bug-fix
fix: trace_name format [A-Za-z0-9-_];retry if trace_file not find
This commit is contained in:
commit
26fa06b071
|
@ -398,15 +398,13 @@ fill_default(Trace = #?TRACE{end_at = undefined, start_at = StartAt}) ->
|
||||||
fill_default(Trace#?TRACE{end_at = StartAt + 10 * 60});
|
fill_default(Trace#?TRACE{end_at = StartAt + 10 * 60});
|
||||||
fill_default(Trace) -> Trace.
|
fill_default(Trace) -> Trace.
|
||||||
|
|
||||||
|
-define(NAME_RE, "^[A-Za-z]+[A-Za-z0-9-_]*$").
|
||||||
|
|
||||||
to_trace([], Rec) -> {ok, Rec};
|
to_trace([], Rec) -> {ok, Rec};
|
||||||
to_trace([{name, Name} | Trace], Rec) ->
|
to_trace([{name, Name} | Trace], Rec) ->
|
||||||
case io_lib:printable_unicode_list(unicode:characters_to_list(Name, utf8)) of
|
case re:run(Name, ?NAME_RE) of
|
||||||
true ->
|
nomatch -> {error, "Name should be " ?NAME_RE};
|
||||||
case binary:match(Name, [<<"/">>], []) of
|
_ -> to_trace(Trace, Rec#?TRACE{name = Name})
|
||||||
nomatch -> to_trace(Trace, Rec#?TRACE{name = Name});
|
|
||||||
_ -> {error, "name cannot contain /"}
|
|
||||||
end;
|
|
||||||
false -> {error, "name must printable unicode"}
|
|
||||||
end;
|
end;
|
||||||
to_trace([{type, Type} | Trace], Rec) ->
|
to_trace([{type, Type} | Trace], Rec) ->
|
||||||
case lists:member(Type, [<<"clientid">>, <<"topic">>, <<"ip_address">>]) of
|
case lists:member(Type, [<<"clientid">>, <<"topic">>, <<"ip_address">>]) of
|
||||||
|
@ -453,7 +451,8 @@ validate_topic(TopicName) ->
|
||||||
to_system_second(At) ->
|
to_system_second(At) ->
|
||||||
try
|
try
|
||||||
Sec = calendar:rfc3339_to_system_time(binary_to_list(At), [{unit, second}]),
|
Sec = calendar:rfc3339_to_system_time(binary_to_list(At), [{unit, second}]),
|
||||||
{ok, Sec}
|
Now = erlang:system_time(second),
|
||||||
|
{ok, erlang:max(Now, Sec)}
|
||||||
catch error: {badmatch, _} ->
|
catch error: {badmatch, _} ->
|
||||||
{error, ["The rfc3339 specification not satisfied: ", At]}
|
{error, ["The rfc3339 specification not satisfied: ", At]}
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -143,7 +143,7 @@ stream_log_file(#{name := Name}, Params) ->
|
||||||
Meta = #{<<"position">> => Size, <<"bytes">> => Bytes},
|
Meta = #{<<"position">> => Size, <<"bytes">> => Bytes},
|
||||||
{ok, #{meta => Meta, items => <<"">>}};
|
{ok, #{meta => Meta, items => <<"">>}};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
logger:log(error, "read_file_failed by ~p", [{Name, Reason, Position, Bytes}]),
|
logger:log(error, "read_file_failed by ~p", [{Node, Name, Reason, Position, Bytes}]),
|
||||||
{error, Reason};
|
{error, Reason};
|
||||||
{badrpc, nodedown} ->
|
{badrpc, nodedown} ->
|
||||||
{error, "BadRpc node down"}
|
{error, "BadRpc node down"}
|
||||||
|
@ -165,15 +165,12 @@ get_trace_size() ->
|
||||||
|
|
||||||
%% this is an rpc call for stream_log_file/2
|
%% this is an rpc call for stream_log_file/2
|
||||||
read_trace_file(Name, Position, Limit) ->
|
read_trace_file(Name, Position, Limit) ->
|
||||||
|
case emqx_trace:get_trace_filename(Name) of
|
||||||
|
{error, _} = Error -> Error;
|
||||||
|
{ok, TraceFile} ->
|
||||||
TraceDir = emqx_trace:trace_dir(),
|
TraceDir = emqx_trace:trace_dir(),
|
||||||
{ok, AllFiles} = file:list_dir(TraceDir),
|
|
||||||
TracePrefix = "trace_" ++ binary_to_list(Name) ++ "_",
|
|
||||||
Filter = fun(FileName) -> nomatch =/= string:prefix(FileName, TracePrefix) end,
|
|
||||||
case lists:filter(Filter, AllFiles) of
|
|
||||||
[TraceFile] ->
|
|
||||||
TracePath = filename:join([TraceDir, TraceFile]),
|
TracePath = filename:join([TraceDir, TraceFile]),
|
||||||
read_file(TracePath, Position, Limit);
|
read_file(TracePath, Position, Limit)
|
||||||
[] -> {error, not_found}
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
read_file(Path, Offset, Bytes) ->
|
read_file(Path, Offset, Bytes) ->
|
||||||
|
@ -206,8 +203,8 @@ collect_file_size(Nodes, FileName, AllFiles) ->
|
||||||
Acc#{Node => Size}
|
Acc#{Node => Size}
|
||||||
end, #{}, Nodes).
|
end, #{}, Nodes).
|
||||||
|
|
||||||
%% status(false, _Start, End, Now) when End > Now -> <<"stopped">>;
|
|
||||||
status(false, _Start, _End, _Now) -> <<"stopped">>;
|
status(false, _Start, _End, _Now) -> <<"stopped">>;
|
||||||
status(true, Start, _End, Now) when Now < Start -> <<"waiting">>;
|
%% asynchronously create trace, we should wait 1 seconds
|
||||||
|
status(true, Start, _End, Now) when Now < Start + 2 -> <<"waiting">>;
|
||||||
status(true, _Start, End, Now) when Now >= End -> <<"stopped">>;
|
status(true, _Start, End, Now) when Now >= End -> <<"stopped">>;
|
||||||
status(true, _Start, _End, _Now) -> <<"running">>.
|
status(true, _Start, _End, _Now) -> <<"running">>.
|
||||||
|
|
|
@ -130,7 +130,7 @@ t_create_failed(_Config) ->
|
||||||
InvalidPackets4 = [{<<"name">>, <<"/test">>}, {<<"clientid">>, <<"t">>},
|
InvalidPackets4 = [{<<"name">>, <<"/test">>}, {<<"clientid">>, <<"t">>},
|
||||||
{<<"type">>, <<"clientid">>}],
|
{<<"type">>, <<"clientid">>}],
|
||||||
{error, Reason9} = emqx_trace:create(InvalidPackets4),
|
{error, Reason9} = emqx_trace:create(InvalidPackets4),
|
||||||
?assertEqual(<<"name cannot contain /">>, iolist_to_binary(Reason9)),
|
?assertEqual(<<"Name should be ^[A-Za-z]+[A-Za-z0-9-_]*$">>, iolist_to_binary(Reason9)),
|
||||||
|
|
||||||
?assertEqual({error, "type=[topic,clientid,ip_address] required"},
|
?assertEqual({error, "type=[topic,clientid,ip_address] required"},
|
||||||
emqx_trace:create([{<<"name">>, <<"test-name">>}, {<<"clientid">>, <<"good">>}])),
|
emqx_trace:create([{<<"name">>, <<"test-name">>}, {<<"clientid">>, <<"good">>}])),
|
||||||
|
|
|
@ -200,6 +200,7 @@ t_trace_ip_address(_Config) ->
|
||||||
|
|
||||||
|
|
||||||
filesync(Name, Type) ->
|
filesync(Name, Type) ->
|
||||||
|
ct:sleep(50),
|
||||||
filesync(Name, Type, 3).
|
filesync(Name, Type, 3).
|
||||||
|
|
||||||
%% sometime the handler process is not started yet.
|
%% sometime the handler process is not started yet.
|
||||||
|
|
Loading…
Reference in New Issue