Skip to content

Commit bde008e

Browse files
author
Kelven Yang
committed
bug 12787: improve worker VM cleanup in snapshot operation
1 parent 9b70669 commit bde008e

7 files changed

Lines changed: 68 additions & 35 deletions

File tree

‎core/src/com/cloud/hypervisor/vmware/manager/VmwareHostService.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ public interface VmwareHostService {
1313
void invalidateServiceContext(VmwareContext context);
1414
VmwareHypervisorHost getHyperHost(VmwareContext context, Command cmd);
1515

16-
String getWorkerName(VmwareContext context, Command cmd);
16+
String getWorkerName(VmwareContext context, Command cmd, int workerSequence);
1717
}

‎core/src/com/cloud/hypervisor/vmware/manager/VmwareStorageManagerImpl.java‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public Answer execute(VmwareHostService hostService, BackupSnapshotCommand cmd)
174174
if(vmMo == null) {
175175
dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
176176

177-
workerVMName = hostService.getWorkerName(context, cmd);
177+
workerVMName = hostService.getWorkerName(context, cmd, 0);
178178

179179
// attach a volume to dummay wrapper VM for taking snapshot and exporting the VM for backup
180180
if (!hyperHost.createBlankVm(workerVMName, 1, 512, 0, false, 4, 0, VirtualMachineGuestOsIdentifier._otherGuest.toString(), morDs, false)) {
@@ -203,7 +203,7 @@ public Answer execute(VmwareHostService hostService, BackupSnapshotCommand cmd)
203203
}
204204

205205
snapshotBackupUuid = backupSnapshotToSecondaryStorage(vmMo, accountId, volumeId, cmd.getVolumePath(), snapshotUuid, secondaryStorageUrl, prevSnapshotUuid, prevBackupUuid,
206-
UUID.randomUUID().toString().replace("-", ""));
206+
hostService.getWorkerName(context, cmd, 1));
207207

208208
success = (snapshotBackupUuid != null);
209209
if (success) {
@@ -238,8 +238,6 @@ public Answer execute(VmwareHostService hostService, BackupSnapshotCommand cmd)
238238
return new BackupSnapshotAnswer(cmd, success, details, snapshotBackupUuid, true);
239239
}
240240

