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