Skip to content

Commit 418af7a

Browse files
committed
Khepri: Fix topic routing for empty routing keys
An empty routing key in a binding matched routes incorrectly with Khepri enabled: it would not match the empty routing key and would mistakenly match some keys containing `*` patterns. The first change avoids `split_topic_key_binary/1` since that function counts the empty routing key as a word: `[<<>>]`. This mistakenly lets the `*` pattern match against the component. The second change adds a special case for the empty routing key that connects the bindings for the empty routing key to the root node. This matches the structure of the graph in mnesia. The final change helps debugging failures in that test case: the error message when a mismatch occurs now shows the expected and actual destinations and the routing key which mismatched.
1 parent 811dbbc commit 418af7a

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

deps/rabbit/src/rabbit_db_topic_exchange.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ match_in_mnesia(XName, RoutingKey) ->
190190
mnesia:async_dirty(fun trie_match/2, [XName, Words]).
191191

192192
match_in_khepri(XName, RoutingKey) ->
193-
Words = split_topic_key_binary(RoutingKey),
193+
Words = [list_to_binary(W) || W <- split_topic_key(RoutingKey)],
194194
trie_match_in_khepri(XName, Words).
195195

196196
%% --------------------------------------------------------------

deps/rabbit/src/rabbit_khepri.erl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,13 @@ edges_for_path(
753753
Exchange = rabbit_misc:r(VHost, exchange, ExchangeName),
754754
edges_for_path([root | Components], Bindings, Exchange, []).
755755

756+
edges_for_path([root, <<>>], Bindings, Exchange, Edges) ->
757+
ToNodeId = {bindings, Bindings},
758+
Edge = #topic_trie_edge{trie_edge = #trie_edge{exchange_name = Exchange,
759+
node_id = root,
760+
word = bindings},
761+
node_id = ToNodeId},
762+
[Edge | Edges];
756763
edges_for_path([FromNodeId, To | Rest], Bindings, Exchange, Edges) ->
757764
ToNodeId = [To | FromNodeId],
758765
Edge = #topic_trie_edge{trie_edge = #trie_edge{exchange_name = Exchange,

deps/rabbit/test/unit_access_control_SUITE.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,5 +436,7 @@ test_topic_expect_match(X, List) ->
436436
kind = queue,
437437
name = list_to_binary(Q)}
438438
end, Expected),
439-
true = (lists:usort(ExpectedRes) =:= lists:usort(Res))
439+
?assertEqual(
440+
lists:usort(ExpectedRes), lists:usort(Res),
441+
lists:flatten(io_lib:format("Routing key: ~w", [BinKey])))
440442
end, List).

0 commit comments

Comments
 (0)