Filesystem closed报错问题处理
创始人
2024-05-22 16:13:33
0

使用HDFS的时候

final Configuration conf = new Configuration();
final FileSystem fs = FileSystem.get(URI.create(hdfsFile), conf);
final Path path = new Path(hdfsFile);
if (fs.exists(path)) {final FSDataInputStream is = fs.open(path);final FileStatus stat = fs.getFileStatus(path);final byte[] buffer = new byte[Integer.parseInt(String.valueOf(stat.getLen()))];is.readFully(0, buffer);is.close();fs.close();return buffer;
} else {throw new Exception("the file is not found .");
}

在高并发情况下会报错:

java.io.IOException: Failed on local exception: java.io.InterruptedIOException: Interrupted while waiting for IO on channel java.nio.channels.SocketChannel[connected local=/10.16.3.2:52305 remote=/10.16.3.2:59000]. 60000 millis timeout left.; Host Details : local host is: "hadoop-test/10.16.3.2"; destination host is: "hadoop-alone-test":59000; at org.apache.hadoop.net.NetUtils.wrapException(NetUtils.java:776)at org.apache.hadoop.ipc.Client.call(Client.java:1479)at org.apache.hadoop.ipc.Client.call(Client.java:1412)at org.apache.hadoop.ipc.ProtobufRpcEngine$Invoker.invoke(ProtobufRpcEngine.java:229)at com.sun.proxy.$Proxy109.getListing(Unknown Source)

问题原因为:多线程访问问题,线程A、B同时获取filesystem后使用,线程B使用完后调用了filesystem.close()方法,这个时候线程A还在操作filesystem,所以报错上面种种异常

二、解决办法

禁用FileSystem缓存

Configuration conf = new Configuration();
conf.set("fs.hdfs.impl.disable.cache", "true");

 三、问题原因

FileSystem.get源码分析

