Program: 105640
Project: 105640
Buggy source:
@DB protected boolean doDeleteHost(final long hostId,boolean isForced,final boolean isForceDeleteStorage){
  User caller=_accountMgr.getActiveUser(CallContext.current().getCallingUserId());
  final HostVO host=_hostDao.findById(hostId);
  if (host == null) {
    throw new InvalidParameterValueException("String_Node_Str" + hostId + "String_Node_Str");
  }
  _accountMgr.checkAccessAndSpecifyAuthority(CallContext.current().getCallingAccount(),host.getDataCenterId());
  if (!isForced && host.getResourceState() != ResourceState.Maintenance) {
    throw new CloudRuntimeException("String_Node_Str" + host.getUuid() + "String_Node_Str");
  }
  final List<StoragePoolHostVO> pools=_storagePoolHostDao.listByHostIdIncludingRemoved(hostId);
  ResourceStateAdapter.DeleteHostAnswer answer=(ResourceStateAdapter.DeleteHostAnswer)dispatchToStateAdapters(ResourceStateAdapter.Event.DELETE_HOST,false,host,isForced,isForceDeleteStorage);
  if (answer == null) {
    throw new CloudRuntimeException("String_Node_Str" + host.getName() + "String_Node_Str"+ hostId+ "String_Node_Str"+ host.getHypervisorType()+ "String_Node_Str"+ host.getType());
  }
  if (answer.getIsException()) {
    return false;
  }
  if (!answer.getIsContinue()) {
    return true;
  }
  Transaction.execute(new TransactionCallbackNoReturn(){
    @Override public void doInTransactionWithoutResult(    TransactionStatus status){
      _dcDao.releasePrivateIpAddress(host.getPrivateIpAddress(),host.getDataCenterId(),null);
      _agentMgr.disconnectWithoutInvestigation(hostId,Status.Event.Remove);
      _hostDetailsDao.deleteDetails(hostId);
      _hostGpuGroupsDao.deleteGpuEntries(hostId);
      host.setGuid(null);
      Long clusterId=host.getClusterId();
      host.setClusterId(null);
      _hostDao.update(host.getId(),host);
      _hostDao.remove(hostId);
      if (clusterId != null) {
        List<HostVO> hosts=listAllHostsInCluster(clusterId);
        if (hosts.size() == 0) {
          ClusterVO cluster=_clusterDao.findById(clusterId);
          cluster.setGuid(null);
          _clusterDao.update(clusterId,cluster);
        }
      }
      try {
        resourceStateTransitTo(host,ResourceState.Event.DeleteHost,_nodeId);
      }
 catch (      NoTransitionException e) {
        s_logger.debug("String_Node_Str" + host.getId() + "String_Node_Str",e);
      }
      _storagePoolHostDao.deletePrimaryRecordsForHost(hostId);
      List<VMInstanceVO> vms=_vmDao.listByHostId(hostId);
      for (      VMInstanceVO vm : vms) {
        vm.setState(State.Stopped);
        vm.setHostId(null);
        _vmDao.persist(vm);
      }
      for (      StoragePoolHostVO pool : pools) {
        Long poolId=pool.getPoolId();
        StoragePoolVO storagePool=_storagePoolDao.findById(poolId);
        if (storagePool.isLocal() && isForceDeleteStorage) {
          storagePool.setUuid(null);
          storagePool.setClusterId(null);
          _storagePoolDao.update(poolId,storagePool);
          _storagePoolDao.remove(poolId);
          s_logger.debug("String_Node_Str" + poolId + "String_Node_Str"+ hostId);
        }
      }
      Object[] capacityTypes={Capacity.CAPACITY_TYPE_CPU,Capacity.CAPACITY_TYPE_MEMORY};
      SearchCriteria<CapacityVO> hostCapacitySC=_capacityDao.createSearchCriteria();
      hostCapacitySC.addAnd("String_Node_Str",SearchCriteria.Op.EQ,hostId);
      hostCapacitySC.addAnd("String_Node_Str",SearchCriteria.Op.IN,capacityTypes);
      _capacityDao.remove(hostCapacitySC);
      DedicatedResourceVO dr=_dedicatedDao.findByHostId(hostId);
      if (dr != null) {
        _dedicatedDao.remove(dr.getId());
      }
    }
  }
);
  return true;
}

