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