背景

应用需要对两个集群中的同一目录下的HDFS文件个数和文件总大小进行比对,在测试环境中发现,即使两边HDFS目录下的数据不一样,应用日志显示两边始终比对一致,分下下来发现,应用连的一直是同一个集群。大数据集群:CDH6.2.1

定位分析

应用代码片段

                  Configuration mainconf = new Configuration();
mainconf.addResource(new Path(main_prefix+"/core-site.xml"));
mainconf.addResource(new Path(main_prefix+"/hdfs-site.xml"));
//Main集群hdfs操作,获取指定目录下文件
getHdfsDirectoryTailList("指定目录",mainConf)
Configuration slaveconf=new Configuration();
slaveconf.addResource(new Path(slave_prefix+"/core-site.xml"));
slaveconf.addResource(new Path(slave_prefix+"/hdfs-site.xml"));
//Slave集群hdfs操作,获取指定目录下文件
getHdfsDirectoryTailList("指定目录",slaveConf)
 public static List<String> getHdfsDirectoryTailList(String path,Configuration conf) {
List<String> tailList = new ArrayList<String>();
try {
FileSystem hdfs = FileSystem.get(URI.create(path), conf);
if(!hdfs.exists(new Path(path))){
return tailList;
}
FileStatus[] fs = hdfs.listStatus(new Path(path));
Path[] listPath = FileUtil.stat2Paths(fs);
for (Path p : listPath) {
String[] tailSplit = p.toString().split("\\/");
String tail = tailSplit[tailSplit.length - 1];
if (tail.equals("_SUCCESS")) {
continue;
}
tailList.add(tail);
}
} catch (IOException e) {
logger.error("Extract: getHdfsDirectoryTailList exception", e);
throw e;
}
return tailList;
}

检查两个集群配置及应用代码,确认没有问题

fs.hdfs.impl.disable.cache参数

fs.hdfs.impl.disable.cache参数之前又遇到过,默认值为false,表示使用cache,怀疑又是cache的问题,所以FileSystem.get(URI.create(path), conf),第一次获取的是master集群,第二次使用了cache,所以一直连的是master集群。测试方法,在core-site.xml里加上下面配置,表示不使用cache,加上之后,应用能正常连接两个集群了。

<property>
<name>fs.hdfs.impl.disable.cache</name>
<value>true</value>
</property>

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 FS
return get(conf);
} if (scheme != null && authority == null) { // no authority
URI defaultUri = getDefaultUri(conf);
if (scheme.equals(defaultUri.getScheme()) // if scheme matches default
&& defaultUri.getAuthority() != null) { // & default has authority
return 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 unique Key(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();
} @Override
public 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));
} @Override
public 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;
} @Override
public String toString() {
return "("+ugi.toString() + ")@" + scheme + "://" + authority;
}
}
}

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

解决

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

多个HDFS集群的fs.defaultFS配置一样,造成应用一直连接同一个集群的问题分析的更多相关文章

  1. 获取hdfs集群信息(fs.defaultFS)

    [root@hive-dp-7bd6fd4d55-wctjn hive-1.1.0-cdh5.14.0]# hdfs getconf -confKey fs.default.name19/12/04 ...

  2. 大数据高可用集群环境安装与配置(08)——安装Ganglia监控集群

    1. 安装依赖包和软件 在所有服务器上输入命令进行安装操作 yum install epel-release -y yum install ganglia-web ganglia-gmetad gan ...

  3. 使用DBeaver Enterprise连接redis集群的一些操作记录

    要点总结: 使用DBeaver Enterprise连接redis集群可以通过SQL语句查看key对应的value,但是没法查看key. 使用RedisDesktopManager连接redis集群可 ...

  4. hdfs dfs ls /列出了本地根目录下文件夹和文件Warning: fs.defaultFS is not set when running "ls" command

    [root@node01 customShells]# hdfs dfs -ls /Warning: fs.defaultFS is not set when running "ls&quo ...

  5. 【Spark】---- 在Linux集群上安装和配置Spark

    1 安装JDK   1) 进入JDK官网 2) 下载JDK安装包 3)配置环境变量,在/etc/profile增加以下代码 JAVA_HOME=/home/hadoop/jdk1.6.0_38 PAT ...

  6. 大数据高可用集群环境安装与配置(06)——安装Hadoop高可用集群

    下载Hadoop安装包 登录 https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/ 镜像站,找到我们要安装的版本,点击进去复制下载链接 ...

  7. Dubbo入门到精通学习笔记(十四):ActiveMQ集群的安装、配置、高可用测试,ActiveMQ高可用+负载均衡集群的安装、配置、高可用测试

    文章目录 ActiveMQ 高可用集群安装.配置.高可用测试( ZooKeeper + LevelDB) ActiveMQ高可用+负载均衡集群的安装.配置.高可用测试 准备 正式开始 ActiveMQ ...

  8. hdfs的HA集群搭建的相关配置

    前期准备就不详细说了 .修改Linux主机名 .修改IP .修改主机名和IP的映射关系 ######注意######如果你们公司是租用的服务器或是使用的云主机(如华为用主机.阿里云主机等) /etc/ ...

  9. Hadoop集群搭建-04安装配置HDFS

    Hadoop集群搭建-05安装配置YARN Hadoop集群搭建-04安装配置HDFS  Hadoop集群搭建-03编译安装hadoop Hadoop集群搭建-02安装配置Zookeeper Hado ...

