diff --git a/deps/rabbit/src/rabbit_fifo.erl b/deps/rabbit/src/rabbit_fifo.erl index f9a7f842d689..941fab92afc9 100644 --- a/deps/rabbit/src/rabbit_fifo.erl +++ b/deps/rabbit/src/rabbit_fifo.erl @@ -407,7 +407,14 @@ apply(Meta, #checkout{spec = Spec, meta = ConsumerMeta, #consumer{checked_out = Checked, credit = Credit, delivery_count = DeliveryCount, - next_msg_id = NextMsgId} = Consumer, + next_msg_id = NextMsgId0} = Consumer, + NextMsgId = case map_size(Checked) of + 0 -> + NextMsgId0; + _ -> + lists:min(maps:keys(Checked)) + end, + %% reply with a consumer summary Reply = {ok, #{next_msg_id => NextMsgId, credit => Credit, diff --git a/deps/rabbit/test/rabbit_fifo_SUITE.erl b/deps/rabbit/test/rabbit_fifo_SUITE.erl index aaf82be3de79..d3493e9e2ce2 100644 --- a/deps/rabbit/test/rabbit_fifo_SUITE.erl +++ b/deps/rabbit/test/rabbit_fifo_SUITE.erl @@ -1990,6 +1990,28 @@ chunk_disk_msgs_test(_Config) -> rabbit_fifo:chunk_disk_msgs(TwoBigMsgs, 0, [[]])), ok. +checkout_metadata_test(Config) -> + Cid = {<<"cid">>, self()}, + {State00, _} = enq(Config, 1, 1, first, test_init(test)), + {State0, _} = enq(Config, 2, 2, second, State00), + %% NB: the consumer meta data is taken _before_ it runs a checkout + %% so in this case num_checked_out will be 0 + {State1, {ok, #{next_msg_id := 0, + num_checked_out := 0}}, _} = + apply(meta(Config, ?LINE), + rabbit_fifo:make_checkout(Cid, {auto, 1, simple_prefetch}, #{}), + State0), + {State2, _, _} = apply(meta(Config, ?LINE), + rabbit_fifo:make_checkout(Cid, cancel, #{}), State1), + %% check out again with pending messages should set the next message id + %% to the lowest in-flight message id + {_State3, {ok, #{next_msg_id := 0, + num_checked_out := 1}}, _} = + apply(meta(Config, ?LINE), + rabbit_fifo:make_checkout(Cid, {auto, 1, simple_prefetch}, #{}), + State2), + ok. + %% Utility init(Conf) -> rabbit_fifo:init(Conf).