Program: 26559
Project: 26559
Buggy source:
/** 
 * Create and run the cluster.
 * @return exit code
 * @throws Throwable on a failure
 */
private int createAndRunCluster(String clustername) throws Throwable {
  String hoyaClusterDir=serviceArgs.hoyaClusterURI;
  URI hoyaClusterURI=new URI(hoyaClusterDir);
  Path clusterDirPath=new Path(hoyaClusterURI);
  Path clusterSpecPath=new Path(clusterDirPath,HoyaKeys.CLUSTER_SPECIFICATION_FILE);
  FileSystem fs=getClusterFS();
  ClusterDescription.verifyClusterSpecExists(clustername,fs,clusterSpecPath);
  ClusterDescription clusterSpec=ClusterDescription.load(fs,clusterSpecPath);
  File confDir=getLocalConfDir();
  if (!confDir.exists() || !confDir.isDirectory()) {
    throw new BadCommandArgumentsException("String_Node_Str",confDir);
  }
  YarnConfiguration conf=new YarnConfiguration(getConfig());
  String providerType=clusterSpec.type;
  log.info("String_Node_Str",providerType);
  HoyaProviderFactory factory=HoyaProviderFactory.createHoyaProviderFactory(providerType);
  providerService=factory.createServerProvider();
  runChildService(providerService);
  providerService.validateClusterSpec(clusterSpec);
  HoyaAMClientProvider clientProvider=new HoyaAMClientProvider(conf);
  InetSocketAddress address=HoyaUtils.getRmSchedulerAddress(conf);
  log.info("String_Node_Str",address);
  yarmRPC=YarnRPC.create(conf);
  appMasterContainerID=ConverterUtils.toContainerId(HoyaUtils.mandatoryEnvVariable(ApplicationConstants.Environment.CONTAINER_ID.name()));
  appAttemptID=appMasterContainerID.getApplicationAttemptId();
  ApplicationId appid=appAttemptID.getApplicationId();
  log.info("String_Node_Str",appid.getId());
  Credentials credentials=UserGroupInformation.getCurrentUser().getCredentials();
  DataOutputBuffer dob=new DataOutputBuffer();
  credentials.writeTokenStorageToStream(dob);
  Iterator<Token<?>> iter=credentials.getAllTokens().iterator();
  while (iter.hasNext()) {
    Token<?> token=iter.next();
    log.info("String_Node_Str",token.getKind());
    if (token.getKind().equals(AMRMTokenIdentifier.KIND_NAME)) {
      iter.remove();
    }
  }
  allTokens=ByteBuffer.wrap(dob.getData(),0,dob.getLength());
  secretManager=new ClientToAMTokenSecretManager(appAttemptID,null);
  int heartbeatInterval=HEARTBEAT_INTERVAL;
  asyncRMClient=AMRMClientAsync.createAMRMClientAsync(HEARTBEAT_INTERVAL,this);
  addService(asyncRMClient);
  rmOperationHandler=new AsyncRMOperationHandler(asyncRMClient);
  runChildService(asyncRMClient);
  nmClientAsync=new NMClientAsyncImpl("String_Node_Str",this);
  runChildService(nmClientAsync);
  startHoyaRPCServer();
  InetSocketAddress rpcServiceAddr=rpcService.getConnectAddress();
  appMasterHostname=rpcServiceAddr.getHostName();
  appMasterRpcPort=rpcServiceAddr.getPort();
  appMasterTrackingUrl=null;
  log.info("String_Node_Str",appMasterHostname,appMasterRpcPort);
  List<ProviderRole> providerRoles=new ArrayList<ProviderRole>(providerService.getRoles());
  providerRoles.addAll(clientProvider.getRoles());
  int infoport=clusterSpec.getRoleOptInt(ROLE_HOYA_AM,RoleKeys.APP_INFOPORT,0);
  if (0 == infoport) {
    infoport=HoyaUtils.findFreePort(providerService.getDefaultMasterInfoPort(),128);
    clusterSpec.setRoleOpt(ROLE_HOYA_AM,RoleKeys.APP_INFOPORT,infoport);
  }
  appMasterTrackingUrl="String_Node_Str" + appMasterHostname + "String_Node_Str"+ infoport;
  address=HoyaUtils.getRmSchedulerAddress(asyncRMClient.getConfig());
  log.info("String_Node_Str",appMasterRpcPort,appMasterTrackingUrl);
  RegisterApplicationMasterResponse response=asyncRMClient.registerApplicationMaster(appMasterHostname,appMasterRpcPort,appMasterTrackingUrl);
  Resource maxResources=response.getMaximumResourceCapability();
  containerMaxMemory=maxResources.getMemory();
  containerMaxCores=maxResources.getVirtualCores();
  appState.setContainerLimits(maxResources.getMemory(),maxResources.getVirtualCores());
  boolean securityEnabled=UserGroupInformation.isSecurityEnabled();
  if (securityEnabled) {
    secretManager.setMasterKey(response.getClientToAMTokenMasterKey().array());
    applicationACLs=response.getApplicationACLs();
    rpcService.getServer().refreshServiceAcl(conf,new HoyaAMPolicyProvider());
  }
  String siteXMLFilename=providerService.getSiteXMLFilename();
  File siteXML=new File(confDir,siteXMLFilename);
  if (!siteXML.exists()) {
    throw new BadCommandArgumentsException("String_Node_Str",confDir,siteXMLFilename,HoyaUtils.listDir(confDir));
  }
  Configuration siteConf=ConfigHelper.loadConfFromFile(siteXML);
  providerService.validateApplicationConfiguration(clusterSpec,confDir,securityEnabled);
  Path historyDir=new Path(clusterDirPath,HISTORY_DIR_NAME);
  appState.buildInstance(clusterSpec,siteConf,providerRoles,fs,historyDir);
  appState.buildAppMasterNode(appMasterContainerID);
  launchService=new RoleLaunchService(this,providerService,getClusterFS(),new Path(getDFSConfDir()));
  runChildService(launchService);
  boolean noLocalProcess=clusterSpec.getDesiredInstanceCount(ROLE_HOYA_AM,1) <= 0;
  if (noLocalProcess) {
    log.info("String_Node_Str");
    eventCallbackEvent();
  }
 else {
    appState.noteAMLaunched();
    launchProviderService(clusterSpec,confDir);
  }
  try {
    waitForAMCompletionSignal();
  }
  finally {
    finish();
  }
  return buildExitCode();
}

