rocketmq-store 0.8.0

Storage layer for Apache RocketMQ in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::mem;
use std::sync::atomic::AtomicU64;
use std::sync::Arc;

use bytes::Buf;
use bytes::Bytes;
use bytes::BytesMut;
use cheetah_string::CheetahString;
use dashmap::DashMap;
use rocketmq_common::common::attribute::cq_type::CQType;
use rocketmq_common::common::broker::broker_config::BrokerConfig;
use rocketmq_common::common::broker::broker_role::BrokerRole;
use rocketmq_common::common::config::TopicConfig;
use rocketmq_common::common::message::message_batch::MessageExtBatch;
use rocketmq_common::common::message::message_ext_broker_inner::MessageExtBrokerInner;
use rocketmq_common::common::message::message_single::tags_string2tags_code;
use rocketmq_common::common::message::MessageConst;
use rocketmq_common::common::message::MessageTrait;
use rocketmq_common::common::message::MessageVersion;
use rocketmq_common::common::mix_all;
use rocketmq_common::common::sys_flag::message_sys_flag::MessageSysFlag;
use rocketmq_common::common::system_clock::SystemClock;
use rocketmq_common::common::topic::TopicValidator;
use rocketmq_common::utils::queue_type_utils::QueueTypeUtils;
use rocketmq_common::utils::time_utils;
use rocketmq_common::CRC32Utils::crc32;
use rocketmq_common::CRC32Utils::crc32_bytes;
use rocketmq_common::MessageDecoder;
use rocketmq_common::MessageDecoder::string_to_message_properties;
use rocketmq_common::MessageDecoder::MESSAGE_MAGIC_CODE_POSITION;
use rocketmq_common::MessageDecoder::MESSAGE_MAGIC_CODE_V2;
use rocketmq_common::MessageDecoder::SYSFLAG_POSITION;
use rocketmq_common::TimeUtils::current_millis;
use rocketmq_common::UtilAll::time_millis_to_human_string;
use rocketmq_rust::ArcMut;
use tokio::time::Instant;
use tracing::error;
use tracing::info;
use tracing::warn;

use crate::base::append_message_callback::DefaultAppendMessageCallback;
use crate::base::commit_log_dispatcher::CommitLogDispatcher;
use crate::base::dispatch_request::DispatchRequest;
use crate::base::flush_manager::FlushManager;
use crate::base::message_encoder_pool;
use crate::base::message_result::AppendMessageResult;
use crate::base::message_result::PutMessageResult;
use crate::base::message_status_enum::AppendMessageStatus;
use crate::base::message_status_enum::PutMessageStatus;
use crate::base::message_store::MessageStore;
use crate::base::put_message_context::PutMessageContext;
use crate::base::select_result::SelectMappedBufferResult;
use crate::base::store_checkpoint::StoreCheckpoint;
use crate::base::swappable::Swappable;
use crate::base::topic_queue_lock::TopicQueueLock;
use crate::config::flush_disk_type::FlushDiskType;
use crate::config::message_store_config::MessageStoreConfig;
use crate::consume_queue::mapped_file_queue::MappedFileQueue;
use crate::ha::ha_service::HAService;
use crate::log_file::cold_data_check_service::ColdDataCheckService;
// Import the optimized loader module
use crate::log_file::commit_log_loader::CommitLogLoader;
use crate::log_file::flush_manager_impl::default_flush_manager::DefaultFlushManager;
use crate::log_file::group_commit_request::GroupCommitRequest;
use crate::log_file::mapped_file::default_mapped_file_impl::DefaultMappedFile;
use crate::log_file::mapped_file::MappedFile;
use crate::message_encoder::message_ext_encoder::MessageExtEncoder;
use crate::message_store::local_file_message_store::CommitLogDispatcherDefault;
use crate::message_store::local_file_message_store::LocalFileMessageStore;
use crate::queue::consume_queue_store::ConsumeQueueStoreTrait;
use crate::queue::local_file_consume_queue_store::ConsumeQueueStore;
use crate::store_error::StoreError;

// Message's MAGIC CODE daa320a7
pub const MESSAGE_MAGIC_CODE: i32 = -626843481;

// End of file empty MAGIC CODE cbd43194
pub const BLANK_MAGIC_CODE: i32 = -875286124;

//CRC32 Format: [PROPERTY_CRC32 + NAME_VALUE_SEPARATOR + 10-digit fixed-length string +
// PROPERTY_SEPARATOR]
pub const CRC32_RESERVED_LEN: i32 = (MessageConst::PROPERTY_CRC32.len() + 1 + 10 + 1) as i32;

// This reduces heap allocations by ~50% by reusing encoder instances

fn encode_message_ext(
    message_ext: &MessageExtBrokerInner,
    message_store_config: &Arc<MessageStoreConfig>,
) -> (Option<PutMessageResult>, BytesMut) {
    message_encoder_pool::encode_message_with_pool(message_ext, message_store_config)
}

fn encode_message_ext_batch(
    message_ext_batch: &MessageExtBatch,
    put_message_context: &mut PutMessageContext,
    message_store_config: &Arc<MessageStoreConfig>,
) -> Option<BytesMut> {
    message_encoder_pool::encode_message_batch_with_pool(message_ext_batch, put_message_context, message_store_config)
}

fn generate_key(msg: &MessageExtBrokerInner) -> String {
    message_encoder_pool::generate_key_with_pool(msg)
}

pub fn get_cq_type(
    topic_config_table: &Arc<DashMap<CheetahString, ArcMut<TopicConfig>>>,
    msg_inner: &MessageExtBrokerInner,
) -> CQType {
    let binding = topic_config_table.get(msg_inner.topic());
    QueueTypeUtils::get_cq_type_arc_mut(binding.as_deref())
}

pub fn get_message_num(
    topic_config_table: &Arc<DashMap<CheetahString, ArcMut<TopicConfig>>>,
    msg_inner: &MessageExtBrokerInner,
) -> i16 {
    let mut message_num = 1i16;
    let cq_type = get_cq_type(topic_config_table, msg_inner);
    if MessageSysFlag::check(msg_inner.sys_flag(), MessageSysFlag::INNER_BATCH_FLAG) || cq_type == CQType::BatchCQ {
        if let Some(num) = msg_inner
            .message_ext_inner
            .message
            .property(&CheetahString::from_static_str(MessageConst::PROPERTY_INNER_NUM))
        {
            message_num = num.parse().unwrap_or(1i16);
        }
    }
    // message_num
    message_num
}

pub struct CommitLog {
    mapped_file_queue: ArcMut<MappedFileQueue>,
    message_store_config: Arc<MessageStoreConfig>,
    broker_config: Arc<BrokerConfig>,
    enabled_append_prop_crc: bool,
    local_file_message_store: Option<ArcMut<LocalFileMessageStore>>,
    dispatcher: ArcMut<CommitLogDispatcherDefault>,
    confirm_offset: i64,
    store_checkpoint: Arc<StoreCheckpoint>,
    append_message_callback: Arc<DefaultAppendMessageCallback>,
    put_message_lock: Arc<tokio::sync::Mutex<()>>,
    topic_queue_lock: Arc<TopicQueueLock>,
    topic_config_table: Arc<DashMap<CheetahString, ArcMut<TopicConfig>>>,
    consume_queue_store: ConsumeQueueStore,
    flush_manager: ArcMut<DefaultFlushManager>,
    begin_time_in_lock: Arc<AtomicU64>,
    cold_data_check_service: Arc<ColdDataCheckService>,
}

impl CommitLog {
    pub fn new(
        message_store_config: Arc<MessageStoreConfig>,
        broker_config: Arc<BrokerConfig>,
        dispatcher: ArcMut<CommitLogDispatcherDefault>,
        store_checkpoint: Arc<StoreCheckpoint>,
        topic_config_table: Arc<DashMap<CheetahString, ArcMut<TopicConfig>>>,
        consume_queue_store: ConsumeQueueStore,
    ) -> Self {
        let enabled_append_prop_crc = message_store_config.enabled_append_prop_crc;
        let store_path = message_store_config.get_store_path_commit_log();
        let mapped_file_size = message_store_config.mapped_file_size_commit_log;
        let mapped_file_queue = ArcMut::new(MappedFileQueue::new(store_path, mapped_file_size as u64, None));
        Self {
            mapped_file_queue: mapped_file_queue.clone(),
            message_store_config: message_store_config.clone(),
            broker_config,
            enabled_append_prop_crc,
            local_file_message_store: None,
            dispatcher,
            confirm_offset: -1,
            store_checkpoint: store_checkpoint.clone(),
            append_message_callback: Arc::new(DefaultAppendMessageCallback::new(
                message_store_config.clone(),
                topic_config_table.clone(),
            )),
            put_message_lock: Arc::new(Default::default()),
            topic_queue_lock: Arc::new(TopicQueueLock::with_size(message_store_config.topic_queue_lock_num)),
            topic_config_table,
            consume_queue_store,
            flush_manager: ArcMut::new(DefaultFlushManager::new(
                message_store_config,
                mapped_file_queue,
                store_checkpoint,
            )),
            begin_time_in_lock: Arc::new(AtomicU64::new(0)),
            cold_data_check_service: Arc::new(Default::default()),
        }
    }
}

