Skip to content

Commit 815a30f

Browse files
authored
(KBOSS) Add synchronous backup compression flag (#13901)
* add sync backup flag * fix * default true * address reviews * minor fix * Update api/src/main/java/org/apache/cloudstack/api/command/user/backup/CreateBackupOfferingCmd.java
1 parent cc30155 commit 815a30f

13 files changed

Lines changed: 149 additions & 52 deletions

File tree

‎api/src/main/java/org/apache/cloudstack/api/ApiConstants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,7 @@ public class ApiConstants {
15381538
public static final String SCHEDULED = "scheduled";
15391539
public static final String SCHEDULED_DATE = "scheduleddate";
15401540
public static final String BACKUP_PROVIDER = "backupprovider";
1541+
public static final String COMPRESS_ASYNC = "compressasync";
15411542

15421543
/**
15431544
* This enum specifies IO Drivers, each option controls specific policies on I/O.

‎api/src/main/java/org/apache/cloudstack/api/command/user/backup/CreateBackupOfferingCmd.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.apache.cloudstack.backup.Backup;
3535
import org.apache.cloudstack.backup.BackupManager;
3636
import org.apache.cloudstack.backup.BackupOffering;
37-
37+
import org.apache.commons.lang3.BooleanUtils;
3838

3939
import javax.inject.Inject;
4040
import java.util.List;
@@ -92,6 +92,10 @@ public class CreateBackupOfferingCmd extends BaseCmd {
9292
description = "Restrict the backup offering to the Domains identified by these IDs.")
9393
private List<Long> domainIds;
9494

95+
@Parameter(name = ApiConstants.COMPRESS_ASYNC, type = CommandType.BOOLEAN, description = "Set to false to compress synchronously during backup creation; conversely, set to true to compress asynchronously later. " +
96+
"Default true.")
97+
private Boolean compressAsync;
98+
9599
/////////////////////////////////////////////////////
96100
/////////////////// Accessors ///////////////////////
97101
/////////////////////////////////////////////////////
@@ -166,6 +170,10 @@ public Boolean getUserDrivenBackups() {
166170
return userDrivenBackups;
167171
}
168172

173+
public boolean isCompressAsync() {
174+
return BooleanUtils.toBooleanDefaultIfNull(this.compressAsync, true);
175+
}
176+
169177
/////////////////////////////////////////////////////
170178
/////////////// API Implementation///////////////////
171179
/////////////////////////////////////////////////////

‎core/src/main/java/org/apache/cloudstack/backup/TakeKbossBackupCommand.java‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ public class TakeKbossBackupCommand extends Command {
4242

4343
private boolean isolated;
4444

45-
public TakeKbossBackupCommand(boolean quiesceVm, boolean runningVM, boolean endChain, String vmName, String imageStoreUrl, List<String> backupChainImageStoreUrls, List<KbossTO> kbossTOS, boolean isolated) {
45+
private boolean compress;
46+
47+
private Backup.CompressionLibrary compressionLib;
48+
49+
private Integer coroutines;
50+
51+
private Integer rateLimit;
52+
53+
public TakeKbossBackupCommand(boolean quiesceVm, boolean runningVM, boolean endChain, String vmName, String imageStoreUrl, List<String> backupChainImageStoreUrls,
54+
List<KbossTO> kbossTOS, boolean isolated) {
4655
this.quiesceVm = quiesceVm;
4756
this.runningVM = runningVM;
4857
this.endChain = endChain;
@@ -85,6 +94,38 @@ public boolean isIsolated() {
8594
return isolated;
8695
}
8796

97+
public void setRateLimit(Integer rateLimit) {
98+
this.rateLimit = rateLimit;
99+
}
100+
101+
public void setCoroutines(Integer coroutines) {
102+
this.coroutines = coroutines;
103+
}
104+
105+
public void setCompressionLib(Backup.CompressionLibrary compressionLib) {
106+
this.compressionLib = compressionLib;
107+
}
108+
109+
public void setCompress(boolean compress) {
110+
this.compress = compress;
111+
}
112+
113+
public boolean isCompress() {
114+
return compress;
115+
}
116+
117+
public Backup.CompressionLibrary getCompressionLib() {
118+
return compressionLib;
119+
}
120+
121+
public Integer getCoroutines() {
122+
return coroutines;
123+
}
124+
125+
public Integer getRateLimit() {
126+
return rateLimit;
127+
}
128+
88129
@Override
89130
public boolean executeInSequence() {
90131
return false;

‎plugins/backup/kboss/src/main/java/org/apache/cloudstack/backup/KbossBackupProvider.java‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,13 @@ public Pair<Boolean, Long> orchestrateTakeBackup(Backup backup, boolean quiesceV
448448
parentBackupDeltasOnPrimary, volumeUuidToDeltaPrimaryRef, volumeUuidToDeltaSecondaryRef, succeedingVmSnapshot, kbossTO);
449449
}
450450

451+
boolean supportsCompression = offeringSupportsCompression(newBackupJoin);
452+
451453
TakeKbossBackupCommand command = new TakeKbossBackupCommand(quiesceVm, runningVm, newBackupJoin.getEndOfChain(), userVm.getInstanceName(), imageStore.getUri(),
452454
chainImageStoreUrls, kbossTOs, isolated);
453455

456+
boolean compressNow = checkSyncCompressionAndConfigureCommand(backupOfferingVO, supportsCompression, command, hostVO);
457+
454458
Answer answer = sendBackupCommand(hostId, command);
455459

456460
if (answer == null || !answer.getResult()) {
@@ -459,20 +463,34 @@ public Pair<Boolean, Long> orchestrateTakeBackup(Backup backup, boolean quiesceV
459463
}
460464

461465
processBackupSuccess(runningVm, volumeTOs, volumeUuidToDeltaPrimaryRef, volumeUuidToDeltaSecondaryRef, (TakeKbossBackupAnswer)answer, parentBackupDeltasOnPrimary,
462-
succeedingVmSnapshotList, backupVO, fullBackup, userVm, hostId, newBackupJoin.getEndOfChain(), isolated);
466+
succeedingVmSnapshotList, backupVO, fullBackup, userVm, hostId, newBackupJoin.getEndOfChain(), isolated, compressNow);
463467

464468
if (!isolated) {
465469
updateCurrentBackup(newBackupJoin);
466470
}
467471

468-
if (offeringSupportsCompression(newBackupJoin)) {
472+
if (supportsCompression && !compressNow) {
469473
compressBackupAsync(newBackupJoin, backup.getZoneId(), userVm.getAccountId());
470474
} else {
471475
validateBackupAsyncIfHasOfferingSupport(newBackupJoin, backup.getZoneId(), userVm.getAccountId());
472476
}
473477
return new Pair<>(Boolean.TRUE, backupVO.getId());
474478
}
475479

480+
protected boolean checkSyncCompressionAndConfigureCommand(BackupOfferingVO backupOfferingVO, boolean supportsCompression, TakeKbossBackupCommand command, HostVO hostVO) {
481+
BackupOfferingDetailsVO compressAsync = backupOfferingDetailsDao.findDetail(backupOfferingVO.getId(), ApiConstants.COMPRESS_ASYNC);
482+
boolean compressNow = compressAsync != null && !Boolean.parseBoolean(compressAsync.getValue()) && supportsCompression;
483+
484+
if (compressNow) {
485+
BackupOfferingDetailsVO detail = backupOfferingDetailsDao.findDetail(backupOfferingVO.getId(), ApiConstants.COMPRESSION_LIBRARY);
486+
command.setCompress(true);
487+
command.setCompressionLib(detail == null ? Backup.CompressionLibrary.zstd : Backup.CompressionLibrary.valueOf(detail.getValue()));
488+
command.setCoroutines(backupCompressionCoroutines.valueIn(hostVO.getClusterId()));
489+
command.setRateLimit(backupCompressionRateLimit.valueIn(hostVO.getClusterId()));
490+
}
491+
return compressNow;
492+
}
493+
476494
@Override
477495
public boolean deleteBackup(Backup backup, boolean forced) {
478496
logger.debug("Queueing backup [{}] deletion.", backup.getUuid());
@@ -782,7 +800,7 @@ public boolean startBackupCompression(long backupId, long hostId) {
782800
BackupOfferingDetailsVO detail = backupOfferingDetailsDao.findDetail(backupOfferingVO.getId(), ApiConstants.COMPRESSION_LIBRARY);
783801
List<InternalBackupJoinVO> backupChain = getBackupJoinParents(backupVO, true);
784802
List<String> chainImageStoreUrls = getChainImageStoreUrls(backupChain);
785-
CompressBackupCommand cmd = new CompressBackupCommand(deltasToCompressAndParents, chainImageStoreUrls, minFreeStorage, detail == null ? null :
803+
CompressBackupCommand cmd = new CompressBackupCommand(deltasToCompressAndParents, chainImageStoreUrls, minFreeStorage, detail == null ? Backup.CompressionLibrary.zstd :
786804
Backup.CompressionLibrary.valueOf(detail.getValue()), backupCompressionCoroutines.valueIn(hostVO.getClusterId()),
787805
backupCompressionRateLimit.valueIn(hostVO.getClusterId()));
788806
cmd.setWait(backupCompressionTimeout.valueIn(hostVO.getClusterId()));
@@ -2084,7 +2102,8 @@ protected List<VolumeObjectTO> getVolumesThatAreNotPartOfTheBackup(List<VolumeOb
20842102

20852103
protected void processBackupSuccess(boolean runningVm, List<VolumeObjectTO> volumeTOs, HashMap<String, InternalBackupStoragePoolVO> volumeUuidToDeltaPrimaryRef,
20862104
HashMap<String, InternalBackupDataStoreVO> volumeUuidToDeltaSecondaryRef, TakeKbossBackupAnswer answer, List<InternalBackupStoragePoolVO> parentBackupDeltasOnPrimary,
2087-
List<VMSnapshotVO> succeedingVmSnapshots, BackupVO backupVO, boolean fullBackup, VirtualMachine userVm, Long hostId, boolean endChain, boolean isolated) {
2105+
List<VMSnapshotVO> succeedingVmSnapshots, BackupVO backupVO, boolean fullBackup, VirtualMachine userVm, Long hostId, boolean endChain, boolean isolated,
2106+
boolean compressNow) {
20882107
long physicalBackupSize = 0;
20892108
logger.debug("Processing backup [{}] success.", backupVO.getUuid());
20902109
for (VolumeObjectTO volumeObjectTO : volumeTOs) {
@@ -2094,6 +2113,9 @@ protected void processBackupSuccess(boolean runningVm, List<VolumeObjectTO> volu
20942113

20952114
expungeOldDeltasAndUpdateVmSnapshotIfNeeded(parentBackupDeltasOnPrimary, succeedingVmSnapshots.isEmpty() ? null : succeedingVmSnapshots.get(0));
20962115

2116+
if (compressNow) {
2117+
backupVO.setCompressionStatus(Backup.CompressionStatus.Compressed);
2118+
}
20972119
backupVO.setSize(physicalBackupSize);
20982120
backupVO.setStatus(Backup.Status.BackedUp);
20992121
backupVO.setBackedUpVolumes(backupManager.createVolumeInfoFromVolumes(new ArrayList<>(volumeDao.findByInstance(userVm.getId()))));

‎plugins/backup/kboss/src/test/java/org/apache/cloudstack/backup/KbossBackupProviderTest.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,8 @@ public void orchestrateTakeBackupTestIsolatedBackupFailed() {
787787
doNothing().when(kbossBackupProviderSpy).validateStorages(any(), any());
788788
doReturn(internalBackupJoinVoMock).when(internalBackupJoinDaoMock).findById(any());
789789
doReturn(dataStoreMock).when(kbossBackupProviderSpy).getImageStoreForBackup(any(), any());
790+
doReturn(false).when(kbossBackupProviderSpy).offeringSupportsCompression(any());
791+
doReturn(false).when(kbossBackupProviderSpy).checkSyncCompressionAndConfigureCommand(any(), anyBoolean(), any(), any());
790792

791793
Pair<Boolean, Long> result = kbossBackupProviderSpy.orchestrateTakeBackup(backupVoMock, false, true);
792794
assertFalse(result.first());
@@ -815,17 +817,18 @@ public void orchestrateTakeBackupTestIsolatedBackupSuccessWithCompression() {
815817
doReturn(takeKbossBackupAnswerMock).when(kbossBackupProviderSpy).sendBackupCommand(anyLong(), any());
816818
doReturn(true).when(takeKbossBackupAnswerMock).getResult();
817819
doNothing().when(kbossBackupProviderSpy).processBackupSuccess(anyBoolean(), any(), any(), any(), any(), any(), any(), any(), anyBoolean(), any(),
818-
anyLong(), anyBoolean(), anyBoolean());
819-
doReturn(true).when(kbossBackupProviderSpy).offeringSupportsCompression(internalBackupJoinVoMock);
820+
anyLong(), anyBoolean(), anyBoolean(), anyBoolean());
820821
doNothing().when(kbossBackupProviderSpy).compressBackupAsync(internalBackupJoinVoMock, 0, 0);
822+
doReturn(true).when(kbossBackupProviderSpy).offeringSupportsCompression(any());
823+
doReturn(false).when(kbossBackupProviderSpy).checkSyncCompressionAndConfigureCommand(any(), anyBoolean(), any(), any());
821824

822825
Pair<Boolean, Long> result = kbossBackupProviderSpy.orchestrateTakeBackup(backupVoMock, false, true);
823826
assertTrue(result.first());
824827
assertEquals(backupId, result.second());
825828
verify(kbossBackupProviderSpy, Mockito.times(1)).setBackupAsIsolated(backupVoMock);
826829
verify(kbossBackupProviderSpy, Mockito.times(2)).createDeltaReferences(Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean(), any(), any(), any(), any(), any(), any(), any());
827830
verify(kbossBackupProviderSpy, Mockito.times(1)).processBackupSuccess(anyBoolean(), any(), any(), any(), any(), any(), any(), any(), anyBoolean(), any(),
828-
anyLong(), anyBoolean(), anyBoolean());
831+
anyLong(), anyBoolean(), anyBoolean(), anyBoolean());
829832
verify(kbossBackupProviderSpy, Mockito.times(1)).compressBackupAsync(internalBackupJoinVoMock, 0, 0);
830833
}
831834

@@ -850,9 +853,10 @@ public void orchestrateTakeBackupTestBackupSuccessWithValidation() {
850853
doReturn(takeKbossBackupAnswerMock).when(kbossBackupProviderSpy).sendBackupCommand(anyLong(), any());
851854
doReturn(true).when(takeKbossBackupAnswerMock).getResult();
852855
doNothing().when(kbossBackupProviderSpy).processBackupSuccess(anyBoolean(), any(), any(), any(), any(), any(), any(), any(), anyBoolean(), any(),
853-
anyLong(), anyBoolean(), anyBoolean());
854-
doReturn(false).when(kbossBackupProviderSpy).offeringSupportsCompression(internalBackupJoinVoMock);
856+
anyLong(), anyBoolean(), anyBoolean(), anyBoolean());
855857
doNothing().when(kbossBackupProviderSpy).validateBackupAsyncIfHasOfferingSupport(any(), anyLong(), anyLong());
858+
doReturn(false).when(kbossBackupProviderSpy).offeringSupportsCompression(any());
859+
doReturn(false).when(kbossBackupProviderSpy).checkSyncCompressionAndConfigureCommand(any(), anyBoolean(), any(), any());
856860

857861
Pair<Boolean, Long> result = kbossBackupProviderSpy.orchestrateTakeBackup(backupVoMock, false, false);
858862
assertTrue(result.first());
@@ -861,7 +865,7 @@ public void orchestrateTakeBackupTestBackupSuccessWithValidation() {
861865
verify(internalBackupDataStoreDaoMock).listByBackupId(0);
862866
verify(kbossBackupProviderSpy, Mockito.times(2)).createDeltaReferences(Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean(), any(), any(), any(), any(), any(), any(), any());
863867
verify(kbossBackupProviderSpy, Mockito.times(1)).processBackupSuccess(anyBoolean(), any(), any(), any(), any(), any(), any(), any(), anyBoolean(), any(),
864-
anyLong(), anyBoolean(), anyBoolean());
868+
anyLong(), anyBoolean(), anyBoolean(), anyBoolean());
865869
verify(kbossBackupProviderSpy, Mockito.times(1)).validateBackupAsyncIfHasOfferingSupport(internalBackupJoinVoMock, 0, 0);
866870
}
867871

‎plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCompressBackupCommandWrapper.java‎

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
2525
import com.cloud.resource.CommandWrapper;
2626
import com.cloud.resource.ResourceWrapper;
27-
import com.cloud.utils.exception.CloudRuntimeException;
2827
import org.apache.cloudstack.backup.Backup;
2928
import org.apache.cloudstack.backup.CompressBackupCommand;
3029
import org.apache.cloudstack.storage.formatinspector.Qcow2Inspector;
@@ -54,7 +53,7 @@ public Answer execute(CompressBackupCommand command, LibvirtComputingResource se
5453
logger.info("Starting compression for backup deltas [{}].", deltas);
5554
try {
5655
QemuImg qemuImg = new QemuImg(command.getWait() * 1000);
57-
Integer rateLimit = validateAndGetRateLimit(command, qemuImg);
56+
Integer rateLimit = qemuImg.validateAndGetRateLimit(command.getRateLimit());
5857

5958
KVMStoragePool mainSecStorage = storagePoolManager.getStoragePoolByURI(deltas.stream().findFirst().orElseThrow().getChild().getDataStore().getUrl());
6059
secondaryStorages.add(mainSecStorage);
@@ -82,7 +81,7 @@ public Answer execute(CompressBackupCommand command, LibvirtComputingResource se
8281

8382
HashMap<String, String> options = new HashMap<>();
8483
Backup.CompressionLibrary compressionLib = getCompressionLibrary(command, fullDeltaPath);
85-
setCompressionTypeOptionIfAvailable(qemuImg, options, compressionLib);
84+
qemuImg.setCompressionTypeOptionIfAvailable(options, compressionLib);
8685
int coroutines = command.getCoroutines();
8786
logger.info("Starting compression for backup delta [{}] with parent [{}] using [{}] coroutines.", child, parent, coroutines);
8887
qemuImg.convert(originalBackup, compressedBackup, backingFile, options, null, new QemuImageOptions(originalBackup.getFormat(), originalBackup.getFileName(),
@@ -99,30 +98,6 @@ public Answer execute(CompressBackupCommand command, LibvirtComputingResource se
9998
return new Answer(command);
10099
}
101100

102-
private Integer validateAndGetRateLimit(CompressBackupCommand command, QemuImg qemuImg) {
103-
if (command.getRateLimit() < 1) {
104-
return null;
105-
}
106-
107-
if (qemuImg.getVersion() < QemuImg.QEMU_5_2) {
108-
throw new CloudRuntimeException("Qemu version is lower than 5.2.0, unable to set the rate limit.");
109-
}
110-
111-
return command.getRateLimit();
112-
}
113-
114-
/**
115-
* Sets the compression type option if qemu-img is at least in version 5.1. Otherwise, will not set it and qemu will use zlib.
116-
* */
117-
private void setCompressionTypeOptionIfAvailable(QemuImg qemuImg, HashMap<String, String> options, Backup.CompressionLibrary compressionLib) {
118-
if (qemuImg.getVersion() >= QemuImg.QEMU_5_1) {
119-
options.put(COMPRESSION_TYPE, compressionLib.name());
120-
return;
121-
}
122-
logger.warn("Qemu is at a lower version than 5.1, we will not be able to use zstd to compress backups. Only zlib is supported for this version. Current version is [{}].",
123-
qemuImg.getVersion());
124-
}
125-
126101
private Backup.CompressionLibrary getCompressionLibrary(CompressBackupCommand command, String fullDeltaPath) {
127102
Backup.CompressionLibrary compressionLib = command.getCompressionLib();
128103
if (compressionLib == Backup.CompressionLibrary.zlib || !Qcow2Inspector.validateQcow2Version(fullDeltaPath, MIN_QCOW_2_VERSION_FOR_ZSTD)) {

0 commit comments

Comments
 (0)