Skip to content

Commit 5bcbfad

Browse files
committed
rabbit_db_exchange: Relax conditions when creating an exchange with Khepri
[Why] When the module wanted to create an exchange in Khepri, it used `rabbit_khepri:create/2` which ensures that the tree node doesn't exist before. If the tree node exists, it expects it to contain an exchange record which is returned to the caller, indicating an exchange with that name already exists. However, there are several other resources stored under an exchange tree node, like bindings and topic permissions. In particular, during a definitions import, topic permissions used to be imported before exchanges. This caused a crash because the write of the topic permission automatically created a parent tree node for the exchange it dpends on, but without an exchange record obviously (see previous commit). [How] As an addition improvement to the previous commit, we change the conditions: instead of matching on the fact the tree node doesn't exist, the module also accepts that the tree node exists but has no payload. Under any of these conditions, the exchange is considered to be new and written to Khepri.
1 parent 92c572e commit 5bcbfad

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

deps/rabbit/src/rabbit_db_exchange.erl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,12 @@ create_or_get_in_mnesia(#exchange{name = XName} = X) ->
409409
end).
410410

411411
create_or_get_in_khepri(#exchange{name = XName} = X) ->
412-
Path = khepri_exchange_path(XName),
413-
case rabbit_khepri:create(Path, X) of
412+
Path0 = khepri_exchange_path(XName),
413+
Path1 = khepri_path:combine_with_conditions(
414+
Path0, [#if_any{conditions =
415+
[#if_node_exists{exists = false},
416+
#if_has_payload{has_payload = false}]}]),
417+
case rabbit_khepri:put(Path1, X) of
414418
ok ->
415419
{new, X};
416420
{error, {khepri, mismatching_node, #{node_props := #{data := ExistingX}}}} ->

0 commit comments

Comments
 (0)