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 
25 import org.apache.hadoop.classification.InterfaceAudience;
26 import org.apache.hadoop.classification.InterfaceStability;
27 
28 /**
29  * TaskAttemptID represents the immutable and unique identifier for
30  * a task attempt. Each task attempt is one particular instance of a Map or
31  * Reduce Task identified by its TaskID.
32  *
33  * TaskAttemptID consists of 2 parts. First part is the
34  * {@link TaskID}, that this TaskAttemptID belongs to.
35  * Second part is the task attempt number. <br>
36  * An example TaskAttemptID is :
37  * <code>attempt_200707121733_0003_m_000005_0</code> , which represents the
38  * zeroth task attempt for the fifth map task in the third job
39  * running at the jobtracker started at <code>200707121733</code>.
40  * <p>
41  * Applications should never construct or parse TaskAttemptID strings
42  * , but rather use appropriate constructors or {@link #forName(String)}
43  * method.
44  *
45  * @see JobID
46  * @see TaskID
47  */
48 @InterfaceAudience.Public
49 @InterfaceStability.Stable
50 public class TaskAttemptID extends org.apache.hadoop.mapred.ID {
51   protected static final String ATTEMPT = "attempt";
52   private TaskID taskId;
53 
54   /**
55    * Constructs a TaskAttemptID object from given {@link TaskID}.
56    * @param taskId TaskID that this task belongs to
57    * @param id the task attempt number
58    */
TaskAttemptID(TaskID taskId, int id)59   public TaskAttemptID(TaskID taskId, int id) {
60     super(id);
61     if(taskId == null) {
62       throw new IllegalArgumentException("taskId cannot be null");
63     }
64     this.taskId = taskId;
65   }
66 
67   /**
68    * Constructs a TaskId object from given parts.
69    * @param jtIdentifier jobTracker identifier
70    * @param jobId job number
71    * @param type the TaskType
72    * @param taskId taskId number
73    * @param id the task attempt number
74    */
TaskAttemptID(String jtIdentifier, int jobId, TaskType type, int taskId, int id)75   public TaskAttemptID(String jtIdentifier, int jobId, TaskType type,
76                        int taskId, int id) {
77     this(new TaskID(jtIdentifier, jobId, type, taskId), id);
78   }
79 
80   /**
81    * Constructs a TaskId object from given parts.
82    * @param jtIdentifier jobTracker identifier
83    * @param jobId job number
84    * @param isMap whether the tip is a map
85    * @param taskId taskId number
86    * @param id the task attempt number
87    */
88   @Deprecated
TaskAttemptID(String jtIdentifier, int jobId, boolean isMap, int taskId, int id)89   public TaskAttemptID(String jtIdentifier, int jobId, boolean isMap,
90                        int taskId, int id) {
91     this(new TaskID(jtIdentifier, jobId, isMap, taskId), id);
92   }
93 
TaskAttemptID()94   public TaskAttemptID() {
95     taskId = new TaskID();
96   }
97 
98   /** Returns the {@link JobID} object that this task attempt belongs to */
getJobID()99   public JobID getJobID() {
100     return taskId.getJobID();
101   }
102 
103   /** Returns the {@link TaskID} object that this task attempt belongs to */
getTaskID()104   public TaskID getTaskID() {
105     return taskId;
106   }
107 
108   /**Returns whether this TaskID is a map ID */
109   @Deprecated
isMap()110   public boolean isMap() {
111     return taskId.isMap();
112   }
113 
114   /**Returns the TaskType of the TaskAttemptID */
getTaskType()115   public TaskType getTaskType() {
116     return taskId.getTaskType();
117   }
118   @Override
equals(Object o)119   public boolean equals(Object o) {
120     if (!super.equals(o))
121       return false;
122 
123     TaskAttemptID that = (TaskAttemptID)o;
124     return this.taskId.equals(that.taskId);
125   }
126 
127   /**
128    * Add the unique string to the StringBuilder
129    * @param builder the builder to append ot
130    * @return the builder that was passed in.
131    */
appendTo(StringBuilder builder)132   protected StringBuilder appendTo(StringBuilder builder) {
133     return taskId.appendTo(builder).append(SEPARATOR).append(id);
134   }
135 
136   @Override
readFields(DataInput in)137   public void readFields(DataInput in) throws IOException {
138     super.readFields(in);
139     taskId.readFields(in);
140   }
141 
142   @Override
write(DataOutput out)143   public void write(DataOutput out) throws IOException {
144     super.write(out);
145     taskId.write(out);
146   }
147 
148   @Override
hashCode()149   public int hashCode() {
150     return taskId.hashCode() * 5 + id;
151   }
152 
153   /**Compare TaskIds by first tipIds, then by task numbers. */
154   @Override
compareTo(ID o)155   public int compareTo(ID o) {
156     TaskAttemptID that = (TaskAttemptID)o;
157     int tipComp = this.taskId.compareTo(that.taskId);
158     if(tipComp == 0) {
159       return this.id - that.id;
160     }
161     else return tipComp;
162   }
163   @Override
toString()164   public String toString() {
165     return appendTo(new StringBuilder(ATTEMPT)).toString();
166   }
167 
168   /** Construct a TaskAttemptID object from given string
169    * @return constructed TaskAttemptID object or null if the given String is null
170    * @throws IllegalArgumentException if the given string is malformed
171    */
forName(String str )172   public static TaskAttemptID forName(String str
173                                       ) throws IllegalArgumentException {
174     if(str == null)
175       return null;
176     String exceptionMsg = null;
177     try {
178       String[] parts = str.split(Character.toString(SEPARATOR));
179       if(parts.length == 6) {
180         if(parts[0].equals(ATTEMPT)) {
181           String type = parts[3];
182           TaskType t = TaskID.getTaskType(type.charAt(0));
183           if(t != null) {
184             return new org.apache.hadoop.mapred.TaskAttemptID
185             (parts[1],
186              Integer.parseInt(parts[2]),
187              t, Integer.parseInt(parts[4]),
188              Integer.parseInt(parts[5]));
189           } else
190             exceptionMsg = "Bad TaskType identifier. TaskAttemptId string : "
191                 + str + " is not properly formed.";
192         }
193       }
194     } catch (Exception ex) {
195       //fall below
196     }
197     if (exceptionMsg == null) {
198       exceptionMsg = "TaskAttemptId string : " + str
199           + " is not properly formed";
200     }
201     throw new IllegalArgumentException(exceptionMsg);
202   }
203 
204 }
205