Program: 107008
Project: 107008
Buggy source:
@Override @DB public VMTemplateStoragePoolVO prepareTemplateForCreate(VMTemplateVO template,StoragePool pool){
  template=_tmpltDao.findById(template.getId(),true);
  long poolId=pool.getId();
  long templateId=template.getId();
  VMTemplateStoragePoolVO templateStoragePoolRef=null;
  VMTemplateHostVO templateHostRef=null;
  long templateStoragePoolRefId;
  String origUrl=null;
  templateStoragePoolRef=_tmpltPoolDao.findByPoolTemplate(poolId,templateId);
  if (templateStoragePoolRef != null) {
    templateStoragePoolRef.setMarkedForGC(false);
    _tmpltPoolDao.update(templateStoragePoolRef.getId(),templateStoragePoolRef);
    if (templateStoragePoolRef.getDownloadState() == Status.DOWNLOADED) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + templateId + "String_Node_Str"+ poolId);
      }
      return templateStoragePoolRef;
    }
  }
  templateHostRef=_storageMgr.findVmTemplateHost(templateId,pool.getDataCenterId(),pool.getPodId());
  if (templateHostRef == null) {
    s_logger.debug("String_Node_Str");
    return null;
  }
  HostVO sh=_hostDao.findById(templateHostRef.getHostId());
  origUrl=sh.getStorageUrl();
  if (origUrl == null) {
    throw new CloudRuntimeException("String_Node_Str" + sh.toString());
  }
  if (templateStoragePoolRef == null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + templateId + "String_Node_Str"+ poolId);
    }
    templateStoragePoolRef=new VMTemplateStoragePoolVO(poolId,templateId);
    try {
      templateStoragePoolRef=_tmpltPoolDao.persist(templateStoragePoolRef);
      templateStoragePoolRefId=templateStoragePoolRef.getId();
    }
 catch (    Exception e) {
      s_logger.debug("String_Node_Str" + e.getMessage());
      templateStoragePoolRef=_tmpltPoolDao.findByPoolTemplate(poolId,templateId);
      if (templateStoragePoolRef == null) {
        throw new CloudRuntimeException("String_Node_Str" + poolId + "String_Node_Str"+ templateId);
      }
      templateStoragePoolRefId=templateStoragePoolRef.getId();
    }
  }
 else {
    templateStoragePoolRefId=templateStoragePoolRef.getId();
  }
  List<StoragePoolHostVO> vos=_poolHostDao.listByPoolId(poolId);
  templateStoragePoolRef=_tmpltPoolDao.acquireInLockTable(templateStoragePoolRefId,1200);
  if (templateStoragePoolRef == null) {
    throw new CloudRuntimeException("String_Node_Str" + templateStoragePoolRefId);
  }
  try {
    if (templateStoragePoolRef.getDownloadState() == Status.DOWNLOADED) {
      return templateStoragePoolRef;
    }
    String url=origUrl + "String_Node_Str" + templateHostRef.getInstallPath();
    PrimaryStorageDownloadCommand dcmd=new PrimaryStorageDownloadCommand(template.getUniqueName(),url,template.getFormat(),template.getAccountId(),pool.getId(),pool.getUuid());
    HostVO secondaryStorageHost=_hostDao.findById(templateHostRef.getHostId());
    assert(secondaryStorageHost != null);
    dcmd.setSecondaryStorageUrl(secondaryStorageHost.getStorageUrl());
    dcmd.setPrimaryStorageUrl("String_Node_Str" + pool.getHostAddress() + pool.getPath());
    for (    StoragePoolHostVO vo : vos) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + templateId + "String_Node_Str"+ vo.getHostId());
      }
      dcmd.setLocalPath(vo.getLocalPath());
      PrimaryStorageDownloadAnswer answer=(PrimaryStorageDownloadAnswer)_agentMgr.easySend(_hvGuruMgr.getGuruProcessedCommandTargetHost(vo.getHostId(),dcmd),dcmd,120 * 60 * 1000);
      if (answer != null && answer.getResult()) {
        templateStoragePoolRef.setDownloadPercent(100);
        templateStoragePoolRef.setDownloadState(Status.DOWNLOADED);
        templateStoragePoolRef.setLocalDownloadPath(answer.getInstallPath());
        templateStoragePoolRef.setInstallPath(answer.getInstallPath());
        templateStoragePoolRef.setTemplateSize(answer.getTemplateSize());
        _tmpltPoolDao.update(templateStoragePoolRef.getId(),templateStoragePoolRef);
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + templateId + "String_Node_Str"+ vo.getHostId());
        }
        return templateStoragePoolRef;
      }
 else {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + templateId + "String_Node_Str"+ vo.getPoolId()+ "String_Node_Str"+ (answer != null ? answer.getDetails() : "String_Node_Str"));
        }
      }
    }
  }
  finally {
    _tmpltPoolDao.releaseFromLockTable(templateStoragePoolRefId);
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + templateId + "String_Node_Str"+ poolId);
  }
  return null;
}

