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.yarn.api.records;
020
021 import java.text.NumberFormat;
022
023 import org.apache.hadoop.classification.InterfaceAudience.Private;
024 import org.apache.hadoop.classification.InterfaceAudience.Public;
025 import org.apache.hadoop.classification.InterfaceStability.Stable;
026 import org.apache.hadoop.classification.InterfaceStability.Unstable;
027
028 /**
029 * <p><code>ApplicationId</code> represents the <em>globally unique</em>
030 * identifier for an application.</p>
031 *
032 * <p>The globally unique nature of the identifier is achieved by using the
033 * <em>cluster timestamp</em> i.e. start-time of the
034 * <code>ResourceManager</code> along with a monotonically increasing counter
035 * for the application.</p>
036 */
037 @Public
038 @Stable
039 public abstract class ApplicationId implements Comparable<ApplicationId> {
040
041 /**
042 * Get the short integer identifier of the <code>ApplicationId</code>
043 * which is unique for all applications started by a particular instance
044 * of the <code>ResourceManager</code>.
045 * @return short integer identifier of the <code>ApplicationId</code>
046 */
047 @Public
048 @Stable
049 public abstract int getId();
050
051 @Private
052 @Unstable
053 public abstract void setId(int id);
054
055 /**
056 * Get the <em>start time</em> of the <code>ResourceManager</code> which is
057 * used to generate globally unique <code>ApplicationId</code>.
058 * @return <em>start time</em> of the <code>ResourceManager</code>
059 */
060 public abstract long getClusterTimestamp();
061
062 @Private
063 @Unstable
064 public abstract void setClusterTimestamp(long clusterTimestamp);
065
066
067
068 static final ThreadLocal<NumberFormat> appIdFormat =
069 new ThreadLocal<NumberFormat>() {
070 @Override
071 public NumberFormat initialValue() {
072 NumberFormat fmt = NumberFormat.getInstance();
073 fmt.setGroupingUsed(false);
074 fmt.setMinimumIntegerDigits(4);
075 return fmt;
076 }
077 };
078
079 @Override
080 public int compareTo(ApplicationId other) {
081 if (this.getClusterTimestamp() - other.getClusterTimestamp() == 0) {
082 return this.getId() - other.getId();
083 } else {
084 return this.getClusterTimestamp() > other.getClusterTimestamp() ? 1 :
085 this.getClusterTimestamp() < other.getClusterTimestamp() ? -1 : 0;
086 }
087 }
088
089 @Override
090 public String toString() {
091 return "application_" + this.getClusterTimestamp() + "_"
092 + appIdFormat.get().format(getId());
093 }
094
095 @Override
096 public int hashCode() {
097 // Generated by eclipse.
098 final int prime = 31;
099 int result = 1;
100 long clusterTimestamp = getClusterTimestamp();
101 result = prime * result
102 + (int) (clusterTimestamp ^ (clusterTimestamp >>> 32));
103 result = prime * result + getId();
104 return result;
105 }
106
107 @Override
108 public boolean equals(Object obj) {
109 if (this == obj)
110 return true;
111 if (obj == null)
112 return false;
113 if (getClass() != obj.getClass())
114 return false;
115 ApplicationId other = (ApplicationId) obj;
116 if (this.getClusterTimestamp() != other.getClusterTimestamp())
117 return false;
118 if (this.getId() != other.getId())
119 return false;
120 return true;
121 }
122 }