Program: 105476
Project: 105476
Buggy source:
protected void migrate(VMInstanceVO vm,long srcHostId,DeployDestination dest) throws ResourceUnavailableException, ConcurrentOperationException {
  s_logger.info("String_Node_Str" + vm + "String_Node_Str"+ dest);
  long dstHostId=dest.getHost().getId();
  Host fromHost=_hostDao.findById(srcHostId);
  if (fromHost == null) {
    s_logger.info("String_Node_Str" + srcHostId);
    throw new CloudRuntimeException("String_Node_Str" + srcHostId);
  }
  if (fromHost.getClusterId().longValue() != dest.getCluster().getId()) {
    List<VolumeVO> volumes=_volsDao.findCreatedByInstance(vm.getId());
    for (    VolumeVO volume : volumes) {
      if (!(_storagePoolDao.findById(volume.getPoolId())).getScope().equals(ScopeType.ZONE)) {
        s_logger.info("String_Node_Str" + dest.getHost().getId());
        throw new CloudRuntimeException("String_Node_Str" + dest.getHost().getId());
      }
    }
  }
  VirtualMachineGuru vmGuru=getVmGuru(vm);
  if (vm.getState() != State.Running) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vm);
    }
    throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ vm.getState().toString());
  }
  AlertManager.AlertType alertType=AlertManager.AlertType.ALERT_TYPE_USERVM_MIGRATE;
  if (VirtualMachine.Type.DomainRouter.equals(vm.getType())) {
    alertType=AlertManager.AlertType.ALERT_TYPE_DOMAIN_ROUTER_MIGRATE;
  }
 else   if (VirtualMachine.Type.ConsoleProxy.equals(vm.getType())) {
    alertType=AlertManager.AlertType.ALERT_TYPE_CONSOLE_PROXY_MIGRATE;
  }
  VirtualMachineProfile vmSrc=new VirtualMachineProfileImpl(vm);
  for (  NicProfile nic : _networkMgr.getNicProfiles(vm)) {
    vmSrc.addNic(nic);
  }
  VirtualMachineProfile profile=new VirtualMachineProfileImpl(vm,null,_offeringDao.findById(vm.getId(),vm.getServiceOfferingId()),null,null);
  _networkMgr.prepareNicForMigration(profile,dest);
  volumeMgr.prepareForMigration(profile,dest);
  VirtualMachineTO to=toVmTO(profile);
  PrepareForMigrationCommand pfmc=new PrepareForMigrationCommand(to);
  ItWorkVO work=new ItWorkVO(UUID.randomUUID().toString(),_nodeId,State.Migrating,vm.getType(),vm.getId());
  work.setStep(Step.Prepare);
  work.setResourceType(ItWorkVO.ResourceType.Host);
  work.setResourceId(dstHostId);
  work=_workDao.persist(work);
  Answer pfma=null;
  try {
    pfma=_agentMgr.send(dstHostId,pfmc);
    if (pfma == null || !pfma.getResult()) {
      String msg="String_Node_Str" + pfma.getDetails();
      pfma=null;
      throw new AgentUnavailableException(msg,dstHostId);
    }
  }
 catch (  OperationTimedoutException e1) {
    throw new AgentUnavailableException("String_Node_Str",dstHostId);
  }
 finally {
    if (pfma == null) {
      _networkMgr.rollbackNicForMigration(vmSrc,profile);
      work.setStep(Step.Done);
      _workDao.update(work.getId(),work);
    }
  }
  vm.setLastHostId(srcHostId);
  try {
    if (vm == null || vm.getHostId() == null || vm.getHostId() != srcHostId || !changeState(vm,Event.MigrationRequested,dstHostId,work,Step.Migrating)) {
      _networkMgr.rollbackNicForMigration(vmSrc,profile);
      s_logger.info("String_Node_Str" + vm);
      throw new ConcurrentOperationException("String_Node_Str" + vm);
    }
  }
 catch (  NoTransitionException e1) {
    _networkMgr.rollbackNicForMigration(vmSrc,profile);
    s_logger.info("String_Node_Str" + e1.getMessage());
    throw new ConcurrentOperationException("String_Node_Str" + e1.getMessage());
  }
  boolean migrated=false;
  try {
    boolean isWindows=_guestOsCategoryDao.findById(_guestOsDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("String_Node_Str");
    MigrateCommand mc=new MigrateCommand(vm.getInstanceName(),dest.getHost().getPrivateIpAddress(),isWindows,to,getExecuteInSequence(vm.getHypervisorType()));
    mc.setHostGuid(dest.getHost().getGuid());
    try {
      Answer ma=_agentMgr.send(vm.getLastHostId(),mc);
      if (ma == null || !ma.getResult()) {
        throw new CloudRuntimeException("String_Node_Str" + ma.getDetails());
      }
    }
 catch (    OperationTimedoutException e) {
      if (e.isActive()) {
        s_logger.warn("String_Node_Str" + vm);
        _haMgr.scheduleRestart(vm,true);
      }
      throw new AgentUnavailableException("String_Node_Str" + vm,dstHostId);
    }
    try {
      if (!changeState(vm,VirtualMachine.Event.OperationSucceeded,dstHostId,work,Step.Started)) {
        throw new ConcurrentOperationException("String_Node_Str" + vm);
      }
    }
 catch (    NoTransitionException e1) {
      throw new ConcurrentOperationException("String_Node_Str" + e1.getMessage());
    }
    try {
      if (!checkVmOnHost(vm,dstHostId)) {
        s_logger.error("String_Node_Str" + vm);
        try {
          _agentMgr.send(srcHostId,new Commands(cleanup(vm)),null);
        }
 catch (        AgentUnavailableException e) {
          s_logger.error("String_Node_Str" + srcHostId);
        }
        cleanup(vmGuru,new VirtualMachineProfileImpl(vm),work,Event.AgentReportStopped,true);
        throw new CloudRuntimeException("String_Node_Str" + vm);
      }
    }
 catch (    OperationTimedoutException e) {
      s_logger.debug("String_Node_Str" + vm + "String_Node_Str"+ dstHostId,e);
    }
    migrated=true;
  }
  finally {
    if (!migrated) {
      s_logger.info("String_Node_Str" + vm);
      _networkMgr.rollbackNicForMigration(vmSrc,profile);
      _alertMgr.sendAlert(alertType,fromHost.getDataCenterId(),fromHost.getPodId(),"String_Node_Str" + vm.getInstanceName() + "String_Node_Str"+ fromHost.getName()+ "String_Node_Str"+ dest.getDataCenter().getName()+ "String_Node_Str"+ dest.getPod().getName(),"String_Node_Str");
      try {
        _agentMgr.send(dstHostId,new Commands(cleanup(vm)),null);
      }
 catch (      AgentUnavailableException ae) {
        s_logger.info("String_Node_Str");
      }
      try {
        stateTransitTo(vm,Event.OperationFailed,srcHostId);
      }
 catch (      NoTransitionException e) {
        s_logger.warn(e.getMessage());
      }
    }
 else {
      _networkMgr.commitNicForMigration(vmSrc,profile);
    }
    work.setStep(Step.Done);
    _workDao.update(work.getId(),work);
  }
}

