Skip to content

Commit 3e50921

Browse files
Merge pull request #11708 from rabbitmq/mergify/bp/v3.13.x/pr-11707
Use 'try'/'catch' rather than 'ets:whereis/1' for Khepri projections (backport #11700) (backport #11707)
2 parents 8a4b43e + 6f8558c commit 3e50921

7 files changed

+247
-253
lines changed

deps/rabbit/src/rabbit_db_binding.erl

+60-59
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,11 @@ get_all_in_mnesia() ->
413413
end).
414414

415415
get_all_in_khepri() ->
416-
case ets:whereis(?KHEPRI_BINDINGS_PROJECTION) of
417-
undefined ->
418-
[];
419-
Table ->
420-
[B || #route{binding = B} <- ets:tab2list(Table)]
416+
try
417+
[B || #route{binding = B} <- ets:tab2list(?KHEPRI_BINDINGS_PROJECTION)]
418+
catch
419+
error:badarg ->
420+
[]
421421
end.
422422

423423
-spec get_all(VHostName) -> [Binding] when
@@ -444,15 +444,16 @@ get_all_in_mnesia(VHost) ->
444444
[B || #route{binding = B} <- rabbit_db:list_in_mnesia(?MNESIA_TABLE, Match)].
445445

446446
get_all_in_khepri(VHost) ->
447-
case ets:whereis(?KHEPRI_BINDINGS_PROJECTION) of
448-
undefined ->
449-
[];
450-
Table ->
451-
VHostResource = rabbit_misc:r(VHost, '_'),
452-
Match = #route{binding = #binding{source = VHostResource,
453-
destination = VHostResource,
454-
_ = '_'}},
455-
[B || #route{binding = B} <- ets:match_object(Table, Match)]
447+
try
448+
VHostResource = rabbit_misc:r(VHost, '_'),
449+
Match = #route{binding = #binding{source = VHostResource,
450+
destination = VHostResource,
451+
_ = '_'}},
452+
[B || #route{binding = B} <- ets:match_object(
453+
?KHEPRI_BINDINGS_PROJECTION, Match)]
454+
catch
455+
error:badarg ->
456+
[]
456457
end.
457458

458459
-spec get_all(Src, Dst, Reverse) -> [Binding] when
@@ -481,14 +482,15 @@ get_all_in_mnesia(SrcName, DstName, Reverse) ->
481482
mnesia:async_dirty(Fun).
482483

483484
get_all_in_khepri(SrcName, DstName) ->
484-
case ets:whereis(?KHEPRI_BINDINGS_PROJECTION) of
485-
undefined ->
486-
[];
487-
Table ->
488-
MatchHead = #route{binding = #binding{source = SrcName,
489-
destination = DstName,
490-
_ = '_'}},
491-
[B || #route{binding = B} <- ets:match_object(Table, MatchHead)]
485+
try
486+
MatchHead = #route{binding = #binding{source = SrcName,
487+
destination = DstName,
488+
_ = '_'}},
489+
[B || #route{binding = B} <- ets:match_object(
490+
?KHEPRI_BINDINGS_PROJECTION, MatchHead)]
491+
catch
492+
error:badarg ->
493+
[]
492494
end.
493495

494496
%% -------------------------------------------------------------------
@@ -528,12 +530,13 @@ list_for_route(Route, true) ->
528530
end.
529531

530532
get_all_for_source_in_khepri(Resource) ->
531-
case ets:whereis(?KHEPRI_BINDINGS_PROJECTION) of
532-
undefined ->
533-
[];
534-
Table ->
535-
Route = #route{binding = #binding{source = Resource, _ = '_'}},
536-
[B || #route{binding = B} <- ets:match_object(Table, Route)]
533+
try
534+
Route = #route{binding = #binding{source = Resource, _ = '_'}},
535+
[B || #route{binding = B} <- ets:match_object(
536+
?KHEPRI_BINDINGS_PROJECTION, Route)]
537+
catch
538+
error:badarg ->
539+
[]
537540
end.
538541

539542
%% -------------------------------------------------------------------
@@ -563,13 +566,14 @@ get_all_for_destination_in_mnesia(Dst) ->
563566
mnesia:async_dirty(Fun).
564567

565568
get_all_for_destination_in_khepri(Destination) ->
566-
case ets:whereis(?KHEPRI_BINDINGS_PROJECTION) of
567-
undefined ->
568-
[];
569-
Table ->
570-
Match = #route{binding = #binding{destination = Destination,
571-
_ = '_'}},
572-
[B || #route{binding = B} <- ets:match_object(Table, Match)]
569+
try
570+
Match = #route{binding = #binding{destination = Destination,
571+
_ = '_'}},
572+
[B || #route{binding = B} <- ets:match_object(
573+
?KHEPRI_BINDINGS_PROJECTION, Match)]
574+
catch
575+
error:badarg ->
576+
[]
573577
end.
574578

