Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -503,18 +503,26 @@ private CreateCmdResult createTemplateOnPrimary(StoragePoolVO storagePool, Templ
}

/**
* Persists SAN cache identity on {@code template_spool_ref} when a LUN was created.
* NFS returns the template uuid as the create path; {@code install_path} is filled later.
* Persists the template virtual size on {@code template_spool_ref}.
* iSCSI also stores the cache LUN uuid in {@code local_download_path}.
* NFS has no LUN; {@code install_path} is filled later by the KVM copy.
*
* <p>The size is written here, before that copy, because the copy answer reports the
* qcow2's allocated size. {@code TemplateObject} stores that answer only while
* {@code template_size} is still 0.</p>
*/
private String recordTemplateCacheOnSpoolRef(StoragePoolVO storagePool, TemplateInfo templateInfo,
CloudStackVolume created, long sizeInBytes) {
VMTemplateStoragePoolVO templatePoolRef = findTemplatePoolRef(storagePool.getId(), templateInfo.getId());
if (sizeInBytes > 0) {
templatePoolRef.setTemplateSize(sizeInBytes);
}
if (created == null || created.getLun() == null) {
vmTemplatePoolDao.update(templatePoolRef.getId(), templatePoolRef);
return templateInfo.getUuid();
}
Lun lun = created.getLun();
VMTemplateStoragePoolVO templatePoolRef = findTemplatePoolRef(storagePool.getId(), templateInfo.getId());
templatePoolRef.setLocalDownloadPath(lun.getUuid());
templatePoolRef.setTemplateSize(sizeInBytes);
vmTemplatePoolDao.update(templatePoolRef.getId(), templatePoolRef);
return lun.getName();
}
Expand Down Expand Up @@ -554,9 +562,13 @@ private CloudStackVolume cloneCloudStackVolumeFromTemplate(StoragePoolVO storage
templateId, volumeInfo.getId(), storagePool.getId());

