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

change how we store SKF for performance gains #256

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/cli/hpr_cli_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ find_skf(SKToFind, [RouteETS | RoutesETS], Acc0) ->
find_skf(SKToFind, RoutesETS, Acc0);
SKFs ->
Acc1 = lists:filtermap(
fun({{LastUsed, SK}, {DevAddr, MaxCopies}}) ->
fun({LastUsed, SK, DevAddr, MaxCopies}) ->
case SK =:= SKToFind of
true ->
{true, {DevAddr, SK, RouteID, LastUsed * -1, MaxCopies}};
Expand Down Expand Up @@ -513,7 +513,7 @@ format_eui({App, Dev}) ->
hpr_utils:int_to_hex_string(App), hpr_utils:int_to_hex_string(Dev)
]).

format_skf({{Timestamp, SKF}, {DevAddr, MaxCopies}}) ->
format_skf({Timestamp, SKF, DevAddr, MaxCopies}) ->
io_lib:format(" - (~s, ~s, ~w, ~w)~n", [
hpr_utils:int_to_hex_string(DevAddr),
hpr_utils:bin_to_hex_string(SKF),
Expand Down
71 changes: 36 additions & 35 deletions src/grpc/iot_config/hpr_route_ets.erl
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,7 @@ insert_route(Route) ->
[#hpr_route_ets{skf_ets = ETS}] ->
ETS;
_Other ->
ets:new(?ETS_SKFS, [
public,
ordered_set,
{read_concurrency, true},
{write_concurrency, true},
{heir, erlang:whereis(?SKF_HEIR), RouteID}
])
mk_new_skf_ets(RouteID)
end,
?MODULE:insert_route(Route, SKFETS).

Expand Down Expand Up @@ -309,12 +303,9 @@ insert_skf(SKF) ->
MD = skf_md(RouteID, SKF),
case ?MODULE:lookup_route(RouteID) of
[#hpr_route_ets{skf_ets = SKFETS}] ->
Deleted = do_remove_skf(SKFETS, SKF),
do_insert_skf(SKFETS, SKF),
case Deleted of
0 -> lager:debug(MD, "inserted SKF");
_ -> lager:debug([{deleted, Deleted} | MD], "updated SKF")
end;
true = do_remove_skf(SKFETS, SKF),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make them return ok for both?

ok = do_insert_skf(SKFETS, SKF),
lager:debug(MD, "inserted SKF");
_Other ->
lager:error(MD, "failed to insert skf table not found ~p", [
_Other
Expand Down Expand Up @@ -361,18 +352,17 @@ delete_skf(SKF) ->
DevAddr = hpr_skf:devaddr(SKF),
SessionKey = hpr_skf:session_key(SKF),
MaxCopies = hpr_skf:max_copies(SKF),
%% Here we ignore max_copies
MS = [{{{'_', hpr_utils:hex_to_bin(SessionKey)}, {DevAddr, '_'}}, [], [true]}],
Deleted = ets:select_delete(SKFETS, MS),

true = ets:delete(SKFETS, hpr_utils:hex_to_bin(SessionKey)),

lager:debug(
[
{route_id, RouteID},
{devaddr, hpr_utils:int_to_hex_string(DevAddr)},
{session_key, SessionKey},
{max_copies, MaxCopies}
],
"deleted ~p SKF",
[Deleted]
"deleted SKF"
);
_Other ->
lager:warning("failed to delete skf not found ~p for ~s", [
Expand All @@ -384,7 +374,7 @@ delete_skf(SKF) ->
-spec lookup_skf(ETS :: ets:table(), DevAddr :: non_neg_integer()) ->
[{SessionKey :: binary(), Timestamp :: integer(), MaxCopies :: non_neg_integer()}].
lookup_skf(ETS, DevAddr) ->
MS = [{{{'$1', '$2'}, {DevAddr, '$3'}}, [], [{{'$2', '$1', '$3'}}]}],
MS = [{{'$1', '$2', DevAddr, '$3'}, [], [{{'$2', '$1', '$3'}}]}],
ets:select(ETS, MS).

-spec select_skf(Continuation :: ets:continuation()) ->
Expand All @@ -395,7 +385,7 @@ select_skf(Continuation) ->
-spec select_skf(ETS :: ets:table(), DevAddr :: non_neg_integer() | ets:continuation()) ->
{[{binary(), integer(), non_neg_integer()}], ets:continuation()} | '$end_of_table'.
select_skf(ETS, DevAddr) ->
MS = [{{{'$1', '$2'}, {DevAddr, '$3'}}, [], [{{'$2', '$1', '$3'}}]}],
MS = [{{'$1', '$2', DevAddr, '$3'}, [], [{{'$2', '$1', '$3'}}]}],
ets:select(ETS, MS, 100).

-spec delete_all() -> ok.
Expand Down Expand Up @@ -460,13 +450,7 @@ replace_route_devaddrs(RouteID, DevAddrRanges) ->
replace_route_skfs(RouteID, NewSKFs) ->
case ?MODULE:lookup_route(RouteID) of
[#hpr_route_ets{skf_ets = OldTab} = Route] ->
NewTab = ets:new(?ETS_SKFS, [
public,
ordered_set,
{read_concurrency, true},
{write_concurrency, true},
{heir, erlang:whereis(?SKF_HEIR), RouteID}
]),
NewTab = mk_new_skf_ets(RouteID),
lists:foreach(fun(SKF) -> do_insert_skf(NewTab, SKF) end, NewSKFs),
true = ets:insert(?ETS_ROUTES, Route#hpr_route_ets{skf_ets = NewTab}),

Expand Down Expand Up @@ -532,8 +516,10 @@ devaddr_ranges_count_for_route(RouteID) ->

-spec skfs_for_route(RouteID :: hpr_route:id()) ->
list({
Key :: {Timestamp :: integer(), SessionKey :: binary()},
Vals :: {Devaddr :: non_neg_integer(), MaxCopies :: non_neg_integer()}
Timestamp :: integer(),
SessionKey :: binary(),
Devaddr :: non_neg_integer(),
MaxCopies :: non_neg_integer()
}).
skfs_for_route(RouteID) ->
case ?MODULE:lookup_route(RouteID) of
Expand All @@ -556,13 +542,25 @@ skfs_count_for_route(RouteID) ->
%% Internal Function Definitions
%% ------------------------------------------------------------------

-spec do_remove_skf(ets:table(), hpr_skf:skf()) ->
DeletedCount :: non_neg_integer().
%% Table is `ordered_set' with the first being a negated timestamp. This gives
%% us a sort order of most-recently-used. `{keypos, 2}' makes the table unique
%% on the second key.
%% Structure: {Timestamp, SessionKey, DevAddr, MaxCopies}.
-spec mk_new_skf_ets(hpr_route:id()) -> ets:table().
mk_new_skf_ets(RouteID) ->
ets:new(?ETS_SKFS, [
public,
ordered_set,
{read_concurrency, true},
{write_concurrency, true},
{keypos, 2},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk if this will work as the ordered_set will be on the key. Can you 2x check?

Copy link
Member

@macpie macpie Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one key, one object, ordered in Erlang term order

Maybe it's fine

{heir, erlang:whereis(?SKF_HEIR), RouteID}
]).

-spec do_remove_skf(ets:table(), hpr_skf:skf()) -> boolean().
do_remove_skf(SKFETS, SKF) ->
DevAddr = hpr_skf:devaddr(SKF),
SessionKey = hpr_skf:session_key(SKF),
MS = [{{{'_', hpr_utils:hex_to_bin(SessionKey)}, {DevAddr, '_'}}, [], [true]}],
ets:select_delete(SKFETS, MS).
ets:delete(SKFETS, hpr_utils:hex_to_bin(SessionKey)).

-spec do_insert_skf(ets:table(), hpr_skf:skf()) -> ok.
do_insert_skf(SKFETS, SKF) ->
Expand All @@ -572,7 +570,10 @@ do_insert_skf(SKFETS, SKF) ->
%% This is negative to make newest time on top
Now = erlang:system_time(millisecond) * -1,
true = ets:insert(SKFETS, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe create a record for this?

{Now, hpr_utils:hex_to_bin(SessionKey)}, {DevAddr, MaxCopies}
Now,
hpr_utils:hex_to_bin(SessionKey),
DevAddr,
MaxCopies
}),
ok.

Expand Down
Loading