@@ -1440,6 +1440,43 @@ maybe_write_msg_to_disk(Force, MsgStatus = #msg_status {
1440
1440
maybe_write_msg_to_disk (_Force , MsgStatus , State ) ->
1441
1441
{MsgStatus , State }.
1442
1442
1443
+ % % Due to certain optimizations made inside
1444
+ % % rabbit_queue_index:pre_publish/7 we need to have two separate
1445
+ % % functions for index persistence. This one is only used when paging
1446
+ % % during memory pressure. We didn't want to modify
1447
+ % % maybe_write_index_to_disk/3 because that function is used in other
1448
+ % % places.
1449
+ maybe_batch_write_index_to_disk (_Force ,
1450
+ MsgStatus = # msg_status {
1451
+ index_on_disk = true }, State ) ->
1452
+ {MsgStatus , State };
1453
+ maybe_batch_write_index_to_disk (Force ,
1454
+ MsgStatus = # msg_status {
1455
+ msg = Msg ,
1456
+ msg_id = MsgId ,
1457
+ seq_id = SeqId ,
1458
+ is_persistent = IsPersistent ,
1459
+ is_delivered = IsDelivered ,
1460
+ msg_props = MsgProps },
1461
+ State = # vqstate {
1462
+ target_ram_count = TargetRamCount ,
1463
+ disk_write_count = DiskWriteCount ,
1464
+ index_state = IndexState })
1465
+ when Force orelse IsPersistent ->
1466
+ {MsgOrId , DiskWriteCount1 } =
1467
+ case persist_to (MsgStatus ) of
1468
+ msg_store -> {MsgId , DiskWriteCount };
1469
+ queue_index -> {prepare_to_store (Msg ), DiskWriteCount + 1 }
1470
+ end ,
1471
+ IndexState1 = rabbit_queue_index :pre_publish (
1472
+ MsgOrId , SeqId , MsgProps , IsPersistent , IsDelivered ,
1473
+ TargetRamCount , IndexState ),
1474
+ {MsgStatus # msg_status {index_on_disk = true },
1475
+ State # vqstate {index_state = IndexState1 ,
1476
+ disk_write_count = DiskWriteCount1 }};
1477
+ maybe_batch_write_index_to_disk (_Force , MsgStatus , State ) ->
1478
+ {MsgStatus , State }.
1479
+
1443
1480
maybe_write_index_to_disk (_Force , MsgStatus = # msg_status {
1444
1481
index_on_disk = true }, State ) ->
1445
1482
{MsgStatus , State };
@@ -1474,6 +1511,10 @@ maybe_write_to_disk(ForceMsg, ForceIndex, MsgStatus, State) ->
1474
1511
{MsgStatus1 , State1 } = maybe_write_msg_to_disk (ForceMsg , MsgStatus , State ),
1475
1512
maybe_write_index_to_disk (ForceIndex , MsgStatus1 , State1 ).
1476
1513
1514
+ maybe_prepare_write_to_disk (ForceMsg , ForceIndex , MsgStatus , State ) ->
1515
+ {MsgStatus1 , State1 } = maybe_write_msg_to_disk (ForceMsg , MsgStatus , State ),
1516
+ maybe_batch_write_index_to_disk (ForceIndex , MsgStatus1 , State1 ).
1517
+
1477
1518
determine_persist_to (# basic_message {
1478
1519
content = # content {properties = Props ,
1479
1520
properties_bin = PropsBin }},
@@ -1861,16 +1902,16 @@ reduce_memory_use(State = #vqstate {
1861
1902
end .
1862
1903
1863
1904
limit_ram_acks (0 , State ) ->
1864
- {0 , State };
1905
+ {0 , ui ( State ) };
1865
1906
limit_ram_acks (Quota , State = # vqstate { ram_pending_ack = RPA ,
1866
1907
disk_pending_ack = DPA }) ->
1867
1908
case gb_trees :is_empty (RPA ) of
1868
1909
true ->
1869
- {Quota , State };
1910
+ {Quota , ui ( State ) };
1870
1911
false ->
1871
1912
{SeqId , MsgStatus , RPA1 } = gb_trees :take_largest (RPA ),
1872
1913
{MsgStatus1 , State1 } =
1873
- maybe_write_to_disk (true , false , MsgStatus , State ),
1914
+ maybe_prepare_write_to_disk (true , false , MsgStatus , State ),
1874
1915
MsgStatus2 = m (trim_msg_status (MsgStatus1 )),
1875
1916
DPA1 = gb_trees :insert (SeqId , MsgStatus2 , DPA ),
1876
1917
limit_ram_acks (Quota - 1 ,
@@ -2008,16 +2049,17 @@ push_alphas_to_betas(_Generator, _Consumer, Quota, _Q,
2008
2049
when Quota =:= 0 orelse
2009
2050
TargetRamCount =:= infinity orelse
2010
2051
TargetRamCount >= RamMsgCount ->
2011
- {Quota , State };
2052
+ {Quota , ui ( State ) };
2012
2053
push_alphas_to_betas (Generator , Consumer , Quota , Q , State ) ->
2013
2054
case credit_flow :blocked () of
2014
- true -> {Quota , State };
2055
+ true -> {Quota , ui ( State ) };
2015
2056
false -> case Generator (Q ) of
2016
2057
{empty , _Q } ->
2017
- {Quota , State };
2058
+ {Quota , ui ( State ) };
2018
2059
{{value , MsgStatus }, Qa } ->
2019
2060
{MsgStatus1 , State1 } =
2020
- maybe_write_to_disk (true , false , MsgStatus , State ),
2061
+ maybe_prepare_write_to_disk (true , false , MsgStatus ,
2062
+ State ),
2021
2063
MsgStatus2 = m (trim_msg_status (MsgStatus1 )),
2022
2064
State2 = stats (
2023
2065
ready0 , {MsgStatus , MsgStatus2 }, State1 ),
@@ -2058,24 +2100,31 @@ push_betas_to_deltas(Generator, LimitFun, Q, PushState) ->
2058
2100
end
2059
2101
end .
2060
2102
2061
- push_betas_to_deltas1 (_Generator , _Limit , Q , {0 , _Delta , _State } = PushState ) ->
2062
- {Q , PushState };
2063
- push_betas_to_deltas1 (Generator , Limit , Q , {Quota , Delta , State } = PushState ) ->
2103
+ push_betas_to_deltas1 (_Generator , _Limit , Q , {0 , Delta , State } ) ->
2104
+ {Q , { 0 , Delta , ui ( State )} };
2105
+ push_betas_to_deltas1 (Generator , Limit , Q , {Quota , Delta , State }) ->
2064
2106
case Generator (Q ) of
2065
2107
{empty , _Q } ->
2066
- {Q , PushState };
2108
+ {Q , { Quota , Delta , ui ( State )} };
2067
2109
{{value , # msg_status { seq_id = SeqId }}, _Qa }
2068
2110
when SeqId < Limit ->
2069
- {Q , PushState };
2111
+ {Q , { Quota , Delta , ui ( State )} };
2070
2112
{{value , MsgStatus = # msg_status { seq_id = SeqId }}, Qa } ->
2071
2113
{# msg_status { index_on_disk = true }, State1 } =
2072
- maybe_write_index_to_disk (true , MsgStatus , State ),
2114
+ maybe_batch_write_index_to_disk (true , MsgStatus , State ),
2073
2115
State2 = stats (ready0 , {MsgStatus , none }, State1 ),
2074
2116
Delta1 = expand_delta (SeqId , Delta ),
2075
2117
push_betas_to_deltas1 (Generator , Limit , Qa ,
2076
2118
{Quota - 1 , Delta1 , State2 })
2077
2119
end .
2078
2120
2121
+ % % Flushes queue index batch caches and updates queue index state.
2122
+ ui (# vqstate {index_state = IndexState ,
2123
+ target_ram_count = TargetRamCount } = State ) ->
2124
+ IndexState1 = rabbit_queue_index :flush_pre_publish_cache (
2125
+ TargetRamCount , IndexState ),
2126
+ State # vqstate {index_state = IndexState1 }.
2127
+
2079
2128
% %----------------------------------------------------------------------------
2080
2129
% % Upgrading
2081
2130
% %----------------------------------------------------------------------------
0 commit comments