ENع
→RAID
الدرس 45 · File systems

RAID: spreading data across many disks

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

15 دقيقة12 خطوة5 أسئلةAdvanced
عن هذا الدرس

A file system needs a disk that is big, fast and does not lose data, but every real disk is limited and every disk eventually fails. RAID joins several disks so the system sees one. Follow six blocks across four disks: dealt out by striping, protected by an XOR parity block, updated by a small write that costs four disk operations, rebuilt after a disk dies, and finally rearranged so the parity rotates (RAID 5). The Linux lessons on the file system and disks show how arrays look from the command line; here we look inside the layer that maps every block.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 45 · تفاعلي
01 / 12

Many disks, one disk

One disk has limits: one disk's speed, one disk's size, and when it breaks its data is gone. RAID (a redundant array of inexpensive disks) joins several disks so programs see one. Here the RAID layer joins four.

RAID      redundant array of inexpensive disks
          (Patterson, Gibson and Katz, Berkeley, 1988)
outside   looks like one disk: a row of numbered blocks
inside    several disks + a layer that maps each block

Linux     the md driver (Linux software RAID) joins
          disks into one device, /dev/md0
here      4 disks, 2 rows each, blocks of 4096 bytes
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Many disks, one disk

    One disk has limits: one disk's speed, one disk's size, and when it breaks its data is gone. RAID (a redundant array of inexpensive disks) joins several disks so programs see one. Here the RAID layer joins four.

    RAID      redundant array of inexpensive disks
              (Patterson, Gibson and Katz, Berkeley, 1988)
    outside   looks like one disk: a row of numbered blocks
    inside    several disks + a layer that maps each block
    
    Linux     the md driver (Linux software RAID) joins
              disks into one device, /dev/md0
    here      4 disks, 2 rows each, blocks of 4096 bytes
  2. Striping: deal blocks out

    Striping (RAID 0) deals blocks out one by one. Block b goes to disk b mod 3 (remainder of b ÷ 3) and row b div 3 (whole-number division). Blocks 0-2 fill row 0; blocks 3-5 fill row 1. Disk 3 waits.

    block b  ->  disk = b % 3,  row = b // 3
    
    block 0   disk 0  row 0   bits 0110
    block 1   disk 1  row 0   bits 1100
    block 2   disk 2  row 0   bits 0011
    block 3   disk 0  row 1   bits 1010
    block 4   disk 1  row 1   bits 0101
    block 5   disk 2  row 1   bits 0001
    
    (the figure shows 4 bits of each 4096-byte block)
  3. Faster, but fragile

    Reading blocks 0, 1 and 2 keeps three disks busy at once, so a big read runs up to three times as fast. But nothing is stored twice: if any one disk dies, part of the data is simply gone.

    RAID 0 with N disks (OSTEP, Figure 38.8)
    capacity      N x B blocks     all of it
    sequential    N x S MB/s       every disk at once
    failures      0                one dead disk = data lost
    
    here: blocks 0, 1, 2 come from disks 0, 1, 2 together
  4. Parity: one extra disk

    Disk 3 stores parity: the XOR of each row. XOR gives 1 where a column holds an odd number of 1s. Row 0: 0110 XOR 1100 XOR 0011 = 1001. Now any one lost block can be worked out.

    XOR, bit by bit: odd number of 1s -> 1, even -> 0
    
    row 0   0110  1100  0011   P0 = 1001
    row 1   1010  0101  0001   P1 = 1110
    
    every column of a row now holds an even number of 1s
    RAID 4: data like RAID 0 on 3 disks, parity on disk 3
  5. Line 1: a small write

    Line 1 writes new bits 1001 to block 4, so row 1's parity must change too. First the RAID layer reads two old blocks: block 4 (0101) from disk 1 and parity P1 (1110) from disk 3.

    line 1   pwrite(fd, buf, BS, 4 * BS);
             write block 4 (offset 4 x 4096 bytes)
             block 4 -> disk 1, row 1; its parity P1 is on disk 3
    
    read 1   old block 4   0101   (disk 1)
    read 2   old P1        1110   (disk 3)
  6. New parity, two writes

    New parity = old parity XOR old block XOR new block: 1110 XOR 0101 XOR 1001 = 0010. Then two writes: 1001 to block 4 and 0010 to P1. One small write cost four disk operations.

    Pnew = (Cold XOR Cnew) XOR Pold        (OSTEP 38.1)
         = (0101 XOR 1001) XOR 1110 = 0010
    check: 1010 XOR 1001 XOR 0001 = 0010
    
    write 1  block 4 = 1001   (disk 1)
    write 2  P1      = 0010   (disk 3)
    total    2 reads + 2 writes = 4 disk I/Os
  7. A disk dies

    Disk 1 dies, and blocks 1 and 4 die with it. With striping alone that would be the end. With parity the array keeps running in degraded mode: one disk down, but no data lost yet.

    disk 1   failed: block 1 and block 4 unreadable
    array    degraded: still answers every read and write
             (Linux md marks the device faulty and carries on)
    
    RAID 4 and RAID 5 survive 1 failed disk, no more:
    a second failure now would lose data
  8. Line 2: reading a lost block

    Line 2 reads block 1, which was on the dead disk. The RAID layer reads the rest of row 0 and XORs it: 0110 XOR 0011 XOR 1001 = 1100. That is block 1, rebuilt on the fly.

    line 2   pread(fd, buf, BS, 1 * BS);
             read block 1 -> disk 1, row 0: dead
    
    block 0  0110   (disk 0)
    block 2  0011   (disk 2)
    P0       1001   (disk 3)
    XOR      1100   = block 1, as it was
  9. Rebuilding a new disk

    A new disk replaces disk 1. The RAID layer rebuilds it row by row, each lost block the XOR of the rest of its row: block 1 = 1100, block 4 = 1001. Until it finishes, a second failure would lose data.

    row 0   block 1 = B0 XOR B2 XOR P0
                    = 0110 XOR 0011 XOR 1001 = 1100
    row 1   block 4 = B3 XOR B5 XOR P1
                    = 1010 XOR 0001 XOR 0010 = 1001
    
    Linux md: a hot spare is rebuilt ('recover') from
    the parity and the other data blocks
  10. Parity disk: the bottleneck

    Every small write also updates parity, and in RAID 4 all parity lives on disk 3. Writes to block 0 and block 5 use different data disks, but both need disk 3, so they must wait in line.

    RAID 4 small writes at the same time:
    write block 0   data disk 0   parity disk 3
    write block 5   data disk 2   parity disk 3   <- same disk
    
    the parity disk does 2 I/Os (read + write) per write,
    so random small writes run at R/2 MB/s in total,
    however many disks you add (OSTEP 38.6)
  11. RAID 5: rotate the parity

    RAID 5 moves the parity along, row by row. Row 1's parity goes to disk 2 and its blocks shift: 3 to disk 3, 4 to disk 0, 5 to disk 1. Writes to blocks 0 and 5 now share no disk.

    RAID 5 (OSTEP Figure 38.7 pattern, 4 disks)
            disk 0  disk 1  disk 2  disk 3
    row 0   B0      B1      B2      P0    
    row 1   B4      B5      P1      B3    
    
    write block 0   disks 0 + 3
    write block 5   disks 1 + 2   -> both at once
    random small writes: N/4 x R MB/s (OSTEP 38.7)
  12. Choosing a RAID level

    Each level trades disks for safety and speed. With N disks, RAID 0 keeps all N for data and survives no failure. RAID 1 mirrors, keeping N/2. RAID 4 and RAID 5 keep N - 1 and survive one; RAID 5 spreads the writes.

    with N disks of B blocks (OSTEP Figure 38.8)
              capacity     survives   random write
    RAID 0    N x B        0          N x R
    RAID 1    N x B / 2    1          N/2 x R
    RAID 4    (N-1) x B    1          R/2
    RAID 5    (N-1) x B    1          N/4 x R
    
    the whole lesson as a Python simulation:
    
    from functools import reduce
    from operator import xor
    
    N = 4                               # 3 data disks + 1 parity disk (RAID 4)
    def where(b):                       # logical block -> (disk, row)
        return b % (N - 1), b // (N - 1)
    
    data = [0b0110, 0b1100, 0b0011, 0b1010, 0b0101, 0b0001]
    disks = [[None, None] for _ in range(N)]
    for b, v in enumerate(data):
        d, r = where(b)
        disks[d][r] = v
    for r in range(2):                  # parity = XOR of the row
        disks[N - 1][r] = reduce(xor, (disks[d][r] for d in range(N - 1)))
    
    d, r = where(4)                     # small write: block 4 = 0b1001
    old, p = disks[d][r], disks[N - 1][r]           # 2 reads
    disks[d][r], disks[N - 1][r] = 0b1001, p ^ old ^ 0b1001   # 2 writes
    
    disks[1] = None                     # disk 1 dies
    block1 = reduce(xor, (disks[d][0] for d in (0, 2, 3)))
    print("block 1 =", format(block1, "04b"))
    print("parity  =", [format(disks[3][r], "04b") for r in range(2)])
    
    prints:
    block 1 = 1100
    parity  = ['1001', '0010']

اختبار يتاح بعد الخطوة الأخيرة.