分析JobInProgress中Map/Reduce任务分配

1.JobTracker能否决定给当前的TaskTracker节点分配一个Job的具体的哪一个任务?
2.什么是map本地任务?
3.nonRunningMapCache的作用是什么?
4.从TaskTracker节点上分配挂载的本地任务时,如果以前发生过该TaskTracker节点执行某一Map任务失败了的情况,则应将该Map任务该如何处理?


 
分享到:  QQ好友和群QQ好友和群 腾讯微博腾讯微博 QQ空间QQ空间
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对
欢迎加入about云群371358502、39327136,云计算爱好者群,亦可关注about云腾讯认证空间||关注本站微信
 
回复

使用道具 举报

   

681

主题

1201

帖子

6384

积分

超级版主

Rank: 8Rank: 8

积分
6384

最佳新人活跃会员热心会员

沙发
 
 楼主| 发表于 2014-4-6 23:51:53 | 只看该作者
 
众 所周知,JobTracker节点使用配置的任务调度器TaskScheduler来为某一个具体的TaskTracker节点分配任务,同时这个任务调 度器只能决定给该TaskTracker节点分配哪一个Job或者那些Job的任务以及分配多少个任务,但是它却不能决定给当前的TaskTracker 节点分配一个Job的具体的哪一个任务。

另外,针对一个具体的TaskTracker节点而言,任何一个作业都可以判断它的那些Map任务相对于该TaskTracker节点来说属于本地任务,那些Map任务是属于非本地任务的,当然,对于Reduce任务来说,是没有本地任务与非本地任务这一说法的。

因此,具体来讲就是,当任务调度器决定为一个TaskTracker节点分配一个Job的本地任务时,它会调用该JobInProgress对象的 obtainNewLocalMapTask()方法,分配一个非本地任务时,它会调用对应的obtainNewNonLocalMapTask()方 法,那么以这个TaskTracker节点在集群中的物理位置为参考,这个Job可能有多个本地任务和多个非本地任务,至于为该TaskTracker节 点分配哪一个本地或者非本地任务就由JobInProgress来决定了;当任务调度器为TaskTracker节点分配一个Job的Reduce任务 时,就会调用该Job对应的JobInProgress对象的obtainNewReduceTask()方法。至于JobInProgress对象究竟 是如何分配一个本地或非本地Map任务、Reduce任务的,那将是本文接下来要详细讲述的重点了。

1.分配作业的Map任务


     作业的Map任务之所以有本地和非本地之分,主要是因为该Map任务的输入数据和执行该Map任务的TaskTracker节点在集群中的位置有可 能同。本地与非本地Map任务是相对于执行或将要执行该任务的TaskTracker节点来说的,当任务调度器决定为一个TaskTracker节点分配 某一个Job的一个本地Map任务时,它(JobInProgress)会查找这个Job中的那些Map任务的输入数据合该TaskTracker节点在 同一台PC或机架上,那么这些Map任务对于TaskTracker节点来说就是本地任务了。这里要值得一提的是,在作业初始化的时候,就为每一个Map 任务做了一个本地化的预分配工作,即根据Map任务的输入数据的物理位置,将该Map任务挂载到对应的物理节点上,该过程的源代码为:
  1. private Map<Node, List<TaskInProgress>> createCache(JobClient.RawSplit[] splits, int maxLevel) {
  2.     Map<Node, List<TaskInProgress>> cache = new IdentityHashMap<Node, List<TaskInProgress>>(maxLevel);
  3.    
  4.     for (int i = 0; i < splits.length; i++) {
  5.       String[] splitLocations = splits[i].getLocations();//获取该数据切片坐在的物理位置(多个副本)
  6.       if (splitLocations.length == 0) {
  7.         nonLocalMaps.add(maps[i]);
  8.         continue;
  9.       }

  10.       //针对每一个副本的物理位置
  11.       for(String host: splitLocations) {
  12.         //解析副本在集群中的哪一个节点上
  13.         Node node = jobtracker.resolveAndAddToTopology(host);
  14.         LOG.info("tip:" + maps[i].getTIPId() + " has split on node:" + node);
  15.         for (int j = 0; j < maxLevel; j++) {
  16.           List<TaskInProgress> hostMaps = cache.get(node);
  17.           if (hostMaps == null) {
  18.             hostMaps = new ArrayList<TaskInProgress>();
  19.             cache.put(node, hostMaps);
  20.             hostMaps.add(maps[i]);//将Map任务挂载到该节点上
  21.           }
  22.           //去重,避免一个节点挂载了两个相同的Map任务
  23.           if (hostMaps.get(hostMaps.size() - 1) != maps[i]) {
  24.             hostMaps.add(maps[i]);
  25.           }
  26.           node = node.getParent();//获取节点的父节点(由于maxLevel的值是2,所以父节点就是rack节点)
  27.        }
  28.       }
  29.     }
  30.    
  31.     return cache;
  32.   }
