Program: 106865
Project: 106865
Buggy source:
private void peerScan(Connection conn){
  Date cutTime=DateUtil.currentGMTTime();
  List<ManagementServerHostVO> currentList=_mshostDao.getActiveList(conn,new Date(cutTime.getTime() - heartbeatThreshold));
  List<ManagementServerHostVO> removedNodeList=new ArrayList<ManagementServerHostVO>();
  List<ManagementServerHostVO> invalidatedNodeList=new ArrayList<ManagementServerHostVO>();
  if (_mshostId != null) {
    for (    Map.Entry<Long,ManagementServerHostVO> entry : activePeers.entrySet()) {
      ManagementServerHostVO current=getInListById(entry.getKey(),currentList);
      if (current == null) {
        if (entry.getKey().longValue() != _mshostId.longValue()) {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug("String_Node_Str" + entry.getKey() + "String_Node_Str"+ entry.getValue().getServiceIP());
          }
          removedNodeList.add(entry.getValue());
        }
      }
 else {
        if (current.getRunid() == 0) {
          if (entry.getKey().longValue() != _mshostId.longValue()) {
            if (s_logger.isDebugEnabled()) {
              s_logger.debug("String_Node_Str" + entry.getKey() + "String_Node_Str"+ entry.getValue().getServiceIP());
            }
            invalidatedNodeList.add(entry.getValue());
          }
        }
 else {
          if (entry.getValue().getRunid() != current.getRunid()) {
            if (s_logger.isDebugEnabled()) {
              s_logger.debug("String_Node_Str" + entry.getKey() + "String_Node_Str"+ entry.getValue().getServiceIP());
            }
            entry.getValue().setRunid(current.getRunid());
          }
        }
      }
    }
  }
  if (invalidatedNodeList.size() > 0) {
    for (    ManagementServerHostVO mshost : invalidatedNodeList) {
      activePeers.remove(mshost.getId());
      try {
        JmxUtil.unregisterMBean("String_Node_Str","String_Node_Str" + mshost.getId());
      }
 catch (      Exception e) {
        s_logger.warn("String_Node_Str" + e.toString());
      }
    }
    this.queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeRemoved,invalidatedNodeList));
  }
  Iterator<ManagementServerHostVO> it=removedNodeList.iterator();
  while (it.hasNext()) {
    ManagementServerHostVO mshost=it.next();
    if (!pingManagementNode(mshost)) {
      s_logger.warn("String_Node_Str" + mshost.getId() + "String_Node_Str");
      activePeers.remove(mshost.getId());
      _mshostDao.invalidateRunSession(conn,mshost.getId(),mshost.getRunid());
      try {
        JmxUtil.unregisterMBean("String_Node_Str","String_Node_Str" + mshost.getId());
      }
 catch (      Exception e) {
        s_logger.warn("String_Node_Str" + e.toString());
      }
    }
 else {
      s_logger.info("String_Node_Str" + mshost.getId() + "String_Node_Str");
      it.remove();
    }
  }
  if (removedNodeList.size() > 0) {
    this.queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeRemoved,removedNodeList));
  }
  List<ManagementServerHostVO> newNodeList=new ArrayList<ManagementServerHostVO>();
  for (  ManagementServerHostVO mshost : currentList) {
    if (!activePeers.containsKey(mshost.getId())) {
      activePeers.put(mshost.getId(),mshost);
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + mshost.getId() + "String_Node_Str"+ mshost.getServiceIP());
      }
      newNodeList.add(mshost);
      try {
        JmxUtil.registerMBean("String_Node_Str","String_Node_Str" + mshost.getId(),new ClusterManagerMBeanImpl(this,mshost));
      }
 catch (      Exception e) {
        s_logger.warn("String_Node_Str" + ExceptionUtil.toString(e));
      }
    }
  }
  if (newNodeList.size() > 0) {
    this.queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeAdded,newNodeList));
  }
}

