Skip to content

Commit b209d6a

Browse files
committed
AWS peer discovery: ensure consistent hostname path ordering (address feedback on debug logs and sorting helper functions)
1 parent 9c17ecf commit b209d6a

File tree

2 files changed

+13
-52
lines changed

2 files changed

+13
-52
lines changed

deps/rabbitmq_peer_discovery_aws/src/rabbit_peer_discovery_aws.erl

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -372,40 +372,18 @@ get_value(_, []) ->
372372
get_value(Key, Props) when is_integer(Key) ->
373373
{"item", Props2} = lists:nth(Key, Props),
374374
Props2;
375-
get_value("networkInterfaceSet", Props) ->
376-
NetworkInterfaces = proplists:get_value("networkInterfaceSet", Props),
377-
sort_network_interfaces_by_device_index(NetworkInterfaces);
378-
get_value("privateIpAddressesSet", Props) ->
379-
PrivateIpAddresses = proplists:get_value("privateIpAddressesSet", Props),
380-
sort_private_ip_addresses_by_primary(PrivateIpAddresses);
381375
get_value(Key, Props) ->
382-
proplists:get_value(Key, Props).
383-
384-
%% Sort network interfaces by deviceIndex to ensure consistent ENI ordering
385-
-spec sort_network_interfaces_by_device_index(list()) -> list().
386-
sort_network_interfaces_by_device_index(NetworkInterfaces) when is_list(NetworkInterfaces) ->
387-
BeforeInfo = [format_network_interface_info(Props) || {"item", Props} <- NetworkInterfaces],
388-
Sorted = lists:sort(fun({"item", A}, {"item", B}) ->
389-
device_index(A) =< device_index(B)
390-
end, NetworkInterfaces),
391-
AfterInfo = [format_network_interface_info(Props) || {"item", Props} <- Sorted],
392-
?LOG_DEBUG("AWS peer discovery sorted network interfaces from ~tp to ~tp", [BeforeInfo, AfterInfo]),
393-
Sorted;
394-
sort_network_interfaces_by_device_index(Other) ->
395-
Other.
396-
397-
%% Sort private IP addresses by primary flag to ensure primary=true comes first
398-
-spec sort_private_ip_addresses_by_primary(list()) -> list().
399-
sort_private_ip_addresses_by_primary(PrivateIpAddresses) when is_list(PrivateIpAddresses) ->
400-
BeforeInfo = [format_private_ip_info(Props) || {"item", Props} <- PrivateIpAddresses],
401-
Sorted = lists:sort(fun({"item", A}, {"item", B}) ->
402-
is_primary(A) >= is_primary(B)
403-
end, PrivateIpAddresses),
404-
AfterInfo = [format_private_ip_info(Props) || {"item", Props} <- Sorted],
405-
?LOG_DEBUG("AWS peer discovery sorted private IPs from ~tp to ~tp", [BeforeInfo, AfterInfo]),
406-
Sorted;
407-
sort_private_ip_addresses_by_primary(Other) ->
408-
Other.
376+
Value = proplists:get_value(Key, Props),
377+
sort_ec2_hostname_path_set_members(Key, Value).
378+
379+
%% Sort AWS API responses for consistent ordering
380+
-spec sort_ec2_hostname_path_set_members(string(), any()) -> any().
381+
sort_ec2_hostname_path_set_members("networkInterfaceSet", NetworkInterfaces) when is_list(NetworkInterfaces) ->
382+
lists:sort(fun({"item", A}, {"item", B}) -> device_index(A) =< device_index(B) end, NetworkInterfaces);
383+
sort_ec2_hostname_path_set_members("privateIpAddressesSet", PrivateIpAddresses) when is_list(PrivateIpAddresses) ->
384+
lists:sort(fun({"item", A}, {"item", B}) -> is_primary(A) >= is_primary(B) end, PrivateIpAddresses);
385+
sort_ec2_hostname_path_set_members(_, Value) ->
386+
Value.
409387

410388
%% Extract deviceIndex from network interface attachment
411389
-spec device_index(props()) -> integer().
@@ -427,23 +405,6 @@ is_primary(IpAddress) ->
427405
_ -> false
428406
end.
429407

430-
%% Format network interface info for logging
431-
-spec format_network_interface_info(props()) -> string().
432-
format_network_interface_info(Interface) ->
433-
ENI = proplists:get_value("networkInterfaceId", Interface, "unknown"),
434-
DeviceIndex = device_index(Interface),
435-
lists:flatten(io_lib:format("~s:~w", [ENI, DeviceIndex])).
436-
437-
%% Format private IP info for logging
438-
-spec format_private_ip_info(props()) -> string().
439-
format_private_ip_info(IpAddress) ->
440-
IP = proplists:get_value("privateIpAddress", IpAddress, "unknown"),
441-
Primary = case is_primary(IpAddress) of
442-
true -> "primary";
443-
false -> "secondary"
444-
end,
445-
lists:flatten(io_lib:format("~s:~s", [IP, Primary])).
446-
447408
-spec get_tags() -> tags().
448409
get_tags() ->
449410
Tags = get_config_key(aws_ec2_tags, ?CONFIG_MODULE:config_map(?BACKEND_CONFIG_KEY)),

deps/rabbitmq_peer_discovery_aws/test/unit_SUITE.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ network_interface_sorting(_Config) ->
110110
],
111111

112112
%% Should sort ENIs by deviceIndex
113-
Sorted = rabbit_peer_discovery_aws:sort_network_interfaces_by_device_index(NetworkInterfaces),
113+
Sorted = rabbit_peer_discovery_aws:sort_ec2_hostname_path_set_members("networkInterfaceSet", NetworkInterfaces),
114114

115115
%% Should have all 3 ENIs
116116
?assertEqual(3, length(Sorted)),
@@ -147,7 +147,7 @@ private_ip_address_sorting(_Config) ->
147147
]}
148148
],
149149

150-
Sorted = rabbit_peer_discovery_aws:sort_private_ip_addresses_by_primary(PrivateIpAddresses),
150+
Sorted = rabbit_peer_discovery_aws:sort_ec2_hostname_path_set_members("privateIpAddressesSet", PrivateIpAddresses),
151151
?assertEqual(3, length(Sorted)),
152152

153153
%% Primary IP (primary=true) should be first

0 commit comments

Comments
 (0)