Skip to content

Commit 281abef

Browse files
committed
CLOUDSTACK-4133
Introduce a global lock on template and pool id so that concurrent threads wont be inserting the same row in DB and hit MySQLIntegrityConstraintViolationException Signed off by : nitin mehta<nitin.mehta@citrix.com>
1 parent dacc4e1 commit 281abef

1 file changed

Lines changed: 31 additions & 18 deletions

File tree

‎engine/storage/volume/src/org/apache/cloudstack/storage/datastore/PrimaryDataStoreImpl.java‎

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import javax.inject.Inject;
2525

26+
import com.cloud.utils.db.GlobalLock;
2627
import org.apache.cloudstack.engine.subsystem.api.storage.ClusterScope;
2728
import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
2829
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreDriver;
@@ -225,31 +226,43 @@ public DiskFormat getDefaultDiskType() {
225226
public DataObject create(DataObject obj) {
226227
// create template on primary storage
227228
if (obj.getType() == DataObjectType.TEMPLATE) {
228-
VMTemplateStoragePoolVO templateStoragePoolRef = templatePoolDao.findByPoolTemplate(this.getId(),
229-
obj.getId());
230-
if (templateStoragePoolRef == null) {
231-
if (s_logger.isDebugEnabled()) {
232-
s_logger.debug("Not found (templateId: " + obj.getId() + ", poolId: " + this.getId() + ") in template_spool_ref");
229+
try{
230+
String templateIdPoolIdString = "templateId:" + obj.getId() + "poolId:" + this.getId();
231+
VMTemplateStoragePoolVO templateStoragePoolRef;
232+
GlobalLock lock = GlobalLock.getInternLock(templateIdPoolIdString);
233+
if (!lock.lock(5)) {
234+
s_logger.debug("Couldn't lock the db on the string " + templateIdPoolIdString);
235+
return null;
233236
}
234237
try {
235-
if (s_logger.isDebugEnabled()) {
236-
s_logger.debug("Persisting (templateId: " + obj.getId() + ", poolId: " + this.getId() + ") to template_spool_ref");
237-
}
238-
templateStoragePoolRef = new VMTemplateStoragePoolVO(this.getId(), obj.getId());
239-
templateStoragePoolRef = templatePoolDao.persist(templateStoragePoolRef);
240-
} catch (Throwable t) {
241-
if (s_logger.isDebugEnabled()) {
242-
s_logger.debug("Failed to insert (templateId: " + obj.getId() + ", poolId: " + this.getId() + ") to template_spool_ref", t);
243-
}
244-
templateStoragePoolRef = templatePoolDao.findByPoolTemplate(this.getId(), obj.getId());
238+
templateStoragePoolRef = templatePoolDao.findByPoolTemplate(this.getId(),
239+
obj.getId());
245240
if (templateStoragePoolRef == null) {
246-
throw new CloudRuntimeException("Failed to create template storage pool entry");
247-
} else {
241+
248242
if (s_logger.isDebugEnabled()) {
249-
s_logger.debug("Another thread already inserts " + templateStoragePoolRef.getId() + " to template_spool_ref", t);
243+
s_logger.debug("Not found (" + templateIdPoolIdString + ") in template_spool_ref, persisting it");
250244
}
245+
templateStoragePoolRef = new VMTemplateStoragePoolVO(this.getId(), obj.getId());
246+
templateStoragePoolRef = templatePoolDao.persist(templateStoragePoolRef);
251247
}
248+
} catch (Throwable t) {
249+
if (s_logger.isDebugEnabled()) {
250+
s_logger.debug("Failed to insert (" + templateIdPoolIdString + ") to template_spool_ref", t);
251+
}
252+
templateStoragePoolRef = templatePoolDao.findByPoolTemplate(this.getId(), obj.getId());
253+
if (templateStoragePoolRef == null) {
254+
throw new CloudRuntimeException("Failed to create template storage pool entry");
255+
} else {
256+
if (s_logger.isDebugEnabled()) {
257+
s_logger.debug("Another thread already inserts " + templateStoragePoolRef.getId() + " to template_spool_ref", t);
258+
}
259+
}
260+
}finally {
261+
lock.unlock();
262+
lock.releaseRef();
252263
}
264+
} catch (Exception e){
265+
s_logger.debug("Caught exception ", e);
253266
}
254267
} else if (obj.getType() == DataObjectType.SNAPSHOT) {
255268
return objectInStoreMgr.create(obj, this);

0 commit comments

Comments
 (0)