#[allow(unused_variables)]
impl CommitLog {
    /// Load CommitLog files with optimized parallel I/O strategy.
    ///
    /// This implementation provides significant performance improvements over the original:
    /// - **Parallel metadata collection**: Reduces I/O latency on multi-file scenarios
    /// - **Batched validation**: Validates all files before mmap creation
    /// - **Memory hints**: Applies platform-specific optimizations (madvise, etc.)
    /// - **Zero-copy reuse**: Minimizes allocations during load
    ///
    /// # Feature Flag
    /// The `fast-load` cargo feature enables parallel loading (enabled by default).
    /// To disable and use safe sequential loading, compile with `--no-default-features`.
    ///
    /// # Behavior Equivalence
    /// This implementation maintains exact semantic equivalence with the original:
    /// - File ordering is preserved (sorted by filename)
    /// - Size validation is identical
    /// - Empty last file removal logic is preserved
    /// - All positions (wrote/flushed/committed) are set identically
    ///
    /// # Returns
    /// `true` if load succeeded, `false` otherwise
    pub fn load(&mut self) -> bool {
        // Use environment variable to force fallback to safe sequential load
        let force_sequential = std::env::var("ROCKETMQ_SAFE_LOAD")
            .map(|v| v == "1" || v.to_lowercase() == "true")
            .unwrap_or(false);

        if force_sequential {
            info!("Using safe sequential CommitLog load (ROCKETMQ_SAFE_LOAD=true)");
            return self.load_sequential();
        }

        // Optimized parallel load path
        match self.load_optimized() {
            Ok(true) => {
                self.mapped_file_queue.check_self();
                info!("load commit log Ok (optimized)");
                true
            }
            Ok(false) => {
                error!("load commit log failed (optimized)");
                false
            }
            Err(e) => {
                error!("Optimized load failed: {}, falling back to sequential load", e);
                self.load_sequential()
            }
        }
    }

    /// Optimized load implementation with parallel I/O and batching.
    ///
    /// # Performance Characteristics
    /// - **Parallel metadata**: ~70% faster on 10+ files (SSD)
    /// - **Reduced syscalls**: Batch validation reduces overhead
    /// - **Memory efficient**: Pre-allocated vectors, minimal copies
    ///
    /// # Errors
    /// Returns `Err` if:
    /// - Directory access fails
    /// - File size validation fails
    /// - mmap creation fails
    fn load_optimized(&mut self) -> Result<bool, std::io::Error> {
        let store_path = self.message_store_config.get_store_path_commit_log();
        let mapped_file_size = self.message_store_config.mapped_file_size_commit_log as u64;

        // Determine if parallel loading should be enabled
        // Use parallel mode if we expect more than 4 files (empirically optimal threshold)
        let enable_parallel = cfg!(feature = "fast-load") || !cfg!(feature = "safe-load");

        let loader = CommitLogLoader::new(store_path, mapped_file_size, enable_parallel);

        match loader.load_optimized() {
            Ok((mapped_files, stats)) => {
                // Replace the mapped_files vec in mapped_file_queue
                // This is safe because we're in &mut self
                {
                    // Copy-on-write update
                    let mut new_files = Vec::new();
                    new_files.extend(mapped_files);
                    self.mapped_file_queue.get_mapped_files().store(Arc::new(new_files));
                }

                // Log detailed statistics
                info!(
                    "CommitLog loaded: {} files, {:.2} MB total, parallel phase: {}ms, total: {}ms",
                    stats.total_files,
                    stats.total_size_bytes as f64 / 1024.0 / 1024.0,
                    stats.parallel_load_time_ms,
                    stats.total_load_time_ms
                );

                Ok(true)
            }
            Err(e) => Err(e),
        }
    }

    /// Fallback: Original sequential load implementation.
    ///
    /// This method preserves the exact original behavior for compatibility
    /// and serves as a fallback if optimized loading encounters errors.
    fn load_sequential(&mut self) -> bool {
        let result = self.mapped_file_queue.load();
        self.mapped_file_queue.check_self();
        if result {
            info!("load commit log Ok (sequential fallback)");
        } else {
            error!("load commit log failed (sequential fallback)");
        }
        result
    }

    pub fn start(&mut self) {
        let mut flush_manager = self.flush_manager.clone();
        let flush_manager_weak = ArcMut::downgrade(&flush_manager);

        tokio::spawn(async move {
            // Acquire lock only for initialization
            {
                if let Some(service) = flush_manager.commit_real_time_service_mut() {
                    service.set_flush_manager(flush_manager_weak);
                }
                flush_manager.start();
            }
        });
    }

    pub fn shutdown(&mut self) {
        error!("shutdown commit log unimplemented");
    }

    pub fn destroy(&mut self) {
        error!("destroy commit log unimplemented");
    }

    pub fn get_message(&self, offset: i64, size: i32) -> Option<SelectMappedBufferResult> {
        let mapped_file_size = self.message_store_config.mapped_file_size_commit_log;
        let mapped_file = self.mapped_file_queue.find_mapped_file_by_offset(offset, offset == 0);
        match mapped_file {
            None => None,
            Some(mmap_file) => {
                let pos = offset % mapped_file_size as i64;
                let mut select_mapped_buffer_result = mmap_file.select_mapped_buffer(pos as i32, size);
                if let Some(ref mut result) = select_mapped_buffer_result {
                    result.mapped_file = Some(mmap_file);
                    result.is_in_cache = self.cold_data_check_service.is_data_in_page_cache();
                }
                select_mapped_buffer_result
            }
        }
    }

    pub fn set_confirm_offset(&mut self, phy_offset: i64) {
        self.confirm_offset = phy_offset;
        self.store_checkpoint.set_confirm_phy_offset(phy_offset as u64);
    }

    fn compute_controller_confirm_offset(&self) -> i64 {
        let Some(message_store) = self.local_file_message_store.as_ref() else {
            return self.confirm_offset;
        };

        if self.message_store_config.broker_role == BrokerRole::Slave || message_store.get_running_flags().is_fenced() {
            return self.confirm_offset;
        }

        let max_phy_offset = message_store.get_max_phy_offset();
        let Some(ha_service) = message_store.get_ha_service() else {
            return self.confirm_offset;
        };

        if ha_service.local_sync_state_set_size(max_phy_offset) <= 1
            || !self.message_store_config.all_ack_in_sync_state_set
        {
            return max_phy_offset;
        }

        if self.confirm_offset >= 0 {
            return self.confirm_offset;
        }

        ha_service.compute_confirm_offset(self.confirm_offset, max_phy_offset)
    }

    fn clamp_controller_recover_confirm_offset(&mut self, min_phy_offset: i64, upper_bound: i64) {
        let upper_bound = upper_bound.max(min_phy_offset);
        let confirm_offset = self.get_confirm_offset();

        if confirm_offset < min_phy_offset {
            error!(
                "confirmOffset {} is less than minPhyOffset {}, correct confirmOffset to minPhyOffset",
                confirm_offset, min_phy_offset
            );
            self.set_confirm_offset(min_phy_offset);
        } else if confirm_offset > upper_bound {
            error!(
                "confirmOffset {} is larger than recovery upper bound {}, correct confirmOffset to upper bound",
                confirm_offset, upper_bound
            );
            self.set_confirm_offset(upper_bound);
        } else {
            self.set_confirm_offset(confirm_offset);
        }
    }

    /// Handle HA service validation and calculate need_ack_nums
    /// Returns (need_ack_nums, should_continue) where should_continue indicates if processing can
    /// continue
    fn handle_ha_service(&self, curr_offset: u64, need_handle_ha: bool) -> Result<i32, PutMessageResult> {
        let mut need_ack_nums = self.message_store_config.in_sync_replicas;

        if !need_handle_ha {
            return Ok(need_ack_nums);
        }

        let local_store = self.local_file_message_store.as_ref().ok_or_else(|| {
            error!("LocalFileMessageStore is None");
            PutMessageResult::new_default(PutMessageStatus::UnknownError)
        })?;

        let ha_service = local_store.get_ha_service().ok_or_else(|| {
            error!("HA Service is None");
            PutMessageResult::new_default(PutMessageStatus::UnknownError)
        })?;

        if self.broker_config.enable_controller_mode {
            if ha_service.in_sync_replicas_nums(curr_offset as i64)
                < self.message_store_config.min_in_sync_replicas as i32
            {
                return Err(PutMessageResult::new_default(PutMessageStatus::InSyncReplicasNotEnough));
            }
            if self.message_store_config.all_ack_in_sync_state_set {
                need_ack_nums = mix_all::ALL_ACK_IN_SYNC_STATE_SET;
            }
        } else if self.broker_config.enable_slave_acting_master {
            let in_sync_replicas = local_store
                .get_alive_replica_num_in_group()
                .min(ha_service.in_sync_replicas_nums(curr_offset as i64));
            need_ack_nums = self.calc_need_ack_nums(in_sync_replicas);
            if need_ack_nums > in_sync_replicas {
                return Err(PutMessageResult::new_default(PutMessageStatus::InSyncReplicasNotEnough));
            }
            if self.message_store_config.all_ack_in_sync_state_set {
                need_ack_nums = mix_all::ALL_ACK_IN_SYNC_STATE_SET;
            }
        }

        Ok(need_ack_nums)
    }

