Program: 106167
Project: 106167
Buggy source:
@Override public void collectVmDiskStatistics(UserVmVO userVm){
  if (!userVm.getHypervisorType().equals(HypervisorType.KVM))   return;
  long hostId=userVm.getHostId();
  List<String> vmNames=new ArrayList<String>();
  vmNames.add(userVm.getInstanceName());
  HostVO host=_hostDao.findById(hostId);
  GetVmDiskStatsAnswer diskStatsAnswer=null;
  try {
    diskStatsAnswer=(GetVmDiskStatsAnswer)_agentMgr.easySend(hostId,new GetVmDiskStatsCommand(vmNames,host.getGuid(),host.getName()));
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str" + userVm.getHostName() + "String_Node_Str"+ host.getName(),e);
    return;
  }
  if (diskStatsAnswer != null) {
    if (!diskStatsAnswer.getResult()) {
      s_logger.warn("String_Node_Str" + userVm.getHostName() + "String_Node_Str"+ host.getName()+ "String_Node_Str"+ diskStatsAnswer.getDetails());
      return;
    }
    Transaction txn=Transaction.open(Transaction.CLOUD_DB);
    try {
      txn.start();
      HashMap<String,List<VmDiskStatsEntry>> vmDiskStatsByName=diskStatsAnswer.getVmDiskStatsMap();
      List<VmDiskStatsEntry> vmDiskStats=vmDiskStatsByName.get(userVm.getInstanceName());
      if (vmDiskStats == null)       return;
      for (      VmDiskStatsEntry vmDiskStat : vmDiskStats) {
        SearchCriteria<VolumeVO> sc_volume=_volsDao.createSearchCriteria();
        sc_volume.addAnd("String_Node_Str",SearchCriteria.Op.EQ,vmDiskStat.getPath());
        VolumeVO volume=_volsDao.search(sc_volume,null).get(0);
        VmDiskStatisticsVO previousVmDiskStats=_vmDiskStatsDao.findBy(userVm.getAccountId(),userVm.getDataCenterId(),userVm.getId(),volume.getId());
        VmDiskStatisticsVO vmDiskStat_lock=_vmDiskStatsDao.lock(userVm.getAccountId(),userVm.getDataCenterId(),userVm.getId(),volume.getId());
        if ((vmDiskStat.getIORead() == 0) && (vmDiskStat.getIOWrite() == 0) && (vmDiskStat.getBytesRead() == 0)&& (vmDiskStat.getBytesWrite() == 0)) {
          s_logger.debug("String_Node_Str");
          continue;
        }
        if (vmDiskStat_lock == null) {
          s_logger.warn("String_Node_Str" + userVm.getAccountId() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ volume.getId());
          continue;
        }
        if (previousVmDiskStats != null && ((previousVmDiskStats.getCurrentIORead() != vmDiskStat_lock.getCurrentIORead()) || ((previousVmDiskStats.getCurrentIOWrite() != vmDiskStat_lock.getCurrentIOWrite()) || (previousVmDiskStats.getCurrentBytesRead() != vmDiskStat_lock.getCurrentBytesRead()) || (previousVmDiskStats.getCurrentBytesWrite() != vmDiskStat_lock.getCurrentBytesWrite())))) {
          s_logger.debug("String_Node_Str" + "String_Node_Str" + host.getName() + "String_Node_Str"+ vmDiskStat.getVmName()+ "String_Node_Str"+ vmDiskStat.getIORead()+ "String_Node_Str"+ vmDiskStat.getIOWrite()+ "String_Node_Str"+ vmDiskStat.getBytesRead()+ "String_Node_Str"+ vmDiskStat.getBytesWrite());
          continue;
        }
        if (vmDiskStat_lock.getCurrentIORead() > vmDiskStat.getIORead()) {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug("String_Node_Str" + "String_Node_Str" + host.getName() + "String_Node_Str"+ vmDiskStat.getVmName()+ "String_Node_Str"+ vmDiskStat.getIORead()+ "String_Node_Str"+ vmDiskStat_lock.getCurrentIORead());
          }
          vmDiskStat_lock.setNetIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead());
        }
        vmDiskStat_lock.setCurrentIORead(vmDiskStat.getIORead());
        if (vmDiskStat_lock.getCurrentIOWrite() > vmDiskStat.getIOWrite()) {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug("String_Node_Str" + "String_Node_Str" + host.getName() + "String_Node_Str"+ vmDiskStat.getVmName()+ "String_Node_Str"+ vmDiskStat.getIOWrite()+ "String_Node_Str"+ vmDiskStat_lock.getCurrentIOWrite());
          }
          vmDiskStat_lock.setNetIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite());
        }
        vmDiskStat_lock.setCurrentIOWrite(vmDiskStat.getIOWrite());
        if (vmDiskStat_lock.getCurrentBytesRead() > vmDiskStat.getBytesRead()) {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug("String_Node_Str" + "String_Node_Str" + host.getName() + "String_Node_Str"+ vmDiskStat.getVmName()+ "String_Node_Str"+ vmDiskStat.getBytesRead()+ "String_Node_Str"+ vmDiskStat_lock.getCurrentBytesRead());
          }
          vmDiskStat_lock.setNetBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead());
        }
        vmDiskStat_lock.setCurrentBytesRead(vmDiskStat.getBytesRead());
        if (vmDiskStat_lock.getCurrentBytesWrite() > vmDiskStat.getBytesWrite()) {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug("String_Node_Str" + "String_Node_Str" + host.getName() + "String_Node_Str"+ vmDiskStat.getVmName()+ "String_Node_Str"+ vmDiskStat.getBytesWrite()+ "String_Node_Str"+ vmDiskStat_lock.getCurrentBytesWrite());
          }
          vmDiskStat_lock.setNetBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite());
        }
        vmDiskStat_lock.setCurrentBytesWrite(vmDiskStat.getBytesWrite());
        if (!_dailyOrHourly) {
          vmDiskStat_lock.setAggIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead());
          vmDiskStat_lock.setAggIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite());
          vmDiskStat_lock.setAggBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead());
          vmDiskStat_lock.setAggBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite());
        }
        _vmDiskStatsDao.update(vmDiskStat_lock.getId(),vmDiskStat_lock);
      }
      txn.commit();
    }
 catch (    Exception e) {
      txn.rollback();
      s_logger.warn("String_Node_Str" + userVm.getId() + "String_Node_Str"+ hostId,e);
    }
 finally {
      txn.close();
    }
  }
}

