Java Code Examples for org.apache.hadoop.security.UserGroupInformation.addCredentials()

The following are Jave code examples for showing how to use addCredentials() of the org.apache.hadoop.security.UserGroupInformation class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: angel   File: ParameterServer.java   View Source Code Vote up 4 votes
public static void main(String[] argv)  {
  LOG.info("Starting Parameter Server");
  int serverIndex = Integer.valueOf(System.getenv(AngelEnvironment.PARAMETERSERVER_ID.name()));
  String appMasterHost = System.getenv(AngelEnvironment.LISTEN_ADDR.name());
  int appMasterPort = Integer.valueOf(System.getenv(AngelEnvironment.LISTEN_PORT.name()));

  int attemptIndex = Integer.valueOf(System.getenv(AngelEnvironment.PS_ATTEMPT_ID.name()));

  Configuration conf = new Configuration();
  conf.addResource(AngelConf.ANGEL_JOB_CONF_FILE);

  String user = System.getenv(ApplicationConstants.Environment.USER.name());
  UserGroupInformation.setConfiguration(conf);
  
  String runningMode = conf.get(AngelConf.ANGEL_RUNNING_MODE,
      AngelConf.DEFAULT_ANGEL_RUNNING_MODE);
  if(runningMode.equals(RunningMode.ANGEL_PS_WORKER.toString())){
    LOG.debug("AngelEnvironment.TASK_NUMBER.name()=" + AngelEnvironment.TASK_NUMBER.name());
    conf.set(AngelConf.ANGEL_TASK_ACTUAL_NUM,
        System.getenv(AngelEnvironment.TASK_NUMBER.name()));
  }

  final ParameterServer psServer =
      new ParameterServer(serverIndex, attemptIndex, appMasterHost, appMasterPort, conf);

  try{
    Credentials credentials =
      UserGroupInformation.getCurrentUser().getCredentials();
    UserGroupInformation psUGI = UserGroupInformation.createRemoteUser(System
      .getenv(ApplicationConstants.Environment.USER.toString()));
    // Add tokens to new user so that it may execute its task correctly.
    psUGI.addCredentials(credentials);

    psUGI.doAs(new PrivilegedExceptionAction<Object>() {
      @Override
      public Object run() throws Exception {
        psServer.initialize();
        psServer.start();
        return null;
      }
    });
  } catch (Throwable x) {
    LOG.fatal("Start PS failed ", x);
    psServer.failed(x.getMessage());
  }
  LOG.info("Starting Parameter Server successfully.");
}