Program: 115613
Project: 115613
Buggy source:
/** 
 * This thread will monitor the children across all the network partitions and take decision for scale-up or scale-down
 */
public synchronized void monitor(){
  final Collection<NetworkPartitionContext> networkPartitionContexts=this.getNetworkPartitionContextsMap().values();
  Runnable monitoringRunnable=new Runnable(){
    @Override public void run(){
      if (log.isDebugEnabled()) {
        log.debug("String_Node_Str" + id);
      }
      for (      NetworkPartitionContext networkPartitionContext : networkPartitionContexts) {
        for (        InstanceContext instanceContext : networkPartitionContext.getInstanceIdToInstanceContextMap().values()) {
          GroupInstance instance=(GroupInstance)instanceIdToInstanceMap.get(instanceContext.getId());
          if (instance.getStatus().getCode() <= GroupStatus.Active.getCode()) {
            if (!instanceContext.getIdToScalingOverMaxEvent().isEmpty()) {
              handleScalingUpBeyondMax(instanceContext,networkPartitionContext);
            }
 else             if (!instanceContext.getIdToScalingEvent().isEmpty()) {
              handleDependentScaling(instanceContext,networkPartitionContext);
            }
 else             if (!instanceContext.getIdToScalingDownBeyondMinEvent().isEmpty()) {
              handleScalingDownBeyondMin(instanceContext,networkPartitionContext,false);
            }
            instanceContext.setIdToScalingOverMaxEvent(new ConcurrentHashMap<String,ScalingUpBeyondMaxEvent>());
            instanceContext.setIdToScalingEvent(new ConcurrentHashMap<String,ScalingEvent>());
            instanceContext.setIdToScalingOverMaxEvent(new ConcurrentHashMap<String,ScalingUpBeyondMaxEvent>());
          }
        }
        ParentLevelNetworkPartitionContext parentLevelNetworkPartitionContext=(ParentLevelNetworkPartitionContext)networkPartitionContext;
        Collection<Instance> parentInstances=parent.getInstances();
        for (        Instance parentInstance : parentInstances) {
          int nonTerminatedInstancesCount=parentLevelNetworkPartitionContext.getNonTerminatedInstancesCount(parentInstance.getInstanceId());
          int minInstances=parentLevelNetworkPartitionContext.getMinInstanceCount();
          int maxInstances=parentLevelNetworkPartitionContext.getMaxInstanceCount();
          int activeInstances=parentLevelNetworkPartitionContext.getActiveInstancesCount(parentInstance.getInstanceId());
          if (nonTerminatedInstancesCount < minInstances) {
            int instancesToBeCreated=minInstances - nonTerminatedInstancesCount;
            for (int i=0; i < instancesToBeCreated; i++) {
              for (              InstanceContext parentInstanceContext : parent.getNetworkPartitionContext(parentLevelNetworkPartitionContext.getId()).getInstanceIdToInstanceContextMap().values()) {
                ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
                int activeAppInstances=((ParentLevelNetworkPartitionContext)appMonitor.getNetworkPartitionContext(parentLevelNetworkPartitionContext.getId())).getActiveInstancesCount();
                if (activeAppInstances > 0) {
                  createInstanceOnDemand(parentInstanceContext.getId());
                }
              }
            }
          }
          if (activeInstances > maxInstances) {
            int instancesToBeTerminated=activeInstances - maxInstances;
            List<InstanceContext> contexts=((ParentLevelNetworkPartitionContext)networkPartitionContext).getInstanceIdToInstanceContextMap(parentInstance.getInstanceId());
            List<InstanceContext> contextList=new ArrayList<InstanceContext>(contexts);
            for (int i=0; i < instancesToBeTerminated; i++) {
              InstanceContext instanceContext=contextList.get(i);
              handleScalingDownBeyondMin(instanceContext,networkPartitionContext,true);
            }
          }
        }
      }
    }
  }
;
  executorService.execute(monitoringRunnable);
}

Fixed version (ground truth):
/** 
 * This thread will monitor the children across all the network partitions and take decision for scale-up or scale-down
 */
