Skip to content

Commit 1db6407

Browse files
committed
response to the reviewer
Signed-off-by: zhengyu <[email protected]>
1 parent 51e7bdf commit 1db6407

14 files changed

+111
-53
lines changed

be/src/cloud/cloud_storage_engine.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,9 @@ bool CloudStorageEngine::stopped() {
280280

281281
Result<BaseTabletSPtr> CloudStorageEngine::get_tablet(int64_t tablet_id,
282282
SyncRowsetStats* sync_stats,
283-
bool force_use_cache) {
284-
return _tablet_mgr->get_tablet(tablet_id, false, true, sync_stats, force_use_cache)
283+
bool force_use_cache, bool cache_on_miss) {
284+
return _tablet_mgr
285+
->get_tablet(tablet_id, false, true, sync_stats, force_use_cache, cache_on_miss)
285286
.transform([](auto&& t) { return static_pointer_cast<BaseTablet>(std::move(t)); });
286287
}
287288

be/src/cloud/cloud_storage_engine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class CloudStorageEngine final : public BaseStorageEngine {
6363
bool stopped() override;
6464

6565
Result<BaseTabletSPtr> get_tablet(int64_t tablet_id, SyncRowsetStats* sync_stats = nullptr,
66-
bool force_use_cache = false) override;
66+
bool force_use_cache = false,
67+
bool cache_on_miss = true) override;
6768

6869
Status start_bg_threads(std::shared_ptr<WorkloadGroup> wg_sptr = nullptr) override;
6970

be/src/cloud/cloud_tablet_mgr.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ void set_tablet_access_time_ms(CloudTablet* tablet) {
160160
Result<std::shared_ptr<CloudTablet>> CloudTabletMgr::get_tablet(int64_t tablet_id, bool warmup_data,
161161
bool sync_delete_bitmap,
162162
SyncRowsetStats* sync_stats,
163-
bool local_only) {
163+
bool local_only,
164+
bool cache_on_miss) {
164165
// LRU value type. `Value`'s lifetime MUST NOT be longer than `CloudTabletMgr`
165166
class Value : public LRUCacheValueBase {
166167
public:
@@ -192,7 +193,7 @@ Result<std::shared_ptr<CloudTablet>> CloudTabletMgr::get_tablet(int64_t tablet_i
192193
if (sync_stats) {
193194
++sync_stats->tablet_meta_cache_miss;
194195
}
195-
auto load_tablet = [this, &key, warmup_data, sync_delete_bitmap,
196+
auto load_tablet = [this, warmup_data, sync_delete_bitmap,
196197
sync_stats](int64_t tablet_id) -> Result<std::shared_ptr<CloudTablet>> {
197198
TabletMetaSharedPtr tablet_meta;
198199
auto start = std::chrono::steady_clock::now();
@@ -208,7 +209,6 @@ Result<std::shared_ptr<CloudTablet>> CloudTabletMgr::get_tablet(int64_t tablet_i
208209
}
209210

210211
auto tablet = std::make_shared<CloudTablet>(_engine, std::move(tablet_meta));
211-
auto value = std::make_unique<Value>(tablet, *_tablet_map);
212212
// MUST sync stats to let compaction scheduler work correctly
213213
SyncOptions options;
214214
options.warmup_delta_data = warmup_data;
@@ -218,16 +218,7 @@ Result<std::shared_ptr<CloudTablet>> CloudTabletMgr::get_tablet(int64_t tablet_i
218218
LOG(WARNING) << "failed to sync tablet " << tablet_id << ": " << st;
219219
return ResultError(st);
220220
}
221-
222-
auto* handle = _cache->insert(key, value.release(), 1, sizeof(CloudTablet),
223-
CachePriority::NORMAL);
224-
auto ret =
225-
std::shared_ptr<CloudTablet>(tablet.get(), [this, handle](CloudTablet* tablet) {
226-
set_tablet_access_time_ms(tablet);
227-
_cache->release(handle);
228-
});
229-
_tablet_map->put(std::move(tablet));
230-
return ret;
221+
return tablet;
231222
};
232223

233224
auto load_result = s_singleflight_load_tablet.load(tablet_id, std::move(load_tablet));
@@ -236,8 +227,22 @@ Result<std::shared_ptr<CloudTablet>> CloudTabletMgr::get_tablet(int64_t tablet_i
236227
load_result.error()));
237228
}
238229
auto tablet = load_result.value();
239-
set_tablet_access_time_ms(tablet.get());
240-
return tablet;
230+
if (!cache_on_miss) {
231+
set_tablet_access_time_ms(tablet.get());
232+
return tablet;
233+
}
234+
235+
auto value = std::make_unique<Value>(tablet, *_tablet_map);
236+
auto* insert_handle =
237+
_cache->insert(key, value.release(), 1, sizeof(CloudTablet), CachePriority::NORMAL);
238+
auto ret = std::shared_ptr<CloudTablet>(tablet.get(),
239+
[this, insert_handle](CloudTablet* tablet_ptr) {
240+
set_tablet_access_time_ms(tablet_ptr);
241+
_cache->release(insert_handle);
242+
});
243+
_tablet_map->put(std::move(tablet));
244+
set_tablet_access_time_ms(ret.get());
245+
return ret;
241246
}
242247
if (sync_stats) {
243248
++sync_stats->tablet_meta_cache_hit;

be/src/cloud/cloud_tablet_mgr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class CloudTabletMgr {
4747
Result<std::shared_ptr<CloudTablet>> get_tablet(int64_t tablet_id, bool warmup_data = false,
4848
bool sync_delete_bitmap = true,
4949
SyncRowsetStats* sync_stats = nullptr,
50-
bool local_only = false);
50+
bool local_only = false,
51+
bool cache_on_miss = true);
5152

5253
void erase_tablet(int64_t tablet_id);
5354

be/src/common/config.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,8 @@ DEFINE_mInt64(file_cache_background_block_lru_update_interval_ms, "5000");
11611161
DEFINE_mInt64(file_cache_background_block_lru_update_qps_limit, "1000");
11621162
DEFINE_mBool(enable_reader_dryrun_when_download_file_cache, "true");
11631163
DEFINE_mInt64(file_cache_background_monitor_interval_ms, "5000");
1164-
DEFINE_mInt64(file_cache_background_ttl_gc_interval_ms, "3000");
1164+
DEFINE_mInt64(file_cache_background_ttl_gc_interval_ms, "180000");
1165+
DEFINE_mInt64(file_cache_background_ttl_info_update_interval_ms, "180000");
11651166
DEFINE_mInt64(file_cache_background_ttl_gc_batch, "1000");
11661167
DEFINE_mInt64(file_cache_background_lru_dump_interval_ms, "60000");
11671168
// dump queue only if the queue update specific times through several dump intervals

be/src/common/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,7 @@ DECLARE_mInt64(file_cache_background_block_lru_update_qps_limit);
11831183
DECLARE_mBool(enable_reader_dryrun_when_download_file_cache);
11841184
DECLARE_mInt64(file_cache_background_monitor_interval_ms);
11851185
DECLARE_mInt64(file_cache_background_ttl_gc_interval_ms);
1186+
DECLARE_mInt64(file_cache_background_ttl_info_update_interval_ms);
11861187
DECLARE_mInt64(file_cache_background_ttl_gc_batch);
11871188
DECLARE_Int32(file_cache_downloader_thread_num_min);
11881189
DECLARE_Int32(file_cache_downloader_thread_num_max);

be/src/io/cache/block_file_cache.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ BlockFileCache::BlockFileCache(const std::string& cache_base_path,
284284

285285
_lru_recorder = std::make_unique<LRUQueueRecorder>(this);
286286
_lru_dumper = std::make_unique<CacheLRUDumper>(this, _lru_recorder.get());
287-
_ttl_mgr = std::make_unique<BlockFileCacheTtlMgr>(this, _meta_store.get());
288287
if (cache_settings.storage == "memory") {
289288
_storage = std::make_unique<MemFileCacheStorage>();
290289
_cache_base_path = "memory";
@@ -383,6 +382,13 @@ Status BlockFileCache::initialize_unlocked(std::lock_guard<std::mutex>& cache_lo
383382
restore_lru_queues_from_disk(cache_lock);
384383
}
385384
RETURN_IF_ERROR(_storage->init(this));
385+
if (!_ttl_mgr) {
386+
if (auto* fs_storage = dynamic_cast<FSFileCacheStorage*>(_storage.get())) {
387+
if (auto* meta_store = fs_storage->get_meta_store()) {
388+
_ttl_mgr = std::make_unique<BlockFileCacheTtlMgr>(this, meta_store);
389+
}
390+
}
391+
}
386392
_cache_background_monitor_thread = std::thread(&BlockFileCache::run_background_monitor, this);
387393
_cache_background_gc_thread = std::thread(&BlockFileCache::run_background_gc, this);
388394
_cache_background_evict_in_advance_thread =
@@ -640,7 +646,9 @@ FileBlocks BlockFileCache::split_range_into_cells(const UInt128Wrapper& hash,
640646
cell->update_atime();
641647
}
642648
}
643-
_ttl_mgr->register_tablet_id(context.tablet_id);
649+
if (_ttl_mgr && context.tablet_id != 0) {
650+
_ttl_mgr->register_tablet_id(context.tablet_id);
651+
}
644652
}
645653

646654
current_pos += current_size;
@@ -2117,6 +2125,21 @@ std::map<size_t, FileBlockSPtr> BlockFileCache::get_blocks_by_key(const UInt128W
21172125
return offset_to_block;
21182126
}
21192127

2128+
void BlockFileCache::modify_expiration_time(const UInt128Wrapper& hash, uint64_t expiration_time) {
2129+
SCOPED_CACHE_LOCK(_mutex, this);
2130+
if (auto iter = _files.find(hash); iter != _files.end()) {
2131+
for (auto& [_, cell] : iter->second) {
2132+
if (cell.file_block) {
2133+
auto st = cell.file_block->update_expiration_time(expiration_time);
2134+
if (!st.ok()) {
2135+
LOG(WARNING) << "Failed to update expiration time for block "
2136+
<< cell.file_block->get_info_for_log() << ", error=" << st;
2137+
}
2138+
}
2139+
}
2140+
}
2141+
}
2142+
21202143
void BlockFileCache::update_ttl_atime(const UInt128Wrapper& hash) {
21212144
SCOPED_CACHE_LOCK(_mutex, this);
21222145
if (auto iter = _files.find(hash); iter != _files.end()) {

be/src/io/cache/block_file_cache.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <optional>
2828
#include <thread>
2929

30+
#include "io/cache/block_file_cache_ttl_mgr.h"
3031
#include "io/cache/cache_lru_dumper.h"
3132
#include "io/cache/file_block.h"
3233
#include "io/cache/file_cache_common.h"
@@ -72,7 +73,6 @@ class LockScopedTimer {
7273
LockScopedTimer cache_lock_timer;
7374

7475
class FSFileCacheStorage;
75-
class BlockFileCacheTtlMgr;
7676

7777
// The BlockFileCache is responsible for the management of the blocks
7878
// The current strategies are lru and ttl.
@@ -204,6 +204,8 @@ class BlockFileCache {
204204
std::string reset_capacity(size_t new_capacity);
205205

206206
std::map<size_t, FileBlockSPtr> get_blocks_by_key(const UInt128Wrapper& hash);
207+
/// Adjust expiration time for every block sharing the specified hash key.
208+
void modify_expiration_time(const UInt128Wrapper& hash, uint64_t expiration_time);
207209
/// For debug and UT
208210
std::string dump_structure(const UInt128Wrapper& hash);
209211
std::string dump_single_cache_type(const UInt128Wrapper& hash, size_t offset);

be/src/io/cache/block_file_cache_ttl_mgr.cpp

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
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

3436
namespace 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

be/src/io/cache/block_file_cache_ttl_mgr.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <thread>
2727
#include <unordered_set>
2828

29+
#include "io/cache/file_block.h"
2930
#include "io/cache/file_cache_common.h"
3031

3132
namespace doris::io {
@@ -35,7 +36,7 @@ class CacheBlockMetaStore;
3536

3637
struct TtlInfo {
3738
uint64_t ttl;
38-
uint64_t create_time;
39+
uint64_t tablet_ctime;
3940
};
4041

4142
class BlockFileCacheTtlMgr {
@@ -45,16 +46,18 @@ class BlockFileCacheTtlMgr {
4546

4647
void register_tablet_id(int64_t tablet_id);
4748

48-
// Background thread functions
49+
// Background thread to update ttl_info_map
4950
void run_backgroud_update_ttl_info_map();
51+
// Background thread to find expired tablet and evict from ttl queue
5052
void run_backgroud_expiration_check();
5153

5254
private:
5355
FileBlocks get_file_blocks_from_tablet_id(int64_t tablet_id);
5456

5557
private:
56-
std::unordered_set<int64_t> _tablet_id_set;
57-
std::map<int64_t, TtlInfo> _ttl_info_map;
58+
// the set contains all the tablet ids which has cache data
59+
std::unordered_set<int64_t> _tablet_id_set; //TODO(zhengyu): clean up old tablet ids
60+
std::map<int64_t /* tablet_id */, TtlInfo> _ttl_info_map;
5861
BlockFileCache* _mgr;
5962
CacheBlockMetaStore* _meta_store;
6063

0 commit comments

Comments
 (0)