TaskTracker中HttpServer doGet源码分析

   TaskTracker节点的内部Http服务组件主要提供两个功能:1)./logtask,获取某一个Task的执行日志;2)./mapOutput,获取某一个Task的map输出数据。对于用户来说,Http服务组件的/logtask功能不是必须的,但是它的/mapOutput功能对于整个Map-Reduce框架实现来说则是至关重要的,因为每一个Job的每一个Reduce任务就是通过该服务来获取它所需要的处理数据(也就是同属一个Job的Map任务的输出数据)的。如果不是Http服务组件负责提供该功能的话,我们完全可以取消该组件来优化TaskTracker节点的性能。所以,下面我将主要围绕Http服务组件的/mapOutput功能来展开。

    作业的Reduce任务在shuffle阶段主要负责从执行该作业的Map任务的TaskTracker节点上抓取属于自己的Map输出数据,当然前提是这些Map任务已经被成功执行了。至于Reduce任务是如何知道作业的那些Map任务完成了,这一点在前面的博文中有详细的谈到。当Reduce任务发现一个完成的Map任务时,它会向负责执行该Map任务的TaskTracker节点发送一个Http请求来获取这个Map输出中属于自己的数据,也就是说作业的Map/Reduce任务之间的数据是通过Http协议来传输的。这个请求连接的URL请求格式是:http://*:*/mapOutput?job=jobId&map=mapId&reduce=partition

   TaskTracker节点的Http服务组件来接受到/mapOutput的http请求之后,就会交给它的一个后台线程来处理。这个后台线程最终会调用对应的MapOutputServlet来处理,该处理的详细操作如下:

