[Enhancement] Clean-up unused mem_pools in mem_tracker_manager (backport #67347) #67752
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why I'm doing:
The
mem_poolproperty for resource groups was introduced to StarRocks in the following pull requests:The collective usage statistics of resource groups which belong to the same memory pool are tracked by a shared memory tracker. These shared memory trackers are managed by the
mem_tracker_managerinstance, which owns an unordered map frommem_poolnames to their shared memory trackers.Currently, memory pools (and their corresponding shared memory trackers) can only be inserted to this map but not removed. A memory pool becomes unused and needs to be deleted when all the resource groups that belong to that pool are deleted.
This pull request adds the necessary functionality to automatically and safely clean up such unused
mem_poolentries.What I'm doing:
register_workgroupandderegister_workgroupmethods, which should be called whenever a new workgroup is constructed and destructed, respectively.register_workgroupmethod is called with a workgroup that has a new mem_pool, it will construct a new shared memory tracker instance and insert the memory pool name, memory tracker pointer, and child count into the unordered map. If it is called with a workgroup with an existing mem_pool, it will increase the number of children for that mem_pool by one.deregister_workgroupmethod is called, it will decrease mem_pool's number of children by one, or erase the mem_pool entry from the unordered map if the child count has reached 0.There is a tricky edge case that is also handled, which is explained below:
register_workgroupcannot simply insert a fresh entry and ignore the children waiting for deletion, but must take their number into account.deregister_workgroupand cause an underflow in the counter.register_workgroup. For fresh mem_pools, it will be 0-initialized and increased to 1. For reused mem_pools, it will keep the old count (expiring child count) and increase it by 1.This pull request also changes the
mark_del()function of theWorkgroupclass and parameterizes it with an expiration time. The value of expiration time is owned by theWorkgroupManagerand has the default of 120 seconds. This change was necessary for testing the correct behavior ofMemTrackerManager.Fixes #issue
What type of PR is this:
Does this PR entail a change in behavior?
If yes, please specify the type of change:
Checklist:
Bugfix cherry-pick branch check:
Note
Implements automatic cleanup of unused
mem_poolshared memory trackers and integrates lifecycle management with workgroups.MemTrackerManagerAPIs:register_workgroup/deregister_workgroupwith child counting; verify cached tracker limit to handle FE deletion/recreate edge case; exposelist_mem_trackersWorkGroupManagerto useregister_workgroup, callderegister_workgroupon cleanup, addlist_memory_pools, and makemark_delexpiration configurable viaset_workgroup_expiration_timeWorkGroup::mark_delto accept expiration; adjust vacuum time precision and defaultsWritten by Cursor Bugbot for commit 0cce91e. This will update automatically on new commits. Configure here.
This is an automatic backport of pull request #67347 done by [Mergify](https://mergify.com).