
etcd:增加30%的写入性能
译自:etcd: getting 30% more write/s
每个etcd集群有5个成员,实例型号为m6i.xlarge,最大支持6000 IOPS。每个成员有3个卷:
- root卷
- write-ahead-log的卷
- 数据库卷
测试写性能
首先(在单独的实例上执行执行etcdctl check perf命令,模拟etcd集群的负载,并打印结果。可以通过--load参数来模拟不同大小的集群负载,支持参数为:s(small, m(medium, l(large, xl(xLarge。
s时,测试是通过的。
l时,测试失败。可以看到,集群可执行6.6K/s的写操作,可以认为我们的集群介于中等集群和大型集群之间。
iostat展示的磁盘状态,其中nvme1n1是etcd的write-ahead-log卷,其IO使用率已经达到100%,导致etcd的线程等待IO。
fio --rw=write --ioengine=sync --fdatasync=1 --directory=benchmark --size=22m --bs=2300 --name=sandbox
...
Jobs: 1 (f=1: [W(1][100.0%][w=1594KiB/s][w=709 IOPS][eta 00m:00s]
...
fsync/fdatasync/sync_file_range:
sync (usec: min=476, max=10320, avg=1422.54, stdev=727.83
sync percentiles (usec:
| 1.00th=[ 523], 5.00th=[ 545], 10.00th=[ 570], 20.00th=[ 603],
| 30.00th=[ 660], 40.00th=[ 775], 50.00th=[ 1811], 60.00th=[ 1909],
| 70.00th=[ 1975], 80.00th=[ 2057], 90.00th=[ 2180], 95.00th=[ 2278],
| 99.00th=[ 2671], 99.50th=[ 2933], 99.90th=[ 4621], 99.95th=[ 5538],
| 99.99th=[ 7767]
...
Disk stats (read/write:
nvme1n1: ios=0/21315, merge=0/11364, ticks=0/13865, in_queue=13865, util=99.40%
可以看到fdatasync延迟的99th百分比为 2671 usec (或 2.7ms,说明集群足够快(etcd官方建议最小10ms。从上面的输出还可以看到报告的IOPS为709,相比gp2 EBS 卷宣称的900 IOPS来说并不算低。
升级为GP3
Jobs: 1 (f=1: [W(1][100.0%][w=2482KiB/s][w=1105 IOPS][eta 00m:00s]
...
iops : min= 912, max= 1140, avg=1040.11, stdev=57.90, samples=19
...
fsync/fdatasync/sync_file_range:
sync (usec: min=327, max=5087, avg=700.24, stdev=240.46
sync percentiles (usec:
| 1.00th=[ 392], 5.00th=[ 429], 10.00th=[ 457], 20.00th=[ 506],
| 30.00th=[ 553], 40.00th=[ 603], 50.00th=[ 652], 60.00th=[ 709],
| 70.00th=[ 734], 80.00th=[ 857], 90.00th=[ 1045], 95.00th=[ 1172],
| 99.00th=[ 1450], 99.50th=[ 1549], 99.90th=[ 1844], 99.95th=[ 1975],
| 99.99th=[ 3556]
...
Disk stats (read/write:
nvme2n1: ios=5628/10328, merge=0/29, ticks=2535/7153, in_queue=9688, util=99.09%
可以看到IOPS变为了1105,但远低于预期,通过查看磁盘的使用率,发现瓶颈仍然是EBS卷。
Jobs: 1 (f=1: [W(1][100.0%][w=2535KiB/s][w=1129 IOPS][eta 00m:00s]
...
fsync/fdatasync/sync_file_range:
sync (usec: min=370, max=3924, avg=611.54, stdev=126.78
sync percentiles (usec:
| 1.00th=[ 420], 5.00th=[ 453], 10.00th=[ 474], 20.00th=[ 506],
| 30.00th=[ 537], 40.00th=[ 562], 50.00th=[ 594], 60.00th=[ 635],
| 70.00th=[ 676], 80.00th=[ 717], 90.00th=[ 734], 95.00th=[ 807],
| 99.00th=[ 963], 99.50th=[ 1057], 99.90th=[ 1254], 99.95th=[ 1336],
| 99.99th=[ 2900]
...
可以看到的确遇到了瓶颈,当IOPS规格从900变为3000时,实际IOPS增加了30%,但IOPS规格从3000变为6000时却没有什么变化。
IOPS到哪里去了?
数据库则不同,它需要知道数据写入的时间和地点。假设一个执行EFTPOS(电子钱包转帐交易的数据库被突然重启,仅仅知道数据被"最终"写入是不够的。
事务敏感的应用对I/O延迟比较敏感,适合使用SSD卷。可以通过保持低队列长度和合适的IOPS数量来保持高IOPS,同时降低延迟。持续增加卷的IOPS会导致I/O延迟的增加。
etcd在每个事务之后都会使用一个fdatasync系统调用,这也是为什么在fio命令中指定—fdatasync=1的原因。
fsync(会将文件描述符fd引用的所有(被修改的核心数据刷新到磁盘设备(或其他永久存储设备,这样就可以检索到这些信息(即便系统崩溃或重启。该调用在设备返回前会被阻塞,此外,它还会刷新文件的元数据(参见stat(2
fdatasync( 类似 fsync(,但不会刷新修改后的元数据(除非需要该元数据才能正确处理后续的数据检索。例如,修改st_atime或st_mtime并不会刷新,因为它们不会影响后续数据的读取,但对文件大小(st_size的修改,则需要刷新元数据。
下表展示了各个卷类型的最大性能:
附录
使用Fio来测试Etcd的存储性能
wal_fsync_duration_seconds,etcd建议当99%的指标值均小于10ms时说明存储足够快。可以使用fio来验证etcd的处理速度,在下面命令中,test-data为测试的挂载点目录:
fio --rw=write --ioengine=sync --fdatasync=1 --directory=test-data --size=22m --bs=2300 --name=mytest在命令输出中,只需关注fdatasync的99th百分比是否小于10ms,在本场景中,为2180微秒,说明存储足够快:
fsync/fdatasync/sync_file_range: sync (usec: min=534, max=15766, avg=1273.08, stdev=1084.70 sync percentiles (usec: | 1.00th=[ 553], 5.00th=[ 578], 10.00th=[ 594], 20.00th=[ 627], | 30.00th=[ 709], 40.00th=[ 750], 50.00th=[ 783], 60.00th=[ 1549], | 70.00th=[ 1729], 80.00th=[ 1991], 90.00th=[ 2180], 95.00th=[ 2278], | 99.00th=[ 2376], 99.50th=[ 9634], 99.90th=[15795], 99.95th=[15795], | 99.99th=[15795]注意:
可以根据特定的场景条件
--size和--bs- 在本例中,fio是唯一的I/O,但在实际场景中,除了和
wal_fsync_duration_seconds相关联的写入之外,很可能还会有其他写入存储的操作,因此,如果从fio观察到的99th百分比略低于10ms时,可能并不是因为存储不够快。- fio的版本不能低于3.5,老版本不支持fdatasync
Etcd WALs
因此,在客户端添加或更新key-value存储前,etcd都会将操作记录到WAL,在进一步处理前,etcd必须100%保证WAL表项被持久化。由于存在缓存,因此仅仅使用
write系统调用是不够的。为了保证数据能够写入持久化存储,需要在write之后执行fdatasync系统调用(这也是etcd实际的做法。使用fio访问存储
fdatasync系统调用。为了达到顺序写的目的,需要指定
--rw=write,为了保证fio使用的是write系统调用,而不是其他系统调用(如 pwrite,需要使用--ioengine=sync,最后,为了保证每个write调用之后都执行fdatasync,需要指定--fdatasync=1,另外两个参数--size和--bs需要根据实际情况进行调整。