Skip to content

Commit 5e4a432

Browse files
Merge pull request #11398 from rabbitmq/md/khepri/read-only-permissions-transaction-queries
Khepri: Use read-only transactions to query for user/topic permissions
2 parents c592405 + d0425e5 commit 5e4a432

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

deps/rabbit/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ rabbitmq_integration_suite(
11661166

11671167
rabbitmq_integration_suite(
11681168
name = "cluster_minority_SUITE",
1169-
size = "large",
1169+
size = "medium",
11701170
additional_beam = [
11711171
":test_clustering_utils_beam",
11721172
],

deps/rabbit/src/rabbit_db_user.erl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,16 @@ match_user_permissions_in_khepri('_' = _Username, VHostName) ->
374374
VHostName,
375375
fun() ->
376376
match_user_permissions_in_khepri_tx(?KHEPRI_WILDCARD_STAR, VHostName)
377-
end));
377+
end),
378+
ro);
378379
match_user_permissions_in_khepri(Username, '_' = _VHostName) ->
379380
rabbit_khepri:transaction(
380381
with_fun_in_khepri_tx(
381382
Username,
382383
fun() ->
383384
match_user_permissions_in_khepri_tx(Username, ?KHEPRI_WILDCARD_STAR)
384-
end));
385+
end),
386+
ro);
385387
match_user_permissions_in_khepri(Username, VHostName) ->
386388
rabbit_khepri:transaction(
387389
with_fun_in_khepri_tx(
@@ -390,7 +392,8 @@ match_user_permissions_in_khepri(Username, VHostName) ->
390392
VHostName,
391393
fun() ->
392394
match_user_permissions_in_khepri_tx(Username, VHostName)
393-
end))).
395+
end)),
396+
ro).
394397

395398
match_user_permissions_in_khepri_tx(Username, VHostName) ->
396399
Path = khepri_user_permission_path(Username, VHostName),
@@ -739,15 +742,16 @@ match_topic_permissions_in_khepri('_' = _Username, '_' = _VHostName, ExchangeNam
739742
fun() ->
740743
match_topic_permissions_in_khepri_tx(
741744
?KHEPRI_WILDCARD_STAR, ?KHEPRI_WILDCARD_STAR, any(ExchangeName))
742-
end);
745+
end, ro);
743746
match_topic_permissions_in_khepri('_' = _Username, VHostName, ExchangeName) ->
744747
rabbit_khepri:transaction(
745748
rabbit_db_vhost:with_fun_in_khepri_tx(
746749
VHostName,
747750
fun() ->
748751
match_topic_permissions_in_khepri_tx(
749752
?KHEPRI_WILDCARD_STAR, VHostName, any(ExchangeName))
750-
end));
753+
end),
754+
ro);
751755
match_topic_permissions_in_khepri(
752756
Username, '_' = _VHostName, ExchangeName) ->
753757
rabbit_khepri:transaction(
@@ -756,7 +760,8 @@ match_topic_permissions_in_khepri(
756760
fun() ->
757761
match_topic_permissions_in_khepri_tx(
758762
Username, ?KHEPRI_WILDCARD_STAR, any(ExchangeName))
759-
end));
763+
end),
764+
ro);
760765
match_topic_permissions_in_khepri(
761766
Username, VHostName, ExchangeName) ->
762767
rabbit_khepri:transaction(
@@ -767,7 +772,8 @@ match_topic_permissions_in_khepri(
767772
fun() ->
768773
match_topic_permissions_in_khepri_tx(
769774
Username, VHostName, any(ExchangeName))
770-
end))).
775+
end)),
776+
ro).
771777

772778
match_topic_permissions_in_khepri_tx(Username, VHostName, ExchangeName) ->
773779
Path = khepri_topic_permission_path(Username, VHostName, ExchangeName),

deps/rabbit/src/rabbit_khepri.erl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,16 @@ transaction(Fun, ReadWrite) ->
974974
transaction(Fun, ReadWrite, #{}).
975975

976976
transaction(Fun, ReadWrite, Options0) ->
977-
Options = maps:merge(?DEFAULT_COMMAND_OPTIONS, Options0),
977+
%% If the transaction is read-only, use the same default options we use
978+
%% for most queries.
979+
DefaultQueryOptions = case ReadWrite of
980+
ro ->
981+
#{favor => low_latency};
982+
_ ->
983+
#{}
984+
end,
985+
Options1 = maps:merge(DefaultQueryOptions, Options0),
986+
Options = maps:merge(?DEFAULT_COMMAND_OPTIONS, Options1),
978987
case khepri:transaction(?STORE_ID, Fun, ReadWrite, Options) of
979988
ok -> ok;
980989
{ok, Result} -> Result;

deps/rabbit/test/cluster_minority_SUITE.erl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
-include_lib("amqp_client/include/amqp_client.hrl").
1111
-include_lib("eunit/include/eunit.hrl").
1212

13-
-compile(export_all).
13+
-compile([export_all, nowarn_export_all]).
1414

1515
all() ->
1616
[
@@ -36,7 +36,8 @@ groups() ->
3636
update_user,
3737
delete_user,
3838
set_policy,
39-
delete_policy
39+
delete_policy,
40+
export_definitions
4041
]},
4142
{cluster_operation_add, [], [add_node]},
4243
{cluster_operation_remove, [], [remove_node]},
@@ -88,6 +89,14 @@ init_per_group(Group, Config0) when Group == client_operations;
8889
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = <<"test-queue">>,
8990
arguments = [{<<"x-queue-type">>, longstr, <<"classic">>}]}),
9091

92+
%% Lower the default Khepri command timeout. By default this is set
93+
%% to 30s in `rabbit_khepri:setup/1' which makes the cases in this
94+
%% group run unnecessarily slow.
95+
[ok = rabbit_ct_broker_helpers:rpc(
96+
Config1, N,
97+
application, set_env,
98+
[khepri, default_timeout, 100]) || N <- lists:seq(0, 4)],
99+
91100
%% Create partition
92101
partition_5_node_cluster(Config1),
93102
Config1
@@ -104,8 +113,8 @@ init_per_group(Group, Config0) ->
104113

105114
end_per_group(_, Config) ->
106115
rabbit_ct_helpers:run_steps(Config,
107-
rabbit_ct_broker_helpers:teardown_steps() ++
108-
rabbit_ct_client_helpers:teardown_steps()).
116+
rabbit_ct_client_helpers:teardown_steps() ++
117+
rabbit_ct_broker_helpers:teardown_steps()).
109118

110119
init_per_testcase(Testcase, Config) ->
111120
rabbit_ct_helpers:testcase_started(Config, Testcase).
@@ -252,6 +261,11 @@ enable_feature_flag(Config) ->
252261
[A | _] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename),
253262
?assertMatch({error, missing_clustered_nodes}, rabbit_ct_broker_helpers:rpc(Config, A, rabbit_feature_flags, enable, [khepri_db])).
254263

264+
export_definitions(Config) ->
265+
Definitions = rabbit_ct_broker_helpers:rpc(
266+
Config, 0,
267+
rabbit_definitions, all_definitions, []),
268+
?assert(is_map(Definitions)).
255269

256270
%% -------------------------------------------------------------------
257271
%% Internal helpers.

0 commit comments

Comments
 (0)