首页 > 解决方案 > hdfs 删除正在写入的文件

问题描述

根据博客,hdfs 使用租约机制来避免两个客户端写入同一个文件。所以我认为一个人不能删除另一个客户端写入的文件。然而,这是错误的。

当客户端 A 正在写入 lock.txt 时,客户端 B 可以立即删除 lock.txt。尽管文件不再存在,客户端 A 可以继续写入。就在关闭流的时候,A会遇到异常: org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException: No lease on /user/lock.txt (inode 643845185): File does not exist. Holder DFSClient_NONMAPREDUCE_-1636584585_1 does not have any open files**

为什么会这样?我的 haddop 版本是 2.7.3。

=================================

这是我的测试代码:</p>

// write process
object Create {

  private val filePath = "/user/lock.txt"

  def main(args: Array[String]): Unit = {
    println("Create start!")
    val fs = FileSystem.get(new Configuration())
    val os = Option(fs.create(new Path(filePath), false))
    if (os.isDefined) {
      println(s"Create result! $os", System.currentTimeMillis())
      0.until(300).foreach(index => {
        os.get.write(100)
        os.get.flush()
        println("Writing...")
        Thread.sleep(1000)
      })
      println("pre close" + System.currentTimeMillis())
      os.get.close()
      println("close success!" + System.currentTimeMillis())
    }
  }

}

// delete process
object Delete {

  private val filePath = "/user/lock.txt"

  def main(args: Array[String]): Unit = {
    println("Delete start!")
    val fs = FileSystem.get(new Configuration())
    while (!fs.exists(new Path(filePath))) {
      println("File no exist!")
      Thread.sleep(1000)
    }
    println("File exist!")
    while (true) {
      println("try delete!")
      val tmp = Option(fs.delete(new Path(filePath), false))
      if (tmp.isDefined) {
        println(s"delete result:${tmp.get}!" + System.currentTimeMillis())
      }
      println("Try recover")
      if (fs.asInstanceOf[DistributedFileSystem].recoverLease(new Path(filePath))) {
        println("Recover lease success!")
        val res = Option(fs.delete(new Path(filePath), false))
        println(s"File delete success:${res.get}")
      } else {
        println("Recover lease failed!")
      }
      Thread.sleep(1000)
    }
  }

}

标签: hadoophdfs

解决方案


推荐阅读