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    @Deprecated
049    @InterfaceAudience.Public
050    @InterfaceStability.Stable
051    public class TaskAttemptID extends org.apache.hadoop.mapreduce.TaskAttemptID {
052      
053      /**
054       * Constructs a TaskAttemptID object from given {@link TaskID}.  
055       * @param taskId TaskID that this task belongs to  
056       * @param id the task attempt number
057       */
058      public TaskAttemptID(TaskID taskId, int id) {
059        super(taskId, id);
060      }
061      
062      /**
063       * Constructs a TaskId object from given parts.
064       * @param jtIdentifier jobTracker identifier
065       * @param jobId job number 
066       * @param isMap whether the tip is a map 
067       * @param taskId taskId number
068       * @param id the task attempt number
069       * @deprecated Use {@link #TaskAttemptID(String, int, TaskType, int, int)}.
070       */
071      @Deprecated
072      public TaskAttemptID(String jtIdentifier, int jobId, boolean isMap, 
073          int taskId, int id) {
074        this(jtIdentifier, jobId, isMap ? TaskType.MAP : TaskType.REDUCE, taskId,
075            id);
076      }
077      
078      /**
079       * Constructs a TaskId object from given parts.
080       * @param jtIdentifier jobTracker identifier
081       * @param jobId job number 
082       * @param type the TaskType 
083       * @param taskId taskId number
084       * @param id the task attempt number
085       */
086      public TaskAttemptID(String jtIdentifier, int jobId, TaskType type, 
087                           int taskId, int id) {
088        this(new TaskID(jtIdentifier, jobId, type, taskId), id);
089      }
090      
091      public TaskAttemptID() { 
092        super(new TaskID(), 0);
093      }
094    
095      /**
096       * Downgrade a new TaskAttemptID to an old one
097       * @param old the new id
098       * @return either old or a new TaskAttemptID constructed to match old
099       */
100      public static 
101      TaskAttemptID downgrade(org.apache.hadoop.mapreduce.TaskAttemptID old) {
102        if (old instanceof TaskAttemptID) {
103          return (TaskAttemptID) old;
104        } else {
105          return new TaskAttemptID(TaskID.downgrade(old.getTaskID()), old.getId());
106        }
107      }
108    
109      public TaskID getTaskID() {
110        return (TaskID) super.getTaskID();
111      }
112    
113      public JobID getJobID() {
114        return (JobID) super.getJobID();
115      }
116    
117      @Deprecated
118      public static TaskAttemptID read(DataInput in) throws IOException {
119        TaskAttemptID taskId = new TaskAttemptID();
120        taskId.readFields(in);
121        return taskId;
122      }
123      
124      /** Construct a TaskAttemptID object from given string 
125       * @return constructed TaskAttemptID object or null if the given String is null
126       * @throws IllegalArgumentException if the given string is malformed
127       */
128      public static TaskAttemptID forName(String str
129                                          ) throws IllegalArgumentException {
130        return (TaskAttemptID) 
131                 org.apache.hadoop.mapreduce.TaskAttemptID.forName(str);
132      }
133      
134      /** 
135       * Returns a regex pattern which matches task attempt IDs. Arguments can 
136       * be given null, in which case that part of the regex will be generic.  
137       * For example to obtain a regex matching <i>all task attempt IDs</i> 
138       * of <i>any jobtracker</i>, in <i>any job</i>, of the <i>first 
139       * map task</i>, we would use :
140       * <pre> 
141       * TaskAttemptID.getTaskAttemptIDsPattern(null, null, true, 1, null);
142       * </pre>
143       * which will return :
144       * <pre> "attempt_[^_]*_[0-9]*_m_000001_[0-9]*" </pre> 
145       * @param jtIdentifier jobTracker identifier, or null
146       * @param jobId job number, or null
147       * @param isMap whether the tip is a map, or null 
148       * @param taskId taskId number, or null
149       * @param attemptId the task attempt number, or null
150       * @return a regex pattern matching TaskAttemptIDs
151       */
152      @Deprecated
153      public static String getTaskAttemptIDsPattern(String jtIdentifier,
154          Integer jobId, Boolean isMap, Integer taskId, Integer attemptId) {
155        return getTaskAttemptIDsPattern(jtIdentifier, jobId,
156            isMap ? TaskType.MAP : TaskType.REDUCE, taskId, attemptId);
157      }
158      
159      /** 
160       * Returns a regex pattern which matches task attempt IDs. Arguments can 
161       * be given null, in which case that part of the regex will be generic.  
162       * For example to obtain a regex matching <i>all task attempt IDs</i> 
163       * of <i>any jobtracker</i>, in <i>any job</i>, of the <i>first 
164       * map task</i>, we would use :
165       * <pre> 
166       * TaskAttemptID.getTaskAttemptIDsPattern(null, null, TaskType.MAP, 1, null);
167       * </pre>
168       * which will return :
169       * <pre> "attempt_[^_]*_[0-9]*_m_000001_[0-9]*" </pre> 
170       * @param jtIdentifier jobTracker identifier, or null
171       * @param jobId job number, or null
172       * @param type the {@link TaskType} 
173       * @param taskId taskId number, or null
174       * @param attemptId the task attempt number, or null
175       * @return a regex pattern matching TaskAttemptIDs
176       */
177      @Deprecated
178      public static String getTaskAttemptIDsPattern(String jtIdentifier,
179          Integer jobId, TaskType type, Integer taskId, Integer attemptId) {
180        StringBuilder builder = new StringBuilder(ATTEMPT).append(SEPARATOR);
181        builder.append(getTaskAttemptIDsPatternWOPrefix(jtIdentifier, jobId,
182            type, taskId, attemptId));
183        return builder.toString();
184      }
185      
186      @Deprecated
187      static StringBuilder getTaskAttemptIDsPatternWOPrefix(String jtIdentifier
188          , Integer jobId, TaskType type, Integer taskId, Integer attemptId) {
189        StringBuilder builder = new StringBuilder();
190        builder.append(TaskID.getTaskIDsPatternWOPrefix(jtIdentifier
191            , jobId, type, taskId))
192            .append(SEPARATOR)
193            .append(attemptId != null ? attemptId : "[0-9]*");
194        return builder;
195      }
196    }