${mapred.local.dir}选择策略--Map Task存放中间结果
上篇说了block在DataNode配置有多个${dfs.data.dir}时的存储策略,本文主要介绍TaskTracker在配置有多个${mapred.local.dir}时的选择策略。
mapred-site.xml
<property>
<name>mapred.local.dir</name>
<value>/mnt/localdir1/local,/mnt/localdir2/local,/mnt/localdir3/local</value>
</property>
当${mapred.local.dir}配置有多个目录分别用来挂载不同的硬盘时,Map Task的结果应该存放在哪个目录中?首先还是看一下方法的调用层次,如下图所示:

下面分析这两个方法:
/** Get a path from the local FS. If size is known, we go
* round-robin over the set of disks (via the configured dirs) and return
* the first complete path which has enough space.
*
* If size is not known, use roulette selection -- pick directories
* with probability proportional to their available space.
*/
public synchronized
Path getLocalPathForWrite(String pathStr, long size,
Configuration conf, boolean checkWrite
) throws IOException {
//检查task目录是否有变化
confChanged(conf);
int numDirs = localDirsPath.length; //获取${mapred.local.dir}目录的个数
int numDirsSearched = 0; //表示已经搜索过的次数
//remove the leading slash from the path (to make sure that the uri
//resolution results in a valid path on the dir being checked)
if (pathStr.startsWith("/")) { //是指output/spill0.out文件
pathStr = pathStr.substring(1);
}
Path returnPath = null;
Path path = new Path(pathStr); //当要写入的数据量大小未知时
if(size == SIZE_UNKNOWN) { //do roulette selection: pick dir with probability
//proportional to available size
long[] availableOnDisk = new long[dirDF.length];
long totalAvailable = 0; //build the "roulette wheel"
for(int i =0; i < dirDF.length; ++i) {
//分别计算每一个${mapred.local.dir}目录可用大小,并计算总的可用大小
availableOnDisk[i] = dirDF[i].getAvailable();
totalAvailable += availableOnDisk[i];
} // Keep rolling the wheel till we get a valid path
Random r = new java.util.Random();
while (numDirsSearched < numDirs && returnPath == null) {
long randomPosition = Math.abs(r.nextLong()) % totalAvailable;
int dir = 0;
while (randomPosition > availableOnDisk[dir]) {
randomPosition -= availableOnDisk[dir];
dir++;
}
dirNumLastAccessed = dir; //表示上次访问过的目录
//从${mapred.local.dir}中选择一个目录,在其下创建output/spill0.out文件
returnPath = createPath(path, checkWrite);
if (returnPath == null) { //如果创建失败(可能存在disk read-only的情况)
totalAvailable -= availableOnDisk[dir];
availableOnDisk[dir] = 0; // skip this disk
numDirsSearched++;
}
}
} else { //写入的数据量已知
while (numDirsSearched < numDirs && returnPath == null) {
long capacity = dirDF[dirNumLastAccessed].getAvailable();
if (capacity > size) {
returnPath = createPath(path, checkWrite);
}
//使用轮流的方式来选择${mapred.local.dir}
dirNumLastAccessed++;
dirNumLastAccessed = dirNumLastAccessed % numDirs;
numDirsSearched++;
}
}
if (returnPath != null) {
return returnPath;
} //no path found
throw new DiskErrorException("Could not find any valid local " +
"directory for " + pathStr);
}
confChanged(conf)方法首先检查原来的目录配置是否改变,这个下面说;然后给numDirs赋值,它表示总的${mapred.local.dir}目录个数,localDirsPath数组变量在confChanged(conf)方法中被更新了;接着在准备创建output/spill0.out文件,这个文件就是Map Task的运算结果在缓冲区写满之后spill到disk生成的文件,序号0代表序号,最后会将多个spill文件合成一个file.out文件;接下来就要选择${mapred.local.dir}目录了。其过程如下:
1、如果要写入的数据量大小未知时:
a) 计算dirDF数组中每个元素的剩余大小,并计算所有元素的总大小totalAvailable;
b) (循环)生成一个Long类型随机正数,该随机数对总大小totalAvailable取余后得randomPosition。
(内层循环)若randomPosition > 某个disk剩余量,则randomPosition减去该disk剩余量,并与下一个disk剩余量比较……
c) 选择了某个disk之后,如果这个disk不能创建文件,则排除这个disk,重新选择disk(总共尝试localDirsPath.length次)
2、要写入的数据量大小已知时:将${mapred.local.dir}组织成一个数组,轮流的使用数组中的目录。dirNumLastAccessed表示上次访问过的目录;
下面反过来分析下confChanged()方法。
实际上该方法中的获取到的localDirs数组所代表的目录,是Map Task或Reduce Task的工作目录(即attempt_jobid_taskid_m_attemptid*)。因为每次不同的Task会使用不同的工作目录。所以每次不同的Task来read/write数据时,该方法都会为他们构造工作目录。具体代码如下:
/** This method gets called everytime before any read/write to make sure
* that any change to localDirs is reflected immediately.
*/
private synchronized void confChanged(Configuration conf
) throws IOException {
//contextCfgItemName="mapred.local.dir"
String newLocalDirs = conf.get(contextCfgItemName);
if (!newLocalDirs.equals(savedLocalDirs)) { //savedLocalDirs代表上个task的工作目录
String[] localDirs = conf.getStrings(contextCfgItemName);
localFS = FileSystem.getLocal(conf);
int numDirs = localDirs.length; //${mapred.local.dir}目录的个数
ArrayList<String> dirs = new ArrayList<String>(numDirs);
ArrayList<DF> dfList = new ArrayList<DF>(numDirs);
for (int i = 0; i < numDirs; i++) {
try {
// filter problematic directories
Path tmpDir = new Path(localDirs[i]);
//检查task的工作目录(attempt....)是否存在,如果不存在,则新建
if(localFS.mkdirs(tmpDir)|| localFS.exists(tmpDir)) {
try {
DiskChecker.checkDir(new File(localDirs[i]));
dirs.add(localDirs[i]);
dfList.add(new DF(new File(localDirs[i]), 30000));
} catch (DiskErrorException de) {
LOG.warn( localDirs[i] + "is not writable\n" +
StringUtils.stringifyException(de));
}
} else {
LOG.warn( "Failed to create " + localDirs[i]);
}
} catch (IOException ie) {
LOG.warn( "Failed to create " + localDirs[i] + ": " +
ie.getMessage() + "\n" + StringUtils.stringifyException(ie));
} //ignore
}
localDirsPath = new Path[dirs.size()];
for(int i=0;i<localDirsPath.length;i++) {
localDirsPath[i] = new Path(dirs.get(i));
}
dirDF = dfList.toArray(new DF[dirs.size()]);
savedLocalDirs = newLocalDirs; //保存此次的task工作目录 // randomize the first disk picked in the round-robin selection
//因为该task所有的工作目录都遍历过了,所以随机选择一个目录作为最后访问过的目录
dirNumLastAccessed = dirIndexRandomizer.nextInt(dirs.size());
}
}
上面代码中的localDirsPath变量的内容如下所示:
/mapred/local/dir1/taskTracker/hadoop/jobcache/job_local1424926029_0001/attempt_local1424926029_0001_m_000000_0
/mapred/local/dir2/taskTracker/hadoop/jobcache/job_local1424926029_0001/attempt_local1424926029_0001_m_000000_0
/mapred/local/dir3/taskTracker/hadoop/jobcache/job_local1424926029_0001/attempt_local1424926029_0001_m_000000_0
可以看到,这些路径中就只有${mapred.local.dir}不同,其下的目录结构都完全一样。
说一下Task的工作目录。TaskTracker会在${mapred.local.dir}下生成相同的目录结构用来存放Map Task处理的结果数据,然后在Job完成时清理掉这些数据和目录。
Task的工作目录就是指:${mapred.local.dir}/taskTracker/${user}/jobcache/jobID/taskID目录。在这个目录下的output文件夹中就存放着Map Task的结果,并以上述方式使用这些目录。
才开始时,output目录下只有spill0.out文件(0代表序号),之后可能会产生多个spill文件。当Map Task执行完毕后会把所有属于该Task(即同一个taskid目录下)的spill文件合并成file.out文件。
变量dirDF代表了一个DF数组,DF类代表了disk的使用情况(使用"df -k"命令得到),包含的属性如下:
/**
* Filesystem disk space usage statistics. Uses the unix 'df' program to get
* mount points, and java.io.File for space utilization. Tested on Linux,
* FreeBSD, Cygwin.
*/
public class DF extends Shell { private final String dirPath;
private final File dirFile;
private String filesystem;
private String mount;
分析完写数据的部分后,读数据的部分就很简单了。使用getLocalPathToRead()方法,从整个${mapred.local.dir}/taskTracker/${user}/jobcache/jobID/taskID中寻找所需要的文件,找到后返回其路径信息即可。
${mapred.local.dir}的选择策略也有以下问题:
1、disk是只读的
2、Disk没有足够空间了(多个线程共享disk)
本文基于hadoop1.2.1
如有错误,还请指正
转载请注明出处:http://www.cnblogs.com/gwgyk/p/4124980.html
随机推荐
- 链表的C语言实现
#ifndef _CONST_H_#define _CONST_H_ #include <stdio.h>#include <stdlib.h> typedef enum { ...
- python 中的 try/except/else/finally语句
1.python中try/except/else/finally正常的语句是这样的: try: normal excute block except A: Except A handle except ...
- jsp页面输出序号
<c:forEach items="${tests}" var="test" varStatus="s"> <li> ...
- 【C语言训练】尼科彻斯定理
题目描述验证尼科彻斯定理,即:任何一个正整数的立方都可以写成一串连续奇数的和. 输入任一正整数 输出该数的立方分解为一串连续奇数的和 样例输入13样例输出13*13*13=2197=157+159+1 ...
- Date Picker和UITool Bar的使用
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Verdana } span.s1 { } span.s2 { background-colo ...
- Kerberos是怎么工作的?
Kerberos是一种计算机网络授权协议,用来在非安全网络中,对个人通信以安全的手段进行身份认证. 采用客户端/服务器结构,并且能够进行相互认证,即客户端和服务器端均可对对方进行身份认证. 关键要素 ...
- [原]Linux ssh远程连接断开问题处理办法
我们在通过远程连接操作Linux server的时候,有可能过一段时间忘记操作,便会发生ssh断开的问题. 而如果是本地的server,比较好办,直连设备kill掉ssh,踢掉无效用户连接,再次链接即 ...
- Top命令 -转
Windows下的任务管理器虽然不好用(个人更喜欢Process Explorer些),但也算方便,可以方便的查看进程,CPU,内存...也可以很容易的结束进程 没有图形化界面下的Linux,也有命令 ...
- Eclipse 实现关键字自动补全功能
一般默认情况下,Eclipse ,MyEclipse 的代码提示功能是比Microsoft Visual Studio的差很多的,主要是Eclipse ,MyEclipse本身有很多选项是默认关闭的, ...
- HTTP协议的报文结构
HTTP 有两类报文: (1) 请求报文----从客户向服务器发送请求报文,见图6-12(a). (2) 响应报文----从服务器到客户的回答,见图6-12(b). 由于 HTTP是面向文本的(tex ...