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 com.google.common.base.Splitter;
022
023import java.text.NumberFormat;
024import java.util.Iterator;
025import org.apache.hadoop.classification.InterfaceAudience.Private;
026import org.apache.hadoop.classification.InterfaceAudience.Public;
027import org.apache.hadoop.classification.InterfaceStability.Stable;
028import org.apache.hadoop.classification.InterfaceStability.Unstable;
029import org.apache.hadoop.yarn.util.Records;
030
031/**
032 * <p><code>ContainerId</code> represents a globally unique identifier
033 * for a {@link Container} in the cluster.</p>
034 */
035@Public
036@Stable
037public abstract class ContainerId implements Comparable<ContainerId>{
038  public static final long CONTAINER_ID_BITMASK = 0xffffffffffL;
039  private static final Splitter _SPLITTER = Splitter.on('_').trimResults();
040  private static final String CONTAINER_PREFIX = "container";
041  private static final String EPOCH_PREFIX = "e";
042
043  @Private
044  @Unstable
045  public static ContainerId newContainerId(ApplicationAttemptId appAttemptId,
046      long containerId) {
047    ContainerId id = Records.newRecord(ContainerId.class);
048    id.setContainerId(containerId);
049    id.setApplicationAttemptId(appAttemptId);
050    id.build();
051    return id;
052  }
053
054  @Private
055  @Deprecated
056  @Unstable
057  public static ContainerId newInstance(ApplicationAttemptId appAttemptId,
058      int containerId) {
059    ContainerId id = Records.newRecord(ContainerId.class);
060    id.setContainerId(containerId);
061    id.setApplicationAttemptId(appAttemptId);
062    id.build();
063    return id;
064  }
065
066  /**
067   * Get the <code>ApplicationAttemptId</code> of the application to which the
068   * <code>Container</code> was assigned.
069   * <p>
070   * Note: If containers are kept alive across application attempts via
071   * {@link ApplicationSubmissionContext#setKeepContainersAcrossApplicationAttempts(boolean)}
072   * the <code>ContainerId</code> does not necessarily contain the current
073   * running application attempt's <code>ApplicationAttemptId</code> This
074   * container can be allocated by previously exited application attempt and
075   * managed by the current running attempt thus have the previous application
076   * attempt's <code>ApplicationAttemptId</code>.
077   * </p>
078   * 
079   * @return <code>ApplicationAttemptId</code> of the application to which the
080   *         <code>Container</code> was assigned
081   */
082  @Public
083  @Stable
084  public abstract ApplicationAttemptId getApplicationAttemptId();
085  
086  @Private
087  @Unstable
088  protected abstract void setApplicationAttemptId(ApplicationAttemptId atId);
089
090  /**
091   * Get the lower 32 bits of identifier of the <code>ContainerId</code>,
092   * which doesn't include epoch. Note that this method will be marked as
093   * deprecated, so please use <code>getContainerId</code> instead.
094   * @return lower 32 bits of identifier of the <code>ContainerId</code>
095   */
096  @Public
097  @Deprecated
098  @Stable
099  public abstract int getId();
100
101  /**
102   * Get the identifier of the <code>ContainerId</code>. Upper 24 bits are
103   * reserved as epoch of cluster, and lower 40 bits are reserved as
104   * sequential number of containers.
105   * @return identifier of the <code>ContainerId</code>
106   */
107  @Public
108  @Unstable
109  public abstract long getContainerId();
110
111  @Private
112  @Unstable
113  protected abstract void setContainerId(long id);
114 
115  
116  // TODO: fail the app submission if attempts are more than 10 or something
117  private static final ThreadLocal<NumberFormat> appAttemptIdAndEpochFormat =
118      new ThreadLocal<NumberFormat>() {
119        @Override
120        public NumberFormat initialValue() {
121          NumberFormat fmt = NumberFormat.getInstance();
122          fmt.setGroupingUsed(false);
123          fmt.setMinimumIntegerDigits(2);
124          return fmt;
125        }
126      };
127  // TODO: Why thread local?
128  // ^ NumberFormat instances are not threadsafe
129  private static final ThreadLocal<NumberFormat> containerIdFormat =
130      new ThreadLocal<NumberFormat>() {
131        @Override
132        public NumberFormat initialValue() {
133          NumberFormat fmt = NumberFormat.getInstance();
134          fmt.setGroupingUsed(false);
135          fmt.setMinimumIntegerDigits(6);
136          return fmt;
137        }
138      };
139
140  @Override
141  public int hashCode() {
142    // Generated by IntelliJ IDEA 13.1.
143    int result = (int) (getContainerId() ^ (getContainerId() >>> 32));
144    result = 31 * result + getApplicationAttemptId().hashCode();
145    return result;
146  }
147
148  @Override
149  public boolean equals(Object obj) {
150    if (this == obj)
151      return true;
152    if (obj == null)
153      return false;
154    if (getClass() != obj.getClass())
155      return false;
156    ContainerId other = (ContainerId) obj;
157    if (!this.getApplicationAttemptId().equals(other.getApplicationAttemptId()))
158      return false;
159    if (this.getContainerId() != other.getContainerId())
160      return false;
161    return true;
162  }
163
164  @Override
165  public int compareTo(ContainerId other) {
166    if (this.getApplicationAttemptId().compareTo(
167        other.getApplicationAttemptId()) == 0) {
168      return Long.valueOf(getContainerId())
169          .compareTo(Long.valueOf(other.getContainerId()));
170    } else {
171      return this.getApplicationAttemptId().compareTo(
172          other.getApplicationAttemptId());
173    }
174  }
175
176  /**
177   * @return A string representation of containerId. The format is
178   * container_e*epoch*_*clusterTimestamp*_*appId*_*attemptId*_*containerId*
179   * when epoch is larger than 0
180   * (e.g. container_e17_1410901177871_0001_01_000005).
181   * *epoch* is increased when RM restarts or fails over.
182   * When epoch is 0, epoch is omitted
183   * (e.g. container_1410901177871_0001_01_000005).
184   */
185  @Override
186  public String toString() {
187    StringBuilder sb = new StringBuilder();
188    sb.append(CONTAINER_PREFIX + "_");
189    long epoch = getContainerId() >> 40;
190    if (epoch > 0) {
191      sb.append(EPOCH_PREFIX)
192          .append(appAttemptIdAndEpochFormat.get().format(epoch)).append("_");;
193    }
194    ApplicationId appId = getApplicationAttemptId().getApplicationId();
195    sb.append(appId.getClusterTimestamp()).append("_");
196    sb.append(ApplicationId.appIdFormat.get().format(appId.getId()))
197        .append("_");
198    sb.append(
199        appAttemptIdAndEpochFormat.get().format(
200            getApplicationAttemptId().getAttemptId())).append("_");
201    sb.append(containerIdFormat.get()
202        .format(CONTAINER_ID_BITMASK & getContainerId()));
203    return sb.toString();
204  }
205
206  @Public
207  @Unstable
208  public static ContainerId fromString(String containerIdStr) {
209    Iterator<String> it = _SPLITTER.split(containerIdStr).iterator();
210    if (!it.next().equals(CONTAINER_PREFIX)) {
211      throw new IllegalArgumentException("Invalid ContainerId prefix: "
212          + containerIdStr);
213    }
214    try {
215      String epochOrClusterTimestampStr = it.next();
216      long epoch = 0;
217      ApplicationAttemptId appAttemptID = null;
218      if (epochOrClusterTimestampStr.startsWith(EPOCH_PREFIX)) {
219        String epochStr = epochOrClusterTimestampStr;
220        epoch = Integer.parseInt(epochStr.substring(EPOCH_PREFIX.length()));
221        appAttemptID = toApplicationAttemptId(it);
222      } else {
223        String clusterTimestampStr = epochOrClusterTimestampStr;
224        long clusterTimestamp = Long.parseLong(clusterTimestampStr);
225        appAttemptID = toApplicationAttemptId(clusterTimestamp, it);
226      }
227      long id = Long.parseLong(it.next());
228      long cid = (epoch << 40) | id;
229      ContainerId containerId = ContainerId.newContainerId(appAttemptID, cid);
230      return containerId;
231    } catch (NumberFormatException n) {
232      throw new IllegalArgumentException("Invalid ContainerId: "
233          + containerIdStr, n);
234    }
235  }
236
237  private static ApplicationAttemptId toApplicationAttemptId(
238      Iterator<String> it) throws NumberFormatException {
239    return toApplicationAttemptId(Long.parseLong(it.next()), it);
240  }
241
242  private static ApplicationAttemptId toApplicationAttemptId(
243      long clusterTimestamp, Iterator<String> it) throws NumberFormatException {
244    ApplicationId appId = ApplicationId.newInstance(clusterTimestamp,
245        Integer.parseInt(it.next()));
246    ApplicationAttemptId appAttemptId =
247        ApplicationAttemptId.newInstance(appId, Integer.parseInt(it.next()));
248    return appAttemptId;
249  }
250
251  protected abstract void build();
252}