1
0
فهرست منبع

YARN-2515. Updated ConverterUtils#toContainerId to parse epoch. Contributed by Tsuyoshi OZAWA

Jian He 10 سال پیش
والد
کامیت
0974f434c4

+ 3 - 0
hadoop-yarn-project/CHANGES.txt

@@ -190,6 +190,9 @@ Release 2.6.0 - UNRELEASED
     YARN-2507. Documented CrossOriginFilter configurations for the timeline
     YARN-2507. Documented CrossOriginFilter configurations for the timeline
     server. (Jonathan Eagles via zjshen)
     server. (Jonathan Eagles via zjshen)
 
 
+    YARN-2515. Updated ConverterUtils#toContainerId to parse epoch.
+    (Tsuyoshi OZAWA via jianhe)
+
   OPTIMIZATIONS
   OPTIMIZATIONS
 
 
   BUG FIXES
   BUG FIXES

+ 38 - 1
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ContainerId.java

@@ -18,8 +18,10 @@
 
 
 package org.apache.hadoop.yarn.api.records;
 package org.apache.hadoop.yarn.api.records;
 
 
-import java.text.NumberFormat;
+import com.google.common.base.Splitter;
 
 
+import java.text.NumberFormat;
+import java.util.Iterator;
 import org.apache.hadoop.classification.InterfaceAudience.Private;
 import org.apache.hadoop.classification.InterfaceAudience.Private;
 import org.apache.hadoop.classification.InterfaceAudience.Public;
 import org.apache.hadoop.classification.InterfaceAudience.Public;
 import org.apache.hadoop.classification.InterfaceStability.Stable;
 import org.apache.hadoop.classification.InterfaceStability.Stable;
@@ -33,6 +35,8 @@ import org.apache.hadoop.yarn.util.Records;
 @Public
 @Public
 @Stable
 @Stable
 public abstract class ContainerId implements Comparable<ContainerId>{
 public abstract class ContainerId implements Comparable<ContainerId>{
+  private static final Splitter _SPLITTER = Splitter.on('_').trimResults();
+  private static final String CONTAINER_PREFIX = "container";
 
 
   @Private
   @Private
   @Unstable
   @Unstable
@@ -163,5 +167,38 @@ public abstract class ContainerId implements Comparable<ContainerId>{
     return sb.toString();
     return sb.toString();
   }
   }
 
 
+  @Public
+  @Unstable
+  public static ContainerId fromString(String containerIdStr) {
+    Iterator<String> it = _SPLITTER.split(containerIdStr).iterator();
+    if (!it.next().equals(CONTAINER_PREFIX)) {
+      throw new IllegalArgumentException("Invalid ContainerId prefix: "
+          + containerIdStr);
+    }
+    try {
+      ApplicationAttemptId appAttemptID = toApplicationAttemptId(it);
+      int id = Integer.parseInt(it.next());
+      int epoch = 0;
+      if (it.hasNext()) {
+        epoch = Integer.parseInt(it.next());
+      }
+      int cid = (epoch << 22) | id;
+      ContainerId containerId = ContainerId.newInstance(appAttemptID, cid);
+      return containerId;
+    } catch (NumberFormatException n) {
+      throw new IllegalArgumentException("Invalid ContainerId: "
+          + containerIdStr, n);
+    }
+  }
+
+  private static ApplicationAttemptId toApplicationAttemptId(
+      Iterator<String> it) throws NumberFormatException {
+    ApplicationId appId = ApplicationId.newInstance(Long.parseLong(it.next()),
+        Integer.parseInt(it.next()));
+    ApplicationAttemptId appAttemptId =
+        ApplicationAttemptId.newInstance(appId, Integer.parseInt(it.next()));
+    return appAttemptId;
+  }
+
   protected abstract void build();
   protected abstract void build();
 }
 }

+ 1 - 14
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ConverterUtils.java

@@ -168,20 +168,7 @@ public class ConverterUtils {
   }
   }
 
 
   public static ContainerId toContainerId(String containerIdStr) {
   public static ContainerId toContainerId(String containerIdStr) {
-    Iterator<String> it = _split(containerIdStr).iterator();
-    if (!it.next().equals(CONTAINER_PREFIX)) {
-      throw new IllegalArgumentException("Invalid ContainerId prefix: "
-          + containerIdStr);
-    }
-    try {
-      ApplicationAttemptId appAttemptID = toApplicationAttemptId(it);
-      ContainerId containerId =
-          ContainerId.newInstance(appAttemptID, Integer.parseInt(it.next()));
-      return containerId;
-    } catch (NumberFormatException n) {
-      throw new IllegalArgumentException("Invalid ContainerId: "
-          + containerIdStr, n);
-    }
+    return ContainerId.fromString(containerIdStr);
   }
   }
 
 
   public static ApplicationAttemptId toApplicationAttemptId(
   public static ApplicationAttemptId toApplicationAttemptId(

+ 4 - 0
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/api/TestContainerId.java

@@ -54,10 +54,14 @@ public class TestContainerId {
     long ts = System.currentTimeMillis();
     long ts = System.currentTimeMillis();
     ContainerId c6 = newContainerId(36473, 4365472, ts, 25645811);
     ContainerId c6 = newContainerId(36473, 4365472, ts, 25645811);
     Assert.assertEquals("container_10_0001_01_000001", c1.toString());
     Assert.assertEquals("container_10_0001_01_000001", c1.toString());
+    Assert.assertEquals(c1,
+        ContainerId.fromString("container_10_0001_01_000001"));
     Assert.assertEquals(479987, 0x003fffff & c6.getId());
     Assert.assertEquals(479987, 0x003fffff & c6.getId());
     Assert.assertEquals(6, c6.getId() >> 22);
     Assert.assertEquals(6, c6.getId() >> 22);
     Assert.assertEquals("container_" + ts + "_36473_4365472_479987_06",
     Assert.assertEquals("container_" + ts + "_36473_4365472_479987_06",
         c6.toString());
         c6.toString());
+    Assert.assertEquals(c6,
+        ContainerId.fromString("container_" + ts + "_36473_4365472_479987_06"));
   }
   }
 
 
   public static ContainerId newContainerId(int appId, int appAttemptId,
   public static ContainerId newContainerId(int appId, int appAttemptId,

+ 9 - 0
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/TestConverterUtils.java

@@ -55,6 +55,15 @@ public class TestConverterUtils {
     assertEquals(gen, id);
     assertEquals(gen, id);
   }
   }
 
 
+  @Test
+  public void testContainerIdWithEpoch() throws URISyntaxException {
+    ContainerId id = TestContainerId.newContainerId(0, 0, 0, 25645811);
+    String cid = ConverterUtils.toString(id);
+    assertEquals("container_0_0000_00_479987_06", cid);
+    ContainerId gen = ConverterUtils.toContainerId(cid);
+    assertEquals(gen.toString(), id.toString());
+  }
+
   @Test
   @Test
   public void testContainerIdNull() throws URISyntaxException {
   public void testContainerIdNull() throws URISyntaxException {
     assertNull(ConverterUtils.toString((ContainerId)null));
     assertNull(ConverterUtils.toString((ContainerId)null));