已开启
KV 存储性能优化:快速路径、TLS 句柄池缓存与批量写入 #37
画晓月创建于 5月11日
KV 存储性能优化:快速路径、TLS 句柄池缓存与批量写入 #37
已开启
合并受阻
5月11日 添加了label:sig/Embedded
opengauss_bot
5月11日 评论:
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 .


5月11日 添加了label:stat/needs-squash
5月11日 添加了label:codecheck-running
此处折叠了90条消息 查看更多
opengauss_bot
5月11日 评论:
5月11日 评论:
5月11日 添加了label:sca-success
opengauss_bot
5月11日 评论:
5月11日 评论:
5月11日 添加了label:codecheck-success
5月11日 删除了label:codecheck-running
【标题】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】:无
【开发自验报告】:
缓存使锁竞争降低约 40%。详细数据见 docs/perf/perf_report.md。
进行性能回归验证。
并发场景:TLS 缓存已考虑线程安全,_thread 变量天然隔离
前向兼容:新增 API(BatchSet、gstor_kv*_fast)与旧接口并存,旧调用路径不受影响
性能场景:快速路径和 TLS 缓存在单线程下无退化
【其他说明】:无