MapReduce——客戶端提交任務源碼分析


計算向數據移動

MR程序並不會在客戶端執行任何的計算操作,它是為計算工作做好准備,例如計算出切片信息,直接影響到Map任務的並行度。

在Driver中提交任務時,會寫到這樣的語句:

  boolean result = job.waitForCompletion(true);

進入到waitForCompletion中:

public boolean waitForCompletion(boolean verbose) throws IOException, InterruptedException,
ClassNotFoundException {
    if (state == JobState.DEFINE) {
       // 提交任務語句
      submit();
    }
                                 ..............

繼續跟進 submit():

 public void submit() throws IOException, InterruptedException, ClassNotFoundException {
     
    ensureState(JobState.DEFINE);
    setUseNewAPI();
    connect();
     
    final JobSubmitter submitter = 
        getJobSubmitter(cluster.getFileSystem(), cluster.getClient());
    status = ugi.doAs(new PrivilegedExceptionAction<JobStatus>() {
      public JobStatus run() throws IOException, InterruptedException, 
      ClassNotFoundException {
          // 執行提交任務
        return submitter.submitJobInternal(Job.this, cluster);
      }
    });
                    ..............
   }

上面代碼可以看出,客戶端經過連接集群,獲得任務提交器submitter后執行了submitJobInternal(Job.this, cluster)方法,進入看(其實我只想看切片方法)

 /**
   * Internal method for submitting jobs to the system.
   * The job submission process involves:
   *   1、Checking the input and output specifications of the job.
   *   2、Computing the InputSplits for the job.
   *   3、Setup the requisite accounting information for the 
   *      DistributedCache of the job, if necessary.
   *   4、Copying the job's jar and configuration to the map-reduce system
   *      directory on the distributed file-system. 
   *   5、Submitting the job to the JobTracker and optionally
   *   monitoring it's status.
   */ 
..............
// Create the splits for the job
      LOG.debug("Creating splits at " + jtFs.makeQualified(submitJobDir));
      int maps = writeSplits(job, submitJobDir);
      conf.setInt(MRJobConfig.NUM_MAPS, maps);
      LOG.info("number of splits:" + maps);
..............

從這個方法頭上的注釋信息可以看到,在真正執行任務之前,客戶端做了這么5件事,稍微翻譯一下:

  • 檢查作業的輸入和輸出規范;
  • 計算輸入切片的數量;
  • 如有必要,為作業的DistributedCache 設置必要的記帳信息;
  • 將作業的 jar 和配置復制到分布式文件系統上的 map-reduce system 目錄;
  • 將作業提交給 JobTracker 並可選擇監控它的狀態

可以看到執行切片的方法時writeSplits(job, submitJobDir)

private int writeSplits(org.apache.hadoop.mapreduce.JobContext job,Path jobSubmitDir) throws IOException,InterruptedException, ClassNotFoundException {
    JobConf jConf = (JobConf)job.getConfiguration();
    int maps;
    if (jConf.getUseNewMapper()) {
      maps = writeNewSplits(job, jobSubmitDir);
    } else {
      maps = writeOldSplits(jConf, jobSubmitDir);
    }
    return maps;
  }

也有新舊API的區分,看新的writeNewSplits(job, jobSubmitDir)

private <T extends InputSplit>
  int writeNewSplits(JobContext job, Path jobSubmitDir) throws IOException,
      InterruptedException, ClassNotFoundException {
    ..................
        // 只看切片方法 
    List<InputSplit> splits = input.getSplits(job);
    T[] array = (T[]) splits.toArray(new InputSplit[splits.size()]); 
    ..............
        // 返回值是數組的長度,也就是切片的個數,也就是mapTask的並行度
    return array.length;
  }

進入切片方法,方法太長了,刪除部分,留下核心業務邏輯。這個得好好說說

  public List<InputSplit> getSplits(JobContext job) throws IOException {
      
    // 如果沒有指定的話,minSize = 1
    long minSize = Math.max(getFormatMinSplitSize(), getMinSplitSize(job));
    // 如果沒有指定的話,maxSize = Long.Max
    long maxSize = getMaxSplitSize(job);

    // generate splits
    List<InputSplit> splits = new ArrayList<InputSplit>();
    // FileStatus這個概念來自於HDFS,存儲客戶端提交文件的元數據
    List<FileStatus> files = listStatus(job);
    for (FileStatus file: files) {
      // 獲取到文件的路徑
      Path path = file.getPath();
      // 獲取到文件的長度
      long length = file.getLen();
      if (length != 0) {
        // 數據塊位置數組,用於存儲該文件對應的數據塊的位置
        BlockLocation[] blkLocations;
        if (file instanceof LocatedFileStatus) {
          blkLocations = ((LocatedFileStatus) file).getBlockLocations();
        } else {
          FileSystem fs = path.getFileSystem(job.getConfiguration());
          blkLocations = fs.getFileBlockLocations(file, 0, length);
        }
        if (isSplitable(job, path)) {  // 沒有指定,默認是可分片的
          long blockSize = file.getBlockSize();
            // 返回默認值:切片大小 = 塊大小
          long splitSize = computeSplitSize(blockSize, minSize, maxSize);
           // 獲取整個文件的長度,用於計算切片的偏移量
          long bytesRemaining = length;
           // SPLIT_SLOP 的大小是1.1
           // 這個判斷表達式的含義是如果剩余的塊體積大大於1.1倍的切片大小,繼續切片
          while (((double) bytesRemaining)/splitSize > SPLIT_SLOP) {
              // 在這計算了一步塊索引
            int blkIndex = getBlockIndex(blkLocations, length-bytesRemaining);
              
//-----------getBlockIndex() begin--------------------------------------------
protected int getBlockIndex(BlockLocation[] blkLocations, long offset) {
      for (int i = 0 ; i < blkLocations.length; i++) {
      // is the offset inside this block?
      if ((blkLocations[i].getOffset() <= offset) &&
          (offset < blkLocations[i].getOffset() + blkLocations[i].getLength())){
          // 代碼邏輯非常簡單,就是返回當前offset是在哪個block里面
        return i;
      }
    }
                    ....................
//-----------getBlockIndex() end----------------------------------------------
                        
            // 計算完成之后加入切片集合
            // 切片信息包括:路徑,偏移量,切片大小,服務器節點【支撐計算向數據移動】
            splits.add(makeSplit(path, length-bytesRemaining, splitSize,
                        blkLocations[blkIndex].getHosts(),
                        blkLocations[blkIndex].getCachedHosts()));
            bytesRemaining -= splitSize;
          }

          // 計算剩余數據塊的切片信息
          if (bytesRemaining != 0) { 
            int blkIndex = getBlockIndex(blkLocations, length-bytesRemaining);
            splits.add(makeSplit(path, length-bytesRemaining, bytesRemaining,
                       blkLocations[blkIndex].getHosts(),
                       blkLocations[blkIndex].getCachedHosts()));
          }
        } else { // not splitable :不能切片,那就是一片
          splits.add(makeSplit(path, 0, length, blkLocations[0].getHosts(),
                      blkLocations[0].getCachedHosts()));
        }
      }
          ......
    // 返回切片文件的集合。根據集合中數據的個數,就可以計算出有多少個maptask
    return splits;
  }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM