【Lease|Block】Recovery

2020-09-21  本文已影响0人  小北觅

分享一波大数据&Java的学习视频和书籍:
### Java与大数据资源分享

一、Introduction

As we all know,网络是时时刻刻会出现故障的,HDFS集群庞大了之后出现节点故障也是常事。因此HDFS一个重要的设计需求就是保证持续地正确地写入操作。这时候lease recovery, block recovery, 和 pipeline recovery就开始发挥作用了。理解这些recovery过程什么时候被调用以及为什么被调用对我们理解hdfs原理有很大帮助,本文来学习租约恢复和块恢复的过程。

在摘录英文前给出自己的总结:

接下来,来点纯正的英文摘录:

Lease recovery, block recovery, and pipeline recovery come into play in this type of situation:

在HDFS中,客户端要想写文件,必须要获得该文件的租约(本质上类似于锁的概念)。如果client想继续写文件,那么必须要在提前定义好的时间周期里(soft limit and hard limit)进行renew 租约。如果没有显式地renew租约或者client 挂掉了,那么HDFS就会close the file并且释放掉代表这个client的租约,从而让其他的clients能够写文件。这个过程就叫做lease recovery。

在写pipeline操作的过程中,一些Datenode可能会发生失败。当发生这种情况时,写操作不能只是失败而已。HDFS会尝试从error中恢复使得pipeline能够keep going,同时让client继续写文件。这就是pipeline recovery。

二、Blocks, Replicas, and Their States

在详述这三个recovery过程之前,我们先来介绍一下block和replicas的概念,以及他们的状态。

为了区分在Namenode和Datanode不同上下文中的blocks。我们用blocks表示在namenode中,用replicas表示在datanode中。

Datanode中的replicas有以下这些状态:(由enum ReplicaState类定义)

Namenode中的blocks有以下状态:(由 enum BlockUCState类定义)

notices:DataNodes persist a replica’s state to disk, but the NameNode doesn’t persist the block state to disk. When the NameNode restarts, it changes the state of the last block of any previously open file to the UNDER_CONSTRUCTION state, and the state of all the other blocks to COMPLETE.

Simplified Replica State Transition:

Simplified Block State Transition:

三、Generation Stamp (GS)

A GS is a monotonically increasing 8-byte number for each block that is maintained persistently by the NameNode。GS的引入有以下目的:

A new GS is needed when any of the following occur:(生成新的GS的场景):

四、Lease Recovery and Block Recovery

lease相关的管理操作由Namenode端的lease manager统一管理。client可能会持有很多文件的租约,client会定期发送一个请求来renew所有这些文件的租约。
org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.RenewLeaseResponseProto

每个Namenode管理一个Namespace,同时持有这个namespace下的leasemanager。联邦HDFS cluster的情况下,每一个namespace有自己的leasemanager。

leasemanager保存了soft limit(1 min)和hard limit(1 hour)。当client的lease超过了soft limit时长并且client没有进行renew操作或者close file时,其他的client可以强制的获取这个文件的lease,从而对这个文件进行写操作。如果超过了hard limit时长并且client还没renew lease,那么HDFS就认为这个client已经退出了,于是自动地为client关闭这个文件,从而recovery lease。

HDFS client通过org.apache.hadoop.hdfs.LeaseRenewer.LeaseRenewer类来renew leases。这个类维护了一个users list并且在每个namenode上为一个用户跑一个线程。这些线程会周期性的向namenode登记,并且当lease period过半时 renew all of the client's leases

4.1 lease recovery process(租约恢复过程)

lease recovery在namenode中被触发,要么是由monitor thread检测超过hard limit,要么是超出soft limit后,一个client尝试接管从其他client那里接管lease。lease recovery检查client打开的每个file,如果file的最后一个block不是Complete状态则进行block recovery,然后close file。

notice:Block recovery of a file is only triggered when recovering the lease of a file

下面给定一个文件f来阐述租约恢复算法,当一个client挂掉的时候,下面的算法对每一个由这个client打开的文件都适用:

  1. Get the DataNodes which contain the last block of f.
  2. Assign one of the DataNodes as the primary DataNode p.
  3. p obtains a new generation stamp from the NameNode.
  4. p gets the block info from each DataNode.
  5. p computes the minimum block length.
  6. p updates the DataNodes, which have a valid generation stamp, with the new generation stamp and the minimum block length.
  7. p acknowledges the NameNode the update results.
  8. NameNode updates the BlockInfo.
  9. NameNode remove f’s lease (other writer can now obtain the lease for writing to f).
  10. NameNode commit changes to edit log.

算法的步骤3--7是block recovery阶段。如果一个file需要进行block recovery,那么namenode会挑选出一个primary datanode(这个datanode持有文件最后一个块的副本)。这个primary datanode 负责协调与其他datanodes的块恢复工作。当块恢复结束后,向namenode汇报。接着namenode会change这个block的。state,移除租约,commit changes to edit log。

参考网站:
https://blog.cloudera.com/

上一篇下一篇

猜你喜欢

热点阅读