Skip to content

Commit

Permalink
Merge pull request #3738 from esl/segmented_cache_update
Browse files Browse the repository at this point in the history
 Handle accumulated affiliations efficiently
  • Loading branch information
chrzaszcz authored Aug 24, 2022
2 parents 26701b0 + 884ca87 commit 42a04dc
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 33 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

%%% Stateful apps
{cache_tab, "1.0.29"},
{segmented_cache, "0.1.1"},
{segmented_cache, "0.2.0"},
{worker_pool, "6.0.1"},

%%% HTTP tools
Expand Down
6 changes: 3 additions & 3 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
{git,"https://github.com/basho/riak-erlang-client.git",
{ref,"f3a0592cf2200fd37b46b5188cabd4cf7058d776"}},
0},
{<<"segmented_cache">>,{pkg,<<"segmented_cache">>,<<"0.1.1">>},0},
{<<"segmented_cache">>,{pkg,<<"segmented_cache">>,<<"0.2.0">>},0},
{<<"semver">>,
{git,"https://github.com/nebularis/semver.git",
{ref,"c7d509f38298ec6594be4efdcd8a8f2322760039"}},
Expand Down Expand Up @@ -190,7 +190,7 @@
{<<"re2">>, <<"8114654E72EF62F605A8A393F219702F253CB7A02F671503918B76D0614DB046">>},
{<<"recon">>, <<"CBA53FA8DB83AD968C9A652E09C3ED7DDCC4DA434F27C3EAA9CA47FFB2B1FF03">>},
{<<"redbug">>, <<"9153EE50E42C39CE3F6EFA65EE746F4A52896DA66862CFB59E7C0F838B7B8414">>},
{<<"segmented_cache">>, <<"8EC348A27B35A578EEC5375A439A0DF9ABE5EDDDAE18191B8CC0DF53F61494CE">>},
{<<"segmented_cache">>, <<"908C7762DC92FFFC176F28176C6722AF8875FA79DE2C22028BC1A0261A5F4497">>},
{<<"ssl_verify_fun">>, <<"28A4D65B7F59893BC2C7DE786DEC1E1555BD742D336043FE644AE956C3497FBE">>},
{<<"stringprep">>, <<"02808C7024BC6285CA6A8A67E7ADDFC16F35DDA55551A582C5181D8EA960E890">>},
{<<"syslogger">>, <<"B0187E00330AA637AAD6E5A4BACC03492FCE172ABDFADB5F968A5A5AC10C660B">>},
Expand Down Expand Up @@ -252,7 +252,7 @@
{<<"re2">>, <<"FF0703CA095B5BEBF57DD12571AF24B3BA404180E9B5E43128790B5D31EBE803">>},
{<<"recon">>, <<"2C7523C8DEE91DFF41F6B3D63CBA2BD49EB6D2FE5BF1EEC0DF7F87EB5E230E1C">>},
{<<"redbug">>, <<"BFC7BCB8743C55DBE0134DBCB89DD6A57606288CC4E2570ECCD701061FBCBD93">>},
{<<"segmented_cache">>, <<"D82BBA16929E0BB0D13C43FF5F90BE2CBC9539A3AB713C46046863CE250D7EB8">>},
{<<"segmented_cache">>, <<"8DCCE6B5E9D4CDF5B4E031B39A883010F6BA766CEBF6C028749A98D3A403EC68">>},
{<<"ssl_verify_fun">>, <<"4F8805EB5C8A939CF2359367CB651A3180B27DFB48444846BE2613D79355D65E">>},
{<<"stringprep">>, <<"A5967B1144CA8002A58A03D16DD109FBD0BCDB82616CEAD2F983944314AF6A00">>},
{<<"syslogger">>, <<"3FE5D3EF59A0AF0E9A9B159DC4E888C9E8470D2DD201C6DFF004507F3ED578AD">>},
Expand Down
4 changes: 2 additions & 2 deletions src/mod_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1288,13 +1288,13 @@ remove_domain(Acc, HostType, Domain) ->

-spec acc_room_affiliations(mongoose_acc:t(), jid:jid()) -> mongoose_acc:t().
acc_room_affiliations(Acc1, Room) ->
case mongoose_acc:get(?MODULE, affiliations, {error, not_found}, Acc1) of
case mongoose_acc:get(?MODULE, {affiliations, Room}, {error, not_found}, Acc1) of
{error, _} ->
case mod_muc_room:get_room_users(Room) of
{error, not_found} ->
Acc1;
{ok, _Affs} = Res ->
mongoose_acc:set(?MODULE, affiliations, Res, Acc1)
mongoose_acc:set(?MODULE, {affiliations, Room}, Res, Acc1)
end;
_Affs ->
Acc1
Expand Down
11 changes: 6 additions & 5 deletions src/mongoose_user_cache.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ start_new_cache(HostType, Module, Opts) ->

do_start_new_cache(HostType, Module, Opts) ->
CacheName = gen_mod:get_module_proc(HostType, Module),
CacheOpts = #{merger_fun => fun maps:merge/2,
CacheOpts = #{scope => mim_scope,
merger_fun => fun maps:merge/2,
segment_num => gen_mod:get_opt(number_of_segments, Opts),
strategy => gen_mod:get_opt(strategy, Opts),
ttl => gen_mod:get_opt(time_to_live, Opts)},
Expand All @@ -78,15 +79,15 @@ do_start_new_cache(HostType, Module, Opts) ->