575579
%% -------------------------------------------------------------------
@@ -644,15 +648,16 @@ match_in_mnesia(SrcName, Match) ->
644648
Routes, Match(Binding)].
645649

646650
match_in_khepri(SrcName, Match) ->
647-
case ets:whereis(?KHEPRI_BINDINGS_PROJECTION) of
648-
undefined ->
649-
[];
650-
Table ->
651-
MatchHead = #route{binding = #binding{source = SrcName,
652-
_ = '_'}},
653-
Routes = ets:select(Table, [{MatchHead, [], [['$_']]}]),
654-
[Dest || [#route{binding = Binding = #binding{destination = Dest}}] <-
655-
Routes, Match(Binding)]
651+
try
652+
MatchHead = #route{binding = #binding{source = SrcName,
653+
_ = '_'}},
654+
Routes = ets:select(
655+
?KHEPRI_BINDINGS_PROJECTION, [{MatchHead, [], [['$_']]}]),
656+
[Dest || [#route{binding = Binding = #binding{destination = Dest}}] <-
657+
Routes, Match(Binding)]
658+
catch
659+
error:badarg ->
660+
[]
656661
end.
657662

658663
%% Routing - HOT CODE PATH
@@ -686,26 +691,22 @@ match_routing_key_in_mnesia(SrcName, RoutingKeys, UseIndex) ->
686691
route_in_mnesia_v1(SrcName, RoutingKeys)
687692
end.
688693

694+
match_routing_key_in_khepri(Src, ['_']) ->
695+
try
696+
MatchHead = #index_route{source_key = {Src, '_'},
697+
destination = '$1',
698+
_ = '_'},
699+
ets:select(?KHEPRI_INDEX_ROUTE_PROJECTION, [{MatchHead, [], ['$1']}])
700+
catch
701+
error:badarg ->
702+
[]
703+
end;
689704
match_routing_key_in_khepri(Src, RoutingKeys) ->
690-
case ets:whereis(?KHEPRI_INDEX_ROUTE_PROJECTION) of
691-
undefined ->
692-
[];
693-
Table ->
694-
do_match_routing_key_in_khepri(Table, Src, RoutingKeys)
695-
end.
696-
697-
do_match_routing_key_in_khepri(Table, Src, ['_']) ->
698-
MatchHead = #index_route{source_key = {Src, '_'},
699-
destination = '$1',
700-
_ = '_'},
701-
ets:select(Table, [{MatchHead, [], ['$1']}]);
702-
703-
do_match_routing_key_in_khepri(Table, Src, RoutingKeys) ->
704705
lists:foldl(
705706
fun(RK, Acc) ->
706707
try
707708
Dst = ets:lookup_element(
708-
Table,
709+
?KHEPRI_INDEX_ROUTE_PROJECTION,
709710
{Src, RK},
710711
#index_route.destination),
711712
Dst ++ Acc

deps/rabbit/src/rabbit_db_exchange.erl

+11-13
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,12 @@ get_in_mnesia(Name) ->
183183
rabbit_mnesia:dirty_read({?MNESIA_TABLE, Name}).
184184

185185
get_in_khepri(Name) ->
186-
case ets:whereis(?KHEPRI_PROJECTION) of
187-
undefined ->
188-
{error, not_found};
189-
Table ->
190-
case ets:lookup(Table, Name) of
191-
[X] -> {ok, X};
192-
[] -> {error, not_found}
193-
end
186+
try ets:lookup(?KHEPRI_PROJECTION, Name) of
187+
[X] -> {ok, X};
188+
[] -> {error, not_found}
189+
catch
190+
error:badarg ->
191+
{error, not_found}
194192
end.
195193

196194
%% -------------------------------------------------------------------
@@ -233,11 +231,11 @@ get_many_in_mnesia(Table, Names) when is_list(Names) ->
233231
lists:append([ets:lookup(Table, Name) || Name <- Names]).
234232

235233
get_many_in_khepri(Names) when is_list(Names) ->
236-
case ets:whereis(?KHEPRI_PROJECTION) of
237-
undefined ->
238-
[];
239-
Table ->
240-
lists:append([ets:lookup(Table, Name) || Name <- Names])
234+
try
235+
lists:append([ets:lookup(?KHEPRI_PROJECTION, Name) || Name <- Names])
236+
catch
237+
error:badarg ->
238+
[]
241239
end.
242240

243241
%% -------------------------------------------------------------------

0 commit comments

Comments
 (0)