YARN-2562. Changed ContainerId#toString() to be more readable. Contributed by Tsuyoshi OZAWA

This commit is contained in:
Jian He 2014-10-03 11:04:02 -07:00
parent 2d8e6e2c4a
commit eb6ce5e97c
4 changed files with 44 additions and 14 deletions

View File

@ -304,6 +304,9 @@ Release 2.6.0 - UNRELEASED
YARN-2627. Added the info logs of attemptFailuresValidityInterval and number
of previous failed attempts. (Xuan Gong via zjshen)
YARN-2562. Changed ContainerId#toString() to be more readable. (Tsuyoshi
OZAWA via jianhe)
OPTIMIZATIONS
BUG FIXES

View File

@ -37,6 +37,7 @@ import org.apache.hadoop.yarn.util.Records;
public abstract class ContainerId implements Comparable<ContainerId>{
private static final Splitter _SPLITTER = Splitter.on('_').trimResults();
private static final String CONTAINER_PREFIX = "container";
private static final String EPOCH_PREFIX = "e";
@Private
@Unstable
@ -158,10 +159,24 @@ public abstract class ContainerId implements Comparable<ContainerId>{
}
}
/**
* @return A string representation of containerId. The format is
* container_e*epoch*_*clusterTimestamp*_*appId*_*attemptId*_*containerId*
* when epoch is larger than 0
* (e.g. container_e17_1410901177871_0001_01_000005).
* *epoch* is increased when RM restarts or fails over.
* When epoch is 0, epoch is omitted
* (e.g. container_1410901177871_0001_01_000005).
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("container_");
sb.append(CONTAINER_PREFIX + "_");
long epoch = getContainerId() >> 40;
if (epoch > 0) {
sb.append(EPOCH_PREFIX)
.append(appAttemptIdAndEpochFormat.get().format(epoch)).append("_");;
}
ApplicationId appId = getApplicationAttemptId().getApplicationId();
sb.append(appId.getClusterTimestamp()).append("_");
sb.append(ApplicationId.appIdFormat.get().format(appId.getId()))
@ -170,10 +185,6 @@ public abstract class ContainerId implements Comparable<ContainerId>{
appAttemptIdAndEpochFormat.get().format(
getApplicationAttemptId().getAttemptId())).append("_");
sb.append(containerIdFormat.get().format(0xffffffffffL & getContainerId()));
long epoch = getContainerId() >> 40;
if (epoch > 0) {
sb.append("_").append(appAttemptIdAndEpochFormat.get().format(epoch));
}
return sb.toString();
}
@ -186,12 +197,19 @@ public abstract class ContainerId implements Comparable<ContainerId>{
+ containerIdStr);
}
try {
ApplicationAttemptId appAttemptID = toApplicationAttemptId(it);
long id = Long.parseLong(it.next());
String epochOrClusterTimestampStr = it.next();
long epoch = 0;
if (it.hasNext()) {
epoch = Integer.parseInt(it.next());
ApplicationAttemptId appAttemptID = null;
if (epochOrClusterTimestampStr.startsWith(EPOCH_PREFIX)) {
String epochStr = epochOrClusterTimestampStr;
epoch = Integer.parseInt(epochStr.substring(EPOCH_PREFIX.length()));
appAttemptID = toApplicationAttemptId(it);
} else {
String clusterTimestampStr = epochOrClusterTimestampStr;
long clusterTimestamp = Long.parseLong(clusterTimestampStr);
appAttemptID = toApplicationAttemptId(clusterTimestamp, it);
}
long id = Long.parseLong(it.next());
long cid = (epoch << 40) | id;
ContainerId containerId = ContainerId.newInstance(appAttemptID, cid);
return containerId;
@ -203,7 +221,12 @@ public abstract class ContainerId implements Comparable<ContainerId>{
private static ApplicationAttemptId toApplicationAttemptId(
Iterator<String> it) throws NumberFormatException {
ApplicationId appId = ApplicationId.newInstance(Long.parseLong(it.next()),
return toApplicationAttemptId(Long.parseLong(it.next()), it);
}
private static ApplicationAttemptId toApplicationAttemptId(
long clusterTimestamp, Iterator<String> it) throws NumberFormatException {
ApplicationId appId = ApplicationId.newInstance(clusterTimestamp,
Integer.parseInt(it.next()));
ApplicationAttemptId appAttemptId =
ApplicationAttemptId.newInstance(appId, Integer.parseInt(it.next()));

View File

@ -63,13 +63,15 @@ public class TestContainerId {
Assert.assertEquals(999799999997L, 0xffffffffffL & c7.getContainerId());
Assert.assertEquals(3, c7.getContainerId() >> 40);
Assert.assertEquals(
"container_" + ts + "_36473_4365472_999799999997_03", c7.toString());
"container_e03_" + ts + "_36473_4365472_999799999997",
c7.toString());
ContainerId c8 = newContainerId(36473, 4365472, ts, 844424930131965L);
Assert.assertEquals(1099511627773L, 0xffffffffffL & c8.getContainerId());
Assert.assertEquals(767, c8.getContainerId() >> 40);
Assert.assertEquals(
"container_" + ts + "_36473_4365472_1099511627773_767", c8.toString());
"container_e767_" + ts + "_36473_4365472_1099511627773",
c8.toString());
}
public static ContainerId newContainerId(int appId, int appAttemptId,

View File

@ -67,14 +67,16 @@ public class TestConverterUtils {
ContainerId id2 =
TestContainerId.newContainerId(36473, 4365472, ts, 4298334883325L);
String cid2 = ConverterUtils.toString(id2);
assertEquals("container_" + ts + "_36473_4365472_999799999997_03", cid2);
assertEquals(
"container_e03_" + ts + "_36473_4365472_999799999997", cid2);
ContainerId gen2 = ConverterUtils.toContainerId(cid2);
assertEquals(gen2.toString(), id2.toString());
ContainerId id3 =
TestContainerId.newContainerId(36473, 4365472, ts, 844424930131965L);
String cid3 = ConverterUtils.toString(id3);
assertEquals("container_" + ts + "_36473_4365472_1099511627773_767", cid3);
assertEquals(
"container_e767_" + ts + "_36473_4365472_1099511627773", cid3);
ContainerId gen3 = ConverterUtils.toContainerId(cid3);
assertEquals(gen3.toString(), id3.toString());
}