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.timeline;
020
021import java.util.Iterator;
022
023import org.apache.hadoop.classification.InterfaceAudience.Private;
024import org.apache.hadoop.classification.InterfaceAudience.Public;
025import org.apache.hadoop.classification.InterfaceStability.Unstable;
026import org.apache.hadoop.yarn.api.records.ApplicationId;
027import com.google.common.base.Splitter;
028
029/**
030 * <p><code>TimelineEntityGroupId</code> is an abstract way for
031 * timeline service users to represent ???a group of related timeline data.
032 * For example, all entities that represents one data flow DAG execution
033 * can be grouped into one timeline entity group. </p>
034 */
035@Public
036@Unstable
037public class TimelineEntityGroupId implements
038    Comparable<TimelineEntityGroupId> {
039
040  private static final Splitter SPLITTER = Splitter.on('_').trimResults();
041
042  private ApplicationId applicationId;
043  private String id;
044
045  @Private
046  @Unstable
047  public static final String TIMELINE_ENTITY_GROUPID_STR_PREFIX =
048      "timelineEntityGroupId";
049
050  public TimelineEntityGroupId() {
051
052  }
053
054  public static TimelineEntityGroupId newInstance(ApplicationId applicationId,
055      String id) {
056    TimelineEntityGroupId timelineEntityGroupId =
057        new TimelineEntityGroupId();
058    timelineEntityGroupId.setApplicationId(applicationId);
059    timelineEntityGroupId.setTimelineEntityGroupId(id);
060    return timelineEntityGroupId;
061  }
062
063  /**
064   * Get the <code>ApplicationId</code> of the
065   * <code>TimelineEntityGroupId</code>.
066   *
067   * @return <code>ApplicationId</code> of the
068   *         <code>TimelineEntityGroupId</code>
069   */
070  public ApplicationId getApplicationId() {
071    return this.applicationId;
072  }
073
074  public void setApplicationId(ApplicationId appID) {
075    this.applicationId = appID;
076  }
077
078  /**
079   * Get the <code>timelineEntityGroupId</code>.
080   *
081   * @return <code>timelineEntityGroupId</code>
082   */
083  public String getTimelineEntityGroupId() {
084    return this.id;
085  }
086
087  @Private
088  @Unstable
089  protected void setTimelineEntityGroupId(String timelineEntityGroupId) {
090    this.id = timelineEntityGroupId;
091  }
092
093  @Override
094  public int hashCode() {
095    int result = getTimelineEntityGroupId().hashCode();
096    result = 31 * result + getApplicationId().hashCode();
097    return result;
098  }
099
100  @Override
101  public boolean equals(Object obj) {
102    if (this == obj) {
103      return true;
104    }
105    if (obj == null) {
106      return false;
107    }
108    if (getClass() != obj.getClass()) {
109      return false;
110    }
111    TimelineEntityGroupId otherObject = (TimelineEntityGroupId) obj;
112    if (!this.getApplicationId().equals(otherObject.getApplicationId())) {
113      return false;
114    }
115    if (!this.getTimelineEntityGroupId().equals(
116        otherObject.getTimelineEntityGroupId())) {
117      return false;
118    }
119    return true;
120  }
121
122  @Override
123  public int compareTo(TimelineEntityGroupId other) {
124    int compareAppIds =
125        this.getApplicationId().compareTo(other.getApplicationId());
126    if (compareAppIds == 0) {
127      return this.getTimelineEntityGroupId().compareTo(
128        other.getTimelineEntityGroupId());
129    } else {
130      return compareAppIds;
131    }
132  }
133
134  @Override
135  public String toString() {
136    StringBuilder sb = new StringBuilder();
137    sb.append(TIMELINE_ENTITY_GROUPID_STR_PREFIX + "_");
138    ApplicationId appId = getApplicationId();
139    sb.append(appId.getClusterTimestamp()).append("_");
140    sb.append(appId.getId()).append("_");
141    sb.append(getTimelineEntityGroupId());
142    return sb.toString();
143  }
144
145  public static TimelineEntityGroupId
146      fromString(String timelineEntityGroupIdStr) {
147    StringBuffer buf = new StringBuffer();
148    Iterator<String> it = SPLITTER.split(timelineEntityGroupIdStr).iterator();
149    if (!it.next().equals(TIMELINE_ENTITY_GROUPID_STR_PREFIX)) {
150      throw new IllegalArgumentException(
151        "Invalid TimelineEntityGroupId prefix: " + timelineEntityGroupIdStr);
152    }
153    ApplicationId appId =
154        ApplicationId.newInstance(Long.parseLong(it.next()),
155          Integer.parseInt(it.next()));
156    buf.append(it.next());
157    while (it.hasNext()) {
158      buf.append("_");
159      buf.append(it.next());
160    }
161    return TimelineEntityGroupId.newInstance(appId, buf.toString());
162  }
163}