From e89c7eb011c82ab9edf8e55f9f4f09edfb2b3458 Mon Sep 17 00:00:00 2001 From: oceanoverflow Date: Mon, 14 Oct 2024 07:43:37 +0000 Subject: [PATCH] fix restore kv cache miss issue --- .../src/lib/statistic_event/ob_stat_event.h | 2 + .../ob_information_kvcache_table.cpp | 2 + src/storage/backup/ob_backup_index_cache.cpp | 19 +++++++++- src/storage/backup/ob_backup_index_cache.h | 1 + src/storage/backup/ob_backup_index_store.cpp | 37 ++++++++++--------- src/storage/backup/ob_backup_index_store.h | 2 +- 6 files changed, 43 insertions(+), 20 deletions(-) diff --git a/deps/oblib/src/lib/statistic_event/ob_stat_event.h b/deps/oblib/src/lib/statistic_event/ob_stat_event.h index 11eb6a4b0..9e970c5e0 100644 --- a/deps/oblib/src/lib/statistic_event/ob_stat_event.h +++ b/deps/oblib/src/lib/statistic_event/ob_stat_event.h @@ -272,6 +272,8 @@ STAT_EVENT_ADD_DEF(DATA_BLOCK_CACHE_MISS, "data block cache miss", ObStatClassId STAT_EVENT_ADD_DEF(INDEX_BLOCK_CACHE_MISS, "index block cache miss", ObStatClassIds::CACHE, 50068, true, true, true) STAT_EVENT_ADD_DEF(MULTI_VERSION_FUSE_ROW_CACHE_HIT, "multi version fuse row cache hit", ObStatClassIds::CACHE, 50069, true, true, true) STAT_EVENT_ADD_DEF(MULTI_VERSION_FUSE_ROW_CACHE_MISS, "multi version fuse row cache miss", ObStatClassIds::CACHE, 50070, true, true, true) +STAT_EVENT_ADD_DEF(BACKUP_INDEX_CACHE_HIT, "backup index cache hit", ObStatClassIds::CACHE, 50071, true, true, true) +STAT_EVENT_ADD_DEF(BACKUP_INDEX_CACHE_MISS, "backup index cache miss", ObStatClassIds::CACHE, 50072, true, true, true) // STORAGE STAT_EVENT_ADD_DEF(MEMSTORE_LOGICAL_READS, "MEMSTORE_LOGICAL_READS", STORAGE, "MEMSTORE_LOGICAL_READS", true, true, false) diff --git a/src/observer/virtual_table/ob_information_kvcache_table.cpp b/src/observer/virtual_table/ob_information_kvcache_table.cpp index 1a08ddbd2..c1e0e676f 100644 --- a/src/observer/virtual_table/ob_information_kvcache_table.cpp +++ b/src/observer/virtual_table/ob_information_kvcache_table.cpp @@ -205,6 +205,8 @@ int ObInfoSchemaKvCacheTable::set_diagnose_info(ObKVCacheInst *inst, ObDiagnoseT inst->status_.total_miss_cnt_ = GLOBAL_EVENT_GET(ObStatEventIds::TABLET_CACHE_MISS); } else if (0 == strcmp(inst->status_.config_->cache_name_,"storage_meta_cache")) { inst->status_.total_miss_cnt_ = GLOBAL_EVENT_GET(ObStatEventIds::STORAGE_META_CACHE_MISS); + } else if (0 == strcmp(inst->status_.config_->cache_name_,"BACKUP_INDEX_CACHE")) { + inst->status_.total_miss_cnt_ = GLOBAL_EVENT_GET(ObStatEventIds::BACKUP_INDEX_CACHE_MISS); } return ret; diff --git a/src/storage/backup/ob_backup_index_cache.cpp b/src/storage/backup/ob_backup_index_cache.cpp index 22fb49bcd..968b5d5a8 100644 --- a/src/storage/backup/ob_backup_index_cache.cpp +++ b/src/storage/backup/ob_backup_index_cache.cpp @@ -57,6 +57,18 @@ bool ObBackupBlockDesc::operator==(const ObBackupBlockDesc &other) const file_id_ == other.file_id_ && offset_ == other.offset_ && length_ == other.length_; } +uint64_t ObBackupBlockDesc::calc_hash(uint64_t seed) const +{ + uint64_t hash_code = 0; + hash_code = murmurhash(&turn_id_, sizeof(turn_id_), seed); + hash_code = murmurhash(&retry_id_, sizeof(retry_id_), hash_code); + hash_code = murmurhash(&file_type_, sizeof(file_type_), hash_code); + hash_code = murmurhash(&file_id_, sizeof(file_id_), hash_code); + hash_code = murmurhash(&offset_, sizeof(offset_), hash_code); + hash_code = murmurhash(&length_, sizeof(length_), hash_code); + return hash_code; +} + /* ObBackupIndexCacheKey */ ObBackupIndexCacheKey::ObBackupIndexCacheKey() @@ -111,7 +123,12 @@ uint64_t ObBackupIndexCacheKey::get_tenant_id() const uint64_t ObBackupIndexCacheKey::hash() const { - return murmurhash(this, sizeof(ObBackupIndexCacheKey), 0); + uint64_t hash_code = 0; + hash_code = murmurhash(&tenant_id_, sizeof(tenant_id_), hash_code); + hash_code = murmurhash(&backup_set_id_, sizeof(backup_set_id_), hash_code); + hash_code = murmurhash(&ls_id_, sizeof(ls_id_), hash_code); + hash_code = block_desc_.calc_hash(hash_code); + return hash_code; } int64_t ObBackupIndexCacheKey::size() const diff --git a/src/storage/backup/ob_backup_index_cache.h b/src/storage/backup/ob_backup_index_cache.h index 4364d935e..b3374ee53 100644 --- a/src/storage/backup/ob_backup_index_cache.h +++ b/src/storage/backup/ob_backup_index_cache.h @@ -28,6 +28,7 @@ struct ObBackupBlockDesc { const int64_t offset, const int64_t length); bool is_valid() const; bool operator==(const ObBackupBlockDesc &other) const; + uint64_t calc_hash(uint64_t seed) const; TO_STRING_KV(K_(turn_id), K_(retry_id), K_(file_type), K_(file_id), K_(offset), K_(length)); int64_t turn_id_; int64_t retry_id_; diff --git a/src/storage/backup/ob_backup_index_store.cpp b/src/storage/backup/ob_backup_index_store.cpp index e83752d69..4a86cbb23 100644 --- a/src/storage/backup/ob_backup_index_store.cpp +++ b/src/storage/backup/ob_backup_index_store.cpp @@ -196,13 +196,11 @@ int ObIBackupIndexStore::decode_index_from_block_(const int64_t data_zlength, co } int ObIBackupIndexStore::fetch_block_(const ObBackupFileType &backup_file_type, const int64_t offset, - const int64_t length, common::ObIAllocator &allocator, blocksstable::ObBufferReader &buffer_reader) + const int64_t length, common::ObIAllocator &allocator, ObKVCacheHandle &handle, blocksstable::ObBufferReader &buffer_reader) { int ret = OB_SUCCESS; ObBackupIndexCacheKey key; const ObBackupIndexCacheValue *pvalue = NULL; - ObKVCacheHandle handle; - char *buf = NULL; if (offset < 0 || length <= 0) { ret = OB_INVALID_ARGUMENT; LOG_WARN("get invalid args", K(ret), K(offset), K(length)); @@ -213,6 +211,7 @@ int ObIBackupIndexStore::fetch_block_(const ObBackupFileType &backup_file_type, LOG_WARN("failed to get backup index cache key", K(ret), K(backup_file_type), K(offset), K(length)); } else if (OB_FAIL(index_kv_cache_->get(key, pvalue, handle))) { if (OB_ENTRY_NOT_EXIST == ret) { + EVENT_INC(ObStatEventIds::BACKUP_INDEX_CACHE_MISS); ret = OB_SUCCESS; if (OB_FAIL(do_on_cache_miss_(backup_file_type, offset, length, allocator, buffer_reader))) { LOG_WARN("failed to do on cache miss", K(ret), K(backup_file_type), K(offset), K(length)); @@ -227,13 +226,9 @@ int ObIBackupIndexStore::fetch_block_(const ObBackupFileType &backup_file_type, } else if (OB_ISNULL(pvalue)) { ret = OB_ERR_UNEXPECTED; LOG_WARN("cache value should not be null", K(ret)); - } else if (OB_ISNULL(buf = static_cast(allocator.alloc(pvalue->len())))) { - ret = OB_ALLOCATE_MEMORY_FAILED; - LOG_WARN("failed to allocate memory", K(ret), K(*pvalue)); } else { - // TODO(yangyi.yyy): refine this method, remove the need of memory copying in 4.1 - MEMCPY(buf, pvalue->buf(), pvalue->len()); - buffer_reader.assign(buf, pvalue->len()); + buffer_reader.assign(pvalue->buf(), pvalue->len()); + EVENT_INC(ObStatEventIds::BACKUP_INDEX_CACHE_HIT); } return ret; } @@ -482,20 +477,23 @@ int ObBackupMetaIndexStore::get_tablet_meta_index_(const ObBackupMetaKey &meta_k } else if (OB_FAIL(meta_key.get_backup_index_file_type(backup_file_type))) { LOG_WARN("failed to get backup index file type", K(ret), K(meta_key)); } else { + ObKVCacheHandle handle; ObBackupMetaIndex index; ObArray index_list; ObBackupMetaIndexIndex index_index; ObArray index_index_list; while (OB_SUCC(ret)) { + buffer_reader.assign(NULL, 0); + handle.reset(); + allocator.reuse(); index_index.reset(); index_index_list.reset(); - buffer_reader.assign(NULL, 0); ObBackupCommonHeader common_header; ObBackupMultiLevelIndexHeader index_header; int64_t data_length = 0; int64_t data_zlength = 0; ObCompressorType compressor_type = ObCompressorType::INVALID_COMPRESSOR; - if (OB_FAIL(fetch_block_(backup_file_type, offset, length, allocator, buffer_reader))) { + if (OB_FAIL(fetch_block_(backup_file_type, offset, length, allocator, handle, buffer_reader))) { LOG_WARN("failed to get block from cache", K(ret), K(backup_file_type), K(offset), K(length)); } else if (OB_FAIL(decode_headers_(buffer_reader, common_header, index_header, data_length, data_zlength, compressor_type))) { @@ -520,7 +518,6 @@ int ObBackupMetaIndexStore::get_tablet_meta_index_(const ObBackupMetaKey &meta_k offset = index_index.offset_; length = index_index.length_; LOG_DEBUG("find tablet meta index index", K(meta_key), K(index_index), K(index_index_list)); - allocator.reuse(); } } } @@ -780,17 +777,20 @@ int ObBackupMacroBlockIndexStore::inner_get_macro_block_range_index_( ObArray index_list; ObBackupMacroRangeIndexIndex index_index; ObArray index_index_list; + ObKVCacheHandle handle; int64_t round = 0; while (OB_SUCC(ret)) { + buffer_reader.assign(NULL, 0); + handle.reset(); + allocator.reuse(); index_index.reset(); index_index_list.reset(); - buffer_reader.assign(NULL, 0); ObBackupCommonHeader common_header; ObBackupMultiLevelIndexHeader index_header; int64_t data_length = 0; int64_t data_zlength = 0; ObCompressorType compressor_type = ObCompressorType::INVALID_COMPRESSOR; - if (OB_FAIL(fetch_block_(backup_file_type, offset, length, allocator, buffer_reader))) { + if (OB_FAIL(fetch_block_(backup_file_type, offset, length, allocator, handle, buffer_reader))) { LOG_WARN("failed to fetch block", K(ret), K(offset), K(length)); } else if (OB_FAIL(decode_headers_(buffer_reader, common_header, index_header, data_length, data_zlength, compressor_type))) { LOG_WARN("failed to decode header", K(ret), K(offset), K(length), K(buffer_reader)); @@ -812,7 +812,6 @@ int ObBackupMacroBlockIndexStore::inner_get_macro_block_range_index_( } else { offset = index_index.offset_; length = index_index.length_; - allocator.reuse(); } } } @@ -1542,18 +1541,21 @@ int ObBackupOrderedMacroBlockIndexStore::get_macro_block_index( } else { ObBackupCommonHeader common_header; ObBackupMultiLevelIndexHeader index_header; + ObKVCacheHandle handle; while (OB_SUCC(ret)) { + buffer_reader.assign(NULL, 0); + handle.reset(); + allocator.reuse(); index.reset(); index_index.reset(); index_list.reset(); index_index_list.reset(); - buffer_reader.assign(NULL, 0); common_header.reset(); index_header.reset(); int64_t data_length = 0; int64_t data_zlength = 0; ObCompressorType compressor_type = ObCompressorType::INVALID_COMPRESSOR; - if (OB_FAIL(fetch_block_(backup_file_type, offset, length, allocator, buffer_reader))) { + if (OB_FAIL(fetch_block_(backup_file_type, offset, length, allocator, handle, buffer_reader))) { LOG_WARN("failed to fetch block", K(ret), K_(trailer)); } else if (OB_FAIL(decode_headers_(buffer_reader, common_header, index_header, data_length, data_zlength, compressor_type))) { @@ -1576,7 +1578,6 @@ int ObBackupOrderedMacroBlockIndexStore::get_macro_block_index( } else { offset = index_index.offset_; length = index_index.length_; - allocator.reuse(); } } } diff --git a/src/storage/backup/ob_backup_index_store.h b/src/storage/backup/ob_backup_index_store.h index 2d824cfc8..c985199be 100644 --- a/src/storage/backup/ob_backup_index_store.h +++ b/src/storage/backup/ob_backup_index_store.h @@ -70,7 +70,7 @@ protected: int decode_index_from_block_(const int64_t data_zlength, const int64_t original_size, const ObCompressorType &compressor_type, blocksstable::ObBufferReader &buffer_reader, common::ObIArray &index_list); int fetch_block_(const ObBackupFileType &backup_file_type, const int64_t offset, const int64_t length, - common::ObIAllocator &allocator, blocksstable::ObBufferReader &buffer_reader); + common::ObIAllocator &allocator, ObKVCacheHandle &handle, blocksstable::ObBufferReader &buffer_reader); int do_on_cache_miss_(const ObBackupFileType &backup_file_type, const int64_t offset, const int64_t length, common::ObIAllocator &allocator, blocksstable::ObBufferReader &buffer_reader); int put_block_to_cache_(const ObBackupFileType &backup_file_type, const int64_t offset, const int64_t length,