Hadoop学习二十六:Hadoop-Hdfs Lease源码

阅读更多

一. Lease

  1.  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.
  2. 翻译如下:Lease管理着一个client占用的所有锁。一个client对应着一个Lease,在client发送请求时,对应Lease的timestamp调整到最新时间。如果client死亡,让对应Lease过期,释放Lease管理的所有锁。
  3. 个人理解:Lease是一个文件写锁,当client需要写文件时,需要申请一个Lease。NameNode负责记录哪个文件上有Lease,Lease的客户是谁,超时时间。
  4. 成员变量
    private final String holder;	//客户端名
        private long lastUpdate;		//最近更新时间
        private final Collection paths = new TreeSet();	//该客户端操作的文件集合
  5. 方法,其方法都很简单,随便举两个例子
     /** 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

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

 

三. LeaseManager

  1.  LeaseManager管理着所有的Lease。
  2. Lease和Monitor都是LeaseManager内部类。Lease的private构造函数保证额只有LeaseManager才能创建一个Lease。
  3. 成员变量
      private final FSNamesystem fsnamesystem;
      //Lease.holder -> Lease
      private SortedMap leases = new TreeMap();
      // Set of: Lease
      private SortedSet sortedLeases = new TreeSet();
    
       //pathnames -> Lease
      private SortedMap sortedLeasesByPath = new TreeMap();
  4. 方法,无非就是对几个集合的增删改查操作,毫无难度。需要注意的是,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类图

Hadoop学习二十六:Hadoop-Hdfs Lease源码_第1张图片 

 

你可能感兴趣的:(Hadoop学习二十六:Hadoop-Hdfs Lease源码)