Program: 106702
Project: 106702
Buggy source:
@Override public List<HostVO> getHostsToRebalance(long msId,int avLoad){
  List<HostVO> allHosts=_hostDao.listByManagementServer(msId);
  if (allHosts.size() <= avLoad) {
    s_logger.debug("String_Node_Str" + allHosts.size() + "String_Node_Str"+ msId+ "String_Node_Str"+ avLoad+ "String_Node_Str");
    return null;
  }
  List<HostVO> directHosts=_hostDao.listDirectHostsBy(msId,Status.Up);
  if (directHosts.isEmpty()) {
    s_logger.debug("String_Node_Str" + Status.Up + "String_Node_Str"+ msId+ "String_Node_Str");
    return null;
  }
  Map<Long,List<HostVO>> hostToClusterMap=new HashMap<Long,List<HostVO>>();
  for (  HostVO directHost : directHosts) {
    Long clusterId=directHost.getClusterId();
    List<HostVO> directHostsPerCluster=null;
    if (!hostToClusterMap.containsKey(clusterId)) {
      directHostsPerCluster=new ArrayList<HostVO>();
    }
 else {
      directHostsPerCluster=hostToClusterMap.get(clusterId);
    }
    directHostsPerCluster.add(directHost);
    hostToClusterMap.put(clusterId,directHostsPerCluster);
  }
  hostToClusterMap=sortByClusterSize(hostToClusterMap);
  int hostsToGive=allHosts.size() - avLoad;
  int hostsLeftToGive=hostsToGive;
  int hostsLeft=directHosts.size();
  List<HostVO> hostsToReturn=new ArrayList<HostVO>();
  for (  Long cluster : hostToClusterMap.keySet()) {
    List<HostVO> hostsInCluster=hostToClusterMap.get(cluster);
    hostsLeft=hostsLeft - hostsInCluster.size();
    if (hostsToReturn.size() < hostsToGive) {
      s_logger.debug("String_Node_Str" + cluster);
      if (hostsInCluster.size() > hostsLeftToGive) {
        if (hostsLeft >= hostsLeftToGive) {
          s_logger.debug("String_Node_Str" + cluster + "String_Node_Str"+ hostsInCluster.size()+ "String_Node_Str"+ hostsLeftToGive);
          continue;
        }
 else {
          s_logger.debug("String_Node_Str" + hostsLeftToGive + "String_Node_Str"+ cluster);
          for (int i=0; i < hostsLeftToGive; i++) {
            hostsToReturn.add(hostsInCluster.get(i));
          }
          break;
        }
      }
 else {
        s_logger.debug("String_Node_Str" + hostsInCluster.size() + "String_Node_Str"+ hostsInCluster+ "String_Node_Str"+ cluster);
        hostsToReturn.addAll(hostsInCluster);
        hostsLeftToGive=hostsLeftToGive - hostsInCluster.size();
      }
    }
 else {
      break;
    }
  }
  return hostsToReturn;
}

Fixed version (ground truth):
@Override public List<HostVO> getHostsToRebalance(long msId,int avLoad){
  List<HostVO> allHosts=_hostDao.listRoutingHostsByManagementServer(msId);
  if (allHosts.size() <= avLoad) {
    s_logger.debug("String_Node_Str" + allHosts.size() + "String_Node_Str"+ msId+ "String_Node_Str"+ avLoad+ "String_Node_Str");
    return null;
  }
  List<HostVO> directHosts=_hostDao.listDirectHostsBy(msId,Status.Up);
  if (directHosts.isEmpty()) {
    s_logger.debug("String_Node_Str" + Status.Up + "String_Node_Str"+ msId+ "String_Node_Str");
    return null;
  }
  Map<Long,List<HostVO>> hostToClusterMap=new HashMap<Long,List<HostVO>>();
  for (  HostVO directHost : directHosts) {
    Long clusterId=directHost.getClusterId();
    List<HostVO> directHostsPerCluster=null;
    if (!hostToClusterMap.containsKey(clusterId)) {
      directHostsPerCluster=new ArrayList<HostVO>();
    }
 else {
      directHostsPerCluster=hostToClusterMap.get(clusterId);
    }
    directHostsPerCluster.add(directHost);
    hostToClusterMap.put(clusterId,directHostsPerCluster);
  }
  hostToClusterMap=sortByClusterSize(hostToClusterMap);
  int hostsToGive=allHosts.size() - avLoad;
  int hostsLeftToGive=hostsToGive;
  int hostsLeft=directHosts.size();
  List<HostVO> hostsToReturn=new ArrayList<HostVO>();
  s_logger.debug("String_Node_Str" + msId + "String_Node_Str"+ hostsToGive+ "String_Node_Str"+ allHosts.size()+ "String_Node_Str"+ avLoad+ "String_Node_Str");
  for (  Long cluster : hostToClusterMap.keySet()) {
    List<HostVO> hostsInCluster=hostToClusterMap.get(cluster);
    hostsLeft=hostsLeft - hostsInCluster.size();
    if (hostsToReturn.size() < hostsToGive) {
      s_logger.debug("String_Node_Str" + cluster);
      if (hostsInCluster.size() > hostsLeftToGive) {
        if (hostsLeft >= hostsLeftToGive) {
          s_logger.debug("String_Node_Str" + cluster + "String_Node_Str"+ hostsInCluster.size()+ "String_Node_Str"+ hostsLeftToGive);
          continue;
        }
 else {
          s_logger.debug("String_Node_Str" + hostsLeftToGive + "String_Node_Str"+ cluster);
          for (int i=0; i < hostsLeftToGive; i++) {
            s_logger.trace("String_Node_Str" + hostsInCluster.get(i) + "String_Node_Str"+ cluster);
            hostsToReturn.add(hostsInCluster.get(i));
          }
          break;
        }
      }
 else {
        s_logger.debug("String_Node_Str" + hostsInCluster.size() + "String_Node_Str"+ hostsInCluster+ "String_Node_Str"+ cluster);
        hostsToReturn.addAll(hostsInCluster);
        hostsLeftToGive=hostsLeftToGive - hostsInCluster.size();
      }
    }
 else {
      break;
    }
  }
  s_logger.debug("String_Node_Str" + msId + "String_Node_Str"+ hostsToReturn.size()+ "String_Node_Str");
  return hostsToReturn;
}