From 76725bfcbb01c33248624c89cd45ccb5ed7b1411 Mon Sep 17 00:00:00 2001 From: Xinyi Zou Date: Tue, 12 Mar 2024 20:00:32 +0800 Subject: [PATCH] 4 --- .../olap/rowset/segment_v2/inverted_index_cache.h | 3 ++- be/src/olap/txn_manager.cpp | 3 +-- be/src/olap/txn_manager.h | 3 +-- be/src/runtime/memory/cache_manager.cpp | 2 +- be/src/runtime/memory/cache_policy.h | 4 ++++ be/src/runtime/memory/lru_cache_policy.h | 15 +++++---------- be/src/runtime/memory/lru_cache_value_base.h | 1 + be/src/service/point_query_executor.h | 4 ++++ 8 files changed, 19 insertions(+), 16 deletions(-) diff --git a/be/src/olap/rowset/segment_v2/inverted_index_cache.h b/be/src/olap/rowset/segment_v2/inverted_index_cache.h index de3db555476cdd6..3292b10e25bd37e 100644 --- a/be/src/olap/rowset/segment_v2/inverted_index_cache.h +++ b/be/src/olap/rowset/segment_v2/inverted_index_cache.h @@ -89,7 +89,8 @@ class InvertedIndexSearcherCache { CacheValue() : LRUCacheValueBase(CachePolicy::CacheType::INVERTEDINDEX_SEARCHER_CACHE) {} explicit CacheValue(IndexSearcherPtr searcher, size_t mem_size, int64_t visit_time) - : index_searcher(std::move(searcher)) { + : LRUCacheValueBase(CachePolicy::CacheType::INVERTEDINDEX_SEARCHER_CACHE), + index_searcher(std::move(searcher)) { size = mem_size; last_visit_time = visit_time; } diff --git a/be/src/olap/txn_manager.cpp b/be/src/olap/txn_manager.cpp index 5f8a0f145d3d6c2..c40551d6285773c 100644 --- a/be/src/olap/txn_manager.cpp +++ b/be/src/olap/txn_manager.cpp @@ -879,8 +879,7 @@ void TxnManager::update_tablet_version_txn(int64_t tablet_id, int64_t version, i CacheKey cache_key((const char*)&key, sizeof(key)); auto* value = new CacheValue; - value->value = new int64_t; - *value->value = txn_id; + value->value = txn_id; auto* handle = _tablet_version_cache->insert(cache_key, value, 1, sizeof(txn_id), CachePriority::NORMAL); _tablet_version_cache->release(handle); diff --git a/be/src/olap/txn_manager.h b/be/src/olap/txn_manager.h index 098488997885283..724255bccafa536 100644 --- a/be/src/olap/txn_manager.h +++ b/be/src/olap/txn_manager.h @@ -129,9 +129,8 @@ class TxnManager { class CacheValue : public LRUCacheValueBase { public: CacheValue() : LRUCacheValueBase(CachePolicy::CacheType::TABLET_VERSION_CACHE) {} - ~CacheValue() override { delete value; } - int64_t* value; + int64_t value; }; // add a txn to manager diff --git a/be/src/runtime/memory/cache_manager.cpp b/be/src/runtime/memory/cache_manager.cpp index 540e15cbe7c01cb..d17954ffe8bbc08 100644 --- a/be/src/runtime/memory/cache_manager.cpp +++ b/be/src/runtime/memory/cache_manager.cpp @@ -27,7 +27,7 @@ int64_t CacheManager::for_each_cache_prune_stale_wrap( int64_t freed_size = 0; std::lock_guard l(_caches_lock); for (const auto& pair : _caches) { - auto* cache_policy = pair->second; + auto* cache_policy = pair.second; if (!cache_policy->enable_prune()) { continue; } diff --git a/be/src/runtime/memory/cache_policy.h b/be/src/runtime/memory/cache_policy.h index c56e3bd6227ad37..459e6423add2da2 100644 --- a/be/src/runtime/memory/cache_policy.h +++ b/be/src/runtime/memory/cache_policy.h @@ -116,6 +116,10 @@ class CachePolicy { _cost_timer = ADD_TIMER(_profile, "CostTime"); } + void init_mem_tracker(const std::string& name) { + _mem_tracker = std::make_shared(MemTrackerLimiter::Type::GLOBAL, name); + } + CacheType _type; std::shared_ptr _mem_tracker; diff --git a/be/src/runtime/memory/lru_cache_policy.h b/be/src/runtime/memory/lru_cache_policy.h index 6e9dbf85cc73e83..35cf767b84636fd 100644 --- a/be/src/runtime/memory/lru_cache_policy.h +++ b/be/src/runtime/memory/lru_cache_policy.h @@ -45,7 +45,8 @@ class LRUCachePolicy : public CachePolicy { CHECK(ExecEnv::GetInstance()->get_dummy_lru_cache()); _cache = ExecEnv::GetInstance()->get_dummy_lru_cache(); } - _init_mem_tracker(); + init_mem_tracker( + fmt::format("{}[{}]", type_string(_type), lru_cache_type_string(_lru_cache_type))); } LRUCachePolicy(CacheType type, size_t capacity, LRUCacheType lru_cache_type, @@ -63,7 +64,8 @@ class LRUCachePolicy : public CachePolicy { CHECK(ExecEnv::GetInstance()->get_dummy_lru_cache()); _cache = ExecEnv::GetInstance()->get_dummy_lru_cache(); } - _init_mem_tracker(); + init_mem_tracker( + fmt::format("{}[{}]", type_string(_type), lru_cache_type_string(_lru_cache_type))); } ~LRUCachePolicy() override { _cache.reset(); } @@ -98,8 +100,7 @@ class LRUCachePolicy : public CachePolicy { CachePriority priority = CachePriority::NORMAL) { size_t bytes_with_handle = _get_bytes_with_handle(key, charge, tracking_bytes); if (value != nullptr && tracking_bytes > 0) { - CHECK(((LRUCacheValueBase*)value)->mem_tracker()->label() == _mem_tracker->label()); - _mem_tracker->cache_consume(bytes_with_handle); + ((LRUCacheValueBase*)value)->mem_tracker()->cache_consume(bytes_with_handle); ((LRUCacheValueBase*)value)->set_tracking_bytes(bytes_with_handle); } return _cache->insert(key, value, charge, priority); @@ -182,12 +183,6 @@ class LRUCachePolicy : public CachePolicy { } private: - void _init_mem_tracker() { - _mem_tracker = std::make_shared( - MemTrackerLimiter::Type::GLOBAL, - fmt::format("{}[{}]", type_string(_type), lru_cache_type_string(_lru_cache_type))); - } - // LRUCacheType::SIZE equal to total_size. size_t _get_bytes_with_handle(const CacheKey& key, size_t charge, size_t bytes) { size_t handle_size = sizeof(LRUHandle) - 1 + key.size(); diff --git a/be/src/runtime/memory/lru_cache_value_base.h b/be/src/runtime/memory/lru_cache_value_base.h index 6f31ae3f5ec3577..2bb06dfaad5fdfa 100644 --- a/be/src/runtime/memory/lru_cache_value_base.h +++ b/be/src/runtime/memory/lru_cache_value_base.h @@ -25,6 +25,7 @@ namespace doris { // Base of the lru cache value. class LRUCacheValueBase { public: + LRUCacheValueBase() = delete; LRUCacheValueBase(CachePolicy::CacheType type) { _mem_tracker = CacheManager::instance()->get_cache(type)->mem_tracker(); } diff --git a/be/src/service/point_query_executor.h b/be/src/service/point_query_executor.h index aab469cc31ea389..58cf2736313a900 100644 --- a/be/src/service/point_query_executor.h +++ b/be/src/service/point_query_executor.h @@ -162,6 +162,10 @@ class RowCache : public LRUCachePolicy { bool valid() { return _cache != nullptr && _handle != nullptr; } LRUCachePolicy* cache() const { return _cache; } + Slice data() const { + return {(char*)((RowCacheValue*)_cache->value(_handle))->cache_value, + reinterpret_cast(_handle)->charge}; + } private: LRUCachePolicy* _cache = nullptr;