incubator-hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tjungb...@apache.org
Subject svn commit: r1215495 - in /incubator/hama/trunk: core/src/main/java/org/apache/hama/bsp/ core/src/main/java/org/apache/hama/util/ core/src/test/java/org/apache/hama/util/ yarn/src/main/java/org/apache/hama/bsp/
Date Sat, 17 Dec 2011 14:18:56 GMT
Author: tjungblut
Date: Sat Dec 17 14:18:55 2011
New Revision: 1215495

URL: http://svn.apache.org/viewvc?rev=1215495&view=rev
Log:
Getting username from JVM property, minor refactoring

Added:
    incubator/hama/trunk/core/src/main/java/org/apache/hama/util/EnvironmentUtil.java   (with
props)
    incubator/hama/trunk/core/src/test/java/org/apache/hama/util/TestEnvironmentUtil.java
  (with props)
Modified:
    incubator/hama/trunk/core/src/main/java/org/apache/hama/bsp/BSPJobClient.java
    incubator/hama/trunk/yarn/src/main/java/org/apache/hama/bsp/YARNBSPJobClient.java

Modified: incubator/hama/trunk/core/src/main/java/org/apache/hama/bsp/BSPJobClient.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/core/src/main/java/org/apache/hama/bsp/BSPJobClient.java?rev=1215495&r1=1215494&r2=1215495&view=diff
==============================================================================
--- incubator/hama/trunk/core/src/main/java/org/apache/hama/bsp/BSPJobClient.java (original)
+++ incubator/hama/trunk/core/src/main/java/org/apache/hama/bsp/BSPJobClient.java Sat Dec
17 14:18:55 2011
@@ -27,7 +27,6 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
-import java.util.StringTokenizer;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -49,11 +48,11 @@ import org.apache.hadoop.ipc.RPC;
 import org.apache.hadoop.mapreduce.Job;
 import org.apache.hadoop.net.NetUtils;
 import org.apache.hadoop.util.ReflectionUtils;
-import org.apache.hadoop.util.Shell;
 import org.apache.hadoop.util.Tool;
 import org.apache.hadoop.util.ToolRunner;
 import org.apache.hama.HamaConfiguration;
 import org.apache.hama.ipc.JobSubmissionProtocol;
