Skip to content

Commit aca8234

Browse files
committed
CLOUDSTACK-10222: Clean previous snaphosts from primary storage when taking new one
When user creates a snapshot (manual or recurring), snapshot remains on the primary storage, even if the snapshot is transferred successfully to secondary storage. This is causing issues because XenServer can only hold a limited number of snapshots in its VDI chain, preventing the user from creating new snapshots after some time, when too many old snapshots are present on the primary storage.
1 parent c9afa8e commit aca8234

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/XenServerStorageProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ protected boolean destroySnapshotOnPrimaryStorageExceptThis(final Connection con
10651065
return false;
10661066
}
10671067

1068-
private boolean destroySnapshotOnPrimaryStorage(final Connection conn, final String lastSnapshotUuid) {
1068+
protected boolean destroySnapshotOnPrimaryStorage(final Connection conn, final String lastSnapshotUuid) {
10691069
try {
10701070
final VDI snapshot = getVDIbyUuid(conn, lastSnapshotUuid);
10711071
if (snapshot == null) {

plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/Xenserver625StorageProcessor.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ public Answer backupSnapshot(final CopyCommand cmd) {
423423
String snapshotBackupUuid = null;
424424
boolean fullbackup = Boolean.parseBoolean(options.get("fullSnapshot"));
425425
Long physicalSize = null;
426+
boolean successful = false;
427+
426428
try {
427429

428430
SR primaryStorageSR = null;
@@ -559,12 +561,12 @@ public Answer backupSnapshot(final CopyCommand cmd) {
559561
physicalSize = Long.parseLong(tmp[1]);
560562
finalPath = folder + File.separator + snapshotBackupUuid + ".vhd";
561563
}
562-
563-
final String volumeUuid = snapshotTO.getVolume().getPath();
564-
565-
destroySnapshotOnPrimaryStorageExceptThis(conn, volumeUuid, snapshotUuid);
566564
}
567565

566+
// remove every snapshot except this one from primary storage
567+
final String volumeUuid = snapshotTO.getVolume().getPath();
568+
destroySnapshotOnPrimaryStorageExceptThis(conn, volumeUuid, snapshotUuid);
569+
568570
final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
569571
newSnapshot.setPath(finalPath);
570572
newSnapshot.setPhysicalSize(physicalSize);
@@ -576,13 +578,20 @@ public Answer backupSnapshot(final CopyCommand cmd) {
576578
s_logger.info("New snapshot details: " + newSnapshot.toString());
577579
s_logger.info("New snapshot physical utilization: "+physicalSize);
578580

581+
successful = true;
582+
579583
return new CopyCmdAnswer(newSnapshot);
580584
} catch (final Types.XenAPIException e) {
581585
details = "BackupSnapshot Failed due to " + e.toString();
582586
s_logger.warn(details, e);
583587
} catch (final Exception e) {
584588
details = "BackupSnapshot Failed due to " + e.getMessage();
585589
s_logger.warn(details, e);
590+
} finally {
591+
if (!successful) {
592+
// remove last bad primary snapshot when exception happens
593+
destroySnapshotOnPrimaryStorage(conn, snapshotUuid);
594+
}
586595
}
587596

588597
return new CopyCmdAnswer(details);

0 commit comments

Comments
 (0)