FlashCache呢是Facebook技術團隊的又一力作,最初是為加速MySQL設計的。
Flashcache是在Linux層面的,所以任何受磁盤IO困繞的軟件或應用都可以方便的使用。
為什么是用於加速MySqL呢,這個就涉及到數據庫的專業知識了,傳統磁盤的隨機寫隨機讀效率都相當低下,需要進行機械化的尋道讀取,盡管很多學術論文對數據庫優化中都曾經在這些方面大做文章。那么SSD的存在似乎很好的解決了隨機讀寫的問題,因為SSD是固態存儲器,即不存在機械尋道和磁化問題,而是通過電位標注01,隨機讀寫效率都相當優秀。
但是SSD有個先天性缺陷,那就是寫入數據之前必須先進行擦除,這是為什么呢,這和SSd的原理有關,電位的轉化問題,就是先都空間全部置位,在進行加電變化每個cell的正確電位,那么勢必會影響SSd的壽命,相當於一次寫轉化為了兩次寫。如果SSD的使用沒有進過良好的優化,那么會SSD的使用壽命將會受限,因此損耗均衡算法也是SSD研究關注的重點,貌似現在應都有了成熟的解決方案。
盡管SSd設備都具備了良好的損耗均衡,但是使用者都是熟知SSD的缺陷,所以不得不小心應對,因此SSD在存儲中的位置,通常作為讀緩存,用於提高系統的IO性能。FlashCache便是以此為目的的使用SSd設備。
FlashCache是Linux的一個模塊,可以動態地加載在Linux中。
Flashcache通過在文件系統(VFS)和設備驅動之間新增了一次緩存層,來實現對熱點數據的緩存。用SSD作為緩存,通過將傳統硬盤上的熱門數據緩存到SSD上,然后利用SSD優秀的讀性能,來加速系統。這個方法較之內存緩存,沒有內存快,但是空間可以比內存大很多。(SSd的價格高於磁盤,低於內存)
圖示:
/dev/sdb是SSD設備,/dev/sda是傳統的磁盤設備,加載了Flashcache之后呢,會將這兩個設備虛擬化為一個帶有緩存的塊設備/dev/mapper/cachedev
好了,廢話說完,我們開始實際操作。
1、環境准備和安裝所需軟件包
# cat /etc/issue CentOS release 6.4 (Final) Kernel \r on an \m
yum -y update rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm yum -y install dkms gcc make yum-utils kernel kernel-devel git make gcc-c++ device-mapper
yum -y install unifdef.x86_64
同時,flashcache需要整個源碼樹,查看一下
[root@localhost ~]# ll /usr/src/kernels/2.6.32-431.29.2.el6.x86_64/ 總用量 3288 drwxr-xr-x. 25 root root 4096 10月 22 15:05 arch drwxr-xr-x. 2 root root 4096 10月 22 15:05 block drwxr-xr-x. 5 root root 4096 10月 22 15:05 crypto drwxr-xr-x. 92 root root 4096 10月 22 15:05 drivers drwxr-xr-x. 3 root root 4096 10月 22 15:05 firmware drwxr-xr-x. 71 root root 4096 10月 22 15:05 fs drwxr-xr-x. 21 root root 4096 10月 22 15:05 include drwxr-xr-x. 2 root root 4096 10月 22 15:05 init drwxr-xr-x. 2 root root 4096 10月 22 15:05 ipc drwxr-xr-x. 8 root root 4096 10月 22 15:05 kernel drwxr-xr-x. 7 root root 4096 10月 22 15:05 lib -rw-r--r--. 1 root root 57000 9月 10 06:02 Makefile -rw-r--r--. 1 root root 1152 9月 10 06:02 Makefile.common drwxr-xr-x. 2 root root 4096 10月 22 15:05 mm -rw-r--r--. 1 root root 697350 9月 10 06:02 Module.symvers drwxr-xr-x. 49 root root 4096 10月 22 15:05 net drwxr-xr-x. 6 root root 4096 10月 22 15:05 samples drwxr-xr-x. 12 root root 4096 10月 22 15:05 scripts drwxr-xr-x. 7 root root 4096 10月 22 15:05 security drwxr-xr-x. 21 root root 4096 10月 22 15:05 sound -rw-r--r--. 1 root root 2519815 9月 10 06:02 System.map drwxr-xr-x. 7 root root 4096 10月 22 15:05 tools drwxr-xr-x. 2 root root 4096 10月 22 15:05 usr drwxr-xr-x. 3 root root 4096 10月 22 15:05 virt
如果沒有的話下載放到指定位置
wget http://ftp.redhat.com/pub/redhat/linux/enterprise/6Server/en/os/SRPMS/kernel-2.6.32-358.el6.src.rpm
進入flashcache目錄編譯
# cd flashcache
# make KERNEL_TREE=/usr/src/kernels/2.6.32-431.29.2.el6.x86_64/
沒有報錯的話繼續
make install
出現錯誤:
[root@localhost flashcache]# make install make -C src KERNEL_TREE=/lib/modules/2.6.32-358.el6.x86_64/build PWD=/root/flashcache/src install make[1]: Entering directory `/root/flashcache/src' make -C /lib/modules/2.6.32-358.el6.x86_64/build M=/root/flashcache/src modules V=0 make: Entering an unknown directory make: *** /lib/modules/2.6.32-358.el6.x86_64/build: 沒有那個文件或目錄。 停止。 make: Leaving an unknown directory make[1]: *** [modules] 錯誤 2 make[1]: Leaving directory `/root/flashcache/src' make: *** [install] 錯誤 2
build 是一個鏈接文件,並且指向的地址不存在。
我發現/usr/src/kernels/目錄下面是開始是沒有內容的,對於centos來說,內核源碼是放在 /usr/src/kernels 目錄中;如果通過在線升級內核,也是放在這個目錄中;如果您的系統中的 /usr/src/kernels/ 中沒有內容,說明沒有安裝內核的源碼包 kernel-devel 軟件包;
通過在線安裝的內核源碼包 ,比如通過 apt+synaptic 或者yum 安裝的,內核源碼會被放到/usr/src/kernel 下的目錄中,您要進入相對應的目錄進行編譯; 在這里我是通過yum安裝的。
yum -y install kernel
yum -y install kernel-devel
因為我已經安裝了源碼包,所有出現:
[root@localhost ~]# yum -y install kernel-devel Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.hustunique.com * epel: mirrors.hustunique.com * extras: mirrors.hustunique.com * updates: mirrors.hustunique.com Setting up Install Process Package kernel-devel-2.6.32-431.29.2.el6.x86_64 already installed and latest version Nothing to do
[root@localhost ~]# cd /lib/modules/2.6.32-358.el6.x86_64/ [root@localhost 2.6.32-358.el6.x86_64]# ln -s /usr/src/kernels/2.6.32-431.29.2.el6.x86_64/ build ln: 創建符號鏈接 "build": 文件已存在
使build指向剛/usr/src/kernels中剛安裝的源碼包,出現錯誤
必須要先刪除才能添加新的鏈接
# rm -rf /lib/modules/2.6.32-358.el6.x86_64/build
重新建立符號鏈接:
[root@localhost 2.6.32-358.el6.x86_64]# ln -s /usr/src/kernels/2.6.32-431.29.2.el6.x86_64/ /lib/modules/2.6.32-358.el6.x86_64/build
之后就可以進入flashcache目錄
[root@localhost flashcache]# make install
[root@localhost flashcache]# make install make -C src KERNEL_TREE=/lib/modules/2.6.32-358.el6.x86_64/build PWD=/root/flashcache/src install make[1]: Entering directory `/root/lashcache/src' make -C /lib/modules/2.6.32-358.el6.x86_64/build M=/root/flashcache/src modules V=0 make[2]: Entering directory `/usr/src/kernels/2.6.32-431.29.2.el6.x86_64' CC [M] /root/flashcache/src/flashcache_conf.o CC [M] /root/flashcache/src/flashcache_main.o CC [M] /root/flashcache/src/flashcache_subr.o CC [M] /root/flashcache/src/flashcache_ioctl.o CC [M] /root/flashcache/src/flashcache_procfs.o CC [M] /root/flashcache/src/flashcache_reclaim.o CC [M] /root/flashcache/src/flashcache_kcopy.o LD [M] /root/flashcache/src/flashcache.o Building modules, stage 2. MODPOST 1 modules CC /root/flashcache/src/flashcache.mod.o LD [M] /root/flashcache/src/flashcache.ko.unsigned NO SIGN [M] /root/flashcache/src/flashcache.ko make[2]: Leaving directory `/usr/src/kernels/2.6.32-431.29.2.el6.x86_64' install -o root -g root -m 0755 -d /lib/modules/2.6.32-358.el6.x86_64/extra/flashcache/ install -o root -g root -m 0755 flashcache.ko /lib/modules/2.6.32-358.el6.x86_64/extra/flashcache/ depmod -a 2.6.32-358.el6.x86_64 make -C utils all make[2]: Entering directory `/root/flashcache/src/utils' make[2]: Nothing to be done for `all'. make[2]: Leaving directory `/root/flashcache/src/utils' make -C utils install make[2]: Entering directory `/root/flashcache/src/utils' install -d -m 755 /sbin/ install -m 755 flashcache_create flashcache_destroy flashcache_load flashcache_setioctl get_agsize /sbin/ make[2]: Leaving directory `/root/flashcache/src/utils' make -C ocf install make[2]: Entering directory `/root/flashcache/src/ocf' install -d -m 755 /usr/lib/ocf/resource.d/flashcache install -m 755 flashcache /usr/lib/ocf/resource.d/flashcache make[2]: Leaving directory `/root/flashcache/src/ocf' make[1]: Leaving directory `/rootflashcache/src'
加載模塊
modprobe flashcache
內核一直顯示報錯:
Oct 22 15:57:49 localhost kernel: flashcache: disagrees about version of symbol dm_kcopyd_client_create Oct 22 15:57:49 localhost kernel: flashcache: Unknown symbol dm_kcopyd_client_create Oct 22 15:57:49 localhost kernel: flashcache: disagrees about version of symbol dm_kcopyd_copy Oct 22 15:57:49 localhost kernel: flashcache: Unknown symbol dm_kcopyd_copy Oct 22 15:57:49 localhost kernel: flashcache: disagrees about version of symbol dm_kcopyd_client_destroy Oct 22 15:57:49 localhost kernel: flashcache: Unknown symbol dm_kcopyd_client_destroy Oct 22 16:02:12 localhost kernel: flashcache: disagrees about version of symbol dm_kcopyd_client_create Oct 22 16:02:12 localhost kernel: flashcache: Unknown symbol dm_kcopyd_client_create Oct 22 16:02:12 localhost kernel: flashcache: disagrees about version of symbol dm_kcopyd_copy Oct 22 16:02:12 localhost kernel: flashcache: Unknown symbol dm_kcopyd_copy Oct 22 16:02:12 localhost kernel: flashcache: disagrees about version of symbol dm_kcopyd_client_destroy Oct 22 16:02:12 localhost kernel: flashcache: Unknown symbol dm_kcopyd_client_destroy
最后沒解決,重新編譯了內核源碼linux-2.6.32,重新編譯flashcache源碼
[root@localhost flashcache]# make KERNEL_TREE=/usr/src/linux-2.6.32/
[root@localhost flashcache]# make install
[root@localhost flashcache]# modprobe flashcache
[root@localhost flashcache]# dmesg | tail
說明成功加載模塊
好了,現在我們可以開始做磁盤加速了,哈哈
例如我的ssd硬盤是/dev/sdb1,要對/dev/md127加速,/dev/sdb1是SSD。
注意:flashcache掛載不能寫到fstab,不然啟動的時候會報錯
創建raid5:
[root@localhost flashcache]# mdadm -C /dev/md127 -l5 -n3 /dev/sdc1 /dev/sdd1 /dev/sde1
flashcache配置
1.首次創建Flashcach設備
請注意,設備上的文件將會被清空 首先確保hdd的分區沒有被掛載,如果掛載了,卸載之 [root@localhost flashcache]# flashcache_create -p back cachedev /dev/sdd1 /dev/md127 cachedev cachedev, ssd_devname /dev/sdd1, disk_devname /dev/md127 cache mode WRITE_BACK block_size 8, md_block_size 8, cache_size 0 Flashcache metadata will use 549MB of your 15901MB main memory
使用fdisk -l
Disk /dev/mapper/cachedev: 107.4 GB, 107380998144 bytes 255 heads, 63 sectors/track, 13054 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 524288 bytes / 1048576 bytes Disk identifier: 0x00000000
2.使用該設備
[root@localhost flashcache]# mkfs.ext2 /dev/mapper/cachedev
[root@localhost flashcache]# mount /dev/mapper/cachedev /root/test
3.如何重建flashcache
[root@localhost flashcache]# umount /root/test
[root@localhost flashcache]# dmsetup remove cachedev
[root@localhost flashcache]# flashcache_destroy /dev/sdd1
4.開機自動加載
開機加載flashcache模塊,把下述腳本放到/etc/sysconfig/modules/目錄中並賦可執行權限 flashcache.modules #!/bin/sh MODULES="flashcache" for i in $MODULES ; do modprobe $1 >/dev/null 2>&1 done 開機加載flashcache盤 在rc.local里添加 flashcache_load /dev/md127 mount /dev/mapper/cachedev /root/test
5.查詢狀態
dmsetup status cachedev
dmsetup table cachedev
dmsetup info cachedev
flashcache測試
使用fio進行測試: