@@ -280,6 +280,12 @@ FileBlocks BlockFileCache::get_impl(const UInt128Wrapper& hash, const CacheConte
280
280
DCHECK (!file_blocks.empty ());
281
281
// change to ttl if the blocks aren't ttl
282
282
if (context.cache_type == FileCacheType::TTL && _key_to_time.find (hash) == _key_to_time.end ()) {
283
+ for (auto & [_, cell] : file_blocks) {
284
+ Status st = cell.file_block ->update_expiration_time (context.expiration_time );
285
+ if (!st.ok ()) {
286
+ LOG_WARNING (" Failed to change key meta" ).error (st);
287
+ }
288
+ }
283
289
for (auto & [_, cell] : file_blocks) {
284
290
FileCacheType origin_type = cell.file_block ->cache_type ();
285
291
if (origin_type == FileCacheType::TTL) continue ;
@@ -295,9 +301,7 @@ FileBlocks BlockFileCache::get_impl(const UInt128Wrapper& hash, const CacheConte
295
301
} else {
296
302
cell.queue_iterator .reset ();
297
303
}
298
- st = cell.file_block ->update_expiration_time (context.expiration_time );
299
- }
300
- if (!st.ok ()) {
304
+ } else {
301
305
LOG_WARNING (" Failed to change key meta" ).error (st);
302
306
}
303
307
}
@@ -324,7 +328,10 @@ FileBlocks BlockFileCache::get_impl(const UInt128Wrapper& hash, const CacheConte
324
328
}
325
329
if (context.expiration_time == 0 ) {
326
330
for (auto & [_, cell] : file_blocks) {
327
- if (cell.file_block ->change_cache_type_by_mgr (FileCacheType::NORMAL)) {
331
+ auto cache_type = cell.file_block ->cache_type ();
332
+ if (cache_type != FileCacheType::TTL) continue ;
333
+ auto st = cell.file_block ->change_cache_type_by_mgr (FileCacheType::NORMAL);
334
+ if (st.ok ()) {
328
335
if (config::enable_ttl_cache_evict_using_lru) {
329
336
auto & ttl_queue = get_queue (FileCacheType::TTL);
330
337
ttl_queue.remove (cell.queue_iterator .value (), cache_lock);
@@ -333,6 +340,8 @@ FileBlocks BlockFileCache::get_impl(const UInt128Wrapper& hash, const CacheConte
333
340
cell.queue_iterator =
334
341
queue.add (cell.file_block ->get_hash_value (), cell.file_block ->offset (),
335
342
cell.file_block ->range ().size (), cache_lock);
343
+ } else {
344
+ LOG_WARNING (" Failed to change key meta" ).error (st);
336
345
}
337
346
}
338
347
_key_to_time.erase (iter);
@@ -681,22 +690,30 @@ BlockFileCache::FileBlockCell* BlockFileCache::add_cell(const UInt128Wrapper& ha
681
690
<< " .\n Current cache structure: " << dump_structure_unlocked (hash, cache_lock);
682
691
683
692
auto & offsets = _files[hash];
684
- DCHECK ((context.expiration_time == 0 && context.cache_type != FileCacheType::TTL) ||
685
- (context.cache_type == FileCacheType::TTL && context.expiration_time != 0 ))
686
- << fmt::format (" expiration time {}, cache type {}" , context.expiration_time ,
687
- context.cache_type );
688
693
689
694
FileCacheKey key;
690
695
key.hash = hash;
691
696
key.offset = offset;
692
697
key.meta .type = context.cache_type ;
693
698
key.meta .expiration_time = context.expiration_time ;
694
699
FileBlockCell cell (std::make_shared<FileBlock>(key, size, this , state), cache_lock);
695
- if (context.cache_type != FileCacheType::TTL || config::enable_ttl_cache_evict_using_lru) {
696
- auto & queue = get_queue (context.cache_type );
700
+ Status st;
701
+ if (context.expiration_time == 0 && context.cache_type == FileCacheType::TTL) {
702
+ st = cell.file_block ->change_cache_type_by_mgr (FileCacheType::NORMAL);
703
+ } else if (context.cache_type != FileCacheType::TTL && context.expiration_time != 0 ) {
704
+ st = cell.file_block ->change_cache_type_by_mgr (FileCacheType::TTL);
705
+ }
706
+ if (!st.ok ()) {
707
+ LOG (WARNING) << " Cannot change cache type. expiration_time=" << context.expiration_time
708
+ << " cache_type=" << cache_type_to_string (context.cache_type )
709
+ << " error=" << st.msg ();
710
+ }
711
+ if (cell.file_block ->cache_type () != FileCacheType::TTL ||
712
+ config::enable_ttl_cache_evict_using_lru) {
713
+ auto & queue = get_queue (cell.file_block ->cache_type ());
697
714
cell.queue_iterator = queue.add (hash, offset, size, cache_lock);
698
715
}
699
- if (context. cache_type == FileCacheType::TTL) {
716
+ if (cell. file_block -> cache_type () == FileCacheType::TTL) {
700
717
if (_key_to_time.find (hash) == _key_to_time.end ()) {
701
718
_key_to_time[hash] = context.expiration_time ;
702
719
_time_to_key.insert (std::make_pair (context.expiration_time , hash));
@@ -1005,19 +1022,18 @@ bool BlockFileCache::remove_if_ttl_file_unlock(const UInt128Wrapper& file_key, b
1005
1022
}
1006
1023
}
1007
1024
for (auto & [_, cell] : _files[file_key]) {
1008
- if (cell.file_block ->cache_type () == FileCacheType::TTL) {
1009
- auto st = cell.file_block ->change_cache_type_by_mgr (FileCacheType::NORMAL);
1010
- if (st.ok ()) {
1011
- if (config::enable_ttl_cache_evict_using_lru) {
1012
- ttl_queue.remove (cell.queue_iterator .value (), cache_lock);
1013
- }
1014
- auto & queue = get_queue (FileCacheType::NORMAL);
1015
- cell.queue_iterator = queue.add (
1016
- cell.file_block ->get_hash_value (), cell.file_block ->offset (),
1017
- cell.file_block ->range ().size (), cache_lock);
1018
- } else {
1019
- LOG_WARNING (" Failed to change cache type to normal" ).error (st);
1025
+ if (cell.file_block ->cache_type () == FileCacheType::NORMAL) continue ;
1026
+ auto st = cell.file_block ->change_cache_type_by_mgr (FileCacheType::NORMAL);
1027
+ if (st.ok ()) {
1028
+ if (config::enable_ttl_cache_evict_using_lru) {
1029
+ ttl_queue.remove (cell.queue_iterator .value (), cache_lock);
1020
1030
}
1031
+ auto & queue = get_queue (FileCacheType::NORMAL);
1032
+ cell.queue_iterator =
1033
+ queue.add (cell.file_block ->get_hash_value (), cell.file_block ->offset (),
1034
+ cell.file_block ->range ().size (), cache_lock);
1035
+ } else {
1036
+ LOG_WARNING (" Failed to change cache type to normal" ).error (st);
1021
1037
}
1022
1038
}
1023
1039
} else {
@@ -1579,6 +1595,7 @@ void BlockFileCache::modify_expiration_time(const UInt128Wrapper& hash,
1579
1595
for (auto & [_, cell] : _files[hash]) {
1580
1596
Status st = cell.file_block ->update_expiration_time (new_expiration_time);
1581
1597
if (!st.ok ()) {
1598
+ LOG_WARNING (" Failed to modify expiration time" ).error (st);
1582
1599
}
1583
1600
}
1584
1601
@@ -1588,12 +1605,13 @@ void BlockFileCache::modify_expiration_time(const UInt128Wrapper& hash,
1588
1605
if (auto iter = _files.find (hash); iter != _files.end ()) {
1589
1606
for (auto & [_, cell] : iter->second ) {
1590
1607
Status st = cell.file_block ->update_expiration_time (new_expiration_time);
1591
- if (!st.ok () && !st. is <ErrorCode::NOT_FOUND>() ) {
1608
+ if (!st.ok ()) {
1592
1609
LOG_WARNING (" " ).error (st);
1593
1610
}
1594
1611
}
1595
1612
for (auto & [_, cell] : iter->second ) {
1596
1613
FileCacheType origin_type = cell.file_block ->cache_type ();
1614
+ if (origin_type == FileCacheType::TTL) continue ;
1597
1615
auto st = cell.file_block ->change_cache_type_by_mgr (FileCacheType::TTL);
1598
1616
if (st.ok ()) {
1599
1617
auto & queue = get_queue (origin_type);
0 commit comments