复制代码


通过这样的一个预处理过程,最终Node与Map任务之间的映射关系被保存在它的一个属性nonRunningMapCache中了。当 JobInProgress为一个TaskTracker节点分配一个本地Map任务时,它可以只需要解析该TaskTracker节点在集群中的哪一个 节点node上,根据该node就可以从nonRunningMapCache中获取一个Map任务,该Map任务相对于当前这个TaskTracker 来说就是本地任务了;当JobInProgress为一个TaskTracker节点分配一个非本地Map任务时,它可以获取集群中所有的rack节点 (除它自己所在的rack外),通过这些rack节点node,就可以从nonRunningMapCache中获取一个Map任务,该Map任务相对于 当前这个TaskTracker来说就是非本地任务了。

根据上面的源代码可以看出,所谓的本地任务之分就是由maxLevel来确定的,即Map任务的输入数据与TaskTracker节点在集群中的物理距 离,在目前的版本中(Hadoop-0.20.2.0),maxLevel的默认值是2,也可由JobTracker节点的配置文件来设置,对应的配置项 为:mapred.task.cache.levels。另外,从上面的源代码可以看出,这个预处理过程也明确地定义了非本地Map任务,即map操作的 输入数据的位置为null的Map任务,这并不代表说该Map任务没有输入数据。因为,Hadoop为用户提供了自定义数据切片的API(用户自己实现 InputSplit),这里的RawSplit并没有直接保存map操作所需的输入数据的位置信息,而是对真正的InputSplit进行了封装,这告 诉我们两个很重要的情况,

一:用户在自定义Map任务的InputSplit时,应考虑这个Map任务是否可以作为某些TaskTracker节点的本地任务(比如某一个Map任务的输入数据在跨越多个节点,那么这个Map任务永远也不可能是本地任务);

二:Map任务的InputSplit实现可以为map操作带入少量的输入数据(例如,某一个Map任务需要两个输入数据,一个数据很大,另一个数据很 小,只有几百或上千Bytes,那么,用户就可以自定义一个InputSplit来保存这个小数据,很明显,用HDFS保存这样小的数据根本不划算)。这 个非本地Map任务保存在nonLocalMaps属性中。

1.1 分配本地Map任务

        JobInProgress给某一个TaskTracker节点分配一个本地Map任务的操作比较的简单,不过,这其中有一个异常情况,就是当这个 TaskTracker节点无法被解析成集群中的一个Node时,那么,本次的本地Map任务分配会被当做一次分配非本地Map任务来操作。这个过程的源 代码如下:
  1. public synchronized Task obtainNewLocalMapTask(TaskTrackerStatus tts,int clusterSize, int numUniqueHosts) throws IOException {
  2.     if (!tasksInited.get()) {
  3.       return null;
  4.     }

  5.     //为当前的计算节点获取一个本地map任务
  6.     int target = findNewMapTask(tts, clusterSize, numUniqueHosts, maxLevel, status.mapProgress());
  7.     if (target == -1) {
  8.       return null;
  9.     }

  10.     Task result = maps[target].getTaskToRun(tts.getTrackerName());
  11.     if (result != null) {
  12.       addRunningTaskToTIP(maps[target], result.getTaskID(), tts, true);
  13.     }

  14.     return result;
  15.   }

