Skip to content

Commit

Permalink
Use a separate table for each HostType in mod_keystore
Browse files Browse the repository at this point in the history
Remove CETS table for mod_keystore, once testing is done
Trying to solve this command failing:
./tools/test-runner.sh --skip-small-tests --db redis pgsql --preset pgsql_cets --skip-cover  --skip-stop-nodes oauth graphql_cets
  • Loading branch information
arcusfelis committed Oct 6, 2023
1 parent 3bfd122 commit 37d7c46
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/mod_keystore.erl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ start(HostType, Opts) ->
-spec stop(mongooseim:host_type()) -> ok.
stop(HostType) ->
clear_keystore_ets(HostType),
mod_keystore_backend:stop(HostType),
ok.

-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list().
Expand Down
8 changes: 7 additions & 1 deletion src/mod_keystore_backend.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
%% the backend modules (i.e. mod_keystore_mnesia...).
-module(mod_keystore_backend).

-export([init/2, init_ram_key/2, get_key/1]).
-export([init/2, stop/1, init_ram_key/2, get_key/1]).

-define(MAIN_MODULE, mod_keystore).

-callback init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
-callback stop(mongooseim:host_type()) -> ok.

%% Cluster members race to decide whose key gets stored in the distributed database.
%% That's why ProposedKey (the key this cluster member tries to propagate to other nodes)
Expand All @@ -24,6 +25,11 @@ init(HostType, Opts) ->
Args = [HostType, Opts],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec stop(mongooseim:host_type()) -> ok.
stop(HostType) ->
Args = [HostType],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

%% Cluster members race to decide whose key gets stored in the distributed database.
%% That's why ProposedKey (the key this cluster member tries to propagate to other nodes)
%% might not be the same as ActualKey (key of the member who will have won the race).
Expand Down
31 changes: 22 additions & 9 deletions src/mod_keystore_cets.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
-behaviour(mod_keystore_backend).

-export([init/2,
stop/1,
init_ram_key/1,
get_key/1]).

Expand All @@ -13,13 +14,23 @@
-include("mod_keystore.hrl").
-include("mongoose_logger.hrl").

-define(TABLE, cets_keystore).
table_name(HostType) ->
binary_to_atom(<<"cets_keystore_", HostType/binary>>).

-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
init(_HostType, _Opts) ->
init(HostType, _Opts) ->
%% There is no logic to remove keys.
cets:start(?TABLE, #{handle_conflict => fun ?MODULE:handle_conflict/2}),
cets_discovery:add_table(mongoose_cets_discovery, ?TABLE),
%% Separate table per HostType (so we could remove the table once the module is unloaded).
Tab = table_name(HostType),
cets:start(Tab, #{handle_conflict => fun ?MODULE:handle_conflict/2}),
cets_discovery:add_table(mongoose_cets_discovery, Tab),
ok.

-spec stop(mongooseim:host_type()) -> ok.
stop(HostType) ->
Tab = table_name(HostType),
cets_discovery:delete_table(mongoose_cets_discovery, Tab),
cets:stop(Tab),
ok.

%% We need to choose one key consistently.
Expand All @@ -42,12 +53,13 @@ init_ram_key(ProposedKey) ->
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, key = PropKey}, N, Retries) ->
case cets:insert_new(?TABLE, {Id, PropKey}) of
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(?TABLE, Id) of
case ets:lookup(Tab, Id) of
[{Id, Key}] ->
%% Return already inserted key
{ok, #key{id = Id, key = Key}};
Expand All @@ -59,5 +71,6 @@ init_ram_key(ProposedKey = #key{id = Id, key = PropKey}, N, Retries) ->
end.

-spec get_key(Id :: mod_keystore:key_id()) -> mod_keystore:key_list().
get_key(Id) ->
ets:lookup(?TABLE, Id).
get_key(Id = {_, HostType}) ->
Tab = table_name(HostType),
ets:lookup(Tab, Id).
5 changes: 5 additions & 0 deletions src/mod_keystore_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
-behaviour(mod_keystore_backend).

-export([init/2,
stop/1,
init_ram_key/1,
get_key/1]).

Expand All @@ -18,6 +19,10 @@ init(_HostType, _Opts) ->
mnesia:add_table_copy(key, node(), ram_copies),
ok.

-spec stop(mongooseim:host_type()) -> ok.
stop(_HostType) ->
ok.

-spec init_ram_key(ProposedKey) -> Result when
ProposedKey :: mod_keystore:key(),
Result :: {ok, ActualKey} | {error, any()},
Expand Down

0 comments on commit 37d7c46

Please sign in to comment.