Program: 106946
Project: 106946
Buggy source:
@Override public <T extends VMInstanceVO>T advanceStart(T vm,Map<VirtualMachineProfile.Param,Object> params,User caller,Account account,DeploymentPlan planToDeploy) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
  long vmId=vm.getId();
  VirtualMachineGuru<T> vmGuru;
  if (vm.getHypervisorType() == HypervisorType.BareMetal) {
    vmGuru=getBareMetalVmGuru(vm);
  }
 else {
    vmGuru=getVmGuru(vm);
  }
  vm=vmGuru.findById(vm.getId());
  Ternary<T,ReservationContext,ItWorkVO> start=changeToStartState(vmGuru,vm,caller,account);
  if (start == null) {
    return vmGuru.findById(vmId);
  }
  vm=start.first();
  ReservationContext ctx=start.second();
  ItWorkVO work=start.third();
  T startedVm=null;
  ServiceOfferingVO offering=_offeringDao.findById(vm.getServiceOfferingId());
  VMTemplateVO template=_templateDao.findById(vm.getTemplateId());
  DataCenterDeployment plan=new DataCenterDeployment(vm.getDataCenterIdToDeployIn(),vm.getPodIdToDeployIn(),null,null,null);
  if (planToDeploy != null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str");
    }
    plan=(DataCenterDeployment)planToDeploy;
  }
  HypervisorGuru hvGuru=_hvGuruMgr.getGuru(vm.getHypervisorType());
  boolean canRetry=true;
  try {
    Journal journal=start.second().getJournal();
    List<VolumeVO> vols=_volsDao.findReadyRootVolumesByInstance(vm.getId());
    for (    VolumeVO vol : vols) {
      Long volTemplateId=vol.getTemplateId();
      if (volTemplateId != null && volTemplateId.longValue() != template.getId()) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug(vol + "String_Node_Str" + vm+ "String_Node_Str");
        }
        continue;
      }
      StoragePoolVO pool=_storagePoolDao.findById(vol.getPoolId());
      if (!pool.isInMaintenance()) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str");
        }
        long rootVolDcId=pool.getDataCenterId();
        Long rootVolPodId=pool.getPodId();
        Long rootVolClusterId=pool.getClusterId();
        if (planToDeploy != null) {
          Long clusterIdSpecified=planToDeploy.getClusterId();
          if (clusterIdSpecified != null && rootVolClusterId != null) {
            if (rootVolClusterId.longValue() != clusterIdSpecified.longValue()) {
              if (s_logger.isDebugEnabled()) {
                s_logger.debug("String_Node_Str" + rootVolClusterId + "String_Node_Str"+ clusterIdSpecified);
              }
              throw new ResourceUnavailableException("String_Node_Str" + vm,Cluster.class,clusterIdSpecified);
            }
          }
          plan=new DataCenterDeployment(planToDeploy.getDataCenterId(),planToDeploy.getPodId(),planToDeploy.getClusterId(),planToDeploy.getHostId(),vol.getPoolId());
        }
 else {
          plan=new DataCenterDeployment(rootVolDcId,rootVolPodId,rootVolClusterId,null,vol.getPoolId());
          if (s_logger.isDebugEnabled()) {
            s_logger.debug(vol + "String_Node_Str" + rootVolDcId+ "String_Node_Str"+ rootVolPodId+ "String_Node_Str"+ rootVolClusterId);
          }
        }
      }
    }
    ExcludeList avoids=new ExcludeList();
    int retry=_retry;
    while (retry-- != 0) {
      VirtualMachineProfileImpl<T> vmProfile=new VirtualMachineProfileImpl<T>(vm,template,offering,account,params);
      DeployDestination dest=null;
      for (      DeploymentPlanner planner : _planners) {
        if (planner.canHandle(vmProfile,plan,avoids)) {
          dest=planner.plan(vmProfile,plan,avoids);
        }
 else {
          continue;
        }
        if (dest != null) {
          avoids.addHost(dest.getHost().getId());
          journal.record("String_Node_Str",vmProfile,dest);
          break;
        }
      }
      if (dest == null) {
        throw new InsufficientServerCapacityException("String_Node_Str" + vmProfile,DataCenter.class,plan.getDataCenterId());
      }
      long destHostId=dest.getHost().getId();
      try {
        if (!changeState(vm,Event.OperationRetry,destHostId,work,Step.Prepare)) {
          throw new ConcurrentOperationException("String_Node_Str");
        }
      }
 catch (      NoTransitionException e1) {
        throw new ConcurrentOperationException(e1.getMessage());
      }
      try {
        _networkMgr.prepare(vmProfile,dest,ctx);
        if (vm.getHypervisorType() != HypervisorType.BareMetal) {
          _storageMgr.prepare(vmProfile,dest);
        }
        vmGuru.finalizeVirtualMachineProfile(vmProfile,dest,ctx);
        VirtualMachineTO vmTO=hvGuru.implement(vmProfile);
        Commands cmds=new Commands(OnError.Stop);
        cmds.addCommand(new StartCommand(vmTO));
        vmGuru.finalizeDeployment(cmds,vmProfile,dest,ctx);
        vm.setPodId(dest.getPod().getId());
        work=_workDao.findById(work.getId());
        if (work == null || work.getStep() != Step.Prepare) {
          throw new ConcurrentOperationException("String_Node_Str" + work);
        }
        _workDao.updateStep(work,Step.Starting);
        _agentMgr.send(destHostId,cmds);
        _workDao.updateStep(work,Step.Started);
        Answer startAnswer=cmds.getAnswer(StartAnswer.class);
        if (startAnswer != null && startAnswer.getResult()) {
          if (vmGuru.finalizeStart(vmProfile,destHostId,cmds,ctx)) {
            if (!changeState(vm,Event.OperationSucceeded,destHostId,work,Step.Done)) {
              throw new ConcurrentOperationException("String_Node_Str");
            }
            startedVm=vm;
            if (s_logger.isDebugEnabled()) {
              s_logger.debug("String_Node_Str" + vm);
            }
            return startedVm;
          }
 else {
            if (s_logger.isDebugEnabled()) {
              s_logger.info("String_Node_Str" + vm);
            }
            StopCommand cmd=new StopCommand(vm.getInstanceName());
            StopAnswer answer=(StopAnswer)_agentMgr.easySend(destHostId,cmd);
            if (answer == null || !answer.getResult()) {
              s_logger.warn("String_Node_Str" + vm + "String_Node_Str"+ (answer != null ? answer.getDetails() : "String_Node_Str"));
              canRetry=false;
              _haMgr.scheduleStop(vm,destHostId,WorkType.ForceStop);
              throw new ExecutionException("String_Node_Str" + vm + "String_Node_Str");
            }
          }
        }
        s_logger.info("String_Node_Str" + dest.getHost() + "String_Node_Str"+ (startAnswer == null ? "String_Node_Str" : startAnswer.getDetails()));
      }
 catch (      OperationTimedoutException e) {
        s_logger.debug("String_Node_Str" + dest.getHost());
        if (e.isActive()) {
          _haMgr.scheduleStop(vm,destHostId,WorkType.CheckStop);
        }
        canRetry=false;
        throw new AgentUnavailableException("String_Node_Str" + vm.getHostName(),destHostId,e);
      }
catch (      ResourceUnavailableException e) {
        s_logger.info("String_Node_Str",e);
        if (!avoids.add(e)) {
          if (e.getScope() == Volume.class || e.getScope() == Nic.class) {
            throw e;
          }
 else {
            s_logger.warn("String_Node_Str" + e.getScope().getName(),e);
            throw e;
          }
        }
      }
catch (      InsufficientCapacityException e) {
        s_logger.info("String_Node_Str",e);
        if (!avoids.add(e)) {
          if (e.getScope() == Volume.class || e.getScope() == Nic.class) {
            throw e;
          }
 else {
            s_logger.warn("String_Node_Str" + e.getScope().getName(),e);
          }
        }
      }
catch (      Exception e) {
        s_logger.error("String_Node_Str" + vm,e);
        throw new AgentUnavailableException("String_Node_Str",destHostId,e);
      }
 finally {
        if (startedVm == null && canRetry) {
          _workDao.updateStep(work,Step.Release);
          cleanup(vmGuru,vmProfile,work,Event.OperationFailed,false,caller,account);
        }
      }
    }
  }
  finally {
    if (startedVm == null) {
      if (vm.getType().equals(VirtualMachine.Type.User) && (vm.getLastHostId() == null)) {
        _accountMgr.decrementResourceCount(vm.getAccountId(),ResourceType.user_vm);
      }
      if (canRetry) {
        try {
          changeState(vm,Event.OperationFailed,null,work,Step.Done);
        }
 catch (        NoTransitionException e) {
          throw new ConcurrentOperationException(e.getMessage());
        }
      }
    }
  }
  return startedVm;
}

