^v^

知行合一 止于至善

Bcache使用介绍

12 Dec 2017 » 存储


bcache 介绍

Hard drives are cheap and big, SSDs are fast but small and expensive. Wouldn’t it be nice if you could transparently get the advantages of both? With Bcache, you can have your cake and eat it too.

从价格、容量、性能方面,HDD盘和SSD盘恰好可以互补。 HDD盘价格便宜、容量大、性能低,SSD价格贵、容量小、性能高。

By default it won’t cache sequential IO, just the random reads and writes that SSDs excel at.

Bcache默认不缓存顺序IO,主要考虑两方面: 首先,顺序IO写HDD盘,没有寻道,速度本身就快; 其次,顺序IO快速占用稀缺的SSD资源,影响后续随机IO。

数据安全,掉电不丢数据。

Bcache is designed around the performance characteristics of SSDs. It’s designed to minimize write inflation to the greatest extent possible, and never itself does random writes. It turns random writes into sequential writes - first when it writes them to the SSD, and then with writeback caching it can use your SSD to buffer gigabytes of writes and write them all out in order to your hard drive or raid array

Bcache将数据从SSD盘刷入HDD盘时,会将随机写IO转换为顺序写,依次提高写HDD盘的性能。

概念

  1. cache set
  2. cache 设备
  3. backing 设备
  4. block
  5. bucket

特性

A single cache device can be used to cache an arbitrary number of backing devices, and backing devices can be attached and detached at runtime, while mounted and in use (they run in passthrough mode when they don’t have a cache).

  1. 允许一块SSD盘带多块HDD盘,允许HDD盘在运行时被attach和dettach,detach后HDD盘走passthrough模式。

  2. 支持Writethrough, writeback, writearound三种缓存模式。

Detects and bypasses sequential IO (with a configurable threshold, and can be disabled).

  1. 检测并绕过顺序IO。

Throttles traffic to the SSD if it becomes congested, detected by latency to the SSD exceeding a configurable threshold (useful if you’ve got one SSD for many disks).

  1. 流控。

Readahead on cache miss (disabled by default).

  1. 预读(默认禁用)
  2. 稳定,生产环境中使用。

bcache 使用

编译安装

下载bcache-tools源码,编译安装:

git clone https://github.com/g2p/bcache-tools.git

检查bcache模块:

nbs@pubbeta1-nova105:~$ sudo modprobe bcache
nbs@pubbeta1-nova105:~$ sudo lsmod | grep bcache
bcache                193649  0

Format cache设备

使用make-bcache工具分别对用作cache设备的SSD盘和用作backing设备的HDD盘进行格式化。

nbs@pubbeta1-nova105:~$ ls /sys/fs/bcache/
register  register_quiet
nbs@pubbeta1-nova105:~$ ls /sys/block/
sda  sdb  sdc  sdd  sde  sdf  sdg  sdh  sdi  sdj  sdk


nbs@pubbeta1-nova105:~$ sudo make-bcache -C /dev/sdk
UUID         : 81c56dcc-2ab0-44e5-933f-22102aca198a
Set UUID     : 00615c9a-91cc-4b54-8d48-3b0e0044e80b
version      : 0
nbuckets     : 3051264
block_size   : 1
bucket_size  : 1024
nr_in_set    : 1
nr_this_dev  : 0
first_bucket : 1
nbs@pubbeta1-nova105:~$ ls /sys/fs/bcache/
00615c9a-91cc-4b54-8d48-3b0e0044e80b  register  register_quiet

Format backing设备

nbs@pubbeta1-nova105:~$ sudo make-bcache -B /dev/sdj
UUID        : 3b860b17-481d-4921-8a87-55c4279df52e
Set UUID    : 4facf1d6-5cc9-4f8b-998c-0490d9cd3c83
version     : 1
block_size  : 1
data_offset : 16
nbs@pubbeta1-nova105:~$ lsblk
NAME      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdj         8:144  0  1.5T  0 disk
└─bcache0 253:0    0  1.5T  0 disk
sdk         8:160  0  1.5T  0 disk

/dev目录下生成一个和sdj盘相同大小的块设备bcache0。

nbs@pubbeta1-nova105:~$ ls -l /sys/block/
lrwxrwxrwx 1 root root 0 Dec 19 14:57 bcache0 -> ../devices/virtual/block/bcache0

nbs@pubbeta1-nova105:~$ ls /dev/bcache/by-uuid/ -l
lrwxrwxrwx 1 root root 13 Dec 19 14:57 3b860b17-481d-4921-8a87-55c4279df52e -> ../../bcache0

backing设备

可以在以下几个地方修改backing设备的参数:

  1. /sys/block/<bdev>/bcache
  2. /sys/block/bcache*/bcache
  3. /sys/fs/bcache/<cset-uuid>/bdev* 在这个目录修改的前提条件是设备已attach。

attach

将backing设备加入到cache set。 执行echo <CSET-UUID> > /sys/block/bcache0/bcache/attach命令,将backing设备加入到cache set。

echo 00615c9a-91cc-4b54-8d48-3b0e0044e80b > /sys/block/bcache0/bcache/attach

注意权限问题,本例中使用root权限才可以添加。

cache_mode

默认的缓存模式为writethrough,还可以设置为writeback、writearound或者none。

查看给定设备的缓存模式:

nbs@pubbeta1-nova105:~$ cat /sys/block/bcache0/bcache/cache_mode
[writethrough] writeback writearound none

修改给定设备的缓存模式:

echo writeback > /sys/block/bcache0/bcache/cache_mode

detach

从cache set中删除指定的设备,如果cache中有数据,那么会先flush数据。

dirty_data