复制代码
  1. /**
  2. * 为当前的计算节点从作业的map任务集中选取一个合适的任务;
  3. * 参数maxCacheLevel决定了当前分配的是本地任务还是非本地任务
  4. */
  5. private synchronized int findNewMapTask(final TaskTrackerStatus tts, final int clusterSize, final int numUniqueHosts, final int maxCacheLevel, final double avgProgress) {

  6.     ...

  7.    Node node = jobtracker.getNode(tts.getHost());  //根据当前计算节点的主机/IP来获取其在集群拓扑结构中对应的位置节点
  8.    
  9.     //
  10.     // I) Non-running TIP :
  11.     // 1. check from local node to the root [bottom up cache lookup]
  12.     //    i.e if the cache is available and the host has been resolved
  13.     //    (node!=null)
  14.     if (node != null) {
  15.       Node key = node;    //当前待分配的map任务的输入数据所在的节点
  16.       int level = 0;
  17.       // maxCacheLevel might be greater than this.maxLevel if findNewMapTask is
  18.       // called to schedule any task (local, rack-local, off-switch or speculative)
  19.       // tasks or it might be NON_LOCAL_CACHE_LEVEL (i.e. -1) if findNewMapTask is
  20.       //  (i.e. -1) if findNewMapTask is to only schedule off-switch/speculative
  21.       // tasks
  22.       int maxLevelToSchedule = Math.min(maxCacheLevel, maxLevel);
  23.       for (level = 0;level < maxLevelToSchedule; ++level) {
  24.         List <TaskInProgress> cacheForLevel = nonRunningMapCache.get(key);    //获取节点key上还未分配的map任务
  25.         if (cacheForLevel != null) {
  26.           tip = findTaskFromList(cacheForLevel, tts, numUniqueHosts,level == 0);   //从一个map任务集中为当前的计算节点找到一个合适的任务
  27.           if (tip != null) {
  28.             // Add to running cache
  29.             scheduleMap(tip);

  30.             // remove the cache if its empty
  31.             if (cacheForLevel.size() == 0) {
  32.               nonRunningMapCache.remove(key);
  33.             }

  34.             return tip.getIdWithinJob();
  35.           }
  36.         }
  37.         
  38.         key = key.getParent();
  39.       }
  40.       
  41.       // Check if we need to only schedule a local task (node-local/rack-local)
  42.       if (level == maxCacheLevel) {
  43.         return -1;
  44.       }
  45.       
  46.     }

  47.     ...

  48. }
复制代码

还有一个值得注意的问题就是,如果该TaskTracker节点所在的Node上有Map任务时,当从该Node上分配挂载的本地任务时,如果以前发生过 该TaskTracker节点执行某一Map任务失败了的情况,则应将该Map任务从Node上删除,同时,对于无法执行或正在执行的Map任务也应该从 Node上删除,对应的源码为:

  1. private synchronized TaskInProgress findTaskFromList(Collection<TaskInProgress> tips, TaskTrackerStatus ttStatus, int numUniqueHosts, boolean removeFailedTip) {
  2.    
  3.     Iterator<TaskInProgress> iter = tips.iterator();
  4.     while (iter.hasNext()) {
  5.       TaskInProgress tip = iter.next();

  6.       // Select a tip if
  7.       //   1. runnable   : still needs to be run and is not completed
  8.       //   2. ~running   : no other node is running it
  9.       //   3. earlier attempt failed : has not failed on this host
  10.       //                               and has failed on all the other hosts
  11.       // A TIP is removed from the list if
  12.       // (1) this tip is scheduled
  13.       // (2) if the passed list is a level 0 (host) cache
  14.       // (3) when the TIP is non-schedulable (running, killed, complete)
  15.       if (tip.isRunnable() && !tip.isRunning()) {
  16.         // check if the tip has failed on this host
  17.         if (!tip.hasFailedOnMachine(ttStatus.getHost()) || tip.getNumberOfFailedMachines() >= numUniqueHosts) {
  18.           // check if the tip has failed on all the nodes
  19.           iter.remove();
  20.           return tip;
  21.         }
  22.         else if (removeFailedTip) {
  23.           // the case where we want to remove a failed tip from the host cache
  24.           // point#3 in the TIP removal logic above
  25.           iter.remove();
  26.         }
  27.       } else {
  28.         // see point#3 in the comment above for TIP removal logic
  29.         iter.remove();
  30.       }
  31.     }
  32.    
  33.     return null;
  34.   }

