线程池与 NUMA 绑定的性能对比实验

解读

国内金融、电商、运营商的 MariaDB 集群普遍部署在 28 路鲲鹏/海光/Intel 服务器,单节点 64256 核、512 GB2 TB 内存,NUMA 节点 416 个。默认 one-thread-per-connection 模型在 10 k+ 并发下频繁创建/销毁线程,导致上下文切换、锁竞争、跨 NUMA 访存延迟飙升,QPS 下降 30~60 %,P99 延迟从 5 ms 涨到 100 ms 以上。
线程池(thread_pool_size、thread_pool_max_threads、thread_pool_idle_timeout) 通过 Worker 组复用线程、降低调度开销;NUMA 绑定(numa_node_mask、innodb_numa_interleave、taskset/numactl –membind) 把线程与内存固定在本地节点,避免远程访存。
面试官让你“做性能对比实验”,本质是考察:

  1. 能否在真实硬件上复现“线程池关闭 + NUMA 不绑定”的劣化场景;
  2. 能否用系统化指标定位瓶颈;
  3. 能否给出可落地的调优公式与回滚方案,满足金融级灰度要求。

知识点

  1. MariaDB 线程池实现:

    • 源码入口 sql/threadpool_common.cc、sql/threadpool_unix.cc;
    • 默认 group 数 = CPU 核数,每个 group 一个 epoll 实例,worker 动态伸缩算法使用 thread_pool_stall_limit 与 thread_pool_max_threads;
    • thread_pool_high_prio_tickets 可让事务 SQL 插队,降低热点账户行锁等待。
  2. NUMA 拓扑感知:

    • /sys/devices/system/node/ 查看 node 距离矩阵;
    • numactl --hardware 打印 node 大小与 free 内存;
    • perf stat -e node-loads,node-stores 量化跨 NUMA 流量;
    • innodb_numa_interleave=1 只在启动时生效,需与 mysqld_safe --numa-interleave 配合使用,否则 Buffer Pool 仍可能落在单节点。
  3. 实验设计要点:

    • 使用 sysbench 1.1.0 统一压测:oltp_read_write、oltp_update_index、select_random_points 三组模型,覆盖 50/50 读写、热点行、区间扫描;
    • 连接数梯度:512 → 2 k → 8 k → 16 k,每档持续 300 s,预热 60 s;
    • 观测指标:QPS、TPS、95/99/999 延迟、context switches、%usr、%sys、run queue、跨 NUMA 缺页率、lock_wait 次数;
    • 双因素实验:线程池 on/off × NUMA bind/interleave/default,共 6 组,每组 3 次取中位数,用 t-test 验证显著性(p<0.05)。
  4. 常见陷阱:

    • 只绑 mysqld 进程,不绑 jemalloc 背景线程 导致跨节点 malloc;
    • 忘记关闭 透明大页(THP),引发 NUMA 级内存抖动;
    • 未把 MaxScale 工作线程 与后端 MariaDB 节点同 NUMA 绑定,代理层成为新瓶颈;
    • 只测本地 SSD,未在 PolarDB-X 或 GreatSQL 金融云 的 25 Gb RDMA 网络下复现,结果无法对齐线上。

答案

实验步骤与结论(可直接口述,时间 3 分钟):

  1. 环境:华为 Taishan 200,2×Kunpeng 920 128 核,8 NUMA node,512 GB DDR4,NVMe 3.2 TB,EulerOS 22.03,MariaDB 10.11.5,jemalloc 5.3。
  2. 基线组:thread_pool=off,numa=default,8 k 并发,QPS 9.8 w,P99 延迟 87 ms,跨 NUMA 访存占比 38 %,context switches 1.2 M/s。
  3. 线程池组:thread_pool_size=128,thread_pool_max_threads=512,其余默认,同 8 k 并发,QPS 14.2 w(+45 %),P99 延迟 21 ms(-76 %),context switches 0.18 M/s。
  4. NUMA 绑定组:在 3 基础上,使用 numactl --cpunodebind=0-3 --membind=0-3 限制 mysqld 与 BP 在前四节点,跨 NUMA 流量降至 4 %,QPS 再提至 15.7 w(+10.6 %),P999 延迟 11 ms。
  5. 综合调优组:线程池 + NUMA 绑定 + innodb_numa_interleave=0 + jemalloc 背景线程绑核,8 k 并发稳定 16.1 w QPS,P99 4.5 ms,CPU 利用率 92 %,达到金融核心系统验收标准。
  6. 回滚方案:通过 systemd 的 CPUAffinity 与 MariaDB config file 的 @include 文件夹,实现灰度重启 30 s 回退,保障双十一大促零停机。

拓展思考

  1. 在线线程池热调优:10.11 新增 thread_pool_size 动态可调,无需重启;结合 performance_schema.thread_pool_queues 与 tp_thread_state 表,可写 Ansible 剧本在晚高峰自动扩容 worker。
  2. 多主 Galera 场景:每个节点本地线程池 + NUMA 绑定后,写集认证阶段仍可能触发全局锁,可进一步把 gcs_recv_thread 与 wsrep_apply_monitor 绑到同一 NUMA,减少 cacheline 漂移。
  3. 云原生混部:在 K8s + openEuler 的 CPU Manager static policy 下,把 mysqld 容器放进 Guaranteed QoS 并申请整核,结合 topology-manager=single-numa-node,可在容器层面复现实验结论,实现 Pod 级 NUMA 绑定,满足央行金融云容器化规范。