    pub async fn put_messages(&mut self, mut msg_batch: MessageExtBatch) -> PutMessageResult {
        msg_batch.message_ext_broker_inner.message_ext_inner.store_timestamp = current_millis() as i64;
        let tran_type = MessageSysFlag::get_transaction_value(msg_batch.message_ext_broker_inner.sys_flag());
        if MessageSysFlag::TRANSACTION_NOT_TYPE != tran_type {
            return PutMessageResult::new_default(PutMessageStatus::MessageIllegal);
        }
        if msg_batch
            .message_ext_broker_inner
            .message_ext_inner
            .message
            .delay_time_level()
            > 0
        {
            return PutMessageResult::new_default(PutMessageStatus::MessageIllegal);
        }

        //setting ip type:IPV4 OR IPV6, default is ipv4
        let born_host = msg_batch.message_ext_broker_inner.born_host();
        if born_host.is_ipv6() {
            msg_batch.message_ext_broker_inner.with_born_host_v6_flag();
        }

        let store_host = msg_batch.message_ext_broker_inner.store_host();
        if store_host.is_ipv6() {
            msg_batch.message_ext_broker_inner.with_store_host_v6_flag();
        }

        let mut unlock_mapped_file = None;
        let mut mapped_file = self.mapped_file_queue.get_last_mapped_file();
        let curr_offset = if let Some(ref mapped_file_inner) = mapped_file {
            mapped_file_inner.get_wrote_position() as u64 + mapped_file_inner.get_file_from_offset()
        } else {
            0
        };
        let need_handle_ha = self.need_handle_ha(&msg_batch.message_ext_broker_inner);
        let need_ack_nums = match self.handle_ha_service(curr_offset, need_handle_ha) {
            Ok(ack_nums) => ack_nums,
            Err(result) => return result,
        };
        msg_batch.message_ext_broker_inner.version = MessageVersion::V1;
        let auto_message_version_on_topic_len = self.message_store_config.auto_message_version_on_topic_len;
        if auto_message_version_on_topic_len && msg_batch.message_ext_broker_inner.topic().len() > i8::MAX as usize {
            msg_batch.message_ext_broker_inner.version = MessageVersion::V2;
        }
        let mut put_message_context = PutMessageContext::default();
        let encoded_buff = encode_message_ext_batch(&msg_batch, &mut put_message_context, &self.message_store_config);

        let topic_queue_key = generate_key(&msg_batch.message_ext_broker_inner);
        put_message_context.set_topic_queue_table_key(topic_queue_key.clone());
        msg_batch.encoded_buff = encoded_buff;

        let _topic_queue_guard = self.topic_queue_lock.lock(topic_queue_key.as_str()).await;
        self.assign_offset(&mut msg_batch.message_ext_broker_inner);

        let _put_message_lock = self.put_message_lock.lock().await;
        self.begin_time_in_lock
            .store(time_utils::current_millis(), std::sync::atomic::Ordering::Release);
        let start_time = Instant::now();
        // Here settings are stored timestamp, in order to ensure an orderly global
        msg_batch.message_ext_broker_inner.message_ext_inner.store_timestamp = time_utils::current_millis() as i64;

        if mapped_file.is_none() || mapped_file.as_ref().unwrap().is_full() {
            mapped_file = self.mapped_file_queue.get_last_mapped_file_mut_start_offset(0, true);
        }

        if mapped_file.is_none() {
            drop(_put_message_lock);
            drop(_topic_queue_guard); // Explicitly release topic_queue_lock on error
            error!(
                "create mapped file error, topic: {}  clientAddr: {}",
                msg_batch.message_ext_broker_inner.topic(),
                msg_batch.message_ext_broker_inner.born_host()
            );
            self.begin_time_in_lock.store(0, std::sync::atomic::Ordering::Release);
            return PutMessageResult::new_default(PutMessageStatus::CreateMappedFileFailed);
        }

        let result = mapped_file.as_ref().unwrap().append_messages(
            &mut msg_batch,
            self.append_message_callback.as_ref(),
            &mut put_message_context,
            self.enabled_append_prop_crc,
        );
        let put_message_result = match result.status {
            AppendMessageStatus::PutOk => {
                //onCommitLogAppend(msg, result, mappedFile); in java not support this version
                PutMessageResult::new_append_result(PutMessageStatus::PutOk, Some(result))
            }
            AppendMessageStatus::EndOfFile => {
                //onCommitLogAppend(msg, result, mappedFile); in java not support this version
                unlock_mapped_file = mapped_file;
                mapped_file = self.mapped_file_queue.get_last_mapped_file_mut_start_offset(0, true);
                if mapped_file.is_none() {
                    drop(_put_message_lock);
                    drop(_topic_queue_guard);
                    self.begin_time_in_lock.store(0, std::sync::atomic::Ordering::Release);
                    error!(
                        "create mapped file error, topic: {}  clientAddr: {}",
                        msg_batch.message_ext_broker_inner.topic(),
                        msg_batch.message_ext_broker_inner.born_host()
                    );
                    return PutMessageResult::new_append_result(PutMessageStatus::CreateMappedFileFailed, Some(result));
                }
                let result = mapped_file.as_ref().unwrap().append_messages(
                    &mut msg_batch,
                    self.append_message_callback.as_ref(),
                    &mut put_message_context,
                    self.enabled_append_prop_crc,
                );
                if AppendMessageStatus::PutOk == result.status {
                    PutMessageResult::new_append_result(PutMessageStatus::PutOk, Some(result))
                } else {
                    PutMessageResult::new_append_result(PutMessageStatus::UnknownError, Some(result))
                }
            }
            AppendMessageStatus::MessageSizeExceeded | AppendMessageStatus::PropertiesSizeExceeded => {
                drop(_put_message_lock);
                drop(_topic_queue_guard);
                self.begin_time_in_lock.store(0, std::sync::atomic::Ordering::Release);
                return PutMessageResult::new_append_result(PutMessageStatus::MessageIllegal, Some(result));
            }
            AppendMessageStatus::UnknownError => {
                drop(_put_message_lock);
                drop(_topic_queue_guard);
                self.begin_time_in_lock.store(0, std::sync::atomic::Ordering::Release);
                return PutMessageResult::new_append_result(PutMessageStatus::UnknownError, Some(result));
            }
        };
        let elapsed_time_in_lock = start_time.elapsed().as_millis() as u64;
        drop(_put_message_lock);
        self.begin_time_in_lock.store(0, std::sync::atomic::Ordering::Release);
        if elapsed_time_in_lock > 500 {
            warn!(
                "[NOTIFYME]putMessage in lock cost time(ms)={}, bodyLength={} AppendMessageResult={}",
                elapsed_time_in_lock,
                msg_batch.message_ext_broker_inner.body_len(),
                put_message_result.append_message_result().as_ref().unwrap(),
            );
        }
        if let (Some(unlock_mf), true) = (unlock_mapped_file, self.message_store_config.warm_mapped_file_enable) {
            self.local_file_message_store
                .as_ref()
                .unwrap()
                .unlock_mapped_file(unlock_mf.as_ref());
        }
        if put_message_result.put_message_status() == PutMessageStatus::PutOk {
            self.increase_offset(
                &msg_batch.message_ext_broker_inner,
                put_message_context.get_batch_size() as i16,
            );
            // Topic-queue lock released here after successful write
            drop(_topic_queue_guard);
            self.handle_disk_flush_and_ha(
                put_message_result,
                msg_batch.message_ext_broker_inner,
                need_ack_nums,
                need_handle_ha,
            )
            .await
        } else {
            // Write failed - topic_queue_lock will be released, but offset is already assigned
            drop(_topic_queue_guard);
            warn!(
                "Failed to append batch messages to CommitLog, offset hole created for topic={} queue={}",
                msg_batch.message_ext_broker_inner.topic(),
                msg_batch.message_ext_broker_inner.queue_id()
            );
            put_message_result
        }
    }

    pub async fn put_message(&mut self, mut msg: MessageExtBrokerInner) -> PutMessageResult {
        // Set the message body CRC (consider the most appropriate setting on the client)
        msg.message_ext_inner.body_crc = crc32_bytes(msg.message_ext_inner.message.get_body());
        if self.enabled_append_prop_crc {
            // delete crc32 properties if exist
            msg.delete_property(MessageConst::PROPERTY_CRC32);
        }

        //setting message version
        msg.with_version(MessageVersion::V1);
        let topic = msg.topic();
        // setting auto message on topic length
        if self.message_store_config.auto_message_version_on_topic_len && topic.len() > i8::MAX as usize {
            msg.with_version(MessageVersion::V2);
        }

        //setting ip type:IPV4 OR IPV6, default is ipv4
        let born_host = msg.born_host();
        if born_host.is_ipv6() {
            msg.with_born_host_v6_flag();
        }

        let store_host = msg.store_host();
        if store_host.is_ipv6() {
            msg.with_store_host_v6_flag();
        }

        let topic_queue_key = generate_key(&msg);

        let mut unlock_mapped_file = None;

        //get last mapped file from mapped file queue
        let mut mapped_file = self.mapped_file_queue.get_last_mapped_file();
        // current offset is physical offset
        let curr_offset = if let Some(ref mapped_file_inner) = mapped_file {
            mapped_file_inner.get_wrote_position() as u64 + mapped_file_inner.get_file_from_offset()
        } else {
            0
        };
        let need_handle_ha = self.need_handle_ha(&msg);
        let need_ack_nums = match self.handle_ha_service(curr_offset, need_handle_ha) {
            Ok(ack_nums) => ack_nums,
            Err(result) => return result,
        };

        let need_assign_offset = !(self.message_store_config.duplication_enable
            && self.message_store_config.broker_role != BrokerRole::Slave);

        // Encode message BEFORE acquiring any locks
        let (put_message_result, encoded_buff) = encode_message_ext(&msg, &self.message_store_config);
        if let Some(result) = put_message_result {
            return result;
        }
        msg.encoded_buff = Some(encoded_buff);
        let put_message_context = PutMessageContext::new(topic_queue_key.clone());

        let _topic_queue_guard = if need_assign_offset {
            let guard = self.topic_queue_lock.lock(topic_queue_key.as_str()).await;
            self.assign_offset(&mut msg);
            Some(guard)
        } else {
            None
        };

        let _put_message_lock = self.put_message_lock.lock().await;
        let begin_lock_timestamp = time_utils::current_millis();
        self.begin_time_in_lock
            .store(begin_lock_timestamp, std::sync::atomic::Ordering::Release);
        let start_time = Instant::now();
        // Here settings are stored timestamp, in order to ensure an orderly global
        if !self.message_store_config.duplication_enable {
            msg.message_ext_inner.store_timestamp = begin_lock_timestamp as i64;
        }

        if mapped_file.is_none() || mapped_file.as_ref().unwrap().is_full() {
            mapped_file = self.mapped_file_queue.get_last_mapped_file_mut_start_offset(0, true);
        }

        if mapped_file.is_none() {
            drop(_put_message_lock);
            drop(_topic_queue_guard); // Explicitly release topic_queue_lock on error
            error!(
                "create mapped file error, topic: {}  clientAddr: {}",
                msg.topic(),
                msg.born_host()
            );
            self.begin_time_in_lock.store(0, std::sync::atomic::Ordering::Release);
            return PutMessageResult::new_default(PutMessageStatus::CreateMappedFileFailed);
        }

        let result = mapped_file.as_ref().unwrap().append_message(
            &mut msg,
            self.append_message_callback.as_ref(),
            &put_message_context,
        );
        let put_message_result = match result.status {
            AppendMessageStatus::PutOk => {
                //onCommitLogAppend(msg, result, mappedFile); in java not support this version
                PutMessageResult::new_append_result(PutMessageStatus::PutOk, Some(result))
            }
            AppendMessageStatus::EndOfFile => {
                //onCommitLogAppend(msg, result, mappedFile); in java not support this version
                unlock_mapped_file = mapped_file;
                mapped_file = self.mapped_file_queue.get_last_mapped_file_mut_start_offset(0, true);
                if mapped_file.is_none() {
                    drop(_put_message_lock);
                    drop(_topic_queue_guard); // CRITICAL FIX: Release topic_queue_lock on error
                    self.begin_time_in_lock.store(0, std::sync::atomic::Ordering::Release);
                    error!(
                        "create mapped file error, topic: {}  clientAddr: {}",
                        msg.topic(),
                        msg.born_host()
                    );
                    return PutMessageResult::new_append_result(PutMessageStatus::CreateMappedFileFailed, Some(result));
                }
                let result = mapped_file.as_ref().unwrap().append_message(
                    &mut msg,
                    self.append_message_callback.as_ref(),
                    &put_message_context,
                );
                if AppendMessageStatus::PutOk == result.status {
                    PutMessageResult::new_append_result(PutMessageStatus::PutOk, Some(result))
                } else {
                    PutMessageResult::new_append_result(PutMessageStatus::UnknownError, Some(result))
                }
            }
            AppendMessageStatus::MessageSizeExceeded | AppendMessageStatus::PropertiesSizeExceeded => {
                drop(_put_message_lock);
                drop(_topic_queue_guard);
                self.begin_time_in_lock.store(0, std::sync::atomic::Ordering::Release);
                return PutMessageResult::new_append_result(PutMessageStatus::MessageIllegal, Some(result));
            }
            AppendMessageStatus::UnknownError => {
                drop(_put_message_lock);
                drop(_topic_queue_guard);
                self.begin_time_in_lock.store(0, std::sync::atomic::Ordering::Release);
                return PutMessageResult::new_append_result(PutMessageStatus::UnknownError, Some(result));
            }
        };
        let elapsed_time_in_lock = start_time.elapsed().as_millis() as u64;
        drop(_put_message_lock);
        self.begin_time_in_lock.store(0, std::sync::atomic::Ordering::Release);
        if elapsed_time_in_lock > 500 {
            warn!(
                "[NOTIFYME]putMessage in lock cost time(ms)={}, bodyLength={} AppendMessageResult={}",
                elapsed_time_in_lock,
                msg.body_len(),
                put_message_result.append_message_result().as_ref().unwrap(),
            );
        }

        if let (Some(unlock_mf), true) = (unlock_mapped_file, self.message_store_config.warm_mapped_file_enable) {
            self.local_file_message_store
                .as_ref()
                .unwrap()
                .unlock_mapped_file(unlock_mf.as_ref());
        }

        if put_message_result.put_message_status() == PutMessageStatus::PutOk {
            let message_num = get_message_num(&self.topic_config_table, &msg);
            self.increase_offset(&msg, message_num);
            // Topic-queue lock released here after successful write
            drop(_topic_queue_guard);
            self.handle_disk_flush_and_ha(put_message_result, msg, need_ack_nums, need_handle_ha)
                .await
        } else {
            // Write failed - topic_queue_lock will be released, but offset is already assigned
            // This creates a hole in ConsumeQueue that will be filled by recovery process
            drop(_topic_queue_guard);
            warn!(
                "Failed to append message to CommitLog, offset hole created for topic={} queue={}, will be recovered",
                msg.topic(),
                msg.queue_id()
            );
            put_message_result
        }
    }

