@@ -395,9 +395,9 @@ public AsyncCallFuture<VolumeApiResult> expungeVolumeAsync(VolumeInfo volume) {
395395 }
396396
397397 // Find out if the volume is at state of download_in_progress on secondary storage
398- VolumeDataStoreVO volumeStore = _volumeStoreDao .findByVolume (volume .getId ());
399- if (volumeStore != null ) {
400- if (volumeStore .getDownloadState () == VMTemplateStorageResourceAssoc .Status .DOWNLOAD_IN_PROGRESS ) {
398+ VolumeDataStoreVO volumeOnImageStore = _volumeStoreDao .findByVolume (volume .getId ());
399+ if (volumeOnImageStore != null ) {
400+ if (volumeOnImageStore .getDownloadState () == VMTemplateStorageResourceAssoc .Status .DOWNLOAD_IN_PROGRESS ) {
401401 String msg = String .format ("Volume: %s is currently being uploaded; can't delete it." , volume );
402402 logger .debug (msg );
403403 result .setSuccess (false );
@@ -416,10 +416,10 @@ public AsyncCallFuture<VolumeApiResult> expungeVolumeAsync(VolumeInfo volume) {
416416
417417 if (!volumeExistsOnPrimary (vol )) {
418418 // not created on primary store
419- if (volumeStore == null ) {
419+ if (volumeOnImageStore == null ) {
420420 // also not created on secondary store
421421 if (logger .isDebugEnabled ()) {
422- logger .debug ("Marking volume that was never created as destroyed: " + vol );
422+ logger .debug ("Marking volume that was never created as destroyed: {}" , vol );
423423 }
424424 VMTemplateVO template = templateDao .findById (vol .getTemplateId ());
425425 if (template != null && !template .isDeployAsIs ()) {
@@ -435,11 +435,21 @@ public AsyncCallFuture<VolumeApiResult> expungeVolumeAsync(VolumeInfo volume) {
435435 if (volume .getDataStore ().getRole () == DataStoreRole .Image ) {
436436 // no need to change state in volumes table
437437 volume .processEventOnly (Event .DestroyRequested );
438+ if (volumeOnImageStore == null ) {
439+ logger .debug ("Volume {} doesn't exist on image store, no need to delete" , vol );
440+ future .complete (result );
441+ return future ;
442+ }
438443 } else if (volume .getDataStore ().getRole () == DataStoreRole .Primary ) {
439444 if (vol .getState () == Volume .State .Expunging ) {
440445 logger .info ("Volume {} is already in Expunging, retrying" , volume );
441446 }
442447 volume .processEvent (Event .ExpungeRequested );
448+ if (!volumeExistsOnPrimary (vol )) {
449+ logger .debug ("Volume {} doesn't exist on primary storage, no need to delete" , vol );
450+ future .complete (result );
451+ return future ;
452+ }
443453 }
444454
445455 DeleteVolumeContext <VolumeApiResult > context = new DeleteVolumeContext <>(null , vo , future );
@@ -460,13 +470,11 @@ public void ensureVolumeIsExpungeReady(long volumeId) {
460470
461471 private boolean volumeExistsOnPrimary (VolumeVO vol ) {
462472 Long poolId = vol .getPoolId ();
463-
464473 if (poolId == null ) {
465474 return false ;
466475 }
467476
468477 PrimaryDataStore primaryStore = dataStoreMgr .getPrimaryDataStore (poolId );
469-
470478 if (primaryStore == null ) {
471479 return false ;
472480 }
@@ -476,8 +484,7 @@ private boolean volumeExistsOnPrimary(VolumeVO vol) {
476484 }
477485
478486 String volumePath = vol .getPath ();
479-
480- if (volumePath == null || volumePath .trim ().isEmpty ()) {
487+ if (StringUtils .isBlank (volumePath )) {
481488 return false ;
482489 }
483490
0 commit comments