Fixed version (ground truth):
@Override @DB public VMTemplateStoragePoolVO prepareTemplateForCreate(VMTemplateVO template,StoragePool pool){
  template=_tmpltDao.findById(template.getId(),true);
  long poolId=pool.getId();
  long templateId=template.getId();
  VMTemplateStoragePoolVO templateStoragePoolRef=null;
  VMTemplateHostVO templateHostRef=null;
  long templateStoragePoolRefId;
  String origUrl=null;
  templateStoragePoolRef=_tmpltPoolDao.findByPoolTemplate(poolId,templateId);
  if (templateStoragePoolRef != null) {
    templateStoragePoolRef.setMarkedForGC(false);
    _tmpltPoolDao.update(templateStoragePoolRef.getId(),templateStoragePoolRef);
    if (templateStoragePoolRef.getDownloadState() == Status.DOWNLOADED) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + templateId + "String_Node_Str"+ poolId);
      }
      return templateStoragePoolRef;
    }
  }
  templateHostRef=_storageMgr.findVmTemplateHost(templateId,pool);
  if (templateHostRef == null) {
    s_logger.debug("String_Node_Str");
    return null;
  }
  HostVO sh=_hostDao.findById(templateHostRef.getHostId());
  origUrl=sh.getStorageUrl();
  if (origUrl == null) {
    throw new CloudRuntimeException("String_Node_Str" + sh.toString());
  }
  if (templateStoragePoolRef == null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + templateId + "String_Node_Str"+ poolId);
    }
    templateStoragePoolRef=new VMTemplateStoragePoolVO(poolId,templateId);
    try {
      templateStoragePoolRef=_tmpltPoolDao.persist(templateStoragePoolRef);
      templateStoragePoolRefId=templateStoragePoolRef.getId();
    }
 catch (    Exception e) {
      s_logger.debug("String_Node_Str" + e.getMessage());
      templateStoragePoolRef=_tmpltPoolDao.findByPoolTemplate(poolId,templateId);
      if (templateStoragePoolRef == null) {
        throw new CloudRuntimeException("String_Node_Str" + poolId + "String_Node_Str"+ templateId);
      }
      templateStoragePoolRefId=templateStoragePoolRef.getId();
    }
  }
 else {
    templateStoragePoolRefId=templateStoragePoolRef.getId();
  }
  List<StoragePoolHostVO> vos=_poolHostDao.listByPoolId(poolId);
  templateStoragePoolRef=_tmpltPoolDao.acquireInLockTable(templateStoragePoolRefId,1200);
  if (templateStoragePoolRef == null) {
    throw new CloudRuntimeException("String_Node_Str" + templateStoragePoolRefId);
  }
  try {
    if (templateStoragePoolRef.getDownloadState() == Status.DOWNLOADED) {
      return templateStoragePoolRef;
    }
    String url=origUrl + "String_Node_Str" + templateHostRef.getInstallPath();
    PrimaryStorageDownloadCommand dcmd=new PrimaryStorageDownloadCommand(template.getUniqueName(),url,template.getFormat(),template.getAccountId(),pool.getId(),pool.getUuid());
    HostVO secondaryStorageHost=_hostDao.findById(templateHostRef.getHostId());
    assert(secondaryStorageHost != null);
    dcmd.setSecondaryStorageUrl(secondaryStorageHost.getStorageUrl());
    dcmd.setPrimaryStorageUrl("String_Node_Str" + pool.getHostAddress() + pool.getPath());
    for (    StoragePoolHostVO vo : vos) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + templateId + "String_Node_Str"+ vo.getHostId());
      }
      dcmd.setLocalPath(vo.getLocalPath());
      PrimaryStorageDownloadAnswer answer=(PrimaryStorageDownloadAnswer)_agentMgr.easySend(_hvGuruMgr.getGuruProcessedCommandTargetHost(vo.getHostId(),dcmd),dcmd,120 * 60 * 1000);
      if (answer != null && answer.getResult()) {
        templateStoragePoolRef.setDownloadPercent(100);
        templateStoragePoolRef.setDownloadState(Status.DOWNLOADED);
        templateStoragePoolRef.setLocalDownloadPath(answer.getInstallPath());
        templateStoragePoolRef.setInstallPath(answer.getInstallPath());
        templateStoragePoolRef.setTemplateSize(answer.getTemplateSize());
        _tmpltPoolDao.update(templateStoragePoolRef.getId(),templateStoragePoolRef);
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + templateId + "String_Node_Str"+ vo.getHostId());
        }
        return templateStoragePoolRef;
      }
 else {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + templateId + "String_Node_Str"+ vo.getPoolId()+ "String_Node_Str"+ (answer != null ? answer.getDetails() : "String_Node_Str"));
        }
      }
    }
  }
  finally {
    _tmpltPoolDao.releaseFromLockTable(templateStoragePoolRefId);
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + templateId + "String_Node_Str"+ poolId);
  }
  return null;
}