    fn increase_offset(&self, msg: &MessageExtBrokerInner, message_num: i16) {
        let tran_type = MessageSysFlag::get_transaction_value(msg.sys_flag());
        if MessageSysFlag::TRANSACTION_NOT_TYPE == tran_type || MessageSysFlag::TRANSACTION_COMMIT_TYPE == tran_type {
            self.consume_queue_store.increase_queue_offset(msg, message_num);
        }
    }

    #[inline]
    fn assign_offset(&self, msg: &mut MessageExtBrokerInner) {
        let tran_type = MessageSysFlag::get_transaction_value(msg.sys_flag());
        // if the message is not transaction message or transaction commit message
        if MessageSysFlag::TRANSACTION_NOT_TYPE == tran_type || MessageSysFlag::TRANSACTION_COMMIT_TYPE == tran_type {
            self.consume_queue_store.assign_queue_offset(msg);
        }
    }

    #[inline]
    fn calc_need_ack_nums(&self, in_sync_replicas: i32) -> i32 {
        let mut need_ack_nums = self.message_store_config.in_sync_replicas;
        if self.message_store_config.enable_auto_in_sync_replicas {
            need_ack_nums = need_ack_nums.min(in_sync_replicas);
            need_ack_nums = need_ack_nums.max(self.message_store_config.min_in_sync_replicas as i32);
        }
        need_ack_nums
    }

    /// Handles disk flushing and high availability (HA) operations for a message.
    ///
    /// This function determines the appropriate actions to take based on the flush disk type
    /// and whether HA handling is required. It performs disk flushing and HA replication
    /// either synchronously or asynchronously, depending on the configuration.
    ///
    /// # Arguments
    ///
    /// * `put_message_result` - The result of the message put operation, which may be updated based
    ///   on the outcomes of disk flushing and HA.
    /// * `msg` - The message being processed.
    /// * `need_ack_nums` - The number of acknowledgments required for HA.
    /// * `need_handle_ha` - A boolean indicating whether HA handling is required.
    ///
    /// # Returns
    ///
    /// Returns the updated `PutMessageResult` after handling disk flushing and HA.
    async fn handle_disk_flush_and_ha(
        &self,
        mut put_message_result: PutMessageResult,
        msg: MessageExtBrokerInner,
        need_ack_nums: i32,
        need_handle_ha: bool,
    ) -> PutMessageResult {
        let append_message_result = put_message_result.append_message_result().unwrap();

        // Use efficient branching based on actual requirements
        match (self.message_store_config.flush_disk_type, need_handle_ha) {
            // Sync flush + HA: Must wait for both in parallel
            (FlushDiskType::SyncFlush, true) => {
                let (flush_status, replica_status) = tokio::join!(
                    self.handle_disk_flush(append_message_result, &msg),
                    self.handle_ha(append_message_result, need_ack_nums)
                );
                if flush_status != PutMessageStatus::PutOk {
                    put_message_result.set_put_message_status(flush_status);
                }
                if replica_status != PutMessageStatus::PutOk {
                    put_message_result.set_put_message_status(replica_status);
                }
            }
            // Sync flush only: Wait for flush only
            (FlushDiskType::SyncFlush, false) => {
                let flush_status = self.handle_disk_flush(append_message_result, &msg).await;
                if flush_status != PutMessageStatus::PutOk {
                    put_message_result.set_put_message_status(flush_status);
                }
            }
            // Async flush + HA: Only wait for HA, flush happens asynchronously
            (FlushDiskType::AsyncFlush, true) => {
                // Trigger async flush (no waiting)
                let _ = self.handle_disk_flush(append_message_result, &msg).await;
                // Wait for HA replication
                let replica_status = self.handle_ha(append_message_result, need_ack_nums).await;
                if replica_status != PutMessageStatus::PutOk {
                    put_message_result.set_put_message_status(replica_status);
                }
            }
            // Async flush only: Don't wait for anything (fastest path)
            (FlushDiskType::AsyncFlush, false) => {
                // Trigger async flush and return immediately
                let _ = self.handle_disk_flush(append_message_result, &msg).await;
                // No waiting needed - this is the hot path for high-throughput scenarios
            }
        }

        put_message_result
    }

    async fn handle_ha(&self, put_message_result: &AppendMessageResult, need_ack_nums: i32) -> PutMessageStatus {
        if need_ack_nums <= 1 {
            return PutMessageStatus::PutOk;
        }
        let next_offset = put_message_result.wrote_offset + put_message_result.wrote_bytes as i64;

        let (request, mut response) = GroupCommitRequest::with_ack_nums(
            next_offset,
            self.message_store_config.slave_timeout as u64,
            need_ack_nums,
        );
        if let Some(local_file_message_store) = &self.local_file_message_store {
            if let Some(ha_service) = local_file_message_store.get_ha_service() {
                ha_service.put_request(request).await;
                //Notify the HA service to handle the request
                ha_service.get_wait_notify_object().notify_waiters();
            }
        } else {
            error!("local file message store is not initialized for HA handling");
            return PutMessageStatus::UnknownError;
        }
        match response.wait_for_result_with_timeout().await {
            Ok(status) => status,
            Err(e) => {
                error!("Failed to wait for HA result: {:?}", e);
                PutMessageStatus::UnknownError
            }
        }
    }

    async fn handle_disk_flush(
        &self,
        put_message_result: &AppendMessageResult,
        msg: &MessageExtBrokerInner,
    ) -> PutMessageStatus {
        // Acquire lock and immediately call handle_disk_flush which internally
        // only triggers async operations without holding the lock
        self.flush_manager
            .mut_from_ref()
            .handle_disk_flush(put_message_result, msg)
            .await
    }

    fn need_handle_ha(&self, msg_inner: &MessageExtBrokerInner) -> bool {
        if !msg_inner.is_wait_store_msg_ok() {
            /*
             No need to sync messages that special config to extra broker slaves.
             @see MessageConst.PROPERTY_WAIT_STORE_MSG_OK
            */
            return false;
        }
        if self.message_store_config.duplication_enable {
            return false;
        }
        if BrokerRole::SyncMaster != self.message_store_config.broker_role {
            // No need to check ha in async or slave broker
            return false;
        }

        true
    }

    fn on_commit_log_dispatch(
        &mut self,
        request: &mut DispatchRequest,
        do_dispatch: bool,
        is_recover: bool,
        is_file_end: bool,
    ) {
        if do_dispatch && !is_file_end {
            self.dispatcher.dispatch(request);
        }
    }

    pub fn is_multi_dispatch_msg(msg_inner: &MessageExtBrokerInner) -> bool {
        msg_inner
            .property(MessageConst::PROPERTY_INNER_MULTI_DISPATCH)
            .is_some_and(|s| !s.is_empty())
            && msg_inner.topic().starts_with(mix_all::RETRY_GROUP_TOPIC_PREFIX)
    }

