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 019package org.apache.hadoop.yarn.api.records; 020 021import java.text.NumberFormat; 022 023import org.apache.hadoop.classification.InterfaceAudience.Private; 024import org.apache.hadoop.classification.InterfaceAudience.Public; 025import org.apache.hadoop.classification.InterfaceStability.Stable; 026import org.apache.hadoop.classification.InterfaceStability.Unstable; 027 028/** 029 * <p><code>ContainerId</code> represents a globally unique identifier 030 * for a {@link Container} in the cluster.</p> 031 */ 032@Public 033@Stable 034public abstract class ContainerId implements Comparable<ContainerId>{ 035 /** 036 * Get the <code>ApplicationAttemptId</code> of the application to which 037 * the <code>Container</code> was assigned. 038 * @return <code>ApplicationAttemptId</code> of the application to which 039 * the <code>Container</code> was assigned 040 */ 041 @Public 042 @Stable 043 public abstract ApplicationAttemptId getApplicationAttemptId(); 044 045 @Private 046 @Unstable 047 public abstract void setApplicationAttemptId(ApplicationAttemptId atId); 048 049 /** 050 * Get the identifier of the <code>ContainerId</code>. 051 * @return identifier of the <code>ContainerId</code> 052 */ 053 @Public 054 @Stable 055 public abstract int getId(); 056 057 @Private 058 @Unstable 059 public abstract void setId(int id); 060 061 062 // TODO: fail the app submission if attempts are more than 10 or something 063 private static final ThreadLocal<NumberFormat> appAttemptIdFormat = 064 new ThreadLocal<NumberFormat>() { 065 @Override 066 public NumberFormat initialValue() { 067 NumberFormat fmt = NumberFormat.getInstance(); 068 fmt.setGroupingUsed(false); 069 fmt.setMinimumIntegerDigits(2); 070 return fmt; 071 } 072 }; 073 // TODO: Why thread local? 074 // ^ NumberFormat instances are not threadsafe 075 private static final ThreadLocal<NumberFormat> containerIdFormat = 076 new ThreadLocal<NumberFormat>() { 077 @Override 078 public NumberFormat initialValue() { 079 NumberFormat fmt = NumberFormat.getInstance(); 080 fmt.setGroupingUsed(false); 081 fmt.setMinimumIntegerDigits(6); 082 return fmt; 083 } 084 }; 085 086 @Override 087 public int hashCode() { 088 // Generated by eclipse. 089 final int prime = 31; 090 int result = 1; 091 result = prime * result + getId(); 092 result = prime * result + getApplicationAttemptId().hashCode(); 093 return result; 094 } 095 096 @Override 097 public boolean equals(Object obj) { 098 if (this == obj) 099 return true; 100 if (obj == null) 101 return false; 102 if (getClass() != obj.getClass()) 103 return false; 104 ContainerId other = (ContainerId) obj; 105 if (!this.getApplicationAttemptId().equals(other.getApplicationAttemptId())) 106 return false; 107 if (this.getId() != other.getId()) 108 return false; 109 return true; 110 } 111 112 @Override 113 public int compareTo(ContainerId other) { 114 if (this.getApplicationAttemptId().compareTo( 115 other.getApplicationAttemptId()) == 0) { 116 return this.getId() - other.getId(); 117 } else { 118 return this.getApplicationAttemptId().compareTo( 119 other.getApplicationAttemptId()); 120 } 121 122 } 123 124 @Override 125 public String toString() { 126 StringBuilder sb = new StringBuilder(); 127 sb.append("container_"); 128 ApplicationId appId = getApplicationAttemptId().getApplicationId(); 129 sb.append(appId.getClusterTimestamp()).append("_"); 130 sb.append(ApplicationId.appIdFormat.get().format(appId.getId())) 131 .append("_"); 132 sb.append( 133 appAttemptIdFormat.get().format( 134 getApplicationAttemptId().getAttemptId())).append("_"); 135 sb.append(containerIdFormat.get().format(getId())); 136 return sb.toString(); 137 } 138}