hadoop 2.6.3 CachedDNSToSwitchMapping源代码分析
2016-01-28 10:34
316 查看
CachedDNSToSwitchMapping的作用是对已经解析了的ip地址和rack的地址的映射进行缓冲。
在resolve,没有缓冲的ip,调用rawMapping.resolve方法来解析它的networklocation.
/** * A cached implementation of DNSToSwitchMapping that takes an * raw DNSToSwitchMapping and stores the resolved network location in * a cache. The following calls to a resolved network location * will get its location from the cache. * */里面有两个变量,cache用来存储ip地址和network location的地址的映射,第二个用来对未缓冲的ip 地址进行解析。
private Map<String, String> cache = new ConcurrentHashMap<String, String>(); /** * The uncached mapping */ protected final DNSToSwitchMapping rawMapping;
在resolve,没有缓冲的ip,调用rawMapping.resolve方法来解析它的networklocation.
@Override public List<String> resolve(List<String> names) { // normalize all input names to be in the form of IP addresses names = NetUtils.normalizeHostNames(names); List <String> result = new ArrayList<String>(names.size()); if (names.isEmpty()) { return result; } List<String> uncachedHosts = getUncachedHosts(names); // Resolve the uncached hosts List<String> resolvedHosts = rawMapping.resolve(uncachedHosts); //cache them cacheResolvedHosts(uncachedHosts, resolvedHosts); //now look up the entire list in the cache return getCachedHosts(names); }getUncachedHosts,判断ip是否在缓冲里。
/** * @param names a list of hostnames to probe for being cached * @return the hosts from 'names' that have not been cached previously */ private List<String> getUncachedHosts(List<String> names) { // find out all names without cached resolved location List<String> unCachedHosts = new ArrayList<String>(names.size()); for (String name : names) { if (cache.get(name) == null) { unCachedHosts.add(name); } } return unCachedHosts; }cacheResolvedHosts对新解析的ip和networklocation进行缓冲,两个list的大小必须相等。
/** * Caches the resolved host:rack mappings. The two list * parameters must be of equal size. * * @param uncachedHosts a list of hosts that were uncached * @param resolvedHosts a list of resolved host entries where the element * at index(i) is the resolved value for the entry in uncachedHosts[i] */ private void cacheResolvedHosts(List<String> uncachedHosts, List<String> resolvedHosts) { // Cache the result if (resolvedHosts != null) { for (int i=0; i<uncachedHosts.size(); i++) { cache.put(uncachedHosts.get(i), resolvedHosts.get(i)); } } }其它的方法非常简单,如下:
/** * Get the (host x switch) map. * @return a copy of the cached map of hosts to rack */ @Override public Map<String, String> getSwitchMap() { Map<String, String > switchMap = new HashMap<String, String>(cache); return switchMap; } @Override public String toString() { return "cached switch mapping relaying to " + rawMapping; } /** * Delegate the switch topology query to the raw mapping, via * {@link AbstractDNSToSwitchMapping#isMappingSingleSwitch(DNSToSwitchMapping)} * @return true iff the raw mapper is considered single-switch. */ @Override public boolean isSingleSwitch() { return isMappingSingleSwitch(rawMapping); } @Override public void reloadCachedMappings() { cache.clear(); } @Override public void reloadCachedMappings(List<String> names) { for (String name : names) { cache.remove(name); } }
相关文章推荐
- Android 基于google Zxing实现对手机中的二维码进行扫描
- iOS开发-Objective-C与JavaScript交互
- iOS中捕获用户的崩溃日志
- 基于Android自带插入器的图形波动效果
- iOS自动布局
- iOS之自动调节输入文本框的高度
- Unity 2D 闪电特效
- android中的进程和线程
- iOS下载大文件原理解析一
- Android Design Support控件之DrawerLayout简单使用
- Android TextUtils类 (字符串相关)
- Saving data to a file in your Android application
- iOS开发常用第三方库
- Android第二天
- App Store审核指南App Store Review Guidelines
- 微信红包照片的揭秘
- Android开发中View.VISIBLE、View.INVISIBLE和View.GONE之间的区别
- MindMapper计划表怎么进行打印
- IOS通讯录分区section汉字转拼音截取首字母
- 导入adt项目进入android studio报Error:(2) Error parsing XML: prefix must not be bound to one of the reserved