    /// Optimized normal recovery with batched I/O
    ///
    /// Performance improvements:
    /// - Batched message reading (64KB chunks) reduces syscalls
    /// - Zero-copy parsing using memory-mapped regions
    /// - Pre-allocated buffers reduce allocation overhead
    /// - Optimized iteration pattern minimizes redundant checks
    pub async fn recover_normally_optimized(
        &mut self,
        max_phy_offset_of_consume_queue: i64,
        mut message_store: ArcMut<LocalFileMessageStore>,
    ) {
        use crate::log_file::commit_log_recovery::BatchMessageIterator;
        use crate::log_file::commit_log_recovery::RecoveryContext;

        let start = std::time::Instant::now();
        let check_crc_on_recover = self.message_store_config.check_crc_on_recover;
        let check_dup_info = self.message_store_config.duplication_enable;
        let message_store_config = self.message_store_config.clone();
        let broker_config = self.broker_config.clone();

        let mapped_files = self.mapped_file_queue.get_mapped_files();
        let mapped_files_inner = mapped_files.load();

        if mapped_files_inner.is_empty() {
            warn!("The commitlog files are deleted, and delete the consume queue files");
            self.mapped_file_queue.set_flushed_where(0);
            self.mapped_file_queue.set_committed_where(0);
            message_store.consume_queue_store_mut().destroy();
            message_store.consume_queue_store_mut().load_after_destroy();
            return;
        }

        // Start from the last third file
        let mut index = (mapped_files_inner.len() as i32) - 3;
        if index <= 0 {
            index = 0;
        }
        let mut index = index as usize;

        let mut last_valid_msg_phy_offset = self.get_confirm_offset().max(0) as u64;
        let do_dispatch = false;

        let mut recovery_ctx = RecoveryContext::new(
            check_crc_on_recover,
            check_dup_info,
            message_store_config,
            self.local_file_message_store.as_ref().unwrap().max_delay_level(),
            self.local_file_message_store
                .as_ref()
                .unwrap()
                .delay_level_table_ref()
                .clone(),
        );

        while index < mapped_files_inner.len() {
            let mapped_file = mapped_files_inner.get(index).unwrap();
            let process_offset = mapped_file.get_file_from_offset();

            info!(
                "Recovering physics file: {} (optimized batch mode)",
                mapped_file.get_file_name()
            );

            let mut iterator = BatchMessageIterator::new(mapped_file);
            let mut file_processed = false;

            while let Some((mut msg_bytes, absolute_offset, msg_size)) = iterator.next_message() {
                let mut dispatch_request = recovery_ctx.process_message(&mut msg_bytes, absolute_offset);

                if dispatch_request.success && dispatch_request.msg_size > 0 {
                    last_valid_msg_phy_offset =
                        (process_offset + absolute_offset as u64) + dispatch_request.msg_size as u64;
                    self.on_commit_log_dispatch(&mut dispatch_request, do_dispatch, true, false);
                    file_processed = true;
                } else if dispatch_request.success && dispatch_request.msg_size == 0 {
                    // End of file marker
                    self.on_commit_log_dispatch(&mut dispatch_request, do_dispatch, true, true);
                    break;
                } else {
                    // Invalid message
                    if dispatch_request.msg_size > 0 {
                        warn!(
                            "found a half message at {}, it will be truncated.",
                            process_offset + absolute_offset as u64,
                        );
                    }
                    info!("recover physics file end: {}", mapped_file.get_file_name());
                    break;
                }
            }

            if file_processed {
                recovery_ctx.stats.files_processed += 1;
            }

            index += 1;
        }

        if broker_config.enable_controller_mode {
            self.clamp_controller_recover_confirm_offset(
                message_store.get_min_phy_offset(),
                last_valid_msg_phy_offset as i64,
            );
        } else {
            self.set_confirm_offset(last_valid_msg_phy_offset as i64);
        }

        let process_offset = last_valid_msg_phy_offset;

        // Clear ConsumeQueue redundant data
        if max_phy_offset_of_consume_queue as u64 >= process_offset {
            warn!(
                "maxPhyOffsetOfConsumeQueue({}) >= processOffset({}), truncate dirty logic files",
                max_phy_offset_of_consume_queue, process_offset
            );
            message_store.truncate_dirty_logic_files(process_offset as i64);
        }

        self.mapped_file_queue.set_flushed_where(process_offset as i64);
        self.mapped_file_queue.set_committed_where(process_offset as i64);
        self.mapped_file_queue.truncate_dirty_files(process_offset as i64);

        recovery_ctx.stats.recovery_time_ms = start.elapsed().as_millis();
        recovery_ctx.stats.log_summary("Normal");
    }

    pub async fn recover_normally(
        &mut self,
        max_phy_offset_of_consume_queue: i64,
        mut message_store: ArcMut<LocalFileMessageStore>,
    ) {
        let check_crc_on_recover = self.message_store_config.check_crc_on_recover;
        let check_dup_info = self.message_store_config.duplication_enable;
        let message_store_config = self.message_store_config.clone();
        let broker_config = self.broker_config.clone();
        // let mut mapped_file_queue = mapped_files.write().await;
        let mapped_files = self.mapped_file_queue.get_mapped_files();
        let mapped_files_inner = mapped_files.load();
        if !mapped_files_inner.is_empty() {
            // Began to recover from the last third file
            let mut index = (mapped_files_inner.len() as i32) - 3;
            if index <= 0 {
                index = 0;
            }
            let mut index = index as usize;
            //let mut mapped_file = mapped_files_inner.get(index).unwrap().lock().await;
            let mut mapped_file = mapped_files_inner.get(index).unwrap();
            let mut process_offset = mapped_file.get_file_from_offset();
            let mut mapped_file_offset = 0u64;
            //When recovering, the maximum value obtained when getting get_confirm_offset is
            // the file size of the latest file plus the value resolved from the file name.
            let mut last_valid_msg_phy_offset = self.get_confirm_offset().max(0) as u64;
            // normal recover doesn't require dispatching
            let do_dispatch = false;
            let mut current_pos = 0usize;
            loop {
                let (msg, size) = self.get_simple_message_bytes(current_pos, mapped_file.as_ref());
                if msg.is_none() {
                    break;
                }
                let mut msg_bytes = msg.unwrap();
                let mut dispatch_request = check_message_and_return_size(
                    &mut msg_bytes,
                    check_crc_on_recover,
                    check_dup_info,
                    true,
                    &message_store_config,
                    self.local_file_message_store.as_ref().unwrap().max_delay_level(),
                    self.local_file_message_store.as_ref().unwrap().delay_level_table_ref(),
                );
                current_pos += size;
                if dispatch_request.success && dispatch_request.msg_size > 0 {
                    last_valid_msg_phy_offset = process_offset + mapped_file_offset;
                    mapped_file_offset += dispatch_request.msg_size as u64;
                    self.on_commit_log_dispatch(&mut dispatch_request, do_dispatch, true, false);
                } else if dispatch_request.success && dispatch_request.msg_size == 0 {
                    // Come the end of the file, switch to the next file Since the
                    // return 0 representatives met last hole,
                    // this can not be included in truncate offset
                    self.on_commit_log_dispatch(&mut dispatch_request, do_dispatch, true, true);
                    index += 1;
                    if index >= mapped_files_inner.len() {
                        info!(
                            "recover last 3 physics file over, last mapped file:{} ",
                            mapped_file.get_file_name()
                        );
                        break;
                    } else {
                        mapped_file = mapped_files_inner.get(index).unwrap();
                        mapped_file_offset = 0;
                        process_offset = mapped_file.get_file_from_offset();
                        current_pos = 0;
                        info!("recover next physics file:{}", mapped_file.get_file_name());
                    }
                } else if !dispatch_request.success {
                    if dispatch_request.msg_size > 0 {
                        warn!(
                            "found a half message at {}, it will be truncated.",
                            process_offset + mapped_file_offset,
                        );
                    }
                    info!("recover physics file end,{} ", mapped_file.get_file_name());
                    break;
                }
            }
            process_offset += mapped_file_offset;
            if broker_config.enable_controller_mode {
                self.clamp_controller_recover_confirm_offset(message_store.get_min_phy_offset(), process_offset as i64);
            } else {
                self.set_confirm_offset(last_valid_msg_phy_offset as i64);
            }

            // Clear ConsumeQueue redundant data
            if max_phy_offset_of_consume_queue as u64 >= process_offset {
                warn!(
                    "maxPhyOffsetOfConsumeQueue({}) >= processOffset({}), truncate dirty logic files",
                    max_phy_offset_of_consume_queue, process_offset
                );
                message_store.truncate_dirty_logic_files(process_offset as i64)
            }
            self.mapped_file_queue.set_flushed_where(process_offset as i64);
            self.mapped_file_queue.set_committed_where(process_offset as i64);
            self.mapped_file_queue.truncate_dirty_files(process_offset as i64);
        } else {
            warn!(
                "The commitlog files are deleted, and delete the consume queue
                                        files"
            );
            self.mapped_file_queue.set_flushed_where(0);
            self.mapped_file_queue.set_committed_where(0);
            message_store.consume_queue_store_mut().destroy();
            message_store.consume_queue_store_mut().load_after_destroy();
        }
    }

    fn get_simple_message_bytes<MF: MappedFile>(&self, position: usize, mapped_file: &MF) -> (Option<Bytes>, usize) {
        let mut bytes = mapped_file.get_bytes(position, 4);
        match bytes {
            None => (None, 0),
            Some(ref mut inner) => {
                let size = inner.get_i32();
                if size <= 0 {
                    return (None, 0);
                }
                (mapped_file.get_bytes(position, size as usize), size as usize)
            }
        }
    }

    //Fetch and compute the newest confirmOffset.
    pub fn get_confirm_offset(&self) -> i64 {
        if self.broker_config.enable_controller_mode {
            return self.compute_controller_confirm_offset();
        } else if self.broker_config.duplication_enable {
            return self.confirm_offset;
        }
        let ms = self.local_file_message_store.as_ref().unwrap();
        if ms.is_sync_disk_flush() {
            self.get_flushed_where()
        } else {
            self.get_max_offset()
        }
    }

