文章目录
概述
本文是MIT 6.824 Lec4的相关课程笔记。
Primary-Backup Replication
故障恢复
对于分布式系统来说,常见的故障有以下几种:
- fail-stop:server因各种原因停止工作
- Bugs:软件或硬件存在bugs
- environment:地震,停电
replication机制只能处理第一种和第三种故障。
复制策略
有两种常见的复制策略。
- State transfer:primary执行服务,并定时将状态副本发送给backup。
- Replicated state machine:client发送operations给primary,primary将执行的顺序和operations发送给backup,backup与priamry执行相同的operations。
State transter的复制策略比较简单,但是状态的传输非常消耗资源。 Replicated state machine的方式只需要传输少量的数据,但是保证primary和backup操作的一致性需要比较复杂的机制。
Replicated state machine
困难
在采用这种复制方式时,通常需要解决以下几个问题:
- What state to replicate?
- Does primary have to wait for backup?
- When to cut over to backup?
- Are anomalies visible at cut-over?
- How to bring a replacement backup up to speed?
复制级别
- application level:GFS,只是复制应用相关的数据,如数据库表等,非常高效。
- machine level:复制server上发生的所有变动,包括RAM,寄存器,中断等,比较复杂。
VM FT
Overview
- primary将所有的接收到的外部输入发送给backup vm,以保证一致性
- 信息以log entry的形式通过log channel进行传输
- primary和backup共享外部disk server
- 只有priamry需要和disk server进行通信,backup的output会被vmm丢弃
容错机制
在VM FT中,当出现以下几种事件可能会导致primary和backup执行不一致:
- 外部输入(如,network packets),通常需要DMA + 中断来进行处理
- 时钟中断
- 与状态无关的相关操作,如获取当前时间,获取当前设备ID
- 多核并行(不考虑,本文假设的都是单核情况)
在VM FT中,primary和backup的行为不一致可能会导致非常严重的问题。比如我们在VM中运行GFS master服务,primary chunkserver在60s的lease到期之前请求renew lease。在primary vm中,时钟中断发生在renew lease消息之后,因此chunkserver重新续约。如果在backup vm中,时钟中断发生在renew lease之前,此时租约就会过期。如果此时primary vm故障,backup vm接管,它会认为此时没有primary chunkserver,就会重新颁发lease,这样做的后果就是会产生split brain。
因此,backup vm和primary vm必须以相同的顺序和在cpu指令流中的相同位置看到事件的发生。
VM FT中的log entry可能会包含以下数据:
- instruction sequence number
- type
- data
Time Interrupts
下面举例VM FT是如何处理时钟中断的。
Primary:
- FT fields the timer interrupt
- FT reads instruction number from CPU
- FT sends “timer interrupt at instruction X” on logging channel
- FT delivers interrupt to primary, and resumes it (this relies on CPU support to interrupt after the X’th instruction)
BackUp:
- ignores its own timer hardware
- FT sees log entry before backup gets to instruction X
- FT tells CPU to interrupt (to FT) at instruction X
- FT mimics a timer interrupt to backup
Network Packets
下面距离VM TF是如何处理网络包的。
Primary:
- FT tells NIC to copy packet data into FT’s private “bounce buffer”
- At some point NIC does DMA, then interrupts
- FT gets the interrupt
- FT pauses the primary
- FT copies the bounce buffer into the primary’s memory
- FT simulates a NIC interrupt in primary
- FT sends the packet data and the instruction # to the backup
BackUp:
- FT gets data and instruction # from log stream
- FT tells CPU to interrupt (to FT) at instruction X
- FT copies the data to backup memory, simulates NIC interrupt in backup
bounce buffer是一个缓冲区,它可以保证primary vm和backup vm在收到input时,不会因为DMA机制导致数据主线在内存中的时间不一样。
non-deterministic instructions
下面距离VM FT时如何处理non-deterministic instructions的。
Primary:
- FT sets up the CPU to interrupt if primary executes such an instruction
- FT executes the instruction and records the result
- sends result and instruction # to backup
BackUp:
- FT reads log entry, sets up for interrupt at instruction #
- FT then supplies value that the primary got
output
对于output操作,primary vm和backup vm都会产生输出,但只有primary vm的output有效,primary vm的output会被丢弃。
我们假设一个DB Server的例子来更好的立即VM FT的output机制,假设primary vm上部署了一个DB Server,并存储有数据10,client支持increment操作。client向primary vm发起increment操作,primary收到后将input发送给backup vm,并更新自己的数据为11,将结果返回给client。backup vm收到log entry后也执行increment操作,将自己的数据变为11,并产生output(被hypervisor丢弃)。
问题:如果primary发送了output之后宕机,并且log channel出现异常,backup vm没有收到log entry。此时backup vm接管primary,但是其内存中的数据为10,而不是11,出现不一致的问题。
解决方案:output rule,primary 必须收到 backup 的ack后才能产生output
场景一:primary在收到ack前故障
FT流程:backup vm在replay完最后一个log entry时成为primary vm,并将output发送到client,通信正常。
场景二:primary在发送output后故障
FT流程:backup vm在接管后会再产生一次output,所以会产生两次output。对于TCP连接来说,由于primary和back的状态一样,使用sequence number也一样,因此会进行重复数据包处理。对于disk读写来说,会在同一个地方覆盖写,因此都不会产生问题。