背景

应用需要对两个集群中的同一目录下的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. laravel 验证器使用

    1.前后端不分离 (form表单提交) 控制器定义验证规则 <?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Req ...

  2. ESP定律脱壳——NsPack3.x脱壳

    首先进行查壳,NsPack 将程序拖入x64dbg 程序入口处标志性的push F8单步,发现仅有esp寄存器有变化 在esp上右键,在内存窗口查看,下硬件断点 F9运行程序,程序断在pop之后. 使 ...

  3. 面试阿里,字节,美团必看的Spring的Bean管理详解

    IOC容器 工厂只负责创建对象,而Spring当然不仅仅是一个对象工厂,其核心是一个对象容器,其具备控制反转的能力,所以也称为IOC容器. 帮助我们存放对象,并且管理对象,包括:创建.销毁.装配,这样 ...

  4. Comparator比较器

    Comparator比较器 简介 为什么写? comparator 是javase中的接口,位于java.util包下,该接口抽象度极高,有必要掌握该接口的使用 大多数文章告诉大家comparator ...

  5. 到底为什么不要用SELECT *

    SELECT * 无论工作还是面试,说到sql优化,比说的一个问题就是,代码中sql不要出现 SELECT *,之前一直也没有深入去研究研究,为什么,只是记住了,代码中注意了,但是就在今天逛某某论坛时 ...

  6. SpringBoot---WebMvcConfigurer详解

    1. 简介 2. WebMvcConfigurer接口 2.1 addInterceptors:拦截器 2.2 addViewControllers:页面跳转 2.3 addResourceHandl ...

  7. appium服务器参数

    appium服务器初始化参数 Appium服务器初始化参数(功能) 设置能力 能力参数项 获取appPackage与appActivity 查看当前活动名称 使用uiautomatorviewer定位 ...

  8. Java蓝桥杯02——第二题集锦:生日蜡烛、星期一、方格计数、猴子分香蕉

    第二题 生日蜡烛(结果填空) 某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛. 现在算起来,他一共吹熄了236根蜡烛. 请问,他从多少岁开始过生日party的? 请填 ...

  9. 【python】Matplotlib作图常用marker类型、线型和颜色

    python作图中常常会考虑用什么颜色.marker.线型,这个资料查了又查,所以自己总结在这个地方,以便常用. 一.常用marker表示 1.普通marker 这类普通marker直接marker ...

  10. B. Irreducible Anagrams【CF 1290B】

    思路: 设tx为t类别字符的个数. ①对于长度小于2的t明显是"YES"②对于字符类别只有1个的t明显是"YES"③对于字符类别有2个的t,如左上图:如果str ...