From 23712b9f90e7ba87fa9b09f5a23ea3a1d6f7e217 Mon Sep 17 00:00:00 2001 From: GavinMar Date: Thu, 19 Sep 2024 16:31:41 +0800 Subject: [PATCH] [BugFix] Check the kv cache usability in datacache functions in case that the uninitialized members are called in some cases. Signed-off-by: GavinMar --- be/src/block_cache/block_cache.cpp | 49 +++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/be/src/block_cache/block_cache.cpp b/be/src/block_cache/block_cache.cpp index 04a9285ec17785..e6bb8221e05b2b 100644 --- a/be/src/block_cache/block_cache.cpp +++ b/be/src/block_cache/block_cache.cpp @@ -66,6 +66,9 @@ Status BlockCache::init(const CacheOptions& options) { Status BlockCache::write_buffer(const CacheKey& cache_key, off_t offset, const IOBuffer& buffer, WriteCacheOptions* options) { + if (!_kv_cache) { + return Status::Uninitialized("block cache is uninitialized"); + } if (offset % _block_size != 0) { LOG(WARNING) << "write block key: " << cache_key << " with invalid args, offset: " << offset; return Status::InvalidArgument(strings::Substitute("offset must be aligned by block size $0", _block_size)); @@ -83,6 +86,9 @@ static void empty_deleter(void*) {} Status BlockCache::write_buffer(const CacheKey& cache_key, off_t offset, size_t size, const char* data, WriteCacheOptions* options) { + if (!_kv_cache) { + return Status::Uninitialized("block cache is uninitialized"); + } if (!data) { return Status::InvalidArgument("invalid data buffer"); } @@ -94,6 +100,9 @@ Status BlockCache::write_buffer(const CacheKey& cache_key, off_t offset, size_t Status BlockCache::write_object(const CacheKey& cache_key, const void* ptr, size_t size, DeleterFunc deleter, DataCacheHandle* handle, WriteCacheOptions* options) { + if (!_kv_cache) { + return Status::Uninitialized("block cache is uninitialized"); + } if (!ptr) { return Status::InvalidArgument("invalid object pointer"); } @@ -102,6 +111,9 @@ Status BlockCache::write_object(const CacheKey& cache_key, const void* ptr, size Status BlockCache::read_buffer(const CacheKey& cache_key, off_t offset, size_t size, IOBuffer* buffer, ReadCacheOptions* options) { + if (!_kv_cache) { + return Status::Uninitialized("block cache is uninitialized"); + } if (size == 0) { return Status::OK(); } @@ -113,6 +125,9 @@ Status BlockCache::read_buffer(const CacheKey& cache_key, off_t offset, size_t s StatusOr BlockCache::read_buffer(const CacheKey& cache_key, off_t offset, size_t size, char* data, ReadCacheOptions* options) { + if (!_kv_cache) { + return Status::Uninitialized("block cache is uninitialized"); + } IOBuffer buffer; RETURN_IF_ERROR(read_buffer(cache_key, offset, size, &buffer, options)); buffer.copy_to(data); @@ -120,10 +135,16 @@ StatusOr BlockCache::read_buffer(const CacheKey& cache_key, off_t offset } Status BlockCache::read_object(const CacheKey& cache_key, DataCacheHandle* handle, ReadCacheOptions* options) { + if (!_kv_cache) { + return Status::Uninitialized("block cache is uninitialized"); + } return _kv_cache->read_object(cache_key, handle, options); } bool BlockCache::exist(const starcache::CacheKey& cache_key, off_t offset, size_t size) const { + if (!_kv_cache) { + return false; + } if (size == 0) { return true; } @@ -133,6 +154,9 @@ bool BlockCache::exist(const starcache::CacheKey& cache_key, off_t offset, size_ } Status BlockCache::remove(const CacheKey& cache_key, off_t offset, size_t size) { + if (!_kv_cache) { + return Status::Uninitialized("block cache is uninitialized"); + } if (offset % _block_size != 0) { LOG(WARNING) << "remove block key: " << cache_key << " with invalid args, offset: " << offset << ", size: " << size; @@ -149,12 +173,18 @@ Status BlockCache::remove(const CacheKey& cache_key, off_t offset, size_t size) } Status BlockCache::update_mem_quota(size_t quota_bytes, bool flush_to_disk) { + if (!_kv_cache) { + return Status::Uninitialized("block cache is uninitialized"); + } Status st = _kv_cache->update_mem_quota(quota_bytes, flush_to_disk); _refresh_quota(); return st; } Status BlockCache::update_disk_spaces(const std::vector& spaces) { + if (!_kv_cache) { + return Status::Uninitialized("block cache is uninitialized"); + } Status st = _kv_cache->update_disk_spaces(spaces); _refresh_quota(); return st; @@ -170,15 +200,23 @@ Status BlockCache::adjust_disk_spaces(const std::vector& spaces) { } void BlockCache::record_read_remote(size_t size, int64_t lateny_us) { - _kv_cache->record_read_remote(size, lateny_us); + if (_kv_cache) { + _kv_cache->record_read_remote(size, lateny_us); + } } void BlockCache::record_read_cache(size_t size, int64_t lateny_us) { - _kv_cache->record_read_cache(size, lateny_us); + if (_kv_cache) { + _kv_cache->record_read_cache(size, lateny_us); + } } const DataCacheMetrics BlockCache::cache_metrics(int level) const { - return _kv_cache->cache_metrics(level); + if (_kv_cache) { + return _kv_cache->cache_metrics(level); + } + DataCacheMetrics dummy_metrics; + return dummy_metrics; } Status BlockCache::shutdown() { @@ -194,7 +232,10 @@ Status BlockCache::shutdown() { } DataCacheEngineType BlockCache::engine_type() { - return _kv_cache->engine_type(); + if (_kv_cache) { + return _kv_cache->engine_type(); + } + return DataCacheEngineType::STARCACHE; } void BlockCache::_refresh_quota() {