背景

应用需要对两个集群中的同一目录下的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. 深度分析:java设计模式中的原型模式,看完就没有说不懂的

    前言 原型模式(Prototype模式)是指:用原型实例指定创建对象的种类,并且通过拷贝这些原型,创建新的对象 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象,无需知道如何创建的 ...

  2. 下载器Folx怎么安装使用

    应该使用哪个下载工具?这个如果是Windows上会有无数答案的问题,在Mac上却变得异常的纠结.比如Leech和Aria2,这两款软件,前者功能相对比较简单,后者的配置又稍微有点复杂,很难找到一款相对 ...

  3. Meetings S 题解

    题目描述 题目链接 有两个牛棚位于一维数轴上的点 \(0\) 和 \(L\) 处.同时有 \(N\) 头奶牛位于数轴上不同的位置(将牛棚和奶牛看作点).每头奶牛 \(i\) 初始时位于某个位置 \(x ...

  4. C语言讲义——开发工具Dev C++

    20世纪60年代,编程语言界发生"结构化程序设计"变革, 丹尼斯·里奇(Dennis Ritchie)& 肯·汤普森(Ken Thompson)发明C语言,率先建立了面向过 ...

  5. Java基础教程——二维数组

    二维数组 Java里的二维数组其实是数组的数组,即每个数组元素都是一个数组. 每个数组的长度不要求一致,但最好一致. // 同样有两种风格的定义方法 int[][] _arr21_推荐 = { { 1 ...

  6. linux下安装python3.7.1

    一.安装依赖环境 输入命令:yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readlin ...

  7. houdini 鱼眼相机

    http://mattebb.com/weblog/houdini-fisheye-camera/ 这个网站是有提供一个相机shader的,,如图是方形的,国内的用户,比较多是做球幕的小伙伴,圆形就行 ...

  8. 你的Idea还可用吗?不妨试试这个神器!

    @ 目录 一.STS安装 1.STS下载 2.STS安装 二.STS使用 1.STS配置JDK 2.STS配置Maven 3.使用STS创建SpringBoot项目 三.优化STS 1.主题美化 2. ...

  9. 笔记本无法连接校园网,windows诊断显示校园网之未响应

    打开cmd(管理员): 输入以下四条,每一条都按enter ipconfig /flushdns ipconfig /registerdns ipconfig /release ipconfig / ...

  10. 如何破解QQ闪照

    1.如何下载 通过公主公众号 "全是软件" 然后输入 294 即可获得下载链接 https://qsrj.lanzous.com/iU4Hddnnmne 目前的闪照破解工具只能破解 ...