Program: 115751
Project: 115751
Buggy source:
public synchronized void monitor(){
  final Collection<NetworkPartitionContext> networkPartitionContexts=this.getNetworkPartitionCtxts().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);
            }
          }
        }
        int nonTerminatedInstancesCount=networkPartitionContext.getNonTerminatedInstancesCount();
        int minInstances=((GroupLevelNetworkPartitionContext)networkPartitionContext).getMinInstanceCount();
        if (nonTerminatedInstancesCount < minInstances) {
          int instancesToBeCreated=minInstances - nonTerminatedInstancesCount;
          for (int i=0; i < instancesToBeCreated; i++) {
            for (            InstanceContext parentInstanceContext : parent.getNetworkPartitionContext(networkPartitionContext.getId()).getInstanceIdToInstanceContextMap().values()) {
              ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
              int activeAppInstances=appMonitor.getNetworkPartitionContext(networkPartitionContext.getId()).getActiveInstancesCount();
              if (activeAppInstances > 0) {
                createInstanceOnDemand(parentInstanceContext.getId());
              }
            }
          }
        }
      }
    }
  }
;
  executorService.execute(monitoringRunnable);
}

Fixed version (ground truth):
public synchronized void monitor(){
  final Collection<NetworkPartitionContext> networkPartitionContexts=this.getNetworkPartitionCtxts().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);
            }
          }
        }
        int nonTerminatedInstancesCount=networkPartitionContext.getNonTerminatedInstancesCount();
        int minInstances=((GroupLevelNetworkPartitionContext)networkPartitionContext).getMinInstanceCount();
        int maxInstances=((GroupLevelNetworkPartitionContext)networkPartitionContext).getMaxInstanceCount();
        int activeInstances=((GroupLevelNetworkPartitionContext)networkPartitionContext).getActiveInstancesCount();
        if (nonTerminatedInstancesCount < minInstances) {
          int instancesToBeCreated=minInstances - nonTerminatedInstancesCount;
          for (int i=0; i < instancesToBeCreated; i++) {
            for (            InstanceContext parentInstanceContext : parent.getNetworkPartitionContext(networkPartitionContext.getId()).getInstanceIdToInstanceContextMap().values()) {
              ApplicationMonitor appMonitor=AutoscalerContext.getInstance().getAppMonitor(appId);
              int activeAppInstances=appMonitor.getNetworkPartitionContext(networkPartitionContext.getId()).getActiveInstancesCount();
              if (activeAppInstances > 0) {
                createInstanceOnDemand(parentInstanceContext.getId());
              }
            }
          }
        }
        if (activeInstances > maxInstances) {
          int instancesToBeTerminated=activeInstances - maxInstances;
          Collection<InstanceContext> contexts=networkPartitionContext.getInstanceIdToInstanceContextMap().values();
          List<InstanceContext> contextList=new ArrayList<InstanceContext>();
          for (int i=0; i < instancesToBeTerminated; i++) {
            InstanceContext instanceContext=contextList.get(i);
            handleScalingDownBeyondMin(instanceContext,networkPartitionContext,true);
          }
        }
      }
    }
  }
;
  executorService.execute(monitoringRunnable);
}