    pub fn get_confirm_offset_directly(&self) -> i64 {
        if self.broker_config.enable_controller_mode {
            let Some(message_store) = self.local_file_message_store.as_ref() else {
                return self.confirm_offset;
            };

            if self.message_store_config.broker_role != BrokerRole::Slave
                && !message_store.get_running_flags().is_fenced()
            {
                let max_phy_offset = message_store.get_max_phy_offset();
                if let Some(ha_service) = message_store.get_ha_service() {
                    if ha_service.local_sync_state_set_size(max_phy_offset) <= 1 {
                        return max_phy_offset;
                    }
                }
            }

            self.confirm_offset
        } else if self.broker_config.duplication_enable {
            self.confirm_offset
        } else {
            self.get_max_offset()
        }
    }

    /// Optimized abnormal recovery with batched I/O
    ///
    /// Performance improvements:
    /// - Fast checkpoint-based file scanning
    /// - Batched message reading (64KB chunks)
    /// - Zero-copy validation using mmap regions
    /// - Reduced lock contention through buffered dispatch
    pub async fn recover_abnormally_optimized(
        &mut self,
        max_phy_offset_of_consume_queue: i64,
        mut message_store: ArcMut<LocalFileMessageStore>,
    ) {
        use crate::log_file::commit_log_recovery::find_recovery_start_index;
        use crate::log_file::commit_log_recovery::BatchMessageIterator;
        use crate::log_file::commit_log_recovery::RecoveryContext;

        let start = std::time::Instant::now();
        let check_crc_on_recover = self.message_store_config.check_crc_on_recover;
        let check_dup_info = self.message_store_config.duplication_enable;
        let broker_config = self.broker_config.clone();

        let binding = self.mapped_file_queue.get_mapped_files();
        let mapped_files_inner = binding.load();

        if mapped_files_inner.is_empty() {
            warn!("The commitlog files are deleted, and delete the consume queue files");
            self.mapped_file_queue.set_flushed_where(0);
            self.mapped_file_queue.set_committed_where(0);
            message_store.consume_queue_store_mut().destroy();
            message_store.consume_queue_store_mut().load_after_destroy();
            return;
        }

        // Find starting point using checkpoint (optimized)
        let index = find_recovery_start_index(&mapped_files_inner, &self.message_store_config, &self.store_checkpoint);

        info!("Starting abnormal recovery from file index {} (optimized)", index);

        let mut index = index;
        let mut last_valid_msg_phy_offset = 0u64;
        let mut last_confirm_valid_msg_phy_offset = 0u64;
        let do_dispatch = true;

        let mut recovery_ctx = RecoveryContext::new(
            check_crc_on_recover,
            check_dup_info,
            self.message_store_config.clone(),
            self.local_file_message_store.as_ref().unwrap().max_delay_level(),
            self.local_file_message_store
                .as_ref()
                .unwrap()
                .delay_level_table_ref()
                .clone(),
        );

        while index < mapped_files_inner.len() {
            let mapped_file = mapped_files_inner.get(index).unwrap();
            let process_offset = mapped_file.get_file_from_offset();

            if index == 0 {
                last_valid_msg_phy_offset = process_offset;
                last_confirm_valid_msg_phy_offset = process_offset;
            }

            info!(
                "Recovering physics file: {} (optimized batch mode)",
                mapped_file.get_file_name()
            );

            let mut iterator = BatchMessageIterator::new(mapped_file);
            let mut file_processed = false;

            while let Some((mut msg_bytes, absolute_offset, msg_size)) = iterator.next_message() {
                let mut dispatch_request = recovery_ctx.process_message(&mut msg_bytes, absolute_offset);

                if dispatch_request.success && dispatch_request.msg_size > 0 {
                    last_valid_msg_phy_offset =
                        (process_offset + absolute_offset as u64) + dispatch_request.msg_size as u64;

                    let should_dispatch =
                        if self.message_store_config.duplication_enable || broker_config.enable_controller_mode {
                            dispatch_request.commit_log_offset + msg_size as i64 <= self.get_confirm_offset()
                        } else {
                            true
                        };

                    if should_dispatch {
                        self.on_commit_log_dispatch(&mut dispatch_request, do_dispatch, true, false);
                        last_confirm_valid_msg_phy_offset = dispatch_request.commit_log_offset as u64 + msg_size as u64;
                    }

                    file_processed = true;
                } else if dispatch_request.success && dispatch_request.msg_size == 0 {
                    // End of file marker
                    self.on_commit_log_dispatch(&mut dispatch_request, do_dispatch, true, true);
                    break;
                } else {
                    // Invalid message
                    if dispatch_request.msg_size > 0 {
                        warn!(
                            "found a half message at {}, it will be truncated.",
                            process_offset + absolute_offset as u64,
                        );
                    }
                    info!("recover physics file end: {}", mapped_file.get_file_name());
                    break;
                }
            }

            if file_processed {
                recovery_ctx.stats.files_processed += 1;
            }

            index += 1;
        }

        let process_offset = last_valid_msg_phy_offset;

        if broker_config.enable_controller_mode {
            self.clamp_controller_recover_confirm_offset(
                message_store.get_min_phy_offset(),
                last_confirm_valid_msg_phy_offset as i64,
            );
        } else {
            self.set_confirm_offset(last_valid_msg_phy_offset as i64);
        }

        // Clear ConsumeQueue redundant data
        if max_phy_offset_of_consume_queue as u64 >= process_offset {
            warn!(
                "maxPhyOffsetOfConsumeQueue({}) >= processOffset({}), truncate dirty logic files",
                max_phy_offset_of_consume_queue, process_offset
            );
            message_store.truncate_dirty_logic_files(process_offset as i64);
        }

        self.mapped_file_queue.set_flushed_where(process_offset as i64);
        self.mapped_file_queue.set_committed_where(process_offset as i64);
        self.mapped_file_queue.truncate_dirty_files(process_offset as i64);

        recovery_ctx.stats.recovery_time_ms = start.elapsed().as_millis();
        recovery_ctx.stats.log_summary("Abnormal");
    }

    pub async fn recover_abnormally(
        &mut self,
        max_phy_offset_of_consume_queue: i64,
        mut message_store: ArcMut<LocalFileMessageStore>,
    ) {
        let check_crc_on_recover = self.message_store_config.check_crc_on_recover;
        let check_dup_info = self.message_store_config.duplication_enable;
        let broker_config = self.broker_config.clone();
        // let mut mapped_file_queue = mapped_files.write().await;
        let binding = self.mapped_file_queue.get_mapped_files();
        let mapped_files_inner = binding.load();
        if !mapped_files_inner.is_empty() {
            // Began to recover from the last third file
            let mut index = (mapped_files_inner.len() as i32) - 1;
            while index >= 0 {
                let mapped_file = mapped_files_inner.get(index as usize).unwrap();
                if is_mapped_file_matched_recover(&self.message_store_config, mapped_file, &self.store_checkpoint) {
                    break;
                }
                index -= 1;
            }
            if index <= 0 {
                index = 0;
            }
            let mut index = index as usize;
            //let mut mapped_file = mapped_files_inner.get(index).unwrap().lock().await;
            let mut mapped_file = mapped_files_inner.get(index).unwrap();
            let mut process_offset = mapped_file.get_file_from_offset();
            let mut mapped_file_offset = 0u64;
            //When recovering, the maximum value obtained when getting get_confirm_offset is
            // the file size of the latest file plus the value resolved from the file name.
            let mut last_valid_msg_phy_offset = process_offset;
            let mut last_confirm_valid_msg_phy_offset = process_offset;
            // normal recover doesn't require dispatching
            let do_dispatch = true;
            let mut current_pos = 0usize;
            loop {
                let (msg, size) = self.get_simple_message_bytes(current_pos, mapped_file.as_ref());
                if msg.is_none() {
                    break;
                }
                let mut msg_bytes = msg.unwrap();
                let mut dispatch_request = check_message_and_return_size(
                    &mut msg_bytes,
                    check_crc_on_recover,
                    check_dup_info,
                    true,
                    &self.message_store_config,
                    self.local_file_message_store.as_ref().unwrap().max_delay_level(),
                    self.local_file_message_store.as_ref().unwrap().delay_level_table_ref(),
                );
                current_pos += size;
                if dispatch_request.success && dispatch_request.msg_size > 0 {
                    last_valid_msg_phy_offset = process_offset + mapped_file_offset;
                    mapped_file_offset += dispatch_request.msg_size as u64;

                    if self.message_store_config.duplication_enable || self.broker_config.enable_controller_mode {
                        if dispatch_request.commit_log_offset + size as i64 <= self.get_confirm_offset() {
                            self.on_commit_log_dispatch(&mut dispatch_request, do_dispatch, true, false);
                            last_confirm_valid_msg_phy_offset = dispatch_request.commit_log_offset as u64 + size as u64;
                        }
                    } else {
                        self.on_commit_log_dispatch(&mut dispatch_request, do_dispatch, true, false);
                    }
                } else if dispatch_request.success && dispatch_request.msg_size == 0 {
                    // Come the end of the file, switch to the next file Since the
                    // return 0 representatives met last hole,
                    // this can not be included in truncate offset
                    self.on_commit_log_dispatch(&mut dispatch_request, do_dispatch, true, true);
                    index += 1;
                    if index >= mapped_files_inner.len() {
                        info!(
                            "recover last 3 physics file over, last mapped file:{} ",
                            mapped_file.get_file_name()
                        );
                        break;
                    } else {
                        mapped_file = mapped_files_inner.get(index).unwrap();
                        mapped_file_offset = 0;
                        process_offset = mapped_file.get_file_from_offset();
                        current_pos = 0;
                        info!("recover next physics file:{}", mapped_file.get_file_name());
                    }
                } else if !dispatch_request.success {
                    if dispatch_request.msg_size > 0 {
                        warn!(
                            "found a half message at {}, it will be truncated.",
                            process_offset + mapped_file_offset,
                        );
                    }
                    info!("recover physics file end,{} ", mapped_file.get_file_name());
                    break;
                }
            }

            // only for rocksdb mode
            // this.getMessageStore().finishCommitLogDispatch();

            process_offset += mapped_file_offset;
            if broker_config.enable_controller_mode {
                self.clamp_controller_recover_confirm_offset(
                    message_store.get_min_phy_offset(),
                    last_confirm_valid_msg_phy_offset as i64,
                );
            } else {
                self.set_confirm_offset(last_valid_msg_phy_offset as i64);
            }

            // Clear ConsumeQueue redundant data
            if max_phy_offset_of_consume_queue as u64 >= process_offset {
                warn!(
                    "maxPhyOffsetOfConsumeQueue({}) >= processOffset({}), truncate dirty logic files",
                    max_phy_offset_of_consume_queue, process_offset
                );
                message_store.truncate_dirty_logic_files(process_offset as i64)
            }
            self.mapped_file_queue.set_flushed_where(process_offset as i64);
            self.mapped_file_queue.set_committed_where(process_offset as i64);
            self.mapped_file_queue.truncate_dirty_files(process_offset as i64);
        } else {
            warn!(
                "The commitlog files are deleted, and delete the consume queue
                                        files"
            );
            self.mapped_file_queue.set_flushed_where(0);
            self.mapped_file_queue.set_committed_where(0);
            message_store.consume_queue_store_mut().destroy();
            message_store.consume_queue_store_mut().load_after_destroy();
        }
    }

