@@ -1379,6 +1379,43 @@ maybe_write_msg_to_disk(Force, MsgStatus = #msg_status {
1379
1379
maybe_write_msg_to_disk (_Force , MsgStatus , State ) ->
1380
1380
{MsgStatus , State }.
1381
1381
1382
+ % % Due to certain optimizations made inside
1383
+ % % rabbit_queue_index:pre_publish/7 we need to have two separate
1384
+ % % functions for index persistence. This one is only used when paging
1385
+ % % during memory pressure. We didn't want to modify
1386
+ % % maybe_write_index_to_disk/3 because that function is used in other
1387
+ % % places.
1388
+ maybe_batch_write_index_to_disk (_Force ,
1389
+ MsgStatus = # msg_status {
1390
+ index_on_disk = true }, State ) ->
1391
+ {MsgStatus , State };
1392
+ maybe_batch_write_index_to_disk (Force ,
1393
+ MsgStatus = # msg_status {
1394
+ msg = Msg ,
1395
+ msg_id = MsgId ,
1396
+ seq_id = SeqId ,
1397
+ is_persistent = IsPersistent ,
1398
+ is_delivered = IsDelivered ,
1399
+ msg_props = MsgProps },
1400
+ State = # vqstate {
1401
+ target_ram_count = TargetRamCount ,
1402
+ disk_write_count = DiskWriteCount ,
1403
+ index_state = IndexState })
1404
+ when Force orelse IsPersistent ->
1405
+ {MsgOrId , DiskWriteCount1 } =
1406
+ case persist_to (MsgStatus ) of
1407
+ msg_store -> {MsgId , DiskWriteCount };
1408
+ queue_index -> {prepare_to_store (Msg ), DiskWriteCount + 1 }
1409
+ end ,
1410
+ IndexState1 = rabbit_queue_index :pre_publish (
1411
+ MsgOrId , SeqId , MsgProps , IsPersistent , IsDelivered ,
1412
+ TargetRamCount , IndexState ),
1413
+ {MsgStatus # msg_status {index_on_disk = true },
1414
+ State # vqstate {index_state = IndexState1 ,
1415
+ disk_write_count = DiskWriteCount1 }};
1416
+ maybe_batch_write_index_to_disk (_Force , MsgStatus , State ) ->
1417
+ {MsgStatus , State }.
1418
+
1382
1419
maybe_write_index_to_disk (_Force , MsgStatus = # msg_status {
1383
1420
index_on_disk = true }, State ) ->
1384
1421
{MsgStatus , State };
@@ -1413,6 +1450,10 @@ maybe_write_to_disk(ForceMsg, ForceIndex, MsgStatus, State) ->
1413
1450
{MsgStatus1 , State1 } = maybe_write_msg_to_disk (ForceMsg , MsgStatus , State ),
1414
1451
maybe_write_index_to_disk (ForceIndex , MsgStatus1 , State1 ).
1415
1452
1453
+ maybe_prepare_write_to_disk (ForceMsg , ForceIndex , MsgStatus , State ) ->
1454
+ {MsgStatus1 , State1 } = maybe_write_msg_to_disk (ForceMsg , MsgStatus , State ),
1455
+ maybe_batch_write_index_to_disk (ForceIndex , MsgStatus1 , State1 ).
1456
+
1416
1457
determine_persist_to (# basic_message {
1417
1458
content = # content {properties = Props ,
1418
1459
properties_bin = PropsBin }},
@@ -1800,16 +1841,16 @@ reduce_memory_use(State = #vqstate {
1800
1841
end .
1801
1842
1802
1843
limit_ram_acks (0 , State ) ->
1803
- {0 , State };
1844
+ {0 , ui ( State ) };
1804
1845
limit_ram_acks (Quota , State = # vqstate { ram_pending_ack = RPA ,
1805
1846
disk_pending_ack = DPA }) ->
1806
1847
case gb_trees :is_empty (RPA ) of
1807
1848
true ->
1808
- {Quota , State };
1849
+ {Quota , ui ( State ) };
1809
1850
false ->
1810
1851
{SeqId , MsgStatus , RPA1 } = gb_trees :take_largest (RPA ),
1811
1852
{MsgStatus1 , State1 } =
1812
- maybe_write_to_disk (true , false , MsgStatus , State ),
1853
+ maybe_prepare_write_to_disk (true , false , MsgStatus , State ),
1813
1854
MsgStatus2 = m (trim_msg_status (MsgStatus1 )),
1814
1855
DPA1 = gb_trees :insert (SeqId , MsgStatus2 , DPA ),
1815
1856
limit_ram_acks (Quota - 1 ,
@@ -1947,16 +1988,17 @@ push_alphas_to_betas(_Generator, _Consumer, Quota, _Q,
1947
1988
when Quota =:= 0 orelse
1948
1989
TargetRamCount =:= infinity orelse
1949
1990
TargetRamCount >= RamMsgCount ->
1950
- {Quota , State };
1991
+ {Quota , ui ( State ) };
1951
1992
push_alphas_to_betas (Generator , Consumer , Quota , Q , State ) ->
1952
1993
case credit_flow :blocked () of
1953
- true -> {Quota , State };
1994
+ true -> {Quota , ui ( State ) };
1954
1995
false -> case Generator (Q ) of
1955
1996
{empty , _Q } ->
1956
- {Quota , State };
1997
+ {Quota , ui ( State ) };
1957
1998
{{value , MsgStatus }, Qa } ->
1958
1999
{MsgStatus1 , State1 } =
1959
- maybe_write_to_disk (true , false , MsgStatus , State ),
2000
+ maybe_prepare_write_to_disk (true , false , MsgStatus ,
2001
+ State ),
1960
2002
MsgStatus2 = m (trim_msg_status (MsgStatus1 )),
1961
2003
State2 = stats (
1962
2004
ready0 , {MsgStatus , MsgStatus2 }, State1 ),
@@ -1997,24 +2039,31 @@ push_betas_to_deltas(Generator, LimitFun, Q, PushState) ->
1997
2039
end
1998
2040
end .
1999
2041
2000
- push_betas_to_deltas1 (_Generator , _Limit , Q , {0 , _Delta , _State } = PushState ) ->
2001
- {Q , PushState };
2002
- push_betas_to_deltas1 (Generator , Limit , Q , {Quota , Delta , State } = PushState ) ->
2042
+ push_betas_to_deltas1 (_Generator , _Limit , Q , {0 , Delta , State } ) ->
2043
+ {Q , { 0 , Delta , ui ( State )} };
2044
+ push_betas_to_deltas1 (Generator , Limit , Q , {Quota , Delta , State }) ->
2003
2045
case Generator (Q ) of
2004
2046
{empty , _Q } ->
2005
- {Q , PushState };
2047
+ {Q , { Quota , Delta , ui ( State )} };
2006
2048
{{value , # msg_status { seq_id = SeqId }}, _Qa }
2007
2049
when SeqId < Limit ->
2008
- {Q , PushState };
2050
+ {Q , { Quota , Delta , ui ( State )} };
2009
2051
{{value , MsgStatus = # msg_status { seq_id = SeqId }}, Qa } ->
2010
2052
{# msg_status { index_on_disk = true }, State1 } =
2011
- maybe_write_index_to_disk (true , MsgStatus , State ),
2053
+ maybe_batch_write_index_to_disk (true , MsgStatus , State ),
2012
2054
State2 = stats (ready0 , {MsgStatus , none }, State1 ),
2013
2055
Delta1 = expand_delta (SeqId , Delta ),
2014
2056
push_betas_to_deltas1 (Generator , Limit , Qa ,
2015
2057
{Quota - 1 , Delta1 , State2 })
2016
2058
end .
2017
2059
2060
+ % % Flushes queue index batch caches and updates queue index state.
2061
+ ui (# vqstate {index_state = IndexState ,
2062
+ target_ram_count = TargetRamCount } = State ) ->
2063
+ IndexState1 = rabbit_queue_index :flush_pre_publish_cache (
2064
+ TargetRamCount , IndexState ),
2065
+ State # vqstate {index_state = IndexState1 }.
2066
+
2018
2067
% %----------------------------------------------------------------------------
2019
2068
% % Upgrading
2020
2069
% %----------------------------------------------------------------------------
0 commit comments