1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.apache.hadoop.mapreduce;
20 
21 import java.io.DataInput;
22 import java.io.DataOutput;
23 import java.io.IOException;
24 import java.text.NumberFormat;
25 
26 /**
27  * TaskID represents the immutable and unique identifier for
28  * a Map or Reduce Task. Each TaskID encompasses multiple attempts made to
29  * execute the Map or Reduce Task, each of which are uniquely indentified by
30  * their TaskAttemptID.
31  *
32  * TaskID consists of 3 parts. First part is the {@link JobID}, that this
33  * TaskInProgress belongs to. Second part of the TaskID is either 'm' or 'r'
34  * representing whether the task is a map task or a reduce task.
35  * And the third part is the task number. <br>
36  * An example TaskID is :
37  * <code>task_200707121733_0003_m_000005</code> , which represents the
38  * fifth map task in the third job running at the jobtracker
39  * started at <code>200707121733</code>.
40  * <p>
41  * Applications should never construct or parse TaskID strings
42  * , but rather use appropriate constructors or {@link #forName(String)}
43  * method.
44  *
45  * @see JobID
46  * @see TaskAttemptID
47  */
48 public class TaskID extends org.apache.hadoop.mapred.ID {
49   protected static final String TASK = "task";
50   protected static final NumberFormat idFormat = NumberFormat.getInstance();
51   static {
52     idFormat.setGroupingUsed(false);
53     idFormat.setMinimumIntegerDigits(6);
54   }
55 
56   private JobID jobId;
57   private boolean isMap;
58 
59   /**
60    * Constructs a TaskID object from given {@link JobID}.
61    * @param jobId JobID that this tip belongs to
62    * @param isMap whether the tip is a map
63    * @param id the tip number
64    */
TaskID(JobID jobId, boolean isMap, int id)65   public TaskID(JobID jobId, boolean isMap, int id) {
66     super(id);
67     if(jobId == null) {
68       throw new IllegalArgumentException("jobId cannot be null");
69     }
70     this.jobId = jobId;
71     this.isMap = isMap;
72   }
73 
74   /**
75    * Constructs a TaskInProgressId object from given parts.
76    * @param jtIdentifier jobTracker identifier
77    * @param jobId job number
78    * @param isMap whether the tip is a map
79    * @param id the tip number
80    */
TaskID(String jtIdentifier, int jobId, boolean isMap, int id)81   public TaskID(String jtIdentifier, int jobId, boolean isMap, int id) {
82     this(new JobID(jtIdentifier, jobId), isMap, id);
83   }
84 
TaskID()85   public TaskID() {
86     jobId = new JobID();
87   }
88 
89   /** Returns the {@link JobID} object that this tip belongs to */
getJobID()90   public JobID getJobID() {
91     return jobId;
92   }
93 
94   /**Returns whether this TaskID is a map ID */
isMap()95   public boolean isMap() {
96     return isMap;
97   }
98 
99   @Override
equals(Object o)100   public boolean equals(Object o) {
101     if (!super.equals(o))
102       return false;
103 
104     TaskID that = (TaskID)o;
105     return this.isMap == that.isMap && this.jobId.equals(that.jobId);
106   }
107 
108   /**Compare TaskInProgressIds by first jobIds, then by tip numbers. Reduces are
109    * defined as greater then maps.*/
110   @Override
compareTo(ID o)111   public int compareTo(ID o) {
112     TaskID that = (TaskID)o;
113     int jobComp = this.jobId.compareTo(that.jobId);
114     if(jobComp == 0) {
115       if(this.isMap == that.isMap) {
116         return this.id - that.id;
117       }
118       else return this.isMap ? -1 : 1;
119     }
120     else return jobComp;
121   }
122   @Override
toString()123   public String toString() {
124     return appendTo(new StringBuilder(TASK)).toString();
125   }
126 
127   /**
128    * Add the unique string to the given builder.
129    * @param builder the builder to append to
130    * @return the builder that was passed in
131    */
appendTo(StringBuilder builder)132   protected StringBuilder appendTo(StringBuilder builder) {
133     return jobId.appendTo(builder).
134                  append(SEPARATOR).
135                  append(isMap ? 'm' : 'r').
136                  append(SEPARATOR).
137                  append(idFormat.format(id));
138   }
139 
140   @Override
hashCode()141   public int hashCode() {
142     return jobId.hashCode() * 524287 + id;
143   }
144 
145   @Override
readFields(DataInput in)146   public void readFields(DataInput in) throws IOException {
147     super.readFields(in);
148     jobId.readFields(in);
149     isMap = in.readBoolean();
150   }
151 
152   @Override
write(DataOutput out)153   public void write(DataOutput out) throws IOException {
154     super.write(out);
155     jobId.write(out);
156     out.writeBoolean(isMap);
157   }
158 
159   /** Construct a TaskID object from given string
160    * @return constructed TaskID object or null if the given String is null
161    * @throws IllegalArgumentException if the given string is malformed
162    */
forName(String str)163   public static TaskID forName(String str)
164     throws IllegalArgumentException {
165     if(str == null)
166       return null;
167     try {
168       String[] parts = str.split("_");
169       if(parts.length == 5) {
170         if(parts[0].equals(TASK)) {
171           boolean isMap = false;
172           if(parts[3].equals("m")) isMap = true;
173           else if(parts[3].equals("r")) isMap = false;
174           else throw new Exception();
175           return new org.apache.hadoop.mapred.TaskID(parts[1],
176                                                      Integer.parseInt(parts[2]),
177                                                      isMap,
178                                                      Integer.parseInt(parts[4]));
179         }
180       }
181     }catch (Exception ex) {//fall below
182     }
183     throw new IllegalArgumentException("TaskId string : " + str
184         + " is not properly formed");
185   }
186 
187 }
188