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;
020
021import java.io.DataInput;
022import java.io.IOException;
023
024import org.apache.hadoop.classification.InterfaceAudience;
025import org.apache.hadoop.classification.InterfaceStability;
026import org.apache.hadoop.mapreduce.TaskType;
027
028/**
029 * TaskID represents the immutable and unique identifier for 
030 * a Map or Reduce Task. Each TaskID encompasses multiple attempts made to
031 * execute the Map or Reduce Task, each of which are uniquely indentified by
032 * their TaskAttemptID.
033 * 
034 * TaskID consists of 3 parts. First part is the {@link JobID}, that this 
035 * TaskInProgress belongs to. Second part of the TaskID is either 'm' or 'r' 
036 * representing whether the task is a map task or a reduce task. 
037 * And the third part is the task number. <br> 
038 * An example TaskID is : 
039 * <code>task_200707121733_0003_m_000005</code> , which represents the
040 * fifth map task in the third job running at the jobtracker 
041 * started at <code>200707121733</code>. 
042 * <p>
043 * Applications should never construct or parse TaskID strings
044 * , but rather use appropriate constructors or {@link #forName(String)} 
045 * method. 
046 * 
047 * @see JobID
048 * @see TaskAttemptID
049 */
050@InterfaceAudience.Public
051@InterfaceStability.Stable
052public class TaskID extends org.apache.hadoop.mapreduce.TaskID {
053
054  /**
055   * Constructs a TaskID object from given {@link JobID}.  
056   * @param jobId JobID that this tip belongs to 
057   * @param isMap whether the tip is a map 
058   * @param id the tip number
059   * @deprecated Use {@link #TaskID(String, int, TaskType, int)}
060   */
061  @Deprecated
062  public TaskID(org.apache.hadoop.mapreduce.JobID jobId, boolean isMap,int id) {
063    this(jobId, isMap ? TaskType.MAP : TaskType.REDUCE, id);
064  }
065   
066  /**
067   * Constructs a TaskInProgressId object from given parts.
068   * @param jtIdentifier jobTracker identifier
069   * @param jobId job number 
070   * @param isMap whether the tip is a map 
071   * @param id the tip number
072   * @deprecated Use {@link #TaskID(org.apache.hadoop.mapreduce.JobID, TaskType,
073   * int)}
074   */
075  @Deprecated
076  public TaskID(String jtIdentifier, int jobId, boolean isMap, int id) {
077    this(jtIdentifier, jobId, isMap ? TaskType.MAP : TaskType.REDUCE, id);
078  }
079    
080  /**
081   * Constructs a TaskID object from given {@link JobID}.  
082   * @param jobId JobID that this tip belongs to 
083   * @param type the {@link TaskType} 
084   * @param id the tip number
085   */
086  public TaskID(org.apache.hadoop.mapreduce.JobID jobId, TaskType type,int id) {
087    super(jobId, type, id);
088  }
089  
090  /**
091   * Constructs a TaskInProgressId object from given parts.
092   * @param jtIdentifier jobTracker identifier
093   * @param jobId job number 
094   * @param type the {@link TaskType} 
095   * @param id the tip number
096   */
097  public TaskID(String jtIdentifier, int jobId, TaskType type, int id) {
098    this(new JobID(jtIdentifier, jobId), type, id);
099  }
100  
101  public TaskID() {
102    super(new JobID(), TaskType.REDUCE, 0);
103  }
104  
105  /**
106   * Downgrade a new TaskID to an old one
107   * @param old a new or old TaskID
108   * @return either old or a new TaskID build to match old
109   */
110  public static TaskID downgrade(org.apache.hadoop.mapreduce.TaskID old) {
111    if (old instanceof TaskID) {
112      return (TaskID) old;
113    } else {
114      return new TaskID(JobID.downgrade(old.getJobID()), old.getTaskType(), 
115                        old.getId());
116    }
117  }
118
119  @Deprecated
120  public static TaskID read(DataInput in) throws IOException {
121    TaskID tipId = new TaskID();
122    tipId.readFields(in);
123    return tipId;
124  }
125  
126  public JobID getJobID() {
127    return (JobID) super.getJobID();
128  }
129
130  /** 
131   * Returns a regex pattern which matches task IDs. Arguments can 
132   * be given null, in which case that part of the regex will be generic.  
133   * For example to obtain a regex matching <i>the first map task</i> 
134   * of <i>any jobtracker</i>, of <i>any job</i>, we would use :
135   * <pre> 
136   * TaskID.getTaskIDsPattern(null, null, true, 1);
137   * </pre>
138   * which will return :
139   * <pre> "task_[^_]*_[0-9]*_m_000001*" </pre> 
140   * @param jtIdentifier jobTracker identifier, or null
141   * @param jobId job number, or null
142   * @param isMap whether the tip is a map, or null 
143   * @param taskId taskId number, or null
144   * @return a regex pattern matching TaskIDs
145   * @deprecated Use {@link TaskID#getTaskIDsPattern(String, Integer, TaskType,
146   * Integer)}
147   */
148  @Deprecated
149  public static String getTaskIDsPattern(String jtIdentifier, Integer jobId
150      , Boolean isMap, Integer taskId) {
151    return getTaskIDsPattern(jtIdentifier, jobId,
152        isMap ? TaskType.MAP : TaskType.REDUCE, taskId);
153  }
154  
155  /** 
156   * Returns a regex pattern which matches task IDs. Arguments can 
157   * be given null, in which case that part of the regex will be generic.  
158   * For example to obtain a regex matching <i>the first map task</i> 
159   * of <i>any jobtracker</i>, of <i>any job</i>, we would use :
160   * <pre> 
161   * TaskID.getTaskIDsPattern(null, null, true, 1);
162   * </pre>
163   * which will return :
164   * <pre> "task_[^_]*_[0-9]*_m_000001*" </pre> 
165   * @param jtIdentifier jobTracker identifier, or null
166   * @param jobId job number, or null
167   * @param type the {@link TaskType}, or null 
168   * @param taskId taskId number, or null
169   * @return a regex pattern matching TaskIDs
170   */
171  @Deprecated
172  public static String getTaskIDsPattern(String jtIdentifier, Integer jobId
173      , TaskType type, Integer taskId) {
174    StringBuilder builder = new StringBuilder(TASK).append(SEPARATOR)
175      .append(getTaskIDsPatternWOPrefix(jtIdentifier, jobId, type, taskId));
176    return builder.toString();
177  }
178  
179  @Deprecated
180  static StringBuilder getTaskIDsPatternWOPrefix(String jtIdentifier
181      , Integer jobId, TaskType type, Integer taskId) {
182    StringBuilder builder = new StringBuilder();
183    builder.append(JobID.getJobIDsPatternWOPrefix(jtIdentifier, jobId))
184      .append(SEPARATOR)
185      .append(type != null ? 
186          (org.apache.hadoop.mapreduce.TaskID.getRepresentingCharacter(type)) : 
187            org.apache.hadoop.mapreduce.TaskID.getAllTaskTypes()).
188            append(SEPARATOR)
189      .append(taskId != null ? idFormat.format(taskId) : "[0-9]*");
190    return builder;
191  }
192
193  public static TaskID forName(String str
194                               ) throws IllegalArgumentException {
195    return (TaskID) org.apache.hadoop.mapreduce.TaskID.forName(str);
196  }
197
198}