Skip to content

Commit 2af36aa

Browse files
committed
Clean up template_zone_ref entries in case of create template failure.
1 parent 8f549db commit 2af36aa

4 files changed

Lines changed: 35 additions & 18 deletions

File tree

engine/schema/src/com/cloud/storage/dao/VMTemplateZoneDao.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323

2424
public interface VMTemplateZoneDao extends GenericDao<VMTemplateZoneVO, Long> {
2525
public List<VMTemplateZoneVO> listByZoneId(long id);
26-
26+
2727
public List<VMTemplateZoneVO> listByTemplateId(long templateId);
28-
28+
2929
public VMTemplateZoneVO findByZoneTemplate(long zoneId, long templateId);
30-
30+
3131
public List<VMTemplateZoneVO> listByZoneTemplate(Long zoneId, long templateId);
3232

33+
public void deletePrimaryRecordsForTemplate(long templateId);
34+
3335
}

engine/schema/src/com/cloud/storage/dao/VMTemplateZoneDaoImpl.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,41 @@
2020

2121
import javax.ejb.Local;
2222

23+
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
2324
import org.apache.log4j.Logger;
2425
import org.springframework.stereotype.Component;
2526

2627
import com.cloud.storage.VMTemplateZoneVO;
2728
import com.cloud.utils.db.GenericDaoBase;
2829
import com.cloud.utils.db.SearchBuilder;
2930
import com.cloud.utils.db.SearchCriteria;
31+
import com.cloud.utils.db.Transaction;
3032

3133
@Component
3234
@Local(value={VMTemplateZoneDao.class})
3335
public class VMTemplateZoneDaoImpl extends GenericDaoBase<VMTemplateZoneVO, Long> implements VMTemplateZoneDao {
3436
public static final Logger s_logger = Logger.getLogger(VMTemplateZoneDaoImpl.class.getName());
35-
37+
3638
protected final SearchBuilder<VMTemplateZoneVO> ZoneSearch;
3739
protected final SearchBuilder<VMTemplateZoneVO> TemplateSearch;
3840
protected final SearchBuilder<VMTemplateZoneVO> ZoneTemplateSearch;
39-
40-
41+
42+
4143
public VMTemplateZoneDaoImpl () {
4244
ZoneSearch = createSearchBuilder();
4345
ZoneSearch.and("zone_id", ZoneSearch.entity().getZoneId(), SearchCriteria.Op.EQ);
4446
ZoneSearch.done();
45-
47+
4648
TemplateSearch = createSearchBuilder();
4749
TemplateSearch.and("template_id", TemplateSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
4850
TemplateSearch.done();
49-
51+
5052
ZoneTemplateSearch = createSearchBuilder();
5153
ZoneTemplateSearch.and("zone_id", ZoneTemplateSearch.entity().getZoneId(), SearchCriteria.Op.EQ);
5254
ZoneTemplateSearch.and("template_id", ZoneTemplateSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
5355
ZoneTemplateSearch.done();
5456
}
55-
57+
5658

5759
@Override
5860
public List<VMTemplateZoneVO> listByZoneId(long id) {
@@ -84,6 +86,19 @@ public List<VMTemplateZoneVO> listByZoneTemplate(Long zoneId, long templateId) {
8486
}
8587
sc.setParameters("template_id", templateId);
8688
return listBy(sc);
87-
}
89+
}
90+
91+
92+
@Override
93+
public void deletePrimaryRecordsForTemplate(long templateId) {
94+
SearchCriteria<VMTemplateZoneVO> sc = TemplateSearch.create();
95+
sc.setParameters("template_id", templateId);
96+
Transaction txn = Transaction.currentTxn();
97+
txn.start();
98+
remove(sc);
99+
txn.commit();
100+
101+
}
102+
88103

89104
}

engine/storage/cache/src/org/apache/cloudstack/storage/cache/manager/StorageCacheManagerImpl.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public DataObject createCacheObject(DataObject data, Scope scope) {
142142
s_logger.debug("there is already one in the cache store");
143143
return objectInStoreMgr.get(data, cacheStore);
144144
}
145-
145+
146146
//TODO: consider multiple thread to create
147147
DataObject objOnCacheStore = cacheStore.create(data);
148148

@@ -155,7 +155,7 @@ public DataObject createCacheObject(DataObject data, Scope scope) {
155155
result = future.get();
156156

157157
if (result.isFailed()) {
158-
cacheStore.delete(data);
158+
objOnCacheStore.processEvent(Event.OperationFailed);
159159
} else {
160160
objOnCacheStore.processEvent(Event.OperationSuccessed, result.getAnswer());
161161
return objOnCacheStore;
@@ -168,7 +168,7 @@ public DataObject createCacheObject(DataObject data, Scope scope) {
168168
s_logger.debug("create cache storage failed: " + e.toString());
169169
} finally {
170170
if (result == null) {
171-
cacheStore.delete(data);
171+
objOnCacheStore.processEvent(Event.OperationFailed);
172172
}
173173
}
174174

@@ -191,8 +191,7 @@ protected Void createCacheObjectCallBack(AsyncCallbackDispatcher<StorageCacheMan
191191
}
192192

193193
@Override
194-
public DataObject deleteCacheObject(DataObject data) {
195-
// TODO Auto-generated method stub
196-
return null;
194+
public boolean deleteCacheObject(DataObject data) {
195+
return objectInStoreMgr.delete(data);
197196
}
198197
}

server/src/com/cloud/template/TemplateManagerImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,8 +1456,9 @@ public VirtualMachineTemplate createPrivateTemplate(CreateTemplateCmd command)
14561456
if (privateTemplate == null) {
14571457
Transaction txn = Transaction.currentTxn();
14581458
txn.start();
1459-
// Remove the template_store_ref record first, otherwise, we cannot remove the template record due to FK constraints
1460-
this._tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
1459+
// template_store_ref entries should have been removed using our DataObject.processEvent command in case of failure.
1460+
// Remove the template_zone_ref record
1461+
this._tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
14611462
// Remove the template record
14621463
this._tmpltDao.expunge(templateId);
14631464

0 commit comments

Comments
 (0)