[BugFix] Fix the bug where datacache_evict_probability does not take effect in DataCacheMem (#63770)

Signed-off-by: trueeyu <lxhhust350@qq.com>
This commit is contained in:
trueeyu 2025-10-03 10:04:14 +08:00 committed by GitHub
parent 35e984856e
commit 6dd2c4e555
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 43 additions and 22 deletions

View File

@ -57,11 +57,6 @@ struct DiskCacheWriteOptions {
bool allow_zero_copy = false;
std::function<void(int, const std::string&)> callback = nullptr;
// The probability to evict other items if the cache space is full, which can help avoid frequent cache replacement
// and improve cache hit rate sometimes.
// It is expressed as a percentage. If evict_probability is 10, it means the probability to evict other data is 10%.
int32_t evict_probability = 100;
// The base frequency for target cache.
// When using multiple segment lru, a higher frequency may cause the cache is written to warm segment directly.
// For the default cache options, that `lru_segment_freq_bits` is 0:

View File

@ -85,7 +85,7 @@ Status StarCacheEngine::write(const std::string& key, const IOBuffer& buffer, Di
} else {
opts.mode = starcache::WriteOptions::WriteMode::WRITE_THROUGH;
}
opts.evict_probability = options->evict_probability;
opts.evict_probability = 100;
opts.ignore_inline = true;
Status st;
{

View File

@ -28,6 +28,11 @@ struct MemCacheOptions {
struct MemCacheWriteOptions {
// The priority of the cache object, only support 0 and 1 now.
int8_t priority = 0;
// The probability to evict other items if the cache space is full, which can help avoid frequent cache replacement
// and improve cache hit rate sometimes.
// It is expressed as a percentage. If evict_probability is 10, it means the probability to evict other data is 10%.
int32_t evict_probability = 100;
};
struct MemCacheReadOptions {};

View File

@ -25,6 +25,9 @@ Status LRUCacheEngine::init(const MemCacheOptions& options) {
Status LRUCacheEngine::insert(const std::string& key, void* value, size_t size, MemCacheDeleter deleter,
MemCacheHandlePtr* handle, const MemCacheWriteOptions& options) {
if (!_check_write(size, options)) {
return Status::InternalError("cache insertion is rejected");
}
auto* lru_handle = _cache->insert(key, value, size, deleter, static_cast<CachePriority>(options.priority));
if (handle) {
*handle = reinterpret_cast<MemCacheHandlePtr>(lru_handle);
@ -113,4 +116,24 @@ size_t LRUCacheEngine::hit_count() const {
return _cache->get_hit_count();
}
bool LRUCacheEngine::_check_write(size_t charge, const MemCacheWriteOptions& options) const {
if (options.evict_probability >= 100) {
return true;
}
if (options.evict_probability <= 0) {
return false;
}
/*
// TODO: The cost of this call may be relatively high, and it needs to be optimized later.
if (_cache->get_memory_usage() + charge <= _cache->get_capacity()) {
return true;
}
*/
if (butil::fast_rand_less_than(100) < options.evict_probability) {
return true;
}
return false;
}
} // namespace starrocks

View File

@ -60,7 +60,9 @@ public:
Status prune() override;
private:
bool _check_write(size_t charge, const MemCacheWriteOptions& options) const;
std::atomic<bool> _initialized = false;
std::unique_ptr<ShardedLRUCache> _cache;
};
} // namespace starrocks
} // namespace starrocks

View File

@ -471,6 +471,7 @@ StatusOr<FileMetaDataPtr> FileMetaDataParser::get_file_metadata() {
if (file_metadata_size > 0) {
auto deleter = [](const starrocks::CacheKey& key, void* value) { delete (FileMetaDataPtr*)value; };
MemCacheWriteOptions options;
options.evict_probability = _datacache_options->datacache_evict_probability;
auto capture = std::make_unique<FileMetaDataPtr>(file_metadata);
Status st = _cache->insert(metacache_key, (void*)(capture.get()), file_metadata_size, deleter, options,
&cache_handle);

View File

@ -92,7 +92,7 @@ Status PageReader::_deal_page_with_cache() {
return Status::OK();
}
RETURN_IF_ERROR(_read_and_decompress_internal(true));
MemCacheWriteOptions opts;
MemCacheWriteOptions opts{.evict_probability = _opts.datacache_options->datacache_evict_probability};
auto st = _cache->insert(page_cache_key, _cache_buf, opts, &cache_handle);
if (st.ok()) {
_page_handle = PageHandle(std::move(cache_handle));

View File

@ -153,14 +153,13 @@ Status CacheInputStream::_read_from_cache(const int64_t offset, const int64_t si
read_size = block_size;
if (res.ok() && _enable_populate_cache) {
DiskCacheWriteOptions options;
options.async = _enable_async_populate_mode;
options.evict_probability = _datacache_evict_probability;
options.priority = _priority;
options.ttl_seconds = _ttl_seconds;
options.frequency = _frequency;
options.allow_zero_copy = true;
_write_cache(block_offset, block.buffer, &options);
DiskCacheWriteOptions write_options;
write_options.async = _enable_async_populate_mode;
write_options.priority = _priority;
write_options.ttl_seconds = _ttl_seconds;
write_options.frequency = _frequency;
write_options.allow_zero_copy = true;
_write_cache(block_offset, block.buffer, &write_options);
}
}
@ -447,7 +446,6 @@ void CacheInputStream::_populate_to_cache(const char* p, int64_t offset, int64_t
auto f = [sb, this](const char* buf, size_t off, size_t size) {
DiskCacheWriteOptions options;
options.async = _enable_async_populate_mode;
options.evict_probability = _datacache_evict_probability;
options.priority = _priority;
options.ttl_seconds = _ttl_seconds;
options.frequency = _frequency;

View File

@ -76,8 +76,6 @@ public:
void set_enable_cache_io_adaptor(bool v) { _enable_cache_io_adaptor = v; }
void set_datacache_evict_probability(int32_t v) { _datacache_evict_probability = v; }
void set_priority(const int8_t priority) { _priority = priority; }
void set_frequency(const int8_t frequency) { _frequency = frequency; }
@ -128,7 +126,6 @@ protected:
bool _enable_async_populate_mode = false;
bool _enable_block_buffer = false;
bool _enable_cache_io_adaptor = false;
int32_t _datacache_evict_probability = 100;
std::string _peer_host;
int32_t _peer_port = 0;

View File

@ -84,8 +84,8 @@ void TableMetricsManager::cleanup(bool force) {
if (!config::enable_table_metrics) {
return;
}
int64_t current_second = MonotonicSeconds();
#ifndef BE_TEST
int64_t current_second = MonotonicSeconds();
if (!force && current_second - _last_cleanup_ts <= kCleanupIntervalSeconds) {
return;
}

View File

@ -385,7 +385,7 @@ DataCacheOptions FileReaderTest::_mock_datacache_options() {
.enable_datacache_async_populate_mode = true,
.enable_datacache_io_adaptor = true,
.modification_time = 100000,
.datacache_evict_probability = 0,
.datacache_evict_probability = 100,
.datacache_priority = 0,
.datacache_ttl_seconds = 0};
}