001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.mapred.jobcontrol;
020
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.apache.hadoop.classification.InterfaceAudience;
029import org.apache.hadoop.classification.InterfaceStability;
030import org.apache.hadoop.mapred.JobClient;
031import org.apache.hadoop.mapred.JobConf;
032import org.apache.hadoop.mapred.JobID;
033import org.apache.hadoop.mapreduce.lib.jobcontrol.ControlledJob;
034
035@InterfaceAudience.Public
036@InterfaceStability.Stable
037public class Job extends ControlledJob {
038  static final Log LOG = LogFactory.getLog(Job.class);
039
040  final public static int SUCCESS = 0;
041  final public static int WAITING = 1;
042  final public static int RUNNING = 2;
043  final public static int READY = 3;
044  final public static int FAILED = 4;
045  final public static int DEPENDENT_FAILED = 5;
046
047  /** 
048   * Construct a job.
049   * @param jobConf a mapred job configuration representing a job to be executed.
050   * @param dependingJobs an array of jobs the current job depends on
051   */
052  @SuppressWarnings("unchecked")
053  public Job(JobConf jobConf, ArrayList<?> dependingJobs) throws IOException {
054    super(new org.apache.hadoop.mapreduce.Job(jobConf), 
055          (List<ControlledJob>) dependingJobs);
056  }
057
058  public Job(JobConf conf) throws IOException {
059    super(conf);
060  }
061
062  /**
063   * @return the mapred ID of this job as assigned by the 
064   * mapred framework.
065   */
066  public JobID getAssignedJobID() {
067    org.apache.hadoop.mapreduce.JobID temp = super.getMapredJobID();
068    if(temp == null) {
069      return null;
070    }
071    return JobID.downgrade(temp);
072  }
073
074  /**
075   * @deprecated setAssignedJobID should not be called.
076   * JOBID is set by the framework.
077   */
078  @Deprecated
079  public void setAssignedJobID(JobID mapredJobID) {
080    // do nothing
081  }
082
083  /**
084   * @return the mapred job conf of this job
085   */
086  public synchronized JobConf getJobConf() {
087    return new JobConf(super.getJob().getConfiguration());
088  }
089
090
091  /**
092   * Set the mapred job conf for this job.
093   * @param jobConf the mapred job conf for this job.
094   */
095  public synchronized void setJobConf(JobConf jobConf) {
096    try {
097      super.setJob(new org.apache.hadoop.mapreduce.Job(jobConf));
098    } catch (IOException ioe) { 
099      LOG.info("Exception" + ioe);
100    }
101  }
102
103  /**
104   * @return the state of this job
105   */
106  public synchronized int getState() {
107    State state = super.getJobState();
108    if (state == State.SUCCESS) {
109      return SUCCESS;
110    } 
111    if (state == State.WAITING) {
112      return WAITING;
113    }
114    if (state == State.RUNNING) {
115      return RUNNING;
116    }
117    if (state == State.READY) {
118      return READY;
119    }
120    if (state == State.FAILED ) {
121      return FAILED;
122    }
123    if (state == State.DEPENDENT_FAILED ) {
124      return DEPENDENT_FAILED;
125    }
126    return -1;
127  }
128  
129  /**
130   * @return the job client of this job
131   */
132  public JobClient getJobClient() {
133    try {
134      return new JobClient(super.getJob().getConfiguration());
135    } catch (IOException ioe) {
136      return null;
137    }
138  }
139
140  /**
141   * @return the depending jobs of this job
142   */
143  public ArrayList<Job> getDependingJobs() {
144    return JobControl.castToJobList(super.getDependentJobs());
145  }
146
147}