Fixed version (ground truth):
private void peerScan(){
  Date cutTime=DateUtil.currentGMTTime();
  List<ManagementServerHostVO> currentList=_mshostDao.getActiveList(new Date(cutTime.getTime() - heartbeatThreshold));
  List<ManagementServerHostVO> removedNodeList=new ArrayList<ManagementServerHostVO>();
  List<ManagementServerHostVO> invalidatedNodeList=new ArrayList<ManagementServerHostVO>();
  if (_mshostId != null) {
    for (    Map.Entry<Long,ManagementServerHostVO> entry : activePeers.entrySet()) {
      ManagementServerHostVO current=getInListById(entry.getKey(),currentList);
      if (current == null) {
        if (entry.getKey().longValue() != _mshostId.longValue()) {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug("String_Node_Str" + entry.getKey() + "String_Node_Str"+ entry.getValue().getServiceIP());
          }
          removedNodeList.add(entry.getValue());
        }
      }
 else {
        if (current.getRunid() == 0) {
          if (entry.getKey().longValue() != _mshostId.longValue()) {
            if (s_logger.isDebugEnabled()) {
              s_logger.debug("String_Node_Str" + entry.getKey() + "String_Node_Str"+ entry.getValue().getServiceIP());
            }
            invalidatedNodeList.add(entry.getValue());
          }
        }
 else {
          if (entry.getValue().getRunid() != current.getRunid()) {
            if (s_logger.isDebugEnabled()) {
              s_logger.debug("String_Node_Str" + entry.getKey() + "String_Node_Str"+ entry.getValue().getServiceIP());
            }
            entry.getValue().setRunid(current.getRunid());
          }
        }
      }
    }
  }
  if (invalidatedNodeList.size() > 0) {
    for (    ManagementServerHostVO mshost : invalidatedNodeList) {
      activePeers.remove(mshost.getId());
      try {
        JmxUtil.unregisterMBean("String_Node_Str","String_Node_Str" + mshost.getId());
      }
 catch (      Exception e) {
        s_logger.warn("String_Node_Str" + e.toString());
      }
    }
    this.queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeRemoved,invalidatedNodeList));
  }
  Iterator<ManagementServerHostVO> it=removedNodeList.iterator();
  while (it.hasNext()) {
    ManagementServerHostVO mshost=it.next();
    if (!pingManagementNode(mshost)) {
      s_logger.warn("String_Node_Str" + mshost.getId() + "String_Node_Str");
      activePeers.remove(mshost.getId());
      _mshostDao.invalidateRunSession(mshost.getId(),mshost.getRunid());
      try {
        JmxUtil.unregisterMBean("String_Node_Str","String_Node_Str" + mshost.getId());
      }
 catch (      Exception e) {
        s_logger.warn("String_Node_Str" + e.toString());
      }
    }
 else {
      s_logger.info("String_Node_Str" + mshost.getId() + "String_Node_Str");
      it.remove();
    }
  }
  if (removedNodeList.size() > 0) {
    this.queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeRemoved,removedNodeList));
  }
  List<ManagementServerHostVO> newNodeList=new ArrayList<ManagementServerHostVO>();
  for (  ManagementServerHostVO mshost : currentList) {
    if (!activePeers.containsKey(mshost.getId())) {
      activePeers.put(mshost.getId(),mshost);
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + mshost.getId() + "String_Node_Str"+ mshost.getServiceIP());
      }
      newNodeList.add(mshost);
      try {
        JmxUtil.registerMBean("String_Node_Str","String_Node_Str" + mshost.getId(),new ClusterManagerMBeanImpl(this,mshost));
      }
 catch (      Exception e) {
        s_logger.warn("String_Node_Str" + ExceptionUtil.toString(e));
      }
    }
  }
  if (newNodeList.size() > 0) {
    this.queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeAdded,newNodeList));
  }
}