Skip to content

Commit

Permalink
[BugFix] Check the kv cache usability in datacache functions in case …
Browse files Browse the repository at this point in the history
…that the uninitialized members are called in some cases.

Signed-off-by: GavinMar <[email protected]>
  • Loading branch information
GavinMar committed Sep 19, 2024
1 parent a051e48 commit 23712b9
Showing 1 changed file with 45 additions and 4 deletions.
49 changes: 45 additions & 4 deletions be/src/block_cache/block_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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();
}
Expand All @@ -113,17 +125,26 @@ Status BlockCache::read_buffer(const CacheKey& cache_key, off_t offset, size_t s

StatusOr<size_t> 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);
return buffer.size();
}

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;
}
Expand All @@ -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;
Expand All @@ -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<DirSpace>& spaces) {
if (!_kv_cache) {
return Status::Uninitialized("block cache is uninitialized");
}
Status st = _kv_cache->update_disk_spaces(spaces);
_refresh_quota();
return st;
Expand All @@ -170,15 +200,23 @@ Status BlockCache::adjust_disk_spaces(const std::vector<DirSpace>& 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() {
Expand All @@ -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() {
Expand Down

0 comments on commit 23712b9

Please sign in to comment.