Program: 107026
Project: 107026
Buggy source:
@Override @DB public boolean backupSnapshotToSecondaryStorage(SnapshotVO ss){
  long snapshotId=ss.getId();
  SnapshotVO snapshot=_snapshotDao.acquireInLockTable(snapshotId);
  if (snapshot == null) {
    throw new CloudRuntimeException("String_Node_Str" + ss);
  }
  try {
    snapshot.setStatus(Snapshot.Status.BackingUp);
    _snapshotDao.update(snapshot.getId(),snapshot);
    long volumeId=snapshot.getVolumeId();
    VolumeVO volume=_volsDao.lockRow(volumeId,true);
    String primaryStoragePoolNameLabel=_storageMgr.getPrimaryStorageNameLabel(volume);
    Long dcId=volume.getDataCenterId();
    Long accountId=volume.getAccountId();
    HostVO secHost=getSecHost(volumeId,volume.getDataCenterId());
    String secondaryStoragePoolUrl=secHost.getStorageUrl();
    String snapshotUuid=snapshot.getPath();
    SnapshotVO prevSnapshot=null;
    String prevSnapshotUuid=null;
    String prevBackupUuid=null;
    SwiftVO swift=_swiftDao.findById(1L);
    long prevSnapshotId=snapshot.getPrevSnapshotId();
    if (prevSnapshotId > 0) {
      prevSnapshot=_snapshotDao.findByIdIncludingRemoved(prevSnapshotId);
      if (prevSnapshot.getBackupSnapshotId() != null && swift == null) {
        if (prevSnapshot.getVersion() != null && prevSnapshot.getVersion().equals("String_Node_Str")) {
          prevBackupUuid=prevSnapshot.getBackupSnapshotId();
          prevSnapshotUuid=prevSnapshot.getPath();
        }
      }
 else       if (prevSnapshot.getSwiftName() != null && swift != null) {
        prevBackupUuid=prevSnapshot.getSwiftName();
        prevSnapshotUuid=prevSnapshot.getPath();
      }
    }
    boolean isVolumeInactive=_storageMgr.volumeInactive(volume);
    String vmName=_storageMgr.getVmNameOnVolume(volume);
    BackupSnapshotCommand backupSnapshotCommand=new BackupSnapshotCommand(primaryStoragePoolNameLabel,secondaryStoragePoolUrl,dcId,accountId,volumeId,snapshot.getId(),volume.getPath(),snapshotUuid,snapshot.getName(),prevSnapshotUuid,prevBackupUuid,isVolumeInactive,vmName);
    if (swift != null) {
      backupSnapshotCommand.setSwift(toSwiftTO(swift));
    }
    String backedUpSnapshotUuid=null;
    boolean backedUp=false;
    BackupSnapshotAnswer answer=(BackupSnapshotAnswer)sendToPool(volume,backupSnapshotCommand);
    if (answer != null && answer.getResult()) {
      backedUpSnapshotUuid=answer.getBackupSnapshotName();
      if (backedUpSnapshotUuid != null) {
        backedUp=true;
      }
    }
 else     if (answer != null) {
      s_logger.error(answer.getDetails());
    }
    Transaction txn=Transaction.currentTxn();
    txn.start();
    if (backedUp) {
      if (backupSnapshotCommand.getSwift() != null) {
        snapshot.setSwiftId(1L);
        snapshot.setSwiftName(backedUpSnapshotUuid);
      }
 else {
        snapshot.setBackupSnapshotId(backedUpSnapshotUuid);
      }
      if (answer.isFull()) {
        snapshot.setPrevSnapshotId(0);
      }
      snapshot.setStatus(Snapshot.Status.BackedUp);
      _snapshotDao.update(snapshotId,snapshot);
      if (snapshot.getType() == Type.RECURRING) {
        _accountMgr.incrementResourceCount(snapshot.getAccountId(),ResourceType.snapshot);
      }
    }
 else {
      s_logger.warn("String_Node_Str");
      UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_SNAPSHOT_DELETE,snapshot.getAccountId(),snapshot.getDataCenterId(),snapshotId,snapshot.getName(),null,null,0L);
      _usageEventDao.persist(usageEvent);
      _snapshotDao.remove(snapshotId);
    }
    txn.commit();
    return backedUp;
  }
  finally {
    if (snapshot != null) {
      _snapshotDao.releaseFromLockTable(snapshotId);
    }
  }
}