Fixed version (ground truth):
@DB protected boolean doDeleteHost(final long hostId,boolean isForced,final boolean isForceDeleteStorage){
  _accountMgr.getActiveUser(CallContext.current().getCallingUserId());
  final HostVO host=_hostDao.findById(hostId);
  if (host == null) {
    throw new InvalidParameterValueException("String_Node_Str" + hostId + "String_Node_Str");
  }
  _accountMgr.checkAccessAndSpecifyAuthority(CallContext.current().getCallingAccount(),host.getDataCenterId());
  if (!isForced && host.getResourceState() != ResourceState.Maintenance) {
    throw new CloudRuntimeException("String_Node_Str" + host.getUuid() + "String_Node_Str");
  }
  final List<StoragePoolHostVO> pools=_storagePoolHostDao.listByHostIdIncludingRemoved(hostId);
  ResourceStateAdapter.DeleteHostAnswer answer=(ResourceStateAdapter.DeleteHostAnswer)dispatchToStateAdapters(ResourceStateAdapter.Event.DELETE_HOST,false,host,isForced,isForceDeleteStorage);
  if (answer == null) {
    throw new CloudRuntimeException("String_Node_Str" + host.getName() + "String_Node_Str"+ hostId+ "String_Node_Str"+ host.getHypervisorType()+ "String_Node_Str"+ host.getType());
  }
  if (answer.getIsException()) {
    return false;
  }
  if (!answer.getIsContinue()) {
    return true;
  }
  Transaction.execute(new TransactionCallbackNoReturn(){
    @Override public void doInTransactionWithoutResult(    TransactionStatus status){
      _dcDao.releasePrivateIpAddress(host.getPrivateIpAddress(),host.getDataCenterId(),null);
      _agentMgr.disconnectWithoutInvestigation(hostId,Status.Event.Remove);
      _hostDetailsDao.deleteDetails(hostId);
      _hostGpuGroupsDao.deleteGpuEntries(hostId);
      host.setGuid(null);
      Long clusterId=host.getClusterId();
      host.setClusterId(null);
      _hostDao.update(host.getId(),host);
      _hostDao.remove(hostId);
      if (clusterId != null) {
        List<HostVO> hosts=listAllHostsInCluster(clusterId);
        if (hosts.size() == 0) {
          ClusterVO cluster=_clusterDao.findById(clusterId);
          cluster.setGuid(null);
          _clusterDao.update(clusterId,cluster);
        }
      }
      try {
        resourceStateTransitTo(host,ResourceState.Event.DeleteHost,_nodeId);
      }
 catch (      NoTransitionException e) {
        s_logger.debug("String_Node_Str" + host.getId() + "String_Node_Str",e);
      }
      _storagePoolHostDao.deletePrimaryRecordsForHost(hostId);
      List<VMInstanceVO> vms=_vmDao.listByHostId(hostId);
      for (      VMInstanceVO vm : vms) {
        vm.setState(State.Stopped);
        vm.setHostId(null);
        _vmDao.persist(vm);
      }
      for (      StoragePoolHostVO pool : pools) {
        Long poolId=pool.getPoolId();
        StoragePoolVO storagePool=_storagePoolDao.findById(poolId);
        if (storagePool.isLocal() && isForceDeleteStorage) {
          storagePool.setUuid(null);
          storagePool.setClusterId(null);
          _storagePoolDao.update(poolId,storagePool);
          _storagePoolDao.remove(poolId);
          s_logger.debug("String_Node_Str" + poolId + "String_Node_Str"+ hostId);
        }
      }
      Object[] capacityTypes={Capacity.CAPACITY_TYPE_CPU,Capacity.CAPACITY_TYPE_MEMORY};
      SearchCriteria<CapacityVO> hostCapacitySC=_capacityDao.createSearchCriteria();
      hostCapacitySC.addAnd("String_Node_Str",SearchCriteria.Op.EQ,hostId);
      hostCapacitySC.addAnd("String_Node_Str",SearchCriteria.Op.IN,capacityTypes);
      _capacityDao.remove(hostCapacitySC);
      DedicatedResourceVO dr=_dedicatedDao.findByHostId(hostId);
      if (dr != null) {
        _dedicatedDao.remove(dr.getId());
      }
    }
  }
);
  return true;
}