diff --git a/api/src/com/cloud/exception/ConcurrentOperationException.java b/api/src/com/cloud/exception/ConcurrentOperationException.java index cfe6ba3fa0a..018dba55f2e 100644 --- a/api/src/com/cloud/exception/ConcurrentOperationException.java +++ b/api/src/com/cloud/exception/ConcurrentOperationException.java @@ -17,8 +17,9 @@ package com.cloud.exception; import com.cloud.utils.SerialVersionUID; +import com.cloud.utils.exception.CloudRuntimeException; -public class ConcurrentOperationException extends CloudException { +public class ConcurrentOperationException extends CloudRuntimeException { private static final long serialVersionUID = SerialVersionUID.ConcurrentOperationException; diff --git a/awsapi/src/com/cloud/bridge/persist/dao/BucketPolicyDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/BucketPolicyDaoImpl.java index dd354a39ffb..00486cbaceb 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/BucketPolicyDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/BucketPolicyDaoImpl.java @@ -26,7 +26,7 @@ import com.cloud.bridge.model.BucketPolicyVO; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={BucketPolicyDao.class}) @@ -42,7 +42,7 @@ public class BucketPolicyDaoImpl extends GenericDaoBase im public BucketPolicyVO getByName( String bucketName ) { SearchBuilder searchByBucket = createSearchBuilder(); searchByBucket.and("BucketName", searchByBucket.entity().getBucketName(), SearchCriteria.Op.EQ); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = searchByBucket.create(); @@ -59,7 +59,7 @@ public class BucketPolicyDaoImpl extends GenericDaoBase im public void deletePolicy( String bucketName ) { SearchBuilder deleteByBucket = createSearchBuilder(); deleteByBucket.and("BucketName", deleteByBucket.entity().getBucketName(), SearchCriteria.Op.EQ); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = deleteByBucket.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/CloudStackAccountDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/CloudStackAccountDaoImpl.java index 8fbc7c8e3af..75a693e4955 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/CloudStackAccountDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/CloudStackAccountDaoImpl.java @@ -25,6 +25,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={CloudStackAccountDao.class}) @@ -34,7 +35,7 @@ public class CloudStackAccountDaoImpl extends GenericDaoBase SearchByUUID = createSearchBuilder(); - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { txn.start(); SearchByUUID.and("uuid", SearchByUUID.entity().getUuid(), diff --git a/awsapi/src/com/cloud/bridge/persist/dao/CloudStackConfigurationDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/CloudStackConfigurationDaoImpl.java index bc77ea1d886..644dcdcef37 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/CloudStackConfigurationDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/CloudStackConfigurationDaoImpl.java @@ -27,6 +27,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={CloudStackConfigurationDao.class}) @@ -42,7 +43,7 @@ public class CloudStackConfigurationDaoImpl extends GenericDaoBase sc = NameSearch.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/CloudStackSvcOfferingDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/CloudStackSvcOfferingDaoImpl.java index 8021eb618e9..cb8d129f528 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/CloudStackSvcOfferingDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/CloudStackSvcOfferingDaoImpl.java @@ -29,6 +29,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={CloudStackSvcOfferingDao.class}) @@ -42,7 +43,7 @@ public class CloudStackSvcOfferingDaoImpl extends GenericDaoBase searchByName = createSearchBuilder(); searchByName.and("name", searchByName.entity().getName(), SearchCriteria.Op.EQ); searchByName.done(); - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { txn.start(); SearchCriteria sc = searchByName.create(); @@ -61,7 +62,7 @@ public class CloudStackSvcOfferingDaoImpl extends GenericDaoBase searchByID = createSearchBuilder(); searchByID.and("uuid", searchByID.entity().getUuid(), SearchCriteria.Op.EQ); searchByID.done(); - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { txn.start(); SearchCriteria sc = searchByID.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/CloudStackUserDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/CloudStackUserDaoImpl.java index f7e1da65dc6..7fe1dabee4d 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/CloudStackUserDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/CloudStackUserDaoImpl.java @@ -26,6 +26,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.crypt.DBEncryptionUtil; @Component @@ -43,7 +44,7 @@ public class CloudStackUserDaoImpl extends GenericDaoBase searchByAccessKey = createSearchBuilder(); searchByAccessKey.and("apiKey", searchByAccessKey.entity().getApiKey(), SearchCriteria.Op.EQ); searchByAccessKey.done(); - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { txn.start(); SearchCriteria sc = searchByAccessKey.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/MHostDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/MHostDaoImpl.java index 222325498b9..b52fcaf221b 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/MHostDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/MHostDaoImpl.java @@ -25,6 +25,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={MHostDao.class}) @@ -38,7 +39,7 @@ public class MHostDaoImpl extends GenericDaoBase implements MHost @Override public MHostVO getByHostKey(String hostKey) { NameSearch.and("MHostKey", NameSearch.entity().getHostKey(), SearchCriteria.Op.EQ); - Transaction txn = Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); + TransactionLegacy txn = TransactionLegacy.open("cloudbridge", TransactionLegacy.AWSAPI_DB, true); try { txn.start(); SearchCriteria sc = NameSearch.create(); @@ -52,7 +53,7 @@ public class MHostDaoImpl extends GenericDaoBase implements MHost @Override public void updateHeartBeat(MHostVO mhost) { - Transaction txn = Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); + TransactionLegacy txn = TransactionLegacy.open("cloudbridge", TransactionLegacy.AWSAPI_DB, true); try { txn.start(); update(mhost.getId(), mhost); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/MHostMountDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/MHostMountDaoImpl.java index 8b99f487911..8a7153a9b85 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/MHostMountDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/MHostMountDaoImpl.java @@ -25,6 +25,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={MHostMountDao.class}) @@ -37,7 +38,7 @@ public class MHostMountDaoImpl extends GenericDaoBase implem public MHostMountVO getHostMount(long mHostId, long sHostId) { SearchByMHostID.and("MHostID", SearchByMHostID.entity().getmHostID(), SearchCriteria.Op.EQ); SearchByMHostID.and("SHostID", SearchByMHostID.entity().getsHostID(), SearchCriteria.Op.EQ); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = SearchByMHostID.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/MultiPartPartsDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/MultiPartPartsDaoImpl.java index 6f314951697..f1472e675aa 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/MultiPartPartsDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/MultiPartPartsDaoImpl.java @@ -28,6 +28,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={MultiPartPartsDao.class}) @@ -42,7 +43,7 @@ public class MultiPartPartsDaoImpl extends GenericDaoBase sc = ByUploadID.create(); @@ -61,7 +62,7 @@ public class MultiPartPartsDaoImpl extends GenericDaoBase byUploadID = createSearchBuilder(); byUploadID.and("UploadID", byUploadID.entity().getUploadid(), SearchCriteria.Op.EQ); byUploadID.and("partNumber", byUploadID.entity().getPartNumber(), SearchCriteria.Op.GT); - Transaction txn = Transaction.currentTxn(); // Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); + TransactionLegacy txn = TransactionLegacy.currentTxn(); // Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); try { txn.start(); SearchCriteria sc = byUploadID.create(); @@ -82,7 +83,7 @@ public class MultiPartPartsDaoImpl extends GenericDaoBase byUploadID = createSearchBuilder(); byUploadID.and("UploadID", byUploadID.entity().getUploadid(), SearchCriteria.Op.EQ); byUploadID.and("partNumber", byUploadID.entity().getPartNumber(), SearchCriteria.Op.EQ); - Transaction txn = Transaction.currentTxn(); // Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); + TransactionLegacy txn = TransactionLegacy.currentTxn(); // Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); try { txn.start(); SearchCriteria sc = byUploadID.create(); @@ -102,7 +103,7 @@ public class MultiPartPartsDaoImpl extends GenericDaoBase byUploadID = createSearchBuilder(); byUploadID.and("UploadID", byUploadID.entity().getUploadid(), SearchCriteria.Op.EQ); byUploadID.and("partNumber", byUploadID.entity().getPartNumber(), SearchCriteria.Op.EQ); - Transaction txn = Transaction.currentTxn(); // Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); + TransactionLegacy txn = TransactionLegacy.currentTxn(); // Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); try { txn.start(); SearchCriteria sc = byUploadID.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/MultiPartUploadsDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/MultiPartUploadsDaoImpl.java index 0f76e80a952..41133a06e92 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/MultiPartUploadsDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/MultiPartUploadsDaoImpl.java @@ -33,6 +33,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={MultiPartUploadsDao.class}) @@ -42,9 +43,9 @@ public class MultiPartUploadsDaoImpl extends GenericDaoBase multipartExits( int uploadId ) { MultiPartUploadsVO uploadvo = null; - Transaction txn = null; + TransactionLegacy txn = null; try { - txn = Transaction.open(Transaction.AWSAPI_DB); + txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); uploadvo = findById(new Long(uploadId)); if (null != uploadvo) return new OrderedPair(uploadvo.getAccessKey(), uploadvo.getNameKey()); @@ -58,9 +59,9 @@ public class MultiPartUploadsDaoImpl extends GenericDaoBase sc = byBucket.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/MultipartLoadDao.java b/awsapi/src/com/cloud/bridge/persist/dao/MultipartLoadDao.java index c1a69dc5e47..4e6ff3d1b25 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/MultipartLoadDao.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/MultipartLoadDao.java @@ -34,6 +34,7 @@ import com.cloud.bridge.service.core.s3.S3MultipartPart; import com.cloud.bridge.service.core.s3.S3MultipartUpload; import com.cloud.bridge.util.OrderedPair; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; public class MultipartLoadDao { public static final Logger logger = Logger.getLogger(MultipartLoadDao.class); @@ -94,9 +95,9 @@ public class MultipartLoadDao { */ public int initiateUpload( String accessKey, String bucketName, String key, String cannedAccess, S3MetaDataEntry[] meta ) { int uploadId = -1; - Transaction txn = null; + TransactionLegacy txn = null; try { - txn = Transaction.open(Transaction.AWSAPI_DB); + txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); Date tod = new Date(); MultiPartUploadsVO uploadVO = new MultiPartUploadsVO(accessKey, bucketName, key, cannedAccess, tod); @@ -315,9 +316,9 @@ public class MultipartLoadDao { private void saveMultipartMeta( int uploadId, S3MetaDataEntry[] meta ) { if (null == meta) return; - Transaction txn = null; + TransactionLegacy txn = null; try { - txn = Transaction.open(Transaction.AWSAPI_DB); + txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); for( int i=0; i < meta.length; i++ ) { S3MetaDataEntry entry = meta[i]; diff --git a/awsapi/src/com/cloud/bridge/persist/dao/MultipartMetaDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/MultipartMetaDaoImpl.java index 7ab93599d22..fec0a2c1280 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/MultipartMetaDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/MultipartMetaDaoImpl.java @@ -27,6 +27,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={MultipartMetaDao.class}) @@ -37,7 +38,7 @@ public class MultipartMetaDaoImpl extends GenericDaoBase SearchBuilder searchByUID = createSearchBuilder(); searchByUID.and("UploadID", searchByUID.entity().getUploadID(), SearchCriteria.Op.EQ); searchByUID.done(); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = searchByUID.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/OfferingDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/OfferingDaoImpl.java index ea7d264f80c..963f1084134 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/OfferingDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/OfferingDaoImpl.java @@ -29,6 +29,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={OfferingDao.class}) @@ -39,7 +40,7 @@ public class OfferingDaoImpl extends GenericDaoBase impl @Override public int getOfferingCount() { - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); return listAll().size(); @@ -56,7 +57,7 @@ public class OfferingDaoImpl extends GenericDaoBase impl SearchBuilder searchByAmazon = createSearchBuilder(); searchByAmazon.and("AmazonEC2Offering", searchByAmazon.entity().getAmazonOffering() , SearchCriteria.Op.EQ); searchByAmazon.done(); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = searchByAmazon.create(); @@ -74,7 +75,7 @@ public class OfferingDaoImpl extends GenericDaoBase impl SearchBuilder searchByAmazon = createSearchBuilder(); searchByAmazon.and("CloudStackOffering", searchByAmazon.entity().getAmazonOffering() , SearchCriteria.Op.EQ); searchByAmazon.done(); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = searchByAmazon.create(); @@ -93,7 +94,7 @@ public class OfferingDaoImpl extends GenericDaoBase impl searchByAmazon.and("CloudStackOffering", searchByAmazon.entity().getAmazonOffering() , SearchCriteria.Op.EQ); searchByAmazon.and("AmazonEC2Offering", searchByAmazon.entity().getCloudstackOffering() , SearchCriteria.Op.EQ); searchByAmazon.done(); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); OfferingBundleVO offering = null; try { txn.start(); @@ -122,7 +123,7 @@ public class OfferingDaoImpl extends GenericDaoBase impl SearchBuilder searchByAmazon = createSearchBuilder(); searchByAmazon.and("AmazonEC2Offering", searchByAmazon.entity().getAmazonOffering() , SearchCriteria.Op.EQ); searchByAmazon.done(); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = searchByAmazon.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/SAclDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/SAclDaoImpl.java index d88660e05c9..d4b4c90fedc 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/SAclDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/SAclDaoImpl.java @@ -32,6 +32,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={SAclDao.class}) @@ -46,7 +47,7 @@ public class SAclDaoImpl extends GenericDaoBase implements SAclDao SearchByTarget.and("TargetID", SearchByTarget.entity().getTargetId(), SearchCriteria.Op.EQ); SearchByTarget.done(); Filter filter = new Filter(SAclVO.class, "grantOrder", Boolean.TRUE, null, null); - Transaction txn = Transaction.open( Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open( TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = SearchByTarget.create(); @@ -66,7 +67,7 @@ public class SAclDaoImpl extends GenericDaoBase implements SAclDao SearchByAcl.and("TargetID", SearchByAcl.entity().getTargetId(), SearchCriteria.Op.EQ); SearchByAcl.and("GranteeCanonicalID", SearchByAcl.entity().getGranteeCanonicalId(), SearchCriteria.Op.EQ); Filter filter = new Filter(SAclVO.class, "grantOrder", Boolean.TRUE, null, null); - Transaction txn = Transaction.open( Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open( TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = SearchByAcl.create(); @@ -85,7 +86,7 @@ public class SAclDaoImpl extends GenericDaoBase implements SAclDao SearchByTarget.and("Target", SearchByTarget.entity().getTarget(), SearchCriteria.Op.EQ); SearchByTarget.and("TargetID", SearchByTarget.entity().getTargetId(), SearchCriteria.Op.EQ); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = SearchByTarget.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/SBucketDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/SBucketDaoImpl.java index 817c682a946..552281d8b85 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/SBucketDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/SBucketDaoImpl.java @@ -29,6 +29,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={SBucketDao.class}) @@ -42,7 +43,7 @@ public class SBucketDaoImpl extends GenericDaoBase implements S SearchBuilder SearchByName = createSearchBuilder(); SearchByName.and("Name", SearchByName.entity().getName(), SearchCriteria.Op.EQ); //Transaction txn = Transaction.open(Transaction.AWSAPI_DB); - Transaction txn = Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); + TransactionLegacy txn = TransactionLegacy.open("cloudbridge", TransactionLegacy.AWSAPI_DB, true); try { txn.start(); SearchCriteria sc = SearchByName.create(); @@ -59,7 +60,7 @@ public class SBucketDaoImpl extends GenericDaoBase implements S SearchBuilder ByCanonicalID = createSearchBuilder(); ByCanonicalID.and("OwnerCanonicalID", ByCanonicalID.entity().getOwnerCanonicalId(), SearchCriteria.Op.EQ); Filter filter = new Filter(SBucketVO.class, "createTime", Boolean.TRUE, null, null); - Transaction txn = Transaction.currentTxn(); // Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); + TransactionLegacy txn = TransactionLegacy.currentTxn(); // Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); try { txn.start(); SearchCriteria sc = ByCanonicalID.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/SHostDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/SHostDaoImpl.java index 9b6b5359759..5d2e9b901b3 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/SHostDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/SHostDaoImpl.java @@ -25,6 +25,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={SHostDao.class}) @@ -36,7 +37,7 @@ public class SHostDaoImpl extends GenericDaoBase implements SHost SearchBuilder HostSearch = createSearchBuilder(); HostSearch.and("Host", HostSearch.entity().getHost(), SearchCriteria.Op.EQ); HostSearch.done(); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = HostSearch.create(); @@ -55,7 +56,7 @@ public class SHostDaoImpl extends GenericDaoBase implements SHost LocalStorageHostSearch.and("MHostID", LocalStorageHostSearch.entity().getMhostid(), SearchCriteria.Op.EQ); LocalStorageHostSearch.and("ExportRoot", LocalStorageHostSearch.entity().getExportRoot(), SearchCriteria.Op.EQ); LocalStorageHostSearch.done(); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); SearchCriteria sc = LocalStorageHostSearch.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/SMetaDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/SMetaDaoImpl.java index 8fdc9493d82..95355b92689 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/SMetaDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/SMetaDaoImpl.java @@ -28,6 +28,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={SMetaDao.class}) @@ -41,7 +42,7 @@ public class SMetaDaoImpl extends GenericDaoBase implements SMeta SearchByTarget.and("Target", SearchByTarget.entity().getTarget(), SearchCriteria.Op.EQ); SearchByTarget.and("TargetID", SearchByTarget.entity().getTargetId(), SearchCriteria.Op.EQ); SearchByTarget.done(); - Transaction txn = Transaction.open( Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open( TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = SearchByTarget.create(); @@ -71,7 +72,7 @@ public class SMetaDaoImpl extends GenericDaoBase implements SMeta SearchBuilder SearchByTarget = createSearchBuilder(); SearchByTarget.and("Target", SearchByTarget.entity().getTarget(), SearchCriteria.Op.EQ); SearchByTarget.and("TargetID", SearchByTarget.entity().getTargetId(), SearchCriteria.Op.EQ); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = SearchByTarget.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/SObjectDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/SObjectDaoImpl.java index 6d23757b8b5..e6370feca1b 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/SObjectDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/SObjectDaoImpl.java @@ -33,6 +33,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={SObjectDao.class}) @@ -47,7 +48,7 @@ public class SObjectDaoImpl extends GenericDaoBase implements S SearchBuilder SearchByName = createSearchBuilder(); SearchByName.and("SBucketID", SearchByName.entity().getBucketID() , SearchCriteria.Op.EQ); SearchByName.and("NameKey", SearchByName.entity().getNameKey() , SearchCriteria.Op.EQ); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = SearchByName.create(); @@ -76,7 +77,7 @@ public class SObjectDaoImpl extends GenericDaoBase implements S SearchByBucket.and("SBucketID", SearchByBucket.entity().getBucketID(), SearchCriteria.Op.EQ); SearchByBucket.and("DeletionMark", SearchByBucket.entity().getDeletionMark(), SearchCriteria.Op.NULL); - Transaction txn = Transaction.currentTxn(); // Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); + TransactionLegacy txn = TransactionLegacy.currentTxn(); // Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); try { txn.start(); SearchCriteria sc = SearchByBucket.create(); @@ -100,7 +101,7 @@ public class SObjectDaoImpl extends GenericDaoBase implements S List objects = new ArrayList(); getAllBuckets.and("SBucketID", getAllBuckets.entity().getBucketID(), SearchCriteria.Op.EQ); - Transaction txn = Transaction.currentTxn(); // Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); + TransactionLegacy txn = TransactionLegacy.currentTxn(); // Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); try { txn.start(); SearchCriteria sc = getAllBuckets.create(); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/SObjectItemDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/SObjectItemDaoImpl.java index 57140c49072..294b32d4d4f 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/SObjectItemDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/SObjectItemDaoImpl.java @@ -27,6 +27,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={SObjectItemDao.class}) @@ -39,7 +40,7 @@ public class SObjectItemDaoImpl extends GenericDaoBase impl @Override public SObjectItemVO getByObjectIdNullVersion(long id) { - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); SearchBuilder SearchByID = createSearchBuilder(); SearchByID.and("ID", SearchByID.entity().getId(), SearchCriteria.Op.EQ); @@ -56,7 +57,7 @@ public class SObjectItemDaoImpl extends GenericDaoBase impl @Override public List getItems(long sobjectID) { - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); SearchBuilder SearchBySobjectID = createSearchBuilder(); SearchBySobjectID.and("SObjectID", SearchBySobjectID.entity().getId(), SearchCriteria.Op.EQ); diff --git a/awsapi/src/com/cloud/bridge/persist/dao/UserCredentialsDaoImpl.java b/awsapi/src/com/cloud/bridge/persist/dao/UserCredentialsDaoImpl.java index c45886f794c..b60a717a3ee 100644 --- a/awsapi/src/com/cloud/bridge/persist/dao/UserCredentialsDaoImpl.java +++ b/awsapi/src/com/cloud/bridge/persist/dao/UserCredentialsDaoImpl.java @@ -29,6 +29,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={UserCredentialsDao.class}) @@ -41,7 +42,7 @@ public class UserCredentialsDaoImpl extends GenericDaoBase SearchByAccessKey = createSearchBuilder(); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchByAccessKey.and("AccessKey", SearchByAccessKey.entity() @@ -60,7 +61,7 @@ public class UserCredentialsDaoImpl extends GenericDaoBase SearchByCertID = createSearchBuilder(); SearchByCertID.and("CertUniqueId", SearchByCertID.entity().getCertUniqueId(), SearchCriteria.Op.EQ); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); SearchCriteria sc = SearchByCertID.create(); diff --git a/awsapi/src/com/cloud/bridge/service/EC2RestServlet.java b/awsapi/src/com/cloud/bridge/service/EC2RestServlet.java index 50ac26f2901..1ef04a4aebd 100644 --- a/awsapi/src/com/cloud/bridge/service/EC2RestServlet.java +++ b/awsapi/src/com/cloud/bridge/service/EC2RestServlet.java @@ -161,6 +161,7 @@ import com.cloud.bridge.util.ConfigurationHelper; import com.cloud.bridge.util.EC2RestAuth; import com.cloud.stack.models.CloudStackAccount; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component("EC2RestServlet") public class EC2RestServlet extends HttpServlet { @@ -377,7 +378,7 @@ public class EC2RestServlet extends HttpServlet { private void setUserKeys( HttpServletRequest request, HttpServletResponse response ) { String[] accessKey = null; String[] secretKey = null; - Transaction txn = null; + TransactionLegacy txn = null; try { // -> all these parameters are required accessKey = request.getParameterValues( "accesskey" ); @@ -398,7 +399,7 @@ public class EC2RestServlet extends HttpServlet { return; } try { - txn = Transaction.open(Transaction.AWSAPI_DB); + txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); txn.start(); // -> use the keys to see if the account actually exists ServiceProvider.getInstance().getEC2Engine().validateAccount( accessKey[0], secretKey[0] ); @@ -434,7 +435,7 @@ public class EC2RestServlet extends HttpServlet { */ private void setCertificate( HttpServletRequest request, HttpServletResponse response ) throws Exception { - Transaction txn = null; + TransactionLegacy txn = null; try { // [A] Pull the cert and cloud AccessKey from the request String[] certificate = request.getParameterValues( "cert" ); @@ -470,7 +471,7 @@ public class EC2RestServlet extends HttpServlet { // [C] Associate the cert's uniqueId with the Cloud API keys String uniqueId = AuthenticationUtils.X509CertUniqueId( userCert ); logger.debug( "SetCertificate, uniqueId: " + uniqueId ); - txn = Transaction.open(Transaction.AWSAPI_DB); + txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); txn.start(); UserCredentialsVO user = ucDao.getByAccessKey(accessKey[0]); user.setCertUniqueId(uniqueId); @@ -505,7 +506,7 @@ public class EC2RestServlet extends HttpServlet { */ private void deleteCertificate( HttpServletRequest request, HttpServletResponse response ) throws Exception { - Transaction txn = null; + TransactionLegacy txn = null; try { String [] accessKey = request.getParameterValues( "AWSAccessKeyId" ); if ( null == accessKey || 0 == accessKey.length ) { @@ -527,7 +528,7 @@ public class EC2RestServlet extends HttpServlet { /* UserCredentialsDao credentialDao = new UserCredentialsDao(); credentialDao.setCertificateId( accessKey[0], null ); - */ txn = Transaction.open(Transaction.AWSAPI_DB); + */ txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); UserCredentialsVO user = ucDao.getByAccessKey(accessKey[0]); user.setCertUniqueId(null); ucDao.update(user.getId(), user); diff --git a/awsapi/src/com/cloud/bridge/service/S3RestServlet.java b/awsapi/src/com/cloud/bridge/service/S3RestServlet.java index 7e69fd65087..192e1a28e51 100644 --- a/awsapi/src/com/cloud/bridge/service/S3RestServlet.java +++ b/awsapi/src/com/cloud/bridge/service/S3RestServlet.java @@ -67,6 +67,7 @@ import com.cloud.bridge.util.RestAuth; import com.cloud.bridge.util.S3SoapAuth; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; public class S3RestServlet extends HttpServlet { private static final long serialVersionUID = -6168996266762804877L; public static final String ENABLE_S3_API="enable.s3.api"; @@ -139,7 +140,7 @@ public class S3RestServlet extends HttpServlet { */ private void processRequest( HttpServletRequest request, HttpServletResponse response, String method ) { - Transaction txn = Transaction.open("cloudbridge", Transaction.AWSAPI_DB, true); + TransactionLegacy txn = TransactionLegacy.open("cloudbridge", TransactionLegacy.AWSAPI_DB, true); try { logRequest(request); @@ -274,7 +275,7 @@ public class S3RestServlet extends HttpServlet { // -> use the keys to see if the account actually exists //ServiceProvider.getInstance().getEC2Engine().validateAccount( accessKey[0], secretKey[0] ); //UserCredentialsDaoImpl credentialDao = new UserCredentialsDao(); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); txn.start(); UserCredentialsVO user = new UserCredentialsVO(accessKey[0], secretKey[0]); user = ucDao.persist(user); diff --git a/awsapi/src/com/cloud/bridge/service/controller/s3/S3BucketAction.java b/awsapi/src/com/cloud/bridge/service/controller/s3/S3BucketAction.java index c98de34a698..4d7c41a75b3 100644 --- a/awsapi/src/com/cloud/bridge/service/controller/s3/S3BucketAction.java +++ b/awsapi/src/com/cloud/bridge/service/controller/s3/S3BucketAction.java @@ -94,6 +94,7 @@ import com.cloud.bridge.util.XSerializer; import com.cloud.bridge.util.XSerializerXmlAdapter; import com.cloud.bridge.util.XmlHelper; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; public class S3BucketAction implements ServletAction { @@ -371,7 +372,7 @@ public class S3BucketAction implements ServletAction { response.setStatus(403); return; } - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); // [B] Place the policy into the database over writting an existing policy try { // -> first make sure that the policy is valid by parsing it diff --git a/awsapi/src/com/cloud/bridge/service/controller/s3/ServiceProvider.java b/awsapi/src/com/cloud/bridge/service/controller/s3/ServiceProvider.java index 2623efe1f61..0854741699f 100644 --- a/awsapi/src/com/cloud/bridge/service/controller/s3/ServiceProvider.java +++ b/awsapi/src/com/cloud/bridge/service/controller/s3/ServiceProvider.java @@ -62,6 +62,7 @@ import com.cloud.bridge.util.OrderedPair; import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component public class ServiceProvider extends ManagerBase { @@ -90,7 +91,7 @@ public class ServiceProvider extends ManagerBase { protected ServiceProvider() throws IOException { // register service implementation object - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); txn.close(); } @@ -183,7 +184,7 @@ public class ServiceProvider extends ManagerBase { public UserInfo getUserInfo(String accessKey) { UserInfo info = new UserInfo(); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); try { txn.start(); UserCredentialsVO cloudKeys = ucDao.getByAccessKey( accessKey ); @@ -253,7 +254,7 @@ public class ServiceProvider extends ManagerBase { multipartDir = properties.getProperty("storage.multipartDir"); - Transaction txn1 = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn1 = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); timer.schedule(getHeartbeatTask(), HEARTBEAT_INTERVAL, HEARTBEAT_INTERVAL); txn1.close(); diff --git a/awsapi/src/com/cloud/bridge/service/core/s3/S3Engine.java b/awsapi/src/com/cloud/bridge/service/core/s3/S3Engine.java index 7beb012d4b7..05e87d788db 100644 --- a/awsapi/src/com/cloud/bridge/service/core/s3/S3Engine.java +++ b/awsapi/src/com/cloud/bridge/service/core/s3/S3Engine.java @@ -86,6 +86,7 @@ import com.cloud.bridge.util.StringHelper; import com.cloud.bridge.util.Triple; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; /** * The CRUD control actions to be invoked from S3BucketAction or S3ObjectAction. @@ -195,7 +196,7 @@ public class S3Engine { String cannedAccessPolicy = request.getCannedAccess(); String bucketName = request.getBucketName(); response.setBucketName( bucketName ); - Transaction txn= null; + TransactionLegacy txn= null; verifyBucketName( bucketName, false ); S3PolicyContext context = new S3PolicyContext( PolicyActions.CreateBucket, bucketName ); @@ -205,7 +206,7 @@ public class S3Engine { OrderedPair shost_storagelocation_pair = null; boolean success = false; try { - txn = Transaction.open(Transaction.AWSAPI_DB); + txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); if (bucketDao.getByName(request.getBucketName()) != null) throw new ObjectAlreadyExistsException("Bucket already exists"); @@ -257,10 +258,10 @@ public class S3Engine { String bucketName = request.getBucketName(); SBucketVO sbucket = bucketDao.getByName(bucketName); - Transaction txn = null; + TransactionLegacy txn = null; if ( sbucket != null ) { - txn = Transaction.open(Transaction.AWSAPI_DB); + txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); txn.start(); S3PolicyContext context = new S3PolicyContext( PolicyActions.DeleteBucket, bucketName ); switch( verifyPolicy( context )) @@ -699,7 +700,7 @@ public class S3Engine { if (null != version) httpResp.addHeader("x-amz-version-id", version); httpResp.flushBuffer(); - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); // [C] Re-assemble the object from its uploaded file parts try { // explicit transaction control to avoid holding transaction during @@ -752,11 +753,11 @@ public class S3Engine { S3BucketAdapter bucketAdapter = getStorageHostBucketAdapter(host_storagelocation_pair.getFirst()); String itemFileName = object_objectitem_pair.getSecond().getStoredPath(); InputStream is = null; - Transaction txn = null; + TransactionLegacy txn = null; try { // explicit transaction control to avoid holding transaction during file-copy process - txn = Transaction.open(Transaction.AWSAPI_DB); + txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); txn.start(); is = request.getDataInputStream(); String md5Checksum = bucketAdapter.saveObject(is, host_storagelocation_pair.getSecond(), bucket.getName(), itemFileName); @@ -813,11 +814,11 @@ public class S3Engine { S3BucketAdapter bucketAdapter = getStorageHostBucketAdapter(host_storagelocation_pair.getFirst()); String itemFileName = object_objectitem_pair.getSecond().getStoredPath(); InputStream is = null; - Transaction txn = null; + TransactionLegacy txn = null; try { // explicit transaction control to avoid holding transaction during file-copy process - txn = Transaction.open(Transaction.AWSAPI_DB); + txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); txn.start(); is = request.getInputStream(); @@ -1505,7 +1506,7 @@ public class S3Engine { context.setEvalParam( ConditionKeys.Acl, cannedAccessPolicy); verifyAccess( context, "SBucket", bucket.getId(), SAcl.PERMISSION_WRITE ); // TODO - check this validates plain POSTs - Transaction txn = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); txn.start(); // [B] If versioning is off them we over write a null object item @@ -1554,7 +1555,7 @@ public class S3Engine { } else { - Transaction txn1 = Transaction.open(Transaction.AWSAPI_DB); + TransactionLegacy txn1 = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB); txn1.start(); // -> there is no object nor an object item object = new SObjectVO(); diff --git a/engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java b/engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java index 6804dc1ef68..3e684cc9fd4 100755 --- a/engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java +++ b/engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java @@ -97,6 +97,7 @@ import com.cloud.utils.concurrency.NamedThreadFactory; import com.cloud.utils.db.DB; import com.cloud.utils.db.EntityManager; import com.cloud.utils.db.QueryBuilder; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CloudRuntimeException; @@ -355,7 +356,7 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl @DB protected boolean noDbTxn() { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); return !txn.dbTxnStarted(); } @@ -1252,7 +1253,7 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl @Override protected void doTask(final Task task) throws Exception { - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { final Type type = task.getType(); if (type == Task.Type.DATA) { diff --git a/engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java b/engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java index 07fa73caae0..8681263347e 100755 --- a/engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java +++ b/engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java @@ -85,8 +85,8 @@ import com.cloud.serializer.GsonHelper; import com.cloud.utils.DateUtil; import com.cloud.utils.concurrency.NamedThreadFactory; import com.cloud.utils.db.QueryBuilder; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.nio.Link; import com.cloud.utils.nio.Task; @@ -591,7 +591,7 @@ public class ClusteredAgentManagerImpl extends AgentManagerImpl implements Clust @Override protected void doTask(final Task task) throws Exception { - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { if (task.getType() != Task.Type.DATA) { super.doTask(task); diff --git a/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java index c2242c7a34d..4423d95e30a 100755 --- a/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java @@ -36,7 +36,6 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; - import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao; import org.apache.cloudstack.context.CallContext; import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; @@ -165,6 +164,11 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.EntityManager; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.exception.ExecutionException; import com.cloud.utils.fsm.NoTransitionException; @@ -311,12 +315,12 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac @Override @DB - public void allocate(String vmInstanceName, VirtualMachineTemplate template, ServiceOffering serviceOffering, Pair rootDiskOffering, - LinkedHashMap dataDiskOfferings, LinkedHashMap auxiliaryNetworks, DeploymentPlan plan, + public void allocate(String vmInstanceName, final VirtualMachineTemplate template, ServiceOffering serviceOffering, final Pair rootDiskOffering, + LinkedHashMap dataDiskOfferings, final LinkedHashMap auxiliaryNetworks, DeploymentPlan plan, HypervisorType hyperType) throws InsufficientCapacityException { VMInstanceVO vm = _vmDao.findVMByInstanceName(vmInstanceName); - Account owner = _entityMgr.findById(Account.class, vm.getAccountId()); + final Account owner = _entityMgr.findById(Account.class, vm.getAccountId()); if (s_logger.isDebugEnabled()) { s_logger.debug("Allocating entries for VM: " + vm); @@ -327,46 +331,46 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac vm.setPodId(plan.getPodId()); } assert (plan.getClusterId() == null && plan.getPoolId() == null) : "We currently don't support cluster and pool preset yet"; - vm = _vmDao.persist(vm); + final VMInstanceVO vmFinal = _vmDao.persist(vm); + final LinkedHashMap dataDiskOfferingsFinal = dataDiskOfferings == null ? + new LinkedHashMap() : dataDiskOfferings; - VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, serviceOffering, null, null); - Transaction txn = Transaction.currentTxn(); - txn.start(); + final VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmFinal, template, serviceOffering, null, null); + + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws InsufficientCapacityException { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Allocating nics for " + vmFinal); + } + + try { + _networkMgr.allocate(vmProfile, auxiliaryNetworks); + } catch (ConcurrentOperationException e) { + throw new CloudRuntimeException("Concurrent operation while trying to allocate resources for the VM", e); + } + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Allocating disks for " + vmFinal); + } + + if (template.getFormat() == ImageFormat.ISO) { + volumeMgr.allocateRawVolume(Type.ROOT, "ROOT-" + vmFinal.getId(), rootDiskOffering.first(), rootDiskOffering.second(), vmFinal, template, owner); + } else if (template.getFormat() == ImageFormat.BAREMETAL) { + // Do nothing + } else { + volumeMgr.allocateTemplatedVolume(Type.ROOT, "ROOT-" + vmFinal.getId(), rootDiskOffering.first(), template, vmFinal, owner); + } + + for (Map.Entry offering : dataDiskOfferingsFinal.entrySet()) { + volumeMgr.allocateRawVolume(Type.DATADISK, "DATA-" + vmFinal.getId(), offering.getKey(), offering.getValue(), vmFinal, template, owner); + } + } + }); if (s_logger.isDebugEnabled()) { - s_logger.debug("Allocating nics for " + vm); - } - - try { - _networkMgr.allocate(vmProfile, auxiliaryNetworks); - } catch (ConcurrentOperationException e) { - throw new CloudRuntimeException("Concurrent operation while trying to allocate resources for the VM", e); - } - - if (dataDiskOfferings == null) { - dataDiskOfferings = new LinkedHashMap(0); - } - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Allocating disks for " + vm); - } - - if (template.getFormat() == ImageFormat.ISO) { - volumeMgr.allocateRawVolume(Type.ROOT, "ROOT-" + vm.getId(), rootDiskOffering.first(), rootDiskOffering.second(), vm, template, owner); - } else if (template.getFormat() == ImageFormat.BAREMETAL) { - // Do nothing - } else { - volumeMgr.allocateTemplatedVolume(Type.ROOT, "ROOT-" + vm.getId(), rootDiskOffering.first(), template, vm, owner); - } - - for (Map.Entry offering : dataDiskOfferings.entrySet()) { - volumeMgr.allocateRawVolume(Type.DATADISK, "DATA-" + vm.getId(), offering.getKey(), offering.getValue(), vm, template, owner); - } - - txn.commit(); - if (s_logger.isDebugEnabled()) { - s_logger.debug("Allocation completed for VM: " + vm); + s_logger.debug("Allocation completed for VM: " + vmFinal); } } @@ -551,37 +555,41 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac } @DB - protected Ternary changeToStartState(VirtualMachineGuru vmGuru, VMInstanceVO vm, User caller, Account account) + protected Ternary changeToStartState(VirtualMachineGuru vmGuru, final VMInstanceVO vm, final User caller, final Account account) throws ConcurrentOperationException { long vmId = vm.getId(); ItWorkVO work = new ItWorkVO(UUID.randomUUID().toString(), _nodeId, State.Starting, vm.getType(), vm.getId()); int retry = VmOpLockStateRetry.value(); while (retry-- != 0) { - Transaction txn = Transaction.currentTxn(); - Ternary result = null; - txn.start(); try { - Journal journal = new Journal.LogJournal("Creating " + vm, s_logger); - work = _workDao.persist(work); - ReservationContextImpl context = new ReservationContextImpl(work.getId(), journal, caller, account); + final ItWorkVO workFinal = work; + Ternary result = + Transaction.execute(new TransactionCallbackWithException, NoTransitionException>() { + @Override + public Ternary doInTransaction(TransactionStatus status) throws NoTransitionException { + Journal journal = new Journal.LogJournal("Creating " + vm, s_logger); + ItWorkVO work = _workDao.persist(workFinal); + ReservationContextImpl context = new ReservationContextImpl(work.getId(), journal, caller, account); - if (stateTransitTo(vm, Event.StartRequested, null, work.getId())) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Successfully transitioned to start state for " + vm + " reservation id = " + work.getId()); + if (stateTransitTo(vm, Event.StartRequested, null, work.getId())) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Successfully transitioned to start state for " + vm + " reservation id = " + work.getId()); + } + return new Ternary(vm, context, work); + } + + return new Ternary(null, null, work); } - result = new Ternary(vm, context, work); - txn.commit(); + }); + + work = result.third(); + if (result.first() != null) return result; - } } catch (NoTransitionException e) { if (s_logger.isDebugEnabled()) { s_logger.debug("Unable to transition into Starting state due to " + e.getMessage()); } - } finally { - if (result == null) { - txn.rollback(); - } } VMInstanceVO instance = _vmDao.findById(vmId); diff --git a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/DcDetailsDaoImpl.java b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/DcDetailsDaoImpl.java index 2ace8a0fbcb..43385488d12 100644 --- a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/DcDetailsDaoImpl.java +++ b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/DcDetailsDaoImpl.java @@ -29,7 +29,7 @@ import org.springframework.stereotype.Component; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component(value="EngineDcDetailsDao") @Local(value=DcDetailsDao.class) @@ -83,7 +83,7 @@ public class DcDetailsDaoImpl extends GenericDaoBase implement @Override public void persist(long dcId, Map details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = DcSearch.create(); sc.setParameters("dcId", dcId); diff --git a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineClusterDaoImpl.java b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineClusterDaoImpl.java index e7eea6c42c3..4251baf06c6 100644 --- a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineClusterDaoImpl.java +++ b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineClusterDaoImpl.java @@ -45,7 +45,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.UpdateBuilder; import com.cloud.utils.exception.CloudRuntimeException; @@ -162,7 +162,7 @@ public class EngineClusterDaoImpl extends GenericDaoBase @Override public Map> getPodClusterIdMap(List clusterIds){ - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; Map> result = new HashMap>(); @@ -243,7 +243,7 @@ public class EngineClusterDaoImpl extends GenericDaoBase @Override public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); EngineClusterVO cluster = createForUpdate(); cluster.setName(null); diff --git a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineDataCenterDaoImpl.java b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineDataCenterDaoImpl.java index 5d8ef8d3243..de710532d98 100644 --- a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineDataCenterDaoImpl.java +++ b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineDataCenterDaoImpl.java @@ -40,7 +40,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SequenceFetcher; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.UpdateBuilder; import com.cloud.utils.net.NetUtils; @@ -216,7 +216,7 @@ public class EngineDataCenterDaoImpl extends GenericDaoBase implem @Override @DB public List findAndUpdateDirectAgentToLoad(long lastPingSecondsAfter, Long limit, long managementServerId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = UnmanagedDirectConnectSearch.create(); sc.setParameters("lastPinged", lastPingSecondsAfter); @@ -356,7 +356,7 @@ public class EngineHostDaoImpl extends GenericDaoBase implem @Override @DB public List findAndUpdateApplianceToLoad(long lastPingSecondsAfter, long managementServerId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = UnmanagedApplianceSearch.create(); @@ -495,7 +495,7 @@ public class EngineHostDaoImpl extends GenericDaoBase implem @DB @Override public List findLostHosts(long timeout) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); ResultSet rs = null; @@ -546,7 +546,7 @@ public class EngineHostDaoImpl extends GenericDaoBase implem public EngineHostVO persist(EngineHostVO host) { final String InsertSequenceSql = "INSERT INTO op_host(id) VALUES(?)"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); EngineHostVO dbHost = super.persist(host); @@ -572,7 +572,7 @@ public class EngineHostDaoImpl extends GenericDaoBase implem @Override @DB public boolean update(Long hostId, EngineHostVO host) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); boolean persisted = super.update(hostId, host); @@ -598,7 +598,7 @@ public class EngineHostDaoImpl extends GenericDaoBase implem ArrayList l = new ArrayList(); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); ; PreparedStatement pstmt = null; try { diff --git a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineHostPodDaoImpl.java b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineHostPodDaoImpl.java index 7bf85d530e5..2bcfdd148f9 100644 --- a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineHostPodDaoImpl.java +++ b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/EngineHostPodDaoImpl.java @@ -41,7 +41,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.UpdateBuilder; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component(value="EngineHostPodDao") @Local(value={EngineHostPodDao.class}) @@ -97,7 +97,7 @@ public class EngineHostPodDaoImpl extends GenericDaoBase HashMap> currentPodCidrSubnets = new HashMap>(); String selectSql = "SELECT id, cidr_address, cidr_size FROM host_pod_ref WHERE data_center_id=" + zoneId +" and removed IS NULL"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql); ResultSet rs = stmt.executeQuery(); @@ -123,7 +123,7 @@ public class EngineHostPodDaoImpl extends GenericDaoBase @Override public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); EngineHostPodVO pod = createForUpdate(); pod.setName(null); diff --git a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/HostDetailsDaoImpl.java b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/HostDetailsDaoImpl.java index e0ae778911c..e0a6dceac31 100644 --- a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/HostDetailsDaoImpl.java +++ b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/HostDetailsDaoImpl.java @@ -31,7 +31,7 @@ import com.cloud.utils.crypt.DBEncryptionUtil; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component(value="EngineHostDetailsDao") @@ -96,7 +96,7 @@ public class HostDetailsDaoImpl extends GenericDaoBase implement public void persist(long hostId, Map details) { final String InsertOrUpdateSql = "INSERT INTO `cloud`.`host_details` (host_id, name, value) VALUES (?,?,?) ON DUPLICATE KEY UPDATE value=?"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); for (Map.Entry detail : details.entrySet()) { diff --git a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/HostTagsDaoImpl.java b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/HostTagsDaoImpl.java index a70b7d1b234..17d6f811a61 100644 --- a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/HostTagsDaoImpl.java +++ b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/HostTagsDaoImpl.java @@ -27,7 +27,7 @@ import com.cloud.host.HostTagVO; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component(value="EngineHostTagsDao") @Local(value=HostTagsDao.class) @@ -56,7 +56,7 @@ public class HostTagsDaoImpl extends GenericDaoBase implements @Override public void persist(long hostId, List hostTags) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = HostSearch.create(); diff --git a/engine/orchestration/src/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java b/engine/orchestration/src/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java index 8ba3fdf983f..5636e0783b4 100755 --- a/engine/orchestration/src/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java +++ b/engine/orchestration/src/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java @@ -167,6 +167,10 @@ import com.cloud.utils.concurrency.NamedThreadFactory; import com.cloud.utils.db.DB; import com.cloud.utils.db.EntityManager; import com.cloud.utils.db.GlobalLock; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.db.JoinBuilder.JoinType; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria.Op; @@ -346,7 +350,7 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra @DB public boolean configure(final String name, final Map params) throws ConfigurationException { // populate providers - Map> defaultSharedNetworkOfferingProviders = new HashMap>(); + final Map> defaultSharedNetworkOfferingProviders = new HashMap>(); Set defaultProviders = new HashSet(); defaultProviders.add(Network.Provider.VirtualRouter); @@ -354,7 +358,7 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra defaultSharedNetworkOfferingProviders.put(Service.Dns, defaultProviders); defaultSharedNetworkOfferingProviders.put(Service.UserData, defaultProviders); - Map> defaultIsolatedNetworkOfferingProviders = defaultSharedNetworkOfferingProviders; + final Map> defaultIsolatedNetworkOfferingProviders = defaultSharedNetworkOfferingProviders; defaultIsolatedNetworkOfferingProviders.put(Service.Dhcp, defaultProviders); defaultIsolatedNetworkOfferingProviders.put(Service.Dns, defaultProviders); defaultIsolatedNetworkOfferingProviders.put(Service.UserData, defaultProviders); @@ -365,7 +369,7 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra defaultIsolatedNetworkOfferingProviders.put(Service.PortForwarding, defaultProviders); defaultIsolatedNetworkOfferingProviders.put(Service.Vpn, defaultProviders); - Map> defaultSharedSGEnabledNetworkOfferingProviders = new HashMap>(); + final Map> defaultSharedSGEnabledNetworkOfferingProviders = new HashMap>(); defaultSharedSGEnabledNetworkOfferingProviders.put(Service.Dhcp, defaultProviders); defaultSharedSGEnabledNetworkOfferingProviders.put(Service.Dns, defaultProviders); defaultSharedSGEnabledNetworkOfferingProviders.put(Service.UserData, defaultProviders); @@ -373,7 +377,7 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra sgProviders.add(Provider.SecurityGroupProvider); defaultSharedSGEnabledNetworkOfferingProviders.put(Service.SecurityGroup, sgProviders); - Map> defaultIsolatedSourceNatEnabledNetworkOfferingProviders = new HashMap>(); + final Map> defaultIsolatedSourceNatEnabledNetworkOfferingProviders = new HashMap>(); defaultProviders.clear(); defaultProviders.add(Network.Provider.VirtualRouter); defaultIsolatedSourceNatEnabledNetworkOfferingProviders.put(Service.Dhcp, defaultProviders); @@ -387,7 +391,7 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra defaultIsolatedSourceNatEnabledNetworkOfferingProviders.put(Service.PortForwarding, defaultProviders); defaultIsolatedSourceNatEnabledNetworkOfferingProviders.put(Service.Vpn, defaultProviders); - Map> defaultVPCOffProviders = new HashMap>(); + final Map> defaultVPCOffProviders = new HashMap>(); defaultProviders.clear(); defaultProviders.add(Network.Provider.VPCVirtualRouter); defaultVPCOffProviders.put(Service.Dhcp, defaultProviders); @@ -401,133 +405,134 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra defaultVPCOffProviders.put(Service.PortForwarding, defaultProviders); defaultVPCOffProviders.put(Service.Vpn, defaultProviders); - Transaction txn = Transaction.currentTxn(); - txn.start(); - - NetworkOfferingVO offering = null; - //#1 - quick cloud network offering - if (_networkOfferingDao.findByUniqueName(NetworkOffering.QuickCloudNoServices) == null) { - offering = _configMgr.createNetworkOffering(NetworkOffering.QuickCloudNoServices, "Offering for QuickCloud with no services", TrafficType.Guest, null, true, - Availability.Optional, null, new HashMap>(), true, Network.GuestType.Shared, false, null, true, null, true, false, null, + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + NetworkOfferingVO offering = null; + //#1 - quick cloud network offering + if (_networkOfferingDao.findByUniqueName(NetworkOffering.QuickCloudNoServices) == null) { + offering = _configMgr.createNetworkOffering(NetworkOffering.QuickCloudNoServices, "Offering for QuickCloud with no services", TrafficType.Guest, null, true, + Availability.Optional, null, new HashMap>(), true, Network.GuestType.Shared, false, null, true, null, true, false, null, false, null, true); - offering.setState(NetworkOffering.State.Enabled); - _networkOfferingDao.update(offering.getId(), offering); - } - - //#2 - SG enabled network offering - if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultSharedNetworkOfferingWithSGService) == null) { - offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultSharedNetworkOfferingWithSGService, "Offering for Shared Security group enabled networks", - TrafficType.Guest, null, true, Availability.Optional, null, defaultSharedNetworkOfferingProviders, true, Network.GuestType.Shared, false, null, true, null, true, + offering.setState(NetworkOffering.State.Enabled); + _networkOfferingDao.update(offering.getId(), offering); + } + + //#2 - SG enabled network offering + if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultSharedNetworkOfferingWithSGService) == null) { + offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultSharedNetworkOfferingWithSGService, "Offering for Shared Security group enabled networks", + TrafficType.Guest, null, true, Availability.Optional, null, defaultSharedNetworkOfferingProviders, true, Network.GuestType.Shared, false, null, true, null, true, false, null, false, null, true); - offering.setState(NetworkOffering.State.Enabled); - _networkOfferingDao.update(offering.getId(), offering); - } - - //#3 - shared network offering with no SG service - if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultSharedNetworkOffering) == null) { - offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultSharedNetworkOffering, "Offering for Shared networks", TrafficType.Guest, null, true, + offering.setState(NetworkOffering.State.Enabled); + _networkOfferingDao.update(offering.getId(), offering); + } + + //#3 - shared network offering with no SG service + if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultSharedNetworkOffering) == null) { + offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultSharedNetworkOffering, "Offering for Shared networks", TrafficType.Guest, null, true, Availability.Optional, null, defaultSharedNetworkOfferingProviders, true, Network.GuestType.Shared, false, null, true, null, true, false, null, false, null, true); - offering.setState(NetworkOffering.State.Enabled); - _networkOfferingDao.update(offering.getId(), offering); - } - - //#4 - default isolated offering with Source nat service - if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultIsolatedNetworkOfferingWithSourceNatService) == null) { - offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultIsolatedNetworkOfferingWithSourceNatService, - "Offering for Isolated networks with Source Nat service enabled", TrafficType.Guest, null, false, Availability.Required, null, + offering.setState(NetworkOffering.State.Enabled); + _networkOfferingDao.update(offering.getId(), offering); + } + + //#4 - default isolated offering with Source nat service + if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultIsolatedNetworkOfferingWithSourceNatService) == null) { + offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultIsolatedNetworkOfferingWithSourceNatService, + "Offering for Isolated networks with Source Nat service enabled", TrafficType.Guest, null, false, Availability.Required, null, defaultIsolatedSourceNatEnabledNetworkOfferingProviders, true, Network.GuestType.Isolated, false, null, true, null, false, false, null, false, null, true); - - offering.setState(NetworkOffering.State.Enabled); - _networkOfferingDao.update(offering.getId(), offering); - } - - //#5 - default vpc offering with LB service - if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworks) == null) { - offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworks, - "Offering for Isolated VPC networks with Source Nat service enabled", TrafficType.Guest, null, false, Availability.Optional, null, defaultVPCOffProviders, true, + + offering.setState(NetworkOffering.State.Enabled); + _networkOfferingDao.update(offering.getId(), offering); + } + + //#5 - default vpc offering with LB service + if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworks) == null) { + offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworks, + "Offering for Isolated VPC networks with Source Nat service enabled", TrafficType.Guest, null, false, Availability.Optional, null, defaultVPCOffProviders, true, Network.GuestType.Isolated, false, null, false, null, false, false, null, false, null, true); - offering.setState(NetworkOffering.State.Enabled); - _networkOfferingDao.update(offering.getId(), offering); - } - - //#6 - default vpc offering with no LB service - if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksNoLB) == null) { - //remove LB service - defaultVPCOffProviders.remove(Service.Lb); - offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksNoLB, - "Offering for Isolated VPC networks with Source Nat service enabled and LB service disabled", TrafficType.Guest, null, false, Availability.Optional, null, + offering.setState(NetworkOffering.State.Enabled); + _networkOfferingDao.update(offering.getId(), offering); + } + + //#6 - default vpc offering with no LB service + if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksNoLB) == null) { + //remove LB service + defaultVPCOffProviders.remove(Service.Lb); + offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksNoLB, + "Offering for Isolated VPC networks with Source Nat service enabled and LB service disabled", TrafficType.Guest, null, false, Availability.Optional, null, defaultVPCOffProviders, true, Network.GuestType.Isolated, false, null, false, null, false, false, null, false, null, true); - offering.setState(NetworkOffering.State.Enabled); - _networkOfferingDao.update(offering.getId(), offering); - } - - //#7 - isolated offering with source nat disabled - if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultIsolatedNetworkOffering) == null) { - offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultIsolatedNetworkOffering, "Offering for Isolated networks with no Source Nat service", - TrafficType.Guest, null, true, Availability.Optional, null, defaultIsolatedNetworkOfferingProviders, true, Network.GuestType.Isolated, false, null, true, null, + offering.setState(NetworkOffering.State.Enabled); + _networkOfferingDao.update(offering.getId(), offering); + } + + //#7 - isolated offering with source nat disabled + if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultIsolatedNetworkOffering) == null) { + offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultIsolatedNetworkOffering, "Offering for Isolated networks with no Source Nat service", + TrafficType.Guest, null, true, Availability.Optional, null, defaultIsolatedNetworkOfferingProviders, true, Network.GuestType.Isolated, false, null, true, null, true, false, null, false, null, true); - offering.setState(NetworkOffering.State.Enabled); - _networkOfferingDao.update(offering.getId(), offering); - } - - //#8 - network offering with internal lb service - Map> internalLbOffProviders = new HashMap>(); - Set defaultVpcProvider = new HashSet(); - defaultVpcProvider.add(Network.Provider.VPCVirtualRouter); - - Set defaultInternalLbProvider = new HashSet(); - defaultInternalLbProvider.add(Network.Provider.InternalLbVm); - - internalLbOffProviders.put(Service.Dhcp, defaultVpcProvider); - internalLbOffProviders.put(Service.Dns, defaultVpcProvider); - internalLbOffProviders.put(Service.UserData, defaultVpcProvider); - internalLbOffProviders.put(Service.NetworkACL, defaultVpcProvider); - internalLbOffProviders.put(Service.Gateway, defaultVpcProvider); - internalLbOffProviders.put(Service.Lb, defaultInternalLbProvider); - internalLbOffProviders.put(Service.SourceNat, defaultVpcProvider); - - if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksWithInternalLB) == null) { - offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksWithInternalLB, - "Offering for Isolated VPC networks with Internal Lb support", TrafficType.Guest, null, false, Availability.Optional, null, internalLbOffProviders, true, + offering.setState(NetworkOffering.State.Enabled); + _networkOfferingDao.update(offering.getId(), offering); + } + + //#8 - network offering with internal lb service + Map> internalLbOffProviders = new HashMap>(); + Set defaultVpcProvider = new HashSet(); + defaultVpcProvider.add(Network.Provider.VPCVirtualRouter); + + Set defaultInternalLbProvider = new HashSet(); + defaultInternalLbProvider.add(Network.Provider.InternalLbVm); + + internalLbOffProviders.put(Service.Dhcp, defaultVpcProvider); + internalLbOffProviders.put(Service.Dns, defaultVpcProvider); + internalLbOffProviders.put(Service.UserData, defaultVpcProvider); + internalLbOffProviders.put(Service.NetworkACL, defaultVpcProvider); + internalLbOffProviders.put(Service.Gateway, defaultVpcProvider); + internalLbOffProviders.put(Service.Lb, defaultInternalLbProvider); + internalLbOffProviders.put(Service.SourceNat, defaultVpcProvider); + + if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksWithInternalLB) == null) { + offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksWithInternalLB, + "Offering for Isolated VPC networks with Internal Lb support", TrafficType.Guest, null, false, Availability.Optional, null, internalLbOffProviders, true, Network.GuestType.Isolated, false, null, false, null, false, false, null, false, null, true); - offering.setState(NetworkOffering.State.Enabled); - offering.setInternalLb(true); - offering.setPublicLb(false); - _networkOfferingDao.update(offering.getId(), offering); - } - - Map> netscalerServiceProviders = new HashMap>(); - Set vrProvider = new HashSet(); - vrProvider.add(Provider.VirtualRouter); - Set sgProvider = new HashSet(); - sgProvider.add(Provider.SecurityGroupProvider); - Set nsProvider = new HashSet(); - nsProvider.add(Provider.Netscaler); - netscalerServiceProviders.put(Service.Dhcp, vrProvider); - netscalerServiceProviders.put(Service.Dns, vrProvider); - netscalerServiceProviders.put(Service.UserData, vrProvider); - netscalerServiceProviders.put(Service.SecurityGroup, sgProvider); - netscalerServiceProviders.put(Service.StaticNat, nsProvider); - netscalerServiceProviders.put(Service.Lb, nsProvider); - - Map> serviceCapabilityMap = new HashMap>(); - Map elb = new HashMap(); - elb.put(Capability.ElasticLb, "true"); - Map eip = new HashMap(); - eip.put(Capability.ElasticIp, "true"); - serviceCapabilityMap.put(Service.Lb, elb); - serviceCapabilityMap.put(Service.StaticNat, eip); - - if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultSharedEIPandELBNetworkOffering) == null) { - offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultSharedEIPandELBNetworkOffering, - "Offering for Shared networks with Elastic IP and Elastic LB capabilities", TrafficType.Guest, null, true, Availability.Optional, null, netscalerServiceProviders, + offering.setState(NetworkOffering.State.Enabled); + offering.setInternalLb(true); + offering.setPublicLb(false); + _networkOfferingDao.update(offering.getId(), offering); + } + + Map> netscalerServiceProviders = new HashMap>(); + Set vrProvider = new HashSet(); + vrProvider.add(Provider.VirtualRouter); + Set sgProvider = new HashSet(); + sgProvider.add(Provider.SecurityGroupProvider); + Set nsProvider = new HashSet(); + nsProvider.add(Provider.Netscaler); + netscalerServiceProviders.put(Service.Dhcp, vrProvider); + netscalerServiceProviders.put(Service.Dns, vrProvider); + netscalerServiceProviders.put(Service.UserData, vrProvider); + netscalerServiceProviders.put(Service.SecurityGroup, sgProvider); + netscalerServiceProviders.put(Service.StaticNat, nsProvider); + netscalerServiceProviders.put(Service.Lb, nsProvider); + + Map> serviceCapabilityMap = new HashMap>(); + Map elb = new HashMap(); + elb.put(Capability.ElasticLb, "true"); + Map eip = new HashMap(); + eip.put(Capability.ElasticIp, "true"); + serviceCapabilityMap.put(Service.Lb, elb); + serviceCapabilityMap.put(Service.StaticNat, eip); + + if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultSharedEIPandELBNetworkOffering) == null) { + offering = _configMgr.createNetworkOffering(NetworkOffering.DefaultSharedEIPandELBNetworkOffering, + "Offering for Shared networks with Elastic IP and Elastic LB capabilities", TrafficType.Guest, null, true, Availability.Optional, null, netscalerServiceProviders, true, Network.GuestType.Shared, false, null, true, serviceCapabilityMap, true, false, null, false, null, true); - offering.setState(NetworkOffering.State.Enabled); - offering.setDedicatedLB(false); - _networkOfferingDao.update(offering.getId(), offering); - } + offering.setState(NetworkOffering.State.Enabled); + offering.setDedicatedLB(false); + _networkOfferingDao.update(offering.getId(), offering); + } + } + }); - txn.commit(); AssignIpAddressSearch = _ipAddressDao.createSearchBuilder(); AssignIpAddressSearch.and("dc", AssignIpAddressSearch.entity().getDataCenterId(), Op.EQ); @@ -590,8 +595,8 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra @Override @DB - public List setupNetwork(Account owner, NetworkOffering offering, Network predefined, DeploymentPlan plan, String name, String displayText, - boolean errorIfAlreadySetup, Long domainId, ACLType aclType, Boolean subdomainAccess, Long vpcId, Boolean isDisplayNetworkEnabled) throws ConcurrentOperationException { + public List setupNetwork(final Account owner, final NetworkOffering offering, final Network predefined, final DeploymentPlan plan, final String name, final String displayText, + boolean errorIfAlreadySetup, final Long domainId, final ACLType aclType, final Boolean subdomainAccess, final Long vpcId, final Boolean isDisplayNetworkEnabled) throws ConcurrentOperationException { Account locked = _accountDao.acquireInLockTable(owner.getId()); if (locked == null) { @@ -622,12 +627,12 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra } } - List networks = new ArrayList(); + final List networks = new ArrayList(); long related = -1; - for (NetworkGuru guru : _networkGurus) { - Network network = guru.design(offering, plan, predefined, owner); + for (final NetworkGuru guru : _networkGurus) { + final Network network = guru.design(offering, plan, predefined, owner); if (network == null) { continue; } @@ -641,28 +646,26 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra continue; } - long id = _networksDao.getNextInSequence(Long.class, "id"); + final long id = _networksDao.getNextInSequence(Long.class, "id"); if (related == -1) { related = id; } - Transaction txn = Transaction.currentTxn(); - txn.start(); - - NetworkVO vo = new NetworkVO(id, network, offering.getId(), guru.getName(), owner.getDomainId(), owner.getId(), related, name, displayText, - predefined.getNetworkDomain(), offering.getGuestType(), plan.getDataCenterId(), plan.getPhysicalNetworkId(), aclType, offering.getSpecifyIpRanges(), vpcId); - vo.setDisplayNetwork(isDisplayNetworkEnabled == null ? true : isDisplayNetworkEnabled); - networks.add(_networksDao.persist(vo, vo.getGuestType() == Network.GuestType.Isolated, - finalizeServicesAndProvidersForNetwork(offering, plan.getPhysicalNetworkId()))); - - if (domainId != null && aclType == ACLType.Domain) { - if (subdomainAccess == null) { - subdomainAccess = true; + final long relatedFile = related; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + NetworkVO vo = new NetworkVO(id, network, offering.getId(), guru.getName(), owner.getDomainId(), owner.getId(), relatedFile, name, displayText, + predefined.getNetworkDomain(), offering.getGuestType(), plan.getDataCenterId(), plan.getPhysicalNetworkId(), aclType, offering.getSpecifyIpRanges(), vpcId); + vo.setDisplayNetwork(isDisplayNetworkEnabled == null ? true : isDisplayNetworkEnabled); + networks.add(_networksDao.persist(vo, vo.getGuestType() == Network.GuestType.Isolated, + finalizeServicesAndProvidersForNetwork(offering, plan.getPhysicalNetworkId()))); + + if (domainId != null && aclType == ACLType.Domain) { + _networksDao.addDomainToNetwork(id, domainId, subdomainAccess == null ? true : subdomainAccess); + } } - _networksDao.addDomainToNetwork(id, domainId, subdomainAccess); - } - - txn.commit(); + }); } if (networks.size() < 1) { @@ -681,73 +684,74 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra @Override @DB - public void allocate(VirtualMachineProfile vm, LinkedHashMap networks) throws InsufficientCapacityException, + public void allocate(final VirtualMachineProfile vm, final LinkedHashMap networks) throws InsufficientCapacityException, ConcurrentOperationException { - Transaction txn = Transaction.currentTxn(); - txn.start(); - int deviceId = 0; - - boolean[] deviceIds = new boolean[networks.size()]; - Arrays.fill(deviceIds, false); - - List nics = new ArrayList(networks.size()); - NicProfile defaultNic = null; - - for (Map.Entry network : networks.entrySet()) { - Network config = network.getKey(); - NicProfile requested = network.getValue(); - - Boolean isDefaultNic = false; - if (vm != null && (requested != null && requested.isDefaultNic())) { - isDefaultNic = true; - } - - while (deviceIds[deviceId] && deviceId < deviceIds.length) { - deviceId++; - } - - Pair vmNicPair = allocateNic(requested, config, isDefaultNic, deviceId, vm); - - NicProfile vmNic = vmNicPair.first(); - if (vmNic == null) { - continue; - } - - deviceId = vmNicPair.second(); - - int devId = vmNic.getDeviceId(); - if (devId > deviceIds.length) { - throw new IllegalArgumentException("Device id for nic is too large: " + vmNic); - } - if (deviceIds[devId]) { - throw new IllegalArgumentException("Conflicting device id for two different nics: " + vmNic); - } - - deviceIds[devId] = true; - - if (vmNic.isDefaultNic()) { - if (defaultNic != null) { - throw new IllegalArgumentException("You cannot specify two nics as default nics: nic 1 = " + defaultNic + "; nic 2 = " + vmNic); + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws InsufficientCapacityException { + int deviceId = 0; + + boolean[] deviceIds = new boolean[networks.size()]; + Arrays.fill(deviceIds, false); + + List nics = new ArrayList(networks.size()); + NicProfile defaultNic = null; + + for (Map.Entry network : networks.entrySet()) { + Network config = network.getKey(); + NicProfile requested = network.getValue(); + + Boolean isDefaultNic = false; + if (vm != null && (requested != null && requested.isDefaultNic())) { + isDefaultNic = true; + } + + while (deviceIds[deviceId] && deviceId < deviceIds.length) { + deviceId++; + } + + Pair vmNicPair = allocateNic(requested, config, isDefaultNic, deviceId, vm); + + NicProfile vmNic = vmNicPair.first(); + if (vmNic == null) { + continue; + } + + deviceId = vmNicPair.second(); + + int devId = vmNic.getDeviceId(); + if (devId > deviceIds.length) { + throw new IllegalArgumentException("Device id for nic is too large: " + vmNic); + } + if (deviceIds[devId]) { + throw new IllegalArgumentException("Conflicting device id for two different nics: " + vmNic); + } + + deviceIds[devId] = true; + + if (vmNic.isDefaultNic()) { + if (defaultNic != null) { + throw new IllegalArgumentException("You cannot specify two nics as default nics: nic 1 = " + defaultNic + "; nic 2 = " + vmNic); + } + defaultNic = vmNic; + } + + nics.add(vmNic); + vm.addNic(vmNic); + + } + + if (nics.size() != networks.size()) { + s_logger.warn("Number of nics " + nics.size() + " doesn't match number of requested networks " + networks.size()); + throw new CloudRuntimeException("Number of nics " + nics.size() + " doesn't match number of requested networks " + networks.size()); + } + + if (nics.size() == 1) { + nics.get(0).setDefaultNic(true); } - defaultNic = vmNic; } - - nics.add(vmNic); - vm.addNic(vmNic); - - } - - if (nics.size() != networks.size()) { - s_logger.warn("Number of nics " + nics.size() + " doesn't match number of requested networks " + networks.size()); - throw new CloudRuntimeException("Number of nics " + nics.size() + " doesn't match number of requested networks " + networks.size()); - } - - if (nics.size() == 1) { - nics.get(0).setDefaultNic(true); - } - - txn.commit(); + }); } @DB @@ -900,7 +904,6 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra @DB public Pair implementNetwork(long networkId, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { - Transaction.currentTxn(); Pair implemented = new Pair(null, null); NetworkVO network = _networksDao.findById(networkId); @@ -1176,22 +1179,23 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra } @DB - protected void updateNic(NicVO nic, long networkId, int count) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - _nicDao.update(nic.getId(), nic); - - if (nic.getVmType() == VirtualMachine.Type.User) { - s_logger.debug("Changing active number of nics for network id=" + networkId + " on " + count); - _networksDao.changeActiveNicsBy(networkId, count); - } - - if (nic.getVmType() == VirtualMachine.Type.User || - (nic.getVmType() == VirtualMachine.Type.DomainRouter && _networksDao.findById(networkId).getTrafficType() == TrafficType.Guest)) { - _networksDao.setCheckForGc(networkId); - } - - txn.commit(); + protected void updateNic(final NicVO nic, final long networkId, final int count) { + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _nicDao.update(nic.getId(), nic); + + if (nic.getVmType() == VirtualMachine.Type.User) { + s_logger.debug("Changing active number of nics for network id=" + networkId + " on " + count); + _networksDao.changeActiveNicsBy(networkId, count); + } + + if (nic.getVmType() == VirtualMachine.Type.User || + (nic.getVmType() == VirtualMachine.Type.DomainRouter && _networksDao.findById(networkId).getTrafficType() == TrafficType.Guest)) { + _networksDao.setCheckForGc(networkId); + } + } + }); } @Override @@ -1414,59 +1418,63 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra } @DB - protected void releaseNic(VirtualMachineProfile vmProfile, long nicId) throws ConcurrentOperationException, ResourceUnavailableException { - //lock the nic - Transaction txn = Transaction.currentTxn(); - txn.start(); + protected void releaseNic(final VirtualMachineProfile vmProfile, final long nicId) throws ConcurrentOperationException, ResourceUnavailableException { + Pair networkToRelease = Transaction.execute(new TransactionCallback>() { + @Override + public Pair doInTransaction(TransactionStatus status) { + NicVO nic = _nicDao.lockRow(nicId, true); + if (nic == null) { + throw new ConcurrentOperationException("Unable to acquire lock on nic " + nic); + } - NicVO nic = _nicDao.lockRow(nicId, true); - if (nic == null) { - throw new ConcurrentOperationException("Unable to acquire lock on nic " + nic); - } + Nic.State originalState = nic.getState(); + NetworkVO network = _networksDao.findById(nic.getNetworkId()); - Nic.State originalState = nic.getState(); - NetworkVO network = _networksDao.findById(nic.getNetworkId()); - - if (originalState == Nic.State.Reserved || originalState == Nic.State.Reserving) { - if (nic.getReservationStrategy() == Nic.ReservationStrategy.Start) { - NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, network.getGuruName()); - nic.setState(Nic.State.Releasing); - _nicDao.update(nic.getId(), nic); - NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, _networkModel.isSecurityGroupSupportedInNetwork(network), - _networkModel.getNetworkTag(vmProfile.getHypervisorType(), network)); - if (guru.release(profile, vmProfile, nic.getReservationId())) { - applyProfileToNicForRelease(nic, profile); - nic.setState(Nic.State.Allocated); - if (originalState == Nic.State.Reserved) { - updateNic(nic, network.getId(), -1); - } else { + if (originalState == Nic.State.Reserved || originalState == Nic.State.Reserving) { + if (nic.getReservationStrategy() == Nic.ReservationStrategy.Start) { + NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, network.getGuruName()); + nic.setState(Nic.State.Releasing); _nicDao.update(nic.getId(), nic); - } - } - //commit the transaction before proceeding releasing nic profile on the network elements - txn.commit(); - - // Perform release on network elements - List providersToImplement = getNetworkProviders(network.getId()); - for (NetworkElement element : _networkElements) { - if (providersToImplement.contains(element.getProvider())) { - if (!_networkModel.isProviderEnabledInPhysicalNetwork(_networkModel.getPhysicalNetworkId(network), element.getProvider().getName())) { - throw new CloudRuntimeException("Service provider " + element.getProvider().getName() + - " either doesn't exist or is not enabled in physical network id: " + network.getPhysicalNetworkId()); + NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, _networkModel.isSecurityGroupSupportedInNetwork(network), + _networkModel.getNetworkTag(vmProfile.getHypervisorType(), network)); + if (guru.release(profile, vmProfile, nic.getReservationId())) { + applyProfileToNicForRelease(nic, profile); + nic.setState(Nic.State.Allocated); + if (originalState == Nic.State.Reserved) { + updateNic(nic, network.getId(), -1); + } else { + _nicDao.update(nic.getId(), nic); + } } - if (s_logger.isDebugEnabled()) { - s_logger.debug("Asking " + element.getName() + " to release " + nic); - } - //NOTE: Context appear to never be used in release method - //implementations. Consider removing it from interface Element - element.release(network, profile, vmProfile, null); + // Perform release on network elements + return new Pair(network, profile); + } else { + nic.setState(Nic.State.Allocated); + updateNic(nic, network.getId(), -1); } } - } else { - nic.setState(Nic.State.Allocated); - updateNic(nic, network.getId(), -1); - txn.commit(); + return null; + } + }); + + if (networkToRelease != null) { + Network network = networkToRelease.first(); + NicProfile profile = networkToRelease.second(); + List providersToImplement = getNetworkProviders(network.getId()); + for (NetworkElement element : _networkElements) { + if (providersToImplement.contains(element.getProvider())) { + if (!_networkModel.isProviderEnabledInPhysicalNetwork(_networkModel.getPhysicalNetworkId(network), element.getProvider().getName())) { + throw new CloudRuntimeException("Service provider " + element.getProvider().getName() + + " either doesn't exist or is not enabled in physical network id: " + network.getPhysicalNetworkId()); + } + if (s_logger.isDebugEnabled()) { + s_logger.debug("Asking " + element.getName() + " to release " + profile); + } + //NOTE: Context appear to never be used in release method + //implementations. Consider removing it from interface Element + element.release(network, profile, vmProfile, null); + } } } } @@ -1563,15 +1571,17 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra Network network = _networksDao.findById(nic.getNetworkId()); DhcpServiceProvider dhcpServiceProvider = getDhcpServiceProvider(network); try { - NicIpAliasVO ipAlias = _nicIpAliasDao.findByGatewayAndNetworkIdAndState(nic.getGateway(), network.getId(), NicIpAlias.state.active); + final NicIpAliasVO ipAlias = _nicIpAliasDao.findByGatewayAndNetworkIdAndState(nic.getGateway(), network.getId(), NicIpAlias.state.active); if (ipAlias != null) { ipAlias.setState(NicIpAlias.state.revoked); - Transaction txn = Transaction.currentTxn(); - txn.start(); - _nicIpAliasDao.update(ipAlias.getId(), ipAlias); - IPAddressVO aliasIpaddressVo = _publicIpAddressDao.findByIpAndSourceNetworkId(ipAlias.getNetworkId(), ipAlias.getIp4Address()); - _publicIpAddressDao.unassignIpAddress(aliasIpaddressVo.getId()); - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _nicIpAliasDao.update(ipAlias.getId(), ipAlias); + IPAddressVO aliasIpaddressVo = _publicIpAddressDao.findByIpAndSourceNetworkId(ipAlias.getNetworkId(), ipAlias.getIp4Address()); + _publicIpAddressDao.unassignIpAddress(aliasIpaddressVo.getId()); + } + }); if (!dhcpServiceProvider.removeDhcpSupportForSubnet(network)) { s_logger.warn("Failed to remove the ip alias on the router, marking it as removed in db and freed the allocated ip " + ipAlias.getIp4Address()); } @@ -1593,18 +1603,18 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra @Override @DB - public Network createGuestNetwork(long networkOfferingId, String name, String displayText, String gateway, String cidr, String vlanId, String networkDomain, Account owner, - Long domainId, PhysicalNetwork pNtwk, long zoneId, ACLType aclType, Boolean subdomainAccess, Long vpcId, String ip6Gateway, String ip6Cidr, - Boolean isDisplayNetworkEnabled, String isolatedPvlan) throws ConcurrentOperationException, InsufficientCapacityException, ResourceAllocationException { + public Network createGuestNetwork(long networkOfferingId, final String name, final String displayText, final String gateway, final String cidr, String vlanId, String networkDomain, final Account owner, + final Long domainId, final PhysicalNetwork pNtwk, final long zoneId, final ACLType aclType, Boolean subdomainAccess, final Long vpcId, final String ip6Gateway, final String ip6Cidr, + final Boolean isDisplayNetworkEnabled, final String isolatedPvlan) throws ConcurrentOperationException, InsufficientCapacityException, ResourceAllocationException { - NetworkOfferingVO ntwkOff = _networkOfferingDao.findById(networkOfferingId); + final NetworkOfferingVO ntwkOff = _networkOfferingDao.findById(networkOfferingId); // this method supports only guest network creation if (ntwkOff.getTrafficType() != TrafficType.Guest) { s_logger.warn("Only guest networks can be created using this method"); return null; } - boolean updateResourceCount = resourceCountNeedsUpdate(ntwkOff, aclType); + final boolean updateResourceCount = resourceCountNeedsUpdate(ntwkOff, aclType); //check resource limits if (updateResourceCount) { _resourceLimitMgr.checkResourceLimit(owner, ResourceType.network); @@ -1632,7 +1642,7 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra ipv6 = true; } // Validate zone - DataCenterVO zone = _dcDao.findById(zoneId); + final DataCenterVO zone = _dcDao.findById(zoneId); if (zone.getNetworkType() == NetworkType.Basic) { if (ipv6) { throw new InvalidParameterValueException("IPv6 is not supported in Basic zone"); @@ -1819,79 +1829,84 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra } } - Transaction txn = Transaction.currentTxn(); - txn.start(); - - Long physicalNetworkId = null; - if (pNtwk != null) { - physicalNetworkId = pNtwk.getId(); - } - DataCenterDeployment plan = new DataCenterDeployment(zoneId, null, null, null, null, physicalNetworkId); - NetworkVO userNetwork = new NetworkVO(); - userNetwork.setNetworkDomain(networkDomain); - - if (cidr != null && gateway != null) { - userNetwork.setCidr(cidr); - userNetwork.setGateway(gateway); - } - - if (ip6Cidr != null && ip6Gateway != null) { - userNetwork.setIp6Cidr(ip6Cidr); - userNetwork.setIp6Gateway(ip6Gateway); - } - - if (vlanId != null) { - if (isolatedPvlan == null) { - URI uri = BroadcastDomainType.fromString(vlanId); - userNetwork.setBroadcastUri(uri); - if (!vlanId.equalsIgnoreCase(Vlan.UNTAGGED)) { - userNetwork.setBroadcastDomainType(BroadcastDomainType.Vlan); - } else { - userNetwork.setBroadcastDomainType(BroadcastDomainType.Native); - } - } else { - if (vlanId.equalsIgnoreCase(Vlan.UNTAGGED)) { - throw new InvalidParameterValueException("Cannot support pvlan with untagged primary vlan!"); - } - userNetwork.setBroadcastUri(NetUtils.generateUriForPvlan(vlanId, isolatedPvlan)); - userNetwork.setBroadcastDomainType(BroadcastDomainType.Pvlan); - } - } + final String networkDomainFinal = networkDomain; + final String vlanIdFinal = vlanId; + final Boolean subdomainAccessFinal = subdomainAccess; + Network network = Transaction.execute(new TransactionCallback() { + @Override + public Network doInTransaction(TransactionStatus status) { + Long physicalNetworkId = null; + if (pNtwk != null) { + physicalNetworkId = pNtwk.getId(); + } + DataCenterDeployment plan = new DataCenterDeployment(zoneId, null, null, null, null, physicalNetworkId); + NetworkVO userNetwork = new NetworkVO(); + userNetwork.setNetworkDomain(networkDomainFinal); - List networks = setupNetwork(owner, ntwkOff, userNetwork, plan, name, displayText, true, domainId, aclType, subdomainAccess, vpcId, - isDisplayNetworkEnabled); - - Network network = null; - if (networks == null || networks.isEmpty()) { - throw new CloudRuntimeException("Fail to create a network"); - } else { - if (networks.size() > 0 && networks.get(0).getGuestType() == Network.GuestType.Isolated && networks.get(0).getTrafficType() == TrafficType.Guest) { - Network defaultGuestNetwork = networks.get(0); - for (Network nw : networks) { - if (nw.getCidr() != null && nw.getCidr().equals(zone.getGuestNetworkCidr())) { - defaultGuestNetwork = nw; + if (cidr != null && gateway != null) { + userNetwork.setCidr(cidr); + userNetwork.setGateway(gateway); + } + + if (ip6Cidr != null && ip6Gateway != null) { + userNetwork.setIp6Cidr(ip6Cidr); + userNetwork.setIp6Gateway(ip6Gateway); + } + + if (vlanIdFinal != null) { + if (isolatedPvlan == null) { + URI uri = BroadcastDomainType.fromString(vlanIdFinal); + userNetwork.setBroadcastUri(uri); + if (!vlanIdFinal.equalsIgnoreCase(Vlan.UNTAGGED)) { + userNetwork.setBroadcastDomainType(BroadcastDomainType.Vlan); + } else { + userNetwork.setBroadcastDomainType(BroadcastDomainType.Native); + } + } else { + if (vlanIdFinal.equalsIgnoreCase(Vlan.UNTAGGED)) { + throw new InvalidParameterValueException("Cannot support pvlan with untagged primary vlan!"); + } + userNetwork.setBroadcastUri(NetUtils.generateUriForPvlan(vlanIdFinal, isolatedPvlan)); + userNetwork.setBroadcastDomainType(BroadcastDomainType.Pvlan); + } + } + + List networks = setupNetwork(owner, ntwkOff, userNetwork, plan, name, displayText, true, domainId, aclType, subdomainAccessFinal, vpcId, + isDisplayNetworkEnabled); + + Network network = null; + if (networks == null || networks.isEmpty()) { + throw new CloudRuntimeException("Fail to create a network"); + } else { + if (networks.size() > 0 && networks.get(0).getGuestType() == Network.GuestType.Isolated && networks.get(0).getTrafficType() == TrafficType.Guest) { + Network defaultGuestNetwork = networks.get(0); + for (Network nw : networks) { + if (nw.getCidr() != null && nw.getCidr().equals(zone.getGuestNetworkCidr())) { + defaultGuestNetwork = nw; + } + } + network = defaultGuestNetwork; + } else { + // For shared network + network = networks.get(0); } } - network = defaultGuestNetwork; - } else { - // For shared network - network = networks.get(0); + + if (updateResourceCount) { + _resourceLimitMgr.incrementResourceCount(owner.getId(), ResourceType.network); + } + + return network; } - } + }); - if (updateResourceCount) { - _resourceLimitMgr.incrementResourceCount(owner.getId(), ResourceType.network); - } - - txn.commit(); CallContext.current().setEventDetails("Network Id: " + network.getId()); return network; } @Override @DB - public boolean shutdownNetwork(long networkId, ReservationContext context, boolean cleanupElements) { - boolean result = false; + public boolean shutdownNetwork(final long networkId, ReservationContext context, boolean cleanupElements) { NetworkVO network = _networksDao.findById(networkId); if (network.getState() == Network.State.Allocated) { s_logger.debug("Network is already shutdown: " + network); @@ -1936,43 +1951,51 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra } } - boolean success = shutdownNetworkElementsAndResources(context, cleanupElements, network); + final boolean success = shutdownNetworkElementsAndResources(context, cleanupElements, network); - Transaction txn = Transaction.currentTxn(); - txn.start(); - if (success) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Network id=" + networkId + " is shutdown successfully, cleaning up corresponding resources now."); - } - NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, network.getGuruName()); - NetworkProfile profile = convertNetworkToNetworkProfile(network.getId()); - guru.shutdown(profile, _networkOfferingDao.findById(network.getNetworkOfferingId())); + final NetworkVO networkFinal = network; + boolean result = Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + boolean result = false; - applyProfileToNetwork(network, profile); - DataCenterVO zone = _dcDao.findById(network.getDataCenterId()); - if (isSharedNetworkOfferingWithServices(network.getNetworkOfferingId()) && (zone.getNetworkType() == NetworkType.Advanced)) { - network.setState(Network.State.Setup); - } else { - try { - stateTransitTo(network, Event.OperationSucceeded); - } catch (NoTransitionException e) { - network.setState(Network.State.Allocated); - network.setRestartRequired(false); + if (success) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Network id=" + networkId + " is shutdown successfully, cleaning up corresponding resources now."); + } + NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, networkFinal.getGuruName()); + NetworkProfile profile = convertNetworkToNetworkProfile(networkFinal.getId()); + guru.shutdown(profile, _networkOfferingDao.findById(networkFinal.getNetworkOfferingId())); + + applyProfileToNetwork(networkFinal, profile); + DataCenterVO zone = _dcDao.findById(networkFinal.getDataCenterId()); + if (isSharedNetworkOfferingWithServices(networkFinal.getNetworkOfferingId()) && (zone.getNetworkType() == NetworkType.Advanced)) { + networkFinal.setState(Network.State.Setup); + } else { + try { + stateTransitTo(networkFinal, Event.OperationSucceeded); + } catch (NoTransitionException e) { + networkFinal.setState(Network.State.Allocated); + networkFinal.setRestartRequired(false); + } + } + _networksDao.update(networkFinal.getId(), networkFinal); + _networksDao.clearCheckForGc(networkId); + result = true; + } else { + try { + stateTransitTo(networkFinal, Event.OperationFailed); + } catch (NoTransitionException e) { + networkFinal.setState(Network.State.Implemented); + _networksDao.update(networkFinal.getId(), networkFinal); + } + result = false; } + + return result; } - _networksDao.update(network.getId(), network); - _networksDao.clearCheckForGc(networkId); - result = true; - } else { - try { - stateTransitTo(network, Event.OperationFailed); - } catch (NoTransitionException e) { - network.setState(Network.State.Implemented); - _networksDao.update(network.getId(), network); - } - result = false; - } - txn.commit(); + }); + return result; } finally { if (network != null) { @@ -2036,8 +2059,8 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra @Override @DB - public boolean destroyNetwork(long networkId, ReservationContext context) { - Account callerAccount = context.getAccount(); + public boolean destroyNetwork(long networkId, final ReservationContext context) { + final Account callerAccount = context.getAccount(); NetworkVO network = _networksDao.findById(networkId); if (network == null) { @@ -2126,38 +2149,48 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra if (s_logger.isDebugEnabled()) { s_logger.debug("Network id=" + networkId + " is destroyed successfully, cleaning up corresponding resources now."); } - NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, network.getGuruName()); - Transaction txn = Transaction.currentTxn(); - txn.start(); - guru.trash(network, _networkOfferingDao.findById(network.getNetworkOfferingId())); - - if (!deleteVlansInNetwork(network.getId(), context.getCaller().getId(), callerAccount)) { - success = false; - s_logger.warn("Failed to delete network " + network + "; was unable to cleanup corresponding ip ranges"); - } else { - // commit transaction only when ips and vlans for the network are released successfully - try { - stateTransitTo(network, Event.DestroyNetwork); - } catch (NoTransitionException e) { - s_logger.debug(e.getMessage()); - } - if (_networksDao.remove(network.getId())) { - NetworkDomainVO networkDomain = _networkDomainDao.getDomainNetworkMapByNetworkId(network.getId()); - if (networkDomain != null) - _networkDomainDao.remove(networkDomain.getId()); - - NetworkAccountVO networkAccount = _networkAccountDao.getAccountNetworkMapByNetworkId(network.getId()); - if (networkAccount != null) - _networkAccountDao.remove(networkAccount.getId()); - } - - NetworkOffering ntwkOff = _entityMgr.findById(NetworkOffering.class, network.getNetworkOfferingId()); - boolean updateResourceCount = resourceCountNeedsUpdate(ntwkOff, network.getAclType()); - if (updateResourceCount) { - _resourceLimitMgr.decrementResourceCount(network.getAccountId(), ResourceType.network); - } - txn.commit(); + final NetworkVO networkFinal = network; + try { + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, networkFinal.getGuruName()); + + guru.trash(networkFinal, _networkOfferingDao.findById(networkFinal.getNetworkOfferingId())); + + if (!deleteVlansInNetwork(networkFinal.getId(), context.getCaller().getId(), callerAccount)) { + s_logger.warn("Failed to delete network " + networkFinal + "; was unable to cleanup corresponding ip ranges"); + throw new CloudRuntimeException("Failed to delete network " + networkFinal + "; was unable to cleanup corresponding ip ranges"); + } else { + // commit transaction only when ips and vlans for the network are released successfully + try { + stateTransitTo(networkFinal, Event.DestroyNetwork); + } catch (NoTransitionException e) { + s_logger.debug(e.getMessage()); + } + if (_networksDao.remove(networkFinal.getId())) { + NetworkDomainVO networkDomain = _networkDomainDao.getDomainNetworkMapByNetworkId(networkFinal.getId()); + if (networkDomain != null) + _networkDomainDao.remove(networkDomain.getId()); + + NetworkAccountVO networkAccount = _networkAccountDao.getAccountNetworkMapByNetworkId(networkFinal.getId()); + if (networkAccount != null) + _networkAccountDao.remove(networkAccount.getId()); + } + + NetworkOffering ntwkOff = _entityMgr.findById(NetworkOffering.class, networkFinal.getNetworkOfferingId()); + boolean updateResourceCount = resourceCountNeedsUpdate(ntwkOff, networkFinal.getAclType()); + if (updateResourceCount) { + _resourceLimitMgr.decrementResourceCount(networkFinal.getAccountId(), ResourceType.network); + } + } + } + }); + return false; + } catch ( CloudRuntimeException e ) { + s_logger.error("Failed to delete network", e); + return false; } } @@ -2426,24 +2459,22 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra @DB @Override - public boolean reallocate(VirtualMachineProfile vm, DataCenterDeployment dest) throws InsufficientCapacityException, ConcurrentOperationException { + public boolean reallocate(final VirtualMachineProfile vm, DataCenterDeployment dest) throws InsufficientCapacityException, ConcurrentOperationException { VMInstanceVO vmInstance = _vmDao.findById(vm.getId()); DataCenterVO dc = _dcDao.findById(vmInstance.getDataCenterId()); if (dc.getNetworkType() == NetworkType.Basic) { List nics = _nicDao.listByVmId(vmInstance.getId()); NetworkVO network = _networksDao.findById(nics.get(0).getNetworkId()); - LinkedHashMap profiles = new LinkedHashMap(); + final LinkedHashMap profiles = new LinkedHashMap(); profiles.put(network, null); - Transaction txn = Transaction.currentTxn(); - txn.start(); - - try { - cleanupNics(vm); - allocate(vm, profiles); - } finally { - txn.commit(); - } + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws InsufficientCapacityException { + cleanupNics(vm); + allocate(vm, profiles); + } + }); } return true; } @@ -3050,17 +3081,20 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra return nic.getSecondaryIp(); } - private boolean removeVmSecondaryIpsOfNic(long nicId) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - List ipList = _nicSecondaryIpDao.listByNicId(nicId); - if (ipList != null) { - for (NicSecondaryIpVO ip : ipList) { - _nicSecondaryIpDao.remove(ip.getId()); + private boolean removeVmSecondaryIpsOfNic(final long nicId) { + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + List ipList = _nicSecondaryIpDao.listByNicId(nicId); + if (ipList != null) { + for (NicSecondaryIpVO ip : ipList) { + _nicSecondaryIpDao.remove(ip.getId()); + } + s_logger.debug("Revoving nic secondary ip entry ..."); + } } - s_logger.debug("Revoving nic secondary ip entry ..."); - } - txn.commit(); + }); + return true; } diff --git a/engine/orchestration/src/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java b/engine/orchestration/src/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java index 0817393ab9c..7d812325cf5 100644 --- a/engine/orchestration/src/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java +++ b/engine/orchestration/src/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java @@ -31,7 +31,6 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; - import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService; import org.apache.cloudstack.engine.subsystem.api.storage.DataStore; import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager; @@ -97,6 +96,9 @@ import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.EntityManager; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.fsm.NoTransitionException; import com.cloud.utils.fsm.StateMachine2; @@ -681,9 +683,7 @@ public class VolumeOrchestrator extends ManagerBase implements VolumeOrchestrati } @DB - protected VolumeVO switchVolume(VolumeVO existingVolume, VirtualMachineProfile vm) throws StorageUnavailableException { - Transaction txn = Transaction.currentTxn(); - + protected VolumeVO switchVolume(final VolumeVO existingVolume, final VirtualMachineProfile vm) throws StorageUnavailableException { Long templateIdToUse = null; Long volTemplateId = existingVolume.getTemplateId(); long vmTemplateId = vm.getTemplateId(); @@ -695,22 +695,26 @@ public class VolumeOrchestrator extends ManagerBase implements VolumeOrchestrati templateIdToUse = vmTemplateId; } - txn.start(); - VolumeVO newVolume = allocateDuplicateVolumeVO(existingVolume, templateIdToUse); - // In case of Vmware if vm reference is not removed then during root - // disk cleanup - // the vm also gets deleted, so remove the reference - if (vm.getHypervisorType() == HypervisorType.VMware) { - _volsDao.detachVolume(existingVolume.getId()); - } - try { - stateTransitTo(existingVolume, Volume.Event.DestroyRequested); - } catch (NoTransitionException e) { - s_logger.debug("Unable to destroy existing volume: " + e.toString()); - } - txn.commit(); - return newVolume; - + final Long templateIdToUseFinal = templateIdToUse; + return Transaction.execute(new TransactionCallback() { + @Override + public VolumeVO doInTransaction(TransactionStatus status) { + VolumeVO newVolume = allocateDuplicateVolumeVO(existingVolume, templateIdToUseFinal); + // In case of Vmware if vm reference is not removed then during root + // disk cleanup + // the vm also gets deleted, so remove the reference + if (vm.getHypervisorType() == HypervisorType.VMware) { + _volsDao.detachVolume(existingVolume.getId()); + } + try { + stateTransitTo(existingVolume, Volume.Event.DestroyRequested); + } catch (NoTransitionException e) { + s_logger.debug("Unable to destroy existing volume: " + e.toString()); + } + + return newVolume; + } + }); } @Override @@ -724,28 +728,32 @@ public class VolumeOrchestrator extends ManagerBase implements VolumeOrchestrati if (s_logger.isDebugEnabled()) { s_logger.debug("Cleaning storage for vm: " + vmId); } - List volumesForVm = _volsDao.findByInstance(vmId); - List toBeExpunged = new ArrayList(); - Transaction txn = Transaction.currentTxn(); - txn.start(); - for (VolumeVO vol : volumesForVm) { - if (vol.getVolumeType().equals(Type.ROOT)) { - // Destroy volume if not already destroyed - boolean volumeAlreadyDestroyed = (vol.getState() == Volume.State.Destroy || vol.getState() == Volume.State.Expunged || vol.getState() == Volume.State.Expunging); - if (!volumeAlreadyDestroyed) { - volService.destroyVolume(vol.getId()); - } else { - s_logger.debug("Skipping destroy for the volume " + vol + " as its in state " + vol.getState().toString()); + final List volumesForVm = _volsDao.findByInstance(vmId); + final List toBeExpunged = new ArrayList(); + + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (VolumeVO vol : volumesForVm) { + if (vol.getVolumeType().equals(Type.ROOT)) { + // Destroy volume if not already destroyed + boolean volumeAlreadyDestroyed = (vol.getState() == Volume.State.Destroy || vol.getState() == Volume.State.Expunged || vol.getState() == Volume.State.Expunging); + if (!volumeAlreadyDestroyed) { + volService.destroyVolume(vol.getId()); + } else { + s_logger.debug("Skipping destroy for the volume " + vol + " as its in state " + vol.getState().toString()); + } + toBeExpunged.add(vol); + } else { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Detaching " + vol); + } + _volsDao.detachVolume(vol.getId()); + } } - toBeExpunged.add(vol); - } else { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Detaching " + vol); - } - _volsDao.detachVolume(vol.getId()); } - } - txn.commit(); + }); + AsyncCallFuture future = null; for (VolumeVO expunge : toBeExpunged) { future = volService.expungeVolumeAsync(volFactory.getVolume(expunge.getId())); diff --git a/engine/schema/src/com/cloud/alert/dao/AlertDaoImpl.java b/engine/schema/src/com/cloud/alert/dao/AlertDaoImpl.java index 7c0a56224f3..57102cbb50b 100755 --- a/engine/schema/src/com/cloud/alert/dao/AlertDaoImpl.java +++ b/engine/schema/src/com/cloud/alert/dao/AlertDaoImpl.java @@ -29,7 +29,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = { AlertDao.class }) @@ -116,7 +116,7 @@ public class AlertDaoImpl extends GenericDaoBase implements Alert return result; } if (alerts != null && !alerts.isEmpty()) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); for (AlertVO alert : alerts) { alert = lockRow(alert.getId(), true); diff --git a/engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java b/engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java index bd1e6f034ab..c642504df1d 100755 --- a/engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java +++ b/engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java @@ -46,7 +46,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -186,7 +186,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements @Override public List listClustersCrossingThreshold(short capacityType, Long zoneId, String configName, long compute_requested){ - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); StringBuilder sql = new StringBuilder(LIST_CLUSTERS_CROSSING_THRESHOLD); @@ -241,7 +241,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements @Override public List findCapacityBy(Integer capacityType, Long zoneId, Long podId, Long clusterId, String resource_state){ - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); @@ -291,7 +291,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements public List listCapacitiesGroupedByLevelAndType(Integer capacityType, Long zoneId, Long podId, Long clusterId, int level, Long limit){ StringBuilder finalQuery = new StringBuilder(); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); @@ -378,7 +378,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements @Override public List findCapacityBy(Integer capacityType, Long zoneId, Long podId, Long clusterId){ - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); @@ -425,7 +425,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements } public void updateAllocated(Long hostId, long allocatedAmount, short capacityType, boolean add) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { txn.start(); @@ -458,7 +458,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements @Override public List listClustersInZoneOrPodByHostCapacities(long id, int requiredCpu, long requiredRam, short capacityTypeForOrdering, boolean isZone){ - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); @@ -503,7 +503,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements @Override public List listHostsWithEnoughCapacity(int requiredCpu, long requiredRam, Long clusterId, String hostType){ - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); @@ -599,7 +599,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements @Override public List findByClusterPodZone(Long zoneId, Long podId, Long clusterId){ - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); @@ -711,7 +711,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements @Override public Pair, Map> orderClustersByAggregateCapacity(long id, short capacityTypeForOrdering, boolean isZone){ - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); Map clusterCapacityMap = new HashMap(); @@ -764,7 +764,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements @Override public List listPodsByHostCapacities(long zoneId, int requiredCpu, long requiredRam, short capacityType) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); @@ -798,7 +798,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements @Override public Pair, Map> orderPodsByAggregateCapacity(long zoneId, short capacityTypeForOrdering) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); Map podCapacityMap = new HashMap(); @@ -840,7 +840,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements @Override public void updateCapacityState(Long dcId, Long podId, Long clusterId, Long hostId, String capacityState) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); StringBuilder sql = new StringBuilder(UPDATE_CAPACITY_STATE); List resourceIdList = new ArrayList(); diff --git a/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java b/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java index 9070ff912b3..0d99d30f11b 100644 --- a/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java +++ b/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java @@ -40,7 +40,7 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={ResourceCountDao.class}) @@ -158,7 +158,7 @@ public class ResourceCountDaoImpl extends GenericDaoBase @Override @DB public void createResourceCounts(long ownerId, ResourceLimit.ResourceOwnerType ownerType){ - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); ResourceType[] resourceTypes = Resource.ResourceType.values(); diff --git a/engine/schema/src/com/cloud/dc/ClusterDetailsDaoImpl.java b/engine/schema/src/com/cloud/dc/ClusterDetailsDaoImpl.java index f7f0f11ba07..72ac400ba72 100755 --- a/engine/schema/src/com/cloud/dc/ClusterDetailsDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/ClusterDetailsDaoImpl.java @@ -30,7 +30,7 @@ import com.cloud.utils.crypt.DBEncryptionUtil; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Local(value=ClusterDetailsDao.class) public class ClusterDetailsDaoImpl extends GenericDaoBase implements ClusterDetailsDao, ScopedConfigStorage { @@ -100,7 +100,7 @@ public class ClusterDetailsDaoImpl extends GenericDaoBase details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = ClusterSearch.create(); sc.setParameters("clusterId", clusterId); @@ -119,7 +119,7 @@ public class ClusterDetailsDaoImpl extends GenericDaoBase sc = DetailSearch.create(); sc.setParameters("clusterId", clusterId); diff --git a/engine/schema/src/com/cloud/dc/dao/ClusterDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/ClusterDaoImpl.java index d115ce9e973..bd45ecd93fa 100644 --- a/engine/schema/src/com/cloud/dc/dao/ClusterDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/ClusterDaoImpl.java @@ -40,7 +40,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -156,7 +156,7 @@ public class ClusterDaoImpl extends GenericDaoBase implements C @Override public Map> getPodClusterIdMap(List clusterIds){ - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; Map> result = new HashMap>(); @@ -243,7 +243,7 @@ public class ClusterDaoImpl extends GenericDaoBase implements C @Override public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); ClusterVO cluster = createForUpdate(); cluster.setName(null); diff --git a/engine/schema/src/com/cloud/dc/dao/ClusterVSMMapDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/ClusterVSMMapDaoImpl.java index c3d3f2a9352..a01af0656a6 100644 --- a/engine/schema/src/com/cloud/dc/dao/ClusterVSMMapDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/ClusterVSMMapDaoImpl.java @@ -26,7 +26,7 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value=ClusterVSMMapDao.class) @@ -79,7 +79,7 @@ public class ClusterVSMMapDaoImpl extends GenericDaoBase } public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); ClusterVSMMapVO cluster = createForUpdate(); //cluster.setClusterId(null); diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/DataCenterDaoImpl.java index 47b5522326a..e3b70a2f9af 100755 --- a/engine/schema/src/com/cloud/dc/dao/DataCenterDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/DataCenterDaoImpl.java @@ -44,7 +44,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SequenceFetcher; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.net.NetUtils; /** @@ -343,7 +343,7 @@ public class DataCenterDaoImpl extends GenericDaoBase implem @Override @DB public boolean update(Long zoneId, DataCenterVO zone) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); boolean persisted = super.update(zoneId, zone); if (!persisted) { @@ -408,7 +408,7 @@ public class DataCenterDaoImpl extends GenericDaoBase implem @Override public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); DataCenterVO zone = createForUpdate(); zone.setName(null); diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java index 0ec2bb50c7b..c2ed551a668 100755 --- a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java @@ -33,7 +33,7 @@ import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; @@ -52,7 +52,7 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase { public List listAllocatedVnets(long physicalNetworkId); @@ -33,7 +34,7 @@ public interface DataCenterVnetDao extends GenericDao { public void delete(long physicalNetworkId); - public void deleteVnets(Transaction txn, long dcId, long physicalNetworkId, List vnets); + public void deleteVnets(TransactionLegacy txn, long dcId, long physicalNetworkId, List vnets); public void lockRange(long dcId, long physicalNetworkId, Integer start, Integer end); diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDaoImpl.java index c052026eb2d..4e2aabf117f 100755 --- a/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/DataCenterVnetDaoImpl.java @@ -38,7 +38,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; /** @@ -121,7 +121,7 @@ public class DataCenterVnetDaoImpl extends GenericDaoBase vnets) { String insertVnet = "INSERT INTO `cloud`.`op_dc_vnet_alloc` (vnet, data_center_id, physical_network_id) VALUES ( ?, ?, ?)"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); PreparedStatement stmt = txn.prepareAutoCloseStatement(insertVnet); @@ -139,7 +139,7 @@ public class DataCenterVnetDaoImpl extends GenericDaoBase argument each string is a vlan. not a vlanRange. - public void deleteVnets(Transaction txn, long dcId, long physicalNetworkId, List vnets) { + public void deleteVnets(TransactionLegacy txn, long dcId, long physicalNetworkId, List vnets) { String deleteVnet = "DELETE FROM `cloud`.`op_dc_vnet_alloc` WHERE data_center_id=? AND physical_network_id=? AND taken IS NULL AND vnet=?"; try { PreparedStatement stmt = txn.prepareAutoCloseStatement(deleteVnet); @@ -171,7 +171,7 @@ public class DataCenterVnetDaoImpl extends GenericDaoBase implements DcDetailsDao, ScopedConfigStorage { @@ -106,7 +106,7 @@ public class DcDetailsDaoImpl extends GenericDaoBase implement @Override public void persist(long dcId, Map details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = DcSearch.create(); sc.setParameters("dcId", dcId); diff --git a/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java index f77dedae1b7..384fa00350f 100644 --- a/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java @@ -35,7 +35,7 @@ import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={HostPodDao.class}) @@ -85,7 +85,7 @@ public class HostPodDaoImpl extends GenericDaoBase implements H HashMap> currentPodCidrSubnets = new HashMap>(); String selectSql = "SELECT id, cidr_address, cidr_size FROM host_pod_ref WHERE data_center_id=" + zoneId +" and removed IS NULL"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql); ResultSet rs = stmt.executeQuery(); @@ -111,7 +111,7 @@ public class HostPodDaoImpl extends GenericDaoBase implements H @Override public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); HostPodVO pod = createForUpdate(); pod.setName(null); diff --git a/engine/schema/src/com/cloud/dc/dao/PodVlanDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/PodVlanDaoImpl.java index 413f9ed6c18..9e9a0e77d18 100755 --- a/engine/schema/src/com/cloud/dc/dao/PodVlanDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/PodVlanDaoImpl.java @@ -28,7 +28,7 @@ import com.cloud.utils.db.GenericDao; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; /** @@ -49,7 +49,7 @@ public class PodVlanDaoImpl extends GenericDaoBase implements P public void add(long podId, int start, int end) { String insertVnet = "INSERT INTO `cloud`.`op_pod_vlan_alloc` (vlan, pod_id) VALUES ( ?, ?)"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); PreparedStatement stmt = txn.prepareAutoCloseStatement(insertVnet); @@ -68,7 +68,7 @@ public class PodVlanDaoImpl extends GenericDaoBase implements P public void delete(long podId) { String deleteVnet = "DELETE FROM `cloud`.`op_pod_vlan_alloc` WHERE pod_id = ?"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(deleteVnet); stmt.setLong(1, podId); @@ -82,7 +82,7 @@ public class PodVlanDaoImpl extends GenericDaoBase implements P SearchCriteria sc = FreeVlanSearch.create(); sc.setParameters("podId", podId); Date now = new Date(); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); PodVlanVO vo = lockOneRandomRow(sc, true); diff --git a/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java index a7bb9c1c005..d6a38794ba5 100755 --- a/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/StorageNetworkIpAddressDaoImpl.java @@ -30,10 +30,10 @@ import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.GenericQueryBuilder; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={StorageNetworkIpAddressDao.class}) @@ -87,7 +87,7 @@ public class StorageNetworkIpAddressDaoImpl extends GenericDaoBase sc = untakenIp.create(); sc.setParameters("rangeId", rangeId); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); StorageNetworkIpAddressVO ip = lockOneRandomRow(sc, true); if (ip == null) { diff --git a/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java index 6f5a01f1476..6ec819d17f1 100755 --- a/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/VlanDaoImpl.java @@ -28,7 +28,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; import org.springframework.stereotype.Component; @@ -301,7 +301,7 @@ public class VlanDaoImpl extends GenericDaoBase implements VlanDao StringBuilder sql = new StringBuilder(FindZoneWideVlans); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(sql.toString()); diff --git a/engine/schema/src/com/cloud/domain/dao/DomainDaoImpl.java b/engine/schema/src/com/cloud/domain/dao/DomainDaoImpl.java index 2a3642e63e6..3fd1d582302 100644 --- a/engine/schema/src/com/cloud/domain/dao/DomainDaoImpl.java +++ b/engine/schema/src/com/cloud/domain/dao/DomainDaoImpl.java @@ -35,7 +35,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={DomainDao.class}) @@ -117,7 +117,7 @@ public class DomainDaoImpl extends GenericDaoBase implements Dom return null; } - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); @@ -168,7 +168,7 @@ public class DomainDaoImpl extends GenericDaoBase implements Dom String sql1 = "SELECT * from domain where parent = " + id + " and removed is null"; boolean success = false; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); DomainVO parentDomain = super.lockRow(domain.getParent(), true); diff --git a/engine/schema/src/com/cloud/event/dao/EventDaoImpl.java b/engine/schema/src/com/cloud/event/dao/EventDaoImpl.java index e5615db3433..f3a11c601e3 100644 --- a/engine/schema/src/com/cloud/event/dao/EventDaoImpl.java +++ b/engine/schema/src/com/cloud/event/dao/EventDaoImpl.java @@ -30,7 +30,7 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.SearchCriteria.Op; @Component @@ -104,7 +104,7 @@ public class EventDaoImpl extends GenericDaoBase implements Event @Override public void archiveEvents(List events) { if (events != null && !events.isEmpty()) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); for (EventVO event : events) { event = lockRow(event.getId(), true); diff --git a/engine/schema/src/com/cloud/event/dao/UsageEventDaoImpl.java b/engine/schema/src/com/cloud/event/dao/UsageEventDaoImpl.java index 4f05bbe8fcd..43339038c53 100644 --- a/engine/schema/src/com/cloud/event/dao/UsageEventDaoImpl.java +++ b/engine/schema/src/com/cloud/event/dao/UsageEventDaoImpl.java @@ -36,7 +36,7 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -90,7 +90,7 @@ public class UsageEventDaoImpl extends GenericDaoBase implem public synchronized List getRecentEvents(Date endDate) { long recentEventId = getMostRecentEventId(); long maxEventId = getMaxEventId(endDate); - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); String sql = COPY_EVENTS; if (recentEventId == 0) { if (s_logger.isDebugEnabled()) { @@ -120,7 +120,7 @@ public class UsageEventDaoImpl extends GenericDaoBase implem @DB private long getMostRecentEventId() { - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); try { List latestEvents = getLatestEvent(); @@ -140,7 +140,7 @@ public class UsageEventDaoImpl extends GenericDaoBase implem } private List findRecentEvents(Date endDate) { - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); try { return listLatestEvents(endDate); } catch (Exception ex) { @@ -152,7 +152,7 @@ public class UsageEventDaoImpl extends GenericDaoBase implem } private long getMaxEventId(Date endDate) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { String sql = MAX_EVENT; diff --git a/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java b/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java index caf7c014bd0..c51e2c7ff60 100755 --- a/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java +++ b/engine/schema/src/com/cloud/host/dao/HostDaoImpl.java @@ -59,7 +59,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.UpdateBuilder; import com.cloud.utils.exception.CloudRuntimeException; @@ -490,7 +490,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao @Override @DB public List findAndUpdateDirectAgentToLoad(long lastPingSecondsAfter, Long limit, long managementServerId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); if (s_logger.isDebugEnabled()) { @@ -589,7 +589,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao @Override @DB public List findAndUpdateApplianceToLoad(long lastPingSecondsAfter, long managementServerId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = UnmanagedApplianceSearch.create(); @@ -728,7 +728,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao @DB @Override public List findLostHosts(long timeout) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); ResultSet rs = null; @@ -779,7 +779,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao public HostVO persist(HostVO host) { final String InsertSequenceSql = "INSERT INTO op_host(id) VALUES(?)"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); HostVO dbHost = super.persist(host); @@ -805,7 +805,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao @Override @DB public boolean update(Long hostId, HostVO host) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); boolean persisted = super.update(hostId, host); @@ -831,7 +831,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao ArrayList l = new ArrayList(); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); ; PreparedStatement pstmt = null; try { diff --git a/engine/schema/src/com/cloud/host/dao/HostDetailsDaoImpl.java b/engine/schema/src/com/cloud/host/dao/HostDetailsDaoImpl.java index 47cdeb30633..7b7267cccf5 100644 --- a/engine/schema/src/com/cloud/host/dao/HostDetailsDaoImpl.java +++ b/engine/schema/src/com/cloud/host/dao/HostDetailsDaoImpl.java @@ -31,7 +31,7 @@ import com.cloud.utils.crypt.DBEncryptionUtil; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -96,7 +96,7 @@ public class HostDetailsDaoImpl extends GenericDaoBase implement public void persist(long hostId, Map details) { final String InsertOrUpdateSql = "INSERT INTO `cloud`.`host_details` (host_id, name, value) VALUES (?,?,?) ON DUPLICATE KEY UPDATE value=?"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); for (Map.Entry detail : details.entrySet()) { diff --git a/engine/schema/src/com/cloud/host/dao/HostTagsDaoImpl.java b/engine/schema/src/com/cloud/host/dao/HostTagsDaoImpl.java index 0e93275d360..b12fd7e1ac1 100644 --- a/engine/schema/src/com/cloud/host/dao/HostTagsDaoImpl.java +++ b/engine/schema/src/com/cloud/host/dao/HostTagsDaoImpl.java @@ -27,7 +27,7 @@ import com.cloud.host.HostTagVO; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value=HostTagsDao.class) @@ -56,7 +56,7 @@ public class HostTagsDaoImpl extends GenericDaoBase implements @Override public void persist(long hostId, List hostTags) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = HostSearch.create(); diff --git a/engine/schema/src/com/cloud/keystore/KeystoreDaoImpl.java b/engine/schema/src/com/cloud/keystore/KeystoreDaoImpl.java index f53f228e6b3..370f874ecd9 100644 --- a/engine/schema/src/com/cloud/keystore/KeystoreDaoImpl.java +++ b/engine/schema/src/com/cloud/keystore/KeystoreDaoImpl.java @@ -30,7 +30,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -74,7 +74,7 @@ public class KeystoreDaoImpl extends GenericDaoBase implements @Override @DB public void save(String name, String certificate, String key, String domainSuffix) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); diff --git a/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsDaoImpl.java b/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsDaoImpl.java index b007e19e779..26f399d1246 100644 --- a/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsDaoImpl.java +++ b/engine/schema/src/com/cloud/network/dao/FirewallRulesCidrsDaoImpl.java @@ -28,7 +28,7 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value=FirewallRulesCidrsDao.class) @@ -58,7 +58,7 @@ public class FirewallRulesCidrsDaoImpl extends GenericDaoBase sourceCidrs) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); for (String tag : sourceCidrs) { diff --git a/engine/schema/src/com/cloud/network/dao/FirewallRulesDaoImpl.java b/engine/schema/src/com/cloud/network/dao/FirewallRulesDaoImpl.java index cd7878f08c8..6aa8910ab04 100644 --- a/engine/schema/src/com/cloud/network/dao/FirewallRulesDaoImpl.java +++ b/engine/schema/src/com/cloud/network/dao/FirewallRulesDaoImpl.java @@ -39,7 +39,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = FirewallRulesDao.class) @@ -220,7 +220,7 @@ public class FirewallRulesDaoImpl extends GenericDaoBase i @Override @DB public FirewallRuleVO persist(FirewallRuleVO firewallRule) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); FirewallRuleVO dbfirewallRule = super.persist(firewallRule); @@ -309,7 +309,7 @@ public class FirewallRulesDaoImpl extends GenericDaoBase i @Override @DB public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); FirewallRuleVO entry = findById(id); if (entry != null) { diff --git a/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java b/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java index 9f5f403631b..46a34c5eca5 100755 --- a/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java +++ b/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java @@ -31,7 +31,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.net.Ip; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @@ -285,7 +285,7 @@ public class IPAddressDaoImpl extends GenericDaoBase implemen @Override @DB public int countIPs(long dcId, Long accountId, String vlanId, String vlanGateway, String vlanNetmask) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); int ipCount = 0; try { String sql = "SELECT count(*) FROM user_ip_address u INNER JOIN vlan v on (u.vlan_db_id = v.id AND v.data_center_id = ? AND v.vlan_id = ? AND v.vlan_gateway = ? AND v.vlan_netmask = ? AND u.account_id = ?)"; @@ -390,7 +390,7 @@ public class IPAddressDaoImpl extends GenericDaoBase implemen @Override @DB public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); IPAddressVO entry = findById(id); if (entry != null) { diff --git a/engine/schema/src/com/cloud/network/dao/NetworkDaoImpl.java b/engine/schema/src/com/cloud/network/dao/NetworkDaoImpl.java index 72b357d659c..6599d6be870 100644 --- a/engine/schema/src/com/cloud/network/dao/NetworkDaoImpl.java +++ b/engine/schema/src/com/cloud/network/dao/NetworkDaoImpl.java @@ -289,7 +289,7 @@ public class NetworkDaoImpl extends GenericDaoBase implements N @Override @DB public NetworkVO persist(NetworkVO network, boolean gc, Map serviceProviderMap) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); // 1) create network @@ -309,7 +309,7 @@ public class NetworkDaoImpl extends GenericDaoBase implements N @Override @DB public boolean update(Long networkId, NetworkVO network, Map serviceProviderMap) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); super.update(networkId, network); @@ -325,7 +325,7 @@ public class NetworkDaoImpl extends GenericDaoBase implements N @Override @DB public void persistNetworkServiceProviders(long networkId, Map serviceProviderMap) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); for (String service : serviceProviderMap.keySet()) { NetworkServiceMapVO serviceMap = new NetworkServiceMapVO(networkId, Service.getService(service), Provider.getProvider(serviceProviderMap.get(service))); @@ -576,7 +576,7 @@ public class NetworkDaoImpl extends GenericDaoBase implements N @Override @DB public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); NetworkVO entry = findById(id); if (entry != null) { @@ -599,7 +599,7 @@ public class NetworkDaoImpl extends GenericDaoBase implements N @Override public boolean updateState(State currentState, Event event, State nextState, Network vo, Object data) { // TODO: ensure this update is correct - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); NetworkVO networkVo = (NetworkVO) vo; diff --git a/engine/schema/src/com/cloud/network/dao/PortProfileDaoImpl.java b/engine/schema/src/com/cloud/network/dao/PortProfileDaoImpl.java index 2325c4a9705..64a846c6d5b 100644 --- a/engine/schema/src/com/cloud/network/dao/PortProfileDaoImpl.java +++ b/engine/schema/src/com/cloud/network/dao/PortProfileDaoImpl.java @@ -29,7 +29,7 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.exception.CloudRuntimeException; @@ -66,7 +66,7 @@ public class PortProfileDaoImpl extends GenericDaoBase impl String condition = "(trunk_low_vlan_id BETWEEN " + lowVlanId + " AND " + highVlanId + ")" + " OR (trunk_high_vlan_id BETWEEN " + lowVlanId + " AND " + highVlanId + ")"; String selectSql = "SELECT * FROM `" + dbName + "`.`" + tableName + "` WHERE " + condition; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql); ResultSet rs = stmt.executeQuery(); diff --git a/engine/schema/src/com/cloud/network/security/dao/SecurityGroupDaoImpl.java b/engine/schema/src/com/cloud/network/security/dao/SecurityGroupDaoImpl.java index d82cc4a79b5..ee8c160ff69 100644 --- a/engine/schema/src/com/cloud/network/security/dao/SecurityGroupDaoImpl.java +++ b/engine/schema/src/com/cloud/network/security/dao/SecurityGroupDaoImpl.java @@ -32,7 +32,7 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={SecurityGroupDao.class}) @@ -109,7 +109,7 @@ public class SecurityGroupDaoImpl extends GenericDaoBase @Override @DB public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SecurityGroupVO entry = findById(id); if (entry != null) { @@ -123,7 +123,7 @@ public class SecurityGroupDaoImpl extends GenericDaoBase @Override @DB public boolean expunge(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SecurityGroupVO entry = findById(id); if (entry != null) { diff --git a/engine/schema/src/com/cloud/network/security/dao/SecurityGroupWorkDaoImpl.java b/engine/schema/src/com/cloud/network/security/dao/SecurityGroupWorkDaoImpl.java index 3154ffeb873..98cc0d3fc65 100644 --- a/engine/schema/src/com/cloud/network/security/dao/SecurityGroupWorkDaoImpl.java +++ b/engine/schema/src/com/cloud/network/security/dao/SecurityGroupWorkDaoImpl.java @@ -33,7 +33,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -100,7 +100,7 @@ public class SecurityGroupWorkDaoImpl extends GenericDaoBase sc = UntakenWorkSearch.create(); sc.setParameters("step", Step.Scheduled); @@ -149,7 +149,7 @@ public class SecurityGroupWorkDaoImpl extends GenericDaoBase sc = VmIdSeqNumSearch.create(); sc.setParameters("vmId", vmId); @@ -180,7 +180,7 @@ public class SecurityGroupWorkDaoImpl extends GenericDaoBase im return createOrUpdateUsingMultiInsert(workItems); } - private int executeWithRetryOnDeadlock(Transaction txn, String pstmt, List vmIds) throws SQLException { + private int executeWithRetryOnDeadlock(TransactionLegacy txn, String pstmt, List vmIds) throws SQLException { int numUpdated = 0; final int maxTries = 3; @@ -120,7 +120,7 @@ public class VmRulesetLogDaoImpl extends GenericDaoBase im } protected int createOrUpdateUsingMultiInsert(Set workItems) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); int size = workItems.size(); int count = 0; @@ -156,7 +156,7 @@ public class VmRulesetLogDaoImpl extends GenericDaoBase im } protected int createOrUpdateUsingBatch(Set workItems) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement stmtInsert = null; int [] queryResult = null; int count=0; diff --git a/engine/schema/src/com/cloud/network/vpc/dao/PrivateIpDaoImpl.java b/engine/schema/src/com/cloud/network/vpc/dao/PrivateIpDaoImpl.java index 5ed7fb2f757..7511657c0ea 100644 --- a/engine/schema/src/com/cloud/network/vpc/dao/PrivateIpDaoImpl.java +++ b/engine/schema/src/com/cloud/network/vpc/dao/PrivateIpDaoImpl.java @@ -32,7 +32,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = PrivateIpDao.class) @@ -78,7 +78,7 @@ public class PrivateIpDaoImpl extends GenericDaoBase implemen sc.setParameters("ipAddress", requestedIp); } - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); PrivateIpVO vo = lockOneRandomRow(sc, true); if (vo == null) { diff --git a/engine/schema/src/com/cloud/network/vpc/dao/StaticRouteDaoImpl.java b/engine/schema/src/com/cloud/network/vpc/dao/StaticRouteDaoImpl.java index e7e006e852e..f5a9ed5107e 100644 --- a/engine/schema/src/com/cloud/network/vpc/dao/StaticRouteDaoImpl.java +++ b/engine/schema/src/com/cloud/network/vpc/dao/StaticRouteDaoImpl.java @@ -36,7 +36,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = StaticRouteDao.class) @@ -106,7 +106,7 @@ public class StaticRouteDaoImpl extends GenericDaoBase impl @Override @DB public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); StaticRouteVO entry = findById(id); if (entry != null) { diff --git a/engine/schema/src/com/cloud/network/vpc/dao/VpcDaoImpl.java b/engine/schema/src/com/cloud/network/vpc/dao/VpcDaoImpl.java index 289b896899a..c34b551c970 100644 --- a/engine/schema/src/com/cloud/network/vpc/dao/VpcDaoImpl.java +++ b/engine/schema/src/com/cloud/network/vpc/dao/VpcDaoImpl.java @@ -38,7 +38,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = VpcDao.class) @@ -107,7 +107,7 @@ public class VpcDaoImpl extends GenericDaoBase implements VpcDao{ @Override @DB public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); VpcVO entry = findById(id); if (entry != null) { @@ -129,7 +129,7 @@ public class VpcDaoImpl extends GenericDaoBase implements VpcDao{ @Override @DB public VpcVO persist(VpcVO vpc, Map> serviceProviderMap) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); VpcVO newVpc = super.persist(vpc); persistVpcServiceProviders(vpc.getId(), serviceProviderMap); @@ -140,7 +140,7 @@ public class VpcDaoImpl extends GenericDaoBase implements VpcDao{ @Override @DB public void persistVpcServiceProviders(long vpcId, Map> serviceProviderMap) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); for (String service : serviceProviderMap.keySet()) { for (String provider : serviceProviderMap.get(service)) { diff --git a/engine/schema/src/com/cloud/network/vpc/dao/VpcOfferingDaoImpl.java b/engine/schema/src/com/cloud/network/vpc/dao/VpcOfferingDaoImpl.java index b5a7e4dd7bf..3453da04483 100644 --- a/engine/schema/src/com/cloud/network/vpc/dao/VpcOfferingDaoImpl.java +++ b/engine/schema/src/com/cloud/network/vpc/dao/VpcOfferingDaoImpl.java @@ -26,7 +26,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = VpcOfferingDao.class) @@ -51,7 +51,7 @@ public class VpcOfferingDaoImpl extends GenericDaoBase impl @Override @DB public boolean remove(Long vpcOffId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); VpcOfferingVO offering = findById(vpcOffId); offering.setUniqueName(null); diff --git a/engine/schema/src/com/cloud/offerings/dao/NetworkOfferingDaoImpl.java b/engine/schema/src/com/cloud/offerings/dao/NetworkOfferingDaoImpl.java index 1c0a1d74743..19290a60693 100644 --- a/engine/schema/src/com/cloud/offerings/dao/NetworkOfferingDaoImpl.java +++ b/engine/schema/src/com/cloud/offerings/dao/NetworkOfferingDaoImpl.java @@ -38,7 +38,7 @@ import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = NetworkOfferingDao.class) @@ -129,7 +129,7 @@ public class NetworkOfferingDaoImpl extends GenericDaoBase details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); //1) persist the offering NetworkOfferingVO vo = super.persist(off); diff --git a/engine/schema/src/com/cloud/projects/dao/ProjectDaoImpl.java b/engine/schema/src/com/cloud/projects/dao/ProjectDaoImpl.java index ecf40dacaff..fd6c802b384 100644 --- a/engine/schema/src/com/cloud/projects/dao/ProjectDaoImpl.java +++ b/engine/schema/src/com/cloud/projects/dao/ProjectDaoImpl.java @@ -36,7 +36,7 @@ import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = { ProjectDao.class }) @@ -75,7 +75,7 @@ public class ProjectDaoImpl extends GenericDaoBase implements P @DB public boolean remove(Long projectId) { boolean result = false; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); ProjectVO projectToRemove = findById(projectId); projectToRemove.setName(null); diff --git a/engine/schema/src/com/cloud/service/dao/ServiceOfferingDetailsDaoImpl.java b/engine/schema/src/com/cloud/service/dao/ServiceOfferingDetailsDaoImpl.java index 91d736a38c4..70fd5701507 100644 --- a/engine/schema/src/com/cloud/service/dao/ServiceOfferingDetailsDaoImpl.java +++ b/engine/schema/src/com/cloud/service/dao/ServiceOfferingDetailsDaoImpl.java @@ -28,7 +28,7 @@ import com.cloud.service.ServiceOfferingDetailsVO; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value=ServiceOfferingDetailsDao.class) @@ -82,7 +82,7 @@ public class ServiceOfferingDetailsDaoImpl extends GenericDaoBase details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = ServiceOfferingSearch.create(); sc.setParameters("serviceOfferingId", serviceOfferingId); diff --git a/engine/schema/src/com/cloud/storage/dao/LaunchPermissionDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/LaunchPermissionDaoImpl.java index 3e32f1a9529..60c87af0fcc 100644 --- a/engine/schema/src/com/cloud/storage/dao/LaunchPermissionDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/LaunchPermissionDaoImpl.java @@ -35,7 +35,7 @@ import com.cloud.utils.DateUtil; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -69,7 +69,7 @@ public class LaunchPermissionDaoImpl extends GenericDaoBase accountIds) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { txn.start(); @@ -106,7 +106,7 @@ public class LaunchPermissionDaoImpl extends GenericDaoBase listPermittedTemplates(long accountId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); List permittedTemplates = new ArrayList(); PreparedStatement pstmt = null; try { diff --git a/engine/schema/src/com/cloud/storage/dao/SnapshotDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/SnapshotDaoImpl.java index f5319ea120a..94b3871077e 100644 --- a/engine/schema/src/com/cloud/storage/dao/SnapshotDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/SnapshotDaoImpl.java @@ -47,7 +47,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.vm.VMInstanceVO; import com.cloud.vm.dao.VMInstanceDao; @@ -204,7 +204,7 @@ public class SnapshotDaoImpl extends GenericDaoBase implements @Override public Long getSecHostId(long volumeId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; String sql = GET_SECHOST_ID; try { @@ -221,7 +221,7 @@ public class SnapshotDaoImpl extends GenericDaoBase implements @Override public long getLastSnapshot(long volumeId, DataStoreRole role) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; String sql = GET_LAST_SNAPSHOT; try { @@ -240,7 +240,7 @@ public class SnapshotDaoImpl extends GenericDaoBase implements @Override public long updateSnapshotVersion(long volumeId, String from, String to) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; String sql = UPDATE_SNAPSHOT_VERSION; try { @@ -258,7 +258,7 @@ public class SnapshotDaoImpl extends GenericDaoBase implements @Override public long updateSnapshotSecHost(long dcId, long secHostId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; String sql = UPDATE_SECHOST_ID; try { @@ -304,7 +304,7 @@ public class SnapshotDaoImpl extends GenericDaoBase implements @Override @DB public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SnapshotVO entry = findById(id); if (entry != null) { @@ -324,7 +324,7 @@ public class SnapshotDaoImpl extends GenericDaoBase implements @Override public boolean updateState(State currentState, Event event, State nextState, SnapshotVO snapshot, Object data) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SnapshotVO snapshotVO = (SnapshotVO) snapshot; snapshotVO.setState(nextState); diff --git a/engine/schema/src/com/cloud/storage/dao/StoragePoolDetailsDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/StoragePoolDetailsDaoImpl.java index f60ea176f52..7859ca796b0 100644 --- a/engine/schema/src/com/cloud/storage/dao/StoragePoolDetailsDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/StoragePoolDetailsDaoImpl.java @@ -31,7 +31,7 @@ import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailsDao; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Local(value = StoragePoolDetailsDao.class) public class StoragePoolDetailsDaoImpl extends GenericDaoBase implements StoragePoolDetailsDao, ScopedConfigStorage { @@ -48,7 +48,7 @@ public class StoragePoolDetailsDaoImpl extends GenericDaoBase details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); SearchCriteria sc = PoolSearch.create(); sc.setParameters("pool", poolId); diff --git a/engine/schema/src/com/cloud/storage/dao/StoragePoolHostDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/StoragePoolHostDaoImpl.java index 730216b82b0..ba0c48daf09 100644 --- a/engine/schema/src/com/cloud/storage/dao/StoragePoolHostDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/StoragePoolHostDaoImpl.java @@ -33,7 +33,7 @@ import com.cloud.utils.Pair; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = { StoragePoolHostDao.class }) @@ -104,7 +104,7 @@ public class StoragePoolHostDaoImpl extends GenericDaoBase listByHostStatus(long poolId, Status hostStatus) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); ResultSet rs = null; @@ -142,7 +142,7 @@ public class StoragePoolHostDaoImpl extends GenericDaoBase> getDatacenterStoragePoolHostInfo(long dcId, boolean sharedOnly) { ArrayList> l = new ArrayList>(); String sql = sharedOnly ? SHARED_STORAGE_POOL_HOST_INFO : STORAGE_POOL_HOST_INFO; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); ; PreparedStatement pstmt = null; try { @@ -169,7 +169,7 @@ public class StoragePoolHostDaoImpl extends GenericDaoBase sc = HostSearch.create(); sc.setParameters("host_id", hostId); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); remove(sc); txn.commit(); @@ -180,7 +180,7 @@ public class StoragePoolHostDaoImpl extends GenericDaoBase sc = PoolHostSearch.create(); sc.setParameters("host_id", hostId); sc.setParameters("pool_id", poolId); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); remove(sc); txn.commit(); diff --git a/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDaoImpl.java index aa3e0df04ea..df0b6f2312b 100644 --- a/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/StoragePoolWorkDaoImpl.java @@ -31,7 +31,7 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -125,7 +125,7 @@ public class StoragePoolWorkDaoImpl extends GenericDaoBase implem List l = new ArrayList(); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { @@ -438,7 +438,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem * permittedAccountsStr = permittedAccountsStr.substring(0, * permittedAccountsStr.length() - 1); } * - * Transaction txn = Transaction.currentTxn(); txn.start(); + * TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); * * Set> templateZonePairList = new HashSet>(); PreparedStatement pstmt = null; ResultSet rs = null; String sql @@ -522,7 +522,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem * permittedAccountsStr = permittedAccountsStr.substring(0, * permittedAccountsStr.length()-1); } * - * Transaction txn = Transaction.currentTxn(); txn.start(); + * TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); * * // Use LinkedHashSet here to guarantee iteration order Set> templateZonePairList = new LinkedHashSet>(); @@ -737,7 +737,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem @Override @DB public long addTemplateToZone(VMTemplateVO tmplt, long zoneId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); VMTemplateVO tmplt2 = findById(tmplt.getId()); if (tmplt2 == null) { @@ -872,7 +872,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem @Override @DB public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); VMTemplateVO template = createForUpdate(); template.setRemoved(new Date()); @@ -907,7 +907,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem * * final String permittedAccountsStr = join(",", permittedAccounts); * - * final Transaction txn = Transaction.currentTxn(); txn.start(); + * final TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); * * Set> templateZonePairList = new HashSet>(); PreparedStatement pstmt = null; ResultSet rs = null; try { diff --git a/engine/schema/src/com/cloud/storage/dao/VMTemplateDetailsDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VMTemplateDetailsDaoImpl.java index 5ebed532665..34b5cc542d8 100644 --- a/engine/schema/src/com/cloud/storage/dao/VMTemplateDetailsDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/VMTemplateDetailsDaoImpl.java @@ -28,7 +28,7 @@ import com.cloud.storage.VMTemplateDetailVO; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = VMTemplateDetailsDao.class) @@ -84,7 +84,7 @@ public class VMTemplateDetailsDaoImpl extends GenericDaoBase details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = TemplateSearch.create(); sc.setParameters("templateId", templateId); diff --git a/engine/schema/src/com/cloud/storage/dao/VMTemplateHostDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VMTemplateHostDaoImpl.java index 85d8348e36e..d6283b60285 100755 --- a/engine/schema/src/com/cloud/storage/dao/VMTemplateHostDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/VMTemplateHostDaoImpl.java @@ -46,7 +46,7 @@ import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.UpdateBuilder; @Component @@ -163,7 +163,7 @@ public class VMTemplateHostDaoImpl extends GenericDaoBase listByTemplateStatus(long templateId, long datacenterId, VMTemplateHostVO.Status downloadState) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); try { @@ -267,7 +267,7 @@ public class VMTemplateHostDaoImpl extends GenericDaoBase listByTemplateStatus(long templateId, long datacenterId, long podId, VMTemplateHostVO.Status downloadState) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); ResultSet rs = null; diff --git a/engine/schema/src/com/cloud/storage/dao/VMTemplatePoolDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VMTemplatePoolDaoImpl.java index ba8135b5f32..b121297437a 100644 --- a/engine/schema/src/com/cloud/storage/dao/VMTemplatePoolDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/VMTemplatePoolDaoImpl.java @@ -38,7 +38,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.UpdateBuilder; @Component @@ -150,7 +150,7 @@ public class VMTemplatePoolDaoImpl extends GenericDaoBase listByTemplateStatus(long templateId, long datacenterId, VMTemplateStoragePoolVO.Status downloadState) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); try { @@ -173,7 +173,7 @@ public class VMTemplatePoolDaoImpl extends GenericDaoBase listByTemplateStatus(long templateId, long datacenterId, long podId, VMTemplateStoragePoolVO.Status downloadState) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); ResultSet rs = null; @@ -210,7 +210,7 @@ public class VMTemplatePoolDaoImpl extends GenericDaoBase listByHostTemplate(long hostId, long templateId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); ResultSet rs = null; diff --git a/engine/schema/src/com/cloud/storage/dao/VMTemplateZoneDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VMTemplateZoneDaoImpl.java index c4a4dc7230a..a56959d32ab 100644 --- a/engine/schema/src/com/cloud/storage/dao/VMTemplateZoneDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/VMTemplateZoneDaoImpl.java @@ -27,7 +27,7 @@ import com.cloud.storage.VMTemplateZoneVO; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = { VMTemplateZoneDao.class }) @@ -89,7 +89,7 @@ public class VMTemplateZoneDaoImpl extends GenericDaoBase sc = TemplateSearch.create(); sc.setParameters("template_id", templateId); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); remove(sc); txn.commit(); diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java index 2b796649551..59e61e92f79 100755 --- a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java @@ -48,7 +48,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.UpdateBuilder; import com.cloud.utils.exception.CloudRuntimeException; @@ -237,7 +237,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol @DB public HypervisorType getHypervisorType(long volumeId) { /* lookup from cluster of pool */ - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; String sql = null; try { @@ -458,7 +458,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol @Override public List listPoolIdsByVolumeCount(long dcId, Long podId, Long clusterId, long accountId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); try { @@ -484,7 +484,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol @Override public List listZoneWidePoolIdsByVolumeCount(long dcId, long accountId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); try { @@ -519,7 +519,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol @Override @DB public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); VolumeVO entry = findById(id); if (entry != null) { @@ -533,7 +533,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol @Override public ScopeType getVolumeStoragePoolScope(long volumeId) { // finding the storage scope where the volume is present - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDetailsDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VolumeDetailsDaoImpl.java index 40af999032f..54158f5d9e2 100644 --- a/engine/schema/src/com/cloud/storage/dao/VolumeDetailsDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/VolumeDetailsDaoImpl.java @@ -27,7 +27,7 @@ import org.springframework.stereotype.Component; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value=VolumeDetailsDao.class) @@ -98,7 +98,7 @@ public class VolumeDetailsDaoImpl extends GenericDaoBase i @Override public void persist(long volumeId, Map details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = VolumeSearch.create(); sc.setParameters("volumeId", volumeId); diff --git a/engine/schema/src/com/cloud/upgrade/DatabaseCreator.java b/engine/schema/src/com/cloud/upgrade/DatabaseCreator.java index c97085b6e9e..8260aa125e5 100755 --- a/engine/schema/src/com/cloud/upgrade/DatabaseCreator.java +++ b/engine/schema/src/com/cloud/upgrade/DatabaseCreator.java @@ -30,11 +30,11 @@ import java.util.Properties; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cloud.utils.PropertiesUtil; - import com.cloud.utils.component.ComponentContext; import com.cloud.utils.component.SystemIntegrityChecker; import com.cloud.utils.db.ScriptRunner; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; // Creates the CloudStack Database by using the 4.0 schema and apply // upgrade steps to it. @@ -172,7 +172,7 @@ public class DatabaseCreator { } try { - Transaction.initDataSource(dbPropsFile); + TransactionLegacy.initDataSource(dbPropsFile); } catch (NullPointerException e) { } initDB(dbPropsFile, rootPassword, databases, dryRun); @@ -187,7 +187,7 @@ public class DatabaseCreator { } System.out.println("========> Processing SQL file at " + sqlScript.getAbsolutePath()); - Connection conn = Transaction.getStandaloneConnection(); + Connection conn = TransactionLegacy.getStandaloneConnection(); try { FileReader reader = null; try { @@ -207,7 +207,7 @@ public class DatabaseCreator { } } - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { // Process db upgrade classes for (String upgradeClass: upgradeClasses) { diff --git a/engine/schema/src/com/cloud/upgrade/DatabaseIntegrityChecker.java b/engine/schema/src/com/cloud/upgrade/DatabaseIntegrityChecker.java index 50eb47be027..b6e45ec8995 100755 --- a/engine/schema/src/com/cloud/upgrade/DatabaseIntegrityChecker.java +++ b/engine/schema/src/com/cloud/upgrade/DatabaseIntegrityChecker.java @@ -29,12 +29,11 @@ import org.springframework.stereotype.Component; import com.cloud.maint.Version; import com.cloud.upgrade.dao.VersionDao; - import com.cloud.utils.component.AdapterBase; import com.cloud.utils.component.ComponentLifecycle; import com.cloud.utils.component.SystemIntegrityChecker; import com.cloud.utils.db.GlobalLock; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -72,7 +71,7 @@ public class DatabaseIntegrityChecker extends AdapterBase implements SystemInteg } private Boolean checkDuplicateHostWithTheSameLocalStorage() { - Transaction txn = Transaction.open("Integrity"); + TransactionLegacy txn = TransactionLegacy.open("Integrity"); txn.start(); try { Connection conn; @@ -167,7 +166,7 @@ public class DatabaseIntegrityChecker extends AdapterBase implements SystemInteg } private boolean checkMissedPremiumUpgradeFor228() { - Transaction txn = Transaction.open("Integrity"); + TransactionLegacy txn = TransactionLegacy.open("Integrity"); txn.start(); try { String dbVersion = _dao.getCurrentVersion(); diff --git a/engine/schema/src/com/cloud/upgrade/DatabaseUpgradeChecker.java b/engine/schema/src/com/cloud/upgrade/DatabaseUpgradeChecker.java index 85e36a5afc1..2eec64816a1 100755 --- a/engine/schema/src/com/cloud/upgrade/DatabaseUpgradeChecker.java +++ b/engine/schema/src/com/cloud/upgrade/DatabaseUpgradeChecker.java @@ -73,7 +73,7 @@ import com.cloud.upgrade.dao.VersionVO.Step; import com.cloud.utils.component.SystemIntegrityChecker; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.ScriptRunner; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Local(value = {SystemIntegrityChecker.class}) @@ -269,7 +269,7 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker { s_logger.debug("Running upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade.getUpgradableVersionRange()[1] + " to " + upgrade.getUpgradedVersion()); - Transaction txn = Transaction.open("Upgrade"); + TransactionLegacy txn = TransactionLegacy.open("Upgrade"); txn.start(); try { Connection conn; @@ -339,7 +339,7 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker { VersionVO version = _dao.findByVersion(upgradedVersion, Step.Upgrade); s_logger.debug("Upgrading to version " + upgradedVersion + "..."); - Transaction txn = Transaction.open("Cleanup"); + TransactionLegacy txn = TransactionLegacy.open("Cleanup"); try { if (version != null) { for (DbUpgrade upgrade : versionUpgrades) { diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade307to410.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade307to410.java index 663105065c8..aeeb6d5863f 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade307to410.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade307to410.java @@ -29,7 +29,6 @@ import com.cloud.utils.PropertiesUtil; import com.cloud.utils.crypt.EncryptionSecretKeyChecker; import org.apache.log4j.Logger; -import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.script.Script; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; diff --git a/engine/schema/src/com/cloud/upgrade/dao/VersionDaoImpl.java b/engine/schema/src/com/cloud/upgrade/dao/VersionDaoImpl.java index 8c92765e48b..12c99b2b6c8 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/VersionDaoImpl.java +++ b/engine/schema/src/com/cloud/upgrade/dao/VersionDaoImpl.java @@ -35,7 +35,7 @@ import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -79,7 +79,7 @@ public class VersionDaoImpl extends GenericDaoBase implements V try { s_logger.debug("Checking to see if the database is at a version before it was the version table is created"); - conn = Transaction.getStandaloneConnection(); + conn = TransactionLegacy.getStandaloneConnection(); PreparedStatement pstmt = conn.prepareStatement("SHOW TABLES LIKE 'version'"); ResultSet rs = pstmt.executeQuery(); diff --git a/engine/schema/src/com/cloud/usage/dao/UsageDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageDaoImpl.java index ce6d9e4a477..e1e843e9ed5 100644 --- a/engine/schema/src/com/cloud/usage/dao/UsageDaoImpl.java +++ b/engine/schema/src/com/cloud/usage/dao/UsageDaoImpl.java @@ -38,7 +38,7 @@ import com.cloud.utils.DateUtil; import com.cloud.utils.db.Filter; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -74,7 +74,7 @@ public class UsageDaoImpl extends GenericDaoBase implements Usage @Override public void deleteRecordsForAccount(Long accountId) { String sql = ((accountId == null) ? DELETE_ALL : DELETE_ALL_BY_ACCOUNTID); - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); PreparedStatement pstmt = null; try { txn.start(); @@ -99,7 +99,7 @@ public class UsageDaoImpl extends GenericDaoBase implements Usage @Override public void saveAccounts(List accounts) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); String sql = INSERT_ACCOUNT; @@ -133,7 +133,7 @@ public class UsageDaoImpl extends GenericDaoBase implements Usage @Override public void updateAccounts(List accounts) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); String sql = UPDATE_ACCOUNT; @@ -163,7 +163,7 @@ public class UsageDaoImpl extends GenericDaoBase implements Usage @Override public void saveUserStats(List userStats) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); String sql = INSERT_USER_STATS; @@ -204,7 +204,7 @@ public class UsageDaoImpl extends GenericDaoBase implements Usage @Override public void updateUserStats(List userStats) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); String sql = UPDATE_USER_STATS; @@ -231,7 +231,7 @@ public class UsageDaoImpl extends GenericDaoBase implements Usage @Override public Long getLastAccountId() { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; String sql = GET_LAST_ACCOUNT; try { @@ -248,7 +248,7 @@ public class UsageDaoImpl extends GenericDaoBase implements Usage @Override public Long getLastUserStatsId() { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; String sql = GET_LAST_USER_STATS; try { @@ -265,7 +265,7 @@ public class UsageDaoImpl extends GenericDaoBase implements Usage @Override public List listPublicTemplatesByAccount(long accountId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; String sql = GET_PUBLIC_TEMPLATES_BY_ACCOUNTID; List templateList = new ArrayList(); @@ -284,7 +284,7 @@ public class UsageDaoImpl extends GenericDaoBase implements Usage @Override public Long getLastVmDiskStatsId() { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; String sql = GET_LAST_VM_DISK_STATS; try { @@ -301,7 +301,7 @@ public class UsageDaoImpl extends GenericDaoBase implements Usage @Override public void updateVmDiskStats(List vmDiskStats) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); String sql = UPDATE_VM_DISK_STATS; @@ -335,7 +335,7 @@ public class UsageDaoImpl extends GenericDaoBase implements Usage @Override public void saveVmDiskStats(List vmDiskStats) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); String sql = INSERT_VM_DISK_STATS; @@ -381,7 +381,7 @@ public class UsageDaoImpl extends GenericDaoBase implements Usage @Override public void saveUsageRecords(List usageRecords) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); String sql = INSERT_USGAE_RECORDS; diff --git a/engine/schema/src/com/cloud/usage/dao/UsageIPAddressDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageIPAddressDaoImpl.java index 9af4a267097..00145530a9c 100644 --- a/engine/schema/src/com/cloud/usage/dao/UsageIPAddressDaoImpl.java +++ b/engine/schema/src/com/cloud/usage/dao/UsageIPAddressDaoImpl.java @@ -32,7 +32,7 @@ import com.cloud.usage.UsageIPAddressVO; import com.cloud.user.Account; import com.cloud.utils.DateUtil; import com.cloud.utils.db.GenericDaoBase; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={UsageIPAddressDao.class}) @@ -56,7 +56,7 @@ public class UsageIPAddressDaoImpl extends GenericDaoBase implements @Override public long getLastJobSuccessDateMillis() { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; String sql = GET_LAST_JOB_SUCCESS_DATE_MILLIS; try { @@ -61,7 +61,7 @@ public class UsageJobDaoImpl extends GenericDaoBase implements @Override public void updateJobSuccess(Long jobId, long startMillis, long endMillis, long execTime, boolean success) { - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); try { txn.start(); @@ -115,7 +115,7 @@ public class UsageJobDaoImpl extends GenericDaoBase implements @Override public UsageJobVO isOwner(String hostname, int pid) { - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); try { if ((hostname == null) || (pid <= 0)) { return null; diff --git a/engine/schema/src/com/cloud/usage/dao/UsageLoadBalancerPolicyDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageLoadBalancerPolicyDaoImpl.java index fa632236b79..70b1764498b 100644 --- a/engine/schema/src/com/cloud/usage/dao/UsageLoadBalancerPolicyDaoImpl.java +++ b/engine/schema/src/com/cloud/usage/dao/UsageLoadBalancerPolicyDaoImpl.java @@ -31,7 +31,7 @@ import org.springframework.stereotype.Component; import com.cloud.usage.UsageLoadBalancerPolicyVO; import com.cloud.utils.DateUtil; import com.cloud.utils.db.GenericDaoBase; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={UsageLoadBalancerPolicyDao.class}) @@ -56,7 +56,7 @@ public class UsageLoadBalancerPolicyDaoImpl extends GenericDaoBase im @Override public Map getRecentNetworkStats() { - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); String sql = SELECT_LATEST_STATS; PreparedStatement pstmt = null; try { @@ -85,7 +85,7 @@ public class UsageNetworkDaoImpl extends GenericDaoBase im @Override public void deleteOldStats(long maxEventTime) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); String sql = DELETE_OLD_STATS; PreparedStatement pstmt = null; try { @@ -102,7 +102,7 @@ public class UsageNetworkDaoImpl extends GenericDaoBase im @Override public void saveUsageNetworks (List usageNetworks) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); String sql = INSERT_USAGE_NETWORK; diff --git a/engine/schema/src/com/cloud/usage/dao/UsageNetworkOfferingDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageNetworkOfferingDaoImpl.java index 4ed7c27d491..6fb03e7252d 100644 --- a/engine/schema/src/com/cloud/usage/dao/UsageNetworkOfferingDaoImpl.java +++ b/engine/schema/src/com/cloud/usage/dao/UsageNetworkOfferingDaoImpl.java @@ -31,7 +31,7 @@ import org.springframework.stereotype.Component; import com.cloud.usage.UsageNetworkOfferingVO; import com.cloud.utils.DateUtil; import com.cloud.utils.db.GenericDaoBase; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={UsageNetworkOfferingDao.class}) @@ -55,7 +55,7 @@ public class UsageNetworkOfferingDaoImpl extends GenericDaoBase im } public void removeBy(long accountId, long volId, int storage_type) { - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); PreparedStatement pstmt = null; try { txn.start(); @@ -113,7 +113,7 @@ public class UsageStorageDaoImpl extends GenericDaoBase im } public void update(UsageStorageVO usage) { - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); PreparedStatement pstmt = null; try { txn.start(); @@ -158,7 +158,7 @@ public class UsageStorageDaoImpl extends GenericDaoBase im sql += " LIMIT " + startIndex + ",500"; } - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); PreparedStatement pstmt = null; try { diff --git a/engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java index fc827548781..ef313904956 100644 --- a/engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java +++ b/engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java @@ -31,7 +31,7 @@ import org.springframework.stereotype.Component; import com.cloud.usage.UsageVMInstanceVO; import com.cloud.utils.DateUtil; import com.cloud.utils.db.GenericDaoBase; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={UsageVMInstanceDao.class}) @@ -51,7 +51,7 @@ public class UsageVMInstanceDaoImpl extends GenericDaoBase getUsageRecords(long accountId, Date startDate, Date endDate) { - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); PreparedStatement pstmt = null; List usageInstances = new ArrayList(); try { diff --git a/engine/schema/src/com/cloud/usage/dao/UsageVMSnapshotDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageVMSnapshotDaoImpl.java index 9f98bbf1be5..b9f5f8ce358 100644 --- a/engine/schema/src/com/cloud/usage/dao/UsageVMSnapshotDaoImpl.java +++ b/engine/schema/src/com/cloud/usage/dao/UsageVMSnapshotDaoImpl.java @@ -20,7 +20,7 @@ package com.cloud.usage.dao; import com.cloud.usage.UsageVMSnapshotVO; import com.cloud.utils.DateUtil; import com.cloud.utils.db.GenericDaoBase; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @@ -51,7 +51,7 @@ public class UsageVMSnapshotDaoImpl extends GenericDaoBase usageRecords = new ArrayList(); String sql = GET_USAGE_RECORDS_BY_ACCOUNT; - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); PreparedStatement pstmt = null; try { @@ -130,7 +130,7 @@ public class UsageVMSnapshotDaoImpl extends GenericDaoBase usageRecords = new ArrayList(); String sql = PREVIOUS_QUERY; - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); PreparedStatement pstmt = null; try { int i = 1; diff --git a/engine/schema/src/com/cloud/usage/dao/UsageVPNUserDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageVPNUserDaoImpl.java index d6bf13b41bf..d8d14552cca 100644 --- a/engine/schema/src/com/cloud/usage/dao/UsageVPNUserDaoImpl.java +++ b/engine/schema/src/com/cloud/usage/dao/UsageVPNUserDaoImpl.java @@ -31,7 +31,7 @@ import org.springframework.stereotype.Component; import com.cloud.usage.UsageVPNUserVO; import com.cloud.utils.DateUtil; import com.cloud.utils.db.GenericDaoBase; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={UsageVPNUserDao.class}) @@ -55,7 +55,7 @@ public class UsageVPNUserDaoImpl extends GenericDaoBase im public UsageVPNUserDaoImpl() {} public void update(UsageVPNUserVO usage) { - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); PreparedStatement pstmt = null; try { txn.start(); @@ -99,7 +99,7 @@ public class UsageVPNUserDaoImpl extends GenericDaoBase im sql += " LIMIT " + startIndex + ",500"; } - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); PreparedStatement pstmt = null; try { diff --git a/engine/schema/src/com/cloud/usage/dao/UsageVmDiskDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageVmDiskDaoImpl.java index 8436c5955c8..d2cb6730854 100644 --- a/engine/schema/src/com/cloud/usage/dao/UsageVmDiskDaoImpl.java +++ b/engine/schema/src/com/cloud/usage/dao/UsageVmDiskDaoImpl.java @@ -29,7 +29,7 @@ import org.springframework.stereotype.Component; import com.cloud.usage.UsageVmDiskVO; import com.cloud.utils.db.GenericDaoBase; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -51,7 +51,7 @@ public class UsageVmDiskDaoImpl extends GenericDaoBase impl @Override public Map getRecentVmDiskStats() { - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); String sql = SELECT_LATEST_STATS; PreparedStatement pstmt = null; try { @@ -89,7 +89,7 @@ public class UsageVmDiskDaoImpl extends GenericDaoBase impl @Override public void deleteOldStats(long maxEventTime) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); String sql = DELETE_OLD_STATS; PreparedStatement pstmt = null; try { @@ -106,7 +106,7 @@ public class UsageVmDiskDaoImpl extends GenericDaoBase impl @Override public void saveUsageVmDisks(List usageVmDisks) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); String sql = INSERT_USAGE_VM_DISK; diff --git a/engine/schema/src/com/cloud/usage/dao/UsageVolumeDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageVolumeDaoImpl.java index 039d8f4be90..8bf95b63aa3 100644 --- a/engine/schema/src/com/cloud/usage/dao/UsageVolumeDaoImpl.java +++ b/engine/schema/src/com/cloud/usage/dao/UsageVolumeDaoImpl.java @@ -31,7 +31,7 @@ import org.springframework.stereotype.Component; import com.cloud.usage.UsageVolumeVO; import com.cloud.utils.DateUtil; import com.cloud.utils.db.GenericDaoBase; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={UsageVolumeDao.class}) @@ -56,7 +56,7 @@ public class UsageVolumeDaoImpl extends GenericDaoBase impl public UsageVolumeDaoImpl() {} public void removeBy(long accountId, long volId) { - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); PreparedStatement pstmt = null; try { txn.start(); @@ -75,7 +75,7 @@ public class UsageVolumeDaoImpl extends GenericDaoBase impl } public void update(UsageVolumeVO usage) { - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); PreparedStatement pstmt = null; try { txn.start(); @@ -119,7 +119,7 @@ public class UsageVolumeDaoImpl extends GenericDaoBase impl sql += " LIMIT " + startIndex + ",500"; } - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); PreparedStatement pstmt = null; try { diff --git a/engine/schema/src/com/cloud/user/AccountDetailsDaoImpl.java b/engine/schema/src/com/cloud/user/AccountDetailsDaoImpl.java index 30bbe73a568..9bdaff60309 100755 --- a/engine/schema/src/com/cloud/user/AccountDetailsDaoImpl.java +++ b/engine/schema/src/com/cloud/user/AccountDetailsDaoImpl.java @@ -31,7 +31,7 @@ import com.cloud.utils.db.QueryBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Local(value = {AccountDetailsDao.class}) public class AccountDetailsDaoImpl extends GenericDaoBase implements AccountDetailsDao, ScopedConfigStorage { @@ -57,7 +57,7 @@ public class AccountDetailsDaoImpl extends GenericDaoBase @Override public void persist(long accountId, Map details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = accountSearch.create(); sc.setParameters("accountId", accountId); diff --git a/engine/schema/src/com/cloud/user/dao/AccountDaoImpl.java b/engine/schema/src/com/cloud/user/dao/AccountDaoImpl.java index 41f1f734732..91226e79846 100755 --- a/engine/schema/src/com/cloud/user/dao/AccountDaoImpl.java +++ b/engine/schema/src/com/cloud/user/dao/AccountDaoImpl.java @@ -39,7 +39,7 @@ import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={AccountDao.class}) @@ -124,7 +124,7 @@ public class AccountDaoImpl extends GenericDaoBase implements A @Override public Pair findUserAccountByApiKey(String apiKey) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; Pair userAcctPair = null; try { diff --git a/engine/schema/src/com/cloud/user/dao/UserStatisticsDaoImpl.java b/engine/schema/src/com/cloud/user/dao/UserStatisticsDaoImpl.java index 4a1a51c6d9f..ae21dfec16e 100644 --- a/engine/schema/src/com/cloud/user/dao/UserStatisticsDaoImpl.java +++ b/engine/schema/src/com/cloud/user/dao/UserStatisticsDaoImpl.java @@ -33,7 +33,7 @@ import com.cloud.utils.DateUtil; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={UserStatisticsDao.class}) @@ -100,7 +100,7 @@ public class UserStatisticsDaoImpl extends GenericDaoBase userStats = new ArrayList(); if (minRemovedDate == null) return userStats; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { String sql = ACTIVE_AND_RECENTLY_DELETED_SEARCH + " LIMIT " + startIndex + "," + limit; PreparedStatement pstmt = null; @@ -120,7 +120,7 @@ public class UserStatisticsDaoImpl extends GenericDaoBase listUpdatedStats() { List userStats = new ArrayList(); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement pstmt = null; pstmt = txn.prepareAutoCloseStatement(UPDATED_STATS_SEARCH); diff --git a/engine/schema/src/com/cloud/user/dao/VmDiskStatisticsDaoImpl.java b/engine/schema/src/com/cloud/user/dao/VmDiskStatisticsDaoImpl.java index 02f3406c497..2a3d132eeaa 100644 --- a/engine/schema/src/com/cloud/user/dao/VmDiskStatisticsDaoImpl.java +++ b/engine/schema/src/com/cloud/user/dao/VmDiskStatisticsDaoImpl.java @@ -33,7 +33,7 @@ import com.cloud.utils.DateUtil; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={VmDiskStatisticsDao.class}) @@ -97,7 +97,7 @@ public class VmDiskStatisticsDaoImpl extends GenericDaoBase vmDiskStats = new ArrayList(); if (minRemovedDate == null) return vmDiskStats; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { String sql = ACTIVE_AND_RECENTLY_DELETED_SEARCH + " LIMIT " + startIndex + "," + limit; PreparedStatement pstmt = null; @@ -117,7 +117,7 @@ public class VmDiskStatisticsDaoImpl extends GenericDaoBase listUpdatedStats() { List vmDiskStats = new ArrayList(); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement pstmt = null; pstmt = txn.prepareAutoCloseStatement(UPDATED_VM_NETWORK_STATS_SEARCH); diff --git a/engine/schema/src/com/cloud/vm/dao/ConsoleProxyDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/ConsoleProxyDaoImpl.java index 9af371eb3c8..4238f6a38fd 100644 --- a/engine/schema/src/com/cloud/vm/dao/ConsoleProxyDaoImpl.java +++ b/engine/schema/src/com/cloud/vm/dao/ConsoleProxyDaoImpl.java @@ -34,7 +34,7 @@ import com.cloud.utils.db.Attribute; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.UpdateBuilder; import com.cloud.vm.ConsoleProxyVO; import com.cloud.vm.VirtualMachine.State; @@ -162,7 +162,7 @@ public class ConsoleProxyDaoImpl extends GenericDaoBase im @Override public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); ConsoleProxyVO proxy = createForUpdate(); proxy.setPublicIpAddress(null); @@ -227,7 +227,7 @@ public class ConsoleProxyDaoImpl extends GenericDaoBase im public List> getProxyLoadMatrix() { ArrayList> l = new ArrayList>(); - Transaction txn = Transaction.currentTxn();; + TransactionLegacy txn = TransactionLegacy.currentTxn();; PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(PROXY_ASSIGNMENT_MATRIX); @@ -245,7 +245,7 @@ public class ConsoleProxyDaoImpl extends GenericDaoBase im public List> getDatacenterStoragePoolHostInfo(long dcId, boolean countAllPoolTypes) { ArrayList> l = new ArrayList>(); - Transaction txn = Transaction.currentTxn();; + TransactionLegacy txn = TransactionLegacy.currentTxn();; PreparedStatement pstmt = null; try { if(countAllPoolTypes) { @@ -267,7 +267,7 @@ public class ConsoleProxyDaoImpl extends GenericDaoBase im @Override public int getProxyStaticLoad(long proxyVmId) { - Transaction txn = Transaction.currentTxn();; + TransactionLegacy txn = TransactionLegacy.currentTxn();; PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(GET_PROXY_LOAD); @@ -285,7 +285,7 @@ public class ConsoleProxyDaoImpl extends GenericDaoBase im @Override public int getProxyActiveLoad(long proxyVmId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(GET_PROXY_ACTIVE_LOAD); @@ -304,7 +304,7 @@ public class ConsoleProxyDaoImpl extends GenericDaoBase im private List getDatacenterLoadMatrix(String sql) { ArrayList l = new ArrayList(); - Transaction txn = Transaction.currentTxn();; + TransactionLegacy txn = TransactionLegacy.currentTxn();; PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(sql); @@ -325,7 +325,7 @@ public class ConsoleProxyDaoImpl extends GenericDaoBase im @Override public List getRunningProxyListByMsid(long msid) { List l = new ArrayList(); - Transaction txn = Transaction.currentTxn();; + TransactionLegacy txn = TransactionLegacy.currentTxn();; PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement( diff --git a/engine/schema/src/com/cloud/vm/dao/DomainRouterDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/DomainRouterDaoImpl.java index 65b9d3b27c7..7676e2daf8d 100755 --- a/engine/schema/src/com/cloud/vm/dao/DomainRouterDaoImpl.java +++ b/engine/schema/src/com/cloud/vm/dao/DomainRouterDaoImpl.java @@ -47,7 +47,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.UpdateBuilder; import com.cloud.vm.DomainRouterVO; import com.cloud.vm.VirtualMachine; @@ -143,7 +143,7 @@ public class DomainRouterDaoImpl extends GenericDaoBase im @Override public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); DomainRouterVO router = createForUpdate(); router.setPublicIpAddress(null); @@ -307,7 +307,7 @@ public class DomainRouterDaoImpl extends GenericDaoBase im @Override @DB public DomainRouterVO persist(DomainRouterVO router, List guestNetworks) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); // 1) create network @@ -330,7 +330,7 @@ public class DomainRouterDaoImpl extends GenericDaoBase im if (_routerNetworkDao.findByRouterAndNetwork(router.getId(), guestNetwork.getId()) == null) { NetworkOffering off = _offDao.findById(guestNetwork.getNetworkOfferingId()); if (!(off.getName().equalsIgnoreCase(NetworkOffering.SystemPrivateGatewayNetworkOffering))) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); //1) add router to network RouterNetworkVO routerNtwkMap = new RouterNetworkVO(router.getId(), guestNetwork.getId(), guestNetwork.getGuestType()); diff --git a/engine/schema/src/com/cloud/vm/dao/NicDetailDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/NicDetailDaoImpl.java index 67bc0b5b871..033acca34ec 100644 --- a/engine/schema/src/com/cloud/vm/dao/NicDetailDaoImpl.java +++ b/engine/schema/src/com/cloud/vm/dao/NicDetailDaoImpl.java @@ -19,7 +19,7 @@ package com.cloud.vm.dao; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.vm.NicDetailVO; import org.springframework.stereotype.Component; @@ -75,7 +75,7 @@ public class NicDetailDaoImpl extends GenericDaoBase implemen @Override public void persist(long nicId, Map details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = NicSearch.create(); sc.setParameters("nicId", nicId); diff --git a/engine/schema/src/com/cloud/vm/dao/SecondaryStorageVmDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/SecondaryStorageVmDaoImpl.java index f802a90d39f..2fcc3e7809d 100644 --- a/engine/schema/src/com/cloud/vm/dao/SecondaryStorageVmDaoImpl.java +++ b/engine/schema/src/com/cloud/vm/dao/SecondaryStorageVmDaoImpl.java @@ -31,7 +31,7 @@ import com.cloud.utils.db.Attribute; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.UpdateBuilder; import com.cloud.vm.SecondaryStorageVm; import com.cloud.vm.SecondaryStorageVmVO; @@ -105,7 +105,7 @@ public class SecondaryStorageVmDaoImpl extends GenericDaoBase getRunningSecStorageVmListByMsid(SecondaryStorageVm.Role role, long msid) { List l = new ArrayList(); - Transaction txn = Transaction.currentTxn();; + TransactionLegacy txn = TransactionLegacy.currentTxn();; PreparedStatement pstmt = null; try { String sql; @@ -237,7 +237,7 @@ public class SecondaryStorageVmDaoImpl extends GenericDaoBase listRunningSecStorageOrderByLoad(SecondaryStorageVm.Role role, long zoneId) { List l = new ArrayList(); - Transaction txn = Transaction.currentTxn();; + TransactionLegacy txn = TransactionLegacy.currentTxn();; PreparedStatement pstmt = null; try { String sql; diff --git a/engine/schema/src/com/cloud/vm/dao/UserVmDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/UserVmDaoImpl.java index 8afce099394..9cef0ae65da 100755 --- a/engine/schema/src/com/cloud/vm/dao/UserVmDaoImpl.java +++ b/engine/schema/src/com/cloud/vm/dao/UserVmDaoImpl.java @@ -46,7 +46,7 @@ import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.NicVO; import com.cloud.vm.UserVmVO; @@ -350,7 +350,7 @@ public class UserVmDaoImpl extends GenericDaoBase implements Use @Override public List listPodIdsHavingVmsforAccount(long zoneId, long accountId){ - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); @@ -374,7 +374,7 @@ public class UserVmDaoImpl extends GenericDaoBase implements Use @Override public Hashtable listVmDetails(Hashtable userVmDataHash){ - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { @@ -575,7 +575,7 @@ public class UserVmDaoImpl extends GenericDaoBase implements Use @Override public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); _tagsDao.removeByIdAndType(id, TaggedResourceType.UserVm); boolean result = super.remove(id); diff --git a/engine/schema/src/com/cloud/vm/dao/UserVmDetailsDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/UserVmDetailsDaoImpl.java index 6156b523b2b..6cf9baf1309 100644 --- a/engine/schema/src/com/cloud/vm/dao/UserVmDetailsDaoImpl.java +++ b/engine/schema/src/com/cloud/vm/dao/UserVmDetailsDaoImpl.java @@ -27,7 +27,7 @@ import org.springframework.stereotype.Component; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.vm.UserVmDetailVO; @Component @@ -92,7 +92,7 @@ public class UserVmDetailsDaoImpl extends GenericDaoBase i @Override public void persist(long vmId, Map details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = VmSearch.create(); sc.setParameters("vmId", vmId); diff --git a/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java index cf8e0a89c40..301944b8f07 100644 --- a/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java +++ b/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java @@ -48,7 +48,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.UpdateBuilder; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.NicVO; @@ -484,7 +484,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem @Override public Pair, Map> listClusterIdsInZoneByVmCount(long zoneId, long accountId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); Map clusterVmCountMap = new HashMap(); @@ -513,7 +513,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem @Override public Pair, Map> listClusterIdsInPodByVmCount(long podId, long accountId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); Map clusterVmCountMap = new HashMap(); @@ -543,7 +543,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem @Override public Pair, Map> listPodIdsInZoneByVmCount(long dataCenterId, long accountId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); Map podVmCountMap = new HashMap(); @@ -569,7 +569,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem @Override public List listHostIdsByVmCount(long dcId, Long podId, Long clusterId, long accountId) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); try { @@ -654,7 +654,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem @Override @DB public boolean remove(Long id) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); VMInstanceVO vm = findById(id); if (vm != null && vm.getType() == Type.User) { diff --git a/engine/schema/src/org/apache/cloudstack/affinity/dao/AffinityGroupVMMapDaoImpl.java b/engine/schema/src/org/apache/cloudstack/affinity/dao/AffinityGroupVMMapDaoImpl.java index ad8f4f0a4ea..c2d48d42366 100644 --- a/engine/schema/src/org/apache/cloudstack/affinity/dao/AffinityGroupVMMapDaoImpl.java +++ b/engine/schema/src/org/apache/cloudstack/affinity/dao/AffinityGroupVMMapDaoImpl.java @@ -32,7 +32,7 @@ import com.cloud.utils.db.JoinBuilder.JoinType; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Local(value = { AffinityGroupVMMapDao.class }) public class AffinityGroupVMMapDaoImpl extends GenericDaoBase implements @@ -160,7 +160,7 @@ public class AffinityGroupVMMapDaoImpl extends GenericDaoBase affinityGroupIds) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = createSearchCriteria(); diff --git a/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMComputeTagDaoImpl.java b/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMComputeTagDaoImpl.java index 6f70b353176..54a557bfcb7 100644 --- a/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMComputeTagDaoImpl.java +++ b/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMComputeTagDaoImpl.java @@ -31,7 +31,7 @@ import org.springframework.stereotype.Component; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = { VMComputeTagDao.class }) @@ -52,7 +52,7 @@ public class VMComputeTagDaoImpl extends GenericDaoBase im @Override public void persist(long vmId, List computeTags) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = VmIdSearch.create(); diff --git a/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMEntityDaoImpl.java b/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMEntityDaoImpl.java index 3082d21c1ee..fa2ce81a657 100644 --- a/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMEntityDaoImpl.java +++ b/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMEntityDaoImpl.java @@ -37,7 +37,7 @@ import com.cloud.network.dao.NetworkVO; import com.cloud.utils.Pair; import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.vm.NicProfile; @@ -78,7 +78,7 @@ public class VMEntityDaoImpl extends GenericDaoBase implements @Override @DB public VMEntityVO persist(VMEntityVO vm) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); VMEntityVO dbVO = super.persist(vm); diff --git a/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMNetworkMapDaoImpl.java b/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMNetworkMapDaoImpl.java index 0f2c4ccb77e..f86e788ea1a 100644 --- a/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMNetworkMapDaoImpl.java +++ b/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMNetworkMapDaoImpl.java @@ -28,7 +28,7 @@ import com.cloud.network.dao.NetworkDao; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = { VMNetworkMapDao.class }) @@ -52,7 +52,7 @@ public class VMNetworkMapDaoImpl extends GenericDaoBase im @Override public void persist(long vmId, List networks) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = VmIdSearch.create(); diff --git a/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMReservationDaoImpl.java b/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMReservationDaoImpl.java index 73b4dd2194c..31ed5b8650b 100644 --- a/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMReservationDaoImpl.java +++ b/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMReservationDaoImpl.java @@ -35,7 +35,7 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = { VMReservationDao.class }) @@ -80,7 +80,7 @@ public class VMReservationDaoImpl extends GenericDaoBase @Override @DB public VMReservationVO persist(VMReservationVO reservation) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); VMReservationVO dbVO = super.persist(reservation); diff --git a/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMRootDiskTagDaoImpl.java b/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMRootDiskTagDaoImpl.java index be194bbfcaa..cd06b202b7b 100644 --- a/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMRootDiskTagDaoImpl.java +++ b/engine/schema/src/org/apache/cloudstack/engine/cloud/entity/api/db/dao/VMRootDiskTagDaoImpl.java @@ -31,7 +31,7 @@ import org.springframework.stereotype.Component; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = { VMRootDiskTagDao.class }) @@ -52,7 +52,7 @@ public class VMRootDiskTagDaoImpl extends GenericDaoBase @Override public void persist(long vmId, List rootDiskTags) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); SearchCriteria sc = VmIdSearch.create(); diff --git a/engine/schema/src/org/apache/cloudstack/storage/datastore/db/PrimaryDataStoreDaoImpl.java b/engine/schema/src/org/apache/cloudstack/storage/datastore/db/PrimaryDataStoreDaoImpl.java index 9e2bb365518..8ed84ef8244 100644 --- a/engine/schema/src/org/apache/cloudstack/storage/datastore/db/PrimaryDataStoreDaoImpl.java +++ b/engine/schema/src/org/apache/cloudstack/storage/datastore/db/PrimaryDataStoreDaoImpl.java @@ -40,7 +40,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Local(value = { PrimaryDataStoreDao.class }) @@ -226,7 +226,7 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase @Override @DB public StoragePoolVO persist(StoragePoolVO pool, Map details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); pool = super.persist(pool); if (details != null) { @@ -254,7 +254,7 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase } sql.delete(sql.length() - 4, sql.length()); sql.append(DetailsSqlSuffix); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(sql.toString()); @@ -331,7 +331,7 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase } sql.delete(sql.length() - 4, sql.length()); sql.append(ZoneWideDetailsSqlSuffix); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(sql.toString()); @@ -357,7 +357,7 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase StringBuilder sql = new StringBuilder(FindPoolTagDetails); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(sql.toString()); diff --git a/engine/storage/src/org/apache/cloudstack/storage/endpoint/DefaultEndPointSelector.java b/engine/storage/src/org/apache/cloudstack/storage/endpoint/DefaultEndPointSelector.java index e7c66279e06..196b08b2f42 100644 --- a/engine/storage/src/org/apache/cloudstack/storage/endpoint/DefaultEndPointSelector.java +++ b/engine/storage/src/org/apache/cloudstack/storage/endpoint/DefaultEndPointSelector.java @@ -47,6 +47,7 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.QueryBuilder; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -110,7 +111,7 @@ public class DefaultEndPointSelector implements EndPointSelector { PreparedStatement pstmt = null; ResultSet rs = null; HostVO host = null; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { pstmt = txn.prepareStatement(sql); diff --git a/engine/storage/src/org/apache/cloudstack/storage/image/db/ImageStoreDetailsDaoImpl.java b/engine/storage/src/org/apache/cloudstack/storage/image/db/ImageStoreDetailsDaoImpl.java index ad52042bc7c..3c766cf80c7 100644 --- a/engine/storage/src/org/apache/cloudstack/storage/image/db/ImageStoreDetailsDaoImpl.java +++ b/engine/storage/src/org/apache/cloudstack/storage/image/db/ImageStoreDetailsDaoImpl.java @@ -29,7 +29,7 @@ import org.springframework.stereotype.Component; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = ImageStoreDetailsDao.class) @@ -46,7 +46,7 @@ public class ImageStoreDetailsDaoImpl extends GenericDaoBase details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); SearchCriteria sc = storeSearch.create(); sc.setParameters("store", storeId); diff --git a/engine/storage/src/org/apache/cloudstack/storage/image/db/SnapshotDataStoreDaoImpl.java b/engine/storage/src/org/apache/cloudstack/storage/image/db/SnapshotDataStoreDaoImpl.java index d8280851a38..48416a2d96c 100644 --- a/engine/storage/src/org/apache/cloudstack/storage/image/db/SnapshotDataStoreDaoImpl.java +++ b/engine/storage/src/org/apache/cloudstack/storage/image/db/SnapshotDataStoreDaoImpl.java @@ -40,7 +40,7 @@ import com.cloud.storage.DataStoreRole; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.UpdateBuilder; @@ -164,7 +164,7 @@ public class SnapshotDataStoreDaoImpl extends GenericDaoBase sc = storeSearch.create(); sc.setParameters("store_id", id); sc.setParameters("store_role", role); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); remove(sc); txn.commit(); @@ -182,7 +182,7 @@ public class SnapshotDataStoreDaoImpl extends GenericDaoBase sc = storeSearch.create(); sc.setParameters("store_id", id); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); remove(sc); txn.commit(); @@ -212,7 +212,7 @@ public class TemplateDataStoreDaoImpl extends GenericDaoBase sc = templateSearch.create(); sc.setParameters("template_id", templateId); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); expunge(sc); txn.commit(); diff --git a/engine/storage/src/org/apache/cloudstack/storage/image/db/VolumeDataStoreDaoImpl.java b/engine/storage/src/org/apache/cloudstack/storage/image/db/VolumeDataStoreDaoImpl.java index 04f8b70e44b..2bacd03560a 100644 --- a/engine/storage/src/org/apache/cloudstack/storage/image/db/VolumeDataStoreDaoImpl.java +++ b/engine/storage/src/org/apache/cloudstack/storage/image/db/VolumeDataStoreDaoImpl.java @@ -33,7 +33,7 @@ import org.springframework.stereotype.Component; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.UpdateBuilder; @@ -143,7 +143,7 @@ public class VolumeDataStoreDaoImpl extends GenericDaoBase sc = storeSearch.create(); sc.setParameters("store_id", id); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); remove(sc); txn.commit(); diff --git a/engine/storage/src/org/apache/cloudstack/storage/volume/datastore/PrimaryDataStoreHelper.java b/engine/storage/src/org/apache/cloudstack/storage/volume/datastore/PrimaryDataStoreHelper.java index 4ea4ceec555..90ad17aa6d8 100644 --- a/engine/storage/src/org/apache/cloudstack/storage/volume/datastore/PrimaryDataStoreHelper.java +++ b/engine/storage/src/org/apache/cloudstack/storage/volume/datastore/PrimaryDataStoreHelper.java @@ -43,7 +43,7 @@ import com.cloud.storage.StorageManager; import com.cloud.storage.StoragePoolHostVO; import com.cloud.storage.StoragePoolStatus; import com.cloud.storage.dao.StoragePoolHostDao; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -181,7 +181,7 @@ public class PrimaryDataStoreHelper { public boolean deletePrimaryDataStore(DataStore store) { List hostPoolRecords = this.storagePoolHostDao.listByPoolId(store.getId()); StoragePoolVO poolVO = this.dataStoreDao.findById(store.getId()); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); for (StoragePoolHostVO host : hostPoolRecords) { storagePoolHostDao.deleteStoragePoolHostDetails(host.getHostId(), host.getPoolId()); diff --git a/engine/storage/src/org/apache/cloudstack/storage/volume/db/PrimaryDataStoreDetailsDaoImpl.java b/engine/storage/src/org/apache/cloudstack/storage/volume/db/PrimaryDataStoreDetailsDaoImpl.java index 9d174348c73..be741da3846 100644 --- a/engine/storage/src/org/apache/cloudstack/storage/volume/db/PrimaryDataStoreDetailsDaoImpl.java +++ b/engine/storage/src/org/apache/cloudstack/storage/volume/db/PrimaryDataStoreDetailsDaoImpl.java @@ -27,7 +27,7 @@ import org.springframework.stereotype.Component; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component public class PrimaryDataStoreDetailsDaoImpl extends GenericDaoBase implements @@ -45,7 +45,7 @@ public class PrimaryDataStoreDetailsDaoImpl extends GenericDaoBase details) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); SearchCriteria sc = PoolSearch.create(); sc.setParameters("pool", poolId); diff --git a/framework/cluster/src/com/cloud/cluster/ClusterManagerImpl.java b/framework/cluster/src/com/cloud/cluster/ClusterManagerImpl.java index 56d405e98e9..35968ee015e 100644 --- a/framework/cluster/src/com/cloud/cluster/ClusterManagerImpl.java +++ b/framework/cluster/src/com/cloud/cluster/ClusterManagerImpl.java @@ -44,7 +44,6 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; - import org.apache.cloudstack.framework.config.ConfigDepot; import org.apache.cloudstack.framework.config.ConfigKey; import org.apache.cloudstack.framework.config.Configurable; @@ -62,6 +61,9 @@ import com.cloud.utils.concurrency.NamedThreadFactory; import com.cloud.utils.db.ConnectionConcierge; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionLegacy; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.events.SubscriptionMgr; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.exception.ExceptionUtil; @@ -532,7 +534,7 @@ public class ClusterManagerImpl extends ManagerBase implements ClusterManager, C return new ManagedContextRunnable() { @Override protected void runInContext() { - Transaction txn = Transaction.open("ClusterHeartbeat"); + TransactionLegacy txn = TransactionLegacy.open("ClusterHeartbeat"); try { Profiler profiler = new Profiler(); Profiler profilerHeartbeatUpdate = new Profiler(); @@ -599,7 +601,7 @@ public class ClusterManagerImpl extends ManagerBase implements ClusterManager, C invalidHeartbeatConnection(); } finally { - txn.transitToAutoManagedConnection(Transaction.CLOUD_DB); + txn.transitToAutoManagedConnection(TransactionLegacy.CLOUD_DB); txn.close("ClusterHeartbeat"); } } @@ -620,7 +622,7 @@ public class ClusterManagerImpl extends ManagerBase implements ClusterManager, C private Connection getHeartbeatConnection() throws SQLException { if(_heartbeatConnection == null) { - Connection conn = Transaction.getStandaloneConnectionWithException(); + Connection conn = TransactionLegacy.getStandaloneConnectionWithException(); _heartbeatConnection = new ConnectionConcierge("ClusterManagerHeartbeat", conn, false); } @@ -629,9 +631,9 @@ public class ClusterManagerImpl extends ManagerBase implements ClusterManager, C private void invalidHeartbeatConnection() { if(_heartbeatConnection != null) { - Connection conn = Transaction.getStandaloneConnection(); + Connection conn = TransactionLegacy.getStandaloneConnection(); if (conn != null) { - _heartbeatConnection.reset(Transaction.getStandaloneConnection()); + _heartbeatConnection.reset(TransactionLegacy.getStandaloneConnection()); } } } @@ -942,58 +944,54 @@ public class ClusterManagerImpl extends ManagerBase implements ClusterManager, C s_logger.info("Starting cluster manager, msid : " + _msId); } - Transaction txn = Transaction.currentTxn(); - try { - txn.start(); + ManagementServerHostVO mshost = Transaction.execute(new TransactionCallback() { + @Override + public ManagementServerHostVO doInTransaction(TransactionStatus status) { - final Class c = this.getClass(); - String version = c.getPackage().getImplementationVersion(); - - ManagementServerHostVO mshost = _mshostDao.findByMsid(_msId); - if (mshost == null) { - mshost = new ManagementServerHostVO(); - mshost.setMsid(_msId); - mshost.setRunid(getCurrentRunId()); - mshost.setName(NetUtils.getHostName()); - mshost.setVersion(version); - mshost.setServiceIP(_clusterNodeIP); - mshost.setServicePort(_currentServiceAdapter.getServicePort()); - mshost.setLastUpdateTime(DateUtil.currentGMTTime()); - mshost.setRemoved(null); - mshost.setAlertCount(0); - mshost.setState(ManagementServerHost.State.Up); - _mshostDao.persist(mshost); - - if (s_logger.isInfoEnabled()) { - s_logger.info("New instance of management server msid " + _msId + " is being started"); + final Class c = this.getClass(); + String version = c.getPackage().getImplementationVersion(); + + ManagementServerHostVO mshost = _mshostDao.findByMsid(_msId); + if (mshost == null) { + mshost = new ManagementServerHostVO(); + mshost.setMsid(_msId); + mshost.setRunid(getCurrentRunId()); + mshost.setName(NetUtils.getHostName()); + mshost.setVersion(version); + mshost.setServiceIP(_clusterNodeIP); + mshost.setServicePort(_currentServiceAdapter.getServicePort()); + mshost.setLastUpdateTime(DateUtil.currentGMTTime()); + mshost.setRemoved(null); + mshost.setAlertCount(0); + mshost.setState(ManagementServerHost.State.Up); + _mshostDao.persist(mshost); + + if (s_logger.isInfoEnabled()) { + s_logger.info("New instance of management server msid " + _msId + " is being started"); + } + } else { + if (s_logger.isInfoEnabled()) { + s_logger.info("Management server " + _msId + " is being started"); + } + + _mshostDao.update(mshost.getId(), getCurrentRunId(), NetUtils.getHostName(), version, _clusterNodeIP, _currentServiceAdapter.getServicePort(), DateUtil.currentGMTTime()); } - } else { - if (s_logger.isInfoEnabled()) { - s_logger.info("Management server " + _msId + " is being started"); - } - - _mshostDao.update(mshost.getId(), getCurrentRunId(), NetUtils.getHostName(), version, _clusterNodeIP, _currentServiceAdapter.getServicePort(), DateUtil.currentGMTTime()); + + return mshost; } + }); - txn.commit(); - - _mshostId = mshost.getId(); - if (s_logger.isInfoEnabled()) { - s_logger.info("Management server (host id : " + _mshostId + ") is being started at " + _clusterNodeIP + ":" + _currentServiceAdapter.getServicePort()); - } - - _mshostPeerDao.clearPeerInfo(_mshostId); - - // use seperate thread for heartbeat updates - _heartbeatScheduler.scheduleAtFixedRate(getHeartbeatTask(), HeartbeatInterval.value(), HeartbeatInterval.value(), TimeUnit.MILLISECONDS); - _notificationExecutor.submit(getNotificationTask()); - - } catch (Throwable e) { - s_logger.error("Unexpected exception : ", e); - txn.rollback(); - - throw new CloudRuntimeException("Unable to initialize cluster info into database"); + _mshostId = mshost.getId(); + if (s_logger.isInfoEnabled()) { + s_logger.info("Management server (host id : " + _mshostId + ") is being started at " + _clusterNodeIP + ":" + _currentServiceAdapter.getServicePort()); } + + _mshostPeerDao.clearPeerInfo(_mshostId); + + // use seperate thread for heartbeat updates + _heartbeatScheduler.scheduleAtFixedRate(getHeartbeatTask(), HeartbeatInterval.value(), HeartbeatInterval.value(), TimeUnit.MILLISECONDS); + _notificationExecutor.submit(getNotificationTask()); + if (s_logger.isInfoEnabled()) { s_logger.info("Cluster manager was started successfully"); diff --git a/framework/cluster/src/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java b/framework/cluster/src/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java index 879c4ce3a27..246bfe6bcd4 100644 --- a/framework/cluster/src/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java +++ b/framework/cluster/src/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java @@ -38,7 +38,7 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Local(value={ManagementServerHostDao.class}) @@ -52,7 +52,7 @@ public class ManagementServerHostDaoImpl extends GenericDaoBase listOrphanMsids() { List orphanList = new ArrayList(); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement( diff --git a/framework/cluster/src/com/cloud/cluster/dao/ManagementServerHostPeerDaoImpl.java b/framework/cluster/src/com/cloud/cluster/dao/ManagementServerHostPeerDaoImpl.java index 8ef2e82a943..f51076ceb47 100644 --- a/framework/cluster/src/com/cloud/cluster/dao/ManagementServerHostPeerDaoImpl.java +++ b/framework/cluster/src/com/cloud/cluster/dao/ManagementServerHostPeerDaoImpl.java @@ -28,7 +28,7 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Local(value={ManagementServerHostPeerDao.class}) public class ManagementServerHostPeerDaoImpl extends GenericDaoBase implements ManagementServerHostPeerDao { @@ -68,7 +68,7 @@ public class ManagementServerHostPeerDaoImpl extends GenericDaoBase extends Compone List groupByValues = addGroupBy(str, sc); addFilter(str, filter); - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); if (lock != null) { assert (txn.dbTxnStarted() == true) : "As nice as I can here now....how do you lock when there's no DB transaction? Review your db 101 course from college."; str.append(lock ? FOR_UPDATE_CLAUSE : SHARE_MODE_CLAUSE); @@ -452,7 +452,7 @@ public abstract class GenericDaoBase extends Compone final String sql = str.toString(); - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(sql); @@ -770,7 +770,7 @@ public abstract class GenericDaoBase extends Compone } SearchCriteria sc = createSearchCriteria(); sc.addAnd(_idAttributes.get(_table)[0], SearchCriteria.Op.EQ, id); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); try { @@ -791,7 +791,7 @@ public abstract class GenericDaoBase extends Compone public int update(UpdateBuilder ub, final SearchCriteria sc, Integer rows) { StringBuilder sql = null; PreparedStatement pstmt = null; - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); try { final String searchClause = sc.getWhereClause(); @@ -971,7 +971,7 @@ public abstract class GenericDaoBase extends Compone if (lock != null) { sql.append(lock ? FOR_UPDATE_CLAUSE : SHARE_MODE_CLAUSE); } - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(sql.toString()); @@ -994,7 +994,7 @@ public abstract class GenericDaoBase extends Compone @Override public T acquireInLockTable(final ID id, int seconds) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); T t = null; boolean locked = false; try { @@ -1014,7 +1014,7 @@ public abstract class GenericDaoBase extends Compone @Override public boolean releaseFromLockTable(final ID id) { - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); return txn.release(_table + id); } @@ -1025,13 +1025,13 @@ public abstract class GenericDaoBase extends Compone @Override public boolean lockInLockTable(final String id, int seconds) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); return txn.lock(_table + id, seconds); } @Override public boolean unlockFromLockTable(final String id) { - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); return txn.release(_table + id); } @@ -1076,7 +1076,7 @@ public abstract class GenericDaoBase extends Compone } protected List executeList(final String sql, final Object... params) { - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; final List result = new ArrayList(); try { @@ -1118,7 +1118,7 @@ public abstract class GenericDaoBase extends Compone @Override public boolean expunge(final ID id) { - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; String sql = null; try { @@ -1158,7 +1158,7 @@ public abstract class GenericDaoBase extends Compone final String sql = str.toString(); - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(sql); @@ -1296,7 +1296,7 @@ public abstract class GenericDaoBase extends Compone } ID id = null; - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; String sql = null; try { @@ -1359,7 +1359,7 @@ public abstract class GenericDaoBase extends Compone } protected void insertElementCollection(T entity, Attribute idAttribute, ID id, Map ecAttributes) throws SQLException { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); for (Map.Entry entry : ecAttributes.entrySet()) { Attribute attr = entry.getKey(); @@ -1591,7 +1591,7 @@ public abstract class GenericDaoBase extends Compone protected void loadCollection(T entity, Attribute attr) { EcInfo ec = (EcInfo)attr.attache; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); ResultSet rs = null; PreparedStatement pstmt = null; try { @@ -1675,7 +1675,7 @@ public abstract class GenericDaoBase extends Compone } final StringBuilder sql = new StringBuilder("DELETE FROM "); sql.append(_table).append(" WHERE ").append(_removed.first()).append(" IS NOT NULL"); - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { txn.start(); @@ -1708,7 +1708,7 @@ public abstract class GenericDaoBase extends Compone return expunge(id); } - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { @@ -1817,7 +1817,7 @@ public abstract class GenericDaoBase extends Compone // we have to disable group by in getting count, since count for groupBy clause will be different. //List groupByValues = addGroupBy(str, sc); - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); final String sql = str.toString(); PreparedStatement pstmt = null; diff --git a/framework/db/src/com/cloud/utils/db/Merovingian2.java b/framework/db/src/com/cloud/utils/db/Merovingian2.java index 0e0e8f250b1..8544aab0384 100644 --- a/framework/db/src/com/cloud/utils/db/Merovingian2.java +++ b/framework/db/src/com/cloud/utils/db/Merovingian2.java @@ -65,7 +65,7 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean { _msId = msId; Connection conn = null; try { - conn = Transaction.getStandaloneConnectionWithException(); + conn = TransactionLegacy.getStandaloneConnectionWithException(); conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); conn.setAutoCommit(true); _concierge = new ConnectionConcierge("LockMaster", conn, true); diff --git a/framework/db/src/com/cloud/utils/db/SequenceFetcher.java b/framework/db/src/com/cloud/utils/db/SequenceFetcher.java index 88235527fc2..bb45847a7bb 100644 --- a/framework/db/src/com/cloud/utils/db/SequenceFetcher.java +++ b/framework/db/src/com/cloud/utils/db/SequenceFetcher.java @@ -98,7 +98,7 @@ public class SequenceFetcher { sql.append(_tg.valueColumnName()).append(" FROM ").append(_tg.table()); sql.append(" WHERE ").append(_tg.pkColumnName()).append(" = ? FOR UPDATE"); - Transaction txn = Transaction.open("Sequence"); + TransactionLegacy txn = TransactionLegacy.open("Sequence"); PreparedStatement selectStmt = txn.prepareStatement(sql.toString()); if (_key == null) { diff --git a/framework/db/src/com/cloud/utils/db/Transaction.java b/framework/db/src/com/cloud/utils/db/Transaction.java index 8c2f1996a1c..4b19be088c8 100755 --- a/framework/db/src/com/cloud/utils/db/Transaction.java +++ b/framework/db/src/com/cloud/utils/db/Transaction.java @@ -16,1158 +16,39 @@ // under the License. package com.cloud.utils.db; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Savepoint; -import java.sql.Statement; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Properties; import java.util.concurrent.atomic.AtomicLong; -import javax.sql.DataSource; - -import org.apache.commons.dbcp.ConnectionFactory; -import org.apache.commons.dbcp.DriverManagerConnectionFactory; -import org.apache.commons.dbcp.PoolableConnectionFactory; -import org.apache.commons.dbcp.PoolingDataSource; -import org.apache.commons.pool.KeyedObjectPoolFactory; -import org.apache.commons.pool.impl.GenericObjectPool; -import org.apache.commons.pool.impl.StackKeyedObjectPoolFactory; -import org.apache.log4j.Logger; -import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; -import org.jasypt.properties.EncryptableProperties; - -import com.cloud.utils.Pair; -import com.cloud.utils.PropertiesUtil; -import com.cloud.utils.crypt.EncryptionSecretKeyChecker; -import com.cloud.utils.exception.CloudRuntimeException; -import com.cloud.utils.mgmt.JmxUtil; - -/** - * Transaction abstracts away the Connection object in JDBC. It allows the - * following things that the Connection object does not. - * - * 1. Transaction can be started at an entry point and whether the DB - * actions should be auto-commit or not determined at that point. - * 2. DB Connection is allocated only when it is needed. - * 3. Code does not need to know if a transaction has been started or not. - * It just starts/ends a transaction and we resolve it correctly with - * the previous actions. - * - * Note that this class is not synchronous but it doesn't need to be because - * it is stored with TLS and is one per thread. Use appropriately. - */ public class Transaction { - private static final Logger s_logger = Logger.getLogger(Transaction.class.getName() + "." + "Transaction"); - private static final Logger s_stmtLogger = Logger.getLogger(Transaction.class.getName() + "." + "Statement"); - private static final Logger s_lockLogger = Logger.getLogger(Transaction.class.getName() + "." + "Lock"); - private static final Logger s_connLogger = Logger.getLogger(Transaction.class.getName() + "." + "Connection"); + private final static AtomicLong counter = new AtomicLong(0); + private final static TransactionStatus STATUS = new TransactionStatus() { + }; - private static final ThreadLocal tls = new ThreadLocal(); - private static final String START_TXN = "start_txn"; - private static final String CURRENT_TXN = "current_txn"; - private static final String CREATE_TXN = "create_txn"; - private static final String CREATE_CONN = "create_conn"; - private static final String STATEMENT = "statement"; - private static final String ATTACHMENT = "attachment"; - - public static final short CLOUD_DB = 0; - public static final short USAGE_DB = 1; - public static final short AWSAPI_DB = 2; - public static final short SIMULATOR_DB = 3; - public static final short CONNECTED_DB = -1; - - private static AtomicLong s_id = new AtomicLong(); - private static final TransactionMBeanImpl s_mbean = new TransactionMBeanImpl(); - static { + @SuppressWarnings("deprecation") + public static T execute(TransactionCallbackWithException callback) throws E { + String name = "tx-" + counter.incrementAndGet(); + short databaseId = TransactionLegacy.CLOUD_DB; + TransactionLegacy currentTxn = TransactionLegacy.currentTxn(false); + if ( currentTxn != null ) { + databaseId = currentTxn.getDatabaseId(); + } + TransactionLegacy txn = TransactionLegacy.open(name, databaseId, false); try { - JmxUtil.registerMBean("Transaction", "Transaction", s_mbean); - } catch (Exception e) { - s_logger.error("Unable to register mbean for transaction", e); + txn.start(); + T result = callback.doInTransaction(STATUS); + txn.commit(); + return result; + } finally { + txn.close(); } - - /* FIXME: We need a better solution for this - * Initialize encryption if we need it for db.properties - */ - EncryptionSecretKeyChecker enc = new EncryptionSecretKeyChecker(); - enc.check(); } - private final LinkedList _stack; - private long _id; - - private final LinkedList> _lockTimes = new LinkedList>(); - - private String _name; - private Connection _conn; - private boolean _txn; - private short _dbId; - private long _txnTime; - private Statement _stmt; - private String _creator; - - private Transaction _prev = null; - - public static Transaction currentTxn() { - Transaction txn = tls.get(); - assert txn != null : "No Transaction on stack. Did you mark the method with @DB?"; - - assert checkAnnotation(3, txn) : "Did you even read the guide to use Transaction...IOW...other people's code? Try method can't be private. What about @DB? hmmm... could that be it? " + txn; - return txn; - } - - public static Transaction open(final short databaseId) { - String name = buildName(); - if (name == null) { - name = CURRENT_TXN; - } - return open(name, databaseId, true); - } - - // - // Usage of this transaction setup should be limited, it will always open a new transaction context regardless of whether or not there is other - // transaction context in the stack. It is used in special use cases that we want to control DB connection explicitly and in the mean time utilize - // the existing DAO features - // - public void transitToUserManagedConnection(Connection conn) { - assert(_conn == null /*&& _stack.size() <= 1*/) : "Can't change to a user managed connection unless the stack is empty and the db connection is null, you may have forgotten to invoke transitToAutoManagedConnection to close out the DB connection: " + toString(); - _conn = conn; - _dbId = CONNECTED_DB; - } - - public void transitToAutoManagedConnection(short dbId) { - // assert(_stack.size() <= 1) : "Can't change to auto managed connection unless your stack is empty"; - _dbId = dbId; - _conn = null; - } - - public static Transaction open(final String name) { - return open(name, CLOUD_DB, false); - } - - public static Transaction open(final String name, final short databaseId, final boolean forceDbChange) { - Transaction txn = tls.get(); - boolean isNew = false; - if (txn == null) { - if (s_logger.isTraceEnabled()) { - s_logger.trace("Creating the transaction: " + name); + public static T execute(final TransactionCallback callback) { + return execute(new TransactionCallbackWithException() { + @Override + public T doInTransaction(TransactionStatus status) throws RuntimeException { + return callback.doInTransaction(status); } - txn = new Transaction(name, false, databaseId); - tls.set(txn); - isNew = true; - } else if (forceDbChange) { - final short currentDbId = txn.getDatabaseId(); - if (currentDbId != databaseId) { - // we need to end the current transaction and switch databases - txn.close(txn.getName()); - - txn = new Transaction(name, false, databaseId); - tls.set(txn); - isNew = true; - } - } - - txn.takeOver(name, false); - if (isNew) { - s_mbean.addTransaction(txn); - } - return txn; + }); } - protected StackElement peekInStack(Object obj) { - final Iterator it = _stack.iterator(); - while (it.hasNext()) { - StackElement next = it.next(); - if (next.type == obj) { - return next; - } - } - return null; - } - - public void registerLock(String sql) { - if (_txn && s_lockLogger.isDebugEnabled()) { - Pair time = new Pair(sql, System.currentTimeMillis()); - _lockTimes.add(time); - } - } - - public boolean dbTxnStarted() { - return _txn; - } - - public static Connection getStandaloneConnectionWithException() throws SQLException { - Connection conn = s_ds.getConnection(); - if (s_connLogger.isTraceEnabled()) { - s_connLogger.trace("Retrieving a standalone connection: dbconn" + System.identityHashCode(conn)); - } - return conn; - } - - public static Connection getStandaloneConnection() { - try { - return getStandaloneConnectionWithException(); - } catch (SQLException e) { - s_logger.error("Unexpected exception: ", e); - return null; - } - } - - public static Connection getStandaloneUsageConnection() { - try { - Connection conn = s_usageDS.getConnection(); - if (s_connLogger.isTraceEnabled()) { - s_connLogger.trace("Retrieving a standalone connection for usage: dbconn" + System.identityHashCode(conn)); - } - return conn; - } catch (SQLException e) { - s_logger.warn("Unexpected exception: ", e); - return null; - } - } - - public static Connection getStandaloneAwsapiConnection() { - try { - Connection conn = s_awsapiDS.getConnection(); - if (s_connLogger.isTraceEnabled()) { - s_connLogger.trace("Retrieving a standalone connection for usage: dbconn" + System.identityHashCode(conn)); - } - return conn; - } catch (SQLException e) { - s_logger.warn("Unexpected exception: ", e); - return null; - } - } - - public static Connection getStandaloneSimulatorConnection() { - try { - Connection conn = s_simulatorDS.getConnection(); - if (s_connLogger.isTraceEnabled()) { - s_connLogger.trace("Retrieving a standalone connection for simulator: dbconn" + System.identityHashCode(conn)); - } - return conn; - } catch (SQLException e) { - s_logger.warn("Unexpected exception: ", e); - return null; - } - } - - protected void attach(TransactionAttachment value) { - _stack.push(new StackElement(ATTACHMENT, value)); - } - - protected TransactionAttachment detach(String name) { - Iterator it = _stack.descendingIterator(); - while (it.hasNext()) { - StackElement element = it.next(); - if (element.type == ATTACHMENT) { - TransactionAttachment att = (TransactionAttachment)element.ref; - if (name.equals(att.getName())) { - it.remove(); - return att; - } - } - } - assert false : "Are you sure you attached this: " + name; - return null; - } - - public static void attachToTxn(TransactionAttachment value) { - Transaction txn = tls.get(); - assert txn != null && txn.peekInStack(CURRENT_TXN) != null: "Come on....how can we attach something to the transaction if you haven't started it?"; - - txn.attach(value); - } - - public static TransactionAttachment detachFromTxn(String name) { - Transaction txn = tls.get(); - assert txn != null : "No Transaction in TLS"; - return txn.detach(name); - } - - protected static boolean checkAnnotation(int stack, Transaction txn) { - final StackTraceElement[] stacks = Thread.currentThread().getStackTrace(); - StackElement se = txn.peekInStack(CURRENT_TXN); - if (se == null) { - return false; - } - - StringBuffer sb = new StringBuffer(); - for (; stack < stacks.length; stack++) { - String methodName = stacks[stack].getMethodName(); - sb.append(" ").append(methodName); - if (methodName.equals(se.ref)){ - return true; - } - } - - // relax stack structure for several places that @DB required injection is not in place - s_logger.warn("Non-standard stack context that Transaction context is manaully placed into the calling chain. Stack chain: " + sb); - return true; - } - - protected static String buildName() { - if (s_logger.isDebugEnabled()) { - final StackTraceElement[] stacks = Thread.currentThread().getStackTrace(); - final StringBuilder str = new StringBuilder(); - int i = 3, j = 3; - while (j < 15 && i < stacks.length) { - StackTraceElement element = stacks[i]; - String filename = element.getFileName(); - String method = element.getMethodName(); - if ((filename != null && filename.equals("")) || (method != null && method.equals("invokeSuper"))) { - i++; - continue; - } - - str.append("-").append(stacks[i].getClassName().substring(stacks[i].getClassName().lastIndexOf(".") + 1)).append(".").append(stacks[i].getMethodName()).append(":").append(stacks[i].getLineNumber()); - j++; - i++; - } - return str.toString(); - } - - return ""; - } - - public Transaction(final String name, final boolean forLocking, final short databaseId) { - _name = name; - _conn = null; - _stack = new LinkedList(); - _txn = false; - _dbId = databaseId; - _id = s_id.incrementAndGet(); - _creator = Thread.currentThread().getName(); - } - - public String getCreator() { - return _creator; - } - - public long getId() { - return _id; - } - - public String getName() { - return _name; - } - - public Short getDatabaseId() { - return _dbId; - } - - @Override - public String toString() { - final StringBuilder str = new StringBuilder((_name != null ? _name : "")); - str.append(" : "); - for (final StackElement se : _stack) { - if (se.type == CURRENT_TXN) { - str.append(se.ref).append(", "); - } - } - - return str.toString(); - } - - protected void mark(final String name) { - _stack.push(new StackElement(CURRENT_TXN, name)); - } - - public boolean lock(final String name, final int timeoutSeconds) { - Merovingian2 lockMaster = Merovingian2.getLockMaster(); - if (lockMaster == null) { - throw new CloudRuntimeException("There's no support for locking yet"); - } - return lockMaster.acquire(name, timeoutSeconds); - } - - public boolean release(final String name) { - Merovingian2 lockMaster = Merovingian2.getLockMaster(); - if (lockMaster == null) { - throw new CloudRuntimeException("There's no support for locking yet"); - } - return lockMaster.release(name); - } - - public void start() { - if (s_logger.isTraceEnabled()) { - s_logger.trace("txn: start requested by: " + buildName()); - } - - _stack.push(new StackElement(START_TXN, null)); - - if (_txn) { - s_logger.trace("txn: has already been started."); - return; - } - - _txn = true; - - _txnTime = System.currentTimeMillis(); - if (_conn != null) { - try { - s_logger.trace("txn: set auto commit to false"); - _conn.setAutoCommit(false); - } catch (final SQLException e) { - s_logger.warn("Unable to set auto commit: ", e); - throw new CloudRuntimeException("Unable to set auto commit: ", e); - } - } - } - - protected void closePreviousStatement() { - if (_stmt != null) { - try { - if (s_stmtLogger.isTraceEnabled()) { - s_stmtLogger.trace("Closing: " + _stmt.toString()); - } - try { - ResultSet rs = _stmt.getResultSet(); - if (rs != null && _stmt.getResultSetHoldability() != ResultSet.HOLD_CURSORS_OVER_COMMIT) { - rs.close(); - } - } catch(SQLException e) { - s_stmtLogger.trace("Unable to close resultset"); - } - _stmt.close(); - } catch (final SQLException e) { - s_stmtLogger.trace("Unable to close statement: " + _stmt.toString()); - } finally { - _stmt = null; - } - } - } - - /** - * Prepares an auto close statement. The statement is closed automatically if it is - * retrieved with this method. - * - * @param sql sql String - * @return PreparedStatement - * @throws SQLException if problem with JDBC layer. - * - * @see java.sql.Connection - */ - public PreparedStatement prepareAutoCloseStatement(final String sql) throws SQLException { - PreparedStatement stmt = prepareStatement(sql); - closePreviousStatement(); - _stmt = stmt; - return stmt; - } - - public PreparedStatement prepareStatement(final String sql) throws SQLException { - final Connection conn = getConnection(); - final PreparedStatement pstmt = conn.prepareStatement(sql); - if (s_stmtLogger.isTraceEnabled()) { - s_stmtLogger.trace("Preparing: " + sql); - } - return pstmt; - } - - /** - * Prepares an auto close statement. The statement is closed automatically if it is - * retrieved with this method. - * - * @param sql sql String - * @param autoGeneratedKeys keys that are generated - * @return PreparedStatement - * @throws SQLException if problem with JDBC layer. - * - * @see java.sql.Connection - */ - public PreparedStatement prepareAutoCloseStatement(final String sql, final int autoGeneratedKeys) throws SQLException { - final Connection conn = getConnection(); - final PreparedStatement pstmt = conn.prepareStatement(sql, autoGeneratedKeys); - if (s_stmtLogger.isTraceEnabled()) { - s_stmtLogger.trace("Preparing: " + sql); - } - closePreviousStatement(); - _stmt = pstmt; - return pstmt; - } - - /** - * Prepares an auto close statement. The statement is closed automatically if it is - * retrieved with this method. - * - * @param sql sql String - * @param columnNames names of the columns - * @return PreparedStatement - * @throws SQLException if problem with JDBC layer. - * - * @see java.sql.Connection - */ - public PreparedStatement prepareAutoCloseStatement(final String sql, final String[] columnNames) throws SQLException { - final Connection conn = getConnection(); - final PreparedStatement pstmt = conn.prepareStatement(sql, columnNames); - if (s_stmtLogger.isTraceEnabled()) { - s_stmtLogger.trace("Preparing: " + sql); - } - closePreviousStatement(); - _stmt = pstmt; - return pstmt; - } - - /** - * Prepares an auto close statement. The statement is closed automatically if it is - * retrieved with this method. - * - * @param sql sql String - * @return PreparedStatement - * @throws SQLException if problem with JDBC layer. - * - * @see java.sql.Connection - */ - public PreparedStatement prepareAutoCloseStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { - final Connection conn = getConnection(); - final PreparedStatement pstmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability); - if (s_stmtLogger.isTraceEnabled()) { - s_stmtLogger.trace("Preparing: " + sql); - } - closePreviousStatement(); - _stmt = pstmt; - return pstmt; - } - - /** - * Returns the db connection. - * - * Note: that you can call getConnection() but beaware that - * all prepare statements from the Connection are not garbage - * collected! - * - * @return DB Connection but make sure you understand that - * you are responsible for closing the PreparedStatement. - * @throws SQLException - */ - public Connection getConnection() throws SQLException { - if (_conn == null) { - switch (_dbId) { - case CLOUD_DB: - if(s_ds != null) { - _conn = s_ds.getConnection(); - } else { - s_logger.warn("A static-initialized variable becomes null, process is dying?"); - throw new CloudRuntimeException("Database is not initialized, process is dying?"); - } - break; - case USAGE_DB: - if(s_usageDS != null) { - _conn = s_usageDS.getConnection(); - } else { - s_logger.warn("A static-initialized variable becomes null, process is dying?"); - throw new CloudRuntimeException("Database is not initialized, process is dying?"); - } - break; - case AWSAPI_DB: - if(s_awsapiDS != null) { - _conn = s_awsapiDS.getConnection(); - } else { - s_logger.warn("A static-initialized variable becomes null, process is dying?"); - throw new CloudRuntimeException("Database is not initialized, process is dying?"); - } - break; - - case SIMULATOR_DB: - if(s_simulatorDS != null) { - _conn = s_simulatorDS.getConnection(); - } else { - s_logger.warn("A static-initialized variable becomes null, process is dying?"); - throw new CloudRuntimeException("Database is not initialized, process is dying?"); - } - break; - default: - - throw new CloudRuntimeException("No database selected for the transaction"); - } - _conn.setAutoCommit(!_txn); - - // - // MySQL default transaction isolation level is REPEATABLE READ, - // to reduce chances of DB deadlock, we will use READ COMMITED isolation level instead - // see http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html - // - _stack.push(new StackElement(CREATE_CONN, null)); - if (s_connLogger.isTraceEnabled()) { - s_connLogger.trace("Creating a DB connection with " + (_txn ? " txn: " : " no txn: ") + " for " + _dbId + ": dbconn" + System.identityHashCode(_conn) + ". Stack: " + buildName()); - } - } else { - s_logger.trace("conn: Using existing DB connection"); - } - - return _conn; - } - - protected boolean takeOver(final String name, final boolean create) { - if (_stack.size() != 0) { - if (!create) { - // If it is not a create transaction, then let's just use the current one. - if (s_logger.isTraceEnabled()) { - s_logger.trace("Using current transaction: " + toString()); - } - mark(name); - return false; - } - - final StackElement se = _stack.getFirst(); - if (se.type == CREATE_TXN) { - // This create is called inside of another create. Which is ok? - // We will let that create be responsible for cleaning up. - if (s_logger.isTraceEnabled()) { - s_logger.trace("Create using current transaction: " + toString()); - } - mark(name); - return false; - } - - s_logger.warn("Encountered a transaction that has leaked. Cleaning up. " + toString()); - cleanup(); - } - - if (s_logger.isTraceEnabled()) { - s_logger.trace("Took over the transaction: " + name); - } - _stack.push(new StackElement(create ? CREATE_TXN : CURRENT_TXN, name)); - _name = name; - return true; - } - - public void cleanup() { - closePreviousStatement(); - - removeUpTo(null, null); - if (_txn) { - rollbackTransaction(); - } - _txn = false; - _name = null; - - closeConnection(); - - _stack.clear(); - Merovingian2 lockMaster = Merovingian2.getLockMaster(); - if (lockMaster != null) { - lockMaster.cleanupThread(); - } - } - - public void close() { - removeUpTo(CURRENT_TXN, null); - - if (_stack.size() == 0) { - s_logger.trace("Transaction is done"); - cleanup(); - } - } - - /** - * close() is used by endTxn to close the connection. This method only - * closes the connection if the name is the same as what's stored. - * - * @param name - * @return true if this close actually closes the connection. false if not. - */ - public boolean close(final String name) { - if (_name == null) { // Already cleaned up. - if (s_logger.isTraceEnabled()) { - s_logger.trace("Already cleaned up." + buildName()); - } - return true; - } - - if (!_name.equals(name)) { - close(); - return false; - } - - if (s_logger.isDebugEnabled() && _stack.size() > 2) { - s_logger.debug("Transaction is not closed properly: " + toString() + ". Called by " + buildName()); - } - - cleanup(); - - s_logger.trace("All done"); - return true; - } - - protected boolean hasTxnInStack() { - return peekInStack(START_TXN) != null; - } - - protected void clearLockTimes() { - if (s_lockLogger.isDebugEnabled()) { - for (Pair time : _lockTimes) { - s_lockLogger.trace("SQL " + time.first() + " took " + (System.currentTimeMillis() - time.second())); - } - _lockTimes.clear(); - } - } - - public boolean commit() { - if (!_txn) { - s_logger.warn("txn: Commit called when it is not a transaction: " + buildName()); - return false; - } - - Iterator it = _stack.iterator(); - while (it.hasNext()) { - StackElement st = it.next(); - if (st.type == START_TXN) { - it.remove(); - break; - } - } - - if (hasTxnInStack()) { - if (s_logger.isTraceEnabled()) { - s_logger.trace("txn: Not committing because transaction started elsewhere: " + buildName() + " / " + toString()); - } - return false; - } - - _txn = false; - try { - if (_conn != null) { - _conn.commit(); - s_logger.trace("txn: DB Changes committed. Time = " + (System.currentTimeMillis() - _txnTime)); - clearLockTimes(); - closeConnection(); - } - return true; - } catch (final SQLException e) { - rollbackTransaction(); - throw new CloudRuntimeException("Unable to commit or close the connection. ", e); - } - } - - protected void closeConnection() { - closePreviousStatement(); - - if (_conn == null) { - return; - } - - if (_txn) { - s_connLogger.trace("txn: Not closing DB connection because we're still in a transaction."); - return; - } - - try { - // we should only close db connection when it is not user managed - if (this._dbId != CONNECTED_DB) { - if (s_connLogger.isTraceEnabled()) { - s_connLogger.trace("Closing DB connection: dbconn" + System.identityHashCode(_conn)); - } - _conn.close(); - _conn = null; - } - - } catch (final SQLException e) { - s_logger.warn("Unable to close connection", e); - } - } - - protected void removeUpTo(String type, Object ref) { - boolean rollback = false; - Iterator it = _stack.iterator(); - while (it.hasNext()) { - StackElement item = it.next(); - - it.remove(); - - try { - if (item.type == type && (ref == null || item.ref == ref)) { - break; - } - - if (item.type == CURRENT_TXN) { - if (s_logger.isTraceEnabled()) { - s_logger.trace("Releasing the current txn: " + (item.ref != null ? item.ref : "")); - } - } else if (item.type == CREATE_CONN) { - closeConnection(); - } else if (item.type == START_TXN) { - if (item.ref == null) { - rollback = true; - } else { - try { - _conn.rollback((Savepoint)ref); - rollback = false; - } catch (final SQLException e) { - s_logger.warn("Unable to rollback Txn.", e); - } - } - } else if (item.type == STATEMENT) { - try { - if (s_stmtLogger.isTraceEnabled()) { - s_stmtLogger.trace("Closing: " + ref.toString()); - } - Statement stmt = (Statement)ref; - try { - ResultSet rs = stmt.getResultSet(); - if (rs != null) { - rs.close(); - } - } catch(SQLException e) { - s_stmtLogger.trace("Unable to close resultset"); - } - stmt.close(); - } catch (final SQLException e) { - s_stmtLogger.trace("Unable to close statement: " + item); - } - } else if (item.type == ATTACHMENT) { - TransactionAttachment att = (TransactionAttachment)item.ref; - if (s_logger.isTraceEnabled()) { - s_logger.trace("Cleaning up " + att.getName()); - } - att.cleanup(); - } - } catch(Exception e) { - s_logger.error("Unable to clean up " + item, e); - } - } - - if (rollback) { - rollback(); - } - } - - protected void rollbackTransaction() { - closePreviousStatement(); - if (!_txn) { - if (s_logger.isTraceEnabled()) { - s_logger.trace("Rollback called for " + _name + " when there's no transaction: " + buildName()); - } - return; - } - assert (!hasTxnInStack()) : "Who's rolling back transaction when there's still txn in stack?"; - _txn = false; - try { - if (_conn != null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Rolling back the transaction: Time = " + (System.currentTimeMillis() - _txnTime) + " Name = " + _name + "; called by " + buildName()); - } - _conn.rollback(); - } - clearLockTimes(); - closeConnection(); - } catch(final SQLException e) { - s_logger.warn("Unable to rollback", e); - } - } - - protected void rollbackSavepoint(Savepoint sp) { - try { - if (_conn != null) { - _conn.rollback(sp); - } - } catch (SQLException e) { - s_logger.warn("Unable to rollback to savepoint " + sp); - } - - if (!hasTxnInStack()) { - _txn = false; - closeConnection(); - } - } - - public void rollback() { - Iterator it = _stack.iterator(); - while (it.hasNext()) { - StackElement st = it.next(); - if (st.type == START_TXN) { - if (st.ref == null) { - it.remove(); - } else { - rollback((Savepoint)st.ref); - return; - } - } - } - - rollbackTransaction(); - } - - public Savepoint setSavepoint() throws SQLException { - _txn = true; - StackElement st = new StackElement(START_TXN, null); - _stack.push(st); - final Connection conn = getConnection(); - final Savepoint sp = conn.setSavepoint(); - st.ref = sp; - - return sp; - } - - public Savepoint setSavepoint(final String name) throws SQLException { - _txn = true; - StackElement st = new StackElement(START_TXN, null); - _stack.push(st); - final Connection conn = getConnection(); - final Savepoint sp = conn.setSavepoint(name); - st.ref = sp; - - return sp; - } - - public void releaseSavepoint(final Savepoint sp) throws SQLException { - removeTxn(sp); - if (_conn != null) { - _conn.releaseSavepoint(sp); - } - - if (!hasTxnInStack()) { - _txn = false; - closeConnection(); - } - } - - protected boolean hasSavepointInStack(Savepoint sp) { - Iterator it = _stack.iterator(); - while (it.hasNext()) { - StackElement se = it.next(); - if (se.type == START_TXN && se.ref == sp) { - return true; - } - } - return false; - } - - protected void removeTxn(Savepoint sp) { - assert hasSavepointInStack(sp) : "Removing a save point that's not in the stack"; - - if (!hasSavepointInStack(sp)) { - return; - } - - Iterator it = _stack.iterator(); - while (it.hasNext()) { - StackElement se = it.next(); - if (se.type == START_TXN) { - it.remove(); - if (se.ref == sp) { - return; - } - } - } - } - - public void rollback(final Savepoint sp) { - removeTxn(sp); - - rollbackSavepoint(sp); - } - - public Connection getCurrentConnection() { - return _conn; - } - - public List getStack() { - return _stack; - } - - protected Transaction() { - _name = null; - _conn = null; - _stack = null; - _txn = false; - _dbId = -1; - } - - @Override - protected void finalize() throws Throwable { - if (!(_conn == null && (_stack == null || _stack.size() == 0))) { - assert (false) : "Oh Alex oh alex...something is wrong with how we're doing this"; - s_logger.error("Something went wrong that a transaction is orphaned before db connection is closed"); - cleanup(); - } - } - - protected class StackElement { - public String type; - public Object ref; - - public StackElement (String type, Object ref) { - this.type = type; - this.ref = ref; - } - - @Override - public String toString() { - return type + "-" + ref; - } - } - - private static DataSource s_ds; - private static DataSource s_usageDS; - private static DataSource s_awsapiDS; - private static DataSource s_simulatorDS; - - static { - // Initialize with assumed db.properties file - initDataSource("db.properties"); - } - - public static void initDataSource(String propsFileName) { - try { - File dbPropsFile = PropertiesUtil.findConfigFile(propsFileName); - final Properties dbProps; - if (EncryptionSecretKeyChecker.useEncryption()) { - StandardPBEStringEncryptor encryptor = EncryptionSecretKeyChecker.getEncryptor(); - dbProps = new EncryptableProperties(encryptor); - } else { - dbProps = new Properties(); - } - try { - dbProps.load(new FileInputStream(dbPropsFile)); - } catch (IOException e) { - s_logger.fatal("Unable to load db properties file, pl. check the classpath and file path configuration", e); - return; - } catch (NullPointerException e) { - s_logger.fatal("Unable to locate db properties file within classpath or absolute path: " + propsFileName); - return; - } - - // FIXME: If params are missing...default them???? - final int cloudMaxActive = Integer.parseInt(dbProps.getProperty("db.cloud.maxActive")); - final int cloudMaxIdle = Integer.parseInt(dbProps.getProperty("db.cloud.maxIdle")); - final long cloudMaxWait = Long.parseLong(dbProps.getProperty("db.cloud.maxWait")); - final String cloudUsername = dbProps.getProperty("db.cloud.username"); - final String cloudPassword = dbProps.getProperty("db.cloud.password"); - final String cloudHost = dbProps.getProperty("db.cloud.host"); - final int cloudPort = Integer.parseInt(dbProps.getProperty("db.cloud.port")); - final String cloudDbName = dbProps.getProperty("db.cloud.name"); - final boolean cloudAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.cloud.autoReconnect")); - final String cloudValidationQuery = dbProps.getProperty("db.cloud.validationQuery"); - final String cloudIsolationLevel = dbProps.getProperty("db.cloud.isolation.level"); - - int isolationLevel = Connection.TRANSACTION_READ_COMMITTED; - if (cloudIsolationLevel == null) { - isolationLevel = Connection.TRANSACTION_READ_COMMITTED; - } else if (cloudIsolationLevel.equalsIgnoreCase("readcommitted")) { - isolationLevel = Connection.TRANSACTION_READ_COMMITTED; - } else if (cloudIsolationLevel.equalsIgnoreCase("repeatableread")) { - isolationLevel = Connection.TRANSACTION_REPEATABLE_READ; - } else if (cloudIsolationLevel.equalsIgnoreCase("serializable")) { - isolationLevel = Connection.TRANSACTION_SERIALIZABLE; - } else if (cloudIsolationLevel.equalsIgnoreCase("readuncommitted")) { - isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED; - } else { - s_logger.warn("Unknown isolation level " + cloudIsolationLevel + ". Using read uncommitted"); - } - - final boolean cloudTestOnBorrow = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testOnBorrow")); - final boolean cloudTestWhileIdle = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testWhileIdle")); - final long cloudTimeBtwEvictionRunsMillis = Long.parseLong(dbProps.getProperty("db.cloud.timeBetweenEvictionRunsMillis")); - final long cloudMinEvcitableIdleTimeMillis = Long.parseLong(dbProps.getProperty("db.cloud.minEvictableIdleTimeMillis")); - final boolean cloudPoolPreparedStatements = Boolean.parseBoolean(dbProps.getProperty("db.cloud.poolPreparedStatements")); - final String url = dbProps.getProperty("db.cloud.url.params"); - - final boolean useSSL = Boolean.parseBoolean(dbProps.getProperty("db.cloud.useSSL")); - if (useSSL) { - System.setProperty("javax.net.ssl.keyStore", dbProps.getProperty("db.cloud.keyStore")); - System.setProperty("javax.net.ssl.keyStorePassword", dbProps.getProperty("db.cloud.keyStorePassword")); - System.setProperty("javax.net.ssl.trustStore", dbProps.getProperty("db.cloud.trustStore")); - System.setProperty("javax.net.ssl.trustStorePassword", dbProps.getProperty("db.cloud.trustStorePassword")); - } - - final GenericObjectPool cloudConnectionPool = new GenericObjectPool(null, cloudMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, - cloudMaxWait, cloudMaxIdle, cloudTestOnBorrow, false, cloudTimeBtwEvictionRunsMillis, 1, cloudMinEvcitableIdleTimeMillis, cloudTestWhileIdle); - - final ConnectionFactory cloudConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + cloudHost + ":" + cloudPort + "/" + cloudDbName + - "?autoReconnect=" + cloudAutoReconnect + (url != null ? "&" + url : "") + (useSSL ? "&useSSL=true" : ""), cloudUsername, cloudPassword); - - final KeyedObjectPoolFactory poolableObjFactory = (cloudPoolPreparedStatements ? new StackKeyedObjectPoolFactory() : null); - - final PoolableConnectionFactory cloudPoolableConnectionFactory = new PoolableConnectionFactory(cloudConnectionFactory, cloudConnectionPool, poolableObjFactory, - cloudValidationQuery, false, false, isolationLevel); - - // Default Data Source for CloudStack - s_ds = new PoolingDataSource(cloudPoolableConnectionFactory.getPool()); - - // Configure the usage db - final int usageMaxActive = Integer.parseInt(dbProps.getProperty("db.usage.maxActive")); - final int usageMaxIdle = Integer.parseInt(dbProps.getProperty("db.usage.maxIdle")); - final long usageMaxWait = Long.parseLong(dbProps.getProperty("db.usage.maxWait")); - final String usageUsername = dbProps.getProperty("db.usage.username"); - final String usagePassword = dbProps.getProperty("db.usage.password"); - final String usageHost = dbProps.getProperty("db.usage.host"); - final int usagePort = Integer.parseInt(dbProps.getProperty("db.usage.port")); - final String usageDbName = dbProps.getProperty("db.usage.name"); - final boolean usageAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.usage.autoReconnect")); - final String usageUrl = dbProps.getProperty("db.usage.url.params"); - - final GenericObjectPool usageConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, - usageMaxWait, usageMaxIdle); - - final ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + usageHost + ":" + usagePort + "/" + usageDbName + - "?autoReconnect=" + usageAutoReconnect + (usageUrl != null ? "&" + usageUrl : ""), usageUsername, usagePassword); - - final PoolableConnectionFactory usagePoolableConnectionFactory = new PoolableConnectionFactory(usageConnectionFactory, usageConnectionPool, - new StackKeyedObjectPoolFactory(), null, false, false); - - // Data Source for usage server - s_usageDS = new PoolingDataSource(usagePoolableConnectionFactory.getPool()); - - // Configure awsapi db - final String awsapiDbName = dbProps.getProperty("db.awsapi.name"); - final GenericObjectPool awsapiConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, - usageMaxWait, usageMaxIdle); - final ConnectionFactory awsapiConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + cloudHost + ":" + cloudPort + "/" + awsapiDbName + - "?autoReconnect=" + usageAutoReconnect, cloudUsername, cloudPassword); - final PoolableConnectionFactory awsapiPoolableConnectionFactory = new PoolableConnectionFactory(awsapiConnectionFactory, awsapiConnectionPool, - new StackKeyedObjectPoolFactory(), null, false, false); - - // Data Source for awsapi - s_awsapiDS = new PoolingDataSource(awsapiPoolableConnectionFactory.getPool()); - - try { - // Configure the simulator db - final int simulatorMaxActive = Integer.parseInt(dbProps.getProperty("db.simulator.maxActive")); - final int simulatorMaxIdle = Integer.parseInt(dbProps.getProperty("db.simulator.maxIdle")); - final long simulatorMaxWait = Long.parseLong(dbProps.getProperty("db.simulator.maxWait")); - final String simulatorUsername = dbProps.getProperty("db.simulator.username"); - final String simulatorPassword = dbProps.getProperty("db.simulator.password"); - final String simulatorHost = dbProps.getProperty("db.simulator.host"); - final int simulatorPort = Integer.parseInt(dbProps.getProperty("db.simulator.port")); - final String simulatorDbName = dbProps.getProperty("db.simulator.name"); - final boolean simulatorAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.simulator.autoReconnect")); - - final GenericObjectPool simulatorConnectionPool = new GenericObjectPool(null, simulatorMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, - simulatorMaxWait, simulatorMaxIdle); - - final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + simulatorHost + ":" + simulatorPort + "/" + simulatorDbName + - "?autoReconnect=" + simulatorAutoReconnect, simulatorUsername, simulatorPassword); - - final PoolableConnectionFactory simulatorPoolableConnectionFactory = new PoolableConnectionFactory(simulatorConnectionFactory, simulatorConnectionPool, - new StackKeyedObjectPoolFactory(), null, false, false); - s_simulatorDS = new PoolingDataSource(simulatorPoolableConnectionFactory.getPool()); - } catch (Exception e) { - s_logger.debug("Simulator DB properties are not available. Not initializing simulator DS"); - } - } catch (final Exception e) { - s_ds = getDefaultDataSource("cloud"); - s_usageDS = getDefaultDataSource("cloud_usage"); - s_simulatorDS = getDefaultDataSource("cloud_simulator"); - s_logger.warn("Unable to load db configuration, using defaults with 5 connections. Falling back on assumed datasource on localhost:3306 using username:password=cloud:cloud. Please check your configuration", e); - } - } - - private static DataSource getDefaultDataSource(final String database) { - final GenericObjectPool connectionPool = new GenericObjectPool(null, 5); - final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( - "jdbc:mysql://localhost:3306/" + database, "cloud", "cloud"); - final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory( - connectionFactory, connectionPool, null, null, false, true); - return new PoolingDataSource( - /* connectionPool */poolableConnectionFactory.getPool()); - } - } diff --git a/framework/db/src/com/cloud/utils/db/TransactionCallback.java b/framework/db/src/com/cloud/utils/db/TransactionCallback.java new file mode 100644 index 00000000000..2f68a937bc8 --- /dev/null +++ b/framework/db/src/com/cloud/utils/db/TransactionCallback.java @@ -0,0 +1,7 @@ +package com.cloud.utils.db; + +public interface TransactionCallback { + + public T doInTransaction(TransactionStatus status); + +} diff --git a/framework/db/src/com/cloud/utils/db/TransactionCallbackNoReturn.java b/framework/db/src/com/cloud/utils/db/TransactionCallbackNoReturn.java new file mode 100644 index 00000000000..2db290a034b --- /dev/null +++ b/framework/db/src/com/cloud/utils/db/TransactionCallbackNoReturn.java @@ -0,0 +1,13 @@ +package com.cloud.utils.db; + +public abstract class TransactionCallbackNoReturn implements TransactionCallback { + + @Override + public final Object doInTransaction(TransactionStatus status) { + doInTransactionWithoutResult(status); + return null; + } + + public abstract void doInTransactionWithoutResult(TransactionStatus status); + +} diff --git a/framework/db/src/com/cloud/utils/db/TransactionCallbackWithException.java b/framework/db/src/com/cloud/utils/db/TransactionCallbackWithException.java new file mode 100644 index 00000000000..67d7826a17f --- /dev/null +++ b/framework/db/src/com/cloud/utils/db/TransactionCallbackWithException.java @@ -0,0 +1,7 @@ +package com.cloud.utils.db; + +public interface TransactionCallbackWithException { + + public T doInTransaction(TransactionStatus status) throws E; + +} diff --git a/framework/db/src/com/cloud/utils/db/TransactionCallbackWithExceptionNoReturn.java b/framework/db/src/com/cloud/utils/db/TransactionCallbackWithExceptionNoReturn.java new file mode 100644 index 00000000000..d5bc44fe7b3 --- /dev/null +++ b/framework/db/src/com/cloud/utils/db/TransactionCallbackWithExceptionNoReturn.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.cloud.utils.db; + +public abstract class TransactionCallbackWithExceptionNoReturn implements TransactionCallbackWithException { + + @Override + public final Boolean doInTransaction(TransactionStatus status) throws E { + doInTransactionWithoutResult(status); + return true; + } + + public abstract void doInTransactionWithoutResult(TransactionStatus status) throws E; + +} + diff --git a/framework/db/src/com/cloud/utils/db/TransactionContextBuilder.java b/framework/db/src/com/cloud/utils/db/TransactionContextBuilder.java index 40fcbbf5593..d60ab7b7f43 100644 --- a/framework/db/src/com/cloud/utils/db/TransactionContextBuilder.java +++ b/framework/db/src/com/cloud/utils/db/TransactionContextBuilder.java @@ -46,19 +46,19 @@ public class TransactionContextBuilder implements ComponentMethodInterceptor { @Override public Object interceptStart(Method method, Object target) { - return Transaction.open(method.getName()); + return TransactionLegacy.open(method.getName()); } @Override public void interceptComplete(Method method, Object target, Object objReturnedInInterceptStart) { - Transaction txn = (Transaction)objReturnedInInterceptStart; + TransactionLegacy txn = (TransactionLegacy)objReturnedInInterceptStart; if(txn != null) txn.close(); } @Override public void interceptException(Method method, Object target, Object objReturnedInInterceptStart) { - Transaction txn = (Transaction)objReturnedInInterceptStart; + TransactionLegacy txn = (TransactionLegacy)objReturnedInInterceptStart; if(txn != null) txn.close(); } diff --git a/framework/db/src/com/cloud/utils/db/TransactionLegacy.java b/framework/db/src/com/cloud/utils/db/TransactionLegacy.java new file mode 100755 index 00000000000..ffa06707df9 --- /dev/null +++ b/framework/db/src/com/cloud/utils/db/TransactionLegacy.java @@ -0,0 +1,1192 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.utils.db; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Savepoint; +import java.sql.Statement; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicLong; + +import javax.sql.DataSource; + +import org.apache.commons.dbcp.ConnectionFactory; +import org.apache.commons.dbcp.DriverManagerConnectionFactory; +import org.apache.commons.dbcp.PoolableConnectionFactory; +import org.apache.commons.dbcp.PoolingDataSource; +import org.apache.commons.pool.KeyedObjectPoolFactory; +import org.apache.commons.pool.impl.GenericObjectPool; +import org.apache.commons.pool.impl.StackKeyedObjectPoolFactory; +import org.apache.log4j.Logger; +import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; +import org.jasypt.properties.EncryptableProperties; + +import com.cloud.utils.Pair; +import com.cloud.utils.PropertiesUtil; +import com.cloud.utils.crypt.EncryptionSecretKeyChecker; +import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.mgmt.JmxUtil; + +/** + * Transaction abstracts away the Connection object in JDBC. It allows the + * following things that the Connection object does not. + * + * 1. Transaction can be started at an entry point and whether the DB + * actions should be auto-commit or not determined at that point. + * 2. DB Connection is allocated only when it is needed. + * 3. Code does not need to know if a transaction has been started or not. + * It just starts/ends a transaction and we resolve it correctly with + * the previous actions. + * + * Note that this class is not synchronous but it doesn't need to be because + * it is stored with TLS and is one per thread. Use appropriately. + */ +public class TransactionLegacy { + private static final Logger s_logger = Logger.getLogger(Transaction.class.getName() + "." + "Transaction"); + private static final Logger s_stmtLogger = Logger.getLogger(Transaction.class.getName() + "." + "Statement"); + private static final Logger s_lockLogger = Logger.getLogger(Transaction.class.getName() + "." + "Lock"); + private static final Logger s_connLogger = Logger.getLogger(Transaction.class.getName() + "." + "Connection"); + + private static final ThreadLocal tls = new ThreadLocal(); + private static final String START_TXN = "start_txn"; + private static final String CURRENT_TXN = "current_txn"; + private static final String CREATE_TXN = "create_txn"; + private static final String CREATE_CONN = "create_conn"; + private static final String STATEMENT = "statement"; + private static final String ATTACHMENT = "attachment"; + + public static final short CLOUD_DB = 0; + public static final short USAGE_DB = 1; + public static final short AWSAPI_DB = 2; + public static final short SIMULATOR_DB = 3; + + public static final short CONNECTED_DB = -1; + + private static AtomicLong s_id = new AtomicLong(); + private static final TransactionMBeanImpl s_mbean = new TransactionMBeanImpl(); + static { + try { + JmxUtil.registerMBean("Transaction", "Transaction", s_mbean); + } catch (Exception e) { + s_logger.error("Unable to register mbean for transaction", e); + } + + /* FIXME: We need a better solution for this + * Initialize encryption if we need it for db.properties + */ + EncryptionSecretKeyChecker enc = new EncryptionSecretKeyChecker(); + enc.check(); + } + + private final LinkedList _stack; + private long _id; + + private final LinkedList> _lockTimes = new LinkedList>(); + + private String _name; + private Connection _conn; + private boolean _txn; + private short _dbId; + private long _txnTime; + private Statement _stmt; + private String _creator; + + private TransactionLegacy _prev = null; + + public static TransactionLegacy currentTxn() { + return currentTxn(true); + } + + protected static TransactionLegacy currentTxn(boolean check) { + TransactionLegacy txn = tls.get(); + if (check) { + assert txn != null : "No Transaction on stack. Did you mark the method with @DB?"; + + assert checkAnnotation(4, txn) : "Did you even read the guide to use Transaction...IOW...other people's code? Try method can't be private. What about @DB? hmmm... could that be it? " + txn; + } + return txn; + } + + public static TransactionLegacy open(final short databaseId) { + String name = buildName(); + if (name == null) { + name = CURRENT_TXN; + } + return open(name, databaseId, true); + } + + // + // Usage of this transaction setup should be limited, it will always open a new transaction context regardless of whether or not there is other + // transaction context in the stack. It is used in special use cases that we want to control DB connection explicitly and in the mean time utilize + // the existing DAO features + // + public void transitToUserManagedConnection(Connection conn) { + assert(_conn == null /*&& _stack.size() <= 1*/) : "Can't change to a user managed connection unless the stack is empty and the db connection is null, you may have forgotten to invoke transitToAutoManagedConnection to close out the DB connection: " + toString(); + _conn = conn; + _dbId = CONNECTED_DB; + } + + public void transitToAutoManagedConnection(short dbId) { + // assert(_stack.size() <= 1) : "Can't change to auto managed connection unless your stack is empty"; + _dbId = dbId; + _conn = null; + } + + public static TransactionLegacy open(final String name) { + return open(name, TransactionLegacy.CLOUD_DB, false); + } + + public static TransactionLegacy open(final String name, final short databaseId, final boolean forceDbChange) { + TransactionLegacy txn = tls.get(); + boolean isNew = false; + if (txn == null) { + if (s_logger.isTraceEnabled()) { + s_logger.trace("Creating the transaction: " + name); + } + txn = new TransactionLegacy(name, false, databaseId); + tls.set(txn); + isNew = true; + } else if (forceDbChange) { + final short currentDbId = txn.getDatabaseId(); + if (currentDbId != databaseId) { + // we need to end the current transaction and switch databases + txn.close(txn.getName()); + + txn = new TransactionLegacy(name, false, databaseId); + tls.set(txn); + isNew = true; + } + } + + txn.takeOver(name, false); + if (isNew) { + s_mbean.addTransaction(txn); + } + return txn; + } + + protected StackElement peekInStack(Object obj) { + final Iterator it = _stack.iterator(); + while (it.hasNext()) { + StackElement next = it.next(); + if (next.type == obj) { + return next; + } + } + return null; + } + + public void registerLock(String sql) { + if (_txn && s_lockLogger.isDebugEnabled()) { + Pair time = new Pair(sql, System.currentTimeMillis()); + _lockTimes.add(time); + } + } + + public boolean dbTxnStarted() { + return _txn; + } + + public static Connection getStandaloneConnectionWithException() throws SQLException { + Connection conn = s_ds.getConnection(); + if (s_connLogger.isTraceEnabled()) { + s_connLogger.trace("Retrieving a standalone connection: dbconn" + System.identityHashCode(conn)); + } + return conn; + } + + public static Connection getStandaloneConnection() { + try { + return getStandaloneConnectionWithException(); + } catch (SQLException e) { + s_logger.error("Unexpected exception: ", e); + return null; + } + } + + public static Connection getStandaloneUsageConnection() { + try { + Connection conn = s_usageDS.getConnection(); + if (s_connLogger.isTraceEnabled()) { + s_connLogger.trace("Retrieving a standalone connection for usage: dbconn" + System.identityHashCode(conn)); + } + return conn; + } catch (SQLException e) { + s_logger.warn("Unexpected exception: ", e); + return null; + } + } + + public static Connection getStandaloneAwsapiConnection() { + try { + Connection conn = s_awsapiDS.getConnection(); + if (s_connLogger.isTraceEnabled()) { + s_connLogger.trace("Retrieving a standalone connection for usage: dbconn" + System.identityHashCode(conn)); + } + return conn; + } catch (SQLException e) { + s_logger.warn("Unexpected exception: ", e); + return null; + } + } + + public static Connection getStandaloneSimulatorConnection() { + try { + Connection conn = s_simulatorDS.getConnection(); + if (s_connLogger.isTraceEnabled()) { + s_connLogger.trace("Retrieving a standalone connection for simulator: dbconn" + System.identityHashCode(conn)); + } + return conn; + } catch (SQLException e) { + s_logger.warn("Unexpected exception: ", e); + return null; + } + } + + protected void attach(TransactionAttachment value) { + _stack.push(new StackElement(ATTACHMENT, value)); + } + + protected TransactionAttachment detach(String name) { + Iterator it = _stack.descendingIterator(); + while (it.hasNext()) { + StackElement element = it.next(); + if (element.type == ATTACHMENT) { + TransactionAttachment att = (TransactionAttachment)element.ref; + if (name.equals(att.getName())) { + it.remove(); + return att; + } + } + } + assert false : "Are you sure you attached this: " + name; + return null; + } + + public static void attachToTxn(TransactionAttachment value) { + TransactionLegacy txn = tls.get(); + assert txn != null && txn.peekInStack(CURRENT_TXN) != null: "Come on....how can we attach something to the transaction if you haven't started it?"; + + txn.attach(value); + } + + public static TransactionAttachment detachFromTxn(String name) { + TransactionLegacy txn = tls.get(); + assert txn != null : "No Transaction in TLS"; + return txn.detach(name); + } + + protected static boolean checkAnnotation(int stack, TransactionLegacy txn) { + final StackTraceElement[] stacks = Thread.currentThread().getStackTrace(); + StackElement se = txn.peekInStack(CURRENT_TXN); + if (se == null) { + return false; + } + + StringBuffer sb = new StringBuffer(); + for (; stack < stacks.length; stack++) { + String methodName = stacks[stack].getMethodName(); + sb.append(" ").append(methodName); + if (methodName.equals(se.ref)){ + return true; + } + } + + // relax stack structure for several places that @DB required injection is not in place + s_logger.warn("Non-standard stack context that Transaction context is manaully placed into the calling chain. Stack chain: " + sb); + return true; + } + + protected static String buildName() { + if (s_logger.isDebugEnabled()) { + final StackTraceElement[] stacks = Thread.currentThread().getStackTrace(); + final StringBuilder str = new StringBuilder(); + int i = 3, j = 3; + while (j < 15 && i < stacks.length) { + StackTraceElement element = stacks[i]; + String filename = element.getFileName(); + String method = element.getMethodName(); + if ((filename != null && filename.equals("")) || (method != null && method.equals("invokeSuper"))) { + i++; + continue; + } + + str.append("-").append(stacks[i].getClassName().substring(stacks[i].getClassName().lastIndexOf(".") + 1)).append(".").append(stacks[i].getMethodName()).append(":").append(stacks[i].getLineNumber()); + j++; + i++; + } + return str.toString(); + } + + return ""; + } + + public TransactionLegacy(final String name, final boolean forLocking, final short databaseId) { + _name = name; + _conn = null; + _stack = new LinkedList(); + _txn = false; + _dbId = databaseId; + _id = s_id.incrementAndGet(); + _creator = Thread.currentThread().getName(); + } + + public String getCreator() { + return _creator; + } + + public long getId() { + return _id; + } + + public String getName() { + return _name; + } + + public Short getDatabaseId() { + return _dbId; + } + + @Override + public String toString() { + final StringBuilder str = new StringBuilder((_name != null ? _name : "")); + str.append(" : "); + for (final StackElement se : _stack) { + if (se.type == CURRENT_TXN) { + str.append(se.ref).append(", "); + } + } + + return str.toString(); + } + + protected void mark(final String name) { + _stack.push(new StackElement(CURRENT_TXN, name)); + } + + public boolean lock(final String name, final int timeoutSeconds) { + Merovingian2 lockMaster = Merovingian2.getLockMaster(); + if (lockMaster == null) { + throw new CloudRuntimeException("There's no support for locking yet"); + } + return lockMaster.acquire(name, timeoutSeconds); + } + + public boolean release(final String name) { + Merovingian2 lockMaster = Merovingian2.getLockMaster(); + if (lockMaster == null) { + throw new CloudRuntimeException("There's no support for locking yet"); + } + return lockMaster.release(name); + } + + /** + * @deprecated Use {@link Transaction} for new code + */ + @Deprecated + public void start() { + if (s_logger.isTraceEnabled()) { + s_logger.trace("txn: start requested by: " + buildName()); + } + + _stack.push(new StackElement(START_TXN, null)); + + if (_txn) { + s_logger.trace("txn: has already been started."); + return; + } + + _txn = true; + + _txnTime = System.currentTimeMillis(); + if (_conn != null) { + try { + s_logger.trace("txn: set auto commit to false"); + _conn.setAutoCommit(false); + } catch (final SQLException e) { + s_logger.warn("Unable to set auto commit: ", e); + throw new CloudRuntimeException("Unable to set auto commit: ", e); + } + } + } + + protected void closePreviousStatement() { + if (_stmt != null) { + try { + if (s_stmtLogger.isTraceEnabled()) { + s_stmtLogger.trace("Closing: " + _stmt.toString()); + } + try { + ResultSet rs = _stmt.getResultSet(); + if (rs != null && _stmt.getResultSetHoldability() != ResultSet.HOLD_CURSORS_OVER_COMMIT) { + rs.close(); + } + } catch(SQLException e) { + s_stmtLogger.trace("Unable to close resultset"); + } + _stmt.close(); + } catch (final SQLException e) { + s_stmtLogger.trace("Unable to close statement: " + _stmt.toString()); + } finally { + _stmt = null; + } + } + } + + /** + * Prepares an auto close statement. The statement is closed automatically if it is + * retrieved with this method. + * + * @param sql sql String + * @return PreparedStatement + * @throws SQLException if problem with JDBC layer. + * + * @see java.sql.Connection + */ + public PreparedStatement prepareAutoCloseStatement(final String sql) throws SQLException { + PreparedStatement stmt = prepareStatement(sql); + closePreviousStatement(); + _stmt = stmt; + return stmt; + } + + public PreparedStatement prepareStatement(final String sql) throws SQLException { + final Connection conn = getConnection(); + final PreparedStatement pstmt = conn.prepareStatement(sql); + if (s_stmtLogger.isTraceEnabled()) { + s_stmtLogger.trace("Preparing: " + sql); + } + return pstmt; + } + + /** + * Prepares an auto close statement. The statement is closed automatically if it is + * retrieved with this method. + * + * @param sql sql String + * @param autoGeneratedKeys keys that are generated + * @return PreparedStatement + * @throws SQLException if problem with JDBC layer. + * + * @see java.sql.Connection + */ + public PreparedStatement prepareAutoCloseStatement(final String sql, final int autoGeneratedKeys) throws SQLException { + final Connection conn = getConnection(); + final PreparedStatement pstmt = conn.prepareStatement(sql, autoGeneratedKeys); + if (s_stmtLogger.isTraceEnabled()) { + s_stmtLogger.trace("Preparing: " + sql); + } + closePreviousStatement(); + _stmt = pstmt; + return pstmt; + } + + /** + * Prepares an auto close statement. The statement is closed automatically if it is + * retrieved with this method. + * + * @param sql sql String + * @param columnNames names of the columns + * @return PreparedStatement + * @throws SQLException if problem with JDBC layer. + * + * @see java.sql.Connection + */ + public PreparedStatement prepareAutoCloseStatement(final String sql, final String[] columnNames) throws SQLException { + final Connection conn = getConnection(); + final PreparedStatement pstmt = conn.prepareStatement(sql, columnNames); + if (s_stmtLogger.isTraceEnabled()) { + s_stmtLogger.trace("Preparing: " + sql); + } + closePreviousStatement(); + _stmt = pstmt; + return pstmt; + } + + /** + * Prepares an auto close statement. The statement is closed automatically if it is + * retrieved with this method. + * + * @param sql sql String + * @return PreparedStatement + * @throws SQLException if problem with JDBC layer. + * + * @see java.sql.Connection + */ + public PreparedStatement prepareAutoCloseStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { + final Connection conn = getConnection(); + final PreparedStatement pstmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability); + if (s_stmtLogger.isTraceEnabled()) { + s_stmtLogger.trace("Preparing: " + sql); + } + closePreviousStatement(); + _stmt = pstmt; + return pstmt; + } + + /** + * Returns the db connection. + * + * Note: that you can call getConnection() but beaware that + * all prepare statements from the Connection are not garbage + * collected! + * + * @return DB Connection but make sure you understand that + * you are responsible for closing the PreparedStatement. + * @throws SQLException + */ + public Connection getConnection() throws SQLException { + if (_conn == null) { + switch (_dbId) { + case CLOUD_DB: + if(s_ds != null) { + _conn = s_ds.getConnection(); + } else { + s_logger.warn("A static-initialized variable becomes null, process is dying?"); + throw new CloudRuntimeException("Database is not initialized, process is dying?"); + } + break; + case USAGE_DB: + if(s_usageDS != null) { + _conn = s_usageDS.getConnection(); + } else { + s_logger.warn("A static-initialized variable becomes null, process is dying?"); + throw new CloudRuntimeException("Database is not initialized, process is dying?"); + } + break; + case AWSAPI_DB: + if(s_awsapiDS != null) { + _conn = s_awsapiDS.getConnection(); + } else { + s_logger.warn("A static-initialized variable becomes null, process is dying?"); + throw new CloudRuntimeException("Database is not initialized, process is dying?"); + } + break; + + case SIMULATOR_DB: + if(s_simulatorDS != null) { + _conn = s_simulatorDS.getConnection(); + } else { + s_logger.warn("A static-initialized variable becomes null, process is dying?"); + throw new CloudRuntimeException("Database is not initialized, process is dying?"); + } + break; + default: + + throw new CloudRuntimeException("No database selected for the transaction"); + } + _conn.setAutoCommit(!_txn); + + // + // MySQL default transaction isolation level is REPEATABLE READ, + // to reduce chances of DB deadlock, we will use READ COMMITED isolation level instead + // see http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html + // + _stack.push(new StackElement(CREATE_CONN, null)); + if (s_connLogger.isTraceEnabled()) { + s_connLogger.trace("Creating a DB connection with " + (_txn ? " txn: " : " no txn: ") + " for " + _dbId + ": dbconn" + System.identityHashCode(_conn) + ". Stack: " + buildName()); + } + } else { + s_logger.trace("conn: Using existing DB connection"); + } + + return _conn; + } + + protected boolean takeOver(final String name, final boolean create) { + if (_stack.size() != 0) { + if (!create) { + // If it is not a create transaction, then let's just use the current one. + if (s_logger.isTraceEnabled()) { + s_logger.trace("Using current transaction: " + toString()); + } + mark(name); + return false; + } + + final StackElement se = _stack.getFirst(); + if (se.type == CREATE_TXN) { + // This create is called inside of another create. Which is ok? + // We will let that create be responsible for cleaning up. + if (s_logger.isTraceEnabled()) { + s_logger.trace("Create using current transaction: " + toString()); + } + mark(name); + return false; + } + + s_logger.warn("Encountered a transaction that has leaked. Cleaning up. " + toString()); + cleanup(); + } + + if (s_logger.isTraceEnabled()) { + s_logger.trace("Took over the transaction: " + name); + } + _stack.push(new StackElement(create ? CREATE_TXN : CURRENT_TXN, name)); + _name = name; + return true; + } + + public void cleanup() { + closePreviousStatement(); + + removeUpTo(null, null); + if (_txn) { + rollbackTransaction(); + } + _txn = false; + _name = null; + + closeConnection(); + + _stack.clear(); + Merovingian2 lockMaster = Merovingian2.getLockMaster(); + if (lockMaster != null) { + lockMaster.cleanupThread(); + } + } + + public void close() { + removeUpTo(CURRENT_TXN, null); + + if (_stack.size() == 0) { + s_logger.trace("Transaction is done"); + cleanup(); + } + } + + /** + * close() is used by endTxn to close the connection. This method only + * closes the connection if the name is the same as what's stored. + * + * @param name + * @return true if this close actually closes the connection. false if not. + */ + public boolean close(final String name) { + if (_name == null) { // Already cleaned up. + if (s_logger.isTraceEnabled()) { + s_logger.trace("Already cleaned up." + buildName()); + } + return true; + } + + if (!_name.equals(name)) { + close(); + return false; + } + + if (s_logger.isDebugEnabled() && _stack.size() > 2) { + s_logger.debug("Transaction is not closed properly: " + toString() + ". Called by " + buildName()); + } + + cleanup(); + + s_logger.trace("All done"); + return true; + } + + protected boolean hasTxnInStack() { + return peekInStack(START_TXN) != null; + } + + protected void clearLockTimes() { + if (s_lockLogger.isDebugEnabled()) { + for (Pair time : _lockTimes) { + s_lockLogger.trace("SQL " + time.first() + " took " + (System.currentTimeMillis() - time.second())); + } + _lockTimes.clear(); + } + } + + public boolean commit() { + if (!_txn) { + s_logger.warn("txn: Commit called when it is not a transaction: " + buildName()); + return false; + } + + Iterator it = _stack.iterator(); + while (it.hasNext()) { + StackElement st = it.next(); + if (st.type == START_TXN) { + it.remove(); + break; + } + } + + if (hasTxnInStack()) { + if (s_logger.isTraceEnabled()) { + s_logger.trace("txn: Not committing because transaction started elsewhere: " + buildName() + " / " + toString()); + } + return false; + } + + _txn = false; + try { + if (_conn != null) { + _conn.commit(); + s_logger.trace("txn: DB Changes committed. Time = " + (System.currentTimeMillis() - _txnTime)); + clearLockTimes(); + closeConnection(); + } + return true; + } catch (final SQLException e) { + rollbackTransaction(); + throw new CloudRuntimeException("Unable to commit or close the connection. ", e); + } + } + + protected void closeConnection() { + closePreviousStatement(); + + if (_conn == null) { + return; + } + + if (_txn) { + s_connLogger.trace("txn: Not closing DB connection because we're still in a transaction."); + return; + } + + try { + // we should only close db connection when it is not user managed + if (this._dbId != CONNECTED_DB) { + if (s_connLogger.isTraceEnabled()) { + s_connLogger.trace("Closing DB connection: dbconn" + System.identityHashCode(_conn)); + } + _conn.close(); + _conn = null; + } + + } catch (final SQLException e) { + s_logger.warn("Unable to close connection", e); + } + } + + protected void removeUpTo(String type, Object ref) { + boolean rollback = false; + Iterator it = _stack.iterator(); + while (it.hasNext()) { + StackElement item = it.next(); + + it.remove(); + + try { + if (item.type == type && (ref == null || item.ref == ref)) { + break; + } + + if (item.type == CURRENT_TXN) { + if (s_logger.isTraceEnabled()) { + s_logger.trace("Releasing the current txn: " + (item.ref != null ? item.ref : "")); + } + } else if (item.type == CREATE_CONN) { + closeConnection(); + } else if (item.type == START_TXN) { + if (item.ref == null) { + rollback = true; + } else { + try { + _conn.rollback((Savepoint)ref); + rollback = false; + } catch (final SQLException e) { + s_logger.warn("Unable to rollback Txn.", e); + } + } + } else if (item.type == STATEMENT) { + try { + if (s_stmtLogger.isTraceEnabled()) { + s_stmtLogger.trace("Closing: " + ref.toString()); + } + Statement stmt = (Statement)ref; + try { + ResultSet rs = stmt.getResultSet(); + if (rs != null) { + rs.close(); + } + } catch(SQLException e) { + s_stmtLogger.trace("Unable to close resultset"); + } + stmt.close(); + } catch (final SQLException e) { + s_stmtLogger.trace("Unable to close statement: " + item); + } + } else if (item.type == ATTACHMENT) { + TransactionAttachment att = (TransactionAttachment)item.ref; + if (s_logger.isTraceEnabled()) { + s_logger.trace("Cleaning up " + att.getName()); + } + att.cleanup(); + } + } catch(Exception e) { + s_logger.error("Unable to clean up " + item, e); + } + } + + if (rollback) { + rollback(); + } + } + + protected void rollbackTransaction() { + closePreviousStatement(); + if (!_txn) { + if (s_logger.isTraceEnabled()) { + s_logger.trace("Rollback called for " + _name + " when there's no transaction: " + buildName()); + } + return; + } + assert (!hasTxnInStack()) : "Who's rolling back transaction when there's still txn in stack?"; + _txn = false; + try { + if (_conn != null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Rolling back the transaction: Time = " + (System.currentTimeMillis() - _txnTime) + " Name = " + _name + "; called by " + buildName()); + } + _conn.rollback(); + } + clearLockTimes(); + closeConnection(); + } catch(final SQLException e) { + s_logger.warn("Unable to rollback", e); + } + } + + protected void rollbackSavepoint(Savepoint sp) { + try { + if (_conn != null) { + _conn.rollback(sp); + } + } catch (SQLException e) { + s_logger.warn("Unable to rollback to savepoint " + sp); + } + + if (!hasTxnInStack()) { + _txn = false; + closeConnection(); + } + } + + public void rollback() { + Iterator it = _stack.iterator(); + while (it.hasNext()) { + StackElement st = it.next(); + if (st.type == START_TXN) { + if (st.ref == null) { + it.remove(); + } else { + rollback((Savepoint)st.ref); + return; + } + } + } + + rollbackTransaction(); + } + + public Savepoint setSavepoint() throws SQLException { + _txn = true; + StackElement st = new StackElement(START_TXN, null); + _stack.push(st); + final Connection conn = getConnection(); + final Savepoint sp = conn.setSavepoint(); + st.ref = sp; + + return sp; + } + + public Savepoint setSavepoint(final String name) throws SQLException { + _txn = true; + StackElement st = new StackElement(START_TXN, null); + _stack.push(st); + final Connection conn = getConnection(); + final Savepoint sp = conn.setSavepoint(name); + st.ref = sp; + + return sp; + } + + public void releaseSavepoint(final Savepoint sp) throws SQLException { + removeTxn(sp); + if (_conn != null) { + _conn.releaseSavepoint(sp); + } + + if (!hasTxnInStack()) { + _txn = false; + closeConnection(); + } + } + + protected boolean hasSavepointInStack(Savepoint sp) { + Iterator it = _stack.iterator(); + while (it.hasNext()) { + StackElement se = it.next(); + if (se.type == START_TXN && se.ref == sp) { + return true; + } + } + return false; + } + + protected void removeTxn(Savepoint sp) { + assert hasSavepointInStack(sp) : "Removing a save point that's not in the stack"; + + if (!hasSavepointInStack(sp)) { + return; + } + + Iterator it = _stack.iterator(); + while (it.hasNext()) { + StackElement se = it.next(); + if (se.type == START_TXN) { + it.remove(); + if (se.ref == sp) { + return; + } + } + } + } + + public void rollback(final Savepoint sp) { + removeTxn(sp); + + rollbackSavepoint(sp); + } + + public Connection getCurrentConnection() { + return _conn; + } + + public List getStack() { + return _stack; + } + + protected TransactionLegacy() { + _name = null; + _conn = null; + _stack = null; + _txn = false; + _dbId = -1; + } + + @Override + protected void finalize() throws Throwable { + if (!(_conn == null && (_stack == null || _stack.size() == 0))) { + assert (false) : "Oh Alex oh alex...something is wrong with how we're doing this"; + s_logger.error("Something went wrong that a transaction is orphaned before db connection is closed"); + cleanup(); + } + } + + protected class StackElement { + public String type; + public Object ref; + + public StackElement (String type, Object ref) { + this.type = type; + this.ref = ref; + } + + @Override + public String toString() { + return type + "-" + ref; + } + } + + private static DataSource s_ds; + private static DataSource s_usageDS; + private static DataSource s_awsapiDS; + private static DataSource s_simulatorDS; + + static { + // Initialize with assumed db.properties file + initDataSource("db.properties"); + } + + public static void initDataSource(String propsFileName) { + try { + File dbPropsFile = PropertiesUtil.findConfigFile(propsFileName); + final Properties dbProps; + if (EncryptionSecretKeyChecker.useEncryption()) { + StandardPBEStringEncryptor encryptor = EncryptionSecretKeyChecker.getEncryptor(); + dbProps = new EncryptableProperties(encryptor); + } else { + dbProps = new Properties(); + } + try { + dbProps.load(new FileInputStream(dbPropsFile)); + } catch (IOException e) { + s_logger.fatal("Unable to load db properties file, pl. check the classpath and file path configuration", e); + return; + } catch (NullPointerException e) { + s_logger.fatal("Unable to locate db properties file within classpath or absolute path: " + propsFileName); + return; + } + + // FIXME: If params are missing...default them???? + final int cloudMaxActive = Integer.parseInt(dbProps.getProperty("db.cloud.maxActive")); + final int cloudMaxIdle = Integer.parseInt(dbProps.getProperty("db.cloud.maxIdle")); + final long cloudMaxWait = Long.parseLong(dbProps.getProperty("db.cloud.maxWait")); + final String cloudUsername = dbProps.getProperty("db.cloud.username"); + final String cloudPassword = dbProps.getProperty("db.cloud.password"); + final String cloudHost = dbProps.getProperty("db.cloud.host"); + final int cloudPort = Integer.parseInt(dbProps.getProperty("db.cloud.port")); + final String cloudDbName = dbProps.getProperty("db.cloud.name"); + final boolean cloudAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.cloud.autoReconnect")); + final String cloudValidationQuery = dbProps.getProperty("db.cloud.validationQuery"); + final String cloudIsolationLevel = dbProps.getProperty("db.cloud.isolation.level"); + + int isolationLevel = Connection.TRANSACTION_READ_COMMITTED; + if (cloudIsolationLevel == null) { + isolationLevel = Connection.TRANSACTION_READ_COMMITTED; + } else if (cloudIsolationLevel.equalsIgnoreCase("readcommitted")) { + isolationLevel = Connection.TRANSACTION_READ_COMMITTED; + } else if (cloudIsolationLevel.equalsIgnoreCase("repeatableread")) { + isolationLevel = Connection.TRANSACTION_REPEATABLE_READ; + } else if (cloudIsolationLevel.equalsIgnoreCase("serializable")) { + isolationLevel = Connection.TRANSACTION_SERIALIZABLE; + } else if (cloudIsolationLevel.equalsIgnoreCase("readuncommitted")) { + isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED; + } else { + s_logger.warn("Unknown isolation level " + cloudIsolationLevel + ". Using read uncommitted"); + } + + final boolean cloudTestOnBorrow = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testOnBorrow")); + final boolean cloudTestWhileIdle = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testWhileIdle")); + final long cloudTimeBtwEvictionRunsMillis = Long.parseLong(dbProps.getProperty("db.cloud.timeBetweenEvictionRunsMillis")); + final long cloudMinEvcitableIdleTimeMillis = Long.parseLong(dbProps.getProperty("db.cloud.minEvictableIdleTimeMillis")); + final boolean cloudPoolPreparedStatements = Boolean.parseBoolean(dbProps.getProperty("db.cloud.poolPreparedStatements")); + final String url = dbProps.getProperty("db.cloud.url.params"); + + final boolean useSSL = Boolean.parseBoolean(dbProps.getProperty("db.cloud.useSSL")); + if (useSSL) { + System.setProperty("javax.net.ssl.keyStore", dbProps.getProperty("db.cloud.keyStore")); + System.setProperty("javax.net.ssl.keyStorePassword", dbProps.getProperty("db.cloud.keyStorePassword")); + System.setProperty("javax.net.ssl.trustStore", dbProps.getProperty("db.cloud.trustStore")); + System.setProperty("javax.net.ssl.trustStorePassword", dbProps.getProperty("db.cloud.trustStorePassword")); + } + + final GenericObjectPool cloudConnectionPool = new GenericObjectPool(null, cloudMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, + cloudMaxWait, cloudMaxIdle, cloudTestOnBorrow, false, cloudTimeBtwEvictionRunsMillis, 1, cloudMinEvcitableIdleTimeMillis, cloudTestWhileIdle); + + final ConnectionFactory cloudConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + cloudHost + ":" + cloudPort + "/" + cloudDbName + + "?autoReconnect=" + cloudAutoReconnect + (url != null ? "&" + url : "") + (useSSL ? "&useSSL=true" : ""), cloudUsername, cloudPassword); + + final KeyedObjectPoolFactory poolableObjFactory = (cloudPoolPreparedStatements ? new StackKeyedObjectPoolFactory() : null); + + final PoolableConnectionFactory cloudPoolableConnectionFactory = new PoolableConnectionFactory(cloudConnectionFactory, cloudConnectionPool, poolableObjFactory, + cloudValidationQuery, false, false, isolationLevel); + + // Default Data Source for CloudStack + s_ds = new PoolingDataSource(cloudPoolableConnectionFactory.getPool()); + + // Configure the usage db + final int usageMaxActive = Integer.parseInt(dbProps.getProperty("db.usage.maxActive")); + final int usageMaxIdle = Integer.parseInt(dbProps.getProperty("db.usage.maxIdle")); + final long usageMaxWait = Long.parseLong(dbProps.getProperty("db.usage.maxWait")); + final String usageUsername = dbProps.getProperty("db.usage.username"); + final String usagePassword = dbProps.getProperty("db.usage.password"); + final String usageHost = dbProps.getProperty("db.usage.host"); + final int usagePort = Integer.parseInt(dbProps.getProperty("db.usage.port")); + final String usageDbName = dbProps.getProperty("db.usage.name"); + final boolean usageAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.usage.autoReconnect")); + final String usageUrl = dbProps.getProperty("db.usage.url.params"); + + final GenericObjectPool usageConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, + usageMaxWait, usageMaxIdle); + + final ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + usageHost + ":" + usagePort + "/" + usageDbName + + "?autoReconnect=" + usageAutoReconnect + (usageUrl != null ? "&" + usageUrl : ""), usageUsername, usagePassword); + + final PoolableConnectionFactory usagePoolableConnectionFactory = new PoolableConnectionFactory(usageConnectionFactory, usageConnectionPool, + new StackKeyedObjectPoolFactory(), null, false, false); + + // Data Source for usage server + s_usageDS = new PoolingDataSource(usagePoolableConnectionFactory.getPool()); + + // Configure awsapi db + final String awsapiDbName = dbProps.getProperty("db.awsapi.name"); + final GenericObjectPool awsapiConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, + usageMaxWait, usageMaxIdle); + final ConnectionFactory awsapiConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + cloudHost + ":" + cloudPort + "/" + awsapiDbName + + "?autoReconnect=" + usageAutoReconnect, cloudUsername, cloudPassword); + final PoolableConnectionFactory awsapiPoolableConnectionFactory = new PoolableConnectionFactory(awsapiConnectionFactory, awsapiConnectionPool, + new StackKeyedObjectPoolFactory(), null, false, false); + + // Data Source for awsapi + s_awsapiDS = new PoolingDataSource(awsapiPoolableConnectionFactory.getPool()); + + try { + // Configure the simulator db + final int simulatorMaxActive = Integer.parseInt(dbProps.getProperty("db.simulator.maxActive")); + final int simulatorMaxIdle = Integer.parseInt(dbProps.getProperty("db.simulator.maxIdle")); + final long simulatorMaxWait = Long.parseLong(dbProps.getProperty("db.simulator.maxWait")); + final String simulatorUsername = dbProps.getProperty("db.simulator.username"); + final String simulatorPassword = dbProps.getProperty("db.simulator.password"); + final String simulatorHost = dbProps.getProperty("db.simulator.host"); + final int simulatorPort = Integer.parseInt(dbProps.getProperty("db.simulator.port")); + final String simulatorDbName = dbProps.getProperty("db.simulator.name"); + final boolean simulatorAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.simulator.autoReconnect")); + + final GenericObjectPool simulatorConnectionPool = new GenericObjectPool(null, simulatorMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, + simulatorMaxWait, simulatorMaxIdle); + + final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + simulatorHost + ":" + simulatorPort + "/" + simulatorDbName + + "?autoReconnect=" + simulatorAutoReconnect, simulatorUsername, simulatorPassword); + + final PoolableConnectionFactory simulatorPoolableConnectionFactory = new PoolableConnectionFactory(simulatorConnectionFactory, simulatorConnectionPool, + new StackKeyedObjectPoolFactory(), null, false, false); + s_simulatorDS = new PoolingDataSource(simulatorPoolableConnectionFactory.getPool()); + } catch (Exception e) { + s_logger.debug("Simulator DB properties are not available. Not initializing simulator DS"); + } + } catch (final Exception e) { + s_ds = getDefaultDataSource("cloud"); + s_usageDS = getDefaultDataSource("cloud_usage"); + s_simulatorDS = getDefaultDataSource("cloud_simulator"); + s_logger.warn("Unable to load db configuration, using defaults with 5 connections. Falling back on assumed datasource on localhost:3306 using username:password=cloud:cloud. Please check your configuration", e); + } + } + + private static DataSource getDefaultDataSource(final String database) { + final GenericObjectPool connectionPool = new GenericObjectPool(null, 5); + final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( + "jdbc:mysql://localhost:3306/" + database, "cloud", "cloud"); + final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory( + connectionFactory, connectionPool, null, null, false, true); + return new PoolingDataSource( + /* connectionPool */poolableConnectionFactory.getPool()); + } + + /** + * Used for unit testing primarily + * + * @param conn + */ + protected void setConnection(Connection conn) { + this._conn = conn; + } +} diff --git a/framework/db/src/com/cloud/utils/db/TransactionMBeanImpl.java b/framework/db/src/com/cloud/utils/db/TransactionMBeanImpl.java index d51a9bd0cc7..73511b17ce4 100644 --- a/framework/db/src/com/cloud/utils/db/TransactionMBeanImpl.java +++ b/framework/db/src/com/cloud/utils/db/TransactionMBeanImpl.java @@ -25,21 +25,21 @@ import java.util.concurrent.ConcurrentHashMap; import javax.management.StandardMBean; -import com.cloud.utils.db.Transaction.StackElement; +import com.cloud.utils.db.TransactionLegacy.StackElement; public class TransactionMBeanImpl extends StandardMBean implements TransactionMBean { - Map _txns = new ConcurrentHashMap(); + Map _txns = new ConcurrentHashMap(); public TransactionMBeanImpl() { super(TransactionMBean.class, false); } - public void addTransaction(Transaction txn) { + public void addTransaction(TransactionLegacy txn) { _txns.put(txn.getId(), txn); } - public void removeTransaction(Transaction txn) { + public void removeTransaction(TransactionLegacy txn) { _txns.remove(txn.getId()); } @@ -53,7 +53,7 @@ public class TransactionMBeanImpl extends StandardMBean implements TransactionMB int[] count = new int[2]; count[0] = 0; count[1] = 0; - for (Transaction txn : _txns.values()) { + for (TransactionLegacy txn : _txns.values()) { if (txn.getStack().size() > 0) { count[0]++; } @@ -67,7 +67,7 @@ public class TransactionMBeanImpl extends StandardMBean implements TransactionMB @Override public List> getTransactions() { ArrayList> txns = new ArrayList>(); - for (Transaction info : _txns.values()) { + for (TransactionLegacy info : _txns.values()) { txns.add(toMap(info)); } return txns; @@ -76,7 +76,7 @@ public class TransactionMBeanImpl extends StandardMBean implements TransactionMB @Override public List> getActiveTransactions() { ArrayList> txns = new ArrayList>(); - for (Transaction txn : _txns.values()) { + for (TransactionLegacy txn : _txns.values()) { if (txn.getStack().size() > 0 || txn.getCurrentConnection() != null) { txns.add(toMap(txn)); } @@ -84,7 +84,7 @@ public class TransactionMBeanImpl extends StandardMBean implements TransactionMB return txns; } - protected Map toMap(Transaction txn) { + protected Map toMap(TransactionLegacy txn) { Map map = new HashMap(); map.put("name", txn.getName()); map.put("id", Long.toString(txn.getId())); @@ -103,7 +103,7 @@ public class TransactionMBeanImpl extends StandardMBean implements TransactionMB @Override public List> getTransactionsWithDatabaseConnection() { ArrayList> txns = new ArrayList>(); - for (Transaction txn : _txns.values()) { + for (TransactionLegacy txn : _txns.values()) { if (txn.getCurrentConnection() != null) { txns.add(toMap(txn)); } diff --git a/framework/db/src/com/cloud/utils/db/TransactionStatus.java b/framework/db/src/com/cloud/utils/db/TransactionStatus.java new file mode 100644 index 00000000000..a167797ad50 --- /dev/null +++ b/framework/db/src/com/cloud/utils/db/TransactionStatus.java @@ -0,0 +1,7 @@ +package com.cloud.utils.db; + +/** + * Placeholder for possible future features + */ +public interface TransactionStatus { +} diff --git a/framework/db/test/com/cloud/utils/db/DbTestDao.java b/framework/db/test/com/cloud/utils/db/DbTestDao.java index 9530b3b2d44..7db5ba867db 100644 --- a/framework/db/test/com/cloud/utils/db/DbTestDao.java +++ b/framework/db/test/com/cloud/utils/db/DbTestDao.java @@ -29,7 +29,7 @@ public class DbTestDao extends GenericDaoBase implements Generic @DB public void create(int fldInt, long fldLong, String fldString) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { txn.start(); @@ -48,7 +48,7 @@ public class DbTestDao extends GenericDaoBase implements Generic @DB public void update(int fldInt, long fldLong, String fldString) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { txn.start(); diff --git a/framework/db/test/com/cloud/utils/db/DbTestUtils.java b/framework/db/test/com/cloud/utils/db/DbTestUtils.java index 11ae1aa4d0b..2458b8c7e26 100644 --- a/framework/db/test/com/cloud/utils/db/DbTestUtils.java +++ b/framework/db/test/com/cloud/utils/db/DbTestUtils.java @@ -33,7 +33,7 @@ public class DbTestUtils { throw new RuntimeException("Unable to clean the database because I can't find " + file); } - Connection conn = Transaction.getStandaloneConnection(); + Connection conn = TransactionLegacy.getStandaloneConnection(); ScriptRunner runner = new ScriptRunner(conn, autoCommit, stopOnError); FileReader reader; @@ -63,7 +63,7 @@ public class DbTestUtils { throw new RuntimeException("Unable to clean the database because I can't find " + file); } - Connection conn = Transaction.getStandaloneUsageConnection(); + Connection conn = TransactionLegacy.getStandaloneUsageConnection(); ScriptRunner runner = new ScriptRunner(conn, autoCommit, stopOnError); FileReader reader; diff --git a/framework/db/test/com/cloud/utils/db/TestTransaction.java b/framework/db/test/com/cloud/utils/db/TestTransaction.java new file mode 100644 index 00000000000..4ee08f1f66f --- /dev/null +++ b/framework/db/test/com/cloud/utils/db/TestTransaction.java @@ -0,0 +1,154 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.cloud.utils.db; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.io.FileNotFoundException; +import java.sql.Connection; +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +public class TestTransaction { + + TransactionLegacy txn; + Connection conn; + + @Before + public void setup() { + setup(TransactionLegacy.CLOUD_DB); + } + + public void setup(short db) { + txn = TransactionLegacy.open(db); + conn = Mockito.mock(Connection.class); + txn.setConnection(conn); + } + + @After + public void after() { + TransactionLegacy.currentTxn().close(); + } + + @Test + public void testCommit() throws Exception { + assertEquals(42L, Transaction.execute(new TransactionCallback() { + @Override + public Object doInTransaction(TransactionStatus status) { + return 42L; + } + })); + + verify(conn).setAutoCommit(false); + verify(conn, times(1)).commit(); + verify(conn, times(0)).rollback(); + verify(conn, times(1)).close(); + } + + @Test + public void testRollback() throws Exception { + try { + Transaction.execute(new TransactionCallback() { + @Override + public Object doInTransaction(TransactionStatus status) { + throw new RuntimeException("Panic!"); + } + }); + fail(); + } catch (RuntimeException e) { + assertEquals("Panic!", e.getMessage()); + } + + verify(conn).setAutoCommit(false); + verify(conn, times(0)).commit(); + verify(conn, times(1)).rollback(); + verify(conn, times(1)).close(); + } + + @Test + public void testRollbackWithException() throws Exception { + try { + Transaction.execute(new TransactionCallbackWithException() { + @Override + public Object doInTransaction(TransactionStatus status) throws FileNotFoundException { + assertEquals(TransactionLegacy.CLOUD_DB, TransactionLegacy.currentTxn().getDatabaseId().shortValue()); + + throw new FileNotFoundException("Panic!"); + } + }); + fail(); + } catch (FileNotFoundException e) { + assertEquals("Panic!", e.getMessage()); + } + + verify(conn).setAutoCommit(false); + verify(conn, times(0)).commit(); + verify(conn, times(1)).rollback(); + verify(conn, times(1)).close(); + } + + @Test + public void testWithExceptionNoReturn() throws Exception { + final AtomicInteger i = new AtomicInteger(0); + assertTrue(Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws FileNotFoundException { + i.incrementAndGet(); + } + })); + + assertEquals(1, i.get()); + verify(conn).setAutoCommit(false); + verify(conn, times(1)).commit(); + verify(conn, times(0)).rollback(); + verify(conn, times(1)).close(); + } + + @Test + public void testOtherdatabaseRollback() throws Exception { + after(); + setup(TransactionLegacy.AWSAPI_DB); + + try { + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + assertEquals(TransactionLegacy.AWSAPI_DB, TransactionLegacy.currentTxn().getDatabaseId().shortValue()); + + throw new RuntimeException("Panic!"); + } + }); + fail(); + } catch (RuntimeException e) { + assertEquals("Panic!", e.getMessage()); + } + + + verify(conn).setAutoCommit(false); + verify(conn, times(0)).commit(); + verify(conn, times(1)).rollback(); + verify(conn, times(1)).close(); + } + +} diff --git a/framework/db/test/com/cloud/utils/db/TransactionTest.java b/framework/db/test/com/cloud/utils/db/TransactionTest.java index 101a533f836..92b2f36a2fc 100644 --- a/framework/db/test/com/cloud/utils/db/TransactionTest.java +++ b/framework/db/test/com/cloud/utils/db/TransactionTest.java @@ -41,7 +41,7 @@ public class TransactionTest { Connection conn = null; PreparedStatement pstmt = null; try { - conn = Transaction.getStandaloneConnection(); + conn = TransactionLegacy.getStandaloneConnection(); pstmt = conn.prepareStatement("CREATE TABLE `cloud`.`test` (" + "`id` bigint unsigned NOT NULL UNIQUE AUTO_INCREMENT," + "`fld_int` int unsigned," @@ -75,27 +75,27 @@ public class TransactionTest { */ public void testUserManagedConnection() { DbTestDao testDao = ComponentContext.inject(DbTestDao.class); - Transaction txn = Transaction.open("SingleConnectionThread"); + TransactionLegacy txn = TransactionLegacy.open("SingleConnectionThread"); Connection conn = null; try { - conn = Transaction.getStandaloneConnectionWithException(); + conn = TransactionLegacy.getStandaloneConnectionWithException(); txn.transitToUserManagedConnection(conn); // try two SQLs to make sure that they are using the same connection // acquired above. testDao.create(1, 1, "Record 1"); - Connection checkConn = Transaction.currentTxn().getConnection(); + Connection checkConn = TransactionLegacy.currentTxn().getConnection(); if (checkConn != conn) { Assert.fail("A new db connection is acquired instead of using old one after create sql"); } testDao.update(2, 2, "Record 1"); - Connection checkConn2 = Transaction.currentTxn().getConnection(); + Connection checkConn2 = TransactionLegacy.currentTxn().getConnection(); if (checkConn2 != conn) { Assert.fail("A new db connection is acquired instead of using old one after update sql"); } } catch (SQLException e) { Assert.fail(e.getMessage()); } finally { - txn.transitToAutoManagedConnection(Transaction.CLOUD_DB); + txn.transitToAutoManagedConnection(TransactionLegacy.CLOUD_DB); txn.close(); if (conn != null) { @@ -117,28 +117,28 @@ public class TransactionTest { // acquire a db connection and keep it Connection conn = null; try { - conn = Transaction.getStandaloneConnectionWithException(); + conn = TransactionLegacy.getStandaloneConnectionWithException(); } catch (SQLException ex) { throw new CloudRuntimeException("Problem with getting db connection", ex); } // start heartbeat loop, make sure that each loop still use the same // connection - Transaction txn = null; + TransactionLegacy txn = null; for (int i = 0; i < 3; i++) { - txn = Transaction.open("HeartbeatSimulator"); + txn = TransactionLegacy.open("HeartbeatSimulator"); try { txn.transitToUserManagedConnection(conn); testDao.create(i, i, "Record " + i); - Connection checkConn = Transaction.currentTxn().getConnection(); + Connection checkConn = TransactionLegacy.currentTxn().getConnection(); if (checkConn != conn) { Assert.fail("A new db connection is acquired instead of using old one in loop " + i); } } catch (SQLException e) { Assert.fail(e.getMessage()); } finally { - txn.transitToAutoManagedConnection(Transaction.CLOUD_DB); + txn.transitToAutoManagedConnection(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -161,7 +161,7 @@ public class TransactionTest { Connection conn = null; PreparedStatement pstmt = null; try { - conn = Transaction.getStandaloneConnection(); + conn = TransactionLegacy.getStandaloneConnection(); pstmt = conn.prepareStatement("truncate table `cloud`.`test`"); pstmt.execute(); @@ -189,7 +189,7 @@ public class TransactionTest { Connection conn = null; PreparedStatement pstmt = null; try { - conn = Transaction.getStandaloneConnection(); + conn = TransactionLegacy.getStandaloneConnection(); pstmt = conn.prepareStatement("DROP TABLE IF EXISTS `cloud`.`test`"); pstmt.execute(); diff --git a/framework/db/test/db.properties b/framework/db/test/db.properties new file mode 100644 index 00000000000..cc1215ff35c --- /dev/null +++ b/framework/db/test/db.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Just here to make the unit test not blow up \ No newline at end of file diff --git a/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/AsyncJobDaoImpl.java b/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/AsyncJobDaoImpl.java index fb3845caa31..ed161e75d2c 100644 --- a/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/AsyncJobDaoImpl.java +++ b/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/AsyncJobDaoImpl.java @@ -32,7 +32,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; public class AsyncJobDaoImpl extends GenericDaoBase implements AsyncJobDao { private static final Logger s_logger = Logger.getLogger(AsyncJobDaoImpl.class.getName()); @@ -182,7 +182,7 @@ public class AsyncJobDaoImpl extends GenericDaoBase implements + ", job_result='" + jobResultMessage + "' where job_status=" + JobInfo.Status.IN_PROGRESS.ordinal() + " AND (job_executing_msid=? OR (job_executing_msid IS NULL AND job_init_msid=?))"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(sql); diff --git a/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/AsyncJobJoinMapDaoImpl.java b/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/AsyncJobJoinMapDaoImpl.java index 20d8ba69fdc..d4ca0d76cb7 100644 --- a/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/AsyncJobJoinMapDaoImpl.java +++ b/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/AsyncJobJoinMapDaoImpl.java @@ -34,7 +34,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.UpdateBuilder; import com.cloud.utils.exception.CloudRuntimeException; @@ -157,7 +157,7 @@ public class AsyncJobJoinMapDaoImpl extends GenericDaoBase findJobsToWake(long joinedJobId) { // TODO: We should fix this. We shouldn't be crossing daos in a dao code. List standaloneList = new ArrayList(); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); String sql = "SELECT job_id FROM async_job_join_map WHERE join_job_id = ? AND job_id NOT IN (SELECT content_id FROM sync_queue_item)"; try { PreparedStatement pstmt = txn.prepareStatement(sql); @@ -231,7 +231,7 @@ public class AsyncJobJoinMapDaoImpl extends GenericDaoBase findJobsToWakeBetween(Date cutDate) { List standaloneList = new ArrayList(); - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { String sql = "SELECT job_id FROM async_job_join_map WHERE next_wakeup < ? AND expiration > ? AND job_id NOT IN (SELECT content_id FROM sync_queue_item)"; PreparedStatement pstmt = txn.prepareStatement(sql); @@ -260,7 +260,7 @@ public class AsyncJobJoinMapDaoImpl extends GenericDaoBase wakeupByJoinedJobCompletion(long joinedJobId) { // List standaloneList = new ArrayList(); // -// Transaction txn = Transaction.currentTxn(); +// TransactionLegacy txn = TransactionLegacy.currentTxn(); // PreparedStatement pstmt = null; // try { // txn.start(); diff --git a/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/SyncQueueDaoImpl.java b/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/SyncQueueDaoImpl.java index f7d9d72dc0b..01efc4e91d3 100644 --- a/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/SyncQueueDaoImpl.java +++ b/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/SyncQueueDaoImpl.java @@ -30,7 +30,7 @@ import com.cloud.utils.DateUtil; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; public class SyncQueueDaoImpl extends GenericDaoBase implements SyncQueueDao { private static final Logger s_logger = Logger.getLogger(SyncQueueDaoImpl.class.getName()); @@ -51,7 +51,7 @@ public class SyncQueueDaoImpl extends GenericDaoBase implemen String sql = "INSERT IGNORE INTO sync_queue(sync_objtype, sync_objid, created, last_updated)" + " values(?, ?, ?, ?)"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(sql); diff --git a/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/SyncQueueItemDaoImpl.java b/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/SyncQueueItemDaoImpl.java index 0cd231f6b4b..2f04a7cc890 100644 --- a/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/SyncQueueItemDaoImpl.java +++ b/framework/jobs/src/org/apache/cloudstack/framework/jobs/dao/SyncQueueItemDaoImpl.java @@ -37,7 +37,7 @@ import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @DB public class SyncQueueItemDaoImpl extends GenericDaoBase implements SyncQueueItemDao { @@ -83,7 +83,7 @@ public class SyncQueueItemDaoImpl extends GenericDaoBase " ORDER BY i.id " + " LIMIT 0, ?"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; try { pstmt = txn.prepareAutoCloseStatement(sql); diff --git a/framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/AsyncJobManagerImpl.java b/framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/AsyncJobManagerImpl.java index 93d50c11233..ffc7b3ae052 100644 --- a/framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/AsyncJobManagerImpl.java +++ b/framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/AsyncJobManagerImpl.java @@ -70,8 +70,11 @@ import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.exception.ExceptionUtil; import com.cloud.utils.mgmt.JmxUtil; @@ -177,19 +180,22 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager, @SuppressWarnings("unchecked") @Override @DB - public long submitAsyncJob(AsyncJob job, String syncObjType, long syncObjId) { - Transaction txt = Transaction.currentTxn(); + public long submitAsyncJob(final AsyncJob job, final String syncObjType, final long syncObjId) { try { @SuppressWarnings("rawtypes") - GenericDao dao = GenericDaoBase.getDao(job.getClass()); + final GenericDao dao = GenericDaoBase.getDao(job.getClass()); - txt.start(); - job.setInitMsid(getMsid()); - dao.persist(job); + return Transaction.execute(new TransactionCallback() { + @Override + public Long doInTransaction(TransactionStatus status) { + job.setInitMsid(getMsid()); + dao.persist(job); - syncAsyncJobExecution(job, syncObjType, syncObjId, 1); - txt.commit(); - return job.getId(); + syncAsyncJobExecution(job, syncObjType, syncObjId, 1); + + return job.getId(); + } + }); } catch (Exception e) { String errMsg = "Unable to schedule async job for command " + job.getCmd() + ", unexpected exception."; s_logger.warn(errMsg, e); @@ -199,123 +205,110 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager, @Override @DB - public void completeAsyncJob(long jobId, Status jobStatus, int resultCode, String resultObject) { + public void completeAsyncJob(final long jobId, final Status jobStatus, final int resultCode, final String resultObject) { if (s_logger.isDebugEnabled()) { s_logger.debug("Complete async job-" + jobId + ", jobStatus: " + jobStatus + ", resultCode: " + resultCode + ", result: " + resultObject); } - Transaction txn = Transaction.currentTxn(); - try { - txn.start(); - AsyncJobVO job = _jobDao.findById(jobId); - if (job == null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("job-" + jobId + " no longer exists, we just log completion info here. " + jobStatus + ", resultCode: " + resultCode + ", result: " + - resultObject); - } - - txn.rollback(); - return; + final AsyncJobVO job = _jobDao.findById(jobId); + if (job == null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("job-" + jobId + " no longer exists, we just log completion info here. " + jobStatus + ", resultCode: " + resultCode + ", result: " + + resultObject); } - if (job.getStatus() != JobInfo.Status.IN_PROGRESS) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("job-" + jobId + " is already completed."); - } - - txn.rollback(); - return; - } - - job.setCompleteMsid(getMsid()); - job.setStatus(jobStatus); - job.setResultCode(resultCode); - - // reset attached object - job.setInstanceType(null); - job.setInstanceId(null); - - if (resultObject != null) { - job.setResult(resultObject); - } - - job.setLastUpdated(DateUtil.currentGMTTime()); - _jobDao.update(jobId, job); - - List wakeupList = wakeupByJoinedJobCompletion(jobId); - _joinMapDao.disjoinAllJobs(jobId); - - txn.commit(); - - for (Long id : wakeupList) { - // TODO, we assume that all jobs in this category is API job only - AsyncJobVO jobToWakeup = _jobDao.findById(id); - if (jobToWakeup != null && (jobToWakeup.getPendingSignals() & AsyncJob.Constants.SIGNAL_MASK_WAKEUP) != 0) - scheduleExecution(jobToWakeup, false); - } - - _messageBus.publish(null, AsyncJob.Topics.JOB_STATE, PublishScope.GLOBAL, jobId); - } catch (Exception e) { - s_logger.error("Unexpected exception while completing async job-" + jobId, e); - txn.rollback(); + return; } + + if (job.getStatus() != JobInfo.Status.IN_PROGRESS) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("job-" + jobId + " is already completed."); + } + + return; + } + + List wakeupList = Transaction.execute(new TransactionCallback>() { + @Override + public List doInTransaction(TransactionStatus status) { + job.setCompleteMsid(getMsid()); + job.setStatus(jobStatus); + job.setResultCode(resultCode); + + // reset attached object + job.setInstanceType(null); + job.setInstanceId(null); + + if (resultObject != null) { + job.setResult(resultObject); + } + + job.setLastUpdated(DateUtil.currentGMTTime()); + _jobDao.update(jobId, job); + + List wakeupList = wakeupByJoinedJobCompletion(jobId); + _joinMapDao.disjoinAllJobs(jobId); + + return wakeupList; + } + }); + + for (Long id : wakeupList) { + // TODO, we assume that all jobs in this category is API job only + AsyncJobVO jobToWakeup = _jobDao.findById(id); + if (jobToWakeup != null && (jobToWakeup.getPendingSignals() & AsyncJob.Constants.SIGNAL_MASK_WAKEUP) != 0) + scheduleExecution(jobToWakeup, false); + } + + _messageBus.publish(null, AsyncJob.Topics.JOB_STATE, PublishScope.GLOBAL, jobId); } @Override @DB - public void updateAsyncJobStatus(long jobId, int processStatus, String resultObject) { + public void updateAsyncJobStatus(final long jobId, final int processStatus, final String resultObject) { if (s_logger.isDebugEnabled()) { s_logger.debug("Update async-job progress, job-" + jobId + ", processStatus: " + processStatus + ", result: " + resultObject); } - Transaction txt = Transaction.currentTxn(); - try { - txt.start(); - AsyncJobVO job = _jobDao.findById(jobId); - if (job == null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("job-" + jobId + " no longer exists, we just log progress info here. progress status: " + processStatus); - } - - txt.rollback(); - return; + final AsyncJobVO job = _jobDao.findById(jobId); + if (job == null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("job-" + jobId + " no longer exists, we just log progress info here. progress status: " + processStatus); } - job.setProcessStatus(processStatus); - if (resultObject != null) { - job.setResult(resultObject); - } - job.setLastUpdated(DateUtil.currentGMTTime()); - _jobDao.update(jobId, job); - txt.commit(); - } catch (Exception e) { - s_logger.error("Unexpected exception while updating async job-" + jobId + " status: ", e); - txt.rollback(); + return; } + + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + job.setProcessStatus(processStatus); + if (resultObject != null) { + job.setResult(resultObject); + } + job.setLastUpdated(DateUtil.currentGMTTime()); + _jobDao.update(jobId, job); + } + }); } @Override @DB - public void updateAsyncJobAttachment(long jobId, String instanceType, Long instanceId) { + public void updateAsyncJobAttachment(final long jobId, final String instanceType, final Long instanceId) { if (s_logger.isDebugEnabled()) { s_logger.debug("Update async-job attachment, job-" + jobId + ", instanceType: " + instanceType + ", instanceId: " + instanceId); } - Transaction txt = Transaction.currentTxn(); - try { - txt.start(); - - AsyncJobVO job = _jobDao.createForUpdate(); - job.setInstanceType(instanceType); - job.setInstanceId(instanceId); - job.setLastUpdated(DateUtil.currentGMTTime()); - _jobDao.update(jobId, job); - - txt.commit(); - } catch (Exception e) { - s_logger.error("Unexpected exception while updating async job-" + jobId + " attachment: ", e); - txt.rollback(); - } + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + AsyncJobVO job = _jobDao.createForUpdate(); + job.setInstanceType(instanceType); + job.setInstanceId(instanceId); + job.setLastUpdated(DateUtil.currentGMTTime()); + _jobDao.update(jobId, job); + } + }); } @Override @@ -493,15 +486,12 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager, return new ManagedContextRunnable() { @Override protected void runInContext() { - Transaction txn = null; long runNumber = getJobRunNumber(); try { // // setup execution environment // - txn = Transaction.open(Transaction.CLOUD_DB); - try { JmxUtil.registerMBean("AsyncJobManager", "Active Job " + job.getId(), new AsyncJobMBeanImpl(job)); } catch (Exception e) { @@ -564,9 +554,6 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager, s_logger.trace("Unable to unregister job " + job.getId() + " to JMX monitoring due to exception " + ExceptionUtil.toString(e)); } - if (txn != null) - txn.close(); - // // clean execution environment // @@ -690,7 +677,6 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager, return new ManagedContextRunnable() { @Override protected void runInContext() { - Transaction txn = Transaction.open("AsyncJobManagerImpl.getHeartbeatTask"); try { List l = _queueMgr.dequeueFromAny(getMsid(), MAX_ONETIME_SCHEDULE_SIZE); if (l != null && l.size() > 0) { @@ -711,12 +697,6 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager, } } catch (Throwable e) { s_logger.error("Unexpected exception when trying to execute queue item, ", e); - } finally { - try { - txn.close(); - } catch (Throwable e) { - s_logger.error("Unexpected exception", e); - } } } }; @@ -785,13 +765,15 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager, } @DB - protected void expungeAsyncJob(AsyncJobVO job) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - _jobDao.expunge(job.getId()); - //purge corresponding sync queue item - _queueMgr.purgeAsyncJobQueueItemId(job.getId()); - txn.commit(); + protected void expungeAsyncJob(final AsyncJobVO job) { + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _jobDao.expunge(job.getId()); + //purge corresponding sync queue item + _queueMgr.purgeAsyncJobQueueItemId(job.getId()); + } + }); } private long getMsid() { @@ -825,58 +807,60 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager, Collections.sort(result); Long[] ids = result.toArray(new Long[result.size()]); - SearchCriteria jobsSC = JobIdsSearch.create("ids", ids); - SearchCriteria queueItemsSC = QueueJobIdsSearch.create("contentIds", ids); + final SearchCriteria jobsSC = JobIdsSearch.create("ids", ids); + final SearchCriteria queueItemsSC = QueueJobIdsSearch.create("contentIds", ids); - Transaction txn = Transaction.currentTxn(); - txn.start(); - AsyncJobVO job = _jobDao.createForUpdate(); - job.setPendingSignals(AsyncJob.Constants.SIGNAL_MASK_WAKEUP); - _jobDao.update(job, jobsSC); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + AsyncJobVO job = _jobDao.createForUpdate(); + job.setPendingSignals(AsyncJob.Constants.SIGNAL_MASK_WAKEUP); + _jobDao.update(job, jobsSC); - SyncQueueItemVO item = _queueItemDao.createForUpdate(); - item.setLastProcessNumber(null); - item.setLastProcessMsid(null); - _queueItemDao.update(item, queueItemsSC); - txn.commit(); + SyncQueueItemVO item = _queueItemDao.createForUpdate(); + item.setLastProcessNumber(null); + item.setLastProcessMsid(null); + _queueItemDao.update(item, queueItemsSC); + } + }); } return _joinMapDao.findJobsToWake(joinedJobId); } @DB protected List wakeupScan() { - Date cutDate = DateUtil.currentGMTTime(); - Transaction txn = Transaction.currentTxn(); + final Date cutDate = DateUtil.currentGMTTime(); SearchCriteria sc = JoinJobTimeSearch.create(); sc.setParameters("beginTime", cutDate); sc.setParameters("endTime", cutDate); - List result = _joinMapDao.customSearch(sc, null); + final List result = _joinMapDao.customSearch(sc, null); - txn.start(); - if (result.size() > 0) { - Collections.sort(result); - Long[] ids = result.toArray(new Long[result.size()]); + return Transaction.execute(new TransactionCallback>() { + @Override + public List doInTransaction(TransactionStatus status) { + if (result.size() > 0) { + Collections.sort(result); + Long[] ids = result.toArray(new Long[result.size()]); - AsyncJobVO job = _jobDao.createForUpdate(); - job.setPendingSignals(AsyncJob.Constants.SIGNAL_MASK_WAKEUP); + AsyncJobVO job = _jobDao.createForUpdate(); + job.setPendingSignals(AsyncJob.Constants.SIGNAL_MASK_WAKEUP); - SearchCriteria sc2 = JobIdsSearch.create("ids", ids); - SearchCriteria queueItemsSC = QueueJobIdsSearch.create("contentIds", ids); + SearchCriteria sc2 = JobIdsSearch.create("ids", ids); + SearchCriteria queueItemsSC = QueueJobIdsSearch.create("contentIds", ids); - _jobDao.update(job, sc2); + _jobDao.update(job, sc2); - SyncQueueItemVO item = _queueItemDao.createForUpdate(); - item.setLastProcessNumber(null); - item.setLastProcessMsid(null); - _queueItemDao.update(item, queueItemsSC); - } + SyncQueueItemVO item = _queueItemDao.createForUpdate(); + item.setLastProcessNumber(null); + item.setLastProcessMsid(null); + _queueItemDao.update(item, queueItemsSC); + } - List wakupIds = _joinMapDao.findJobsToWakeBetween(cutDate); - txn.commit(); - - return wakupIds; + return _joinMapDao.findJobsToWakeBetween(cutDate); + } + }); } @Override @@ -933,18 +917,18 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager, @Override public void onManagementNodeLeft(List nodeList, long selfNodeId) { - for (ManagementServerHost msHost : nodeList) { - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + for (final ManagementServerHost msHost : nodeList) { try { - txn.start(); - List items = _queueMgr.getActiveQueueItems(msHost.getId(), true); - cleanupPendingJobs(items); - _jobDao.resetJobProcess(msHost.getId(), ApiErrorCode.INTERNAL_ERROR.getHttpCode(), "job cancelled because of management server restart"); - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + List items = _queueMgr.getActiveQueueItems(msHost.getId(), true); + cleanupPendingJobs(items); + _jobDao.resetJobProcess(msHost.getId(), ApiErrorCode.INTERNAL_ERROR.getHttpCode(), "job cancelled because of management server restart"); + } + }); } catch (Throwable e) { s_logger.warn("Unexpected exception ", e); - } finally { - txn.close(); } } } diff --git a/framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/SyncQueueManagerImpl.java b/framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/SyncQueueManagerImpl.java index b9b5d6bdabd..7fb02454c88 100644 --- a/framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/SyncQueueManagerImpl.java +++ b/framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/SyncQueueManagerImpl.java @@ -21,8 +21,8 @@ import java.util.Date; import java.util.List; import javax.inject.Inject; -import org.apache.log4j.Logger; +import org.apache.log4j.Logger; import org.apache.cloudstack.framework.jobs.dao.SyncQueueDao; import org.apache.cloudstack.framework.jobs.dao.SyncQueueItemDao; @@ -30,6 +30,9 @@ import com.cloud.utils.DateUtil; import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; public class SyncQueueManagerImpl extends ManagerBase implements SyncQueueManager { @@ -40,84 +43,83 @@ public class SyncQueueManagerImpl extends ManagerBase implements SyncQueueManage @Override @DB - public SyncQueueVO queue(String syncObjType, long syncObjId, String itemType, long itemId, long queueSizeLimit) { - Transaction txn = Transaction.currentTxn(); + public SyncQueueVO queue(final String syncObjType, final long syncObjId, final String itemType, final long itemId, final long queueSizeLimit) { try { - txn.start(); + return Transaction.execute(new TransactionCallback() { + @Override + public SyncQueueVO doInTransaction(TransactionStatus status) { + _syncQueueDao.ensureQueue(syncObjType, syncObjId); + SyncQueueVO queueVO = _syncQueueDao.find(syncObjType, syncObjId); + if(queueVO == null) + throw new CloudRuntimeException("Unable to queue item into DB, DB is full?"); - _syncQueueDao.ensureQueue(syncObjType, syncObjId); - SyncQueueVO queueVO = _syncQueueDao.find(syncObjType, syncObjId); - if(queueVO == null) - throw new CloudRuntimeException("Unable to queue item into DB, DB is full?"); - - queueVO.setQueueSizeLimit(queueSizeLimit); - _syncQueueDao.update(queueVO.getId(), queueVO); - - Date dt = DateUtil.currentGMTTime(); - SyncQueueItemVO item = new SyncQueueItemVO(); - item.setQueueId(queueVO.getId()); - item.setContentType(itemType); - item.setContentId(itemId); - item.setCreated(dt); - - _syncQueueItemDao.persist(item); - txn.commit(); - - return queueVO; - } catch(Exception e) { - s_logger.error("Unexpected exception: ", e); - txn.rollback(); - } - return null; - } - - @Override - @DB - public SyncQueueItemVO dequeueFromOne(long queueId, Long msid) { - Transaction txt = Transaction.currentTxn(); - try { - txt.start(); - - SyncQueueVO queueVO = _syncQueueDao.lockRow(queueId, true); - if(queueVO == null) { - s_logger.error("Sync queue(id: " + queueId + ") does not exist"); - txt.commit(); - return null; - } - - if(queueReadyToProcess(queueVO)) { - SyncQueueItemVO itemVO = _syncQueueItemDao.getNextQueueItem(queueVO.getId()); - if(itemVO != null) { - Long processNumber = queueVO.getLastProcessNumber(); - if(processNumber == null) - processNumber = new Long(1); - else - processNumber = processNumber + 1; - Date dt = DateUtil.currentGMTTime(); - queueVO.setLastProcessNumber(processNumber); - queueVO.setLastUpdated(dt); - queueVO.setQueueSize(queueVO.getQueueSize() + 1); + queueVO.setQueueSizeLimit(queueSizeLimit); _syncQueueDao.update(queueVO.getId(), queueVO); - itemVO.setLastProcessMsid(msid); - itemVO.setLastProcessNumber(processNumber); - itemVO.setLastProcessTime(dt); - _syncQueueItemDao.update(itemVO.getId(), itemVO); + Date dt = DateUtil.currentGMTTime(); + SyncQueueItemVO item = new SyncQueueItemVO(); + item.setQueueId(queueVO.getId()); + item.setContentType(itemType); + item.setContentId(itemId); + item.setCreated(dt); - txt.commit(); - return itemVO; - } else { - if(s_logger.isDebugEnabled()) - s_logger.debug("Sync queue (" + queueId + ") is currently empty"); + _syncQueueItemDao.persist(item); + return queueVO; } - } else { - if(s_logger.isDebugEnabled()) - s_logger.debug("There is a pending process in sync queue(id: " + queueId + ")"); - } - txt.commit(); + }); + } catch(Exception e) { + s_logger.error("Unexpected exception: ", e); + } + return null; + } + + @Override + @DB + public SyncQueueItemVO dequeueFromOne(final long queueId, final Long msid) { + try { + return Transaction.execute(new TransactionCallback() { + @Override + public SyncQueueItemVO doInTransaction(TransactionStatus status) { + SyncQueueVO queueVO = _syncQueueDao.lockRow(queueId, true); + if(queueVO == null) { + s_logger.error("Sync queue(id: " + queueId + ") does not exist"); + return null; + } + + if(queueReadyToProcess(queueVO)) { + SyncQueueItemVO itemVO = _syncQueueItemDao.getNextQueueItem(queueVO.getId()); + if(itemVO != null) { + Long processNumber = queueVO.getLastProcessNumber(); + if(processNumber == null) + processNumber = new Long(1); + else + processNumber = processNumber + 1; + Date dt = DateUtil.currentGMTTime(); + queueVO.setLastProcessNumber(processNumber); + queueVO.setLastUpdated(dt); + queueVO.setQueueSize(queueVO.getQueueSize() + 1); + _syncQueueDao.update(queueVO.getId(), queueVO); + + itemVO.setLastProcessMsid(msid); + itemVO.setLastProcessNumber(processNumber); + itemVO.setLastProcessTime(dt); + _syncQueueItemDao.update(itemVO.getId(), itemVO); + + return itemVO; + } else { + if(s_logger.isDebugEnabled()) + s_logger.debug("Sync queue (" + queueId + ") is currently empty"); + } + } else { + if(s_logger.isDebugEnabled()) + s_logger.debug("There is a pending process in sync queue(id: " + queueId + ")"); + } + + return null; + } + }); } catch(Exception e) { s_logger.error("Unexpected exception: ", e); - txt.rollback(); } return null; @@ -125,101 +127,104 @@ public class SyncQueueManagerImpl extends ManagerBase implements SyncQueueManage @Override @DB - public List dequeueFromAny(Long msid, int maxItems) { + public List dequeueFromAny(final Long msid, final int maxItems) { + + final List resultList = new ArrayList(); - List resultList = new ArrayList(); - Transaction txt = Transaction.currentTxn(); try { - txt.start(); - - List l = _syncQueueItemDao.getNextQueueItems(maxItems); - if(l != null && l.size() > 0) { - for(SyncQueueItemVO item : l) { - SyncQueueVO queueVO = _syncQueueDao.lockRow(item.getQueueId(), true); - SyncQueueItemVO itemVO = _syncQueueItemDao.lockRow(item.getId(), true); - if(queueReadyToProcess(queueVO) && itemVO.getLastProcessNumber() == null) { - Long processNumber = queueVO.getLastProcessNumber(); - if(processNumber == null) - processNumber = new Long(1); - else - processNumber = processNumber + 1; - - Date dt = DateUtil.currentGMTTime(); - queueVO.setLastProcessNumber(processNumber); - queueVO.setLastUpdated(dt); - queueVO.setQueueSize(queueVO.getQueueSize() + 1); - _syncQueueDao.update(queueVO.getId(), queueVO); - - itemVO.setLastProcessMsid(msid); - itemVO.setLastProcessNumber(processNumber); - itemVO.setLastProcessTime(dt); - _syncQueueItemDao.update(item.getId(), itemVO); - - resultList.add(item); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + List l = _syncQueueItemDao.getNextQueueItems(maxItems); + if(l != null && l.size() > 0) { + for(SyncQueueItemVO item : l) { + SyncQueueVO queueVO = _syncQueueDao.lockRow(item.getQueueId(), true); + SyncQueueItemVO itemVO = _syncQueueItemDao.lockRow(item.getId(), true); + if(queueReadyToProcess(queueVO) && itemVO.getLastProcessNumber() == null) { + Long processNumber = queueVO.getLastProcessNumber(); + if(processNumber == null) + processNumber = new Long(1); + else + processNumber = processNumber + 1; + + Date dt = DateUtil.currentGMTTime(); + queueVO.setLastProcessNumber(processNumber); + queueVO.setLastUpdated(dt); + queueVO.setQueueSize(queueVO.getQueueSize() + 1); + _syncQueueDao.update(queueVO.getId(), queueVO); + + itemVO.setLastProcessMsid(msid); + itemVO.setLastProcessNumber(processNumber); + itemVO.setLastProcessTime(dt); + _syncQueueItemDao.update(item.getId(), itemVO); + + resultList.add(item); + } + } } } - } - txt.commit(); + }); + return resultList; } catch(Exception e) { s_logger.error("Unexpected exception: ", e); - txt.rollback(); } + return null; } @Override @DB - public void purgeItem(long queueItemId) { - Transaction txt = Transaction.currentTxn(); + public void purgeItem(final long queueItemId) { try { - txt.start(); - - SyncQueueItemVO itemVO = _syncQueueItemDao.findById(queueItemId); - if(itemVO != null) { - SyncQueueVO queueVO = _syncQueueDao.lockRow(itemVO.getQueueId(), true); - - _syncQueueItemDao.expunge(itemVO.getId()); - - // if item is active, reset queue information - if (itemVO.getLastProcessMsid() != null) { - queueVO.setLastUpdated(DateUtil.currentGMTTime()); - // decrement the count - assert (queueVO.getQueueSize() > 0) : "Count reduce happens when it's already <= 0!"; - queueVO.setQueueSize(queueVO.getQueueSize() - 1); - _syncQueueDao.update(queueVO.getId(), queueVO); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + SyncQueueItemVO itemVO = _syncQueueItemDao.findById(queueItemId); + if(itemVO != null) { + SyncQueueVO queueVO = _syncQueueDao.lockRow(itemVO.getQueueId(), true); + + _syncQueueItemDao.expunge(itemVO.getId()); + + // if item is active, reset queue information + if (itemVO.getLastProcessMsid() != null) { + queueVO.setLastUpdated(DateUtil.currentGMTTime()); + // decrement the count + assert (queueVO.getQueueSize() > 0) : "Count reduce happens when it's already <= 0!"; + queueVO.setQueueSize(queueVO.getQueueSize() - 1); + _syncQueueDao.update(queueVO.getId(), queueVO); + } + } } - } - txt.commit(); + }); } catch(Exception e) { s_logger.error("Unexpected exception: ", e); - txt.rollback(); } } @Override @DB - public void returnItem(long queueItemId) { - Transaction txt = Transaction.currentTxn(); + public void returnItem(final long queueItemId) { try { - txt.start(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + SyncQueueItemVO itemVO = _syncQueueItemDao.findById(queueItemId); + if(itemVO != null) { + SyncQueueVO queueVO = _syncQueueDao.lockRow(itemVO.getQueueId(), true); - SyncQueueItemVO itemVO = _syncQueueItemDao.findById(queueItemId); - if(itemVO != null) { - SyncQueueVO queueVO = _syncQueueDao.lockRow(itemVO.getQueueId(), true); + itemVO.setLastProcessMsid(null); + itemVO.setLastProcessNumber(null); + itemVO.setLastProcessTime(null); + _syncQueueItemDao.update(queueItemId, itemVO); - itemVO.setLastProcessMsid(null); - itemVO.setLastProcessNumber(null); - itemVO.setLastProcessTime(null); - _syncQueueItemDao.update(queueItemId, itemVO); - - queueVO.setLastUpdated(DateUtil.currentGMTTime()); - _syncQueueDao.update(queueVO.getId(), queueVO); - } - txt.commit(); + queueVO.setLastUpdated(DateUtil.currentGMTTime()); + _syncQueueDao.update(queueVO.getId(), queueVO); + } + } + }); } catch(Exception e) { s_logger.error("Unexpected exception: ", e); - txt.rollback(); } } diff --git a/plugins/affinity-group-processors/explicit-dedication/src/org/apache/cloudstack/affinity/ExplicitDedicationProcessor.java b/plugins/affinity-group-processors/explicit-dedication/src/org/apache/cloudstack/affinity/ExplicitDedicationProcessor.java index da231723d32..cc1d3f350e6 100644 --- a/plugins/affinity-group-processors/explicit-dedication/src/org/apache/cloudstack/affinity/ExplicitDedicationProcessor.java +++ b/plugins/affinity-group-processors/explicit-dedication/src/org/apache/cloudstack/affinity/ExplicitDedicationProcessor.java @@ -24,7 +24,6 @@ import javax.ejb.Local; import javax.inject.Inject; import org.apache.log4j.Logger; - import org.apache.cloudstack.affinity.dao.AffinityGroupDao; import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao; @@ -48,6 +47,8 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachineProfile; @@ -410,7 +411,7 @@ public class ExplicitDedicationProcessor extends AffinityProcessorBase implement @DB @Override - public void handleDeleteGroup(AffinityGroup group) { + public void handleDeleteGroup(final AffinityGroup group) { // When a group of the 'ExplicitDedication' type gets deleted, make sure // to remove the dedicated resources in the group as well. if (group != null) { @@ -419,20 +420,21 @@ public class ExplicitDedicationProcessor extends AffinityProcessorBase implement if (s_logger.isDebugEnabled()) { s_logger.debug("Releasing the dedicated resources under group: " + group); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - SearchBuilder listByAffinityGroup = _dedicatedDao.createSearchBuilder(); - listByAffinityGroup.and("affinityGroupId", listByAffinityGroup.entity().getAffinityGroupId(), - SearchCriteria.Op.EQ); - listByAffinityGroup.done(); - SearchCriteria sc = listByAffinityGroup.create(); - sc.setParameters("affinityGroupId", group.getId()); - - _dedicatedDao.lockRows(sc, null, true); - _dedicatedDao.remove(sc); - - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + SearchBuilder listByAffinityGroup = _dedicatedDao.createSearchBuilder(); + listByAffinityGroup.and("affinityGroupId", listByAffinityGroup.entity().getAffinityGroupId(), + SearchCriteria.Op.EQ); + listByAffinityGroup.done(); + SearchCriteria sc = listByAffinityGroup.create(); + sc.setParameters("affinityGroupId", group.getId()); + + _dedicatedDao.lockRows(sc, null, true); + _dedicatedDao.remove(sc); + } + }); } else { if (s_logger.isDebugEnabled()) { s_logger.debug("No dedicated resources to releease under group: " + group); diff --git a/plugins/dedicated-resources/src/org/apache/cloudstack/dedicated/DedicatedResourceManagerImpl.java b/plugins/dedicated-resources/src/org/apache/cloudstack/dedicated/DedicatedResourceManagerImpl.java index 6a6b47ccfd7..cda46efe2ee 100755 --- a/plugins/dedicated-resources/src/org/apache/cloudstack/dedicated/DedicatedResourceManagerImpl.java +++ b/plugins/dedicated-resources/src/org/apache/cloudstack/dedicated/DedicatedResourceManagerImpl.java @@ -48,7 +48,6 @@ import org.apache.cloudstack.api.response.DedicatePodResponse; import org.apache.cloudstack.api.response.DedicateZoneResponse; import org.apache.cloudstack.context.CallContext; import org.apache.cloudstack.framework.config.dao.ConfigurationDao; - import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @@ -79,6 +78,9 @@ import com.cloud.utils.NumbersUtil; import com.cloud.utils.Pair; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.UserVmVO; import com.cloud.vm.dao.UserVmDao; @@ -113,7 +115,7 @@ public class DedicatedResourceManagerImpl implements DedicatedService { @Override @DB @ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Zone") - public List dedicateZone(Long zoneId, Long domainId, String accountName) { + public List dedicateZone(final Long zoneId, final Long domainId, final String accountName) { Long accountId = null; List hosts = null; if(accountName != null){ @@ -124,7 +126,7 @@ public class DedicatedResourceManagerImpl implements DedicatedService { List childDomainIds = getDomainChildIds(domainId); childDomainIds.add(domainId); checkAccountAndDomain(accountId, domainId); - DataCenterVO dc = _zoneDao.findById(zoneId); + final DataCenterVO dc = _zoneDao.findById(zoneId); if (dc == null) { throw new InvalidParameterValueException("Unable to find zone by id " + zoneId); } else { @@ -220,46 +222,50 @@ public class DedicatedResourceManagerImpl implements DedicatedService { checkHostsSuitabilityForExplicitDedication(accountId, childDomainIds, hosts); - Transaction txn = Transaction.currentTxn(); - txn.start(); - // find or create the affinity group by name under this account/domain - AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountId); - if (group == null) { - s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group"); - throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support."); - } + final Long accountIdFinal = accountId; + return Transaction.execute(new TransactionCallback>() { + @Override + public List doInTransaction(TransactionStatus status) { + // find or create the affinity group by name under this account/domain + AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountIdFinal); + if (group == null) { + s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group"); + throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support."); + } + + DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(zoneId, null, null, null, null, null, + group.getId()); + try { + dedicatedResource.setDomainId(domainId); + if (accountIdFinal != null) { + dedicatedResource.setAccountId(accountIdFinal); + } + dedicatedResource = _dedicatedDao.persist(dedicatedResource); + + // save the domainId in the zone + dc.setDomainId(domainId); + if (!_zoneDao.update(zoneId, dc)) { + throw new CloudRuntimeException( + "Failed to dedicate zone, could not set domainId. Please contact Cloud Support."); + } + + } catch (Exception e) { + s_logger.error("Unable to dedicate zone due to " + e.getMessage(), e); + throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support."); + } + + List result = new ArrayList(); + result.add(dedicatedResource); + return result; - DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(zoneId, null, null, null, null, null, - group.getId()); - try { - dedicatedResource.setDomainId(domainId); - if (accountId != null) { - dedicatedResource.setAccountId(accountId); } - dedicatedResource = _dedicatedDao.persist(dedicatedResource); - - // save the domainId in the zone - dc.setDomainId(domainId); - if (!_zoneDao.update(zoneId, dc)) { - throw new CloudRuntimeException( - "Failed to dedicate zone, could not set domainId. Please contact Cloud Support."); - } - - } catch (Exception e) { - s_logger.error("Unable to dedicate zone due to " + e.getMessage(), e); - throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support."); - } - txn.commit(); - - List result = new ArrayList(); - result.add(dedicatedResource); - return result; + }); } @Override @DB @ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Pod") - public List dedicatePod(Long podId, Long domainId, String accountName) { + public List dedicatePod(final Long podId, final Long domainId, final String accountName) { Long accountId = null; if(accountName != null){ Account caller = CallContext.current().getCallingAccount(); @@ -353,37 +359,40 @@ public class DedicatedResourceManagerImpl implements DedicatedService { checkHostsSuitabilityForExplicitDedication(accountId, childDomainIds, hosts); - Transaction txn = Transaction.currentTxn(); - txn.start(); - // find or create the affinity group by name under this account/domain - AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountId); - if (group == null) { - s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group"); - throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support."); - } - DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(null, podId, null, null, null, null, - group.getId()); - try { - dedicatedResource.setDomainId(domainId); - if (accountId != null) { - dedicatedResource.setAccountId(accountId); - } - dedicatedResource = _dedicatedDao.persist(dedicatedResource); - } catch (Exception e) { - s_logger.error("Unable to dedicate pod due to " + e.getMessage(), e); - throw new CloudRuntimeException("Failed to dedicate pod. Please contact Cloud Support."); - } - txn.commit(); + final Long accountIdFinal = accountId; + return Transaction.execute(new TransactionCallback>() { + @Override + public List doInTransaction(TransactionStatus status) { + // find or create the affinity group by name under this account/domain + AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountIdFinal); + if (group == null) { + s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group"); + throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support."); + } + DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(null, podId, null, null, null, null, + group.getId()); + try { + dedicatedResource.setDomainId(domainId); + if (accountIdFinal != null) { + dedicatedResource.setAccountId(accountIdFinal); + } + dedicatedResource = _dedicatedDao.persist(dedicatedResource); + } catch (Exception e) { + s_logger.error("Unable to dedicate pod due to " + e.getMessage(), e); + throw new CloudRuntimeException("Failed to dedicate pod. Please contact Cloud Support."); + } - List result = new ArrayList(); - result.add(dedicatedResource); - return result; + List result = new ArrayList(); + result.add(dedicatedResource); + return result; + } + }); } @Override @DB @ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Cluster") - public List dedicateCluster(Long clusterId, Long domainId, String accountName) { + public List dedicateCluster(final Long clusterId, final Long domainId, final String accountName) { Long accountId = null; List hosts = null; if(accountName != null){ @@ -463,37 +472,40 @@ public class DedicatedResourceManagerImpl implements DedicatedService { checkHostsSuitabilityForExplicitDedication(accountId, childDomainIds, hosts); - Transaction txn = Transaction.currentTxn(); - txn.start(); - // find or create the affinity group by name under this account/domain - AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountId); - if (group == null) { - s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group"); - throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support."); - } - DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(null, null, clusterId, null, null, null, - group.getId()); - try { - dedicatedResource.setDomainId(domainId); - if (accountId != null) { - dedicatedResource.setAccountId(accountId); - } - dedicatedResource = _dedicatedDao.persist(dedicatedResource); - } catch (Exception e) { - s_logger.error("Unable to dedicate host due to " + e.getMessage(), e); - throw new CloudRuntimeException("Failed to dedicate cluster. Please contact Cloud Support."); - } - txn.commit(); + final Long accountIdFinal = accountId; + return Transaction.execute(new TransactionCallback>() { + @Override + public List doInTransaction(TransactionStatus status) { + // find or create the affinity group by name under this account/domain + AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountIdFinal); + if (group == null) { + s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group"); + throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support."); + } + DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(null, null, clusterId, null, null, null, + group.getId()); + try { + dedicatedResource.setDomainId(domainId); + if (accountIdFinal != null) { + dedicatedResource.setAccountId(accountIdFinal); + } + dedicatedResource = _dedicatedDao.persist(dedicatedResource); + } catch (Exception e) { + s_logger.error("Unable to dedicate host due to " + e.getMessage(), e); + throw new CloudRuntimeException("Failed to dedicate cluster. Please contact Cloud Support."); + } - List result = new ArrayList(); - result.add(dedicatedResource); - return result; + List result = new ArrayList(); + result.add(dedicatedResource); + return result; + } + }); } @Override @DB @ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Host") - public List dedicateHost(Long hostId, Long domainId, String accountName) { + public List dedicateHost(final Long hostId, final Long domainId, final String accountName) { Long accountId = null; if(accountName != null){ Account caller = CallContext.current().getCallingAccount(); @@ -558,31 +570,35 @@ public class DedicatedResourceManagerImpl implements DedicatedService { childDomainIds.add(domainId); checkHostSuitabilityForExplicitDedication(accountId, childDomainIds, hostId); - Transaction txn = Transaction.currentTxn(); - txn.start(); - // find or create the affinity group by name under this account/domain - AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountId); - if (group == null) { - s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group"); - throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support."); - } - DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(null, null, null, hostId, null, null, - group.getId()); - try { - dedicatedResource.setDomainId(domainId); - if (accountId != null) { - dedicatedResource.setAccountId(accountId); - } - dedicatedResource = _dedicatedDao.persist(dedicatedResource); - } catch (Exception e) { - s_logger.error("Unable to dedicate host due to " + e.getMessage(), e); - throw new CloudRuntimeException("Failed to dedicate host. Please contact Cloud Support."); - } - txn.commit(); + final Long accountIdFinal = accountId; + return Transaction.execute(new TransactionCallback>() { + @Override + public List doInTransaction(TransactionStatus status) { + // find or create the affinity group by name under this account/domain + AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountIdFinal); + if (group == null) { + s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group"); + throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support."); + } + DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(null, null, null, hostId, null, null, + group.getId()); + try { + dedicatedResource.setDomainId(domainId); + if (accountIdFinal != null) { + dedicatedResource.setAccountId(accountIdFinal); + } + dedicatedResource = _dedicatedDao.persist(dedicatedResource); + } catch (Exception e) { + s_logger.error("Unable to dedicate host due to " + e.getMessage(), e); + throw new CloudRuntimeException("Failed to dedicate host. Please contact Cloud Support."); + } + + List result = new ArrayList(); + result.add(dedicatedResource); + return result; + } + }); - List result = new ArrayList(); - result.add(dedicatedResource); - return result; } private AffinityGroup findOrCreateDedicatedAffinityGroup(Long domainId, Long accountId) { @@ -889,9 +905,8 @@ public class DedicatedResourceManagerImpl implements DedicatedService { @Override @DB @ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE_RELEASE, eventDescription = "Releasing dedicated resource") - public boolean releaseDedicatedResource(Long zoneId, Long podId, Long clusterId, Long hostId) throws InvalidParameterValueException{ + public boolean releaseDedicatedResource(final Long zoneId, Long podId, Long clusterId, Long hostId) throws InvalidParameterValueException{ DedicatedResourceVO resource = null; - Long resourceId = null; if (zoneId != null) { resource = _dedicatedDao.findByZoneId(zoneId); } @@ -907,26 +922,28 @@ public class DedicatedResourceManagerImpl implements DedicatedService { if (resource == null){ throw new InvalidParameterValueException("No Dedicated Resource available to release"); } else { - Transaction txn = Transaction.currentTxn(); - txn.start(); - resourceId = resource.getId(); - if (!_dedicatedDao.remove(resourceId)) { - throw new CloudRuntimeException("Failed to delete Resource " + resourceId); - } - if (zoneId != null) { - // remove the domainId set in zone - DataCenterVO dc = _zoneDao.findById(zoneId); - if (dc != null) { - dc.setDomainId(null); - dc.setDomain(null); - if (!_zoneDao.update(zoneId, dc)) { - throw new CloudRuntimeException( - "Failed to release dedicated zone, could not clear domainId. Please contact Cloud Support."); + final DedicatedResourceVO resourceFinal = resource; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + Long resourceId = resourceFinal.getId(); + if (!_dedicatedDao.remove(resourceId)) { + throw new CloudRuntimeException("Failed to delete Resource " + resourceId); + } + if (zoneId != null) { + // remove the domainId set in zone + DataCenterVO dc = _zoneDao.findById(zoneId); + if (dc != null) { + dc.setDomainId(null); + dc.setDomain(null); + if (!_zoneDao.update(zoneId, dc)) { + throw new CloudRuntimeException( + "Failed to release dedicated zone, could not clear domainId. Please contact Cloud Support."); + } + } } } - } - - txn.commit(); + }); // find the group associated and check if there are any more // resources under that group diff --git a/plugins/file-systems/netapp/src/com/cloud/netapp/NetappManagerImpl.java b/plugins/file-systems/netapp/src/com/cloud/netapp/NetappManagerImpl.java index 8f7b6d8df8d..e0c2f57d62d 100644 --- a/plugins/file-systems/netapp/src/com/cloud/netapp/NetappManagerImpl.java +++ b/plugins/file-systems/netapp/src/com/cloud/netapp/NetappManagerImpl.java @@ -50,7 +50,7 @@ import com.cloud.netapp.dao.PoolDao; import com.cloud.netapp.dao.VolumeDao; import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.DB; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -225,7 +225,7 @@ public class NetappManagerImpl extends ManagerBase implements NetappManager throw new ResourceInUseException("There are luns on the volume"); } - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); PoolVO pool = _poolDao.findById(volume.getPoolId()); if (pool == null) { @@ -388,7 +388,7 @@ public class NetappManagerImpl extends ManagerBase implements NetappManager } Long volumeId = null; - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); NetappVolumeVO volume = null; volume = _volumeDao.findVolume(ipAddress, aggName, volName); @@ -624,7 +624,7 @@ public class NetappManagerImpl extends ManagerBase implements NetappManager String[] result = new String[3]; StringBuilder lunName = new StringBuilder("lun-"); LunVO lun = null; - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); PoolVO pool = _poolDao.findPool(poolName); @@ -802,7 +802,7 @@ public class NetappManagerImpl extends ManagerBase implements NetappManager @DB public void destroyLunOnFiler(String lunName) throws InvalidParameterValueException, ServerException{ - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); LunVO lun = _lunDao.findByName(lunName); diff --git a/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BareMetalPingServiceImpl.java b/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BareMetalPingServiceImpl.java index 314c9577279..4c07dae70ab 100755 --- a/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BareMetalPingServiceImpl.java +++ b/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BareMetalPingServiceImpl.java @@ -255,15 +255,12 @@ public class BareMetalPingServiceImpl extends BareMetalPxeServiceBase implements } BaremetalPxeVO vo = new BaremetalPxeVO(); - Transaction txn = Transaction.currentTxn(); vo.setHostId(pxeServer.getId()); vo.setNetworkServiceProviderId(ntwkSvcProvider.getId()); vo.setPodId(pod.getId()); vo.setPhysicalNetworkId(pcmd.getPhysicalNetworkId()); vo.setDeviceType(BaremetalPxeType.PING.toString()); - txn.start(); _pxeDao.persist(vo); - txn.commit(); return vo; } diff --git a/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetaNetworkGuru.java b/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetaNetworkGuru.java index 787137778cd..29e180deafc 100755 --- a/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetaNetworkGuru.java +++ b/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetaNetworkGuru.java @@ -22,7 +22,6 @@ import javax.ejb.Local; import javax.inject.Inject; import org.apache.log4j.Logger; - import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; @@ -53,6 +52,8 @@ import com.cloud.network.guru.DirectPodBasedNetworkGuru; import com.cloud.network.guru.NetworkGuru; import com.cloud.offerings.dao.NetworkOfferingDao; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.vm.NicProfile; import com.cloud.vm.ReservationContext; import com.cloud.vm.VirtualMachineProfile; @@ -100,18 +101,18 @@ public class BaremetaNetworkGuru extends DirectPodBasedNetworkGuru { } else { // we need to get a new ip address if we try to deploy a vm in a // different pod - IPAddressVO ipVO = _ipAddressDao.findByIpAndSourceNetworkId(network.getId(), oldIp); + final IPAddressVO ipVO = _ipAddressDao.findByIpAndSourceNetworkId(network.getId(), oldIp); if (ipVO != null) { PodVlanMapVO mapVO = _podVlanDao.listPodVlanMapsByVlan(ipVO.getVlanId()); if (mapVO.getPodId() != dest.getPod().getId()) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - - // release the old ip here - _ipAddrMgr.markIpAsUnavailable(ipVO.getId()); - _ipAddressDao.unassignIpAddress(ipVO.getId()); - - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + // release the old ip here + _ipAddrMgr.markIpAsUnavailable(ipVO.getId()); + _ipAddressDao.unassignIpAddress(ipVO.getId()); + } + }); nic.setIp4Address(null); getNewIp = true; diff --git a/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalDhcpElement.java b/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalDhcpElement.java index 6cf94f2db94..8057cd42f91 100755 --- a/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalDhcpElement.java +++ b/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalDhcpElement.java @@ -50,7 +50,6 @@ import com.cloud.utils.component.AdapterBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.QueryBuilder; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; import com.cloud.vm.NicProfile; import com.cloud.vm.NicVO; import com.cloud.vm.ReservationContext; @@ -115,14 +114,11 @@ public class BaremetalDhcpElement extends AdapterBase implements DhcpServiceProv return false; } - Transaction txn = Transaction.currentTxn(); - txn.start(); nic.setMacAddress(host.getPrivateMacAddress()); NicVO vo = _nicDao.findById(nic.getId()); assert vo != null : "Where ths nic " + nic.getId() + " going???"; vo.setMacAddress(nic.getMacAddress()); _nicDao.update(vo.getId(), vo); - txn.commit(); return true; } diff --git a/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalDhcpManagerImpl.java b/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalDhcpManagerImpl.java index 9e1fd65a239..82397f5b31b 100755 --- a/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalDhcpManagerImpl.java +++ b/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalDhcpManagerImpl.java @@ -69,7 +69,6 @@ import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.QueryBuilder; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.NicProfile; import com.cloud.vm.ReservationContext; @@ -278,10 +277,7 @@ public class BaremetalDhcpManagerImpl extends ManagerBase implements BaremetalDh vo.setHostId(dhcpServer.getId()); vo.setNetworkServiceProviderId(ntwkSvcProvider.getId()); vo.setPhysicalNetworkId(cmd.getPhysicalNetworkId()); - Transaction txn = Transaction.currentTxn(); - txn.start(); _extDhcpDao.persist(vo); - txn.commit(); return vo; } diff --git a/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalKickStartServiceImpl.java b/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalKickStartServiceImpl.java index aa0ff1c0360..cb4babf93d7 100755 --- a/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalKickStartServiceImpl.java +++ b/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalKickStartServiceImpl.java @@ -27,11 +27,10 @@ import java.util.Map; import javax.ejb.Local; import javax.inject.Inject; -import org.apache.log4j.Logger; - import org.apache.cloudstack.api.AddBaremetalKickStartPxeCmd; import org.apache.cloudstack.api.AddBaremetalPxeCmd; import org.apache.cloudstack.api.ListBaremetalPxeServersCmd; +import org.apache.log4j.Logger; import com.cloud.agent.api.Answer; import com.cloud.agent.api.baremetal.IpmISetBootDevCommand; @@ -58,7 +57,6 @@ import com.cloud.uservm.UserVm; import com.cloud.utils.db.DB; import com.cloud.utils.db.QueryBuilder; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.NicProfile; import com.cloud.vm.ReservationContext; @@ -225,14 +223,11 @@ public class BaremetalKickStartServiceImpl extends BareMetalPxeServiceBase imple } BaremetalPxeVO vo = new BaremetalPxeVO(); - Transaction txn = Transaction.currentTxn(); vo.setHostId(pxeServer.getId()); vo.setNetworkServiceProviderId(ntwkSvcProvider.getId()); vo.setPhysicalNetworkId(kcmd.getPhysicalNetworkId()); vo.setDeviceType(BaremetalPxeType.KICK_START.toString()); - txn.start(); _pxeDao.persist(vo); - txn.commit(); return vo; } diff --git a/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalPxeElement.java b/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalPxeElement.java index 244ce8c823c..b9dbc776692 100755 --- a/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalPxeElement.java +++ b/plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BaremetalPxeElement.java @@ -48,7 +48,6 @@ import com.cloud.utils.component.AdapterBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.QueryBuilder; import com.cloud.utils.db.SearchCriteria.Op; -import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.NicProfile; import com.cloud.vm.NicVO; @@ -117,14 +116,11 @@ public class BaremetalPxeElement extends AdapterBase implements NetworkElement { VMInstanceVO vo = _vmDao.findById(vm.getId()); if (vo.getLastHostId() == null) { - Transaction txn = Transaction.currentTxn(); - txn.start(); nic.setMacAddress(dest.getHost().getPrivateMacAddress()); NicVO nicVo = _nicDao.findById(nic.getId()); assert vo != null : "Where ths nic " + nic.getId() + " going???"; nicVo.setMacAddress(nic.getMacAddress()); _nicDao.update(nicVo.getId(), nicVo); - txn.commit(); /*This vm is just being created */ if (!_pxeMgr.prepare(vm, nic, dest, context)) { diff --git a/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockAgentManagerImpl.java b/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockAgentManagerImpl.java index fcc1b4f2209..dec6b38ee92 100644 --- a/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockAgentManagerImpl.java +++ b/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockAgentManagerImpl.java @@ -42,8 +42,10 @@ import com.cloud.utils.component.ManagerBase; import com.cloud.utils.concurrency.NamedThreadFactory; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; + import org.apache.cloudstack.api.command.admin.host.AddSecondaryStorageCmd; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @@ -51,6 +53,7 @@ import org.springframework.stereotype.Component; import javax.ejb.Local; import javax.inject.Inject; import javax.naming.ConfigurationException; + import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.HashMap; @@ -159,7 +162,7 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage mockHost.setVersion(this.getClass().getPackage().getImplementationVersion()); mockHost.setResource("com.cloud.agent.AgentRoutingResource"); - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); mockHost = _mockHostDao.persist(mockHost); @@ -170,7 +173,7 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage throw new CloudRuntimeException("Error configuring agent", ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -256,7 +259,7 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage private void handleSystemVMStop() { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { if (this.mode.equalsIgnoreCase("Stop")) { txn.start(); @@ -279,7 +282,7 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage throw new CloudRuntimeException("Unable to get host " + guid + " due to " + ex.getMessage(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -323,7 +326,7 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage mockHost.setVersion(this.getClass().getPackage().getImplementationVersion()); mockHost.setResource(resource); mockHost.setVmId(vmId); - Transaction simtxn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy simtxn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { simtxn.start(); mockHost = _mockHostDao.persist(mockHost); @@ -334,7 +337,7 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage + ex.getMessage(), ex); } finally { simtxn.close(); - simtxn = Transaction.open(Transaction.CLOUD_DB); + simtxn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); simtxn.close(); } @@ -366,7 +369,7 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage @Override public MockHost getHost(String guid) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); MockHost _host = _mockHostDao.findByGuid(guid); @@ -382,7 +385,7 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage throw new CloudRuntimeException("Unable to get host " + guid + " due to " + ex.getMessage(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -391,7 +394,7 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage public GetHostStatsAnswer getHostStatistic(GetHostStatsCommand cmd) { String hostGuid = cmd.getHostGuid(); MockHost host = null; - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); host = _mockHostDao.findByGuid(hostGuid); @@ -404,11 +407,11 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage throw new CloudRuntimeException("Unable to get host " + hostGuid + " due to " + ex.getMessage(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } - Transaction vmtxn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy vmtxn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { vmtxn.start(); List vms = _mockVmDao.findByHostId(host.getId()); @@ -435,7 +438,7 @@ public class MockAgentManagerImpl extends ManagerBase implements MockAgentManage + ex.getMessage(), ex); } finally { vmtxn.close(); - vmtxn = Transaction.open(Transaction.CLOUD_DB); + vmtxn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); vmtxn.close(); } } diff --git a/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockStorageManagerImpl.java b/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockStorageManagerImpl.java index d23575bfee3..64463d486ae 100644 --- a/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockStorageManagerImpl.java +++ b/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockStorageManagerImpl.java @@ -76,7 +76,7 @@ import com.cloud.storage.dao.VMTemplateDao; import com.cloud.storage.template.TemplateProp; import com.cloud.utils.NumbersUtil; import com.cloud.utils.component.ManagerBase; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.DiskProfile; import com.cloud.vm.VirtualMachine.State; @@ -120,7 +120,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa VMTemplateDao templateDao; private MockVolumeVO findVolumeFromSecondary(String path, String ssUrl, MockVolumeType type) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); String volumePath = path.replaceAll(ssUrl, ""); @@ -141,7 +141,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Unable to find volume " + path + " on secondary " + ssUrl, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -154,7 +154,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa return new PrimaryStorageDownloadAnswer("Can't find primary storage"); } - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); MockStoragePoolVO primaryStorage = null; try { txn.start(); @@ -168,7 +168,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when finding primary storagee " + cmd.getPoolUuid(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -179,7 +179,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa newVolume.setPoolId(primaryStorage.getId()); newVolume.setSize(template.getSize()); newVolume.setType(MockVolumeType.VOLUME); - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); _mockVolumeDao.persist(newVolume); @@ -189,7 +189,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when saving volume " + newVolume, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } return new PrimaryStorageDownloadAnswer(newVolume.getPath(), newVolume.getSize()); @@ -200,7 +200,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa StorageFilerTO sf = cmd.getPool(); DiskProfile dskch = cmd.getDiskCharacteristics(); MockStoragePoolVO storagePool = null; - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); storagePool = _mockStoragePoolDao.findByUuid(sf.getUuid()); @@ -213,7 +213,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when finding storage " + sf.getUuid(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -224,7 +224,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa volume.setPath(storagePool.getMountPoint() + volumeName); volume.setSize(dskch.getSize()); volume.setType(MockVolumeType.VOLUME); - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); volume = _mockVolumeDao.persist(volume); @@ -234,7 +234,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when saving volume " + volume, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -246,7 +246,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public AttachVolumeAnswer AttachVolume(AttachVolumeCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); String poolid = cmd.getPoolUuid(); @@ -270,7 +270,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa + cmd.getVmName(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -284,7 +284,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa } String vmName = cmd.getVmName(); - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); MockVMVO vm = null; try { txn.start(); @@ -298,7 +298,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when attaching iso to vm " + vm.getName(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } return new Answer(cmd); @@ -306,7 +306,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public Answer DeleteStoragePool(DeleteStoragePoolCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); MockStoragePoolVO storage = _mockStoragePoolDao.findByUuid(cmd.getPool().getUuid()); @@ -321,7 +321,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when deleting storage pool " + cmd.getPool().getPath(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -329,7 +329,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public ModifyStoragePoolAnswer ModifyStoragePool(ModifyStoragePoolCommand cmd) { StorageFilerTO sf = cmd.getPool(); - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); MockStoragePoolVO storagePool = null; try { txn.start(); @@ -361,7 +361,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when modifying storage pool " + cmd.getPool().getPath(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } return new ModifyStoragePoolAnswer(cmd, storagePool.getCapacity(), 0, new HashMap()); @@ -370,7 +370,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public Answer CreateStoragePool(CreateStoragePoolCommand cmd) { StorageFilerTO sf = cmd.getPool(); - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); MockStoragePoolVO storagePool = null; try { txn.start(); @@ -402,7 +402,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when creating storage pool " + cmd.getPool().getPath(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } return new ModifyStoragePoolAnswer(cmd, storagePool.getCapacity(), 0, new HashMap()); @@ -410,7 +410,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public Answer SecStorageSetup(SecStorageSetupCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); MockSecStorageVO storage = null; try { txn.start(); @@ -424,7 +424,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when setting up sec storage" + cmd.getSecUrl(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } return new SecStorageSetupAnswer(storage.getMountPoint()); @@ -432,7 +432,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public Answer ListVolumes(ListVolumeCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); MockSecStorageVO storage = null; try { txn.start(); @@ -446,11 +446,11 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when finding sec storage " + cmd.getSecUrl(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); List volumes = _mockVolumeDao.findByStorageIdAndType(storage.getId(), @@ -468,7 +468,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when finding template on sec storage " + storage.getId(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -483,7 +483,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa MockSecStorageVO storage = null; String nfsUrl = ((NfsTO) store).getUrl(); - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); storage = _mockSecStorageDao.findByUrl(nfsUrl); try { txn.start(); @@ -501,14 +501,14 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when finding template on sec storage " + storage.getId(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @Override public Answer Destroy(DestroyCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); MockVolumeVO volume = _mockVolumeDao.findByStoragePathAndType(cmd.getVolume().getPath()); @@ -530,7 +530,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when destroying volume " + cmd.getVolume().getPath(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } return new Answer(cmd); @@ -539,7 +539,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public DownloadAnswer Download(DownloadCommand cmd) { MockSecStorageVO ssvo = null; - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); ssvo = _mockSecStorageDao.findByUrl(cmd.getSecUrl()); @@ -553,7 +553,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error accessing secondary storage " + cmd.getSecUrl(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -564,7 +564,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa volume.setSize(0); volume.setType(MockVolumeType.TEMPLATE); volume.setStatus(Status.DOWNLOAD_IN_PROGRESS); - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); volume = _mockVolumeDao.persist(volume); @@ -574,7 +574,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when saving volume " + volume, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } return new DownloadAnswer(String.valueOf(volume.getId()), 0, "Downloading", Status.DOWNLOAD_IN_PROGRESS, @@ -583,7 +583,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public DownloadAnswer DownloadProcess(DownloadProgressCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); String volumeId = cmd.getJobId(); @@ -616,7 +616,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error during download job " + cmd.getJobId(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -624,7 +624,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public GetStorageStatsAnswer GetStorageStats(GetStorageStatsCommand cmd) { String uuid = cmd.getStorageId(); - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); if (uuid == null) { @@ -653,7 +653,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("DBException during storage stats collection for pool " + uuid, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -663,7 +663,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa String volPath = cmd.getVolumePath(); MockVolumeVO volume = null; MockStoragePoolVO storagePool = null; - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); volume = _mockVolumeDao.findByStoragePathAndType(volPath); @@ -680,7 +680,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Unable to perform snapshot", ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -693,7 +693,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa snapshot.setPoolId(storagePool.getId()); snapshot.setType(MockVolumeType.SNAPSHOT); snapshot.setStatus(Status.DOWNLOADED); - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); snapshot = _mockVolumeDao.persist(snapshot); @@ -703,7 +703,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when saving snapshot " + snapshot, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -717,7 +717,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa MockVolumeVO volume = null; MockVolumeVO snapshot = null; MockSecStorageVO secStorage = null; - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); volume = _mockVolumeDao.findByStoragePathAndType(cmd.getVolumePath()); @@ -742,7 +742,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when backing up snapshot"); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -754,7 +754,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa newsnapshot.setSize(snapshot.getSize()); newsnapshot.setStatus(Status.DOWNLOADED); newsnapshot.setType(MockVolumeType.SNAPSHOT); - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); snapshot = _mockVolumeDao.persist(snapshot); @@ -764,7 +764,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when backing up snapshot " + newsnapshot, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -773,7 +773,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public CreateVolumeFromSnapshotAnswer CreateVolumeFromSnapshot(CreateVolumeFromSnapshotCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); MockVolumeVO backSnapshot = null; MockStoragePoolVO primary = null; try { @@ -795,7 +795,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when creating volume from snapshot", ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -808,7 +808,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa volume.setSize(backSnapshot.getSize()); volume.setStatus(Status.DOWNLOADED); volume.setType(MockVolumeType.VOLUME); - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); _mockVolumeDao.persist(volume); @@ -818,7 +818,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when creating volume from snapshot " + volume, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -828,7 +828,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public Answer Delete(DeleteCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); MockVolumeVO template = _mockVolumeDao.findByStoragePathAndType(cmd.getData().getPath()); @@ -842,7 +842,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when deleting object"); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } return new Answer(cmd); @@ -879,7 +879,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public void preinstallTemplates(String url, long zoneId) { MockSecStorageVO storage = null; - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); storage = _mockSecStorageDao.findByUrl(url); @@ -933,14 +933,14 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa } catch (Exception ex) { throw new CloudRuntimeException("Unable to find sec storage at " + url, ex); } finally { - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @Override public StoragePoolInfo getLocalStorage(String hostGuid) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); MockHost host = null; MockStoragePoolVO storagePool = null; try { @@ -953,7 +953,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Unable to find host " + hostGuid, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -965,7 +965,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa storagePool.setCapacity(DEFAULT_HOST_STORAGE_SIZE); storagePool.setHostGuid(hostGuid); storagePool.setStorageType(StoragePoolType.Filesystem); - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); storagePool = _mockStoragePoolDao.persist(storagePool); @@ -975,7 +975,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when saving storagePool " + storagePool, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -985,7 +985,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public StoragePoolInfo getLocalStorage(String hostGuid, Long storageSize) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); MockHost host = null; try { txn.start(); @@ -996,13 +996,13 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Unable to find host " + hostGuid, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } if (storageSize == null) { storageSize = DEFAULT_HOST_STORAGE_SIZE; } - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); MockStoragePoolVO storagePool = null; try { txn.start(); @@ -1013,7 +1013,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when finding storagePool " + storagePool, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } if (storagePool == null) { @@ -1024,7 +1024,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa storagePool.setCapacity(storageSize); storagePool.setHostGuid(hostGuid); storagePool.setStorageType(StoragePoolType.Filesystem); - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); storagePool = _mockStoragePoolDao.persist(storagePool); @@ -1034,7 +1034,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when saving storagePool " + storagePool, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -1044,7 +1044,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public CreatePrivateTemplateAnswer CreatePrivateTemplateFromSnapshot(CreatePrivateTemplateFromSnapshotCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); MockVolumeVO snapshot = null; MockSecStorageVO sec = null; try { @@ -1066,7 +1066,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa txn.commit(); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -1078,7 +1078,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa template.setSize(snapshot.getSize()); template.setStatus(Status.DOWNLOADED); template.setType(MockVolumeType.TEMPLATE); - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); template = _mockVolumeDao.persist(template); @@ -1088,7 +1088,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when saving template " + template, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -1098,7 +1098,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public Answer ComputeChecksum(ComputeChecksumCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); MockVolumeVO volume = _mockVolumeDao.findByName(cmd.getTemplatePath()); @@ -1116,14 +1116,14 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa return new Answer(cmd, true, md5); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @Override public CreatePrivateTemplateAnswer CreatePrivateTemplateFromVolume(CreatePrivateTemplateFromVolumeCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); MockVolumeVO volume = null; MockSecStorageVO sec = null; try { @@ -1143,7 +1143,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa throw new CloudRuntimeException("Error when creating private template from volume"); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -1155,7 +1155,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa template.setSize(volume.getSize()); template.setStatus(Status.DOWNLOADED); template.setType(MockVolumeType.TEMPLATE); - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); template = _mockVolumeDao.persist(template); @@ -1166,7 +1166,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa + template.getName(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -1176,7 +1176,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa @Override public CopyVolumeAnswer CopyVolume(CopyVolumeCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); boolean toSecondaryStorage = cmd.toSecondaryStorage(); MockSecStorageVO sec = null; MockStoragePoolVO primaryStorage = null; @@ -1193,11 +1193,11 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa + cmd.getSecondaryStorageURL(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); primaryStorage = _mockStoragePoolDao.findByUuid(cmd.getPool().getUuid()); @@ -1211,12 +1211,12 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa + cmd.getPool(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } MockVolumeVO volume = null; - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); volume = _mockVolumeDao.findByStoragePathAndType(cmd.getVolumePath()); @@ -1230,7 +1230,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa + cmd.getVolumePath(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -1243,7 +1243,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa vol.setSize(volume.getSize()); vol.setStatus(Status.DOWNLOADED); vol.setType(MockVolumeType.VOLUME); - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); vol = _mockVolumeDao.persist(vol); @@ -1254,7 +1254,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa + vol.getName(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } return new CopyVolumeAnswer(cmd, true, null, sec.getMountPoint(), vol.getPath()); @@ -1266,7 +1266,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa vol.setSize(volume.getSize()); vol.setStatus(Status.DOWNLOADED); vol.setType(MockVolumeType.VOLUME); - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); vol = _mockVolumeDao.persist(vol); @@ -1277,7 +1277,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa + vol.getName(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } return new CopyVolumeAnswer(cmd, true, null, primaryStorage.getMountPoint(), vol.getPath()); diff --git a/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockVmManagerImpl.java b/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockVmManagerImpl.java index e503bc6e7d1..28e235e556e 100644 --- a/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockVmManagerImpl.java +++ b/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockVmManagerImpl.java @@ -72,7 +72,7 @@ import com.cloud.simulator.dao.MockVMDao; import com.cloud.utils.Pair; import com.cloud.utils.Ternary; import com.cloud.utils.component.ManagerBase; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.VirtualMachine.State; import org.apache.log4j.Logger; @@ -111,7 +111,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { int cpuHz, long ramSize, String bootArgs, String hostGuid) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); MockHost host = null; MockVm vm = null; try { @@ -128,7 +128,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { throw new CloudRuntimeException("Unable to start VM " + vmName, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } @@ -153,7 +153,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { } else if (vmName.startsWith("i-")) { vm.setType("User"); } - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); vm = _mockVmDao.persist((MockVMVO) vm); @@ -163,13 +163,13 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { throw new CloudRuntimeException("unable to save vm to db " + vm.getName(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } else { if(vm.getState() == State.Stopped) { vm.setState(State.Running); - txn = Transaction.open(Transaction.SIMULATOR_DB); + txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); _mockVmDao.update(vm.getId(), (MockVMVO)vm); @@ -179,7 +179,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { throw new CloudRuntimeException("unable to update vm " + vm.getName(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -230,7 +230,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { @Override public Map getVms(String hostGuid) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); List vms = _mockVmDao.findByHostGuid(hostGuid); @@ -245,7 +245,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { throw new CloudRuntimeException("unable to fetch vms from host " + hostGuid, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -282,7 +282,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { @Override public Map getVmStates(String hostGuid) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); Map states = new HashMap(); @@ -301,7 +301,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { throw new CloudRuntimeException("unable to fetch vms from host " + hostGuid, ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -338,7 +338,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { @Override public CheckVirtualMachineAnswer checkVmState(CheckVirtualMachineCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); MockVMVO vm = _mockVmDao.findByVmName(cmd.getVmName()); @@ -353,7 +353,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { throw new CloudRuntimeException("unable to fetch vm state " + cmd.getVmName(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -378,7 +378,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { @Override public MigrateAnswer Migrate(MigrateCommand cmd, SimulatorInfo info) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); String vmName = cmd.getVmName(); @@ -405,14 +405,14 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { throw new CloudRuntimeException("unable to migrate vm " + cmd.getVmName(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @Override public PrepareForMigrationAnswer prepareForMigrate(PrepareForMigrationCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); VirtualMachineTO vmTo = cmd.getVirtualMachine(); try { txn.start(); @@ -425,7 +425,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { throw new CloudRuntimeException("unable to find vm " + vmTo.getName(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); return new PrepareForMigrationAnswer(cmd); } @@ -438,7 +438,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { @Override public Answer CleanupNetworkRules(CleanupNetworkRulesCmd cmd, SimulatorInfo info) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); List rules = _mockSecurityDao.findByHost(info.getHostUuid()); @@ -455,7 +455,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { throw new CloudRuntimeException("unable to clean up rules", ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -503,7 +503,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { @Override public StopAnswer stopVM(StopCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); String vmName = cmd.getVmName(); @@ -523,14 +523,14 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { throw new CloudRuntimeException("unable to stop vm " + cmd.getVmName(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @Override public RebootAnswer rebootVM(RebootCommand cmd) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); MockVm vm = _mockVmDao.findByVmName(cmd.getVmName()); @@ -545,7 +545,7 @@ public class MockVmManagerImpl extends ManagerBase implements MockVmManager { throw new CloudRuntimeException("unable to stop vm " + cmd.getVmName(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } diff --git a/plugins/hypervisors/simulator/src/com/cloud/agent/manager/SimulatorManagerImpl.java b/plugins/hypervisors/simulator/src/com/cloud/agent/manager/SimulatorManagerImpl.java index f6bd2b651ba..f30bd3661d0 100644 --- a/plugins/hypervisors/simulator/src/com/cloud/agent/manager/SimulatorManagerImpl.java +++ b/plugins/hypervisors/simulator/src/com/cloud/agent/manager/SimulatorManagerImpl.java @@ -99,7 +99,7 @@ import com.cloud.utils.Pair; import com.cloud.utils.component.ManagerBase; import com.cloud.utils.component.PluggableService; import com.cloud.utils.db.DB; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.VirtualMachine.State; import org.apache.cloudstack.storage.command.DeleteCommand; @@ -182,7 +182,7 @@ public class SimulatorManagerImpl extends ManagerBase implements SimulatorManage @DB @Override public Answer simulate(Command cmd, String hostGuid) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { MockHost host = _mockHost.findByGuid(hostGuid); String cmdName = cmd.toString(); @@ -373,7 +373,7 @@ public class SimulatorManagerImpl extends ManagerBase implements SimulatorManage return new Answer(cmd, false, e.toString()); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } } @@ -403,7 +403,7 @@ public class SimulatorManagerImpl extends ManagerBase implements SimulatorManage @Override public boolean configureSimulator(Long zoneId, Long podId, Long clusterId, Long hostId, String command, String values) { - Transaction txn = Transaction.open(Transaction.SIMULATOR_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB); try { txn.start(); MockConfigurationVO config = _mockConfigDao.findByCommand(zoneId, podId, clusterId, hostId, command); @@ -427,7 +427,7 @@ public class SimulatorManagerImpl extends ManagerBase implements SimulatorManage throw new CloudRuntimeException("Unable to configure simulator because of " + ex.getMessage(), ex); } finally { txn.close(); - txn = Transaction.open(Transaction.CLOUD_DB); + txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); } return true; diff --git a/plugins/hypervisors/simulator/src/com/cloud/simulator/dao/MockConfigurationDaoImpl.java b/plugins/hypervisors/simulator/src/com/cloud/simulator/dao/MockConfigurationDaoImpl.java index fd825b751ed..a681c061a28 100644 --- a/plugins/hypervisors/simulator/src/com/cloud/simulator/dao/MockConfigurationDaoImpl.java +++ b/plugins/hypervisors/simulator/src/com/cloud/simulator/dao/MockConfigurationDaoImpl.java @@ -20,7 +20,7 @@ import com.cloud.simulator.MockConfigurationVO; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import org.springframework.stereotype.Component; import javax.ejb.Local; @@ -117,7 +117,7 @@ public class MockConfigurationDaoImpl extends GenericDaoBase validateAndAddVsm(String vsmIp, String vsmUser, String vsmPassword, long clusterId, String clusterName) throws ResourceInUseException { + public Pair validateAndAddVsm(final String vsmIp, final String vsmUser, final String vsmPassword, final long clusterId, String clusterName) throws ResourceInUseException { CiscoNexusVSMDeviceVO vsm = null; boolean vsmAdded = false; Long vsmId = 0L; @@ -293,36 +295,24 @@ public class CiscoNexusVSMElement extends CiscoNexusVSMDeviceManagerImpl impleme } } // persist credentials to database if the VSM entry is not already in the db. - if (_vsmDao.getVSMbyIpaddress(vsmIp) == null) { - vsm = new CiscoNexusVSMDeviceVO(vsmIp, vsmUser, vsmPassword); - txn = Transaction.currentTxn(); - try { - txn.start(); - vsm = _vsmDao.persist(vsm); - txn.commit(); - } catch (Exception e) { - txn.rollback(); - s_logger.error("Failed to persist Cisco Nexus 1000v VSM details to database. Exception: " + e.getMessage()); - throw new CloudRuntimeException(e.getMessage()); + vsm = Transaction.execute(new TransactionCallback() { + @Override + public CiscoNexusVSMDeviceVO doInTransaction(TransactionStatus status) { + CiscoNexusVSMDeviceVO vsm = null; + if (_vsmDao.getVSMbyIpaddress(vsmIp) == null) { + vsm = new CiscoNexusVSMDeviceVO(vsmIp, vsmUser, vsmPassword); + vsm = _vsmDao.persist(vsm); + } + // Create a mapping between the cluster and the vsm. + vsm = _vsmDao.getVSMbyIpaddress(vsmIp); + if (vsm != null) { + ClusterVSMMapVO connectorObj = new ClusterVSMMapVO(clusterId, vsm.getId()); + _clusterVSMDao.persist(connectorObj); + } + return vsm; } - } - // Create a mapping between the cluster and the vsm. - vsm = _vsmDao.getVSMbyIpaddress(vsmIp); - if (vsm != null) { - ClusterVSMMapVO connectorObj = new ClusterVSMMapVO(clusterId, vsm.getId()); - txn = Transaction.currentTxn(); - try { - txn.start(); - _clusterVSMDao.persist(connectorObj); - txn.commit(); - } catch (Exception e) { - txn.rollback(); - s_logger.error("Failed to associate Cisco Nexus 1000v VSM with cluster: " + clusterName + ". Exception: " + e.getMessage()); - _vsmDao.remove(vsm.getId()); // Removing VSM from virtual_supervisor_module table because association with cluster failed. - // Cluster would be deleted from cluster table by callee. - throw new CloudRuntimeException(e.getMessage()); - } - } + }); + } else { String msg; msg = "The global parameter " + Config.VmwareUseNexusVSwitch.toString() + diff --git a/plugins/network-elements/bigswitch-vns/src/com/cloud/network/element/BigSwitchVnsElement.java b/plugins/network-elements/bigswitch-vns/src/com/cloud/network/element/BigSwitchVnsElement.java index cb6e7fcec61..6b75634f653 100644 --- a/plugins/network-elements/bigswitch-vns/src/com/cloud/network/element/BigSwitchVnsElement.java +++ b/plugins/network-elements/bigswitch-vns/src/com/cloud/network/element/BigSwitchVnsElement.java @@ -29,7 +29,6 @@ import javax.naming.ConfigurationException; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; - import org.apache.cloudstack.network.ExternalNetworkDeviceManager.NetworkDevice; import com.cloud.agent.AgentManager; @@ -84,6 +83,9 @@ import com.cloud.resource.UnableDeleteHostException; import com.cloud.utils.component.AdapterBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.NicProfile; import com.cloud.vm.ReservationContext; @@ -329,11 +331,10 @@ public class BigSwitchVnsElement extends AdapterBase implements @DB public BigSwitchVnsDeviceVO addBigSwitchVnsDevice(AddBigSwitchVnsDeviceCmd cmd) { ServerResource resource = new BigSwitchVnsResource(); - String deviceName = VnsConstants.BigSwitchVns.getName(); + final String deviceName = VnsConstants.BigSwitchVns.getName(); NetworkDevice networkDevice = NetworkDevice .getNetworkDevice(deviceName); - Long physicalNetworkId = cmd.getPhysicalNetworkId(); - BigSwitchVnsDeviceVO bigswitchVnsDevice = null; + final Long physicalNetworkId = cmd.getPhysicalNetworkId(); PhysicalNetworkVO physicalNetwork = _physicalNetworkDao .findById(physicalNetworkId); @@ -344,7 +345,7 @@ public class BigSwitchVnsElement extends AdapterBase implements } long zoneId = physicalNetwork.getDataCenterId(); - PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao + final PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao .findByServiceProvider(physicalNetwork.getId(), networkDevice.getNetworkServiceProvder()); if (ntwkSvcProvider == null) { @@ -377,33 +378,33 @@ public class BigSwitchVnsElement extends AdapterBase implements Map hostdetails = new HashMap(); hostdetails.putAll(params); - Transaction txn = Transaction.currentTxn(); try { resource.configure(cmd.getHost(), hostdetails); - Host host = _resourceMgr.addHost(zoneId, resource, + final Host host = _resourceMgr.addHost(zoneId, resource, Host.Type.L2Networking, params); if (host != null) { - txn.start(); - - bigswitchVnsDevice = new BigSwitchVnsDeviceVO(host.getId(), - physicalNetworkId, ntwkSvcProvider.getProviderName(), - deviceName); - _bigswitchVnsDao.persist(bigswitchVnsDevice); - - DetailVO detail = new DetailVO(host.getId(), - "bigswitchvnsdeviceid", - String.valueOf(bigswitchVnsDevice.getId())); - _hostDetailsDao.persist(detail); - - txn.commit(); - return bigswitchVnsDevice; + return Transaction.execute(new TransactionCallback() { + @Override + public BigSwitchVnsDeviceVO doInTransaction(TransactionStatus status) { + BigSwitchVnsDeviceVO bigswitchVnsDevice = new BigSwitchVnsDeviceVO(host.getId(), + physicalNetworkId, ntwkSvcProvider.getProviderName(), + deviceName); + _bigswitchVnsDao.persist(bigswitchVnsDevice); + + DetailVO detail = new DetailVO(host.getId(), + "bigswitchvnsdeviceid", + String.valueOf(bigswitchVnsDevice.getId())); + _hostDetailsDao.persist(detail); + + return bigswitchVnsDevice; + } + }); } else { throw new CloudRuntimeException( "Failed to add BigSwitch Vns Device due to internal error."); } } catch (ConfigurationException e) { - txn.rollback(); throw new CloudRuntimeException(e.getMessage()); } } diff --git a/plugins/network-elements/cisco-vnmc/src/com/cloud/network/element/CiscoVnmcElement.java b/plugins/network-elements/cisco-vnmc/src/com/cloud/network/element/CiscoVnmcElement.java index e27a0599b0b..5beb1bae520 100644 --- a/plugins/network-elements/cisco-vnmc/src/com/cloud/network/element/CiscoVnmcElement.java +++ b/plugins/network-elements/cisco-vnmc/src/com/cloud/network/element/CiscoVnmcElement.java @@ -29,7 +29,6 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; - import org.apache.cloudstack.context.CallContext; import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; import org.apache.cloudstack.network.ExternalNetworkDeviceManager.NetworkDevice; @@ -71,6 +70,7 @@ import com.cloud.dc.dao.ClusterVSMMapDao; import com.cloud.dc.dao.VlanDao; import com.cloud.deploy.DeployDestination; import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.InsufficientAddressCapacityException; import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.InvalidParameterValueException; import com.cloud.exception.ResourceAllocationException; @@ -123,7 +123,11 @@ import com.cloud.user.Account; import com.cloud.utils.component.AdapterBase; import com.cloud.utils.db.EntityManager; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.exception.ExceptionUtil; import com.cloud.utils.net.NetUtils; import com.cloud.vm.NicProfile; import com.cloud.vm.ReservationContext; @@ -274,11 +278,11 @@ public class CiscoVnmcElement extends AdapterBase implements SourceNatServicePro } @Override - public boolean implement(Network network, NetworkOffering offering, - DeployDestination dest, ReservationContext context) + public boolean implement(final Network network, final NetworkOffering offering, + final DeployDestination dest, final ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { - DataCenter zone = _entityMgr.findById(DataCenter.class, network.getDataCenterId()); + final DataCenter zone = _entityMgr.findById(DataCenter.class, network.getDataCenterId()); if (zone.getNetworkType() == NetworkType.Basic) { s_logger.debug("Not handling network implement in zone of type " + NetworkType.Basic); @@ -289,7 +293,7 @@ public class CiscoVnmcElement extends AdapterBase implements SourceNatServicePro return false; } - List devices = _ciscoVnmcDao.listByPhysicalNetwork(network.getPhysicalNetworkId()); + final List devices = _ciscoVnmcDao.listByPhysicalNetwork(network.getPhysicalNetworkId()); if (devices.isEmpty()) { s_logger.error("No Cisco Vnmc device on network " + network.getName()); return false; @@ -312,114 +316,119 @@ public class CiscoVnmcElement extends AdapterBase implements SourceNatServicePro return false; } - Transaction txn = Transaction.currentTxn(); - boolean status = false; try { - txn.start(); + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws InsufficientAddressCapacityException, ResourceUnavailableException { - // ensure that there is an ASA 1000v assigned to this network - CiscoAsa1000vDevice assignedAsa = assignAsa1000vToNetwork(network); - if (assignedAsa == null) { - s_logger.error("Unable to assign ASA 1000v device to network " + network.getName()); - return false; - } - - ClusterVO asaCluster = _clusterDao.findById(assignedAsa.getClusterId()); - ClusterVSMMapVO clusterVsmMap = _clusterVsmMapDao.findByClusterId(assignedAsa.getClusterId()); - if (clusterVsmMap == null) { - s_logger.error("Vmware cluster " + asaCluster.getName() + " has no Cisco Nexus VSM device associated with it"); - return false; - } - - CiscoNexusVSMDeviceVO vsmDevice = _vsmDeviceDao.findById(clusterVsmMap.getVsmId()); - if (vsmDevice == null) { - s_logger.error("Unable to load details of Cisco Nexus VSM device associated with cluster " + asaCluster.getName()); - return false; - } - - CiscoVnmcControllerVO ciscoVnmcDevice = devices.get(0); - HostVO ciscoVnmcHost = _hostDao.findById(ciscoVnmcDevice.getHostId()); - _hostDao.loadDetails(ciscoVnmcHost); - Account owner = context.getAccount(); - PublicIp sourceNatIp = _ipAddrMgr.assignSourceNatIpAddressToGuestNetwork(owner, network); - long vlanId = Long.parseLong(BroadcastDomainType.getValue(network.getBroadcastUri())); - - List vlanVOList = _vlanDao.listVlansByPhysicalNetworkId(network.getPhysicalNetworkId()); - List publicGateways = new ArrayList(); - for (VlanVO vlanVO : vlanVOList) { - publicGateways.add(vlanVO.getVlanGateway()); - } - - // due to VNMC limitation of not allowing source NAT ip as the outside ip of firewall, - // an additional public ip needs to acquired for assigning as firewall outside ip. - // In case there are already additional ip addresses available (network restart) use one - // of them such that it is not the source NAT ip - IpAddress outsideIp = null; - List publicIps = _ipAddressDao.listByAssociatedNetwork(network.getId(), null); - for (IPAddressVO ip : publicIps) { - if (!ip.isSourceNat()) { - outsideIp = ip; - break; + // ensure that there is an ASA 1000v assigned to this network + CiscoAsa1000vDevice assignedAsa = assignAsa1000vToNetwork(network); + if (assignedAsa == null) { + s_logger.error("Unable to assign ASA 1000v device to network " + network.getName()); + throw new CloudRuntimeException("Unable to assign ASA 1000v device to network " + network.getName()); + } + + ClusterVO asaCluster = _clusterDao.findById(assignedAsa.getClusterId()); + ClusterVSMMapVO clusterVsmMap = _clusterVsmMapDao.findByClusterId(assignedAsa.getClusterId()); + if (clusterVsmMap == null) { + s_logger.error("Vmware cluster " + asaCluster.getName() + " has no Cisco Nexus VSM device associated with it"); + throw new CloudRuntimeException("Vmware cluster " + asaCluster.getName() + " has no Cisco Nexus VSM device associated with it"); + } + + CiscoNexusVSMDeviceVO vsmDevice = _vsmDeviceDao.findById(clusterVsmMap.getVsmId()); + if (vsmDevice == null) { + s_logger.error("Unable to load details of Cisco Nexus VSM device associated with cluster " + asaCluster.getName()); + throw new CloudRuntimeException("Unable to load details of Cisco Nexus VSM device associated with cluster " + asaCluster.getName()); + } + + CiscoVnmcControllerVO ciscoVnmcDevice = devices.get(0); + HostVO ciscoVnmcHost = _hostDao.findById(ciscoVnmcDevice.getHostId()); + _hostDao.loadDetails(ciscoVnmcHost); + Account owner = context.getAccount(); + PublicIp sourceNatIp = _ipAddrMgr.assignSourceNatIpAddressToGuestNetwork(owner, network); + long vlanId = Long.parseLong(BroadcastDomainType.getValue(network.getBroadcastUri())); + + List vlanVOList = _vlanDao.listVlansByPhysicalNetworkId(network.getPhysicalNetworkId()); + List publicGateways = new ArrayList(); + for (VlanVO vlanVO : vlanVOList) { + publicGateways.add(vlanVO.getVlanGateway()); + } + + // due to VNMC limitation of not allowing source NAT ip as the outside ip of firewall, + // an additional public ip needs to acquired for assigning as firewall outside ip. + // In case there are already additional ip addresses available (network restart) use one + // of them such that it is not the source NAT ip + IpAddress outsideIp = null; + List publicIps = _ipAddressDao.listByAssociatedNetwork(network.getId(), null); + for (IPAddressVO ip : publicIps) { + if (!ip.isSourceNat()) { + outsideIp = ip; + break; + } + } + if (outsideIp == null) { // none available, acquire one + try { + Account caller = CallContext.current().getCallingAccount(); + long callerUserId = CallContext.current().getCallingUserId(); + outsideIp = _ipAddrMgr.allocateIp(owner, false, caller, callerUserId, zone); + } catch (ResourceAllocationException e) { + s_logger.error("Unable to allocate additional public Ip address. Exception details " + e); + throw new CloudRuntimeException("Unable to allocate additional public Ip address. Exception details " + e); + } + + try { + outsideIp = _ipAddrMgr.associateIPToGuestNetwork(outsideIp.getId(), network.getId(), true); + } catch (ResourceAllocationException e) { + s_logger.error("Unable to assign allocated additional public Ip " + outsideIp.getAddress().addr() + " to network with vlan " + vlanId + ". Exception details " + + e); + throw new CloudRuntimeException("Unable to assign allocated additional public Ip " + outsideIp.getAddress().addr() + " to network with vlan " + vlanId + ". Exception details " + + e); + } + } + + // create logical edge firewall in VNMC + String gatewayNetmask = NetUtils.getCidrNetmask(network.getCidr()); + // due to ASA limitation of allowing single subnet to be assigned to firewall interfaces, + // all public ip addresses must be from same subnet, this essentially means single public subnet in zone + if (!createLogicalEdgeFirewall(vlanId, network.getGateway(), gatewayNetmask, + outsideIp.getAddress().addr(), sourceNatIp.getNetmask(), publicGateways, ciscoVnmcHost.getId())) { + s_logger.error("Failed to create logical edge firewall in Cisco VNMC device for network " + network.getName()); + throw new CloudRuntimeException("Failed to create logical edge firewall in Cisco VNMC device for network " + network.getName()); + } + + // create stuff in VSM for ASA device + if (!configureNexusVsmForAsa(vlanId, network.getGateway(), + vsmDevice.getUserName(), vsmDevice.getPassword(), vsmDevice.getipaddr(), + assignedAsa.getInPortProfile(), ciscoVnmcHost.getId())) { + s_logger.error("Failed to configure Cisco Nexus VSM " + vsmDevice.getipaddr() + + " for ASA device for network " + network.getName()); + throw new CloudRuntimeException("Failed to configure Cisco Nexus VSM " + vsmDevice.getipaddr() + + " for ASA device for network " + network.getName()); + } + + // configure source NAT + if (!configureSourceNat(vlanId, network.getCidr(), sourceNatIp, ciscoVnmcHost.getId())) { + s_logger.error("Failed to configure source NAT in Cisco VNMC device for network " + network.getName()); + throw new CloudRuntimeException("Failed to configure source NAT in Cisco VNMC device for network " + network.getName()); + } + + // associate Asa 1000v instance with logical edge firewall + if (!associateAsaWithLogicalEdgeFirewall(vlanId, assignedAsa.getManagementIp(), ciscoVnmcHost.getId())) { + s_logger.error("Failed to associate Cisco ASA 1000v (" + assignedAsa.getManagementIp() + + ") with logical edge firewall in VNMC for network " + network.getName()); + throw new CloudRuntimeException("Failed to associate Cisco ASA 1000v (" + assignedAsa.getManagementIp() + + ") with logical edge firewall in VNMC for network " + network.getName()); + } } - } - if (outsideIp == null) { // none available, acquire one - try { - Account caller = CallContext.current().getCallingAccount(); - long callerUserId = CallContext.current().getCallingUserId(); - outsideIp = _ipAddrMgr.allocateIp(owner, false, caller, callerUserId, zone); - } catch (ResourceAllocationException e) { - s_logger.error("Unable to allocate additional public Ip address. Exception details " + e); - return false; - } - - try { - outsideIp = _ipAddrMgr.associateIPToGuestNetwork(outsideIp.getId(), network.getId(), true); - } catch (ResourceAllocationException e) { - s_logger.error("Unable to assign allocated additional public Ip " + outsideIp.getAddress().addr() + " to network with vlan " + vlanId + ". Exception details " - + e); - return false; - } - } - - // create logical edge firewall in VNMC - String gatewayNetmask = NetUtils.getCidrNetmask(network.getCidr()); - // due to ASA limitation of allowing single subnet to be assigned to firewall interfaces, - // all public ip addresses must be from same subnet, this essentially means single public subnet in zone - if (!createLogicalEdgeFirewall(vlanId, network.getGateway(), gatewayNetmask, - outsideIp.getAddress().addr(), sourceNatIp.getNetmask(), publicGateways, ciscoVnmcHost.getId())) { - s_logger.error("Failed to create logical edge firewall in Cisco VNMC device for network " + network.getName()); - return false; - } - - // create stuff in VSM for ASA device - if (!configureNexusVsmForAsa(vlanId, network.getGateway(), - vsmDevice.getUserName(), vsmDevice.getPassword(), vsmDevice.getipaddr(), - assignedAsa.getInPortProfile(), ciscoVnmcHost.getId())) { - s_logger.error("Failed to configure Cisco Nexus VSM " + vsmDevice.getipaddr() + - " for ASA device for network " + network.getName()); - return false; - } - - // configure source NAT - if (!configureSourceNat(vlanId, network.getCidr(), sourceNatIp, ciscoVnmcHost.getId())) { - s_logger.error("Failed to configure source NAT in Cisco VNMC device for network " + network.getName()); - return false; - } - - // associate Asa 1000v instance with logical edge firewall - if (!associateAsaWithLogicalEdgeFirewall(vlanId, assignedAsa.getManagementIp(), ciscoVnmcHost.getId())) { - s_logger.error("Failed to associate Cisco ASA 1000v (" + assignedAsa.getManagementIp() + - ") with logical edge firewall in VNMC for network " + network.getName()); - return false; - } - - status = true; - txn.commit(); - } finally { - if (!status) { - txn.rollback(); - //FIXME: also undo changes in VNMC, VSM if anything failed - } + }); + } catch (CloudRuntimeException e) { + s_logger.error("CiscoVnmcElement failed", e); + return false; + } catch (Exception e) { + ExceptionUtil.rethrowRuntime(e); + ExceptionUtil.rethrow(e, InsufficientAddressCapacityException.class); + ExceptionUtil.rethrow(e, ResourceUnavailableException.class); + throw new IllegalStateException(e); } return true; @@ -522,9 +531,9 @@ public class CiscoVnmcElement extends AdapterBase implements SourceNatServicePro @Override public CiscoVnmcController addCiscoVnmcResource(AddCiscoVnmcResourceCmd cmd) { - String deviceName = Provider.CiscoVnmc.getName(); + final String deviceName = Provider.CiscoVnmc.getName(); NetworkDevice networkDevice = NetworkDevice.getNetworkDevice(deviceName); - Long physicalNetworkId = cmd.getPhysicalNetworkId(); + final Long physicalNetworkId = cmd.getPhysicalNetworkId(); CiscoVnmcController ciscoVnmcResource = null; PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId); @@ -533,7 +542,7 @@ public class CiscoVnmcElement extends AdapterBase implements SourceNatServicePro } long zoneId = physicalNetwork.getDataCenterId(); - PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao.findByServiceProvider(physicalNetwork.getId(), + final PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao.findByServiceProvider(physicalNetwork.getId(), networkDevice.getNetworkServiceProvder()); if (ntwkSvcProvider == null) { throw new CloudRuntimeException("Network Service Provider: " + networkDevice.getNetworkServiceProvder() + @@ -560,27 +569,27 @@ public class CiscoVnmcElement extends AdapterBase implements SourceNatServicePro hostdetails.putAll(params); ServerResource resource = new CiscoVnmcResource(); - Transaction txn = Transaction.currentTxn(); try { resource.configure(cmd.getHost(), hostdetails); - Host host = _resourceMgr.addHost(zoneId, resource, Host.Type.ExternalFirewall, params); + final Host host = _resourceMgr.addHost(zoneId, resource, Host.Type.ExternalFirewall, params); if (host != null) { - txn.start(); - - ciscoVnmcResource = new CiscoVnmcControllerVO(host.getId(), physicalNetworkId, ntwkSvcProvider.getProviderName(), deviceName); - _ciscoVnmcDao.persist((CiscoVnmcControllerVO)ciscoVnmcResource); - - DetailVO detail = new DetailVO(host.getId(), "deviceid", String.valueOf(ciscoVnmcResource.getId())); - _hostDetailsDao.persist(detail); - - txn.commit(); - return ciscoVnmcResource; + return Transaction.execute(new TransactionCallback() { + @Override + public CiscoVnmcController doInTransaction(TransactionStatus status) { + CiscoVnmcController ciscoVnmcResource = new CiscoVnmcControllerVO(host.getId(), physicalNetworkId, ntwkSvcProvider.getProviderName(), deviceName); + _ciscoVnmcDao.persist((CiscoVnmcControllerVO)ciscoVnmcResource); + + DetailVO detail = new DetailVO(host.getId(), "deviceid", String.valueOf(ciscoVnmcResource.getId())); + _hostDetailsDao.persist(detail); + + return ciscoVnmcResource; + } + }); } else { throw new CloudRuntimeException("Failed to add Cisco Vnmc device due to internal error."); } } catch (ConfigurationException e) { - txn.rollback(); throw new CloudRuntimeException(e.getMessage()); } } diff --git a/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java b/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java index ab414de7eef..5c6f2e7dd63 100644 --- a/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java +++ b/plugins/network-elements/elastic-loadbalancer/src/com/cloud/network/lb/ElasticLoadBalancerManagerImpl.java @@ -123,6 +123,10 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.Ip; import com.cloud.vm.DomainRouterVO; @@ -582,19 +586,21 @@ public class ElasticLoadBalancerManagerImpl extends ManagerBase implements Elast } @DB - public PublicIp allocDirectIp(Account account, long guestNetworkId) throws InsufficientAddressCapacityException { - Network frontEndNetwork = _networkModel.getNetwork(guestNetworkId); - Transaction txn = Transaction.currentTxn(); - txn.start(); - - PublicIp ip = _ipAddrMgr.assignPublicIpAddress(frontEndNetwork.getDataCenterId(), null, account, VlanType.DirectAttached, frontEndNetwork.getId(), null, true); - IPAddressVO ipvo = _ipAddressDao.findById(ip.getId()); - ipvo.setAssociatedWithNetworkId(frontEndNetwork.getId()); - _ipAddressDao.update(ipvo.getId(), ipvo); - txn.commit(); - s_logger.info("Acquired frontend IP for ELB " + ip); + public PublicIp allocDirectIp(final Account account, final long guestNetworkId) throws InsufficientAddressCapacityException { + return Transaction.execute(new TransactionCallbackWithException() { + @Override + public PublicIp doInTransaction(TransactionStatus status) throws InsufficientAddressCapacityException { + Network frontEndNetwork = _networkModel.getNetwork(guestNetworkId); - return ip; + PublicIp ip = _ipAddrMgr.assignPublicIpAddress(frontEndNetwork.getDataCenterId(), null, account, VlanType.DirectAttached, frontEndNetwork.getId(), null, true); + IPAddressVO ipvo = _ipAddressDao.findById(ip.getId()); + ipvo.setAssociatedWithNetworkId(frontEndNetwork.getId()); + _ipAddressDao.update(ipvo.getId(), ipvo); + s_logger.info("Acquired frontend IP for ELB " + ip); + + return ip; + } + }); } public void releaseIp(long ipId, long userId, Account caller) { diff --git a/plugins/network-elements/midonet/src/com/cloud/network/guru/MidoNetPublicNetworkGuru.java b/plugins/network-elements/midonet/src/com/cloud/network/guru/MidoNetPublicNetworkGuru.java index eccf4aa3944..c11c8719496 100644 --- a/plugins/network-elements/midonet/src/com/cloud/network/guru/MidoNetPublicNetworkGuru.java +++ b/plugins/network-elements/midonet/src/com/cloud/network/guru/MidoNetPublicNetworkGuru.java @@ -46,6 +46,8 @@ import com.cloud.user.AccountVO; import com.cloud.user.dao.AccountDao; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.Nic; import com.cloud.vm.NicProfile; @@ -206,16 +208,15 @@ public class MidoNetPublicNetworkGuru extends PublicNetworkGuru { s_logger.debug("public network deallocate network: networkId: " + nic.getNetworkId() + ", ip: " + nic.getIp4Address()); } - IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), nic.getIp4Address()); + final IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), nic.getIp4Address()); if (ip != null && nic.getReservationStrategy() != Nic.ReservationStrategy.Managed) { - - Transaction txn = Transaction.currentTxn(); - txn.start(); - - _ipAddrMgr.markIpAsUnavailable(ip.getId()); - _ipAddressDao.unassignIpAddress(ip.getId()); - - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _ipAddrMgr.markIpAsUnavailable(ip.getId()); + _ipAddressDao.unassignIpAddress(ip.getId()); + } + }); } nic.deallocate(); diff --git a/plugins/network-elements/netscaler/src/com/cloud/network/element/NetscalerElement.java b/plugins/network-elements/netscaler/src/com/cloud/network/element/NetscalerElement.java index d63b14f8a58..8101864840b 100644 --- a/plugins/network-elements/netscaler/src/com/cloud/network/element/NetscalerElement.java +++ b/plugins/network-elements/netscaler/src/com/cloud/network/element/NetscalerElement.java @@ -31,7 +31,6 @@ import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.framework.config.dao.ConfigurationDao; import org.apache.cloudstack.network.ExternalNetworkDeviceManager.NetworkDevice; import org.apache.cloudstack.region.gslb.GslbServiceProvider; - import org.apache.log4j.Logger; import com.cloud.agent.AgentManager; @@ -110,13 +109,14 @@ import com.cloud.offering.NetworkOffering; import com.cloud.utils.NumbersUtil; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.UrlUtil; import com.cloud.vm.NicProfile; import com.cloud.vm.ReservationContext; import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachineProfile; - import com.google.gson.Gson; @Local(value = {NetworkElement.class, StaticNatServiceProvider.class, LoadBalancingServiceProvider.class, GslbServiceProvider.class}) @@ -409,9 +409,9 @@ public class NetscalerElement extends ExternalLoadBalancerDeviceManagerImpl impl } @DB - private ExternalLoadBalancerDeviceVO configureNetscalerLoadBalancer(long lbDeviceId, Long capacity, Boolean dedicatedUse, List newPodsConfig) { - ExternalLoadBalancerDeviceVO lbDeviceVo = _lbDeviceDao.findById(lbDeviceId); - Map lbDetails = _detailsDao.findDetails(lbDeviceVo.getHostId()); + private ExternalLoadBalancerDeviceVO configureNetscalerLoadBalancer(final long lbDeviceId, Long capacity, Boolean dedicatedUse, List newPodsConfig) { + final ExternalLoadBalancerDeviceVO lbDeviceVo = _lbDeviceDao.findById(lbDeviceId); + final Map lbDetails = _detailsDao.findDetails(lbDeviceVo.getHostId()); if ((lbDeviceVo == null) || !isNetscalerDevice(lbDeviceVo.getDeviceName())) { throw new InvalidParameterValueException("No netscaler device found with ID: " + lbDeviceId); @@ -425,7 +425,7 @@ public class NetscalerElement extends ExternalLoadBalancerDeviceManagerImpl impl } } - List podsToAssociate = new ArrayList(); + final List podsToAssociate = new ArrayList(); if (newPodsConfig != null && newPodsConfig.size() > 0) { for (Long podId: newPodsConfig) { HostPodVO pod = _podDao.findById(podId); @@ -441,7 +441,7 @@ public class NetscalerElement extends ExternalLoadBalancerDeviceManagerImpl impl } } - List podsToDeassociate = new ArrayList(); + final List podsToDeassociate = new ArrayList(); for (Long podId: currentPodsConfig) { if (!newPodsConfig.contains(podId)) { podsToDeassociate.add(podId); @@ -482,26 +482,28 @@ public class NetscalerElement extends ExternalLoadBalancerDeviceManagerImpl impl lbDeviceVo.setIsDedicatedDevice(dedicatedUse); } - Transaction txn = Transaction.currentTxn(); - txn.start(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _lbDeviceDao.update(lbDeviceId, lbDeviceVo); + + for (Long podId: podsToAssociate) { + NetScalerPodVO nsPodVo = new NetScalerPodVO(lbDeviceId, podId); + _netscalerPodDao.persist(nsPodVo); + } + + for (Long podId: podsToDeassociate) { + NetScalerPodVO nsPodVo = _netscalerPodDao.findByPodId(podId); + _netscalerPodDao.remove(nsPodVo.getId()); + } + + // FIXME get the row lock to avoid race condition + _detailsDao.persist(lbDeviceVo.getHostId(), lbDetails); - _lbDeviceDao.update(lbDeviceId, lbDeviceVo); - - for (Long podId: podsToAssociate) { - NetScalerPodVO nsPodVo = new NetScalerPodVO(lbDeviceId, podId); - _netscalerPodDao.persist(nsPodVo); - } - - for (Long podId: podsToDeassociate) { - NetScalerPodVO nsPodVo = _netscalerPodDao.findByPodId(podId); - _netscalerPodDao.remove(nsPodVo.getId()); - } - - // FIXME get the row lock to avoid race condition - _detailsDao.persist(lbDeviceVo.getHostId(), lbDetails); + } + }); HostVO host = _hostDao.findById(lbDeviceVo.getHostId()); - txn.commit(); - + _agentMgr.reconnect(host.getId()); return lbDeviceVo; } diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/element/NiciraNvpElement.java b/plugins/network-elements/nicira-nvp/src/com/cloud/network/element/NiciraNvpElement.java index 5400dd4137c..ef8ec863c9a 100644 --- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/element/NiciraNvpElement.java +++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/element/NiciraNvpElement.java @@ -30,7 +30,6 @@ import javax.naming.ConfigurationException; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; - import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; import org.apache.cloudstack.network.ExternalNetworkDeviceManager.NetworkDevice; @@ -114,6 +113,8 @@ import com.cloud.user.Account; import com.cloud.utils.component.AdapterBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; import com.cloud.vm.NicProfile; @@ -552,12 +553,10 @@ public class NiciraNvpElement extends AdapterBase implements @DB public NiciraNvpDeviceVO addNiciraNvpDevice(AddNiciraNvpDeviceCmd cmd) { ServerResource resource = new NiciraNvpResource(); - String deviceName = Network.Provider.NiciraNvp.getName(); + final String deviceName = Network.Provider.NiciraNvp.getName(); NetworkDevice networkDevice = NetworkDevice .getNetworkDevice(deviceName); - Long physicalNetworkId = cmd.getPhysicalNetworkId(); - NiciraNvpDeviceVO niciraNvpDevice = null; - + final Long physicalNetworkId = cmd.getPhysicalNetworkId(); PhysicalNetworkVO physicalNetwork = _physicalNetworkDao .findById(physicalNetworkId); if (physicalNetwork == null) { @@ -567,7 +566,7 @@ public class NiciraNvpElement extends AdapterBase implements } long zoneId = physicalNetwork.getDataCenterId(); - PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao + final PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao .findByServiceProvider(physicalNetwork.getId(), networkDevice.getNetworkServiceProvder()); if (ntwkSvcProvider == null) { @@ -606,33 +605,33 @@ public class NiciraNvpElement extends AdapterBase implements Map hostdetails = new HashMap(); hostdetails.putAll(params); - Transaction txn = Transaction.currentTxn(); try { resource.configure(cmd.getHost(), hostdetails); - Host host = _resourceMgr.addHost(zoneId, resource, + final Host host = _resourceMgr.addHost(zoneId, resource, Host.Type.L2Networking, params); if (host != null) { - txn.start(); + return Transaction.execute(new TransactionCallback() { + @Override + public NiciraNvpDeviceVO doInTransaction(TransactionStatus status) { + NiciraNvpDeviceVO niciraNvpDevice = new NiciraNvpDeviceVO(host.getId(), + physicalNetworkId, ntwkSvcProvider.getProviderName(), + deviceName); + _niciraNvpDao.persist(niciraNvpDevice); + + DetailVO detail = new DetailVO(host.getId(), + "niciranvpdeviceid", String.valueOf(niciraNvpDevice + .getId())); + _hostDetailsDao.persist(detail); - niciraNvpDevice = new NiciraNvpDeviceVO(host.getId(), - physicalNetworkId, ntwkSvcProvider.getProviderName(), - deviceName); - _niciraNvpDao.persist(niciraNvpDevice); - - DetailVO detail = new DetailVO(host.getId(), - "niciranvpdeviceid", String.valueOf(niciraNvpDevice - .getId())); - _hostDetailsDao.persist(detail); - - txn.commit(); - return niciraNvpDevice; + return niciraNvpDevice; + } + }); } else { throw new CloudRuntimeException( "Failed to add Nicira Nvp Device due to internal error."); } } catch (ConfigurationException e) { - txn.rollback(); throw new CloudRuntimeException(e.getMessage()); } } diff --git a/plugins/storage/volume/solidfire/test/org/apache/cloudstack/storage/test/AopTestAdvice.java b/plugins/storage/volume/solidfire/test/org/apache/cloudstack/storage/test/AopTestAdvice.java index 902f5953eb1..233b0461150 100644 --- a/plugins/storage/volume/solidfire/test/org/apache/cloudstack/storage/test/AopTestAdvice.java +++ b/plugins/storage/volume/solidfire/test/org/apache/cloudstack/storage/test/AopTestAdvice.java @@ -18,11 +18,11 @@ package org.apache.cloudstack.storage.test; import org.aspectj.lang.ProceedingJoinPoint; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; public class AopTestAdvice { public Object AopTestMethod(ProceedingJoinPoint call) throws Throwable { - Transaction txn = Transaction.open(call.getSignature().getName()); + TransactionLegacy txn = TransactionLegacy.open(call.getSignature().getName()); Object ret = null; try { ret = call.proceed(); diff --git a/server/src/com/cloud/api/ApiServer.java b/server/src/com/cloud/api/ApiServer.java index b73045d5fce..aedf68b9457 100755 --- a/server/src/com/cloud/api/ApiServer.java +++ b/server/src/com/cloud/api/ApiServer.java @@ -44,7 +44,6 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import javax.annotation.PostConstruct; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import javax.inject.Inject; @@ -52,37 +51,6 @@ import javax.naming.ConfigurationException; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; -import org.apache.commons.codec.binary.Base64; -import org.apache.http.ConnectionClosedException; -import org.apache.http.HttpException; -import org.apache.http.HttpRequest; -import org.apache.http.HttpResponse; -import org.apache.http.HttpServerConnection; -import org.apache.http.HttpStatus; -import org.apache.http.NameValuePair; -import org.apache.http.client.utils.URLEncodedUtils; -import org.apache.http.entity.BasicHttpEntity; -import org.apache.http.impl.DefaultHttpResponseFactory; -import org.apache.http.impl.DefaultHttpServerConnection; -import org.apache.http.impl.NoConnectionReuseStrategy; -import org.apache.http.impl.SocketHttpServerConnection; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.params.CoreConnectionPNames; -import org.apache.http.params.CoreProtocolPNames; -import org.apache.http.params.HttpParams; -import org.apache.http.protocol.BasicHttpContext; -import org.apache.http.protocol.BasicHttpProcessor; -import org.apache.http.protocol.HttpContext; -import org.apache.http.protocol.HttpRequestHandler; -import org.apache.http.protocol.HttpRequestHandlerRegistry; -import org.apache.http.protocol.HttpService; -import org.apache.http.protocol.ResponseConnControl; -import org.apache.http.protocol.ResponseContent; -import org.apache.http.protocol.ResponseDate; -import org.apache.http.protocol.ResponseServer; -import org.apache.log4j.Logger; -import org.springframework.stereotype.Component; - import org.apache.cloudstack.acl.APIChecker; import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiErrorCode; @@ -120,6 +88,36 @@ import org.apache.cloudstack.framework.jobs.AsyncJob; import org.apache.cloudstack.framework.jobs.AsyncJobManager; import org.apache.cloudstack.framework.jobs.impl.AsyncJobVO; import org.apache.cloudstack.managed.context.ManagedContextRunnable; +import org.apache.commons.codec.binary.Base64; +import org.apache.http.ConnectionClosedException; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.HttpServerConnection; +import org.apache.http.HttpStatus; +import org.apache.http.NameValuePair; +import org.apache.http.client.utils.URLEncodedUtils; +import org.apache.http.entity.BasicHttpEntity; +import org.apache.http.impl.DefaultHttpResponseFactory; +import org.apache.http.impl.DefaultHttpServerConnection; +import org.apache.http.impl.NoConnectionReuseStrategy; +import org.apache.http.impl.SocketHttpServerConnection; +import org.apache.http.params.BasicHttpParams; +import org.apache.http.params.CoreConnectionPNames; +import org.apache.http.params.CoreProtocolPNames; +import org.apache.http.params.HttpParams; +import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.protocol.BasicHttpProcessor; +import org.apache.http.protocol.HttpContext; +import org.apache.http.protocol.HttpRequestHandler; +import org.apache.http.protocol.HttpRequestHandlerRegistry; +import org.apache.http.protocol.HttpService; +import org.apache.http.protocol.ResponseConnControl; +import org.apache.http.protocol.ResponseContent; +import org.apache.http.protocol.ResponseDate; +import org.apache.http.protocol.ResponseServer; +import org.apache.log4j.Logger; +import org.springframework.stereotype.Component; import com.cloud.api.response.ApiResponseSerializer; import com.cloud.configuration.Config; @@ -149,7 +147,7 @@ import com.cloud.utils.component.PluggableService; import com.cloud.utils.concurrency.NamedThreadFactory; import com.cloud.utils.db.EntityManager; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.exception.ExceptionProxyObject; @@ -708,7 +706,7 @@ public class ApiServer extends ManagerBase implements HttpRequestHandler, ApiSer } } - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); User user = null; // verify there is a user with this api key diff --git a/server/src/com/cloud/capacity/CapacityManagerImpl.java b/server/src/com/cloud/capacity/CapacityManagerImpl.java index b28fe201f78..72905a7c0c4 100755 --- a/server/src/com/cloud/capacity/CapacityManagerImpl.java +++ b/server/src/com/cloud/capacity/CapacityManagerImpl.java @@ -28,7 +28,6 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; - import org.apache.cloudstack.framework.config.ConfigDepot; import org.apache.cloudstack.framework.config.ConfigKey; import org.apache.cloudstack.framework.config.Configurable; @@ -61,6 +60,7 @@ import com.cloud.host.dao.HostDao; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.hypervisor.dao.HypervisorCapabilitiesDao; import com.cloud.offering.ServiceOffering; +import com.cloud.org.Cluster; import com.cloud.org.Grouping.AllocationState; import com.cloud.resource.ResourceListener; import com.cloud.resource.ResourceManager; @@ -81,6 +81,9 @@ import com.cloud.utils.concurrency.NamedThreadFactory; import com.cloud.utils.db.DB; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.fsm.StateListener; import com.cloud.vm.UserVmDetailVO; import com.cloud.vm.UserVmVO; @@ -170,100 +173,104 @@ public class CapacityManagerImpl extends ManagerBase implements CapacityManager, @DB @Override - public boolean releaseVmCapacity(VirtualMachine vm, boolean moveFromReserved, boolean moveToReservered, Long hostId) { - ServiceOfferingVO svo = _offeringsDao.findById(vm.getServiceOfferingId()); + public boolean releaseVmCapacity(VirtualMachine vm, final boolean moveFromReserved, final boolean moveToReservered, final Long hostId) { + final ServiceOfferingVO svo = _offeringsDao.findById(vm.getServiceOfferingId()); CapacityVO capacityCpu = _capacityDao.findByHostIdType(hostId, CapacityVO.CAPACITY_TYPE_CPU); CapacityVO capacityMemory = _capacityDao.findByHostIdType(hostId, CapacityVO.CAPACITY_TYPE_MEMORY); Long clusterId = null; if (hostId != null) { - HostVO host = _hostDao.findById(hostId); + HostVO host = _hostDao.findById(hostId); clusterId = host.getClusterId(); } if (capacityCpu == null || capacityMemory == null || svo == null) { return false; } - Transaction txn = Transaction.currentTxn(); try { - txn.start(); - - capacityCpu = _capacityDao.lockRow(capacityCpu.getId(), true); - capacityMemory = _capacityDao.lockRow(capacityMemory.getId(), true); - - long usedCpu = capacityCpu.getUsedCapacity(); - long usedMem = capacityMemory.getUsedCapacity(); - long reservedCpu = capacityCpu.getReservedCapacity(); - long reservedMem = capacityMemory.getReservedCapacity(); - long actualTotalCpu = capacityCpu.getTotalCapacity(); - float cpuOvercommitRatio =Float.parseFloat(_clusterDetailsDao.findDetail(clusterId,"cpuOvercommitRatio").getValue()); - float memoryOvercommitRatio = Float.parseFloat(_clusterDetailsDao.findDetail(clusterId,"memoryOvercommitRatio").getValue()); - int vmCPU = svo.getCpu() * svo.getSpeed(); - long vmMem = svo.getRamSize() * 1024L * 1024L; - long actualTotalMem = capacityMemory.getTotalCapacity(); - long totalMem = (long) (actualTotalMem * memoryOvercommitRatio); - long totalCpu = (long) (actualTotalCpu * cpuOvercommitRatio); - if (s_logger.isDebugEnabled()) { - s_logger.debug("Hosts's actual total CPU: " + actualTotalCpu + " and CPU after applying overprovisioning: " + totalCpu); - s_logger.debug("Hosts's actual total RAM: " + actualTotalMem + " and RAM after applying overprovisioning: " + totalMem); - } - - - if (!moveFromReserved) { - /* move resource from used */ - if (usedCpu >= vmCPU) { - capacityCpu.setUsedCapacity(usedCpu - vmCPU); - } - if (usedMem >= vmMem) { - capacityMemory.setUsedCapacity(usedMem - vmMem); - } - - if (moveToReservered) { - if (reservedCpu + vmCPU <= totalCpu) { - capacityCpu.setReservedCapacity(reservedCpu + vmCPU); + final Long clusterIdFinal = clusterId; + final long capacityCpuId = capacityCpu.getId(); + final long capacityMemoryId = capacityMemory.getId(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + CapacityVO capacityCpu = _capacityDao.lockRow(capacityCpuId, true); + CapacityVO capacityMemory = _capacityDao.lockRow(capacityMemoryId, true); + + long usedCpu = capacityCpu.getUsedCapacity(); + long usedMem = capacityMemory.getUsedCapacity(); + long reservedCpu = capacityCpu.getReservedCapacity(); + long reservedMem = capacityMemory.getReservedCapacity(); + long actualTotalCpu = capacityCpu.getTotalCapacity(); + float cpuOvercommitRatio =Float.parseFloat(_clusterDetailsDao.findDetail(clusterIdFinal,"cpuOvercommitRatio").getValue()); + float memoryOvercommitRatio = Float.parseFloat(_clusterDetailsDao.findDetail(clusterIdFinal,"memoryOvercommitRatio").getValue()); + int vmCPU = svo.getCpu() * svo.getSpeed(); + long vmMem = svo.getRamSize() * 1024L * 1024L; + long actualTotalMem = capacityMemory.getTotalCapacity(); + long totalMem = (long) (actualTotalMem * memoryOvercommitRatio); + long totalCpu = (long) (actualTotalCpu * cpuOvercommitRatio); + if (s_logger.isDebugEnabled()) { + s_logger.debug("Hosts's actual total CPU: " + actualTotalCpu + " and CPU after applying overprovisioning: " + totalCpu); + s_logger.debug("Hosts's actual total RAM: " + actualTotalMem + " and RAM after applying overprovisioning: " + totalMem); } - if (reservedMem + vmMem <= totalMem) { - capacityMemory.setReservedCapacity(reservedMem + vmMem); + + + if (!moveFromReserved) { + /* move resource from used */ + if (usedCpu >= vmCPU) { + capacityCpu.setUsedCapacity(usedCpu - vmCPU); + } + if (usedMem >= vmMem) { + capacityMemory.setUsedCapacity(usedMem - vmMem); + } + + if (moveToReservered) { + if (reservedCpu + vmCPU <= totalCpu) { + capacityCpu.setReservedCapacity(reservedCpu + vmCPU); + } + if (reservedMem + vmMem <= totalMem) { + capacityMemory.setReservedCapacity(reservedMem + vmMem); + } + } + } else { + if (reservedCpu >= vmCPU) { + capacityCpu.setReservedCapacity(reservedCpu - vmCPU); + } + if (reservedMem >= vmMem) { + capacityMemory.setReservedCapacity(reservedMem - vmMem); + } } - } - } else { - if (reservedCpu >= vmCPU) { - capacityCpu.setReservedCapacity(reservedCpu - vmCPU); - } - if (reservedMem >= vmMem) { - capacityMemory.setReservedCapacity(reservedMem - vmMem); - } - } - s_logger.debug("release cpu from host: " + hostId + ", old used: " + usedCpu + ",reserved: " + reservedCpu + ", actual total: " - + actualTotalCpu + ", total with overprovisioning: " + totalCpu + "; new used: " + capacityCpu.getUsedCapacity() + ",reserved:" - + capacityCpu.getReservedCapacity() + "; movedfromreserved: " + moveFromReserved + ",moveToReservered" + moveToReservered); + s_logger.debug("release cpu from host: " + hostId + ", old used: " + usedCpu + ",reserved: " + reservedCpu + ", actual total: " + + actualTotalCpu + ", total with overprovisioning: " + totalCpu + "; new used: " + capacityCpu.getUsedCapacity() + ",reserved:" + + capacityCpu.getReservedCapacity() + "; movedfromreserved: " + moveFromReserved + ",moveToReservered" + moveToReservered); - s_logger.debug("release mem from host: " + hostId + ", old used: " + usedMem + ",reserved: " + reservedMem + ", total: " + totalMem - + "; new used: " + capacityMemory.getUsedCapacity() + ",reserved:" + capacityMemory.getReservedCapacity() - + "; movedfromreserved: " + moveFromReserved + ",moveToReservered" + moveToReservered); + s_logger.debug("release mem from host: " + hostId + ", old used: " + usedMem + ",reserved: " + reservedMem + ", total: " + totalMem + + "; new used: " + capacityMemory.getUsedCapacity() + ",reserved:" + capacityMemory.getReservedCapacity() + + "; movedfromreserved: " + moveFromReserved + ",moveToReservered" + moveToReservered); + + _capacityDao.update(capacityCpu.getId(), capacityCpu); + _capacityDao.update(capacityMemory.getId(), capacityMemory); + } + }); - _capacityDao.update(capacityCpu.getId(), capacityCpu); - _capacityDao.update(capacityMemory.getId(), capacityMemory); - txn.commit(); return true; } catch (Exception e) { s_logger.debug("Failed to transit vm's state, due to " + e.getMessage()); - txn.rollback(); return false; } } @DB @Override - public void allocateVmCapacity(VirtualMachine vm, boolean fromLastHost) { + public void allocateVmCapacity(VirtualMachine vm, final boolean fromLastHost) { - long hostId = vm.getHostId(); + final long hostId = vm.getHostId(); HostVO host = _hostDao.findById(hostId); - long clusterId = host.getClusterId(); - float cpuOvercommitRatio = Float.parseFloat(_clusterDetailsDao.findDetail(clusterId, "cpuOvercommitRatio").getValue()); - float memoryOvercommitRatio = Float.parseFloat(_clusterDetailsDao.findDetail(clusterId, "memoryOvercommitRatio").getValue()); + final long clusterId = host.getClusterId(); + final float cpuOvercommitRatio = Float.parseFloat(_clusterDetailsDao.findDetail(clusterId, "cpuOvercommitRatio").getValue()); + final float memoryOvercommitRatio = Float.parseFloat(_clusterDetailsDao.findDetail(clusterId, "memoryOvercommitRatio").getValue()); - ServiceOfferingVO svo = _offeringsDao.findById(vm.getServiceOfferingId()); + final ServiceOfferingVO svo = _offeringsDao.findById(vm.getServiceOfferingId()); CapacityVO capacityCpu = _capacityDao.findByHostIdType(hostId, CapacityVO.CAPACITY_TYPE_CPU); CapacityVO capacityMem = _capacityDao.findByHostIdType(hostId, CapacityVO.CAPACITY_TYPE_MEMORY); @@ -272,74 +279,78 @@ public class CapacityManagerImpl extends ManagerBase implements CapacityManager, return; } - int cpu = svo.getCpu() * svo.getSpeed(); - long ram = svo.getRamSize() * 1024L * 1024L; + final int cpu = svo.getCpu() * svo.getSpeed(); + final long ram = svo.getRamSize() * 1024L * 1024L; - Transaction txn = Transaction.currentTxn(); - try { - txn.start(); - capacityCpu = _capacityDao.lockRow(capacityCpu.getId(), true); - capacityMem = _capacityDao.lockRow(capacityMem.getId(), true); + final long capacityCpuId = capacityCpu.getId(); + final long capacityMemId = capacityMem.getId(); - long usedCpu = capacityCpu.getUsedCapacity(); - long usedMem = capacityMem.getUsedCapacity(); - long reservedCpu = capacityCpu.getReservedCapacity(); - long reservedMem = capacityMem.getReservedCapacity(); - long actualTotalCpu = capacityCpu.getTotalCapacity(); - long actualTotalMem = capacityMem.getTotalCapacity(); - long totalCpu = (long) (actualTotalCpu * cpuOvercommitRatio); - long totalMem = (long) (actualTotalMem * memoryOvercommitRatio); - if (s_logger.isDebugEnabled()) { - s_logger.debug("Hosts's actual total CPU: " + actualTotalCpu + " and CPU after applying overprovisioning: " + totalCpu); - } + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + CapacityVO capacityCpu = _capacityDao.lockRow(capacityCpuId, true); + CapacityVO capacityMem = _capacityDao.lockRow(capacityMemId, true); - long freeCpu = totalCpu - (reservedCpu + usedCpu); - long freeMem = totalMem - (reservedMem + usedMem); - - if (s_logger.isDebugEnabled()) { - s_logger.debug("We are allocating VM, increasing the used capacity of this host:" + hostId); - s_logger.debug("Current Used CPU: " + usedCpu + " , Free CPU:" + freeCpu + " ,Requested CPU: " + cpu); - s_logger.debug("Current Used RAM: " + usedMem + " , Free RAM:" + freeMem + " ,Requested RAM: " + ram); - } - capacityCpu.setUsedCapacity(usedCpu + cpu); - capacityMem.setUsedCapacity(usedMem + ram); - - if (fromLastHost) { - /* alloc from reserved */ - if (s_logger.isDebugEnabled()) { - s_logger.debug("We are allocating VM to the last host again, so adjusting the reserved capacity if it is not less than required"); - s_logger.debug("Reserved CPU: " + reservedCpu + " , Requested CPU: " + cpu); - s_logger.debug("Reserved RAM: " + reservedMem + " , Requested RAM: " + ram); - } - if (reservedCpu >= cpu && reservedMem >= ram) { - capacityCpu.setReservedCapacity(reservedCpu - cpu); - capacityMem.setReservedCapacity(reservedMem - ram); - } - } else { - /* alloc from free resource */ - if (!((reservedCpu + usedCpu + cpu <= totalCpu) && (reservedMem + usedMem + ram <= totalMem))) { + long usedCpu = capacityCpu.getUsedCapacity(); + long usedMem = capacityMem.getUsedCapacity(); + long reservedCpu = capacityCpu.getReservedCapacity(); + long reservedMem = capacityMem.getReservedCapacity(); + long actualTotalCpu = capacityCpu.getTotalCapacity(); + long actualTotalMem = capacityMem.getTotalCapacity(); + long totalCpu = (long) (actualTotalCpu * cpuOvercommitRatio); + long totalMem = (long) (actualTotalMem * memoryOvercommitRatio); if (s_logger.isDebugEnabled()) { - s_logger.debug("Host doesnt seem to have enough free capacity, but increasing the used capacity anyways, since the VM is already starting on this host "); + s_logger.debug("Hosts's actual total CPU: " + actualTotalCpu + " and CPU after applying overprovisioning: " + totalCpu); } + + long freeCpu = totalCpu - (reservedCpu + usedCpu); + long freeMem = totalMem - (reservedMem + usedMem); + + if (s_logger.isDebugEnabled()) { + s_logger.debug("We are allocating VM, increasing the used capacity of this host:" + hostId); + s_logger.debug("Current Used CPU: " + usedCpu + " , Free CPU:" + freeCpu + " ,Requested CPU: " + cpu); + s_logger.debug("Current Used RAM: " + usedMem + " , Free RAM:" + freeMem + " ,Requested RAM: " + ram); + } + capacityCpu.setUsedCapacity(usedCpu + cpu); + capacityMem.setUsedCapacity(usedMem + ram); + + if (fromLastHost) { + /* alloc from reserved */ + if (s_logger.isDebugEnabled()) { + s_logger.debug("We are allocating VM to the last host again, so adjusting the reserved capacity if it is not less than required"); + s_logger.debug("Reserved CPU: " + reservedCpu + " , Requested CPU: " + cpu); + s_logger.debug("Reserved RAM: " + reservedMem + " , Requested RAM: " + ram); + } + if (reservedCpu >= cpu && reservedMem >= ram) { + capacityCpu.setReservedCapacity(reservedCpu - cpu); + capacityMem.setReservedCapacity(reservedMem - ram); + } + } else { + /* alloc from free resource */ + if (!((reservedCpu + usedCpu + cpu <= totalCpu) && (reservedMem + usedMem + ram <= totalMem))) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Host doesnt seem to have enough free capacity, but increasing the used capacity anyways, since the VM is already starting on this host "); + } + } + } + + s_logger.debug("CPU STATS after allocation: for host: " + hostId + ", old used: " + usedCpu + ", old reserved: " + reservedCpu + + ", actual total: " + actualTotalCpu + ", total with overprovisioning: " + totalCpu + "; new used:" + + capacityCpu.getUsedCapacity() + ", reserved:" + capacityCpu.getReservedCapacity() + "; requested cpu:" + cpu + + ",alloc_from_last:" + fromLastHost); + + s_logger.debug("RAM STATS after allocation: for host: " + hostId + ", old used: " + usedMem + ", old reserved: " + reservedMem + + ", total: " + totalMem + "; new used: " + capacityMem.getUsedCapacity() + ", reserved: " + capacityMem.getReservedCapacity() + + "; requested mem: " + ram + ",alloc_from_last:" + fromLastHost); + + _capacityDao.update(capacityCpu.getId(), capacityCpu); + _capacityDao.update(capacityMem.getId(), capacityMem); } - } - - s_logger.debug("CPU STATS after allocation: for host: " + hostId + ", old used: " + usedCpu + ", old reserved: " + reservedCpu - + ", actual total: " + actualTotalCpu + ", total with overprovisioning: " + totalCpu + "; new used:" - + capacityCpu.getUsedCapacity() + ", reserved:" + capacityCpu.getReservedCapacity() + "; requested cpu:" + cpu - + ",alloc_from_last:" + fromLastHost); - - s_logger.debug("RAM STATS after allocation: for host: " + hostId + ", old used: " + usedMem + ", old reserved: " + reservedMem - + ", total: " + totalMem + "; new used: " + capacityMem.getUsedCapacity() + ", reserved: " + capacityMem.getReservedCapacity() - + "; requested mem: " + ram + ",alloc_from_last:" + fromLastHost); - - _capacityDao.update(capacityCpu.getId(), capacityCpu); - _capacityDao.update(capacityMem.getId(), capacityMem); - txn.commit(); + }); } catch (Exception e) { - txn.rollback(); + s_logger.error("Exception allocating VM capacity", e); return; } } @@ -512,7 +523,7 @@ public class CapacityManagerImpl extends ManagerBase implements CapacityManager, @DB @Override - public void updateCapacityForHost(Host host){ + public void updateCapacityForHost(final Host host){ // prepare the service offerings List offerings = _offeringsDao.listAllIncludingRemoved(); Map offeringsMap = new HashMap(); @@ -625,36 +636,42 @@ public class CapacityManagerImpl extends ManagerBase implements CapacityManager, s_logger.error("Caught exception while updating cpu/memory capacity for the host " +host.getId(), e); } }else { - Transaction txn = Transaction.currentTxn(); - txn.start(); - CapacityVO capacity = new CapacityVO(host.getId(), - host.getDataCenterId(), host.getPodId(), host.getClusterId(), usedMemory, - host.getTotalMemory(), - CapacityVO.CAPACITY_TYPE_MEMORY); - capacity.setReservedCapacity(reservedMemory); - CapacityState capacityState = CapacityState.Enabled; - if (host.getClusterId() != null) { - cluster = ApiDBUtils.findClusterById(host.getClusterId()); - if (cluster != null) { - capacityState = _configMgr.findClusterAllocationState(cluster) == AllocationState.Disabled ? CapacityState.Disabled - : CapacityState.Enabled; + final long usedMemoryFinal = usedMemory; + final long reservedMemoryFinal = reservedMemory; + final long usedCpuFinal = usedCpu; + final long reservedCpuFinal = reservedCpu; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + CapacityVO capacity = new CapacityVO(host.getId(), + host.getDataCenterId(), host.getPodId(), host.getClusterId(), usedMemoryFinal, + host.getTotalMemory(), + CapacityVO.CAPACITY_TYPE_MEMORY); + capacity.setReservedCapacity(reservedMemoryFinal); + CapacityState capacityState = CapacityState.Enabled; + if (host.getClusterId() != null) { + ClusterVO cluster = ApiDBUtils.findClusterById(host.getClusterId()); + if (cluster != null) { + capacityState = _configMgr.findClusterAllocationState(cluster) == AllocationState.Disabled ? CapacityState.Disabled + : CapacityState.Enabled; + capacity.setCapacityState(capacityState); + } + } + _capacityDao.persist(capacity); + + capacity = new CapacityVO( + host.getId(), + host.getDataCenterId(), + host.getPodId(), + host.getClusterId(), + usedCpuFinal, + host.getCpus().longValue() * host.getSpeed().longValue(), + CapacityVO.CAPACITY_TYPE_CPU); + capacity.setReservedCapacity(reservedCpuFinal); capacity.setCapacityState(capacityState); + _capacityDao.persist(capacity); } - } - _capacityDao.persist(capacity); - - capacity = new CapacityVO( - host.getId(), - host.getDataCenterId(), - host.getPodId(), - host.getClusterId(), - usedCpu, - host.getCpus().longValue() * host.getSpeed().longValue(), - CapacityVO.CAPACITY_TYPE_CPU); - capacity.setReservedCapacity(reservedCpu); - capacity.setCapacityState(capacityState); - _capacityDao.persist(capacity); - txn.commit(); + }); } diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java index 6460276fd1c..32459bcba22 100755 --- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java @@ -36,7 +36,6 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; - import org.apache.cloudstack.acl.SecurityChecker; import org.apache.cloudstack.affinity.AffinityGroup; import org.apache.cloudstack.affinity.AffinityGroupService; @@ -192,6 +191,10 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionLegacy; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; import com.cloud.vm.NicIpAlias; @@ -508,7 +511,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati } // Execute all updates in a single transaction - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); if (!_configDao.update(name, category, value)) { @@ -906,7 +909,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati selectSql += " and removed IS NULL"; } - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql); stmt.setLong(1, podId); @@ -992,9 +995,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati @Override @DB public boolean deletePod(DeletePodCmd cmd) { - Long podId = cmd.getId(); - - Transaction txn = Transaction.currentTxn(); + final Long podId = cmd.getId(); // Make sure the pod exists if (!validPod(podId)) { @@ -1003,50 +1004,52 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati checkIfPodIsDeletable(podId); - HostPodVO pod = _podDao.findById(podId); + final HostPodVO pod = _podDao.findById(podId); - txn.start(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + // Delete private ip addresses for the pod if there are any + List privateIps = _privateIpAddressDao.listByPodIdDcId(Long.valueOf(podId), + pod.getDataCenterId()); + if (!privateIps.isEmpty()) { + if (!(_privateIpAddressDao.deleteIpAddressByPod(podId))) { + throw new CloudRuntimeException("Failed to cleanup private ip addresses for pod " + podId); + } + } - // Delete private ip addresses for the pod if there are any - List privateIps = _privateIpAddressDao.listByPodIdDcId(Long.valueOf(podId), - pod.getDataCenterId()); - if (!privateIps.isEmpty()) { - if (!(_privateIpAddressDao.deleteIpAddressByPod(podId))) { - throw new CloudRuntimeException("Failed to cleanup private ip addresses for pod " + podId); + // Delete link local ip addresses for the pod + List localIps = _LinkLocalIpAllocDao.listByPodIdDcId(podId, + pod.getDataCenterId()); + if (!localIps.isEmpty()) { + if (!(_LinkLocalIpAllocDao.deleteIpAddressByPod(podId))) { + throw new CloudRuntimeException("Failed to cleanup private ip addresses for pod " + podId); + } + } + + // Delete vlans associated with the pod + List vlans = _networkModel.listPodVlans(podId); + if (vlans != null && !vlans.isEmpty()) { + for (Vlan vlan : vlans) { + _vlanDao.remove(vlan.getId()); + } + } + + // Delete corresponding capacity records + _capacityDao.removeBy(null, null, podId, null, null); + + // Delete the pod + if (!(_podDao.remove(podId))) { + throw new CloudRuntimeException("Failed to delete pod " + podId); + } + + // remove from dedicated resources + DedicatedResourceVO dr = _dedicatedDao.findByPodId(podId); + if (dr != null) { + _dedicatedDao.remove(dr.getId()); + } } - } - - // Delete link local ip addresses for the pod - List localIps = _LinkLocalIpAllocDao.listByPodIdDcId(podId, - pod.getDataCenterId()); - if (!localIps.isEmpty()) { - if (!(_LinkLocalIpAllocDao.deleteIpAddressByPod(podId))) { - throw new CloudRuntimeException("Failed to cleanup private ip addresses for pod " + podId); - } - } - - // Delete vlans associated with the pod - List vlans = _networkModel.listPodVlans(podId); - if (vlans != null && !vlans.isEmpty()) { - for (Vlan vlan : vlans) { - _vlanDao.remove(vlan.getId()); - } - } - - // Delete corresponding capacity records - _capacityDao.removeBy(null, null, podId, null, null); - - // Delete the pod - if (!(_podDao.remove(podId))) { - throw new CloudRuntimeException("Failed to delete pod " + podId); - } - - // remove from dedicated resources - DedicatedResourceVO dr = _dedicatedDao.findByPodId(podId); - if (dr != null) { - _dedicatedDao.remove(dr.getId()); - } - txn.commit(); + }); return true; } @@ -1059,12 +1062,12 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati @Override @DB - public Pod editPod(long id, String name, String startIp, String endIp, String gateway, String netmask, + public Pod editPod(final long id, String name, String startIp, String endIp, String gateway, String netmask, String allocationStateStr) { // verify parameters - HostPodVO pod = _podDao.findById(id); - ; + final HostPodVO pod = _podDao.findById(id); + if (pod == null) { throw new InvalidParameterValueException("Unable to find pod by id " + id); } @@ -1146,59 +1149,73 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati } // Verify pod's attributes - String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask); + final String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask); boolean checkForDuplicates = !oldPodName.equals(name); checkPodAttributes(id, name, pod.getDataCenterId(), gateway, cidr, startIp, endIp, allocationStateStr, checkForDuplicates, false); - Transaction txn = Transaction.currentTxn(); try { - txn.start(); - long zoneId = pod.getDataCenterId(); - if (!allowToDownsize) { - if (leftRangeToAdd != null) { - _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), leftRangeToAdd[0], leftRangeToAdd[1]); + final String[] existingPodIpRangeFinal = existingPodIpRange; + final String[] leftRangeToAddFinal = leftRangeToAdd; + final String[] rightRangeToAddFinal = rightRangeToAdd; + final boolean allowToDownsizeFinal = allowToDownsize; + final String allocationStateStrFinal = allocationStateStr; + final String startIpFinal = startIp; + final String endIpFinal = endIp; + final String nameFinal = name; + final String gatewayFinal = gateway; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + long zoneId = pod.getDataCenterId(); + + String startIp = startIpFinal; + String endIp = endIpFinal; + + if (!allowToDownsizeFinal) { + if (leftRangeToAddFinal != null) { + _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), leftRangeToAddFinal[0], leftRangeToAddFinal[1]); + } + + if (rightRangeToAddFinal != null) { + _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), rightRangeToAddFinal[0], rightRangeToAddFinal[1]); + } + + } else { + // delete the old range + _zoneDao.deletePrivateIpAddressByPod(pod.getId()); + + // add the new one + if (startIp == null) { + startIp = existingPodIpRangeFinal[0]; + } + + if (endIp == null) { + endIp = existingPodIpRangeFinal[1]; + } + + _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), startIp, endIp); + } + + pod.setName(nameFinal); + pod.setDataCenterId(zoneId); + pod.setGateway(gatewayFinal); + pod.setCidrAddress(getCidrAddress(cidr)); + pod.setCidrSize(getCidrSize(cidr)); + + String ipRange = startIp + "-" + endIp; + pod.setDescription(ipRange); + Grouping.AllocationState allocationState = null; + if (allocationStateStrFinal != null && !allocationStateStrFinal.isEmpty()) { + allocationState = Grouping.AllocationState.valueOf(allocationStateStrFinal); + _capacityDao.updateCapacityState(null, pod.getId(), null, null, allocationStateStrFinal); + pod.setAllocationState(allocationState); + } + + _podDao.update(id, pod); } - - if (rightRangeToAdd != null) { - _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), rightRangeToAdd[0], rightRangeToAdd[1]); - } - - } else { - // delete the old range - _zoneDao.deletePrivateIpAddressByPod(pod.getId()); - - // add the new one - if (startIp == null) { - startIp = existingPodIpRange[0]; - } - - if (endIp == null) { - endIp = existingPodIpRange[1]; - } - - _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), startIp, endIp); - } - - pod.setName(name); - pod.setDataCenterId(zoneId); - pod.setGateway(gateway); - pod.setCidrAddress(getCidrAddress(cidr)); - pod.setCidrSize(getCidrSize(cidr)); - - String ipRange = startIp + "-" + endIp; - pod.setDescription(ipRange); - Grouping.AllocationState allocationState = null; - if (allocationStateStr != null && !allocationStateStr.isEmpty()) { - allocationState = Grouping.AllocationState.valueOf(allocationStateStr); - _capacityDao.updateCapacityState(null, pod.getId(), null, null, allocationStateStr); - pod.setAllocationState(allocationState); - } - - _podDao.update(id, pod); - - txn.commit(); + }); } catch (Exception e) { s_logger.error("Unable to edit pod due to " + e.getMessage(), e); throw new CloudRuntimeException("Failed to edit pod. Please contact Cloud Support."); @@ -1221,7 +1238,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati @Override @DB - public HostPodVO createPod(long userId, String podName, long zoneId, String gateway, String cidr, String startIp, + public HostPodVO createPod(long userId, String podName, final long zoneId, String gateway, String cidr, final String startIp, String endIp, String allocationStateStr, boolean skipGatewayOverlapCheck) { // Check if the zone is valid @@ -1260,31 +1277,33 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati throw new InvalidParameterValueException("Start ip is required parameter"); } - HostPodVO pod = new HostPodVO(podName, zoneId, gateway, cidrAddress, cidrSize, ipRange); + final HostPodVO podFinal = new HostPodVO(podName, zoneId, gateway, cidrAddress, cidrSize, ipRange); Grouping.AllocationState allocationState = null; if (allocationStateStr != null && !allocationStateStr.isEmpty()) { allocationState = Grouping.AllocationState.valueOf(allocationStateStr); - pod.setAllocationState(allocationState); + podFinal.setAllocationState(allocationState); } - Transaction txn = Transaction.currentTxn(); - txn.start(); + final String endIpFinal = endIp; + return Transaction.execute(new TransactionCallback() { + @Override + public HostPodVO doInTransaction(TransactionStatus status) { - pod = _podDao.persist(pod); + HostPodVO pod = _podDao.persist(podFinal); - if (startIp != null) { - _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), startIp, endIp); - } + if (startIp != null) { + _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), startIp, endIpFinal); + } - String[] linkLocalIpRanges = getLinkLocalIPRange(); - if (linkLocalIpRanges != null) { - _zoneDao.addLinkLocalIpAddress(zoneId, pod.getId(), linkLocalIpRanges[0], linkLocalIpRanges[1]); - } + String[] linkLocalIpRanges = getLinkLocalIPRange(); + if (linkLocalIpRanges != null) { + _zoneDao.addLinkLocalIpAddress(zoneId, pod.getId(), linkLocalIpRanges[0], linkLocalIpRanges[1]); + } - txn.commit(); - - return pod; + return pod; + } + }); } @DB @@ -1369,7 +1388,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati selectSql += " AND state != '" + VirtualMachine.State.Expunging.toString() + "'"; } - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql); stmt.setLong(1, zoneId); @@ -1504,11 +1523,8 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati @ActionEvent(eventType = EventTypes.EVENT_ZONE_DELETE, eventDescription = "deleting zone", async = false) public boolean deleteZone(DeleteZoneCmd cmd) { - Transaction txn = Transaction.currentTxn(); - boolean success = false; - Long userId = CallContext.current().getCallingUserId(); - Long zoneId = cmd.getId(); + final Long zoneId = cmd.getId(); if (userId == null) { userId = Long.valueOf(User.UID_SYSTEM); @@ -1521,38 +1537,38 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati checkIfZoneIsDeletable(zoneId); - txn.start(); - - // delete vlans for this zone - List vlans = _vlanDao.listByZone(zoneId); - for (VlanVO vlan : vlans) { - _vlanDao.remove(vlan.getId()); - } - - success = _zoneDao.remove(zoneId); - - if (success) { - // delete all capacity records for the zone - _capacityDao.removeBy(null, zoneId, null, null, null); - // remove from dedicated resources - DedicatedResourceVO dr = _dedicatedDao.findByZoneId(zoneId); - if (dr != null) { - _dedicatedDao.remove(dr.getId()); - // find the group associated and check if there are any more - // resources under that group - List resourcesInGroup = _dedicatedDao.listByAffinityGroupId(dr - .getAffinityGroupId()); - if (resourcesInGroup.isEmpty()) { - // delete the group - _affinityGroupService.deleteAffinityGroup(dr.getAffinityGroupId(), null, null, null); + return Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + // delete vlans for this zone + List vlans = _vlanDao.listByZone(zoneId); + for (VlanVO vlan : vlans) { + _vlanDao.remove(vlan.getId()); } + + boolean success = _zoneDao.remove(zoneId); + + if (success) { + // delete all capacity records for the zone + _capacityDao.removeBy(null, zoneId, null, null, null); + // remove from dedicated resources + DedicatedResourceVO dr = _dedicatedDao.findByZoneId(zoneId); + if (dr != null) { + _dedicatedDao.remove(dr.getId()); + // find the group associated and check if there are any more + // resources under that group + List resourcesInGroup = _dedicatedDao.listByAffinityGroupId(dr + .getAffinityGroupId()); + if (resourcesInGroup.isEmpty()) { + // delete the group + _affinityGroupService.deleteAffinityGroup(dr.getAffinityGroupId(), null, null, null); + } + } + } + + return success; } - } - - txn.commit(); - - return success; - + }); } @Override @@ -1560,7 +1576,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati @ActionEvent(eventType = EventTypes.EVENT_ZONE_EDIT, eventDescription = "editing zone", async = false) public DataCenter editZone(UpdateZoneCmd cmd) { // Parameter validation as from execute() method in V1 - Long zoneId = cmd.getId(); + final Long zoneId = cmd.getId(); String zoneName = cmd.getZoneName(); String dns1 = cmd.getDns1(); String dns2 = cmd.getDns2(); @@ -1570,14 +1586,14 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati String internalDns2 = cmd.getInternalDns2(); String guestCidr = cmd.getGuestCidrAddress(); List dnsSearchOrder = cmd.getDnsSearchOrder(); - Boolean isPublic = cmd.isPublic(); - String allocationStateStr = cmd.getAllocationState(); - String dhcpProvider = cmd.getDhcpProvider(); + final Boolean isPublic = cmd.isPublic(); + final String allocationStateStr = cmd.getAllocationState(); + final String dhcpProvider = cmd.getDhcpProvider(); Map detailsMap = cmd.getDetails(); String networkDomain = cmd.getDomain(); Boolean localStorageEnabled = cmd.getLocalStorageEnabled(); - Map newDetails = new HashMap(); + final Map newDetails = new HashMap(); if (detailsMap != null) { Collection zoneDetailsCollection = detailsMap.values(); Iterator iter = zoneDetailsCollection.iterator(); @@ -1611,7 +1627,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati newDetails.put(ZoneConfig.DnsSearchOrder.getName(), StringUtils.join(dnsSearchOrder, ",")); } - DataCenterVO zone = _zoneDao.findById(zoneId); + final DataCenterVO zone = _zoneDao.findById(zoneId); if (zone == null) { throw new InvalidParameterValueException("unable to find zone by id " + zoneId); } @@ -1698,93 +1714,94 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati } } - Transaction txn = Transaction.currentTxn(); - txn.start(); - - Map updatedDetails = new HashMap(); - _zoneDao.loadDetails(zone); - if (zone.getDetails() != null) { - updatedDetails.putAll(zone.getDetails()); - } - updatedDetails.putAll(newDetails); - zone.setDetails(updatedDetails); - - if (allocationStateStr != null && !allocationStateStr.isEmpty()) { - Grouping.AllocationState allocationState = Grouping.AllocationState.valueOf(allocationStateStr); - - if (allocationState == Grouping.AllocationState.Enabled) { - // check if zone has necessary trafficTypes before enabling - try { - PhysicalNetwork mgmtPhyNetwork; - // zone should have a physical network with management - // traffiType - mgmtPhyNetwork = _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, - TrafficType.Management); - if (NetworkType.Advanced == zone.getNetworkType() && !zone.isSecurityGroupEnabled()) { - // advanced zone without SG should have a physical - // network with public Thpe - _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, TrafficType.Public); - } - - try { - _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, TrafficType.Storage); - } catch (InvalidParameterValueException noStorage) { - PhysicalNetworkTrafficTypeVO mgmtTraffic = _trafficTypeDao.findBy(mgmtPhyNetwork.getId(), - TrafficType.Management); - _networkSvc.addTrafficTypeToPhysicalNetwork(mgmtPhyNetwork.getId(), - TrafficType.Storage.toString(), mgmtTraffic.getXenNetworkLabel(), - mgmtTraffic.getKvmNetworkLabel(), mgmtTraffic.getVmwareNetworkLabel(), - mgmtTraffic.getSimulatorNetworkLabel(), mgmtTraffic.getVlan()); - s_logger.info("No storage traffic type was specified by admin, create default storage traffic on physical network " - + mgmtPhyNetwork.getId() + " with same configure of management traffic type"); - } - } catch (InvalidParameterValueException ex) { - throw new InvalidParameterValueException("Cannot enable this Zone since: " + ex.getMessage()); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + Map updatedDetails = new HashMap(); + _zoneDao.loadDetails(zone); + if (zone.getDetails() != null) { + updatedDetails.putAll(zone.getDetails()); } - } - _capacityDao.updateCapacityState(zone.getId(), null, null, null, allocationStateStr); - zone.setAllocationState(allocationState); - } - - if (dhcpProvider != null) { - zone.setDhcpProvider(dhcpProvider); - } + updatedDetails.putAll(newDetails); + zone.setDetails(updatedDetails); - // update a private zone to public; not vice versa - if (isPublic != null && isPublic) { - zone.setDomainId(null); - zone.setDomain(null); - - // release the dedication for this zone - DedicatedResourceVO resource = _dedicatedDao.findByZoneId(zoneId); - Long resourceId = null; - if (resource != null) { - resourceId = resource.getId(); - if (!_dedicatedDao.remove(resourceId)) { - throw new CloudRuntimeException("Failed to delete dedicated Zone Resource " + resourceId); + if (allocationStateStr != null && !allocationStateStr.isEmpty()) { + Grouping.AllocationState allocationState = Grouping.AllocationState.valueOf(allocationStateStr); + + if (allocationState == Grouping.AllocationState.Enabled) { + // check if zone has necessary trafficTypes before enabling + try { + PhysicalNetwork mgmtPhyNetwork; + // zone should have a physical network with management + // traffiType + mgmtPhyNetwork = _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, + TrafficType.Management); + if (NetworkType.Advanced == zone.getNetworkType() && !zone.isSecurityGroupEnabled()) { + // advanced zone without SG should have a physical + // network with public Thpe + _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, TrafficType.Public); + } + + try { + _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, TrafficType.Storage); + } catch (InvalidParameterValueException noStorage) { + PhysicalNetworkTrafficTypeVO mgmtTraffic = _trafficTypeDao.findBy(mgmtPhyNetwork.getId(), + TrafficType.Management); + _networkSvc.addTrafficTypeToPhysicalNetwork(mgmtPhyNetwork.getId(), + TrafficType.Storage.toString(), mgmtTraffic.getXenNetworkLabel(), + mgmtTraffic.getKvmNetworkLabel(), mgmtTraffic.getVmwareNetworkLabel(), + mgmtTraffic.getSimulatorNetworkLabel(), mgmtTraffic.getVlan()); + s_logger.info("No storage traffic type was specified by admin, create default storage traffic on physical network " + + mgmtPhyNetwork.getId() + " with same configure of management traffic type"); + } + } catch (InvalidParameterValueException ex) { + throw new InvalidParameterValueException("Cannot enable this Zone since: " + ex.getMessage()); + } + } + _capacityDao.updateCapacityState(zone.getId(), null, null, null, allocationStateStr); + zone.setAllocationState(allocationState); } - // find the group associated and check if there are any more - // resources under that group - List resourcesInGroup = _dedicatedDao.listByAffinityGroupId(resource.getAffinityGroupId()); - if (resourcesInGroup.isEmpty()) { - // delete the group - _affinityGroupService.deleteAffinityGroup(resource.getAffinityGroupId(), null, null, null); + + if (dhcpProvider != null) { + zone.setDhcpProvider(dhcpProvider); + } + + // update a private zone to public; not vice versa + if (isPublic != null && isPublic) { + zone.setDomainId(null); + zone.setDomain(null); + + // release the dedication for this zone + DedicatedResourceVO resource = _dedicatedDao.findByZoneId(zoneId); + Long resourceId = null; + if (resource != null) { + resourceId = resource.getId(); + if (!_dedicatedDao.remove(resourceId)) { + throw new CloudRuntimeException("Failed to delete dedicated Zone Resource " + resourceId); + } + // find the group associated and check if there are any more + // resources under that group + List resourcesInGroup = _dedicatedDao.listByAffinityGroupId(resource.getAffinityGroupId()); + if (resourcesInGroup.isEmpty()) { + // delete the group + _affinityGroupService.deleteAffinityGroup(resource.getAffinityGroupId(), null, null, null); + } + } + } + + if (!_zoneDao.update(zoneId, zone)) { + throw new CloudRuntimeException("Failed to edit zone. Please contact Cloud Support."); } } - } + }); - if (!_zoneDao.update(zoneId, zone)) { - throw new CloudRuntimeException("Failed to edit zone. Please contact Cloud Support."); - } - - txn.commit(); return zone; } @Override @DB public DataCenterVO createZone(long userId, String zoneName, String dns1, String dns2, String internalDns1, - String internalDns2, String guestCidr, String domain, Long domainId, NetworkType zoneType, + String internalDns2, String guestCidr, String domain, final Long domainId, NetworkType zoneType, String allocationStateStr, String networkDomain, boolean isSecurityGroupEnabled, boolean isLocalStorageEnabled, String ip6Dns1, String ip6Dns2) { @@ -1809,43 +1826,40 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati byte[] bytes = (zoneName + System.currentTimeMillis()).getBytes(); String zoneToken = UUID.nameUUIDFromBytes(bytes).toString(); - Transaction txn = Transaction.currentTxn(); - try { - txn.start(); - // Create the new zone in the database - DataCenterVO zone = new DataCenterVO(zoneName, null, dns1, dns2, internalDns1, internalDns2, guestCidr, - domain, domainId, zoneType, zoneToken, networkDomain, isSecurityGroupEnabled, - isLocalStorageEnabled, - ip6Dns1, ip6Dns2); - if (allocationStateStr != null && !allocationStateStr.isEmpty()) { - Grouping.AllocationState allocationState = Grouping.AllocationState.valueOf(allocationStateStr); - zone.setAllocationState(allocationState); - } else { - // Zone will be disabled since 3.0. Admin should enable it after - // physical network and providers setup. - zone.setAllocationState(Grouping.AllocationState.Disabled); - } - zone = _zoneDao.persist(zone); - if (domainId != null) { - // zone is explicitly dedicated to this domain - // create affinity group associated and dedicate the zone. - AffinityGroup group = createDedicatedAffinityGroup(null, domainId, null); - DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(zone.getId(), null, null, null, - domainId, null, group.getId()); - _dedicatedDao.persist(dedicatedResource); - } - // Create default system networks - createDefaultSystemNetworks(zone.getId()); - txn.commit(); - return zone; - } catch (Exception ex) { - txn.rollback(); - s_logger.warn("Exception: ", ex); - throw new CloudRuntimeException("Fail to create a network"); - } finally { - txn.close(); + // Create the new zone in the database + final DataCenterVO zoneFinal = new DataCenterVO(zoneName, null, dns1, dns2, internalDns1, internalDns2, guestCidr, + domain, domainId, zoneType, zoneToken, networkDomain, isSecurityGroupEnabled, + isLocalStorageEnabled, + ip6Dns1, ip6Dns2); + if (allocationStateStr != null && !allocationStateStr.isEmpty()) { + Grouping.AllocationState allocationState = Grouping.AllocationState.valueOf(allocationStateStr); + zoneFinal.setAllocationState(allocationState); + } else { + // Zone will be disabled since 3.0. Admin should enable it after + // physical network and providers setup. + zoneFinal.setAllocationState(Grouping.AllocationState.Disabled); } + + return Transaction.execute(new TransactionCallback() { + @Override + public DataCenterVO doInTransaction(TransactionStatus status) { + DataCenterVO zone = _zoneDao.persist(zoneFinal); + if (domainId != null) { + // zone is explicitly dedicated to this domain + // create affinity group associated and dedicate the zone. + AffinityGroup group = createDedicatedAffinityGroup(null, domainId, null); + DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(zone.getId(), null, null, null, + domainId, null, group.getId()); + _dedicatedDao.persist(dedicatedResource); + } + + // Create default system networks + createDefaultSystemNetworks(zone.getId()); + + return zone; + } + }); } private AffinityGroup createDedicatedAffinityGroup(String affinityGroupName, Long domainId, Long accountId) { @@ -2662,31 +2676,43 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati if (ipv4) { checkOverlapPrivateIpRange(zoneId, startIP, endIP); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - if ((sameSubnet == null || sameSubnet.first() == false) && (network.getTrafficType()== TrafficType.Guest) && (network.getGuestType() == GuestType.Shared) && (_vlanDao.listVlansByNetworkId(networkId) != null)) { - Map dhcpCapabilities = _networkSvc.getNetworkOfferingServiceCapabilities(_networkOfferingDao.findById(network.getNetworkOfferingId()), Service.Dhcp); - String supportsMultipleSubnets = dhcpCapabilities.get(Capability.DhcpAccrossMultipleSubnets); - if (supportsMultipleSubnets == null || !Boolean.valueOf(supportsMultipleSubnets)) { - throw new InvalidParameterValueException("The Dhcp serivice provider for this network dose not support the dhcp across multiple subnets"); + return commitVlan(zoneId, podId, startIP, endIP, newVlanGateway, newVlanNetmask, vlanId, + forVirtualNetwork, networkId, physicalNetworkId, startIPv6, endIPv6, ip6Gateway, ip6Cidr, vlanOwner, + network, sameSubnet); + } + + private Vlan commitVlan(final Long zoneId, final Long podId, final String startIP, final String endIP, final String newVlanGatewayFinal, + final String newVlanNetmaskFinal, final String vlanId, final Boolean forVirtualNetwork, final Long networkId, final Long physicalNetworkId, + final String startIPv6, final String endIPv6, final String ip6Gateway, final String ip6Cidr, final Account vlanOwner, final Network network, + final Pair> sameSubnet) { + return Transaction.execute(new TransactionCallback() { + @Override + public Vlan doInTransaction(TransactionStatus status) { + String newVlanNetmask = newVlanNetmaskFinal; + String newVlanGateway = newVlanGatewayFinal; + + if ((sameSubnet == null || sameSubnet.first() == false) && (network.getTrafficType()== TrafficType.Guest) && (network.getGuestType() == GuestType.Shared) && (_vlanDao.listVlansByNetworkId(networkId) != null)) { + Map dhcpCapabilities = _networkSvc.getNetworkOfferingServiceCapabilities(_networkOfferingDao.findById(network.getNetworkOfferingId()), Service.Dhcp); + String supportsMultipleSubnets = dhcpCapabilities.get(Capability.DhcpAccrossMultipleSubnets); + if (supportsMultipleSubnets == null || !Boolean.valueOf(supportsMultipleSubnets)) { + throw new InvalidParameterValueException("The Dhcp serivice provider for this network dose not support the dhcp across multiple subnets"); + } + s_logger.info("adding a new subnet to the network " + network.getId()); + } else if (sameSubnet != null) { + // if it is same subnet the user might not send the vlan and the + // netmask details. so we are + // figuring out while validation and setting them here. + newVlanGateway = sameSubnet.second().first(); + newVlanNetmask = sameSubnet.second().second(); + } + Vlan vlan = createVlanAndPublicIpRange(zoneId, networkId, physicalNetworkId, forVirtualNetwork, podId, startIP, + endIP, newVlanGateway, newVlanNetmask, vlanId, vlanOwner, startIPv6, endIPv6, ip6Gateway, ip6Cidr); + // create an entry in the nic_secondary table. This will be the new + // gateway that will be configured on the corresponding routervm. + return vlan; } - s_logger.info("adding a new subnet to the network " + network.getId()); - } else if (sameSubnet != null) { - // if it is same subnet the user might not send the vlan and the - // netmask details. so we are - // figuring out while validation and setting them here. - newVlanGateway = sameSubnet.second().first(); - newVlanNetmask = sameSubnet.second().second(); - } - Vlan vlan = createVlanAndPublicIpRange(zoneId, networkId, physicalNetworkId, forVirtualNetwork, podId, startIP, - endIP, newVlanGateway, newVlanNetmask, vlanId, vlanOwner, startIPv6, endIPv6, ip6Gateway, ip6Cidr); - // create an entry in the nic_secondary table. This will be the new - // gateway that will be configured on the corresponding routervm. - - txn.commit(); - - return vlan; + }); } public NetUtils.supersetOrSubset checkIfSubsetOrSuperset(String newVlanGateway, String newVlanNetmask, VlanVO vlan, String startIP, @@ -3021,52 +3047,62 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati } // Everything was fine, so persist the VLAN - Transaction txn = Transaction.currentTxn(); - txn.start(); - - VlanVO vlan = new VlanVO(vlanType, vlanId, vlanGateway, vlanNetmask, zone.getId(), ipRange, networkId, - physicalNetworkId, vlanIp6Gateway, vlanIp6Cidr, ipv6Range); - s_logger.debug("Saving vlan range " + vlan); - vlan = _vlanDao.persist(vlan); - - // IPv6 use a used ip map, is different from ipv4, no need to save - // public ip range - if (ipv4) { - if (!savePublicIPRange(startIP, endIP, zoneId, vlan.getId(), networkId, physicalNetworkId)) { - throw new CloudRuntimeException("Failed to save IPv4 range. Please contact Cloud Support."); - } - } - - if (vlanOwner != null) { - // This VLAN is account-specific, so create an AccountVlanMapVO - // entry - AccountVlanMapVO accountVlanMapVO = new AccountVlanMapVO(vlanOwner.getId(), vlan.getId()); - _accountVlanMapDao.persist(accountVlanMapVO); - - // generate usage event for dedication of every ip address in the - // range - List ips = _publicIpAddressDao.listByVlanId(vlan.getId()); - for (IPAddressVO ip : ips) { - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, vlanOwner.getId(), ip - .getDataCenterId(), ip.getId(), ip.getAddress().toString(), ip.isSourceNat(), vlan - .getVlanType().toString(), ip.getSystem(), ip.getClass().getName(), ip.getUuid()); - } - // increment resource count for dedicated public ip's - _resourceLimitMgr.incrementResourceCount(vlanOwner.getId(), ResourceType.public_ip, new Long(ips.size())); - } else if (podId != null) { - // This VLAN is pod-wide, so create a PodVlanMapVO entry - PodVlanMapVO podVlanMapVO = new PodVlanMapVO(podId, vlan.getId()); - _podVlanMapDao.persist(podVlanMapVO); - } - - txn.commit(); + VlanVO vlan = commitVlanAndIpRange(zoneId, networkId, physicalNetworkId, podId, startIP, endIP, vlanGateway, + vlanNetmask, vlanId, vlanOwner, vlanIp6Gateway, vlanIp6Cidr, ipv4, zone, vlanType, ipv6Range, ipRange); return vlan; } + private VlanVO commitVlanAndIpRange(final long zoneId, final long networkId, final long physicalNetworkId, final Long podId, + final String startIP, final String endIP, final String vlanGateway, final String vlanNetmask, final String vlanId, final Account vlanOwner, + final String vlanIp6Gateway, final String vlanIp6Cidr, final boolean ipv4, final DataCenterVO zone, final VlanType vlanType, + final String ipv6Range, final String ipRange) { + return Transaction.execute(new TransactionCallback() { + @Override + public VlanVO doInTransaction(TransactionStatus status) { + VlanVO vlan = new VlanVO(vlanType, vlanId, vlanGateway, vlanNetmask, zone.getId(), ipRange, networkId, + physicalNetworkId, vlanIp6Gateway, vlanIp6Cidr, ipv6Range); + s_logger.debug("Saving vlan range " + vlan); + vlan = _vlanDao.persist(vlan); + + // IPv6 use a used ip map, is different from ipv4, no need to save + // public ip range + if (ipv4) { + if (!savePublicIPRange(startIP, endIP, zoneId, vlan.getId(), networkId, physicalNetworkId)) { + throw new CloudRuntimeException("Failed to save IPv4 range. Please contact Cloud Support."); + } + } + + if (vlanOwner != null) { + // This VLAN is account-specific, so create an AccountVlanMapVO + // entry + AccountVlanMapVO accountVlanMapVO = new AccountVlanMapVO(vlanOwner.getId(), vlan.getId()); + _accountVlanMapDao.persist(accountVlanMapVO); + + // generate usage event for dedication of every ip address in the + // range + List ips = _publicIpAddressDao.listByVlanId(vlan.getId()); + for (IPAddressVO ip : ips) { + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, vlanOwner.getId(), ip + .getDataCenterId(), ip.getId(), ip.getAddress().toString(), ip.isSourceNat(), vlan + .getVlanType().toString(), ip.getSystem(), ip.getClass().getName(), ip.getUuid()); + } + // increment resource count for dedicated public ip's + _resourceLimitMgr.incrementResourceCount(vlanOwner.getId(), ResourceType.public_ip, new Long(ips.size())); + } else if (podId != null) { + // This VLAN is pod-wide, so create a PodVlanMapVO entry + PodVlanMapVO podVlanMapVO = new PodVlanMapVO(podId, vlan.getId()); + _podVlanMapDao.persist(podVlanMapVO); + } + return vlan; + } + }); + + } + @Override @DB - public boolean deleteVlanAndPublicIpRange(long userId, long vlanDbId, Account caller) { + public boolean deleteVlanAndPublicIpRange(long userId, final long vlanDbId, Account caller) { VlanVO vlanRange = _vlanDao.findById(vlanDbId); if (vlanRange == null) { throw new InvalidParameterValueException("Please specify a valid IP range id."); @@ -3153,12 +3189,13 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati } } - - Transaction txn = Transaction.currentTxn(); - txn.start(); - _publicIpAddressDao.deletePublicIPRange(vlanDbId); - _vlanDao.expunge(vlanDbId); - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _publicIpAddressDao.deletePublicIPRange(vlanDbId); + _vlanDao.expunge(vlanDbId); + } + }); return true; } @@ -3236,15 +3273,10 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati } } - Transaction txn = Transaction.currentTxn(); - txn.start(); - // Create an AccountVlanMapVO entry AccountVlanMapVO accountVlanMapVO = new AccountVlanMapVO(vlanOwner.getId(), vlan.getId()); _accountVlanMapDao.persist(accountVlanMapVO); - txn.commit(); - // generate usage event for dedication of every ip address in the range for (IPAddressVO ip : ips) { UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, vlanOwner.getId(), ip.getDataCenterId(), @@ -3339,16 +3371,20 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati } @DB - protected boolean savePublicIPRange(String startIP, String endIP, long zoneId, long vlanDbId, long sourceNetworkid, - long physicalNetworkId) { - long startIPLong = NetUtils.ip2Long(startIP); - long endIPLong = NetUtils.ip2Long(endIP); - Transaction txn = Transaction.currentTxn(); - txn.start(); - IPRangeConfig config = new IPRangeConfig(); - List problemIps = config.savePublicIPRange(txn, startIPLong, endIPLong, zoneId, vlanDbId, - sourceNetworkid, physicalNetworkId); - txn.commit(); + protected boolean savePublicIPRange(String startIP, String endIP, final long zoneId, final long vlanDbId, final long sourceNetworkid, + final long physicalNetworkId) { + final long startIPLong = NetUtils.ip2Long(startIP); + final long endIPLong = NetUtils.ip2Long(endIP); + + List problemIps = Transaction.execute(new TransactionCallback>() { + @Override + public List doInTransaction(TransactionStatus status) { + IPRangeConfig config = new IPRangeConfig(); + return config.savePublicIPRange(TransactionLegacy.currentTxn(), startIPLong, endIPLong, zoneId, vlanDbId, + sourceNetworkid, physicalNetworkId); + } + }); + return problemIps != null && problemIps.size() == 0; } @@ -3988,10 +4024,10 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati @DB public NetworkOfferingVO createNetworkOffering(String name, String displayText, TrafficType trafficType, String tags, boolean specifyVlan, Availability availability, Integer networkRate, - Map> serviceProviderMap, boolean isDefault, Network.GuestType type, + final Map> serviceProviderMap, boolean isDefault, Network.GuestType type, boolean systemOnly, Long serviceOfferingId, boolean conserveMode, Map> serviceCapabilityMap, boolean specifyIpRanges, boolean isPersistent, - Map details, boolean egressDefaultPolicy, Integer maxconn, boolean enableKeepAlive) { + final Map details, boolean egressDefaultPolicy, final Integer maxconn, final boolean enableKeepAlive) { String multicastRateStr = _configDao.getValue("multicast.throttling.rate"); int multicastRate = ((multicastRateStr == null) ? 10 : Integer.parseInt(multicastRateStr)); @@ -4131,13 +4167,13 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati publicLb = true; } - NetworkOfferingVO offering = new NetworkOfferingVO(name, displayText, trafficType, systemOnly, specifyVlan, + final NetworkOfferingVO offeringFinal = new NetworkOfferingVO(name, displayText, trafficType, systemOnly, specifyVlan, networkRate, multicastRate, isDefault, availability, tags, type, conserveMode, dedicatedLb, sharedSourceNat, redundantRouter, elasticIp, elasticLb, specifyIpRanges, inline, isPersistent, associatePublicIp, publicLb, internalLb, egressDefaultPolicy); if (serviceOfferingId != null) { - offering.setServiceOfferingId(serviceOfferingId); + offeringFinal.setServiceOfferingId(serviceOfferingId); } // validate the details @@ -4145,47 +4181,50 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati validateNtwkOffDetails(details, serviceProviderMap); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - // 1) create network offering object - s_logger.debug("Adding network offering " + offering); - offering.setConcurrentConnections(maxconn); - offering.setKeepAliveEnabled(enableKeepAlive); - offering = _networkOfferingDao.persist(offering, details); - // 2) populate services and providers - if (serviceProviderMap != null) { - for (Network.Service service : serviceProviderMap.keySet()) { - Set providers = serviceProviderMap.get(service); - if (providers != null && !providers.isEmpty()) { - boolean vpcOff = false; - for (Network.Provider provider : providers) { - if (provider == Provider.VPCVirtualRouter) { - vpcOff = true; + return Transaction.execute(new TransactionCallback() { + @Override + public NetworkOfferingVO doInTransaction(TransactionStatus status) { + NetworkOfferingVO offering = offeringFinal; + + // 1) create network offering object + s_logger.debug("Adding network offering " + offering); + offering.setConcurrentConnections(maxconn); + offering.setKeepAliveEnabled(enableKeepAlive); + offering = _networkOfferingDao.persist(offering, details); + // 2) populate services and providers + if (serviceProviderMap != null) { + for (Network.Service service : serviceProviderMap.keySet()) { + Set providers = serviceProviderMap.get(service); + if (providers != null && !providers.isEmpty()) { + boolean vpcOff = false; + for (Network.Provider provider : providers) { + if (provider == Provider.VPCVirtualRouter) { + vpcOff = true; + } + NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO(offering.getId(), + service, provider); + _ntwkOffServiceMapDao.persist(offService); + s_logger.trace("Added service for the network offering: " + offService + " with provider " + + provider.getName()); + } + + if (vpcOff) { + List supportedSvcs = new ArrayList(); + supportedSvcs.addAll(serviceProviderMap.keySet()); + _vpcMgr.validateNtwkOffForVpc(offering, supportedSvcs); + } + } else { + NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO(offering.getId(), service, + null); + _ntwkOffServiceMapDao.persist(offService); + s_logger.trace("Added service for the network offering: " + offService + " with null provider"); } - NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO(offering.getId(), - service, provider); - _ntwkOffServiceMapDao.persist(offService); - s_logger.trace("Added service for the network offering: " + offService + " with provider " - + provider.getName()); } - - if (vpcOff) { - List supportedSvcs = new ArrayList(); - supportedSvcs.addAll(serviceProviderMap.keySet()); - _vpcMgr.validateNtwkOffForVpc(offering, supportedSvcs); - } - } else { - NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO(offering.getId(), service, - null); - _ntwkOffServiceMapDao.persist(offService); - s_logger.trace("Added service for the network offering: " + offService + " with null provider"); } + + return offering; } - } - - txn.commit(); - - return offering; + }); } protected void validateNtwkOffDetails(Map details, Map> serviceProviderMap) { @@ -4712,27 +4751,29 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati @Override @DB - public boolean releaseAccountSpecificVirtualRanges(long accountId) { - List maps = _accountVlanMapDao.listAccountVlanMapsByAccount(accountId); - boolean result = true; + public boolean releaseAccountSpecificVirtualRanges(final long accountId) { + final List maps = _accountVlanMapDao.listAccountVlanMapsByAccount(accountId); if (maps != null && !maps.isEmpty()) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - for (AccountVlanMapVO map : maps) { - if (!releasePublicIpRange(map.getVlanDbId(), _accountMgr.getSystemUser().getId(), - _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM))) { - result = false; - } - } - if (result) { - txn.commit(); - } else { - s_logger.error("Failed to release account specific virtual ip ranges for account id=" + accountId); + try { + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (AccountVlanMapVO map : maps) { + if (!releasePublicIpRange(map.getVlanDbId(), _accountMgr.getSystemUser().getId(), + _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM))) { + throw new CloudRuntimeException("Failed to release account specific virtual ip ranges for account id=" + accountId); + } + } + } + }); + } catch ( CloudRuntimeException e ) { + s_logger.error(e); + return false; } } else { s_logger.trace("Account id=" + accountId + " has no account specific virtual ip ranges, nothing to release"); } - return result; + return true; } @Override @@ -4803,14 +4844,14 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati @ActionEvent(eventType = EventTypes.EVENT_PORTABLE_IP_RANGE_CREATE, eventDescription = "creating portable ip range", async = false) public PortableIpRange createPortableIpRange(CreatePortableIpRangeCmd cmd) throws ConcurrentOperationException { - Integer regionId = cmd.getRegionId(); - String startIP = cmd.getStartIp(); - String endIP = cmd.getEndIp(); - String gateway = cmd.getGateway(); - String netmask = cmd.getNetmask(); + final Integer regionId = cmd.getRegionId(); + final String startIP = cmd.getStartIp(); + final String endIP = cmd.getEndIp(); + final String gateway = cmd.getGateway(); + final String netmask = cmd.getNetmask(); String vlanId = cmd.getVlan(); - RegionVO region = _regionDao.findById(regionId); + final RegionVO region = _regionDao.findById(regionId); if (region == null) { throw new InvalidParameterValueException("Invalid region ID: " + regionId); } @@ -4856,28 +4897,33 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati } GlobalLock portableIpLock = GlobalLock.getInternLock("PortablePublicIpRange"); portableIpLock.lock(5); - Transaction txn = Transaction.currentTxn(); - txn.start(); + try { + final String vlanIdFinal = vlanId; + return Transaction.execute(new TransactionCallback() { + @Override + public PortableIpRangeVO doInTransaction(TransactionStatus status) { + PortableIpRangeVO portableIpRange = new PortableIpRangeVO(regionId, vlanIdFinal, gateway, netmask, startIP, endIP); + portableIpRange = _portableIpRangeDao.persist(portableIpRange); - PortableIpRangeVO portableIpRange = new PortableIpRangeVO(regionId, vlanId, gateway, netmask, startIP, endIP); - portableIpRange = _portableIpRangeDao.persist(portableIpRange); + long startIpLong = NetUtils.ip2Long(startIP); + long endIpLong = NetUtils.ip2Long(endIP); + while (startIpLong <= endIpLong) { + PortableIpVO portableIP = new PortableIpVO(regionId, portableIpRange.getId(), vlanIdFinal, gateway, netmask, + NetUtils.long2Ip(startIpLong)); + _portableIpDao.persist(portableIP); + startIpLong++; + } - long startIpLong = NetUtils.ip2Long(startIP); - long endIpLong = NetUtils.ip2Long(endIP); - while (startIpLong <= endIpLong) { - PortableIpVO portableIP = new PortableIpVO(regionId, portableIpRange.getId(), vlanId, gateway, netmask, - NetUtils.long2Ip(startIpLong)); - _portableIpDao.persist(portableIP); - startIpLong++; + // implicitly enable portable IP service for the region + region.setPortableipEnabled(true); + _regionDao.update(region.getId(), region); + + return portableIpRange; + } + }); + } finally { + portableIpLock.unlock(); } - - // implicitly enable portable IP service for the region - region.setPortableipEnabled(true); - _regionDao.update(region.getId(), region); - - txn.commit(); - portableIpLock.unlock(); - return portableIpRange; } @Override diff --git a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java index 030a31fb5e6..e55477a3f56 100755 --- a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java +++ b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java @@ -118,6 +118,8 @@ import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.QueryBuilder; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; import com.cloud.utils.events.SubscriptionMgr; @@ -569,10 +571,10 @@ VirtualMachineGuru, SystemVmLoadScanHandler, ResourceStateAdapter { } catch (ResourceUnavailableException e) { s_logger.warn("Exception while trying to start console proxy", e); return null; - } catch (CloudRuntimeException e) { + } catch (ConcurrentOperationException e) { s_logger.warn("Runtime Exception while trying to start console proxy", e); return null; - } catch (ConcurrentOperationException e) { + } catch (CloudRuntimeException e) { s_logger.warn("Runtime Exception while trying to start console proxy", e); return null; } catch (OperationTimedoutException e) { @@ -1049,25 +1051,24 @@ VirtualMachineGuru, SystemVmLoadScanHandler, ResourceStateAdapter { @Override @DB - public void setManagementState(ConsoleProxyManagementState state) { - Transaction txn = Transaction.currentTxn(); + public void setManagementState(final ConsoleProxyManagementState state) { try { - txn.start(); - - ConsoleProxyManagementState lastState = getManagementState(); + final ConsoleProxyManagementState lastState = getManagementState(); if (lastState == null) { - txn.commit(); return; } if (lastState != state) { - _configDao.update(Config.ConsoleProxyManagementLastState.key(), Config.ConsoleProxyManagementLastState.getCategory(), lastState.toString()); - _configDao.update(Config.ConsoleProxyManagementState.key(), Config.ConsoleProxyManagementState.getCategory(), state.toString()); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _configDao.update(Config.ConsoleProxyManagementLastState.key(), Config.ConsoleProxyManagementLastState.getCategory(), lastState.toString()); + _configDao.update(Config.ConsoleProxyManagementState.key(), Config.ConsoleProxyManagementState.getCategory(), state.toString()); + } + }); } - - txn.commit(); } catch (Throwable e) { - txn.rollback(); + s_logger.error("Failed to set managment state", e); } } @@ -1090,23 +1091,18 @@ VirtualMachineGuru, SystemVmLoadScanHandler, ResourceStateAdapter { @Override @DB public void resumeLastManagementState() { - Transaction txn = Transaction.currentTxn(); try { - txn.start(); ConsoleProxyManagementState state = getManagementState(); ConsoleProxyManagementState lastState = getLastManagementState(); if (lastState == null) { - txn.commit(); return; } if (lastState != state) { _configDao.update(Config.ConsoleProxyManagementState.key(), Config.ConsoleProxyManagementState.getCategory(), lastState.toString()); } - - txn.commit(); } catch (Throwable e) { - txn.rollback(); + s_logger.error("Failed to resume last management state", e); } } diff --git a/server/src/com/cloud/dc/dao/DedicatedResourceDaoImpl.java b/server/src/com/cloud/dc/dao/DedicatedResourceDaoImpl.java index 1936b85015e..3902847c4f9 100644 --- a/server/src/com/cloud/dc/dao/DedicatedResourceDaoImpl.java +++ b/server/src/com/cloud/dc/dao/DedicatedResourceDaoImpl.java @@ -29,6 +29,7 @@ import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; @@ -338,7 +339,7 @@ public class DedicatedResourceDaoImpl extends GenericDaoBase() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + final PlannerHostReservationVO lockedEntry = _plannerHostReserveDao.lockRow(id, true); + if (lockedEntry == null) { + s_logger.error("Unable to lock the host entry for reservation, host: " + hostId); return false; } + // check before updating + if (lockedEntry.getResourceUsage() == null) { + lockedEntry.setResourceUsage(resourceUsageRequired); + _plannerHostReserveDao.persist(lockedEntry); + return true; + } else { + // someone updated it earlier. check if we can still use it + if (lockedEntry.getResourceUsage() == resourceUsageRequired) { + return true; + } else { + s_logger.debug("Cannot use this host for usage: " + resourceUsageRequired + + ", since this host has been reserved for planner usage : " + hostResourceTypeFinal); + return false; + } + } } - } finally { - txn.commit(); - } + }); + } } @@ -600,7 +602,7 @@ public class DeploymentPlanningManagerImpl extends ManagerBase implements Deploy } @DB - public boolean checkHostReservationRelease(Long hostId) { + public boolean checkHostReservationRelease(final Long hostId) { if (hostId != null) { PlannerHostReservationVO reservationEntry = _plannerHostReserveDao.findByHostId(hostId); @@ -662,26 +664,26 @@ public class DeploymentPlanningManagerImpl extends ManagerBase implements Deploy s_logger.debug("Host has no VMs associated, releasing the planner reservation for host " + hostId); } - long id = reservationEntry.getId(); - final Transaction txn = Transaction.currentTxn(); + final long id = reservationEntry.getId(); - try { - txn.start(); + return Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + final PlannerHostReservationVO lockedEntry = _plannerHostReserveDao.lockRow(id, true); + if (lockedEntry == null) { + s_logger.error("Unable to lock the host entry for reservation, host: " + hostId); + return false; + } + // check before updating + if (lockedEntry.getResourceUsage() != null) { + lockedEntry.setResourceUsage(null); + _plannerHostReserveDao.persist(lockedEntry); + return true; + } - final PlannerHostReservationVO lockedEntry = _plannerHostReserveDao.lockRow(id, true); - if (lockedEntry == null) { - s_logger.error("Unable to lock the host entry for reservation, host: " + hostId); return false; } - // check before updating - if (lockedEntry.getResourceUsage() != null) { - lockedEntry.setResourceUsage(null); - _plannerHostReserveDao.persist(lockedEntry); - return true; - } - } finally { - txn.commit(); - } + }); } } @@ -1251,50 +1253,51 @@ public class DeploymentPlanningManagerImpl extends ManagerBase implements Deploy @DB @Override - public String finalizeReservation(DeployDestination plannedDestination, - VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoids) + public String finalizeReservation(final DeployDestination plannedDestination, + final VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoids) throws InsufficientServerCapacityException, AffinityConflictException { - VirtualMachine vm = vmProfile.getVirtualMachine(); - long vmGroupCount = _affinityGroupVMMapDao.countAffinityGroupsForVm(vm.getId()); + final VirtualMachine vm = vmProfile.getVirtualMachine(); + final long vmGroupCount = _affinityGroupVMMapDao.countAffinityGroupsForVm(vm.getId()); - boolean saveReservation = true; - final Transaction txn = Transaction.currentTxn(); - try { - txn.start(); - if (vmGroupCount > 0) { - List groupIds = _affinityGroupVMMapDao.listAffinityGroupIdsByVmId(vm.getId()); - SearchCriteria criteria = _affinityGroupDao.createSearchCriteria(); - criteria.addAnd("id", SearchCriteria.Op.IN, groupIds.toArray(new Object[groupIds.size()])); - List groups = _affinityGroupDao.lockRows(criteria, null, true); + return Transaction.execute(new TransactionCallback() { + @Override + public String doInTransaction(TransactionStatus status) { + boolean saveReservation = true; - for (AffinityGroupProcessor processor : _affinityProcessors) { - if (!processor.check(vmProfile, plannedDestination)) { - saveReservation = false; - break; + if (vmGroupCount > 0) { + List groupIds = _affinityGroupVMMapDao.listAffinityGroupIdsByVmId(vm.getId()); + SearchCriteria criteria = _affinityGroupDao.createSearchCriteria(); + criteria.addAnd("id", SearchCriteria.Op.IN, groupIds.toArray(new Object[groupIds.size()])); + List groups = _affinityGroupDao.lockRows(criteria, null, true); + + for (AffinityGroupProcessor processor : _affinityProcessors) { + if (!processor.check(vmProfile, plannedDestination)) { + saveReservation = false; + break; + } } } - } - if (saveReservation) { - VMReservationVO vmReservation = new VMReservationVO(vm.getId(), plannedDestination.getDataCenter() - .getId(), plannedDestination.getPod().getId(), plannedDestination.getCluster().getId(), - plannedDestination.getHost().getId()); - Map volumeReservationMap = new HashMap(); + if (saveReservation) { + VMReservationVO vmReservation = new VMReservationVO(vm.getId(), plannedDestination.getDataCenter() + .getId(), plannedDestination.getPod().getId(), plannedDestination.getCluster().getId(), + plannedDestination.getHost().getId()); + Map volumeReservationMap = new HashMap(); - if (vm.getHypervisorType() != HypervisorType.BareMetal) { - for (Volume vo : plannedDestination.getStorageForDisks().keySet()) { - volumeReservationMap.put(vo.getId(), plannedDestination.getStorageForDisks().get(vo).getId()); + if (vm.getHypervisorType() != HypervisorType.BareMetal) { + for (Volume vo : plannedDestination.getStorageForDisks().keySet()) { + volumeReservationMap.put(vo.getId(), plannedDestination.getStorageForDisks().get(vo).getId()); + } + vmReservation.setVolumeReservation(volumeReservationMap); } - vmReservation.setVolumeReservation(volumeReservationMap); + _reservationDao.persist(vmReservation); + return vmReservation.getUuid(); } - _reservationDao.persist(vmReservation); - return vmReservation.getUuid(); + + return null; } - } finally { - txn.commit(); - } - return null; + }); } @Override diff --git a/server/src/com/cloud/ha/HighAvailabilityManagerExtImpl.java b/server/src/com/cloud/ha/HighAvailabilityManagerExtImpl.java index 1107a7a031d..801e7dac572 100644 --- a/server/src/com/cloud/ha/HighAvailabilityManagerExtImpl.java +++ b/server/src/com/cloud/ha/HighAvailabilityManagerExtImpl.java @@ -24,14 +24,13 @@ import javax.ejb.Local; import javax.inject.Inject; import javax.naming.ConfigurationException; -import org.springframework.context.annotation.Primary; -import org.springframework.stereotype.Component; import org.apache.cloudstack.framework.config.dao.ConfigurationDao; import org.apache.cloudstack.managed.context.ManagedContextRunnable; import com.cloud.alert.AlertManager; import com.cloud.usage.dao.UsageJobDao; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Local(value={HighAvailabilityManager.class}) public class HighAvailabilityManagerExtImpl extends HighAvailabilityManagerImpl { @@ -74,7 +73,7 @@ public class HighAvailabilityManagerExtImpl extends HighAvailabilityManagerImpl try { boolean isRunning = false; - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); try { Date lastHeartbeat = _usageJobDao.getLastHeartbeat(); if (lastHeartbeat != null) { @@ -91,7 +90,7 @@ public class HighAvailabilityManagerExtImpl extends HighAvailabilityManagerImpl txn.close(); // switch back to VMOPS db - Transaction swap = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy swap = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); swap.close(); } diff --git a/server/src/com/cloud/ha/dao/HighAvailabilityDaoImpl.java b/server/src/com/cloud/ha/dao/HighAvailabilityDaoImpl.java index 83a71b80299..d800483b472 100644 --- a/server/src/com/cloud/ha/dao/HighAvailabilityDaoImpl.java +++ b/server/src/com/cloud/ha/dao/HighAvailabilityDaoImpl.java @@ -33,6 +33,7 @@ import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; @Component @@ -130,7 +131,7 @@ public class HighAvailabilityDaoImpl extends GenericDaoBase impl @Override public HaWorkVO take(final long serverId) { - final Transaction txn = Transaction.currentTxn(); + final TransactionLegacy txn = TransactionLegacy.currentTxn(); try { final SearchCriteria sc = TBASearch.create(); sc.setParameters("time", System.currentTimeMillis() >> 10); diff --git a/server/src/com/cloud/metadata/ResourceMetaDataManagerImpl.java b/server/src/com/cloud/metadata/ResourceMetaDataManagerImpl.java index 444650b7750..875bfac7131 100644 --- a/server/src/com/cloud/metadata/ResourceMetaDataManagerImpl.java +++ b/server/src/com/cloud/metadata/ResourceMetaDataManagerImpl.java @@ -62,6 +62,8 @@ import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDao; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionStatus; import com.cloud.uuididentity.dao.IdentityDao; import com.cloud.vm.NicDetailVO; import com.cloud.vm.dao.NicDao; @@ -192,51 +194,51 @@ public class ResourceMetaDataManagerImpl extends ManagerBase implements Resource @Override @DB @ActionEvent(eventType = EventTypes.EVENT_RESOURCE_DETAILS_CREATE, eventDescription = "creating resource meta data") - public boolean addResourceMetaData(String resourceId, TaggedResourceType resourceType, Map details){ + public boolean addResourceMetaData(final String resourceId, final TaggedResourceType resourceType, final Map details){ + return Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + for (String key : details.keySet()) { + Long id = _taggedResourceMgr.getResourceId(resourceId, resourceType); - Transaction txn = Transaction.currentTxn(); - txn.start(); + //check if object exists + if (_daoMap.get(resourceType).findById(id) == null) { + throw new InvalidParameterValueException("Unable to find resource by id " + resourceId + + " and type " + resourceType); + } - for (String key : details.keySet()) { - Long id = _taggedResourceMgr.getResourceId(resourceId, resourceType); + String value = details.get(key); + + if (value == null || value.isEmpty()) { + throw new InvalidParameterValueException("Value for the key " + key + " is either null or empty"); + } + + // TODO - Have a better design here. + if(resourceType == TaggedResourceType.Volume){ + VolumeDetailVO v = new VolumeDetailVO(id, key, value); + _volumeDetailDao.persist(v); + } else if (resourceType == TaggedResourceType.Nic){ + NicDetailVO n = new NicDetailVO(id, key, value); + _nicDetailDao.persist(n); + } else if (resourceType == TaggedResourceType.Zone){ + DcDetailVO dataCenterDetail = new DcDetailVO(id, key, value); + _dcDetailsDao.persist(dataCenterDetail); + } else if (resourceType == TaggedResourceType.Network){ + NetworkDetailVO networkDetail = new NetworkDetailVO(id, key, value); + _networkDetailsDao.persist(networkDetail); + } else if (resourceType == TaggedResourceType.UserVm) { + _userVmDetailsDao.addVmDetail(id, key, value); + } else if (resourceType == TaggedResourceType.Template) { + _templateDetailsDao.addTemplateDetail(id, key, value); + } else{ + throw new InvalidParameterValueException("The resource type " + resourceType + " is not supported by the API yet"); + } - //check if object exists - if (_daoMap.get(resourceType).findById(id) == null) { - throw new InvalidParameterValueException("Unable to find resource by id " + resourceId + - " and type " + resourceType); } - String value = details.get(key); - - if (value == null || value.isEmpty()) { - throw new InvalidParameterValueException("Value for the key " + key + " is either null or empty"); - } - - // TODO - Have a better design here. - if (resourceType == TaggedResourceType.Volume){ - VolumeDetailVO v = new VolumeDetailVO(id, key, value); - _volumeDetailDao.persist(v); - } else if (resourceType == TaggedResourceType.Nic){ - NicDetailVO n = new NicDetailVO(id, key, value); - _nicDetailDao.persist(n); - } else if (resourceType == TaggedResourceType.Zone){ - DcDetailVO dataCenterDetail = new DcDetailVO(id, key, value); - _dcDetailsDao.persist(dataCenterDetail); - } else if (resourceType == TaggedResourceType.Network){ - NetworkDetailVO networkDetail = new NetworkDetailVO(id, key, value); - _networkDetailsDao.persist(networkDetail); - } else if (resourceType == TaggedResourceType.UserVm) { - _userVmDetailsDao.addVmDetail(id, key, value); - } else if (resourceType == TaggedResourceType.Template) { - _templateDetailsDao.addTemplateDetail(id, key, value); - } else{ - throw new InvalidParameterValueException("The resource type " + resourceType + " is not supported by the API yet"); - } - } - - txn.commit(); - - return true; + return true; + } + }); } diff --git a/server/src/com/cloud/network/ExternalDeviceUsageManagerImpl.java b/server/src/com/cloud/network/ExternalDeviceUsageManagerImpl.java index 00dd25c4a86..cd3532e837d 100644 --- a/server/src/com/cloud/network/ExternalDeviceUsageManagerImpl.java +++ b/server/src/com/cloud/network/ExternalDeviceUsageManagerImpl.java @@ -81,8 +81,9 @@ import com.cloud.utils.component.ManagerBase; import com.cloud.utils.concurrency.NamedThreadFactory; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; -import com.cloud.utils.exception.ExecutionException; import com.cloud.vm.DomainRouterVO; import com.cloud.vm.NicVO; import com.cloud.vm.dao.DomainRouterDao; @@ -288,12 +289,19 @@ public class ExternalDeviceUsageManagerImpl extends ManagerBase implements Exter newCurrentBytesReceived += bytesSentAndReceived[1]; } - UserStatisticsVO userStats; - final Transaction txn = Transaction.currentTxn(); - try { - txn.start(); - userStats = _userStatsDao.lock(accountId, zone.getId(), networkId, publicIp, externalLoadBalancer.getId(), externalLoadBalancer.getType().toString()); + commitStats(networkId, externalLoadBalancer, accountId, publicIp, zone, statsEntryIdentifier, + newCurrentBytesSent, newCurrentBytesReceived); + } + } + private void commitStats(final long networkId, final HostVO externalLoadBalancer, final long accountId, final String publicIp, + final DataCenterVO zone, final String statsEntryIdentifier, final long newCurrentBytesSent, final long newCurrentBytesReceived) { + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + UserStatisticsVO userStats; + userStats = _userStatsDao.lock(accountId, zone.getId(), networkId, publicIp, externalLoadBalancer.getId(), externalLoadBalancer.getType().toString()); + if (userStats != null) { long oldNetBytesSent = userStats.getNetBytesSent(); long oldNetBytesReceived = userStats.getNetBytesReceived(); @@ -301,19 +309,19 @@ public class ExternalDeviceUsageManagerImpl extends ManagerBase implements Exter long oldCurrentBytesReceived = userStats.getCurrentBytesReceived(); String warning = "Received an external network stats byte count that was less than the stored value. Zone ID: " + userStats.getDataCenterId() + ", account ID: " + userStats.getAccountId() + "."; - + userStats.setCurrentBytesSent(newCurrentBytesSent); if (oldCurrentBytesSent > newCurrentBytesSent) { s_logger.warn(warning + "Stored bytes sent: " + oldCurrentBytesSent + ", new bytes sent: " + newCurrentBytesSent + "."); userStats.setNetBytesSent(oldNetBytesSent + oldCurrentBytesSent); } - + userStats.setCurrentBytesReceived(newCurrentBytesReceived); if (oldCurrentBytesReceived > newCurrentBytesReceived) { s_logger.warn(warning + "Stored bytes received: " + oldCurrentBytesReceived + ", new bytes received: " + newCurrentBytesReceived + "."); userStats.setNetBytesReceived(oldNetBytesReceived + oldCurrentBytesReceived); } - + if (_userStatsDao.update(userStats.getId(), userStats)) { s_logger.debug("Successfully updated stats for " + statsEntryIdentifier); } else { @@ -322,13 +330,8 @@ public class ExternalDeviceUsageManagerImpl extends ManagerBase implements Exter } else { s_logger.warn("Unable to find user stats entry for " + statsEntryIdentifier); } - - txn.commit(); - } catch (final Exception e) { - txn.rollback(); - throw new CloudRuntimeException("Problem getting stats after reboot/stop ", e); } - } + }); } protected class ExternalDeviceNetworkUsageTask extends ManagedContextRunnable { @@ -601,78 +604,78 @@ public class ExternalDeviceUsageManagerImpl extends ManagerBase implements Exter * Stats entries are created for source NAT IP addresses, static NAT rules, port forwarding rules, and load * balancing rules */ - private boolean manageStatsEntries(boolean create, long accountId, long zoneId, Network network, - HostVO externalFirewall, ExternalNetworkResourceUsageAnswer firewallAnswer, - HostVO externalLoadBalancer, ExternalNetworkResourceUsageAnswer lbAnswer) { - String accountErrorMsg = "Failed to update external network stats entry. Details: account ID = " + accountId; - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + private boolean manageStatsEntries(final boolean create, final long accountId, final long zoneId, final Network network, + final HostVO externalFirewall, final ExternalNetworkResourceUsageAnswer firewallAnswer, + final HostVO externalLoadBalancer, final ExternalNetworkResourceUsageAnswer lbAnswer) { + final String accountErrorMsg = "Failed to update external network stats entry. Details: account ID = " + accountId; try { - txn.start(); - String networkErrorMsg = accountErrorMsg + ", network ID = " + network.getId(); - - boolean sharedSourceNat = false; - Map sourceNatCapabilities = _networkModel.getNetworkServiceCapabilities(network.getId(), Network.Service.SourceNat); - if (sourceNatCapabilities != null) { - String supportedSourceNatTypes = sourceNatCapabilities.get(Network.Capability.SupportedSourceNatTypes).toLowerCase(); - if (supportedSourceNatTypes.contains("zone")) { - sharedSourceNat = true; - } - } - - if (externalFirewall != null && firewallAnswer != null) { - if (!sharedSourceNat) { - // Manage the entry for this network's source NAT IP address - List sourceNatIps = _ipAddressDao.listByAssociatedNetwork(network.getId(), true); - if (sourceNatIps.size() == 1) { - String publicIp = sourceNatIps.get(0).getAddress().addr(); - if (!createOrUpdateStatsEntry(create, accountId, zoneId, network.getId(), publicIp, externalFirewall.getId(), firewallAnswer, false)) { - throw new ExecutionException(networkErrorMsg + ", source NAT IP = " + publicIp); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + String networkErrorMsg = accountErrorMsg + ", network ID = " + network.getId(); + + boolean sharedSourceNat = false; + Map sourceNatCapabilities = _networkModel.getNetworkServiceCapabilities(network.getId(), Network.Service.SourceNat); + if (sourceNatCapabilities != null) { + String supportedSourceNatTypes = sourceNatCapabilities.get(Network.Capability.SupportedSourceNatTypes).toLowerCase(); + if (supportedSourceNatTypes.contains("zone")) { + sharedSourceNat = true; } } - - // Manage one entry for each static NAT rule in this network - List staticNatIps = _ipAddressDao.listStaticNatPublicIps(network.getId()); - for (IPAddressVO staticNatIp : staticNatIps) { - String publicIp = staticNatIp.getAddress().addr(); - if (!createOrUpdateStatsEntry(create, accountId, zoneId, network.getId(), publicIp, externalFirewall.getId(), firewallAnswer, false)) { - throw new ExecutionException(networkErrorMsg + ", static NAT rule public IP = " + publicIp); + + if (externalFirewall != null && firewallAnswer != null) { + if (!sharedSourceNat) { + // Manage the entry for this network's source NAT IP address + List sourceNatIps = _ipAddressDao.listByAssociatedNetwork(network.getId(), true); + if (sourceNatIps.size() == 1) { + String publicIp = sourceNatIps.get(0).getAddress().addr(); + if (!createOrUpdateStatsEntry(create, accountId, zoneId, network.getId(), publicIp, externalFirewall.getId(), firewallAnswer, false)) { + throw new CloudRuntimeException(networkErrorMsg + ", source NAT IP = " + publicIp); + } + } + + // Manage one entry for each static NAT rule in this network + List staticNatIps = _ipAddressDao.listStaticNatPublicIps(network.getId()); + for (IPAddressVO staticNatIp : staticNatIps) { + String publicIp = staticNatIp.getAddress().addr(); + if (!createOrUpdateStatsEntry(create, accountId, zoneId, network.getId(), publicIp, externalFirewall.getId(), firewallAnswer, false)) { + throw new CloudRuntimeException(networkErrorMsg + ", static NAT rule public IP = " + publicIp); + } + } + + // Manage one entry for each port forwarding rule in this network + List portForwardingRules = _portForwardingRulesDao.listByNetwork(network.getId()); + for (PortForwardingRuleVO portForwardingRule : portForwardingRules) { + String publicIp = _networkModel.getIp(portForwardingRule.getSourceIpAddressId()).getAddress().addr(); + if (!createOrUpdateStatsEntry(create, accountId, zoneId, network.getId(), publicIp, externalFirewall.getId(), firewallAnswer, false)) { + throw new CloudRuntimeException(networkErrorMsg + ", port forwarding rule public IP = " + publicIp); + } + } + } else { + // Manage the account-wide entry for the external firewall + if (!createOrUpdateStatsEntry(create, accountId, zoneId, network.getId(), null, externalFirewall.getId(), firewallAnswer, false)) { + throw new CloudRuntimeException(networkErrorMsg); + } } } - - // Manage one entry for each port forwarding rule in this network - List portForwardingRules = _portForwardingRulesDao.listByNetwork(network.getId()); - for (PortForwardingRuleVO portForwardingRule : portForwardingRules) { - String publicIp = _networkModel.getIp(portForwardingRule.getSourceIpAddressId()).getAddress().addr(); - if (!createOrUpdateStatsEntry(create, accountId, zoneId, network.getId(), publicIp, externalFirewall.getId(), firewallAnswer, false)) { - throw new ExecutionException(networkErrorMsg + ", port forwarding rule public IP = " + publicIp); + + // If an external load balancer is added, manage one entry for each load balancing rule in this network + if (externalLoadBalancer != null && lbAnswer != null) { + boolean inline = _networkModel.isNetworkInlineMode(network); + List loadBalancers = _loadBalancerDao.listByNetworkIdAndScheme(network.getId(), Scheme.Public); + for (LoadBalancerVO loadBalancer : loadBalancers) { + String publicIp = _networkModel.getIp(loadBalancer.getSourceIpAddressId()).getAddress().addr(); + if (!createOrUpdateStatsEntry(create, accountId, zoneId, network.getId(), publicIp, externalLoadBalancer.getId(), lbAnswer, inline)) { + throw new CloudRuntimeException(networkErrorMsg + ", load balancing rule public IP = " + publicIp); + } } } - } else { - // Manage the account-wide entry for the external firewall - if (!createOrUpdateStatsEntry(create, accountId, zoneId, network.getId(), null, externalFirewall.getId(), firewallAnswer, false)) { - throw new ExecutionException(networkErrorMsg); - } } - } - - // If an external load balancer is added, manage one entry for each load balancing rule in this network - if (externalLoadBalancer != null && lbAnswer != null) { - boolean inline = _networkModel.isNetworkInlineMode(network); - List loadBalancers = _loadBalancerDao.listByNetworkIdAndScheme(network.getId(), Scheme.Public); - for (LoadBalancerVO loadBalancer : loadBalancers) { - String publicIp = _networkModel.getIp(loadBalancer.getSourceIpAddressId()).getAddress().addr(); - if (!createOrUpdateStatsEntry(create, accountId, zoneId, network.getId(), publicIp, externalLoadBalancer.getId(), lbAnswer, inline)) { - throw new ExecutionException(networkErrorMsg + ", load balancing rule public IP = " + publicIp); - } - } - } - return txn.commit(); + }); + return true; } catch (Exception e) { s_logger.warn("Exception: ", e); - txn.rollback(); return false; - } finally { - txn.close(); } } } diff --git a/server/src/com/cloud/network/ExternalFirewallDeviceManagerImpl.java b/server/src/com/cloud/network/ExternalFirewallDeviceManagerImpl.java index 629bef5eca1..32744ef187a 100644 --- a/server/src/com/cloud/network/ExternalFirewallDeviceManagerImpl.java +++ b/server/src/com/cloud/network/ExternalFirewallDeviceManagerImpl.java @@ -26,7 +26,6 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; - import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.response.ExternalFirewallResponse; import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; @@ -108,6 +107,8 @@ import com.cloud.utils.component.AdapterBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; import com.cloud.utils.net.UrlUtil; @@ -159,7 +160,7 @@ public abstract class ExternalFirewallDeviceManagerImpl extends AdapterBase impl @Override @DB - public ExternalFirewallDeviceVO addExternalFirewall(long physicalNetworkId, String url, String username, String password, String deviceName, ServerResource resource) { + public ExternalFirewallDeviceVO addExternalFirewall(long physicalNetworkId, String url, String username, String password, final String deviceName, ServerResource resource) { String guid; PhysicalNetworkVO pNetwork = null; NetworkDevice ntwkDevice = NetworkDevice.getNetworkDevice(deviceName); @@ -176,7 +177,7 @@ public abstract class ExternalFirewallDeviceManagerImpl extends AdapterBase impl } zoneId = pNetwork.getDataCenterId(); - PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao.findByServiceProvider(pNetwork.getId(), ntwkDevice.getNetworkServiceProvder()); + final PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao.findByServiceProvider(pNetwork.getId(), ntwkDevice.getNetworkServiceProvder()); if (ntwkSvcProvider == null ) { throw new CloudRuntimeException("Network Service Provider: " + ntwkDevice.getNetworkServiceProvder() + " is not enabled in the physical network: " + physicalNetworkId + "to add this device" ); @@ -204,7 +205,7 @@ public abstract class ExternalFirewallDeviceManagerImpl extends AdapterBase impl hostDetails.put("username", username); hostDetails.put("password", password); hostDetails.put("deviceName", deviceName); - Map configParams = new HashMap(); + final Map configParams = new HashMap(); UrlUtil.parseQueryParameters(uri.getQuery(), false, configParams); hostDetails.putAll(configParams); @@ -215,27 +216,29 @@ public abstract class ExternalFirewallDeviceManagerImpl extends AdapterBase impl throw new CloudRuntimeException(e.getMessage()); } - Host externalFirewall = _resourceMgr.addHost(zoneId, resource, Host.Type.ExternalFirewall, hostDetails); + final Host externalFirewall = _resourceMgr.addHost(zoneId, resource, Host.Type.ExternalFirewall, hostDetails); if (externalFirewall != null) { - Transaction txn = Transaction.currentTxn(); - txn.start(); + final PhysicalNetworkVO pNetworkFinal = pNetwork; + return Transaction.execute(new TransactionCallback() { + @Override + public ExternalFirewallDeviceVO doInTransaction(TransactionStatus status) { + boolean dedicatedUse = (configParams.get(ApiConstants.FIREWALL_DEVICE_DEDICATED) != null) ? Boolean.parseBoolean(configParams.get(ApiConstants.FIREWALL_DEVICE_DEDICATED)) : false; + long capacity = NumbersUtil.parseLong(configParams.get(ApiConstants.FIREWALL_DEVICE_CAPACITY), 0); + if (capacity == 0) { + capacity = _defaultFwCapacity; + } - boolean dedicatedUse = (configParams.get(ApiConstants.FIREWALL_DEVICE_DEDICATED) != null) ? Boolean.parseBoolean(configParams.get(ApiConstants.FIREWALL_DEVICE_DEDICATED)) : false; - long capacity = NumbersUtil.parseLong(configParams.get(ApiConstants.FIREWALL_DEVICE_CAPACITY), 0); - if (capacity == 0) { - capacity = _defaultFwCapacity; - } + ExternalFirewallDeviceVO fwDevice = new ExternalFirewallDeviceVO(externalFirewall.getId(), pNetworkFinal.getId(), ntwkSvcProvider.getProviderName(), + deviceName, capacity, dedicatedUse); - ExternalFirewallDeviceVO fwDevice = new ExternalFirewallDeviceVO(externalFirewall.getId(), pNetwork.getId(), ntwkSvcProvider.getProviderName(), - deviceName, capacity, dedicatedUse); + _externalFirewallDeviceDao.persist(fwDevice); - _externalFirewallDeviceDao.persist(fwDevice); + DetailVO hostDetail = new DetailVO(externalFirewall.getId(), ApiConstants.FIREWALL_DEVICE_ID, String.valueOf(fwDevice.getId())); + _hostDetailDao.persist(hostDetail); - DetailVO hostDetail = new DetailVO(externalFirewall.getId(), ApiConstants.FIREWALL_DEVICE_ID, String.valueOf(fwDevice.getId())); - _hostDetailDao.persist(hostDetail); - - txn.commit(); - return fwDevice; + return fwDevice; + } + }); } else { return null; } @@ -343,7 +346,6 @@ public abstract class ExternalFirewallDeviceManagerImpl extends AdapterBase impl @DB protected boolean freeFirewallForNetwork(Network network) { - Transaction txn = Transaction.currentTxn(); GlobalLock deviceMapLock = GlobalLock.getInternLock("NetworkFirewallDeviceMap"); try { if (deviceMapLock.lock(120)) { @@ -353,7 +355,6 @@ public abstract class ExternalFirewallDeviceManagerImpl extends AdapterBase impl _networkExternalFirewallDao.remove(fwDeviceForNetwork.getId()); } } catch (Exception exception) { - txn.rollback(); s_logger.error("Failed to release firewall device for the network" + network.getId() + " due to " + exception.getMessage()); return false; } finally { @@ -363,7 +364,6 @@ public abstract class ExternalFirewallDeviceManagerImpl extends AdapterBase impl } finally { deviceMapLock.releaseRef(); } - txn.commit(); return true; } diff --git a/server/src/com/cloud/network/ExternalLoadBalancerDeviceManagerImpl.java b/server/src/com/cloud/network/ExternalLoadBalancerDeviceManagerImpl.java index dd4893030fe..6ca40c01e98 100644 --- a/server/src/com/cloud/network/ExternalLoadBalancerDeviceManagerImpl.java +++ b/server/src/com/cloud/network/ExternalLoadBalancerDeviceManagerImpl.java @@ -27,7 +27,6 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; - import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.response.ExternalLoadBalancerResponse; import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; @@ -118,6 +117,9 @@ import com.cloud.utils.component.AdapterBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; import com.cloud.utils.net.UrlUtil; @@ -196,11 +198,11 @@ public abstract class ExternalLoadBalancerDeviceManagerImpl extends AdapterBase @Override @DB public ExternalLoadBalancerDeviceVO addExternalLoadBalancer(long physicalNetworkId, String url, - String username, String password, String deviceName, ServerResource resource, boolean gslbProvider, - String gslbSitePublicIp, String gslbSitePrivateIp) { + String username, String password, final String deviceName, ServerResource resource, final boolean gslbProvider, + final String gslbSitePublicIp, final String gslbSitePrivateIp) { PhysicalNetworkVO pNetwork = null; - NetworkDevice ntwkDevice = NetworkDevice.getNetworkDevice(deviceName); + final NetworkDevice ntwkDevice = NetworkDevice.getNetworkDevice(deviceName); long zoneId; if ((ntwkDevice == null) || (url == null) || (username == null) || (resource == null) || (password == null)) { @@ -258,39 +260,41 @@ public abstract class ExternalLoadBalancerDeviceManagerImpl extends AdapterBase UrlUtil.parseQueryParameters(uri.getQuery(), false, configParams); hostDetails.putAll(configParams); - Transaction txn = Transaction.currentTxn(); try { resource.configure(hostName, hostDetails); - Host host = _resourceMgr.addHost(zoneId, resource, Host.Type.ExternalLoadBalancer, hostDetails); + final Host host = _resourceMgr.addHost(zoneId, resource, Host.Type.ExternalLoadBalancer, hostDetails); if (host != null) { - boolean dedicatedUse = (configParams.get(ApiConstants.LOAD_BALANCER_DEVICE_DEDICATED) != null) ? Boolean.parseBoolean(configParams + final boolean dedicatedUse = (configParams.get(ApiConstants.LOAD_BALANCER_DEVICE_DEDICATED) != null) ? Boolean.parseBoolean(configParams .get(ApiConstants.LOAD_BALANCER_DEVICE_DEDICATED)) : false; long capacity = NumbersUtil.parseLong(configParams.get(ApiConstants.LOAD_BALANCER_DEVICE_CAPACITY), 0); if (capacity == 0) { capacity = _defaultLbCapacity; } - ExternalLoadBalancerDeviceVO lbDeviceVO; - txn.start(); - lbDeviceVO = new ExternalLoadBalancerDeviceVO(host.getId(), pNetwork.getId(), ntwkDevice.getNetworkServiceProvder(), - deviceName, capacity, dedicatedUse, gslbProvider); - if (gslbProvider) { - lbDeviceVO.setGslbSitePublicIP(gslbSitePublicIp); - lbDeviceVO.setGslbSitePrivateIP(gslbSitePrivateIp); - } - _externalLoadBalancerDeviceDao.persist(lbDeviceVO); - DetailVO hostDetail = new DetailVO(host.getId(), ApiConstants.LOAD_BALANCER_DEVICE_ID, String.valueOf(lbDeviceVO.getId())); - _hostDetailDao.persist(hostDetail); + final long capacityFinal = capacity; + final PhysicalNetworkVO pNetworkFinal = pNetwork; + return Transaction.execute(new TransactionCallback() { + @Override + public ExternalLoadBalancerDeviceVO doInTransaction(TransactionStatus status) { + ExternalLoadBalancerDeviceVO lbDeviceVO = new ExternalLoadBalancerDeviceVO(host.getId(), pNetworkFinal.getId(), ntwkDevice.getNetworkServiceProvder(), + deviceName, capacityFinal, dedicatedUse, gslbProvider); + if (gslbProvider) { + lbDeviceVO.setGslbSitePublicIP(gslbSitePublicIp); + lbDeviceVO.setGslbSitePrivateIP(gslbSitePrivateIp); + } + _externalLoadBalancerDeviceDao.persist(lbDeviceVO); + DetailVO hostDetail = new DetailVO(host.getId(), ApiConstants.LOAD_BALANCER_DEVICE_ID, String.valueOf(lbDeviceVO.getId())); + _hostDetailDao.persist(hostDetail); - txn.commit(); - return lbDeviceVO; + return lbDeviceVO; + } + }); } else { throw new CloudRuntimeException("Failed to add load balancer device due to internal error."); } } catch (ConfigurationException e) { - txn.rollback(); throw new CloudRuntimeException(e.getMessage()); } } @@ -402,7 +406,7 @@ public abstract class ExternalLoadBalancerDeviceManagerImpl extends AdapterBase } @DB - protected ExternalLoadBalancerDeviceVO allocateLoadBalancerForNetwork(Network guestConfig) throws InsufficientCapacityException { + protected ExternalLoadBalancerDeviceVO allocateLoadBalancerForNetwork(final Network guestConfig) throws InsufficientCapacityException { boolean retry = true; boolean tryLbProvisioning = false; ExternalLoadBalancerDeviceVO lbDevice = null; @@ -412,34 +416,33 @@ public abstract class ExternalLoadBalancerDeviceManagerImpl extends AdapterBase while (retry) { GlobalLock deviceMapLock = GlobalLock.getInternLock("LoadBalancerAllocLock"); - Transaction txn = Transaction.currentTxn(); try { if (deviceMapLock.lock(120)) { try { - boolean dedicatedLB = offering.getDedicatedLB(); // does network offering supports a dedicated -// load balancer? - long lbDeviceId; + final boolean dedicatedLB = offering.getDedicatedLB(); // does network offering supports a dedicated load balancer? - txn.start(); try { - // FIXME: should the device allocation be done during network implement phase or do a - // lazy allocation when first rule for the network is configured?? - - // find a load balancer device for this network as per the network offering - lbDevice = findSuitableLoadBalancerForNetwork(guestConfig, dedicatedLB); - lbDeviceId = lbDevice.getId(); - - // persist the load balancer device id that will be used for this network. Once a network - // is implemented on a LB device then later on all rules will be programmed on to same -// device - NetworkExternalLoadBalancerVO networkLB = new NetworkExternalLoadBalancerVO(guestConfig.getId(), lbDeviceId); - _networkExternalLBDao.persist(networkLB); - - // mark device to be either dedicated or shared use - lbDevice.setAllocationState(dedicatedLB ? LBDeviceAllocationState.Dedicated : LBDeviceAllocationState.Shared); - _externalLoadBalancerDeviceDao.update(lbDeviceId, lbDevice); - - txn.commit(); + lbDevice = Transaction.execute(new TransactionCallbackWithException() { + @Override + public ExternalLoadBalancerDeviceVO doInTransaction(TransactionStatus status) throws InsufficientCapacityException { + // FIXME: should the device allocation be done during network implement phase or do a + // lazy allocation when first rule for the network is configured?? + + // find a load balancer device for this network as per the network offering + ExternalLoadBalancerDeviceVO lbDevice = findSuitableLoadBalancerForNetwork(guestConfig, dedicatedLB); + long lbDeviceId = lbDevice.getId(); + + // persist the load balancer device id that will be used for this network. Once a network + // is implemented on a LB device then later on all rules will be programmed on to same device + NetworkExternalLoadBalancerVO networkLB = new NetworkExternalLoadBalancerVO(guestConfig.getId(), lbDeviceId); + _networkExternalLBDao.persist(networkLB); + + // mark device to be either dedicated or shared use + lbDevice.setAllocationState(dedicatedLB ? LBDeviceAllocationState.Dedicated : LBDeviceAllocationState.Shared); + _externalLoadBalancerDeviceDao.update(lbDeviceId, lbDevice); + return lbDevice; + } + }); // allocated load balancer for the network, so skip retry tryLbProvisioning = false; @@ -448,28 +451,22 @@ public abstract class ExternalLoadBalancerDeviceManagerImpl extends AdapterBase // if already attempted to provision load balancer then throw out of capacity exception, if (tryLbProvisioning) { retry = false; - // TODO: throwing warning instead of error for now as its possible another provider can -// service this network + // TODO: throwing warning instead of error for now as its possible another provider can service this network s_logger.warn("There are no load balancer device with the capacity for implementing this network"); throw exception; } else { - tryLbProvisioning = true; // if possible provision a LB appliance in to the physical -// network + tryLbProvisioning = true; // if possible provision a LB appliance in to the physical network } } } finally { deviceMapLock.unlock(); - if (lbDevice == null) { - txn.rollback(); - } } } } finally { deviceMapLock.releaseRef(); } - // there are no LB devices or there is no free capacity on the devices in the physical network so provision -// a new LB appliance + // there are no LB devices or there is no free capacity on the devices in the physical network so provision a new LB appliance if (tryLbProvisioning) { // check if LB appliance can be dynamically provisioned List providerLbDevices = _externalLoadBalancerDeviceDao.listByProviderAndDeviceAllocationState(physicalNetworkId, provider, @@ -477,8 +474,7 @@ public abstract class ExternalLoadBalancerDeviceManagerImpl extends AdapterBase if ((providerLbDevices != null) && (!providerLbDevices.isEmpty())) { for (ExternalLoadBalancerDeviceVO lbProviderDevice : providerLbDevices) { if (lbProviderDevice.getState() == LBDeviceState.Enabled) { - // acquire a private IP from the data center which will be used as management IP of -// provisioned LB appliance, + // acquire a private IP from the data center which will be used as management IP of provisioned LB appliance, DataCenterIpAddressVO dcPrivateIp = _dcDao.allocatePrivateIpAddress(guestConfig.getDataCenterId(), lbProviderDevice.getUuid()); if (dcPrivateIp == null) { throw new InsufficientNetworkCapacityException("failed to acquire a priavate IP in the zone " + guestConfig.getDataCenterId() + @@ -509,13 +505,11 @@ public abstract class ExternalLoadBalancerDeviceManagerImpl extends AdapterBase String publicIf = createLbAnswer.getPublicInterface(); String privateIf = createLbAnswer.getPrivateInterface(); - // we have provisioned load balancer so add the appliance as cloudstack provisioned external -// load balancer + // we have provisioned load balancer so add the appliance as cloudstack provisioned external load balancer String dedicatedLb = offering.getDedicatedLB() ? "true" : "false"; String capacity = Long.toString(lbProviderDevice.getCapacity()); - // acquire a public IP to associate with lb appliance (used as subnet IP to make the -// appliance part of private network) + // acquire a public IP to associate with lb appliance (used as subnet IP to make the appliance part of private network) PublicIp publicIp = _ipAddrMgr.assignPublicIpAddress(guestConfig.getDataCenterId(), null, _accountMgr.getSystemAccount(), @@ -542,8 +536,7 @@ public abstract class ExternalLoadBalancerDeviceManagerImpl extends AdapterBase } if (lbAppliance != null) { - // mark the load balancer as cloudstack managed and set parent host id on which lb -// appliance is provisioned + // mark the load balancer as cloudstack managed and set parent host id on which lb appliance is provisioned ExternalLoadBalancerDeviceVO managedLb = _externalLoadBalancerDeviceDao.findById(lbAppliance.getId()); managedLb.setIsManagedDevice(true); managedLb.setParentHostId(lbProviderDevice.getHostId()); @@ -557,8 +550,7 @@ public abstract class ExternalLoadBalancerDeviceManagerImpl extends AdapterBase if (answer == null || !answer.getResult()) { s_logger.warn("Failed to destroy load balancer appliance created"); } else { - // release the public & private IP back to dc pool, as the load balancer -// appliance is now destroyed + // release the public & private IP back to dc pool, as the load balancer appliance is now destroyed _dcDao.releasePrivateIpAddress(lbIP, guestConfig.getDataCenterId(), null); _ipAddrMgr.disassociatePublicIpAddress(publicIp.getId(), _accountMgr.getSystemUser().getId(), _accountMgr.getSystemAccount()); } @@ -656,34 +648,40 @@ public abstract class ExternalLoadBalancerDeviceManagerImpl extends AdapterBase } @DB - protected boolean freeLoadBalancerForNetwork(Network guestConfig) { - Transaction txn = Transaction.currentTxn(); + protected boolean freeLoadBalancerForNetwork(final Network guestConfig) { GlobalLock deviceMapLock = GlobalLock.getInternLock("LoadBalancerAllocLock"); try { if (deviceMapLock.lock(120)) { - txn.start(); - // since network is shutdown remove the network mapping to the load balancer device - NetworkExternalLoadBalancerVO networkLBDevice = _networkExternalLBDao.findByNetworkId(guestConfig.getId()); - long lbDeviceId = networkLBDevice.getExternalLBDeviceId(); - _networkExternalLBDao.remove(networkLBDevice.getId()); + ExternalLoadBalancerDeviceVO lbDevice = Transaction.execute(new TransactionCallback() { + @Override + public ExternalLoadBalancerDeviceVO doInTransaction(TransactionStatus status) { + // since network is shutdown remove the network mapping to the load balancer device + NetworkExternalLoadBalancerVO networkLBDevice = _networkExternalLBDao.findByNetworkId(guestConfig.getId()); + long lbDeviceId = networkLBDevice.getExternalLBDeviceId(); + _networkExternalLBDao.remove(networkLBDevice.getId()); - List ntwksMapped = _networkExternalLBDao.listByLoadBalancerDeviceId(networkLBDevice.getExternalLBDeviceId()); - ExternalLoadBalancerDeviceVO lbDevice = _externalLoadBalancerDeviceDao.findById(lbDeviceId); - boolean lbInUse = !(ntwksMapped == null || ntwksMapped.isEmpty()); - boolean lbCloudManaged = lbDevice.getIsManagedDevice(); + List ntwksMapped = _networkExternalLBDao.listByLoadBalancerDeviceId(networkLBDevice.getExternalLBDeviceId()); + ExternalLoadBalancerDeviceVO lbDevice = _externalLoadBalancerDeviceDao.findById(lbDeviceId); + boolean lbInUse = !(ntwksMapped == null || ntwksMapped.isEmpty()); + boolean lbCloudManaged = lbDevice.getIsManagedDevice(); - if (!lbInUse && !lbCloudManaged) { - // this is the last network mapped to the load balancer device so set device allocation state to be -// free - lbDevice.setAllocationState(LBDeviceAllocationState.Free); - _externalLoadBalancerDeviceDao.update(lbDevice.getId(), lbDevice); - } + if (!lbInUse && !lbCloudManaged) { + // this is the last network mapped to the load balancer device so set device allocation state to be free + lbDevice.setAllocationState(LBDeviceAllocationState.Free); + _externalLoadBalancerDeviceDao.update(lbDevice.getId(), lbDevice); + } - // commit the changes before sending agent command to destroy cloudstack managed LB - txn.commit(); + // commit the changes before sending agent command to destroy cloudstack managed LB + if (!lbInUse && lbCloudManaged) { + return lbDevice; + } else { + return null; + } + } + }); - if (!lbInUse && lbCloudManaged) { + if (lbDevice != null) { // send DestroyLoadBalancerApplianceCommand to the host where load balancer appliance is provisioned Host lbHost = _hostDao.findById(lbDevice.getHostId()); String lbIP = lbHost.getPrivateIpAddress(); @@ -723,7 +721,6 @@ public abstract class ExternalLoadBalancerDeviceManagerImpl extends AdapterBase return false; } } catch (Exception exception) { - txn.rollback(); s_logger.error("Failed to release load balancer device for the network" + guestConfig.getId() + " due to " + exception.getMessage()); } finally { deviceMapLock.releaseRef(); diff --git a/server/src/com/cloud/network/IpAddressManagerImpl.java b/server/src/com/cloud/network/IpAddressManagerImpl.java index 74f316bb12c..8619ae178cd 100644 --- a/server/src/com/cloud/network/IpAddressManagerImpl.java +++ b/server/src/com/cloud/network/IpAddressManagerImpl.java @@ -29,7 +29,6 @@ import java.util.UUID; import javax.inject.Inject; import org.apache.log4j.Logger; - import org.apache.cloudstack.acl.ControlledEntity.ACLType; import org.apache.cloudstack.acl.SecurityChecker.AccessType; import org.apache.cloudstack.context.CallContext; @@ -133,17 +132,24 @@ import com.cloud.user.dao.AccountDao; import com.cloud.user.dao.UserDao; import com.cloud.utils.Journal; import com.cloud.utils.Pair; +import com.cloud.utils.Ternary; import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.EntityManager; import com.cloud.utils.db.Filter; import com.cloud.utils.db.GlobalLock; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithException; import com.cloud.utils.db.JoinBuilder.JoinType; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.exception.ExceptionUtil; import com.cloud.utils.net.Ip; import com.cloud.utils.net.NetUtils; import com.cloud.vm.Nic; @@ -609,28 +615,30 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage @DB @Override - public boolean releasePortableIpAddress(long addrId) { - Transaction txn = Transaction.currentTxn(); - GlobalLock portableIpLock = GlobalLock.getInternLock("PortablePublicIpRange"); + public boolean releasePortableIpAddress(final long addrId) { + final GlobalLock portableIpLock = GlobalLock.getInternLock("PortablePublicIpRange"); - txn.start(); try { - portableIpLock.lock(5); - IPAddressVO ip = _ipAddressDao.findById(addrId); + return Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + portableIpLock.lock(5); + IPAddressVO ip = _ipAddressDao.findById(addrId); - // unassign portable IP - PortableIpVO portableIp = _portableIpDao.findByIpAddress(ip.getAddress().addr()); - _portableIpDao.unassignIpAddress(portableIp.getId()); + // unassign portable IP + PortableIpVO portableIp = _portableIpDao.findByIpAddress(ip.getAddress().addr()); + _portableIpDao.unassignIpAddress(portableIp.getId()); - // removed the provisioned vlan - VlanVO vlan = _vlanDao.findById(ip.getVlanId()); - _vlanDao.remove(vlan.getId()); + // removed the provisioned vlan + VlanVO vlan = _vlanDao.findById(ip.getVlanId()); + _vlanDao.remove(vlan.getId()); - // remove the provisioned public ip address - _ipAddressDao.remove(ip.getId()); + // remove the provisioned public ip address + _ipAddressDao.remove(ip.getId()); - txn.commit(); - return true; + return true; + } + }); } finally { portableIpLock.releaseRef(); } @@ -649,135 +657,138 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage } @DB - public PublicIp fetchNewPublicIp(long dcId, Long podId, List vlanDbIds, Account owner, VlanType vlanUse, Long guestNetworkId, boolean sourceNat, boolean assign, - String requestedIp, boolean isSystem, Long vpcId) throws InsufficientAddressCapacityException { - StringBuilder errorMessage = new StringBuilder("Unable to get ip adress in "); - boolean fetchFromDedicatedRange = false; - List dedicatedVlanDbIds = new ArrayList(); - List nonDedicatedVlanDbIds = new ArrayList(); + public PublicIp fetchNewPublicIp(final long dcId, final Long podId, final List vlanDbIds, final Account owner, final VlanType vlanUse, final Long guestNetworkId, final boolean sourceNat, final boolean assign, + final String requestedIp, final boolean isSystem, final Long vpcId) throws InsufficientAddressCapacityException { + IPAddressVO addr = Transaction.execute(new TransactionCallbackWithException() { + @Override + public IPAddressVO doInTransaction(TransactionStatus status) throws InsufficientAddressCapacityException { + StringBuilder errorMessage = new StringBuilder("Unable to get ip adress in "); + boolean fetchFromDedicatedRange = false; + List dedicatedVlanDbIds = new ArrayList(); + List nonDedicatedVlanDbIds = new ArrayList(); - Transaction txn = Transaction.currentTxn(); - txn.start(); - SearchCriteria sc = null; - if (podId != null) { - sc = AssignIpAddressFromPodVlanSearch.create(); - sc.setJoinParameters("podVlanMapSB", "podId", podId); - errorMessage.append(" pod id=" + podId); - } else { - sc = AssignIpAddressSearch.create(); - errorMessage.append(" zone id=" + dcId); - } + SearchCriteria sc = null; + if (podId != null) { + sc = AssignIpAddressFromPodVlanSearch.create(); + sc.setJoinParameters("podVlanMapSB", "podId", podId); + errorMessage.append(" pod id=" + podId); + } else { + sc = AssignIpAddressSearch.create(); + errorMessage.append(" zone id=" + dcId); + } - // If owner has dedicated Public IP ranges, fetch IP from the dedicated range - // Otherwise fetch IP from the system pool - List maps = _accountVlanMapDao.listAccountVlanMapsByAccount(owner.getId()); - for (AccountVlanMapVO map : maps) { - if (vlanDbIds == null || vlanDbIds.contains(map.getVlanDbId())) - dedicatedVlanDbIds.add(map.getVlanDbId()); - } - List nonDedicatedVlans = _vlanDao.listZoneWideNonDedicatedVlans(dcId); - for (VlanVO nonDedicatedVlan : nonDedicatedVlans) { - if (vlanDbIds == null || vlanDbIds.contains(nonDedicatedVlan.getId())) - nonDedicatedVlanDbIds.add(nonDedicatedVlan.getId()); - } - if (dedicatedVlanDbIds != null && !dedicatedVlanDbIds.isEmpty()) { - fetchFromDedicatedRange = true; - sc.setParameters("vlanId", dedicatedVlanDbIds.toArray()); - errorMessage.append(", vlanId id=" + dedicatedVlanDbIds.toArray()); - } else if (nonDedicatedVlanDbIds != null && !nonDedicatedVlanDbIds.isEmpty()) { - sc.setParameters("vlanId", nonDedicatedVlanDbIds.toArray()); - errorMessage.append(", vlanId id=" + nonDedicatedVlanDbIds.toArray()); - } else { - if (podId != null) { - InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", Pod.class, podId); - ex.addProxyObject(ApiDBUtils.findPodById(podId).getUuid()); - throw ex; + // If owner has dedicated Public IP ranges, fetch IP from the dedicated range + // Otherwise fetch IP from the system pool + List maps = _accountVlanMapDao.listAccountVlanMapsByAccount(owner.getId()); + for (AccountVlanMapVO map : maps) { + if (vlanDbIds == null || vlanDbIds.contains(map.getVlanDbId())) + dedicatedVlanDbIds.add(map.getVlanDbId()); + } + List nonDedicatedVlans = _vlanDao.listZoneWideNonDedicatedVlans(dcId); + for (VlanVO nonDedicatedVlan : nonDedicatedVlans) { + if (vlanDbIds == null || vlanDbIds.contains(nonDedicatedVlan.getId())) + nonDedicatedVlanDbIds.add(nonDedicatedVlan.getId()); + } + if (dedicatedVlanDbIds != null && !dedicatedVlanDbIds.isEmpty()) { + fetchFromDedicatedRange = true; + sc.setParameters("vlanId", dedicatedVlanDbIds.toArray()); + errorMessage.append(", vlanId id=" + dedicatedVlanDbIds.toArray()); + } else if (nonDedicatedVlanDbIds != null && !nonDedicatedVlanDbIds.isEmpty()) { + sc.setParameters("vlanId", nonDedicatedVlanDbIds.toArray()); + errorMessage.append(", vlanId id=" + nonDedicatedVlanDbIds.toArray()); + } else { + if (podId != null) { + InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", Pod.class, podId); + ex.addProxyObject(ApiDBUtils.findPodById(podId).getUuid()); + throw ex; + } + s_logger.warn(errorMessage.toString()); + InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", DataCenter.class, dcId); + ex.addProxyObject(ApiDBUtils.findZoneById(dcId).getUuid()); + throw ex; + } + + sc.setParameters("dc", dcId); + + DataCenter zone = _entityMgr.findById(DataCenter.class, dcId); + + // for direct network take ip addresses only from the vlans belonging to the network + if (vlanUse == VlanType.DirectAttached) { + sc.setJoinParameters("vlan", "networkId", guestNetworkId); + errorMessage.append(", network id=" + guestNetworkId); + } + sc.setJoinParameters("vlan", "type", vlanUse); + + if (requestedIp != null) { + sc.addAnd("address", SearchCriteria.Op.EQ, requestedIp); + errorMessage.append(": requested ip " + requestedIp + " is not available"); + } + + Filter filter = new Filter(IPAddressVO.class, "vlanId", true, 0l, 1l); + + List addrs = _ipAddressDao.lockRows(sc, filter, true); + + // If all the dedicated IPs of the owner are in use fetch an IP from the system pool + if (addrs.size() == 0 && fetchFromDedicatedRange) { + // Verify if account is allowed to acquire IPs from the system + boolean useSystemIps = UseSystemPublicIps.valueIn(owner.getId()); + if (useSystemIps && nonDedicatedVlanDbIds != null && !nonDedicatedVlanDbIds.isEmpty()) { + fetchFromDedicatedRange = false; + sc.setParameters("vlanId", nonDedicatedVlanDbIds.toArray()); + errorMessage.append(", vlanId id=" + nonDedicatedVlanDbIds.toArray()); + addrs = _ipAddressDao.lockRows(sc, filter, true); + } + } + + if (addrs.size() == 0) { + if (podId != null) { + InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", Pod.class, podId); + // for now, we hardcode the table names, but we should ideally do a lookup for the tablename from the VO object. + ex.addProxyObject(ApiDBUtils.findPodById(podId).getUuid()); + throw ex; + } + s_logger.warn(errorMessage.toString()); + InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", DataCenter.class, dcId); + ex.addProxyObject(ApiDBUtils.findZoneById(dcId).getUuid()); + throw ex; + } + + assert (addrs.size() == 1) : "Return size is incorrect: " + addrs.size(); + + if (!fetchFromDedicatedRange) { + // Check that the maximum number of public IPs for the given accountId will not be exceeded + try { + _resourceLimitMgr.checkResourceLimit(owner, ResourceType.public_ip); + } catch (ResourceAllocationException ex) { + s_logger.warn("Failed to allocate resource of type " + ex.getResourceType() + " for account " + owner); + throw new AccountLimitException("Maximum number of public IP addresses for account: " + owner.getAccountName() + " has been exceeded."); + } + } + + IPAddressVO addr = addrs.get(0); + addr.setSourceNat(sourceNat); + addr.setAllocatedTime(new Date()); + addr.setAllocatedInDomainId(owner.getDomainId()); + addr.setAllocatedToAccountId(owner.getId()); + addr.setSystem(isSystem); + + if (assign) { + markPublicIpAsAllocated(addr); + } else { + addr.setState(IpAddress.State.Allocating); + } + addr.setState(assign ? IpAddress.State.Allocated : IpAddress.State.Allocating); + + if (vlanUse != VlanType.DirectAttached) { + addr.setAssociatedWithNetworkId(guestNetworkId); + addr.setVpcId(vpcId); + } + + _ipAddressDao.update(addr.getId(), addr); + + return addr; } - s_logger.warn(errorMessage.toString()); - InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", DataCenter.class, dcId); - ex.addProxyObject(ApiDBUtils.findZoneById(dcId).getUuid()); - throw ex; - } - - sc.setParameters("dc", dcId); - - DataCenter zone = _entityMgr.findById(DataCenter.class, dcId); - - // for direct network take ip addresses only from the vlans belonging to the network - if (vlanUse == VlanType.DirectAttached) { - sc.setJoinParameters("vlan", "networkId", guestNetworkId); - errorMessage.append(", network id=" + guestNetworkId); - } - sc.setJoinParameters("vlan", "type", vlanUse); - - if (requestedIp != null) { - sc.addAnd("address", SearchCriteria.Op.EQ, requestedIp); - errorMessage.append(": requested ip " + requestedIp + " is not available"); - } - - Filter filter = new Filter(IPAddressVO.class, "vlanId", true, 0l, 1l); - - List addrs = _ipAddressDao.lockRows(sc, filter, true); - - // If all the dedicated IPs of the owner are in use fetch an IP from the system pool - if (addrs.size() == 0 && fetchFromDedicatedRange) { - // Verify if account is allowed to acquire IPs from the system - boolean useSystemIps = UseSystemPublicIps.valueIn(owner.getId()); - if (useSystemIps && nonDedicatedVlanDbIds != null && !nonDedicatedVlanDbIds.isEmpty()) { - fetchFromDedicatedRange = false; - sc.setParameters("vlanId", nonDedicatedVlanDbIds.toArray()); - errorMessage.append(", vlanId id=" + nonDedicatedVlanDbIds.toArray()); - addrs = _ipAddressDao.lockRows(sc, filter, true); - } - } - - if (addrs.size() == 0) { - if (podId != null) { - InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", Pod.class, podId); - // for now, we hardcode the table names, but we should ideally do a lookup for the tablename from the VO object. - ex.addProxyObject(ApiDBUtils.findPodById(podId).getUuid()); - throw ex; - } - s_logger.warn(errorMessage.toString()); - InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", DataCenter.class, dcId); - ex.addProxyObject(ApiDBUtils.findZoneById(dcId).getUuid()); - throw ex; - } - - assert (addrs.size() == 1) : "Return size is incorrect: " + addrs.size(); - - if (!fetchFromDedicatedRange) { - // Check that the maximum number of public IPs for the given accountId will not be exceeded - try { - _resourceLimitMgr.checkResourceLimit(owner, ResourceType.public_ip); - } catch (ResourceAllocationException ex) { - s_logger.warn("Failed to allocate resource of type " + ex.getResourceType() + " for account " + owner); - throw new AccountLimitException("Maximum number of public IP addresses for account: " + owner.getAccountName() + " has been exceeded."); - } - } - - IPAddressVO addr = addrs.get(0); - addr.setSourceNat(sourceNat); - addr.setAllocatedTime(new Date()); - addr.setAllocatedInDomainId(owner.getDomainId()); - addr.setAllocatedToAccountId(owner.getId()); - addr.setSystem(isSystem); - - if (assign) { - markPublicIpAsAllocated(addr); - } else { - addr.setState(IpAddress.State.Allocating); - } - addr.setState(assign ? IpAddress.State.Allocated : IpAddress.State.Allocating); - - if (vlanUse != VlanType.DirectAttached) { - addr.setAssociatedWithNetworkId(guestNetworkId); - addr.setVpcId(vpcId); - } - - _ipAddressDao.update(addr.getId(), addr); - - txn.commit(); + }); if (vlanUse == VlanType.VirtualNetwork) { _firewallMgr.addSystemFirewallRules(addr, owner); @@ -788,44 +799,43 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage @DB @Override - public void markPublicIpAsAllocated(IPAddressVO addr) { + public void markPublicIpAsAllocated(final IPAddressVO addr) { assert (addr.getState() == IpAddress.State.Allocating || addr.getState() == IpAddress.State.Free) : "Unable to transition from state " + addr.getState() + " to " + IpAddress.State.Allocated; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + Account owner = _accountMgr.getAccount(addr.getAllocatedToAccountId()); - Transaction txn = Transaction.currentTxn(); + addr.setState(IpAddress.State.Allocated); + _ipAddressDao.update(addr.getId(), addr); - Account owner = _accountMgr.getAccount(addr.getAllocatedToAccountId()); + // Save usage event + if (owner.getAccountId() != Account.ACCOUNT_ID_SYSTEM) { + VlanVO vlan = _vlanDao.findById(addr.getVlanId()); - txn.start(); - addr.setState(IpAddress.State.Allocated); - _ipAddressDao.update(addr.getId(), addr); + String guestType = vlan.getVlanType().toString(); - // Save usage event - if (owner.getAccountId() != Account.ACCOUNT_ID_SYSTEM) { - VlanVO vlan = _vlanDao.findById(addr.getVlanId()); + if (!isIpDedicated(addr)) { + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, + owner.getId(), + addr.getDataCenterId(), + addr.getId(), + addr.getAddress().toString(), + addr.isSourceNat(), + guestType, + addr.getSystem(), + addr.getClass().getName(), + addr.getUuid()); + } - String guestType = vlan.getVlanType().toString(); - - if (!isIpDedicated(addr)) { - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, - owner.getId(), - addr.getDataCenterId(), - addr.getId(), - addr.getAddress().toString(), - addr.isSourceNat(), - guestType, - addr.getSystem(), - addr.getClass().getName(), - addr.getUuid()); + if (updateIpResourceCount(addr)) { + _resourceLimitMgr.incrementResourceCount(owner.getId(), ResourceType.public_ip); + } + } } - - if (updateIpResourceCount(addr)) { - _resourceLimitMgr.incrementResourceCount(owner.getId(), ResourceType.public_ip); - } - } - - txn.commit(); + }); } private boolean isIpDedicated(IPAddressVO addr) { @@ -855,35 +865,38 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage @DB @Override - public PublicIp assignDedicateIpAddress(Account owner, Long guestNtwkId, Long vpcId, long dcId, boolean isSourceNat) throws ConcurrentOperationException, + public PublicIp assignDedicateIpAddress(Account owner, final Long guestNtwkId, final Long vpcId, final long dcId, final boolean isSourceNat) throws ConcurrentOperationException, InsufficientAddressCapacityException { - long ownerId = owner.getId(); + final long ownerId = owner.getId(); PublicIp ip = null; - Transaction txn = Transaction.currentTxn(); try { - txn.start(); + ip = Transaction.execute(new TransactionCallbackWithException() { + @Override + public PublicIp doInTransaction(TransactionStatus status) throws InsufficientAddressCapacityException { + Account owner = _accountDao.acquireInLockTable(ownerId); - owner = _accountDao.acquireInLockTable(ownerId); + if (owner == null) { + // this ownerId comes from owner or type Account. See the class "AccountVO" and the annotations in that class + // to get the table name and field name that is queried to fill this ownerid. + ConcurrentOperationException ex = new ConcurrentOperationException("Unable to lock account"); + throw ex; + } + if (s_logger.isDebugEnabled()) { + s_logger.debug("lock account " + ownerId + " is acquired"); + } - if (owner == null) { - // this ownerId comes from owner or type Account. See the class "AccountVO" and the annotations in that class - // to get the table name and field name that is queried to fill this ownerid. - ConcurrentOperationException ex = new ConcurrentOperationException("Unable to lock account"); - throw ex; - } - if (s_logger.isDebugEnabled()) { - s_logger.debug("lock account " + ownerId + " is acquired"); - } + PublicIp ip = fetchNewPublicIp(dcId, null, null, owner, VlanType.VirtualNetwork, guestNtwkId, isSourceNat, false, null, false, vpcId); + IPAddressVO publicIp = ip.ip(); - ip = fetchNewPublicIp(dcId, null, null, owner, VlanType.VirtualNetwork, guestNtwkId, isSourceNat, false, null, false, vpcId); - IPAddressVO publicIp = ip.ip(); + markPublicIpAsAllocated(publicIp); + _ipAddressDao.update(publicIp.getId(), publicIp); - markPublicIpAsAllocated(publicIp); - _ipAddressDao.update(publicIp.getId(), publicIp); + return ip; + } + }); - txn.commit(); return ip; } finally { if (owner != null) { @@ -894,7 +907,6 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage _accountDao.releaseFromLockTable(ownerId); } if (ip == null) { - txn.rollback(); s_logger.error("Unable to get source nat ip address for account " + ownerId); } } @@ -977,11 +989,11 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage @DB @Override - public IpAddress allocateIp(Account ipOwner, boolean isSystem, Account caller, long callerUserId, DataCenter zone) throws ConcurrentOperationException, + public IpAddress allocateIp(final Account ipOwner, final boolean isSystem, Account caller, long callerUserId, final DataCenter zone) throws ConcurrentOperationException, ResourceAllocationException, InsufficientAddressCapacityException { - VlanType vlanType = VlanType.VirtualNetwork; - boolean assign = false; + final VlanType vlanType = VlanType.VirtualNetwork; + final boolean assign = false; if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(caller.getType())) { // zone is of type DataCenter. See DataCenterVO.java. @@ -992,7 +1004,6 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage PublicIp ip = null; - Transaction txn = Transaction.currentTxn(); Account accountToLock = null; try { if (s_logger.isDebugEnabled()) { @@ -1008,21 +1019,25 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage s_logger.debug("Associate IP address lock acquired"); } - txn.start(); + ip = Transaction.execute(new TransactionCallbackWithException() { + @Override + public PublicIp doInTransaction(TransactionStatus status) throws InsufficientAddressCapacityException { + PublicIp ip = fetchNewPublicIp(zone.getId(), null, null, ipOwner, vlanType, null, false, assign, null, isSystem, null); - ip = fetchNewPublicIp(zone.getId(), null, null, ipOwner, vlanType, null, false, assign, null, isSystem, null); + if (ip == null) { + InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Unable to find available public IP addresses", DataCenter.class, zone.getId()); + ex.addProxyObject(ApiDBUtils.findZoneById(zone.getId()).getUuid()); + throw ex; + } + CallContext.current().setEventDetails("Ip Id: " + ip.getId()); + Ip ipAddress = ip.getAddress(); - if (ip == null) { - InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Unable to find available public IP addresses", DataCenter.class, zone.getId()); - ex.addProxyObject(ApiDBUtils.findZoneById(zone.getId()).getUuid()); - throw ex; - } - CallContext.current().setEventDetails("Ip Id: " + ip.getId()); - Ip ipAddress = ip.getAddress(); + s_logger.debug("Got " + ipAddress + " to assign for account " + ipOwner.getId() + " in zone " + zone.getId()); - s_logger.debug("Got " + ipAddress + " to assign for account " + ipOwner.getId() + " in zone " + zone.getId()); + return ip; + } + }); - txn.commit(); } finally { if (accountToLock != null) { if (s_logger.isDebugEnabled()) { @@ -1037,75 +1052,77 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage @Override @DB - public IpAddress allocatePortableIp(Account ipOwner, Account caller, long dcId, Long networkId, Long vpcID) throws ConcurrentOperationException, ResourceAllocationException, + public IpAddress allocatePortableIp(final Account ipOwner, Account caller, final long dcId, final Long networkId, final Long vpcID) throws ConcurrentOperationException, ResourceAllocationException, InsufficientAddressCapacityException { - Transaction txn = Transaction.currentTxn(); GlobalLock portableIpLock = GlobalLock.getInternLock("PortablePublicIpRange"); - PortableIpVO allocatedPortableIp; IPAddressVO ipaddr; try { portableIpLock.lock(5); - txn.start(); + ipaddr = Transaction.execute(new TransactionCallbackWithException() { + @Override + public IPAddressVO doInTransaction(TransactionStatus status) throws InsufficientAddressCapacityException { + PortableIpVO allocatedPortableIp; - List portableIpVOs = _portableIpDao.listByRegionIdAndState(1, PortableIp.State.Free); - if (portableIpVOs == null || portableIpVOs.isEmpty()) { - InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Unable to find available portable IP addresses", Region.class, new Long(1)); - throw ex; - } + List portableIpVOs = _portableIpDao.listByRegionIdAndState(1, PortableIp.State.Free); + if (portableIpVOs == null || portableIpVOs.isEmpty()) { + InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Unable to find available portable IP addresses", Region.class, new Long(1)); + throw ex; + } + + // allocate first portable IP to the user + allocatedPortableIp = portableIpVOs.get(0); + allocatedPortableIp.setAllocatedTime(new Date()); + allocatedPortableIp.setAllocatedToAccountId(ipOwner.getAccountId()); + allocatedPortableIp.setAllocatedInDomainId(ipOwner.getDomainId()); + allocatedPortableIp.setState(PortableIp.State.Allocated); + _portableIpDao.update(allocatedPortableIp.getId(), allocatedPortableIp); + + // To make portable IP available as a zone level resource we need to emulate portable IP's (which are + // provisioned at region level) as public IP provisioned in a zone. user_ip_address and vlan combo give the + // identity of a public IP in zone. Create entry for portable ip in these tables. + + // provision portable IP range VLAN into the zone + long physicalNetworkId = _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(dcId, TrafficType.Public).getId(); + Network network = _networkModel.getSystemNetworkByZoneAndTrafficType(dcId, TrafficType.Public); + String range = allocatedPortableIp.getAddress() + "-" + allocatedPortableIp.getAddress(); + VlanVO vlan = new VlanVO(VlanType.VirtualNetwork, + allocatedPortableIp.getVlan(), + allocatedPortableIp.getGateway(), + allocatedPortableIp.getNetmask(), + dcId, + range, + network.getId(), + physicalNetworkId, + null, + null, + null); + vlan = _vlanDao.persist(vlan); - // allocate first portable IP to the user - allocatedPortableIp = portableIpVOs.get(0); - allocatedPortableIp.setAllocatedTime(new Date()); - allocatedPortableIp.setAllocatedToAccountId(ipOwner.getAccountId()); - allocatedPortableIp.setAllocatedInDomainId(ipOwner.getDomainId()); - allocatedPortableIp.setState(PortableIp.State.Allocated); - _portableIpDao.update(allocatedPortableIp.getId(), allocatedPortableIp); + // provision the portable IP in to user_ip_address table + IPAddressVO ipaddr = new IPAddressVO(new Ip(allocatedPortableIp.getAddress()), dcId, networkId, vpcID, physicalNetworkId, network.getId(), vlan.getId(), true); + ipaddr.setState(State.Allocated); + ipaddr.setAllocatedTime(new Date()); + ipaddr.setAllocatedInDomainId(ipOwner.getDomainId()); + ipaddr.setAllocatedToAccountId(ipOwner.getId()); + ipaddr = _ipAddressDao.persist(ipaddr); - // To make portable IP available as a zone level resource we need to emulate portable IP's (which are - // provisioned at region level) as public IP provisioned in a zone. user_ip_address and vlan combo give the - // identity of a public IP in zone. Create entry for portable ip in these tables. - - // provision portable IP range VLAN into the zone - long physicalNetworkId = _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(dcId, TrafficType.Public).getId(); - Network network = _networkModel.getSystemNetworkByZoneAndTrafficType(dcId, TrafficType.Public); - String range = allocatedPortableIp.getAddress() + "-" + allocatedPortableIp.getAddress(); - VlanVO vlan = new VlanVO(VlanType.VirtualNetwork, - allocatedPortableIp.getVlan(), - allocatedPortableIp.getGateway(), - allocatedPortableIp.getNetmask(), - dcId, - range, - network.getId(), - physicalNetworkId, - null, - null, - null); - vlan = _vlanDao.persist(vlan); - - // provision the portable IP in to user_ip_address table - ipaddr = new IPAddressVO(new Ip(allocatedPortableIp.getAddress()), dcId, networkId, vpcID, physicalNetworkId, network.getId(), vlan.getId(), true); - ipaddr.setState(State.Allocated); - ipaddr.setAllocatedTime(new Date()); - ipaddr.setAllocatedInDomainId(ipOwner.getDomainId()); - ipaddr.setAllocatedToAccountId(ipOwner.getId()); - ipaddr = _ipAddressDao.persist(ipaddr); - - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_PORTABLE_IP_ASSIGN, - ipaddr.getId(), - ipaddr.getDataCenterId(), - ipaddr.getId(), - ipaddr.getAddress().toString(), - ipaddr.isSourceNat(), - null, - ipaddr.getSystem(), - ipaddr.getClass().getName(), - ipaddr.getUuid()); - - txn.commit(); + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_PORTABLE_IP_ASSIGN, + ipaddr.getId(), + ipaddr.getDataCenterId(), + ipaddr.getId(), + ipaddr.getAddress().toString(), + ipaddr.isSourceNat(), + null, + ipaddr.getSystem(), + ipaddr.getClass().getName(), + ipaddr.getUuid()); + return ipaddr; + } + }); } finally { portableIpLock.unlock(); } @@ -1392,7 +1409,7 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage @DB @Override - public void transferPortableIP(long ipAddrId, long currentNetworkId, long newNetworkId) throws ResourceAllocationException, ResourceUnavailableException, + public void transferPortableIP(final long ipAddrId, long currentNetworkId, long newNetworkId) throws ResourceAllocationException, ResourceUnavailableException, InsufficientAddressCapacityException, ConcurrentOperationException { Network srcNetwork = _networksDao.findById(currentNetworkId); @@ -1400,18 +1417,16 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage throw new InvalidParameterValueException("Invalid source network id " + currentNetworkId + " is given"); } - Network dstNetwork = _networksDao.findById(newNetworkId); + final Network dstNetwork = _networksDao.findById(newNetworkId); if (dstNetwork == null) { throw new InvalidParameterValueException("Invalid source network id " + newNetworkId + " is given"); } - IPAddressVO ip = _ipAddressDao.findById(ipAddrId); + final IPAddressVO ip = _ipAddressDao.findById(ipAddrId); if (ip == null) { throw new InvalidParameterValueException("Invalid portable ip address id is given"); } - Transaction txn = Transaction.currentTxn(); - assert (isPortableIpTransferableFromNetwork(ipAddrId, currentNetworkId)); // disassociate portable IP with current network/VPC network @@ -1424,39 +1439,42 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage // If portable IP need to be transferred across the zones, then mark the entry corresponding to portable ip // in user_ip_address and vlan tables so as to emulate portable IP as provisioned in destination data center if (srcNetwork.getDataCenterId() != dstNetwork.getDataCenterId()) { - txn.start(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + long physicalNetworkId = _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(dstNetwork.getDataCenterId(), TrafficType.Public).getId(); + long publicNetworkId = _networkModel.getSystemNetworkByZoneAndTrafficType(dstNetwork.getDataCenterId(), TrafficType.Public).getId(); - long physicalNetworkId = _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(dstNetwork.getDataCenterId(), TrafficType.Public).getId(); - long publicNetworkId = _networkModel.getSystemNetworkByZoneAndTrafficType(dstNetwork.getDataCenterId(), TrafficType.Public).getId(); + ip.setDataCenterId(dstNetwork.getDataCenterId()); + ip.setPhysicalNetworkId(physicalNetworkId); + ip.setSourceNetworkId(publicNetworkId); + _ipAddressDao.update(ipAddrId, ip); - ip.setDataCenterId(dstNetwork.getDataCenterId()); - ip.setPhysicalNetworkId(physicalNetworkId); - ip.setSourceNetworkId(publicNetworkId); - _ipAddressDao.update(ipAddrId, ip); - - VlanVO vlan = _vlanDao.findById(ip.getVlanId()); - vlan.setPhysicalNetworkId(physicalNetworkId); - vlan.setNetworkId(publicNetworkId); - vlan.setDataCenterId(dstNetwork.getDataCenterId()); - _vlanDao.update(ip.getVlanId(), vlan); - - txn.commit(); + VlanVO vlan = _vlanDao.findById(ip.getVlanId()); + vlan.setPhysicalNetworkId(physicalNetworkId); + vlan.setNetworkId(publicNetworkId); + vlan.setDataCenterId(dstNetwork.getDataCenterId()); + _vlanDao.update(ip.getVlanId(), vlan); + } + }); } // associate portable IP with new network/VPC network associatePortableIPToGuestNetwork(ipAddrId, newNetworkId, false); - txn.start(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + if (dstNetwork.getVpcId() != null) { + ip.setVpcId(dstNetwork.getVpcId()); + } else { + ip.setVpcId(null); + } - if (dstNetwork.getVpcId() != null) { - ip.setVpcId(dstNetwork.getVpcId()); - } else { - ip.setVpcId(null); - } + _ipAddressDao.update(ipAddrId, ip); + } + }); - _ipAddressDao.update(ipAddrId, ip); - - txn.commit(); // trigger an action event for the transfer of portable IP across the networks, so that external entities // monitoring for this event can initiate the route advertisement for the availability of IP from the zoe @@ -1474,106 +1492,121 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage @Override @DB - public boolean associateIpAddressListToAccount(long userId, long accountId, long zoneId, Long vlanId, Network guestNetwork) throws InsufficientCapacityException, + public boolean associateIpAddressListToAccount(long userId, final long accountId, final long zoneId, final Long vlanId, final Network guestNetworkFinal) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, ResourceAllocationException { - Account owner = _accountMgr.getActiveAccountById(accountId); - boolean createNetwork = false; + final Account owner = _accountMgr.getActiveAccountById(accountId); - if (guestNetwork != null && guestNetwork.getTrafficType() != TrafficType.Guest) { - throw new InvalidParameterValueException("Network " + guestNetwork + " is not of a type " + TrafficType.Guest); + if (guestNetworkFinal != null && guestNetworkFinal.getTrafficType() != TrafficType.Guest) { + throw new InvalidParameterValueException("Network " + guestNetworkFinal + " is not of a type " + TrafficType.Guest); } - Transaction txn = Transaction.currentTxn(); - txn.start(); + Ternary, Network> pair = null; + try { + pair = Transaction.execute(new TransactionCallbackWithException, Network>,Exception>() { + @Override + public Ternary, Network> doInTransaction(TransactionStatus status) throws InsufficientCapacityException, ResourceAllocationException { + boolean createNetwork = false; + Network guestNetwork = guestNetworkFinal; - if (guestNetwork == null) { - List networks = getIsolatedNetworksWithSourceNATOwnedByAccountInZone(zoneId, owner); - if (networks.size() == 0) { - createNetwork = true; - } else if (networks.size() == 1) { - guestNetwork = networks.get(0); - } else { - throw new InvalidParameterValueException("Error, more than 1 Guest Isolated Networks with SourceNAT " - + "service enabled found for this account, cannot assosiate the IP range, please provide the network ID"); - } - } - - // create new Virtual network (Isolated with SourceNAT) for the user if it doesn't exist - List requiredOfferings = _networkOfferingDao.listByAvailability(Availability.Required, false); - if (requiredOfferings.size() < 1) { - throw new CloudRuntimeException("Unable to find network offering with availability=" + Availability.Required + - " to automatically create the network as part of createVlanIpRange"); - } - if (createNetwork) { - if (requiredOfferings.get(0).getState() == NetworkOffering.State.Enabled) { - long physicalNetworkId = _networkModel.findPhysicalNetworkId(zoneId, requiredOfferings.get(0).getTags(), requiredOfferings.get(0).getTrafficType()); - // Validate physical network - PhysicalNetwork physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId); - if (physicalNetwork == null) { - throw new InvalidParameterValueException("Unable to find physical network with id: " + physicalNetworkId + " and tag: " + requiredOfferings.get(0).getTags()); + if (guestNetwork == null) { + List networks = getIsolatedNetworksWithSourceNATOwnedByAccountInZone(zoneId, owner); + if (networks.size() == 0) { + createNetwork = true; + } else if (networks.size() == 1) { + guestNetwork = networks.get(0); + } else { + throw new InvalidParameterValueException("Error, more than 1 Guest Isolated Networks with SourceNAT " + + "service enabled found for this account, cannot assosiate the IP range, please provide the network ID"); + } + } + + // create new Virtual network (Isolated with SourceNAT) for the user if it doesn't exist + List requiredOfferings = _networkOfferingDao.listByAvailability(Availability.Required, false); + if (requiredOfferings.size() < 1) { + throw new CloudRuntimeException("Unable to find network offering with availability=" + Availability.Required + + " to automatically create the network as part of createVlanIpRange"); + } + if (createNetwork) { + if (requiredOfferings.get(0).getState() == NetworkOffering.State.Enabled) { + long physicalNetworkId = _networkModel.findPhysicalNetworkId(zoneId, requiredOfferings.get(0).getTags(), requiredOfferings.get(0).getTrafficType()); + // Validate physical network + PhysicalNetwork physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId); + if (physicalNetwork == null) { + throw new InvalidParameterValueException("Unable to find physical network with id: " + physicalNetworkId + " and tag: " + requiredOfferings.get(0).getTags()); + } + + s_logger.debug("Creating network for account " + owner + " from the network offering id=" + requiredOfferings.get(0).getId() + + " as a part of createVlanIpRange process"); + guestNetwork = _networkMgr.createGuestNetwork(requiredOfferings.get(0).getId(), + owner.getAccountName() + "-network", + owner.getAccountName() + "-network", + null, + null, + null, + null, + owner, + null, + physicalNetwork, + zoneId, + ACLType.Account, + null, + null, + null, + null, + true, + null); + if (guestNetwork == null) { + s_logger.warn("Failed to create default Virtual network for the account " + accountId + "in zone " + zoneId); + throw new CloudRuntimeException("Failed to create a Guest Isolated Networks with SourceNAT " + + "service enabled as a part of createVlanIpRange, for the account " + accountId + "in zone " + zoneId); + } + } else { + throw new CloudRuntimeException("Required network offering id=" + requiredOfferings.get(0).getId() + " is not in " + NetworkOffering.State.Enabled); + } + } + + // Check if there is a source nat ip address for this account; if not - we have to allocate one + boolean allocateSourceNat = false; + List sourceNat = _ipAddressDao.listByAssociatedNetwork(guestNetwork.getId(), true); + if (sourceNat.isEmpty()) { + allocateSourceNat = true; + } + + // update all ips with a network id, mark them as allocated and update resourceCount/usage + List ips = _ipAddressDao.listByVlanId(vlanId); + boolean isSourceNatAllocated = false; + for (IPAddressVO addr : ips) { + if (addr.getState() != State.Allocated) { + if (!isSourceNatAllocated && allocateSourceNat) { + addr.setSourceNat(true); + isSourceNatAllocated = true; + } else { + addr.setSourceNat(false); + } + addr.setAssociatedWithNetworkId(guestNetwork.getId()); + addr.setVpcId(guestNetwork.getVpcId()); + addr.setAllocatedTime(new Date()); + addr.setAllocatedInDomainId(owner.getDomainId()); + addr.setAllocatedToAccountId(owner.getId()); + addr.setSystem(false); + addr.setState(IpAddress.State.Allocating); + markPublicIpAsAllocated(addr); + } + } + return new Ternary, Network>(createNetwork, requiredOfferings, guestNetwork); } - - s_logger.debug("Creating network for account " + owner + " from the network offering id=" + requiredOfferings.get(0).getId() + - " as a part of createVlanIpRange process"); - guestNetwork = _networkMgr.createGuestNetwork(requiredOfferings.get(0).getId(), - owner.getAccountName() + "-network", - owner.getAccountName() + "-network", - null, - null, - null, - null, - owner, - null, - physicalNetwork, - zoneId, - ACLType.Account, - null, - null, - null, - null, - true, - null); - if (guestNetwork == null) { - s_logger.warn("Failed to create default Virtual network for the account " + accountId + "in zone " + zoneId); - throw new CloudRuntimeException("Failed to create a Guest Isolated Networks with SourceNAT " + - "service enabled as a part of createVlanIpRange, for the account " + accountId + "in zone " + zoneId); - } - } else { - throw new CloudRuntimeException("Required network offering id=" + requiredOfferings.get(0).getId() + " is not in " + NetworkOffering.State.Enabled); - } + }); + } catch (Exception e1) { + ExceptionUtil.rethrowRuntime(e1); + ExceptionUtil.rethrow(e1, InsufficientCapacityException.class); + ExceptionUtil.rethrow(e1, ResourceAllocationException.class); + throw new IllegalStateException(e1); } - // Check if there is a source nat ip address for this account; if not - we have to allocate one - boolean allocateSourceNat = false; - List sourceNat = _ipAddressDao.listByAssociatedNetwork(guestNetwork.getId(), true); - if (sourceNat.isEmpty()) { - allocateSourceNat = true; - } - - // update all ips with a network id, mark them as allocated and update resourceCount/usage - List ips = _ipAddressDao.listByVlanId(vlanId); - boolean isSourceNatAllocated = false; - for (IPAddressVO addr : ips) { - if (addr.getState() != State.Allocated) { - if (!isSourceNatAllocated && allocateSourceNat) { - addr.setSourceNat(true); - isSourceNatAllocated = true; - } else { - addr.setSourceNat(false); - } - addr.setAssociatedWithNetworkId(guestNetwork.getId()); - addr.setVpcId(guestNetwork.getVpcId()); - addr.setAllocatedTime(new Date()); - addr.setAllocatedInDomainId(owner.getDomainId()); - addr.setAllocatedToAccountId(owner.getId()); - addr.setSystem(false); - addr.setState(IpAddress.State.Allocating); - markPublicIpAsAllocated(addr); - } - } - - txn.commit(); - + boolean createNetwork = pair.first(); + List requiredOfferings = pair.second(); + Network guestNetwork = pair.third(); + // if the network offering has persistent set to true, implement the network if (createNetwork && requiredOfferings.get(0).getIsPersistent()) { DataCenter zone = _dcDao.findById(zoneId); @@ -1602,10 +1635,8 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage @DB @Override - public IPAddressVO markIpAsUnavailable(long addrId) { - Transaction txn = Transaction.currentTxn(); - - IPAddressVO ip = _ipAddressDao.findById(addrId); + public IPAddressVO markIpAsUnavailable(final long addrId) { + final IPAddressVO ip = _ipAddressDao.findById(addrId); if (ip.getAllocatedToAccountId() == null && ip.getAllocatedTime() == null) { s_logger.trace("Ip address id=" + addrId + " is already released"); @@ -1613,35 +1644,36 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage } if (ip.getState() != State.Releasing) { - txn.start(); + return Transaction.execute(new TransactionCallback() { + @Override + public IPAddressVO doInTransaction(TransactionStatus status) { + if (updateIpResourceCount(ip)) { + _resourceLimitMgr.decrementResourceCount(_ipAddressDao.findById(addrId).getAllocatedToAccountId(), ResourceType.public_ip); + } - if (updateIpResourceCount(ip)) { - _resourceLimitMgr.decrementResourceCount(_ipAddressDao.findById(addrId).getAllocatedToAccountId(), ResourceType.public_ip); - } + // Save usage event + if (ip.getAllocatedToAccountId() != null && ip.getAllocatedToAccountId() != Account.ACCOUNT_ID_SYSTEM) { + VlanVO vlan = _vlanDao.findById(ip.getVlanId()); - // Save usage event - if (ip.getAllocatedToAccountId() != null && ip.getAllocatedToAccountId() != Account.ACCOUNT_ID_SYSTEM) { - VlanVO vlan = _vlanDao.findById(ip.getVlanId()); - - String guestType = vlan.getVlanType().toString(); - if (!isIpDedicated(ip)) { - String eventType = ip.isPortable() ? EventTypes.EVENT_PORTABLE_IP_RELEASE : EventTypes.EVENT_NET_IP_RELEASE; - UsageEventUtils.publishUsageEvent(eventType, - ip.getAllocatedToAccountId(), - ip.getDataCenterId(), - addrId, - ip.getAddress().addr(), - ip.isSourceNat(), - guestType, - ip.getSystem(), - ip.getClass().getName(), - ip.getUuid()); + String guestType = vlan.getVlanType().toString(); + if (!isIpDedicated(ip)) { + String eventType = ip.isPortable() ? EventTypes.EVENT_PORTABLE_IP_RELEASE : EventTypes.EVENT_NET_IP_RELEASE; + UsageEventUtils.publishUsageEvent(eventType, + ip.getAllocatedToAccountId(), + ip.getDataCenterId(), + addrId, + ip.getAddress().addr(), + ip.isSourceNat(), + guestType, + ip.getSystem(), + ip.getClass().getName(), + ip.getUuid()); + } + } + + return _ipAddressDao.markAsUnavailable(addrId); } - } - - ip = _ipAddressDao.markAsUnavailable(addrId); - - txn.commit(); + }); } return ip; @@ -1826,73 +1858,73 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage @Override @DB - public void allocateDirectIp(NicProfile nic, DataCenter dc, VirtualMachineProfile vm, Network network, String requestedIpv4, String requestedIpv6) + public void allocateDirectIp(final NicProfile nic, final DataCenter dc, final VirtualMachineProfile vm, final Network network, final String requestedIpv4, final String requestedIpv6) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { - //This method allocates direct ip for the Shared network in Advance zones - boolean ipv4 = false; + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws InsufficientAddressCapacityException { + //This method allocates direct ip for the Shared network in Advance zones + boolean ipv4 = false; - Transaction txn = Transaction.currentTxn(); - txn.start(); + if (network.getGateway() != null) { + if (nic.getIp4Address() == null) { + ipv4 = true; + PublicIp ip = null; - if (network.getGateway() != null) { - if (nic.getIp4Address() == null) { - ipv4 = true; - PublicIp ip = null; + //Get ip address from the placeholder and don't allocate a new one + if (requestedIpv4 != null && vm.getType() == VirtualMachine.Type.DomainRouter) { + Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, null); + if (placeholderNic != null) { + IPAddressVO userIp = _ipAddressDao.findByIpAndSourceNetworkId(network.getId(), placeholderNic.getIp4Address()); + ip = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId())); + s_logger.debug("Nic got an ip address " + placeholderNic.getIp4Address() + " stored in placeholder nic for the network " + network); + } + } - //Get ip address from the placeholder and don't allocate a new one - if (requestedIpv4 != null && vm.getType() == VirtualMachine.Type.DomainRouter) { - Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, null); - if (placeholderNic != null) { - IPAddressVO userIp = _ipAddressDao.findByIpAndSourceNetworkId(network.getId(), placeholderNic.getIp4Address()); - ip = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId())); - s_logger.debug("Nic got an ip address " + placeholderNic.getIp4Address() + " stored in placeholder nic for the network " + network); + if (ip == null) { + ip = assignPublicIpAddress(dc.getId(), null, vm.getOwner(), VlanType.DirectAttached, network.getId(), requestedIpv4, false); + } + + nic.setIp4Address(ip.getAddress().toString()); + nic.setGateway(ip.getGateway()); + nic.setNetmask(ip.getNetmask()); + nic.setIsolationUri(IsolationType.Vlan.toUri(ip.getVlanTag())); + //nic.setBroadcastType(BroadcastDomainType.Vlan); + //nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(ip.getVlanTag())); + nic.setBroadcastType(network.getBroadcastDomainType()); + nic.setBroadcastUri(network.getBroadcastUri()); + nic.setFormat(AddressFormat.Ip4); + nic.setReservationId(String.valueOf(ip.getVlanTag())); + nic.setMacAddress(ip.getMacAddress()); } + nic.setDns1(dc.getDns1()); + nic.setDns2(dc.getDns2()); } - if (ip == null) { - ip = assignPublicIpAddress(dc.getId(), null, vm.getOwner(), VlanType.DirectAttached, network.getId(), requestedIpv4, false); - } - - nic.setIp4Address(ip.getAddress().toString()); - nic.setGateway(ip.getGateway()); - nic.setNetmask(ip.getNetmask()); - nic.setIsolationUri(IsolationType.Vlan.toUri(ip.getVlanTag())); - //nic.setBroadcastType(BroadcastDomainType.Vlan); - //nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(ip.getVlanTag())); - nic.setBroadcastType(network.getBroadcastDomainType()); - nic.setBroadcastUri(network.getBroadcastUri()); - nic.setFormat(AddressFormat.Ip4); - nic.setReservationId(String.valueOf(ip.getVlanTag())); - nic.setMacAddress(ip.getMacAddress()); - } - nic.setDns1(dc.getDns1()); - nic.setDns2(dc.getDns2()); - } - - //FIXME - get ipv6 address from the placeholder if it's stored there - if (network.getIp6Gateway() != null) { - if (nic.getIp6Address() == null) { - UserIpv6Address ip = _ipv6Mgr.assignDirectIp6Address(dc.getId(), vm.getOwner(), network.getId(), requestedIpv6); - Vlan vlan = _vlanDao.findById(ip.getVlanId()); - nic.setIp6Address(ip.getAddress().toString()); - nic.setIp6Gateway(vlan.getIp6Gateway()); - nic.setIp6Cidr(vlan.getIp6Cidr()); - if (ipv4) { - nic.setFormat(AddressFormat.DualStack); - } else { - nic.setIsolationUri(IsolationType.Vlan.toUri(vlan.getVlanTag())); - nic.setBroadcastType(BroadcastDomainType.Vlan); - nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlan.getVlanTag())); - nic.setFormat(AddressFormat.Ip6); - nic.setReservationId(String.valueOf(vlan.getVlanTag())); - nic.setMacAddress(ip.getMacAddress()); + //FIXME - get ipv6 address from the placeholder if it's stored there + if (network.getIp6Gateway() != null) { + if (nic.getIp6Address() == null) { + UserIpv6Address ip = _ipv6Mgr.assignDirectIp6Address(dc.getId(), vm.getOwner(), network.getId(), requestedIpv6); + Vlan vlan = _vlanDao.findById(ip.getVlanId()); + nic.setIp6Address(ip.getAddress().toString()); + nic.setIp6Gateway(vlan.getIp6Gateway()); + nic.setIp6Cidr(vlan.getIp6Cidr()); + if (ipv4) { + nic.setFormat(AddressFormat.DualStack); + } else { + nic.setIsolationUri(IsolationType.Vlan.toUri(vlan.getVlanTag())); + nic.setBroadcastType(BroadcastDomainType.Vlan); + nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlan.getVlanTag())); + nic.setFormat(AddressFormat.Ip6); + nic.setReservationId(String.valueOf(vlan.getVlanTag())); + nic.setMacAddress(ip.getMacAddress()); + } + } + nic.setIp6Dns1(dc.getIp6Dns1()); + nic.setIp6Dns2(dc.getIp6Dns2()); } } - nic.setIp6Dns1(dc.getIp6Dns1()); - nic.setIp6Dns2(dc.getIp6Dns2()); - } - - txn.commit(); + }); } @Override diff --git a/server/src/com/cloud/network/NetworkServiceImpl.java b/server/src/com/cloud/network/NetworkServiceImpl.java index 61c070a86c4..c9a14cdf47f 100755 --- a/server/src/com/cloud/network/NetworkServiceImpl.java +++ b/server/src/com/cloud/network/NetworkServiceImpl.java @@ -41,7 +41,6 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; - import org.apache.cloudstack.acl.ControlledEntity.ACLType; import org.apache.cloudstack.acl.SecurityChecker.AccessType; import org.apache.cloudstack.api.command.admin.network.DedicateGuestVlanRangeCmd; @@ -159,9 +158,15 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionLegacy; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.exception.ExceptionUtil; import com.cloud.utils.net.NetUtils; import com.cloud.vm.Nic; import com.cloud.vm.NicSecondaryIp; @@ -635,11 +640,8 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { @Override - public NicSecondaryIp allocateSecondaryGuestIP (Account ipOwner, long zoneId, Long nicId, Long networkId, String requestedIp) throws InsufficientAddressCapacityException { + public NicSecondaryIp allocateSecondaryGuestIP (Account ipOwner, long zoneId, final Long nicId, final Long networkId, String requestedIp) throws InsufficientAddressCapacityException { - Long accountId = null; - Long domainId = null; - Long vmId = null; String ipaddr = null; if (networkId == null) { @@ -649,7 +651,7 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { Account caller = CallContext.current().getCallingAccount(); //check whether the nic belongs to user vm. - NicVO nicVO = _nicDao.findById(nicId); + final NicVO nicVO = _nicDao.findById(nicId); if (nicVO == null) { throw new InvalidParameterValueException("There is no nic for the " + nicId); } @@ -670,8 +672,8 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { if (network == null) { throw new InvalidParameterValueException("Invalid network id is given"); } - accountId = ipOwner.getAccountId(); - domainId = ipOwner.getDomainId(); + final Long accountId = ipOwner.getAccountId(); + final Long domainId = ipOwner.getDomainId(); // Validate network offering NetworkOfferingVO ntwkOff = _networkOfferingDao.findById(network.getNetworkOfferingId()); @@ -716,26 +718,29 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { return null; } - NicSecondaryIpVO secondaryIpVO; if (ipaddr != null) { // we got the ip addr so up the nics table and secodary ip - Transaction txn = Transaction.currentTxn(); - txn.start(); + final String addrFinal = ipaddr; + long id = Transaction.execute(new TransactionCallback() { + @Override + public Long doInTransaction(TransactionStatus status) { + boolean nicSecondaryIpSet = nicVO.getSecondaryIp(); + if (!nicSecondaryIpSet) { + nicVO.setSecondaryIp(true); + // commit when previously set ?? + s_logger.debug("Setting nics table ..."); + _nicDao.update(nicId, nicVO); + } + + s_logger.debug("Setting nic_secondary_ip table ..."); + Long vmId = nicVO.getInstanceId(); + NicSecondaryIpVO secondaryIpVO = new NicSecondaryIpVO(nicId, addrFinal, vmId, accountId, domainId, networkId); + _nicSecondaryIpDao.persist(secondaryIpVO); + return secondaryIpVO.getId(); + } + }); - boolean nicSecondaryIpSet = nicVO.getSecondaryIp(); - if (!nicSecondaryIpSet) { - nicVO.setSecondaryIp(true); - // commit when previously set ?? - s_logger.debug("Setting nics table ..."); - _nicDao.update(nicId, nicVO); - } - - s_logger.debug("Setting nic_secondary_ip table ..."); - vmId = nicVO.getInstanceId(); - secondaryIpVO = new NicSecondaryIpVO(nicId, ipaddr, vmId, accountId, domainId, networkId); - _nicSecondaryIpDao.persist(secondaryIpVO); - txn.commit(); - return getNicSecondaryIp(secondaryIpVO.getId()); + return getNicSecondaryIp(id); } else { return null; } @@ -805,13 +810,15 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { throw new InvalidParameterValueException("Can' remove the ip " + secondaryIp + "is associate with static NAT rule public IP address id " + publicIpVO.getId()); } } else if (dc.getNetworkType() == NetworkType.Basic || ntwkOff.getGuestType() == Network.GuestType.Shared) { - IPAddressVO ip = _ipAddressDao.findByIpAndNetworkId(secIpVO.getNetworkId(), secIpVO.getIp4Address()); + final IPAddressVO ip = _ipAddressDao.findByIpAndNetworkId(secIpVO.getNetworkId(), secIpVO.getIp4Address()); if (ip != null) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - _ipAddrMgr.markIpAsUnavailable(ip.getId()); - _ipAddressDao.unassignIpAddress(ip.getId()); - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _ipAddrMgr.markIpAsUnavailable(ip.getId()); + _ipAddressDao.unassignIpAddress(ip.getId()); + } + }); } } else { throw new InvalidParameterValueException("Not supported for this network now"); @@ -821,22 +828,24 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { return success; } - boolean removeNicSecondaryIP(NicSecondaryIpVO ipVO, boolean lastIp) { - Transaction txn = Transaction.currentTxn(); - long nicId = ipVO.getNicId(); - NicVO nic = _nicDao.findById(nicId); + boolean removeNicSecondaryIP(final NicSecondaryIpVO ipVO, final boolean lastIp) { + final long nicId = ipVO.getNicId(); + final NicVO nic = _nicDao.findById(nicId); - txn.start(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + if (lastIp) { + nic.setSecondaryIp(false); + s_logger.debug("Setting nics secondary ip to false ..."); + _nicDao.update(nicId, nic); + } - if (lastIp) { - nic.setSecondaryIp(false); - s_logger.debug("Setting nics secondary ip to false ..."); - _nicDao.update(nicId, nic); - } + s_logger.debug("Revoving nic secondary ip entry ..."); + _nicSecondaryIpDao.remove(ipVO.getId()); + } + }); - s_logger.debug("Revoving nic secondary ip entry ..."); - _nicSecondaryIpDao.remove(ipVO.getId()); - txn.commit(); return true; } @@ -1255,67 +1264,10 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { throw ex; } - Transaction txn = Transaction.currentTxn(); - txn.start(); - - Long sharedDomainId = null; - if (isDomainSpecific) { - if (domainId != null) { - sharedDomainId = domainId; - } else { - sharedDomainId = _domainMgr.getDomain(Domain.ROOT_DOMAIN).getId(); - subdomainAccess = true; - } - } - - // default owner to system if network has aclType=Domain - if (aclType == ACLType.Domain) { - owner = _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM); - } - - //Create guest network - Network network = null; - if (vpcId != null) { - if (!_configMgr.isOfferingForVpc(ntwkOff)){ - throw new InvalidParameterValueException("Network offering can't be used for VPC networks"); - } - - if(aclId != null){ - NetworkACL acl = _networkACLDao.findById(aclId); - if(acl == null){ - throw new InvalidParameterValueException("Unable to find specified NetworkACL"); - } - - if(aclId != NetworkACL.DEFAULT_DENY && aclId != NetworkACL.DEFAULT_ALLOW) { - //ACL is not default DENY/ALLOW - // ACL should be associated with a VPC - if(!vpcId.equals(acl.getVpcId())){ - throw new InvalidParameterValueException("ACL: "+aclId+" do not belong to the VPC"); - } - } - } - network = _vpcMgr.createVpcGuestNetwork(networkOfferingId, name, displayText, gateway, cidr, vlanId, - networkDomain, owner, sharedDomainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId, aclId, caller, displayNetwork); - } else { - if (_configMgr.isOfferingForVpc(ntwkOff)){ - throw new InvalidParameterValueException("Network offering can be used for VPC networks only"); - } - if (ntwkOff.getInternalLb()) { - throw new InvalidParameterValueException("Internal Lb can be enabled on vpc networks only"); - } - - network = _networkMgr.createGuestNetwork(networkOfferingId, name, displayText, gateway, cidr, vlanId, - networkDomain, owner, sharedDomainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId, - ip6Gateway, ip6Cidr, displayNetwork, isolatedPvlan); - } - - if (caller.getType() == Account.ACCOUNT_TYPE_ADMIN && createVlan) { - // Create vlan ip range - _configMgr.createVlanAndPublicIpRange(pNtwk.getDataCenterId(), network.getId(), physicalNetworkId, - false, null, startIP, endIP, gateway, netmask, vlanId, null, startIPv6, endIPv6, ip6Gateway, ip6Cidr); - } - - txn.commit(); + Network network = commitNetwork(networkOfferingId, gateway, startIP, endIP, netmask, networkDomain, vlanId, + name, displayText, caller, physicalNetworkId, zoneId, domainId, isDomainSpecific, subdomainAccess, + vpcId, startIPv6, endIPv6, ip6Gateway, ip6Cidr, displayNetwork, aclId, isolatedPvlan, ntwkOff, pNtwk, + aclType, owner, cidr, createVlan); // if the network offering has persistent set to true, implement the network if ( ntwkOff.getIsPersistent() ) { @@ -1344,6 +1296,87 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { return network; } + private Network commitNetwork(final Long networkOfferingId, final String gateway, final String startIP, final String endIP, final String netmask, + final String networkDomain, final String vlanId, final String name, final String displayText, final Account caller, + final Long physicalNetworkId, final Long zoneId, final Long domainId, final boolean isDomainSpecific, final Boolean subdomainAccessFinal, + final Long vpcId, final String startIPv6, final String endIPv6, final String ip6Gateway, final String ip6Cidr, final Boolean displayNetwork, + final Long aclId, final String isolatedPvlan, final NetworkOfferingVO ntwkOff, final PhysicalNetwork pNtwk, final ACLType aclType, + final Account ownerFinal, final String cidr, final boolean createVlan) throws InsufficientCapacityException, + ResourceAllocationException { + try { + return Transaction.execute(new TransactionCallbackWithException() { + @Override + public Network doInTransaction(TransactionStatus status) throws InsufficientCapacityException, ResourceAllocationException { + Account owner = ownerFinal; + Boolean subdomainAccess = subdomainAccessFinal; + + Long sharedDomainId = null; + if (isDomainSpecific) { + if (domainId != null) { + sharedDomainId = domainId; + } else { + sharedDomainId = _domainMgr.getDomain(Domain.ROOT_DOMAIN).getId(); + subdomainAccess = true; + } + } + + // default owner to system if network has aclType=Domain + if (aclType == ACLType.Domain) { + owner = _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM); + } + + //Create guest network + Network network = null; + if (vpcId != null) { + if (!_configMgr.isOfferingForVpc(ntwkOff)){ + throw new InvalidParameterValueException("Network offering can't be used for VPC networks"); + } + + if(aclId != null){ + NetworkACL acl = _networkACLDao.findById(aclId); + if(acl == null){ + throw new InvalidParameterValueException("Unable to find specified NetworkACL"); + } + + if(aclId != NetworkACL.DEFAULT_DENY && aclId != NetworkACL.DEFAULT_ALLOW) { + //ACL is not default DENY/ALLOW + // ACL should be associated with a VPC + if(!vpcId.equals(acl.getVpcId())){ + throw new InvalidParameterValueException("ACL: "+aclId+" do not belong to the VPC"); + } + } + } + network = _vpcMgr.createVpcGuestNetwork(networkOfferingId, name, displayText, gateway, cidr, vlanId, + networkDomain, owner, sharedDomainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId, aclId, caller, displayNetwork); + } else { + if (_configMgr.isOfferingForVpc(ntwkOff)){ + throw new InvalidParameterValueException("Network offering can be used for VPC networks only"); + } + if (ntwkOff.getInternalLb()) { + throw new InvalidParameterValueException("Internal Lb can be enabled on vpc networks only"); + } + + network = _networkMgr.createGuestNetwork(networkOfferingId, name, displayText, gateway, cidr, vlanId, + networkDomain, owner, sharedDomainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId, + ip6Gateway, ip6Cidr, displayNetwork, isolatedPvlan); + } + + if (caller.getType() == Account.ACCOUNT_TYPE_ADMIN && createVlan) { + // Create vlan ip range + _configMgr.createVlanAndPublicIpRange(pNtwk.getDataCenterId(), network.getId(), physicalNetworkId, + false, null, startIP, endIP, gateway, netmask, vlanId, null, startIPv6, endIPv6, ip6Gateway, ip6Cidr); + } + return network; + } + }); + } catch (Exception e) { + ExceptionUtil.rethrowRuntime(e); + ExceptionUtil.rethrow(e, InsufficientCapacityException.class); + ExceptionUtil.rethrow(e, ResourceAllocationException.class); + throw new IllegalStateException(e); + } + } + @Override public List searchForNetworks(ListNetworksCmd cmd) { Long id = cmd.getId(); @@ -1915,13 +1948,13 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { @Override @DB @ActionEvent(eventType = EventTypes.EVENT_NETWORK_UPDATE, eventDescription = "updating network", async = true) - public Network updateGuestNetwork(long networkId, String name, String displayText, Account callerAccount, - User callerUser, String domainSuffix, Long networkOfferingId, Boolean changeCidr, String guestVmCidr, Boolean displayNetwork) { + public Network updateGuestNetwork(final long networkId, String name, String displayText, Account callerAccount, + User callerUser, String domainSuffix, final Long networkOfferingId, Boolean changeCidr, String guestVmCidr, Boolean displayNetwork) { boolean restartNetwork = false; // verify input parameters - NetworkVO network = _networksDao.findById(networkId); + final NetworkVO network = _networksDao.findById(networkId); if (network == null) { // see NetworkVO.java InvalidParameterValueException ex = new InvalidParameterValueException("Specified network id doesn't exist in the system"); @@ -1975,7 +2008,7 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { boolean networkOfferingChanged = false; - long oldNetworkOfferingId = network.getNetworkOfferingId(); + final long oldNetworkOfferingId = network.getNetworkOfferingId(); NetworkOffering oldNtwkOff = _networkOfferingDao.findByIdIncludingRemoved(oldNetworkOfferingId); NetworkOfferingVO networkOffering = _networkOfferingDao.findById(networkOfferingId); if (networkOfferingId != null) { @@ -2024,10 +2057,8 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { } } - Map newSvcProviders = new HashMap(); - if (networkOfferingChanged) { - newSvcProviders = _networkMgr.finalizeServicesAndProvidersForNetwork(_entityMgr.findById(NetworkOffering.class, networkOfferingId), network.getPhysicalNetworkId()); - } + final Map newSvcProviders = networkOfferingChanged ? _networkMgr.finalizeServicesAndProvidersForNetwork(_entityMgr.findById(NetworkOffering.class, networkOfferingId), network.getPhysicalNetworkId()) + : new HashMap(); // don't allow to modify network domain if the service is not supported if (domainSuffix != null) { @@ -2201,29 +2232,31 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { if (networkOfferingId != null) { if (networkOfferingChanged) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - network.setNetworkOfferingId(networkOfferingId); - _networksDao.update(networkId, network, newSvcProviders); - // get all nics using this network - // log remove usage events for old offering - // log assign usage events for new offering - List nics = _nicDao.listByNetworkId(networkId); - for (NicVO nic : nics) { - long vmId = nic.getInstanceId(); - VMInstanceVO vm = _vmDao.findById(vmId); - if (vm == null) { - s_logger.error("Vm for nic " + nic.getId() + " not found with Vm Id:" + vmId); - continue; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + network.setNetworkOfferingId(networkOfferingId); + _networksDao.update(networkId, network, newSvcProviders); + // get all nics using this network + // log remove usage events for old offering + // log assign usage events for new offering + List nics = _nicDao.listByNetworkId(networkId); + for (NicVO nic : nics) { + long vmId = nic.getInstanceId(); + VMInstanceVO vm = _vmDao.findById(vmId); + if (vm == null) { + s_logger.error("Vm for nic " + nic.getId() + " not found with Vm Id:" + vmId); + continue; + } + long isDefault = (nic.isDefaultNic()) ? 1 : 0; + String nicIdString = Long.toString(nic.getId()); + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE, vm.getAccountId(), vm.getDataCenterId(), + vm.getId(), nicIdString, oldNetworkOfferingId, null, isDefault, VirtualMachine.class.getName(), vm.getUuid()); + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vm.getAccountId(), vm.getDataCenterId(), + vm.getId(), nicIdString, networkOfferingId, null, isDefault, VirtualMachine.class.getName(), vm.getUuid()); + } } - long isDefault = (nic.isDefaultNic()) ? 1 : 0; - String nicIdString = Long.toString(nic.getId()); - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE, vm.getAccountId(), vm.getDataCenterId(), - vm.getId(), nicIdString, oldNetworkOfferingId, null, isDefault, VirtualMachine.class.getName(), vm.getUuid()); - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vm.getAccountId(), vm.getDataCenterId(), - vm.getId(), nicIdString, networkOfferingId, null, isDefault, VirtualMachine.class.getName(), vm.getUuid()); - } - txn.commit(); + }); } else { network.setNetworkOfferingId(networkOfferingId); _networksDao.update(networkId, network, _networkMgr.finalizeServicesAndProvidersForNetwork(_entityMgr.findById(NetworkOffering.class, networkOfferingId), network.getPhysicalNetworkId())); @@ -2379,8 +2412,8 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { @Override @DB @ActionEvent(eventType = EventTypes.EVENT_PHYSICAL_NETWORK_CREATE, eventDescription = "Creating Physical Network", create = true) - public PhysicalNetwork createPhysicalNetwork(Long zoneId, String vnetRange, String networkSpeed, List - isolationMethods, String broadcastDomainRangeStr, Long domainId, List tags, String name) { + public PhysicalNetwork createPhysicalNetwork(final Long zoneId, final String vnetRange, final String networkSpeed, final List + isolationMethods, String broadcastDomainRangeStr, final Long domainId, final List tags, final String name) { // Check if zone exists if (zoneId == null) { @@ -2445,39 +2478,42 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { } } - Transaction txn = Transaction.currentTxn(); try { - txn.start(); - // Create the new physical network in the database - long id = _physicalNetworkDao.getNextInSequence(Long.class, "id"); - PhysicalNetworkVO pNetwork = new PhysicalNetworkVO(id, zoneId, vnetRange, networkSpeed, domainId, broadcastDomainRange, name); - pNetwork.setTags(tags); - pNetwork.setIsolationMethods(isolationMethods); + final BroadcastDomainRange broadcastDomainRangeFinal = broadcastDomainRange; + return Transaction.execute(new TransactionCallback() { + @Override + public PhysicalNetworkVO doInTransaction(TransactionStatus status) { + // Create the new physical network in the database + long id = _physicalNetworkDao.getNextInSequence(Long.class, "id"); + PhysicalNetworkVO pNetwork = new PhysicalNetworkVO(id, zoneId, vnetRange, networkSpeed, domainId, broadcastDomainRangeFinal, name); + pNetwork.setTags(tags); + pNetwork.setIsolationMethods(isolationMethods); - pNetwork = _physicalNetworkDao.persist(pNetwork); + pNetwork = _physicalNetworkDao.persist(pNetwork); - // Add vnet entries for the new zone if zone type is Advanced - if (vnetRange != null) { - addOrRemoveVnets(vnetRange.split(","), pNetwork); - } + // Add vnet entries for the new zone if zone type is Advanced + if (vnetRange != null) { + addOrRemoveVnets(vnetRange.split(","), pNetwork); + } - // add VirtualRouter as the default network service provider - addDefaultVirtualRouterToPhysicalNetwork(pNetwork.getId()); + // add VirtualRouter as the default network service provider + addDefaultVirtualRouterToPhysicalNetwork(pNetwork.getId()); - // add security group provider to the physical network - addDefaultSecurityGroupProviderToPhysicalNetwork(pNetwork.getId()); + // add security group provider to the physical network + addDefaultSecurityGroupProviderToPhysicalNetwork(pNetwork.getId()); - // add VPCVirtualRouter as the defualt network service provider - addDefaultVpcVirtualRouterToPhysicalNetwork(pNetwork.getId()); + // add VPCVirtualRouter as the defualt network service provider + addDefaultVpcVirtualRouterToPhysicalNetwork(pNetwork.getId()); - // add baremetal as the defualt network service provider - addDefaultBaremetalProvidersToPhysicalNetwork(pNetwork.getId()); + // add baremetal as the defualt network service provider + addDefaultBaremetalProvidersToPhysicalNetwork(pNetwork.getId()); - //Add Internal Load Balancer element as a default network service provider - addDefaultInternalLbProviderToPhysicalNetwork(pNetwork.getId()); - - txn.commit(); - return pNetwork; + //Add Internal Load Balancer element as a default network service provider + addDefaultInternalLbProviderToPhysicalNetwork(pNetwork.getId()); + + return pNetwork; + } + }); } catch (Exception ex) { s_logger.warn("Exception: ", ex); throw new CloudRuntimeException("Fail to create a physical network"); @@ -2568,7 +2604,7 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { } @DB - public void addOrRemoveVnets(String [] listOfRanges, PhysicalNetworkVO network) { + public void addOrRemoveVnets(String [] listOfRanges, final PhysicalNetworkVO network) { List addVnets = null; List removeVnets =null; HashSet tempVnets = new HashSet(); @@ -2611,22 +2647,27 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { } network.setVnet(comaSeperatedStingOfVnetRanges); - Transaction txn = Transaction.currentTxn(); - txn.start(); - if (addVnets != null) { - s_logger.debug("Adding vnet range " + addVnets.toString()+ " for the physicalNetwork id= " + network.getId() + " and zone id=" + network.getDataCenterId() - + " as a part of updatePhysicalNetwork call"); - //add vnet takes a list of strings to be added. each string is a vnet. - _dcDao.addVnet(network.getDataCenterId(), network.getId(), addVnets); - } - if (removeVnets != null) { - s_logger.debug("removing vnet range " + removeVnets.toString()+ " for the physicalNetwork id= " + network.getId() + " and zone id=" + network.getDataCenterId() - + " as a part of updatePhysicalNetwork call"); - //deleteVnets takes a list of strings to be removed. each string is a vnet. - _datacneter_vnet.deleteVnets(txn, network.getDataCenterId(), network.getId(), removeVnets); - } - _physicalNetworkDao.update(network.getId(), network); - txn.commit(); + final List addVnetsFinal = addVnets; + final List removeVnetsFinal = removeVnets; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + if (addVnetsFinal != null) { + s_logger.debug("Adding vnet range " + addVnetsFinal.toString()+ " for the physicalNetwork id= " + network.getId() + " and zone id=" + network.getDataCenterId() + + " as a part of updatePhysicalNetwork call"); + //add vnet takes a list of strings to be added. each string is a vnet. + _dcDao.addVnet(network.getDataCenterId(), network.getId(), addVnetsFinal); + } + if (removeVnetsFinal != null) { + s_logger.debug("removing vnet range " + removeVnetsFinal.toString()+ " for the physicalNetwork id= " + network.getId() + " and zone id=" + network.getDataCenterId() + + " as a part of updatePhysicalNetwork call"); + //deleteVnets takes a list of strings to be removed. each string is a vnet. + _datacneter_vnet.deleteVnets(TransactionLegacy.currentTxn(), network.getDataCenterId(), network.getId(), removeVnetsFinal); + } + _physicalNetworkDao.update(network.getId(), network); + } + }); + _physicalNetworkDao.releaseFromLockTable(network.getId()); } } @@ -2771,7 +2812,7 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { @Override @ActionEvent(eventType = EventTypes.EVENT_PHYSICAL_NETWORK_DELETE, eventDescription = "deleting physical network", async = true) @DB - public boolean deletePhysicalNetwork(Long physicalNetworkId) { + public boolean deletePhysicalNetwork(final Long physicalNetworkId) { // verify input parameters PhysicalNetworkVO pNetwork = _physicalNetworkDao.findById(physicalNetworkId); @@ -2783,48 +2824,47 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { checkIfPhysicalNetworkIsDeletable(physicalNetworkId); - Transaction txn = Transaction.currentTxn(); - txn.start(); - // delete vlans for this zone - List vlans = _vlanDao.listVlansByPhysicalNetworkId(physicalNetworkId); - for (VlanVO vlan : vlans) { - _vlanDao.remove(vlan.getId()); - } - - // Delete networks - List networks = _networksDao.listByPhysicalNetwork(physicalNetworkId); - if (networks != null && !networks.isEmpty()) { - for (NetworkVO network : networks) { - _networksDao.remove(network.getId()); + return Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + // delete vlans for this zone + List vlans = _vlanDao.listVlansByPhysicalNetworkId(physicalNetworkId); + for (VlanVO vlan : vlans) { + _vlanDao.remove(vlan.getId()); + } + + // Delete networks + List networks = _networksDao.listByPhysicalNetwork(physicalNetworkId); + if (networks != null && !networks.isEmpty()) { + for (NetworkVO network : networks) { + _networksDao.remove(network.getId()); + } + } + + // delete vnets + _dcDao.deleteVnet(physicalNetworkId); + + // delete service providers + List providers = _pNSPDao.listBy(physicalNetworkId); + + for(PhysicalNetworkServiceProviderVO provider : providers){ + try { + deleteNetworkServiceProvider(provider.getId()); + }catch (ResourceUnavailableException e) { + s_logger.warn("Unable to complete destroy of the physical network provider: " + provider.getProviderName() + ", id: "+ provider.getId(), e); + return false; + } catch (ConcurrentOperationException e) { + s_logger.warn("Unable to complete destroy of the physical network provider: " + provider.getProviderName() + ", id: "+ provider.getId(), e); + return false; + } + } + + // delete traffic types + _pNTrafficTypeDao.deleteTrafficTypes(physicalNetworkId); + + return _physicalNetworkDao.remove(physicalNetworkId); } - } - - // delete vnets - _dcDao.deleteVnet(physicalNetworkId); - - // delete service providers - List providers = _pNSPDao.listBy(physicalNetworkId); - - for(PhysicalNetworkServiceProviderVO provider : providers){ - try { - deleteNetworkServiceProvider(provider.getId()); - }catch (ResourceUnavailableException e) { - s_logger.warn("Unable to complete destroy of the physical network provider: " + provider.getProviderName() + ", id: "+ provider.getId(), e); - return false; - } catch (ConcurrentOperationException e) { - s_logger.warn("Unable to complete destroy of the physical network provider: " + provider.getProviderName() + ", id: "+ provider.getId(), e); - return false; - } - } - - // delete traffic types - _pNTrafficTypeDao.deleteTrafficTypes(physicalNetworkId); - - boolean success = _physicalNetworkDao.remove(physicalNetworkId); - - txn.commit(); - - return success; + }); } @DB @@ -2882,7 +2922,7 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { selectSql += " AND taken IS NOT NULL"; } - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql); stmt.setLong(1, physicalNetworkId); @@ -3045,12 +3085,9 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { accountGuestVlanMapVO.setGuestVlanRange(updatedVlanRange); _accountGuestVlanMapDao.update(guestVlanMapId, accountGuestVlanMapVO); } else { - Transaction txn = Transaction.currentTxn(); - txn.start(); accountGuestVlanMapVO = new AccountGuestVlanMapVO(vlanOwner.getAccountId(), physicalNetworkId); accountGuestVlanMapVO.setGuestVlanRange(startVlan + "-" + endVlan); _accountGuestVlanMapDao.persist(accountGuestVlanMapVO); - txn.commit(); } // For every guest vlan set the corresponding account guest vlan map id List finaVlanTokens = getVlanFromRange(accountGuestVlanMapVO.getGuestVlanRange()); @@ -3276,9 +3313,7 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { services = new ArrayList(element.getCapabilities().keySet()); } - Transaction txn = Transaction.currentTxn(); try { - txn.start(); // Create the new physical network in the database PhysicalNetworkServiceProviderVO nsp = new PhysicalNetworkServiceProviderVO(physicalNetworkId, providerName); // set enabled services @@ -3289,7 +3324,6 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { } nsp = _pNSPDao.persist(nsp); - txn.commit(); return nsp; } catch (Exception ex) { s_logger.warn("Exception: ", ex); @@ -3537,9 +3571,7 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { } } - Transaction txn = Transaction.currentTxn(); try { - txn.start(); // Create the new traffic type in the database if (xenLabel == null) { xenLabel = getDefaultXenNetworkLabel(trafficType); @@ -3547,7 +3579,6 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { PhysicalNetworkTrafficTypeVO pNetworktrafficType = new PhysicalNetworkTrafficTypeVO(physicalNetworkId, trafficType, xenLabel, kvmLabel, vmwareLabel, simulatorLabel, vlan); pNetworktrafficType = _pNTrafficTypeDao.persist(pNetworktrafficType); - txn.commit(); return pNetworktrafficType; } catch (Exception ex) { s_logger.warn("Exception: ", ex); @@ -3809,11 +3840,11 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { @Override @DB - public Network createPrivateNetwork(String networkName, String displayText, long physicalNetworkId, - String broadcastUriString, String startIp, String endIp, String gateway, String netmask, long networkOwnerId, Long vpcId, Boolean sourceNat, Long networkOfferingId) + public Network createPrivateNetwork(final String networkName, final String displayText, long physicalNetworkId, + String broadcastUriString, final String startIp, String endIp, final String gateway, String netmask, final long networkOwnerId, final Long vpcId, final Boolean sourceNat, final Long networkOfferingId) throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException { - Account owner = _accountMgr.getAccount(networkOwnerId); + final Account owner = _accountMgr.getAccount(networkOwnerId); // Get system network offering NetworkOfferingVO ntwkOff = null; @@ -3827,7 +3858,7 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { } // Validate physical network - PhysicalNetwork pNtwk = _physicalNetworkDao.findById(physicalNetworkId); + final PhysicalNetwork pNtwk = _physicalNetworkDao.findById(physicalNetworkId); if (pNtwk == null) { InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a physical network" + " having the given id"); @@ -3846,7 +3877,6 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { throw new InvalidParameterValueException("Invalid format for the endIp address parameter"); } - String cidr = null; if (!NetUtils.isValidIp(gateway)) { throw new InvalidParameterValueException("Invalid gateway"); } @@ -3854,10 +3884,10 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { throw new InvalidParameterValueException("Invalid netmask"); } - cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask); + final String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask); URI uri = BroadcastDomainType.fromString(broadcastUriString); - String uriString = uri.toString(); + final String uriString = uri.toString(); BroadcastDomainType tiep = BroadcastDomainType.getSchemeValue(uri); // numeric vlan or vlan uri are ok for now // TODO make a test for any supported scheme @@ -3866,49 +3896,58 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService { throw new InvalidParameterValueException("unsupported type of broadcastUri specified: " + broadcastUriString); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - - //lock datacenter as we need to get mac address seq from there - DataCenterVO dc = _dcDao.lockRow(pNtwk.getDataCenterId(), true); - - //check if we need to create guest network - Network privateNetwork = _networksDao.getPrivateNetwork(uriString, cidr, - networkOwnerId, pNtwk.getDataCenterId(), networkOfferingId); - if (privateNetwork == null) { - //create Guest network - privateNetwork = _networkMgr.createGuestNetwork(ntwkOff.getId(), networkName, displayText, gateway, cidr, uriString, - null, owner, null, pNtwk, pNtwk.getDataCenterId(), ACLType.Account, null, vpcId, null, null, true, null); - s_logger.debug("Created private network " + privateNetwork); - } else { - s_logger.debug("Private network already exists: " + privateNetwork); - //Do not allow multiple private gateways with same Vlan within a VPC - if(vpcId.equals(privateNetwork.getVpcId())){ - throw new InvalidParameterValueException("Private network for the vlan: " + uriString + " and cidr "+ cidr +" already exists " + - "for Vpc "+vpcId+" in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName()); - } + final NetworkOfferingVO ntwkOffFinal = ntwkOff; + try { + return Transaction.execute(new TransactionCallbackWithException() { + @Override + public Network doInTransaction(TransactionStatus status) throws ResourceAllocationException, InsufficientCapacityException { + //lock datacenter as we need to get mac address seq from there + DataCenterVO dc = _dcDao.lockRow(pNtwk.getDataCenterId(), true); + + //check if we need to create guest network + Network privateNetwork = _networksDao.getPrivateNetwork(uriString, cidr, + networkOwnerId, pNtwk.getDataCenterId(), networkOfferingId); + if (privateNetwork == null) { + //create Guest network + privateNetwork = _networkMgr.createGuestNetwork(ntwkOffFinal.getId(), networkName, displayText, gateway, cidr, uriString, + null, owner, null, pNtwk, pNtwk.getDataCenterId(), ACLType.Account, null, vpcId, null, null, true, null); + s_logger.debug("Created private network " + privateNetwork); + } else { + s_logger.debug("Private network already exists: " + privateNetwork); + //Do not allow multiple private gateways with same Vlan within a VPC + if(vpcId.equals(privateNetwork.getVpcId())){ + throw new InvalidParameterValueException("Private network for the vlan: " + uriString + " and cidr "+ cidr +" already exists " + + "for Vpc "+vpcId+" in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName()); + } + } + + //add entry to private_ip_address table + PrivateIpVO privateIp = _privateIpDao.findByIpAndSourceNetworkIdAndVpcId(privateNetwork.getId(), startIp, vpcId); + if (privateIp != null) { + throw new InvalidParameterValueException("Private ip address " + startIp + " already used for private gateway" + + " in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName()); + } + + Long mac = dc.getMacAddress(); + Long nextMac = mac + 1; + dc.setMacAddress(nextMac); + + privateIp = new PrivateIpVO(startIp, privateNetwork.getId(), nextMac, vpcId, sourceNat); + _privateIpDao.persist(privateIp); + + _dcDao.update(dc.getId(), dc); + + s_logger.debug("Private network " + privateNetwork + " is created"); + + return privateNetwork; + } + }); + } catch (Exception e) { + ExceptionUtil.rethrowRuntime(e); + ExceptionUtil.rethrow(e, ResourceAllocationException.class); + ExceptionUtil.rethrow(e, InsufficientCapacityException.class); + throw new IllegalStateException(e); } - - //add entry to private_ip_address table - PrivateIpVO privateIp = _privateIpDao.findByIpAndSourceNetworkIdAndVpcId(privateNetwork.getId(), startIp, vpcId); - if (privateIp != null) { - throw new InvalidParameterValueException("Private ip address " + startIp + " already used for private gateway" + - " in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName()); - } - - Long mac = dc.getMacAddress(); - Long nextMac = mac + 1; - dc.setMacAddress(nextMac); - - privateIp = new PrivateIpVO(startIp, privateNetwork.getId(), nextMac, vpcId, sourceNat); - _privateIpDao.persist(privateIp); - - _dcDao.update(dc.getId(), dc); - - txn.commit(); - s_logger.debug("Private network " + privateNetwork + " is created"); - - return privateNetwork; } diff --git a/server/src/com/cloud/network/NetworkUsageManagerImpl.java b/server/src/com/cloud/network/NetworkUsageManagerImpl.java index 41c1bc2912b..0f2feb07b91 100755 --- a/server/src/com/cloud/network/NetworkUsageManagerImpl.java +++ b/server/src/com/cloud/network/NetworkUsageManagerImpl.java @@ -88,6 +88,8 @@ import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CloudRuntimeException; @@ -290,11 +292,11 @@ public class NetworkUsageManagerImpl extends ManagerBase implements NetworkUsage return false; } - private boolean collectDirectNetworkUsage(HostVO host){ + private boolean collectDirectNetworkUsage(final HostVO host){ s_logger.debug("Direct Network Usage stats collector is running..."); - long zoneId = host.getDataCenterId(); - DetailVO lastCollectDetail = _detailsDao.findDetail(host.getId(),"last_collection"); + final long zoneId = host.getDataCenterId(); + final DetailVO lastCollectDetail = _detailsDao.findDetail(host.getId(),"last_collection"); if(lastCollectDetail == null){ s_logger.warn("Last collection time not available. Skipping direct usage collection for Traffic Monitor: "+host.getId()); return false; @@ -309,7 +311,7 @@ public class NetworkUsageManagerImpl extends ManagerBase implements NetworkUsage // This coule be made configurable rightNow.add(Calendar.HOUR_OF_DAY, -2); - Date now = rightNow.getTime(); + final Date now = rightNow.getTime(); if(lastCollection.after(now)){ s_logger.debug("Current time is less than 2 hours after last collection time : " + lastCollection.toString() + ". Skipping direct network usage collection"); @@ -361,7 +363,7 @@ public class NetworkUsageManagerImpl extends ManagerBase implements NetworkUsage } - List collectedStats = new ArrayList(); + final List collectedStats = new ArrayList(); //Get usage for Ips which were assigned for the entire duration if(fullDurationIpUsage.size() > 0){ @@ -431,28 +433,26 @@ public class NetworkUsageManagerImpl extends ManagerBase implements NetworkUsage return false; } //Persist all the stats and last_collection time in a single transaction - Transaction txn = Transaction.open(Transaction.CLOUD_DB); - try { - txn.start(); - for(UserStatisticsVO stat : collectedStats){ - UserStatisticsVO stats = _statsDao.lock(stat.getAccountId(), stat.getDataCenterId(), 0L, null, host.getId(), "DirectNetwork"); - if (stats == null) { - stats = new UserStatisticsVO(stat.getAccountId(), zoneId, null, host.getId(), "DirectNetwork", 0L); - stats.setCurrentBytesSent(stat.getCurrentBytesSent()); - stats.setCurrentBytesReceived(stat.getCurrentBytesReceived()); - _statsDao.persist(stats); - } else { - stats.setCurrentBytesSent(stats.getCurrentBytesSent() + stat.getCurrentBytesSent()); - stats.setCurrentBytesReceived(stats.getCurrentBytesReceived() + stat.getCurrentBytesReceived()); - _statsDao.update(stats.getId(), stats); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for(UserStatisticsVO stat : collectedStats){ + UserStatisticsVO stats = _statsDao.lock(stat.getAccountId(), stat.getDataCenterId(), 0L, null, host.getId(), "DirectNetwork"); + if (stats == null) { + stats = new UserStatisticsVO(stat.getAccountId(), zoneId, null, host.getId(), "DirectNetwork", 0L); + stats.setCurrentBytesSent(stat.getCurrentBytesSent()); + stats.setCurrentBytesReceived(stat.getCurrentBytesReceived()); + _statsDao.persist(stats); + } else { + stats.setCurrentBytesSent(stats.getCurrentBytesSent() + stat.getCurrentBytesSent()); + stats.setCurrentBytesReceived(stats.getCurrentBytesReceived() + stat.getCurrentBytesReceived()); + _statsDao.update(stats.getId(), stats); + } } + lastCollectDetail.setValue(""+now.getTime()); + _detailsDao.update(lastCollectDetail.getId(), lastCollectDetail); } - lastCollectDetail.setValue(""+now.getTime()); - _detailsDao.update(lastCollectDetail.getId(), lastCollectDetail); - txn.commit(); - } finally { - txn.close(); - } + }); return true; } diff --git a/server/src/com/cloud/network/PortProfileManagerImpl.java b/server/src/com/cloud/network/PortProfileManagerImpl.java index f17ee6f45b6..c731597925f 100644 --- a/server/src/com/cloud/network/PortProfileManagerImpl.java +++ b/server/src/com/cloud/network/PortProfileManagerImpl.java @@ -55,18 +55,7 @@ public class PortProfileManagerImpl { // Else, go ahead and create the port profile. PortProfileVO portProfileObj = new PortProfileVO(portProfName, vsmId, vlanId, pType, bType); - Transaction txn = Transaction.currentTxn(); - try { - txn.start(); - _portProfileDao.persist(portProfileObj); - txn.commit(); - } catch (Exception e) { - txn.rollback(); - throw new CloudRuntimeException(e.getMessage()); - } - - // Return the PortProfileVO object created. - return portProfileObj; + return _portProfileDao.persist(portProfileObj); } @DB @@ -95,18 +84,7 @@ public class PortProfileManagerImpl { // Else, go ahead and create the port profile. portProfileObj = new PortProfileVO(portProfName, vsmId, lowVlanId, highVlanId, pType, bType); - Transaction txn = Transaction.currentTxn(); - try { - txn.start(); - _portProfileDao.persist(portProfileObj); - txn.commit(); - } catch (Exception e) { - txn.rollback(); - throw new CloudRuntimeException(e.getMessage()); - } - - // Return the PortProfileVO object created. - return portProfileObj; + return _portProfileDao.persist(portProfileObj); } @DB @@ -121,16 +99,7 @@ public class PortProfileManagerImpl { // TODO: Should we be putting any checks here before removing // the port profile record from the db? - Transaction txn = Transaction.currentTxn(); - try { - txn.start(); - // Remove the VSM entry in CiscoNexusVSMDeviceVO's table. - _portProfileDao.remove(portProfileId); - txn.commit(); - } catch (Exception e) { - s_logger.info("Caught exception when trying to delete Port Profile record.." + e.getMessage()); - throw new CloudRuntimeException("Failed to delete Port Profile"); - } - return true; + // Remove the VSM entry in CiscoNexusVSMDeviceVO's table. + return _portProfileDao.remove(portProfileId); } } \ No newline at end of file diff --git a/server/src/com/cloud/network/StorageNetworkManagerImpl.java b/server/src/com/cloud/network/StorageNetworkManagerImpl.java index c0cb2894fc0..4719a42e8be 100755 --- a/server/src/com/cloud/network/StorageNetworkManagerImpl.java +++ b/server/src/com/cloud/network/StorageNetworkManagerImpl.java @@ -26,7 +26,6 @@ import javax.inject.Inject; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; - import org.apache.cloudstack.api.command.admin.network.CreateStorageNetworkIpRangeCmd; import org.apache.cloudstack.api.command.admin.network.DeleteStorageNetworkIpRangeCmd; import org.apache.cloudstack.api.command.admin.network.ListStorageNetworkIpRangeCmd; @@ -46,8 +45,12 @@ import com.cloud.network.dao.NetworkVO; import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.QueryBuilder; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; import com.cloud.vm.SecondaryStorageVmVO; @@ -93,7 +96,7 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet } } - private void createStorageIpEntires(Transaction txn, long rangeId, String startIp, String endIp, long zoneId) throws SQLException { + private void createStorageIpEntires(TransactionLegacy txn, long rangeId, String startIp, String endIp, long zoneId) throws SQLException { long startIPLong = NetUtils.ip2Long(startIp); long endIPLong = NetUtils.ip2Long(endIp); String insertSql = "INSERT INTO `cloud`.`op_dc_storage_network_ip_address` (range_id, ip_address, mac_address, taken) VALUES (?, ?, (select mac_address from `cloud`.`data_center` where id=?), ?)"; @@ -120,11 +123,11 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet @Override @DB public StorageNetworkIpRange updateIpRange(UpdateStorageNetworkIpRangeCmd cmd) { - Integer vlan = cmd.getVlan(); - Long rangeId = cmd.getId(); + final Integer vlan = cmd.getVlan(); + final Long rangeId = cmd.getId(); String startIp = cmd.getStartIp(); String endIp = cmd.getEndIp(); - String netmask = cmd.getNetmask(); + final String netmask = cmd.getNetmask(); if (netmask != null && !NetUtils.isValidNetmask(netmask)) { throw new CloudRuntimeException("Invalid netmask:" + netmask); @@ -147,45 +150,50 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet checkOverlapStorageIpRange(podId, startIp, endIp); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - try { - range = _sNwIpRangeDao.acquireInLockTable(range.getId()); - if (range == null) { - throw new CloudRuntimeException("Cannot acquire lock on storage ip range " + rangeId); + final String startIpFinal = startIp; + final String endIpFinal = endIp; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + StorageNetworkIpRangeVO range = null; + try { + range = _sNwIpRangeDao.acquireInLockTable(rangeId); + if (range == null) { + throw new CloudRuntimeException("Cannot acquire lock on storage ip range " + rangeId); + } + StorageNetworkIpRangeVO vo = _sNwIpRangeDao.createForUpdate(); + if (vlan != null) { + vo.setVlan(vlan); + } + if (startIpFinal != null) { + vo.setStartIp(startIpFinal); + } + if (endIpFinal != null) { + vo.setEndIp(endIpFinal); + } + if (netmask != null) { + vo.setNetmask(netmask); + } + _sNwIpRangeDao.update(rangeId, vo); + } finally { + if (range != null) { + _sNwIpRangeDao.releaseFromLockTable(range.getId()); + } + } } - StorageNetworkIpRangeVO vo = _sNwIpRangeDao.createForUpdate(); - if (vlan != null) { - vo.setVlan(vlan); - } - if (startIp != null) { - vo.setStartIp(startIp); - } - if (endIp != null) { - vo.setEndIp(endIp); - } - if (netmask != null) { - vo.setNetmask(netmask); - } - _sNwIpRangeDao.update(rangeId, vo); - } finally { - if (range != null) { - _sNwIpRangeDao.releaseFromLockTable(range.getId()); - } - } - txn.commit(); + }); return _sNwIpRangeDao.findById(rangeId); } @Override @DB - public StorageNetworkIpRange createIpRange(CreateStorageNetworkIpRangeCmd cmd) throws SQLException { - Long podId = cmd.getPodId(); - String startIp = cmd.getStartIp(); + public StorageNetworkIpRange createIpRange(final CreateStorageNetworkIpRangeCmd cmd) throws SQLException { + final Long podId = cmd.getPodId(); + final String startIp = cmd.getStartIp(); String endIp = cmd.getEndIp(); - Integer vlan = cmd.getVlan(); - String netmask = cmd.getNetmask(); + final Integer vlan = cmd.getVlan(); + final String netmask = cmd.getNetmask(); if (endIp == null) { endIp = startIp; @@ -199,7 +207,7 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet if (pod == null) { throw new CloudRuntimeException("Cannot find pod " + podId); } - Long zoneId = pod.getDataCenterId(); + final Long zoneId = pod.getDataCenterId(); List nws = _networkDao.listByZoneAndTrafficType(zoneId, TrafficType.Storage); if (nws.size() == 0) { @@ -208,34 +216,35 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet if (nws.size() > 1) { throw new CloudRuntimeException("Find more than one storage network in zone " + zoneId + "," + nws.size() + " found"); } - NetworkVO nw = nws.get(0); + final NetworkVO nw = nws.get(0); checkOverlapPrivateIpRange(podId, startIp, endIp); checkOverlapStorageIpRange(podId, startIp, endIp); - Transaction txn = Transaction.currentTxn(); StorageNetworkIpRangeVO range = null; - txn.start(); - range = new StorageNetworkIpRangeVO(zoneId, podId, nw.getId(), startIp, endIp, vlan, netmask, cmd.getGateWay()); - _sNwIpRangeDao.persist(range); - try { - createStorageIpEntires(txn, range.getId(), startIp, endIp, zoneId); - } catch (SQLException e) { - txn.rollback(); - StringBuilder err = new StringBuilder(); - err.append("Create storage network range failed."); - err.append("startIp=" + startIp); - err.append("endIp=" + endIp); - err.append("netmask=" + netmask); - err.append("zoneId=" + zoneId); - s_logger.debug(err.toString(), e); - throw e; - } + final String endIpFinal = endIp; + return Transaction.execute(new TransactionCallbackWithException() { + @Override + public StorageNetworkIpRangeVO doInTransaction(TransactionStatus status) throws SQLException { + StorageNetworkIpRangeVO range = new StorageNetworkIpRangeVO(zoneId, podId, nw.getId(), startIp, endIpFinal, vlan, netmask, cmd.getGateWay()); + _sNwIpRangeDao.persist(range); + try { + createStorageIpEntires(TransactionLegacy.currentTxn(), range.getId(), startIp, endIpFinal, zoneId); + } catch (SQLException e) { + StringBuilder err = new StringBuilder(); + err.append("Create storage network range failed."); + err.append("startIp=" + startIp); + err.append("endIp=" + endIpFinal); + err.append("netmask=" + netmask); + err.append("zoneId=" + zoneId); + s_logger.debug(err.toString(), e); + throw e; + } - txn.commit(); - - return range; + return range; + } + }); } private String getInUseIpAddress(long rangeId) { @@ -251,7 +260,7 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet @Override @DB public void deleteIpRange(DeleteStorageNetworkIpRangeCmd cmd) { - long rangeId = cmd.getId(); + final long rangeId = cmd.getId(); StorageNetworkIpRangeVO range = _sNwIpRangeDao.findById(rangeId); if (range == null) { throw new CloudRuntimeException("Can not find storage network ip range " + rangeId); @@ -261,26 +270,30 @@ public class StorageNetworkManagerImpl extends ManagerBase implements StorageNet throw new CloudRuntimeException(getInUseIpAddress(rangeId)); } - final Transaction txn = Transaction.currentTxn(); - txn.start(); - try { - range = _sNwIpRangeDao.acquireInLockTable(rangeId); - if (range == null) { - String msg = "Unable to acquire lock on storage network ip range id=" + rangeId + ", delete failed"; - s_logger.warn(msg); - throw new CloudRuntimeException(msg); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + StorageNetworkIpRangeVO range = null; + try { + range = _sNwIpRangeDao.acquireInLockTable(rangeId); + if (range == null) { + String msg = "Unable to acquire lock on storage network ip range id=" + rangeId + ", delete failed"; + s_logger.warn(msg); + throw new CloudRuntimeException(msg); + } + /* + * entries in op_dc_storage_network_ip_address will be deleted automatically due to + * fk_storage_ip_address__range_id constraint key + */ + _sNwIpRangeDao.remove(rangeId); + } finally { + if (range != null) { + _sNwIpRangeDao.releaseFromLockTable(rangeId); + } + } } - /* - * entries in op_dc_storage_network_ip_address will be deleted automatically due to - * fk_storage_ip_address__range_id constraint key - */ - _sNwIpRangeDao.remove(rangeId); - } finally { - if (range != null) { - _sNwIpRangeDao.releaseFromLockTable(rangeId); - } - } - txn.commit(); + }); + } @Override diff --git a/server/src/com/cloud/network/as/AutoScaleManagerImpl.java b/server/src/com/cloud/network/as/AutoScaleManagerImpl.java index 859211bd572..58a90bb1116 100644 --- a/server/src/com/cloud/network/as/AutoScaleManagerImpl.java +++ b/server/src/com/cloud/network/as/AutoScaleManagerImpl.java @@ -95,8 +95,10 @@ import com.cloud.utils.db.GenericDao; import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionCallback; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.net.NetUtils; @Local(value = { AutoScaleService.class, AutoScaleManager.class }) @@ -428,9 +430,9 @@ public class AutoScaleManagerImpl extends ManagerBase implements AutoScale } @DB - protected AutoScalePolicyVO checkValidityAndPersist(AutoScalePolicyVO autoScalePolicyVO, List conditionIds) { - int duration = autoScalePolicyVO.getDuration(); - int quietTime = autoScalePolicyVO.getQuietTime(); + protected AutoScalePolicyVO checkValidityAndPersist(final AutoScalePolicyVO autoScalePolicyVOFinal, final List conditionIds) { + final int duration = autoScalePolicyVOFinal.getDuration(); + final int quietTime = autoScalePolicyVOFinal.getQuietTime(); if (duration < 0) { throw new InvalidParameterValueException("duration is an invalid value: " + duration); @@ -440,48 +442,49 @@ public class AutoScaleManagerImpl extends ManagerBase implements AutoScale throw new InvalidParameterValueException("quiettime is an invalid value: " + quietTime); } - final Transaction txn = Transaction.currentTxn(); - txn.start(); - - autoScalePolicyVO = _autoScalePolicyDao.persist(autoScalePolicyVO); - - if (conditionIds != null) { - SearchBuilder conditionsSearch = _conditionDao.createSearchBuilder(); - conditionsSearch.and("ids", conditionsSearch.entity().getId(), Op.IN); - conditionsSearch.done(); - SearchCriteria sc = conditionsSearch.create(); - - sc.setParameters("ids", conditionIds.toArray(new Object[0])); - List conditions = _conditionDao.search(sc, null); - - ControlledEntity[] sameOwnerEntities = conditions.toArray(new ControlledEntity[conditions.size() + 1]); - sameOwnerEntities[sameOwnerEntities.length - 1] = autoScalePolicyVO; - _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, sameOwnerEntities); - - if (conditionIds.size() != conditions.size()) { - // TODO report the condition id which could not be found - throw new InvalidParameterValueException("Unable to find the condition specified"); - } - - ArrayList counterIds = new ArrayList(); - for (ConditionVO condition : conditions) { - if (counterIds.contains(condition.getCounterid())) { - throw new InvalidParameterValueException("atleast two conditions in the conditionids have the same counter. It is not right to apply two different conditions for the same counter"); + return Transaction.execute(new TransactionCallback() { + @Override + public AutoScalePolicyVO doInTransaction(TransactionStatus status) { + AutoScalePolicyVO autoScalePolicyVO = _autoScalePolicyDao.persist(autoScalePolicyVOFinal); + + if (conditionIds != null) { + SearchBuilder conditionsSearch = _conditionDao.createSearchBuilder(); + conditionsSearch.and("ids", conditionsSearch.entity().getId(), Op.IN); + conditionsSearch.done(); + SearchCriteria sc = conditionsSearch.create(); + + sc.setParameters("ids", conditionIds.toArray(new Object[0])); + List conditions = _conditionDao.search(sc, null); + + ControlledEntity[] sameOwnerEntities = conditions.toArray(new ControlledEntity[conditions.size() + 1]); + sameOwnerEntities[sameOwnerEntities.length - 1] = autoScalePolicyVO; + _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, sameOwnerEntities); + + if (conditionIds.size() != conditions.size()) { + // TODO report the condition id which could not be found + throw new InvalidParameterValueException("Unable to find the condition specified"); + } + + ArrayList counterIds = new ArrayList(); + for (ConditionVO condition : conditions) { + if (counterIds.contains(condition.getCounterid())) { + throw new InvalidParameterValueException("atleast two conditions in the conditionids have the same counter. It is not right to apply two different conditions for the same counter"); + } + counterIds.add(condition.getCounterid()); + } + + /* For update case remove the existing mappings and create fresh ones */ + _autoScalePolicyConditionMapDao.removeByAutoScalePolicyId(autoScalePolicyVO.getId()); + + for (Long conditionId : conditionIds) { + AutoScalePolicyConditionMapVO policyConditionMapVO = new AutoScalePolicyConditionMapVO(autoScalePolicyVO.getId(), conditionId); + _autoScalePolicyConditionMapDao.persist(policyConditionMapVO); + } } - counterIds.add(condition.getCounterid()); + + return autoScalePolicyVO; } - - /* For update case remove the existing mappings and create fresh ones */ - _autoScalePolicyConditionMapDao.removeByAutoScalePolicyId(autoScalePolicyVO.getId()); - - for (Long conditionId : conditionIds) { - AutoScalePolicyConditionMapVO policyConditionMapVO = new AutoScalePolicyConditionMapVO(autoScalePolicyVO.getId(), conditionId); - _autoScalePolicyConditionMapDao.persist(policyConditionMapVO); - } - } - - txn.commit(); - return autoScalePolicyVO; + }); } @Override @@ -511,7 +514,7 @@ public class AutoScaleManagerImpl extends ManagerBase implements AutoScale @Override @DB @ActionEvent(eventType = EventTypes.EVENT_AUTOSCALEPOLICY_DELETE, eventDescription = "deleting autoscale policy") - public boolean deleteAutoScalePolicy(long id) { + public boolean deleteAutoScalePolicy(final long id) { /* Check if entity is in database */ getEntityInDatabase(CallContext.current().getCallingAccount(), "AutoScale Policy", id, _autoScalePolicyDao); @@ -519,23 +522,25 @@ public class AutoScaleManagerImpl extends ManagerBase implements AutoScale throw new InvalidParameterValueException("Cannot delete AutoScale Policy when it is in use by one or more AutoScale Vm Groups"); } - Transaction txn = Transaction.currentTxn(); - txn.start(); + return Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + boolean success = true; + success = _autoScalePolicyDao.remove(id); + if (!success) { + s_logger.warn("Failed to remove AutoScale Policy db object"); + return false; + } + success = _autoScalePolicyConditionMapDao.removeByAutoScalePolicyId(id); + if (!success) { + s_logger.warn("Failed to remove AutoScale Policy Condition mappings"); + return false; + } + s_logger.info("Successfully deleted autoscale policy id : " + id); - boolean success = true; - success = _autoScalePolicyDao.remove(id); - if (!success) { - s_logger.warn("Failed to remove AutoScale Policy db object"); - return false; - } - success = _autoScalePolicyConditionMapDao.removeByAutoScalePolicyId(id); - if (!success) { - s_logger.warn("Failed to remove AutoScale Policy Condition mappings"); - return false; - } - txn.commit(); - s_logger.info("Successfully deleted autoscale policy id : " + id); - return true; // successful + return success; + } + }); } public void checkCallerAccess(String accountName, Long domainId) @@ -745,7 +750,7 @@ public class AutoScaleManagerImpl extends ManagerBase implements AutoScale @Override @DB @ActionEvent(eventType = EventTypes.EVENT_AUTOSCALEVMGROUP_DELETE, eventDescription = "deleting autoscale vm group") - public boolean deleteAutoScaleVmGroup(long id) { + public boolean deleteAutoScaleVmGroup(final long id) { AutoScaleVmGroupVO autoScaleVmGroupVO = getEntityInDatabase(CallContext.current().getCallingAccount(), "AutoScale Vm Group", id, _autoScaleVmGroupDao); if (autoScaleVmGroupVO.getState().equals(AutoScaleVmGroup.State_New)) { @@ -769,24 +774,27 @@ public class AutoScaleManagerImpl extends ManagerBase implements AutoScale } } - Transaction txn = Transaction.currentTxn(); - txn.start(); - success = _autoScaleVmGroupDao.remove(id); + return Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + boolean success = _autoScaleVmGroupDao.remove(id); + + if (!success) { + s_logger.warn("Failed to remove AutoScale Group db object"); + return false; + } + + success = _autoScaleVmGroupPolicyMapDao.removeByGroupId(id); + if (!success) { + s_logger.warn("Failed to remove AutoScale Group Policy mappings"); + return false; + } + + s_logger.info("Successfully deleted autoscale vm group id : " + id); + return success; // Successfull + } + }); - if (!success) { - s_logger.warn("Failed to remove AutoScale Group db object"); - return false; - } - - success = _autoScaleVmGroupPolicyMapDao.removeByGroupId(id); - if (!success) { - s_logger.warn("Failed to remove AutoScale Group Policy mappings"); - return false; - } - - txn.commit(); - s_logger.info("Successfully deleted autoscale vm group id : " + id); - return success; // Successfull } @Override @@ -831,13 +839,13 @@ public class AutoScaleManagerImpl extends ManagerBase implements AutoScale } @DB - protected AutoScaleVmGroupVO checkValidityAndPersist(AutoScaleVmGroupVO vmGroup, List passedScaleUpPolicyIds, List passedScaleDownPolicyIds) { + protected AutoScaleVmGroupVO checkValidityAndPersist(final AutoScaleVmGroupVO vmGroup, final List passedScaleUpPolicyIds, final List passedScaleDownPolicyIds) { int minMembers = vmGroup.getMinMembers(); int maxMembers = vmGroup.getMaxMembers(); int interval = vmGroup.getInterval(); List counters = new ArrayList(); List policies = new ArrayList(); - List policyIds = new ArrayList(); + final List policyIds = new ArrayList(); List currentScaleUpPolicyIds = new ArrayList(); List currentScaleDownPolicyIds = new ArrayList(); if (vmGroup.getCreated() != null) { @@ -887,20 +895,23 @@ public class AutoScaleManagerImpl extends ManagerBase implements AutoScale sameOwnerEntities[sameOwnerEntities.length - 1] = profileVO; _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, sameOwnerEntities); - final Transaction txn = Transaction.currentTxn(); - txn.start(); - vmGroup = _autoScaleVmGroupDao.persist(vmGroup); + return Transaction.execute(new TransactionCallback() { + @Override + public AutoScaleVmGroupVO doInTransaction(TransactionStatus status) { + AutoScaleVmGroupVO vmGroupNew = _autoScaleVmGroupDao.persist(vmGroup); + + if (passedScaleUpPolicyIds != null || passedScaleDownPolicyIds != null) { + _autoScaleVmGroupPolicyMapDao.removeByGroupId(vmGroupNew.getId()); + + for (Long policyId : policyIds) { + _autoScaleVmGroupPolicyMapDao.persist(new AutoScaleVmGroupPolicyMapVO(vmGroupNew.getId(), policyId)); + } + } - if (passedScaleUpPolicyIds != null || passedScaleDownPolicyIds != null) { - _autoScaleVmGroupPolicyMapDao.removeByGroupId(vmGroup.getId()); - - for (Long policyId : policyIds) { - _autoScaleVmGroupPolicyMapDao.persist(new AutoScaleVmGroupPolicyMapVO(vmGroup.getId(), policyId)); + return vmGroupNew; } - } - txn.commit(); + }); - return vmGroup; } @Override diff --git a/server/src/com/cloud/network/firewall/FirewallManagerImpl.java b/server/src/com/cloud/network/firewall/FirewallManagerImpl.java index c86476e2957..01504ab99f7 100644 --- a/server/src/com/cloud/network/firewall/FirewallManagerImpl.java +++ b/server/src/com/cloud/network/firewall/FirewallManagerImpl.java @@ -29,7 +29,6 @@ import javax.naming.ConfigurationException; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; - import org.apache.cloudstack.api.command.user.firewall.ListEgressFirewallRulesCmd; import org.apache.cloudstack.api.command.user.firewall.ListFirewallRulesCmd; import org.apache.cloudstack.context.CallContext; @@ -89,6 +88,9 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CloudRuntimeException; @@ -181,9 +183,9 @@ public class FirewallManagerImpl extends ManagerBase implements FirewallService, } @DB - protected FirewallRule createFirewallRule(Long ipAddrId, Account caller, String xId, Integer portStart, - Integer portEnd, String protocol, List sourceCidrList, Integer icmpCode, Integer icmpType, - Long relatedRuleId, FirewallRule.FirewallRuleType type, Long networkId, FirewallRule.TrafficType trafficType) throws NetworkRuleConflictException { + protected FirewallRule createFirewallRule(final Long ipAddrId, Account caller, final String xId, final Integer portStart, + final Integer portEnd, final String protocol, final List sourceCidrList, final Integer icmpCode, final Integer icmpType, + final Long relatedRuleId, final FirewallRule.FirewallRuleType type, final Long networkId, final FirewallRule.TrafficType trafficType) throws NetworkRuleConflictException { IPAddressVO ipAddress = null; if (ipAddrId != null){ @@ -222,25 +224,27 @@ public class FirewallManagerImpl extends ManagerBase implements FirewallService, domainId = network.getDomainId(); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - - FirewallRuleVO newRule = new FirewallRuleVO(xId, ipAddrId, portStart, portEnd, protocol.toLowerCase(), networkId, - accountId, domainId, Purpose.Firewall, sourceCidrList, icmpCode, icmpType, relatedRuleId, trafficType); - newRule.setType(type); - newRule = _firewallDao.persist(newRule); - - if (type == FirewallRuleType.User) - detectRulesConflict(newRule); - - if (!_firewallDao.setStateToAdd(newRule)) { - throw new CloudRuntimeException("Unable to update the state to add for " + newRule); - } - CallContext.current().setEventDetails("Rule Id: " + newRule.getId()); - - txn.commit(); - - return newRule; + final Long accountIdFinal = accountId; + final Long domainIdFinal = domainId; + return Transaction.execute(new TransactionCallbackWithException() { + @Override + public FirewallRuleVO doInTransaction(TransactionStatus status) throws NetworkRuleConflictException { + FirewallRuleVO newRule = new FirewallRuleVO(xId, ipAddrId, portStart, portEnd, protocol.toLowerCase(), networkId, + accountIdFinal, domainIdFinal, Purpose.Firewall, sourceCidrList, icmpCode, icmpType, relatedRuleId, trafficType); + newRule.setType(type); + newRule = _firewallDao.persist(newRule); + + if (type == FirewallRuleType.User) + detectRulesConflict(newRule); + + if (!_firewallDao.setStateToAdd(newRule)) { + throw new CloudRuntimeException("Unable to update the state to add for " + newRule); + } + CallContext.current().setEventDetails("Rule Id: " + newRule.getId()); + + return newRule; + } + }); } @Override @@ -724,33 +728,34 @@ public class FirewallManagerImpl extends ManagerBase implements FirewallService, @Override @DB - public void revokeRule(FirewallRuleVO rule, Account caller, long userId, boolean needUsageEvent) { + public void revokeRule(final FirewallRuleVO rule, Account caller, long userId, final boolean needUsageEvent) { if (caller != null) { _accountMgr.checkAccess(caller, null, true, rule); } - Transaction txn = Transaction.currentTxn(); - boolean generateUsageEvent = false; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + boolean generateUsageEvent = false; - txn.start(); - if (rule.getState() == State.Staged) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Found a rule that is still in stage state so just removing it: " + rule); + if (rule.getState() == State.Staged) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Found a rule that is still in stage state so just removing it: " + rule); + } + removeRule(rule); + generateUsageEvent = true; + } else if (rule.getState() == State.Add || rule.getState() == State.Active) { + rule.setState(State.Revoke); + _firewallDao.update(rule.getId(), rule); + generateUsageEvent = true; + } + + if (generateUsageEvent && needUsageEvent) { + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_RULE_DELETE, rule.getAccountId(), 0, rule.getId(), + null, rule.getClass().getName(), rule.getUuid()); + } } - removeRule(rule); - generateUsageEvent = true; - } else if (rule.getState() == State.Add || rule.getState() == State.Active) { - rule.setState(State.Revoke); - _firewallDao.update(rule.getId(), rule); - generateUsageEvent = true; - } - - if (generateUsageEvent && needUsageEvent) { - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_RULE_DELETE, rule.getAccountId(), 0, rule.getId(), - null, rule.getClass().getName(), rule.getUuid()); - } - - txn.commit(); + }); } @Override diff --git a/server/src/com/cloud/network/guru/DirectNetworkGuru.java b/server/src/com/cloud/network/guru/DirectNetworkGuru.java index 55da113a148..801d252ef81 100755 --- a/server/src/com/cloud/network/guru/DirectNetworkGuru.java +++ b/server/src/com/cloud/network/guru/DirectNetworkGuru.java @@ -22,7 +22,6 @@ import javax.ejb.Local; import javax.inject.Inject; import org.apache.log4j.Logger; - import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; import com.cloud.dc.DataCenter; @@ -33,6 +32,7 @@ import com.cloud.deploy.DeployDestination; import com.cloud.deploy.DeploymentPlan; import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.InsufficientAddressCapacityException; +import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.InsufficientVirtualNetworkCapcityException; import com.cloud.exception.InvalidParameterValueException; import com.cloud.network.IpAddressManager; @@ -56,6 +56,11 @@ import com.cloud.user.Account; import com.cloud.utils.component.AdapterBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionStatus; +import com.cloud.utils.exception.ExceptionUtil; import com.cloud.vm.Nic; import com.cloud.vm.Nic.ReservationStrategy; import com.cloud.vm.NicProfile; @@ -228,22 +233,30 @@ public class DirectNetworkGuru extends AdapterBase implements NetworkGuru { } @DB - protected void allocateDirectIp(NicProfile nic, Network network, VirtualMachineProfile vm, DataCenter dc, String requestedIp4Addr, String requestedIp6Addr) + protected void allocateDirectIp(final NicProfile nic, final Network network, final VirtualMachineProfile vm, final DataCenter dc, final String requestedIp4Addr, final String requestedIp6Addr) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { - - Transaction txn = Transaction.currentTxn(); - txn.start(); - _ipAddrMgr.allocateDirectIp(nic, dc, vm, network, requestedIp4Addr, requestedIp6Addr); - //save the placeholder nic if the vm is the Virtual router - if (vm.getType() == VirtualMachine.Type.DomainRouter) { - Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, null); - if (placeholderNic == null) { - s_logger.debug("Saving placeholder nic with ip4 address " + nic.getIp4Address() + " and ipv6 address " + nic.getIp6Address() + " for the network " + network); - _networkMgr.savePlaceholderNic(network, nic.getIp4Address(), nic.getIp6Address(), VirtualMachine.Type.DomainRouter); - } + + try { + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { + _ipAddrMgr.allocateDirectIp(nic, dc, vm, network, requestedIp4Addr, requestedIp6Addr); + //save the placeholder nic if the vm is the Virtual router + if (vm.getType() == VirtualMachine.Type.DomainRouter) { + Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, null); + if (placeholderNic == null) { + s_logger.debug("Saving placeholder nic with ip4 address " + nic.getIp4Address() + " and ipv6 address " + nic.getIp6Address() + " for the network " + network); + _networkMgr.savePlaceholderNic(network, nic.getIp4Address(), nic.getIp6Address(), VirtualMachine.Type.DomainRouter); + } + } + } + }); + } catch (InsufficientCapacityException e) { + ExceptionUtil.rethrow(e, InsufficientVirtualNetworkCapcityException.class); + ExceptionUtil.rethrow(e, InsufficientAddressCapacityException.class); + throw new IllegalStateException(e); } - txn.commit(); } @Override @@ -257,37 +270,37 @@ public class DirectNetworkGuru extends AdapterBase implements NetworkGuru { } @Override @DB - public void deallocate(Network network, NicProfile nic, VirtualMachineProfile vm) { + public void deallocate(final Network network, final NicProfile nic, VirtualMachineProfile vm) { if (s_logger.isDebugEnabled()) { s_logger.debug("Deallocate network: networkId: " + nic.getNetworkId() + ", ip: " + nic.getIp4Address()); } if (nic.getIp4Address() != null) { - IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), nic.getIp4Address()); + final IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), nic.getIp4Address()); if (ip != null) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - - // if the ip address a part of placeholder, don't release it - Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, null); - if (placeholderNic != null && placeholderNic.getIp4Address().equalsIgnoreCase(ip.getAddress().addr())) { - s_logger.debug("Not releasing direct ip " + ip.getId() +" yet as its ip is saved in the placeholder"); - } else { - _ipAddrMgr.markIpAsUnavailable(ip.getId()); - _ipAddressDao.unassignIpAddress(ip.getId()); - } - - //unassign nic secondary ip address - s_logger.debug("remove nic " + nic.getId() + " secondary ip "); - List nicSecIps = null; - nicSecIps = _nicSecondaryIpDao.getSecondaryIpAddressesForNic(nic.getId()); - for (String secIp: nicSecIps) { - IPAddressVO pubIp = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), secIp); - _ipAddrMgr.markIpAsUnavailable(pubIp.getId()); - _ipAddressDao.unassignIpAddress(pubIp.getId()); - } - - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + // if the ip address a part of placeholder, don't release it + Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, null); + if (placeholderNic != null && placeholderNic.getIp4Address().equalsIgnoreCase(ip.getAddress().addr())) { + s_logger.debug("Not releasing direct ip " + ip.getId() +" yet as its ip is saved in the placeholder"); + } else { + _ipAddrMgr.markIpAsUnavailable(ip.getId()); + _ipAddressDao.unassignIpAddress(ip.getId()); + } + + //unassign nic secondary ip address + s_logger.debug("remove nic " + nic.getId() + " secondary ip "); + List nicSecIps = null; + nicSecIps = _nicSecondaryIpDao.getSecondaryIpAddressesForNic(nic.getId()); + for (String secIp: nicSecIps) { + IPAddressVO pubIp = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), secIp); + _ipAddrMgr.markIpAsUnavailable(pubIp.getId()); + _ipAddressDao.unassignIpAddress(pubIp.getId()); + } + } + }); } } @@ -305,21 +318,23 @@ public class DirectNetworkGuru extends AdapterBase implements NetworkGuru { @DB public boolean trash(Network network, NetworkOffering offering) { //Have to remove all placeholder nics - List nics = _nicDao.listPlaceholderNicsByNetworkId(network.getId()); - Transaction txn = Transaction.currentTxn(); - txn.start(); - for (Nic nic : nics) { - if (nic.getIp4Address() != null) { - s_logger.debug("Releasing ip " + nic.getIp4Address() + " of placeholder nic " + nic); - IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), nic.getIp4Address()); - _ipAddrMgr.markIpAsUnavailable(ip.getId()); - _ipAddressDao.unassignIpAddress(ip.getId()); - s_logger.debug("Removing placeholder nic " + nic); - _nicDao.remove(nic.getId()); + final List nics = _nicDao.listPlaceholderNicsByNetworkId(network.getId()); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (Nic nic : nics) { + if (nic.getIp4Address() != null) { + s_logger.debug("Releasing ip " + nic.getIp4Address() + " of placeholder nic " + nic); + IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), nic.getIp4Address()); + _ipAddrMgr.markIpAsUnavailable(ip.getId()); + _ipAddressDao.unassignIpAddress(ip.getId()); + s_logger.debug("Removing placeholder nic " + nic); + _nicDao.remove(nic.getId()); + } + } } - } - - txn.commit(); + }); + return true; } diff --git a/server/src/com/cloud/network/guru/DirectPodBasedNetworkGuru.java b/server/src/com/cloud/network/guru/DirectPodBasedNetworkGuru.java index 31bc021ff73..053a786e67b 100755 --- a/server/src/com/cloud/network/guru/DirectPodBasedNetworkGuru.java +++ b/server/src/com/cloud/network/guru/DirectPodBasedNetworkGuru.java @@ -23,7 +23,6 @@ import javax.ejb.Local; import javax.inject.Inject; import org.apache.log4j.Logger; - import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; import com.cloud.configuration.ZoneConfig; @@ -53,6 +52,9 @@ import com.cloud.offering.NetworkOffering; import com.cloud.offerings.dao.NetworkOfferingDao; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.Nic; import com.cloud.vm.Nic.ReservationStrategy; @@ -133,18 +135,18 @@ public class DirectPodBasedNetworkGuru extends DirectNetworkGuru { getNewIp = true; } else { // we need to get a new ip address if we try to deploy a vm in a different pod - IPAddressVO ipVO = _ipAddressDao.findByIpAndSourceNetworkId(network.getId(), oldIp); + final IPAddressVO ipVO = _ipAddressDao.findByIpAndSourceNetworkId(network.getId(), oldIp); if (ipVO != null) { PodVlanMapVO mapVO = _podVlanDao.listPodVlanMapsByVlan(ipVO.getVlanId()); if (mapVO.getPodId() != dest.getPod().getId()) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - - //release the old ip here - _ipAddrMgr.markIpAsUnavailable(ipVO.getId()); - _ipAddressDao.unassignIpAddress(ipVO.getId()); - - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + //release the old ip here + _ipAddrMgr.markIpAsUnavailable(ipVO.getId()); + _ipAddressDao.unassignIpAddress(ipVO.getId()); + } + }); nic.setIp4Address(null); getNewIp = true; @@ -163,54 +165,55 @@ public class DirectPodBasedNetworkGuru extends DirectNetworkGuru { } @DB - protected void getIp(NicProfile nic, Pod pod, VirtualMachineProfile vm, Network network) throws InsufficientVirtualNetworkCapcityException, + protected void getIp(final NicProfile nic, final Pod pod, final VirtualMachineProfile vm, final Network network) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException { - DataCenter dc = _dcDao.findById(pod.getDataCenterId()); + final DataCenter dc = _dcDao.findById(pod.getDataCenterId()); if (nic.getIp4Address() == null) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - - PublicIp ip = null; - List podRefs = _podVlanDao.listPodVlanMapsByPod(pod.getId()); - String podRangeGateway = null; - if (!podRefs.isEmpty()) { - podRangeGateway = _vlanDao.findById(podRefs.get(0).getVlanDbId()).getVlanGateway(); - } - //Get ip address from the placeholder and don't allocate a new one - if (vm.getType() == VirtualMachine.Type.DomainRouter) { - Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, pod.getId()); - if (placeholderNic != null) { - IPAddressVO userIp = _ipAddressDao.findByIpAndSourceNetworkId(network.getId(), placeholderNic.getIp4Address()); - ip = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId())); - s_logger.debug("Nic got an ip address " + placeholderNic.getIp4Address() + " stored in placeholder nic for the network " + network + " and gateway " + podRangeGateway); + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws InsufficientAddressCapacityException { + PublicIp ip = null; + List podRefs = _podVlanDao.listPodVlanMapsByPod(pod.getId()); + String podRangeGateway = null; + if (!podRefs.isEmpty()) { + podRangeGateway = _vlanDao.findById(podRefs.get(0).getVlanDbId()).getVlanGateway(); + } + //Get ip address from the placeholder and don't allocate a new one + if (vm.getType() == VirtualMachine.Type.DomainRouter) { + Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, pod.getId()); + if (placeholderNic != null) { + IPAddressVO userIp = _ipAddressDao.findByIpAndSourceNetworkId(network.getId(), placeholderNic.getIp4Address()); + ip = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId())); + s_logger.debug("Nic got an ip address " + placeholderNic.getIp4Address() + " stored in placeholder nic for the network " + network + " and gateway " + podRangeGateway); + } + } + + if (ip == null) { + ip = _ipAddrMgr.assignPublicIpAddress(dc.getId(), pod.getId(), vm.getOwner(), VlanType.DirectAttached, network.getId(), null, false); + } + + nic.setIp4Address(ip.getAddress().toString()); + nic.setFormat(AddressFormat.Ip4); + nic.setGateway(ip.getGateway()); + nic.setNetmask(ip.getNetmask()); + if (ip.getVlanTag() != null && ip.getVlanTag().equalsIgnoreCase(Vlan.UNTAGGED)) { + nic.setIsolationUri(IsolationType.Ec2.toUri(Vlan.UNTAGGED)); + nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED)); + nic.setBroadcastType(BroadcastDomainType.Native); + } + nic.setReservationId(String.valueOf(ip.getVlanTag())); + nic.setMacAddress(ip.getMacAddress()); + + //save the placeholder nic if the vm is the Virtual router + if (vm.getType() == VirtualMachine.Type.DomainRouter) { + Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, pod.getId()); + if (placeholderNic == null) { + s_logger.debug("Saving placeholder nic with ip4 address " + nic.getIp4Address() + " for the network " + network); + _networkMgr.savePlaceholderNic(network, nic.getIp4Address(), null, VirtualMachine.Type.DomainRouter); + } + } } - } - - if (ip == null) { - ip = _ipAddrMgr.assignPublicIpAddress(dc.getId(), pod.getId(), vm.getOwner(), VlanType.DirectAttached, network.getId(), null, false); - } - - nic.setIp4Address(ip.getAddress().toString()); - nic.setFormat(AddressFormat.Ip4); - nic.setGateway(ip.getGateway()); - nic.setNetmask(ip.getNetmask()); - if (ip.getVlanTag() != null && ip.getVlanTag().equalsIgnoreCase(Vlan.UNTAGGED)) { - nic.setIsolationUri(IsolationType.Ec2.toUri(Vlan.UNTAGGED)); - nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED)); - nic.setBroadcastType(BroadcastDomainType.Native); - } - nic.setReservationId(String.valueOf(ip.getVlanTag())); - nic.setMacAddress(ip.getMacAddress()); - - //save the placeholder nic if the vm is the Virtual router - if (vm.getType() == VirtualMachine.Type.DomainRouter) { - Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, pod.getId()); - if (placeholderNic == null) { - s_logger.debug("Saving placeholder nic with ip4 address " + nic.getIp4Address() + " for the network " + network); - _networkMgr.savePlaceholderNic(network, nic.getIp4Address(), null, VirtualMachine.Type.DomainRouter); - } - } - txn.commit(); + }); } nic.setDns1(dc.getDns1()); nic.setDns2(dc.getDns2()); diff --git a/server/src/com/cloud/network/guru/GuestNetworkGuru.java b/server/src/com/cloud/network/guru/GuestNetworkGuru.java index 20b0ce5b86a..85728c22fbb 100755 --- a/server/src/com/cloud/network/guru/GuestNetworkGuru.java +++ b/server/src/com/cloud/network/guru/GuestNetworkGuru.java @@ -24,7 +24,6 @@ import javax.ejb.Local; import javax.inject.Inject; import org.apache.log4j.Logger; - import org.apache.cloudstack.context.CallContext; import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; import org.apache.cloudstack.framework.config.ConfigKey; @@ -70,6 +69,8 @@ import com.cloud.utils.Pair; import com.cloud.utils.component.AdapterBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; import com.cloud.vm.Nic.ReservationStrategy; @@ -223,13 +224,15 @@ public abstract class GuestNetworkGuru extends AdapterBase implements NetworkGur s_logger.debug("Deallocate network: networkId: " + nic.getNetworkId() + ", ip: " + nic.getIp4Address()); } - IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), nic.getIp4Address()); + final IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), nic.getIp4Address()); if (ip != null) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - _ipAddrMgr.markIpAsUnavailable(ip.getId()); - _ipAddressDao.unassignIpAddress(ip.getId()); - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _ipAddrMgr.markIpAsUnavailable(ip.getId()); + _ipAddressDao.unassignIpAddress(ip.getId()); + } + }); } nic.deallocate(); } diff --git a/server/src/com/cloud/network/guru/PublicNetworkGuru.java b/server/src/com/cloud/network/guru/PublicNetworkGuru.java index f82e22e8dde..6fed1a6ff7e 100755 --- a/server/src/com/cloud/network/guru/PublicNetworkGuru.java +++ b/server/src/com/cloud/network/guru/PublicNetworkGuru.java @@ -20,7 +20,6 @@ import javax.ejb.Local; import javax.inject.Inject; import org.apache.log4j.Logger; - import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; import com.cloud.dc.DataCenter; @@ -50,6 +49,8 @@ import com.cloud.user.Account; import com.cloud.utils.component.AdapterBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.Nic.ReservationStrategy; import com.cloud.vm.NicProfile; @@ -192,16 +193,15 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru { s_logger.debug("public network deallocate network: networkId: " + nic.getNetworkId() + ", ip: " + nic.getIp4Address()); } - IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), nic.getIp4Address()); + final IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), nic.getIp4Address()); if (ip != null && nic.getReservationStrategy() != ReservationStrategy.Managed) { - - Transaction txn = Transaction.currentTxn(); - txn.start(); - - _ipAddrMgr.markIpAsUnavailable(ip.getId()); - _ipAddressDao.unassignIpAddress(ip.getId()); - - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _ipAddrMgr.markIpAsUnavailable(ip.getId()); + _ipAddressDao.unassignIpAddress(ip.getId()); + } + }); } nic.deallocate(); diff --git a/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java b/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java index c685ee3e40b..780a9423b2f 100755 --- a/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java +++ b/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java @@ -152,6 +152,10 @@ import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.Ip; import com.cloud.utils.net.NetUtils; @@ -379,11 +383,11 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements @Override @DB - public boolean configureLbAutoScaleVmGroup(long vmGroupid, String currentState) throws ResourceUnavailableException { - AutoScaleVmGroupVO vmGroup = _autoScaleVmGroupDao.findById(vmGroupid); + public boolean configureLbAutoScaleVmGroup(final long vmGroupid, String currentState) throws ResourceUnavailableException { + final AutoScaleVmGroupVO vmGroup = _autoScaleVmGroupDao.findById(vmGroupid); boolean success = false; - LoadBalancerVO loadBalancer = _lbDao.findById(vmGroup.getLoadBalancerId()); + final LoadBalancerVO loadBalancer = _lbDao.findById(vmGroup.getLoadBalancerId()); FirewallRule.State backupState = loadBalancer.getState(); @@ -416,14 +420,17 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements if (success) { if (vmGroup.getState().equals(AutoScaleVmGroup.State_New)) { - Transaction.currentTxn().start(); - loadBalancer.setState(FirewallRule.State.Active); - s_logger.debug("LB rule " + loadBalancer.getId() + " state is set to Active"); - _lbDao.persist(loadBalancer); - vmGroup.setState(AutoScaleVmGroup.State_Enabled); - _autoScaleVmGroupDao.persist(vmGroup); - s_logger.debug("LB Auto Scale Vm Group with Id: " + vmGroupid + " is set to Enabled state."); - Transaction.currentTxn().commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + loadBalancer.setState(FirewallRule.State.Active); + s_logger.debug("LB rule " + loadBalancer.getId() + " state is set to Active"); + _lbDao.persist(loadBalancer); + vmGroup.setState(AutoScaleVmGroup.State_Enabled); + _autoScaleVmGroupDao.persist(vmGroup); + s_logger.debug("LB Auto Scale Vm Group with Id: " + vmGroupid + " is set to Enabled state."); + } + }); } s_logger.info("Successfully configured LB Autoscale Vm Group with Id: " + vmGroupid); } @@ -813,7 +820,7 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements throw new InvalidParameterException("Invalid Load balancer : " + healthCheckPolicy.getLoadBalancerId() + " for HealthCheck policy id: " + healthCheckPolicyId); } - long loadBalancerId = loadBalancer.getId(); + final long loadBalancerId = loadBalancer.getId(); FirewallRule.State backupState = loadBalancer.getState(); _accountMgr.checkAccess(caller.getCallingAccount(), null, true, loadBalancer); @@ -830,17 +837,19 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements + ", healthCheckpolicyID " + healthCheckPolicyId); // removing the state of services set by the monitor. - List maps = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId); + final List maps = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId); if (maps != null) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - s_logger.debug("Resetting health state policy for services in loadbalancing rule id : " - + loadBalancerId); - for (LoadBalancerVMMapVO map : maps) { - map.setState(null); - _lb2VmMapDao.persist(map); - } - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + s_logger.debug("Resetting health state policy for services in loadbalancing rule id : " + + loadBalancerId); + for (LoadBalancerVMMapVO map : maps) { + map.setState(null); + _lb2VmMapDao.persist(map); + } + } + }); } try { @@ -957,7 +966,7 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements CallContext ctx = CallContext.current(); Account caller = ctx.getCallingAccount(); - LoadBalancerVO loadBalancer = _lbDao.findById(loadBalancerId); + final LoadBalancerVO loadBalancer = _lbDao.findById(loadBalancerId); if (loadBalancer == null) { throw new InvalidParameterValueException("Failed to assign to load balancer " + loadBalancerId + ", the load balancer was not found."); @@ -969,7 +978,7 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements mappedInstanceIds.add(Long.valueOf(mappedInstance.getInstanceId())); } - List vmsToAdd = new ArrayList(); + final List vmsToAdd = new ArrayList(); if (instanceIds == null || instanceIds.isEmpty()) { s_logger.warn("List of vms to assign to the lb, is empty"); @@ -1022,13 +1031,16 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements vmsToAdd.add(vm); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - for (UserVm vm : vmsToAdd) { - LoadBalancerVMMapVO map = new LoadBalancerVMMapVO(loadBalancer.getId(), vm.getId(), false); - map = _lb2VmMapDao.persist(map); - } - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (UserVm vm : vmsToAdd) { + LoadBalancerVMMapVO map = new LoadBalancerVMMapVO(loadBalancer.getId(), vm.getId(), false); + map = _lb2VmMapDao.persist(map); + } + } + }); + if (_autoScaleVmGroupDao.isAutoScaleLoadBalancer(loadBalancerId)) { // For autoscaled loadbalancer, the rules need not be applied, // meaning the call need not reach the resource layer. @@ -1044,13 +1056,15 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements success = true; } catch (ResourceUnavailableException e) { if (isRollBackAllowedForProvider(loadBalancer)) { - List vmInstanceIds = new ArrayList(); - txn = Transaction.currentTxn(); - txn.start(); - for (UserVm vm : vmsToAdd) { - vmInstanceIds.add(vm.getId()); - } - txn.commit(); + final List vmInstanceIds = new ArrayList(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (UserVm vm : vmsToAdd) { + vmInstanceIds.add(vm.getId()); + } + } + }); if (!vmInstanceIds.isEmpty()) { _lb2VmMapDao.remove(loadBalancer.getId(), vmInstanceIds, null); s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + " while attaching VM: " @@ -1203,49 +1217,52 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements } @DB - public boolean deleteLoadBalancerRule(long loadBalancerId, boolean apply, Account caller, long callerUserId, + public boolean deleteLoadBalancerRule(final long loadBalancerId, boolean apply, Account caller, long callerUserId, boolean rollBack) { - LoadBalancerVO lb = _lbDao.findById(loadBalancerId); - Transaction txn = Transaction.currentTxn(); - boolean generateUsageEvent = false; - boolean success = true; + final LoadBalancerVO lb = _lbDao.findById(loadBalancerId); FirewallRule.State backupState = lb.getState(); - txn.start(); - if (lb.getState() == FirewallRule.State.Staged) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Found a rule that is still in stage state so just removing it: " + lb); + List backupMaps = Transaction.execute(new TransactionCallback>() { + @Override + public List doInTransaction(TransactionStatus status) { + boolean generateUsageEvent = false; + + if (lb.getState() == FirewallRule.State.Staged) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Found a rule that is still in stage state so just removing it: " + lb); + } + generateUsageEvent = true; + } else if (lb.getState() == FirewallRule.State.Add || lb.getState() == FirewallRule.State.Active) { + lb.setState(FirewallRule.State.Revoke); + _lbDao.persist(lb); + generateUsageEvent = true; + } + List backupMaps = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId); + List maps = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId); + if (maps != null) { + for (LoadBalancerVMMapVO map : maps) { + map.setRevoke(true); + _lb2VmMapDao.persist(map); + s_logger.debug("Set load balancer rule for revoke: rule id " + loadBalancerId + ", vmId " + + map.getInstanceId()); + } + } + + List hcPolicies = _lb2healthcheckDao.listByLoadBalancerId(loadBalancerId); + for (LBHealthCheckPolicyVO lbHealthCheck : hcPolicies) { + lbHealthCheck.setRevoke(true); + _lb2healthcheckDao.persist(lbHealthCheck); + } + + if (generateUsageEvent) { + // Generate usage event right after all rules were marked for revoke + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_LOAD_BALANCER_DELETE, lb.getAccountId(), 0, lb.getId(), + null, LoadBalancingRule.class.getName(), lb.getUuid()); + } + + return backupMaps; } - generateUsageEvent = true; - } else if (lb.getState() == FirewallRule.State.Add || lb.getState() == FirewallRule.State.Active) { - lb.setState(FirewallRule.State.Revoke); - _lbDao.persist(lb); - generateUsageEvent = true; - } - List backupMaps = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId); - List maps = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId); - if (maps != null) { - for (LoadBalancerVMMapVO map : maps) { - map.setRevoke(true); - _lb2VmMapDao.persist(map); - s_logger.debug("Set load balancer rule for revoke: rule id " + loadBalancerId + ", vmId " - + map.getInstanceId()); - } - } - - List hcPolicies = _lb2healthcheckDao.listByLoadBalancerId(loadBalancerId); - for (LBHealthCheckPolicyVO lbHealthCheck : hcPolicies) { - lbHealthCheck.setRevoke(true); - _lb2healthcheckDao.persist(lbHealthCheck); - } - - if (generateUsageEvent) { - // Generate usage event right after all rules were marked for revoke - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_LOAD_BALANCER_DELETE, lb.getAccountId(), 0, lb.getId(), - null, LoadBalancingRule.class.getName(), lb.getUuid()); - } - - txn.commit(); + }); // gather external network usage stats for this lb rule NetworkVO network = _networkDao.findById(lb.getNetworkId()); @@ -1283,7 +1300,7 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements if (relatedRule != null) { s_logger.warn("Unable to remove firewall rule id=" + lb.getId() + " as it has related firewall rule id=" + relatedRule.getId() + "; leaving it in Revoke state"); - success = false; + return false; } else { _firewallMgr.removeRule(lb); } @@ -1293,11 +1310,9 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements // Bug CS-15411 opened to document this // _elbMgr.handleDeleteLoadBalancerRule(lb, callerUserId, caller); - if (success) { - s_logger.debug("Load balancer with id " + lb.getId() + " is removed successfully"); - } - - return success; + s_logger.debug("Load balancer with id " + lb.getId() + " is removed successfully"); + + return true; } @Override @@ -1392,8 +1407,8 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements @DB @Override - public LoadBalancer createPublicLoadBalancer(String xId, String name, String description, - int srcPort, int destPort, long sourceIpId, String protocol, String algorithm, boolean openFirewall, CallContext caller) + public LoadBalancer createPublicLoadBalancer(final String xId, final String name, final String description, + final int srcPort, final int destPort, final long sourceIpId, final String protocol, final String algorithm, final boolean openFirewall, final CallContext caller) throws NetworkRuleConflictException { if (!NetUtils.isValidPort(destPort)) { @@ -1404,7 +1419,7 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements throw new InvalidParameterValueException("Invalid algorithm: " + algorithm); } - IPAddressVO ipAddr = _ipAddressDao.findById(sourceIpId); + final IPAddressVO ipAddr = _ipAddressDao.findById(sourceIpId); // make sure ip address exists if (ipAddr == null || !ipAddr.readyToUse()) { InvalidParameterValueException ex = new InvalidParameterValueException( @@ -1426,7 +1441,7 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements _accountMgr.checkAccess(caller.getCallingAccount(), null, true, ipAddr); - Long networkId = ipAddr.getAssociatedWithNetworkId(); + final Long networkId = ipAddr.getAssociatedWithNetworkId(); if (networkId == null) { InvalidParameterValueException ex = new InvalidParameterValueException( "Unable to create load balancer rule ; specified sourceip id is not associated with any network"); @@ -1440,61 +1455,60 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements _firewallMgr.validateFirewallRule(caller.getCallingAccount(), ipAddr, srcPort, srcPort, protocol, Purpose.LoadBalancing, FirewallRuleType.User, networkId, null); - LoadBalancerVO newRule = new LoadBalancerVO(xId, name, description, - sourceIpId, srcPort, destPort, algorithm, - networkId, ipAddr.getAllocatedToAccountId(), ipAddr.getAllocatedInDomainId()); - // verify rule is supported by Lb provider of the network - Ip sourceIp = getSourceIp(newRule); - LoadBalancingRule loadBalancing = new LoadBalancingRule(newRule, new ArrayList(), - new ArrayList(), new ArrayList(), sourceIp); - if (!validateLbRule(loadBalancing)) { - throw new InvalidParameterValueException("LB service provider cannot support this rule"); - } + return Transaction.execute(new TransactionCallbackWithException() { + @Override + public LoadBalancerVO doInTransaction(TransactionStatus status) throws NetworkRuleConflictException { + LoadBalancerVO newRule = new LoadBalancerVO(xId, name, description, + sourceIpId, srcPort, destPort, algorithm, + networkId, ipAddr.getAllocatedToAccountId(), ipAddr.getAllocatedInDomainId()); - Transaction txn = Transaction.currentTxn(); - txn.start(); + // verify rule is supported by Lb provider of the network + Ip sourceIp = getSourceIp(newRule); + LoadBalancingRule loadBalancing = new LoadBalancingRule(newRule, new ArrayList(), + new ArrayList(), new ArrayList(), sourceIp); + if (!validateLbRule(loadBalancing)) { + throw new InvalidParameterValueException("LB service provider cannot support this rule"); + } + + newRule = _lbDao.persist(newRule); - newRule = _lbDao.persist(newRule); - - //create rule for all CIDRs - if (openFirewall) { - _firewallMgr.createRuleForAllCidrs(sourceIpId, caller.getCallingAccount(), srcPort, - srcPort, protocol, null, null, newRule.getId(), networkId); - } - - boolean success = true; - - try { - _firewallMgr.detectRulesConflict(newRule); - if (!_firewallDao.setStateToAdd(newRule)) { - throw new CloudRuntimeException("Unable to update the state to add for " + newRule); + //create rule for all CIDRs + if (openFirewall) { + _firewallMgr.createRuleForAllCidrs(sourceIpId, caller.getCallingAccount(), srcPort, + srcPort, protocol, null, null, newRule.getId(), networkId); + } + + boolean success = true; + + try { + _firewallMgr.detectRulesConflict(newRule); + if (!_firewallDao.setStateToAdd(newRule)) { + throw new CloudRuntimeException("Unable to update the state to add for " + newRule); + } + s_logger.debug("Load balancer " + newRule.getId() + " for Ip address id=" + sourceIpId + ", public port " + + srcPort + ", private port " + destPort + " is added successfully."); + CallContext.current().setEventDetails("Load balancer Id: " + newRule.getId()); + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_LOAD_BALANCER_CREATE, ipAddr.getAllocatedToAccountId(), + ipAddr.getDataCenterId(), newRule.getId(), null, LoadBalancingRule.class.getName(), + newRule.getUuid()); + + return newRule; + } catch (Exception e) { + success = false; + if (e instanceof NetworkRuleConflictException) { + throw (NetworkRuleConflictException) e; + } + throw new CloudRuntimeException("Unable to add rule for ip address id=" + newRule.getSourceIpAddressId(), e); + } finally { + if (!success && newRule != null) { + _firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false); + removeLBRule(newRule); + } + } } - s_logger.debug("Load balancer " + newRule.getId() + " for Ip address id=" + sourceIpId + ", public port " - + srcPort + ", private port " + destPort + " is added successfully."); - CallContext.current().setEventDetails("Load balancer Id: " + newRule.getId()); - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_LOAD_BALANCER_CREATE, ipAddr.getAllocatedToAccountId(), - ipAddr.getDataCenterId(), newRule.getId(), null, LoadBalancingRule.class.getName(), - newRule.getUuid()); - txn.commit(); + }); - return newRule; - } catch (Exception e) { - success = false; - if (e instanceof NetworkRuleConflictException) { - throw (NetworkRuleConflictException) e; - } - throw new CloudRuntimeException("Unable to add rule for ip address id=" + newRule.getSourceIpAddressId(), e); - } finally { - if (!success && newRule != null) { - - txn.start(); - _firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false); - removeLBRule(newRule); - - txn.commit(); - } - } } @Override @@ -1583,7 +1597,6 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements @DB protected boolean applyLoadBalancerRules(List lbs, boolean updateRulesInDB) throws ResourceUnavailableException { - Transaction txn = Transaction.currentTxn(); List rules = new ArrayList(); for (LoadBalancerVO lb : lbs) { rules.add(getLoadBalancerRuleToApply(lb)); @@ -1595,57 +1608,63 @@ public class LoadBalancingRulesManagerImpl extends ManagerBase implements } if (updateRulesInDB) { - for (LoadBalancerVO lb : lbs) { - boolean checkForReleaseElasticIp = false; - txn.start(); - if (lb.getState() == FirewallRule.State.Revoke) { - removeLBRule(lb); - s_logger.debug("LB " + lb.getId() + " is successfully removed"); - checkForReleaseElasticIp = true; - } else if (lb.getState() == FirewallRule.State.Add) { - lb.setState(FirewallRule.State.Active); - s_logger.debug("LB rule " + lb.getId() + " state is set to Active"); - _lbDao.persist(lb); - } + for (final LoadBalancerVO lb : lbs) { + boolean checkForReleaseElasticIp = Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + boolean checkForReleaseElasticIp = false; - // remove LB-Vm mappings that were state to revoke - List lbVmMaps = _lb2VmMapDao.listByLoadBalancerId(lb.getId(), true); - List instanceIds = new ArrayList(); + if (lb.getState() == FirewallRule.State.Revoke) { + removeLBRule(lb); + s_logger.debug("LB " + lb.getId() + " is successfully removed"); + checkForReleaseElasticIp = true; + } else if (lb.getState() == FirewallRule.State.Add) { + lb.setState(FirewallRule.State.Active); + s_logger.debug("LB rule " + lb.getId() + " state is set to Active"); + _lbDao.persist(lb); + } + + // remove LB-Vm mappings that were state to revoke + List lbVmMaps = _lb2VmMapDao.listByLoadBalancerId(lb.getId(), true); + List instanceIds = new ArrayList(); + + for (LoadBalancerVMMapVO lbVmMap : lbVmMaps) { + instanceIds.add(lbVmMap.getInstanceId()); + } + + if (!instanceIds.isEmpty()) { + _lb2VmMapDao.remove(lb.getId(), instanceIds, null); + s_logger.debug("Load balancer rule id " + lb.getId() + " is removed for vms " + instanceIds); + } + + if (_lb2VmMapDao.listByLoadBalancerId(lb.getId()).isEmpty()) { + lb.setState(FirewallRule.State.Add); + _lbDao.persist(lb); + s_logger.debug("LB rule " + lb.getId() + + " state is set to Add as there are no more active LB-VM mappings"); + } + + // remove LB-Stickiness policy mapping that were state to revoke + List stickinesspolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId( + lb.getId(), true); + if (!stickinesspolicies.isEmpty()) { + _lb2stickinesspoliciesDao.remove(lb.getId(), true); + s_logger.debug("Load balancer rule id " + lb.getId() + " is removed stickiness policies"); + } + + // remove LB-HealthCheck policy mapping that were state to + // revoke + List healthCheckpolicies = _lb2healthcheckDao.listByLoadBalancerId(lb.getId(), + true); + if (!healthCheckpolicies.isEmpty()) { + _lb2healthcheckDao.remove(lb.getId(), true); + s_logger.debug("Load balancer rule id " + lb.getId() + " is removed health check monitors policies"); + } + + return checkForReleaseElasticIp; + } + }); - for (LoadBalancerVMMapVO lbVmMap : lbVmMaps) { - instanceIds.add(lbVmMap.getInstanceId()); - } - - if (!instanceIds.isEmpty()) { - _lb2VmMapDao.remove(lb.getId(), instanceIds, null); - s_logger.debug("Load balancer rule id " + lb.getId() + " is removed for vms " + instanceIds); - } - - if (_lb2VmMapDao.listByLoadBalancerId(lb.getId()).isEmpty()) { - lb.setState(FirewallRule.State.Add); - _lbDao.persist(lb); - s_logger.debug("LB rule " + lb.getId() - + " state is set to Add as there are no more active LB-VM mappings"); - } - - // remove LB-Stickiness policy mapping that were state to revoke - List stickinesspolicies = _lb2stickinesspoliciesDao.listByLoadBalancerId( - lb.getId(), true); - if (!stickinesspolicies.isEmpty()) { - _lb2stickinesspoliciesDao.remove(lb.getId(), true); - s_logger.debug("Load balancer rule id " + lb.getId() + " is removed stickiness policies"); - } - - // remove LB-HealthCheck policy mapping that were state to - // revoke - List healthCheckpolicies = _lb2healthcheckDao.listByLoadBalancerId(lb.getId(), - true); - if (!healthCheckpolicies.isEmpty()) { - _lb2healthcheckDao.remove(lb.getId(), true); - s_logger.debug("Load balancer rule id " + lb.getId() + " is removed health check monitors policies"); - } - - txn.commit(); if (checkForReleaseElasticIp && lb.getSourceIpAddressId() != null) { boolean success = true; long count = _firewallDao.countRulesByIpId(lb.getSourceIpAddressId()); diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index 3dfcad51bf9..5b9d84e4d04 100755 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -230,6 +230,8 @@ import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.Ip; import com.cloud.utils.net.MacAddress; @@ -578,34 +580,30 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V @DB public void processStopOrRebootAnswer(final DomainRouterVO router, Answer answer) { - final Transaction txn = Transaction.currentTxn(); - try { - txn.start(); - //FIXME!!! - UserStats command should grab bytesSent/Received for all guest interfaces of the VR - List routerGuestNtwkIds = _routerDao.getRouterNetworks(router.getId()); - for (Long guestNtwkId : routerGuestNtwkIds) { - final UserStatisticsVO userStats = _userStatsDao.lock(router.getAccountId(), router.getDataCenterId(), - guestNtwkId, null, router.getId(), router.getType().toString()); - if (userStats != null) { - final long currentBytesRcvd = userStats.getCurrentBytesReceived(); - userStats.setCurrentBytesReceived(0); - userStats.setNetBytesReceived(userStats.getNetBytesReceived() + currentBytesRcvd); - - final long currentBytesSent = userStats.getCurrentBytesSent(); - userStats.setCurrentBytesSent(0); - userStats.setNetBytesSent(userStats.getNetBytesSent() + currentBytesSent); - _userStatsDao.update(userStats.getId(), userStats); - s_logger.debug("Successfully updated user statistics as a part of domR " + router + " reboot/stop"); - } else { - s_logger.warn("User stats were not created for account " + router.getAccountId() + " and dc " + router.getDataCenterId()); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + //FIXME!!! - UserStats command should grab bytesSent/Received for all guest interfaces of the VR + List routerGuestNtwkIds = _routerDao.getRouterNetworks(router.getId()); + for (Long guestNtwkId : routerGuestNtwkIds) { + final UserStatisticsVO userStats = _userStatsDao.lock(router.getAccountId(), router.getDataCenterId(), + guestNtwkId, null, router.getId(), router.getType().toString()); + if (userStats != null) { + final long currentBytesRcvd = userStats.getCurrentBytesReceived(); + userStats.setCurrentBytesReceived(0); + userStats.setNetBytesReceived(userStats.getNetBytesReceived() + currentBytesRcvd); + + final long currentBytesSent = userStats.getCurrentBytesSent(); + userStats.setCurrentBytesSent(0); + userStats.setNetBytesSent(userStats.getNetBytesSent() + currentBytesSent); + _userStatsDao.update(userStats.getId(), userStats); + s_logger.debug("Successfully updated user statistics as a part of domR " + router + " reboot/stop"); + } else { + s_logger.warn("User stats were not created for account " + router.getAccountId() + " and dc " + router.getDataCenterId()); + } + } } - } - - txn.commit(); - } catch (final Exception e) { - txn.rollback(); - throw new CloudRuntimeException("Problem updating stats after reboot/stop ", e); - } + }); } @Override @ActionEvent(eventType = EventTypes.EVENT_ROUTER_REBOOT, eventDescription = "rebooting router Vm", async = true) @@ -862,21 +860,21 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V final List routers = _routerDao.listByStateAndNetworkType(State.Running, GuestType.Isolated, mgmtSrvrId); s_logger.debug("Found " + routers.size() + " running routers. "); - for (DomainRouterVO router : routers) { + for (final DomainRouterVO router : routers) { String privateIP = router.getPrivateIpAddress(); if (privateIP != null) { - boolean forVpc = router.getVpcId() != null; + final boolean forVpc = router.getVpcId() != null; List routerNics = _nicDao.listByVmId(router.getId()); - for (Nic routerNic : routerNics) { - Network network = _networkModel.getNetwork(routerNic.getNetworkId()); + for (final Nic routerNic : routerNics) { + final Network network = _networkModel.getNetwork(routerNic.getNetworkId()); //Send network usage command for public nic in VPC VR //Send network usage command for isolated guest nic of non VPC VR if ((forVpc && network.getTrafficType() == TrafficType.Public) || (!forVpc && network.getTrafficType() == TrafficType.Guest && network.getGuestType() == Network.GuestType.Isolated)) { final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName(), forVpc, routerNic.getIp4Address()); - String routerType = router.getType().toString(); - UserStatisticsVO previousStats = _userStatsDao.findBy(router.getAccountId(), + final String routerType = router.getType().toString(); + final UserStatisticsVO previousStats = _userStatsDao.findBy(router.getAccountId(), router.getDataCenterId(), network.getId(), (forVpc ? routerNic.getIp4Address() : null), router.getId(), routerType); NetworkUsageAnswer answer = null; try { @@ -891,62 +889,63 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V s_logger.warn("Error while collecting network stats from router: " + router.getInstanceName() + " from host: " + router.getHostId() + "; details: " + answer.getDetails()); continue; } - Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { if ((answer.getBytesReceived() == 0) && (answer.getBytesSent() == 0)) { s_logger.debug("Recieved and Sent bytes are both 0. Not updating user_statistics"); continue; } - txn.start(); - UserStatisticsVO stats = _userStatsDao.lock(router.getAccountId(), - router.getDataCenterId(), network.getId(), (forVpc ? routerNic.getIp4Address() : null), router.getId(), routerType); - if (stats == null) { - s_logger.warn("unable to find stats for account: " + router.getAccountId()); - continue; - } - - if (previousStats != null - && ((previousStats.getCurrentBytesReceived() != stats.getCurrentBytesReceived()) - || (previousStats.getCurrentBytesSent() != stats.getCurrentBytesSent()))) { - s_logger.debug("Router stats changed from the time NetworkUsageCommand was sent. " + - "Ignoring current answer. Router: " + answer.getRouterName() + " Rcvd: " + - answer.getBytesReceived() + "Sent: " + answer.getBytesSent()); - continue; - } - - if (stats.getCurrentBytesReceived() > answer.getBytesReceived()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Received # of bytes that's less than the last one. " + - "Assuming something went wrong and persisting it. Router: " + - answer.getRouterName() + " Reported: " + answer.getBytesReceived() - + " Stored: " + stats.getCurrentBytesReceived()); + final NetworkUsageAnswer answerFinal = answer; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + UserStatisticsVO stats = _userStatsDao.lock(router.getAccountId(), + router.getDataCenterId(), network.getId(), (forVpc ? routerNic.getIp4Address() : null), router.getId(), routerType); + if (stats == null) { + s_logger.warn("unable to find stats for account: " + router.getAccountId()); + return; + } + + if (previousStats != null + && ((previousStats.getCurrentBytesReceived() != stats.getCurrentBytesReceived()) + || (previousStats.getCurrentBytesSent() != stats.getCurrentBytesSent()))) { + s_logger.debug("Router stats changed from the time NetworkUsageCommand was sent. " + + "Ignoring current answer. Router: " + answerFinal.getRouterName() + " Rcvd: " + + answerFinal.getBytesReceived() + "Sent: " + answerFinal.getBytesSent()); + return; + } + + if (stats.getCurrentBytesReceived() > answerFinal.getBytesReceived()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Received # of bytes that's less than the last one. " + + "Assuming something went wrong and persisting it. Router: " + + answerFinal.getRouterName() + " Reported: " + answerFinal.getBytesReceived() + + " Stored: " + stats.getCurrentBytesReceived()); + } + stats.setNetBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); + } + stats.setCurrentBytesReceived(answerFinal.getBytesReceived()); + if (stats.getCurrentBytesSent() > answerFinal.getBytesSent()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Received # of bytes that's less than the last one. " + + "Assuming something went wrong and persisting it. Router: " + + answerFinal.getRouterName() + " Reported: " + answerFinal.getBytesSent() + + " Stored: " + stats.getCurrentBytesSent()); + } + stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); + } + stats.setCurrentBytesSent(answerFinal.getBytesSent()); + if (! _dailyOrHourly) { + //update agg bytes + stats.setAggBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); + stats.setAggBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); + } + _userStatsDao.update(stats.getId(), stats); } - stats.setNetBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); - } - stats.setCurrentBytesReceived(answer.getBytesReceived()); - if (stats.getCurrentBytesSent() > answer.getBytesSent()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Received # of bytes that's less than the last one. " + - "Assuming something went wrong and persisting it. Router: " + - answer.getRouterName() + " Reported: " + answer.getBytesSent() - + " Stored: " + stats.getCurrentBytesSent()); - } - stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); - } - stats.setCurrentBytesSent(answer.getBytesSent()); - if (! _dailyOrHourly) { - //update agg bytes - stats.setAggBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); - stats.setAggBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); - } - _userStatsDao.update(stats.getId(), stats); - txn.commit(); + }); + } catch (Exception e) { - txn.rollback(); s_logger.warn("Unable to update user statistics for account: " + router.getAccountId() + " Rx: " + answer.getBytesReceived() + "; Tx: " + answer.getBytesSent()); - } finally { - txn.close(); } } } @@ -977,30 +976,30 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V scanLock.unlock(); return; } - Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { - txn.start(); - //get all stats with delta > 0 - List updatedStats = _userStatsDao.listUpdatedStats(); - Date updatedTime = new Date(); - for(UserStatisticsVO stat : updatedStats){ - //update agg bytes - stat.setAggBytesReceived(stat.getCurrentBytesReceived() + stat.getNetBytesReceived()); - stat.setAggBytesSent(stat.getCurrentBytesSent() + stat.getNetBytesSent()); - _userStatsDao.update(stat.getId(), stat); - //insert into op_user_stats_log - UserStatsLogVO statsLog = new UserStatsLogVO(stat.getId(), stat.getNetBytesReceived(), stat.getNetBytesSent(), stat.getCurrentBytesReceived(), - stat.getCurrentBytesSent(), stat.getAggBytesReceived(), stat.getAggBytesSent(), updatedTime); - _userStatsLogDao.persist(statsLog); - } - s_logger.debug("Successfully updated aggregate network stats"); - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + //get all stats with delta > 0 + List updatedStats = _userStatsDao.listUpdatedStats(); + Date updatedTime = new Date(); + for(UserStatisticsVO stat : updatedStats){ + //update agg bytes + stat.setAggBytesReceived(stat.getCurrentBytesReceived() + stat.getNetBytesReceived()); + stat.setAggBytesSent(stat.getCurrentBytesSent() + stat.getNetBytesSent()); + _userStatsDao.update(stat.getId(), stat); + //insert into op_user_stats_log + UserStatsLogVO statsLog = new UserStatsLogVO(stat.getId(), stat.getNetBytesReceived(), stat.getNetBytesSent(), stat.getCurrentBytesReceived(), + stat.getCurrentBytesSent(), stat.getAggBytesReceived(), stat.getAggBytesSent(), updatedTime); + _userStatsLogDao.persist(statsLog); + } + s_logger.debug("Successfully updated aggregate network stats"); + } + }); } catch (Exception e){ - txn.rollback(); s_logger.debug("Failed to update aggregate network stats", e); } finally { scanLock.unlock(); - txn.close(); } } } catch (Exception e){ @@ -1137,17 +1136,7 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V } } if (updated) { - Transaction txn = Transaction.open(Transaction.CLOUD_DB); - try { - txn.start(); - _routerDao.update(router.getId(), router); - txn.commit(); - } catch (Exception e) { - txn.rollback(); - s_logger.warn("Unable to update router status for account: " + router.getAccountId()); - } finally { - txn.close(); - } + _routerDao.update(router.getId(), router); } RedundantState currState = router.getRedundantState(); if (prevState != currState) { @@ -2859,12 +2848,15 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V configDnsMasq(router, network, cmds); boolean result = sendCommandsToRouter(router, cmds); if (result == false) { - NicIpAliasVO ipAliasVO = _nicIpAliasDao.findByInstanceIdAndNetworkId(network.getId(), router.getId()); - Transaction txn = Transaction.currentTxn(); - txn.start(); - _nicIpAliasDao.expunge(ipAliasVO.getId()); - _ipAddressDao.unassignIpAddress(routerPublicIP.getId()); - txn.commit(); + final NicIpAliasVO ipAliasVO = _nicIpAliasDao.findByInstanceIdAndNetworkId(network.getId(), router.getId()); + final PublicIp routerPublicIPFinal = routerPublicIP; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _nicIpAliasDao.expunge(ipAliasVO.getId()); + _ipAddressDao.unassignIpAddress(routerPublicIPFinal.getId()); + } + }); throw new CloudRuntimeException("failed to configure ip alias on the router as a part of dhcp config"); } } @@ -2889,7 +2881,7 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V } Commands cmds = new Commands(Command.OnError.Continue); - List revokedIpAliasVOs = _nicIpAliasDao.listByNetworkIdAndState(network.getId(), NicIpAlias.state.revoked); + final List revokedIpAliasVOs = _nicIpAliasDao.listByNetworkIdAndState(network.getId(), NicIpAlias.state.revoked); s_logger.debug("Found" + revokedIpAliasVOs.size() + "ip Aliases to revoke on the router as a part of dhcp configuration"); List revokedIpAliasTOs = new ArrayList(); for (NicIpAliasVO revokedAliasVO : revokedIpAliasVOs) { @@ -2905,12 +2897,14 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V configDnsMasq(router, network, cmds); boolean result = sendCommandsToRouter(router, cmds); if (result) { - Transaction txn= Transaction.currentTxn(); - txn.start(); - for (NicIpAliasVO revokedAliasVO : revokedIpAliasVOs) { - _nicIpAliasDao.expunge(revokedAliasVO.getId()); - } - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (NicIpAliasVO revokedAliasVO : revokedIpAliasVOs) { + _nicIpAliasDao.expunge(revokedAliasVO.getId()); + } + } + }); return true; } } @@ -3967,7 +3961,7 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V public void prepareStop(VirtualMachineProfile profile){ //Collect network usage before stopping Vm - DomainRouterVO router = _routerDao.findById(profile.getVirtualMachine().getId()); + final DomainRouterVO router = _routerDao.findById(profile.getVirtualMachine().getId()); if(router == null){ return; } @@ -3975,17 +3969,17 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V String privateIP = router.getPrivateIpAddress(); if (privateIP != null) { - boolean forVpc = router.getVpcId() != null; + final boolean forVpc = router.getVpcId() != null; List routerNics = _nicDao.listByVmId(router.getId()); - for (Nic routerNic : routerNics) { - Network network = _networkModel.getNetwork(routerNic.getNetworkId()); + for (final Nic routerNic : routerNics) { + final Network network = _networkModel.getNetwork(routerNic.getNetworkId()); //Send network usage command for public nic in VPC VR //Send network usage command for isolated guest nic of non VPC VR if ((forVpc && network.getTrafficType() == TrafficType.Public) || (!forVpc && network.getTrafficType() == TrafficType.Guest && network.getGuestType() == Network.GuestType.Isolated)) { final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName(), forVpc, routerNic.getIp4Address()); - String routerType = router.getType().toString(); - UserStatisticsVO previousStats = _userStatsDao.findBy(router.getAccountId(), + final String routerType = router.getType().toString(); + final UserStatisticsVO previousStats = _userStatsDao.findBy(router.getAccountId(), router.getDataCenterId(), network.getId(), (forVpc ? routerNic.getIp4Address() : null), router.getId(), routerType); NetworkUsageAnswer answer = null; try { @@ -4000,62 +3994,63 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V s_logger.warn("Error while collecting network stats from router: " + router.getInstanceName() + " from host: " + router.getHostId() + "; details: " + answer.getDetails()); continue; } - Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { if ((answer.getBytesReceived() == 0) && (answer.getBytesSent() == 0)) { s_logger.debug("Recieved and Sent bytes are both 0. Not updating user_statistics"); continue; } - txn.start(); - UserStatisticsVO stats = _userStatsDao.lock(router.getAccountId(), - router.getDataCenterId(), network.getId(), (forVpc ? routerNic.getIp4Address() : null), router.getId(), routerType); - if (stats == null) { - s_logger.warn("unable to find stats for account: " + router.getAccountId()); - continue; - } - - if (previousStats != null - && ((previousStats.getCurrentBytesReceived() != stats.getCurrentBytesReceived()) - || (previousStats.getCurrentBytesSent() != stats.getCurrentBytesSent()))){ - s_logger.debug("Router stats changed from the time NetworkUsageCommand was sent. " + - "Ignoring current answer. Router: " + answer.getRouterName() + " Rcvd: " + - answer.getBytesReceived() + "Sent: " + answer.getBytesSent()); - continue; - } - - if (stats.getCurrentBytesReceived() > answer.getBytesReceived()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Received # of bytes that's less than the last one. " + - "Assuming something went wrong and persisting it. Router: " + - answer.getRouterName() + " Reported: " + answer.getBytesReceived() - + " Stored: " + stats.getCurrentBytesReceived()); + + final NetworkUsageAnswer answerFinal = answer; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + UserStatisticsVO stats = _userStatsDao.lock(router.getAccountId(), + router.getDataCenterId(), network.getId(), (forVpc ? routerNic.getIp4Address() : null), router.getId(), routerType); + if (stats == null) { + s_logger.warn("unable to find stats for account: " + router.getAccountId()); + return; + } + + if (previousStats != null + && ((previousStats.getCurrentBytesReceived() != stats.getCurrentBytesReceived()) + || (previousStats.getCurrentBytesSent() != stats.getCurrentBytesSent()))){ + s_logger.debug("Router stats changed from the time NetworkUsageCommand was sent. " + + "Ignoring current answer. Router: " + answerFinal.getRouterName() + " Rcvd: " + + answerFinal.getBytesReceived() + "Sent: " + answerFinal.getBytesSent()); + return; + } + + if (stats.getCurrentBytesReceived() > answerFinal.getBytesReceived()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Received # of bytes that's less than the last one. " + + "Assuming something went wrong and persisting it. Router: " + + answerFinal.getRouterName() + " Reported: " + answerFinal.getBytesReceived() + + " Stored: " + stats.getCurrentBytesReceived()); + } + stats.setNetBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); + } + stats.setCurrentBytesReceived(answerFinal.getBytesReceived()); + if (stats.getCurrentBytesSent() > answerFinal.getBytesSent()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Received # of bytes that's less than the last one. " + + "Assuming something went wrong and persisting it. Router: " + + answerFinal.getRouterName() + " Reported: " + answerFinal.getBytesSent() + + " Stored: " + stats.getCurrentBytesSent()); + } + stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); + } + stats.setCurrentBytesSent(answerFinal.getBytesSent()); + if (! _dailyOrHourly) { + //update agg bytes + stats.setAggBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); + stats.setAggBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); + } + _userStatsDao.update(stats.getId(), stats); } - stats.setNetBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); - } - stats.setCurrentBytesReceived(answer.getBytesReceived()); - if (stats.getCurrentBytesSent() > answer.getBytesSent()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Received # of bytes that's less than the last one. " + - "Assuming something went wrong and persisting it. Router: " + - answer.getRouterName() + " Reported: " + answer.getBytesSent() - + " Stored: " + stats.getCurrentBytesSent()); - } - stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); - } - stats.setCurrentBytesSent(answer.getBytesSent()); - if (! _dailyOrHourly) { - //update agg bytes - stats.setAggBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); - stats.setAggBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); - } - _userStatsDao.update(stats.getId(), stats); - txn.commit(); + }); } catch (Exception e) { - txn.rollback(); s_logger.warn("Unable to update user statistics for account: " + router.getAccountId() + " Rx: " + answer.getBytesReceived() + "; Tx: " + answer.getBytesSent()); - } finally { - txn.close(); } } } diff --git a/server/src/com/cloud/network/rules/RulesManagerImpl.java b/server/src/com/cloud/network/rules/RulesManagerImpl.java index 6e326b0f652..d98dd299c7b 100755 --- a/server/src/com/cloud/network/rules/RulesManagerImpl.java +++ b/server/src/com/cloud/network/rules/RulesManagerImpl.java @@ -26,7 +26,6 @@ import javax.ejb.Local; import javax.inject.Inject; import org.apache.log4j.Logger; - import org.apache.cloudstack.api.command.user.firewall.ListPortForwardingRulesCmd; import org.apache.cloudstack.context.CallContext; import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; @@ -76,8 +75,12 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithException; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.Ip; import com.cloud.utils.net.NetUtils; @@ -197,12 +200,12 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules @Override @DB @ActionEvent(eventType = EventTypes.EVENT_NET_RULE_ADD, eventDescription = "creating forwarding rule", create = true) - public PortForwardingRule createPortForwardingRule(PortForwardingRule rule, Long vmId, Ip vmIp, boolean openFirewall) + public PortForwardingRule createPortForwardingRule(final PortForwardingRule rule, final Long vmId, Ip vmIp, final boolean openFirewall) throws NetworkRuleConflictException { CallContext ctx = CallContext.current(); - Account caller = ctx.getCallingAccount(); + final Account caller = ctx.getCallingAccount(); - Long ipAddrId = rule.getSourceIpAddressId(); + final Long ipAddrId = rule.getSourceIpAddressId(); IPAddressVO ipAddress = _ipAddressDao.findById(ipAddrId); @@ -213,7 +216,7 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules throw new InvalidParameterValueException("Unable to create port forwarding rule; ip id=" + ipAddrId + " has static nat enabled"); } - Long networkId = rule.getNetworkId(); + final Long networkId = rule.getNetworkId(); Network network = _networkModel.getNetwork(networkId); //associate ip address to network (if needed) boolean performedIpAssoc = false; @@ -245,8 +248,8 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules _firewallMgr.validateFirewallRule(caller, ipAddress, rule.getSourcePortStart(), rule.getSourcePortEnd(), rule.getProtocol(), Purpose.PortForwarding, FirewallRuleType.User, networkId, rule.getTrafficType()); - Long accountId = ipAddress.getAllocatedToAccountId(); - Long domainId = ipAddress.getAllocatedInDomainId(); + final Long accountId = ipAddress.getAllocatedToAccountId(); + final Long domainId = ipAddress.getAllocatedInDomainId(); // start port can't be bigger than end port if (rule.getDestinationPortStart() > rule.getDestinationPortEnd()) { @@ -308,46 +311,48 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules } } - Transaction txn = Transaction.currentTxn(); - txn.start(); - - PortForwardingRuleVO newRule = new PortForwardingRuleVO(rule.getXid(), rule.getSourceIpAddressId(), - rule.getSourcePortStart(), rule.getSourcePortEnd(), dstIp, rule.getDestinationPortStart(), - rule.getDestinationPortEnd(), rule.getProtocol().toLowerCase(), networkId, accountId, domainId, vmId); - newRule = _portForwardingDao.persist(newRule); - - // create firewallRule for 0.0.0.0/0 cidr - if (openFirewall) { - _firewallMgr.createRuleForAllCidrs(ipAddrId, caller, rule.getSourcePortStart(), rule.getSourcePortEnd(), - rule.getProtocol(), null, null, newRule.getId(), networkId); - } - - try { - _firewallMgr.detectRulesConflict(newRule); - if (!_firewallDao.setStateToAdd(newRule)) { - throw new CloudRuntimeException("Unable to update the state to add for " + newRule); - } - CallContext.current().setEventDetails("Rule Id: " + newRule.getId()); - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_RULE_ADD, newRule.getAccountId(), - ipAddress.getDataCenterId(), newRule.getId(), null, PortForwardingRule.class.getName(), - newRule.getUuid()); - txn.commit(); - return newRule; - } catch (Exception e) { - if (newRule != null) { - txn.start(); - // no need to apply the rule as it wasn't programmed on the backend yet - _firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false); - removePFRule(newRule); - txn.commit(); + final Ip dstIpFinal = dstIp; + final IPAddressVO ipAddressFinal = ipAddress; + return Transaction.execute(new TransactionCallbackWithException() { + @Override + public PortForwardingRuleVO doInTransaction(TransactionStatus status) throws NetworkRuleConflictException { + PortForwardingRuleVO newRule = new PortForwardingRuleVO(rule.getXid(), rule.getSourceIpAddressId(), + rule.getSourcePortStart(), rule.getSourcePortEnd(), dstIpFinal, rule.getDestinationPortStart(), + rule.getDestinationPortEnd(), rule.getProtocol().toLowerCase(), networkId, accountId, domainId, vmId); + newRule = _portForwardingDao.persist(newRule); + + // create firewallRule for 0.0.0.0/0 cidr + if (openFirewall) { + _firewallMgr.createRuleForAllCidrs(ipAddrId, caller, rule.getSourcePortStart(), rule.getSourcePortEnd(), + rule.getProtocol(), null, null, newRule.getId(), networkId); + } + + try { + _firewallMgr.detectRulesConflict(newRule); + if (!_firewallDao.setStateToAdd(newRule)) { + throw new CloudRuntimeException("Unable to update the state to add for " + newRule); + } + CallContext.current().setEventDetails("Rule Id: " + newRule.getId()); + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_RULE_ADD, newRule.getAccountId(), + ipAddressFinal.getDataCenterId(), newRule.getId(), null, PortForwardingRule.class.getName(), + newRule.getUuid()); + return newRule; + } catch (Exception e) { + if (newRule != null) { + // no need to apply the rule as it wasn't programmed on the backend yet + _firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false); + removePFRule(newRule); + } + + if (e instanceof NetworkRuleConflictException) { + throw (NetworkRuleConflictException) e; + } + + throw new CloudRuntimeException("Unable to add rule for the ip id=" + ipAddrId, e); + } } + }); - if (e instanceof NetworkRuleConflictException) { - throw (NetworkRuleConflictException) e; - } - - throw new CloudRuntimeException("Unable to add rule for the ip id=" + ipAddrId, e); - } } finally { // release ip address if ipassoc was perfored if (performedIpAssoc) { @@ -361,10 +366,10 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules @Override @DB @ActionEvent(eventType = EventTypes.EVENT_NET_RULE_ADD, eventDescription = "creating static nat rule", create = true) - public StaticNatRule createStaticNatRule(StaticNatRule rule, boolean openFirewall) throws NetworkRuleConflictException { - Account caller = CallContext.current().getCallingAccount(); + public StaticNatRule createStaticNatRule(final StaticNatRule rule, final boolean openFirewall) throws NetworkRuleConflictException { + final Account caller = CallContext.current().getCallingAccount(); - Long ipAddrId = rule.getSourceIpAddressId(); + final Long ipAddrId = rule.getSourceIpAddressId(); IPAddressVO ipAddress = _ipAddressDao.findById(ipAddrId); @@ -377,9 +382,9 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules _firewallMgr.validateFirewallRule(caller, ipAddress, rule.getSourcePortStart(), rule.getSourcePortEnd(), rule.getProtocol(), Purpose.StaticNat, FirewallRuleType.User,null, rule.getTrafficType() ); - Long networkId = ipAddress.getAssociatedWithNetworkId(); - Long accountId = ipAddress.getAllocatedToAccountId(); - Long domainId = ipAddress.getAllocatedInDomainId(); + final Long networkId = ipAddress.getAssociatedWithNetworkId(); + final Long accountId = ipAddress.getAllocatedToAccountId(); + final Long domainId = ipAddress.getAllocatedInDomainId(); _networkModel.checkIpForService(ipAddress, Service.StaticNat, null); @@ -390,48 +395,48 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules } //String dstIp = _networkModel.getIpInNetwork(ipAddress.getAssociatedWithVmId(), networkId); - String dstIp = ipAddress.getVmIp(); - Transaction txn = Transaction.currentTxn(); - txn.start(); + final String dstIp = ipAddress.getVmIp(); + return Transaction.execute(new TransactionCallbackWithException() { + @Override + public StaticNatRule doInTransaction(TransactionStatus status) throws NetworkRuleConflictException { - FirewallRuleVO newRule = new FirewallRuleVO(rule.getXid(), rule.getSourceIpAddressId(), rule.getSourcePortStart(), rule.getSourcePortEnd(), rule.getProtocol().toLowerCase(), - networkId, accountId, domainId, rule.getPurpose(), null, null, null, null, null); + FirewallRuleVO newRule = new FirewallRuleVO(rule.getXid(), rule.getSourceIpAddressId(), rule.getSourcePortStart(), rule.getSourcePortEnd(), rule.getProtocol().toLowerCase(), + networkId, accountId, domainId, rule.getPurpose(), null, null, null, null, null); - newRule = _firewallDao.persist(newRule); + newRule = _firewallDao.persist(newRule); - // create firewallRule for 0.0.0.0/0 cidr - if (openFirewall) { - _firewallMgr.createRuleForAllCidrs(ipAddrId, caller, rule.getSourcePortStart(), rule.getSourcePortEnd(), rule.getProtocol(), null, null, newRule.getId(), networkId); - } + // create firewallRule for 0.0.0.0/0 cidr + if (openFirewall) { + _firewallMgr.createRuleForAllCidrs(ipAddrId, caller, rule.getSourcePortStart(), rule.getSourcePortEnd(), rule.getProtocol(), null, null, newRule.getId(), networkId); + } - try { - _firewallMgr.detectRulesConflict(newRule); - if (!_firewallDao.setStateToAdd(newRule)) { - throw new CloudRuntimeException("Unable to update the state to add for " + newRule); + try { + _firewallMgr.detectRulesConflict(newRule); + if (!_firewallDao.setStateToAdd(newRule)) { + throw new CloudRuntimeException("Unable to update the state to add for " + newRule); + } + CallContext.current().setEventDetails("Rule Id: " + newRule.getId()); + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_RULE_ADD, newRule.getAccountId(), 0, newRule.getId(), + null, FirewallRule.class.getName(), newRule.getUuid()); + + StaticNatRule staticNatRule = new StaticNatRuleImpl(newRule, dstIp); + + return staticNatRule; + } catch (Exception e) { + if (newRule != null) { + // no need to apply the rule as it wasn't programmed on the backend yet + _firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false); + _firewallMgr.removeRule(newRule); + } + + if (e instanceof NetworkRuleConflictException) { + throw (NetworkRuleConflictException) e; + } + throw new CloudRuntimeException("Unable to add static nat rule for the ip id=" + newRule.getSourceIpAddressId(), e); + } } - CallContext.current().setEventDetails("Rule Id: " + newRule.getId()); - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_RULE_ADD, newRule.getAccountId(), 0, newRule.getId(), - null, FirewallRule.class.getName(), newRule.getUuid()); + }); - txn.commit(); - StaticNatRule staticNatRule = new StaticNatRuleImpl(newRule, dstIp); - - return staticNatRule; - } catch (Exception e) { - - if (newRule != null) { - txn.start(); - // no need to apply the rule as it wasn't programmed on the backend yet - _firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false); - _firewallMgr.removeRule(newRule); - txn.commit(); - } - - if (e instanceof NetworkRuleConflictException) { - throw (NetworkRuleConflictException) e; - } - throw new CloudRuntimeException("Unable to add static nat rule for the ip id=" + newRule.getSourceIpAddressId(), e); - } } @Override @@ -1146,23 +1151,25 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules @Override @DB - public FirewallRuleVO[] reservePorts(IpAddress ip, String protocol, FirewallRule.Purpose purpose, - boolean openFirewall, Account caller, int... ports) throws NetworkRuleConflictException { - FirewallRuleVO[] rules = new FirewallRuleVO[ports.length]; + public FirewallRuleVO[] reservePorts(final IpAddress ip, final String protocol, final FirewallRule.Purpose purpose, + final boolean openFirewall, final Account caller, final int... ports) throws NetworkRuleConflictException { + final FirewallRuleVO[] rules = new FirewallRuleVO[ports.length]; - Transaction txn = Transaction.currentTxn(); - txn.start(); - for (int i = 0; i < ports.length; i++) { - - rules[i] = new FirewallRuleVO(null, ip.getId(), ports[i], protocol, ip.getAssociatedWithNetworkId(), ip.getAllocatedToAccountId(), ip.getAllocatedInDomainId(), purpose, null, null, null, null); - rules[i] = _firewallDao.persist(rules[i]); - - if (openFirewall) { - _firewallMgr.createRuleForAllCidrs(ip.getId(), caller, ports[i], ports[i], protocol, null, null, - rules[i].getId(), ip.getAssociatedWithNetworkId()); + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws NetworkRuleConflictException { + for (int i = 0; i < ports.length; i++) { + + rules[i] = new FirewallRuleVO(null, ip.getId(), ports[i], protocol, ip.getAssociatedWithNetworkId(), ip.getAllocatedToAccountId(), ip.getAllocatedInDomainId(), purpose, null, null, null, null); + rules[i] = _firewallDao.persist(rules[i]); + + if (openFirewall) { + _firewallMgr.createRuleForAllCidrs(ip.getId(), caller, ports[i], ports[i], protocol, null, null, + rules[i].getId(), ip.getAssociatedWithNetworkId()); + } + } } - } - txn.commit(); + }); boolean success = false; try { @@ -1173,12 +1180,14 @@ public class RulesManagerImpl extends ManagerBase implements RulesManager, Rules return rules; } finally { if (!success) { - txn.start(); - - for (FirewallRuleVO newRule : rules) { - _firewallMgr.removeRule(newRule); - } - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (FirewallRuleVO newRule : rules) { + _firewallMgr.removeRule(newRule); + } + } + }); } } } diff --git a/server/src/com/cloud/network/security/SecurityGroupManagerImpl.java b/server/src/com/cloud/network/security/SecurityGroupManagerImpl.java index 8b2db9dde90..c7b6e1eb3f7 100755 --- a/server/src/com/cloud/network/security/SecurityGroupManagerImpl.java +++ b/server/src/com/cloud/network/security/SecurityGroupManagerImpl.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.ConcurrentModificationException; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -34,6 +35,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import javax.ejb.ConcurrentAccessException; import javax.ejb.Local; import javax.inject.Inject; import javax.naming.ConfigurationException; @@ -96,6 +98,10 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.Filter; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.fsm.StateListener; import com.cloud.utils.net.NetUtils; @@ -191,12 +197,7 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro @Override protected void runInContext() { try { - Transaction txn = Transaction.open("SG Work"); - try { - work(); - } finally { - txn.close("SG Work"); - } + work(); } catch (Throwable th) { try { s_logger.error("Problem with SG work", th); @@ -204,24 +205,15 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro } } } - - WorkerThread() { - - } } public class CleanupThread extends ManagedContextRunnable { @Override protected void runInContext() { try { - Transaction txn = Transaction.open("SG Cleanup"); - try { - cleanupFinishedWork(); - cleanupUnfinishedWork(); - //processScheduledWork(); - } finally { - txn.close("SG Cleanup"); - } + cleanupFinishedWork(); + cleanupUnfinishedWork(); + //processScheduledWork(); } catch (Throwable th) { try { s_logger.error("Problem with SG Cleanup", th); @@ -229,10 +221,6 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro } } } - - CleanupThread() { - - } } public static class PortAndProto implements Comparable { @@ -400,7 +388,7 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro } @DB - public void scheduleRulesetUpdateToHosts(List affectedVms, boolean updateSeqno, Long delayMs) { + public void scheduleRulesetUpdateToHosts(final List affectedVms, final boolean updateSeqno, Long delayMs) { if (affectedVms.size() == 0) { return; } @@ -422,39 +410,43 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro if (s_logger.isTraceEnabled()) { s_logger.trace("Security Group Mgr: acquired global work lock"); } - Transaction txn = Transaction.currentTxn(); + try { - txn.start(); - for (Long vmId : affectedVms) { - if (s_logger.isTraceEnabled()) { - s_logger.trace("Security Group Mgr: scheduling ruleset update for " + vmId); - } - VmRulesetLogVO log = null; - SecurityGroupWorkVO work = null; - - log = _rulesetLogDao.findByVmId(vmId); - if (log == null) { - log = new VmRulesetLogVO(vmId); - log = _rulesetLogDao.persist(log); - } - - if (log != null && updateSeqno) { - log.incrLogsequence(); - _rulesetLogDao.update(log.getId(), log); - } - work = _workDao.findByVmIdStep(vmId, Step.Scheduled); - if (work == null) { - work = new SecurityGroupWorkVO(vmId, null, null, SecurityGroupWork.Step.Scheduled, null); - work = _workDao.persist(work); - if (s_logger.isTraceEnabled()) { - s_logger.trace("Security Group Mgr: created new work item for " + vmId + "; id = " + work.getId()); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (Long vmId : affectedVms) { + if (s_logger.isTraceEnabled()) { + s_logger.trace("Security Group Mgr: scheduling ruleset update for " + vmId); + } + VmRulesetLogVO log = null; + SecurityGroupWorkVO work = null; + + log = _rulesetLogDao.findByVmId(vmId); + if (log == null) { + log = new VmRulesetLogVO(vmId); + log = _rulesetLogDao.persist(log); + } + + if (log != null && updateSeqno) { + log.incrLogsequence(); + _rulesetLogDao.update(log.getId(), log); + } + work = _workDao.findByVmIdStep(vmId, Step.Scheduled); + if (work == null) { + work = new SecurityGroupWorkVO(vmId, null, null, SecurityGroupWork.Step.Scheduled, null); + work = _workDao.persist(work); + if (s_logger.isTraceEnabled()) { + s_logger.trace("Security Group Mgr: created new work item for " + vmId + "; id = " + work.getId()); + } + } + + work.setLogsequenceNumber(log.getLogsequence()); + _workDao.update(work.getId(), work); } } + }); - work.setLogsequenceNumber(log.getLogsequence()); - _workDao.update(work.getId(), work); - } - txn.commit(); for (Long vmId : affectedVms) { _executorPool.schedule(new WorkerThread(), delayMs, TimeUnit.MILLISECONDS); } @@ -595,7 +587,7 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro return authorizeSecurityGroupRule(securityGroupId,protocol,startPort,endPort,icmpType,icmpCode,cidrList,groupList,SecurityRuleType.IngressRule); } - private List authorizeSecurityGroupRule(Long securityGroupId,String protocol,Integer startPort,Integer endPort,Integer icmpType,Integer icmpCode,List cidrList,Map groupList,SecurityRuleType ruleType) { + private List authorizeSecurityGroupRule(final Long securityGroupId, String protocol,Integer startPort,Integer endPort,Integer icmpType,Integer icmpCode,final List cidrList,Map groupList, final SecurityRuleType ruleType) { Integer startPortOrType = null; Integer endPortOrCode = null; @@ -713,66 +705,71 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro } } - final Transaction txn = Transaction.currentTxn(); final Set authorizedGroups2 = new TreeSet(new SecurityGroupVOComparator()); authorizedGroups2.addAll(authorizedGroups); // Ensure we don't re-lock the same row - txn.start(); - // Prevents other threads/management servers from creating duplicate security rules - securityGroup = _securityGroupDao.acquireInLockTable(securityGroupId); - if (securityGroup == null) { - s_logger.warn("Could not acquire lock on network security group: id= " + securityGroupId); - return null; - } - List newRules = new ArrayList(); - try { - for (final SecurityGroupVO ngVO : authorizedGroups2) { - final Long ngId = ngVO.getId(); - // Don't delete the referenced group from under us - if (ngVO.getId() != securityGroup.getId()) { - final SecurityGroupVO tmpGrp = _securityGroupDao.lockRow(ngId, false); - if (tmpGrp == null) { - s_logger.warn("Failed to acquire lock on security group: " + ngId); - txn.rollback(); - return null; + final Integer startPortOrTypeFinal = startPortOrType; + final Integer endPortOrCodeFinal = endPortOrCode; + final String protocolFinal = protocol; + return Transaction.execute(new TransactionCallback>() { + @Override + public List doInTransaction(TransactionStatus status) { + // Prevents other threads/management servers from creating duplicate security rules + SecurityGroup securityGroup = _securityGroupDao.acquireInLockTable(securityGroupId); + if (securityGroup == null) { + s_logger.warn("Could not acquire lock on network security group: id= " + securityGroupId); + return null; + } + List newRules = new ArrayList(); + try { + for (final SecurityGroupVO ngVO : authorizedGroups2) { + final Long ngId = ngVO.getId(); + // Don't delete the referenced group from under us + if (ngVO.getId() != securityGroup.getId()) { + final SecurityGroupVO tmpGrp = _securityGroupDao.lockRow(ngId, false); + if (tmpGrp == null) { + s_logger.warn("Failed to acquire lock on security group: " + ngId); + throw new ConcurrentAccessException("Failed to acquire lock on security group: " + ngId); + } + } + SecurityGroupRuleVO securityGroupRule = _securityGroupRuleDao.findByProtoPortsAndAllowedGroupId(securityGroup.getId(), protocolFinal, startPortOrTypeFinal, endPortOrCodeFinal, ngVO.getId()); + if ((securityGroupRule != null) && (securityGroupRule.getRuleType() == ruleType)) { + continue; // rule already exists. + } + securityGroupRule = new SecurityGroupRuleVO(ruleType, securityGroup.getId(), startPortOrTypeFinal, endPortOrCodeFinal, protocolFinal, ngVO.getId()); + securityGroupRule = _securityGroupRuleDao.persist(securityGroupRule); + newRules.add(securityGroupRule); + } + if (cidrList != null) { + for (String cidr : cidrList) { + SecurityGroupRuleVO securityGroupRule = _securityGroupRuleDao.findByProtoPortsAndCidr(securityGroup.getId(), protocolFinal, startPortOrTypeFinal, endPortOrCodeFinal, cidr); + if ((securityGroupRule != null) && (securityGroupRule.getRuleType() == ruleType)) { + continue; + } + securityGroupRule = new SecurityGroupRuleVO(ruleType, securityGroup.getId(), startPortOrTypeFinal, endPortOrCodeFinal, protocolFinal, cidr); + securityGroupRule = _securityGroupRuleDao.persist(securityGroupRule); + newRules.add(securityGroupRule); + } + } + if (s_logger.isDebugEnabled()) { + s_logger.debug("Added " + newRules.size() + " rules to security group " + securityGroup.getName()); + } + final ArrayList affectedVms = new ArrayList(); + affectedVms.addAll(_securityGroupVMMapDao.listVmIdsBySecurityGroup(securityGroup.getId())); + scheduleRulesetUpdateToHosts(affectedVms, true, null); + return newRules; + } catch (Exception e) { + s_logger.warn("Exception caught when adding security group rules ", e); + throw new CloudRuntimeException("Exception caught when adding security group rules", e); + } finally { + if (securityGroup != null) { + _securityGroupDao.releaseFromLockTable(securityGroup.getId()); } } - SecurityGroupRuleVO securityGroupRule = _securityGroupRuleDao.findByProtoPortsAndAllowedGroupId(securityGroup.getId(), protocol, startPortOrType, endPortOrCode, ngVO.getId()); - if ((securityGroupRule != null) && (securityGroupRule.getRuleType() == ruleType)) { - continue; // rule already exists. - } - securityGroupRule = new SecurityGroupRuleVO(ruleType, securityGroup.getId(), startPortOrType, endPortOrCode, protocol, ngVO.getId()); - securityGroupRule = _securityGroupRuleDao.persist(securityGroupRule); - newRules.add(securityGroupRule); } - if (cidrList != null) { - for (String cidr : cidrList) { - SecurityGroupRuleVO securityGroupRule = _securityGroupRuleDao.findByProtoPortsAndCidr(securityGroup.getId(), protocol, startPortOrType, endPortOrCode, cidr); - if ((securityGroupRule != null) && (securityGroupRule.getRuleType() == ruleType)) { - continue; - } - securityGroupRule = new SecurityGroupRuleVO(ruleType, securityGroup.getId(), startPortOrType, endPortOrCode, protocol, cidr); - securityGroupRule = _securityGroupRuleDao.persist(securityGroupRule); - newRules.add(securityGroupRule); - } - } - if (s_logger.isDebugEnabled()) { - s_logger.debug("Added " + newRules.size() + " rules to security group " + securityGroup.getName()); - } - txn.commit(); - final ArrayList affectedVms = new ArrayList(); - affectedVms.addAll(_securityGroupVMMapDao.listVmIdsBySecurityGroup(securityGroup.getId())); - scheduleRulesetUpdateToHosts(affectedVms, true, null); - return newRules; - } catch (Exception e) { - s_logger.warn("Exception caught when adding security group rules ", e); - throw new CloudRuntimeException("Exception caught when adding security group rules", e); - } finally { - if (securityGroup != null) { - _securityGroupDao.releaseFromLockTable(securityGroup.getId()); - } - } + }); + } @Override @@ -792,11 +789,11 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro return revokeSecurityGroupRule(id, SecurityRuleType.IngressRule); } - private boolean revokeSecurityGroupRule(Long id, SecurityRuleType type) { + private boolean revokeSecurityGroupRule(final Long id, SecurityRuleType type) { // input validation Account caller = CallContext.current().getCallingAccount(); - SecurityGroupRuleVO rule = _securityGroupRuleDao.findById(id); + final SecurityGroupRuleVO rule = _securityGroupRuleDao.findById(id); if (rule == null) { s_logger.debug("Unable to find security rule with id " + id); throw new InvalidParameterValueException("Unable to find security rule with id " + id); @@ -812,36 +809,37 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro SecurityGroup securityGroup = _securityGroupDao.findById(rule.getSecurityGroupId()); _accountMgr.checkAccess(caller, null, true, securityGroup); - SecurityGroupVO groupHandle = null; - final Transaction txn = Transaction.currentTxn(); + return Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + SecurityGroupVO groupHandle = null; - try { - txn.start(); - // acquire lock on parent group (preserving this logic) - groupHandle = _securityGroupDao.acquireInLockTable(rule.getSecurityGroupId()); - if (groupHandle == null) { - s_logger.warn("Could not acquire lock on security group id: " + rule.getSecurityGroupId()); - return false; + try { + // acquire lock on parent group (preserving this logic) + groupHandle = _securityGroupDao.acquireInLockTable(rule.getSecurityGroupId()); + if (groupHandle == null) { + s_logger.warn("Could not acquire lock on security group id: " + rule.getSecurityGroupId()); + return false; + } + + _securityGroupRuleDao.remove(id); + s_logger.debug("revokeSecurityGroupRule succeeded for security rule id: " + id); + + final ArrayList affectedVms = new ArrayList(); + affectedVms.addAll(_securityGroupVMMapDao.listVmIdsBySecurityGroup(groupHandle.getId())); + scheduleRulesetUpdateToHosts(affectedVms, true, null); + + return true; + } catch (Exception e) { + s_logger.warn("Exception caught when deleting security rules ", e); + throw new CloudRuntimeException("Exception caught when deleting security rules", e); + } finally { + if (groupHandle != null) { + _securityGroupDao.releaseFromLockTable(groupHandle.getId()); + } + } } - - _securityGroupRuleDao.remove(id); - s_logger.debug("revokeSecurityGroupRule succeeded for security rule id: " + id); - - final ArrayList affectedVms = new ArrayList(); - affectedVms.addAll(_securityGroupVMMapDao.listVmIdsBySecurityGroup(groupHandle.getId())); - scheduleRulesetUpdateToHosts(affectedVms, true, null); - - return true; - } catch (Exception e) { - s_logger.warn("Exception caught when deleting security rules ", e); - throw new CloudRuntimeException("Exception caught when deleting security rules", e); - } finally { - if (groupHandle != null) { - _securityGroupDao.releaseFromLockTable(groupHandle.getId()); - } - txn.commit(); - } - + }); } @Override @@ -939,7 +937,7 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro } return; } - Long userVmId = work.getInstanceId(); + final Long userVmId = work.getInstanceId(); if (work.getStep() == Step.Done) { if (s_logger.isDebugEnabled()) { s_logger.debug("Security Group work: found a job in done state, rescheduling for vm: " + userVmId); @@ -949,68 +947,73 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro scheduleRulesetUpdateToHosts(affectedVms, false, _timeBetweenCleanups*1000l); return; } - UserVm vm = null; - Long seqnum = null; s_logger.debug("Working on " + work); - final Transaction txn = Transaction.currentTxn(); - txn.start(); - boolean locked = false; - try { - vm = _userVMDao.acquireInLockTable(work.getInstanceId()); - if (vm == null) { - vm = _userVMDao.findById(work.getInstanceId()); - if (vm == null) { - s_logger.info("VM " + work.getInstanceId() + " is removed"); - locked = true; - return; - } - s_logger.warn("Unable to acquire lock on vm id=" + userVmId); - return; - } - locked = true; - Long agentId = null; - VmRulesetLogVO log = _rulesetLogDao.findByVmId(userVmId); - if (log == null) { - s_logger.warn("Cannot find log record for vm id=" + userVmId); - return; - } - seqnum = log.getLogsequence(); - if (vm != null && vm.getState() == State.Running) { - Map> ingressRules = generateRulesForVM(userVmId, SecurityRuleType.IngressRule); - Map> egressRules = generateRulesForVM(userVmId, SecurityRuleType.EgressRule); - agentId = vm.getHostId(); - if (agentId != null) { - // get nic secondary ip address - String privateIp = vm.getPrivateIpAddress(); - NicVO nic = _nicDao.findByIp4AddressAndVmId(privateIp, vm.getId()); - List nicSecIps = null; - if (nic != null) { - if (nic.getSecondaryIp()) { - //get secondary ips of the vm - long networkId = nic.getNetworkId(); - nicSecIps = _nicSecIpDao.getSecondaryIpAddressesForNic(nic.getId()); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + UserVm vm = null; + Long seqnum = null; + + boolean locked = false; + try { + vm = _userVMDao.acquireInLockTable(work.getInstanceId()); + if (vm == null) { + vm = _userVMDao.findById(work.getInstanceId()); + if (vm == null) { + s_logger.info("VM " + work.getInstanceId() + " is removed"); + locked = true; + return; + } + s_logger.warn("Unable to acquire lock on vm id=" + userVmId); + return; + } + locked = true; + Long agentId = null; + VmRulesetLogVO log = _rulesetLogDao.findByVmId(userVmId); + if (log == null) { + s_logger.warn("Cannot find log record for vm id=" + userVmId); + return; + } + seqnum = log.getLogsequence(); + + if (vm != null && vm.getState() == State.Running) { + Map> ingressRules = generateRulesForVM(userVmId, SecurityRuleType.IngressRule); + Map> egressRules = generateRulesForVM(userVmId, SecurityRuleType.EgressRule); + agentId = vm.getHostId(); + if (agentId != null) { + // get nic secondary ip address + String privateIp = vm.getPrivateIpAddress(); + NicVO nic = _nicDao.findByIp4AddressAndVmId(privateIp, vm.getId()); + List nicSecIps = null; + if (nic != null) { + if (nic.getSecondaryIp()) { + //get secondary ips of the vm + long networkId = nic.getNetworkId(); + nicSecIps = _nicSecIpDao.getSecondaryIpAddressesForNic(nic.getId()); + } + } + SecurityGroupRulesCmd cmd = generateRulesetCmd( vm.getInstanceName(), vm.getPrivateIpAddress(), vm.getPrivateMacAddress(), vm.getId(), generateRulesetSignature(ingressRules, egressRules), seqnum, + ingressRules, egressRules, nicSecIps); + Commands cmds = new Commands(cmd); + try { + _agentMgr.send(agentId, cmds, _answerListener); + } catch (AgentUnavailableException e) { + s_logger.debug("Unable to send ingress rules updates for vm: " + userVmId + "(agentid=" + agentId + ")"); + _workDao.updateStep(work.getInstanceId(), seqnum, Step.Done); + } + } } - SecurityGroupRulesCmd cmd = generateRulesetCmd( vm.getInstanceName(), vm.getPrivateIpAddress(), vm.getPrivateMacAddress(), vm.getId(), generateRulesetSignature(ingressRules, egressRules), seqnum, - ingressRules, egressRules, nicSecIps); - Commands cmds = new Commands(cmd); - try { - _agentMgr.send(agentId, cmds, _answerListener); - } catch (AgentUnavailableException e) { - s_logger.debug("Unable to send ingress rules updates for vm: " + userVmId + "(agentid=" + agentId + ")"); - _workDao.updateStep(work.getInstanceId(), seqnum, Step.Done); + } finally { + if (locked) { + _userVMDao.releaseFromLockTable(userVmId); + _workDao.updateStep(work.getId(), Step.Done); } - } } - } finally { - if (locked) { - _userVMDao.releaseFromLockTable(userVmId); - _workDao.updateStep(work.getId(), Step.Done); - } - txn.commit(); - } + }); + } @Override @@ -1021,41 +1024,40 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro return false; } if (groups != null && !groups.isEmpty()) { - - final Transaction txn = Transaction.currentTxn(); - txn.start(); - UserVm userVm = _userVMDao.acquireInLockTable(userVmId); // ensures that duplicate entries are not created. - List sgs = new ArrayList(); - for (Long sgId : groups) { - sgs.add(_securityGroupDao.findById(sgId)); - } - final Set uniqueGroups = new TreeSet(new SecurityGroupVOComparator()); - uniqueGroups.addAll(sgs); - if (userVm == null) { - s_logger.warn("Failed to acquire lock on user vm id=" + userVmId); - } - try { - for (SecurityGroupVO securityGroup : uniqueGroups) { - // don't let the group be deleted from under us. - SecurityGroupVO ngrpLock = _securityGroupDao.lockRow(securityGroup.getId(), false); - if (ngrpLock == null) { - s_logger.warn("Failed to acquire lock on network group id=" + securityGroup.getId() + " name=" + securityGroup.getName()); - txn.rollback(); - return false; + return Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + UserVm userVm = _userVMDao.acquireInLockTable(userVmId); // ensures that duplicate entries are not created. + List sgs = new ArrayList(); + for (Long sgId : groups) { + sgs.add(_securityGroupDao.findById(sgId)); } - if (_securityGroupVMMapDao.findByVmIdGroupId(userVmId, securityGroup.getId()) == null) { - SecurityGroupVMMapVO groupVmMapVO = new SecurityGroupVMMapVO(securityGroup.getId(), userVmId); - _securityGroupVMMapDao.persist(groupVmMapVO); + final Set uniqueGroups = new TreeSet(new SecurityGroupVOComparator()); + uniqueGroups.addAll(sgs); + if (userVm == null) { + s_logger.warn("Failed to acquire lock on user vm id=" + userVmId); + } + try { + for (SecurityGroupVO securityGroup : uniqueGroups) { + // don't let the group be deleted from under us. + SecurityGroupVO ngrpLock = _securityGroupDao.lockRow(securityGroup.getId(), false); + if (ngrpLock == null) { + s_logger.warn("Failed to acquire lock on network group id=" + securityGroup.getId() + " name=" + securityGroup.getName()); + throw new ConcurrentModificationException("Failed to acquire lock on network group id=" + securityGroup.getId() + " name=" + securityGroup.getName()); + } + if (_securityGroupVMMapDao.findByVmIdGroupId(userVmId, securityGroup.getId()) == null) { + SecurityGroupVMMapVO groupVmMapVO = new SecurityGroupVMMapVO(securityGroup.getId(), userVmId); + _securityGroupVMMapDao.persist(groupVmMapVO); + } + } + return true; + } finally { + if (userVm != null) { + _userVMDao.releaseFromLockTable(userVmId); + } } } - txn.commit(); - return true; - } finally { - if (userVm != null) { - _userVMDao.releaseFromLockTable(userVmId); - } - } - + }); } return false; @@ -1063,22 +1065,24 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro @Override @DB - public void removeInstanceFromGroups(long userVmId) { + public void removeInstanceFromGroups(final long userVmId) { if (_securityGroupVMMapDao.countSGForVm(userVmId) < 1) { s_logger.trace("No security groups found for vm id=" + userVmId + ", returning"); return; } - final Transaction txn = Transaction.currentTxn(); - txn.start(); - UserVm userVm = _userVMDao.acquireInLockTable(userVmId); // ensures that duplicate entries are not created in - // addInstance - if (userVm == null) { - s_logger.warn("Failed to acquire lock on user vm id=" + userVmId); - } - int n = _securityGroupVMMapDao.deleteVM(userVmId); - s_logger.info("Disassociated " + n + " network groups " + " from uservm " + userVmId); - _userVMDao.releaseFromLockTable(userVmId); - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + UserVm userVm = _userVMDao.acquireInLockTable(userVmId); // ensures that duplicate entries are not created in + // addInstance + if (userVm == null) { + s_logger.warn("Failed to acquire lock on user vm id=" + userVmId); + } + int n = _securityGroupVMMapDao.deleteVM(userVmId); + s_logger.info("Disassociated " + n + " network groups " + " from uservm " + userVmId); + _userVMDao.releaseFromLockTable(userVmId); + } + }); s_logger.debug("Security group mappings are removed successfully for vm id=" + userVmId); } @@ -1086,7 +1090,7 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro @Override @ActionEvent(eventType = EventTypes.EVENT_SECURITY_GROUP_DELETE, eventDescription = "deleting security group") public boolean deleteSecurityGroup(DeleteSecurityGroupCmd cmd) throws ResourceInUseException { - Long groupId = cmd.getId(); + final Long groupId = cmd.getId(); Account caller = CallContext.current().getCallingAccount(); SecurityGroupVO group = _securityGroupDao.findById(groupId); @@ -1097,32 +1101,34 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro // check permissions _accountMgr.checkAccess(caller, null, true, group); - final Transaction txn = Transaction.currentTxn(); - txn.start(); + return Transaction.execute(new TransactionCallbackWithException() { + @Override + public Boolean doInTransaction(TransactionStatus status) throws ResourceInUseException { + SecurityGroupVO group = _securityGroupDao.lockRow(groupId, true); + if (group == null) { + throw new InvalidParameterValueException("Unable to find security group by id " + groupId); + } + + if (group.getName().equalsIgnoreCase(SecurityGroupManager.DEFAULT_GROUP_NAME)) { + throw new InvalidParameterValueException("The network group default is reserved"); + } + + List allowingRules = _securityGroupRuleDao.listByAllowedSecurityGroupId(groupId); + List securityGroupVmMap = _securityGroupVMMapDao.listBySecurityGroup(groupId); + if (!allowingRules.isEmpty()) { + throw new ResourceInUseException("Cannot delete group when there are security rules that allow this group"); + } else if (!securityGroupVmMap.isEmpty()) { + throw new ResourceInUseException("Cannot delete group when it's in use by virtual machines"); + } + + _securityGroupDao.expunge(groupId); - group = _securityGroupDao.lockRow(groupId, true); - if (group == null) { - throw new InvalidParameterValueException("Unable to find security group by id " + groupId); - } + s_logger.debug("Deleted security group id=" + groupId); + + return true; + } + }); - if (group.getName().equalsIgnoreCase(SecurityGroupManager.DEFAULT_GROUP_NAME)) { - throw new InvalidParameterValueException("The network group default is reserved"); - } - - List allowingRules = _securityGroupRuleDao.listByAllowedSecurityGroupId(groupId); - List securityGroupVmMap = _securityGroupVMMapDao.listBySecurityGroup(groupId); - if (!allowingRules.isEmpty()) { - throw new ResourceInUseException("Cannot delete group when there are security rules that allow this group"); - } else if (!securityGroupVmMap.isEmpty()) { - throw new ResourceInUseException("Cannot delete group when it's in use by virtual machines"); - } - - _securityGroupDao.expunge(groupId); - txn.commit(); - - s_logger.debug("Deleted security group id=" + groupId); - - return true; } diff --git a/server/src/com/cloud/network/vpc/NetworkACLManagerImpl.java b/server/src/com/cloud/network/vpc/NetworkACLManagerImpl.java index 9923db56461..30d39e06b91 100644 --- a/server/src/com/cloud/network/vpc/NetworkACLManagerImpl.java +++ b/server/src/com/cloud/network/vpc/NetworkACLManagerImpl.java @@ -23,7 +23,6 @@ import javax.ejb.Local; import javax.inject.Inject; import org.apache.log4j.Logger; - import org.apache.cloudstack.context.CallContext; import com.cloud.configuration.ConfigurationManager; @@ -48,6 +47,8 @@ import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.EntityManager; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; @@ -214,30 +215,35 @@ public class NetworkACLManagerImpl extends ManagerBase implements NetworkACLMana @Override @DB @ActionEvent(eventType = EventTypes.EVENT_NETWORK_ACL_ITEM_CREATE, eventDescription = "creating network ACL Item", create = true) - public NetworkACLItem createNetworkACLItem(Integer portStart, Integer portEnd, String protocol, List sourceCidrList, - Integer icmpCode, Integer icmpType, NetworkACLItem.TrafficType trafficType, Long aclId, - String action, Integer number) { - NetworkACLItem.Action ruleAction = NetworkACLItem.Action.Allow; - if("deny".equalsIgnoreCase(action)){ - ruleAction = NetworkACLItem.Action.Deny; - } + public NetworkACLItem createNetworkACLItem(final Integer portStart, final Integer portEnd, final String protocol, final List sourceCidrList, + final Integer icmpCode, final Integer icmpType, final NetworkACLItem.TrafficType trafficType, final Long aclId, + final String action, Integer number) { // If number is null, set it to currentMax + 1 (for backward compatibility) if(number == null){ number = _networkACLItemDao.getMaxNumberByACL(aclId) + 1; } - Transaction txn = Transaction.currentTxn(); - txn.start(); + final Integer numberFinal = number; + NetworkACLItemVO newRule = Transaction.execute(new TransactionCallback() { + @Override + public NetworkACLItemVO doInTransaction(TransactionStatus status) { + NetworkACLItem.Action ruleAction = NetworkACLItem.Action.Allow; + if("deny".equalsIgnoreCase(action)){ + ruleAction = NetworkACLItem.Action.Deny; + } - NetworkACLItemVO newRule = new NetworkACLItemVO(portStart, portEnd, protocol.toLowerCase(), aclId, sourceCidrList, icmpCode, icmpType, trafficType, ruleAction, number); - newRule = _networkACLItemDao.persist(newRule); + NetworkACLItemVO newRule = new NetworkACLItemVO(portStart, portEnd, protocol.toLowerCase(), aclId, sourceCidrList, icmpCode, icmpType, trafficType, ruleAction, numberFinal); + newRule = _networkACLItemDao.persist(newRule); - if (!_networkACLItemDao.setStateToAdd(newRule)) { - throw new CloudRuntimeException("Unable to update the state to add for " + newRule); - } - CallContext.current().setEventDetails("ACL Item Id: " + newRule.getId()); + if (!_networkACLItemDao.setStateToAdd(newRule)) { + throw new CloudRuntimeException("Unable to update the state to add for " + newRule); + } + CallContext.current().setEventDetails("ACL Item Id: " + newRule.getId()); + + return newRule; + } + }); - txn.commit(); return getNetworkACLItem(newRule.getId()); } diff --git a/server/src/com/cloud/network/vpc/VpcManagerImpl.java b/server/src/com/cloud/network/vpc/VpcManagerImpl.java index 651e82c6d21..c072bfa6d00 100644 --- a/server/src/com/cloud/network/vpc/VpcManagerImpl.java +++ b/server/src/com/cloud/network/vpc/VpcManagerImpl.java @@ -118,9 +118,15 @@ import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.exception.ExceptionUtil; import com.cloud.utils.net.NetUtils; import com.cloud.vm.ReservationContext; import com.cloud.vm.ReservationContextImpl; @@ -206,50 +212,50 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis @DB public boolean configure(String name, Map params) throws ConfigurationException { //configure default vpc offering - Transaction txn = Transaction.currentTxn(); - txn.start(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + if (_vpcOffDao.findByUniqueName(VpcOffering.defaultVPCOfferingName) == null) { + s_logger.debug("Creating default VPC offering " + VpcOffering.defaultVPCOfferingName); + + Map> svcProviderMap = new HashMap>(); + Set defaultProviders = new HashSet(); + defaultProviders.add(Provider.VPCVirtualRouter); + for (Service svc : getSupportedServices()) { + if (svc == Service.Lb) { + Set lbProviders = new HashSet(); + lbProviders.add(Provider.VPCVirtualRouter); + lbProviders.add(Provider.InternalLbVm); + svcProviderMap.put(svc, lbProviders); + } else { + svcProviderMap.put(svc, defaultProviders); + } + } + createVpcOffering(VpcOffering.defaultVPCOfferingName, VpcOffering.defaultVPCOfferingName, svcProviderMap, + true, State.Enabled); + } - if (_vpcOffDao.findByUniqueName(VpcOffering.defaultVPCOfferingName) == null) { - s_logger.debug("Creating default VPC offering " + VpcOffering.defaultVPCOfferingName); - - Map> svcProviderMap = new HashMap>(); - Set defaultProviders = new HashSet(); - defaultProviders.add(Provider.VPCVirtualRouter); - for (Service svc : getSupportedServices()) { - if (svc == Service.Lb) { - Set lbProviders = new HashSet(); - lbProviders.add(Provider.VPCVirtualRouter); - lbProviders.add(Provider.InternalLbVm); - svcProviderMap.put(svc, lbProviders); - } else { - svcProviderMap.put(svc, defaultProviders); + //configure default vpc offering with Netscaler as LB Provider + if (_vpcOffDao.findByUniqueName(VpcOffering.defaultVPCNSOfferingName ) == null) { + s_logger.debug("Creating default VPC offering with Netscaler as LB Provider" + VpcOffering.defaultVPCNSOfferingName); + Map> svcProviderMap = new HashMap>(); + Set defaultProviders = new HashSet(); + defaultProviders.add(Provider.VPCVirtualRouter); + for (Service svc : getSupportedServices()) { + if (svc == Service.Lb) { + Set lbProviders = new HashSet(); + lbProviders.add(Provider.Netscaler); + lbProviders.add(Provider.InternalLbVm); + svcProviderMap.put(svc, lbProviders); + } else { + svcProviderMap.put(svc, defaultProviders); + } + } + createVpcOffering(VpcOffering.defaultVPCNSOfferingName, VpcOffering.defaultVPCNSOfferingName, + svcProviderMap, false, State.Enabled); } } - createVpcOffering(VpcOffering.defaultVPCOfferingName, VpcOffering.defaultVPCOfferingName, svcProviderMap, - true, State.Enabled); - } - - //configure default vpc offering with Netscaler as LB Provider - if (_vpcOffDao.findByUniqueName(VpcOffering.defaultVPCNSOfferingName ) == null) { - s_logger.debug("Creating default VPC offering with Netscaler as LB Provider" + VpcOffering.defaultVPCNSOfferingName); - Map> svcProviderMap = new HashMap>(); - Set defaultProviders = new HashSet(); - defaultProviders.add(Provider.VPCVirtualRouter); - for (Service svc : getSupportedServices()) { - if (svc == Service.Lb) { - Set lbProviders = new HashSet(); - lbProviders.add(Provider.Netscaler); - lbProviders.add(Provider.InternalLbVm); - svcProviderMap.put(svc, lbProviders); - } else { - svcProviderMap.put(svc, defaultProviders); - } - } - createVpcOffering(VpcOffering.defaultVPCNSOfferingName, VpcOffering.defaultVPCNSOfferingName, - svcProviderMap, false, State.Enabled); - } - - txn.commit(); + }); Map configs = _configDao.getConfiguration(params); String value = configs.get(Config.VpcCleanupInterval.key()); @@ -375,36 +381,38 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis @DB - protected VpcOffering createVpcOffering(String name, String displayText, Map> svcProviderMap, boolean isDefault, State state) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - // create vpc offering object - VpcOfferingVO offering = new VpcOfferingVO(name, displayText, isDefault, null); - - if (state != null) { - offering.setState(state); - } - s_logger.debug("Adding vpc offering " + offering); - offering = _vpcOffDao.persist(offering); - // populate services and providers - if (svcProviderMap != null) { - for (Network.Service service : svcProviderMap.keySet()) { - Set providers = svcProviderMap.get(service); - if (providers != null && !providers.isEmpty()) { - for (Network.Provider provider : providers) { - VpcOfferingServiceMapVO offService = new VpcOfferingServiceMapVO(offering.getId(), service, provider); - _vpcOffSvcMapDao.persist(offService); - s_logger.trace("Added service for the vpc offering: " + offService + " with provider " + provider.getName()); - } - } else { - throw new InvalidParameterValueException("Provider is missing for the VPC offering service " + service.getName()); - } - } - } - txn.commit(); + protected VpcOffering createVpcOffering(final String name, final String displayText, final Map> svcProviderMap, final boolean isDefault, final State state) { + return Transaction.execute(new TransactionCallback() { + @Override + public VpcOffering doInTransaction(TransactionStatus status) { + // create vpc offering object + VpcOfferingVO offering = new VpcOfferingVO(name, displayText, isDefault, null); - return offering; + if (state != null) { + offering.setState(state); + } + s_logger.debug("Adding vpc offering " + offering); + offering = _vpcOffDao.persist(offering); + // populate services and providers + if (svcProviderMap != null) { + for (Network.Service service : svcProviderMap.keySet()) { + Set providers = svcProviderMap.get(service); + if (providers != null && !providers.isEmpty()) { + for (Network.Provider provider : providers) { + VpcOfferingServiceMapVO offService = new VpcOfferingServiceMapVO(offering.getId(), service, provider); + _vpcOffSvcMapDao.persist(offService); + s_logger.trace("Added service for the vpc offering: " + offService + " with provider " + provider.getName()); + } + } else { + throw new InvalidParameterValueException("Provider is missing for the VPC offering service " + service.getName()); + } + } + } + + return offering; + } + }); } @Override @@ -635,14 +643,14 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis @DB - protected Vpc createVpc(long zoneId, long vpcOffId, Account vpcOwner, String vpcName, String displayText, String cidr, - String networkDomain) { - + protected Vpc createVpc(final long zoneId, final long vpcOffId, final Account vpcOwner, final String vpcName, final String displayText, final String cidr, + final String networkDomain) { + //Validate CIDR if (!NetUtils.isValidCIDR(cidr)) { throw new InvalidParameterValueException("Invalid CIDR specified " + cidr); } - + //cidr has to be RFC 1918 complient if (!NetUtils.validateGuestCidr(cidr)) { throw new InvalidParameterValueException("Guest Cidr " + cidr + " is not RFC1918 compliant"); @@ -657,17 +665,19 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis + "and the hyphen ('-'); can't start or end with \"-\""); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - VpcVO vpc = new VpcVO (zoneId, vpcName, displayText, vpcOwner.getId(), vpcOwner.getDomainId(), vpcOffId, cidr, - networkDomain); - vpc = _vpcDao.persist(vpc, finalizeServicesAndProvidersForVpc(zoneId, vpcOffId)); - _resourceLimitMgr.incrementResourceCount(vpcOwner.getId(), ResourceType.vpc); - txn.commit(); + return Transaction.execute(new TransactionCallback() { + @Override + public VpcVO doInTransaction(TransactionStatus status) { + VpcVO vpc = new VpcVO (zoneId, vpcName, displayText, vpcOwner.getId(), vpcOwner.getDomainId(), vpcOffId, cidr, + networkDomain); + vpc = _vpcDao.persist(vpc, finalizeServicesAndProvidersForVpc(zoneId, vpcOffId)); + _resourceLimitMgr.incrementResourceCount(vpcOwner.getId(), ResourceType.vpc); - s_logger.debug("Created VPC " + vpc); + s_logger.debug("Created VPC " + vpc); - return vpc; + return vpc; + } + }); } private Map> finalizeServicesAndProvidersForVpc(long zoneId, long offeringId) { @@ -722,7 +732,7 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis @Override @DB - public boolean destroyVpc(Vpc vpc, Account caller, Long callerUserId) throws ConcurrentOperationException, ResourceUnavailableException { + public boolean destroyVpc(final Vpc vpc, Account caller, Long callerUserId) throws ConcurrentOperationException, ResourceUnavailableException { s_logger.debug("Destroying vpc " + vpc); //don't allow to delete vpc if it's in use by existing non system networks (system networks are networks of a private gateway of the VPC, @@ -735,16 +745,18 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis //mark VPC as inactive if (vpc.getState() != Vpc.State.Inactive) { s_logger.debug("Updating VPC " + vpc + " with state " + Vpc.State.Inactive + " as a part of vpc delete"); - VpcVO vpcVO = _vpcDao.findById(vpc.getId()); + final VpcVO vpcVO = _vpcDao.findById(vpc.getId()); vpcVO.setState(Vpc.State.Inactive); - Transaction txn = Transaction.currentTxn(); - txn.start(); - _vpcDao.update(vpc.getId(), vpcVO); - - //decrement resource count - _resourceLimitMgr.decrementResourceCount(vpc.getAccountId(), ResourceType.vpc); - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _vpcDao.update(vpc.getId(), vpcVO); + + //decrement resource count + _resourceLimitMgr.decrementResourceCount(vpc.getAccountId(), ResourceType.vpc); + } + }); } //shutdown VPC @@ -1140,67 +1152,68 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis } @DB - protected void validateNewVpcGuestNetwork(String cidr, String gateway, Account networkOwner, Vpc vpc, String networkDomain) { + protected void validateNewVpcGuestNetwork(final String cidr, final String gateway, final Account networkOwner, final Vpc vpc, final String networkDomain) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - Vpc locked = _vpcDao.acquireInLockTable(vpc.getId()); - if (locked == null) { - throw new CloudRuntimeException("Unable to acquire lock on " + vpc); - } - - try { - //check number of active networks in vpc - if (_ntwkDao.countVpcNetworks(vpc.getId()) >= _maxNetworks) { - throw new CloudRuntimeException("Number of networks per VPC can't extend " - + _maxNetworks + "; increase it using global config " + Config.VpcMaxNetworks); - } - - - //1) CIDR is required - if (cidr == null) { - throw new InvalidParameterValueException("Gateway/netmask are required when create network for VPC"); - } - - //2) Network cidr should be within vpcCidr - if (!NetUtils.isNetworkAWithinNetworkB(cidr, vpc.getCidr())) { - throw new InvalidParameterValueException("Network cidr " + cidr + " is not within vpc " + vpc + " cidr"); - } - - //3) Network cidr shouldn't cross the cidr of other vpc network cidrs - List ntwks = _ntwkDao.listByVpc(vpc.getId()); - for (Network ntwk : ntwks) { - assert (cidr != null) : "Why the network cidr is null when it belongs to vpc?"; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + Vpc locked = _vpcDao.acquireInLockTable(vpc.getId()); + if (locked == null) { + throw new CloudRuntimeException("Unable to acquire lock on " + vpc); + } - if (NetUtils.isNetworkAWithinNetworkB(ntwk.getCidr(), cidr) - || NetUtils.isNetworkAWithinNetworkB(cidr, ntwk.getCidr())) { - throw new InvalidParameterValueException("Network cidr " + cidr + " crosses other network cidr " + ntwk + - " belonging to the same vpc " + vpc); + try { + //check number of active networks in vpc + if (_ntwkDao.countVpcNetworks(vpc.getId()) >= _maxNetworks) { + throw new CloudRuntimeException("Number of networks per VPC can't extend " + + _maxNetworks + "; increase it using global config " + Config.VpcMaxNetworks); + } + + + //1) CIDR is required + if (cidr == null) { + throw new InvalidParameterValueException("Gateway/netmask are required when create network for VPC"); + } + + //2) Network cidr should be within vpcCidr + if (!NetUtils.isNetworkAWithinNetworkB(cidr, vpc.getCidr())) { + throw new InvalidParameterValueException("Network cidr " + cidr + " is not within vpc " + vpc + " cidr"); + } + + //3) Network cidr shouldn't cross the cidr of other vpc network cidrs + List ntwks = _ntwkDao.listByVpc(vpc.getId()); + for (Network ntwk : ntwks) { + assert (cidr != null) : "Why the network cidr is null when it belongs to vpc?"; + + if (NetUtils.isNetworkAWithinNetworkB(ntwk.getCidr(), cidr) + || NetUtils.isNetworkAWithinNetworkB(cidr, ntwk.getCidr())) { + throw new InvalidParameterValueException("Network cidr " + cidr + " crosses other network cidr " + ntwk + + " belonging to the same vpc " + vpc); + } + } + + //4) vpc and network should belong to the same owner + if (vpc.getAccountId() != networkOwner.getId()) { + throw new InvalidParameterValueException("Vpc " + vpc + " owner is different from the network owner " + + networkOwner); + } + + //5) network domain should be the same as VPC's + if (!networkDomain.equalsIgnoreCase(vpc.getNetworkDomain())) { + throw new InvalidParameterValueException("Network domain of the new network should match network" + + " domain of vpc " + vpc); + } + + //6) gateway should never be equal to the cidr subnet + if (NetUtils.getCidrSubNet(cidr).equalsIgnoreCase(gateway)) { + throw new InvalidParameterValueException("Invalid gateway specified. It should never be equal to the cidr subnet value"); + } + } finally { + s_logger.debug("Releasing lock for " + locked); + _vpcDao.releaseFromLockTable(locked.getId()); } } - - //4) vpc and network should belong to the same owner - if (vpc.getAccountId() != networkOwner.getId()) { - throw new InvalidParameterValueException("Vpc " + vpc + " owner is different from the network owner " - + networkOwner); - } - - //5) network domain should be the same as VPC's - if (!networkDomain.equalsIgnoreCase(vpc.getNetworkDomain())) { - throw new InvalidParameterValueException("Network domain of the new network should match network" + - " domain of vpc " + vpc); - } - - //6) gateway should never be equal to the cidr subnet - if (NetUtils.getCidrSubNet(cidr).equalsIgnoreCase(gateway)) { - throw new InvalidParameterValueException("Invalid gateway specified. It should never be equal to the cidr subnet value"); - } - - txn.commit(); - } finally { - s_logger.debug("Releasing lock for " + locked); - _vpcDao.releaseFromLockTable(locked.getId()); - } + }); } @@ -1361,12 +1374,12 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis @Override @DB @ActionEvent(eventType = EventTypes.EVENT_PRIVATE_GATEWAY_CREATE, eventDescription = "creating vpc private gateway", create=true) - public PrivateGateway createVpcPrivateGateway(long vpcId, Long physicalNetworkId, String broadcastUri, String ipAddress, - String gateway, String netmask, long gatewayOwnerId, Long networkOfferingId, Boolean isSourceNat, Long aclId) throws ResourceAllocationException, + public PrivateGateway createVpcPrivateGateway(final long vpcId, Long physicalNetworkId, final String broadcastUri, final String ipAddress, + final String gateway, final String netmask, final long gatewayOwnerId, final Long networkOfferingId, final Boolean isSourceNat, final Long aclId) throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException { //Validate parameters - Vpc vpc = getActiveVpc(vpcId); + final Vpc vpc = getActiveVpc(vpcId); if (vpc == null) { InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find Enabled VPC by id specified"); ex.addProxyObject(String.valueOf(vpcId), "VPC"); @@ -1387,66 +1400,79 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis if (physNet == null) { physNet = _entityMgr.findById(PhysicalNetwork.class,physicalNetworkId); } - Long dcId = physNet.getDataCenterId(); + final Long dcId = physNet.getDataCenterId(); - Transaction txn = Transaction.currentTxn(); - txn.start(); - s_logger.debug("Creating Private gateway for VPC " + vpc); - //1) create private network unless it is existing and lswitch'd - Network privateNtwk = null; - if (BroadcastDomainType.getSchemeValue(BroadcastDomainType.fromString(broadcastUri)) == BroadcastDomainType.Lswitch) { - String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask); - - privateNtwk = _ntwkDao.getPrivateNetwork(broadcastUri, cidr, - gatewayOwnerId, dcId, networkOfferingId); - s_logger.info("found and using existing network for vpc " + vpc + ": " + broadcastUri); + final Long physicalNetworkIdFinal = physicalNetworkId; + final PhysicalNetwork physNetFinal = physNet; + VpcGatewayVO gatewayVO = null; + try { + gatewayVO = Transaction.execute(new TransactionCallbackWithException() { + @Override + public VpcGatewayVO doInTransaction(TransactionStatus status) throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException { + s_logger.debug("Creating Private gateway for VPC " + vpc); + //1) create private network unless it is existing and lswitch'd + Network privateNtwk = null; + if (BroadcastDomainType.getSchemeValue(BroadcastDomainType.fromString(broadcastUri)) == BroadcastDomainType.Lswitch) { + String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask); + + privateNtwk = _ntwkDao.getPrivateNetwork(broadcastUri, cidr, + gatewayOwnerId, dcId, networkOfferingId); + s_logger.info("found and using existing network for vpc " + vpc + ": " + broadcastUri); + } + if (privateNtwk == null) { + s_logger.info("creating new network for vpc " + vpc + " using broadcast uri: " + broadcastUri); + String networkName = "vpc-" + vpc.getName() + "-privateNetwork"; + privateNtwk = _ntwkSvc.createPrivateNetwork(networkName, networkName, physicalNetworkIdFinal, + broadcastUri, ipAddress, null, gateway, netmask, gatewayOwnerId, vpcId, isSourceNat, networkOfferingId); + } else { // create the nic/ip as createPrivateNetwork doesn''t do that work for us now + DataCenterVO dc = _dcDao.lockRow(physNetFinal.getDataCenterId(), true); + + //add entry to private_ip_address table + PrivateIpVO privateIp = _privateIpDao.findByIpAndSourceNetworkId(privateNtwk.getId(), ipAddress); + if (privateIp != null) { + throw new InvalidParameterValueException("Private ip address " + ipAddress + " already used for private gateway" + + " in zone " + _entityMgr.findById(DataCenter.class,dcId).getName()); + } + + Long mac = dc.getMacAddress(); + Long nextMac = mac + 1; + dc.setMacAddress(nextMac); + + privateIp = new PrivateIpVO(ipAddress, privateNtwk.getId(), nextMac, vpcId, true); + _privateIpDao.persist(privateIp); + + _dcDao.update(dc.getId(), dc); + } + + long networkAclId = NetworkACL.DEFAULT_DENY; + if (aclId != null) { + NetworkACLVO aclVO = _networkAclDao.findById(aclId); + if ( aclVO == null) { + throw new InvalidParameterValueException("Invalid network acl id passed "); + } + if ((aclVO.getVpcId() != vpcId) && !(aclId == NetworkACL.DEFAULT_DENY || aclId == NetworkACL.DEFAULT_ALLOW)) { + throw new InvalidParameterValueException("Private gateway and network acl are not in the same vpc"); + } + + networkAclId = aclId; + } + + //2) create gateway entry + VpcGatewayVO gatewayVO = new VpcGatewayVO(ipAddress, VpcGateway.Type.Private, vpcId, privateNtwk.getDataCenterId(), + privateNtwk.getId(), broadcastUri, gateway, netmask, vpc.getAccountId(), vpc.getDomainId(), isSourceNat, networkAclId); + _vpcGatewayDao.persist(gatewayVO); + + s_logger.debug("Created vpc gateway entry " + gatewayVO); + + return gatewayVO; + } + }); + } catch (Exception e) { + ExceptionUtil.rethrowRuntime(e); + ExceptionUtil.rethrow(e, InsufficientCapacityException.class); + ExceptionUtil.rethrow(e, ResourceAllocationException.class); + throw new IllegalStateException(e); } - if (privateNtwk == null) { - s_logger.info("creating new network for vpc " + vpc + " using broadcast uri: " + broadcastUri); - String networkName = "vpc-" + vpc.getName() + "-privateNetwork"; - privateNtwk = _ntwkSvc.createPrivateNetwork(networkName, networkName, physicalNetworkId, - broadcastUri, ipAddress, null, gateway, netmask, gatewayOwnerId, vpcId, isSourceNat, networkOfferingId); - } else { // create the nic/ip as createPrivateNetwork doesn''t do that work for us now - DataCenterVO dc = _dcDao.lockRow(physNet.getDataCenterId(), true); - - //add entry to private_ip_address table - PrivateIpVO privateIp = _privateIpDao.findByIpAndSourceNetworkId(privateNtwk.getId(), ipAddress); - if (privateIp != null) { - throw new InvalidParameterValueException("Private ip address " + ipAddress + " already used for private gateway" + - " in zone " + _entityMgr.findById(DataCenter.class,dcId).getName()); - } - - Long mac = dc.getMacAddress(); - Long nextMac = mac + 1; - dc.setMacAddress(nextMac); - - privateIp = new PrivateIpVO(ipAddress, privateNtwk.getId(), nextMac, vpcId, true); - _privateIpDao.persist(privateIp); - - _dcDao.update(dc.getId(), dc); - } - - long networkAclId = NetworkACL.DEFAULT_DENY; - if (aclId != null) { - NetworkACLVO aclVO = _networkAclDao.findById(aclId); - if ( aclVO == null) { - throw new InvalidParameterValueException("Invalid network acl id passed "); - } - if ((aclVO.getVpcId() != vpcId) && !(aclId == NetworkACL.DEFAULT_DENY || aclId == NetworkACL.DEFAULT_ALLOW)) { - throw new InvalidParameterValueException("Private gateway and network acl are not in the same vpc"); - } - - networkAclId = aclId; - } - - //2) create gateway entry - VpcGatewayVO gatewayVO = new VpcGatewayVO(ipAddress, VpcGateway.Type.Private, vpcId, privateNtwk.getDataCenterId(), - privateNtwk.getId(), broadcastUri, gateway, netmask, vpc.getAccountId(), vpc.getDomainId(), isSourceNat, networkAclId); - _vpcGatewayDao.persist(gatewayVO); - - s_logger.debug("Created vpc gateway entry " + gatewayVO); - - txn.commit(); return getVpcPrivateGateway(gatewayVO.getId()); } @@ -1496,26 +1522,28 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis @DB public boolean deleteVpcPrivateGateway(long gatewayId) throws ConcurrentOperationException, ResourceUnavailableException { - Transaction txn = Transaction.currentTxn(); - txn.start(); - VpcGatewayVO gatewayVO = _vpcGatewayDao.acquireInLockTable(gatewayId); + final VpcGatewayVO gatewayVO = _vpcGatewayDao.acquireInLockTable(gatewayId); if (gatewayVO == null || gatewayVO.getType() != VpcGateway.Type.Private) { throw new ConcurrentOperationException("Unable to lock gateway " + gatewayId); } try { - //don't allow to remove gateway when there are static routes associated with it - long routeCount = _staticRouteDao.countRoutesByGateway(gatewayVO.getId()); - if (routeCount > 0) { - throw new CloudRuntimeException("Can't delete private gateway " + gatewayVO + " as it has " + routeCount + - " static routes applied. Remove the routes first"); - } - - gatewayVO.setState(VpcGateway.State.Deleting); - _vpcGatewayDao.update(gatewayVO.getId(), gatewayVO); - s_logger.debug("Marked gateway " + gatewayVO + " with state " + VpcGateway.State.Deleting); - - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + //don't allow to remove gateway when there are static routes associated with it + long routeCount = _staticRouteDao.countRoutesByGateway(gatewayVO.getId()); + if (routeCount > 0) { + throw new CloudRuntimeException("Can't delete private gateway " + gatewayVO + " as it has " + routeCount + + " static routes applied. Remove the routes first"); + } + + gatewayVO.setState(VpcGateway.State.Deleting); + _vpcGatewayDao.update(gatewayVO.getId(), gatewayVO); + s_logger.debug("Marked gateway " + gatewayVO + " with state " + VpcGateway.State.Deleting); + } + }); + //1) delete the gateway on the backend PrivateGateway gateway = getVpcPrivateGateway(gatewayId); @@ -1539,9 +1567,9 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis } @DB - protected boolean deletePrivateGatewayFromTheDB(PrivateGateway gateway) { + protected boolean deletePrivateGatewayFromTheDB(final PrivateGateway gateway) { //check if there are ips allocted in the network - long networkId = gateway.getNetworkId(); + final long networkId = gateway.getNetworkId(); boolean deleteNetwork = true; List privateIps = _privateIpDao.listByNetworkId(networkId); @@ -1550,27 +1578,31 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis deleteNetwork = false; } - Transaction txn = Transaction.currentTxn(); - txn.start(); + //TODO: Clean this up, its bad. There is a DB transaction wrapping calls to NetworkElements (destroyNetwork will + // call network elements). + final boolean deleteNetworkFinal = deleteNetwork; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + PrivateIpVO ip = _privateIpDao.findByIpAndVpcId(gateway.getVpcId(), gateway.getIp4Address()); + if (ip != null) { + _privateIpDao.remove(ip.getId()); + s_logger.debug("Deleted private ip " + ip); + } + + if (deleteNetworkFinal) { + User callerUser = _accountMgr.getActiveUser(CallContext.current().getCallingUserId()); + Account owner = _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM); + ReservationContext context = new ReservationContextImpl(null, null, callerUser, owner); + _ntwkMgr.destroyNetwork(networkId, context); + s_logger.debug("Deleted private network id=" + networkId); + } + + _vpcGatewayDao.remove(gateway.getId()); + s_logger.debug("Deleted private gateway " + gateway); + } + }); - PrivateIpVO ip = _privateIpDao.findByIpAndVpcId(gateway.getVpcId(), gateway.getIp4Address()); - if (ip != null) { - _privateIpDao.remove(ip.getId()); - s_logger.debug("Deleted private ip " + ip); - } - - if (deleteNetwork) { - User callerUser = _accountMgr.getActiveUser(CallContext.current().getCallingUserId()); - Account owner = _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM); - ReservationContext context = new ReservationContextImpl(null, null, callerUser, owner); - _ntwkMgr.destroyNetwork(networkId, context); - s_logger.debug("Deleted private network id=" + networkId); - } - - _vpcGatewayDao.remove(gateway.getId()); - s_logger.debug("Deleted private gateway " + gateway); - - txn.commit(); return true; } @@ -1729,18 +1761,20 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis } @DB - protected boolean revokeStaticRoutesForVpc(long vpcId, Account caller) throws ResourceUnavailableException { + protected boolean revokeStaticRoutesForVpc(long vpcId, final Account caller) throws ResourceUnavailableException { //get all static routes for the vpc - List routes = _staticRouteDao.listByVpcId(vpcId); + final List routes = _staticRouteDao.listByVpcId(vpcId); s_logger.debug("Found " + routes.size() + " to revoke for the vpc " + vpcId); if (!routes.isEmpty()) { //mark all of them as revoke - Transaction txn = Transaction.currentTxn(); - txn.start(); - for (StaticRouteVO route : routes) { - markStaticRouteForRevoke(route, caller); - } - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (StaticRouteVO route : routes) { + markStaticRouteForRevoke(route, caller); + } + } + }); return applyStaticRoutes(vpcId); } @@ -1750,11 +1784,11 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis @Override @DB @ActionEvent(eventType = EventTypes.EVENT_STATIC_ROUTE_CREATE, eventDescription = "creating static route", create=true) - public StaticRoute createStaticRoute(long gatewayId, String cidr) throws NetworkRuleConflictException { + public StaticRoute createStaticRoute(long gatewayId, final String cidr) throws NetworkRuleConflictException { Account caller = CallContext.current().getCallingAccount(); //parameters validation - VpcGateway gateway = _vpcGatewayDao.findById(gatewayId); + final VpcGateway gateway = _vpcGatewayDao.findById(gatewayId); if (gateway == null) { throw new InvalidParameterValueException("Invalid gateway id is given"); } @@ -1763,7 +1797,7 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis throw new InvalidParameterValueException("Gateway is not in the " + VpcGateway.State.Ready + " state: " + gateway.getState()); } - Vpc vpc = getActiveVpc(gateway.getVpcId()); + final Vpc vpc = getActiveVpc(gateway.getVpcId()); if (vpc == null) { throw new InvalidParameterValueException("Can't add static route to VPC that is being deleted"); } @@ -1789,23 +1823,23 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis throw new InvalidParameterValueException("The static gateway cidr overlaps with one of the blacklisted routes of the zone the VPC belongs to"); } - Transaction txn = Transaction.currentTxn(); - txn.start(); + return Transaction.execute(new TransactionCallbackWithException() { + @Override + public StaticRouteVO doInTransaction(TransactionStatus status) throws NetworkRuleConflictException { + StaticRouteVO newRoute = new StaticRouteVO(gateway.getId(), cidr, vpc.getId(), vpc.getAccountId(), vpc.getDomainId()); + s_logger.debug("Adding static route " + newRoute); + newRoute = _staticRouteDao.persist(newRoute); + + detectRoutesConflict(newRoute); - StaticRouteVO newRoute = new StaticRouteVO(gateway.getId(), cidr, vpc.getId(), vpc.getAccountId(), vpc.getDomainId()); - s_logger.debug("Adding static route " + newRoute); - newRoute = _staticRouteDao.persist(newRoute); - - detectRoutesConflict(newRoute); - - if (!_staticRouteDao.setStateToAdd(newRoute)) { - throw new CloudRuntimeException("Unable to update the state to add for " + newRoute); - } - CallContext.current().setEventDetails("Static route Id: " + newRoute.getId()); - - txn.commit(); - - return newRoute; + if (!_staticRouteDao.setStateToAdd(newRoute)) { + throw new CloudRuntimeException("Unable to update the state to add for " + newRoute); + } + CallContext.current().setEventDetails("Static route Id: " + newRoute.getId()); + + return newRoute; + } + }); } protected boolean isCidrBlacklisted(String cidr, long zoneId) { @@ -1947,23 +1981,22 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis return; } - Transaction txn = null; try { - txn = Transaction.open(Transaction.CLOUD_DB); - - // Cleanup inactive VPCs - List inactiveVpcs = _vpcDao.listInactiveVpcs(); - s_logger.info("Found " + inactiveVpcs.size() + " removed VPCs to cleanup"); - for (VpcVO vpc : inactiveVpcs) { - s_logger.debug("Cleaning up " + vpc); - destroyVpc(vpc, _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM), User.UID_SYSTEM); - } + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws Exception { + // Cleanup inactive VPCs + List inactiveVpcs = _vpcDao.listInactiveVpcs(); + s_logger.info("Found " + inactiveVpcs.size() + " removed VPCs to cleanup"); + for (VpcVO vpc : inactiveVpcs) { + s_logger.debug("Cleaning up " + vpc); + destroyVpc(vpc, _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM), User.UID_SYSTEM); + } + } + }); } catch (Exception e) { s_logger.error("Exception ", e); } finally { - if (txn != null) { - txn.close(); - } lock.unlock(); } } catch (Exception e) { @@ -1976,7 +2009,7 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis @DB @Override @ActionEvent(eventType = EventTypes.EVENT_NET_IP_ASSIGN, eventDescription = "associating Ip", async = true) - public IpAddress associateIPToVpc(long ipId, long vpcId) throws ResourceAllocationException, ResourceUnavailableException, + public IpAddress associateIPToVpc(final long ipId, final long vpcId) throws ResourceAllocationException, ResourceUnavailableException, InsufficientAddressCapacityException, ConcurrentOperationException { Account caller = CallContext.current().getCallingAccount(); Account owner = null; @@ -2005,17 +2038,20 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis s_logger.debug("Associating ip " + ipToAssoc + " to vpc " + vpc); - Transaction txn = Transaction.currentTxn(); - txn.start(); - IPAddressVO ip = _ipAddressDao.findById(ipId); - //update ip address with networkId - ip.setVpcId(vpcId); - ip.setSourceNat(isSourceNat); - _ipAddressDao.update(ipId, ip); + final boolean isSourceNatFinal = isSourceNat; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + IPAddressVO ip = _ipAddressDao.findById(ipId); + //update ip address with networkId + ip.setVpcId(vpcId); + ip.setSourceNat(isSourceNatFinal); + _ipAddressDao.update(ipId, ip); - //mark ip as allocated - _ipAddrMgr.markPublicIpAsAllocated(ip); - txn.commit(); + //mark ip as allocated + _ipAddrMgr.markPublicIpAsAllocated(ip); + } + }); s_logger.debug("Successfully assigned ip " + ipToAssoc + " to vpc " + vpc); diff --git a/server/src/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java b/server/src/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java index 25c64e415ec..831223bc261 100755 --- a/server/src/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java +++ b/server/src/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java @@ -26,7 +26,6 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; - import org.apache.cloudstack.api.command.user.vpn.ListRemoteAccessVpnsCmd; import org.apache.cloudstack.api.command.user.vpn.ListVpnUsersCmd; import org.apache.cloudstack.context.CallContext; @@ -80,8 +79,12 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.net.NetUtils; @Local(value = RemoteAccessVpnService.class) @@ -114,13 +117,13 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc @Override @DB - public RemoteAccessVpn createRemoteAccessVpn(long publicIpId, String ipRange, boolean openFirewall, long networkId) + public RemoteAccessVpn createRemoteAccessVpn(final long publicIpId, String ipRange, final boolean openFirewall, long networkId) throws NetworkRuleConflictException { CallContext ctx = CallContext.current(); - Account caller = ctx.getCallingAccount(); + final Account caller = ctx.getCallingAccount(); // make sure ip address exists - PublicIpAddress ipAddr = _networkMgr.getPublicIpAddress(publicIpId); + final PublicIpAddress ipAddr = _networkMgr.getPublicIpAddress(publicIpId); if (ipAddr == null) { throw new InvalidParameterValueException("Unable to create remote access vpn, invalid public IP address id" + publicIpId); } @@ -163,7 +166,7 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc if (ipRange == null) { ipRange = RemoteAccessVpnClientIpRange.valueIn(ipAddr.getAccountId()); } - String[] range = ipRange.split("-"); + final String[] range = ipRange.split("-"); if (range.length != 2) { throw new InvalidParameterValueException("Invalid ip range"); } @@ -187,19 +190,18 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc // TODO: check overlap with private and public ip ranges in datacenter long startIp = NetUtils.ip2Long(range[0]); - String newIpRange = NetUtils.long2Ip(++startIp) + "-" + range[1]; - String sharedSecret = PasswordGenerator.generatePresharedKey(_pskLength); + final String newIpRange = NetUtils.long2Ip(++startIp) + "-" + range[1]; + final String sharedSecret = PasswordGenerator.generatePresharedKey(_pskLength); - Transaction txn = Transaction.currentTxn(); - txn.start(); - - _rulesMgr.reservePorts(ipAddr, NetUtils.UDP_PROTO, Purpose.Vpn, openFirewall, caller, NetUtils.VPN_PORT, NetUtils.VPN_L2TP_PORT, NetUtils.VPN_NATT_PORT); - vpnVO = new RemoteAccessVpnVO(ipAddr.getAccountId(), ipAddr.getDomainId(), ipAddr.getAssociatedWithNetworkId(), - publicIpId, range[0], newIpRange, sharedSecret); - RemoteAccessVpn vpn = _remoteAccessVpnDao.persist(vpnVO); - - txn.commit(); - return vpn; + return Transaction.execute(new TransactionCallbackWithException() { + @Override + public RemoteAccessVpn doInTransaction(TransactionStatus status) throws NetworkRuleConflictException { + _rulesMgr.reservePorts(ipAddr, NetUtils.UDP_PROTO, Purpose.Vpn, openFirewall, caller, NetUtils.VPN_PORT, NetUtils.VPN_L2TP_PORT, NetUtils.VPN_NATT_PORT); + RemoteAccessVpnVO vpnVO = new RemoteAccessVpnVO(ipAddr.getAccountId(), ipAddr.getDomainId(), ipAddr.getAssociatedWithNetworkId(), + publicIpId, range[0], newIpRange, sharedSecret); + return _remoteAccessVpnDao.persist(vpnVO); + } + }); } private void validateRemoteAccessVpnConfiguration() throws ConfigurationException { @@ -230,7 +232,7 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc @Override @DB public void destroyRemoteAccessVpnForIp(long ipId, Account caller) throws ResourceUnavailableException { - RemoteAccessVpnVO vpn = _remoteAccessVpnDao.findByPublicIpAddress(ipId); + final RemoteAccessVpnVO vpn = _remoteAccessVpnDao.findByPublicIpAddress(ipId); if (vpn == null) { s_logger.debug("there are no Remote access vpns for public ip address id=" + ipId); return; @@ -255,28 +257,28 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc } finally { if (success) { //Cleanup corresponding ports - List vpnFwRules = _rulesDao.listByIpAndPurpose(ipId, Purpose.Vpn); - Transaction txn = Transaction.currentTxn(); + final List vpnFwRules = _rulesDao.listByIpAndPurpose(ipId, Purpose.Vpn); boolean applyFirewall = false; - List fwRules = new ArrayList(); + final List fwRules = new ArrayList(); //if related firewall rule is created for the first vpn port, it would be created for the 2 other ports as well, so need to cleanup the backend if (_rulesDao.findByRelatedId(vpnFwRules.get(0).getId()) != null) { applyFirewall = true; } if (applyFirewall) { - txn.start(); - - for (FirewallRule vpnFwRule : vpnFwRules) { - //don't apply on the backend yet; send all 3 rules in a banch - _firewallMgr.revokeRelatedFirewallRule(vpnFwRule.getId(), false); - fwRules.add(_rulesDao.findByRelatedId(vpnFwRule.getId())); - } - - s_logger.debug("Marked " + fwRules.size() + " firewall rules as Revoked as a part of disable remote access vpn"); - - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (FirewallRule vpnFwRule : vpnFwRules) { + //don't apply on the backend yet; send all 3 rules in a banch + _firewallMgr.revokeRelatedFirewallRule(vpnFwRule.getId(), false); + fwRules.add(_rulesDao.findByRelatedId(vpnFwRule.getId())); + } + + s_logger.debug("Marked " + fwRules.size() + " firewall rules as Revoked as a part of disable remote access vpn"); + } + }); //now apply vpn rules on the backend s_logger.debug("Reapplying firewall rules for ip id=" + ipId + " as a part of disable remote access vpn"); @@ -285,26 +287,28 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc if (success) { try { - txn.start(); - _remoteAccessVpnDao.remove(vpn.getId()); - // Stop billing of VPN users when VPN is removed. VPN_User_ADD events will be generated when VPN is created again - List vpnUsers = _vpnUsersDao.listByAccount(vpn.getAccountId()); - for(VpnUserVO user : vpnUsers){ - // VPN_USER_REMOVE event is already generated for users in Revoke state - if(user.getState() != VpnUser.State.Revoke){ - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VPN_USER_REMOVE, user.getAccountId(), - 0, user.getId(), user.getUsername(), user.getClass().getName(), user.getUuid()); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _remoteAccessVpnDao.remove(vpn.getId()); + // Stop billing of VPN users when VPN is removed. VPN_User_ADD events will be generated when VPN is created again + List vpnUsers = _vpnUsersDao.listByAccount(vpn.getAccountId()); + for(VpnUserVO user : vpnUsers){ + // VPN_USER_REMOVE event is already generated for users in Revoke state + if(user.getState() != VpnUser.State.Revoke){ + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VPN_USER_REMOVE, user.getAccountId(), + 0, user.getId(), user.getUsername(), user.getClass().getName(), user.getUuid()); + } + } + if (vpnFwRules != null) { + for (FirewallRule vpnFwRule : vpnFwRules) { + _rulesDao.remove(vpnFwRule.getId()); + s_logger.debug("Successfully removed firewall rule with ip id=" + vpnFwRule.getSourceIpAddressId() + " and port " + vpnFwRule.getSourcePortStart() + " as a part of vpn cleanup"); + } + } } - } - if (vpnFwRules != null) { - for (FirewallRule vpnFwRule : vpnFwRules) { - _rulesDao.remove(vpnFwRule.getId()); - s_logger.debug("Successfully removed firewall rule with ip id=" + vpnFwRule.getSourceIpAddressId() + " and port " + vpnFwRule.getSourcePortStart() + " as a part of vpn cleanup"); - } - } - txn.commit(); + }); } catch (Exception ex) { - txn.rollback(); s_logger.warn("Unable to release the three vpn ports from the firewall rules", ex); } } @@ -314,8 +318,8 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc @Override @DB - public VpnUser addVpnUser(long vpnOwnerId, String username, String password) { - Account caller = CallContext.current().getCallingAccount(); + public VpnUser addVpnUser(final long vpnOwnerId, final String username, final String password) { + final Account caller = CallContext.current().getCallingAccount(); if (!username.matches("^[a-zA-Z0-9][a-zA-Z0-9@._-]{2,63}$")) { throw new InvalidParameterValueException( @@ -324,46 +328,54 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc if (!password.matches("^[a-zA-Z0-9][a-zA-Z0-9@#+=._-]{2,31}$")) { throw new InvalidParameterValueException("Password has to be 3-32 characters including alphabets, numbers and the set '@#+=.-_'"); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - Account owner = _accountDao.lockRow(vpnOwnerId, true); - if (owner == null) { - throw new InvalidParameterValueException("Unable to add vpn user: Another operation active"); - } - _accountMgr.checkAccess(caller, null, true, owner); - //don't allow duplicated user names for the same account - VpnUserVO vpnUser = _vpnUsersDao.findByAccountAndUsername(owner.getId(), username); - if (vpnUser != null) { - throw new InvalidParameterValueException("VPN User with name " + username + " is already added for account " + owner); - } - - long userCount = _vpnUsersDao.getVpnUserCount(owner.getId()); - if (userCount >= _userLimit) { - throw new AccountLimitException("Cannot add more than " + _userLimit + " remote access vpn users"); - } - - VpnUser user = _vpnUsersDao.persist(new VpnUserVO(vpnOwnerId, owner.getDomainId(), username, password)); - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VPN_USER_ADD, user.getAccountId(), 0, user.getId(), - user.getUsername(), user.getClass().getName(), user.getUuid()); - txn.commit(); - return user; + return Transaction.execute(new TransactionCallback() { + @Override + public VpnUser doInTransaction(TransactionStatus status) { + Account owner = _accountDao.lockRow(vpnOwnerId, true); + if (owner == null) { + throw new InvalidParameterValueException("Unable to add vpn user: Another operation active"); + } + _accountMgr.checkAccess(caller, null, true, owner); + + //don't allow duplicated user names for the same account + VpnUserVO vpnUser = _vpnUsersDao.findByAccountAndUsername(owner.getId(), username); + if (vpnUser != null) { + throw new InvalidParameterValueException("VPN User with name " + username + " is already added for account " + owner); + } + + long userCount = _vpnUsersDao.getVpnUserCount(owner.getId()); + if (userCount >= _userLimit) { + throw new AccountLimitException("Cannot add more than " + _userLimit + " remote access vpn users"); + } + + VpnUser user = _vpnUsersDao.persist(new VpnUserVO(vpnOwnerId, owner.getDomainId(), username, password)); + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VPN_USER_ADD, user.getAccountId(), 0, user.getId(), + user.getUsername(), user.getClass().getName(), user.getUuid()); + + return user; + } + }); } @DB @Override public boolean removeVpnUser(long vpnOwnerId, String username, Account caller) { - VpnUserVO user = _vpnUsersDao.findByAccountAndUsername(vpnOwnerId, username); + final VpnUserVO user = _vpnUsersDao.findByAccountAndUsername(vpnOwnerId, username); if (user == null) { throw new InvalidParameterValueException("Could not find vpn user " + username); } _accountMgr.checkAccess(caller, null, true, user); - Transaction txn = Transaction.currentTxn(); - txn.start(); - user.setState(State.Revoke); - _vpnUsersDao.update(user.getId(), user); - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VPN_USER_REMOVE, user.getAccountId(), 0, user.getId(), - user.getUsername(), user.getClass().getName(), user.getUuid()); - txn.commit(); + + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + user.setState(State.Revoke); + _vpnUsersDao.update(user.getId(), user); + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VPN_USER_REMOVE, user.getAccountId(), 0, user.getId(), + user.getUsername(), user.getClass().getName(), user.getUuid()); + } + }); + return true; } @@ -379,7 +391,7 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc public RemoteAccessVpnVO startRemoteAccessVpn(long ipAddressId, boolean openFirewall) throws ResourceUnavailableException { Account caller = CallContext.current().getCallingAccount(); - RemoteAccessVpnVO vpn = _remoteAccessVpnDao.findByPublicIpAddress(ipAddressId); + final RemoteAccessVpnVO vpn = _remoteAccessVpnDao.findByPublicIpAddress(ipAddressId); if (vpn == null) { throw new InvalidParameterValueException("Unable to find your vpn: " + ipAddressId); } @@ -407,20 +419,22 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc return vpn; } finally { if (started) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - vpn.setState(RemoteAccessVpn.State.Running); - _remoteAccessVpnDao.update(vpn.getId(), vpn); - - // Start billing of existing VPN users in ADD and Active state - List vpnUsers = _vpnUsersDao.listByAccount(vpn.getAccountId()); - for(VpnUserVO user : vpnUsers){ - if(user.getState() != VpnUser.State.Revoke){ - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VPN_USER_ADD, user.getAccountId(), 0, - user.getId(), user.getUsername(), user.getClass().getName(), user.getUuid()); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + vpn.setState(RemoteAccessVpn.State.Running); + _remoteAccessVpnDao.update(vpn.getId(), vpn); + + // Start billing of existing VPN users in ADD and Active state + List vpnUsers = _vpnUsersDao.listByAccount(vpn.getAccountId()); + for(VpnUserVO user : vpnUsers){ + if(user.getState() != VpnUser.State.Revoke){ + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VPN_USER_ADD, user.getAccountId(), 0, + user.getId(), user.getUsername(), user.getClass().getName(), user.getUuid()); + } + } } - } - txn.commit(); + }); } } } @@ -479,7 +493,7 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc } for (int i = 0; i < finals.length; i++) { - VpnUserVO user = users.get(i); + final VpnUserVO user = users.get(i); if (finals[i]) { if (user.getState() == State.Add) { user.setState(State.Active); @@ -489,12 +503,14 @@ public class RemoteAccessVpnManagerImpl extends ManagerBase implements RemoteAcc } } else { if (user.getState() == State.Add && (user.getUsername()).equals(userName)) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - _vpnUsersDao.remove(user.getId()); - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VPN_USER_REMOVE, user.getAccountId(), - 0, user.getId(), user.getUsername(), user.getClass().getName(), user.getUuid()); - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _vpnUsersDao.remove(user.getId()); + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VPN_USER_REMOVE, user.getAccountId(), + 0, user.getId(), user.getUsername(), user.getClass().getName(), user.getUuid()); + } + }); } s_logger.warn("Failed to apply vpn for user " + user.getUsername() + ", accountId=" + user.getAccountId()); } diff --git a/server/src/com/cloud/projects/ProjectManagerImpl.java b/server/src/com/cloud/projects/ProjectManagerImpl.java index a385739f9aa..22e2020ad22 100755 --- a/server/src/com/cloud/projects/ProjectManagerImpl.java +++ b/server/src/com/cloud/projects/ProjectManagerImpl.java @@ -82,6 +82,10 @@ import com.cloud.utils.component.ManagerBase; import com.cloud.utils.concurrency.NamedThreadFactory; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.sun.mail.smtp.SMTPMessage; import com.sun.mail.smtp.SMTPSSLTransport; @@ -175,7 +179,7 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { @Override @ActionEvent(eventType = EventTypes.EVENT_PROJECT_CREATE, eventDescription = "creating project", create=true) @DB - public Project createProject(String name, String displayText, String accountName, Long domainId) throws ResourceAllocationException{ + public Project createProject(final String name, final String displayText, String accountName, final Long domainId) throws ResourceAllocationException{ Account caller = CallContext.current().getCallingAccount(); Account owner = caller; @@ -201,30 +205,32 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { //do resource limit check _resourceLimitMgr.checkResourceLimit(owner, ResourceType.project); - Transaction txn = Transaction.currentTxn(); - txn.start(); + final Account ownerFinal = owner; + return Transaction.execute(new TransactionCallback() { + @Override + public Project doInTransaction(TransactionStatus status) { - //Create an account associated with the project - StringBuilder acctNm = new StringBuilder("PrjAcct-"); - acctNm.append(name).append("-").append(owner.getDomainId()); + //Create an account associated with the project + StringBuilder acctNm = new StringBuilder("PrjAcct-"); + acctNm.append(name).append("-").append(ownerFinal.getDomainId()); + + Account projectAccount = _accountMgr.createAccount(acctNm.toString(), Account.ACCOUNT_TYPE_PROJECT, domainId, null, null, UUID.randomUUID().toString()); + + Project project = _projectDao.persist(new ProjectVO(name, displayText, ownerFinal.getDomainId(), projectAccount.getId())); + + //assign owner to the project + assignAccountToProject(project, ownerFinal.getId(), ProjectAccount.Role.Admin); + + if (project != null) { + CallContext.current().setEventDetails("Project id=" + project.getId()); + } + + //Increment resource count + _resourceLimitMgr.incrementResourceCount(ownerFinal.getId(), ResourceType.project); - Account projectAccount = _accountMgr.createAccount(acctNm.toString(), Account.ACCOUNT_TYPE_PROJECT, domainId, null, null, UUID.randomUUID().toString()); - - Project project = _projectDao.persist(new ProjectVO(name, displayText, owner.getDomainId(), projectAccount.getId())); - - //assign owner to the project - assignAccountToProject(project, owner.getId(), ProjectAccount.Role.Admin); - - if (project != null) { - CallContext.current().setEventDetails("Project id=" + project.getId()); - } - - //Increment resource count - _resourceLimitMgr.incrementResourceCount(owner.getId(), ResourceType.project); - - txn.commit(); - - return project; + return project; + } + }); } @@ -268,20 +274,24 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { @DB @Override - public boolean deleteProject(Account caller, long callerUserId, ProjectVO project) { + public boolean deleteProject(Account caller, long callerUserId, final ProjectVO project) { //mark project as inactive first, so you can't add resources to it - Transaction txn = Transaction.currentTxn(); - txn.start(); - s_logger.debug("Marking project id=" + project.getId() + " with state " + State.Disabled + " as a part of project delete..."); - project.setState(State.Disabled); - boolean updateResult = _projectDao.update(project.getId(), project); - //owner can be already removed at this point, so adding the conditional check - Account projectOwner = getProjectOwner(project.getId()); - if (projectOwner != null) { - _resourceLimitMgr.decrementResourceCount(projectOwner.getId(), ResourceType.project); - } + boolean updateResult = Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + s_logger.debug("Marking project id=" + project.getId() + " with state " + State.Disabled + " as a part of project delete..."); + project.setState(State.Disabled); + boolean updateResult = _projectDao.update(project.getId(), project); + //owner can be already removed at this point, so adding the conditional check + Account projectOwner = getProjectOwner(project.getId()); + if (projectOwner != null) { + _resourceLimitMgr.decrementResourceCount(projectOwner.getId(), ResourceType.project); + } + + return updateResult; + } + }); - txn.commit(); if (updateResult) { //pass system caller when clenaup projects account @@ -298,7 +308,7 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { } @DB - private boolean cleanupProject(Project project, AccountVO caller, Long callerUserId) { + private boolean cleanupProject(final Project project, AccountVO caller, Long callerUserId) { boolean result=true; //Delete project's account AccountVO account = _accountDao.findById(project.getProjectAccountId()); @@ -308,20 +318,22 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { if (result) { //Unassign all users from the project - - Transaction txn = Transaction.currentTxn(); - txn.start(); - - s_logger.debug("Unassigning all accounts from project " + project + " as a part of project cleanup..."); - List projectAccounts = _projectAccountDao.listByProjectId(project.getId()); - for (ProjectAccount projectAccount : projectAccounts) { - result = result && unassignAccountFromProject(projectAccount.getProjectId(), projectAccount.getAccountId()); - } - - s_logger.debug("Removing all invitations for the project " + project + " as a part of project cleanup..."); - _projectInvitationDao.cleanupInvitations(project.getId()); - - txn.commit(); + result = Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + boolean result = true; + s_logger.debug("Unassigning all accounts from project " + project + " as a part of project cleanup..."); + List projectAccounts = _projectAccountDao.listByProjectId(project.getId()); + for (ProjectAccount projectAccount : projectAccounts) { + result = result && unassignAccountFromProject(projectAccount.getProjectId(), projectAccount.getAccountId()); + } + + s_logger.debug("Removing all invitations for the project " + project + " as a part of project cleanup..."); + _projectInvitationDao.cleanupInvitations(project.getId()); + + return result; + } + }); if (result) { s_logger.debug("Accounts are unassign successfully from project " + project + " as a part of project cleanup..."); } @@ -366,26 +378,28 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { } @Override @DB - public boolean deleteAccountFromProject(long projectId, long accountId) { - boolean success = true; - Transaction txn = Transaction.currentTxn(); - txn.start(); + public boolean deleteAccountFromProject(final long projectId, final long accountId) { + return Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + boolean success = true; - //remove account - ProjectAccountVO projectAccount = _projectAccountDao.findByProjectIdAccountId(projectId, accountId); - success = _projectAccountDao.remove(projectAccount.getId()); + //remove account + ProjectAccountVO projectAccount = _projectAccountDao.findByProjectIdAccountId(projectId, accountId); + success = _projectAccountDao.remove(projectAccount.getId()); - //remove all invitations for account - if (success) { - s_logger.debug("Removed account " + accountId + " from project " + projectId + " , cleaning up old invitations for account/project..."); - ProjectInvitation invite = _projectInvitationDao.findByAccountIdProjectId(accountId, projectId); - if (invite != null) { - success = success && _projectInvitationDao.remove(invite.getId()); + //remove all invitations for account + if (success) { + s_logger.debug("Removed account " + accountId + " from project " + projectId + " , cleaning up old invitations for account/project..."); + ProjectInvitation invite = _projectInvitationDao.findByAccountIdProjectId(accountId, projectId); + if (invite != null) { + success = success && _projectInvitationDao.remove(invite.getId()); + } + } + + return success; } - } - - txn.commit(); - return success; + }); } @Override @@ -442,11 +456,11 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { @Override @DB @ActionEvent(eventType = EventTypes.EVENT_PROJECT_UPDATE, eventDescription = "updating project", async=true) - public Project updateProject(long projectId, String displayText, String newOwnerName) throws ResourceAllocationException{ + public Project updateProject(final long projectId, final String displayText, final String newOwnerName) throws ResourceAllocationException{ Account caller = CallContext.current().getCallingAccount(); //check that the project exists - ProjectVO project = getProject(projectId); + final ProjectVO project = getProject(projectId); if (project == null) { throw new InvalidParameterValueException("Unable to find the project id=" + projectId); @@ -455,47 +469,49 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { //verify permissions _accountMgr.checkAccess(caller,AccessType.ModifyProject, true, _accountMgr.getAccount(project.getProjectAccountId())); - Transaction txn = Transaction.currentTxn(); - txn.start(); - if (displayText != null) { - project.setDisplayText(displayText); - _projectDao.update(projectId, project); - } - - if (newOwnerName != null) { - //check that the new owner exists - Account futureOwnerAccount = _accountMgr.getActiveAccountByName(newOwnerName, project.getDomainId()); - if (futureOwnerAccount == null) { - throw new InvalidParameterValueException("Unable to find account name=" + newOwnerName + " in domain id=" + project.getDomainId()); - } - Account currentOwnerAccount = getProjectOwner(projectId); - if (currentOwnerAccount.getId() != futureOwnerAccount.getId()) { - ProjectAccountVO futureOwner = _projectAccountDao.findByProjectIdAccountId(projectId, futureOwnerAccount.getAccountId()); - if (futureOwner == null) { - throw new InvalidParameterValueException("Account " + newOwnerName + " doesn't belong to the project. Add it to the project first and then change the project's ownership"); + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws ResourceAllocationException { + if (displayText != null) { + project.setDisplayText(displayText); + _projectDao.update(projectId, project); } - - //do resource limit check - _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(futureOwnerAccount.getId()), ResourceType.project); - - //unset the role for the old owner - ProjectAccountVO currentOwner = _projectAccountDao.findByProjectIdAccountId(projectId, currentOwnerAccount.getId()); - currentOwner.setAccountRole(Role.Regular); - _projectAccountDao.update(currentOwner.getId(), currentOwner); - _resourceLimitMgr.decrementResourceCount(currentOwnerAccount.getId(), ResourceType.project); - - //set new owner - futureOwner.setAccountRole(Role.Admin); - _projectAccountDao.update(futureOwner.getId(), futureOwner); - _resourceLimitMgr.incrementResourceCount(futureOwnerAccount.getId(), ResourceType.project); - - - } else { - s_logger.trace("Future owner " + newOwnerName + "is already the owner of the project id=" + projectId); + + if (newOwnerName != null) { + //check that the new owner exists + Account futureOwnerAccount = _accountMgr.getActiveAccountByName(newOwnerName, project.getDomainId()); + if (futureOwnerAccount == null) { + throw new InvalidParameterValueException("Unable to find account name=" + newOwnerName + " in domain id=" + project.getDomainId()); + } + Account currentOwnerAccount = getProjectOwner(projectId); + if (currentOwnerAccount.getId() != futureOwnerAccount.getId()) { + ProjectAccountVO futureOwner = _projectAccountDao.findByProjectIdAccountId(projectId, futureOwnerAccount.getAccountId()); + if (futureOwner == null) { + throw new InvalidParameterValueException("Account " + newOwnerName + " doesn't belong to the project. Add it to the project first and then change the project's ownership"); + } + + //do resource limit check + _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(futureOwnerAccount.getId()), ResourceType.project); + + //unset the role for the old owner + ProjectAccountVO currentOwner = _projectAccountDao.findByProjectIdAccountId(projectId, currentOwnerAccount.getId()); + currentOwner.setAccountRole(Role.Regular); + _projectAccountDao.update(currentOwner.getId(), currentOwner); + _resourceLimitMgr.decrementResourceCount(currentOwnerAccount.getId(), ResourceType.project); + + //set new owner + futureOwner.setAccountRole(Role.Admin); + _projectAccountDao.update(futureOwner.getId(), futureOwner); + _resourceLimitMgr.incrementResourceCount(futureOwnerAccount.getId(), ResourceType.project); + + + } else { + s_logger.trace("Future owner " + newOwnerName + "is already the owner of the project id=" + projectId); + } + } } - } + }); - txn.commit(); return _projectDao.findById(projectId); @@ -649,37 +665,40 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { } @DB - public boolean activeInviteExists(Project project, Long accountId, String email) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - //verify if the invitation was already generated - ProjectInvitationVO invite = null; - if (accountId != null) { - invite = _projectInvitationDao.findByAccountIdProjectId(accountId, project.getId()); - } else if (email != null) { - invite = _projectInvitationDao.findByEmailAndProjectId(email, project.getId()); - } - - if (invite != null) { - if (invite.getState() == ProjectInvitation.State.Completed || - (invite.getState() == ProjectInvitation.State.Pending && _projectInvitationDao.isActive(invite.getId(), _invitationTimeOut))) { - return true; - } else { - if (invite.getState() == ProjectInvitation.State.Pending) { - expireInvitation(invite); - } - //remove the expired/declined invitation + public boolean activeInviteExists(final Project project, final Long accountId, final String email) { + return Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + //verify if the invitation was already generated + ProjectInvitationVO invite = null; if (accountId != null) { - s_logger.debug("Removing invitation in state " + invite.getState() + " for account id=" + accountId + " to project " + project); + invite = _projectInvitationDao.findByAccountIdProjectId(accountId, project.getId()); } else if (email != null) { - s_logger.debug("Removing invitation in state " + invite.getState() + " for email " + email + " to project " + project); + invite = _projectInvitationDao.findByEmailAndProjectId(email, project.getId()); } - _projectInvitationDao.expunge(invite.getId()); + if (invite != null) { + if (invite.getState() == ProjectInvitation.State.Completed || + (invite.getState() == ProjectInvitation.State.Pending && _projectInvitationDao.isActive(invite.getId(), _invitationTimeOut))) { + return true; + } else { + if (invite.getState() == ProjectInvitation.State.Pending) { + expireInvitation(invite); + } + //remove the expired/declined invitation + if (accountId != null) { + s_logger.debug("Removing invitation in state " + invite.getState() + " for account id=" + accountId + " to project " + project); + } else if (email != null) { + s_logger.debug("Removing invitation in state " + invite.getState() + " for email " + email + " to project " + project); + } + + _projectInvitationDao.expunge(invite.getId()); + } + } + + return false; } - } - txn.commit(); - return false; + }); } public ProjectInvitation generateTokenBasedInvitation(Project project, String email, String token) { @@ -710,7 +729,7 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { @Override @DB @ActionEvent(eventType = EventTypes.EVENT_PROJECT_INVITATION_UPDATE, eventDescription = "updating project invitation", async=true) - public boolean updateInvitation(long projectId, String accountName, String token, boolean accept) { + public boolean updateInvitation(final long projectId, String accountName, String token, final boolean accept) { Account caller = CallContext.current().getCallingAccount(); Long accountId = null; boolean result = true; @@ -721,7 +740,7 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { } //check that the project exists - Project project = getProject(projectId); + final Project project = getProject(projectId); if (project == null) { throw new InvalidParameterValueException("Unable to find the project id=" + projectId); @@ -755,29 +774,37 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { expireInvitation(invite); throw new InvalidParameterValueException("Invitation is expired for account id=" + accountName + " to the project id=" + projectId); } else { - Transaction txn = Transaction.currentTxn(); - txn.start(); - - ProjectInvitation.State newState = accept ? ProjectInvitation.State.Completed : ProjectInvitation.State.Declined; - - //update invitation - s_logger.debug("Marking invitation " + invite + " with state " + newState); - invite.setState(newState); - result = _projectInvitationDao.update(invite.getId(), invite); - - if (result && accept) { - //check if account already exists for the project (was added before invitation got accepted) - ProjectAccount projectAccount = _projectAccountDao.findByProjectIdAccountId(projectId, accountId); - if (projectAccount != null) { - s_logger.debug("Account " + accountName + " already added to the project id=" + projectId); - } else { - assignAccountToProject(project, accountId, ProjectAccount.Role.Regular); + + final ProjectInvitationVO inviteFinal = invite; + final Long accountIdFinal = accountId; + final String accountNameFinal = accountName; + result = Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + boolean result = true; + + ProjectInvitation.State newState = accept ? ProjectInvitation.State.Completed : ProjectInvitation.State.Declined; + + //update invitation + s_logger.debug("Marking invitation " + inviteFinal + " with state " + newState); + inviteFinal.setState(newState); + result = _projectInvitationDao.update(inviteFinal.getId(), inviteFinal); + + if (result && accept) { + //check if account already exists for the project (was added before invitation got accepted) + ProjectAccount projectAccount = _projectAccountDao.findByProjectIdAccountId(projectId, accountIdFinal); + if (projectAccount != null) { + s_logger.debug("Account " + accountNameFinal + " already added to the project id=" + projectId); + } else { + assignAccountToProject(project, accountIdFinal, ProjectAccount.Role.Regular); + } + } else { + s_logger.warn("Failed to update project invitation " + inviteFinal + " with state " + newState); + } + + return result; } - } else { - s_logger.warn("Failed to update project invitation " + invite + " with state " + newState); - } - - txn.commit(); + }); } } else { throw new InvalidParameterValueException("Unable to find invitation for account name=" + accountName + " to the project id=" + projectId); @@ -794,11 +821,11 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { @Override @ActionEvent(eventType = EventTypes.EVENT_PROJECT_ACTIVATE, eventDescription = "activating project") @DB - public Project activateProject(long projectId) { + public Project activateProject(final long projectId) { Account caller = CallContext.current().getCallingAccount(); //check that the project exists - ProjectVO project = getProject(projectId); + final ProjectVO project = getProject(projectId); if (project == null) { InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project with specified id"); @@ -821,15 +848,15 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager { throw new InvalidParameterValueException("Can't activate the project in " + currentState + " state"); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - - project.setState(Project.State.Active); - _projectDao.update(projectId, project); - - _accountMgr.enableAccount(project.getProjectAccountId()); - - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + project.setState(Project.State.Active); + _projectDao.update(projectId, project); + + _accountMgr.enableAccount(project.getProjectAccountId()); + } + }); return _projectDao.findById(projectId); } diff --git a/server/src/com/cloud/resource/ResourceManagerImpl.java b/server/src/com/cloud/resource/ResourceManagerImpl.java index 7a9343cff41..2a245f138c3 100755 --- a/server/src/com/cloud/resource/ResourceManagerImpl.java +++ b/server/src/com/cloud/resource/ResourceManagerImpl.java @@ -143,6 +143,9 @@ import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.QueryBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; @@ -783,10 +786,10 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager, } @DB - protected boolean doDeleteHost(long hostId, boolean isForced, boolean isForceDeleteStorage) { + protected boolean doDeleteHost(final long hostId, boolean isForced, final boolean isForceDeleteStorage) { User caller = _accountMgr.getActiveUser(CallContext.current().getCallingUserId()); // Verify that host exists - HostVO host = _hostDao.findById(hostId); + final HostVO host = _hostDao.findById(hostId); if (host == null) { throw new InvalidParameterValueException("Host with id " + hostId + " doesn't exist"); } @@ -799,7 +802,7 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager, // Get storage pool host mappings here because they can be removed as a // part of handleDisconnect later // TODO: find out the bad boy, what's a buggy logic! - List pools = _storagePoolHostDao.listByHostIdIncludingRemoved(hostId); + final List pools = _storagePoolHostDao.listByHostIdIncludingRemoved(hostId); ResourceStateAdapter.DeleteHostAnswer answer = (ResourceStateAdapter.DeleteHostAnswer)dispatchToStateAdapters(ResourceStateAdapter.Event.DELETE_HOST, false, host, new Boolean(isForced), new Boolean(isForceDeleteStorage)); @@ -817,74 +820,77 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager, return true; } - Transaction txn = Transaction.currentTxn(); - txn.start(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { - _dcDao.releasePrivateIpAddress(host.getPrivateIpAddress(), host.getDataCenterId(), null); - _agentMgr.disconnectWithoutInvestigation(hostId, Status.Event.Remove); - - // delete host details - _hostDetailsDao.deleteDetails(hostId); - - host.setGuid(null); - Long clusterId = host.getClusterId(); - host.setClusterId(null); - _hostDao.update(host.getId(), host); - - _hostDao.remove(hostId); - if (clusterId != null) { - List hosts = listAllHostsInCluster(clusterId); - if (hosts.size() == 0) { - ClusterVO cluster = _clusterDao.findById(clusterId); - cluster.setGuid(null); - _clusterDao.update(clusterId, cluster); + _dcDao.releasePrivateIpAddress(host.getPrivateIpAddress(), host.getDataCenterId(), null); + _agentMgr.disconnectWithoutInvestigation(hostId, Status.Event.Remove); + + // delete host details + _hostDetailsDao.deleteDetails(hostId); + + host.setGuid(null); + Long clusterId = host.getClusterId(); + host.setClusterId(null); + _hostDao.update(host.getId(), host); + + _hostDao.remove(hostId); + if (clusterId != null) { + List hosts = listAllHostsInCluster(clusterId); + if (hosts.size() == 0) { + ClusterVO cluster = _clusterDao.findById(clusterId); + cluster.setGuid(null); + _clusterDao.update(clusterId, cluster); + } + } + + try { + resourceStateTransitTo(host, ResourceState.Event.DeleteHost, _nodeId); + } catch (NoTransitionException e) { + s_logger.debug("Cannot transmit host " + host.getId() + "to Enabled state", e); + } + + // Delete the associated entries in host ref table + _storagePoolHostDao.deletePrimaryRecordsForHost(hostId); + + // Make sure any VMs that were marked as being on this host are cleaned up + List vms = _vmDao.listByHostId(hostId); + for (VMInstanceVO vm : vms) { + // this is how VirtualMachineManagerImpl does it when it syncs VM states + vm.setState(State.Stopped); + vm.setHostId(null); + _vmDao.persist(vm); + } + + // For pool ids you got, delete local storage host entries in pool table + // where + for (StoragePoolHostVO pool : pools) { + Long poolId = pool.getPoolId(); + StoragePoolVO storagePool = _storagePoolDao.findById(poolId); + if (storagePool.isLocal() && isForceDeleteStorage) { + storagePool.setUuid(null); + storagePool.setClusterId(null); + _storagePoolDao.update(poolId, storagePool); + _storagePoolDao.remove(poolId); + s_logger.debug("Local storage id=" + poolId + " is removed as a part of host removal id=" + hostId); + } + } + + // delete the op_host_capacity entry + Object[] capacityTypes = {Capacity.CAPACITY_TYPE_CPU, Capacity.CAPACITY_TYPE_MEMORY}; + SearchCriteria hostCapacitySC = _capacityDao.createSearchCriteria(); + hostCapacitySC.addAnd("hostOrPoolId", SearchCriteria.Op.EQ, hostId); + hostCapacitySC.addAnd("capacityType", SearchCriteria.Op.IN, capacityTypes); + _capacityDao.remove(hostCapacitySC); + // remove from dedicated resources + DedicatedResourceVO dr = _dedicatedDao.findByHostId(hostId); + if (dr != null) { + _dedicatedDao.remove(dr.getId()); + } } - } + }); - try { - resourceStateTransitTo(host, ResourceState.Event.DeleteHost, _nodeId); - } catch (NoTransitionException e) { - s_logger.debug("Cannot transmit host " + host.getId() + "to Enabled state", e); - } - - // Delete the associated entries in host ref table - _storagePoolHostDao.deletePrimaryRecordsForHost(hostId); - - // Make sure any VMs that were marked as being on this host are cleaned up - List vms = _vmDao.listByHostId(hostId); - for (VMInstanceVO vm : vms) { - // this is how VirtualMachineManagerImpl does it when it syncs VM states - vm.setState(State.Stopped); - vm.setHostId(null); - _vmDao.persist(vm); - } - - // For pool ids you got, delete local storage host entries in pool table - // where - for (StoragePoolHostVO pool : pools) { - Long poolId = pool.getPoolId(); - StoragePoolVO storagePool = _storagePoolDao.findById(poolId); - if (storagePool.isLocal() && isForceDeleteStorage) { - storagePool.setUuid(null); - storagePool.setClusterId(null); - _storagePoolDao.update(poolId, storagePool); - _storagePoolDao.remove(poolId); - s_logger.debug("Local storage id=" + poolId + " is removed as a part of host removal id=" + hostId); - } - } - - // delete the op_host_capacity entry - Object[] capacityTypes = {Capacity.CAPACITY_TYPE_CPU, Capacity.CAPACITY_TYPE_MEMORY}; - SearchCriteria hostCapacitySC = _capacityDao.createSearchCriteria(); - hostCapacitySC.addAnd("hostOrPoolId", SearchCriteria.Op.EQ, hostId); - hostCapacitySC.addAnd("capacityType", SearchCriteria.Op.IN, capacityTypes); - _capacityDao.remove(hostCapacitySC); - // remove from dedicated resources - DedicatedResourceVO dr = _dedicatedDao.findByHostId(hostId); - if (dr != null) { - _dedicatedDao.remove(dr.getId()); - } - txn.commit(); return true; } @@ -904,63 +910,61 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager, @Override @DB - public boolean deleteCluster(DeleteClusterCmd cmd) { - Transaction txn = Transaction.currentTxn(); + public boolean deleteCluster(final DeleteClusterCmd cmd) { try { - txn.start(); - ClusterVO cluster = _clusterDao.lockRow(cmd.getId(), true); - if (cluster == null) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Cluster: " + cmd.getId() + " does not even exist. Delete call is ignored."); - } - txn.rollback(); - throw new CloudRuntimeException("Cluster: " + cmd.getId() + " does not exist"); - } + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + ClusterVO cluster = _clusterDao.lockRow(cmd.getId(), true); + if (cluster == null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Cluster: " + cmd.getId() + " does not even exist. Delete call is ignored."); + } + throw new CloudRuntimeException("Cluster: " + cmd.getId() + " does not exist"); + } + + Hypervisor.HypervisorType hypervisorType = cluster.getHypervisorType(); + + List hosts = listAllHostsInCluster(cmd.getId()); + if (hosts.size() > 0) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Cluster: " + cmd.getId() + " still has hosts, can't remove"); + } + throw new CloudRuntimeException("Cluster: " + cmd.getId() + " cannot be removed. Cluster still has hosts"); + } + + // don't allow to remove the cluster if it has non-removed storage + // pools + List storagePools = _storagePoolDao.listPoolsByCluster(cmd.getId()); + if (storagePools.size() > 0) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Cluster: " + cmd.getId() + " still has storage pools, can't remove"); + } + throw new CloudRuntimeException("Cluster: " + cmd.getId() + " cannot be removed. Cluster still has storage pools"); + } + + if (_clusterDao.remove(cmd.getId())) { + _capacityDao.removeBy(null, null, null, cluster.getId(), null); + // If this cluster is of type vmware, and if the nexus vswitch + // global parameter setting is turned + // on, remove the row in cluster_vsm_map for this cluster id. + if (hypervisorType == HypervisorType.VMware && Boolean.parseBoolean(_configDao.getValue(Config.VmwareUseNexusVSwitch.toString()))) { + _clusterVSMMapDao.removeByClusterId(cmd.getId()); + } + // remove from dedicated resources + DedicatedResourceVO dr = _dedicatedDao.findByClusterId(cluster.getId()); + if (dr != null) { + _dedicatedDao.remove(dr.getId()); + } + } - Hypervisor.HypervisorType hypervisorType = cluster.getHypervisorType(); - - List hosts = listAllHostsInCluster(cmd.getId()); - if (hosts.size() > 0) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Cluster: " + cmd.getId() + " still has hosts, can't remove"); } - txn.rollback(); - throw new CloudRuntimeException("Cluster: " + cmd.getId() + " cannot be removed. Cluster still has hosts"); - } - - // don't allow to remove the cluster if it has non-removed storage - // pools - List storagePools = _storagePoolDao.listPoolsByCluster(cmd.getId()); - if (storagePools.size() > 0) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Cluster: " + cmd.getId() + " still has storage pools, can't remove"); - } - txn.rollback(); - throw new CloudRuntimeException("Cluster: " + cmd.getId() + " cannot be removed. Cluster still has storage pools"); - } - - if (_clusterDao.remove(cmd.getId())) { - _capacityDao.removeBy(null, null, null, cluster.getId(), null); - // If this cluster is of type vmware, and if the nexus vswitch - // global parameter setting is turned - // on, remove the row in cluster_vsm_map for this cluster id. - if (hypervisorType == HypervisorType.VMware && Boolean.parseBoolean(_configDao.getValue(Config.VmwareUseNexusVSwitch.toString()))) { - _clusterVSMMapDao.removeByClusterId(cmd.getId()); - } - // remove from dedicated resources - DedicatedResourceVO dr = _dedicatedDao.findByClusterId(cluster.getId()); - if (dr != null) { - _dedicatedDao.remove(dr.getId()); - } - } - - txn.commit(); + }); return true; } catch (CloudRuntimeException e) { throw e; } catch (Throwable t) { s_logger.error("Unable to delete cluster: " + cmd.getId(), t); - txn.rollback(); return false; } } @@ -1034,26 +1038,15 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager, } if (doUpdate) { - Transaction txn = Transaction.currentTxn(); - try { - txn.start(); - _clusterDao.update(cluster.getId(), cluster); - txn.commit(); - } catch (Exception e) { - s_logger.error("Unable to update cluster due to " + e.getMessage(), e); - throw new CloudRuntimeException("Failed to update cluster. Please contact Cloud Support."); - } + _clusterDao.update(cluster.getId(), cluster); } if (newManagedState != null && !newManagedState.equals(oldManagedState)) { - Transaction txn = Transaction.currentTxn(); if (newManagedState.equals(Managed.ManagedState.Unmanaged)) { boolean success = false; try { - txn.start(); cluster.setManagedState(Managed.ManagedState.PrepareUnmanaged); _clusterDao.update(cluster.getId(), cluster); - txn.commit(); List hosts = listAllUpAndEnabledHosts(Host.Type.Routing, cluster.getId(), cluster.getPodId(), cluster.getDataCenterId()); for (HostVO host : hosts) { if (host.getType().equals(Host.Type.Routing) && !host.getStatus().equals(Status.Down) && !host.getStatus().equals(Status.Disconnected) && @@ -1092,16 +1085,12 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager, throw new CloudRuntimeException("PrepareUnmanaged Failed due to some hosts are still in UP status after 5 Minutes, please try later "); } } finally { - txn.start(); cluster.setManagedState(success ? Managed.ManagedState.Unmanaged : Managed.ManagedState.PrepareUnmanagedError); _clusterDao.update(cluster.getId(), cluster); - txn.commit(); } } else if (newManagedState.equals(Managed.ManagedState.Managed)) { - txn.start(); cluster.setManagedState(Managed.ManagedState.Managed); _clusterDao.update(cluster.getId(), cluster); - txn.commit(); } } @@ -2459,35 +2448,37 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager, @Override @DB @ActionEvent(eventType = EventTypes.EVENT_HOST_RESERVATION_RELEASE, eventDescription = "releasing host reservation", async = true) - public boolean releaseHostReservation(Long hostId) { - Transaction txn = Transaction.currentTxn(); + public boolean releaseHostReservation(final Long hostId) { try { - txn.start(); - PlannerHostReservationVO reservationEntry = _plannerHostReserveDao.findByHostId(hostId); - if (reservationEntry != null) { - long id = reservationEntry.getId(); - PlannerHostReservationVO hostReservation = _plannerHostReserveDao.lockRow(id, true); - if (hostReservation == null) { + return Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + PlannerHostReservationVO reservationEntry = _plannerHostReserveDao.findByHostId(hostId); + if (reservationEntry != null) { + long id = reservationEntry.getId(); + PlannerHostReservationVO hostReservation = _plannerHostReserveDao.lockRow(id, true); + if (hostReservation == null) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Host reservation for host: " + hostId + " does not even exist. Release reservartion call is ignored."); + } + return false; + } + hostReservation.setResourceUsage(null); + _plannerHostReserveDao.persist(hostReservation); + return true; + } + if (s_logger.isDebugEnabled()) { s_logger.debug("Host reservation for host: " + hostId + " does not even exist. Release reservartion call is ignored."); } - txn.rollback(); + return false; } - hostReservation.setResourceUsage(null); - _plannerHostReserveDao.persist(hostReservation); - txn.commit(); - return true; - } - if (s_logger.isDebugEnabled()) { - s_logger.debug("Host reservation for host: " + hostId + " does not even exist. Release reservartion call is ignored."); - } - return false; + }); } catch (CloudRuntimeException e) { throw e; } catch (Throwable t) { s_logger.error("Unable to release host reservation for host: " + hostId, t); - txn.rollback(); return false; } } diff --git a/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java b/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java index 1f8713aae76..7417754da0c 100755 --- a/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java +++ b/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java @@ -90,6 +90,9 @@ import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.SearchCriteria.Op; import com.cloud.utils.db.Transaction; @@ -361,8 +364,8 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim @Override @DB - public void checkResourceLimit(Account account, ResourceType type, long... count) throws ResourceAllocationException { - long numResources = ((count.length == 0) ? 1 : count[0]); + public void checkResourceLimit(final Account account, final ResourceType type, long... count) throws ResourceAllocationException { + final long numResources = ((count.length == 0) ? 1 : count[0]); Project project = null; // Don't place any limits on system or root admin accounts @@ -374,53 +377,53 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim project = _projectDao.findByProjectAccountId(account.getId()); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - try { - // Lock all rows first so nobody else can read it - Set rowIdsToLock = _resourceCountDao.listAllRowsToUpdate(account.getId(), ResourceOwnerType.Account, type); - SearchCriteria sc = ResourceCountSearch.create(); - sc.setParameters("id", rowIdsToLock.toArray()); - _resourceCountDao.lockRows(sc, null, true); - - // Check account limits - long accountLimit = findCorrectResourceLimitForAccount(account, type); - long potentialCount = _resourceCountDao.getResourceCount(account.getId(), ResourceOwnerType.Account, type) + numResources; - if (accountLimit != Resource.RESOURCE_UNLIMITED && potentialCount > accountLimit) { - String message = "Maximum number of resources of type '" + type + "' for account name=" + account.getAccountName() - + " in domain id=" + account.getDomainId() + " has been exceeded."; - if (project != null) { - message = "Maximum number of resources of type '" + type + "' for project name=" + project.getName() + final Project projectFinal = project; + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws ResourceAllocationException { + // Lock all rows first so nobody else can read it + Set rowIdsToLock = _resourceCountDao.listAllRowsToUpdate(account.getId(), ResourceOwnerType.Account, type); + SearchCriteria sc = ResourceCountSearch.create(); + sc.setParameters("id", rowIdsToLock.toArray()); + _resourceCountDao.lockRows(sc, null, true); + + // Check account limits + long accountLimit = findCorrectResourceLimitForAccount(account, type); + long potentialCount = _resourceCountDao.getResourceCount(account.getId(), ResourceOwnerType.Account, type) + numResources; + if (accountLimit != Resource.RESOURCE_UNLIMITED && potentialCount > accountLimit) { + String message = "Maximum number of resources of type '" + type + "' for account name=" + account.getAccountName() + " in domain id=" + account.getDomainId() + " has been exceeded."; + if (projectFinal != null) { + message = "Maximum number of resources of type '" + type + "' for project name=" + projectFinal.getName() + + " in domain id=" + account.getDomainId() + " has been exceeded."; + } + throw new ResourceAllocationException(message, type); } - throw new ResourceAllocationException(message, type); - } - - // check all domains in the account's domain hierarchy - Long domainId = null; - if (project != null) { - domainId = project.getDomainId(); - } else { - domainId = account.getDomainId(); - } - - while (domainId != null) { - DomainVO domain = _domainDao.findById(domainId); - // no limit check if it is ROOT domain - if (domainId != Domain.ROOT_DOMAIN) { - ResourceLimitVO domainLimit = _resourceLimitDao.findByOwnerIdAndType(domainId, ResourceOwnerType.Domain, type); - if (domainLimit != null && domainLimit.getMax().longValue() != Resource.RESOURCE_UNLIMITED) { - long domainCount = _resourceCountDao.getResourceCount(domainId, ResourceOwnerType.Domain, type); - if ((domainCount + numResources) > domainLimit.getMax().longValue()) { - throw new ResourceAllocationException("Maximum number of resources of type '" + type + "' for domain id=" + domainId + " has been exceeded.", type); + + // check all domains in the account's domain hierarchy + Long domainId = null; + if (projectFinal != null) { + domainId = projectFinal.getDomainId(); + } else { + domainId = account.getDomainId(); + } + + while (domainId != null) { + DomainVO domain = _domainDao.findById(domainId); + // no limit check if it is ROOT domain + if (domainId != Domain.ROOT_DOMAIN) { + ResourceLimitVO domainLimit = _resourceLimitDao.findByOwnerIdAndType(domainId, ResourceOwnerType.Domain, type); + if (domainLimit != null && domainLimit.getMax().longValue() != Resource.RESOURCE_UNLIMITED) { + long domainCount = _resourceCountDao.getResourceCount(domainId, ResourceOwnerType.Domain, type); + if ((domainCount + numResources) > domainLimit.getMax().longValue()) { + throw new ResourceAllocationException("Maximum number of resources of type '" + type + "' for domain id=" + domainId + " has been exceeded.", type); + } } } + domainId = domain.getParent(); } - domainId = domain.getParent(); } - } finally { - txn.commit(); - } + }); } @Override @@ -716,143 +719,143 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim } @DB - protected boolean updateResourceCountForAccount(long accountId, ResourceType type, boolean increment, long delta) { - boolean result = true; + protected boolean updateResourceCountForAccount(final long accountId, final ResourceType type, final boolean increment, final long delta) { try { - Transaction txn = Transaction.currentTxn(); - txn.start(); - - Set rowsToLock = _resourceCountDao.listAllRowsToUpdate(accountId, ResourceOwnerType.Account, type); - - // Lock rows first - SearchCriteria sc = ResourceCountSearch.create(); - sc.setParameters("id", rowsToLock.toArray()); - List rowsToUpdate = _resourceCountDao.lockRows(sc, null, true); - - for (ResourceCountVO rowToUpdate : rowsToUpdate) { - if (!_resourceCountDao.updateById(rowToUpdate.getId(), increment, delta)) { - s_logger.trace("Unable to update resource count for the row " + rowToUpdate); - result = false; + return Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + boolean result = true; + Set rowsToLock = _resourceCountDao.listAllRowsToUpdate(accountId, ResourceOwnerType.Account, type); + + // Lock rows first + SearchCriteria sc = ResourceCountSearch.create(); + sc.setParameters("id", rowsToLock.toArray()); + List rowsToUpdate = _resourceCountDao.lockRows(sc, null, true); + + for (ResourceCountVO rowToUpdate : rowsToUpdate) { + if (!_resourceCountDao.updateById(rowToUpdate.getId(), increment, delta)) { + s_logger.trace("Unable to update resource count for the row " + rowToUpdate); + result = false; + } + } + + return result; } - } - - txn.commit(); + }); } catch (Exception ex) { s_logger.error("Failed to update resource count for account id=" + accountId); - result = false; + return false; } - return result; } @DB - protected long recalculateDomainResourceCount(long domainId, ResourceType type) { - long newCount = 0; + protected long recalculateDomainResourceCount(final long domainId, final ResourceType type) { + return Transaction.execute(new TransactionCallback() { + @Override + public Long doInTransaction(TransactionStatus status) { + long newCount = 0; - Transaction txn = Transaction.currentTxn(); - txn.start(); - - try { - // Lock all rows first so nobody else can read it - Set rowIdsToLock = _resourceCountDao.listAllRowsToUpdate(domainId, ResourceOwnerType.Domain, type); - SearchCriteria sc = ResourceCountSearch.create(); - sc.setParameters("id", rowIdsToLock.toArray()); - _resourceCountDao.lockRows(sc, null, true); - - ResourceCountVO domainRC = _resourceCountDao.findByOwnerAndType(domainId, ResourceOwnerType.Domain, type); - long oldCount = domainRC.getCount(); - - List domainChildren = _domainDao.findImmediateChildrenForParent(domainId); - // for each child domain update the resource count - if (type.supportsOwner(ResourceOwnerType.Domain)) { - - // calculate project count here - if (type == ResourceType.project) { - newCount = newCount + _projectDao.countProjectsForDomain(domainId); + // Lock all rows first so nobody else can read it + Set rowIdsToLock = _resourceCountDao.listAllRowsToUpdate(domainId, ResourceOwnerType.Domain, type); + SearchCriteria sc = ResourceCountSearch.create(); + sc.setParameters("id", rowIdsToLock.toArray()); + _resourceCountDao.lockRows(sc, null, true); + + ResourceCountVO domainRC = _resourceCountDao.findByOwnerAndType(domainId, ResourceOwnerType.Domain, type); + long oldCount = domainRC.getCount(); + + List domainChildren = _domainDao.findImmediateChildrenForParent(domainId); + // for each child domain update the resource count + if (type.supportsOwner(ResourceOwnerType.Domain)) { + + // calculate project count here + if (type == ResourceType.project) { + newCount = newCount + _projectDao.countProjectsForDomain(domainId); + } + + for (DomainVO domainChild : domainChildren) { + long domainCount = recalculateDomainResourceCount(domainChild.getId(), type); + newCount = newCount + domainCount; // add the child domain count to parent domain count + } } - - for (DomainVO domainChild : domainChildren) { - long domainCount = recalculateDomainResourceCount(domainChild.getId(), type); - newCount = newCount + domainCount; // add the child domain count to parent domain count + + if (type.supportsOwner(ResourceOwnerType.Account)) { + List accounts = _accountDao.findActiveAccountsForDomain(domainId); + for (AccountVO account : accounts) { + long accountCount = recalculateAccountResourceCount(account.getId(), type); + newCount = newCount + accountCount; // add account's resource count to parent domain count + } } - } - - if (type.supportsOwner(ResourceOwnerType.Account)) { - List accounts = _accountDao.findActiveAccountsForDomain(domainId); - for (AccountVO account : accounts) { - long accountCount = recalculateAccountResourceCount(account.getId(), type); - newCount = newCount + accountCount; // add account's resource count to parent domain count + _resourceCountDao.setResourceCount(domainId, ResourceOwnerType.Domain, type, newCount); + + if (oldCount != newCount) { + s_logger.info("Discrepency in the resource count " + "(original count=" + oldCount + " correct count = " + + newCount + ") for type " + type + " for domain ID " + domainId + " is fixed during resource count recalculation."); } + + return newCount; } - _resourceCountDao.setResourceCount(domainId, ResourceOwnerType.Domain, type, newCount); - - if (oldCount != newCount) { - s_logger.info("Discrepency in the resource count " + "(original count=" + oldCount + " correct count = " + - newCount + ") for type " + type + " for domain ID " + domainId + " is fixed during resource count recalculation."); - } - } catch (Exception e) { - throw new CloudRuntimeException("Failed to update resource count for domain with Id " + domainId); - } finally { - txn.commit(); - } - - return newCount; + }); } @DB - protected long recalculateAccountResourceCount(long accountId, ResourceType type) { - Long newCount = null; + protected long recalculateAccountResourceCount(final long accountId, final ResourceType type) { + Long newCount = Transaction.execute(new TransactionCallback() { + @Override + public Long doInTransaction(TransactionStatus status) { + Long newCount = null; - Transaction txn = Transaction.currentTxn(); - txn.start(); - - // this lock guards against the updates to user_vm, volume, snapshot, public _ip and template table - // as any resource creation precedes with the resourceLimitExceeded check which needs this lock too - SearchCriteria sc = ResourceCountSearch.create(); - sc.setParameters("accountId", accountId); - _resourceCountDao.lockRows(sc, null, true); - - ResourceCountVO accountRC = _resourceCountDao.findByOwnerAndType(accountId, ResourceOwnerType.Account, type); - long oldCount = 0; - if (accountRC != null) - oldCount = accountRC.getCount(); - - if (type == Resource.ResourceType.user_vm) { - newCount = _userVmDao.countAllocatedVMsForAccount(accountId); - } else if (type == Resource.ResourceType.volume) { - newCount = _volumeDao.countAllocatedVolumesForAccount(accountId); - long virtualRouterCount = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId).size(); - newCount = newCount - virtualRouterCount; // don't count the volumes of virtual router - } else if (type == Resource.ResourceType.snapshot) { - newCount = _snapshotDao.countSnapshotsForAccount(accountId); - } else if (type == Resource.ResourceType.public_ip) { - newCount = calculatePublicIpForAccount(accountId); - } else if (type == Resource.ResourceType.template) { - newCount = _vmTemplateDao.countTemplatesForAccount(accountId); - } else if (type == Resource.ResourceType.project) { - newCount = _projectAccountDao.countByAccountIdAndRole(accountId, Role.Admin); - } else if (type == Resource.ResourceType.network) { - newCount = _networkDao.countNetworksUserCanCreate(accountId); - } else if (type == Resource.ResourceType.vpc) { - newCount = _vpcDao.countByAccountId(accountId); - } else if (type == Resource.ResourceType.cpu) { - newCount = countCpusForAccount(accountId); - } else if (type == Resource.ResourceType.memory) { - newCount = calculateMemoryForAccount(accountId); - } else if (type == Resource.ResourceType.primary_storage) { - List virtualRouters = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId); - newCount = _volumeDao.primaryStorageUsedForAccount(accountId, virtualRouters); - } else if (type == Resource.ResourceType.secondary_storage) { - newCount = calculateSecondaryStorageForAccount(accountId); - } else { - throw new InvalidParameterValueException("Unsupported resource type " + type); - } - _resourceCountDao.setResourceCount(accountId, ResourceOwnerType.Account, type, (newCount == null) ? 0 : newCount.longValue()); - - if (oldCount != newCount) { - s_logger.info("Discrepency in the resource count " + "(original count=" + oldCount + " correct count = " + - newCount + ") for type " + type + " for account ID " + accountId + " is fixed during resource count recalculation."); - } - txn.commit(); + // this lock guards against the updates to user_vm, volume, snapshot, public _ip and template table + // as any resource creation precedes with the resourceLimitExceeded check which needs this lock too + SearchCriteria sc = ResourceCountSearch.create(); + sc.setParameters("accountId", accountId); + _resourceCountDao.lockRows(sc, null, true); + + ResourceCountVO accountRC = _resourceCountDao.findByOwnerAndType(accountId, ResourceOwnerType.Account, type); + long oldCount = 0; + if (accountRC != null) + oldCount = accountRC.getCount(); + + if (type == Resource.ResourceType.user_vm) { + newCount = _userVmDao.countAllocatedVMsForAccount(accountId); + } else if (type == Resource.ResourceType.volume) { + newCount = _volumeDao.countAllocatedVolumesForAccount(accountId); + long virtualRouterCount = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId).size(); + newCount = newCount - virtualRouterCount; // don't count the volumes of virtual router + } else if (type == Resource.ResourceType.snapshot) { + newCount = _snapshotDao.countSnapshotsForAccount(accountId); + } else if (type == Resource.ResourceType.public_ip) { + newCount = calculatePublicIpForAccount(accountId); + } else if (type == Resource.ResourceType.template) { + newCount = _vmTemplateDao.countTemplatesForAccount(accountId); + } else if (type == Resource.ResourceType.project) { + newCount = _projectAccountDao.countByAccountIdAndRole(accountId, Role.Admin); + } else if (type == Resource.ResourceType.network) { + newCount = _networkDao.countNetworksUserCanCreate(accountId); + } else if (type == Resource.ResourceType.vpc) { + newCount = _vpcDao.countByAccountId(accountId); + } else if (type == Resource.ResourceType.cpu) { + newCount = countCpusForAccount(accountId); + } else if (type == Resource.ResourceType.memory) { + newCount = calculateMemoryForAccount(accountId); + } else if (type == Resource.ResourceType.primary_storage) { + List virtualRouters = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId); + newCount = _volumeDao.primaryStorageUsedForAccount(accountId, virtualRouters); + } else if (type == Resource.ResourceType.secondary_storage) { + newCount = calculateSecondaryStorageForAccount(accountId); + } else { + throw new InvalidParameterValueException("Unsupported resource type " + type); + } + _resourceCountDao.setResourceCount(accountId, ResourceOwnerType.Account, type, (newCount == null) ? 0 : newCount.longValue()); + + if (oldCount != newCount) { + s_logger.info("Discrepency in the resource count " + "(original count=" + oldCount + " correct count = " + + newCount + ") for type " + type + " for account ID " + accountId + " is fixed during resource count recalculation."); + } + + return newCount; + } + }); return (newCount == null) ? 0 : newCount.longValue(); } diff --git a/server/src/com/cloud/server/ConfigurationServerImpl.java b/server/src/com/cloud/server/ConfigurationServerImpl.java index af1847a9a35..51f99d6e529 100755 --- a/server/src/com/cloud/server/ConfigurationServerImpl.java +++ b/server/src/com/cloud/server/ConfigurationServerImpl.java @@ -42,14 +42,13 @@ import javax.crypto.SecretKey; import javax.inject.Inject; import javax.naming.ConfigurationException; -import org.apache.commons.codec.binary.Base64; -import org.apache.commons.io.FileUtils; -import org.apache.log4j.Logger; - import org.apache.cloudstack.framework.config.ConfigDepot; import org.apache.cloudstack.framework.config.ConfigDepotAdmin; import org.apache.cloudstack.framework.config.dao.ConfigurationDao; import org.apache.cloudstack.framework.config.impl.ConfigurationVO; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.io.FileUtils; +import org.apache.log4j.Logger; import com.cloud.cluster.ClusterManager; import com.cloud.configuration.Config; @@ -107,6 +106,10 @@ import com.cloud.utils.component.ManagerBase; import com.cloud.utils.crypt.DBEncryptionUtil; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionLegacy; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; import com.cloud.utils.script.Script; @@ -249,11 +252,9 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio // Create userIpAddress ranges // Update existing vlans with networkId - Transaction txn = Transaction.currentTxn(); - List vlans = _vlanDao.listAll(); if (vlans != null && !vlans.isEmpty()) { - for (VlanVO vlan : vlans) { + for (final VlanVO vlan : vlans) { if (vlan.getNetworkId().longValue() == 0) { updateVlanWithNetworkId(vlan); } @@ -261,15 +262,19 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio // Create vlan user_ip_address range String ipPange = vlan.getIpRange(); String[] range = ipPange.split("-"); - String startIp = range[0]; - String endIp = range[1]; + final String startIp = range[0]; + final String endIp = range[1]; + + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + IPRangeConfig config = new IPRangeConfig(); + long startIPLong = NetUtils.ip2Long(startIp); + long endIPLong = NetUtils.ip2Long(endIp); + config.savePublicIPRange(TransactionLegacy.currentTxn(), startIPLong, endIPLong, vlan.getDataCenterId(), vlan.getId(), vlan.getNetworkId(), vlan.getPhysicalNetworkId()); + } + }); - txn.start(); - IPRangeConfig config = new IPRangeConfig(); - long startIPLong = NetUtils.ip2Long(startIp); - long endIPLong = NetUtils.ip2Long(endIp); - config.savePublicIPRange(txn, startIPLong, endIPLong, vlan.getDataCenterId(), vlan.getId(), vlan.getNetworkId(), vlan.getPhysicalNetworkId()); - txn.commit(); } } } @@ -432,7 +437,7 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio protected void saveUser() { // insert system account String insertSql = "INSERT INTO `cloud`.`account` (id, uuid, account_name, type, domain_id, account.default) VALUES (1, UUID(), 'system', '1', '1', 1)"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql); stmt.executeUpdate(); @@ -441,7 +446,7 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio // insert system user insertSql = "INSERT INTO `cloud`.`user` (id, uuid, username, password, account_id, firstname, lastname, created, user.default)" + " VALUES (1, UUID(), 'system', RAND(), 1, 'system', 'cloud', now(), 1)"; - txn = Transaction.currentTxn(); + txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql); stmt.executeUpdate(); @@ -457,7 +462,7 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio // create an account for the admin user first insertSql = "INSERT INTO `cloud`.`account` (id, uuid, account_name, type, domain_id, account.default) VALUES (" + id + ", UUID(), '" + username + "', '1', '1', 1)"; - txn = Transaction.currentTxn(); + txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql); stmt.executeUpdate(); @@ -468,7 +473,7 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio insertSql = "INSERT INTO `cloud`.`user` (id, uuid, username, password, account_id, firstname, lastname, created, state, user.default) " + "VALUES (" + id + ", UUID(), '" + username + "', RAND(), 2, '" + firstname + "','" + lastname + "',now(), 'disabled', 1)"; - txn = Transaction.currentTxn(); + txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql); stmt.executeUpdate(); @@ -499,7 +504,7 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio "VALUES ('default', 'Default Security Group', 2, 1, 'admin')"; } - txn = Transaction.currentTxn(); + txn = TransactionLegacy.currentTxn(); try { stmt = txn.prepareAutoCloseStatement(insertSql); stmt.executeUpdate(); @@ -639,7 +644,7 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio String already = _configDao.getValue("system.vm.password"); if (already == null) { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { String rpassword = PasswordGenerator.generatePresharedKey(8); String wSql = "INSERT INTO `cloud`.`configuration` (category, instance, component, name, value, description) " @@ -723,7 +728,7 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio String insertSql2 = "INSERT INTO `cloud`.`configuration` (category, instance, component, name, value, description) " + "VALUES ('Hidden','DEFAULT', 'management-server','ssh.publickey', '" + DBEncryptionUtil.encrypt(publicKey) + "','Public key for the entire CloudStack')"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt1 = txn.prepareAutoCloseStatement(insertSql1); stmt1.executeUpdate(); @@ -858,7 +863,7 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio String insertSql1 = "INSERT INTO `cloud`.`configuration` (category, instance, component, name, value, description) " + "VALUES ('Hidden','DEFAULT', 'management-server','secstorage.copy.password', '" + DBEncryptionUtil.encrypt(password) + "','Password used to authenticate zone-to-zone template copy requests')"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt1 = txn.prepareAutoCloseStatement(insertSql1); stmt1.executeUpdate(); @@ -886,7 +891,7 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio } @DB - protected HostPodVO createPod(long userId, String podName, long zoneId, String gateway, String cidr, String startIp, String endIp) throws InternalErrorException { + protected HostPodVO createPod(long userId, String podName, final long zoneId, String gateway, String cidr, final String startIp, String endIp) throws InternalErrorException { String[] cidrPair = cidr.split("\\/"); String cidrAddress = cidrPair[0]; int cidrSize = Integer.parseInt(cidrPair[1]); @@ -908,37 +913,35 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio ipRange = ""; } - HostPodVO pod = new HostPodVO(podName, zoneId, gateway, cidrAddress, cidrSize, ipRange); - Transaction txn = Transaction.currentTxn(); + final HostPodVO pod = new HostPodVO(podName, zoneId, gateway, cidrAddress, cidrSize, ipRange); try { - txn.start(); - - if (_podDao.persist(pod) == null) { - txn.rollback(); - throw new InternalErrorException("Failed to create new pod. Please contact Cloud Support."); - } - - if (startIp != null) { - _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), startIp, endIp); - } - - String ipNums = _configDao.getValue("linkLocalIp.nums"); - int nums = Integer.parseInt(ipNums); - if (nums > 16 || nums <= 0) { - throw new InvalidParameterValueException("The linkLocalIp.nums: " + nums + "is wrong, should be 1~16"); - } - /* local link ip address starts from 169.254.0.2 - 169.254.(nums) */ - String[] linkLocalIpRanges = NetUtils.getLinkLocalIPRange(nums); - if (linkLocalIpRanges == null) { - throw new InvalidParameterValueException("The linkLocalIp.nums: " + nums + "may be wrong, should be 1~16"); - } else { - _zoneDao.addLinkLocalIpAddress(zoneId, pod.getId(), linkLocalIpRanges[0], linkLocalIpRanges[1]); - } - - txn.commit(); - + final String endIpFinal = endIp; + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws InternalErrorException { + if (_podDao.persist(pod) == null) { + throw new InternalErrorException("Failed to create new pod. Please contact Cloud Support."); + } + + if (startIp != null) { + _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), startIp, endIpFinal); + } + + String ipNums = _configDao.getValue("linkLocalIp.nums"); + int nums = Integer.parseInt(ipNums); + if (nums > 16 || nums <= 0) { + throw new InvalidParameterValueException("The linkLocalIp.nums: " + nums + "is wrong, should be 1~16"); + } + /* local link ip address starts from 169.254.0.2 - 169.254.(nums) */ + String[] linkLocalIpRanges = NetUtils.getLinkLocalIPRange(nums); + if (linkLocalIpRanges == null) { + throw new InvalidParameterValueException("The linkLocalIp.nums: " + nums + "may be wrong, should be 1~16"); + } else { + _zoneDao.addLinkLocalIpAddress(zoneId, pod.getId(), linkLocalIpRanges[0], linkLocalIpRanges[1]); + } + } + }); } catch (Exception e) { - txn.rollback(); s_logger.error("Unable to create new pod due to " + e.getMessage(), e); throw new InternalErrorException("Failed to create new pod. Please contact Cloud Support."); } @@ -999,20 +1002,20 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio privateGatewayNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(privateGatewayNetworkOffering); //populate providers - Map defaultSharedNetworkOfferingProviders = new HashMap(); + final Map defaultSharedNetworkOfferingProviders = new HashMap(); defaultSharedNetworkOfferingProviders.put(Service.Dhcp, Provider.VirtualRouter); defaultSharedNetworkOfferingProviders.put(Service.Dns, Provider.VirtualRouter); defaultSharedNetworkOfferingProviders.put(Service.UserData, Provider.VirtualRouter); - Map defaultIsolatedNetworkOfferingProviders = defaultSharedNetworkOfferingProviders; + final Map defaultIsolatedNetworkOfferingProviders = defaultSharedNetworkOfferingProviders; - Map defaultSharedSGNetworkOfferingProviders = new HashMap(); + final Map defaultSharedSGNetworkOfferingProviders = new HashMap(); defaultSharedSGNetworkOfferingProviders.put(Service.Dhcp, Provider.VirtualRouter); defaultSharedSGNetworkOfferingProviders.put(Service.Dns, Provider.VirtualRouter); defaultSharedSGNetworkOfferingProviders.put(Service.UserData, Provider.VirtualRouter); defaultSharedSGNetworkOfferingProviders.put(Service.SecurityGroup, Provider.SecurityGroupProvider); - Map defaultIsolatedSourceNatEnabledNetworkOfferingProviders = new HashMap(); + final Map defaultIsolatedSourceNatEnabledNetworkOfferingProviders = new HashMap(); defaultIsolatedSourceNatEnabledNetworkOfferingProviders.put(Service.Dhcp, Provider.VirtualRouter); defaultIsolatedSourceNatEnabledNetworkOfferingProviders.put(Service.Dns, Provider.VirtualRouter); defaultIsolatedSourceNatEnabledNetworkOfferingProviders.put(Service.UserData, Provider.VirtualRouter); @@ -1024,7 +1027,7 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio defaultIsolatedSourceNatEnabledNetworkOfferingProviders.put(Service.PortForwarding, Provider.VirtualRouter); defaultIsolatedSourceNatEnabledNetworkOfferingProviders.put(Service.Vpn, Provider.VirtualRouter); - Map netscalerServiceProviders = new HashMap(); + final Map netscalerServiceProviders = new HashMap(); netscalerServiceProviders.put(Service.Dhcp, Provider.VirtualRouter); netscalerServiceProviders.put(Service.Dns, Provider.VirtualRouter); netscalerServiceProviders.put(Service.UserData, Provider.VirtualRouter); @@ -1034,182 +1037,182 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio // The only one diff between 1 and 2 network offerings is that the first one has SG enabled. In Basic zone only // first network offering has to be enabled, in Advance zone - the second one - Transaction txn = Transaction.currentTxn(); - txn.start(); - - // Offering #1 - NetworkOfferingVO defaultSharedSGNetworkOffering = new NetworkOfferingVO( - NetworkOffering.DefaultSharedNetworkOfferingWithSGService, - "Offering for Shared Security group enabled networks", - TrafficType.Guest, - false, true, null, null, true, Availability.Optional, - null, Network.GuestType.Shared, true, true, false, false, false); - - defaultSharedSGNetworkOffering.setState(NetworkOffering.State.Enabled); - defaultSharedSGNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(defaultSharedSGNetworkOffering); - - for (Service service : defaultSharedSGNetworkOfferingProviders.keySet()) { - NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO(defaultSharedSGNetworkOffering.getId(), service, defaultSharedSGNetworkOfferingProviders.get(service)); - _ntwkOfferingServiceMapDao.persist(offService); - s_logger.trace("Added service for the network offering: " + offService); - } - - // Offering #2 - NetworkOfferingVO defaultSharedNetworkOffering = new NetworkOfferingVO( - NetworkOffering.DefaultSharedNetworkOffering, - "Offering for Shared networks", - TrafficType.Guest, - false, true, null, null, true, Availability.Optional, - null, Network.GuestType.Shared, true, true, false, false, false); - - defaultSharedNetworkOffering.setState(NetworkOffering.State.Enabled); - defaultSharedNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(defaultSharedNetworkOffering); - - for (Service service : defaultSharedNetworkOfferingProviders.keySet()) { - NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO(defaultSharedNetworkOffering.getId(), service, defaultSharedNetworkOfferingProviders.get(service)); - _ntwkOfferingServiceMapDao.persist(offService); - s_logger.trace("Added service for the network offering: " + offService); - } - - // Offering #3 - NetworkOfferingVO defaultIsolatedSourceNatEnabledNetworkOffering = new NetworkOfferingVO( - NetworkOffering.DefaultIsolatedNetworkOfferingWithSourceNatService, - "Offering for Isolated networks with Source Nat service enabled", - TrafficType.Guest, - false, false, null, null, true, Availability.Required, - null, Network.GuestType.Isolated, true, false, false, false, true); - - defaultIsolatedSourceNatEnabledNetworkOffering.setState(NetworkOffering.State.Enabled); - defaultIsolatedSourceNatEnabledNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(defaultIsolatedSourceNatEnabledNetworkOffering); - - for (Service service : defaultIsolatedSourceNatEnabledNetworkOfferingProviders.keySet()) { - NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO - (defaultIsolatedSourceNatEnabledNetworkOffering.getId(), service, defaultIsolatedSourceNatEnabledNetworkOfferingProviders.get(service)); - _ntwkOfferingServiceMapDao.persist(offService); - s_logger.trace("Added service for the network offering: " + offService); - } - - // Offering #4 - NetworkOfferingVO defaultIsolatedEnabledNetworkOffering = new NetworkOfferingVO( - NetworkOffering.DefaultIsolatedNetworkOffering, - "Offering for Isolated networks with no Source Nat service", - TrafficType.Guest, - false, true, null, null, true, Availability.Optional, - null, Network.GuestType.Isolated, true, true, false, false, false); - - defaultIsolatedEnabledNetworkOffering.setState(NetworkOffering.State.Enabled); - defaultIsolatedEnabledNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(defaultIsolatedEnabledNetworkOffering); - - for (Service service : defaultIsolatedNetworkOfferingProviders.keySet()) { - NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO(defaultIsolatedEnabledNetworkOffering.getId(), service, defaultIsolatedNetworkOfferingProviders.get(service)); - _ntwkOfferingServiceMapDao.persist(offService); - s_logger.trace("Added service for the network offering: " + offService); - } - - // Offering #5 - NetworkOfferingVO defaultNetscalerNetworkOffering = new NetworkOfferingVO( - NetworkOffering.DefaultSharedEIPandELBNetworkOffering, - "Offering for Shared networks with Elastic IP and Elastic LB capabilities", - TrafficType.Guest, - false, true, null, null, true, Availability.Optional, - null, Network.GuestType.Shared, true, false, false, false, true, true, true, false, false, true, true, false, false); - - defaultNetscalerNetworkOffering.setState(NetworkOffering.State.Enabled); - defaultNetscalerNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(defaultNetscalerNetworkOffering); - - for (Service service : netscalerServiceProviders.keySet()) { - NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO(defaultNetscalerNetworkOffering.getId(), service, netscalerServiceProviders.get(service)); - _ntwkOfferingServiceMapDao.persist(offService); - s_logger.trace("Added service for the network offering: " + offService); - } - - // Offering #6 - NetworkOfferingVO defaultNetworkOfferingForVpcNetworks = new NetworkOfferingVO( - NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworks, - "Offering for Isolated Vpc networks with Source Nat service enabled", - TrafficType.Guest, - false, false, null, null, true, Availability.Optional, - null, Network.GuestType.Isolated, false, false, false, false, true); - - defaultNetworkOfferingForVpcNetworks.setState(NetworkOffering.State.Enabled); - defaultNetworkOfferingForVpcNetworks = _networkOfferingDao.persistDefaultNetworkOffering(defaultNetworkOfferingForVpcNetworks); - - Map defaultVpcNetworkOfferingProviders = new HashMap(); - defaultVpcNetworkOfferingProviders.put(Service.Dhcp, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProviders.put(Service.Dns, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProviders.put(Service.UserData, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProviders.put(Service.NetworkACL, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProviders.put(Service.Gateway, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProviders.put(Service.Lb, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProviders.put(Service.SourceNat, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProviders.put(Service.StaticNat, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProviders.put(Service.PortForwarding, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProviders.put(Service.Vpn, Provider.VPCVirtualRouter); - - for (Service service : defaultVpcNetworkOfferingProviders.keySet()) { - NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO - (defaultNetworkOfferingForVpcNetworks.getId(), service, defaultVpcNetworkOfferingProviders.get(service)); - _ntwkOfferingServiceMapDao.persist(offService); - s_logger.trace("Added service for the network offering: " + offService); - } - - // Offering #7 - NetworkOfferingVO defaultNetworkOfferingForVpcNetworksNoLB = new NetworkOfferingVO( - NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksNoLB, - "Offering for Isolated Vpc networks with Source Nat service enabled and LB service Disabled", - TrafficType.Guest, - false, false, null, null, true, Availability.Optional, - null, Network.GuestType.Isolated, false, false, false, false, false); - - defaultNetworkOfferingForVpcNetworksNoLB.setState(NetworkOffering.State.Enabled); - defaultNetworkOfferingForVpcNetworksNoLB = _networkOfferingDao.persistDefaultNetworkOffering(defaultNetworkOfferingForVpcNetworksNoLB); - - Map defaultVpcNetworkOfferingProvidersNoLB = new HashMap(); - defaultVpcNetworkOfferingProvidersNoLB.put(Service.Dhcp, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProvidersNoLB.put(Service.Dns, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProvidersNoLB.put(Service.UserData, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProvidersNoLB.put(Service.NetworkACL, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProvidersNoLB.put(Service.Gateway, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProvidersNoLB.put(Service.SourceNat, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProvidersNoLB.put(Service.StaticNat, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProvidersNoLB.put(Service.PortForwarding, Provider.VPCVirtualRouter); - defaultVpcNetworkOfferingProvidersNoLB.put(Service.Vpn, Provider.VPCVirtualRouter); - - for (Service service : defaultVpcNetworkOfferingProvidersNoLB.keySet()) { - NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO - (defaultNetworkOfferingForVpcNetworksNoLB.getId(), service, defaultVpcNetworkOfferingProvidersNoLB.get(service)); - _ntwkOfferingServiceMapDao.persist(offService); - s_logger.trace("Added service for the network offering: " + offService); - } + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + // Offering #1 + NetworkOfferingVO defaultSharedSGNetworkOffering = new NetworkOfferingVO( + NetworkOffering.DefaultSharedNetworkOfferingWithSGService, + "Offering for Shared Security group enabled networks", + TrafficType.Guest, + false, true, null, null, true, Availability.Optional, + null, Network.GuestType.Shared, true, true, false, false, false); - //offering #8 - network offering with internal lb service - NetworkOfferingVO internalLbOff = new NetworkOfferingVO( - NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksWithInternalLB, - "Offering for Isolated Vpc networks with Internal LB support", - TrafficType.Guest, - false, false, null, null, true, Availability.Optional, - null, Network.GuestType.Isolated, false, false, false, true, false); - - internalLbOff.setState(NetworkOffering.State.Enabled); - internalLbOff = _networkOfferingDao.persistDefaultNetworkOffering(internalLbOff); - - Map internalLbOffProviders = new HashMap(); - internalLbOffProviders.put(Service.Dhcp, Provider.VPCVirtualRouter); - internalLbOffProviders.put(Service.Dns, Provider.VPCVirtualRouter); - internalLbOffProviders.put(Service.UserData, Provider.VPCVirtualRouter); - internalLbOffProviders.put(Service.NetworkACL, Provider.VPCVirtualRouter); - internalLbOffProviders.put(Service.Gateway, Provider.VPCVirtualRouter); - internalLbOffProviders.put(Service.Lb, Provider.InternalLbVm); - internalLbOffProviders.put(Service.SourceNat, Provider.VPCVirtualRouter); - - for (Service service : internalLbOffProviders.keySet()) { - NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO - (internalLbOff.getId(), service, internalLbOffProviders.get(service)); - _ntwkOfferingServiceMapDao.persist(offService); - s_logger.trace("Added service for the network offering: " + offService); - } - - txn.commit(); + defaultSharedSGNetworkOffering.setState(NetworkOffering.State.Enabled); + defaultSharedSGNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(defaultSharedSGNetworkOffering); + + for (Service service : defaultSharedSGNetworkOfferingProviders.keySet()) { + NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO(defaultSharedSGNetworkOffering.getId(), service, defaultSharedSGNetworkOfferingProviders.get(service)); + _ntwkOfferingServiceMapDao.persist(offService); + s_logger.trace("Added service for the network offering: " + offService); + } + + // Offering #2 + NetworkOfferingVO defaultSharedNetworkOffering = new NetworkOfferingVO( + NetworkOffering.DefaultSharedNetworkOffering, + "Offering for Shared networks", + TrafficType.Guest, + false, true, null, null, true, Availability.Optional, + null, Network.GuestType.Shared, true, true, false, false, false); + + defaultSharedNetworkOffering.setState(NetworkOffering.State.Enabled); + defaultSharedNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(defaultSharedNetworkOffering); + + for (Service service : defaultSharedNetworkOfferingProviders.keySet()) { + NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO(defaultSharedNetworkOffering.getId(), service, defaultSharedNetworkOfferingProviders.get(service)); + _ntwkOfferingServiceMapDao.persist(offService); + s_logger.trace("Added service for the network offering: " + offService); + } + + // Offering #3 + NetworkOfferingVO defaultIsolatedSourceNatEnabledNetworkOffering = new NetworkOfferingVO( + NetworkOffering.DefaultIsolatedNetworkOfferingWithSourceNatService, + "Offering for Isolated networks with Source Nat service enabled", + TrafficType.Guest, + false, false, null, null, true, Availability.Required, + null, Network.GuestType.Isolated, true, false, false, false, true); + + defaultIsolatedSourceNatEnabledNetworkOffering.setState(NetworkOffering.State.Enabled); + defaultIsolatedSourceNatEnabledNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(defaultIsolatedSourceNatEnabledNetworkOffering); + + for (Service service : defaultIsolatedSourceNatEnabledNetworkOfferingProviders.keySet()) { + NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO + (defaultIsolatedSourceNatEnabledNetworkOffering.getId(), service, defaultIsolatedSourceNatEnabledNetworkOfferingProviders.get(service)); + _ntwkOfferingServiceMapDao.persist(offService); + s_logger.trace("Added service for the network offering: " + offService); + } + + // Offering #4 + NetworkOfferingVO defaultIsolatedEnabledNetworkOffering = new NetworkOfferingVO( + NetworkOffering.DefaultIsolatedNetworkOffering, + "Offering for Isolated networks with no Source Nat service", + TrafficType.Guest, + false, true, null, null, true, Availability.Optional, + null, Network.GuestType.Isolated, true, true, false, false, false); + + defaultIsolatedEnabledNetworkOffering.setState(NetworkOffering.State.Enabled); + defaultIsolatedEnabledNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(defaultIsolatedEnabledNetworkOffering); + + for (Service service : defaultIsolatedNetworkOfferingProviders.keySet()) { + NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO(defaultIsolatedEnabledNetworkOffering.getId(), service, defaultIsolatedNetworkOfferingProviders.get(service)); + _ntwkOfferingServiceMapDao.persist(offService); + s_logger.trace("Added service for the network offering: " + offService); + } + + // Offering #5 + NetworkOfferingVO defaultNetscalerNetworkOffering = new NetworkOfferingVO( + NetworkOffering.DefaultSharedEIPandELBNetworkOffering, + "Offering for Shared networks with Elastic IP and Elastic LB capabilities", + TrafficType.Guest, + false, true, null, null, true, Availability.Optional, + null, Network.GuestType.Shared, true, false, false, false, true, true, true, false, false, true, true, false, false); + + defaultNetscalerNetworkOffering.setState(NetworkOffering.State.Enabled); + defaultNetscalerNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(defaultNetscalerNetworkOffering); + + for (Service service : netscalerServiceProviders.keySet()) { + NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO(defaultNetscalerNetworkOffering.getId(), service, netscalerServiceProviders.get(service)); + _ntwkOfferingServiceMapDao.persist(offService); + s_logger.trace("Added service for the network offering: " + offService); + } + + // Offering #6 + NetworkOfferingVO defaultNetworkOfferingForVpcNetworks = new NetworkOfferingVO( + NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworks, + "Offering for Isolated Vpc networks with Source Nat service enabled", + TrafficType.Guest, + false, false, null, null, true, Availability.Optional, + null, Network.GuestType.Isolated, false, false, false, false, true); + + defaultNetworkOfferingForVpcNetworks.setState(NetworkOffering.State.Enabled); + defaultNetworkOfferingForVpcNetworks = _networkOfferingDao.persistDefaultNetworkOffering(defaultNetworkOfferingForVpcNetworks); + + Map defaultVpcNetworkOfferingProviders = new HashMap(); + defaultVpcNetworkOfferingProviders.put(Service.Dhcp, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProviders.put(Service.Dns, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProviders.put(Service.UserData, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProviders.put(Service.NetworkACL, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProviders.put(Service.Gateway, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProviders.put(Service.Lb, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProviders.put(Service.SourceNat, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProviders.put(Service.StaticNat, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProviders.put(Service.PortForwarding, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProviders.put(Service.Vpn, Provider.VPCVirtualRouter); + + for (Service service : defaultVpcNetworkOfferingProviders.keySet()) { + NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO + (defaultNetworkOfferingForVpcNetworks.getId(), service, defaultVpcNetworkOfferingProviders.get(service)); + _ntwkOfferingServiceMapDao.persist(offService); + s_logger.trace("Added service for the network offering: " + offService); + } + + // Offering #7 + NetworkOfferingVO defaultNetworkOfferingForVpcNetworksNoLB = new NetworkOfferingVO( + NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksNoLB, + "Offering for Isolated Vpc networks with Source Nat service enabled and LB service Disabled", + TrafficType.Guest, + false, false, null, null, true, Availability.Optional, + null, Network.GuestType.Isolated, false, false, false, false, false); + + defaultNetworkOfferingForVpcNetworksNoLB.setState(NetworkOffering.State.Enabled); + defaultNetworkOfferingForVpcNetworksNoLB = _networkOfferingDao.persistDefaultNetworkOffering(defaultNetworkOfferingForVpcNetworksNoLB); + + Map defaultVpcNetworkOfferingProvidersNoLB = new HashMap(); + defaultVpcNetworkOfferingProvidersNoLB.put(Service.Dhcp, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProvidersNoLB.put(Service.Dns, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProvidersNoLB.put(Service.UserData, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProvidersNoLB.put(Service.NetworkACL, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProvidersNoLB.put(Service.Gateway, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProvidersNoLB.put(Service.SourceNat, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProvidersNoLB.put(Service.StaticNat, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProvidersNoLB.put(Service.PortForwarding, Provider.VPCVirtualRouter); + defaultVpcNetworkOfferingProvidersNoLB.put(Service.Vpn, Provider.VPCVirtualRouter); + + for (Service service : defaultVpcNetworkOfferingProvidersNoLB.keySet()) { + NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO + (defaultNetworkOfferingForVpcNetworksNoLB.getId(), service, defaultVpcNetworkOfferingProvidersNoLB.get(service)); + _ntwkOfferingServiceMapDao.persist(offService); + s_logger.trace("Added service for the network offering: " + offService); + } + + //offering #8 - network offering with internal lb service + NetworkOfferingVO internalLbOff = new NetworkOfferingVO( + NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksWithInternalLB, + "Offering for Isolated Vpc networks with Internal LB support", + TrafficType.Guest, + false, false, null, null, true, Availability.Optional, + null, Network.GuestType.Isolated, false, false, false, true, false); + + internalLbOff.setState(NetworkOffering.State.Enabled); + internalLbOff = _networkOfferingDao.persistDefaultNetworkOffering(internalLbOff); + + Map internalLbOffProviders = new HashMap(); + internalLbOffProviders.put(Service.Dhcp, Provider.VPCVirtualRouter); + internalLbOffProviders.put(Service.Dns, Provider.VPCVirtualRouter); + internalLbOffProviders.put(Service.UserData, Provider.VPCVirtualRouter); + internalLbOffProviders.put(Service.NetworkACL, Provider.VPCVirtualRouter); + internalLbOffProviders.put(Service.Gateway, Provider.VPCVirtualRouter); + internalLbOffProviders.put(Service.Lb, Provider.InternalLbVm); + internalLbOffProviders.put(Service.SourceNat, Provider.VPCVirtualRouter); + + for (Service service : internalLbOffProviders.keySet()) { + NetworkOfferingServiceMapVO offService = new NetworkOfferingServiceMapVO + (internalLbOff.getId(), service, internalLbOffProviders.get(service)); + _ntwkOfferingServiceMapDao.persist(offService); + s_logger.trace("Added service for the network offering: " + offService); + } + } + }); } private void createDefaultNetworks() { @@ -1323,8 +1326,8 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio List domainResourceCount = _resourceCountDao.listResourceCountByOwnerType(ResourceOwnerType.Domain); List accountResourceCount = _resourceCountDao.listResourceCountByOwnerType(ResourceOwnerType.Account); - List accountSupportedResourceTypes = new ArrayList(); - List domainSupportedResourceTypes = new ArrayList(); + final List accountSupportedResourceTypes = new ArrayList(); + final List domainSupportedResourceTypes = new ArrayList(); for (ResourceType resourceType : resourceTypes) { if (resourceType.supportsOwner(ResourceOwnerType.Account)) { @@ -1335,59 +1338,63 @@ public class ConfigurationServerImpl extends ManagerBase implements Configuratio } } - int accountExpectedCount = accountSupportedResourceTypes.size(); - int domainExpectedCount = domainSupportedResourceTypes.size(); + final int accountExpectedCount = accountSupportedResourceTypes.size(); + final int domainExpectedCount = domainSupportedResourceTypes.size(); if ((domainResourceCount.size() < domainExpectedCount * domains.size())) { s_logger.debug("resource_count table has records missing for some domains...going to insert them"); - for (DomainVO domain : domains) { + for (final DomainVO domain : domains) { // Lock domain - Transaction txn = Transaction.currentTxn(); - txn.start(); - _domainDao.lockRow(domain.getId(), true); - List domainCounts = _resourceCountDao.listByOwnerId(domain.getId(), ResourceOwnerType.Domain); - List domainCountStr = new ArrayList(); - for (ResourceCountVO domainCount : domainCounts) { - domainCountStr.add(domainCount.getType().toString()); - } - - if (domainCountStr.size() < domainExpectedCount) { - for (ResourceType resourceType : domainSupportedResourceTypes) { - if (!domainCountStr.contains(resourceType.toString())) { - ResourceCountVO resourceCountVO = new ResourceCountVO(resourceType, 0, domain.getId(), ResourceOwnerType.Domain); - s_logger.debug("Inserting resource count of type " + resourceType + " for domain id=" + domain.getId()); - _resourceCountDao.persist(resourceCountVO); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _domainDao.lockRow(domain.getId(), true); + List domainCounts = _resourceCountDao.listByOwnerId(domain.getId(), ResourceOwnerType.Domain); + List domainCountStr = new ArrayList(); + for (ResourceCountVO domainCount : domainCounts) { + domainCountStr.add(domainCount.getType().toString()); + } + + if (domainCountStr.size() < domainExpectedCount) { + for (ResourceType resourceType : domainSupportedResourceTypes) { + if (!domainCountStr.contains(resourceType.toString())) { + ResourceCountVO resourceCountVO = new ResourceCountVO(resourceType, 0, domain.getId(), ResourceOwnerType.Domain); + s_logger.debug("Inserting resource count of type " + resourceType + " for domain id=" + domain.getId()); + _resourceCountDao.persist(resourceCountVO); + } + } } } - } - txn.commit(); + }); + } } if ((accountResourceCount.size() < accountExpectedCount * accounts.size())) { s_logger.debug("resource_count table has records missing for some accounts...going to insert them"); - for (AccountVO account : accounts) { + for (final AccountVO account : accounts) { // lock account - Transaction txn = Transaction.currentTxn(); - txn.start(); - _accountDao.lockRow(account.getId(), true); - List accountCounts = _resourceCountDao.listByOwnerId(account.getId(), ResourceOwnerType.Account); - List accountCountStr = new ArrayList(); - for (ResourceCountVO accountCount : accountCounts) { - accountCountStr.add(accountCount.getType().toString()); - } - - if (accountCountStr.size() < accountExpectedCount) { - for (ResourceType resourceType : accountSupportedResourceTypes) { - if (!accountCountStr.contains(resourceType.toString())) { - ResourceCountVO resourceCountVO = new ResourceCountVO(resourceType, 0, account.getId(), ResourceOwnerType.Account); - s_logger.debug("Inserting resource count of type " + resourceType + " for account id=" + account.getId()); - _resourceCountDao.persist(resourceCountVO); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + _accountDao.lockRow(account.getId(), true); + List accountCounts = _resourceCountDao.listByOwnerId(account.getId(), ResourceOwnerType.Account); + List accountCountStr = new ArrayList(); + for (ResourceCountVO accountCount : accountCounts) { + accountCountStr.add(accountCount.getType().toString()); + } + + if (accountCountStr.size() < accountExpectedCount) { + for (ResourceType resourceType : accountSupportedResourceTypes) { + if (!accountCountStr.contains(resourceType.toString())) { + ResourceCountVO resourceCountVO = new ResourceCountVO(resourceType, 0, account.getId(), ResourceOwnerType.Account); + s_logger.debug("Inserting resource count of type " + resourceType + " for account id=" + account.getId()); + _resourceCountDao.persist(resourceCountVO); + } + } } } - } - - txn.commit(); + }); } } } diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 4637da6b2b4..bce6b45ccf5 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -565,6 +565,9 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.Filter; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.JoinBuilder; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.db.JoinBuilder.JoinType; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; @@ -2166,12 +2169,12 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe @Override @DB public DomainVO updateDomain(UpdateDomainCmd cmd) { - Long domainId = cmd.getId(); - String domainName = cmd.getDomainName(); - String networkDomain = cmd.getNetworkDomain(); + final Long domainId = cmd.getId(); + final String domainName = cmd.getDomainName(); + final String networkDomain = cmd.getNetworkDomain(); // check if domain exists in the system - DomainVO domain = _domainDao.findById(domainId); + final DomainVO domain = _domainDao.findById(domainId); if (domain == null) { InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find domain with specified domain id"); ex.addProxyObject(domainId.toString(), "domainId"); @@ -2212,27 +2215,26 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe } } - Transaction txn = Transaction.currentTxn(); - - txn.start(); - - if (domainName != null) { - String updatedDomainPath = getUpdatedDomainPath(domain.getPath(), domainName); - updateDomainChildren(domain, updatedDomainPath); - domain.setName(domainName); - domain.setPath(updatedDomainPath); - } - - if (networkDomain != null) { - if (networkDomain.isEmpty()) { - domain.setNetworkDomain(null); - } else { - domain.setNetworkDomain(networkDomain); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + if (domainName != null) { + String updatedDomainPath = getUpdatedDomainPath(domain.getPath(), domainName); + updateDomainChildren(domain, updatedDomainPath); + domain.setName(domainName); + domain.setPath(updatedDomainPath); + } + + if (networkDomain != null) { + if (networkDomain.isEmpty()) { + domain.setNetworkDomain(null); + } else { + domain.setNetworkDomain(networkDomain); + } + } + _domainDao.update(domainId, domain); } - } - _domainDao.update(domainId, domain); - - txn.commit(); + }); return _domainDao.findById(domainId); @@ -3651,7 +3653,7 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe @Override @DB - public boolean updateHostPassword(UpdateHostPasswordCmd cmd) { + public boolean updateHostPassword(final UpdateHostPasswordCmd cmd) { if (cmd.getClusterId() == null && cmd.getHostId() == null) { throw new InvalidParameterValueException("You should provide one of cluster id or a host id."); } else if (cmd.getClusterId() == null) { @@ -3668,35 +3670,30 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe throw new InvalidParameterValueException("This operation is not supported for this hypervisor type"); } // get all the hosts in this cluster - List hosts = _resourceMgr.listAllHostsInCluster(cmd.getClusterId()); - Transaction txn = Transaction.currentTxn(); - try { - txn.start(); - for (HostVO h : hosts) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Changing password for host name = " + h.getName()); - } - // update password for this host - DetailVO nv = _detailsDao.findDetail(h.getId(), ApiConstants.USERNAME); - if (nv.getValue().equals(cmd.getUsername())) { - DetailVO nvp = _detailsDao.findDetail(h.getId(), ApiConstants.PASSWORD); - nvp.setValue(DBEncryptionUtil.encrypt(cmd.getPassword())); - _detailsDao.persist(nvp); - } else { - // if one host in the cluster has diff username then - // rollback to maintain consistency - txn.rollback(); - throw new InvalidParameterValueException( - "The username is not same for all hosts, please modify passwords for individual hosts."); + final List hosts = _resourceMgr.listAllHostsInCluster(cmd.getClusterId()); + + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (HostVO h : hosts) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Changing password for host name = " + h.getName()); + } + // update password for this host + DetailVO nv = _detailsDao.findDetail(h.getId(), ApiConstants.USERNAME); + if (nv.getValue().equals(cmd.getUsername())) { + DetailVO nvp = _detailsDao.findDetail(h.getId(), ApiConstants.PASSWORD); + nvp.setValue(DBEncryptionUtil.encrypt(cmd.getPassword())); + _detailsDao.persist(nvp); + } else { + // if one host in the cluster has diff username then + // rollback to maintain consistency + throw new InvalidParameterValueException( + "The username is not same for all hosts, please modify passwords for individual hosts."); + } } } - txn.commit(); - // if hypervisor is xenserver then we update it in - // CitrixResourceBase - } catch (Exception e) { - txn.rollback(); - throw new CloudRuntimeException("Failed to update password " + e.getMessage()); - } + }); } return true; diff --git a/server/src/com/cloud/server/StatsCollector.java b/server/src/com/cloud/server/StatsCollector.java index b4ec9155faf..699c3c0f55c 100755 --- a/server/src/com/cloud/server/StatsCollector.java +++ b/server/src/com/cloud/server/StatsCollector.java @@ -85,6 +85,8 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.net.MacAddress; import com.cloud.vm.UserVmManager; import com.cloud.vm.UserVmVO; @@ -365,29 +367,29 @@ public class StatsCollector extends ManagerBase implements ComponentMethodInterc scanLock.unlock(); return; } - Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { - txn.start(); - //get all stats with delta > 0 - List updatedVmNetStats = _vmDiskStatsDao.listUpdatedStats(); - for(VmDiskStatisticsVO stat : updatedVmNetStats){ - if (_dailyOrHourly) { - //update agg bytes - stat.setAggBytesRead(stat.getCurrentBytesRead() + stat.getNetBytesRead()); - stat.setAggBytesWrite(stat.getCurrentBytesWrite() + stat.getNetBytesWrite()); - stat.setAggIORead(stat.getCurrentIORead() + stat.getNetIORead()); - stat.setAggIOWrite(stat.getCurrentIOWrite() + stat.getNetIOWrite()); - _vmDiskStatsDao.update(stat.getId(), stat); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + //get all stats with delta > 0 + List updatedVmNetStats = _vmDiskStatsDao.listUpdatedStats(); + for(VmDiskStatisticsVO stat : updatedVmNetStats){ + if (_dailyOrHourly) { + //update agg bytes + stat.setAggBytesRead(stat.getCurrentBytesRead() + stat.getNetBytesRead()); + stat.setAggBytesWrite(stat.getCurrentBytesWrite() + stat.getNetBytesWrite()); + stat.setAggIORead(stat.getCurrentIORead() + stat.getNetIORead()); + stat.setAggIOWrite(stat.getCurrentIOWrite() + stat.getNetIOWrite()); + _vmDiskStatsDao.update(stat.getId(), stat); + } + } + s_logger.debug("Successfully updated aggregate vm disk stats"); } - } - s_logger.debug("Successfully updated aggregate vm disk stats"); - txn.commit(); + }); } catch (Exception e){ - txn.rollback(); s_logger.debug("Failed to update aggregate disk stats", e); } finally { scanLock.unlock(); - txn.close(); } } } catch (Exception e){ @@ -402,122 +404,121 @@ public class StatsCollector extends ManagerBase implements ComponentMethodInterc @Override protected void runInContext() { // collect the vm disk statistics(total) from hypervisor. added by weizhou, 2013.03. - Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { - txn.start(); - SearchCriteria sc = _hostDao.createSearchCriteria(); - sc.addAnd("status", SearchCriteria.Op.EQ, Status.Up.toString()); - sc.addAnd("resourceState", SearchCriteria.Op.NIN, ResourceState.Maintenance, ResourceState.PrepareForMaintenance, ResourceState.ErrorInMaintenance); - sc.addAnd("type", SearchCriteria.Op.EQ, Host.Type.Routing.toString()); - sc.addAnd("hypervisorType", SearchCriteria.Op.EQ, HypervisorType.KVM); // support KVM only util 2013.06.25 - List hosts = _hostDao.search(sc, null); - - for (HostVO host : hosts) { - List vms = _userVmDao.listRunningByHostId(host.getId()); - List vmIds = new ArrayList(); - - for (UserVmVO vm : vms) { - if (vm.getType() == VirtualMachine.Type.User) // user vm - vmIds.add(vm.getId()); - } - - HashMap> vmDiskStatsById = _userVmMgr.getVmDiskStatistics(host.getId(), host.getName(), vmIds); - if (vmDiskStatsById == null) - continue; - - Set vmIdSet = vmDiskStatsById.keySet(); - for(Long vmId : vmIdSet) - { - List vmDiskStats = vmDiskStatsById.get(vmId); - if (vmDiskStats == null) + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + SearchCriteria sc = _hostDao.createSearchCriteria(); + sc.addAnd("status", SearchCriteria.Op.EQ, Status.Up.toString()); + sc.addAnd("resourceState", SearchCriteria.Op.NIN, ResourceState.Maintenance, ResourceState.PrepareForMaintenance, ResourceState.ErrorInMaintenance); + sc.addAnd("type", SearchCriteria.Op.EQ, Host.Type.Routing.toString()); + sc.addAnd("hypervisorType", SearchCriteria.Op.EQ, HypervisorType.KVM); // support KVM only util 2013.06.25 + List hosts = _hostDao.search(sc, null); + + for (HostVO host : hosts) { + List vms = _userVmDao.listRunningByHostId(host.getId()); + List vmIds = new ArrayList(); + + for (UserVmVO vm : vms) { + if (vm.getType() == VirtualMachine.Type.User) // user vm + vmIds.add(vm.getId()); + } + + HashMap> vmDiskStatsById = _userVmMgr.getVmDiskStatistics(host.getId(), host.getName(), vmIds); + if (vmDiskStatsById == null) continue; - UserVmVO userVm = _userVmDao.findById(vmId); - for (VmDiskStatsEntry vmDiskStat:vmDiskStats) { - SearchCriteria sc_volume = _volsDao.createSearchCriteria(); - sc_volume.addAnd("path", SearchCriteria.Op.EQ, vmDiskStat.getPath()); - VolumeVO volume = _volsDao.search(sc_volume, null).get(0); - VmDiskStatisticsVO previousVmDiskStats = _vmDiskStatsDao.findBy(userVm.getAccountId(), userVm.getDataCenterId(), vmId, volume.getId()); - VmDiskStatisticsVO vmDiskStat_lock = _vmDiskStatsDao.lock(userVm.getAccountId(), userVm.getDataCenterId(), vmId, volume.getId()); - - if ((vmDiskStat.getBytesRead() == 0) && (vmDiskStat.getBytesWrite() == 0) - && (vmDiskStat.getIORead() == 0) && (vmDiskStat.getIOWrite() == 0)) { - s_logger.debug("IO/bytes read and write are all 0. Not updating vm_disk_statistics"); - continue; - } - - if (vmDiskStat_lock == null) { - s_logger.warn("unable to find vm disk stats from host for account: " + userVm.getAccountId() + " with vmId: " + userVm.getId()+ " and volumeId:" + volume.getId()); - continue; - } - - if (previousVmDiskStats != null - && ((previousVmDiskStats.getCurrentBytesRead() != vmDiskStat_lock.getCurrentBytesRead()) - || (previousVmDiskStats.getCurrentBytesWrite() != vmDiskStat_lock.getCurrentBytesWrite()) - || (previousVmDiskStats.getCurrentIORead() != vmDiskStat_lock.getCurrentIORead()) - || (previousVmDiskStats.getCurrentIOWrite() != vmDiskStat_lock.getCurrentIOWrite()))) { - s_logger.debug("vm disk stats changed from the time GetVmDiskStatsCommand was sent. " + - "Ignoring current answer. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + - " Read(Bytes): " + vmDiskStat.getBytesRead() + " write(Bytes): " + vmDiskStat.getBytesWrite() + - " Read(IO): " + vmDiskStat.getIORead() + " write(IO): " + vmDiskStat.getIOWrite()); - continue; - } - - if (vmDiskStat_lock.getCurrentBytesRead() > vmDiskStat.getBytesRead()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Read # of bytes that's less than the last one. " + - "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + - " Reported: " + vmDiskStat.getBytesRead() + " Stored: " + vmDiskStat_lock.getCurrentBytesRead()); + + Set vmIdSet = vmDiskStatsById.keySet(); + for(Long vmId : vmIdSet) + { + List vmDiskStats = vmDiskStatsById.get(vmId); + if (vmDiskStats == null) + continue; + UserVmVO userVm = _userVmDao.findById(vmId); + for (VmDiskStatsEntry vmDiskStat:vmDiskStats) { + SearchCriteria sc_volume = _volsDao.createSearchCriteria(); + sc_volume.addAnd("path", SearchCriteria.Op.EQ, vmDiskStat.getPath()); + VolumeVO volume = _volsDao.search(sc_volume, null).get(0); + VmDiskStatisticsVO previousVmDiskStats = _vmDiskStatsDao.findBy(userVm.getAccountId(), userVm.getDataCenterId(), vmId, volume.getId()); + VmDiskStatisticsVO vmDiskStat_lock = _vmDiskStatsDao.lock(userVm.getAccountId(), userVm.getDataCenterId(), vmId, volume.getId()); + + if ((vmDiskStat.getBytesRead() == 0) && (vmDiskStat.getBytesWrite() == 0) + && (vmDiskStat.getIORead() == 0) && (vmDiskStat.getIOWrite() == 0)) { + s_logger.debug("IO/bytes read and write are all 0. Not updating vm_disk_statistics"); + continue; + } + + if (vmDiskStat_lock == null) { + s_logger.warn("unable to find vm disk stats from host for account: " + userVm.getAccountId() + " with vmId: " + userVm.getId()+ " and volumeId:" + volume.getId()); + continue; + } + + if (previousVmDiskStats != null + && ((previousVmDiskStats.getCurrentBytesRead() != vmDiskStat_lock.getCurrentBytesRead()) + || (previousVmDiskStats.getCurrentBytesWrite() != vmDiskStat_lock.getCurrentBytesWrite()) + || (previousVmDiskStats.getCurrentIORead() != vmDiskStat_lock.getCurrentIORead()) + || (previousVmDiskStats.getCurrentIOWrite() != vmDiskStat_lock.getCurrentIOWrite()))) { + s_logger.debug("vm disk stats changed from the time GetVmDiskStatsCommand was sent. " + + "Ignoring current answer. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + + " Read(Bytes): " + vmDiskStat.getBytesRead() + " write(Bytes): " + vmDiskStat.getBytesWrite() + + " Read(IO): " + vmDiskStat.getIORead() + " write(IO): " + vmDiskStat.getIOWrite()); + continue; + } + + if (vmDiskStat_lock.getCurrentBytesRead() > vmDiskStat.getBytesRead()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Read # of bytes that's less than the last one. " + + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + + " Reported: " + vmDiskStat.getBytesRead() + " Stored: " + vmDiskStat_lock.getCurrentBytesRead()); + } + vmDiskStat_lock.setNetBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead()); + } + vmDiskStat_lock.setCurrentBytesRead(vmDiskStat.getBytesRead()); + if (vmDiskStat_lock.getCurrentBytesWrite() > vmDiskStat.getBytesWrite()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Write # of bytes that's less than the last one. " + + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + + " Reported: " + vmDiskStat.getBytesWrite() + " Stored: " + vmDiskStat_lock.getCurrentBytesWrite()); + } + vmDiskStat_lock.setNetBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite()); + } + vmDiskStat_lock.setCurrentBytesWrite(vmDiskStat.getBytesWrite()); + if (vmDiskStat_lock.getCurrentIORead() > vmDiskStat.getIORead()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Read # of IO that's less than the last one. " + + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + + " Reported: " + vmDiskStat.getIORead() + " Stored: " + vmDiskStat_lock.getCurrentIORead()); + } + vmDiskStat_lock.setNetIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead()); + } + vmDiskStat_lock.setCurrentIORead(vmDiskStat.getIORead()); + if (vmDiskStat_lock.getCurrentIOWrite() > vmDiskStat.getIOWrite()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Write # of IO that's less than the last one. " + + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + + " Reported: " + vmDiskStat.getIOWrite() + " Stored: " + vmDiskStat_lock.getCurrentIOWrite()); + } + vmDiskStat_lock.setNetIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite()); + } + vmDiskStat_lock.setCurrentIOWrite(vmDiskStat.getIOWrite()); + + if (! _dailyOrHourly) { + //update agg bytes + vmDiskStat_lock.setAggBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite()); + vmDiskStat_lock.setAggBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead()); + vmDiskStat_lock.setAggIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite()); + vmDiskStat_lock.setAggIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead()); + } + + _vmDiskStatsDao.update(vmDiskStat_lock.getId(), vmDiskStat_lock); } - vmDiskStat_lock.setNetBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead()); } - vmDiskStat_lock.setCurrentBytesRead(vmDiskStat.getBytesRead()); - if (vmDiskStat_lock.getCurrentBytesWrite() > vmDiskStat.getBytesWrite()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Write # of bytes that's less than the last one. " + - "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + - " Reported: " + vmDiskStat.getBytesWrite() + " Stored: " + vmDiskStat_lock.getCurrentBytesWrite()); - } - vmDiskStat_lock.setNetBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite()); - } - vmDiskStat_lock.setCurrentBytesWrite(vmDiskStat.getBytesWrite()); - if (vmDiskStat_lock.getCurrentIORead() > vmDiskStat.getIORead()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Read # of IO that's less than the last one. " + - "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + - " Reported: " + vmDiskStat.getIORead() + " Stored: " + vmDiskStat_lock.getCurrentIORead()); - } - vmDiskStat_lock.setNetIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead()); - } - vmDiskStat_lock.setCurrentIORead(vmDiskStat.getIORead()); - if (vmDiskStat_lock.getCurrentIOWrite() > vmDiskStat.getIOWrite()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Write # of IO that's less than the last one. " + - "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + - " Reported: " + vmDiskStat.getIOWrite() + " Stored: " + vmDiskStat_lock.getCurrentIOWrite()); - } - vmDiskStat_lock.setNetIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite()); - } - vmDiskStat_lock.setCurrentIOWrite(vmDiskStat.getIOWrite()); - - if (! _dailyOrHourly) { - //update agg bytes - vmDiskStat_lock.setAggBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite()); - vmDiskStat_lock.setAggBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead()); - vmDiskStat_lock.setAggIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite()); - vmDiskStat_lock.setAggIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead()); - } - - _vmDiskStatsDao.update(vmDiskStat_lock.getId(), vmDiskStat_lock); } } - } - txn.commit(); + }); } catch (Exception e) { s_logger.warn("Error while collecting vm disk stats from hosts", e); - } finally { - txn.close(); } - } } diff --git a/server/src/com/cloud/servlet/CloudStartupServlet.java b/server/src/com/cloud/servlet/CloudStartupServlet.java index 8fbae924529..4fe96aa505e 100755 --- a/server/src/com/cloud/servlet/CloudStartupServlet.java +++ b/server/src/com/cloud/servlet/CloudStartupServlet.java @@ -30,6 +30,7 @@ import com.cloud.utils.LogUtils; import com.cloud.utils.SerialVersionUID; import com.cloud.utils.component.ComponentContext; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; public class CloudStartupServlet extends HttpServlet { public static final Logger s_logger = Logger.getLogger(CloudStartupServlet.class.getName()); @@ -49,7 +50,7 @@ public class CloudStartupServlet extends HttpServlet { if(ComponentContext.getApplicationContext() != null) { _timer.cancel(); - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { ComponentContext.initComponentsLifeCycle(); } finally { diff --git a/server/src/com/cloud/servlet/ConsoleProxyServlet.java b/server/src/com/cloud/servlet/ConsoleProxyServlet.java index e01d9595215..b3415db3779 100644 --- a/server/src/com/cloud/servlet/ConsoleProxyServlet.java +++ b/server/src/com/cloud/servlet/ConsoleProxyServlet.java @@ -57,6 +57,7 @@ import com.cloud.utils.Pair; import com.cloud.utils.Ternary; import com.cloud.utils.db.EntityManager; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachineManager; @@ -573,7 +574,7 @@ public class ConsoleProxyServlet extends HttpServlet { return false; // no signature, bad request } - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); txn.close(); User user = null; // verify there is a user with this api key diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java index c046057db51..75cedd016b1 100755 --- a/server/src/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/com/cloud/storage/StorageManagerImpl.java @@ -158,6 +158,10 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.JoinBuilder; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionLegacy; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.db.JoinBuilder.JoinType; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; @@ -1066,7 +1070,7 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C String sql = "SELECT volume_id from snapshots, snapshot_store_ref WHERE snapshots.id = snapshot_store_ref.snapshot_id and store_id=? GROUP BY volume_id"; List list = new ArrayList(); try { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); ResultSet rs = null; PreparedStatement pstmt = null; pstmt = txn.prepareAutoCloseStatement(sql); @@ -1086,7 +1090,7 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C List findAllSnapshotForVolume(Long volumeId) { String sql = "SELECT backup_snap_id FROM snapshots WHERE volume_id=? and backup_snap_id is not NULL"; try { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); ResultSet rs = null; PreparedStatement pstmt = null; pstmt = txn.prepareAutoCloseStatement(sql); @@ -1752,7 +1756,7 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C @Override public boolean deleteImageStore(DeleteImageStoreCmd cmd) { - long storeId = cmd.getId(); + final long storeId = cmd.getId(); // Verify that image store exists ImageStoreVO store = _imageStoreDao.findById(storeId); if (store == null) { @@ -1778,17 +1782,20 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C } // ready to delete - Transaction txn = Transaction.currentTxn(); - txn.start(); - // first delete from image_store_details table, we need to do that since - // we are not actually deleting record from main - // image_data_store table, so delete cascade will not work - _imageStoreDetailsDao.deleteDetails(storeId); - _snapshotStoreDao.deletePrimaryRecordsForStore(storeId, DataStoreRole.Image); - _volumeStoreDao.deletePrimaryRecordsForStore(storeId); - _templateStoreDao.deletePrimaryRecordsForStore(storeId); - _imageStoreDao.remove(storeId); - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + // first delete from image_store_details table, we need to do that since + // we are not actually deleting record from main + // image_data_store table, so delete cascade will not work + _imageStoreDetailsDao.deleteDetails(storeId); + _snapshotStoreDao.deletePrimaryRecordsForStore(storeId, DataStoreRole.Image); + _volumeStoreDao.deletePrimaryRecordsForStore(storeId); + _templateStoreDao.deletePrimaryRecordsForStore(storeId); + _imageStoreDao.remove(storeId); + } + }); + return true; } @@ -1862,7 +1869,7 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C @Override public boolean deleteSecondaryStagingStore(DeleteSecondaryStagingStoreCmd cmd) { - long storeId = cmd.getId(); + final long storeId = cmd.getId(); // Verify that cache store exists ImageStoreVO store = _imageStoreDao.findById(storeId); if (store == null) { @@ -1887,17 +1894,20 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C } // ready to delete - Transaction txn = Transaction.currentTxn(); - txn.start(); - // first delete from image_store_details table, we need to do that since - // we are not actually deleting record from main - // image_data_store table, so delete cascade will not work - _imageStoreDetailsDao.deleteDetails(storeId); - _snapshotStoreDao.deletePrimaryRecordsForStore(storeId, DataStoreRole.ImageCache); - _volumeStoreDao.deletePrimaryRecordsForStore(storeId); - _templateStoreDao.deletePrimaryRecordsForStore(storeId); - _imageStoreDao.remove(storeId); - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + // first delete from image_store_details table, we need to do that since + // we are not actually deleting record from main + // image_data_store table, so delete cascade will not work + _imageStoreDetailsDao.deleteDetails(storeId); + _snapshotStoreDao.deletePrimaryRecordsForStore(storeId, DataStoreRole.ImageCache); + _volumeStoreDao.deletePrimaryRecordsForStore(storeId); + _templateStoreDao.deletePrimaryRecordsForStore(storeId); + _imageStoreDao.remove(storeId); + } + }); + return true; } diff --git a/server/src/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/com/cloud/storage/VolumeApiServiceImpl.java index 00df333826a..071281c41fe 100644 --- a/server/src/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/com/cloud/storage/VolumeApiServiceImpl.java @@ -139,6 +139,8 @@ import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.DB; import com.cloud.utils.db.EntityManager; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.fsm.NoTransitionException; import com.cloud.utils.fsm.StateMachine2; @@ -403,34 +405,34 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic } @DB - protected VolumeVO persistVolume(Account owner, Long zoneId, String volumeName, String url, String format) { - - Transaction txn = Transaction.currentTxn(); - txn.start(); - - VolumeVO volume = new VolumeVO(volumeName, zoneId, -1, -1, -1, new Long(-1), null, null, 0, Volume.Type.DATADISK); - volume.setPoolId(null); - volume.setDataCenterId(zoneId); - volume.setPodId(null); - volume.setAccountId(owner.getAccountId()); - volume.setDomainId(owner.getDomainId()); - long diskOfferingId = _diskOfferingDao.findByUniqueName("Cloud.com-Custom").getId(); - volume.setDiskOfferingId(diskOfferingId); - // volume.setSize(size); - volume.setInstanceId(null); - volume.setUpdated(new Date()); - volume.setDomainId((owner == null) ? Domain.ROOT_DOMAIN : owner.getDomainId()); - volume.setFormat(ImageFormat.valueOf(format)); - volume = _volsDao.persist(volume); - CallContext.current().setEventDetails("Volume Id: " + volume.getId()); - - // Increment resource count during allocation; if actual creation fails, - // decrement it - _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.volume); - _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.secondary_storage, UriUtils.getRemoteSize(url)); - - txn.commit(); - return volume; + protected VolumeVO persistVolume(final Account owner, final Long zoneId, final String volumeName, final String url, final String format) { + return Transaction.execute(new TransactionCallback() { + @Override + public VolumeVO doInTransaction(TransactionStatus status) { + VolumeVO volume = new VolumeVO(volumeName, zoneId, -1, -1, -1, new Long(-1), null, null, 0, Volume.Type.DATADISK); + volume.setPoolId(null); + volume.setDataCenterId(zoneId); + volume.setPodId(null); + volume.setAccountId(owner.getAccountId()); + volume.setDomainId(owner.getDomainId()); + long diskOfferingId = _diskOfferingDao.findByUniqueName("Cloud.com-Custom").getId(); + volume.setDiskOfferingId(diskOfferingId); + // volume.setSize(size); + volume.setInstanceId(null); + volume.setUpdated(new Date()); + volume.setDomainId((owner == null) ? Domain.ROOT_DOMAIN : owner.getDomainId()); + volume.setFormat(ImageFormat.valueOf(format)); + volume = _volsDao.persist(volume); + CallContext.current().setEventDetails("Volume Id: " + volume.getId()); + + // Increment resource count during allocation; if actual creation fails, + // decrement it + _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.volume); + _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.secondary_storage, UriUtils.getRemoteSize(url)); + + return volume; + } + }); } /* @@ -599,49 +601,57 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic userSpecifiedName = getRandomVolumeName(); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - - VolumeVO volume = new VolumeVO(userSpecifiedName, -1, -1, -1, -1, new Long(-1), null, null, 0, Volume.Type.DATADISK); - volume.setPoolId(null); - volume.setDataCenterId(zoneId); - volume.setPodId(null); - volume.setAccountId(ownerId); - volume.setDomainId(((caller == null) ? Domain.ROOT_DOMAIN : caller.getDomainId())); - volume.setDiskOfferingId(diskOfferingId); - volume.setSize(size); - volume.setMinIops(minIops); - volume.setMaxIops(maxIops); - volume.setInstanceId(null); - volume.setUpdated(new Date()); - volume.setDomainId((caller == null) ? Domain.ROOT_DOMAIN : caller.getDomainId()); - volume.setDisplayVolume(displayVolumeEnabled); - if (parentVolume != null) { - volume.setTemplateId(parentVolume.getTemplateId()); - volume.setFormat(parentVolume.getFormat()); - } else { - volume.setTemplateId(null); - } - - volume = _volsDao.persist(volume); - if (cmd.getSnapshotId() == null) { - // for volume created from snapshot, create usage event after volume creation - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_CREATE, volume.getAccountId(), volume.getDataCenterId(), volume.getId(), volume.getName(), diskOfferingId, - null, size, Volume.class.getName(), volume.getUuid()); - } - - CallContext.current().setEventDetails("Volume Id: " + volume.getId()); - - // Increment resource count during allocation; if actual creation fails, - // decrement it - _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.volume); - _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, new Long(volume.getSize())); - - txn.commit(); + VolumeVO volume = commitVolume(cmd, caller, ownerId, displayVolumeEnabled, zoneId, diskOfferingId, size, + minIops, maxIops, parentVolume, userSpecifiedName); return volume; } + private VolumeVO commitVolume(final CreateVolumeCmd cmd, final Account caller, final long ownerId, final Boolean displayVolumeEnabled, + final Long zoneId, final Long diskOfferingId, final Long size, final Long minIops, final Long maxIops, final VolumeVO parentVolume, + final String userSpecifiedName) { + return Transaction.execute(new TransactionCallback() { + @Override + public VolumeVO doInTransaction(TransactionStatus status) { + VolumeVO volume = new VolumeVO(userSpecifiedName, -1, -1, -1, -1, new Long(-1), null, null, 0, Volume.Type.DATADISK); + volume.setPoolId(null); + volume.setDataCenterId(zoneId); + volume.setPodId(null); + volume.setAccountId(ownerId); + volume.setDomainId(((caller == null) ? Domain.ROOT_DOMAIN : caller.getDomainId())); + volume.setDiskOfferingId(diskOfferingId); + volume.setSize(size); + volume.setMinIops(minIops); + volume.setMaxIops(maxIops); + volume.setInstanceId(null); + volume.setUpdated(new Date()); + volume.setDomainId((caller == null) ? Domain.ROOT_DOMAIN : caller.getDomainId()); + volume.setDisplayVolume(displayVolumeEnabled); + if (parentVolume != null) { + volume.setTemplateId(parentVolume.getTemplateId()); + volume.setFormat(parentVolume.getFormat()); + } else { + volume.setTemplateId(null); + } + + volume = _volsDao.persist(volume); + if (cmd.getSnapshotId() == null) { + // for volume created from snapshot, create usage event after volume creation + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_CREATE, volume.getAccountId(), volume.getDataCenterId(), volume.getId(), volume.getName(), diskOfferingId, + null, size, Volume.class.getName(), volume.getUuid()); + } + + CallContext.current().setEventDetails("Volume Id: " + volume.getId()); + + // Increment resource count during allocation; if actual creation fails, + // decrement it + _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.volume); + _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, new Long(volume.getSize())); + return volume; + } + }); + } + public boolean validateVolumeSizeRange(long size) { if (size < 0 || (size > 0 && size < (1024 * 1024 * 1024))) { throw new InvalidParameterValueException("Please specify a size of at least 1 Gb."); diff --git a/server/src/com/cloud/tags/TaggedResourceManagerImpl.java b/server/src/com/cloud/tags/TaggedResourceManagerImpl.java index 79aba6340d9..9269dce8f9a 100644 --- a/server/src/com/cloud/tags/TaggedResourceManagerImpl.java +++ b/server/src/com/cloud/tags/TaggedResourceManagerImpl.java @@ -66,6 +66,8 @@ import com.cloud.utils.db.GenericDao; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.uuididentity.dao.IdentityDao; import com.cloud.vm.dao.NicDao; @@ -254,55 +256,55 @@ public class TaggedResourceManagerImpl extends ManagerBase implements TaggedReso @Override @DB @ActionEvent(eventType = EventTypes.EVENT_TAGS_CREATE, eventDescription = "creating resource tags") - public List createTags(List resourceIds, TaggedResourceType resourceType, - Map tags, String customer) { - Account caller = CallContext.current().getCallingAccount(); + public List createTags(final List resourceIds, final TaggedResourceType resourceType, + final Map tags, final String customer) { + final Account caller = CallContext.current().getCallingAccount(); - List resourceTags = new ArrayList(tags.size()); + final List resourceTags = new ArrayList(tags.size()); - Transaction txn = Transaction.currentTxn(); - txn.start(); - - for (String key : tags.keySet()) { - for (String resourceId : resourceIds) { - Long id = getResourceId(resourceId, resourceType); - String resourceUuid = getUuid(resourceId, resourceType); - - //check if object exists - if (_daoMap.get(resourceType).findById(id) == null) { - throw new InvalidParameterValueException("Unable to find resource by id " + resourceId + - " and type " + resourceType); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (String key : tags.keySet()) { + for (String resourceId : resourceIds) { + Long id = getResourceId(resourceId, resourceType); + String resourceUuid = getUuid(resourceId, resourceType); + + //check if object exists + if (_daoMap.get(resourceType).findById(id) == null) { + throw new InvalidParameterValueException("Unable to find resource by id " + resourceId + + " and type " + resourceType); + } + + Pair accountDomainPair = getAccountDomain(id, resourceType); + Long domainId = accountDomainPair.second(); + Long accountId = accountDomainPair.first(); + if (accountId != null) { + _accountMgr.checkAccess(caller, null, false, _accountMgr.getAccount(accountId)); + } else if (domainId != null && caller.getType() != Account.ACCOUNT_TYPE_NORMAL) { + //check permissions; + _accountMgr.checkAccess(caller, _domainMgr.getDomain(domainId)); + } else { + throw new PermissionDeniedException("Account " + caller + " doesn't have permissions to create tags" + + " for resource " + key); + } + + String value = tags.get(key); + + if (value == null || value.isEmpty()) { + throw new InvalidParameterValueException("Value for the key " + key + " is either null or empty"); + } + + ResourceTagVO resourceTag = new ResourceTagVO(key, value, accountDomainPair.first(), + accountDomainPair.second(), + id, resourceType, customer, resourceUuid); + resourceTag = _resourceTagDao.persist(resourceTag); + resourceTags.add(resourceTag); + } } - - Pair accountDomainPair = getAccountDomain(id, resourceType); - Long domainId = accountDomainPair.second(); - Long accountId = accountDomainPair.first(); - if (accountId != null) { - _accountMgr.checkAccess(caller, null, false, _accountMgr.getAccount(accountId)); - } else if (domainId != null && caller.getType() != Account.ACCOUNT_TYPE_NORMAL) { - //check permissions; - _accountMgr.checkAccess(caller, _domainMgr.getDomain(domainId)); - } else { - throw new PermissionDeniedException("Account " + caller + " doesn't have permissions to create tags" + - " for resource " + key); - } - - String value = tags.get(key); - - if (value == null || value.isEmpty()) { - throw new InvalidParameterValueException("Value for the key " + key + " is either null or empty"); - } - - ResourceTagVO resourceTag = new ResourceTagVO(key, value, accountDomainPair.first(), - accountDomainPair.second(), - id, resourceType, customer, resourceUuid); - resourceTag = _resourceTagDao.persist(resourceTag); - resourceTags.add(resourceTag); } - } - - txn.commit(); - + }); + return resourceTags; } @@ -354,7 +356,7 @@ public class TaggedResourceManagerImpl extends ManagerBase implements TaggedReso sc.setParameters("resourceType", resourceType); List resourceTags = _resourceTagDao.search(sc, null);; - List tagsToRemove = new ArrayList(); + final List tagsToRemove = new ArrayList(); // Finalize which tags should be removed for (ResourceTag resourceTag : resourceTags) { @@ -390,13 +392,15 @@ public class TaggedResourceManagerImpl extends ManagerBase implements TaggedReso } //Remove the tags - Transaction txn = Transaction.currentTxn(); - txn.start(); - for (ResourceTag tagToRemove : tagsToRemove) { - _resourceTagDao.remove(tagToRemove.getId()); - s_logger.debug("Removed the tag " + tagToRemove); - } - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (ResourceTag tagToRemove : tagsToRemove) { + _resourceTagDao.remove(tagToRemove.getId()); + s_logger.debug("Removed the tag " + tagToRemove); + } + } + }); return true; } diff --git a/server/src/com/cloud/template/TemplateManagerImpl.java b/server/src/com/cloud/template/TemplateManagerImpl.java index c1ce89205ad..fb1bb272827 100755 --- a/server/src/com/cloud/template/TemplateManagerImpl.java +++ b/server/src/com/cloud/template/TemplateManagerImpl.java @@ -31,7 +31,6 @@ import javax.inject.Inject; import javax.naming.ConfigurationException; import org.apache.log4j.Logger; - import org.apache.cloudstack.acl.SecurityChecker.AccessType; import org.apache.cloudstack.api.BaseListTemplateOrIsoPermissionsCmd; import org.apache.cloudstack.api.BaseUpdateTemplateOrIsoCmd; @@ -177,6 +176,9 @@ import com.cloud.utils.component.ManagerBase; import com.cloud.utils.concurrency.NamedThreadFactory; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.UserVmManager; import com.cloud.vm.UserVmVO; @@ -1163,11 +1165,9 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager, @DB @Override public boolean updateTemplateOrIsoPermissions(BaseUpdateTemplateOrIsoPermissionsCmd cmd) { - Transaction txn = Transaction.currentTxn(); - // Input validation - Long id = cmd.getId(); - Account caller = CallContext.current().getCallingAccount(); + final Long id = cmd.getId(); + final Account caller = CallContext.current().getCallingAccount(); List accountNames = cmd.getAccountNames(); List projectIds = cmd.getProjectIds(); Boolean isFeatured = cmd.isFeatured(); @@ -1284,28 +1284,31 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager, //Derive the domain id from the template owner as updateTemplatePermissions is not cross domain operation Account owner = _accountMgr.getAccount(ownerId); - Domain domain = _domainDao.findById(owner.getDomainId()); + final Domain domain = _domainDao.findById(owner.getDomainId()); if ("add".equalsIgnoreCase(operation)) { - txn.start(); - for (String accountName : accountNames) { - Account permittedAccount = _accountDao.findActiveAccount(accountName, domain.getId()); - if (permittedAccount != null) { - if (permittedAccount.getId() == caller.getId()) { - continue; // don't grant permission to the template - // owner, they implicitly have permission + final List accountNamesFinal = accountNames; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + for (String accountName : accountNamesFinal) { + Account permittedAccount = _accountDao.findActiveAccount(accountName, domain.getId()); + if (permittedAccount != null) { + if (permittedAccount.getId() == caller.getId()) { + continue; // don't grant permission to the template + // owner, they implicitly have permission + } + LaunchPermissionVO existingPermission = _launchPermissionDao.findByTemplateAndAccount(id, permittedAccount.getId()); + if (existingPermission == null) { + LaunchPermissionVO launchPermission = new LaunchPermissionVO(id, permittedAccount.getId()); + _launchPermissionDao.persist(launchPermission); + } + } else { + throw new InvalidParameterValueException("Unable to grant a launch permission to account " + accountName + " in domain id=" + domain.getUuid() + + ", account not found. " + "No permissions updated, please verify the account names and retry."); + } } - LaunchPermissionVO existingPermission = _launchPermissionDao.findByTemplateAndAccount(id, permittedAccount.getId()); - if (existingPermission == null) { - LaunchPermissionVO launchPermission = new LaunchPermissionVO(id, permittedAccount.getId()); - _launchPermissionDao.persist(launchPermission); - } - } else { - txn.rollback(); - throw new InvalidParameterValueException("Unable to grant a launch permission to account " + accountName + " in domain id=" + domain.getUuid() - + ", account not found. " + "No permissions updated, please verify the account names and retry."); } - } - txn.commit(); + }); } else if ("remove".equalsIgnoreCase(operation)) { List accountIds = new ArrayList(); for (String accountName : accountNames) { @@ -1335,11 +1338,11 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager, if (userId == null) { userId = User.UID_SYSTEM; } - long templateId = command.getEntityId(); + final long templateId = command.getEntityId(); Long volumeId = command.getVolumeId(); Long snapshotId = command.getSnapshotId(); VMTemplateVO privateTemplate = null; - Long accountId = null; + final Long accountId = null; SnapshotVO snapshot = null; VolumeVO volume = null; @@ -1428,26 +1431,31 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager, zoneId, accountId, volumeId); }*/ if (privateTemplate == null) { - Transaction txn = Transaction.currentTxn(); - txn.start(); - // template_store_ref entries should have been removed using our - // DataObject.processEvent command in case of failure, but clean - // it up here to avoid - // some leftovers which will cause removing template from - // vm_template table fail. - _tmplStoreDao.deletePrimaryRecordsForTemplate(templateId); - // Remove the template_zone_ref record - _tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId); - // Remove the template record - _tmpltDao.expunge(templateId); + final VolumeVO volumeFinal = volume; + final SnapshotVO snapshotFinal = snapshot; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + // template_store_ref entries should have been removed using our + // DataObject.processEvent command in case of failure, but clean + // it up here to avoid + // some leftovers which will cause removing template from + // vm_template table fail. + _tmplStoreDao.deletePrimaryRecordsForTemplate(templateId); + // Remove the template_zone_ref record + _tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId); + // Remove the template record + _tmpltDao.expunge(templateId); + + // decrement resource count + if (accountId != null) { + _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template); + _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(volumeFinal != null ? volumeFinal.getSize() + : snapshotFinal.getSize())); + } + } + }); - // decrement resource count - if (accountId != null) { - _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template); - _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(volume != null ? volume.getSize() - : snapshot.getSize())); - } - txn.commit(); } } diff --git a/server/src/com/cloud/test/DatabaseConfig.java b/server/src/com/cloud/test/DatabaseConfig.java index 38a1abf7542..9370218be0c 100755 --- a/server/src/com/cloud/test/DatabaseConfig.java +++ b/server/src/com/cloud/test/DatabaseConfig.java @@ -57,6 +57,11 @@ import com.cloud.utils.PropertiesUtil; import com.cloud.utils.component.ComponentContext; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionLegacy; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.net.NfsUtils; public class DatabaseConfig { @@ -407,34 +412,32 @@ public class DatabaseConfig { @DB protected void doConfig() { - Transaction txn = Transaction.currentTxn(); try { - - File configFile = new File(_configFileName); + final File configFile = new File(_configFileName); SAXParserFactory spfactory = SAXParserFactory.newInstance(); - SAXParser saxParser = spfactory.newSAXParser(); - DbConfigXMLHandler handler = new DbConfigXMLHandler(); + final SAXParser saxParser = spfactory.newSAXParser(); + final DbConfigXMLHandler handler = new DbConfigXMLHandler(); handler.setParent(this); - txn.start(); + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws Exception { + // Save user configured values for all fields + saxParser.parse(configFile, handler); + + // Save default values for configuration fields + saveVMTemplate(); + saveRootDomain(); + saveDefaultConfiguations(); + } + }); - // Save user configured values for all fields - saxParser.parse(configFile, handler); - - // Save default values for configuration fields - saveVMTemplate(); - saveRootDomain(); - saveDefaultConfiguations(); - - txn.commit(); // Check pod CIDRs against each other, and against the guest ip network/netmask pzc.checkAllPodCidrSubnets(); - } catch (Exception ex) { System.out.print("ERROR IS"+ex); s_logger.error("error", ex); - txn.rollback(); } } @@ -486,7 +489,7 @@ public class DatabaseConfig { String insertSql1 = "INSERT INTO `host` (`id`, `name`, `status` , `type` , `private_ip_address`, `private_netmask` ,`private_mac_address` , `storage_ip_address` ,`storage_netmask`, `storage_mac_address`, `data_center_id`, `version`, `dom0_memory`, `last_ping`, `resource`, `guid`, `hypervisor_type`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; String insertSqlHostDetails = "INSERT INTO `host_details` (`id`, `host_id`, `name`, `value`) VALUES(?,?,?,?)"; String insertSql2 = "INSERT INTO `op_host` (`id`, `sequence`) VALUES(?, ?)"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql1); stmt.setLong(1, 0); @@ -563,7 +566,7 @@ public class DatabaseConfig { String hypervisor = _currentObjectParams.get("hypervisorType"); String insertSql1 = "INSERT INTO `cluster` (`id`, `name`, `data_center_id` , `pod_id`, `hypervisor_type` , `cluster_type`, `allocation_state`) VALUES (?,?,?,?,?,?,?)"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql1); stmt.setLong(1, id); @@ -599,7 +602,7 @@ public class DatabaseConfig { String insertSql1 = "INSERT INTO `storage_pool` (`id`, `name`, `uuid` , `pool_type` , `port`, `data_center_id` ,`available_bytes` , `capacity_bytes` ,`host_address`, `path`, `created`, `pod_id`,`status` , `cluster_id`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; // String insertSql2 = "INSERT INTO `netfs_storage_pool` VALUES (?,?,?)"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql1); stmt.setLong(1, id); @@ -704,7 +707,7 @@ public class DatabaseConfig { "`firewall_service_provided`, `source_nat_service_provided`, `load_balance_service_provided`, `static_nat_service_provided`," + "`port_forwarding_service_provided`, `user_data_service_provided`, `security_group_service_provided`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql1); stmt.setLong(1, id); @@ -742,7 +745,7 @@ public class DatabaseConfig { String insertSql1 = "INSERT INTO `virtual_router_providers` (`id`, `nsp_id`, `uuid` , `type` , `enabled`) " + "VALUES (?,?,?,?,?)"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql1); stmt.setLong(1, id); @@ -1030,7 +1033,7 @@ public class DatabaseConfig { String insertNWRateSql = "UPDATE `cloud`.`service_offering` SET `nw_rate` = ?"; String insertMCRateSql = "UPDATE `cloud`.`service_offering` SET `mc_rate` = ?"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt; @@ -1109,7 +1112,7 @@ public class DatabaseConfig { protected void saveUser() { // insert system account String insertSql = "INSERT INTO `cloud`.`account` (id, account_name, type, domain_id) VALUES (1, 'system', '1', '1')"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql); stmt.executeUpdate(); @@ -1120,7 +1123,7 @@ public class DatabaseConfig { // insert system user insertSql = "INSERT INTO `cloud`.`user` (id, username, password, account_id, firstname, lastname, created)" + " VALUES (1, 'system', RAND(), 1, 'system', 'cloud', now())"; - txn = Transaction.currentTxn(); + txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql); stmt.executeUpdate(); @@ -1159,7 +1162,7 @@ public class DatabaseConfig { // create an account for the admin user first insertSql = "INSERT INTO `cloud`.`account` (id, account_name, type, domain_id) VALUES (" + id + ", '" + username + "', '1', '1')"; - txn = Transaction.currentTxn(); + txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql); stmt.executeUpdate(); @@ -1171,7 +1174,7 @@ public class DatabaseConfig { insertSql = "INSERT INTO `cloud`.`user` (id, username, password, account_id, firstname, lastname, email, created) " + "VALUES (" + id + ",'" + username + "','" + sb.toString() + "', 2, '" + firstname + "','" + lastname + "','" + email + "',now())"; - txn = Transaction.currentTxn(); + txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql); stmt.executeUpdate(); @@ -1227,7 +1230,7 @@ public class DatabaseConfig { String selectSql = "SELECT name FROM cloud.configuration WHERE name = '" + name + "'"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql); ResultSet result = stmt.executeQuery(); @@ -1270,7 +1273,7 @@ public class DatabaseConfig { @DB protected void saveRootDomain() { String insertSql = "insert into `cloud`.`domain` (id, name, parent, owner, path, level) values (1, 'ROOT', NULL, 2, '/', 0)"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertSql); stmt.executeUpdate(); @@ -1377,7 +1380,7 @@ public class DatabaseConfig { } public static String getDatabaseValueString(String selectSql, String name, String errorMsg) { - Transaction txn = Transaction.open("getDatabaseValueString"); + TransactionLegacy txn = TransactionLegacy.open("getDatabaseValueString"); PreparedStatement stmt = null; try { @@ -1399,7 +1402,7 @@ public class DatabaseConfig { } public static long getDatabaseValueLong(String selectSql, String name, String errorMsg) { - Transaction txn = Transaction.open("getDatabaseValueLong"); + TransactionLegacy txn = TransactionLegacy.open("getDatabaseValueLong"); PreparedStatement stmt = null; try { @@ -1420,7 +1423,7 @@ public class DatabaseConfig { } public static void saveSQL(String sql, String errorMsg) { - Transaction txn = Transaction.open("saveSQL"); + TransactionLegacy txn = TransactionLegacy.open("saveSQL"); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(sql); stmt.executeUpdate(); diff --git a/server/src/com/cloud/test/IPRangeConfig.java b/server/src/com/cloud/test/IPRangeConfig.java index 4b884f8c4b2..23ca1bba3ee 100755 --- a/server/src/com/cloud/test/IPRangeConfig.java +++ b/server/src/com/cloud/test/IPRangeConfig.java @@ -29,6 +29,7 @@ import java.util.Vector; import com.cloud.utils.component.ComponentContext; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.net.NetUtils; @@ -303,7 +304,7 @@ public class IPRangeConfig { endIPLong = NetUtils.ip2Long(endIP); } - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); Vector problemIPs = null; if (type.equals("public")) { problemIPs = deletePublicIPRange(txn, startIPLong, endIPLong, vlanDbId); @@ -314,7 +315,7 @@ public class IPRangeConfig { return problemIPs; } - private Vector deletePublicIPRange(Transaction txn, long startIP, long endIP, long vlanDbId) { + private Vector deletePublicIPRange(TransactionLegacy txn, long startIP, long endIP, long vlanDbId) { String deleteSql = "DELETE FROM `cloud`.`user_ip_address` WHERE public_ip_address = ? AND vlan_id = ?"; String isPublicIPAllocatedSelectSql = "SELECT * FROM `cloud`.`user_ip_address` WHERE public_ip_address = ? AND vlan_id = ?"; @@ -349,7 +350,7 @@ public class IPRangeConfig { return problemIPs; } - private Vector deletePrivateIPRange(Transaction txn, long startIP, long endIP, long podId, long zoneId) { + private Vector deletePrivateIPRange(TransactionLegacy txn, long startIP, long endIP, long podId, long zoneId) { String deleteSql = "DELETE FROM `cloud`.`op_dc_ip_address_alloc` WHERE ip_address = ? AND pod_id = ? AND data_center_id = ?"; String isPrivateIPAllocatedSelectSql = "SELECT * FROM `cloud`.`op_dc_ip_address_alloc` WHERE ip_address = ? AND data_center_id = ? AND pod_id = ?"; @@ -429,7 +430,7 @@ public class IPRangeConfig { endIPLong = NetUtils.ip2Long(endIP); } - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); List problemIPs = null; if (type.equals("public")) { @@ -447,7 +448,7 @@ public class IPRangeConfig { return problemIPs; } - public Vector savePublicIPRange(Transaction txn, long startIP, long endIP, long zoneId, long vlanDbId, Long sourceNetworkId, long physicalNetworkId) { + public Vector savePublicIPRange(TransactionLegacy txn, long startIP, long endIP, long zoneId, long vlanDbId, Long sourceNetworkId, long physicalNetworkId) { String insertSql = "INSERT INTO `cloud`.`user_ip_address` (public_ip_address, data_center_id, vlan_db_id, mac_address, source_network_id, physical_network_id, uuid) VALUES (?, ?, ?, (select mac_address from `cloud`.`data_center` where id=?), ?, ?, ?)"; String updateSql = "UPDATE `cloud`.`data_center` set mac_address = mac_address+1 where id=?"; Vector problemIPs = new Vector(); @@ -485,7 +486,7 @@ public class IPRangeConfig { return problemIPs; } - public List savePrivateIPRange(Transaction txn, long startIP, long endIP, long podId, long zoneId) { + public List savePrivateIPRange(TransactionLegacy txn, long startIP, long endIP, long podId, long zoneId) { String insertSql = "INSERT INTO `cloud`.`op_dc_ip_address_alloc` (ip_address, data_center_id, pod_id, mac_address) VALUES (?, ?, ?, (select mac_address from `cloud`.`data_center` where id=?))"; String updateSql = "UPDATE `cloud`.`data_center` set mac_address = mac_address+1 where id=?"; Vector problemIPs = new Vector(); @@ -519,7 +520,7 @@ public class IPRangeConfig { return problemIPs; } - private Vector saveLinkLocalPrivateIPRange(Transaction txn, long startIP, long endIP, long podId, long zoneId) { + private Vector saveLinkLocalPrivateIPRange(TransactionLegacy txn, long startIP, long endIP, long podId, long zoneId) { String insertSql = "INSERT INTO `cloud`.`op_dc_link_local_ip_address_alloc` (ip_address, data_center_id, pod_id) VALUES (?, ?, ?)"; Vector problemIPs = new Vector(); diff --git a/server/src/com/cloud/test/PodZoneConfig.java b/server/src/com/cloud/test/PodZoneConfig.java index 59f8b6ce12d..628c74603cc 100644 --- a/server/src/com/cloud/test/PodZoneConfig.java +++ b/server/src/com/cloud/test/PodZoneConfig.java @@ -28,6 +28,7 @@ import com.cloud.network.Networks.TrafficType; import com.cloud.utils.component.ComponentContext; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.net.NetUtils; public class PodZoneConfig { @@ -148,7 +149,7 @@ public class PodZoneConfig { HashMap> currentPodCidrSubnets = new HashMap>(); String selectSql = "SELECT id, cidr_address, cidr_size FROM host_pod_ref WHERE data_center_id=" + dcId; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql); ResultSet rs = stmt.executeQuery(); @@ -363,7 +364,7 @@ public class PodZoneConfig { String insertVnet = "INSERT INTO `cloud`.`op_dc_vnet_alloc` (vnet, data_center_id, physical_network_id) VALUES ( ?, ?, ?)"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(insertVnet); for (int i = begin; i <= end; i++) { @@ -483,7 +484,7 @@ public class PodZoneConfig { Vector allZoneIDs = new Vector(); String selectSql = "SELECT id FROM data_center"; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql); ResultSet rs = stmt.executeQuery(); diff --git a/server/src/com/cloud/usage/UsageServiceImpl.java b/server/src/com/cloud/usage/UsageServiceImpl.java index 0fed51ca524..d16d36d8a1f 100755 --- a/server/src/com/cloud/usage/UsageServiceImpl.java +++ b/server/src/com/cloud/usage/UsageServiceImpl.java @@ -34,7 +34,6 @@ import org.apache.cloudstack.context.CallContext; import org.apache.cloudstack.framework.config.dao.ConfigurationDao; import org.apache.cloudstack.usage.UsageService; import org.apache.cloudstack.usage.UsageTypes; - import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @@ -55,6 +54,7 @@ import com.cloud.utils.component.ManagerBase; import com.cloud.utils.db.Filter; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value = { UsageService.class }) @@ -87,7 +87,7 @@ public class UsageServiceImpl extends ManagerBase implements UsageService, Manag @Override public boolean generateUsageRecords(GenerateUsageRecordsCmd cmd) { - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); try { UsageJobVO immediateJob = _usageJobDao.getNextImmediateJob(); if (immediateJob == null) { @@ -105,7 +105,7 @@ public class UsageServiceImpl extends ManagerBase implements UsageService, Manag txn.close(); // switch back to VMOPS_DB - Transaction swap = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy swap = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); swap.close(); } return true; @@ -213,14 +213,14 @@ public class UsageServiceImpl extends ManagerBase implements UsageService, Manag } List usageRecords = null; - Transaction txn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); try { usageRecords = _usageDao.searchAllRecords(sc, usageFilter); } finally { txn.close(); // switch back to VMOPS_DB - Transaction swap = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy swap = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); swap.close(); } diff --git a/server/src/com/cloud/user/AccountManagerImpl.java b/server/src/com/cloud/user/AccountManagerImpl.java index e37fcbe8b6d..27fd66a4b9b 100755 --- a/server/src/com/cloud/user/AccountManagerImpl.java +++ b/server/src/com/cloud/user/AccountManagerImpl.java @@ -136,6 +136,9 @@ import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; import com.cloud.vm.InstanceGroupVO; @@ -480,22 +483,23 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M } @DB - public void updateLoginAttempts(Long id, int attempts, boolean toDisable) { - Transaction txn = Transaction.currentTxn(); - txn.start(); + public void updateLoginAttempts(final Long id, final int attempts, final boolean toDisable) { try { - UserAccountVO user = null; - user = _userAccountDao.lockRow(id, true); - user.setLoginAttempts(attempts); - if(toDisable) { - user.setState(State.disabled.toString()); - } - _userAccountDao.update(id, user); - txn.commit(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + UserAccountVO user = null; + user = _userAccountDao.lockRow(id, true); + user.setLoginAttempts(attempts); + if(toDisable) { + user.setState(State.disabled.toString()); + } + _userAccountDao.update(id, user); + } + }); } catch (Exception e) { s_logger.error("Failed to update login attempts for user with id " + id ); } - txn.close(); } private boolean doSetUserStatus(long userId, State state) { @@ -859,8 +863,8 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M @ActionEvent(eventType = EventTypes.EVENT_ACCOUNT_CREATE, eventDescription = "creating Account"), @ActionEvent(eventType = EventTypes.EVENT_USER_CREATE, eventDescription = "creating User") }) - public UserAccount createUserAccount(String userName, String password, String firstName, String lastName, String email, String timezone, String accountName, short accountType, - Long domainId, String networkDomain, Map details, String accountUUID, String userUUID) { + public UserAccount createUserAccount(final String userName, final String password, final String firstName, final String lastName, final String email, final String timezone, String accountName, final short accountType, + Long domainId, final String networkDomain, final Map details, String accountUUID, final String userUUID) { if (accountName == null) { accountName = userName; @@ -902,31 +906,40 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M } } - Transaction txn = Transaction.currentTxn(); - txn.start(); + final String accountNameFinal = accountName; + final Long domainIdFinal = domainId; + final String accountUUIDFinal = accountUUID; + Pair pair = Transaction.execute(new TransactionCallback>() { + @Override + public Pair doInTransaction(TransactionStatus status) { + // create account + String accountUUID = accountUUIDFinal; + if(accountUUID == null){ + accountUUID = UUID.randomUUID().toString(); + } + AccountVO account = createAccount(accountNameFinal, accountType, domainIdFinal, networkDomain, details, accountUUID); + long accountId = account.getId(); + + // create the first user for the account + UserVO user = createUser(accountId, userName, password, firstName, lastName, email, timezone, userUUID); + + if (accountType == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) { + // set registration token + byte[] bytes = (domainIdFinal + accountNameFinal + userName + System.currentTimeMillis()).getBytes(); + String registrationToken = UUID.nameUUIDFromBytes(bytes).toString(); + user.setRegistrationToken(registrationToken); + } + return new Pair(user.getId(), account); + } + }); - // create account - if(accountUUID == null){ - accountUUID = UUID.randomUUID().toString(); - } - AccountVO account = createAccount(accountName, accountType, domainId, networkDomain, details, accountUUID); - long accountId = account.getId(); - - // create the first user for the account - UserVO user = createUser(accountId, userName, password, firstName, lastName, email, timezone, userUUID); - - if (accountType == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) { - // set registration token - byte[] bytes = (domainId + accountName + userName + System.currentTimeMillis()).getBytes(); - String registrationToken = UUID.nameUUIDFromBytes(bytes).toString(); - user.setRegistrationToken(registrationToken); - } - txn.commit(); + long userId = pair.first(); + Account account = pair.second(); CallContext.current().putContextParameter(Account.class, account.getUuid()); //check success - return _userAccountDao.findById(user.getId()); + return _userAccountDao.findById(userId); } @Override @@ -1132,12 +1145,12 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M @Override @DB @ActionEvent(eventType = EventTypes.EVENT_USER_ENABLE, eventDescription = "enabling User") - public UserAccount enableUser(long userId) { + public UserAccount enableUser(final long userId) { Account caller = CallContext.current().getCallingAccount(); // Check if user exists in the system - User user = _userDao.findById(userId); + final User user = _userDao.findById(userId); if (user == null || user.getRemoved() != null) { throw new InvalidParameterValueException("Unable to find active user by id " + userId); } @@ -1155,15 +1168,18 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M checkAccess(caller, null, true, account); - Transaction txn = Transaction.currentTxn(); - txn.start(); + boolean success = Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + boolean success = doSetUserStatus(userId, State.enabled); + + // make sure the account is enabled too + success = success && enableAccount(user.getAccountId()); + + return success; + } + }); - boolean success = doSetUserStatus(userId, State.enabled); - - // make sure the account is enabled too - success = success && enableAccount(user.getAccountId()); - - txn.commit(); if (success) { // whenever the user is successfully enabled, reset the login attempts to zero @@ -1390,7 +1406,7 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M String accountName = cmd.getAccountName(); String newAccountName = cmd.getNewName(); String networkDomain = cmd.getNetworkDomain(); - Map details = cmd.getDetails(); + final Map details = cmd.getDetails(); boolean success = false; Account account = null; @@ -1434,7 +1450,7 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M } } - AccountVO acctForUpdate = _accountDao.findById(account.getId()); + final AccountVO acctForUpdate = _accountDao.findById(account.getId()); acctForUpdate.setAccountName(newAccountName); if (networkDomain != null) { @@ -1445,16 +1461,19 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M } } - Transaction txn = Transaction.currentTxn(); - txn.start(); + final Account accountFinal = account; + success = Transaction.execute(new TransactionCallback() { + @Override + public Boolean doInTransaction(TransactionStatus status) { + boolean success = _accountDao.update(accountFinal.getId(), acctForUpdate); - success = _accountDao.update(account.getId(), acctForUpdate); + if (details != null && success) { + _accountDetailsDao.update(accountFinal.getId(), details); + } - if (details != null && success) { - _accountDetailsDao.update(account.getId(), details); - } - - txn.commit(); + return success; + } + }); if (success) { CallContext.current().putContextParameter(Account.class, account.getUuid()); @@ -1706,7 +1725,7 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M @Override @DB - public AccountVO createAccount(String accountName, short accountType, Long domainId, String networkDomain, Map details, String uuid) { + public AccountVO createAccount(final String accountName, final short accountType, final Long domainId, final String networkDomain, final Map details, final String uuid) { // Validate domain Domain domain = _domainMgr.getDomain(domainId); if (domain == null) { @@ -1747,29 +1766,30 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M } // Create the account - Transaction txn = Transaction.currentTxn(); - txn.start(); + return Transaction.execute(new TransactionCallback() { + @Override + public AccountVO doInTransaction(TransactionStatus status) { + AccountVO account = _accountDao.persist(new AccountVO(accountName, domainId, networkDomain, accountType, uuid)); + + if (account == null) { + throw new CloudRuntimeException("Failed to create account name " + accountName + " in domain id=" + domainId); + } + + Long accountId = account.getId(); + + if (details != null) { + _accountDetailsDao.persist(accountId, details); + } + + // Create resource count records for the account + _resourceCountDao.createResourceCounts(accountId, ResourceLimit.ResourceOwnerType.Account); + + // Create default security group + _networkGroupMgr.createDefaultSecurityGroup(accountId); - AccountVO account = _accountDao.persist(new AccountVO(accountName, domainId, networkDomain, accountType, uuid)); - - if (account == null) { - throw new CloudRuntimeException("Failed to create account name " + accountName + " in domain id=" + domainId); - } - - Long accountId = account.getId(); - - if (details != null) { - _accountDetailsDao.persist(accountId, details); - } - - // Create resource count records for the account - _resourceCountDao.createResourceCounts(accountId, ResourceLimit.ResourceOwnerType.Account); - - // Create default security group - _networkGroupMgr.createDefaultSecurityGroup(accountId); - txn.commit(); - - return account; + return account; + } + }); } protected UserVO createUser(long accountId, String userName, String password, String firstName, String lastName, String email, String timezone, String userUUID) { @@ -2006,7 +2026,7 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M @Override @DB @ActionEvent(eventType = EventTypes.EVENT_REGISTER_FOR_SECRET_API_KEY, eventDescription = "register for the developer API keys") public String[] createApiKeyAndSecretKey(RegisterCmd cmd) { - Long userId = cmd.getId(); + final Long userId = cmd.getId(); User user = getUserIncludingRemoved(userId); if (user == null) { @@ -2019,12 +2039,14 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M } // generate both an api key and a secret key, update the user table with the keys, return the keys to the user - String[] keys = new String[2]; - Transaction txn = Transaction.currentTxn(); - txn.start(); - keys[0] = createUserApiKey(userId); - keys[1] = createUserSecretKey(userId); - txn.commit(); + final String[] keys = new String[2]; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + keys[0] = createUserApiKey(userId); + keys[1] = createUserSecretKey(userId); + } + }); return keys; } diff --git a/server/src/com/cloud/user/DomainManagerImpl.java b/server/src/com/cloud/user/DomainManagerImpl.java index b885c48eda8..294214826e2 100644 --- a/server/src/com/cloud/user/DomainManagerImpl.java +++ b/server/src/com/cloud/user/DomainManagerImpl.java @@ -26,7 +26,6 @@ import javax.inject.Inject; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; - import org.apache.cloudstack.api.command.admin.domain.ListDomainChildrenCmd; import org.apache.cloudstack.api.command.admin.domain.ListDomainsCmd; import org.apache.cloudstack.api.command.admin.domain.UpdateDomainCmd; @@ -65,6 +64,9 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; import com.cloud.vm.ReservationContext; @@ -158,7 +160,7 @@ public class DomainManagerImpl extends ManagerBase implements DomainManager, Dom @Override @DB - public Domain createDomain(String name, Long parentId, Long ownerId, String networkDomain, String domainUUID) { + public Domain createDomain(final String name, final Long parentId, final Long ownerId, final String networkDomain, String domainUUID) { // Verify network domain if (networkDomain != null) { if (!NetUtils.verifyDomainName(networkDomain)) { @@ -181,11 +183,16 @@ public class DomainManagerImpl extends ManagerBase implements DomainManager, Dom domainUUID = UUID.randomUUID().toString(); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - DomainVO domain = _domainDao.create(new DomainVO(name, ownerId, parentId, networkDomain, domainUUID)); - _resourceCountDao.createResourceCounts(domain.getId(), ResourceLimit.ResourceOwnerType.Domain); - txn.commit(); + final String domainUUIDFinal = domainUUID; + DomainVO domain = Transaction.execute(new TransactionCallback() { + @Override + public DomainVO doInTransaction(TransactionStatus status) { + DomainVO domain = _domainDao.create(new DomainVO(name, ownerId, parentId, networkDomain, domainUUIDFinal)); + _resourceCountDao.createResourceCounts(domain.getId(), ResourceLimit.ResourceOwnerType.Domain); + return domain; + } + }); + CallContext.current().putContextParameter(Domain.class, domain.getUuid()); return domain; } @@ -544,12 +551,12 @@ public class DomainManagerImpl extends ManagerBase implements DomainManager, Dom @ActionEvent(eventType = EventTypes.EVENT_DOMAIN_UPDATE, eventDescription = "updating Domain") @DB public DomainVO updateDomain(UpdateDomainCmd cmd) { - Long domainId = cmd.getId(); - String domainName = cmd.getDomainName(); - String networkDomain = cmd.getNetworkDomain(); + final Long domainId = cmd.getId(); + final String domainName = cmd.getDomainName(); + final String networkDomain = cmd.getNetworkDomain(); // check if domain exists in the system - DomainVO domain = _domainDao.findById(domainId); + final DomainVO domain = _domainDao.findById(domainId); if (domain == null) { InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find domain with specified domain id"); ex.addProxyObject(domainId.toString(), "domainId"); @@ -587,27 +594,28 @@ public class DomainManagerImpl extends ManagerBase implements DomainManager, Dom } } - Transaction txn = Transaction.currentTxn(); - - txn.start(); - - if (domainName != null) { - String updatedDomainPath = getUpdatedDomainPath(domain.getPath(), domainName); - updateDomainChildren(domain, updatedDomainPath); - domain.setName(domainName); - domain.setPath(updatedDomainPath); - } - - if (networkDomain != null) { - if (networkDomain.isEmpty()) { - domain.setNetworkDomain(null); - } else { - domain.setNetworkDomain(networkDomain); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + if (domainName != null) { + String updatedDomainPath = getUpdatedDomainPath(domain.getPath(), domainName); + updateDomainChildren(domain, updatedDomainPath); + domain.setName(domainName); + domain.setPath(updatedDomainPath); + } + + if (networkDomain != null) { + if (networkDomain.isEmpty()) { + domain.setNetworkDomain(null); + } else { + domain.setNetworkDomain(networkDomain); + } + } + _domainDao.update(domainId, domain); + CallContext.current().putContextParameter(Domain.class, domain.getUuid()); } - } - _domainDao.update(domainId, domain); - CallContext.current().putContextParameter(Domain.class, domain.getUuid()); - txn.commit(); + }); + return _domainDao.findById(domainId); diff --git a/server/src/com/cloud/uuididentity/dao/IdentityDaoImpl.java b/server/src/com/cloud/uuididentity/dao/IdentityDaoImpl.java index 18b7d8be186..7e14c69753e 100644 --- a/server/src/com/cloud/uuididentity/dao/IdentityDaoImpl.java +++ b/server/src/com/cloud/uuididentity/dao/IdentityDaoImpl.java @@ -33,6 +33,7 @@ import com.cloud.utils.Pair; import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={IdentityDao.class}) @@ -48,7 +49,7 @@ public class IdentityDaoImpl extends GenericDaoBase implements assert(identityString != null); PreparedStatement pstmt = null; - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { try { try { @@ -100,7 +101,7 @@ public class IdentityDaoImpl extends GenericDaoBase implements assert(tableName != null); PreparedStatement pstmt = null; - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { Long domainId = null; Long accountId = null; @@ -146,7 +147,7 @@ public class IdentityDaoImpl extends GenericDaoBase implements assert(identityString != null); PreparedStatement pstmt = null; - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { try { pstmt = txn.prepareAutoCloseStatement( @@ -187,7 +188,7 @@ public class IdentityDaoImpl extends GenericDaoBase implements assert(tableName != null); List l = getNullUuidRecords(tableName); - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { try { txn.start(); @@ -209,7 +210,7 @@ public class IdentityDaoImpl extends GenericDaoBase implements List l = new ArrayList(); PreparedStatement pstmt = null; - Transaction txn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { try { pstmt = txn.prepareAutoCloseStatement( @@ -231,7 +232,7 @@ public class IdentityDaoImpl extends GenericDaoBase implements @DB void setInitialUuid(String tableName, long id) throws SQLException { - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmtUpdate = null; pstmtUpdate = txn.prepareAutoCloseStatement( diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 2ec033ac651..8de494c24c0 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -33,6 +33,8 @@ import javax.ejb.Local; import javax.inject.Inject; import javax.naming.ConfigurationException; +import org.apache.commons.codec.binary.Base64; +import org.apache.log4j.Logger; import org.apache.cloudstack.acl.ControlledEntity.ACLType; import org.apache.cloudstack.acl.SecurityChecker.AccessType; import org.apache.cloudstack.affinity.AffinityGroupService; @@ -239,8 +241,13 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; import com.cloud.utils.db.SearchCriteria.Func; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.exception.ExecutionException; import com.cloud.utils.fsm.NoTransitionException; @@ -1375,11 +1382,11 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir public UserVm recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationException, CloudRuntimeException { - Long vmId = cmd.getId(); + final Long vmId = cmd.getId(); Account caller = CallContext.current().getCallingAccount(); // Verify input parameters - UserVmVO vm = _vmDao.findById(vmId.longValue()); + final UserVmVO vm = _vmDao.findById(vmId.longValue()); if (vm == null) { throw new InvalidParameterValueException( @@ -1409,68 +1416,70 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir s_logger.debug("Recovering vm " + vmId); } - Transaction txn = Transaction.currentTxn(); - AccountVO account = null; - txn.start(); - - account = _accountDao.lockRow(vm.getAccountId(), true); - - // if the account is deleted, throw error - if (account.getRemoved() != null) { - throw new CloudRuntimeException( - "Unable to recover VM as the account is deleted"); - } - - // Get serviceOffering for Virtual Machine - ServiceOfferingVO serviceOffering = _serviceOfferingDao.findById(vm.getServiceOfferingId()); - - // First check that the maximum number of UserVMs, CPU and Memory limit for the given - // accountId will not be exceeded - resourceLimitCheck(account, new Long(serviceOffering.getCpu()), new Long(serviceOffering.getRamSize())); - - _haMgr.cancelDestroy(vm, vm.getHostId()); - - try { - if (!_itMgr.stateTransitTo(vm, - VirtualMachine.Event.RecoveryRequested, null)) { - s_logger.debug("Unable to recover the vm because it is not in the correct state: " - + vmId); - throw new InvalidParameterValueException( - "Unable to recover the vm because it is not in the correct state: " + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws ResourceAllocationException { + + Account account = _accountDao.lockRow(vm.getAccountId(), true); + + // if the account is deleted, throw error + if (account.getRemoved() != null) { + throw new CloudRuntimeException( + "Unable to recover VM as the account is deleted"); + } + + // Get serviceOffering for Virtual Machine + ServiceOfferingVO serviceOffering = _serviceOfferingDao.findById(vm.getServiceOfferingId()); + + // First check that the maximum number of UserVMs, CPU and Memory limit for the given + // accountId will not be exceeded + resourceLimitCheck(account, new Long(serviceOffering.getCpu()), new Long(serviceOffering.getRamSize())); + + _haMgr.cancelDestroy(vm, vm.getHostId()); + + try { + if (!_itMgr.stateTransitTo(vm, + VirtualMachine.Event.RecoveryRequested, null)) { + s_logger.debug("Unable to recover the vm because it is not in the correct state: " + vmId); - } - } catch (NoTransitionException e) { - throw new InvalidParameterValueException( - "Unable to recover the vm because it is not in the correct state: " - + vmId); - } - - // Recover the VM's disks - List volumes = _volsDao.findByInstance(vmId); - for (VolumeVO volume : volumes) { - if (volume.getVolumeType().equals(Volume.Type.ROOT)) { - // Create an event - Long templateId = volume.getTemplateId(); - Long diskOfferingId = volume.getDiskOfferingId(); - Long offeringId = null; - if (diskOfferingId != null) { - DiskOfferingVO offering = _diskOfferingDao - .findById(diskOfferingId); - if (offering != null - && (offering.getType() == DiskOfferingVO.Type.Disk)) { - offeringId = offering.getId(); + throw new InvalidParameterValueException( + "Unable to recover the vm because it is not in the correct state: " + + vmId); + } + } catch (NoTransitionException e) { + throw new InvalidParameterValueException( + "Unable to recover the vm because it is not in the correct state: " + + vmId); + } + + // Recover the VM's disks + List volumes = _volsDao.findByInstance(vmId); + for (VolumeVO volume : volumes) { + if (volume.getVolumeType().equals(Volume.Type.ROOT)) { + // Create an event + Long templateId = volume.getTemplateId(); + Long diskOfferingId = volume.getDiskOfferingId(); + Long offeringId = null; + if (diskOfferingId != null) { + DiskOfferingVO offering = _diskOfferingDao + .findById(diskOfferingId); + if (offering != null + && (offering.getType() == DiskOfferingVO.Type.Disk)) { + offeringId = offering.getId(); + } + } + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_CREATE, volume.getAccountId(), + volume.getDataCenterId(), volume.getId(), volume.getName(), offeringId, templateId, + volume.getSize(), Volume.class.getName(), volume.getUuid()); } } - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_CREATE, volume.getAccountId(), - volume.getDataCenterId(), volume.getId(), volume.getName(), offeringId, templateId, - volume.getSize(), Volume.class.getName(), volume.getUuid()); + + //Update Resource Count for the given account + resourceCountIncrement(account.getId(), new Long(serviceOffering.getCpu()), + new Long(serviceOffering.getRamSize())); } - } + }); - //Update Resource Count for the given account - resourceCountIncrement(account.getId(), new Long(serviceOffering.getCpu()), - new Long(serviceOffering.getRamSize())); - txn.commit(); return _vmDao.findById(vmId); } @@ -1981,8 +1990,6 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir @DB protected InstanceGroupVO createVmGroup(String groupName, long accountId) { Account account = null; - final Transaction txn = Transaction.currentTxn(); - txn.start(); try { account = _accountDao.acquireInLockTable(accountId); // to ensure // duplicate @@ -2005,7 +2012,6 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir if (account != null) { _accountDao.releaseFromLockTable(accountId); } - txn.commit(); } } @@ -2048,7 +2054,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir @Override @DB - public boolean addInstanceToGroup(long userVmId, String groupName) { + public boolean addInstanceToGroup(final long userVmId, String groupName) { UserVmVO vm = _vmDao.findById(userVmId); InstanceGroupVO group = _vmGroupDao.findByAccountAndName( @@ -2059,43 +2065,47 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir } if (group != null) { - final Transaction txn = Transaction.currentTxn(); - txn.start(); UserVm userVm = _vmDao.acquireInLockTable(userVmId); if (userVm == null) { s_logger.warn("Failed to acquire lock on user vm id=" + userVmId); } try { - // don't let the group be deleted when we are assigning vm to - // it. - InstanceGroupVO ngrpLock = _vmGroupDao.lockRow(group.getId(), - false); - if (ngrpLock == null) { - s_logger.warn("Failed to acquire lock on vm group id=" - + group.getId() + " name=" + group.getName()); - txn.rollback(); - return false; - } + final InstanceGroupVO groupFinal = group; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + // don't let the group be deleted when we are assigning vm to + // it. + InstanceGroupVO ngrpLock = _vmGroupDao.lockRow(groupFinal.getId(), + false); + if (ngrpLock == null) { + s_logger.warn("Failed to acquire lock on vm group id=" + + groupFinal.getId() + " name=" + groupFinal.getName()); + throw new CloudRuntimeException("Failed to acquire lock on vm group id=" + + groupFinal.getId() + " name=" + groupFinal.getName()); + } + + // Currently don't allow to assign a vm to more than one group + if (_groupVMMapDao.listByInstanceId(userVmId) != null) { + // Delete all mappings from group_vm_map table + List groupVmMaps = _groupVMMapDao + .listByInstanceId(userVmId); + for (InstanceGroupVMMapVO groupMap : groupVmMaps) { + SearchCriteria sc = _groupVMMapDao + .createSearchCriteria(); + sc.addAnd("instanceId", SearchCriteria.Op.EQ, + groupMap.getInstanceId()); + _groupVMMapDao.expunge(sc); + } + } + InstanceGroupVMMapVO groupVmMapVO = new InstanceGroupVMMapVO( + groupFinal.getId(), userVmId); + _groupVMMapDao.persist(groupVmMapVO); - // Currently don't allow to assign a vm to more than one group - if (_groupVMMapDao.listByInstanceId(userVmId) != null) { - // Delete all mappings from group_vm_map table - List groupVmMaps = _groupVMMapDao - .listByInstanceId(userVmId); - for (InstanceGroupVMMapVO groupMap : groupVmMaps) { - SearchCriteria sc = _groupVMMapDao - .createSearchCriteria(); - sc.addAnd("instanceId", SearchCriteria.Op.EQ, - groupMap.getInstanceId()); - _groupVMMapDao.expunge(sc); } - } - InstanceGroupVMMapVO groupVmMapVO = new InstanceGroupVMMapVO( - group.getId(), userVmId); - _groupVMMapDao.persist(groupVmMapVO); + }); - txn.commit(); return true; } finally { if (userVm != null) { @@ -2764,102 +2774,9 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir hypervisorType = template.getHypervisorType(); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - UserVmVO vm = new UserVmVO(id, instanceName, displayName, - template.getId(), hypervisorType, template.getGuestOSId(), - offering.getOfferHA(), offering.getLimitCpuUse(), - owner.getDomainId(), owner.getId(), offering.getId(), userData, - hostName, diskOfferingId); - vm.setUuid(uuidName); - vm.setDynamicallyScalable(template.isDynamicallyScalable()); - if (sshPublicKey != null) { - vm.setDetail("SSH.PublicKey", sshPublicKey); - } - - if (keyboard != null && !keyboard.isEmpty()) - vm.setDetail(VmDetailConstants.KEYBOARD, keyboard); - - if (isIso) { - vm.setIsoId(template.getId()); - } - - if(isDisplayVmEnabled != null){ - if(!_accountMgr.isRootAdmin(caller.getType())){ - throw new PermissionDeniedException( "Cannot update parameter displayvm, only admin permitted "); - } - vm.setDisplayVm(isDisplayVmEnabled); - }else { - vm.setDisplayVm(true); - } - - // If hypervisor is vSphere, check for clone type setting. - if (hypervisorType.equals(HypervisorType.VMware)) { - // retrieve clone flag. - UserVmCloneType cloneType = UserVmCloneType.linked; - String value = _configDao.getValue(Config.VmwareCreateFullClone.key()); - if (value != null) { - if (Boolean.parseBoolean(value) == true) - cloneType = UserVmCloneType.full; - } - UserVmCloneSettingVO vmCloneSettingVO = new UserVmCloneSettingVO(id, cloneType.toString()); - _vmCloneSettingDao.persist(vmCloneSettingVO); - } - - long guestOSId = template.getGuestOSId(); - GuestOSVO guestOS = _guestOSDao.findById(guestOSId); - long guestOSCategoryId = guestOS.getCategoryId(); - GuestOSCategoryVO guestOSCategory = _guestOSCategoryDao.findById(guestOSCategoryId); - - // If hypervisor is vSphere and OS is OS X, set special settings. - if (hypervisorType.equals(HypervisorType.VMware)) { - if (guestOS.getDisplayName().toLowerCase().contains("apple mac os")){ - vm.setDetail("smc.present", "TRUE"); - vm.setDetail(VmDetailConstants.ROOK_DISK_CONTROLLER, "scsi"); - vm.setDetail("firmware", "efi"); - s_logger.info("guestOS is OSX : overwrite root disk controller to scsi, use smc and efi"); - } - } - - Map details = template.getDetails(); - if ( details != null && !details.isEmpty() ) { - vm.details.putAll(details); - } - - _vmDao.persist(vm); - _vmDao.saveDetails(vm); - - s_logger.debug("Allocating in the DB for vm"); - DataCenterDeployment plan = new DataCenterDeployment(zone.getId()); - - List computeTags = new ArrayList(); - computeTags.add(offering.getHostTag()); - - List rootDiskTags = new ArrayList(); - rootDiskTags.add(offering.getTags()); - - if(isIso){ - VirtualMachineEntity vmEntity = _orchSrvc.createVirtualMachineFromScratch(vm.getUuid(), Long.toString(owner.getAccountId()), vm.getIsoId().toString(), hostName, displayName, hypervisor.name(), guestOSCategory.getName(), offering.getCpu(), offering.getSpeed(), offering.getRamSize(), diskSize, computeTags, rootDiskTags, networkNicMap, plan); - }else { - VirtualMachineEntity vmEntity = _orchSrvc.createVirtualMachine(vm.getUuid(), Long.toString(owner.getAccountId()), Long.toString(template.getId()), hostName, displayName, hypervisor.name(), offering.getCpu(), offering.getSpeed(), offering.getRamSize(), diskSize, computeTags, rootDiskTags, networkNicMap, plan); - } - - - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Successfully allocated DB entry for " + vm); - } - CallContext.current().setEventDetails("Vm Id: " + vm.getId()); - - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_CREATE, accountId, zone.getId(), vm.getId(), - vm.getHostName(), offering.getId(), template.getId(), hypervisorType.toString(), - VirtualMachine.class.getName(), vm.getUuid()); - - //Update Resource Count for the given account - resourceCountIncrement(accountId, new Long(offering.getCpu()), - new Long(offering.getRamSize())); - - txn.commit(); + UserVmVO vm = commitUserVm(zone, template, hostName, displayName, owner, diskOfferingId, diskSize, userData, + hypervisor, caller, isDisplayVmEnabled, keyboard, accountId, offering, isIso, sshPublicKey, + networkNicMap, id, instanceName, uuidName, hypervisorType); // Assign instance to the group try { @@ -2884,6 +2801,113 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir return vm; } + private UserVmVO commitUserVm(final DataCenter zone, final VirtualMachineTemplate template, final String hostName, + final String displayName, final Account owner, final Long diskOfferingId, final Long diskSize, final String userData, + final HypervisorType hypervisor, final Account caller, final Boolean isDisplayVmEnabled, final String keyboard, final long accountId, + final ServiceOfferingVO offering, final boolean isIso, final String sshPublicKey, + final LinkedHashMap networkNicMap, final long id, final String instanceName, final String uuidName, + final HypervisorType hypervisorType) throws InsufficientCapacityException { + return Transaction.execute(new TransactionCallbackWithException() { + @Override + public UserVmVO doInTransaction(TransactionStatus status) throws InsufficientCapacityException { + UserVmVO vm = new UserVmVO(id, instanceName, displayName, + template.getId(), hypervisorType, template.getGuestOSId(), + offering.getOfferHA(), offering.getLimitCpuUse(), + owner.getDomainId(), owner.getId(), offering.getId(), userData, + hostName, diskOfferingId); + vm.setUuid(uuidName); + vm.setDynamicallyScalable(template.isDynamicallyScalable()); + if (sshPublicKey != null) { + vm.setDetail("SSH.PublicKey", sshPublicKey); + } + + if (keyboard != null && !keyboard.isEmpty()) + vm.setDetail(VmDetailConstants.KEYBOARD, keyboard); + + if (isIso) { + vm.setIsoId(template.getId()); + } + + if(isDisplayVmEnabled != null){ + if(!_accountMgr.isRootAdmin(caller.getType())){ + throw new PermissionDeniedException( "Cannot update parameter displayvm, only admin permitted "); + } + vm.setDisplayVm(isDisplayVmEnabled); + }else { + vm.setDisplayVm(true); + } + + // If hypervisor is vSphere, check for clone type setting. + if (hypervisorType.equals(HypervisorType.VMware)) { + // retrieve clone flag. + UserVmCloneType cloneType = UserVmCloneType.linked; + String value = _configDao.getValue(Config.VmwareCreateFullClone.key()); + if (value != null) { + if (Boolean.parseBoolean(value) == true) + cloneType = UserVmCloneType.full; + } + UserVmCloneSettingVO vmCloneSettingVO = new UserVmCloneSettingVO(id, cloneType.toString()); + _vmCloneSettingDao.persist(vmCloneSettingVO); + } + + long guestOSId = template.getGuestOSId(); + GuestOSVO guestOS = _guestOSDao.findById(guestOSId); + long guestOSCategoryId = guestOS.getCategoryId(); + GuestOSCategoryVO guestOSCategory = _guestOSCategoryDao.findById(guestOSCategoryId); + + + // If hypervisor is vSphere and OS is OS X, set special settings. + if (hypervisorType.equals(HypervisorType.VMware)) { + if (guestOS.getDisplayName().toLowerCase().contains("apple mac os")){ + vm.setDetail("smc.present", "TRUE"); + vm.setDetail(VmDetailConstants.ROOK_DISK_CONTROLLER, "scsi"); + vm.setDetail("firmware", "efi"); + s_logger.info("guestOS is OSX : overwrite root disk controller to scsi, use smc and efi"); + } + } + + Map details = template.getDetails(); + if ( details != null && !details.isEmpty() ) { + vm.details.putAll(details); + } + + _vmDao.persist(vm); + _vmDao.saveDetails(vm); + + s_logger.debug("Allocating in the DB for vm"); + DataCenterDeployment plan = new DataCenterDeployment(zone.getId()); + + List computeTags = new ArrayList(); + computeTags.add(offering.getHostTag()); + + List rootDiskTags = new ArrayList(); + rootDiskTags.add(offering.getTags()); + + if(isIso){ + VirtualMachineEntity vmEntity = _orchSrvc.createVirtualMachineFromScratch(vm.getUuid(), Long.toString(owner.getAccountId()), vm.getIsoId().toString(), hostName, displayName, hypervisor.name(), guestOSCategory.getName(), offering.getCpu(), offering.getSpeed(), offering.getRamSize(), diskSize, computeTags, rootDiskTags, networkNicMap, plan); + }else { + VirtualMachineEntity vmEntity = _orchSrvc.createVirtualMachine(vm.getUuid(), Long.toString(owner.getAccountId()), Long.toString(template.getId()), hostName, displayName, hypervisor.name(), offering.getCpu(), offering.getSpeed(), offering.getRamSize(), diskSize, computeTags, rootDiskTags, networkNicMap, plan); + } + + + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Successfully allocated DB entry for " + vm); + } + CallContext.current().setEventDetails("Vm Id: " + vm.getId()); + + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_CREATE, accountId, zone.getId(), vm.getId(), + vm.getHostName(), offering.getId(), template.getId(), hypervisorType.toString(), + VirtualMachine.class.getName(), vm.getUuid()); + + //Update Resource Count for the given account + resourceCountIncrement(accountId, new Long(offering.getCpu()), + new Long(offering.getRamSize())); + return vm; + } + }); + } + private void validateUserData(String userData, HTTPMethod httpmethod) { byte[] decodedUserData = null; if (userData != null) { @@ -3441,7 +3465,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir } @Override - public void collectVmDiskStatistics (UserVmVO userVm) { + public void collectVmDiskStatistics (final UserVmVO userVm) { // support KVM only util 2013.06.25 if (!userVm.getHypervisorType().equals(HypervisorType.KVM)) return; @@ -3449,7 +3473,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir long hostId = userVm.getHostId(); List vmNames = new ArrayList(); vmNames.add(userVm.getInstanceName()); - HostVO host = _hostDao.findById(hostId); + final HostVO host = _hostDao.findById(hostId); GetVmDiskStatsAnswer diskStatsAnswer = null; try { @@ -3463,98 +3487,98 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir s_logger.warn("Error while collecting disk stats vm: " + userVm.getHostName() + " from host: " + host.getName() + "; details: " + diskStatsAnswer.getDetails()); return; } - Transaction txn = Transaction.open(Transaction.CLOUD_DB); try { - txn.start(); - HashMap> vmDiskStatsByName = diskStatsAnswer.getVmDiskStatsMap(); - if (vmDiskStatsByName == null) - return; - List vmDiskStats = vmDiskStatsByName.get(userVm.getInstanceName()); - if (vmDiskStats == null) - return; - - for (VmDiskStatsEntry vmDiskStat:vmDiskStats) { - SearchCriteria sc_volume = _volsDao.createSearchCriteria(); - sc_volume.addAnd("path", SearchCriteria.Op.EQ, vmDiskStat.getPath()); - VolumeVO volume = _volsDao.search(sc_volume, null).get(0); - VmDiskStatisticsVO previousVmDiskStats = _vmDiskStatsDao.findBy(userVm.getAccountId(), userVm.getDataCenterId(), userVm.getId(), volume.getId()); - VmDiskStatisticsVO vmDiskStat_lock = _vmDiskStatsDao.lock(userVm.getAccountId(), userVm.getDataCenterId(), userVm.getId(), volume.getId()); - - if ((vmDiskStat.getIORead() == 0) && (vmDiskStat.getIOWrite() == 0) && (vmDiskStat.getBytesRead() == 0) && (vmDiskStat.getBytesWrite() == 0)) { - s_logger.debug("Read/Write of IO and Bytes are both 0. Not updating vm_disk_statistics"); - continue; - } - - if (vmDiskStat_lock == null) { - s_logger.warn("unable to find vm disk stats from host for account: " + userVm.getAccountId() + " with vmId: " + userVm.getId()+ " and volumeId:" + volume.getId()); - continue; - } - - if (previousVmDiskStats != null - && ((previousVmDiskStats.getCurrentIORead() != vmDiskStat_lock.getCurrentIORead()) - || ((previousVmDiskStats.getCurrentIOWrite() != vmDiskStat_lock.getCurrentIOWrite()) - || (previousVmDiskStats.getCurrentBytesRead() != vmDiskStat_lock.getCurrentBytesRead()) - || (previousVmDiskStats.getCurrentBytesWrite() != vmDiskStat_lock.getCurrentBytesWrite())))) { - s_logger.debug("vm disk stats changed from the time GetVmDiskStatsCommand was sent. " + - "Ignoring current answer. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + - " IO Read: " + vmDiskStat.getIORead() + " IO Write: " + vmDiskStat.getIOWrite() + - " Bytes Read: " + vmDiskStat.getBytesRead() + " Bytes Write: " + vmDiskStat.getBytesWrite()); - continue; - } - - if (vmDiskStat_lock.getCurrentIORead() > vmDiskStat.getIORead()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Read # of IO that's less than the last one. " + - "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + - " Reported: " + vmDiskStat.getIORead() + " Stored: " + vmDiskStat_lock.getCurrentIORead()); - } - vmDiskStat_lock.setNetIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead()); - } - vmDiskStat_lock.setCurrentIORead(vmDiskStat.getIORead()); - if (vmDiskStat_lock.getCurrentIOWrite() > vmDiskStat.getIOWrite()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Write # of IO that's less than the last one. " + - "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + - " Reported: " + vmDiskStat.getIOWrite() + " Stored: " + vmDiskStat_lock.getCurrentIOWrite()); - } - vmDiskStat_lock.setNetIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite()); - } - vmDiskStat_lock.setCurrentIOWrite(vmDiskStat.getIOWrite()); - if (vmDiskStat_lock.getCurrentBytesRead() > vmDiskStat.getBytesRead()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Read # of Bytes that's less than the last one. " + - "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + - " Reported: " + vmDiskStat.getBytesRead() + " Stored: " + vmDiskStat_lock.getCurrentBytesRead()); - } - vmDiskStat_lock.setNetBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead()); - } - vmDiskStat_lock.setCurrentBytesRead(vmDiskStat.getBytesRead()); - if (vmDiskStat_lock.getCurrentBytesWrite() > vmDiskStat.getBytesWrite()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Write # of Bytes that's less than the last one. " + - "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + - " Reported: " + vmDiskStat.getBytesWrite() + " Stored: " + vmDiskStat_lock.getCurrentBytesWrite()); - } - vmDiskStat_lock.setNetBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite()); - } - vmDiskStat_lock.setCurrentBytesWrite(vmDiskStat.getBytesWrite()); - - if (! _dailyOrHourly) { - //update agg bytes - vmDiskStat_lock.setAggIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead()); - vmDiskStat_lock.setAggIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite()); - vmDiskStat_lock.setAggBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead()); - vmDiskStat_lock.setAggBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite()); - } - - _vmDiskStatsDao.update(vmDiskStat_lock.getId(), vmDiskStat_lock); - } - txn.commit(); + final GetVmDiskStatsAnswer diskStatsAnswerFinal = diskStatsAnswer; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + HashMap> vmDiskStatsByName = diskStatsAnswerFinal.getVmDiskStatsMap(); + if (vmDiskStatsByName == null) + return; + List vmDiskStats = vmDiskStatsByName.get(userVm.getInstanceName()); + if (vmDiskStats == null) + return; + + for (VmDiskStatsEntry vmDiskStat:vmDiskStats) { + SearchCriteria sc_volume = _volsDao.createSearchCriteria(); + sc_volume.addAnd("path", SearchCriteria.Op.EQ, vmDiskStat.getPath()); + VolumeVO volume = _volsDao.search(sc_volume, null).get(0); + VmDiskStatisticsVO previousVmDiskStats = _vmDiskStatsDao.findBy(userVm.getAccountId(), userVm.getDataCenterId(), userVm.getId(), volume.getId()); + VmDiskStatisticsVO vmDiskStat_lock = _vmDiskStatsDao.lock(userVm.getAccountId(), userVm.getDataCenterId(), userVm.getId(), volume.getId()); + + if ((vmDiskStat.getIORead() == 0) && (vmDiskStat.getIOWrite() == 0) && (vmDiskStat.getBytesRead() == 0) && (vmDiskStat.getBytesWrite() == 0)) { + s_logger.debug("Read/Write of IO and Bytes are both 0. Not updating vm_disk_statistics"); + continue; + } + + if (vmDiskStat_lock == null) { + s_logger.warn("unable to find vm disk stats from host for account: " + userVm.getAccountId() + " with vmId: " + userVm.getId()+ " and volumeId:" + volume.getId()); + continue; + } + + if (previousVmDiskStats != null + && ((previousVmDiskStats.getCurrentIORead() != vmDiskStat_lock.getCurrentIORead()) + || ((previousVmDiskStats.getCurrentIOWrite() != vmDiskStat_lock.getCurrentIOWrite()) + || (previousVmDiskStats.getCurrentBytesRead() != vmDiskStat_lock.getCurrentBytesRead()) + || (previousVmDiskStats.getCurrentBytesWrite() != vmDiskStat_lock.getCurrentBytesWrite())))) { + s_logger.debug("vm disk stats changed from the time GetVmDiskStatsCommand was sent. " + + "Ignoring current answer. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + + " IO Read: " + vmDiskStat.getIORead() + " IO Write: " + vmDiskStat.getIOWrite() + + " Bytes Read: " + vmDiskStat.getBytesRead() + " Bytes Write: " + vmDiskStat.getBytesWrite()); + continue; + } + + if (vmDiskStat_lock.getCurrentIORead() > vmDiskStat.getIORead()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Read # of IO that's less than the last one. " + + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + + " Reported: " + vmDiskStat.getIORead() + " Stored: " + vmDiskStat_lock.getCurrentIORead()); + } + vmDiskStat_lock.setNetIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead()); + } + vmDiskStat_lock.setCurrentIORead(vmDiskStat.getIORead()); + if (vmDiskStat_lock.getCurrentIOWrite() > vmDiskStat.getIOWrite()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Write # of IO that's less than the last one. " + + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + + " Reported: " + vmDiskStat.getIOWrite() + " Stored: " + vmDiskStat_lock.getCurrentIOWrite()); + } + vmDiskStat_lock.setNetIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite()); + } + vmDiskStat_lock.setCurrentIOWrite(vmDiskStat.getIOWrite()); + if (vmDiskStat_lock.getCurrentBytesRead() > vmDiskStat.getBytesRead()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Read # of Bytes that's less than the last one. " + + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + + " Reported: " + vmDiskStat.getBytesRead() + " Stored: " + vmDiskStat_lock.getCurrentBytesRead()); + } + vmDiskStat_lock.setNetBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead()); + } + vmDiskStat_lock.setCurrentBytesRead(vmDiskStat.getBytesRead()); + if (vmDiskStat_lock.getCurrentBytesWrite() > vmDiskStat.getBytesWrite()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Write # of Bytes that's less than the last one. " + + "Assuming something went wrong and persisting it. Host: " + host.getName() + " . VM: " + vmDiskStat.getVmName() + + " Reported: " + vmDiskStat.getBytesWrite() + " Stored: " + vmDiskStat_lock.getCurrentBytesWrite()); + } + vmDiskStat_lock.setNetBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite()); + } + vmDiskStat_lock.setCurrentBytesWrite(vmDiskStat.getBytesWrite()); + + if (! _dailyOrHourly) { + //update agg bytes + vmDiskStat_lock.setAggIORead(vmDiskStat_lock.getNetIORead() + vmDiskStat_lock.getCurrentIORead()); + vmDiskStat_lock.setAggIOWrite(vmDiskStat_lock.getNetIOWrite() + vmDiskStat_lock.getCurrentIOWrite()); + vmDiskStat_lock.setAggBytesRead(vmDiskStat_lock.getNetBytesRead() + vmDiskStat_lock.getCurrentBytesRead()); + vmDiskStat_lock.setAggBytesWrite(vmDiskStat_lock.getNetBytesWrite() + vmDiskStat_lock.getCurrentBytesWrite()); + } + + _vmDiskStatsDao.update(vmDiskStat_lock.getId(), vmDiskStat_lock); + } + } + }); } catch (Exception e) { - txn.rollback(); s_logger.warn("Unable to update vm disk statistics for vm: " + userVm.getId() + " from host: " + hostId, e); - } finally { - txn.close(); } } } @@ -4295,7 +4319,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir @DB @Override @ActionEvent(eventType = EventTypes.EVENT_VM_MOVE, eventDescription = "move VM to another user", async = false) - public UserVm moveVMToUser(AssignVMCmd cmd) + public UserVm moveVMToUser(final AssignVMCmd cmd) throws ResourceAllocationException, ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { // VERIFICATIONS and VALIDATIONS @@ -4315,7 +4339,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir } // get and check the valid VM - UserVmVO vm = _vmDao.findById(cmd.getVmId()); + final UserVmVO vm = _vmDao.findById(cmd.getVmId()); if (vm == null) { throw new InvalidParameterValueException( "There is no vm by that id " + cmd.getVmId()); @@ -4330,7 +4354,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir throw ex; } - Account oldAccount = _accountService.getActiveAccountById(vm + final Account oldAccount = _accountService.getActiveAccountById(vm .getAccountId()); if (oldAccount == null) { throw new InvalidParameterValueException("Invalid account for VM " @@ -4343,7 +4367,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir ex.addProxyObject(vm.getUuid(), "vmId"); throw ex; } - Account newAccount = _accountService.getActiveAccountByName( + final Account newAccount = _accountService.getActiveAccountByName( cmd.getAccountName(), cmd.getDomainId()); if (newAccount == null || newAccount.getType() == Account.ACCOUNT_TYPE_PROJECT) { @@ -4400,8 +4424,8 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir DataCenterVO zone = _dcDao.findById(vm.getDataCenterId()); // Get serviceOffering and Volumes for Virtual Machine - ServiceOfferingVO offering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getServiceOfferingId()); - List volumes = _volsDao.findByInstance(cmd.getVmId()); + final ServiceOfferingVO offering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getServiceOfferingId()); + final List volumes = _volsDao.findByInstance(cmd.getVmId()); //Remove vm from instance group removeInstanceFromInstanceGroup(cmd.getVmId()); @@ -4431,55 +4455,57 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir DomainVO domain = _domainDao.findById(cmd.getDomainId()); _accountMgr.checkAccess(newAccount, domain); - Transaction txn = Transaction.currentTxn(); - txn.start(); - //generate destroy vm event for usage - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_DESTROY, vm.getAccountId(), vm.getDataCenterId(), vm.getId(), - vm.getHostName(), vm.getServiceOfferingId(), vm.getTemplateId(), vm.getHypervisorType().toString(), - VirtualMachine.class.getName(), vm.getUuid()); - - // update resource counts for old account - resourceCountDecrement(oldAccount.getAccountId(), new Long(offering.getCpu()), - new Long(offering.getRamSize())); - - // OWNERSHIP STEP 1: update the vm owner - vm.setAccountId(newAccount.getAccountId()); - vm.setDomainId(cmd.getDomainId()); - _vmDao.persist(vm); - - // OS 2: update volume - for (VolumeVO volume : volumes) { - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_DELETE, volume.getAccountId(), - volume.getDataCenterId(), volume.getId(), volume.getName(), Volume.class.getName(), volume.getUuid()); - _resourceLimitMgr.decrementResourceCount(oldAccount.getAccountId(), ResourceType.volume); - _resourceLimitMgr.decrementResourceCount(oldAccount.getAccountId(), ResourceType.primary_storage, - new Long(volume.getSize())); - volume.setAccountId(newAccount.getAccountId()); - volume.setDomainId(newAccount.getDomainId()); - _volsDao.persist(volume); - _resourceLimitMgr.incrementResourceCount(newAccount.getAccountId(), ResourceType.volume); - _resourceLimitMgr.incrementResourceCount(newAccount.getAccountId(), ResourceType.primary_storage, - new Long(volume.getSize())); - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_CREATE, volume.getAccountId(), - volume.getDataCenterId(), volume.getId(), volume.getName(), - volume.getDiskOfferingId(), volume.getTemplateId(), volume.getSize(), Volume.class.getName(), - volume.getUuid()); - //snapshots: mark these removed in db - List snapshots = _snapshotDao.listByVolumeIdIncludingRemoved(volume.getId()); - for (SnapshotVO snapshot: snapshots){ - _snapshotDao.remove(snapshot.getId()); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + //generate destroy vm event for usage + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_DESTROY, vm.getAccountId(), vm.getDataCenterId(), vm.getId(), + vm.getHostName(), vm.getServiceOfferingId(), vm.getTemplateId(), vm.getHypervisorType().toString(), + VirtualMachine.class.getName(), vm.getUuid()); + + // update resource counts for old account + resourceCountDecrement(oldAccount.getAccountId(), new Long(offering.getCpu()), + new Long(offering.getRamSize())); + + // OWNERSHIP STEP 1: update the vm owner + vm.setAccountId(newAccount.getAccountId()); + vm.setDomainId(cmd.getDomainId()); + _vmDao.persist(vm); + + // OS 2: update volume + for (VolumeVO volume : volumes) { + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_DELETE, volume.getAccountId(), + volume.getDataCenterId(), volume.getId(), volume.getName(), Volume.class.getName(), volume.getUuid()); + _resourceLimitMgr.decrementResourceCount(oldAccount.getAccountId(), ResourceType.volume); + _resourceLimitMgr.decrementResourceCount(oldAccount.getAccountId(), ResourceType.primary_storage, + new Long(volume.getSize())); + volume.setAccountId(newAccount.getAccountId()); + volume.setDomainId(newAccount.getDomainId()); + _volsDao.persist(volume); + _resourceLimitMgr.incrementResourceCount(newAccount.getAccountId(), ResourceType.volume); + _resourceLimitMgr.incrementResourceCount(newAccount.getAccountId(), ResourceType.primary_storage, + new Long(volume.getSize())); + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_CREATE, volume.getAccountId(), + volume.getDataCenterId(), volume.getId(), volume.getName(), + volume.getDiskOfferingId(), volume.getTemplateId(), volume.getSize(), Volume.class.getName(), + volume.getUuid()); + //snapshots: mark these removed in db + List snapshots = _snapshotDao.listByVolumeIdIncludingRemoved(volume.getId()); + for (SnapshotVO snapshot: snapshots){ + _snapshotDao.remove(snapshot.getId()); + } + } + + //update resource count of new account + resourceCountIncrement(newAccount.getAccountId(), new Long(offering.getCpu()), new Long(offering.getRamSize())); + + //generate usage events to account for this change + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_CREATE, vm.getAccountId(), vm.getDataCenterId(), vm.getId(), + vm.getHostName(), vm.getServiceOfferingId(), vm.getTemplateId(), vm.getHypervisorType().toString(), + VirtualMachine.class.getName(), vm.getUuid()); } - } + }); - //update resource count of new account - resourceCountIncrement(newAccount.getAccountId(), new Long(offering.getCpu()), new Long(offering.getRamSize())); - - //generate usage events to account for this change - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_CREATE, vm.getAccountId(), vm.getDataCenterId(), vm.getId(), - vm.getHostName(), vm.getServiceOfferingId(), vm.getTemplateId(), vm.getHypervisorType().toString(), - VirtualMachine.class.getName(), vm.getUuid()); - - txn.commit(); VirtualMachine vmoi = _itMgr.findById(vm.getId()); VirtualMachineProfileImpl vmOldProfile = new VirtualMachineProfileImpl(vmoi); diff --git a/server/src/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java b/server/src/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java index aa772fefa9d..cd064f59667 100644 --- a/server/src/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java +++ b/server/src/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java @@ -29,7 +29,6 @@ import javax.naming.ConfigurationException; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; - import org.apache.cloudstack.api.command.user.vmsnapshot.ListVMSnapshotCmd; import org.apache.cloudstack.context.CallContext; import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager; @@ -90,6 +89,11 @@ import com.cloud.utils.db.Filter; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.fsm.NoTransitionException; import com.cloud.utils.fsm.StateMachine2; @@ -482,31 +486,30 @@ public class VMSnapshotManagerImpl extends ManagerBase implements VMSnapshotMana } @DB - protected void processAnswer(VMSnapshotVO vmSnapshot, UserVmVO userVm, Answer as, Long hostId) { - final Transaction txn = Transaction.currentTxn(); + protected void processAnswer(final VMSnapshotVO vmSnapshot, UserVmVO userVm, final Answer as, Long hostId) { try { - txn.start(); - if (as instanceof CreateVMSnapshotAnswer) { - CreateVMSnapshotAnswer answer = (CreateVMSnapshotAnswer) as; - finalizeCreate(vmSnapshot, answer.getVolumeTOs()); - vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationSucceeded); - } else if (as instanceof RevertToVMSnapshotAnswer) { - RevertToVMSnapshotAnswer answer = (RevertToVMSnapshotAnswer) as; - finalizeRevert(vmSnapshot, answer.getVolumeTOs()); - vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationSucceeded); - } else if (as instanceof DeleteVMSnapshotAnswer) { - DeleteVMSnapshotAnswer answer = (DeleteVMSnapshotAnswer) as; - finalizeDelete(vmSnapshot, answer.getVolumeTOs()); - _vmSnapshotDao.remove(vmSnapshot.getId()); - } - txn.commit(); + Transaction.execute(new TransactionCallbackWithExceptionNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) throws NoTransitionException { + if (as instanceof CreateVMSnapshotAnswer) { + CreateVMSnapshotAnswer answer = (CreateVMSnapshotAnswer) as; + finalizeCreate(vmSnapshot, answer.getVolumeTOs()); + vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationSucceeded); + } else if (as instanceof RevertToVMSnapshotAnswer) { + RevertToVMSnapshotAnswer answer = (RevertToVMSnapshotAnswer) as; + finalizeRevert(vmSnapshot, answer.getVolumeTOs()); + vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationSucceeded); + } else if (as instanceof DeleteVMSnapshotAnswer) { + DeleteVMSnapshotAnswer answer = (DeleteVMSnapshotAnswer) as; + finalizeDelete(vmSnapshot, answer.getVolumeTOs()); + _vmSnapshotDao.remove(vmSnapshot.getId()); + } + } + }); } catch (Exception e) { String errMsg = "Error while process answer: " + as.getClass() + " due to " + e.getMessage(); s_logger.error(errMsg, e); - txn.rollback(); throw new CloudRuntimeException(errMsg); - } finally { - txn.close(); } } diff --git a/server/src/org/apache/cloudstack/affinity/AffinityGroupServiceImpl.java b/server/src/org/apache/cloudstack/affinity/AffinityGroupServiceImpl.java index 8f2caad9780..b7b51c9c29f 100644 --- a/server/src/org/apache/cloudstack/affinity/AffinityGroupServiceImpl.java +++ b/server/src/org/apache/cloudstack/affinity/AffinityGroupServiceImpl.java @@ -34,12 +34,16 @@ import org.apache.cloudstack.affinity.dao.AffinityGroupDao; import org.apache.cloudstack.affinity.dao.AffinityGroupDomainMapDao; import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao; import org.apache.cloudstack.context.CallContext; - import org.apache.log4j.Logger; import org.springframework.context.annotation.Primary; + + + + + import com.cloud.deploy.DeploymentPlanner; import com.cloud.domain.DomainVO; import com.cloud.domain.dao.DomainDao; @@ -65,6 +69,9 @@ import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.fsm.StateListener; import com.cloud.vm.UserVmVO; @@ -142,8 +149,8 @@ public class AffinityGroupServiceImpl extends ManagerBase implements AffinityGro @DB @Override - public AffinityGroup createAffinityGroupInternal(String account, Long domainId, String affinityGroupName, - String affinityGroupType, String description) { + public AffinityGroup createAffinityGroupInternal(String account, final Long domainId, final String affinityGroupName, + final String affinityGroupType, final String description) { Account caller = CallContext.current().getCallingAccount(); @@ -159,7 +166,7 @@ public class AffinityGroupServiceImpl extends ManagerBase implements AffinityGro "Unable to create affinity group, no Affinity Group Types configured"); } - AffinityGroupProcessor processor = typeProcessorMap.get(affinityGroupType); + final AffinityGroupProcessor processor = typeProcessorMap.get(affinityGroupType); if (processor.isAdminControlledGroup() && !_accountMgr.isRootAdmin(caller.getType())) { throw new PermissionDeniedException("Cannot create the affinity group"); @@ -210,21 +217,26 @@ public class AffinityGroupServiceImpl extends ManagerBase implements AffinityGro + affinityGroupName + " already exisits under the domain."); } - Transaction txn = Transaction.currentTxn(); - txn.start(); + final Account ownerFinal = owner; + final ControlledEntity.ACLType aclTypeFinal = aclType; + AffinityGroupVO group = Transaction.execute(new TransactionCallback() { + @Override + public AffinityGroupVO doInTransaction(TransactionStatus status) { + AffinityGroupVO group = new AffinityGroupVO(affinityGroupName, affinityGroupType, description, ownerFinal.getDomainId(), + ownerFinal.getId(), aclTypeFinal); + _affinityGroupDao.persist(group); + + if (domainId != null && aclTypeFinal == ACLType.Domain) { + boolean subDomainAccess = false; + subDomainAccess = processor.subDomainAccess(); + AffinityGroupDomainMapVO domainMap = new AffinityGroupDomainMapVO(group.getId(), domainId, subDomainAccess); + _affinityGroupDomainMapDao.persist(domainMap); + } + + return group; + } + }); - AffinityGroupVO group = new AffinityGroupVO(affinityGroupName, affinityGroupType, description, owner.getDomainId(), - owner.getId(), aclType); - _affinityGroupDao.persist(group); - - if (domainId != null && aclType == ACLType.Domain) { - boolean subDomainAccess = false; - subDomainAccess = processor.subDomainAccess(); - AffinityGroupDomainMapVO domainMap = new AffinityGroupDomainMapVO(group.getId(), domainId, subDomainAccess); - _affinityGroupDomainMapDao.persist(domainMap); - } - - txn.commit(); if (s_logger.isDebugEnabled()) { s_logger.debug("Created affinity group =" + affinityGroupName); @@ -265,35 +277,38 @@ public class AffinityGroupServiceImpl extends ManagerBase implements AffinityGro // check permissions _accountMgr.checkAccess(caller, AccessType.ModifyEntry, true, group); - final Transaction txn = Transaction.currentTxn(); - txn.start(); + final Long affinityGroupIdFinal = affinityGroupId; + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { - group = _affinityGroupDao.lockRow(affinityGroupId, true); - if (group == null) { - throw new InvalidParameterValueException("Unable to find affinity group by id " + affinityGroupId); - } - - List affinityGroupVmMap = _affinityGroupVMMapDao.listByAffinityGroup(affinityGroupId); - if (!affinityGroupVmMap.isEmpty()) { - SearchBuilder listByAffinityGroup = _affinityGroupVMMapDao.createSearchBuilder(); - listByAffinityGroup.and("affinityGroupId", listByAffinityGroup.entity().getAffinityGroupId(), - SearchCriteria.Op.EQ); - listByAffinityGroup.done(); - SearchCriteria sc = listByAffinityGroup.create(); - sc.setParameters("affinityGroupId", affinityGroupId); - - _affinityGroupVMMapDao.lockRows(sc, null, true); - _affinityGroupVMMapDao.remove(sc); - } - - // call processor to handle the group delete - AffinityGroupProcessor processor = getAffinityGroupProcessorForType(group.getType()); - if (processor != null) { - processor.handleDeleteGroup(group); - } - - _affinityGroupDao.expunge(affinityGroupId); - txn.commit(); + AffinityGroupVO group = _affinityGroupDao.lockRow(affinityGroupIdFinal, true); + if (group == null) { + throw new InvalidParameterValueException("Unable to find affinity group by id " + affinityGroupIdFinal); + } + + List affinityGroupVmMap = _affinityGroupVMMapDao.listByAffinityGroup(affinityGroupIdFinal); + if (!affinityGroupVmMap.isEmpty()) { + SearchBuilder listByAffinityGroup = _affinityGroupVMMapDao.createSearchBuilder(); + listByAffinityGroup.and("affinityGroupId", listByAffinityGroup.entity().getAffinityGroupId(), + SearchCriteria.Op.EQ); + listByAffinityGroup.done(); + SearchCriteria sc = listByAffinityGroup.create(); + sc.setParameters("affinityGroupId", affinityGroupIdFinal); + + _affinityGroupVMMapDao.lockRows(sc, null, true); + _affinityGroupVMMapDao.remove(sc); + } + + // call processor to handle the group delete + AffinityGroupProcessor processor = getAffinityGroupProcessorForType(group.getType()); + if (processor != null) { + processor.handleDeleteGroup(group); + } + + _affinityGroupDao.expunge(affinityGroupIdFinal); + } + }); if (s_logger.isDebugEnabled()) { s_logger.debug("Deleted affinity group id=" + affinityGroupId); diff --git a/server/src/org/apache/cloudstack/network/lb/ApplicationLoadBalancerManagerImpl.java b/server/src/org/apache/cloudstack/network/lb/ApplicationLoadBalancerManagerImpl.java index 2385edcd1c5..e2d33ac1844 100644 --- a/server/src/org/apache/cloudstack/network/lb/ApplicationLoadBalancerManagerImpl.java +++ b/server/src/org/apache/cloudstack/network/lb/ApplicationLoadBalancerManagerImpl.java @@ -26,7 +26,6 @@ import javax.inject.Inject; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; - import org.apache.cloudstack.acl.SecurityChecker.AccessType; import org.apache.cloudstack.api.command.user.loadbalancer.ListApplicationLoadBalancersCmd; import org.apache.cloudstack.context.CallContext; @@ -72,6 +71,8 @@ import com.cloud.utils.db.JoinBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallbackWithException; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.Ip; import com.cloud.utils.net.NetUtils; @@ -156,37 +157,40 @@ public class ApplicationLoadBalancerManagerImpl extends ManagerBase implements A @DB - protected ApplicationLoadBalancerRule persistLbRule(ApplicationLoadBalancerRuleVO newRule) throws NetworkRuleConflictException { - - Transaction txn = Transaction.currentTxn(); - txn.start(); - - //1) Persist the rule - newRule = _lbDao.persist(newRule); + protected ApplicationLoadBalancerRule persistLbRule(final ApplicationLoadBalancerRuleVO newRuleFinal) throws NetworkRuleConflictException { boolean success = true; - + ApplicationLoadBalancerRuleVO newRule = null; try { - //2) Detect conflicts - detectLbRulesConflicts(newRule); - if (!_firewallDao.setStateToAdd(newRule)) { - throw new CloudRuntimeException("Unable to update the state to add for " + newRule); - } - s_logger.debug("Load balancer " + newRule.getId() + " for Ip address " + newRule.getSourceIp().addr() + ", source port " - + newRule.getSourcePortStart() + ", instance port " + newRule.getDefaultPortStart() + " is added successfully."); - CallContext.current().setEventDetails("Load balancer Id: " + newRule.getId()); - Network ntwk = _networkModel.getNetwork(newRule.getNetworkId()); - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_LOAD_BALANCER_CREATE, newRule.getAccountId(), - ntwk.getDataCenterId(), newRule.getId(), null, LoadBalancingRule.class.getName(), - newRule.getUuid()); - txn.commit(); + newRule = Transaction.execute(new TransactionCallbackWithException() { + @Override + public ApplicationLoadBalancerRuleVO doInTransaction(TransactionStatus status) throws NetworkRuleConflictException { + //1) Persist the rule + ApplicationLoadBalancerRuleVO newRule = _lbDao.persist(newRuleFinal); + //2) Detect conflicts + detectLbRulesConflicts(newRule); + if (!_firewallDao.setStateToAdd(newRule)) { + throw new CloudRuntimeException("Unable to update the state to add for " + newRule); + } + s_logger.debug("Load balancer " + newRule.getId() + " for Ip address " + newRule.getSourceIp().addr() + ", source port " + + newRule.getSourcePortStart() + ", instance port " + newRule.getDefaultPortStart() + " is added successfully."); + CallContext.current().setEventDetails("Load balancer Id: " + newRule.getId()); + Network ntwk = _networkModel.getNetwork(newRule.getNetworkId()); + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_LOAD_BALANCER_CREATE, newRule.getAccountId(), + ntwk.getDataCenterId(), newRule.getId(), null, LoadBalancingRule.class.getName(), + newRule.getUuid()); + + return newRule; + } + }); + return newRule; } catch (Exception e) { success = false; if (e instanceof NetworkRuleConflictException) { throw (NetworkRuleConflictException) e; } - throw new CloudRuntimeException("Unable to add lb rule for ip address " + newRule.getSourceIpAddressId(), e); + throw new CloudRuntimeException("Unable to add lb rule for ip address " + newRuleFinal.getSourceIpAddressId(), e); } finally { if (!success && newRule != null) { _lbMgr.removeLBRule(newRule); diff --git a/server/src/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImpl.java b/server/src/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImpl.java index 25a40ee9051..62b9748de3a 100644 --- a/server/src/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImpl.java +++ b/server/src/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImpl.java @@ -37,6 +37,9 @@ import com.cloud.user.AccountManager; import com.cloud.utils.Pair; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionCallback; +import com.cloud.utils.db.TransactionCallbackNoReturn; +import com.cloud.utils.db.TransactionStatus; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; @@ -46,7 +49,6 @@ import org.apache.cloudstack.context.CallContext; import org.apache.cloudstack.framework.config.dao.ConfigurationDao; import org.apache.cloudstack.region.Region; import org.apache.cloudstack.region.dao.RegionDao; - import org.apache.log4j.Logger; import javax.ejb.Local; @@ -94,15 +96,15 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR "balancer rule", create = true) public GlobalLoadBalancerRule createGlobalLoadBalancerRule(CreateGlobalLoadBalancerRuleCmd newRule) { - Integer regionId = newRule.getRegionId(); - String algorithm = newRule.getAlgorithm(); - String stickyMethod = newRule.getStickyMethod(); - String name = newRule.getName(); - String description = newRule.getDescription(); - String domainName = newRule.getServiceDomainName(); - String serviceType = newRule.getServiceType(); + final Integer regionId = newRule.getRegionId(); + final String algorithm = newRule.getAlgorithm(); + final String stickyMethod = newRule.getStickyMethod(); + final String name = newRule.getName(); + final String description = newRule.getDescription(); + final String domainName = newRule.getServiceDomainName(); + final String serviceType = newRule.getServiceType(); - Account gslbOwner = _accountMgr.getAccount(newRule.getEntityOwnerId()); + final Account gslbOwner = _accountMgr.getAccount(newRule.getEntityOwnerId()); if (!GlobalLoadBalancerRule.Algorithm.isValidAlgorithm(algorithm)) { throw new InvalidParameterValueException("Invalid Algorithm: " + algorithm); @@ -135,18 +137,21 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR throw new CloudRuntimeException("GSLB service is not enabled in region : " + region.getName()); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - GlobalLoadBalancerRuleVO newGslbRule = new GlobalLoadBalancerRuleVO(name, description, domainName, algorithm, - stickyMethod, serviceType, regionId, gslbOwner.getId(), gslbOwner.getDomainId(), - GlobalLoadBalancerRule.State.Staged); - _gslbRuleDao.persist(newGslbRule); + GlobalLoadBalancerRuleVO newGslbRule = Transaction.execute(new TransactionCallback() { + @Override + public GlobalLoadBalancerRuleVO doInTransaction(TransactionStatus status) { + GlobalLoadBalancerRuleVO newGslbRule = new GlobalLoadBalancerRuleVO(name, description, domainName, algorithm, + stickyMethod, serviceType, regionId, gslbOwner.getId(), gslbOwner.getDomainId(), + GlobalLoadBalancerRule.State.Staged); + _gslbRuleDao.persist(newGslbRule); - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_GLOBAL_LOAD_BALANCER_CREATE, newGslbRule.getAccountId(), - 0, newGslbRule.getId(), name, GlobalLoadBalancerRule.class.getName(), - newGslbRule.getUuid()); + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_GLOBAL_LOAD_BALANCER_CREATE, newGslbRule.getAccountId(), + 0, newGslbRule.getId(), name, GlobalLoadBalancerRule.class.getName(), + newGslbRule.getUuid()); - txn.commit(); + return newGslbRule; + } + }); s_logger.debug("successfully created new global load balancer rule for the account " + gslbOwner.getId()); @@ -162,8 +167,8 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR CallContext ctx = CallContext.current(); Account caller = ctx.getCallingAccount(); - long gslbRuleId = assignToGslbCmd.getGlobalLoadBalancerRuleId(); - GlobalLoadBalancerRuleVO gslbRule = _gslbRuleDao.findById(gslbRuleId); + final long gslbRuleId = assignToGslbCmd.getGlobalLoadBalancerRuleId(); + final GlobalLoadBalancerRuleVO gslbRule = _gslbRuleDao.findById(gslbRuleId); if (gslbRule == null) { throw new InvalidParameterValueException("Invalid global load balancer rule id: " + gslbRuleId); } @@ -175,7 +180,7 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR + " is in revoked state"); } - List newLbRuleIds = assignToGslbCmd.getLoadBalancerRulesIds(); + final List newLbRuleIds = assignToGslbCmd.getLoadBalancerRulesIds(); if (newLbRuleIds == null || newLbRuleIds.isEmpty()) { throw new InvalidParameterValueException("empty list of load balancer rule Ids specified to be assigned" + " global load balancer rule"); @@ -244,30 +249,31 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR } } - Map lbRuleWeightMap = assignToGslbCmd.getLoadBalancerRuleWeightMap(); + final Map lbRuleWeightMap = assignToGslbCmd.getLoadBalancerRuleWeightMap(); - Transaction txn = Transaction.currentTxn(); - txn.start(); - - // persist the mapping for the new Lb rule that needs to assigned to a gslb rule - for (Long lbRuleId : newLbRuleIds) { - GlobalLoadBalancerLbRuleMapVO newGslbLbMap = new GlobalLoadBalancerLbRuleMapVO(); - newGslbLbMap.setGslbLoadBalancerId(gslbRuleId); - newGslbLbMap.setLoadBalancerId(lbRuleId); - if (lbRuleWeightMap != null && lbRuleWeightMap.get(lbRuleId) != null) { - newGslbLbMap.setWeight(lbRuleWeightMap.get(lbRuleId)); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + // persist the mapping for the new Lb rule that needs to assigned to a gslb rule + for (Long lbRuleId : newLbRuleIds) { + GlobalLoadBalancerLbRuleMapVO newGslbLbMap = new GlobalLoadBalancerLbRuleMapVO(); + newGslbLbMap.setGslbLoadBalancerId(gslbRuleId); + newGslbLbMap.setLoadBalancerId(lbRuleId); + if (lbRuleWeightMap != null && lbRuleWeightMap.get(lbRuleId) != null) { + newGslbLbMap.setWeight(lbRuleWeightMap.get(lbRuleId)); + } + _gslbLbMapDao.persist(newGslbLbMap); + } + + // mark the gslb rule state as add + if (gslbRule.getState() == GlobalLoadBalancerRule.State.Staged || gslbRule.getState() == + GlobalLoadBalancerRule.State.Active ) { + gslbRule.setState(GlobalLoadBalancerRule.State.Add); + _gslbRuleDao.update(gslbRule.getId(), gslbRule); + } } - _gslbLbMapDao.persist(newGslbLbMap); - } + }); - // mark the gslb rule state as add - if (gslbRule.getState() == GlobalLoadBalancerRule.State.Staged || gslbRule.getState() == - GlobalLoadBalancerRule.State.Active ) { - gslbRule.setState(GlobalLoadBalancerRule.State.Add); - _gslbRuleDao.update(gslbRule.getId(), gslbRule); - } - - txn.commit(); boolean success = false; try { @@ -304,8 +310,8 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR CallContext ctx = CallContext.current(); Account caller = ctx.getCallingAccount(); - long gslbRuleId = removeFromGslbCmd.getGlobalLoadBalancerRuleId(); - GlobalLoadBalancerRuleVO gslbRule = _gslbRuleDao.findById(gslbRuleId); + final long gslbRuleId = removeFromGslbCmd.getGlobalLoadBalancerRuleId(); + final GlobalLoadBalancerRuleVO gslbRule = _gslbRuleDao.findById(gslbRuleId); if (gslbRule == null) { throw new InvalidParameterValueException("Invalid global load balancer rule id: " + gslbRuleId); } @@ -316,7 +322,7 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR throw new InvalidParameterValueException("global load balancer rule id: " + gslbRuleId + " is already in revoked state"); } - List lbRuleIdsToremove = removeFromGslbCmd.getLoadBalancerRulesIds(); + final List lbRuleIdsToremove = removeFromGslbCmd.getLoadBalancerRulesIds(); if (lbRuleIdsToremove == null || lbRuleIdsToremove.isEmpty()) { throw new InvalidParameterValueException("empty list of load balancer rule Ids specified to be un-assigned" + " to global load balancer rule"); @@ -356,23 +362,24 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR } } - Transaction txn = Transaction.currentTxn(); - txn.start(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + // update the mapping of gslb rule to Lb rule, to revoke state + for (Long lbRuleId : lbRuleIdsToremove) { + GlobalLoadBalancerLbRuleMapVO removeGslbLbMap = _gslbLbMapDao.findByGslbRuleIdAndLbRuleId(gslbRuleId, lbRuleId); + removeGslbLbMap.setRevoke(true); + _gslbLbMapDao.update(removeGslbLbMap.getId(), removeGslbLbMap); + } - // update the mapping of gslb rule to Lb rule, to revoke state - for (Long lbRuleId : lbRuleIdsToremove) { - GlobalLoadBalancerLbRuleMapVO removeGslbLbMap = _gslbLbMapDao.findByGslbRuleIdAndLbRuleId(gslbRuleId, lbRuleId); - removeGslbLbMap.setRevoke(true); - _gslbLbMapDao.update(removeGslbLbMap.getId(), removeGslbLbMap); - } + // mark the gslb rule state as add + if (gslbRule.getState() == GlobalLoadBalancerRule.State.Staged) { + gslbRule.setState(GlobalLoadBalancerRule.State.Add); + _gslbRuleDao.update(gslbRule.getId(), gslbRule); + } - // mark the gslb rule state as add - if (gslbRule.getState() == GlobalLoadBalancerRule.State.Staged) { - gslbRule.setState(GlobalLoadBalancerRule.State.Add); - _gslbRuleDao.update(gslbRule.getId(), gslbRule); - } - - txn.commit(); + } + }); boolean success = false; try { @@ -387,19 +394,21 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR throw ex; } - txn.start(); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + // remove the mappings of gslb rule to Lb rule that are in revoked state + for (Long lbRuleId : lbRuleIdsToremove) { + GlobalLoadBalancerLbRuleMapVO removeGslbLbMap = _gslbLbMapDao.findByGslbRuleIdAndLbRuleId(gslbRuleId, lbRuleId); + _gslbLbMapDao.remove(removeGslbLbMap.getId()); + } - // remove the mappings of gslb rule to Lb rule that are in revoked state - for (Long lbRuleId : lbRuleIdsToremove) { - GlobalLoadBalancerLbRuleMapVO removeGslbLbMap = _gslbLbMapDao.findByGslbRuleIdAndLbRuleId(gslbRuleId, lbRuleId); - _gslbLbMapDao.remove(removeGslbLbMap.getId()); - } + // on success set state back to Active + gslbRule.setState(GlobalLoadBalancerRule.State.Active); + _gslbRuleDao.update(gslbRule.getId(), gslbRule); - // on success set state back to Active - gslbRule.setState(GlobalLoadBalancerRule.State.Active); - _gslbRuleDao.update(gslbRule.getId(), gslbRule); - - txn.commit(); + } + }); success = true; } catch (ResourceUnavailableException e) { @@ -429,9 +438,9 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR } @DB - private void revokeGslbRule(long gslbRuleId, Account caller) { + private void revokeGslbRule(final long gslbRuleId, Account caller) { - GlobalLoadBalancerRuleVO gslbRule = _gslbRuleDao.findById(gslbRuleId); + final GlobalLoadBalancerRuleVO gslbRule = _gslbRuleDao.findById(gslbRuleId); if (gslbRule == null) { throw new InvalidParameterValueException("Invalid global load balancer rule id: " + gslbRuleId); @@ -454,19 +463,21 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR _gslbRuleDao.update(gslbRuleId, gslbRule); } - Transaction txn = Transaction.currentTxn(); - txn.start(); - - List gslbLbMapVos = _gslbLbMapDao.listByGslbRuleId(gslbRuleId); - if (gslbLbMapVos != null) { - //mark all the GSLB-LB mapping to be in revoke state - for (GlobalLoadBalancerLbRuleMapVO gslbLbMap : gslbLbMapVos) { - gslbLbMap.setRevoke(true); - _gslbLbMapDao.update(gslbLbMap.getId(), gslbLbMap); + final List gslbLbMapVos = Transaction.execute(new TransactionCallback>() { + @Override + public List doInTransaction(TransactionStatus status) { + List gslbLbMapVos = _gslbLbMapDao.listByGslbRuleId(gslbRuleId); + if (gslbLbMapVos != null) { + //mark all the GSLB-LB mapping to be in revoke state + for (GlobalLoadBalancerLbRuleMapVO gslbLbMap : gslbLbMapVos) { + gslbLbMap.setRevoke(true); + _gslbLbMapDao.update(gslbLbMap.getId(), gslbLbMap); + } + } + + return gslbLbMapVos; } - } - - txn.commit(); + }); boolean success = false; try { @@ -478,22 +489,25 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR throw new CloudRuntimeException("Failed to update the gloabal load balancer"); } - txn.start(); - //remove all mappings between GSLB rule and load balancer rules - if (gslbLbMapVos != null) { - for (GlobalLoadBalancerLbRuleMapVO gslbLbMap : gslbLbMapVos) { - _gslbLbMapDao.remove(gslbLbMap.getId()); + Transaction.execute(new TransactionCallbackNoReturn() { + @Override + public void doInTransactionWithoutResult(TransactionStatus status) { + //remove all mappings between GSLB rule and load balancer rules + if (gslbLbMapVos != null) { + for (GlobalLoadBalancerLbRuleMapVO gslbLbMap : gslbLbMapVos) { + _gslbLbMapDao.remove(gslbLbMap.getId()); + } + } + + //remove the GSLB rule itself + _gslbRuleDao.remove(gslbRuleId); + + UsageEventUtils.publishUsageEvent(EventTypes.EVENT_GLOBAL_LOAD_BALANCER_DELETE, gslbRule.getAccountId(), + 0, gslbRule.getId(), gslbRule.getName(), GlobalLoadBalancerRule.class.getName(), + gslbRule.getUuid()); } - } + }); - //remove the GSLB rule itself - _gslbRuleDao.remove(gslbRuleId); - - UsageEventUtils.publishUsageEvent(EventTypes.EVENT_GLOBAL_LOAD_BALANCER_DELETE, gslbRule.getAccountId(), - 0, gslbRule.getId(), gslbRule.getName(), GlobalLoadBalancerRule.class.getName(), - gslbRule.getUuid()); - - txn.commit(); } @Override @@ -523,8 +537,6 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR throw new InvalidParameterValueException("Invalid persistence: " + stickyMethod); } - Transaction txn = Transaction.currentTxn(); - txn.start(); if (algorithm != null) { gslbRule.setAlgorithm(algorithm); } @@ -536,7 +548,6 @@ public class GlobalLoadBalancingRulesServiceImpl implements GlobalLoadBalancingR } gslbRule.setState(GlobalLoadBalancerRule.State.Add); _gslbRuleDao.update(gslbRule.getId(), gslbRule); - txn.commit(); try { s_logger.debug("Updating global load balancer with id " + gslbRule.getUuid()); diff --git a/server/test/com/cloud/configuration/ConfigurationManagerTest.java b/server/test/com/cloud/configuration/ConfigurationManagerTest.java index 908f0d01056..2d2c03dcbaf 100755 --- a/server/test/com/cloud/configuration/ConfigurationManagerTest.java +++ b/server/test/com/cloud/configuration/ConfigurationManagerTest.java @@ -40,7 +40,6 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; - import org.apache.cloudstack.api.command.admin.vlan.DedicatePublicIpRangeCmd; import org.apache.cloudstack.api.command.admin.vlan.ReleasePublicIpRangeCmd; import org.apache.cloudstack.context.CallContext; @@ -69,6 +68,7 @@ import com.cloud.user.ResourceLimitService; import com.cloud.user.UserVO; import com.cloud.user.dao.AccountDao; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.net.Ip; public class ConfigurationManagerTest { @@ -213,7 +213,7 @@ public class ConfigurationManagerTest { } void runDedicatePublicIpRangePostiveTest() throws Exception { - Transaction txn = Transaction.open("runDedicatePublicIpRangePostiveTest"); + TransactionLegacy txn = TransactionLegacy.open("runDedicatePublicIpRangePostiveTest"); when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(vlan); @@ -239,7 +239,7 @@ public class ConfigurationManagerTest { } void runDedicatePublicIpRangeInvalidRange() throws Exception { - Transaction txn = Transaction.open("runDedicatePublicIpRangeInvalidRange"); + TransactionLegacy txn = TransactionLegacy.open("runDedicatePublicIpRangeInvalidRange"); when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(null); try { @@ -252,7 +252,7 @@ public class ConfigurationManagerTest { } void runDedicatePublicIpRangeDedicatedRange() throws Exception { - Transaction txn = Transaction.open("runDedicatePublicIpRangeDedicatedRange"); + TransactionLegacy txn = TransactionLegacy.open("runDedicatePublicIpRangeDedicatedRange"); when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(vlan); @@ -281,7 +281,7 @@ public class ConfigurationManagerTest { } void runDedicatePublicIpRangeInvalidZone() throws Exception { - Transaction txn = Transaction.open("runDedicatePublicIpRangeInvalidZone"); + TransactionLegacy txn = TransactionLegacy.open("runDedicatePublicIpRangeInvalidZone"); when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(vlan); @@ -307,7 +307,7 @@ public class ConfigurationManagerTest { } void runDedicatePublicIpRangeIPAdressAllocated() throws Exception { - Transaction txn = Transaction.open("runDedicatePublicIpRangeIPAdressAllocated"); + TransactionLegacy txn = TransactionLegacy.open("runDedicatePublicIpRangeIPAdressAllocated"); when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(vlan); @@ -334,7 +334,7 @@ public class ConfigurationManagerTest { } void runReleasePublicIpRangePostiveTest1() throws Exception { - Transaction txn = Transaction.open("runReleasePublicIpRangePostiveTest1"); + TransactionLegacy txn = TransactionLegacy.open("runReleasePublicIpRangePostiveTest1"); when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(vlan); @@ -358,7 +358,7 @@ public class ConfigurationManagerTest { } void runReleasePublicIpRangePostiveTest2() throws Exception { - Transaction txn = Transaction.open("runReleasePublicIpRangePostiveTest2"); + TransactionLegacy txn = TransactionLegacy.open("runReleasePublicIpRangePostiveTest2"); when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(vlan); @@ -392,7 +392,7 @@ public class ConfigurationManagerTest { } void runReleasePublicIpRangeInvalidIpRange() throws Exception { - Transaction txn = Transaction.open("runReleasePublicIpRangeInvalidIpRange"); + TransactionLegacy txn = TransactionLegacy.open("runReleasePublicIpRangeInvalidIpRange"); when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(null); try { @@ -405,7 +405,7 @@ public class ConfigurationManagerTest { } void runReleaseNonDedicatedPublicIpRange() throws Exception { - Transaction txn = Transaction.open("runReleaseNonDedicatedPublicIpRange"); + TransactionLegacy txn = TransactionLegacy.open("runReleaseNonDedicatedPublicIpRange"); when(configurationMgr._vlanDao.findById(anyLong())).thenReturn(vlan); diff --git a/server/test/com/cloud/network/CreatePrivateNetworkTest.java b/server/test/com/cloud/network/CreatePrivateNetworkTest.java index 0e57b027217..9276309037c 100644 --- a/server/test/com/cloud/network/CreatePrivateNetworkTest.java +++ b/server/test/com/cloud/network/CreatePrivateNetworkTest.java @@ -36,7 +36,6 @@ import org.junit.Ignore; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; - import org.apache.cloudstack.acl.ControlledEntity.ACLType; import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; @@ -63,6 +62,7 @@ import com.cloud.user.AccountManager; import com.cloud.user.AccountVO; import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.exception.CloudRuntimeException; //@Ignore("Requires database to be set up") @@ -152,8 +152,8 @@ public class CreatePrivateNetworkTest { @Test @DB public void createInvalidlyHostedPrivateNetwork() { - Transaction __txn; - __txn = Transaction.open("createInvalidlyHostedPrivateNetworkTest"); + TransactionLegacy __txn; + __txn = TransactionLegacy.open("createInvalidlyHostedPrivateNetworkTest"); /* Network nw; */ try { /* nw = */ diff --git a/server/test/com/cloud/network/DedicateGuestVlanRangesTest.java b/server/test/com/cloud/network/DedicateGuestVlanRangesTest.java index d6ee630569e..ea7167b471f 100644 --- a/server/test/com/cloud/network/DedicateGuestVlanRangesTest.java +++ b/server/test/com/cloud/network/DedicateGuestVlanRangesTest.java @@ -37,7 +37,6 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; - import org.apache.cloudstack.api.command.admin.network.DedicateGuestVlanRangeCmd; import org.apache.cloudstack.api.command.admin.network.ListDedicatedGuestVlanRangesCmd; import org.apache.cloudstack.api.command.admin.network.ReleaseDedicatedGuestVlanRangeCmd; @@ -56,6 +55,7 @@ import com.cloud.user.AccountVO; import com.cloud.user.UserVO; import com.cloud.user.dao.AccountDao; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; public class DedicateGuestVlanRangesTest { @@ -177,7 +177,7 @@ public class DedicateGuestVlanRangesTest { } void runDedicateGuestVlanRangePostiveTest() throws Exception { - Transaction txn = Transaction.open("runDedicateGuestVlanRangePostiveTest"); + TransactionLegacy txn = TransactionLegacy.open("runDedicateGuestVlanRangePostiveTest"); Field dedicateVlanField = _dedicateGuestVlanRangeClass.getDeclaredField("vlan"); dedicateVlanField.setAccessible(true); @@ -213,7 +213,7 @@ public class DedicateGuestVlanRangesTest { } void runDedicateGuestVlanRangeInvalidFormat() throws Exception { - Transaction txn = Transaction.open("runDedicateGuestVlanRangeInvalidFormat"); + TransactionLegacy txn = TransactionLegacy.open("runDedicateGuestVlanRangeInvalidFormat"); Field dedicateVlanField = _dedicateGuestVlanRangeClass.getDeclaredField("vlan"); dedicateVlanField.setAccessible(true); @@ -234,7 +234,7 @@ public class DedicateGuestVlanRangesTest { } void runDedicateGuestVlanRangeInvalidRangeValue() throws Exception { - Transaction txn = Transaction.open("runDedicateGuestVlanRangeInvalidRangeValue"); + TransactionLegacy txn = TransactionLegacy.open("runDedicateGuestVlanRangeInvalidRangeValue"); Field dedicateVlanField = _dedicateGuestVlanRangeClass.getDeclaredField("vlan"); dedicateVlanField.setAccessible(true); @@ -255,7 +255,7 @@ public class DedicateGuestVlanRangesTest { } void runDedicateGuestVlanRangeAllocatedVlans() throws Exception { - Transaction txn = Transaction.open("runDedicateGuestVlanRangeAllocatedVlans"); + TransactionLegacy txn = TransactionLegacy.open("runDedicateGuestVlanRangeAllocatedVlans"); Field dedicateVlanField = _dedicateGuestVlanRangeClass.getDeclaredField("vlan"); dedicateVlanField.setAccessible(true); @@ -281,7 +281,7 @@ public class DedicateGuestVlanRangesTest { } void runDedicateGuestVlanRangeDedicatedRange() throws Exception { - Transaction txn = Transaction.open("runDedicateGuestVlanRangeDedicatedRange"); + TransactionLegacy txn = TransactionLegacy.open("runDedicateGuestVlanRangeDedicatedRange"); Field dedicateVlanField = _dedicateGuestVlanRangeClass.getDeclaredField("vlan"); dedicateVlanField.setAccessible(true); @@ -310,7 +310,7 @@ public class DedicateGuestVlanRangesTest { } void runDedicateGuestVlanRangePartiallyDedicated() throws Exception { - Transaction txn = Transaction.open("runDedicateGuestVlanRangePartiallyDedicated"); + TransactionLegacy txn = TransactionLegacy.open("runDedicateGuestVlanRangePartiallyDedicated"); Field dedicateVlanField = _dedicateGuestVlanRangeClass.getDeclaredField("vlan"); dedicateVlanField.setAccessible(true); @@ -339,7 +339,7 @@ public class DedicateGuestVlanRangesTest { } void runReleaseDedicatedGuestVlanRangePostiveTest() throws Exception { - Transaction txn = Transaction.open("runReleaseDedicatedGuestVlanRangePostiveTest"); + TransactionLegacy txn = TransactionLegacy.open("runReleaseDedicatedGuestVlanRangePostiveTest"); AccountGuestVlanMapVO accountGuestVlanMap = new AccountGuestVlanMapVO(1L, 1L); when(networkService._accountGuestVlanMapDao.findById(anyLong())).thenReturn(accountGuestVlanMap); @@ -357,7 +357,7 @@ public class DedicateGuestVlanRangesTest { } void runReleaseDedicatedGuestVlanRangeInvalidRange() throws Exception { - Transaction txn = Transaction.open("runReleaseDedicatedGuestVlanRangeInvalidRange"); + TransactionLegacy txn = TransactionLegacy.open("runReleaseDedicatedGuestVlanRangeInvalidRange"); when(networkService._accountGuestVlanMapDao.findById(anyLong())).thenReturn(null); diff --git a/server/test/com/cloud/network/UpdatePhysicalNetworkTest.java b/server/test/com/cloud/network/UpdatePhysicalNetworkTest.java index 1c0eff6453a..264f91ccdff 100644 --- a/server/test/com/cloud/network/UpdatePhysicalNetworkTest.java +++ b/server/test/com/cloud/network/UpdatePhysicalNetworkTest.java @@ -24,7 +24,8 @@ import com.cloud.network.dao.PhysicalNetworkDao; import com.cloud.network.dao.PhysicalNetworkVO; import com.cloud.utils.Pair; import com.cloud.utils.db.Transaction; -import org.junit.Test; +import com.cloud.utils.db.TransactionLegacy; + import org.junit.*; import org.mockito.ArgumentCaptor; import org.mockito.MockitoAnnotations.*; @@ -58,7 +59,7 @@ public class UpdatePhysicalNetworkTest { @Test public void updatePhysicalNetworkTest(){ - Transaction txn = Transaction.open("updatePhysicalNetworkTest"); + TransactionLegacy txn = TransactionLegacy.open("updatePhysicalNetworkTest"); NetworkServiceImpl networkService = setUp(); existingRange.add("524"); when(_physicalNetworkDao.findById(anyLong())).thenReturn(physicalNetworkVO); diff --git a/server/test/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImplTest.java b/server/test/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImplTest.java index d0f09513e29..504ab9cebb2 100644 --- a/server/test/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImplTest.java +++ b/server/test/org/apache/cloudstack/region/gslb/GlobalLoadBalancingRulesServiceImplTest.java @@ -33,7 +33,6 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; - import org.apache.cloudstack.api.command.user.region.ha.gslb.AssignToGlobalLoadBalancerRuleCmd; import org.apache.cloudstack.api.command.user.region.ha.gslb.CreateGlobalLoadBalancerRuleCmd; import org.apache.cloudstack.api.command.user.region.ha.gslb.DeleteGlobalLoadBalancerRuleCmd; @@ -59,6 +58,7 @@ import com.cloud.user.AccountManager; import com.cloud.user.AccountVO; import com.cloud.user.UserVO; import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.net.Ip; public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { @@ -181,7 +181,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { void runCreateGlobalLoadBalancerRulePostiveTest() throws Exception { - Transaction txn = Transaction.open("runCreateGlobalLoadBalancerRulePostiveTest"); + TransactionLegacy txn = TransactionLegacy.open("runCreateGlobalLoadBalancerRulePostiveTest"); GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); @@ -246,7 +246,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { void runCreateGlobalLoadBalancerRuleInvalidAlgorithm() throws Exception { - Transaction txn = Transaction.open("runCreateGlobalLoadBalancerRulePostiveTest"); + TransactionLegacy txn = TransactionLegacy.open("runCreateGlobalLoadBalancerRulePostiveTest"); GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); @@ -311,7 +311,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { void runCreateGlobalLoadBalancerRuleInvalidStickyMethod() throws Exception { - Transaction txn = Transaction.open("runCreateGlobalLoadBalancerRulePostiveTest"); + TransactionLegacy txn = TransactionLegacy.open("runCreateGlobalLoadBalancerRulePostiveTest"); GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); @@ -376,7 +376,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { void runCreateGlobalLoadBalancerRuleInvalidServiceType() throws Exception { - Transaction txn = Transaction.open("runCreateGlobalLoadBalancerRulePostiveTest"); + TransactionLegacy txn = TransactionLegacy.open("runCreateGlobalLoadBalancerRulePostiveTest"); GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); @@ -441,7 +441,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { void runCreateGlobalLoadBalancerRuleInvalidDomainName() throws Exception { - Transaction txn = Transaction.open("runCreateGlobalLoadBalancerRulePostiveTest"); + TransactionLegacy txn = TransactionLegacy.open("runCreateGlobalLoadBalancerRulePostiveTest"); GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); @@ -507,7 +507,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { void runAssignToGlobalLoadBalancerRuleTest() throws Exception { - Transaction txn = Transaction.open("runAssignToGlobalLoadBalancerRuleTest"); + TransactionLegacy txn = TransactionLegacy.open("runAssignToGlobalLoadBalancerRuleTest"); GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); @@ -570,7 +570,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { void runAssignToGlobalLoadBalancerRuleTestSameZoneLb() throws Exception { - Transaction txn = Transaction.open("runAssignToGlobalLoadBalancerRuleTestSameZoneLb"); + TransactionLegacy txn = TransactionLegacy.open("runAssignToGlobalLoadBalancerRuleTestSameZoneLb"); GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); @@ -655,7 +655,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { void runAssignToGlobalLoadBalancerRuleTestRevokedState() throws Exception { - Transaction txn = Transaction.open("runAssignToGlobalLoadBalancerRuleTestRevokedState"); + TransactionLegacy txn = TransactionLegacy.open("runAssignToGlobalLoadBalancerRuleTestRevokedState"); GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); @@ -712,7 +712,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { void runRemoveFromGlobalLoadBalancerRuleTest() throws Exception { - Transaction txn = Transaction.open("runRemoveFromGlobalLoadBalancerRuleTest"); + TransactionLegacy txn = TransactionLegacy.open("runRemoveFromGlobalLoadBalancerRuleTest"); GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); @@ -785,7 +785,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { void runRemoveFromGlobalLoadBalancerRuleTestUnassignedLb() throws Exception { - Transaction txn = Transaction.open("runRemoveFromGlobalLoadBalancerRuleTestUnassignedLb"); + TransactionLegacy txn = TransactionLegacy.open("runRemoveFromGlobalLoadBalancerRuleTestUnassignedLb"); GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); @@ -846,7 +846,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { void runRemoveFromGlobalLoadBalancerRuleTestInvalidLb() throws Exception { - Transaction txn = Transaction.open("runRemoveFromGlobalLoadBalancerRuleTestInvalidLb"); + TransactionLegacy txn = TransactionLegacy.open("runRemoveFromGlobalLoadBalancerRuleTestInvalidLb"); GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); @@ -890,7 +890,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { void runDeleteGlobalLoadBalancerRuleTestWithNoLbRules() throws Exception { - Transaction txn = Transaction.open("runDeleteGlobalLoadBalancerRuleTestWithNoLbRules"); + TransactionLegacy txn = TransactionLegacy.open("runDeleteGlobalLoadBalancerRuleTestWithNoLbRules"); GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); @@ -936,7 +936,7 @@ public class GlobalLoadBalancingRulesServiceImplTest extends TestCase { void runDeleteGlobalLoadBalancerRuleTestWithLbRules() throws Exception { - Transaction txn = Transaction.open("runDeleteGlobalLoadBalancerRuleTestWithLbRules"); + TransactionLegacy txn = TransactionLegacy.open("runDeleteGlobalLoadBalancerRuleTestWithLbRules"); GlobalLoadBalancingRulesServiceImpl gslbServiceImpl = new GlobalLoadBalancingRulesServiceImpl(); diff --git a/usage/src/com/cloud/usage/UsageManagerImpl.java b/usage/src/com/cloud/usage/UsageManagerImpl.java index 3ff53e0e6f6..1ee21c97e9a 100644 --- a/usage/src/com/cloud/usage/UsageManagerImpl.java +++ b/usage/src/com/cloud/usage/UsageManagerImpl.java @@ -84,7 +84,7 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.Filter; import com.cloud.utils.db.GlobalLock; import com.cloud.utils.db.SearchCriteria; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; @Component @Local(value={UsageManager.class}) @@ -248,7 +248,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna m_sanity = m_sanityExecutor.scheduleAtFixedRate(new SanityCheck(), 1, m_sanityCheckInterval, TimeUnit.DAYS); } - Transaction usageTxn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy usageTxn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); try { if(m_heartbeatLock.lock(3)) { // 3 second timeout try { @@ -381,7 +381,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna s_logger.info("not parsing usage records since start time mills (" + startDateMillis + ") is on or after end time millis (" + endDateMillis + ")"); } - Transaction jobUpdateTxn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy jobUpdateTxn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); try { jobUpdateTxn.start(); // everything seemed to work...set endDate as the last success date @@ -411,7 +411,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna Map networkStats = null; List vmDiskStats = null; Map vmDiskUsages = null; - Transaction userTxn = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy userTxn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); try { Long limit = Long.valueOf(500); Long offset = Long.valueOf(0); @@ -552,7 +552,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna List events = _usageEventDao.getRecentEvents(new Date(endDateMillis)); - Transaction usageTxn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy usageTxn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); try { usageTxn.start(); @@ -779,7 +779,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna usageTxn.close(); // switch back to CLOUD_DB - Transaction swap = Transaction.open(Transaction.CLOUD_DB); + TransactionLegacy swap = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); if(!success){ _alertMgr.sendAlert(AlertManager.ALERT_TYPE_USAGE_SERVER_RESULT, 0, new Long(0), "Usage job failed. Job id: "+job.getId(), "Usage job failed. Job id: "+job.getId()); } else { @@ -1664,7 +1664,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna private class Heartbeat extends ManagedContextRunnable { @Override protected void runInContext() { - Transaction usageTxn = Transaction.open(Transaction.USAGE_DB); + TransactionLegacy usageTxn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); try { if(!m_heartbeatLock.lock(3)) { // 3 second timeout if(s_logger.isTraceEnabled()) @@ -1723,7 +1723,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna @DB protected boolean updateJob(Long jobId, String hostname, Integer pid, Date heartbeat, int scheduled) { boolean changeOwner = false; - Transaction txn = Transaction.currentTxn(); + TransactionLegacy txn = TransactionLegacy.currentTxn(); try { txn.start(); diff --git a/usage/src/com/cloud/usage/UsageSanityChecker.java b/usage/src/com/cloud/usage/UsageSanityChecker.java index ed78a154047..35fbe8178fa 100644 --- a/usage/src/com/cloud/usage/UsageSanityChecker.java +++ b/usage/src/com/cloud/usage/UsageSanityChecker.java @@ -26,7 +26,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import com.cloud.utils.db.Transaction; +import com.cloud.utils.db.TransactionLegacy; public class UsageSanityChecker { @@ -186,7 +186,7 @@ public class UsageSanityChecker { // Error while reading last check id } - Connection conn = Transaction.getStandaloneConnection(); + Connection conn = TransactionLegacy.getStandaloneConnection(); int maxId = 0; PreparedStatement pstmt = conn.prepareStatement("select max(id) from cloud_usage.cloud_usage"); ResultSet rs = pstmt.executeQuery(); diff --git a/utils/src/com/cloud/utils/exception/ExceptionUtil.java b/utils/src/com/cloud/utils/exception/ExceptionUtil.java index 977af928c14..322c23dc754 100644 --- a/utils/src/com/cloud/utils/exception/ExceptionUtil.java +++ b/utils/src/com/cloud/utils/exception/ExceptionUtil.java @@ -35,4 +35,16 @@ public class ExceptionUtil { } return writer.toString(); } + + @SuppressWarnings("unchecked") + public static void rethrow(Throwable t, Class clz) throws T { + if ( clz.isAssignableFrom(t.getClass()) ) + throw (T)t; + } + + public static void rethrowRuntime(Throwable t) { + rethrow(t, RuntimeException.class); + rethrow(t, Error.class); + } + } diff --git a/utils/test/com/cloud/utils/exception/ExceptionUtilTest.java b/utils/test/com/cloud/utils/exception/ExceptionUtilTest.java new file mode 100644 index 00000000000..33fca37692b --- /dev/null +++ b/utils/test/com/cloud/utils/exception/ExceptionUtilTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.cloud.utils.exception; + +import static org.junit.Assert.*; + +import java.io.FileNotFoundException; +import java.io.IOException; + +import org.junit.Test; + +public class ExceptionUtilTest { + + @Test + public void test() throws Exception { + FileNotFoundException fnfe = new FileNotFoundException(); + try { + ExceptionUtil.rethrow(fnfe, IOException.class); + fail(); + } catch (IOException e) { + } + + ExceptionUtil.rethrow(fnfe, ClassNotFoundException.class); + + try { + ExceptionUtil.rethrow(fnfe, FileNotFoundException.class); + fail(); + } catch ( FileNotFoundException e ) { + } + } + +}