|
23 | 23 |
|
24 | 24 | import javax.inject.Inject; |
25 | 25 |
|
| 26 | +import com.cloud.utils.db.GlobalLock; |
26 | 27 | import org.apache.cloudstack.engine.subsystem.api.storage.ClusterScope; |
27 | 28 | import org.apache.cloudstack.engine.subsystem.api.storage.DataObject; |
28 | 29 | import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreDriver; |
@@ -225,31 +226,43 @@ public DiskFormat getDefaultDiskType() { |
225 | 226 | public DataObject create(DataObject obj) { |
226 | 227 | // create template on primary storage |
227 | 228 | 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; |
233 | 236 | } |
234 | 237 | 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()); |
245 | 240 | if (templateStoragePoolRef == null) { |
246 | | - throw new CloudRuntimeException("Failed to create template storage pool entry"); |
247 | | - } else { |
| 241 | + |
248 | 242 | 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"); |
250 | 244 | } |
| 245 | + templateStoragePoolRef = new VMTemplateStoragePoolVO(this.getId(), obj.getId()); |
| 246 | + templateStoragePoolRef = templatePoolDao.persist(templateStoragePoolRef); |
251 | 247 | } |
| 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(); |
252 | 263 | } |
| 264 | + } catch (Exception e){ |
| 265 | + s_logger.debug("Caught exception ", e); |
253 | 266 | } |
254 | 267 | } else if (obj.getType() == DataObjectType.SNAPSHOT) { |
255 | 268 | return objectInStoreMgr.create(obj, this); |
|
0 commit comments