2929#include " io/cache/block_file_cache.h"
3030#include " io/cache/cache_block_meta_store.h"
3131#include " io/cache/file_block.h"
32+ #include " olap/base_tablet.h"
33+ #include " runtime/exec_env.h"
3234#include " util/time.h"
3335
3436namespace doris ::io {
@@ -71,7 +73,6 @@ FileBlocks BlockFileCacheTtlMgr::get_file_blocks_from_tablet_id(int64_t tablet_i
7173
7274 while (iterator->valid ()) {
7375 BlockMetaKey key = iterator->key ();
74- BlockMeta meta = iterator->value ();
7576
7677 // Get all blocks for this hash using get_blocks_by_key
7778 try {
@@ -108,32 +109,45 @@ void BlockFileCacheTtlMgr::run_backgroud_update_ttl_info_map() {
108109 }
109110
110111 for (int64_t tablet_id : tablet_ids_to_process) {
111- // TODO(zhengyu): Implement actual CloudMetaMgr or CloudTabletMgr integration
112- // For now, we'll use placeholder values
113- uint64_t create_time = 0 ;
112+ uint64_t tablet_ctime = 0 ;
114113 uint64_t ttl = 0 ;
115114
116- // Simulate getting tablet metadata
117- // This should be replaced with actual CloudMetaMgr::get_tablet_meta call
118- bool has_ttl = (tablet_id % 10 == 0 ); // Example condition
119- if (has_ttl) {
120- create_time = UnixSeconds (); // Current time as create time
121- ttl = 3600 ; // 1 hour TTL
115+ auto res = ExecEnv::get_tablet (tablet_id, nullptr , false , false );
116+ if (!res.has_value ()) {
117+ LOG (WARNING) << " Failed to get tablet for tablet_id: " << tablet_id
118+ << " , err: " << res.error ();
119+ continue ;
120+ }
121+
122+ auto tablet = res.value ();
123+ const auto & tablet_meta = tablet->tablet_meta ();
124+ if (tablet_meta != nullptr ) {
125+ tablet_ctime = tablet_meta->creation_time ();
126+ }
127+
128+ int64_t ttl_seconds = tablet->ttl_seconds ();
129+ if (ttl_seconds > 0 && tablet_ctime > 0 ) {
130+ ttl = static_cast <uint64_t >(ttl_seconds);
122131 }
123132
124133 // Update TTL info map
125134 {
126135 std::lock_guard<std::mutex> lock (_ttl_info_mutex);
127136 if (ttl > 0 ) {
128- _ttl_info_map[tablet_id] = TtlInfo {ttl, create_time};
137+ auto old_info_it = _ttl_info_map.find (tablet_id);
138+ bool was_zero_ttl = (old_info_it == _ttl_info_map.end () ||
139+ old_info_it->second .ttl == 0 );
140+ _ttl_info_map[tablet_id] = TtlInfo {ttl, tablet_ctime};
129141
130142 // If TTL changed from 0 to non-zero, convert blocks to TTL type
131- auto old_info_it = _ttl_info_map.find (tablet_id);
132- if (old_info_it == _ttl_info_map.end () || old_info_it->second .ttl == 0 ) {
143+ if (was_zero_ttl) {
133144 FileBlocks blocks = get_file_blocks_from_tablet_id (tablet_id);
134145 for (auto & block : blocks) {
135146 if (block->cache_type () != FileCacheType::TTL) {
136- block->change_cache_type (FileCacheType::TTL);
147+ auto st = block->change_cache_type (FileCacheType::TTL);
148+ if (!st.ok ()) {
149+ LOG (WARNING) << " Failed to convert block to TTL cache_type" ;
150+ }
137151 }
138152 }
139153 }
@@ -144,9 +158,8 @@ void BlockFileCacheTtlMgr::run_backgroud_update_ttl_info_map() {
144158 }
145159 }
146160
147- // Sleep for configured interval (use existing TTL GC interval)
148- std::this_thread::sleep_for (
149- std::chrono::milliseconds (config::file_cache_background_ttl_gc_interval_ms));
161+ std::this_thread::sleep_for (std::chrono::milliseconds (
162+ config::file_cache_background_ttl_info_update_interval_ms));
150163
151164 } catch (const std::exception& e) {
152165 LOG (WARNING) << " Exception in TTL update thread: " << e.what ();
@@ -160,7 +173,7 @@ void BlockFileCacheTtlMgr::run_backgroud_expiration_check() {
160173
161174 while (!_stop_background.load (std::memory_order_acquire)) {
162175 try {
163- std::unordered_map <int64_t , TtlInfo> ttl_info_copy;
176+ std::map <int64_t , TtlInfo> ttl_info_copy;
164177
165178 // Copy TTL info for processing
166179 {
@@ -171,12 +184,15 @@ void BlockFileCacheTtlMgr::run_backgroud_expiration_check() {
171184 uint64_t current_time = UnixSeconds ();
172185
173186 for (const auto & [tablet_id, ttl_info] : ttl_info_copy) {
174- if (ttl_info.create_time + ttl_info.ttl < current_time) {
187+ if (ttl_info.tablet_ctime + ttl_info.ttl < current_time) {
175188 // Tablet has expired, convert TTL blocks back to NORMAL type
176189 FileBlocks blocks = get_file_blocks_from_tablet_id (tablet_id);
177190 for (auto & block : blocks) {
178191 if (block->cache_type () == FileCacheType::TTL) {
179- block->change_cache_type (FileCacheType::NORMAL);
192+ auto st = block->change_cache_type (FileCacheType::NORMAL);
193+ if (!st.ok ()) {
194+ LOG (WARNING) << " Failed to convert block back to NORMAL cache_type" ;
195+ }
180196 }
181197 }
182198
0 commit comments