Fixed version (ground truth):
@Override @DB public boolean backupSnapshotToSecondaryStorage(SnapshotVO ss){
  long snapshotId=ss.getId();
  SnapshotVO snapshot=_snapshotDao.acquireInLockTable(snapshotId);
  if (snapshot == null) {
    throw new CloudRuntimeException("String_Node_Str" + ss);
  }
  try {
    snapshot.setStatus(Snapshot.Status.BackingUp);
    _snapshotDao.update(snapshot.getId(),snapshot);
    long volumeId=snapshot.getVolumeId();
    VolumeVO volume=_volsDao.lockRow(volumeId,true);
    String primaryStoragePoolNameLabel=_storageMgr.getPrimaryStorageNameLabel(volume);
    Long dcId=volume.getDataCenterId();
    Long accountId=volume.getAccountId();
    HostVO secHost=getSecHost(volumeId,volume.getDataCenterId());
    String secondaryStoragePoolUrl=secHost.getStorageUrl();
    String snapshotUuid=snapshot.getPath();
    SnapshotVO prevSnapshot=null;
    String prevSnapshotUuid=null;
    String prevBackupUuid=null;
    SwiftVO swift=_swiftDao.findById(1L);
    long prevSnapshotId=snapshot.getPrevSnapshotId();
    if (prevSnapshotId > 0) {
      prevSnapshot=_snapshotDao.findByIdIncludingRemoved(prevSnapshotId);
      if (prevSnapshot.getBackupSnapshotId() != null && swift == null) {
        if (prevSnapshot.getVersion() != null && prevSnapshot.getVersion().equals("String_Node_Str")) {
          prevBackupUuid=prevSnapshot.getBackupSnapshotId();
          prevSnapshotUuid=prevSnapshot.getPath();
        }
      }
 else       if (prevSnapshot.getSwiftName() != null && swift != null) {
        prevBackupUuid=prevSnapshot.getSwiftName();
        prevSnapshotUuid=prevSnapshot.getPath();
      }
    }
    boolean isVolumeInactive=_storageMgr.volumeInactive(volume);
    String vmName=_storageMgr.getVmNameOnVolume(volume);
    BackupSnapshotCommand backupSnapshotCommand=new BackupSnapshotCommand(primaryStoragePoolNameLabel,secondaryStoragePoolUrl,dcId,accountId,volumeId,snapshot.getId(),volume.getPath(),snapshotUuid,snapshot.getName(),prevSnapshotUuid,prevBackupUuid,isVolumeInactive,vmName);
    if (swift != null) {
      backupSnapshotCommand.setSwift(toSwiftTO(swift));
    }
    String backedUpSnapshotUuid=null;
    boolean backedUp=false;
    BackupSnapshotAnswer answer=(BackupSnapshotAnswer)sendToPool(volume,backupSnapshotCommand);
    if (answer != null && answer.getResult()) {
      backedUpSnapshotUuid=answer.getBackupSnapshotName();
      if (backedUpSnapshotUuid != null) {
        backedUp=true;
      }
    }
 else     if (answer != null) {
      s_logger.error(answer.getDetails());
    }
    Transaction txn=Transaction.currentTxn();
    txn.start();
    if (backedUp) {
      if (backupSnapshotCommand.getSwift() != null) {
        snapshot.setSwiftId(1L);
        snapshot.setSwiftName(backedUpSnapshotUuid);
      }
 else {
        snapshot.setBackupSnapshotId(backedUpSnapshotUuid);
      }
      if (answer.isFull()) {
        snapshot.setPrevSnapshotId(0);
      }
      snapshot.setStatus(Snapshot.Status.BackedUp);
      _snapshotDao.update(snapshotId,snapshot);
      if (snapshot.isRecursive()) {
        _accountMgr.incrementResourceCount(snapshot.getAccountId(),ResourceType.snapshot);
      }
    }
 else {
      s_logger.warn("String_Node_Str");
      UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_SNAPSHOT_DELETE,snapshot.getAccountId(),snapshot.getDataCenterId(),snapshotId,snapshot.getName(),null,null,0L);
      _usageEventDao.persist(usageEvent);
      _snapshotDao.remove(snapshotId);
    }
    txn.commit();
    return backedUp;
  }
  finally {
    if (snapshot != null) {
      _snapshotDao.releaseFromLockTable(snapshotId);
    }
  }
}