mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-10-26 08:42:29 +01:00 
			
		
		
		
	Clean up template_zone_ref entries in case of create template failure.
This commit is contained in:
		
							parent
							
								
									8f549db518
								
							
						
					
					
						commit
						2af36aa87b
					
				| @ -23,11 +23,13 @@ import com.cloud.utils.db.GenericDao; | |||||||
| 
 | 
 | ||||||
| public interface VMTemplateZoneDao extends GenericDao<VMTemplateZoneVO, Long> { | public interface VMTemplateZoneDao extends GenericDao<VMTemplateZoneVO, Long> { | ||||||
| 	public List<VMTemplateZoneVO> listByZoneId(long id); | 	public List<VMTemplateZoneVO> listByZoneId(long id); | ||||||
| 	 | 
 | ||||||
| 	public List<VMTemplateZoneVO> listByTemplateId(long templateId); | 	public List<VMTemplateZoneVO> listByTemplateId(long templateId); | ||||||
| 	 | 
 | ||||||
| 	public VMTemplateZoneVO findByZoneTemplate(long zoneId, long templateId); | 	public VMTemplateZoneVO findByZoneTemplate(long zoneId, long templateId); | ||||||
| 	 | 
 | ||||||
|     public List<VMTemplateZoneVO> listByZoneTemplate(Long zoneId, long templateId); |     public List<VMTemplateZoneVO> listByZoneTemplate(Long zoneId, long templateId); | ||||||
| 
 | 
 | ||||||
|  |     public void deletePrimaryRecordsForTemplate(long templateId); | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -20,6 +20,7 @@ import java.util.List; | |||||||
| 
 | 
 | ||||||
| import javax.ejb.Local; | import javax.ejb.Local; | ||||||
| 
 | 
 | ||||||
|  | import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO; | ||||||
| import org.apache.log4j.Logger; | import org.apache.log4j.Logger; | ||||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||||
| 
 | 
 | ||||||
| @ -27,32 +28,33 @@ import com.cloud.storage.VMTemplateZoneVO; | |||||||
| import com.cloud.utils.db.GenericDaoBase; | import com.cloud.utils.db.GenericDaoBase; | ||||||
| import com.cloud.utils.db.SearchBuilder; | import com.cloud.utils.db.SearchBuilder; | ||||||
| import com.cloud.utils.db.SearchCriteria; | import com.cloud.utils.db.SearchCriteria; | ||||||
|  | import com.cloud.utils.db.Transaction; | ||||||
| 
 | 
 | ||||||
| @Component | @Component | ||||||
| @Local(value={VMTemplateZoneDao.class}) | @Local(value={VMTemplateZoneDao.class}) | ||||||
| public class VMTemplateZoneDaoImpl extends GenericDaoBase<VMTemplateZoneVO, Long> implements VMTemplateZoneDao { | public class VMTemplateZoneDaoImpl extends GenericDaoBase<VMTemplateZoneVO, Long> implements VMTemplateZoneDao { | ||||||
| 	public static final Logger s_logger = Logger.getLogger(VMTemplateZoneDaoImpl.class.getName()); | 	public static final Logger s_logger = Logger.getLogger(VMTemplateZoneDaoImpl.class.getName()); | ||||||
| 	 | 
 | ||||||
| 	protected final SearchBuilder<VMTemplateZoneVO> ZoneSearch; | 	protected final SearchBuilder<VMTemplateZoneVO> ZoneSearch; | ||||||
| 	protected final SearchBuilder<VMTemplateZoneVO> TemplateSearch; | 	protected final SearchBuilder<VMTemplateZoneVO> TemplateSearch; | ||||||
| 	protected final SearchBuilder<VMTemplateZoneVO> ZoneTemplateSearch; | 	protected final SearchBuilder<VMTemplateZoneVO> ZoneTemplateSearch; | ||||||
| 	 | 
 | ||||||
| 	 | 
 | ||||||
| 	public VMTemplateZoneDaoImpl () { | 	public VMTemplateZoneDaoImpl () { | ||||||
| 		ZoneSearch = createSearchBuilder(); | 		ZoneSearch = createSearchBuilder(); | ||||||
| 		ZoneSearch.and("zone_id", ZoneSearch.entity().getZoneId(), SearchCriteria.Op.EQ); | 		ZoneSearch.and("zone_id", ZoneSearch.entity().getZoneId(), SearchCriteria.Op.EQ); | ||||||
| 		ZoneSearch.done(); | 		ZoneSearch.done(); | ||||||
| 		 | 
 | ||||||
| 		TemplateSearch = createSearchBuilder(); | 		TemplateSearch = createSearchBuilder(); | ||||||
| 		TemplateSearch.and("template_id", TemplateSearch.entity().getTemplateId(), SearchCriteria.Op.EQ); | 		TemplateSearch.and("template_id", TemplateSearch.entity().getTemplateId(), SearchCriteria.Op.EQ); | ||||||
| 		TemplateSearch.done(); | 		TemplateSearch.done(); | ||||||
| 		 | 
 | ||||||
| 		ZoneTemplateSearch = createSearchBuilder(); | 		ZoneTemplateSearch = createSearchBuilder(); | ||||||
| 		ZoneTemplateSearch.and("zone_id", ZoneTemplateSearch.entity().getZoneId(), SearchCriteria.Op.EQ); | 		ZoneTemplateSearch.and("zone_id", ZoneTemplateSearch.entity().getZoneId(), SearchCriteria.Op.EQ); | ||||||
| 		ZoneTemplateSearch.and("template_id", ZoneTemplateSearch.entity().getTemplateId(), SearchCriteria.Op.EQ); | 		ZoneTemplateSearch.and("template_id", ZoneTemplateSearch.entity().getTemplateId(), SearchCriteria.Op.EQ); | ||||||
| 		ZoneTemplateSearch.done(); | 		ZoneTemplateSearch.done(); | ||||||
| 	} | 	} | ||||||
| 	 | 
 | ||||||
| 
 | 
 | ||||||
| 	@Override | 	@Override | ||||||
| 	public List<VMTemplateZoneVO> listByZoneId(long id) { | 	public List<VMTemplateZoneVO> listByZoneId(long id) { | ||||||
| @ -84,6 +86,19 @@ public class VMTemplateZoneDaoImpl extends GenericDaoBase<VMTemplateZoneVO, Long | |||||||
|         } |         } | ||||||
| 	    sc.setParameters("template_id", templateId); | 	    sc.setParameters("template_id", templateId); | ||||||
| 	    return listBy(sc); | 	    return listBy(sc); | ||||||
| 	}	 | 	} | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void deletePrimaryRecordsForTemplate(long templateId) { | ||||||
|  |         SearchCriteria<VMTemplateZoneVO> sc = TemplateSearch.create(); | ||||||
|  |         sc.setParameters("template_id", templateId); | ||||||
|  |         Transaction txn = Transaction.currentTxn(); | ||||||
|  |         txn.start(); | ||||||
|  |         remove(sc); | ||||||
|  |         txn.commit(); | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -142,7 +142,7 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager { | |||||||
| 			s_logger.debug("there is already one in the cache store"); | 			s_logger.debug("there is already one in the cache store"); | ||||||
| 			return objectInStoreMgr.get(data, cacheStore); | 			return objectInStoreMgr.get(data, cacheStore); | ||||||
| 		} | 		} | ||||||
| 		 | 
 | ||||||
| 		//TODO: consider multiple thread to create | 		//TODO: consider multiple thread to create | ||||||
| 		DataObject objOnCacheStore = cacheStore.create(data); | 		DataObject objOnCacheStore = cacheStore.create(data); | ||||||
| 
 | 
 | ||||||
| @ -155,7 +155,7 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager { | |||||||
| 		    result = future.get(); | 		    result = future.get(); | ||||||
| 
 | 
 | ||||||
| 		    if (result.isFailed()) { | 		    if (result.isFailed()) { | ||||||
| 		        cacheStore.delete(data); | 		        objOnCacheStore.processEvent(Event.OperationFailed); | ||||||
| 		    } else { | 		    } else { | ||||||
| 		        objOnCacheStore.processEvent(Event.OperationSuccessed, result.getAnswer()); | 		        objOnCacheStore.processEvent(Event.OperationSuccessed, result.getAnswer()); | ||||||
| 		        return objOnCacheStore; | 		        return objOnCacheStore; | ||||||
| @ -168,7 +168,7 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager { | |||||||
|             s_logger.debug("create cache storage failed: " + e.toString()); |             s_logger.debug("create cache storage failed: " + e.toString()); | ||||||
|         } finally { |         } finally { | ||||||
|             if (result == null) { |             if (result == null) { | ||||||
|                 cacheStore.delete(data); |                 objOnCacheStore.processEvent(Event.OperationFailed); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| @ -191,8 +191,7 @@ public class StorageCacheManagerImpl implements StorageCacheManager, Manager { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public DataObject deleteCacheObject(DataObject data) { |     public boolean deleteCacheObject(DataObject data) { | ||||||
|         // TODO Auto-generated method stub |         return objectInStoreMgr.delete(data); | ||||||
|         return null; |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -1456,8 +1456,9 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager, | |||||||
|             if (privateTemplate == null) { |             if (privateTemplate == null) { | ||||||
|                 Transaction txn = Transaction.currentTxn(); |                 Transaction txn = Transaction.currentTxn(); | ||||||
|                 txn.start(); |                 txn.start(); | ||||||
|                 // Remove the template_store_ref record first, otherwise, we cannot remove the template record due to FK constraints |                 // template_store_ref entries should have been removed using our DataObject.processEvent command in case of failure. | ||||||
|                 this._tmplStoreDao.deletePrimaryRecordsForTemplate(templateId); |                 // Remove the template_zone_ref record | ||||||
|  |                 this._tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId); | ||||||
|                 // Remove the template record |                 // Remove the template record | ||||||
|                 this._tmpltDao.expunge(templateId); |                 this._tmpltDao.expunge(templateId); | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user