@@ -770,29 +770,42 @@ public List<VMTemplateStoragePoolVO> getUnusedTemplatesInPool(StoragePoolVO pool
770770 }
771771
772772 @ Override
773+ @ DB
773774 public void evictTemplateFromStoragePool (VMTemplateStoragePoolVO templatePoolVO ) {
774- StoragePool pool = (StoragePool ) _dataStoreMgr .getPrimaryDataStore (templatePoolVO .getPoolId ());
775- VMTemplateVO template = _tmpltDao .findByIdIncludingRemoved (templatePoolVO .getTemplateId ());
776-
777- if (s_logger .isDebugEnabled ()) {
778- s_logger .debug ("Evicting " + templatePoolVO );
775+ //Need to hold the lock, otherwise, another thread may create a volume from the template at the same time.
776+ //Assumption here is that, we will hold the same lock during create volume from template
777+ VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao .acquireInLockTable (templatePoolVO .getId ());
778+ if (templatePoolRef == null ) {
779+ s_logger .debug ("can't aquire the lock for template pool ref:" + templatePoolVO .getId ());
780+ return ;
779781 }
780- DestroyCommand cmd = new DestroyCommand (pool , templatePoolVO );
781782
782783 try {
783- Answer answer = _storageMgr .sendToPool (pool , cmd );
784+ StoragePool pool = (StoragePool ) this ._dataStoreMgr .getPrimaryDataStore (templatePoolVO .getPoolId ());
785+ VMTemplateVO template = _tmpltDao .findByIdIncludingRemoved (templatePoolVO .getTemplateId ());
786+
787+ if (s_logger .isDebugEnabled ()) {
788+ s_logger .debug ("Evicting " + templatePoolVO );
789+ }
790+ DestroyCommand cmd = new DestroyCommand (pool , templatePoolVO );
784791
785- if (answer != null && answer .getResult ()) {
786- // Remove the templatePoolVO
787- if (_tmpltPoolDao .remove (templatePoolVO .getId ())) {
788- s_logger .debug ("Successfully evicted template: " + template .getName () + " from storage pool: " + pool .getName ());
792+ try {
793+ Answer answer = _storageMgr .sendToPool (pool , cmd );
794+
795+ if (answer != null && answer .getResult ()) {
796+ // Remove the templatePoolVO
797+ if (_tmpltPoolDao .remove (templatePoolVO .getId ())) {
798+ s_logger .debug ("Successfully evicted template: " + template .getName () + " from storage pool: " + pool .getName ());
799+ }
800+ } else {
801+ s_logger .info ("Will retry evicte template: " + template .getName () + " from storage pool: " + pool .getName ());
789802 }
790- } else {
791- s_logger .info ("Will retry evicte template: " + template .getName () + " from storage pool: " + pool .getName ());
803+ } catch (StorageUnavailableException e ) {
804+ s_logger .info ("Storage is unavailable currently. Will retry evicte template: " + template .getName () + " from storage pool: "
805+ + pool .getName ());
792806 }
793- } catch (StorageUnavailableException e ) {
794- s_logger .info ("Storage is unavailable currently. Will retry evicte template: " + template .getName () + " from storage pool: "
795- + pool .getName ());
807+ } finally {
808+ _tmpltPoolDao .releaseFromLockTable (templatePoolRef .getId ());
796809 }
797810
798811 }
0 commit comments