root@pubbeta1-nova105:~# cat /sys/block/bcache0/bcache/dirty_data
22.6M

查看给定backing设备在cache中的脏数据。

readahead

预读,默认为0。

running

1 if bcache is running.

sequential_cutoff

A sequential IO will bypass the cache once it passes this threshold; the most recent 128 IOs are tracked so sequential IO can be detected even when it isn’t all done at once

顺序IO的阈值,默认4MB。当顺序IOs的大小超过cutoff时,后续的顺序IO会绕过cache设备直接写backing设备。 如果要禁用绕过顺序IO的功能,设置该值为0。

state

backing 设备有以下4中状态:

  1. no cache:没有attach到cache set;
  2. clean: attach到cache set,但cache中没有脏数据;
  3. dirty: attach到cache set,cache中有脏数据;
  4. inconsistent: backing设置正在被使用,但cache set不可用,并且cache中有脏数据。

writeback_delay

默认30秒。 ?

writeback_percent

If nonzero, bcache tries to keep around this percentage of the cache dirty by throttling background writeback and using a PD controller to smoothly adjust the rate.

writeback_rate

writeback_running

If off, writeback of dirty data will not take place at all. Dirty data will still be added to the cache until it is mostly full; only meant for benchmarking. Defaults to on.

stats

最近5分钟、1小时、1天的统计数据。例如最近5分钟的统计数据在/sys/block/bcache0/bcache/stats_five_minute目录:

  1. bypassed。绕过的IO的数量,同时包括read和write。
  2. cache_hits, cache_misses, cache_hit_ratio。缓存命令、未命中、命中比例。
  3. cache_bypass_hits, cache_bypass_misses。应该被绕过的IO,在cache中的命中情况。
  4. cache_miss_collisions。?
  5. cache_readaheads。预读的次数。

cache 设备

cache设备的配置在/sys/block/<cdev>/bcache目录。

block_size

写的最小粒度,应该设置为硬件扇区的整数倍。

btree_written

bucket_size

bucket的大小。

nbuckets

cache中总的buckets数量。

cache_replacement_policy

One of either lru, fifo or random

discard

Boolean; if on a discard/TRIM will be issued to each bucket before it is reused. Defaults to off, since SATA TRIM is an unqueued command (and thus slow)

freelist_percent

priority_stats

written

Sum of all data that has been written to the cache; comparison with btree_written gives the amount of write inflation in bcache.

cache set

cache set的配置位于/sys/fs/bcache/<cset-uuid>目录。

average_key_size

average data per key in the btree.

bdev<0..n>

软连接到已经attach的backing设备。

cache<0..n>

软连接到cache设备。

block_size

cache设备的block大小。

btree_cache_size

btree占用的内存大小。

bucket_size

bucket的大小。

cache_available_percent

clear_stats

清空统计数据。

dirty_data

cache中脏数据的数量。

flash_vol_create

创建1个卷。

unregister

detach所有backing设备,关闭cache设备。如果有脏数据,先disable writeback,再flush数据。

错误处理 Error Handling

  1. 对从cache设备的read操作,如果发生错误,将从backing设备中重试read操作。
  2. 对writethrough的write操作,如果写cache失败,将在cache设备中将对应的数据标记为失效状态。对bypass的write操作,也是同样的做法。
  3. 对writeback的write操作,如果发生错误,将错误返回给上层文件系统或用户空间。
  4. detach设备时,如果是writeback模式,则先flush脏数据到backing设备。

性能调优

缓存模式

bcache默认使用writethrough缓存模式,如果性能没有达到期望,修改缓存模式为writeback。 writeback模式的问题是,一旦SSD故障,将会丢失数据。

绕过顺序IO

参考sequential_cutoff参数。

测试随机读前希望先顺序写8G数据,要求这些数据在缓存中,此时需要先禁用绕过顺序io的功能。

流控

Cache设备可能会成为性能瓶颈,尤其在SSD盘性能比较差,带多块HDD盘,并且大部分都是顺序IO的情况。 为避免这种情况,bcache会track读写cache设备的IO的延迟,当延迟超过给定的阈值时,就逐步地实施流控。 流控的手段是降低旁路的顺序IO。

跟踪IO延迟就影响性能,如果对SSD盘性能比较自信,可以关闭。

echo 0 > /sys/fs/bcache/<cache set>/congested_read_threshold_us
echo 0 > /sys/fs/bcache/<cache set>/congested_write_threshold_us

read操作的默认值为2ms,write操作的默认值为20ms。

bcache应用到ceph

部署问题

扩展ceph-deploy

TBD

磁盘布局

journal omap data cache

监控问题

监控每块盘cache的使用情况。

运维问题

SSD盘故障处理 HDD盘故障处理

SSD&HDD不支持热插拔 将HDD从缓冲池中卸载时需要等待脏数据全部刷完,时间较长。 当HDD损坏时,SSD中对应的脏数据无法清除,造成空间浪费。 Thin-flash卷在系统重启后无法恢复。

Bcache元数据缓存对内存消耗较大,当系统内存不足时会导致大量数据无法缓存,需要从SSD读取,影响性能。

参考资料

  1. https://bcache.evilpiepirate.org/
  2. linux-flashcache-and-bcache-performance-testing
  3. http://www.accelcloud.com/
  4. https://www.kernel.org/doc/Documentation/bcache.txt
  5. Ceph OSDs with bcache experience
  6. 使用bcache为Ceph OSD加速的具体实践
  7. bcache浅析
  8. https://en.wikipedia.org/wiki/Bcache
  9. https://www.redhat.com/archives/dm-devel/2013-June/msg00026.html
  10. PERFORMANCE COMPARISON AMONG dm-cache, bcache and EnhanceIO

Related Posts