Fixed version (ground truth):
@Override public <T extends VMInstanceVO>T advanceStart(T vm,Map<VirtualMachineProfile.Param,Object> params,User caller,Account account,DeploymentPlan planToDeploy) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
  long vmId=vm.getId();
  VirtualMachineGuru<T> vmGuru;
  if (vm.getHypervisorType() == HypervisorType.BareMetal) {
    vmGuru=getBareMetalVmGuru(vm);
  }
 else {
    vmGuru=getVmGuru(vm);
  }
  vm=vmGuru.findById(vm.getId());
  Ternary<T,ReservationContext,ItWorkVO> start=changeToStartState(vmGuru,vm,caller,account);
  if (start == null) {
    return vmGuru.findById(vmId);
  }
  vm=start.first();
  ReservationContext ctx=start.second();
  ItWorkVO work=start.third();
  T startedVm=null;
  ServiceOfferingVO offering=_offeringDao.findById(vm.getServiceOfferingId());
  VMTemplateVO template=_templateDao.findById(vm.getTemplateId());
  DataCenterDeployment plan=new DataCenterDeployment(vm.getDataCenterIdToDeployIn(),vm.getPodIdToDeployIn(),null,null,null);
  if (planToDeploy != null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str");
    }
    plan=(DataCenterDeployment)planToDeploy;
  }
  HypervisorGuru hvGuru=_hvGuruMgr.getGuru(vm.getHypervisorType());
  boolean canRetry=true;
  try {
    Journal journal=start.second().getJournal();
    ExcludeList avoids=new ExcludeList();
    int retry=_retry;
    while (retry-- != 0) {
      List<VolumeVO> vols=_volsDao.findReadyRootVolumesByInstance(vm.getId());
      for (      VolumeVO vol : vols) {
        Long volTemplateId=vol.getTemplateId();
        if (volTemplateId != null && volTemplateId.longValue() != template.getId()) {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug(vol + "String_Node_Str" + vm+ "String_Node_Str");
          }
          continue;
        }
        StoragePoolVO pool=_storagePoolDao.findById(vol.getPoolId());
        if (!pool.isInMaintenance()) {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug("String_Node_Str");
          }
          long rootVolDcId=pool.getDataCenterId();
          Long rootVolPodId=pool.getPodId();
          Long rootVolClusterId=pool.getClusterId();
          if (planToDeploy != null) {
            Long clusterIdSpecified=planToDeploy.getClusterId();
            if (clusterIdSpecified != null && rootVolClusterId != null) {
              if (rootVolClusterId.longValue() != clusterIdSpecified.longValue()) {
                if (s_logger.isDebugEnabled()) {
                  s_logger.debug("String_Node_Str" + rootVolClusterId + "String_Node_Str"+ clusterIdSpecified);
                }
                throw new ResourceUnavailableException("String_Node_Str" + vm,Cluster.class,clusterIdSpecified);
              }
            }
            plan=new DataCenterDeployment(planToDeploy.getDataCenterId(),planToDeploy.getPodId(),planToDeploy.getClusterId(),planToDeploy.getHostId(),vol.getPoolId());
          }
 else {
            plan=new DataCenterDeployment(rootVolDcId,rootVolPodId,rootVolClusterId,null,vol.getPoolId());
            if (s_logger.isDebugEnabled()) {
              s_logger.debug(vol + "String_Node_Str" + rootVolDcId+ "String_Node_Str"+ rootVolPodId+ "String_Node_Str"+ rootVolClusterId);
            }
          }
        }
      }
      VirtualMachineProfileImpl<T> vmProfile=new VirtualMachineProfileImpl<T>(vm,template,offering,account,params);
      DeployDestination dest=null;
      for (      DeploymentPlanner planner : _planners) {
        if (planner.canHandle(vmProfile,plan,avoids)) {
          dest=planner.plan(vmProfile,plan,avoids);
        }
 else {
          continue;
        }
        if (dest != null) {
          avoids.addHost(dest.getHost().getId());
          journal.record("String_Node_Str",vmProfile,dest);
          break;
        }
      }
      if (dest == null) {
        throw new InsufficientServerCapacityException("String_Node_Str" + vmProfile,DataCenter.class,plan.getDataCenterId());
      }
      long destHostId=dest.getHost().getId();
      try {
        if (!changeState(vm,Event.OperationRetry,destHostId,work,Step.Prepare)) {
          throw new ConcurrentOperationException("String_Node_Str");
        }
      }
 catch (      NoTransitionException e1) {
        throw new ConcurrentOperationException(e1.getMessage());
      }
      try {
        _networkMgr.prepare(vmProfile,dest,ctx);
        if (vm.getHypervisorType() != HypervisorType.BareMetal) {
          _storageMgr.prepare(vmProfile,dest);
        }
        vmGuru.finalizeVirtualMachineProfile(vmProfile,dest,ctx);
        VirtualMachineTO vmTO=hvGuru.implement(vmProfile);
        Commands cmds=new Commands(OnError.Stop);
        cmds.addCommand(new StartCommand(vmTO));
        vmGuru.finalizeDeployment(cmds,vmProfile,dest,ctx);
        vm.setPodId(dest.getPod().getId());
        work=_workDao.findById(work.getId());
        if (work == null || work.getStep() != Step.Prepare) {
          throw new ConcurrentOperationException("String_Node_Str" + work);
        }
        _workDao.updateStep(work,Step.Starting);
        _agentMgr.send(destHostId,cmds);
        _workDao.updateStep(work,Step.Started);
        Answer startAnswer=cmds.getAnswer(StartAnswer.class);
        if (startAnswer != null && startAnswer.getResult()) {
          if (vmGuru.finalizeStart(vmProfile,destHostId,cmds,ctx)) {
            if (!changeState(vm,Event.OperationSucceeded,destHostId,work,Step.Done)) {
              throw new ConcurrentOperationException("String_Node_Str");
            }
            startedVm=vm;
            if (s_logger.isDebugEnabled()) {
              s_logger.debug("String_Node_Str" + vm);
            }
            return startedVm;
          }
 else {
            if (s_logger.isDebugEnabled()) {
              s_logger.info("String_Node_Str" + vm);
            }
            StopCommand cmd=new StopCommand(vm.getInstanceName());
            StopAnswer answer=(StopAnswer)_agentMgr.easySend(destHostId,cmd);
            if (answer == null || !answer.getResult()) {
              s_logger.warn("String_Node_Str" + vm + "String_Node_Str"+ (answer != null ? answer.getDetails() : "String_Node_Str"));
              canRetry=false;
              _haMgr.scheduleStop(vm,destHostId,WorkType.ForceStop);
              throw new ExecutionException("String_Node_Str" + vm + "String_Node_Str");
            }
          }
        }
        s_logger.info("String_Node_Str" + dest.getHost() + "String_Node_Str"+ (startAnswer == null ? "String_Node_Str" : startAnswer.getDetails()));
      }
 catch (      OperationTimedoutException e) {
        s_logger.debug("String_Node_Str" + dest.getHost());
        if (e.isActive()) {
          _haMgr.scheduleStop(vm,destHostId,WorkType.CheckStop);
        }
        canRetry=false;
        throw new AgentUnavailableException("String_Node_Str" + vm.getHostName(),destHostId,e);
      }
