Program: 105652
Project: 105652
Buggy source:
@Override public void handleVolumeSync(DataStore store){
  if (store == null) {
    s_logger.warn("String_Node_Str");
    return;
  }
  long storeId=store.getId();
  String lockString="String_Node_Str" + storeId;
  GlobalLock syncLock=GlobalLock.getInternLock(lockString);
  try {
    if (syncLock.lock(3)) {
      try {
        Map<Long,TemplateProp> volumeInfos=listVolume(store);
        if (volumeInfos == null) {
          return;
        }
        List<VolumeDataStoreVO> dbVolumes=_volumeStoreDao.listByStoreId(storeId);
        List<VolumeDataStoreVO> toBeDownloaded=new ArrayList<VolumeDataStoreVO>(dbVolumes);
        for (        VolumeDataStoreVO volumeStore : dbVolumes) {
          VolumeVO volume=_volumeDao.findById(volumeStore.getVolumeId());
          if (volume == null) {
            s_logger.warn("String_Node_Str" + volumeStore.getVolumeId() + "String_Node_Str"+ storeId+ "String_Node_Str");
            volumeStore.setDestroyed(true);
            _volumeStoreDao.update(volumeStore.getId(),volumeStore);
            continue;
          }
          if (volumeInfos.containsKey(volume.getId())) {
            TemplateProp volInfo=volumeInfos.remove(volume.getId());
            toBeDownloaded.remove(volumeStore);
            s_logger.info("String_Node_Str" + volume.getUuid() + "String_Node_Str");
            if (volumeStore.getDownloadState() != Status.DOWNLOADED) {
              volumeStore.setErrorString("String_Node_Str");
            }
            if (volInfo.isCorrupted()) {
              volumeStore.setDownloadState(Status.DOWNLOAD_ERROR);
              String msg="String_Node_Str" + volume.getUuid() + "String_Node_Str";
              volumeStore.setErrorString(msg);
              s_logger.info("String_Node_Str");
              if (volumeStore.getDownloadUrl() == null) {
                msg="String_Node_Str" + volume.getUuid() + "String_Node_Str"+ volInfo.getInstallPath()+ "String_Node_Str"+ volumeStore.getDataStoreId();
                s_logger.warn(msg);
              }
 else {
                s_logger.info("String_Node_Str" + volume.getName());
                _volumeStoreDao.remove(volumeStore.getId());
                toBeDownloaded.add(volumeStore);
              }
            }
 else {
              volumeStore.setDownloadPercent(100);
              volumeStore.setDownloadState(Status.DOWNLOADED);
              volumeStore.setState(ObjectInDataStoreStateMachine.State.Ready);
              volumeStore.setInstallPath(volInfo.getInstallPath());
              volumeStore.setSize(volInfo.getSize());
              volumeStore.setPhysicalSize(volInfo.getPhysicalSize());
              volumeStore.setLastUpdated(new Date());
              _volumeStoreDao.update(volumeStore.getId(),volumeStore);
              if (volume.getSize() == 0) {
                volume.setSize(volInfo.getSize());
                _volumeDao.update(volumeStore.getVolumeId(),volume);
              }
              if (volInfo.getSize() > 0) {
                try {
                  _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(volume.getAccountId()),com.cloud.configuration.Resource.ResourceType.secondary_storage,volInfo.getSize() - volInfo.getPhysicalSize());
                }
 catch (                ResourceAllocationException e) {
                  s_logger.warn(e.getMessage());
                  _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED,volume.getDataCenterId(),volume.getPodId(),e.getMessage(),e.getMessage());
                }
 finally {
                  _resourceLimitMgr.recalculateResourceCount(volume.getAccountId(),volume.getDomainId(),com.cloud.configuration.Resource.ResourceType.secondary_storage.getOrdinal());
                }
              }
            }
            continue;
          }
          if (volumeStore.getDownloadState() != Status.DOWNLOADED) {
            s_logger.info("String_Node_Str" + volume.getName() + "String_Node_Str"+ storeId+ "String_Node_Str");
            toBeDownloaded.add(volumeStore);
          }
        }
        if (toBeDownloaded.size() > 0) {
          for (          VolumeDataStoreVO volumeHost : toBeDownloaded) {
            if (volumeHost.getDownloadUrl() == null) {
              s_logger.info("String_Node_Str" + volumeHost.getVolumeId() + "String_Node_Str");
              continue;
            }
            if (store.getScope().getScopeType() == ScopeType.REGION) {
              if (volumeHost.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED && volumeHost.getInstallPath() == null) {
                s_logger.info("String_Node_Str");
                continue;
              }
            }
            s_logger.debug("String_Node_Str" + volumeHost.getVolumeId() + "String_Node_Str"+ store.getName());
            VolumeInfo vol=volFactory.getVolume(volumeHost.getVolumeId());
            createVolumeAsync(vol,store);
          }
        }
        for (        Long uniqueName : volumeInfos.keySet()) {
          TemplateProp tInfo=volumeInfos.get(uniqueName);
          VolumeObjectTO tmplTO=new VolumeObjectTO();
          tmplTO.setDataStore(store.getTO());
          tmplTO.setPath(tInfo.getInstallPath());
          tmplTO.setId(tInfo.getId());
          DeleteCommand dtCommand=new DeleteCommand(tmplTO);
          EndPoint ep=_epSelector.select(store);
          Answer answer=null;
          if (ep == null) {
            String errMsg="String_Node_Str";
            s_logger.error(errMsg);
            answer=new Answer(dtCommand,false,errMsg);
          }
 else {
            answer=ep.sendMessage(dtCommand);
          }
          if (answer == null || !answer.getResult()) {
            s_logger.info("String_Node_Str" + store.getName());
          }
 else {
            String description="String_Node_Str" + tInfo.getTemplateName() + "String_Node_Str"+ storeId;
            s_logger.info(description);
          }
        }
      }
  finally {
        syncLock.unlock();
      }
    }
 else {
      s_logger.info("String_Node_Str" + lockString + "String_Node_Str"+ storeId+ "String_Node_Str");
    }
  }
  finally {
    syncLock.releaseRef();
  }
}