Fixed version (ground truth):
protected void migrate(VMInstanceVO vm,long srcHostId,DeployDestination dest) throws ResourceUnavailableException, ConcurrentOperationException {
  s_logger.info("String_Node_Str" + vm + "String_Node_Str"+ dest);
  long dstHostId=dest.getHost().getId();
  Host fromHost=_hostDao.findById(srcHostId);
  if (fromHost == null) {
    s_logger.info("String_Node_Str" + srcHostId);
    throw new CloudRuntimeException("String_Node_Str" + srcHostId);
  }
  if (fromHost.getClusterId().longValue() != dest.getCluster().getId()) {
    List<VolumeVO> volumes=_volsDao.findCreatedByInstance(vm.getId());
    for (    VolumeVO volume : volumes) {
      if (!(_storagePoolDao.findById(volume.getPoolId())).getScope().equals(ScopeType.ZONE)) {
        s_logger.info("String_Node_Str" + dest.getHost().getId());
        throw new CloudRuntimeException("String_Node_Str" + dest.getHost().getId());
      }
    }
  }
  VirtualMachineGuru vmGuru=getVmGuru(vm);
  if (vm.getState() != State.Running) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + vm);
    }
    throw new CloudRuntimeException("String_Node_Str" + vm + "String_Node_Str"+ vm.getState().toString());
  }
  AlertManager.AlertType alertType=AlertManager.AlertType.ALERT_TYPE_USERVM_MIGRATE;
  if (VirtualMachine.Type.DomainRouter.equals(vm.getType())) {
    alertType=AlertManager.AlertType.ALERT_TYPE_DOMAIN_ROUTER_MIGRATE;
  }
 else   if (VirtualMachine.Type.ConsoleProxy.equals(vm.getType())) {
    alertType=AlertManager.AlertType.ALERT_TYPE_CONSOLE_PROXY_MIGRATE;
  }
  VirtualMachineProfile vmSrc=new VirtualMachineProfileImpl(vm);
  for (  NicProfile nic : _networkMgr.getNicProfiles(vm)) {
    vmSrc.addNic(nic);
  }
  VirtualMachineProfile profile=new VirtualMachineProfileImpl(vm,null,_offeringDao.findById(vm.getId(),vm.getServiceOfferingId()),null,null);
  _networkMgr.prepareNicForMigration(profile,dest);
  volumeMgr.prepareForMigration(profile,dest);
  VirtualMachineTO to=toVmTO(profile);
  PrepareForMigrationCommand pfmc=new PrepareForMigrationCommand(to);
  ItWorkVO work=new ItWorkVO(UUID.randomUUID().toString(),_nodeId,State.Migrating,vm.getType(),vm.getId());
  work.setStep(Step.Prepare);
  work.setResourceType(ItWorkVO.ResourceType.Host);
  work.setResourceId(dstHostId);
  work=_workDao.persist(work);
  Answer pfma=null;
  try {
    pfma=_agentMgr.send(dstHostId,pfmc);
    if (pfma == null || !pfma.getResult()) {
      String details=(pfma != null) ? pfma.getDetails() : "String_Node_Str";
      String msg="String_Node_Str" + details;
      pfma=null;
      throw new AgentUnavailableException(msg,dstHostId);
    }
  }
 catch (  OperationTimedoutException e1) {
    throw new AgentUnavailableException("String_Node_Str",dstHostId);
  }
 finally {
    if (pfma == null) {
      _networkMgr.rollbackNicForMigration(vmSrc,profile);
      work.setStep(Step.Done);
      _workDao.update(work.getId(),work);
    }
  }
  vm.setLastHostId(srcHostId);
  try {
    if (vm == null || vm.getHostId() == null || vm.getHostId() != srcHostId || !changeState(vm,Event.MigrationRequested,dstHostId,work,Step.Migrating)) {
      _networkMgr.rollbackNicForMigration(vmSrc,profile);
      s_logger.info("String_Node_Str" + vm);
      throw new ConcurrentOperationException("String_Node_Str" + vm);
    }
  }
 catch (  NoTransitionException e1) {
    _networkMgr.rollbackNicForMigration(vmSrc,profile);
    s_logger.info("String_Node_Str" + e1.getMessage());
    throw new ConcurrentOperationException("String_Node_Str" + e1.getMessage());
  }
  boolean migrated=false;
  try {
    boolean isWindows=_guestOsCategoryDao.findById(_guestOsDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("String_Node_Str");
    MigrateCommand mc=new MigrateCommand(vm.getInstanceName(),dest.getHost().getPrivateIpAddress(),isWindows,to,getExecuteInSequence(vm.getHypervisorType()));
    mc.setHostGuid(dest.getHost().getGuid());
    try {
      Answer ma=_agentMgr.send(vm.getLastHostId(),mc);
      if (ma == null || !ma.getResult()) {
        String details=(ma != null) ? ma.getDetails() : "String_Node_Str";
        throw new CloudRuntimeException("String_Node_Str" + details);
      }
    }
 catch (    OperationTimedoutException e) {
      if (e.isActive()) {
        s_logger.warn("String_Node_Str" + vm);
        _haMgr.scheduleRestart(vm,true);
      }
      throw new AgentUnavailableException("String_Node_Str" + vm,dstHostId);
    }
    try {
      if (!changeState(vm,VirtualMachine.Event.OperationSucceeded,dstHostId,work,Step.Started)) {
        throw new ConcurrentOperationException("String_Node_Str" + vm);
      }
    }
 catch (    NoTransitionException e1) {
      throw new ConcurrentOperationException("String_Node_Str" + e1.getMessage());
    }
    try {
      if (!checkVmOnHost(vm,dstHostId)) {
        s_logger.error("String_Node_Str" + vm);
        try {
          _agentMgr.send(srcHostId,new Commands(cleanup(vm)),null);
        }
 catch (        AgentUnavailableException e) {
          s_logger.error("String_Node_Str" + srcHostId);
        }
        cleanup(vmGuru,new VirtualMachineProfileImpl(vm),work,Event.AgentReportStopped,true);
        throw new CloudRuntimeException("String_Node_Str" + vm);
      }
    }
 catch (    OperationTimedoutException e) {
      s_logger.debug("String_Node_Str" + vm + "String_Node_Str"+ dstHostId,e);
    }
    migrated=true;
  }
  finally {
    if (!migrated) {
      s_logger.info("String_Node_Str" + vm);
      _networkMgr.rollbackNicForMigration(vmSrc,profile);
      _alertMgr.sendAlert(alertType,fromHost.getDataCenterId(),fromHost.getPodId(),"String_Node_Str" + vm.getInstanceName() + "String_Node_Str"+ fromHost.getName()+ "String_Node_Str"+ dest.getDataCenter().getName()+ "String_Node_Str"+ dest.getPod().getName(),"String_Node_Str");
      try {
        _agentMgr.send(dstHostId,new Commands(cleanup(vm)),null);
      }
 catch (      AgentUnavailableException ae) {
        s_logger.info("String_Node_Str");
      }
      try {
        stateTransitTo(vm,Event.OperationFailed,srcHostId);
      }
 catch (      NoTransitionException e) {
        s_logger.warn(e.getMessage());
      }
    }
 else {
      _networkMgr.commitNicForMigration(vmSrc,profile);
    }
    work.setStep(Step.Done);
    _workDao.update(work.getId(),work);
  }
}