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>ApplicationAttemptId</code> denotes the particular <em>attempt</em>
030     * of an <code>ApplicationMaster</code> for a given {@link ApplicationId}.</p>
031     * 
032     * <p>Multiple attempts might be needed to run an application to completion due
033     * to temporal failures of the <code>ApplicationMaster</code> such as hardware
034     * failures, connectivity issues etc. on the node on which it was scheduled.</p>
035     */
036    @Public
037    @Stable
038    public abstract class ApplicationAttemptId implements
039        Comparable<ApplicationAttemptId> {
040      
041      public static final String appAttemptIdStrPrefix = "appattempt_";
042    
043      /**
044       * Get the <code>ApplicationId</code> of the <code>ApplicationAttempId</code>. 
045       * @return <code>ApplicationId</code> of the <code>ApplicationAttempId</code>
046       */
047      @Public
048      @Stable
049      public abstract ApplicationId getApplicationId();
050      
051      @Private
052      @Unstable
053      public abstract void setApplicationId(ApplicationId appID);
054      
055      /**
056       * Get the <code>attempt id</code> of the <code>Application</code>.
057       * @return <code>attempt id</code> of the <code>Application</code>
058       */
059      public abstract int getAttemptId();
060      
061      @Private
062      @Unstable
063      public abstract void setAttemptId(int attemptId);
064    
065      static final ThreadLocal<NumberFormat> attemptIdFormat =
066          new ThreadLocal<NumberFormat>() {
067            @Override
068            public NumberFormat initialValue() {
069              NumberFormat fmt = NumberFormat.getInstance();
070              fmt.setGroupingUsed(false);
071              fmt.setMinimumIntegerDigits(6);
072              return fmt;
073            }
074          };
075    
076      @Override
077      public int hashCode() {
078        // Generated by eclipse.
079        final int prime = 31;
080        int result = 1;
081        ApplicationId appId = getApplicationId();
082        result = prime * result +  appId.hashCode();
083        result = prime * result + getAttemptId();
084        return result;
085      }
086    
087      @Override
088      public boolean equals(Object obj) {
089        if (this == obj)
090          return true;
091        if (obj == null)
092          return false;
093        if (getClass() != obj.getClass())
094          return false;
095        ApplicationAttemptId other = (ApplicationAttemptId) obj;
096        if (!this.getApplicationId().equals(other.getApplicationId()))
097          return false;
098        if (this.getAttemptId() != other.getAttemptId())
099          return false;
100        return true;
101      }
102    
103      @Override
104      public int compareTo(ApplicationAttemptId other) {
105        int compareAppIds = this.getApplicationId().compareTo(
106            other.getApplicationId());
107        if (compareAppIds == 0) {
108          return this.getAttemptId() - other.getAttemptId();
109        } else {
110          return compareAppIds;
111        }
112      }
113    
114      @Override
115      public String toString() {
116        StringBuilder sb = new StringBuilder(appAttemptIdStrPrefix);
117        sb.append(this.getApplicationId().getClusterTimestamp()).append("_");
118        sb.append(ApplicationId.appIdFormat.get().format(
119            this.getApplicationId().getId()));
120        sb.append("_").append(attemptIdFormat.get().format(getAttemptId()));
121        return sb.toString();
122      }
123    }