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      public static final String appIdStrPrefix = "application_";
042    
043      /**
044       * Get the short integer identifier of the <code>ApplicationId</code>
045       * which is unique for all applications started by a particular instance
046       * of the <code>ResourceManager</code>.
047       * @return short integer identifier of the <code>ApplicationId</code>
048       */
049      @Public
050      @Stable
051      public abstract int getId();
052      
053      @Private
054      @Unstable
055      public abstract void setId(int id);
056      
057      /**
058       * Get the <em>start time</em> of the <code>ResourceManager</code> which is 
059       * used to generate globally unique <code>ApplicationId</code>.
060       * @return <em>start time</em> of the <code>ResourceManager</code>
061       */
062      public abstract long getClusterTimestamp();
063      
064      @Private
065      @Unstable
066      public abstract void setClusterTimestamp(long clusterTimestamp);
067    
068      
069      
070      static final ThreadLocal<NumberFormat> appIdFormat =
071        new ThreadLocal<NumberFormat>() {
072          @Override
073          public NumberFormat initialValue() {
074            NumberFormat fmt = NumberFormat.getInstance();
075            fmt.setGroupingUsed(false);
076            fmt.setMinimumIntegerDigits(4);
077            return fmt;
078          }
079        };
080    
081      @Override
082      public int compareTo(ApplicationId other) {
083        if (this.getClusterTimestamp() - other.getClusterTimestamp() == 0) {
084          return this.getId() - other.getId();
085        } else {
086          return this.getClusterTimestamp() > other.getClusterTimestamp() ? 1 : 
087            this.getClusterTimestamp() < other.getClusterTimestamp() ? -1 : 0;
088        }
089      }
090    
091      @Override
092      public String toString() {
093        return appIdStrPrefix + this.getClusterTimestamp() + "_"
094            + appIdFormat.get().format(getId());
095      }
096    
097      @Override
098      public int hashCode() {
099        // Generated by eclipse.
100        final int prime = 31;
101        int result = 1;
102        long clusterTimestamp = getClusterTimestamp();
103        result = prime * result
104            + (int) (clusterTimestamp ^ (clusterTimestamp >>> 32));
105        result = prime * result + getId();
106        return result;
107      }
108    
109      @Override
110      public boolean equals(Object obj) {
111        if (this == obj)
112          return true;
113        if (obj == null)
114          return false;
115        if (getClass() != obj.getClass())
116          return false;
117        ApplicationId other = (ApplicationId) obj;
118        if (this.getClusterTimestamp() != other.getClusterTimestamp())
119          return false;
120        if (this.getId() != other.getId())
121          return false;
122        return true;
123      }
124    }