job的提交过程源代码分析

job.waitForCompletion(true)开始: 

waitForCompletion方法如下:
  1. public boolean waitForCompletion (boolean verbose
  2.                                    ) throws IOException, InterruptedException,
  3.                                              ClassNotFoundException {
  4.     if (state == JobState.DEFINE) {
  5.       // 建立了连接 提交作业
  6.       submit();
  7.     }
  8.     if (verbose) {
  9.       jobClient.monitorAndPrintJob( conf, info);
  10.     } else {    
  11.       info.waitForCompletion();
  12.     }
  13.     return isSuccessful();
  14.   }
submit函数:
  1.  public void submit () throws IOException, InterruptedException,
  2.                               ClassNotFoundException {
  3.     ensureState(JobState.DEFINE);
  4.     setUseNewAPI();
  5.     // Connect to the JobTracker and submit the job
  6.     connect();
  7.     info = jobClient.submitJobInternal( conf);
  8.     super.setJobID( info.getID());
  9.     state = JobState.RUNNING;
  10.    }


connect函数:

  1. private void connect() throws IOException, InterruptedException {
  2.     ugi.doAs(new PrivilegedExceptionAction<Object>() {
  3.       public Object run() throws IOException {
  4.         // new JobClient连接到了服务端 并且拿到了服务端的代理对象
  5.         jobClient = new JobClient((JobConf) getConfiguration());   
  6.         return null;
  7.       }
  8.     });
  9.   }


jobClient函数:

  1. public JobClient(JobConf conf) throws IOException {
  2.     setConf(conf);//初始化配置文件
  3.     init(conf);//初始化作业
  4.   }


setConf函数:
  1.   public void setConf (Configuration conf) {
  2.      this. conf = conf;
  3.   }
init函数:
  1. public void init(JobConf conf) throws IOException {
  2.     //读取mapred.job.tracker中指定的jobTracker,如果无法读取,那么使用本地的local的jobTracker
  3.     String tracker = conf.get( "mapred.job.tracker""local");
  4.      tasklogtimeout = conf.getInt(
  5.        TASKLOG_PULL_TIMEOUT_KEYDEFAULT_TASKLOG_TIMEOUT );
  6.      this. ugi = UserGroupInformation. getCurrentUser();
  7.      if ( "local".equals(tracker)) {
  8.       conf.setNumMapTasks(1);
  9.        this. jobSubmitClient = new LocalJobRunner(conf);
  10.     } else {
  11.        this. rpcJobSubmitClient =
  12.            createRPCProxy(JobTracker. getAddress(conf), conf);
  13.        this. jobSubmitClient = createProxy( this. rpcJobSubmitClient, conf);
  14.     }       
  15.   }
createRPCSubmitClient函数:获取服务端对象和方法

  1. private static JobSubmissionProtocol createRPCProxy(InetSocketAddress addr,
  2.       Configuration conf) throws IOException {
  3.    //JobSubmissionProtocol :作业提交协议:提交作业的一个接口
  4.     JobSubmissionProtocol rpcJobSubmitClient =
  5.         (JobSubmissionProtocol)RPC. getProxy(
  6.             JobSubmissionProtocol. class,
  7.             JobSubmissionProtocol. versionID, addr,
  8.             UserGroupInformation. getCurrentUser(), conf,
  9.             NetUtils. getSocketFactory(conf, JobSubmissionProtocol.class ),
  10.             0,
  11.             RetryUtils. getMultipleLinearRandomRetry(
  12.                 conf,
  13.                  MAPREDUCE_CLIENT_RETRY_POLICY_ENABLED_KEY ,
  14.                  MAPREDUCE_CLIENT_RETRY_POLICY_ENABLED_DEFAULT ,
  15.                  MAPREDUCE_CLIENT_RETRY_POLICY_SPEC_KEY ,
  16.                  MAPREDUCE_CLIENT_RETRY_POLICY_SPEC_DEFAULT
  17.                 )
  18.             );
  19.    
  20.      return rpcJobSubmitClient;
  21.   }

