@@ -1679,7 +1679,7 @@ public boolean stateTransitTo(final VirtualMachine vm1, final VirtualMachine.Eve
16791679 }
16801680
16811681 @ Override
1682- public void destroy (final String vmUuid ) throws AgentUnavailableException , OperationTimedoutException , ConcurrentOperationException {
1682+ public void destroy (final String vmUuid , final boolean expunge ) throws AgentUnavailableException , OperationTimedoutException , ConcurrentOperationException {
16831683 VMInstanceVO vm = _vmDao .findByUuid (vmUuid );
16841684 if (vm == null || vm .getState () == State .Destroyed || vm .getState () == State .Expunging || vm .getRemoved () != null ) {
16851685 if (s_logger .isDebugEnabled ()) {
@@ -1689,15 +1689,12 @@ public void destroy(final String vmUuid) throws AgentUnavailableException, Opera
16891689 }
16901690
16911691 if (s_logger .isDebugEnabled ()) {
1692- s_logger .debug ("Destroying vm " + vm );
1692+ s_logger .debug ("Destroying vm " + vm + ", expunge flag " + ( expunge ? "on" : "off" ) );
16931693 }
16941694
16951695 advanceStop (vmUuid , VmDestroyForcestop .value ());
16961696
1697- if (!_vmSnapshotMgr .deleteAllVMSnapshots (vm .getId (), null )) {
1698- s_logger .debug ("Unable to delete all snapshots for " + vm );
1699- throw new CloudRuntimeException ("Unable to delete vm snapshots for " + vm );
1700- }
1697+ deleteVMSnapshots (vm , expunge );
17011698
17021699 // reload the vm object from db
17031700 vm = _vmDao .findByUuid (vmUuid );
@@ -1712,6 +1709,27 @@ public void destroy(final String vmUuid) throws AgentUnavailableException, Opera
17121709 }
17131710 }
17141711
1712+ /**
1713+ * Delete vm snapshots depending on vm's hypervisor type. For Vmware, vm snapshots removal is delegated to vm cleanup thread
1714+ * to reduce tasks sent to hypervisor (one tasks to delete vm snapshots and vm itself
1715+ * instead of one task for each vm snapshot plus another for the vm)
1716+ * @param vm vm
1717+ * @param expunge indicates if vm should be expunged
1718+ */
1719+ private void deleteVMSnapshots (VMInstanceVO vm , boolean expunge ) {
1720+ if (! vm .getHypervisorType ().equals (HypervisorType .VMware )) {
1721+ if (!_vmSnapshotMgr .deleteAllVMSnapshots (vm .getId (), null )) {
1722+ s_logger .debug ("Unable to delete all snapshots for " + vm );
1723+ throw new CloudRuntimeException ("Unable to delete vm snapshots for " + vm );
1724+ }
1725+ }
1726+ else {
1727+ if (expunge ) {
1728+ _vmSnapshotMgr .deleteVMSnapshotsFromDB (vm .getId ());
1729+ }
1730+ }
1731+ }
1732+
17151733 protected boolean checkVmOnHost (final VirtualMachine vm , final long hostId ) throws AgentUnavailableException , OperationTimedoutException {
17161734 final Answer answer = _agentMgr .send (hostId , new CheckVirtualMachineCommand (vm .getInstanceName ()));
17171735 if (answer == null || !answer .getResult ()) {
0 commit comments