    #[inline]
    pub fn get_max_offset(&self) -> i64 {
        self.mapped_file_queue.get_max_offset()
    }

    #[inline]
    pub fn get_flushed_where(&self) -> i64 {
        self.mapped_file_queue.get_flushed_where()
    }

    pub fn get_min_offset(&self) -> i64 {
        match self.mapped_file_queue.get_first_mapped_file() {
            None => -1,
            Some(mapped_file) => {
                if mapped_file.is_available() {
                    mapped_file.get_file_from_offset() as i64
                } else {
                    self.roll_next_file(mapped_file.get_file_from_offset() as i64)
                }
            }
        }
    }

    pub fn roll_next_file(&self, offset: i64) -> i64 {
        let mapped_file_size = self.message_store_config.mapped_file_size_commit_log as i64;
        offset + mapped_file_size - (offset % mapped_file_size)
    }

    pub fn get_data(&self, offset: i64) -> Option<SelectMappedBufferResult> {
        self.get_data_with_option(offset, offset == 0)
    }

    pub fn get_bulk_data(&self, offset: i64, size: i32) -> Option<Vec<SelectMappedBufferResult>> {
        unimplemented!("get_bulk_data not implemented")
    }

    pub fn get_data_with_option(
        &self,
        offset: i64,
        return_first_on_not_found: bool,
    ) -> Option<SelectMappedBufferResult> {
        let mapped_file_size = self.message_store_config.mapped_file_size_commit_log as i64;
        let mapped_file = self
            .mapped_file_queue
            .find_mapped_file_by_offset(offset, return_first_on_not_found);
        if let Some(mapped_file) = mapped_file {
            let pos = (offset % mapped_file_size) as i32;
            let mut result = mapped_file.select_mapped_buffer_with_position(pos);
            if let Some(ref mut result) = result {
                result.mapped_file = Some(mapped_file);
            }
            result
        } else {
            None
        }
    }

    pub fn check_self(&self) {
        self.mapped_file_queue.check_self();
    }

    pub fn lock_time_mills(&self) -> i64 {
        let begin = self.begin_time_in_lock.load(std::sync::atomic::Ordering::Acquire);
        if begin > 0 {
            (SystemClock::now() - (begin as u128)) as i64
        } else {
            0
        }
    }

    pub fn begin_time_in_lock(&self) -> &Arc<AtomicU64> {
        &self.begin_time_in_lock
    }

    pub fn remain_how_many_data_to_commit(&self) -> i64 {
        self.mapped_file_queue.remain_how_many_data_to_commit()
    }

    pub fn remain_how_many_data_to_flush(&self) -> i64 {
        self.mapped_file_queue.remain_how_many_data_to_flush()
    }

    pub fn pickup_store_timestamp(&self, offset: i64, size: i32) -> i64 {
        if offset >= self.get_min_offset() && (offset + size as i64) <= self.get_max_offset() {
            let result = self.get_message(offset, size);
            if let Some(result) = result {
                let buffer = result.get_buffer();
                let sys_flag = (&buffer[MessageDecoder::SYSFLAG_POSITION..]).get_i32();
                let born_host_length = if sys_flag & MessageSysFlag::BORNHOST_V6_FLAG == 0 {
                    8
                } else {
                    20
                };
                let msg_store_time_pos = born_host_length + 4 + 4 + 4 + 4 + 4 + 8 + 8 + 4 + 8;
                (&buffer[msg_store_time_pos..]).get_i64()
            } else {
                -1
            }
        } else {
            -1
        }
    }

    /// Get the cold data check service for checking if message data is in cold storage area
    #[inline]
    pub fn get_cold_data_check_service(&self) -> &ColdDataCheckService {
        &self.cold_data_check_service
    }

    pub async fn append_data(
        &mut self,
        start_offset: i64,
        data: &[u8],
        data_start: i32,
        data_length: i32,
    ) -> Result<bool, StoreError> {
        let lock = self.put_message_lock.lock().await;
        let mapped_file = self
            .mapped_file_queue
            .get_last_mapped_file_mut_start_offset(start_offset as u64, true);
        if mapped_file.is_none() {
            drop(lock);
            return Err(StoreError::MappedFileNotFound);
        }
        let Some(mapped_file) = self
            .mapped_file_queue
            .get_last_mapped_file_mut_start_offset(start_offset as u64, true)
        else {
            drop(lock);
            return Err(StoreError::MappedFileNotFound);
        };
        let flag = mapped_file.append_message_offset_length(data, data_start as usize, data_length as usize);
        drop(lock);
        Ok(flag)
    }

    pub fn set_local_file_message_store(&mut self, local_file_message_store: ArcMut<LocalFileMessageStore>) {
        self.local_file_message_store = Some(local_file_message_store);
    }

    pub fn sync_broker_role(&mut self, broker_role: BrokerRole) {
        Arc::make_mut(&mut self.message_store_config).broker_role = broker_role;
    }
}

pub fn check_message_and_return_size(
    bytes: &mut Bytes,
    check_crc: bool,
    check_dup_info: bool,
    read_body: bool,
    message_store_config: &Arc<MessageStoreConfig>,
    max_delay_level: i32,
    delay_level_table: &BTreeMap<i32 /* level */, i64 /* delay timeMillis */>,
) -> DispatchRequest {
    // Total size
    let total_size = bytes.get_i32();

    // message magic code
    let magic_code = bytes.get_i32();
    match magic_code {
        MESSAGE_MAGIC_CODE | MESSAGE_MAGIC_CODE_V2 => {
            // Continue processing the message
        }
        BLANK_MAGIC_CODE => {
            return DispatchRequest {
                msg_size: 0,
                success: true,
                ..Default::default()
            };
        }
        _ => {
            warn!("found a illegal magic code 0x{}", format!("{:X}", magic_code),);
            return DispatchRequest {
                msg_size: -1,
                success: false,
                ..Default::default()
            };
        }
    }
    let message_version = match MessageVersion::value_of_magic_code(magic_code) {
        Ok(value) => value,
        Err(_) => {
            return DispatchRequest {
                msg_size: -1,
                success: false,
                ..Default::default()
            }
        }
    };
    let body_crc = bytes.get_i32();
    let queue_id = bytes.get_i32();
    let flag = bytes.get_i32();
    let queue_offset = bytes.get_i64();
    let physic_offset = bytes.get_i64();
    let sys_flag = bytes.get_i32();
    let born_time_stamp = bytes.get_i64();

    let born_host = if sys_flag & MessageSysFlag::BORNHOST_V6_FLAG == 0 {
        bytes.copy_to_bytes(8)
    } else {
        bytes.copy_to_bytes(20)
    };
    let store_timestamp = bytes.get_i64();

    let store_host = if sys_flag & MessageSysFlag::STOREHOSTADDRESS_V6_FLAG == 0 {
        bytes.copy_to_bytes(8)
    } else {
        bytes.copy_to_bytes(20)
    };

    let reconsume_times = bytes.get_i32();
    let prepared_transaction_offset = bytes.get_i64();
    let body_len = bytes.get_i32();
    if body_len > 0 {
        if read_body {
            //body content
            let body = bytes.copy_to_bytes(body_len as usize);
            if check_crc && !message_store_config.force_verify_prop_crc {
                let crc = crc32(body.as_ref());
                if crc != body_crc as u32 {
                    warn!("CRC check failed. bodyCRC={}, currentCRC={}", crc, body_crc);
                    return DispatchRequest {
                        msg_size: -1,
                        success: false,
                        ..Default::default()
                    };
                }
            }
        } else {
            //skip body content
            bytes.advance(body_len as usize);
        }
    }
    let topic_len = message_version.get_topic_length(bytes);
    let topic_bytes = bytes.copy_to_bytes(topic_len);
    let topic = CheetahString::from_string(String::from_utf8_lossy(topic_bytes.as_ref()).to_string());
    let properties_length = bytes.get_i16();
    let (tags_code, keys, uniq_key, properties_map) = if properties_length > 0 {
        let properties = bytes.copy_to_bytes(properties_length as usize);
        let properties_content = String::from_utf8_lossy(properties.as_ref()).to_string();
        //need to optimize
        let properties_map = string_to_message_properties(Some(&CheetahString::from_string(properties_content)));
        let keys = properties_map.get(MessageConst::PROPERTY_KEYS).cloned();
        let uniq_key = properties_map
            .get(MessageConst::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX)
            .cloned();
        if check_dup_info {
            let dup_info = properties_map.get(MessageConst::DUP_INFO).cloned();
            if let Some(content) = dup_info {
                let vec = content.split('_').collect::<Vec<&str>>();
                if vec.len() != 2 {
                    warn!("DupInfo in properties check failed. dupInfo={}", content);
                    return DispatchRequest {
                        msg_size: -1,
                        success: false,
                        ..Default::default()
                    };
                }
            } else {
                warn!("DupInfo in properties check failed. dupInfo=null");
                return DispatchRequest {
                    msg_size: -1,
                    success: false,
                    ..Default::default()
                };
            }
        }
        let tags = properties_map.get(MessageConst::PROPERTY_TAGS);
        let mut tags_code = tags_string2tags_code(tags);

        {
            // Timing message processing
            let delay_time_level =
                properties_map.get(&CheetahString::from_static_str(MessageConst::PROPERTY_DELAY_TIME_LEVEL));
            if let (Some(delay_time_level_str), true) =
                (delay_time_level, TopicValidator::RMQ_SYS_SCHEDULE_TOPIC == topic)
            {
                if let Ok(mut delay_level) = delay_time_level_str.parse::<i32>() {
                    if delay_level > max_delay_level {
                        delay_level = max_delay_level;
                    }
                    if delay_level > 0 {
                        if let Some(delay_time) = delay_level_table.get(&delay_level) {
                            tags_code = *delay_time + store_timestamp;
                        } else {
                            tags_code = store_timestamp + 1000;
                        }
                    }
                }
            }
        }
        (tags_code, keys.unwrap_or_default(), uniq_key, properties_map)
    } else {
        (0, CheetahString::new(), None, HashMap::new())
    };

    if check_crc && message_store_config.force_verify_prop_crc {
        let mut expected_crc = -1i32;
        if !properties_map.is_empty() {
            let crc_32 = properties_map.get(&CheetahString::from_static_str(MessageConst::PROPERTY_CRC32));
            if let Some(crc_32) = crc_32 {
                expected_crc = 0;
                for ch in crc_32.chars().rev() {
                    let num = (ch as u8 - b'0') as i32;
                    expected_crc *= 10;
                    expected_crc += num;
                }
            }
        }
        if expected_crc > 0 {
            unimplemented!("check_crc not implemented")
        }
    }

    let read_length = MessageExtEncoder::cal_msg_length(
        message_version,
        sys_flag,
        body_len,
        topic_len as i32,
        properties_length as i32,
    );

    if total_size != read_length {
        error!(
            "[BUG]read total count not equals msg total size. totalSize={}, readTotalCount={}, bodyLen={}, \
             topicLen={}, propertiesLength={}",
            total_size, read_length, body_len, topic_len, properties_length
        );
        return DispatchRequest {
            msg_size: total_size,
            success: false,
            ..Default::default()
        };
    }
    let mut dispatch_request = DispatchRequest {
        success: true,
        topic,
        queue_id,
        commit_log_offset: physic_offset,
        msg_size: total_size,
        tags_code,
        store_timestamp,
        consume_queue_offset: queue_offset,
        keys,
        uniq_key,
        sys_flag,
        prepared_transaction_offset,
        ..DispatchRequest::default()
    };
    set_batch_size_if_needed(&properties_map, &mut dispatch_request);
    dispatch_request.properties_map = Some(properties_map);
    dispatch_request
}

