2021SC@SDUSC
本次我将对hadoop中的common部分中Configuration第二部分内容进行解析:
private static AtomicReferencedeprecationContext = new AtomicReference ( new DeprecationContext(null, defaultDeprecations));
这是一个全局的DeprecationContex对象,它会将默认被遗弃的key加载进去并且他是原子性的
实际上,Configuration类的方法按照访问控制可以分为两类,分别是private和public,private主要是public方法中的一些工具,其中有三个构造方法:
上图第二个可以指定是否加载默认设置,它的默认值为true,第三个是用第一个的configuration对象构造的一个新的configuration对象。
下面是添加配置资源的几个方法:
上图几种资源配置添加的方法分别是添加默认的或者指定的各种来源的配置资源,而途中reloadConfiguration()则是一个清除所有原有配置信息,方便重新加载配置信息的一个方法,这一个对值的覆盖中或者新的配置资源覆盖之前的配置资源有很大的帮助。
静态addDeprecations方法
该addDeprecation方法巧妙的使用了无锁的方法,但是为了保证数据的安全性能,配置了如下代码的安全保证:
public static void addDeprecations(DeprecationDelta[] deltas) {
DeprecationContext prev, next;
do {
prev = deprecationContext.get();
next = new DeprecationContext(prev, deltas);
} while (!deprecationContext.compareAndSet(prev, next));
}
图中的compareAndSet方法是如果当前对象和prev相等时,则更新当前对象为next
setDeprecatedProperties
分析其中的源码,我们会发现,setDeprecatedProperties的作用就是为了更新overlay和properties,使得当我们在获得对应的key时,能够得到最新的状态
public void setDeprecatedProperties() {
DeprecationContext deprecations = deprecationContext.get();
Properties props = getProps();
Properties overlay = getOverlay();
for (Map.Entry entry :
deprecations.getDeprecatedKeyMap().entrySet()) {
String depKey = entry.getKey();
if (!overlay.contains(depKey)) {
for (String newKey : entry.getValue().newKeys) {
String val = overlay.getProperty(newKey);
if (val != null) {
props.setProperty(depKey, val);
overlay.setProperty(depKey, val);
break;
}
}
}
}
}
handleDeprecation
这部分内容就是来判断得到的key是否是被遗弃的,如果是的话,将得到建议用的新的key,否则则更新overlay、properties,并返回建议使用的新的ke数组,所以用handleDeprecation执行刷新操作。
private String[] handleDeprecation(DeprecationContext deprecations,
String name) {
if (null != name) {
name = name.trim();
}
// Initialize the return value with requested name
String[] names = new String[]{name};
// Deprecated keys are logged once and an updated names are returned
DeprecatedKeyInfo keyInfo = deprecations.getDeprecatedKeyMap().get(name);
if (keyInfo != null) {
if (!keyInfo.getAndSetAccessed()) {
logDeprecation(keyInfo.getWarningMessage(name));
}
// Override return value for deprecated keys
names = keyInfo.newKeys;
}
static{}静态代码块
分析这部分源代码我们可以发现:
如果classpath下存在hadoop-site.xml,log4j会打印警告信息,提示没有加载到defaultResources。
默认加载两个核心配置文件为core-default.xml、core-site.xml
static {
// Add default resources
addDefaultResource("core-default.xml");
addDefaultResource("core-site.xml");
// print deprecation warning if hadoop-site.xml is found in classpath
ClassLoader cL = Thread.currentThread().getContextClassLoader();
if (cL == null) {
cL = Configuration.class.getClassLoader();
}
if (cL.getResource("hadoop-site.xml") != null) {
LOG.warn("DEPRECATED: hadoop-site.xml found in the classpath. " +
"Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, "
+ "mapred-site.xml and hdfs-site.xml to override properties of " +
"core-default.xml, mapred-default.xml and hdfs-default.xml " +
"respectively");
addDefaultResource("hadoop-site.xml");
}
}