复制代码


1.2 分配非本地Map任务

       JobInProgress为某一个TaskTracker节点分配一个非本地Map任务相对于分配一个本地任务来说要复杂的多,它首先会先从 nonRunningMapCache中选择一个非本地任务,如果没有找到再从nonLocalMaps中选择一个任务,如果还没有找到,则判断这个作业 是否设置了hasSpeculativeMaps,如果没有设置,则不再为该TaskTracker节点分配非本地Map任务了;如果设置了,则从正在被 其它TaskTracker节点执行的本地或非本地Map任务中选一个,不过这是有优先顺序的,首先从正在运行的runningMapCache中寻找一 个本地Map任务,如果没有找到再从runningMapCache中寻找一个非本地Map任务,最后再从nonLocalRunningMaps中寻找 一个非本地Map任务,此时还没有找到的话,就不再为该TaskTracker节点分配Map任务了。这个过程的源代码如下:

  1. public synchronized Task obtainNewNonLocalMapTask(TaskTrackerStatus tts, int clusterSize, int numUniqueHosts)
  2.   throws IOException {
  3.     if (!tasksInited.get()) {
  4.       return null;
  5.     }

  6.     int target = findNewMapTask(tts, clusterSize, numUniqueHosts, NON_LOCAL_CACHE_LEVEL, status.mapProgress());
  7.     if (target == -1) {
  8.       return null;
  9.     }

  10.     Task result = maps[target].getTaskToRun(tts.getTrackerName());
  11.     if (result != null) {
  12.       addRunningTaskToTIP(maps[target], result.getTaskID(), tts, true);
  13.     }

  14.     return result;
  15.   }

  16. private synchronized int findNewMapTask(final TaskTrackerStatus tts, final int clusterSize, final int numUniqueHosts, final int maxCacheLevel, final double avgProgress) {

  17.     ...

  18.    Collection<Node> nodesAtMaxLevel = jobtracker.getNodesAtMaxLevel();

  19.     // get the node parent at max level
  20.     Node nodeParentAtMaxLevel = (node == null) ? null : JobTracker.getParentNode(node, maxLevel - 1);
  21.    
  22.     for (Node parent : nodesAtMaxLevel) {

  23.       // skip the parent that has already been scanned
  24.       if (parent == nodeParentAtMaxLevel) {
  25.         continue;
  26.       }

  27.       List<TaskInProgress> cache = nonRunningMapCache.get(parent);
  28.       if (cache != null) {
  29.         tip = findTaskFromList(cache, tts, numUniqueHosts, false);
  30.         if (tip != null) {
  31.           // Add to the running cache
  32.           scheduleMap(tip);

  33.           // remove the cache if empty
  34.           if (cache.size() == 0) {
  35.             nonRunningMapCache.remove(parent);
  36.           }
  37.           LOG.info("Choosing a non-local task " + tip.getTIPId());
  38.           return tip.getIdWithinJob();
  39.         }
  40.       }
  41.     }

  42.     // 3. Search non-local tips for a new task
  43.     tip = findTaskFromList(nonLocalMaps, tts, numUniqueHosts, false);
  44.     if (tip != null) {
  45.       // Add to the running list
  46.       scheduleMap(tip);

  47.       LOG.info("Choosing a non-local task " + tip.getTIPId());
  48.       return tip.getIdWithinJob();
  49.     }

  50.     // II) Running TIP :
  51.     if (hasSpeculativeMaps) {
  52.       long currentTime = System.currentTimeMillis();

  53.       // 1. Check bottom up for speculative tasks from the running cache
  54.       if (node != null) {
  55.         Node key = node;
  56.         for (int level = 0; level < maxLevel; ++level) {
  57.           Set<TaskInProgress> cacheForLevel = runningMapCache.get(key);
  58.           if (cacheForLevel != null) {
  59.             tip = findSpeculativeTask(cacheForLevel, tts, avgProgress, currentTime, level == 0);
  60.             if (tip != null) {
  61.               if (cacheForLevel.size() == 0) {
  62.                 runningMapCache.remove(key);
  63.               }
  64.               return tip.getIdWithinJob();
  65.             }
  66.           }
  67.           key = key.getParent();
  68.         }
  69.       }

  70.       // 2. Check breadth-wise for speculative tasks
  71.       for (Node parent : nodesAtMaxLevel) {
  72.         // ignore the parent which is already scanned
  73.         if (parent == nodeParentAtMaxLevel) {
  74.           continue;
  75.         }

  76.         Set<TaskInProgress> cache = runningMapCache.get(parent);
  77.         if (cache != null) {
  78.           tip = findSpeculativeTask(cache, tts, avgProgress, currentTime, false);
  79.           if (tip != null) {
  80.             // remove empty cache entries
  81.             if (cache.size() == 0) {
  82.               runningMapCache.remove(parent);
  83.             }
  84.             LOG.info("Choosing a non-local task " + tip.getTIPId() + " for speculation");
  85.             return tip.getIdWithinJob();
  86.           }
  87.         }
  88.       }

  89.       // 3. Check non-local tips for speculation
  90.       tip = findSpeculativeTask(nonLocalRunningMaps, tts, avgProgress, currentTime, false);
  91.       if (tip != null) {
  92.         LOG.info("Choosing a non-local task " + tip.getTIPId() + " for speculation");
  93.         return tip.getIdWithinJob();
  94.       }
  95.     }
  96.    
  97.     return -1;
  98. }