Fixed version (ground truth):
@Override public void handleVolumeSync(DataStore store){
  if (store == null) {
    s_logger.warn("String_Node_Str");
    return;
  }
  long storeId=store.getId();
  String lockString="String_Node_Str" + storeId;
  GlobalLock syncLock=GlobalLock.getInternLock(lockString);
  try {
    if (syncLock.lock(3)) {
      try {
        Map<Long,TemplateProp> volumeInfos=listVolume(store);
        if (volumeInfos == null) {
          return;
        }
        List<VolumeDataStoreVO> dbVolumes=_volumeStoreDao.listByStoreId(storeId);
        List<VolumeDataStoreVO> toBeDownloaded=new ArrayList<VolumeDataStoreVO>(dbVolumes);
        for (        VolumeDataStoreVO volumeStore : dbVolumes) {
          VolumeVO volume=_volumeDao.findById(volumeStore.getVolumeId());
          if (volume == null) {
            s_logger.warn("String_Node_Str" + volumeStore.getVolumeId() + "String_Node_Str"+ storeId+ "String_Node_Str");
            volumeStore.setDestroyed(true);
            _volumeStoreDao.update(volumeStore.getId(),volumeStore);
            continue;
          }
          if (volumeInfos.containsKey(volume.getId())) {
            TemplateProp volInfo=volumeInfos.remove(volume.getId());
            toBeDownloaded.remove(volumeStore);
            s_logger.info("String_Node_Str" + volume.getUuid() + "String_Node_Str");
            if (volumeStore.getDownloadState() != Status.DOWNLOADED) {
              volumeStore.setErrorString("String_Node_Str");
            }
            if (volInfo.isCorrupted()) {
              volumeStore.setDownloadState(Status.DOWNLOAD_ERROR);
              String msg="String_Node_Str" + volume.getUuid() + "String_Node_Str";
              volumeStore.setErrorString(msg);
              s_logger.info("String_Node_Str");
              if (volumeStore.getDownloadUrl() == null) {
                msg="String_Node_Str" + volume.getUuid() + "String_Node_Str"+ volInfo.getInstallPath()+ "String_Node_Str"+ volumeStore.getDataStoreId();
                s_logger.warn(msg);
              }
 else {
                s_logger.info("String_Node_Str" + volume.getName());
                _volumeStoreDao.remove(volumeStore.getId());
                toBeDownloaded.add(volumeStore);
              }
            }
 else {
              volumeStore.setDownloadPercent(100);
              volumeStore.setDownloadState(Status.DOWNLOADED);
              volumeStore.setState(ObjectInDataStoreStateMachine.State.Ready);
              volumeStore.setInstallPath(volInfo.getInstallPath());
              volumeStore.setSize(volInfo.getSize());
              volumeStore.setPhysicalSize(volInfo.getPhysicalSize());
              volumeStore.setLastUpdated(new Date());
              _volumeStoreDao.update(volumeStore.getId(),volumeStore);
              if (volume.getSize() == 0) {
                volume.setSize(volInfo.getSize());
                _volumeDao.update(volumeStore.getVolumeId(),volume);
              }
              if (volInfo.getSize() > 0) {
                try {
                  _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(volume.getAccountId()),com.cloud.configuration.Resource.ResourceType.secondary_storage,volInfo.getSize() - volInfo.getPhysicalSize());
                }
 catch (                ResourceAllocationException e) {
                  s_logger.warn(e.getMessage());
                  _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED,volume.getDataCenterId(),volume.getPodId(),e.getMessage(),e.getMessage());
                }
 finally {
                  _resourceLimitMgr.recalculateResourceCount(volume.getAccountId(),volume.getDomainId(),com.cloud.configuration.Resource.ResourceType.secondary_storage.getOrdinal());
                }
              }
            }
            continue;
          }
          if (volumeStore.getDownloadState() != Status.DOWNLOADED) {
            s_logger.info("String_Node_Str" + volume.getName() + "String_Node_Str"+ storeId+ "String_Node_Str");
            toBeDownloaded.add(volumeStore);
          }
        }
        if (toBeDownloaded.size() > 0) {
          for (          VolumeDataStoreVO volumeHost : toBeDownloaded) {
            if (volumeHost.getDownloadUrl() == null) {
              s_logger.info("String_Node_Str" + volumeHost.getVolumeId() + "String_Node_Str");
              continue;
            }
            if (store.getScope().getScopeType() == ScopeType.REGION) {
              if (volumeHost.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED && volumeHost.getInstallPath() == null) {
                s_logger.info("String_Node_Str");
                continue;
              }
            }
            s_logger.debug("String_Node_Str" + volumeHost.getVolumeId() + "String_Node_Str"+ store.getName());
            VolumeInfo vol=volFactory.getVolume(volumeHost.getVolumeId());
            createVolumeAsync(vol,store);
          }
        }
        for (        Map.Entry<Long,TemplateProp> entry : volumeInfos.entrySet()) {
          Long uniqueName=entry.getKey();
          TemplateProp tInfo=entry.getValue();
          VolumeObjectTO tmplTO=new VolumeObjectTO();
          tmplTO.setDataStore(store.getTO());
          tmplTO.setPath(tInfo.getInstallPath());
          tmplTO.setId(tInfo.getId());
          DeleteCommand dtCommand=new DeleteCommand(tmplTO);
          EndPoint ep=_epSelector.select(store);
          Answer answer=null;
          if (ep == null) {
            String errMsg="String_Node_Str";
            s_logger.error(errMsg);
            answer=new Answer(dtCommand,false,errMsg);
          }
 else {
            answer=ep.sendMessage(dtCommand);
          }
          if (answer == null || !answer.getResult()) {
            s_logger.info("String_Node_Str" + store.getName());
          }
 else {
            String description="String_Node_Str" + tInfo.getTemplateName() + "String_Node_Str"+ storeId;
            s_logger.info(description);
          }
        }
      }
  finally {
        syncLock.unlock();
      }
    }
 else {
      s_logger.info("String_Node_Str" + lockString + "String_Node_Str"+ storeId+ "String_Node_Str");
    }
  }
  finally {
    syncLock.releaseRef();
  }
}