+import org.apache.hama.util.EnvironmentUtil;
 
 /**
  * BSPJobClient is the primary interface for the user-job to interact with the
@@ -86,7 +85,7 @@ public class BSPJobClient extends Config
     JobProfile profile;
     JobStatus status;
     long statustime;
-    
+
     public NetworkedJob() {
     }
 
@@ -334,8 +333,8 @@ public class BSPJobClient extends Config
     }
 
     // Set the user's name and working directory
-    job.setUser(getUnixUserName());
-    job.set("group.name", getUnixUserGroupName(job.getUser()));
+    job.setUser(EnvironmentUtil.getUnixUserName());
+    job.set("group.name", EnvironmentUtil.getUnixUserGroupName(job.getUser()));
     if (job.getWorkingDirectory() == null) {
       job.setWorkingDirectory(fs.getWorkingDirectory());
     }
@@ -448,7 +447,7 @@ public class BSPJobClient extends Config
   }
 
   private String getPartitionName(int i) {
-    return "part-"  + String.valueOf(100000 + i).substring(1, 6);
+    return "part-" + String.valueOf(100000 + i).substring(1, 6);
   }
 
   /**
@@ -589,7 +588,7 @@ public class BSPJobClient extends Config
         lastReport = report;
       }
     }
-    
+
     LOG.info("The total number of supersteps: " + info.getSuperstepCount());
     // TODO job.getCounters().log(LOG);
     return job.isSuccessful();
@@ -875,54 +874,6 @@ public class BSPJobClient extends Config
     }
   }
 
-  /*
-   * Helper methods for unix operations
-   */
-
-  static String getUnixUserName() throws IOException {
-    String[] result = executeShellCommand(new String[] { Shell.USER_NAME_COMMAND });
-    if (result.length != 1) {
-      throw new IOException("Expect one token as the result of "
-          + Shell.USER_NAME_COMMAND + ": " + toString(result));
-    }
-    return result[0];
-  }
-
-  static String getUnixUserGroupName(String user) throws IOException {
-    String[] result = executeShellCommand(new String[] { "bash", "-c",
-        "id -Gn " + user });
-    if (result.length < 1) {
-      throw new IOException("Expect one token as the result of "
-          + "bash -c id -Gn " + user + ": " + toString(result));
-    }
-    return result[0];
-  }
-
-  protected static String toString(String[] strArray) {
-    if (strArray == null || strArray.length == 0) {
-      return "";
-    }
-    StringBuilder buf = new StringBuilder(strArray[0]);
-    for (int i = 1; i < strArray.length; i++) {
-      buf.append(' ');
-      buf.append(strArray[i]);
-    }
-    return buf.toString();
-  }
-
-  protected static String[] executeShellCommand(String[] command)
-      throws IOException {
-    String groups = Shell.execCommand(command);
-    StringTokenizer tokenizer = new StringTokenizer(groups);
-    int numOfTokens = tokenizer.countTokens();
-    String[] tokens = new String[numOfTokens];
-    for (int i = 0; tokenizer.hasMoreTokens(); i++) {
-      tokens[i] = tokenizer.nextToken();
-    }
-
-    return tokens;
-  }
-
   static class RawSplit implements Writable {
     private String splitClass;
     private BytesWritable bytes = new BytesWritable();

Added: incubator/hama/trunk/core/src/main/java/org/apache/hama/util/EnvironmentUtil.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/core/src/main/java/org/apache/hama/util/EnvironmentUtil.java?rev=1215495&view=auto
==============================================================================
--- incubator/hama/trunk/core/src/main/java/org/apache/hama/util/EnvironmentUtil.java (added)
+++ incubator/hama/trunk/core/src/main/java/org/apache/hama/util/EnvironmentUtil.java Sat
Dec 17 14:18:55 2011
@@ -0,0 +1,82 @@
+/**
+ * Copyright 2007 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.util;
+
+import java.io.IOException;
+import java.util.StringTokenizer;
+
+import org.apache.hadoop.util.Shell;
+
+/**
+ * This class is responsible to get usernames and groups.
+ * 
+ */
+public class EnvironmentUtil {
+
+  public static String getUnixUserName() throws IOException {
+    String jvmName = System.getProperty("user.name");
+    if (jvmName == null) {
+      String[] result = executeShellCommand(new String[] { Shell.USER_NAME_COMMAND });
+      if (result.length != 1) {
+        throw new IOException("Expect one token as the result of "
+            + Shell.USER_NAME_COMMAND + ": " + toString(result));
+      }
+      return result[0];
+    } else {
+      return jvmName;
+    }
+  }
+
+  public static String getUnixUserGroupName(String user) throws IOException {
+    String[] result = executeShellCommand(new String[] { "bash", "-c",
+        "id -Gn " + user });
+    if (result.length < 1) {
+      throw new IOException("Expect one token as the result of "
+          + "bash -c id -Gn " + user + ": " + toString(result));
+    }
+    return result[0];
+  }
+
+  public static String[] executeShellCommand(String[] command)
+      throws IOException {
+    String groups = Shell.execCommand(command);
+    StringTokenizer tokenizer = new StringTokenizer(groups);
+    int numOfTokens = tokenizer.countTokens();
+    String[] tokens = new String[numOfTokens];
+    for (int i = 0; tokenizer.hasMoreTokens(); i++) {
+      tokens[i] = tokenizer.nextToken();
+    }
+
+    return tokens;
+  }
+
+  public static String toString(String[] strArray) {
+    if (strArray == null || strArray.length == 0) {
+      return "";
+    }
+    StringBuilder buf = new StringBuilder(strArray[0]);
+    for (int i = 1; i < strArray.length; i++) {
+      buf.append(' ');
+      buf.append(strArray[i]);
+    }
+    return buf.toString();
+  }
+
+}

Propchange: incubator/hama/trunk/core/src/main/java/org/apache/hama/util/EnvironmentUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/hama/trunk/core/src/test/java/org/apache/hama/util/TestEnvironmentUtil.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/core/src/test/java/org/apache/hama/util/TestEnvironmentUtil.java?rev=1215495&view=auto
==============================================================================
--- incubator/hama/trunk/core/src/test/java/org/apache/hama/util/TestEnvironmentUtil.java
(added)
+++ incubator/hama/trunk/core/src/test/java/org/apache/hama/util/TestEnvironmentUtil.java
Sat Dec 17 14:18:55 2011
@@ -0,0 +1,42 @@
+/**
+ * Copyright 2007 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.util;
+
+import junit.framework.TestCase;
+import org.apache.log4j.Logger;
+
+public class TestEnvironmentUtil extends TestCase {
+  
+  static final Logger LOG = Logger.getLogger(TestEnvironmentUtil.class);
+  
+  public void testGetUserName() throws Exception {
+    String unixUserName = EnvironmentUtil.getUnixUserName();
+    assertNotNull(unixUserName);
+    LOG.info("username of this jvm is: " + unixUserName);
+  }
+  
+  public void testGetUserGroup() throws Exception {
+    String unixUserGroup = EnvironmentUtil.getUnixUserGroupName(EnvironmentUtil.getUnixUserName());
+    assertNotNull(unixUserGroup);
+    LOG.info("usergroup of this jvm is: " + unixUserGroup);
+  }
+  
+
+}

Propchange: incubator/hama/trunk/core/src/test/java/org/apache/hama/util/TestEnvironmentUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/hama/trunk/yarn/src/main/java/org/apache/hama/bsp/YARNBSPJobClient.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/yarn/src/main/java/org/apache/hama/bsp/YARNBSPJobClient.java?rev=1215495&r1=1215494&r2=1215495&view=diff
==============================================================================
--- incubator/hama/trunk/yarn/src/main/java/org/apache/hama/bsp/YARNBSPJobClient.java (original)
+++ incubator/hama/trunk/yarn/src/main/java/org/apache/hama/bsp/YARNBSPJobClient.java Sat
Dec 17 14:18:55 2011
@@ -44,6 +44,7 @@ import org.apache.hadoop.yarn.api.record
 import org.apache.hadoop.yarn.util.ConverterUtils;
 import org.apache.hadoop.yarn.util.Records;
 import org.apache.hama.HamaConfiguration;
+import org.apache.hama.util.EnvironmentUtil;
 
 public class YARNBSPJobClient extends BSPJobClient {
 
@@ -72,7 +73,7 @@ public class YARNBSPJobClient extends BS
     }
 
     if (getConf().get("bsp.user.name") == null) {
-      String s = getUnixUserName();
+      String s = EnvironmentUtil.getUnixUserName();
       getConf().set("bsp.user.name", s);
       LOG.info("Retrieved username: " + s);
     }



Mime
View raw message