复制代码



2. 分配作业的Reduce任务


      由于 Reduce任务的输入数据来源于该作业所有的Map任务的输出,而执行Map任务的TaskTracker节点将map的输出保存在自己本地,所以 Reduce任务的输入数据在绝大多数情况下不可能都在某一个TaskTracker节点上,因此对于任何一个TaskTracker节点来说没有本地和 非本地的Reduce任务之分。JobInProgress为某一个TaskTracker节点分配一个Reduce任务的操作就相当的简单了,这个过程 类似于分配非本地Map任务。

它首先直接从nonRunningReduces中寻找一个任务,如果没有找到则在看这个作业设置了hasSpeculativeReduces没有,若 没有则不分配了;若设置了,则从runningReduces中寻找一个正在被其它TaskTracker节点执行的Reduce任务分配给该 TaskTracker节点。该过程对应的源代码如下:

  1. public synchronized Task obtainNewReduceTask(TaskTrackerStatus tts, int clusterSize, int numUniqueHosts) throws IOException {
  2.     if (status.getRunState() != JobStatus.RUNNING) {
  3.       return null;
  4.     }
  5.    
  6.     // Ensure we have sufficient map outputs ready to shuffle before
  7.     // scheduling reduces
  8.     if (!scheduleReduces()) {
  9.       return null;
  10.     }

  11.     int  target = findNewReduceTask(tts, clusterSize, numUniqueHosts, status.reduceProgress());
  12.     if (target == -1) {
  13.       return null;
  14.     }
  15.    
  16.     Task result = reduces[target].getTaskToRun(tts.getTrackerName());
  17.     if (result != null) {
  18.       addRunningTaskToTIP(reduces[target], result.getTaskID(), tts, true);
  19.     }

  20.     return result;
  21.   }

  22. private synchronized int findNewReduceTask(TaskTrackerStatus tts, int clusterSize, int numUniqueHosts, double avgProgress) {
  23.     if (numReduceTasks == 0) {
  24.       return -1;
  25.     }

  26.     String taskTracker = tts.getTrackerName();
  27.     TaskInProgress tip = null;
  28.    
  29.     // Update the last-known clusterSize
  30.     this.clusterSize = clusterSize;

  31.     if (!shouldRunOnTaskTracker(taskTracker)) {
  32.       return -1;
  33.     }

  34.     long outSize = resourceEstimator.getEstimatedReduceInputSize();
  35.     long availSpace = tts.getResourceStatus().getAvailableSpace();
  36.     if(availSpace < outSize) {
  37.       LOG.warn("No local disk space for reduce task. TaskTracker[" + taskTracker + "] has " + availSpace + " bytes free; but we expect reduce input to take " + outSize);
  38.       return -1; //see if a different TIP might work better.
  39.     }
  40.    
  41.     // 1. check for a never-executed reduce tip
  42.     // reducers don't have a cache and so pass -1 to explicitly call that out
  43.     tip = findTaskFromList(nonRunningReduces, tts, numUniqueHosts, false);
  44.     if (tip != null) {
  45.       scheduleReduce(tip);
  46.       return tip.getIdWithinJob();
  47.     }

  48.     // 2. check for a reduce tip to be speculated
  49.     if (hasSpeculativeReduces) {
  50.       tip = findSpeculativeTask(runningReduces, tts, avgProgress, System.currentTimeMillis(), false);
  51.       if (tip != null) {
  52.         scheduleReduce(tip);
  53.         return tip.getIdWithinJob();
  54.       }
  55.     }

  56.     return -1;
  57.   }

  58. private synchronized TaskInProgress findSpeculativeTask(Collection<TaskInProgress> list, TaskTrackerStatus ttStatus, double avgProgress, long currentTime, boolean shouldRemove) {
  59.    
  60.     Iterator<TaskInProgress> iter = list.iterator();

  61.     while (iter.hasNext()) {
  62.       TaskInProgress tip = iter.next();
  63.       // should never be true! (since we delete completed/failed tasks)
  64.       if (!tip.isRunning()) {
  65.         iter.remove();
  66.         continue;
  67.       }

  68.       //当前TaskTracker节点没有运行该任务
  69.       if (!tip.hasRunOnMachine(ttStatus.getHost(), ttStatus.getTrackerName())) {
  70.         if (tip.hasSpeculativeTask(currentTime, avgProgress)) {
  71.           // In case of shared list we don't remove it. Since the TIP failed
  72.           // on this tracker can be scheduled on some other tracker.
  73.           if (shouldRemove) {
  74.             iter.remove(); //this tracker is never going to run it again
  75.           }
  76.           return tip;
  77.         }
  78.       } else {
  79.         // Check if this tip can be removed from the list.
  80.         // If the list is shared then we should not remove.
  81.         if (shouldRemove) {
  82.           // This tracker will never speculate this tip
  83.           iter.remove();
  84.         }
  85.       }
  86.     }
  87.     return null;
  88.   }
  89.   
复制代码

在目前的Hadoop版本设计中,作业中任务的调度细节被封装到了JobInProgress中,使得作业调度器TaskScheduler可完全控制的 调度粒度限制在Job级,同时JobInProgress为上层的TaskScheduler实现的任务调度提供API,这样做就大大地降低了用户自行设 计TaskScheduler的门槛,即可以很容易的根据自己的应用场景集中在作业级别上实现合适的调度策略。
posted @ 2014-07-23 16:54  弹着钢琴设计  阅读(315)  评论(0编辑  收藏  举报