Fixed version (ground truth):
/** 
 * Create and run the cluster.
 * @return exit code
 * @throws Throwable on a failure
 */
private int createAndRunCluster(String clustername) throws Throwable {
  String hoyaClusterDir=serviceArgs.hoyaClusterURI;
  URI hoyaClusterURI=new URI(hoyaClusterDir);
  Path clusterDirPath=new Path(hoyaClusterURI);
  Path clusterSpecPath=new Path(clusterDirPath,HoyaKeys.CLUSTER_SPECIFICATION_FILE);
  FileSystem fs=getClusterFS();
  ClusterDescription.verifyClusterSpecExists(clustername,fs,clusterSpecPath);
  ClusterDescription clusterSpec=ClusterDescription.load(fs,clusterSpecPath);
  File confDir=getLocalConfDir();
  if (!confDir.exists() || !confDir.isDirectory()) {
    throw new BadCommandArgumentsException("String_Node_Str",confDir);
  }
  YarnConfiguration conf=new YarnConfiguration(getConfig());
  String providerType=clusterSpec.type;
  log.info("String_Node_Str",providerType);
  HoyaProviderFactory factory=HoyaProviderFactory.createHoyaProviderFactory(providerType);
  providerService=factory.createServerProvider();
  runChildService(providerService);
  providerService.validateClusterSpec(clusterSpec);
  HoyaAMClientProvider clientProvider=new HoyaAMClientProvider(conf);
  InetSocketAddress address=HoyaUtils.getRmSchedulerAddress(conf);
  log.info("String_Node_Str",address);
  yarmRPC=YarnRPC.create(conf);
  appMasterContainerID=ConverterUtils.toContainerId(HoyaUtils.mandatoryEnvVariable(ApplicationConstants.Environment.CONTAINER_ID.name()));
  appAttemptID=appMasterContainerID.getApplicationAttemptId();
  ApplicationId appid=appAttemptID.getApplicationId();
  log.info("String_Node_Str",appid.getId());
  Credentials credentials=UserGroupInformation.getCurrentUser().getCredentials();
  DataOutputBuffer dob=new DataOutputBuffer();
  credentials.writeTokenStorageToStream(dob);
  Iterator<Token<?>> iter=credentials.getAllTokens().iterator();
  while (iter.hasNext()) {
    Token<?> token=iter.next();
    log.info("String_Node_Str",token.getKind());
    if (token.getKind().equals(AMRMTokenIdentifier.KIND_NAME)) {
      iter.remove();
    }
  }
  allTokens=ByteBuffer.wrap(dob.getData(),0,dob.getLength());
  secretManager=new ClientToAMTokenSecretManager(appAttemptID,null);
  int heartbeatInterval=HEARTBEAT_INTERVAL;
  asyncRMClient=AMRMClientAsync.createAMRMClientAsync(HEARTBEAT_INTERVAL,this);
  addService(asyncRMClient);
  rmOperationHandler=new AsyncRMOperationHandler(asyncRMClient);
  runChildService(asyncRMClient);
  nmClientAsync=new NMClientAsyncImpl("String_Node_Str",this);
  runChildService(nmClientAsync);
  startHoyaRPCServer();
  InetSocketAddress rpcServiceAddr=rpcService.getConnectAddress();
  appMasterHostname=rpcServiceAddr.getHostName();
  appMasterRpcPort=rpcServiceAddr.getPort();
  appMasterTrackingUrl=null;
  log.info("String_Node_Str",appMasterHostname,appMasterRpcPort);
  List<ProviderRole> providerRoles=new ArrayList<ProviderRole>(providerService.getRoles());
  providerRoles.addAll(clientProvider.getRoles());
  int infoport=clusterSpec.getRoleOptInt(ROLE_HOYA_AM,RoleKeys.APP_INFOPORT,0);
  if (0 == infoport) {
    infoport=HoyaUtils.findFreePort(providerService.getDefaultMasterInfoPort(),128);
    clusterSpec.setRoleOpt(ROLE_HOYA_AM,RoleKeys.APP_INFOPORT,infoport);
  }
  appMasterTrackingUrl="String_Node_Str" + appMasterHostname + "String_Node_Str"+ infoport;
  address=HoyaUtils.getRmSchedulerAddress(asyncRMClient.getConfig());
  log.info("String_Node_Str",appMasterRpcPort,appMasterTrackingUrl);
  RegisterApplicationMasterResponse response=asyncRMClient.registerApplicationMaster(appMasterHostname,appMasterRpcPort,appMasterTrackingUrl);
  Resource maxResources=response.getMaximumResourceCapability();
  containerMaxMemory=maxResources.getMemory();
  containerMaxCores=maxResources.getVirtualCores();
  appState.setContainerLimits(maxResources.getMemory(),maxResources.getVirtualCores());
  boolean securityEnabled=UserGroupInformation.isSecurityEnabled();
  if (securityEnabled) {
    secretManager.setMasterKey(response.getClientToAMTokenMasterKey().array());
    applicationACLs=response.getApplicationACLs();
    rpcService.getServer().refreshServiceAcl(conf,new HoyaAMPolicyProvider());
  }
  String siteXMLFilename=providerService.getSiteXMLFilename();
  File siteXML=new File(confDir,siteXMLFilename);
  if (!siteXML.exists()) {
    dob.close();
    throw new BadCommandArgumentsException("String_Node_Str",confDir,siteXMLFilename,HoyaUtils.listDir(confDir));
  }
  Configuration siteConf=ConfigHelper.loadConfFromFile(siteXML);
  providerService.validateApplicationConfiguration(clusterSpec,confDir,securityEnabled);
  Path historyDir=new Path(clusterDirPath,HISTORY_DIR_NAME);
  appState.buildInstance(clusterSpec,siteConf,providerRoles,fs,historyDir);
  appState.buildAppMasterNode(appMasterContainerID);
  launchService=new RoleLaunchService(this,providerService,getClusterFS(),new Path(getDFSConfDir()));
  runChildService(launchService);
  boolean noLocalProcess=clusterSpec.getDesiredInstanceCount(ROLE_HOYA_AM,1) <= 0;
  if (noLocalProcess) {
    log.info("String_Node_Str");
    eventCallbackEvent();
  }
 else {
    appState.noteAMLaunched();
    launchProviderService(clusterSpec,confDir);
  }
  try {
    waitForAMCompletionSignal();
  }
  finally {
    finish();
  }
  return buildExitCode();
}