title: hdfs delete a file being written
body: <p>Acording the <a href="https://blog.cloudera.com/blog/2015/02/understanding-hdfs-recovery-processes-part-1/" rel="nofollow noreferrer">blog</a>, hdfs uses the lease mechanism to avoid two client writing the same file. So I think one can not delete the file which is written by the other client. Howerver, it's wrong. </p>
  
  <p>When client A is writing the  lock.txt, client B can delete lock.txt immediately. Client A can continue writing although the file no longer exists. Just when closing the stream, A will encounter the exception:
  <code>
   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**
  </code> </p>
  
  <p>Why this happen? My haddop version is 2.7.3.</p>
  
  <p>================================</p>
  
  <p>This is my test code：</p>
  
  <pre><code>// 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 =&gt; {
          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)
      }
    }
  
  }
  </code></pre>