long requestedSize = getDataObjectSizeIncludingHypervisorSnapshotReserve(volumeInfo, storagePool);
if (requestedSize > templatePoolRef.getTemplateSize()) {
long templateSize = templatePoolRef.getTemplateSize();
if (requestedSize > templateSize) {
logger.info("cloneCloudStackVolumeFromTemplate: Growing clone of template [{}] from {} to {} bytes for volume [{}]",
templateId, templatePoolRef.getTemplateSize(), requestedSize, volumeInfo.getId());
templateId, templateSize, requestedSize, volumeInfo.getId());
if (templateSize > 0) {
cloned.setCurrentSize(templateSize);
}
resizeClonedVolumeOrCleanup(storageStrategy, cloned, requestedSize,
"cloneCloudStackVolumeFromTemplate template [" + templateId + "] volume [" + volumeInfo.getId() + "]");
}
Expand Down Expand Up @@ -684,6 +696,7 @@ private CloudStackVolume cloneCloudStackVolumeFromSnapshot(StoragePoolVO storage
if (snapshotSize > 0 && requestedSize > snapshotSize) {
logger.info("cloneCloudStackVolumeFromSnapshot: Growing clone of snapshot [{}] from {} to {} bytes for volume [{}]",
csSnapshotId, snapshotSize, requestedSize, volumeInfo.getId());
cloned.setCurrentSize(snapshotSize);
resizeClonedVolumeOrCleanup(storageStrategy, cloned, requestedSize,
"cloneCloudStackVolumeFromSnapshot snapshot [" + csSnapshotId + "] volume [" + volumeInfo.getId() + "]");
}
Expand All @@ -707,7 +720,7 @@ private CloudStackVolume cloneCloudStackVolumeFromSnapshot(StoragePoolVO storage

/**
* Grows a just-created ONTAP clone to the requested size. If resize fails, deletes the clone
* so the CloudStack create-failure path does not leave an orphaned LUN/file on the array.
* so the CloudStack create-failure path does not leave an orphaned LUN or file on the array.
*/
private void resizeClonedVolumeOrCleanup(StorageStrategy storageStrategy, CloudStackVolume cloned,
long requestedSize, String context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,11 @@ abstract public CloudStackVolume copyCloudStackVolume(VolumeInfo srcVolumeInfo,
* Grows an existing backend object to {@code sizeInBytes}.
*
* <p>Needed after cloning a cached template, because a clone inherits the size of its source
* while the service offering may ask for a larger disk.</p>
* while the disk offering may ask for a larger disk. The volume row already holds that
* offering size, so a clone sets {@link org.apache.cloudstack.storage.service.model.CloudStackVolume#setCurrentSize}
* to the cloned file's virtual size before this call. NFS uses that value as the current
* size. A normal resize leaves it unset and the volume row still holds the current size.
* iSCSI resizes the LUN to {@code sizeInBytes}.</p>
*
* <p>When the request already carries a QoS policy on the LUN or file, that policy is applied
* as part of this resize. iSCSI sends one LUN update with the new size and the policy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public void resizeCloudStackVolume(CloudStackVolume cloudstackVolume, long sizeI
}

DataObject volumeInfo = cloudstackVolume.getVolumeInfo();
Answer answer = resizeVolumeOnKVMHost(volumeInfo, sizeInBytes);
Answer answer = resizeVolumeOnKVMHost(cloudstackVolume, sizeInBytes);
if (answer == null || !answer.getResult()) {
String errMsg = answer != null ? answer.getDetails() : "Failed to resize qcow2 on KVM host";
logger.error("resizeCloudStackVolume: " + errMsg);
Expand All @@ -263,7 +263,8 @@ public void resizeCloudStackVolume(CloudStackVolume cloudstackVolume, long sizeI
logger.info("resizeCloudStackVolume: Resized volume [{}] to {} bytes", volumeInfo.getUuid(), sizeInBytes);
}

private Answer resizeVolumeOnKVMHost(DataObject volumeInfo, long sizeInBytes) {
private Answer resizeVolumeOnKVMHost(CloudStackVolume cloudstackVolume, long sizeInBytes) {
DataObject volumeInfo = cloudstackVolume.getVolumeInfo();
VolumeObject volumeObject = (VolumeObject) volumeInfo;
VolumeVO volume = volumeDao.findById(volumeObject.getId());
if (volume == null) {
Expand All @@ -278,11 +279,15 @@ private Answer resizeVolumeOnKVMHost(DataObject volumeInfo, long sizeInBytes) {
// instanceName is set by VolumeApiServiceImpl.orchestrateResizeVolume() before calling the
// driver — it is the VM instance name when attached, or "none" when the volume is detached.
ResizeVolumePayload resizePayload = volumeObject.getpayload() instanceof ResizeVolumePayload
? (ResizeVolumePayload) volumeObject.getpayload()
: null;
String instanceName = resizePayload != null ? resizePayload.instanceName : "none";
ResizeVolumeCommand cmd = new ResizeVolumeCommand(volume.getPath(), new StorageFilerTO(storagePool),
volume.getSize(), sizeInBytes, false, instanceName);
? (ResizeVolumePayload) volumeObject.getpayload()
: null;
String instanceName = resizePayload != null ? resizePayload.instanceName : "none";
// A clone's volume row is already the offering size. Use the cloned file's virtual size
// when the driver recorded it, so the agent does not see current == new and skip the grow.
Long clonedFileSize = cloudstackVolume.getCurrentSize();
long currentSize = clonedFileSize != null && clonedFileSize > 0 ? clonedFileSize : volume.getSize();
ResizeVolumeCommand cmd = new ResizeVolumeCommand(volume.getPath(), new StorageFilerTO(storagePool),
currentSize, sizeInBytes, false, instanceName);

EndPoint ep = epSelector.select(volumeInfo);
if (ep == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public class CloudStackVolume {

private DataObject volumeInfo; // This is needed as we need DataObject to be passed to agent to create volume

/**
* Virtual size of a qcow2 that was just cloned, when the volume row already holds the
* disk offering size. NFS resize uses this as the current size so the KVM agent grows the
* file. Null on a normal resize, where the volume row still holds the current size.
*/
private Long currentSize;

public FileInfo getFile() {
return file;
}
Expand Down Expand Up @@ -101,4 +108,12 @@ public void setDestinationPath(String destinationPath) {
this.destinationPath = destinationPath;
}

public Long getCurrentSize() {
return currentSize;
}

public void setCurrentSize(Long currentSize) {
this.currentSize = currentSize;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1465,16 +1465,21 @@ void testCreateAsync_TemplateWithISCSI_UnknownSize_Fails() {
}

@Test
void testCreateAsync_TemplateWithNFS_IsMetadataOnly() {
void testCreateAsync_TemplateWithNFS_RecordsVirtualSize() {
storagePoolDetails.put(OntapStorageConstants.PROTOCOL, ProtocolType.NFS3.name());

when(dataStore.getId()).thenReturn(1L);
when(dataStore.getName()).thenReturn("ontap-pool");
when(templateInfo.getType()).thenReturn(TEMPLATE);
when(templateInfo.getId()).thenReturn(50L);
when(templateInfo.getUuid()).thenReturn("template-uuid");
when(templateInfo.getSize()).thenReturn(3758096384L);

when(storagePoolDao.findById(1L)).thenReturn(storagePool);
when(storagePool.getId()).thenReturn(1L);
when(storagePoolDetailsDao.listDetailsKeyPairs(1L)).thenReturn(storagePoolDetails);
when(vmTemplatePoolDao.findByPoolTemplate(1L, 50L, null)).thenReturn(templatePoolRef);
when(templatePoolRef.getId()).thenReturn(7L);

try (MockedStatic<OntapStorageUtils> utilityMock = mockStatic(OntapStorageUtils.class, CALLS_REAL_METHODS)) {
utilityMock.when(() -> OntapStorageUtils.getStrategyByStoragePoolDetails(any())).thenReturn(nasStrategy);
Expand All @@ -1485,7 +1490,9 @@ void testCreateAsync_TemplateWithNFS_IsMetadataOnly() {
ArgumentCaptor<CreateCmdResult> resultCaptor = ArgumentCaptor.forClass(CreateCmdResult.class);
verify(createCallback).complete(resultCaptor.capture());
assertTrue(resultCaptor.getValue().isSuccess());
verify(vmTemplatePoolDao, never()).update(any(Long.class), any(VMTemplateStoragePoolVO.class));
verify(templatePoolRef).setTemplateSize(3758096384L);
verify(templatePoolRef, never()).setLocalDownloadPath(any());
verify(vmTemplatePoolDao).update(7L, templatePoolRef);
}
}

Expand Down Expand Up @@ -1538,6 +1545,7 @@ void testCreateAsync_VolumeClonedFromTemplate_GrowsWhenOfferingIsLarger() {
driver.createAsync(dataStore, volumeInfo, createCallback);

verify(sanStrategy).resizeCloudStackVolume(eq(cloned), eq(21474836480L));
assertEquals(5368709120L, cloned.getCurrentSize());
}
}

Expand Down Expand Up @@ -1722,6 +1730,7 @@ void testCreateAsync_VolumeClonedFromSnapshot_GrowsWhenOfferingIsLarger() {
driver.createAsync(dataStore, volumeInfo, createCallback);

verify(sanStrategy).resizeCloudStackVolume(eq(cloned), eq(21474836480L));
assertEquals(5368709120L, cloned.getCurrentSize());
}
}

Expand Down Expand Up @@ -1923,6 +1932,7 @@ void testCreateAsync_VolumeClonedFromSnapshot_NfsGrowsWhenOfferingIsLarger() {
driver.createAsync(dataStore, volumeInfo, createCallback);

verify(nasStrategy).resizeCloudStackVolume(eq(cloned), eq(21474836480L));
assertEquals(5368709120L, cloned.getCurrentSize());
verify(sanStrategy, never()).cloneCloudStackVolume(any());
}
}
Expand Down Expand Up @@ -6273,15 +6283,17 @@ void createIscsiTemplateCacheRecordsLunUuidAndSizeAndReturnsLunName() {
}

@Test
void createNfsTemplateCacheReturnsTemplateUuidAndLeavesSpoolRefUntouched() {
void createNfsTemplateCacheRecordsVirtualSizeAndReturnsTemplateUuid() {
givenNfs();
when(nasStrategy.createTemplateCache(any(), any(), anyMap(), anyLong())).thenReturn(new CloudStackVolume());

CreateCmdResult result = create(nasStrategy, templateInfo);

assertTrue(result.isSuccess());
assertEquals("template-uuid", result.getPath());
verify(vmTemplatePoolDao, never()).update(anyLong(), any(VMTemplateStoragePoolVO.class));
verify(templatePoolRef).setTemplateSize(4096L);
verify(templatePoolRef, never()).setLocalDownloadPath(any());
verify(vmTemplatePoolDao).update(900L, templatePoolRef);
}

@Test
Expand All @@ -6293,7 +6305,8 @@ void createTemplateCacheReturnsTemplateUuidWhenStrategyReturnsNothing() {
assertTrue(result.isSuccess());
assertEquals("template-uuid", result.getPath());
verify(sanStrategy).createTemplateCache(eq(storagePool), eq(templateInfo), eq(poolDetails), eq(4096L));
verify(vmTemplatePoolDao, never()).update(anyLong(), any(VMTemplateStoragePoolVO.class));
verify(templatePoolRef).setTemplateSize(4096L);
verify(vmTemplatePoolDao).update(900L, templatePoolRef);
}

// ---------------------------------------------------------------- required template size
Expand Down Expand Up @@ -6361,6 +6374,7 @@ void cloneFromTemplateGrowsTheCloneOnlyWhenVolumeIsLargerThanTheCachedTemplate()
assertTrue(create(sanStrategy, volumeInfo).isSuccess());

verify(sanStrategy).resizeCloudStackVolume(cloned, ONE_GB);
assertEquals(ONE_GB - 1, cloned.getCurrentSize());
}

@Test
Expand Down Expand Up @@ -6482,6 +6496,7 @@ void cloneFromSnapshotGrowsOnlyWhenSnapshotIsKnownAndSmallerThanVolume() {
givenCloneOfSnapshot("ISCSI", "/vol/flexvol/lun1", ONE_GB - 1);
assertTrue(create(sanStrategy, volumeInfo).isSuccess());
verify(sanStrategy).resizeCloudStackVolume(cloned, ONE_GB);
assertEquals(ONE_GB - 1, cloned.getCurrentSize());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,35 @@ public void testResizeCloudStackVolume_SendsResizeCommand() {
verify(nasFeignClient, never()).updateFile(anyString(), anyString(), anyString(), any(FileInfo.class));
}

@Test
public void testResizeCloudStackVolume_UsesClonedFileSizeAsCurrentSize() {
VolumeObject volumeObject = mock(VolumeObject.class);
VolumeVO volumeVO = mock(VolumeVO.class);
StoragePoolVO storagePool = mock(StoragePoolVO.class);
EndPoint endPoint = mock(EndPoint.class);

when(volumeObject.getId()).thenReturn(100L);
when(volumeObject.getUuid()).thenReturn("volume-uuid");
when(volumeDao.findById(100L)).thenReturn(volumeVO);
when(volumeVO.getPath()).thenReturn("volume-uuid");
when(volumeVO.getSize()).thenReturn(8589934592L);
when(volumeVO.getPoolId()).thenReturn(1L);
when(primaryDataStoreDao.findById(1L)).thenReturn(storagePool);
when(epSelector.select(volumeObject)).thenReturn(endPoint);
when(endPoint.sendMessage(any(ResizeVolumeCommand.class))).thenReturn(new Answer(null, true, "Success"));

CloudStackVolume request = new CloudStackVolume();
request.setVolumeInfo(volumeObject);
request.setCurrentSize(3758096384L);

strategy.resizeCloudStackVolume(request, 8589934592L);

ArgumentCaptor<ResizeVolumeCommand> commandCaptor = ArgumentCaptor.forClass(ResizeVolumeCommand.class);
verify(endPoint).sendMessage(commandCaptor.capture());
assertEquals(3758096384L, commandCaptor.getValue().getCurrentSize());
assertEquals(8589934592L, commandCaptor.getValue().getNewSize());
}

@Test
public void testResizeCloudStackVolume_AppliesQosAfterFileGrow() {
VolumeObject volumeObject = mock(VolumeObject.class);
Expand Down
Loading