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.v2.api.records; 20 21 import java.text.NumberFormat; 22 23 /** 24 * <p> 25 * <code>TaskId</code> represents the unique identifier for a Map or Reduce 26 * Task. 27 * </p> 28 * 29 * <p> 30 * TaskId consists of 3 parts. First part is <code>JobId</code>, that this Task 31 * belongs to. Second part of the TaskId is either 'm' or 'r' representing 32 * whether the task is a map task or a reduce task. And the third part is the 33 * task number. 34 * </p> 35 */ 36 public abstract class TaskId implements Comparable<TaskId> { 37 38 /** 39 * @return the associated <code>JobId</code> 40 */ getJobId()41 public abstract JobId getJobId(); 42 43 /** 44 * @return the type of the task - MAP/REDUCE 45 */ getTaskType()46 public abstract TaskType getTaskType(); 47 48 /** 49 * @return the task number. 50 */ getId()51 public abstract int getId(); 52 setJobId(JobId jobId)53 public abstract void setJobId(JobId jobId); 54 setTaskType(TaskType taskType)55 public abstract void setTaskType(TaskType taskType); 56 setId(int id)57 public abstract void setId(int id); 58 59 protected static final String TASK = "task"; 60 61 static final ThreadLocal<NumberFormat> taskIdFormat = 62 new ThreadLocal<NumberFormat>() { 63 @Override 64 public NumberFormat initialValue() { 65 NumberFormat fmt = NumberFormat.getInstance(); 66 fmt.setGroupingUsed(false); 67 fmt.setMinimumIntegerDigits(6); 68 return fmt; 69 } 70 }; 71 72 @Override hashCode()73 public int hashCode() { 74 final int prime = 31; 75 int result = 1; 76 result = prime * result + getId(); 77 result = prime * result + getJobId().hashCode(); 78 result = prime * result + getTaskType().hashCode(); 79 return result; 80 } 81 82 @Override equals(Object obj)83 public boolean equals(Object obj) { 84 if (this == obj) 85 return true; 86 if (obj == null) 87 return false; 88 if (getClass() != obj.getClass()) 89 return false; 90 TaskId other = (TaskId) obj; 91 if (getId() != other.getId()) 92 return false; 93 if (!getJobId().equals(other.getJobId())) 94 return false; 95 if (getTaskType() != other.getTaskType()) 96 return false; 97 return true; 98 } 99 100 @Override toString()101 public String toString() { 102 StringBuilder builder = new StringBuilder(TASK); 103 JobId jobId = getJobId(); 104 builder.append("_").append(jobId.getAppId().getClusterTimestamp()); 105 builder.append("_").append( 106 JobId.jobIdFormat.get().format(jobId.getAppId().getId())); 107 builder.append("_"); 108 builder.append(getTaskType() == TaskType.MAP ? "m" : "r").append("_"); 109 builder.append(taskIdFormat.get().format(getId())); 110 return builder.toString(); 111 } 112 113 @Override compareTo(TaskId other)114 public int compareTo(TaskId other) { 115 int jobIdComp = this.getJobId().compareTo(other.getJobId()); 116 if (jobIdComp == 0) { 117 if (this.getTaskType() == other.getTaskType()) { 118 return this.getId() - other.getId(); 119 } else { 120 return this.getTaskType().compareTo(other.getTaskType()); 121 } 122 } else { 123 return jobIdComp; 124 } 125 } 126 }