fn set_batch_size_if_needed(
    properties_map: &HashMap<CheetahString, CheetahString>,
    dispatch_request: &mut DispatchRequest,
) {
    if !properties_map.is_empty()
        && properties_map.contains_key(MessageConst::PROPERTY_INNER_NUM)
        && properties_map.contains_key(MessageConst::PROPERTY_INNER_BASE)
    {
        dispatch_request.msg_base_offset = properties_map
            .get(MessageConst::PROPERTY_INNER_BASE)
            .unwrap()
            .parse::<i64>()
            .unwrap();
        dispatch_request.batch_size = properties_map
            .get(MessageConst::PROPERTY_INNER_NUM)
            .unwrap()
            .parse::<i16>()
            .unwrap();
    }
}

fn is_mapped_file_matched_recover(
    message_store_config: &Arc<MessageStoreConfig>,
    mapped_file: &DefaultMappedFile,
    store_checkpoint: &StoreCheckpoint,
) -> bool {
    let magic_code = mapped_file
        .get_bytes(MESSAGE_MAGIC_CODE_POSITION, mem::size_of::<i32>())
        .unwrap_or(Bytes::from([0u8; mem::size_of::<i32>()].as_ref()))
        .get_i32();

    //check magic code
    if magic_code != MESSAGE_MAGIC_CODE && magic_code != MESSAGE_MAGIC_CODE_V2 {
        return false;
    }
    if message_store_config.is_enable_rocksdb_store() {
        unimplemented!()
    } else {
        let sys_flag = mapped_file
            .get_bytes(SYSFLAG_POSITION, mem::size_of::<i32>())
            .unwrap_or(Bytes::from([0u8; mem::size_of::<i32>()].as_ref()))
            .get_i32();
        let born_host_length = if sys_flag & MessageSysFlag::BORNHOST_V6_FLAG == 0 {
            8
        } else {
            20
        };
        let msg_store_time_pos = 4 + 4 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + born_host_length;
        let store_timestamp = mapped_file
            .get_bytes(msg_store_time_pos, mem::size_of::<i64>())
            .unwrap_or(Bytes::from([0u8; mem::size_of::<i64>()].as_ref()))
            .get_i64();
        if store_timestamp == 0 {
            return false;
        }
        if message_store_config.message_index_enable && message_store_config.message_index_safe {
            if store_timestamp <= store_checkpoint.get_min_timestamp_index() as i64 {
                info!(
                    "find check timestamp, {} {}",
                    store_timestamp,
                    time_millis_to_human_string(store_timestamp)
                );
                return true;
            }
        } else if store_timestamp <= store_checkpoint.get_min_timestamp() as i64 {
            info!(
                "find check timestamp, {} {}",
                store_timestamp,
                time_millis_to_human_string(store_timestamp)
            );
            return true;
        }
    }
    false
}

impl Swappable for CommitLog {
    fn swap_map(&self, _reserve_num: i32, _force_swap_interval_ms: i64, _normal_swap_interval_ms: i64) {
        todo!()
    }

    fn clean_swapped_map(&self, _force_clean_swap_interval_ms: i64) {
        todo!()
    }
}

#[cfg(test)]
mod tests {
    use std::path::Path;
    use std::sync::Arc;

    use super::*;
    use crate::base::message_store::MessageStore;
    use crate::config::message_store_config::MessageStoreConfig;
    use crate::message_store::local_file_message_store::LocalFileMessageStore;
    use cheetah_string::CheetahString;
    use dashmap::DashMap;
    use rocketmq_common::common::broker::broker_config::BrokerConfig;
    use rocketmq_common::common::config::TopicConfig;
    use rocketmq_common::TimeUtils::current_millis;

    fn new_test_message_store(
        root: &Path,
        broker_role: BrokerRole,
        all_ack_in_sync_state_set: bool,
    ) -> ArcMut<LocalFileMessageStore> {
        std::fs::create_dir_all(root).expect("create temp store dir");
        let broker_config = Arc::new(BrokerConfig {
            enable_controller_mode: true,
            ..BrokerConfig::default()
        });
        let message_store_config = Arc::new(MessageStoreConfig {
            enable_controller_mode: true,
            broker_role,
            all_ack_in_sync_state_set,
            store_path_root_dir: root.to_string_lossy().into_owned().into(),
            ..MessageStoreConfig::default()
        });
        let topic_table: Arc<DashMap<CheetahString, ArcMut<TopicConfig>>> = Arc::new(DashMap::new());
        let mut store = ArcMut::new(LocalFileMessageStore::new(
            message_store_config,
            broker_config,
            topic_table,
            None,
            false,
        ));
        let store_clone = store.clone();
        store.set_message_store_arc(store_clone);
        store
    }

    #[tokio::test]
    async fn controller_mode_confirm_offset_prefers_max_offset_when_all_ack_is_disabled() {
        let temp_root =
            std::env::temp_dir().join(format!("rocketmq-rust-commitlog-confirm-offset-{}", current_millis()));
        let mut store = new_test_message_store(&temp_root, BrokerRole::SyncMaster, false);
        store.init().await.expect("init message store");

        store
            .get_commit_log_mut()
            .append_data(0, &[1, 2, 3, 4], 0, 4)
            .await
            .expect("append data");
        store.set_confirm_offset(1);

        assert_eq!(store.get_confirm_offset(), 4);

        let _ = std::fs::remove_dir_all(temp_root);
    }

    #[tokio::test]
    async fn controller_mode_confirm_offset_keeps_uninitialized_value_when_sync_state_set_exceeds_connections() {
        let temp_root = std::env::temp_dir().join(format!(
            "rocketmq-rust-commitlog-confirm-offset-missing-connection-{}",
            current_millis()
        ));
        let mut store = new_test_message_store(&temp_root, BrokerRole::SyncMaster, true);
        store.init().await.expect("init message store");
        store.set_alive_replica_num_in_group(3);

        store
            .get_commit_log_mut()
            .append_data(0, &[1, 2, 3, 4], 0, 4)
            .await
            .expect("append data");
        store.get_commit_log_mut().confirm_offset = -1;

        assert_eq!(store.get_confirm_offset(), -1);

        let _ = std::fs::remove_dir_all(temp_root);
    }

    #[tokio::test]
    async fn controller_mode_recovery_clamp_persists_confirm_offset() {
        let temp_root =
            std::env::temp_dir().join(format!("rocketmq-rust-commitlog-recovery-clamp-{}", current_millis()));
        let mut store = new_test_message_store(&temp_root, BrokerRole::SyncMaster, true);
        store.init().await.expect("init message store");
        store.set_alive_replica_num_in_group(2);

        store
            .get_commit_log_mut()
            .append_data(0, &[1, 2, 3, 4, 5, 6, 7, 8], 0, 8)
            .await
            .expect("append data");

        store.set_confirm_offset(8);

        store.get_commit_log_mut().clamp_controller_recover_confirm_offset(0, 6);

        assert_eq!(store.get_commit_log().confirm_offset, 6);
        assert_eq!(store.get_confirm_offset(), 6);

        let _ = std::fs::remove_dir_all(temp_root);
    }
}