MAPREDUCE-2417. Fix Gridmix in RoundRobinUserResolver mode to map testing/proxy users to unique users in a trace.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/mapreduce/trunk@1097345 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index 7d80a9b..3783a61 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -80,6 +80,9 @@
 
   BUG FIXES
 
+    MAPREDUCE-2417. Fix Gridmix in RoundRobinUserResolver mode to
+    map testing/proxy users to unique users in a trace.
+
     MAPREDUCE-2307. Exception thrown in Jobtracker logs, when the Scheduler
     configured is FairScheduler. (Devaraj K via matei)
 
diff --git a/src/contrib/gridmix/src/java/org/apache/hadoop/mapred/gridmix/RoundRobinUserResolver.java b/src/contrib/gridmix/src/java/org/apache/hadoop/mapred/gridmix/RoundRobinUserResolver.java
index 813e047..4eadc9f 100644
--- a/src/contrib/gridmix/src/java/org/apache/hadoop/mapred/gridmix/RoundRobinUserResolver.java
+++ b/src/contrib/gridmix/src/java/org/apache/hadoop/mapred/gridmix/RoundRobinUserResolver.java
@@ -38,8 +38,13 @@
 
   private int uidx = 0;
   private List<UserGroupInformation> users = Collections.emptyList();
-  private final HashMap<UserGroupInformation,UserGroupInformation> usercache =
-    new HashMap<UserGroupInformation,UserGroupInformation>();
+
+  /**
+   *  Mapping between user names of original cluster and UGIs of proxy users of
+   *  simulated cluster
+   */
+  private final HashMap<String,UserGroupInformation> usercache =
+      new HashMap<String,UserGroupInformation>();
   
   /**
    * Userlist assumes one UGI per line, each UGI matching
@@ -89,11 +94,12 @@
   @Override
   public synchronized boolean setTargetUsers(URI userloc, Configuration conf)
   throws IOException {
+    uidx = 0;
     users = parseUserList(userloc, conf);
     if (users.size() == 0) {
       throw new IOException(buildEmptyUsersErrorMsg(userloc));
     }
-    usercache.keySet().retainAll(users);
+    usercache.clear();
     return true;
   }
 
@@ -105,20 +111,13 @@
   @Override
   public synchronized UserGroupInformation getTargetUgi(
     UserGroupInformation ugi) {
-    UserGroupInformation ret = usercache.get(ugi);
-    if (null == ret) {
-      ret = users.get(uidx++ % users.size());
-      usercache.put(ugi, ret);
+    // UGI of proxy user
+    UserGroupInformation targetUGI = usercache.get(ugi.getUserName());
+    if (null == targetUGI) {
+      targetUGI = users.get(uidx++ % users.size());
+      usercache.put(ugi.getUserName(), targetUGI);
     }
-    UserGroupInformation val = null;
-    try {
-      val = 
-        UserGroupInformation.createProxyUser(ret.getUserName(), 
-          UserGroupInformation.getLoginUser());
-    } catch (IOException e) {
-      LOG.error("Error while creating the proxy user " ,e);
-    }
-    return val;
+    return targetUGI;
   }
 
   /**
diff --git a/src/contrib/gridmix/src/test/org/apache/hadoop/mapred/gridmix/TestUserResolve.java b/src/contrib/gridmix/src/test/org/apache/hadoop/mapred/gridmix/TestUserResolve.java
index 97c6d81..c8b532b 100644
--- a/src/contrib/gridmix/src/test/org/apache/hadoop/mapred/gridmix/TestUserResolve.java
+++ b/src/contrib/gridmix/src/test/org/apache/hadoop/mapred/gridmix/TestUserResolve.java
@@ -117,25 +117,37 @@
 
     // Create user resource file with valid content
     writeUserList(usersFilePath,
-        "user0,groupA,groupB,groupC\nuser1,groupA,groupC\n"
-        + "user2,groupB\nuser3,groupA,groupB,groupC\n");
+        "user0,groupA,groupB,groupC\nuser1,groupA,groupC\n");
+    validateValidUsersFile(rslv, userRsrc);
+  }
 
-    // Validate RoundRobinUserResolver for the case of
-    // user resource file with valid content.
-    assertTrue(rslv.setTargetUsers(new URI(usersFilePath.toString()), conf));
+  // Validate RoundRobinUserResolver for the case of
+  // user resource file with valid content.
+  private void validateValidUsersFile(UserResolver rslv, URI userRsrc)
+      throws IOException {
+    assertTrue(rslv.setTargetUsers(userRsrc, conf));
     UserGroupInformation ugi1 = UserGroupInformation.createRemoteUser("hfre0");
     assertEquals("user0", rslv.getTargetUgi(ugi1).getUserName());
     assertEquals("user1", 
       rslv.getTargetUgi(UserGroupInformation.createRemoteUser("hfre1"))
           .getUserName());
-    assertEquals("user2", 
+    assertEquals("user0",
       rslv.getTargetUgi(UserGroupInformation.createRemoteUser("hfre2"))
           .getUserName());
     assertEquals("user0", rslv.getTargetUgi(ugi1).getUserName());
-    assertEquals("user3", 
+    assertEquals("user1",
       rslv.getTargetUgi(UserGroupInformation.createRemoteUser("hfre3"))
           .getUserName());
-    assertEquals("user0", rslv.getTargetUgi(ugi1).getUserName());
+    // Verify if same user comes again, its mapped user name should be
+    // correct even though UGI is constructed again.
+    assertEquals("user0", rslv.getTargetUgi(
+        UserGroupInformation.createRemoteUser("hfre0")).getUserName());
+    assertEquals("user0",
+        rslv.getTargetUgi(UserGroupInformation.createRemoteUser("hfre5"))
+            .getUserName());
+    assertEquals("user0",
+        rslv.getTargetUgi(UserGroupInformation.createRemoteUser("hfre0"))
+            .getUserName());
   }
 
   @Test