hadoop - 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)
}
}
}
解决方案
推荐阅读
- hadoop - HDFS中的重复目录
- python - RecursionError : 调用 Python 对象时超出最大递归深度
- runtime-error - 为什么即使我的代码在我的 python 空闲时运行良好,我也会在 HackerRank 中收到 EOF 错误?
- algorithm - 当值未知时查找多数元素
- r - 如何在 rshiny 中过滤列和拟合模型
- javascript - 如何在 Firefox 中基于视频约束进行 getusermedia 捕获?在 Chrome 中,一切都按照代码进行,但在 Firefox 中却不是?
- google-sheets - 如果它们符合标准,求和持续时间?(谷歌表)
- node.js - 从对象数组中获取 on 属性/字段的所有值
- java - Java 8:按多个字段分组,然后根据值排序
- r - 如何使用来自漏洞的数据帧导致R