You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SQL execution layer invokes the memory tracker to report the heap usage to the session layer.
The top-level OOM control module regularly checks at a granularity of 100 milliseconds. When the global memory exceeds the server limit, it executes kill sql & session operations to release memory resources and protect the memory safety of the tidb process
The system variable tidb_server_memory_limit controls the threshold for the memory usage of a tidb-server instance.
When the memory usage of TiDB reaches the limit, TiDB cancels the currently running SQL statement with the highest memory usage (greater than tidb_server_memory_limit_sess_min_size). After the SQL statement is successfully canceled, TiDB tries to call Golang GC to immediately reclaim memory to relieve memory stress as soon as possible.
The system variable tidb_mem_quota_query sets the limit for a query in bytes. If the memory quota of a session during execution exceeds the threshold value, TiDB performs the operation defined by tidb_mem_oom_action.
There are 3 issues under the current memory control mechanism:
Kill SQL Risk
When facing concurrent queries, the Kill behavior has a certain randomness. TiDB lacks the ability to control or schedule memory resources, and cannot guarantee that all queries will eventually be successfully executed. Especially for AP scenarios that need to ensure SQL execution, this method is difficult to meet basic needs.
Performance Risk: Heavy Golang GC
When the global memory usage reaches the limit of golang runtime and most of the memory resources are legally occupied, frequent golang GC may be triggered, greatly affecting the delay of normal memory allocation.
There is already a mechanism called memoryLimitTuner to reduce the impact of golang GC. It dynamically adjusts the memory limit of golang runtime (adjusted every 1 minute by default). However, The memory limit of tidb will be larger than tidb_server_memory_limit.
OOM Risk
According to the implementation of servermemorylimit mechanism, each memory tracker is independent. When the controller begins to kill the top sql / session, others may continue to consume heap. Such information isolated island can easily lead to TiDB instance OOM. This phenomenon is particularly serious when facing a large number of concurrent small OLAP queries. It is also one of the common factors of OOM problems in customer scenarios.
Rank the 3 existing issues by severity: OOM ≈ Heavy Golang GC > Kill SQL/session
Kill SQL is the least serious. Although SQL errors may affect upper-level business, they do not affect the security of the tidb instance. Reclaiming memory by KILL only works when there are enough heap resources can be GC (not occupied legally) to make the SQL complete the kill process.
The severity of Heavy Golang GC is approximately equal to OOM. When memory resources are insufficient and mostly legally held, a large amount of CPU is wasted on invalid GC, and the process is prone to freezing.
The OOM issue is the most serious. If golang runtime judges that GC cannot be performed, it will apply for memory from the system, and the process is prone to OOM.
SQL associated with mem-arbitrator, memory subscription requests will all be satisfied until SQL execution is complete; The new SQL will no longer be connected to mem-arbitrator; Disable the Kill mechanism of mem-arbitrator;
STANDARD mode
SQL active CANCEL when memory resources are insufficient
When memory resources are insufficient, CANCEL SQL according to Resource Group priority , and the memory resource subscription task of the same level SQL is executed according to FIFO blocking scheduling (SQL execution time constraint max_execution_time)
CANCEL low-priority SQL order: priority from small to high, memory usage from large to small
detect roo pool hang during CANCEL
await-free pool
For small queries / tasks whose max mem usage is less than 1/1000 of server limit
Feature Request: Global Memory Control Mechanism
Background
This diagram describes the bottom-up process of the current TiDB memory control chain.
The default behavior of TiDB Memory Control is like:
memory trackerto report the heap usage to the session layer.There are 3 issues under the current memory control mechanism:
Killbehavior has a certain randomness. TiDB lacks the ability to control or schedule memory resources, and cannot guarantee that all queries will eventually be successfully executed. Especially for AP scenarios that need to ensure SQL execution, this method is difficult to meet basic needs.memoryLimitTunerto reduce the impact of golang GC. It dynamically adjusts the memory limit of golang runtime (adjusted every1 minuteby default). However, The memory limit of tidb will be larger thantidb_server_memory_limit.servermemorylimitmechanism, each memory tracker is independent. When the controller begins to kill the top sql / session, others may continue to consume heap. Suchinformation isolated islandcan easily lead to TiDB instance OOM. This phenomenon is particularly serious when facing a large number of concurrent small OLAP queries. It is also one of the common factors of OOM problems in customer scenarios.Rank the
3existing issues by severity:OOM≈Heavy Golang GC>Kill SQL/sessionKill SQLis the least serious. Although SQL errors may affect upper-level business, they do not affect the security of the tidb instance. Reclaiming memory byKILLonly works when there are enough heap resources can be GC (not occupied legally) to make the SQL complete the kill process.Heavy Golang GCis approximately equal toOOM. When memory resources are insufficient and mostly legally held, a large amount of CPU is wasted on invalid GC, and the process is prone to freezing.OOMissue is the most serious. If golang runtime judges that GC cannot be performed, it will apply for memory from the system, and the process is prone to OOM.Goals
Alternatives
User Scenarios
Proposal
Progress
Release Plan
Development
MemArbitrator&ResourcePool#60597CANCELwhen memory resources are insufficientCANCELSQL according to Resource Group priority , and the memory resource subscription task of the same level SQL is executed according to FIFO blocking scheduling (SQL execution time constraint max_execution_time)CANCELlow-priority SQL order: priority from small to high, memory usage from large to smallCANCELTests