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