[html]  view plain copy
  1.     private static final int MAX_BYTES_TO_READ = 64 * 1024;  
  2.     @Override  
  3.     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  4.       String mapId = request.getParameter("map");  
  5.       String reduceId = request.getParameter("reduce");  
  6.       String jobId = request.getParameter("job");  
  7.   
  8.       if (jobId == null) {  
  9.         throw new IOException("job parameter is required");  
  10.       }  
  11.   
  12.       if (mapId == null || reduceId == null) {  
  13.         throw new IOException("map and reduce parameters are required");  
  14.       }  
  15.         
  16.       ServletContext context = getServletContext();  
  17.       int reduce = Integer.parseInt(reduceId);  
  18.       byte[] buffer = new byte[MAX_BYTES_TO_READ];  
  19.       // true iff IOException was caused by attempt to access input  
  20.       boolean isInputException = true;  
  21.       OutputStream outStream = null;  
  22.       FSDataInputStream mapOutputIn = null;  
  23.    
  24.       long totalRead = 0;  
  25.       ShuffleServerMetrics shuffleMetrics = (ShuffleServerMetrics) context.getAttribute("shuffleServerMetrics");  
  26.       TaskTracker tracker = (TaskTracker) context.getAttribute("task.tracker");  
  27.   
  28.       try {  
  29.         shuffleMetrics.serverHandlerBusy();  
  30.         //创建输出响应流  
  31.         outStream = response.getOutputStream();  
  32.           
  33.         JobConf conf = (JobConf) context.getAttribute("conf");  
  34.         //TaskTracker节点的本地目录,用来存储Map/Reduce任务的中间结果  
  35.         LocalDirAllocator lDirAlloc = (LocalDirAllocator)context.getAttribute("localDirAllocator");  
  36.         FileSystem rfs = ((LocalFileSystem) context.getAttribute("local.file.system")).getRaw();  
  37.   
  38.         //通过JobId和TaskId就可以找到Map任务的map输出文件及索引文件  
  39.         Path indexFileName = lDirAlloc.getLocalPathToRead(TaskTracker.getIntermediateOutputDir(jobId, mapId) + "/file.out.index", conf);  
  40.         Path mapOutputFileName = lDirAlloc.getLocalPathToRead(TaskTracker.getIntermediateOutputDir(jobId, mapId) + "/file.out", conf);  
  41.   
  42.         /**  
  43.          * Read the index file to get the information about where  
  44.          * the map-output for the given reducer is available.   
  45.          */  
  46.         IndexRecord info = tracker.indexCache.getIndexInformation(mapId, reduce,indexFileName);  
  47.             
  48.         //set the custom "from-map-task" http header to the map task from which  
  49.         //the map output data is being transferred  
  50.         response.setHeader(FROM_MAP_TASK, mapId);  
  51.           
  52.         //set the custom "Raw-Map-Output-Length" http header to   
  53.         //the raw (decompressed) length  
  54.         response.setHeader(RAW_MAP_OUTPUT_LENGTH,Long.toString(info.rawLength));  
  55.   
  56.         //set the custom "Map-Output-Length" http header to   
  57.         //the actual number of bytes being transferred  
  58.         response.setHeader(MAP_OUTPUT_LENGTH, Long.toString(info.partLength));  
  59.   
  60.         //set the custom "for-reduce-task" http header to the reduce task number  
  61.         //for which this map output is being transferred  
  62.         response.setHeader(FOR_REDUCE_TASK, Integer.toString(reduce));  
  63.           
  64.         //use the same buffersize as used for reading the data from disk  
  65.         response.setBufferSize(MAX_BYTES_TO_READ);  
  66.           
  67.         /**  
  68.          * Read the data from the sigle map-output file and  
  69.          * send it to the reducer.  
  70.          */  
  71.         //open the map-output file  
  72.         LOG.debug("open MapTask["+mapId+"]'s output file: "+mapOutputFileName);  
  73.         mapOutputIn = rfs.open(mapOutputFileName);  
  74.   
  75.         //seek to the correct offset for the reduce  
  76.         mapOutputIn.seek(info.startOffset);  
  77.         long rem = info.partLength;  
  78.         int len = mapOutputIn.read(buffer, 0, (int)Math.min(rem, MAX_BYTES_TO_READ));  
  79.         while (rem > 0 && len >= 0) {  
  80.           rem -len;  
  81.           try {  
  82.             shuffleMetrics.outputBytes(len);  
  83.             outStream.write(buffer, 0, len);  
  84.             outStream.flush();  
  85.           } catch (IOException ie) {  
  86.             isInputException = false;  
  87.             throw ie;  
  88.           }  
  89.           totalRead += len;  
  90.           len = mapOutputIn.read(buffer, 0, (int)Math.min(rem, MAX_BYTES_TO_READ));  
  91.         }  
  92.   
  93.         LOG.info("Sent out " + totalRead + " bytes for reduce: " + reduce + " from map: " + mapId + " given " + info.partLength + "/" + info.rawLength);  
  94.       } catch (IOException ie) {  
  95.         Log log = (Log) context.getAttribute("log");  
  96.         String errorMsg = ("getMapOutput(" + mapId + "," + reduceId + ") failed :\n"+ StringUtils.stringifyException(ie));  
  97.         log.warn(errorMsg);  
  98.         //异常是由于map输出造成的,所以通知TaskTracker该Map任务的输出发生了错误  
  99.         if (isInputException) {  
  100.           tracker.mapOutputLost(TaskAttemptID.forName(mapId), errorMsg);  
  101.         }  
  102.         response.sendError(HttpServletResponse.SC_GONE, errorMsg);  
  103.         shuffleMetrics.failedOutput();  
  104.         throw ie;  
  105.       } finally {  
  106.         if (null != mapOutputIn) {  
  107.           mapOutputIn.close();  
  108.         }  
  109.         shuffleMetrics.serverHandlerFree();  
  110.         if (ClientTraceLog.isInfoEnabled()) {  
  111.           ClientTraceLog.info(String.format(MR_CLIENTTRACE_FORMAT, request.getLocalAddr() + ":" + request.getLocalPort(), request.getRemoteAddr() + ":" + request.getRemotePort(), totalRead, "MAPRED_SHUFFLE", mapId));  
  112.         }  
  113.       }  
  114.         
  115.       outStream.close();  
  116.       shuffleMetrics.successOutput();  
  117.     }  
  118.   
   这个处理过程可表示如下图:


     从上面的代码可以看出,MapOutputServlet为了提高响应时间,对Map任务的输出索引文件信息做了缓存,这里想要解释一下的就是Map任务的输出索引文件file.out.index到底存储了什么重要信息。对于map操作的输出key-value,用户通常会根据应用的需要来为作业的Map输出设置一个partitioner,这个绝对了map操作的每一个key-value输出将要交给哪一个Reduce任务来处理。交给相同的Reduce处理的key-value会存储在Map任务输出文件file.out中的一块连续的的位置,那么这个数据块在file.out中的起始位置、原始长度(map的输出由于用户的设置可能被压缩了)、实际长度就会存储在对应的file.out.index文件中。这样设计的合理性在于,1).file.out.index文件是小文件,缓存的它的信息不回消耗多少内存;2).当作业的一个Map任务完成时,该作业的所有Reduce任务都会马上来获取属于他们的map输出数据,这非常符合局部性原理。同时这个缓存空间又被限制了大小,这样设计的主要目的是通过先进先出的缓存策略来自动的删除那些已经无用的Map输出索引信息,这是因为绝大部分作业的生命周期是很短暂的,当一个作业被完成时,它的所有Map任务输出(即中间数据)就没有任何存在的意义了而被TaskTracker节点给删除了。这个缓存的大小可以由TaskTracker的配置文件来设置,对应的配置项为:mapred.tasktracker.indexcache.mb

    当MapOutputServlet在给Reduce任务发送属于它的Map输出数据时,如果发生了读取该Map输出数据异常,则会通知给TaskTracker节点,而TaskTracker节点会认为这个Map任务执行失败了稍后把这个信息报告给JobTracker节点。至于JobTracker节点再是如何处理的,前面的博文有详细的阐述。


ZZ from http://blog.youkuaiyun.com/xhh198781/article/details/7471048

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值