Program: 115603
Project: 115603
Buggy source:
/** 
 * Utility to create application instance by parsing the deployment policy for a monitor
 * @param application the application
 * @return whether the instance created or not
 * @throws TopologyInConsistentException
 * @throws PolicyValidationException
 */
private boolean createInstanceAndStartDependency(Application application) throws TopologyInConsistentException, PolicyValidationException {
  boolean initialStartup=true;
  try {
    List<String> instanceIds=new ArrayList<String>();
    String instanceId;
    ApplicationPolicy applicationPolicy=PolicyManager.getInstance().getApplicationPolicy(application.getApplicationPolicyId());
    if (applicationPolicy == null) {
      String msg=String.format("String_Node_Str" + "String_Node_Str",appId);
      log.error(msg);
      throw new RuntimeException(msg);
    }
    NetworkPartitionAlgorithmContext algorithmContext=AutoscalerContext.getInstance().getNetworkPartitionAlgorithmContext(appId);
    if (algorithmContext == null) {
      String msg=String.format("String_Node_Str" + "String_Node_Str",appId);
      log.error(msg);
      throw new RuntimeException(msg);
    }
    String networkPartitionAlgorithmName=applicationPolicy.getAlgorithm();
    if (log.isDebugEnabled()) {
      String msg=String.format("String_Node_Str",networkPartitionAlgorithmName,appId);
      log.debug(msg);
    }
    NetworkPartitionAlgorithm algorithm=getNetworkPartitionAlgorithm(networkPartitionAlgorithmName);
    if (algorithm == null) {
      String msg=String.format("String_Node_Str" + "String_Node_Str",appId);
      log.error(msg);
      throw new RuntimeException(msg);
    }
    List<String> nextNetworkPartitions=algorithm.getNextNetworkPartitions(algorithmContext);
    if (nextNetworkPartitions == null || nextNetworkPartitions.isEmpty()) {
      String msg=String.format("String_Node_Str" + "String_Node_Str",appId);
      log.warn(msg);
      return false;
    }
    for (    String networkPartitionIds : nextNetworkPartitions) {
      ParentLevelNetworkPartitionContext context=new ParentLevelNetworkPartitionContext(networkPartitionIds);
      ApplicationInstance appInstance=(ApplicationInstance)application.getInstanceByNetworkPartitionId(context.getId());
      if (appInstance != null) {
        instanceId=handleApplicationInstanceCreation(application,context,appInstance);
        initialStartup=false;
      }
 else {
        instanceId=handleApplicationInstanceCreation(application,context,null);
      }
      instanceIds.add(instanceId);
      log.info("String_Node_Str" + networkPartitionIds + "String_Node_Str"+ instanceId);
    }
    startDependency(application,instanceIds);
  }
 catch (  Exception e) {
    log.error(String.format("String_Node_Str",appId),e);
  }
  return initialStartup;
}

Fixed version (ground truth):
/** 
 * Utility to create application instance by parsing the deployment policy for a monitor
 * @param application the application
 * @return whether the instance created or not
 * @throws TopologyInConsistentException
 * @throws PolicyValidationException
 */
private boolean createInstanceAndStartDependency(Application application) throws TopologyInConsistentException, PolicyValidationException {
  boolean initialStartup=true;
  try {
    List<String> instanceIds=new ArrayList<String>();
    String instanceId;
    ApplicationPolicy applicationPolicy=PolicyManager.getInstance().getApplicationPolicy(application.getApplicationPolicyId());
    if (applicationPolicy == null) {
      String msg=String.format("String_Node_Str" + "String_Node_Str",appId);
      log.error(msg);
      throw new RuntimeException(msg);
    }
    NetworkPartitionAlgorithmContext algorithmContext=AutoscalerContext.getInstance().getNetworkPartitionAlgorithmContext(appId);
    if (algorithmContext == null) {
      String msg=String.format("String_Node_Str" + "String_Node_Str",appId);
      log.error(msg);
      throw new RuntimeException(msg);
    }
    String networkPartitionAlgorithmName=applicationPolicy.getAlgorithm();
    if (log.isDebugEnabled()) {
      String msg=String.format("String_Node_Str",networkPartitionAlgorithmName,appId);
      log.debug(msg);
    }
    NetworkPartitionAlgorithm algorithm=getNetworkPartitionAlgorithm(networkPartitionAlgorithmName);
    if (algorithm == null) {
      String msg=String.format("String_Node_Str" + "String_Node_Str",appId);
      log.error(msg);
      throw new RuntimeException(msg);
    }
    List<String> nextNetworkPartitions=algorithm.getNextNetworkPartitions(algorithmContext);
    if (nextNetworkPartitions == null || nextNetworkPartitions.isEmpty()) {
      String msg=String.format("String_Node_Str" + "String_Node_Str",appId);
      log.warn(msg);
      return false;
    }
    for (    String networkPartitionIds : nextNetworkPartitions) {
      ParentLevelNetworkPartitionContext context=new ParentLevelNetworkPartitionContext(networkPartitionIds);
      ApplicationInstance appInstance=(ApplicationInstance)application.getInstanceByNetworkPartitionId(context.getId());
      if (appInstance != null) {
        instanceId=handleApplicationInstanceCreation(application,context,appInstance);
        initialStartup=false;
      }
 else {
        instanceId=handleApplicationInstanceCreation(application,context,null);
      }
      instanceIds.add(instanceId);
      log.info("String_Node_Str" + networkPartitionIds + "String_Node_Str"+ instanceId);
    }
    Map<String,ApplicationInstance> instanceMap=application.getInstanceIdToInstanceContextMap();
    for (    ApplicationInstance instance : instanceMap.values()) {
      if (!instanceIds.contains(instance.getInstanceId())) {
        ParentLevelNetworkPartitionContext context=new ParentLevelNetworkPartitionContext(instance.getNetworkPartitionId());
        ApplicationInstance appInstance=(ApplicationInstance)application.getInstanceByNetworkPartitionId(context.getId());
        handleApplicationInstanceCreation(application,context,appInstance);
        instanceIds.add(instance.getInstanceId());
        log.info("String_Node_Str" + "String_Node_Str" + instance.getNetworkPartitionId() + "String_Node_Str"+ instance.getInstanceId());
      }
    }
    startDependency(application,instanceIds);
  }
 catch (  Exception e) {
    log.error(String.format("String_Node_Str",appId),e);
  }
  return initialStartup;
}