随机推荐

  1. bWAPP----OS Command Injection

    OS Command Injection 界面: 给一个域名,它帮你返回DNS 代码: 1 <div id="main"> 2 3 <h1>OS Comma ...

  2. swlib

    Swoole人性化组件库 ~ Swoole Humanization Component Library

  3. 解析MindMapper选项中的鱼骨选项

    MindMapper思维导图可以画鱼骨图,而且完成的相当漂亮,我们可以在选项设置中定义鱼骨图图的默认样式.下面本文就分析了MindMapper选项中可以更改哪些鱼骨图设置. 我们首先打开MindMap ...

  4. 常见的名片尺寸如何在CorelDRAW预设

    说到名片想必大家肯定不陌生,是我们生活中随处可见的物品,也是商家宣传必不可少的印刷物料.那么名片的尺寸是多少?我们做名片的时候该如何把握好名片的尺寸呢?在CDR中有专门的名片尺寸,下面小编就为大家简单 ...

  5. guitar pro系列教程(十五):Guitar Pro播放没有声音怎么办?

    相信很多朋友在安装了guitar pro打谱后都会发现没有声音,关于这个问题相信大家都很苦恼到底是什么问题呢,今天小编就这个问题与大家好好讨论讨论,希望对大家有帮助,感兴趣的朋友可以进来一起学习哦. ...

  6. 在FL Studio编曲软件中查找采样的音高

    相信玩音乐的小伙伴们都遇到过这种情况,在用FL Studio编曲时,想添加一段音频采样,由于采样的调式与我们正在编曲的调式不同,音频之间的衔接就是一个非常头疼的问题,要解决采样的调式,我们就得先确认这 ...

  7. 【C++】数组中的第k个最小元素

    分治思想求解的问题,但是比较特殊,只有分解问题和求解小问题,不需要合并 每次也只需要经过判断,分解一半,所以比其他分解两边的效率高 最坏情况时间复杂度为O(n^2),期望时间复杂度为O(n) 找基准值 ...

  8. Jmeter生成HTML测试报告

    jmeter轻便小巧,运行速度快,但是缺少直观的可视化测试报告,并且生成测试报告操作稍微有点麻烦. GUI界面没有生成测试报告的功能,只能使用命令行生成测试报告.这里需要提到一个jtl的文件,它是生成 ...

  9. python接口测试3-JSON格式

    什么是JSON? 一种轻量级的数据交换格式.它独立于语言和平台,JSON解析器和JSON库支持不同的编程语言.JSON具有自我描述性,很容易理解. 数据格式: { "name":& ...

  10. 年轻人不讲武德,竟然重构出这么优雅后台 API 接口

    Hello,早上好,我是楼下小黑哥~ 最近偶然间在看到 Spring 官方文档的时候,新学到一个注解 @ControllerAdvice,并且成功使用这个注解重构我们项目的对外 API 接口,去除繁琐 ...