Fixed version (ground truth):
@Override public void collectVmDiskStatistics(UserVmVO userVm){
  if (!userVm.getHypervisorType().equals(HypervisorType.KVM))   return;
  s_logger.debug("String_Node_Str");
  long hostId=userVm.getHostId();
  List<String> vmNames=new ArrayList<String>();
  vmNames.add(userVm.getInstanceName());
  HostVO host=_hostDao.findById(hostId);
  GetVmDiskStatsAnswer diskStatsAnswer=null;
  try {
    diskStatsAnswer=(GetVmDiskStatsAnswer)_agentMgr.easySend(hostId,new GetVmDiskStatsCommand(vmNames,host.getGuid(),host.getName()));
  }
 catch (  Exception e) {
    s_logger.warn("String_Node_Str" + userVm.getHostName() + "String_Node_Str"+ host.getName(),e);
    return;
  }
  if (diskStatsAnswer != null) {
    if (!diskStatsAnswer.getResult()) {
      s_logger.warn("String_Node_Str" + userVm.getHostName() + "String_Node_Str"+ host.getName()+ "String_Node_Str"+ diskStatsAnswer.getDetails());
      return;
    }
    Transaction txn=Transaction.open(Transaction.CLOUD_DB);
    try {
      txn.start();
      HashMap<String,List<VmDiskStatsEntry>> vmDiskStatsByName=diskStatsAnswer.getVmDiskStatsMap();
      if (vmDiskStatsByName == null)       return;
      List<VmDiskStatsEntry> vmDiskStats=vmDiskStatsByName.get(userVm.getInstanceName());
      if (vmDiskStats == null)       return;
      for (      VmDiskStatsEntry vmDiskStat : vmDiskStats) {
        SearchCriteria<VolumeVO> sc_volume=_volsDao.createSearchCriteria();
        sc_volume.addAnd("String_Node_Str",SearchCriteria.Op.EQ,vmDiskStat.getPath());
        VolumeVO volume=_volsDao.search(sc_volume,null).get(0);
        VmDiskStatisticsVO previousVmDiskStats=_vmDiskStatsDao.findBy(userVm.getAccountId(),userVm.getDataCenterId(),userVm.getId(),volume.getId());
        VmDiskStatisticsVO vmDiskStat_lock=_vmDiskStatsDao.lock(userVm.getAccountId(),userVm.getDataCenterId(),userVm.getId(),volume.getId());
        if ((vmDiskStat.getIORead() == 0) && (vmDiskStat.getIOWrite() == 0) && (vmDiskStat.getBytesRead() == 0)&& (vmDiskStat.getBytesWrite() == 0)) {
          s_logger.debug("String_Node_Str");
          continue;
        }
        if (vmDiskStat_lock == null) {
          s_logger.warn("String_Node_Str" + userVm.getAccountId() + "String_Node_Str"+ userVm.getId()+ "String_Node_Str"+ volume.getId());
          continue;
        }
        if (previousVmDiskStats != null && ((previousVmDiskStats.getCurrentIORead() != vmDiskStat_lock.getCurrentIORead()) || ((previousVmDiskStats.getCurrentIOWrite() != vmDiskStat_lock.getCurrentIOWrite()) || (previousVmDiskStats.getCurrentBytesRead() != vmDiskStat_lock.getCurrentBytesRead()) || (previousVmDiskStats.getCurrentBytesWrite() != vmDiskStat_lock.getCurrentBytesWrite())))) {
          s_logger.debug("String_Node_Str" + "String_Node_Str" + host.getName() + "String_Node_Str"+ vmDiskStat.getVmName()+ "String_Node_Str"+ vmDiskStat.getIORead()+ "String_Node_Str"+ vmDiskStat.getIOWrite()+ "String_Node_Str"+ vmDiskStat.getBytesRead()+ "String_Node_Str"+ vmDiskStat.getBytesWrite());
          continue;
        }
        if (vmDiskStat_lock.getCurrentIORead() > vmDiskStat.getIORead()) {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug("String_Node_Str" + "String_Node_Str" + host.getName() + "String_Node_Str"+ vmDiskStat.getVmName()+ "String_Node_Str"+ vmDiskStat.getIORead()+ "String_Node_Str"+ vmDiskStat_lock.getCurrentIORead());
          }
          vmDiskStat_lock.setNetIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead());
        }
        vmDiskStat_lock.setCurrentIORead(vmDiskStat.getIORead());
        if (vmDiskStat_lock.getCurrentIOWrite() > vmDiskStat.getIOWrite()) {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug("String_Node_Str" + "String_Node_Str" + host.getName() + "String_Node_Str"+ vmDiskStat.getVmName()+ "String_Node_Str"+ vmDiskStat.getIOWrite()+ "String_Node_Str"+ vmDiskStat_lock.getCurrentIOWrite());
          }
          vmDiskStat_lock.setNetIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite());
        }
        vmDiskStat_lock.setCurrentIOWrite(vmDiskStat.getIOWrite());
        if (vmDiskStat_lock.getCurrentBytesRead() > vmDiskStat.getBytesRead()) {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug("String_Node_Str" + "String_Node_Str" + host.getName() + "String_Node_Str"+ vmDiskStat.getVmName()+ "String_Node_Str"+ vmDiskStat.getBytesRead()+ "String_Node_Str"+ vmDiskStat_lock.getCurrentBytesRead());
          }
          vmDiskStat_lock.setNetBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead());
        }
        vmDiskStat_lock.setCurrentBytesRead(vmDiskStat.getBytesRead());
        if (vmDiskStat_lock.getCurrentBytesWrite() > vmDiskStat.getBytesWrite()) {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug("String_Node_Str" + "String_Node_Str" + host.getName() + "String_Node_Str"+ vmDiskStat.getVmName()+ "String_Node_Str"+ vmDiskStat.getBytesWrite()+ "String_Node_Str"+ vmDiskStat_lock.getCurrentBytesWrite());
          }
          vmDiskStat_lock.setNetBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite());
        }
        vmDiskStat_lock.setCurrentBytesWrite(vmDiskStat.getBytesWrite());
        if (!_dailyOrHourly) {
          vmDiskStat_lock.setAggIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead());
          vmDiskStat_lock.setAggIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite());
          vmDiskStat_lock.setAggBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead());
          vmDiskStat_lock.setAggBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite());
        }
        _vmDiskStatsDao.update(vmDiskStat_lock.getId(),vmDiskStat_lock);
      }
      txn.commit();
    }
 catch (    Exception e) {
      txn.rollback();
      s_logger.warn("String_Node_Str" + userVm.getId() + "String_Node_Str"+ hostId,e);
    }
 finally {
      txn.close();
    }
  }
}