catch (      ResourceUnavailableException e) {
        s_logger.info("String_Node_Str",e);
        if (!avoids.add(e)) {
          if (e.getScope() == Volume.class || e.getScope() == Nic.class) {
            throw e;
          }
 else {
            s_logger.warn("String_Node_Str" + e.getScope().getName(),e);
            throw e;
          }
        }
      }
catch (      InsufficientCapacityException e) {
        s_logger.info("String_Node_Str",e);
        if (!avoids.add(e)) {
          if (e.getScope() == Volume.class || e.getScope() == Nic.class) {
            throw e;
          }
 else {
            s_logger.warn("String_Node_Str" + e.getScope().getName(),e);
          }
        }
      }
catch (      Exception e) {
        s_logger.error("String_Node_Str" + vm,e);
        throw new AgentUnavailableException("String_Node_Str",destHostId,e);
      }
 finally {
        if (startedVm == null && canRetry) {
          _workDao.updateStep(work,Step.Release);
          cleanup(vmGuru,vmProfile,work,Event.OperationFailed,false,caller,account);
        }
      }
    }
  }
  finally {
    if (startedVm == null) {
      if (vm.getType().equals(VirtualMachine.Type.User) && (vm.getLastHostId() == null)) {
        _accountMgr.decrementResourceCount(vm.getAccountId(),ResourceType.user_vm);
      }
      if (canRetry) {
        try {
          changeState(vm,Event.OperationFailed,null,work,Step.Done);
        }
 catch (        NoTransitionException e) {
          throw new ConcurrentOperationException(e.getMessage());
        }
      }
    }
  }
  return startedVm;
}