在基于 Kubernetes 和 Docker 构建的私有 RDS 中,普遍采用了计算存储分离架构。该架构优势明显, 但对于数据库类 Latency Sensitive 应用而言,IO 性能问题无法回避,下面分享一下我们针对 MySQL 做的优化以及优化后的收益。
计算存储分离架构
架构示意图如下:
存储层由分布式文件系统组成,以 Provisoner 的方式集成到 Kubernetes.
在我们看来, 计算存储分离的最大优势在于:
将有状态的数据下沉到存储层,这使得 RDS 在调度时,无需感知计算节点的存储介质,只需调度到满足计算资源要求的 Node,数据库实例启动时,只需在分布式文件系统挂载 mapping 的 volume 即可,可以显著的提高数据库实例的部署密度和计算资源利用率。
其他的好处还有很多,譬如架构更清晰,扩展更方便,问题定位更简单等,这里不赘述。
计算存储分离架构的缺点
俗话说的好:
上帝为你关上一扇窗的同时,再关上一扇门。
如下图所示
相较本地存储, 网络开销会成为 IO 开销的一部分, 我们认为会带来两个很明显的问题:
以下测试方案的设计,测试数据的梳理来自于沃趣科技MySQL专家@董大爷 和 @波多野老师。
DoubleWrite
在 MySQL 中我们首先想到了 DoubleWrite. 首先看下官方解释,它是干什么的:
The InnoDB doublewrite buffer was implemented to recover from half-written pages. This can happen when there's a power failure while InnoDB is writing a page to disk. On reading that page, InnoDB can discover the corruption from the mismatch of the page checksum. However, in order to recover, an intact copy of the page would be needed.
The double write buffer provides such a copy.
Whenever InnoDB flushes a page to disk, it is first written to the double write buffer. Only when the buffer is safely flushed to disk will InnoDB write the page to the final destination. When recovering, InnoDB scans the double write buffer and for each valid page in the buffer checks if the page in the data file is valid too.
Although data is written twice, the doublewrite buffer does not require twice as much I/O, as data is written to the buffer in a large sequential chunk with a single fsync() call. There is extra time consumed however, and the effect becomes visible with fast storage and a heavy write load.
简单说 DoubleWrite 的实现是防止数据页写入时发生故障导致页损坏(partial write),所以每次写数据文件时都要将一份数据写到共享表空间中,当启动时发现数据页 Checkum 校验不正确时会使用共享表空间中副本进行恢复,从 DoubleWrite 实现来看这部分会产生一定量的 IO .所以:
最好的优化就是减少 IO, 在底层存储介质或文件系统支持 Atomic Write的前提下, 可以关闭MySQL 的 DoubleWrite 以减少 IO。
单机架构 : 关闭DoubleWrite
MariaDB 已支持该功能(底层存储介质需支持 Atomic Write ),并在单机环境做了相关测试。数据如下:
结论:单机环境下,启用Atomic Write(关闭 DoubleWrite )能立即带来30%左右的写性能改善。
原文地址 : http://blog.mariadb.org/mariadb-introduces-atomic-writes/
计算存储分离架构 : 关闭 DoubleWrite
所以, 重点是我们需要测试一下在计算存储分离架构下(分布式存储必须支持 Atomic Write ), 关闭DoubleWrite Buffer 的收益。
测试场景
1、10GB
2、100GB
测试结果:10GB数据量
Sysbench 指标:
分布式文件系统指标:
在计算存储分离架构下, 启用Atomic Write(关闭 DoubleWrite ), 10GB数据量, 因为大部分数据已经缓存到数据库 buffer cache 中, 所以在 IO 不是瓶颈的情况下:
tps ↑0.2656%,qps ↑0.2797%,rst ↑14.9651%
Throughput 下降53%, 显著优化了网络带宽
测试结果:10GB数据量
Sysbench 指标:
分布式文件系统指标:
在计算存储分离架构下, 启用Atomic Write(关闭 DoubleWrite ), 100GB数据量, 因为大部分数据无法缓存到数据库 buffer cache 中, 所以在 IO 是瓶颈的情况下:
TPS ↑28.0892%,QPS ↑28.0893%,RST ↓169.2033%
IOPS 提升22.3%
Latency 下降 39%
在IOPS 提升22.3%的情况下, Throughput 仅多消耗 3.6%
总结
想让数据库安全,高效的运行到 Kubernetes 和 Docker 的架构下并不容易,本文分享的只是众多问题之一,任重而道远……想在上面持续发力的同学,自求多福吧!