Skip to content

4.0.x: Use preprocessor macros for building Khepri path patterns (backport #12723) #12787

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

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deps/rabbit/app.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ def all_srcs(name = "all_srcs"):
"include/amqqueue.hrl",
"include/amqqueue_v2.hrl",
"include/internal_user.hrl",
"include/khepri.hrl",
"include/rabbit_khepri.hrl",
"include/mc.hrl",
"include/rabbit_amqp.hrl",
"include/rabbit_global_counters.hrl",
Expand Down
9 changes: 0 additions & 9 deletions deps/rabbit/include/khepri.hrl

This file was deleted.

64 changes: 64 additions & 0 deletions deps/rabbit/include/rabbit_khepri.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2024 Broadcom. All Rights Reserved. The term “Broadcom”
%% refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%

%% This header has macros that define the `khepri_path:native_pattern()'
%% path patterns used for each piece of metadata in the store. We use macros
%% for these so that we can pattern match on these path patterns as well as
%% create them as new terms.
%%
%% If you are creating a path pattern to use in a call to the Khepri API (for
%% example `rabbit_khepri:get/1') you should prefer using the
%% `khepri_<entity>_path' function from the `rabbit_db_<entity>' modules
%% instead, for example `rabbit_db_queue:khepri_queue_path/2', since those
%% functions have guards to assert that the variables passed are valid pattern
%% components.

-define(RABBITMQ_KHEPRI_ROOT_PATH, ?RABBITMQ_KHEPRI_ROOT_PATH([])).
-define(RABBITMQ_KHEPRI_ROOT_PATH(Rest), [rabbitmq | Rest]).

-define(RABBITMQ_KHEPRI_MAINTENANCE_PATH(Node),
?RABBITMQ_KHEPRI_ROOT_PATH([node_maintenance, Node])).

-define(RABBITMQ_KHEPRI_GLOBAL_RUNTIME_PARAM_PATH(Key),
?RABBITMQ_KHEPRI_ROOT_PATH([runtime_params, Key])).

-define(RABBITMQ_KHEPRI_USER_PATH(Username),
?RABBITMQ_KHEPRI_ROOT_PATH([users, Username])).

-define(RABBITMQ_KHEPRI_MIRRORED_SUPERVISOR_PATH(Group, Id),
?RABBITMQ_KHEPRI_ROOT_PATH([mirrored_supervisors, Group, Id])).

-define(RABBITMQ_KHEPRI_VHOST_PATH(Name),
?RABBITMQ_KHEPRI_VHOST_PATH(Name, [])).
-define(RABBITMQ_KHEPRI_VHOST_PATH(Name, Rest),
?RABBITMQ_KHEPRI_ROOT_PATH([vhosts, Name | Rest])).

-define(RABBITMQ_KHEPRI_VHOST_RUNTIME_PARAM_PATH(VHost, Component, Name),
?RABBITMQ_KHEPRI_VHOST_PATH(VHost, [runtime_params, Component, Name])).

-define(RABBITMQ_KHEPRI_USER_PERMISSION_PATH(VHost, Username),
?RABBITMQ_KHEPRI_VHOST_PATH(VHost, [user_permissions, Username])).

-define(RABBITMQ_KHEPRI_EXCHANGE_PATH(VHost, Name),
?RABBITMQ_KHEPRI_EXCHANGE_PATH(VHost, Name, [])).
-define(RABBITMQ_KHEPRI_EXCHANGE_PATH(VHost, Name, Rest),
?RABBITMQ_KHEPRI_VHOST_PATH(VHost, [exchanges, Name | Rest])).

-define(RABBITMQ_KHEPRI_EXCHANGE_SERIAL_PATH(VHost, Name),
?RABBITMQ_KHEPRI_EXCHANGE_PATH(VHost, Name, [serial])).

-define(RABBITMQ_KHEPRI_TOPIC_PERMISSION_PATH(VHost, Exchange, Username),
?RABBITMQ_KHEPRI_EXCHANGE_PATH(
VHost, Exchange, [user_permissions, Username])).

-define(RABBITMQ_KHEPRI_ROUTE_PATH(VHost, SrcName, Kind, DstName, RoutingKey),
?RABBITMQ_KHEPRI_EXCHANGE_PATH(
VHost, SrcName, [bindings, Kind, DstName, RoutingKey])).

-define(RABBITMQ_KHEPRI_QUEUE_PATH(VHost, Name),
?RABBITMQ_KHEPRI_VHOST_PATH(VHost, [queues, Name])).
5 changes: 3 additions & 2 deletions deps/rabbit/src/rabbit_db_binding.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
-include_lib("khepri/include/khepri.hrl").
-include_lib("rabbit_common/include/rabbit.hrl").

-include("include/rabbit_khepri.hrl").

-export([exists/1,
create/2,
delete/2,
Expand Down Expand Up @@ -1014,8 +1016,7 @@ khepri_route_path(VHost, SrcName, Kind, DstName, RoutingKey)
when ?IS_KHEPRI_PATH_CONDITION(Kind) andalso
?IS_KHEPRI_PATH_CONDITION(DstName) andalso
?IS_KHEPRI_PATH_CONDITION(RoutingKey) ->
ExchangePath = rabbit_db_exchange:khepri_exchange_path(VHost, SrcName),
ExchangePath ++ [bindings, Kind, DstName, RoutingKey].
?RABBITMQ_KHEPRI_ROUTE_PATH(VHost, SrcName, Kind, DstName, RoutingKey).

khepri_route_path_to_args(Path) ->
Pattern = khepri_route_path(
Expand Down
18 changes: 11 additions & 7 deletions deps/rabbit/src/rabbit_db_exchange.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
-include_lib("khepri/include/khepri.hrl").
-include_lib("rabbit_common/include/rabbit.hrl").

-include("include/khepri.hrl").
-include("include/rabbit_khepri.hrl").

-export([
get_all/0,
Expand Down Expand Up @@ -960,11 +960,15 @@ maybe_auto_delete_in_khepri(XName, OnlyDurable) ->
khepri_exchange_path(#resource{virtual_host = VHost, name = Name}) ->
khepri_exchange_path(VHost, Name).

khepri_exchange_path(VHost, Name) when ?IS_KHEPRI_PATH_CONDITION(Name) ->
rabbit_db_vhost:khepri_vhost_path(VHost) ++ [exchanges, Name].
khepri_exchange_path(VHost, Name)
when ?IS_KHEPRI_PATH_CONDITION(VHost) andalso
?IS_KHEPRI_PATH_CONDITION(Name) ->
?RABBITMQ_KHEPRI_EXCHANGE_PATH(VHost, Name).

khepri_exchange_serial_path(#resource{} = Resource) ->
khepri_exchange_path(Resource) ++ [serial].
khepri_exchange_serial_path(#resource{virtual_host = VHost, name = Name}) ->
khepri_exchange_serial_path(VHost, Name).

khepri_exchange_serial_path(VHost, Name) ->
khepri_exchange_path(VHost, Name) ++ [serial].
khepri_exchange_serial_path(VHost, Name)
when ?IS_KHEPRI_PATH_CONDITION(VHost) andalso
?IS_KHEPRI_PATH_CONDITION(Name) ->
?RABBITMQ_KHEPRI_EXCHANGE_SERIAL_PATH(VHost, Name).
4 changes: 2 additions & 2 deletions deps/rabbit/src/rabbit_db_maintenance.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
-include_lib("khepri/include/khepri.hrl").
-include_lib("rabbit_common/include/rabbit.hrl").

-include("include/khepri.hrl").
-include("include/rabbit_khepri.hrl").

-export([
table_definitions/0,
Expand Down Expand Up @@ -170,4 +170,4 @@ get_consistent_in_khepri(Node) ->
%% -------------------------------------------------------------------

khepri_maintenance_path(Node) when ?IS_KHEPRI_PATH_CONDITION(Node) ->
?KHEPRI_ROOT_PATH ++ [node_maintenance, Node].
?RABBITMQ_KHEPRI_MAINTENANCE_PATH(Node).
6 changes: 3 additions & 3 deletions deps/rabbit/src/rabbit_db_msup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
-include_lib("khepri/include/khepri.hrl").
-include("mirrored_supervisor.hrl").

-include("include/khepri.hrl").
-include("include/rabbit_khepri.hrl").

-export([
create_tables/0,
Expand Down Expand Up @@ -328,8 +328,8 @@ clear_in_khepri() ->
khepri_mirrored_supervisor_path(Group, Id)
when ?IS_KHEPRI_PATH_CONDITION(Group) andalso
?IS_KHEPRI_PATH_CONDITION(Id) ->
?KHEPRI_ROOT_PATH ++ [mirrored_supervisors, Group, Id];
?RABBITMQ_KHEPRI_MIRRORED_SUPERVISOR_PATH(Group, Id);
khepri_mirrored_supervisor_path(Group, Id)
when is_atom(Group) ->
IdPath = Group:id_to_khepri_path(Id),
?KHEPRI_ROOT_PATH ++ [mirrored_supervisors, Group] ++ IdPath.
?RABBITMQ_KHEPRI_ROOT_PATH ++ [mirrored_supervisors, Group] ++ IdPath.
8 changes: 6 additions & 2 deletions deps/rabbit/src/rabbit_db_queue.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
-include_lib("rabbit_common/include/rabbit.hrl").
-include("amqqueue.hrl").

-include("include/rabbit_khepri.hrl").

-export([
get/1,
get_many/1,
Expand Down Expand Up @@ -1394,5 +1396,7 @@ list_with_possible_retry_in_khepri(Fun) ->
khepri_queue_path(#resource{virtual_host = VHost, name = Name}) ->
khepri_queue_path(VHost, Name).

khepri_queue_path(VHost, Name) when ?IS_KHEPRI_PATH_CONDITION(Name) ->
rabbit_db_vhost:khepri_vhost_path(VHost) ++ [queues, Name].
khepri_queue_path(VHost, Name)
when ?IS_KHEPRI_PATH_CONDITION(VHost) andalso
?IS_KHEPRI_PATH_CONDITION(Name) ->
?RABBITMQ_KHEPRI_QUEUE_PATH(VHost, Name).
7 changes: 3 additions & 4 deletions deps/rabbit/src/rabbit_db_rtparams.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
-include_lib("khepri/include/khepri.hrl").
-include_lib("rabbit_common/include/rabbit.hrl").

-include("include/khepri.hrl").
-include("include/rabbit_khepri.hrl").

-export([set/2, set/4,
get/1,
Expand Down Expand Up @@ -364,10 +364,9 @@ khepri_rp_path(Key) ->
khepri_global_rp_path(Key).

khepri_global_rp_path(Key) when ?IS_KHEPRI_PATH_CONDITION(Key) ->
?KHEPRI_ROOT_PATH ++ [runtime_params, Key].
?RABBITMQ_KHEPRI_GLOBAL_RUNTIME_PARAM_PATH(Key).

khepri_vhost_rp_path(VHost, Component, Name)
when ?IS_KHEPRI_PATH_CONDITION(Component) andalso
?IS_KHEPRI_PATH_CONDITION(Name) ->
VHostPath = rabbit_db_vhost:khepri_vhost_path(VHost),
VHostPath ++ [runtime_params, Component, Name].
?RABBITMQ_KHEPRI_VHOST_RUNTIME_PARAM_PATH(VHost, Component, Name).
17 changes: 9 additions & 8 deletions deps/rabbit/src/rabbit_db_user.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
-include_lib("khepri/include/khepri.hrl").
-include_lib("rabbit_common/include/rabbit.hrl").

-include("include/khepri.hrl").
-include("include/rabbit_khepri.hrl").

-export([create/1,
update/2,
Expand Down Expand Up @@ -1094,14 +1094,15 @@ clear_in_khepri() ->

khepri_user_path(Username)
when ?IS_KHEPRI_PATH_CONDITION(Username) ->
?KHEPRI_ROOT_PATH ++ [users, Username].
?RABBITMQ_KHEPRI_USER_PATH(Username).

khepri_user_permission_path(Username, VHostName)
when ?IS_KHEPRI_PATH_CONDITION(Username) ->
(rabbit_db_vhost:khepri_vhost_path(VHostName) ++
[user_permissions, Username]).
when ?IS_KHEPRI_PATH_CONDITION(Username) andalso
?IS_KHEPRI_PATH_CONDITION(VHostName) ->
?RABBITMQ_KHEPRI_USER_PERMISSION_PATH(VHostName, Username).

khepri_topic_permission_path(Username, VHostName, Exchange)
when ?IS_KHEPRI_PATH_CONDITION(Username) ->
(rabbit_db_exchange:khepri_exchange_path(VHostName, Exchange) ++
[user_permissions, Username]).
when ?IS_KHEPRI_PATH_CONDITION(Username) andalso
?IS_KHEPRI_PATH_CONDITION(VHostName) andalso
?IS_KHEPRI_PATH_CONDITION(Exchange) ->
?RABBITMQ_KHEPRI_TOPIC_PERMISSION_PATH(VHostName, Exchange, Username).
4 changes: 2 additions & 2 deletions deps/rabbit/src/rabbit_db_vhost.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
-include_lib("rabbit_common/include/logging.hrl").
-include_lib("khepri/include/khepri.hrl").

-include("include/khepri.hrl").
-include("include/rabbit_khepri.hrl").
-include("vhost.hrl").

-export([create_or_get/3,
Expand Down Expand Up @@ -533,4 +533,4 @@ clear_in_khepri() ->
%% --------------------------------------------------------------

khepri_vhost_path(VHost) when ?IS_KHEPRI_PATH_CONDITION(VHost) ->
?KHEPRI_ROOT_PATH ++ [vhosts, VHost].
?RABBITMQ_KHEPRI_VHOST_PATH(VHost).
4 changes: 2 additions & 2 deletions deps/rabbit/src/rabbit_khepri.erl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
-include_lib("rabbit_common/include/logging.hrl").
-include_lib("rabbit_common/include/rabbit.hrl").

-include("include/khepri.hrl").
-include("include/rabbit_khepri.hrl").

-export([setup/0,
setup/1,
Expand Down Expand Up @@ -946,7 +946,7 @@ cluster_status_from_khepri() ->
%% This path must be prepended to all paths used by RabbitMQ subsystems.

root_path() ->
?KHEPRI_ROOT_PATH.
?RABBITMQ_KHEPRI_ROOT_PATH.

%% -------------------------------------------------------------------
%% "Proxy" functions to Khepri API.
Expand Down
Loading