submitJobInternal函数:submitJob方法提交作业

  1. public
  2.   RunningJob submitJobInternal( final JobConf job
  3.                                ) throws FileNotFoundException,
  4.                                         ClassNotFoundException,
  5.                                         InterruptedException,
  6.                                         IOException {
  7.      /*
  8.      * configure the command line options correctly on the submitting dfs
  9.      */
  10.      return ugi.doAs( new PrivilegedExceptionAction<RunningJob>() {
  11.        public RunningJob run() throws FileNotFoundException,
  12.       ClassNotFoundException,
  13.       InterruptedException,
  14.       IOException{
  15.         JobConf jobCopy = job;
  16.         Path jobStagingArea = JobSubmissionFiles.getStagingDir(JobClient. this,
  17.             jobCopy);
  18.         JobID jobId = jobSubmitClient.getNewJobId();
  19.         Path submitJobDir = new Path(jobStagingArea, jobId.toString());
  20.         jobCopy.set( "mapreduce.job.dir" , submitJobDir.toString());
  21.         JobStatus status = null;
  22.          try {
  23.           populateTokenCache(jobCopy, jobCopy.getCredentials());

  24.           copyAndConfigureFiles(jobCopy, submitJobDir);

  25.            // get delegation token for the dir
  26.            TokenCache.obtainTokensForNamenodes(jobCopy.getCredentials(),
  27.                                                new Path [] {submitJobDir},
  28.                                                jobCopy);

  29.           Path submitJobFile = JobSubmissionFiles.getJobConfPath(submitJobDir);
  30.            int reduces = jobCopy.getNumReduceTasks();
  31.           InetAddress ip = InetAddress. getLocalHost();
  32.            if (ip != null) {
  33.             job.setJobSubmitHostAddress(ip.getHostAddress());
  34.             job.setJobSubmitHostName(ip.getHostName());
  35.           }
  36.           JobContext context = new JobContext(jobCopy, jobId);

  37.            // Check the output specification
  38.            if (reduces == 0 ? jobCopy.getUseNewMapper() :
  39.             jobCopy.getUseNewReducer()) {
  40.             org.apache.hadoop.mapreduce.OutputFormat<?,?> output =
  41.                ReflectionUtils.newInstance(context.getOutputFormatClass(),
  42.                   jobCopy);
  43.             output.checkOutputSpecs(context);
  44.           } else {
  45.             jobCopy.getOutputFormat().checkOutputSpecs( fs, jobCopy);
  46.           }
  47.          
  48.           jobCopy = (JobConf)context.getConfiguration();

  49.            // Create the splits for the job
  50.           FileSystem fs = submitJobDir.getFileSystem(jobCopy);
  51.            LOG.debug( "Creating splits at " + fs.makeQualified(submitJobDir));
  52.            int maps = writeSplits(context, submitJobDir);
  53.           jobCopy.setNumMapTasks(maps);

  54.            // write "queue admins of the queue to which job is being submitted"
  55.            // to job file.
  56.           String queue = jobCopy.getQueueName();
  57.           AccessControlList acl = jobSubmitClient .getQueueAdmins(queue);
  58.           jobCopy.set(QueueManager. toFullPropertyName(queue,
  59.               QueueACL. ADMINISTER_JOBS.getAclName()), acl.getACLString());

  60.            // Write job file to JobTracker's fs       
  61.           FSDataOutputStream out =
  62.             FileSystem. create(fs, submitJobFile,
  63.                  new FsPermission(JobSubmissionFiles.JOB_FILE_PERMISSION ));

  64.            try {
  65.             jobCopy.writeXml(out);
  66.           } finally {
  67.             out.close();
  68.           }
  69.            //
  70.            // Now, actually submit the job (using the submit name)
  71.            //
  72.           printTokens(jobId, jobCopy.getCredentials());
  73.           status = jobSubmitClient.submitJob(
  74.               jobId, submitJobDir.toString(), jobCopy.getCredentials());
  75.           JobProfile prof = jobSubmitClient .getJobProfile(jobId);
  76.            if (status != null && prof != null) {
  77.              return new NetworkedJob(status, prof, jobSubmitClient );
  78.           } else {
  79.              throw new IOException( "Could not launch job" );
  80.           }
  81.         } finally {
  82.            if (status == null) {
  83.              LOG.info( "Cleaning up the staging area " + submitJobDir);
  84.              if ( fs != null && submitJobDir != null)
  85.                fs.delete(submitJobDir, true);
  86.           }
  87.         }
  88.       }
  89.     });
  90.   }
submitJob函数,属于 JobSubmissionProtocol接口,jobTracker实现了这个接口,jobTracker实现了这个方法 submitJob
public  JobStatus submitJob(JobID jobName, String jobSubmitDir, Credentials ts);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值