已开启
KV 存储性能优化:快速路径、TLS 句柄池缓存与批量写入 #37
KV 存储性能优化:快速路径、TLS 句柄池缓存与批量写入 #37
已开启
画晓月创建于 5月11日
画晓月
画晓月
5月11日

【标题】KV存储引擎性能优化:快速路径、TLS句柄池缓存与批量写入

【实现内容】:
1. KV 快速路径优化 : 缓存表元数据并内联关键路径,消除重复查找和函数调用开销
2. TLS 句柄池缓存 :引入线程局部存储减少全局 spinlock 竞争
3. KV 批量写入 : 新增 BatchSet API,事务内批量 put 减少往返开销
4. 修复内核构建中统计会话分配/释放的兼容性问题

【根因分析】:
快速路径:每次 KV put/get,都需要通过通用路径打开游标、查找列信息(knl_get_column_count/knl_get_column),这些元数据在表打开后不会变化,属于重复开销
TLS 句柄池:高并发场景下每个 KV 操作都要 db_alloc/db_free,均需获取全局 handle_pool.lock,spinlock,随线程数增加竞争上升
批量写入:上层逐条调用 Set() 时每条独立提交,无法利用事务批量提交的优势

【实现方案】:
模块: 快速路径
方案: 在 ec_handle_t 中新增 kv_cache(缓存 column_count 和 value_column),gstor_kv_put_fast/gstor_kv_get_fast
内联行构建与值提取
涉及文件: gstor_executor.c/h

模块: TLS 缓存
方案: __thread 变量缓存最多 4 个已清理 handle,db_alloc/db_free 优先命中本地缓存,shutdown 时 db_drain_tls_cache()
归还全局池
涉及文件: storage.c/h, base_storage.cpp/h

模块: 批量写入
方案: 新增 gstor_kv_batch_put 将 key-value 数组包装在单次事务内提交;上层 KvConnection::BatchSet 组装 kv_batch_pair_t
数组后调用
涉及文件: kv_connection.cpp/h, gstor_executor.c/h, intarkdb_kv-c.cpp

模块: 内核修复
方案: 补齐 SESSION_ID_STATS/SESSION_ID_TMP_STAT 的分配和释放,修复 IS_STATS_TABLE_TYPE 宏中不支持的表类型
涉及文件: gstor_handle.c, session_defs.h, knl_rstat.c

【关联需求或issue】:无

【开发自验报告】:

  1. 自验结果:通过 docs/perf/perf_test.c 测试程序验证,单条 PUT 延迟降低约 15-20%,批量写入吞吐提升约 30%,高并发下 TLS
    缓存使锁竞争降低约 40%。详细数据见 docs/perf/perf_report.md。
  2. 是否可以添加 fastcheck 测试用例:否,本次变更不涉及 SQL 层功能,为存储引擎内部路径优化,建议通过 perf_test.c
    进行性能回归验证。
  3. 是否涉及资料修改:否。
  4. 是否考虑升级场景:否
  5. 是否考虑在线扩容等扩展场景:否
  6. 是否考虑异常场景/并发场景/前向兼容/性能场景:
    并发场景:TLS 缓存已考虑线程安全,_thread 变量天然隔离
    前向兼容:新增 API(BatchSet、gstor_kv
    *_fast)与旧接口并存,旧调用路径不受影响
    性能场景:快速路径和 TLS 缓存在单线程下无退化
  7. 是否对其他模块产生影响:否,修改集中在 KV 存储层和 handle 管理
  8. 是否需要回合补丁版本:否
  9. DOLPHIN 宏控制:不涉及

【其他说明】:无

likedislike
合并受阻
画晓月画晓月
5月11日 创建了 pull request,commit 5118ae7f
opengauss_botopengauss_bot成员
5月11日 添加了label:sig/Embedded
opengauss_bot
opengauss_bot成员
5月11日 评论:

Welcome To openGauss Community

Hey @m0_74961486 , thanks for your contribution to the community.

Bot Usage Manual

I'm the Bot here serving you. You can find the instructions on how to interact with me at Here . That means you can comment below every pull request or issue to trigger Bot Commands.

Contact Guide

If you have any questions, please contact the SIG: Embedded ,
and any of the maintainers: @CarrotGo, @chendong76, @chenxiaobin19, @congzhou2603, @dodders, @hwworkholic, @jemappellehc, @muyulinzhong, @quemingjian, @shenzheng4, @shirley_zhengx, @superlchf, @totaj, @wlff234, @ywzq1161327784 ,
and any of the committers: @Louisyzh, @caiwy110, @feihongtaxueni, @guochuang_liyuxiang, @libiao2024, @lizhen29, @telebby, @wufeng0, @zengseliang, @zhangxubo .

likedislike
opengauss_botopengauss_bot成员
5月11日 添加了label:stat/needs-squash
opengauss_botopengauss_bot成员
5月11日 添加了label:codecheck-running
此处折叠了90条消息 查看更多
opengauss_bot
opengauss_bot成员
5月11日 评论:

开源片段扫描

Check Name Build Details Check Result Check Detail
sca #35920 ✅ 通过 >>>
likedislike
opengauss_botopengauss_bot成员
5月11日 添加了label:sca-success
opengauss_bot
opengauss_bot成员
5月11日 评论:

静态检查

Check Name Build Details Check Result Check Detail
CodeCheck #73964 ✅ 通过 >>>
likedislike
opengauss_botopengauss_bot成员
5月11日 添加了label:codecheck-success
opengauss_botopengauss_bot成员
5月11日 删除了label:codecheck-running