Hadoop大数据工作生活

hadoop 的lease(一)

2018-09-15  本文已影响0人  Unyielding_L

“租约” ,何为租约,一个租约管理着一个client的所有锁,对于每个client 都有一个相应的租约,但一个client 定期 check in 时租约的时间戳就会更新,当一个client 停止的时候并且允许租约到期,则对应的所有 锁都会释放。
大家来看看租约这个类:

class Lease implements Comparable<Lease> {
    // 租约的持有者,就是客户端名称 
    private final String holder;
   //最后一次检入时间
    private long lastUpdate;
    //持有的文件路径集
    private final Collection<String> paths = new TreeSet<String>();
  }

租约管理的超时限制:

租约恢复算法

private void recoverLeaseInternal(INodeFile fileInode, 
      String src, String holder, String clientMachine, boolean force)
      throws IOException {
    assert hasWriteLock();
    if (fileInode != null && fileInode.isUnderConstruction()) {
      INodeFileUnderConstruction pendingFile = (INodeFileUnderConstruction) fileInode;
      /** 如果文件正在构建中,那么它必须在我们的租约中。找到合适的租赁记录。*/
      Lease lease = leaseManager.getLease(holder);
      //通过文件地址获取租约,若发现原创建该文件,会报异常
      if (!force && lease != null) {
        Lease leaseFile = leaseManager.getLeaseByPath(src);
        if ((leaseFile != null && leaseFile.equals(lease))) { 
          throw new AlreadyBeingCreatedException(
            "failed to create file " + src + " for " + holder +
            " on client " + clientMachine + 
            " because current leaseholder is trying to recreate file.");
        }
      }
      //
      // 获取原始的持有者      
      lease = leaseManager.getLease(pendingFile.getClientName());
      if (lease == null) {
        throw new AlreadyBeingCreatedException(
          "failed to create file " + src + " for " + holder +
          " on client " + clientMachine + 
          " because pendingCreates is non-null but no leases found.");
      }
      if (force) {
        //如果是强制的
        // 直接关闭:不需要 等待软超时过期
        // 只关闭当前的文件
        LOG.info("recoverLease: " + lease + ", src=" + src +
          " from client " + pendingFile.getClientName());
        internalReleaseLease(lease, src, holder);
      } else {
        assert lease.getHolder().equals(pendingFile.getClientName()) :
          "Current lease holder " + lease.getHolder() +
          " does not match file creator " + pendingFile.getClientName();
        //如果软超时已经超时 则尝试恢复租约
        if (lease.expiredSoftLimit()) {
          LOG.info("startFile: recover " + lease + ", src=" + src + " client "
              + pendingFile.getClientName());
          boolean isClosed = internalReleaseLease(lease, src, null);
          if(!isClosed)
            throw new RecoveryInProgressException(
                "Failed to close file " + src +
                ". Lease recovery is in progress. Try again later.");
        } else {
          final BlockInfo lastBlock = pendingFile.getLastBlock();
          if (lastBlock != null
              && lastBlock.getBlockUCState() == BlockUCState.UNDER_RECOVERY) {
            throw new RecoveryInProgressException("Recovery in progress, file ["
                + src + "], " + "lease owner [" + lease.getHolder() + "]");
          } else {
            throw new AlreadyBeingCreatedException("Failed to create file ["
                + src + "] for [" + holder + "] on client [" + clientMachine
                + "], because this file is already being created by ["
                + pendingFile.getClientName() + "] on ["
                + pendingFile.getClientMachine() + "]");
          }
        }
      }
    }
  }

上一篇 下一篇

猜你喜欢

热点阅读