Questions About TiKV OOM Phenomenon

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 关于 TiKV OOM 现象的疑问

| username: OnTheRoad

[Usage Environment] Production (3 TiDB/3 PD/3 TiKV)
[TiDB Version] 5.3.0
[Encountered Problem] Some memory limit parameters of TiKV are not well understood

  1. TiKV instances are deployed independently, with the following memory configuration (host physical memory 128G, free -g shows 125G):
tikv:
    raftdb.defaultcf.block-cache-size: 4GiB
    readpool.unified.max-thread-count: 38
    rocksdb.defaultcf.block-cache-size: 50GiB
    rocksdb.lockcf.block-cache-size: 4GiB
    rocksdb.writecf.block-cache-size: 25GiB
    server.grpc-concurrency: 14
    server.grpc-raft-conn-num: 5
    split.qps-threshold: 2000
    storage.block-cache.capacity: 90GiB
mysql> show config where name like 'storage.block-cache.capacity';
+------+---------------------+------------------------------+-------+
| Type | Instance            | Name                         | Value |
+------+---------------------+------------------------------+-------+
| tikv | 192.168.3.225:20160 | storage.block-cache.capacity | 75GiB |
| tikv | 192.168.3.224:20160 | storage.block-cache.capacity | 75GiB |
| tikv | 192.168.3.226:20160 | storage.block-cache.capacity | 75GiB |
+------+---------------------+------------------------------+-------+
3 rows in set (0.01 sec)
  1. Grafana->TiKV-Detail->RocksDB-KV->Block Cache Size panel
    image

  2. THP (Transparent HugePage) is not disabled

[tidb@idc1-offline-tikv2 scripts]$ cat /proc/meminfo|grep Huge
AnonHugePages:      4096 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
[tidb@idc1-offline-tikv2 scripts]$ cat /sys/kernel/mm/transparent_hugepage/defrag 
[always] madvise never
  1. Before OOM, the TiKV log is as follows:
[2022/09/15 21:02:37.837 +08:00] [WARN] [config.rs:2650] ["memory_usage_limit:ReadableSize(134217728000) > recommanded:ReadableSize(103432923648), maybe page cache isn't enough"]
[2022/09/15 21:02:37.880 +08:00] [WARN] [config.rs:2650] ["memory_usage_limit:ReadableSize(137910564864) > recommanded:ReadableSize(103432923648), maybe page cache isn't enough"]
[2022/09/15 21:02:37.882 +08:00] [INFO] [server.rs:1408] ["beginning system configuration check"]
[2022/09/15 21:02:37.882 +08:00] [INFO] [config.rs:919] ["data dir"] [mount_fs="FsInfo { tp: \"ext4\", opts: \"rw,noatime,nodelalloc,stripe=64,data=ordered\", mnt_dir: \"/tidb-data\", fsname: \"/dev/sdb1\" }"] [data_path=/tidb-data/tikv-20160]
[2022/09/15 21:02:37.883 +08:00] [INFO] [config.rs:919] ["data dir"] [mount_fs="FsInfo { tp: \"ext4\", opts: \"rw,noatime,nodelalloc,stripe=64,data=ordered\", mnt_dir: \"/tidb-data\", fsname: \"/dev/sdb1\" }"] [data_path=/tidb-data/tikv-20160/raft]
[2022/09/15 21:02:37.898 +08:00] [INFO] [server.rs:316] ["using config"] [config="{\"log-level\":\"info\",\"log-file\":\"/tidb-deploy/tikv-20160/log/tikv.log\",\"log-format\":\"text\",\"slow-log-file\":\"\",\"slow-log-threshold\":\"1s\",\"log-rotation-timespan\":\"1d\",\"log-rotation-size\":\"300MiB\",\"panic-when-unexpected-key-or-data\":false,\"enable-io-snoop\":true,\"abort-on-panic\":false,\"memory-usage-limit\":\"125GiB\",\"memory-usage-high-water\":0.9,....

A colleague increased the Block Cache Size to 90G to increase the amount of data cached in memory. I personally believe that the parameter is set too high, which is the main cause of the OOM.

According to the description in https://asktug.com/t/topic/933132/2.

When Block Cache Size is set to 90G:

  1. memory-usage-limit = Block Cache * 10/6 = 150G, since physical memory is 128G, memory-usage-limit=128G.
  2. memory-usage-high-water is 0.9, so memory-usage-limit * 0.9 = 128G * 0.9 = 115G.
  3. Page-Cache = physical memory * 0.25 = 128G * 0.25 = 32G

Questions

  1. What is the role of memory-usage-high-water here? It seems to have no effect?
  2. Can the size of Page-Cache be adjusted? Is it by default 1/4 of the physical memory?
  3. In the future, can we set a total memory limit parameter for TiKV, like Oracle’s SGA, such as tikv_memory_target? Then, TiKV can automatically adjust the memory limits of its internal components based on the ratio or running status. At the same time, the memory limits of TiKV’s internal components can also be explicitly specified?
  4. After one TiKV OOM restarts, the memory usage drops. The memory usage of the other two also drops. What is the reason for this?
  5. By setting storage.block-cache.capacity=75G through the set command, but the TiKV configuration file sets it to 90G. Grafana monitoring shows that storage.block-cache.capacity is 75G. Is it because the system variable set through the set command has a higher priority than the setting in the TiKV cluster configuration file? Will the value set by the set command not be persisted to the cluster configuration file?
| username: 数据小黑 | Original post link

Regarding the 4th issue you mentioned, in my experience, the common scenario is that after an OOM restart, the SQL that caused the OOM fails to execute, so the other two are reduced.

| username: OnTheRoad | Original post link

One of the three TiKV instances on my side experiences a slight increase in memory usage every day, approximately 0.X%. Until last night around 9 PM, it OOMed. The memory usage dropped. The other two TiKV instances also saw a decrease.

| username: xfworld | Original post link

The issue you mentioned probably needs to be discussed in a meeting.

| username: OnTheRoad | Original post link

Reproduction steps:
Set the storage.block-cache.capacity parameter of TiKV to physical memory * 3/5, so that memory-usage-limit = block-cache * 5/3 = physical memory * 3/5 * 5/3 = physical memory.
Theoretically, when the TiKV instance’s memory usage reaches memory-usage-limit * 0.9, the hidden parameter memory-usage-high-water should limit memory growth to avoid OOM.
However, in practice, this hidden parameter does not effectively limit memory growth, causing the TiKV instance to restart due to OOM. After the restart, the memory usage of the 3 TiKV instances all decreased.

| username: OnTheRoad | Original post link

After lowering storage.block-cache.capacity=70G, theoretically, memory-usage-limit should decrease to 116G (i.e., 70*1.66). However, after observing for 2 days, the value of memory-usage-limit has not decreased accordingly and remains at the old value of 125G.