-
Notifications
You must be signed in to change notification settings - Fork 4k
Ensures pending counter in rabbit_shovel_status is always an integer #14614
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,8 @@ | |
| ack/3, | ||
| nack/3, | ||
| forward/3, | ||
| status/1 | ||
| status/1, | ||
| pending_count/1 | ||
| ]). | ||
|
|
||
| -export([ | ||
|
|
@@ -437,6 +438,11 @@ add_routing(Msg0, Dest) -> | |
| status(_) -> | ||
| running. | ||
|
|
||
| pending_count(#{source := #{current := #{unacked_message_q := UAMQ}}}) -> | ||
|
||
| ?QUEUE:len(UAMQ); | ||
| pending_count(_State) -> | ||
| 0. | ||
|
|
||
| %% Internal | ||
|
|
||
| parse_parameter(_, _, none) -> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| %% 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) 2007-2025 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. | ||
| %% | ||
|
|
||
| -module(pending_count_SUITE). | ||
|
|
||
| -compile(export_all). | ||
|
|
||
| -include_lib("eunit/include/eunit.hrl"). | ||
| -include_lib("rabbit/include/mc.hrl"). | ||
| -include("../include/rabbit_shovel.hrl"). | ||
|
|
||
| %%%=================================================================== | ||
| %%% Common Test callbacks | ||
| %%%=================================================================== | ||
|
|
||
| all() -> | ||
| [ | ||
| {group, pending_count_tests} | ||
| ]. | ||
|
|
||
| groups() -> | ||
| [ | ||
| {pending_count_tests, [], [ | ||
| amqp091_pending_count_empty_queue, | ||
| amqp091_pending_count_with_messages, | ||
| amqp091_pending_count_after_drain, | ||
| amqp10_pending_count_empty_list, | ||
| amqp10_pending_count_with_messages, | ||
| amqp10_pending_count_after_clear, | ||
| local_pending_count_empty_queue, | ||
| local_pending_count_with_messages, | ||
| local_pending_count_after_settle, | ||
| behaviour_metrics_includes_pending, | ||
| behaviour_pending_count_delegation | ||
| ]} | ||
| ]. | ||
|
|
||
| init_per_suite(Config) -> | ||
| Config. | ||
|
|
||
| end_per_suite(_Config) -> | ||
| ok. | ||
|
|
||
| init_per_group(_Group, Config) -> | ||
| Config. | ||
|
|
||
| end_per_group(_Group, _Config) -> | ||
| ok. | ||
|
|
||
| init_per_testcase(_TestCase, Config) -> | ||
| Config. | ||
|
|
||
| end_per_testcase(_TestCase, _Config) -> | ||
| meck:unload(), | ||
| ok. | ||
|
|
||
| %%%=================================================================== | ||
| %%% Test cases | ||
| %%%=================================================================== | ||
|
|
||
| %% Test AMQP 0.9.1 pending_count functionality | ||
| amqp091_pending_count_empty_queue(_Config) -> | ||
| %% Test that pending_count returns 0 when no messages are pending | ||
| State = #{dest => #{}}, | ||
| ?assertEqual(0, rabbit_amqp091_shovel:pending_count(State)). | ||
|
|
||
| amqp091_pending_count_with_messages(_Config) -> | ||
| %% Test that pending_count returns correct count when messages are pending | ||
| PendingQueue = queue:from_list([{1, msg1}, {2, msg2}, {3, msg3}]), | ||
| State = #{dest => #{pending => PendingQueue}}, | ||
| ?assertEqual(3, rabbit_amqp091_shovel:pending_count(State)). | ||
|
|
||
| amqp091_pending_count_after_drain(_Config) -> | ||
| %% Test that pending_count returns 0 after messages are drained | ||
| EmptyQueue = queue:new(), | ||
| State = #{dest => #{pending => EmptyQueue}}, | ||
| ?assertEqual(0, rabbit_amqp091_shovel:pending_count(State)). | ||
|
|
||
| %% Test AMQP 1.0 pending_count functionality | ||
| amqp10_pending_count_empty_list(_Config) -> | ||
| %% Test that pending_count returns 0 when no messages are pending | ||
| State = #{dest => #{}}, | ||
| ?assertEqual(0, rabbit_amqp10_shovel:pending_count(State)). | ||
|
|
||
| amqp10_pending_count_with_messages(_Config) -> | ||
| %% Test that pending_count returns correct count when messages are pending | ||
| PendingList = [{1, msg1}, {2, msg2}], | ||
| State = #{dest => #{pending => PendingList}}, | ||
| ?assertEqual(2, rabbit_amqp10_shovel:pending_count(State)). | ||
|
|
||
| amqp10_pending_count_after_clear(_Config) -> | ||
| %% Test that pending_count returns 0 after pending list is cleared | ||
| State = #{dest => #{pending => []}}, | ||
| ?assertEqual(0, rabbit_amqp10_shovel:pending_count(State)). | ||
|
|
||
| %% Test Local shovel pending_count functionality | ||
| local_pending_count_empty_queue(_Config) -> | ||
| %% Test that pending_count returns 0 when unacked message queue is empty | ||
| EmptyQueue = lqueue:new(), | ||
| State = #{source => #{current => #{unacked_message_q => EmptyQueue}}}, | ||
| ?assertEqual(0, rabbit_local_shovel:pending_count(State)). | ||
|
|
||
| local_pending_count_with_messages(_Config) -> | ||
| %% Test that pending_count returns correct count from unacked message queue | ||
| UnackedQueue = lqueue:from_list([msg1, msg2, msg3, msg4]), | ||
| State = #{source => #{current => #{unacked_message_q => UnackedQueue}}}, | ||
| ?assertEqual(4, rabbit_local_shovel:pending_count(State)). | ||
|
|
||
| local_pending_count_after_settle(_Config) -> | ||
| %% Test that pending_count returns 0 when state doesn't contain unacked queue | ||
| State = #{source => #{current => #{}}}, | ||
| ?assertEqual(0, rabbit_local_shovel:pending_count(State)). | ||
|
|
||
| %% Test behaviour module integration | ||
| behaviour_metrics_includes_pending(_Config) -> | ||
| %% Mock the destination module's pending_count and status functions | ||
| meck:new(rabbit_amqp091_shovel, [passthrough]), | ||
| meck:expect(rabbit_amqp091_shovel, pending_count, fun(_) -> 5 end), | ||
| meck:expect(rabbit_amqp091_shovel, status, fun(_) -> running end), | ||
|
|
||
| State = #{source => #{remaining => 10, remaining_unacked => 3}, | ||
| dest => #{module => rabbit_amqp091_shovel, forwarded => 7}}, | ||
|
|
||
| {_Status, Metrics} = rabbit_shovel_behaviour:status(State), | ||
|
|
||
| ?assertMatch(#{remaining := 10, | ||
| remaining_unacked := 3, | ||
| pending := 5, | ||
| forwarded := 7}, Metrics), | ||
|
|
||
| ?assert(meck:validate(rabbit_amqp091_shovel)). | ||
|
|
||
| behaviour_pending_count_delegation(_Config) -> | ||
| %% Test that the behaviour module correctly delegates to the specific implementation | ||
| meck:new(rabbit_amqp10_shovel, [passthrough]), | ||
| meck:expect(rabbit_amqp10_shovel, pending_count, fun(_State) -> 3 end), | ||
| meck:expect(rabbit_amqp10_shovel, status, fun(_State) -> running end), | ||
|
|
||
| State = #{dest => #{module => rabbit_amqp10_shovel}}, | ||
|
|
||
| %% This would be called indirectly through status/1 | ||
| {_Status, Metrics} = rabbit_shovel_behaviour:status(#{source => #{}, | ||
| dest => maps:get(dest, State)}), | ||
|
|
||
| ?assertEqual(3, maps:get(pending, Metrics)), | ||
| ?assert(meck:validate(rabbit_amqp10_shovel)). | ||
|
|
||
| %%%=================================================================== | ||
| %%% Integration tests for pending_count behavior in different scenarios | ||
| %%%=================================================================== | ||
|
|
||
| %% Additional test cases to verify pending_count behavior in realistic scenarios | ||
| %% These could be added if we want to test the actual message flow scenarios | ||
|
|
||
| pending_count_during_flow_control(_Config) -> | ||
| %% Test case outline: Verify pending_count increases when flow control blocks forwarding | ||
| %% and decreases when flow control is lifted | ||
| %% This would require more complex setup with actual message handling | ||
| ok. | ||
|
|
||
| pending_count_with_multiple_ack_modes(_Config) -> | ||
| %% Test case outline: Verify pending_count behaves correctly across different ack modes | ||
| %% (no_ack, on_publish, on_confirm) | ||
| ok. | ||
|
|
||
| pending_count_edge_cases(_Config) -> | ||
| %% Test case outline: Test edge cases like: | ||
| %% - Missing dest/source maps | ||
| %% - Malformed pending data structures | ||
| %% - Very large pending counts | ||
| ok. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pending messages in AMQP0.9.1 are counted in the destination side, thus in AMQP1.0 shovels they should be dest -> unacked
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my understanding is that in case of AMQP 1.0 the dest -> unacked field is only used in on-confirm ack-mode. But there can be pending messages in case of other ack-modes too when there is no more link credit to send the message to the dest. In this case the whole message is buffered in the shovel process. My understanding is that the pending metric only counts the number of buffered ie unsent messages. Unacked additionally counts messages that were sent but not yet acked by the dest.
There is also the metric
remaining_unacked, but I'm not sure it is counted correctly in case of on-confirm. It is decremented when the message is sent (the same way as for other ack-modes) and not when the message is accepted/rejected by the dest.