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

Add insert_new_or_lookup/2 function #36

Merged
merged 2 commits into from
Oct 9, 2023
Merged

Conversation

arcusfelis
Copy link
Contributor

insert_new/2 is useful, but there could be a case, when we want to insert new or read existing record.
And there could be a race condition between these two events (i.e. this race condition is even in the default ets module).
Which means the code would have to implement this:

-spec init_ram_key(ProposedKey) -> Result when
      ProposedKey :: mod_keystore:key(),
      Result :: {ok, ActualKey} | {error, init_ram_key_failed},
      ActualKey :: mod_keystore:key().
init_ram_key(ProposedKey) ->
    init_ram_key(ProposedKey, 1, 3).

%% Inserts new key or returns already inserted.
-spec init_ram_key(Key, TriedTimes, Retries) -> Result when
      Result :: {ok, Key} | {error, init_ram_key_failed},
      Key :: mod_keystore:key(),
      TriedTimes :: non_neg_integer(),
      Retries :: non_neg_integer().
init_ram_key(#key{id = Id, key = Key}, _, 0) ->
    ?LOG_ERROR(#{what => init_ram_key_failed, id => Id, key => Key}),
    {error, init_ram_key_failed};
init_ram_key(ProposedKey = #key{id = Id = {_, HostType}, key = PropKey}, N, Retries) ->
    Tab = table_name(HostType),
    case cets:insert_new(Tab, {Id, PropKey}) of
        true ->
            {ok, ProposedKey};
        false ->
            case ets:lookup(Tab, Id) of
                [{Id, Key}] ->
                    %% Return already inserted key
                    {ok, #key{id = Id, key = Key}};
                [] ->
                    ?LOG_WARNING(#{what => init_ram_key_retry,
                                   id => Id, key => PropKey, tried_times => N}),
                    init_ram_key(ProposedKey, N + 1, Retries - 1)
            end
   end.

Retry mechanism. Would also require tests, would also need to decide when to stop to retry... Sounds too much work.

This new function allows to rewrite the above code as:

-spec init_ram_key(ProposedKey) -> Result when
     ProposedKey :: mod_keystore:key(),
     Result :: {ok, ActualKey} | {error, init_ram_key_failed},
     ActualKey :: mod_keystore:key().
init_ram_key(ProposedKey = #key{id = Id = {_, HostType}, key = PropKey}) ->
   Tab = table_name(HostType),
   case cets:insert_new_or_lookup(Tab, {Id, PropKey}) of
       {true, _} ->
           {ok, ProposedKey};
       {false, [{Id, Key}]} ->
           {ok, #key{id = Id, key = Key}}
  end.

It is in case you want to write a testcase for the false condition. If you don't want to have any special logic in case of conflict (and no separation in the cover reports):

-spec init_ram_key(ProposedKey) -> Result when
    ProposedKey :: mod_keystore:key(),
    Result :: {ok, ActualKey} | {error, init_ram_key_failed},
    ActualKey :: mod_keystore:key().
init_ram_key(ProposedKey = #key{id = Id = {_, HostType}, key = PropKey}) ->
  Tab = table_name(HostType),
  {_, [{Id, Key}]} = cets:insert_new_or_lookup(Tab, {Id, PropKey}),
  {ok, #key{id = Id, key = Key}}.

@codecov
Copy link

codecov bot commented Oct 6, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (e3ad43f) 97.38% compared to head (72b01d2) 97.42%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #36      +/-   ##
==========================================
+ Coverage   97.38%   97.42%   +0.03%     
==========================================
  Files           9        9              
  Lines         613      622       +9     
==========================================
+ Hits          597      606       +9     
  Misses         16       16              
Files Coverage Δ
src/cets.erl 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@arcusfelis arcusfelis marked this pull request as ready for review October 6, 2023 13:54
Copy link
Member

@chrzaszcz chrzaszcz left a comment

Choose a reason for hiding this comment

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

👌

@chrzaszcz chrzaszcz merged commit f715c4a into main Oct 9, 2023
8 checks passed
@chrzaszcz chrzaszcz deleted the insert-new-or-lookup branch October 9, 2023 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants