一,租约机制介绍html
在分布式系统中,每每会有一个中心服务器节点。该节点负责存储、维护系统中的元数据。若是系统中的各类操做都依赖于中心服务器上的元数据,那么中心服务器很容易成为性能瓶颈及存在单点故障。而经过租约机制,能够将中心服务器的“权力”下放给其余机器,就能够减轻中心服务器的压力。固然,租约机制还有许多其余的用途:好比,肯定集群中结点的状态,还能够实现分布式下的读写锁……算法
以下图,GFS master颁发租约给某个chunk server,让它成为Primary 副本,当有多个client并发更新数据块时,由Primary副本肯定并发更新该数据块的顺序。GFS master 将权力下放给 chunk server,缓解了必定的压力。缓存
固然,也能够将中心服务器(元数据服务器)设计成集群的形式。这样,也避免了中心服务器成为瓶颈问题。好比,消息服务器:RocketMQ。其中生产者和消费者都须要NameServer来肯定订阅关系,将NameServer作成无状态的集群,这里就不须要租约机制了。服务器
对于租约而言,就有发布租约方 和 接收租约方。租约规定的内容能够多种多样(这也是租约具备各类应用场景的缘由)。发布租约方通常为上面提到的中心服务器,中心服务器保证在租约的有效期内承诺租约规定的内容的保持不变。网络
好比,分布式缓存系统,元数据服务器(中心服务器)给各个Client发布租约,承诺在租约有效期内不会更改元数据。这样,各个Client只要检查它的租约未过时,就能够直接从本地读取缓存的元数据,而不是每次都访问元数据服务器得到元数据。并发
二,租约机制分析app
①租约机制保证缓存的一致性分布式
服务器发出Lease后,会保证在Lease的有效期内不改变数据。这样,收到Lease的Client在有效期内能够放心地使用数据。在这个有效期内,Client 缓存的数据和 服务器上的数据是一致的。ide
存在的问题:性能
1)服务器修改元数据时,须要 阻塞全部的读请求,此时服务器不能发出新的Lease。以防止新发出的Lease保证的数据与服务器刚才修改的数据不一致。
解决方法:读请求到来时,直接返回数据,不颁发Lease
2)服务器须要等待直至全部的Client的Lease都过时后,再才颁发新“修改”后的Lease。所以,此时服务器上的数据修改了,生成了一个新的Lease版本,须要等到Client上全部的老Lease过时后,该新Lease版本才能颁布给Client。
解决方法:服务器主动通知持久Lease的Client放弃当前的Lease,并请求新Lease
另外一种缓存一致性的保证方法相似于“租约机制”,当Client请求Server的数据时,Server为Client提供一个“回调承诺”,用于保证当其余Client修改此数据时通知该Client。回调承诺 和 请求的数据都保存在Client端,回调承诺有两种状态:有效和取消。
当Server执行了一个更新数据请求时,它会通知它发送了回调承诺的全部Client,即给Client的端的进程发一个回调(server到client的一个远程过程调用)
,当Client进程收到回调时,它将相关数据的回调承诺标识设置为“取消”。
②租约机制可以很好地容纳网络错误异常
1)Lease颁发过程只依赖于单向的网络通讯
服务器颁发Lease后,即便Client没有收到(Client宕机、网络异常),服务器只要等到Lease超时,就能够保证Client再也不cache数据,从而能够放心地修改数据而不会破坏cache的一致性。
2)一旦Lease被Client接收,后续Lease机制再也不依赖于网络通讯。
3)对宕机节点有很好的容错性
颁发Lease的节点宕机了,宕机的颁发者改变不了已经颁发出的Lease的约定,不会影响Lease的正确性。
拥有Lease的节点宕机了,颁发者也不须要作容错处理,只须要等待Lease到期了,就能够收回承诺进行下一步处理。
③租约机制肯定节点的状态
在网络中,如何肯定某个节点的状态呢?因为网络故障(网络分化)的存在,采用“心跳”机制肯定节点的状态会有一些不足。
好比,A、B、C三个节点互为副本,A为primary,Q负责判断A、 B 、C的状态。若是A正常工做,可是A 、Q之间的网络异常,Q也会认为A出现了问题了,因而 Q 从新选择B做为primary,这里会致使“双主”问题。
这里的本质是:Q认为A异常了,可是A本身不认为本身异常。即,因为网络分化形成系统对于“节点状态”认知的不一致。
解决方法有两个:1)可使用全体协商肯定谁为primary(Paxos算法) ,这是一种去中心化协议
2)采用Lease机制
Q收到 A 、B 、C 的heart beat后,给它们颁发一个Lease,表示已经知道了它们的状态,这样 A 、B 、C 能够在有效期内正常工做。同时,Q 能够给 A一个特殊的Lease,表示A能够做为primary工做。当须要切换primary时,只须要等到A的Lease过时,Q给另外节点颁发表示 primary的Lease便可。
三,租约机制在 GFS 的写操做中的做用
We designed the system to minimize the master’s involvement in all operations.
....
A mutation is an operation that changes the contents or metadata of a chunksuch as a write or an append operation.
Each mutation is performed at all the chunk’s replicas. We use leases to maintain a consistent mutation order across replicas.
The master grants a chunk lease to one of the replicas, which we call the primary. The primary picks a serial order for all mutations to the chunk. All replicas follow this order when applying mutations.
为了减少 master 的负载,master 给某个chunk server 颁发lease,使之成为primary,而后由primary肯定 mutation order。
The master may sometimes try to revoke a lease before it expires (e.g., when the master wants to disable mutations on a file that is being renamed).
Even if the master loses communication with a primary, it can safely grant a new lease to another replica after the old lease expires.
这里也采用 “租约机制分析”中讲到的:①master能够在租约还未过时以前 try to revoke a lease。②也能够等到租约过时后,向其余chunk server颁发primary租约(更换primary)。
The lease mechanism is designed to minimize management overhead at the master. A lease has an initial timeout of 60 seconds.
这句话代表,租约减小了master的负载,租约的有效期限是60s
写操做的步骤以下:
1)The client asks the master which chunkserver holds the current lease for the chunk and the locations of the other replicas.
Client向master 请求primary地址和其余chunk server地址
2)The master replies with the identity of the primary and the locations of the other (secondary) replicas.
master返回primary地址及其余chunk server地址。Client可在Lease的有效期内缓存这些信息。
3)The client pushes the data to all the replicas
Client把待修改的数据发给各个chunk server(replicas),这里实现了控制流与数据流的分离(Client在第二步时得到了控制信息)。
4)Once all the replicas have acknowledged receiving the data, the client sends a write request to the primary.
Client先把待写入的数据发给各个 chunk server,等到全部的chunk server都收到这些数据后,向 primary 发起写请求。
5)The primary forwards the write request to all secondary replicas.
由primary制定写请求的顺序。全部的replicas都按照这个顺序写数据。看,采用“中心化”的方式制定写请求的顺序,这样很容易保证顺序的惟一性。
6)The secondaries all reply to the primary indicating that they have completed the operation
全部的replicas(secondaries)都按照相同的顺序写入数据后,向primary发送完成报告。
7)The primary replies to the client.
由primary向Client报告这次写操做的结果。
从上面的整个流程看,上面的操做不多涉及到master。大部分由master 颁发给Lease的primary来完成。
其实,我的感受对于 mutation operation,这里的核心是两个:❶下降master的负载 ❷保证修改顺序的一致性。而经过Lease机制,解决了这两个问题。
master给某台chunk server颁发Lease,使之成为Primary。由Primary接收Client的写请求,由Primary负责其余replicas的写操做是否完成……这都有效地下降了master的负载;其次,由primary制定写操做的序列号,顺序的肯定是由primary肯定的,不是协商肯定的,这种“中心化”的机制容易保证顺序的一致性。
四,参考资料
《分布式系统原理介绍》--刘杰
分布式系统概念--第一篇 一致性协议、一致性模型、拜占庭问题、租约、副本协议
原文:http://www.cnblogs.com/hapjin/p/5620542.html