TaskID.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. package org.apache.hadoop.mapreduce;
  19. import java.io.DataInput;
  20. import java.io.DataOutput;
  21. import java.io.IOException;
  22. import java.text.NumberFormat;
  23. /**
  24. * TaskID represents the immutable and unique identifier for
  25. * a Map or Reduce Task. Each TaskID encompasses multiple attempts made to
  26. * execute the Map or Reduce Task, each of which are uniquely indentified by
  27. * their TaskAttemptID.
  28. *
  29. * TaskID consists of 3 parts. First part is the {@link JobID}, that this
  30. * TaskInProgress belongs to. Second part of the TaskID is either 'm' or 'r'
  31. * representing whether the task is a map task or a reduce task.
  32. * And the third part is the task number. <br>
  33. * An example TaskID is :
  34. * <code>task_200707121733_0003_m_000005</code> , which represents the
  35. * fifth map task in the third job running at the jobtracker
  36. * started at <code>200707121733</code>.
  37. * <p>
  38. * Applications should never construct or parse TaskID strings
  39. * , but rather use appropriate constructors or {@link #forName(String)}
  40. * method.
  41. *
  42. * @see JobID
  43. * @see TaskAttemptID
  44. */
  45. public class TaskID extends ID {
  46. private static final String TASK = "task";
  47. private static char UNDERSCORE = '_';
  48. private static NumberFormat idFormat = NumberFormat.getInstance();
  49. static {
  50. idFormat.setGroupingUsed(false);
  51. idFormat.setMinimumIntegerDigits(6);
  52. }
  53. private JobID jobId;
  54. private boolean isMap;
  55. /**
  56. * Constructs a TaskID object from given {@link JobID}.
  57. * @param jobId JobID that this tip belongs to
  58. * @param isMap whether the tip is a map
  59. * @param id the tip number
  60. */
  61. public TaskID(JobID jobId, boolean isMap, int id) {
  62. super(id);
  63. if(jobId == null) {
  64. throw new IllegalArgumentException("jobId cannot be null");
  65. }
  66. this.jobId = jobId;
  67. this.isMap = isMap;
  68. }
  69. /**
  70. * Constructs a TaskInProgressId object from given parts.
  71. * @param jtIdentifier jobTracker identifier
  72. * @param jobId job number
  73. * @param isMap whether the tip is a map
  74. * @param id the tip number
  75. */
  76. public TaskID(String jtIdentifier, int jobId, boolean isMap, int id) {
  77. this(new JobID(jtIdentifier, jobId), isMap, id);
  78. }
  79. private TaskID() { }
  80. /** Returns the {@link JobID} object that this tip belongs to */
  81. public JobID getJobID() {
  82. return jobId;
  83. }
  84. /**Returns whether this TaskID is a map ID */
  85. public boolean isMap() {
  86. return isMap;
  87. }
  88. @Override
  89. public boolean equals(Object o) {
  90. if (!super.equals(o))
  91. return false;
  92. TaskID that = (TaskID)o;
  93. return this.isMap == that.isMap && this.jobId.equals(that.jobId);
  94. }
  95. /**Compare TaskInProgressIds by first jobIds, then by tip numbers. Reduces are
  96. * defined as greater then maps.*/
  97. @Override
  98. public int compareTo(ID o) {
  99. TaskID that = (TaskID)o;
  100. int jobComp = this.jobId.compareTo(that.jobId);
  101. if(jobComp == 0) {
  102. if(this.isMap == that.isMap) {
  103. return this.id - that.id;
  104. }
  105. else return this.isMap ? -1 : 1;
  106. }
  107. else return jobComp;
  108. }
  109. @Override
  110. public String toString() {
  111. StringBuilder builder = new StringBuilder();
  112. return builder.append(TASK).append(UNDERSCORE)
  113. .append(toStringWOPrefix()).toString();
  114. }
  115. StringBuilder toStringWOPrefix() {
  116. StringBuilder builder = new StringBuilder();
  117. builder.append(jobId.toStringWOPrefix())
  118. .append(isMap ? "_m_" : "_r_");
  119. return builder.append(idFormat.format(id));
  120. }
  121. @Override
  122. public int hashCode() {
  123. return toStringWOPrefix().toString().hashCode();
  124. }
  125. @Override
  126. public void readFields(DataInput in) throws IOException {
  127. super.readFields(in);
  128. this.jobId = JobID.read(in);
  129. this.isMap = in.readBoolean();
  130. }
  131. @Override
  132. public void write(DataOutput out) throws IOException {
  133. super.write(out);
  134. jobId.write(out);
  135. out.writeBoolean(isMap);
  136. }
  137. public static TaskID read(DataInput in) throws IOException {
  138. TaskID tipId = new TaskID();
  139. tipId.readFields(in);
  140. return tipId;
  141. }
  142. /** Construct a TaskID object from given string
  143. * @return constructed TaskID object or null if the given String is null
  144. * @throws IllegalArgumentException if the given string is malformed
  145. */
  146. public static TaskID forName(String str)
  147. throws IllegalArgumentException {
  148. if(str == null)
  149. return null;
  150. try {
  151. String[] parts = str.split("_");
  152. if(parts.length == 5) {
  153. if(parts[0].equals(TASK)) {
  154. boolean isMap = false;
  155. if(parts[3].equals("m")) isMap = true;
  156. else if(parts[3].equals("r")) isMap = false;
  157. else throw new Exception();
  158. return new TaskID(parts[1], Integer.parseInt(parts[2]),
  159. isMap, Integer.parseInt(parts[4]));
  160. }
  161. }
  162. }catch (Exception ex) {//fall below
  163. }
  164. throw new IllegalArgumentException("TaskId string : " + str
  165. + " is not properly formed");
  166. }
  167. /**
  168. * Returns a regex pattern which matches task IDs. Arguments can
  169. * be given null, in which case that part of the regex will be generic.
  170. * For example to obtain a regex matching <i>the first map task</i>
  171. * of <i>any jobtracker</i>, of <i>any job</i>, we would use :
  172. * <pre>
  173. * TaskID.getTaskIDsPattern(null, null, true, 1);
  174. * </pre>
  175. * which will return :
  176. * <pre> "task_[^_]*_[0-9]*_m_000001*" </pre>
  177. * @param jtIdentifier jobTracker identifier, or null
  178. * @param jobId job number, or null
  179. * @param isMap whether the tip is a map, or null
  180. * @param taskId taskId number, or null
  181. * @return a regex pattern matching TaskIDs
  182. */
  183. public static String getTaskIDsPattern(String jtIdentifier, Integer jobId
  184. , Boolean isMap, Integer taskId) {
  185. StringBuilder builder = new StringBuilder(TASK).append(UNDERSCORE)
  186. .append(getTaskIDsPatternWOPrefix(jtIdentifier, jobId, isMap, taskId));
  187. return builder.toString();
  188. }
  189. static StringBuilder getTaskIDsPatternWOPrefix(String jtIdentifier
  190. , Integer jobId, Boolean isMap, Integer taskId) {
  191. StringBuilder builder = new StringBuilder();
  192. builder.append(JobID.getJobIDsPatternWOPrefix(jtIdentifier, jobId))
  193. .append(UNDERSCORE)
  194. .append(isMap != null ? (isMap ? "m" : "r") : "(m|r)").append(UNDERSCORE)
  195. .append(taskId != null ? idFormat.format(taskId) : "[0-9]*");
  196. return builder;
  197. }
  198. }