1*4882a593Smuzhiyun================ 2*4882a593SmuzhiyunRAID 4/5/6 cache 3*4882a593Smuzhiyun================ 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunRaid 4/5/6 could include an extra disk for data cache besides normal RAID 6*4882a593Smuzhiyundisks. The role of RAID disks isn't changed with the cache disk. The cache disk 7*4882a593Smuzhiyuncaches data to the RAID disks. The cache can be in write-through (supported 8*4882a593Smuzhiyunsince 4.4) or write-back mode (supported since 4.10). mdadm (supported since 9*4882a593Smuzhiyun3.4) has a new option '--write-journal' to create array with cache. Please 10*4882a593Smuzhiyunrefer to mdadm manual for details. By default (RAID array starts), the cache is 11*4882a593Smuzhiyunin write-through mode. A user can switch it to write-back mode by:: 12*4882a593Smuzhiyun 13*4882a593Smuzhiyun echo "write-back" > /sys/block/md0/md/journal_mode 14*4882a593Smuzhiyun 15*4882a593SmuzhiyunAnd switch it back to write-through mode by:: 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun echo "write-through" > /sys/block/md0/md/journal_mode 18*4882a593Smuzhiyun 19*4882a593SmuzhiyunIn both modes, all writes to the array will hit cache disk first. This means 20*4882a593Smuzhiyunthe cache disk must be fast and sustainable. 21*4882a593Smuzhiyun 22*4882a593Smuzhiyunwrite-through mode 23*4882a593Smuzhiyun================== 24*4882a593Smuzhiyun 25*4882a593SmuzhiyunThis mode mainly fixes the 'write hole' issue. For RAID 4/5/6 array, an unclean 26*4882a593Smuzhiyunshutdown can cause data in some stripes to not be in consistent state, eg, data 27*4882a593Smuzhiyunand parity don't match. The reason is that a stripe write involves several RAID 28*4882a593Smuzhiyundisks and it's possible the writes don't hit all RAID disks yet before the 29*4882a593Smuzhiyununclean shutdown. We call an array degraded if it has inconsistent data. MD 30*4882a593Smuzhiyuntries to resync the array to bring it back to normal state. But before the 31*4882a593Smuzhiyunresync completes, any system crash will expose the chance of real data 32*4882a593Smuzhiyuncorruption in the RAID array. This problem is called 'write hole'. 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunThe write-through cache will cache all data on cache disk first. After the data 35*4882a593Smuzhiyunis safe on the cache disk, the data will be flushed onto RAID disks. The 36*4882a593Smuzhiyuntwo-step write will guarantee MD can recover correct data after unclean 37*4882a593Smuzhiyunshutdown even the array is degraded. Thus the cache can close the 'write hole'. 38*4882a593Smuzhiyun 39*4882a593SmuzhiyunIn write-through mode, MD reports IO completion to upper layer (usually 40*4882a593Smuzhiyunfilesystems) after the data is safe on RAID disks, so cache disk failure 41*4882a593Smuzhiyundoesn't cause data loss. Of course cache disk failure means the array is 42*4882a593Smuzhiyunexposed to 'write hole' again. 43*4882a593Smuzhiyun 44*4882a593SmuzhiyunIn write-through mode, the cache disk isn't required to be big. Several 45*4882a593Smuzhiyunhundreds megabytes are enough. 46*4882a593Smuzhiyun 47*4882a593Smuzhiyunwrite-back mode 48*4882a593Smuzhiyun=============== 49*4882a593Smuzhiyun 50*4882a593Smuzhiyunwrite-back mode fixes the 'write hole' issue too, since all write data is 51*4882a593Smuzhiyuncached on cache disk. But the main goal of 'write-back' cache is to speed up 52*4882a593Smuzhiyunwrite. If a write crosses all RAID disks of a stripe, we call it full-stripe 53*4882a593Smuzhiyunwrite. For non-full-stripe writes, MD must read old data before the new parity 54*4882a593Smuzhiyuncan be calculated. These synchronous reads hurt write throughput. Some writes 55*4882a593Smuzhiyunwhich are sequential but not dispatched in the same time will suffer from this 56*4882a593Smuzhiyunoverhead too. Write-back cache will aggregate the data and flush the data to 57*4882a593SmuzhiyunRAID disks only after the data becomes a full stripe write. This will 58*4882a593Smuzhiyuncompletely avoid the overhead, so it's very helpful for some workloads. A 59*4882a593Smuzhiyuntypical workload which does sequential write followed by fsync is an example. 60*4882a593Smuzhiyun 61*4882a593SmuzhiyunIn write-back mode, MD reports IO completion to upper layer (usually 62*4882a593Smuzhiyunfilesystems) right after the data hits cache disk. The data is flushed to raid 63*4882a593Smuzhiyundisks later after specific conditions met. So cache disk failure will cause 64*4882a593Smuzhiyundata loss. 65*4882a593Smuzhiyun 66*4882a593SmuzhiyunIn write-back mode, MD also caches data in memory. The memory cache includes 67*4882a593Smuzhiyunthe same data stored on cache disk, so a power loss doesn't cause data loss. 68*4882a593SmuzhiyunThe memory cache size has performance impact for the array. It's recommended 69*4882a593Smuzhiyunthe size is big. A user can configure the size by:: 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun echo "2048" > /sys/block/md0/md/stripe_cache_size 72*4882a593Smuzhiyun 73*4882a593SmuzhiyunToo small cache disk will make the write aggregation less efficient in this 74*4882a593Smuzhiyunmode depending on the workloads. It's recommended to use a cache disk with at 75*4882a593Smuzhiyunleast several gigabytes size in write-back mode. 76*4882a593Smuzhiyun 77*4882a593SmuzhiyunThe implementation 78*4882a593Smuzhiyun================== 79*4882a593Smuzhiyun 80*4882a593SmuzhiyunThe write-through and write-back cache use the same disk format. The cache disk 81*4882a593Smuzhiyunis organized as a simple write log. The log consists of 'meta data' and 'data' 82*4882a593Smuzhiyunpairs. The meta data describes the data. It also includes checksum and sequence 83*4882a593SmuzhiyunID for recovery identification. Data can be IO data and parity data. Data is 84*4882a593Smuzhiyunchecksumed too. The checksum is stored in the meta data ahead of the data. The 85*4882a593Smuzhiyunchecksum is an optimization because MD can write meta and data freely without 86*4882a593Smuzhiyunworry about the order. MD superblock has a field pointed to the valid meta data 87*4882a593Smuzhiyunof log head. 88*4882a593Smuzhiyun 89*4882a593SmuzhiyunThe log implementation is pretty straightforward. The difficult part is the 90*4882a593Smuzhiyunorder in which MD writes data to cache disk and RAID disks. Specifically, in 91*4882a593Smuzhiyunwrite-through mode, MD calculates parity for IO data, writes both IO data and 92*4882a593Smuzhiyunparity to the log, writes the data and parity to RAID disks after the data and 93*4882a593Smuzhiyunparity is settled down in log and finally the IO is finished. Read just reads 94*4882a593Smuzhiyunfrom raid disks as usual. 95*4882a593Smuzhiyun 96*4882a593SmuzhiyunIn write-back mode, MD writes IO data to the log and reports IO completion. The 97*4882a593Smuzhiyundata is also fully cached in memory at that time, which means read must query 98*4882a593Smuzhiyunmemory cache. If some conditions are met, MD will flush the data to RAID disks. 99*4882a593SmuzhiyunMD will calculate parity for the data and write parity into the log. After this 100*4882a593Smuzhiyunis finished, MD will write both data and parity into RAID disks, then MD can 101*4882a593Smuzhiyunrelease the memory cache. The flush conditions could be stripe becomes a full 102*4882a593Smuzhiyunstripe write, free cache disk space is low or free in-kernel memory cache space 103*4882a593Smuzhiyunis low. 104*4882a593Smuzhiyun 105*4882a593SmuzhiyunAfter an unclean shutdown, MD does recovery. MD reads all meta data and data 106*4882a593Smuzhiyunfrom the log. The sequence ID and checksum will help us detect corrupted meta 107*4882a593Smuzhiyundata and data. If MD finds a stripe with data and valid parities (1 parity for 108*4882a593Smuzhiyunraid4/5 and 2 for raid6), MD will write the data and parities to RAID disks. If 109*4882a593Smuzhiyunparities are incompleted, they are discarded. If part of data is corrupted, 110*4882a593Smuzhiyunthey are discarded too. MD then loads valid data and writes them to RAID disks 111*4882a593Smuzhiyunin normal way. 112