create_metrics(HostType, Module, CacheName) ->
telemetry:attach(CacheName,
[segmented_cache, request],
[CacheName, request],
fun ?MODULE:handle_telemetry_event/4,
#{host_type => HostType, module => Module}),
#{host_type => HostType, cache_name => CacheName, module => Module}),
mongoose_metrics:ensure_metric(HostType, [Module, hit], counter),
mongoose_metrics:ensure_metric(HostType, [Module, miss], counter),
mongoose_metrics:ensure_metric(HostType, [Module, latency], histogram).

handle_telemetry_event([segmented_cache, request], #{hit := Hit, time := Latency},
_, #{host_type := HostType, module := Module}) ->
handle_telemetry_event([CacheName, request], #{hit := Hit, time := Latency},
_, #{host_type := HostType, cache_name := CacheName, module := Module}) ->
case Hit of
true -> mongoose_metrics:update(HostType, [Module, hit], 1);
false -> mongoose_metrics:update(HostType, [Module, miss], 1)
Expand Down
41 changes: 25 additions & 16 deletions src/muc_light/mod_muc_light.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
acc_room_affiliations/2,
room_exists/3,
can_access_identity/4]).
-export([get_room_affiliations_from_acc/1]).
-export([get_acc_room_affiliations/2]).

%% For propEr
-export([apply_rsm/3]).
Expand All @@ -77,6 +77,7 @@

-type muc_server() :: jid:lserver().
-type host_type() :: mongooseim:host_type().
-type versioned_affs() :: {ok, aff_users(), binary()}.

%%====================================================================
%% API
Expand Down Expand Up @@ -149,7 +150,7 @@ try_to_create_room(CreatorJid, RoomJID, #create{raw_config = RawConfig} = Creati
| {error, validation_error() | bad_request | not_allowed | not_exists | item_not_found}.
change_room_config(UserJid, RoomID, MUCLightDomain, ConfigReq, Acc1) ->
RoomJID = jid:make(RoomID, MUCLightDomain, <<>>),
{Acc2, AffUsersRes} = get_room_affiliations_from_acc(Acc1, RoomJID),
{Acc2, AffUsersRes} = get_acc_room_affiliations(Acc1, RoomJID),
case mod_muc_light_room:process_request(UserJid, RoomJID, {set, ConfigReq},
AffUsersRes, Acc2) of
{set, ConfigResp, _} ->
Expand Down Expand Up @@ -516,15 +517,15 @@ can_access_room(_, Acc, Room, User) ->
none =/= get_affiliation(Acc, Room, User).

-spec acc_room_affiliations(mongoose_acc:t(), jid:jid()) -> mongoose_acc:t().
acc_room_affiliations(Acc1, Room) ->
case mongoose_acc:get(?MODULE, affiliations, {error, not_exists}, Acc1) of
acc_room_affiliations(Acc1, RoomJid) ->
case get_room_affs_from_acc(Acc1, RoomJid) of
{error, _} ->
HostType = mongoose_acc:host_type(Acc1),
case mod_muc_light_db_backend:get_aff_users(HostType, jid:to_lus(Room)) of
case mod_muc_light_db_backend:get_aff_users(HostType, jid:to_lus(RoomJid)) of
{error, not_exists} ->
Acc1;
{ok, _Affs, _Version} = Res ->
mongoose_acc:set(?MODULE, affiliations, Res, Acc1)
set_room_affs_from_acc(Acc1, RoomJid, Res)
end;
_Affs ->
Acc1
Expand All @@ -534,16 +535,24 @@ acc_room_affiliations(Acc1, Room) ->
room_exists(_, HostType, RoomJid) ->
mod_muc_light_db_backend:room_exists(HostType, jid:to_lus(RoomJid)).

-spec get_room_affiliations_from_acc(mongoose_acc:t()) ->
{ok, aff_users(), binary()} | {error, not_exists}.
get_room_affiliations_from_acc(Acc) ->
mongoose_acc:get(?MODULE, affiliations, {error, not_exists}, Acc).
-spec get_acc_room_affiliations(mongoose_acc:t(), jid:jid()) ->
{mongoose_acc:t(), versioned_affs() | {error, not_exists}}.
get_acc_room_affiliations(Acc1, RoomJid) ->
case get_room_affs_from_acc(Acc1, RoomJid) of
{error, not_exists} ->
Acc2 = mongoose_hooks:acc_room_affiliations(Acc1, RoomJid),
{Acc2, get_room_affs_from_acc(Acc2, RoomJid)};
Res ->
{Acc1, Res}
end.

-spec get_room_affs_from_acc(mongoose_acc:t(), jid:jid()) -> versioned_affs() | {error, not_exists}.
get_room_affs_from_acc(Acc, RoomJid) ->
mongoose_acc:get(?MODULE, {affiliations, RoomJid}, {error, not_exists}, Acc).

-spec get_room_affiliations_from_acc(mongoose_acc:t(), jid:jid()) ->
{mongoose_acc:t(), {ok, aff_users(), binary()} | {error, not_exists}}.
get_room_affiliations_from_acc(Acc1, RoomJid) ->
Acc2 = mongoose_hooks:acc_room_affiliations(Acc1, RoomJid),
{Acc2, mongoose_acc:get(?MODULE, affiliations, {error, not_exists}, Acc2)}.
-spec set_room_affs_from_acc(mongoose_acc:t(), jid:jid(), versioned_affs()) -> mongoose_acc:t().
set_room_affs_from_acc(Acc, RoomJid, Affs) ->
mongoose_acc:set(?MODULE, {affiliations, RoomJid}, Affs, Acc).

-spec can_access_identity(Acc :: boolean(), HostType :: mongooseim:host_type(),
Room :: jid:jid(), User :: jid:jid()) ->
Expand Down Expand Up @@ -580,7 +589,7 @@ max_occupants(HostType) ->
gen_mod:get_module_opt(HostType, ?MODULE, max_occupants).

get_affiliation(Acc, Room, User) ->
case get_room_affiliations_from_acc(Acc, Room) of
case get_acc_room_affiliations(Acc, Room) of
{_, {ok, AffUsers, _}} ->
case lists:keyfind(jid:to_lus(User), 1, AffUsers) of
{_, Aff} -> Aff;
Expand Down
9 changes: 4 additions & 5 deletions src/muc_light/mod_muc_light_room.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@

-spec handle_request(From :: jid:jid(), RoomJID :: jid:jid(), OrigPacket :: exml:element(),
Request :: muc_light_packet(), Acc :: mongoose_acc:t()) -> mongoose_acc:t().
handle_request(From, Room, OrigPacket, Request, Acc1) ->
Acc2 = mongoose_hooks:acc_room_affiliations(Acc1, Room),
AffUsersRes = mod_muc_light:get_room_affiliations_from_acc(Acc2),
Response = process_request(From, Room, Request, AffUsersRes, Acc2),
send_response(From, Room, OrigPacket, Response, Acc2).
handle_request(From, RoomJID, OrigPacket, Request, Acc1) ->
{Acc2, AffUsersRes} = mod_muc_light:get_acc_room_affiliations(Acc1, RoomJID),
Response = process_request(From, RoomJID, Request, AffUsersRes, Acc2),
send_response(From, RoomJID, OrigPacket, Response, Acc2).

-spec maybe_forget(Acc :: mongoose_acc:t(),
RoomUS :: jid:simple_bare_jid(),
Expand Down
2 changes: 1 addition & 1 deletion test/batches_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ end_per_group(_, _Config) ->
ok.

init_per_testcase(_TestCase, Config) ->
pg:start_link(),
pg:start_link(mim_scope),
mim_ct_sup:start_link(ejabberd_sup),
meck:new(gen_mod, [passthrough]),
Config.
Expand Down

0 comments on commit 42a04dc

Please sign in to comment.