241-
242-
243241
@Override
244242
public Answer execute(VmwareHostService hostService, CreatePrivateTemplateFromVolumeCommand cmd) {
245243
String secondaryStoragePoolURL = cmd.getSecondaryStorageUrl();
@@ -268,7 +266,7 @@ public Answer execute(VmwareHostService hostService, CreatePrivateTemplateFromVo
268266
Ternary<String, Long, Long> result = createTemplateFromVolume(vmMo,
269267
accountId, templateId, cmd.getTemplateName(),
270268
secondaryStoragePoolURL, volumePath,
271-
hostService.getWorkerName(context, cmd));
269+
hostService.getWorkerName(context, cmd, 0));
272270

273271
return new CreatePrivateTemplateAnswer(cmd, true, null,
274272
result.first(), result.third(), result.second(),
@@ -331,10 +329,10 @@ public Answer execute(VmwareHostService hostService, CopyVolumeCommand cmd) {
331329

332330
Pair<String, String> result;
333331
if (cmd.toSecondaryStorage()) {
334-
result = copyVolumeToSecStorage(
335-
hyperHost, vmName, volumeId, cmd.getPool().getUuid(), volumePath,
332+
result = copyVolumeToSecStorage(hostService,
333+
hyperHost, cmd, vmName, volumeId, cmd.getPool().getUuid(), volumePath,
336334
secondaryStorageURL,
337-
hostService.getWorkerName(context, cmd));
335+
hostService.getWorkerName(context, cmd, 0));
338336
} else {
339337
StorageFilerTO poolTO = cmd.getPool();
340338

@@ -393,8 +391,7 @@ public Answer execute(VmwareHostService hostService, CreateVolumeFromSnapshotCom
393391

394392
DatastoreMO primaryDsMo = new DatastoreMO(hyperHost.getContext(), morPrimaryDs);
395393
details = createVolumeFromSnapshot(hyperHost, primaryDsMo,
396-
newVolumeName, accountId, volumeId,
397-
secondaryStorageUrl, backedUpSnapshotUuid);
394+
newVolumeName, accountId, volumeId, secondaryStorageUrl, backedUpSnapshotUuid);
398395
if (details == null) {
399396
success = true;
400397
}
@@ -738,8 +735,10 @@ private String deleteSnapshotOnSecondaryStorge(long accountId, long volumeId, St
738735
return "Failed to delete snapshot backup file, backupUuid: " + backupUuid;
739736
}
740737

741-
private Pair<String, String> copyVolumeToSecStorage(VmwareHypervisorHost hyperHost, String vmName, long volumeId, String poolId, String volumePath,
742-
String secStorageUrl, String workerVmName) throws Exception {
738+
private Pair<String, String> copyVolumeToSecStorage(VmwareHostService hostService, VmwareHypervisorHost hyperHost, CopyVolumeCommand cmd,
739+
String vmName, long volumeId, String poolId, String volumePath,
740+
String secStorageUrl, String workerVmName) throws Exception {
741+
743742
String volumeFolder = String.valueOf(volumeId) + "/";
744743
VirtualMachineMO workerVm=null;
745744
VirtualMachineMO vmMo=null;
@@ -756,12 +755,11 @@ private Pair<String, String> copyVolumeToSecStorage(VmwareHypervisorHost hyperHo
756755

757756
vmMo = hyperHost.findVmOnHyperHost(vmName);
758757
if (vmMo == null) {
759-
//create a dummy worker vm for attaching the volume
758+
// create a dummy worker vm for attaching the volume
760759
DatastoreMO dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
761760
//restrict VM name to 32 chars, (else snapshot descriptor file name will be truncated to 32 chars of vm name)
762-
String workerVMName = UUID.randomUUID().toString().replaceAll("-", "");
763761
VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
764-
vmConfig.setName(workerVMName);
762+
vmConfig.setName(workerVmName);
765763
vmConfig.setMemoryMB((long) 4);
766764
vmConfig.setNumCPUs(1);
767765
vmConfig.setGuestId(VirtualMachineGuestOsIdentifier._otherGuest.toString());
@@ -780,7 +778,7 @@ private Pair<String, String> copyVolumeToSecStorage(VmwareHypervisorHost hyperHo
780778
vmConfig.setDeviceChange(new VirtualDeviceConfigSpec[] { scsiControllerSpec });
781779

782780
hyperHost.createVm(vmConfig);
783-
workerVm = hyperHost.findVmOnHyperHost(workerVMName);
781+
workerVm = hyperHost.findVmOnHyperHost(workerVmName);
784782
if (workerVm == null) {
785783
String msg = "Unable to create worker VM to execute CopyVolumeCommand";
786784
s_logger.error(msg);
@@ -795,7 +793,8 @@ private Pair<String, String> copyVolumeToSecStorage(VmwareHypervisorHost hyperHo
795793

796794
vmMo.createSnapshot(exportName, "Temporary snapshot for copy-volume command", false, false);
797795

798-
exportVolumeToSecondaryStroage(vmMo, volumePath, secStorageUrl, "volumes/" + volumeFolder, exportName, workerVmName);
796+
exportVolumeToSecondaryStroage(vmMo, volumePath, secStorageUrl, "volumes/" + volumeFolder, exportName,
797+
hostService.getWorkerName(hyperHost.getContext(), cmd, 1));
799798
return new Pair<String, String>(volumeFolder, exportName);
800799

801800
} finally {

‎core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,7 +2885,7 @@ public synchronized CreateAnswer execute(CreateCommand cmd) {
28852885
if (cmd.getDiskCharacteristics().getType() == Volume.Type.ROOT) {
28862886
if (cmd.getTemplateUrl() == null) {
28872887
// create a root volume for blank VM
2888-
String dummyVmName = getWorkerName(context, cmd);
2888+
String dummyVmName = getWorkerName(context, cmd, 0);
28892889
VirtualMachineMO vmMo = null;
28902890

28912891
try {
@@ -2969,7 +2969,7 @@ public synchronized CreateAnswer execute(CreateCommand cmd) {
29692969
VirtualMachineMO vmMo = null;
29702970
String volumeUuid = UUID.randomUUID().toString().replace("-", "");
29712971
String volumeDatastorePath = String.format("[%s] %s.vmdk", dsMo.getName(), volumeUuid);
2972-
String dummyVmName = getWorkerName(context, cmd);
2972+
String dummyVmName = getWorkerName(context, cmd, 0);
29732973
try {
29742974
vmMo = prepareVolumeHostDummyVm(hyperHost, dsMo, dummyVmName);
29752975
if (vmMo == null) {
@@ -3968,7 +3968,7 @@ public VmwareHypervisorHost getHyperHost(VmwareContext context, Command cmd) {
39683968

39693969
@Override
39703970
@DB
3971-
public String getWorkerName(VmwareContext context, Command cmd) {
3971+
public String getWorkerName(VmwareContext context, Command cmd, int workerSequence) {
39723972
VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
39733973
String vmName = mgr.composeWorkerName();
39743974

‎core/src/com/cloud/storage/resource/VmwareSecondaryStorageResourceHandler.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,18 @@ public Answer executeRequest(Command cmd) {
6666
answer = _resource.defaultAction(cmd);
6767
}
6868

69+
// special handling to pass-back context info for cleanups
6970
if(cmd.getContextParam("execid") != null) {
7071
answer.setContextParam("execid", cmd.getContextParam("execid"));
7172
}
73+
7274
if(cmd.getContextParam("checkpoint") != null) {
7375
answer.setContextParam("checkpoint", cmd.getContextParam("checkpoint"));
7476
}
77+
78+
if(cmd.getContextParam("checkpoint2") != null) {
79+
answer.setContextParam("checkpoint2", cmd.getContextParam("checkpoint2"));
80+
}
7581

7682
return answer;
7783
}
@@ -219,9 +225,13 @@ public VmwareHypervisorHost getHyperHost(VmwareContext context, Command cmd) {
219225
}
220226

221227
@Override
222-
public String getWorkerName(VmwareContext context, Command cmd) {
228+
public String getWorkerName(VmwareContext context, Command cmd, int workerSequence) {
223229
assert(cmd.getContextParam("worker") != null);
224-
return cmd.getContextParam("worker");
230+
assert(workerSequence < 2);
231+
232+
if(workerSequence == 0)
233+
return cmd.getContextParam("worker");
234+
return cmd.getContextParam("worker2");
225235
}
226236

227237
@Override

‎server/src/com/cloud/hypervisor/guru/VMwareGuru.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ public long getCommandHostDelegation(long hostId, Command cmd) {
266266
long checkPointId = _checkPointMgr.pushCheckPoint(new VmwareCleanupMaid(hostDetails.get("guid"), workerName));
267267
cmd.setContextParam("worker", workerName);
268268
cmd.setContextParam("checkpoint", String.valueOf(checkPointId));
269+
270+
// some commands use 2 workers
271+
String workerName2 = _vmwareMgr.composeWorkerName();
272+
long checkPointId2 = _checkPointMgr.pushCheckPoint(new VmwareCleanupMaid(hostDetails.get("guid"), workerName2));
273+
cmd.setContextParam("worker2", workerName2);
274+
cmd.setContextParam("checkpoint2", String.valueOf(checkPointId2));
269275
}
270276

271277
return cmdTarget.first().getId();

‎server/src/com/cloud/hypervisor/vmware/VmwareManagerImpl.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,11 @@ public boolean processAnswers(long agentId, long seq, Answer[] answers) {
749749
if(checkPointIdStr != null) {
750750
_checkPointMgr.popCheckPoint(Long.parseLong(checkPointIdStr));
751751
}
752+
753+
checkPointIdStr = answer.getContextParam("checkpoint2");
754+
if(checkPointIdStr != null) {
755+
_checkPointMgr.popCheckPoint(Long.parseLong(checkPointIdStr));
756+
}
752757
}
753758
}
754759

‎vmware-base/src/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,19 +1411,32 @@ public void cloneFromDiskChain(String clonedVmName, int cpuSpeedMHz, int memoryM
14111411
HostMO hostMo = getRunningHost();
14121412
VirtualMachineConfigInfo vmConfigInfo = getConfigInfo();
14131413

1414-
hostMo.createBlankVm(clonedVmName, 1, cpuSpeedMHz, 0, false, memoryMb, 0, vmConfigInfo.getGuestId(), morDs, false);
1415-
VirtualMachineMO clonedVmMo = hostMo.findVmOnHyperHost(clonedVmName);
1414+
if(!hostMo.createBlankVm(clonedVmName, 1, cpuSpeedMHz, 0, false, memoryMb, 0, vmConfigInfo.getGuestId(), morDs, false))
1415+
throw new Exception("Unable to create a blank VM");
14161416

1417-
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
1418-
VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
1419-
deviceConfigSpecArray[0] = new VirtualDeviceConfigSpec();
1420-
1421-
VirtualDevice device = VmwareHelper.prepareDiskDevice(clonedVmMo, -1, disks, morDs, -1, 1);
1422-
1423-
deviceConfigSpecArray[0].setDevice(device);
1424-
deviceConfigSpecArray[0].setOperation(VirtualDeviceConfigSpecOperation.add);
1425-
vmConfigSpec.setDeviceChange(deviceConfigSpecArray);
1426-
clonedVmMo.configureVm(vmConfigSpec);
1417+
VirtualMachineMO clonedVmMo = hostMo.findVmOnHyperHost(clonedVmName);
1418+
if(clonedVmMo == null)
1419+
throw new Exception("Unable to find just-created blank VM");
1420+
1421+
boolean bSuccess = false;
1422+
try {
1423+
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
1424+
VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
1425+
deviceConfigSpecArray[0] = new VirtualDeviceConfigSpec();
1426+
1427+
VirtualDevice device = VmwareHelper.prepareDiskDevice(clonedVmMo, -1, disks, morDs, -1, 1);
1428+
1429+
deviceConfigSpecArray[0].setDevice(device);
1430+
deviceConfigSpecArray[0].setOperation(VirtualDeviceConfigSpecOperation.add);
1431+
vmConfigSpec.setDeviceChange(deviceConfigSpecArray);
1432+
clonedVmMo.configureVm(vmConfigSpec);
1433+
bSuccess = true;
1434+
} finally {
1435+
if(!bSuccess) {
1436+
clonedVmMo.detachAllDisks();
1437+
clonedVmMo.destroy();
1438+
}
1439+
}
14271440
}
14281441

14291442
public void plugDevice(VirtualDevice device) throws Exception {

0 commit comments

Comments
 (0)