diff --git a/deps/oblib/src/lib/allocator/ob_allocator_v2.h b/deps/oblib/src/lib/allocator/ob_allocator_v2.h index 6b4738cab9..4c2f62506c 100644 --- a/deps/oblib/src/lib/allocator/ob_allocator_v2.h +++ b/deps/oblib/src/lib/allocator/ob_allocator_v2.h @@ -69,7 +69,6 @@ private: __MemoryContext__ *mem_context_; ObMemAttr attr_; const bool use_pm_; - lib::ObTenantCtxAllocatorGuard ta_; void *pm_; lib::ISetLocker *locker_; lib::SetDoNothingLocker do_nothing_locker_; diff --git a/src/sql/plan_cache/ob_lib_cache_node_factory.cpp b/src/sql/plan_cache/ob_lib_cache_node_factory.cpp index fced933793..576a97ed69 100644 --- a/src/sql/plan_cache/ob_lib_cache_node_factory.cpp +++ b/src/sql/plan_cache/ob_lib_cache_node_factory.cpp @@ -27,7 +27,8 @@ namespace sql int ObLCNodeFactory::create_cache_node(ObLibCacheNameSpace ns, ObILibCacheNode*& node, - uint64_t tenant_id) + uint64_t tenant_id, + MemoryContext &parent_context) { int ret = OB_SUCCESS; lib::MemoryContext entity = NULL; @@ -41,8 +42,8 @@ int ObLCNodeFactory::create_cache_node(ObLibCacheNameSpace ns, ret = OB_INVALID_ARGUMENT; LOG_WARN("out of the max type", K(ret), K(ns)); } else if (FALSE_IT(mem_attr.label_ = LC_NS_TYPE_LABELS[ns])) { - } else if (OB_FAIL(ROOT_CONTEXT->CREATE_CONTEXT(entity, - lib::ContextParam().set_mem_attr(mem_attr)))) { + } else if (OB_FAIL(parent_context->CREATE_CONTEXT(entity, + lib::ContextParam().set_mem_attr(mem_attr)))) { LOG_WARN("create entity failed", K(ret), K(mem_attr)); } else if (OB_ISNULL(entity)) { ret = OB_ERR_UNEXPECTED; diff --git a/src/sql/plan_cache/ob_lib_cache_node_factory.h b/src/sql/plan_cache/ob_lib_cache_node_factory.h index e36a25ec6b..10a8bd078f 100644 --- a/src/sql/plan_cache/ob_lib_cache_node_factory.h +++ b/src/sql/plan_cache/ob_lib_cache_node_factory.h @@ -34,7 +34,8 @@ public: void destroy_cache_node(ObILibCacheNode* node); int create_cache_node(ObLibCacheNameSpace ns, ObILibCacheNode*& node, - uint64_t tenant_id); + uint64_t tenant_id, + lib::MemoryContext &parent_context); template static int create(lib::MemoryContext &mem_ctx, ObILibCacheNode*& node, ObPlanCache *lib_cache); diff --git a/src/sql/plan_cache/ob_lib_cache_object_manager.cpp b/src/sql/plan_cache/ob_lib_cache_object_manager.cpp index 8550966d0f..fe3024b495 100644 --- a/src/sql/plan_cache/ob_lib_cache_object_manager.cpp +++ b/src/sql/plan_cache/ob_lib_cache_object_manager.cpp @@ -45,7 +45,8 @@ int ObLCObjectManager::init(int64_t hash_bucket, uint64_t tenant_id) int ObLCObjectManager::alloc(ObCacheObjGuard& guard, ObLibCacheNameSpace ns, - uint64_t tenant_id) + uint64_t tenant_id, + MemoryContext &parent_context) { int ret = OB_SUCCESS; lib::MemoryContext entity = NULL; @@ -60,7 +61,7 @@ int ObLCObjectManager::alloc(ObCacheObjGuard& guard, ret = OB_INVALID_ARGUMENT; LOG_WARN("out of the max type", K(ret), K(ns)); } else if (FALSE_IT(mem_attr.label_ = LC_NS_TYPE_LABELS[ns])) { - } else if (OB_FAIL(ROOT_CONTEXT->CREATE_CONTEXT(entity, + } else if (OB_FAIL(parent_context->CREATE_CONTEXT(entity, lib::ContextParam().set_mem_attr(mem_attr)))) { LOG_WARN("create entity failed", K(ret), K(mem_attr)); } else if (OB_ISNULL(entity)) { @@ -187,4 +188,3 @@ void ObLCObjectManager::inner_free(ObILibCacheObject *cache_obj) } // namespace common } // namespace oceanbase - diff --git a/src/sql/plan_cache/ob_lib_cache_object_manager.h b/src/sql/plan_cache/ob_lib_cache_object_manager.h index a2c931f118..766a8d499b 100644 --- a/src/sql/plan_cache/ob_lib_cache_object_manager.h +++ b/src/sql/plan_cache/ob_lib_cache_object_manager.h @@ -29,12 +29,13 @@ class ObLCObjectManager { public: typedef common::hash::ObHashMap IdCacheObjectMap; - + ObLCObjectManager() : object_id_(0) {} int init(int64_t hash_bucket, uint64_t tenant_id); int alloc(ObCacheObjGuard& guard, ObLibCacheNameSpace ns, - uint64_t tenant_id); + uint64_t tenant_id, + lib::MemoryContext &parent_context); int destroy_cache_obj(const bool is_leaked, const uint64_t object_id); void free(ObILibCacheObject *&obj, const CacheRefHandleID ref_handle) @@ -75,14 +76,14 @@ private: * cache node * | | | * cache_obj cache_obj cache_obj - * + * * In the library cache, each key corresponds to a cache node structure, which is very * inconvenient when traversing the output of the virtual table, so a map of key-cache_obj * is used to facilitate traversal. */ IdCacheObjectMap cache_obj_map_; /** - * + * * All cache_obj objects created by the alloc method will have their reference count incremented * and added to alloc_cache_obj_map_. when the free method is called, if the reference count of the * cache_obj object decreases to 0, it will be removed from alloc_cache_obj_map_. the role of diff --git a/src/sql/plan_cache/ob_lib_cache_register.cpp b/src/sql/plan_cache/ob_lib_cache_register.cpp index b9e5a43cae..61d0de158f 100644 --- a/src/sql/plan_cache/ob_lib_cache_register.cpp +++ b/src/sql/plan_cache/ob_lib_cache_register.cpp @@ -28,7 +28,6 @@ namespace oceanbase namespace common { class ObIAllocator; -class MemoryContext; } namespace sql { diff --git a/src/sql/plan_cache/ob_plan_cache.cpp b/src/sql/plan_cache/ob_plan_cache.cpp index bdda963c1f..b6ca51f930 100644 --- a/src/sql/plan_cache/ob_plan_cache.cpp +++ b/src/sql/plan_cache/ob_plan_cache.cpp @@ -322,6 +322,10 @@ void ObPlanCache::destroy() if (OB_SUCCESS != (cache_evict_all_obj())) { SQL_PC_LOG_RET(WARN, OB_ERROR, "fail to evict all lib cache cache"); } + if (root_context_ != NULL) { + DESTROY_CONTEXT(root_context_); + root_context_ = NULL; + } inited_ = false; } } @@ -331,7 +335,13 @@ int ObPlanCache::init(int64_t hash_bucket, uint64_t tenant_id) int ret = OB_SUCCESS; if (!inited_) { ObPCMemPctConf default_conf; - if (OB_FAIL(co_mgr_.init(hash_bucket, tenant_id))) { + ObMemAttr attr(tenant_id, "PlanCache", ObCtxIds::PLAN_CACHE_CTX_ID); + lib::ContextParam param; + param.set_properties(lib::ADD_CHILD_THREAD_SAFE | lib::ALLOC_THREAD_SAFE) + .set_mem_attr(attr); + if (OB_FAIL(ROOT_CONTEXT->CREATE_CONTEXT(root_context_, param))) { + SQL_PC_LOG(WARN, "failed to create context", K(ret)); + } else if (OB_FAIL(co_mgr_.init(hash_bucket, tenant_id))) { SQL_PC_LOG(WARN, "failed to init lib cache manager", K(ret)); } else if (OB_FAIL(cache_key_node_map_.create(hash::cal_next_prime(hash_bucket), ObModIds::OB_HASH_BUCKET_PLAN_CACHE, @@ -358,6 +368,12 @@ int ObPlanCache::init(int64_t hash_bucket, uint64_t tenant_id) ref_handle_mgr_.set_tenant_id(tenant_id_); inited_ = true; } + if (OB_FAIL(ret)) { + if (root_context_ != NULL) { + DESTROY_CONTEXT(root_context_); + root_context_ = NULL; + } + } } return ret; } @@ -1306,8 +1322,9 @@ int ObPlanCache::create_node_and_add_cache_obj(ObILibCacheKey *cache_key, ret = OB_INVALID_ARGUMENT; SQL_PC_LOG(WARN, "invalid argument", K(ret), K(cache_key), K(cache_obj)); } else if (OB_FAIL(cn_factory_.create_cache_node(cache_key->namespace_, - cache_node, - tenant_id_))) { + cache_node, + tenant_id_, + root_context_))) { SQL_PC_LOG(WARN, "failed to create cache node", K(ret), K_(cache_key->namespace), K_(tenant_id)); } else { // reference count after constructing cache node @@ -1846,7 +1863,7 @@ int ObPlanCache::add_stat_for_cache_obj(ObILibCacheCtx &ctx, ObILibCacheObject * int ObPlanCache::alloc_cache_obj(ObCacheObjGuard& guard, ObLibCacheNameSpace ns, uint64_t tenant_id) { int ret = OB_SUCCESS; - if (OB_FAIL(co_mgr_.alloc(guard, ns, tenant_id))) { + if (OB_FAIL(co_mgr_.alloc(guard, ns, tenant_id, root_context_))) { LOG_WARN("failed to alloc cache obj", K(ret)); } return ret; diff --git a/src/sql/plan_cache/ob_plan_cache.h b/src/sql/plan_cache/ob_plan_cache.h index 10cedac964..3e4d266ca6 100644 --- a/src/sql/plan_cache/ob_plan_cache.h +++ b/src/sql/plan_cache/ob_plan_cache.h @@ -441,6 +441,7 @@ private: int64_t mem_low_pct_; // low water mark percentage int64_t mem_used_; // mem used now int64_t bucket_num_; + lib::MemoryContext root_context_; common::ObMalloc inner_allocator_; common::ObAddr host_; ObPlanCacheStat pc_stat_;