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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CLI route_refresh_all tp handle crashes/error and retry #299

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 67 additions & 47 deletions src/cli/hpr_cli_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ config_route(_, _, _) ->
config_route_refresh_all(["config", "route", "refresh_all"], [], Flags) ->
Options = maps:from_list(Flags),
Min = maps:get(minimum, Options, 1),
erlang:spawn(fun() ->
Pid = erlang:spawn(fun() ->
List = lists:filtermap(
fun(RouteETS) ->
RouteID = hpr_route:id(hpr_route_ets:route(RouteETS)),
Expand All @@ -269,56 +269,76 @@ config_route_refresh_all(["config", "route", "refresh_all"], [], Flags) ->
Sorted = lists:sort(fun({_, A}, {_, B}) -> A > B end, List),
RouteIDs = [ID || {ID, _} <- Sorted],
Total = erlang:length(RouteIDs),
TimeIt = fun(Func) ->
{Time0, Val} = timer:tc(Func),
Time = erlang:convert_time_unit(Time0, microsecond, millisecond),
lager:info("took ~pms", [Time]),
Val
end,
Refresh = fun({Idx, ID}) ->
lager:info("~p/~p===========================================================", [
Idx, Total
]),
TimeIt(fun() ->
try hpr_route_stream_worker:refresh_route(ID) of
{ok, Map} ->
lager:info("| Type | Before | After | Removed | Added |"),
lager:info("|------|---------|---------|---------|---------|"),
lager:info("| ~4w | ~7w | ~7w | ~7w | ~7w |", [
eui,
maps:get(eui_before, Map),
maps:get(eui_after, Map),
maps:get(eui_removed, Map),
maps:get(eui_added, Map)
]),
lager:info("| ~4w | ~7w | ~7w | ~7w | ~7w |", [
skf,
maps:get(skf_before, Map),
maps:get(skf_after, Map),
maps:get(skf_removed, Map),
maps:get(skf_added, Map)
]),
lager:info("| ~4w | ~7w | ~7w | ~7w | ~7w |", [
addr,
maps:get(devaddr_before, Map),
maps:get(devaddr_after, Map),
maps:get(devaddr_removed, Map),
maps:get(devaddr_added, Map)
]);
{error, _R} ->
lager:info("ERROR ~p", [_R])
catch
_E:_R ->
lager:info("CRASHED ~p", [_R])
end
end)
end,
[Refresh(ID) || ID <- lists:zip(lists:seq(1, Total), RouteIDs)]
lager:info("Found ~p routes to update", [Total]),
route_refresh_all(
Total, RouteIDs, backoff:init(timer:seconds(1), timer:minutes(1))
)
end),
c_text("command spawned, look at logs");
c_text("command spawned @ ~p, look at logs and tail hpr_cli_config", [Pid]);
config_route_refresh_all(_, _, _) ->
usage.

route_refresh_all(_Total, [], _Backoff0) ->
lager:info("All done!");
route_refresh_all(Total, [RouteID | T] = RouteIDs, Backoff0) ->
CurrTotal = erlang:length(RouteIDs),
IDX = Total - CurrTotal + 1,
lager:info("~p/~p===== ~s =====", [
IDX, Total, RouteID
]),
Start = erlang:system_time(millisecond),
case try_refresh_route(RouteID) of
ok ->
End = erlang:system_time(millisecond),
lager:info("took ~pms", [End - Start]),
{_, Backoff1} = backoff:succeed(Backoff0),
route_refresh_all(Total, T, Backoff1);
error ->
End = erlang:system_time(millisecond),
lager:info("took ~pms", [End - Start]),
{Delay, Backoff1} = backoff:fail(Backoff0),
lager:info("sleeping ~pms", [Delay]),
timer:sleep(Delay),
route_refresh_all(Total, RouteIDs, Backoff1)
end,
ok.

try_refresh_route(RouteID) ->
try hpr_route_stream_worker:refresh_route(RouteID) of
{ok, Map} ->
lager:info("| Type | Before | After | Removed | Added |"),
lager:info("|------|---------|---------|---------|---------|"),
lager:info("| ~4w | ~7w | ~7w | ~7w | ~7w |", [
eui,
maps:get(eui_before, Map),
maps:get(eui_after, Map),
maps:get(eui_removed, Map),
maps:get(eui_added, Map)
]),
lager:info("| ~4w | ~7w | ~7w | ~7w | ~7w |", [
skf,
maps:get(skf_before, Map),
maps:get(skf_after, Map),
maps:get(skf_removed, Map),
maps:get(skf_added, Map)
]),
lager:info("| ~4w | ~7w | ~7w | ~7w | ~7w |", [
addr,
maps:get(devaddr_before, Map),
maps:get(devaddr_after, Map),
maps:get(devaddr_removed, Map),
maps:get(devaddr_added, Map)
]),
ok;
{error, _R} ->
lager:info("ERROR ~p", [_R]),
error
catch
_E:_R ->
lager:info("CRASHED ~p", [_R]),
error
end.

config_route_refresh(["config", "route", "refresh", RouteID], [], _Flags) ->
case hpr_route_stream_worker:refresh_route(RouteID) of
{ok, RefreshMap} ->
Expand Down
Loading