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      /**
042       * Get the <code>ApplicationId</code> of the <code>ApplicationAttempId</code>. 
043       * @return <code>ApplicationId</code> of the <code>ApplicationAttempId</code>
044       */
045      @Public
046      @Stable
047      public abstract ApplicationId getApplicationId();
048      
049      @Private
050      @Unstable
051      public abstract void setApplicationId(ApplicationId appID);
052      
053      /**
054       * Get the <code>attempt id</code> of the <code>Application</code>.
055       * @return <code>attempt id</code> of the <code>Application</code>
056       */
057      public abstract int getAttemptId();
058      
059      @Private
060      @Unstable
061      public abstract void setAttemptId(int attemptId);
062    
063      static final ThreadLocal<NumberFormat> attemptIdFormat =
064          new ThreadLocal<NumberFormat>() {
065            @Override
066            public NumberFormat initialValue() {
067              NumberFormat fmt = NumberFormat.getInstance();
068              fmt.setGroupingUsed(false);
069              fmt.setMinimumIntegerDigits(6);
070              return fmt;
071            }
072          };
073    
074      @Override
075      public int hashCode() {
076        // Generated by eclipse.
077        final int prime = 31;
078        int result = 1;
079        ApplicationId appId = getApplicationId();
080        result = prime * result +  appId.hashCode();
081        result = prime * result + getAttemptId();
082        return result;
083      }
084    
085      @Override
086      public boolean equals(Object obj) {
087        if (this == obj)
088          return true;
089        if (obj == null)
090          return false;
091        if (getClass() != obj.getClass())
092          return false;
093        ApplicationAttemptId other = (ApplicationAttemptId) obj;
094        if (!this.getApplicationId().equals(other.getApplicationId()))
095          return false;
096        if (this.getAttemptId() != other.getAttemptId())
097          return false;
098        return true;
099      }
100    
101      @Override
102      public int compareTo(ApplicationAttemptId other) {
103        int compareAppIds = this.getApplicationId().compareTo(
104            other.getApplicationId());
105        if (compareAppIds == 0) {
106          return this.getAttemptId() - other.getAttemptId();
107        } else {
108          return compareAppIds;
109        }
110      }
111    
112      @Override
113      public String toString() {
114        StringBuilder sb = new StringBuilder("appattempt_");
115        sb.append(this.getApplicationId().getClusterTimestamp()).append("_");
116        sb.append(ApplicationId.appIdFormat.get().format(
117            this.getApplicationId().getId()));
118        sb.append("_").append(attemptIdFormat.get().format(getAttemptId()));
119        return sb.toString();
120      }
121    }