那么明明使用了两个集群,为什么会使用到Cache呢,分析FileSystem.get源码便知道原因了

  public static FileSystem get(URI uri, Configuration conf) throws IOException {String scheme = uri.getScheme();String authority = uri.getAuthority();if (scheme == null && authority == null) {     // use default FSreturn get(conf);}if (scheme != null && authority == null) {     // no authorityURI defaultUri = getDefaultUri(conf);if (scheme.equals(defaultUri.getScheme())    // if scheme matches default&& defaultUri.getAuthority() != null) {  // & default has authorityreturn get(defaultUri, conf);              // return default}}String disableCacheName = String.format("fs.%s.impl.disable.cache", scheme);if (conf.getBoolean(disableCacheName, false)) {LOGGER.debug("Bypassing cache to create filesystem {}", uri);return createFileSystem(uri, conf);}return CACHE.get(uri, conf);}

应用在获取FileSystem时,提供了完整的hdfs目录,同时没有设置fs.hdfs.impl.disable.cache为true,所以创建slave集群的filesystem对象时,会使用CACHE.get(uri, conf)获取,Cache内部使用一个HashMap来维护filesystem对象,很容易想到,当HashMap的key相同时,便返回了同一个filesystem对象,那么Cache中的key是什么样的呢,代码如下:

    FileSystem get(URI uri, Configuration conf) throws IOException{Key key = new Key(uri, conf);return getInternal(uri, conf, key);}static class Key {final String scheme;final String authority;final UserGroupInformation ugi;final long unique;   // an artificial way to make a key uniqueKey(URI uri, Configuration conf) throws IOException {this(uri, conf, 0);}Key(URI uri, Configuration conf, long unique) throws IOException {scheme = uri.getScheme()==null ?"" : StringUtils.toLowerCase(uri.getScheme());authority = uri.getAuthority()==null ?"" : StringUtils.toLowerCase(uri.getAuthority());this.unique = unique;this.ugi = UserGroupInformation.getCurrentUser();}@Overridepublic int hashCode() {return (scheme + authority).hashCode() + ugi.hashCode() + (int)unique;}static boolean isEqual(Object a, Object b) {return a == b || (a != null && a.equals(b));}@Overridepublic boolean equals(Object obj) {if (obj == this) {return true;}if (obj instanceof Key) {Key that = (Key)obj;return isEqual(this.scheme, that.scheme)&& isEqual(this.authority, that.authority)&& isEqual(this.ugi, that.ugi)&& (this.unique == that.unique);}return false;}@Overridepublic String toString() {return "("+ugi.toString() + ")@" + scheme + "://" + authority;}}}

可以看到Key由四个要素构成,其中前2个跟URI相关,我们使用的为一个hdfs://nameservice1,ugi为安全认证的用户,使用的是同一个,unique为0,因此Key相同,第二次获取filesystem对象时,直接返回了第一次创建的filesystem对象,最终造成了应用虽然使用了不同的集群配置文件,但最中获取的是同一个filesystem对象。

解决

fs.hdfs.impl.disable.cache参数本身不建议修改,修改集群的fs.defaultFS,使不同集群的fs.defaultFS不一样

参考:

多个HDFS集群的fs.defaultFS配置一样,造成应用一直连接同一个集群的问题分析 - 远去的列车 - 博客园
Filesystem closed错误排查 - 简书
 

java.io.IOException: Filesystem closed - 简书

java.io.IOException: Filesystem closed_bitcarmanlee的博客-CSDN博客_filesystem closed

相关内容

热门资讯

安卓4.0系统精美桌面,精美桌... 亲爱的读者们,你是否曾为手机桌面单调无趣而烦恼?别担心,今天我要带你走进一个充满色彩与活力的世界——...
怎么再换回安卓系统,教你如何一... 你是不是最近把手机换成了苹果系统,结果发现安卓的便利性让你有点想念了呢?别急,今天就来手把手教你,怎...
安卓12最新系统,系统革新与用... 你有没有听说?安卓12最新系统已经悄悄上线了!没错,就是那个让无数安卓用户翘首以盼的安卓12!今天,...
仿安卓4系统下载,下载与体验全... 你有没有想过,手机系统就像是我们生活的操作系统,有时候换一个新系统,就像是给生活来个大升级呢!今天,...
安卓手机的系统日志,探寻系统运... 你有没有发现,每次你的安卓手机出了点小状况,比如突然卡顿或者电池耗得飞快,你都会想探究个究竟?别急,...
安卓系统azw3,Androi... 你有没有发现,手机里的安卓系统越来越强大了?今天,就让我带你深入了解一下这个神奇的系统,尤其是那个神...
智能安卓电视系统卡,智能安卓电... 你有没有遇到过这种情况?家里的智能安卓电视系统突然卡住了,屏幕上那个熟悉的界面就像被施了魔法一样,怎...
电脑虚拟安卓系统教程,教程全解... 你有没有想过,让你的电脑也能像手机一样,随时随地玩各种安卓应用?没错,这就是今天我要跟你分享的神奇魔...
qq飞车分安卓系统,QQ飞车安... 你有没有发现,最近QQ飞车这款游戏在安卓系统上可是火得一塌糊涂啊!不管是街头巷尾,还是朋友圈里,都能...
淘手游苹果系统安卓系统,苹果系... 你有没有发现,现在手机游戏越来越火了?不管是走在街头,还是坐在家里,总能看到大家拿着手机,眼睛一眨不...
安卓系统定位app华为,守护您... 你有没有发现,现在手机里的APP真是五花八门,各有各的用处。今天,咱们就来聊聊安卓系统里一个特别实用...
安卓系统显示矫准,打造清晰视觉... 你有没有发现,你的安卓手机屏幕有时候显示得有点歪歪扭扭的?别急,这可不是什么大问题,今天就来给你详细...
安卓系统服务有病毒,病毒生成背... 你知道吗?最近在安卓系统上,服务里竟然悄悄潜入了病毒!这可不是闹着玩的,得赶紧来聊聊这个事儿,让你了...
解决ios系统和安卓系统游戏,... 你是不是也和我一样,手机里装了各种游戏,却因为iOS和安卓系统不兼容而头疼不已?别急,今天就来给你支...
安卓系统浮窗app,便捷多任务... 你有没有发现,手机上的那些小窗口,就像魔法一样,让我们的使用体验瞬间升级?没错,说的就是安卓系统里的...
安卓手工刷谷歌系统,体验原生魅... 你有没有想过,你的安卓手机其实可以焕发第二春呢?没错,就是通过手工刷谷歌系统,让你的手机体验焕然一新...
调整安卓系统时间流速,揭秘安卓... 你有没有发现,时间有时候就像那调皮的小精灵,在我们不经意间溜走?有时候,我们希望时间能慢一些,让生活...
网易云游戏安卓系统,解锁全新游... 亲爱的游戏迷们,你是不是也和我一样,对手机游戏情有独钟?今天,我要和你聊聊一个特别酷的话题——网易云...
安卓系统那个优化最好,探索最佳... 你有没有发现,手机里的安卓系统就像是个调皮的小家伙,总是时不时地给你点小麻烦?不过别担心,今天咱们就...
安卓手机安装windous系统... 你有没有想过,你的安卓手机也能装上Windows系统?是的,你没听错,就是那个曾经陪伴我们无数个日夜...