设为首页 收藏本站
查看: 1046|回复: 0

[经验分享] Hadoop学习二十六:Hadoop-Hdfs Lease源码

[复制链接]

尚未签到

发表于 2016-12-9 10:13:29 | 显示全部楼层 |阅读模式
一. Lease


  •  A Lease governs all the locks held by a single client.
       * For each client there's a corresponding lease, whose
       * timestamp is updated when the client periodically
       * checks in.  If the client dies and allows its lease to
       * expire, all the corresponding locks can be released.
  • 翻译如下:Lease管理着一个client占用的所有锁。一个client对应着一个Lease,在client发送请求时,对应Lease的timestamp调整到最新时间。如果client死亡,让对应Lease过期,释放Lease管理的所有锁。
  • 个人理解:Lease是一个文件写锁,当client需要写文件时,需要申请一个Lease。NameNode负责记录哪个文件上有Lease,Lease的客户是谁,超时时间。
  • 成员变量
    private final String holder;//客户端名
    private long lastUpdate;//最近更新时间
    private final Collection<String> paths = new TreeSet<String>();//该客户端操作的文件集合
  • 方法,其方法都很简单,随便举两个例子
    /** Only LeaseManager object can create a lease */
    private Lease(String holder) {
    this.holder = holder;
    renew();
    }
    /** Only LeaseManager object can renew a lease */
    private void renew() {
    this.lastUpdate = FSNamesystem.now();
    }
    //replacing oldpath with  newpath
    synchronized void changeLease(String src, String dst,
    String overwrite, String replaceBy) {
    }
    synchronized void removeLeaseWithPrefixPath(String prefix) {
    }

     

二. Monitor


  •  Monitor checks for leases that have expired, and disposes of them.
  • 每两秒调用checkLeases()。checkLeases()检查每个Lease是否过期,如果过期,调用fsnamesystem.internalReleaseLeaseOne(oldest, path);
三. LeaseManager


  •  LeaseManager管理着所有的Lease。
  • Lease和Monitor都是LeaseManager内部类。Lease的private构造函数保证额只有LeaseManager才能创建一个Lease。
  • 成员变量
      private final FSNamesystem fsnamesystem;
    //Lease.holder -> Lease
    private SortedMap<String, Lease> leases = new TreeMap<String, Lease>();
    // Set of: Lease
    private SortedSet<Lease> sortedLeases = new TreeSet<Lease>();
    //pathnames -> Lease
    private SortedMap<String, Lease> sortedLeasesByPath = new TreeMap<String, Lease>();
  • 方法,无非就是对几个集合的增删改查操作,毫无难度。需要注意的是,addLease()并没有检查文件(src)上是否已经有Lease,这个需要由LeaseManager调用者保证。
    synchronized Lease addLease(String holder, String src) {
    Lease lease = getLease(holder);
    if (lease == null) {
    lease = new Lease(holder);
    leases.put(holder, lease);
    sortedLeases.add(lease);
    } else {
    renewLease(lease);
    }
    sortedLeasesByPath.put(src, lease);
    lease.paths.add(src);
    return lease;
    }
    /**
    * Remove the specified lease and src.
    */
    synchronized void removeLease(Lease lease, String src) {
    sortedLeasesByPath.remove(src);
    }
    /**
    * Reassign lease for file src to the new holder.
    */
    synchronized Lease reassignLease(Lease lease, String src, String newHolder) {
    return addLease(newHolder, src);
    }
    /**
    * Remove the lease for the specified holder and src
    */
    synchronized void removeLease(String holder, String src) {
    }
    /**
    * Finds the pathname for the specified pendingFile
    */
    synchronized String findPath(INodeFileUnderConstruction pendingFile
    ) throws IOException {
    }
    /**
    * Renew the lease(s) held by the given client
    */
    synchronized void renewLease(String holder) {
    }
四. LeaseManager类图
DSC0000.jpg  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-311837-1-1.html 上篇帖子: Hadoop学习二十五:Hadoop-Hdfs FSImage源码 下篇帖子: Hadoop学习二十七:Hadoop-Hdfs 权限相关 源码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表