public synchronized void monitor(){
  final Collection<NetworkPartitionContext> networkPartitionContexts=this.getNetworkPartitionContextsMap().values();
  Runnable monitoringRunnable=new Runnable(){
    @Override public void run(){
      if (log.isDebugEnabled()) {
        log.debug("String_Node_Str" + id);
      }
      for (      NetworkPartitionContext networkPartitionContext : networkPartitionContexts) {
        for (        InstanceContext instanceContext : networkPartitionContext.getInstanceIdToInstanceContextMap().values()) {
          GroupInstance instance=(GroupInstance)instanceIdToInstanceMap.get(instanceContext.getId());
          if (instance.getStatus().getCode() <= GroupStatus.Active.getCode()) {
            if (!instanceContext.getIdToScalingOverMaxEvent().isEmpty()) {
              handleScalingUpBeyondMax(instanceContext,networkPartitionContext);
            }
 else             if (!instanceContext.getIdToScalingEvent().isEmpty()) {
              handleDependentScaling(instanceContext,networkPartitionContext);
            }
 else             if (!instanceContext.getIdToScalingDownBeyondMinEvent().isEmpty()) {
              handleScalingDownBeyondMin(instanceContext,networkPartitionContext,false);
            }
            instanceContext.setIdToScalingOverMaxEvent(new ConcurrentHashMap<String,ScalingUpBeyondMaxEvent>());
            instanceContext.setIdToScalingEvent(new ConcurrentHashMap<String,ScalingEvent>());
            instanceContext.setIdToScalingOverMaxEvent(new ConcurrentHashMap<String,ScalingUpBeyondMaxEvent>());
          }
        }
        ParentLevelNetworkPartitionContext parentLevelNetworkPartitionContext=(ParentLevelNetworkPartitionContext)networkPartitionContext;
        Collection<Instance> parentInstances=parent.getInstances();
        for (        Instance parentInstance : parentInstances) {
          int nonTerminatedInstancesCount=parentLevelNetworkPartitionContext.getNonTerminatedInstancesCount(parentInstance.getInstanceId());
          int minInstances=parentLevelNetworkPartitionContext.getMinInstanceCount();
          int maxInstances=parentLevelNetworkPartitionContext.getMaxInstanceCount();
          int activeInstances=parentLevelNetworkPartitionContext.getActiveInstancesCount(parentInstance.getInstanceId());
          if (nonTerminatedInstancesCount < minInstances) {
            int instancesToBeCreated=minInstances - nonTerminatedInstancesCount;
            for (int i=0; i < instancesToBeCreated; i++) {
              for (              InstanceContext parentInstanceContext : parent.getNetworkPartitionContext(parentLevelNetworkPartitionContext.getId()).getInstanceIdToInstanceContextMap().values()) {
                ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
                int activeAppInstances=((ParentLevelNetworkPartitionContext)appMonitor.getNetworkPartitionContext(parentLevelNetworkPartitionContext.getId())).getActiveInstancesCount();
                if (activeAppInstances > 0) {
                  if (log.isDebugEnabled()) {
                    log.debug("String_Node_Str" + appId + "String_Node_Str"+ id+ "String_Node_Str");
                  }
                  createInstanceOnDemand(parentInstanceContext.getId());
                }
              }
            }
          }
          if (activeInstances > maxInstances) {
            int instancesToBeTerminated=activeInstances - maxInstances;
            List<InstanceContext> contexts=((ParentLevelNetworkPartitionContext)networkPartitionContext).getInstanceIdToInstanceContextMap(parentInstance.getInstanceId());
            List<InstanceContext> contextList=new ArrayList<InstanceContext>(contexts);
            for (int i=0; i < instancesToBeTerminated; i++) {
              InstanceContext instanceContext=contextList.get(i);
              if (log.isDebugEnabled()) {
                log.debug("String_Node_Str" + appId + "String_Node_Str"+ id+ "String_Node_Str"+ "String_Node_Str"+ instancesToBeTerminated);
              }
              handleScalingDownBeyondMin(instanceContext,networkPartitionContext,true);
            }
          }
        }
      }
    }
  }
;
  executorService.execute(monitoringRunnable);
}