From cb0fde68f84b5c95640933a4ab979b6abaae2e1c Mon Sep 17 00:00:00 2001 From: Abhinandan Prateek Date: Mon, 19 Oct 2015 13:59:01 +0530 Subject: [PATCH] CLOUDSTACK-8592: fixed QuotaBalanceDaoImpl to use the new Transaction --- .../quota/dao/QuotaBalanceDaoImpl.java | 231 +++++++++--------- .../cloudstack/quota/QuotaServiceImpl.java | 21 +- 2 files changed, 118 insertions(+), 134 deletions(-) diff --git a/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaBalanceDaoImpl.java b/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaBalanceDaoImpl.java index 0b7a86ce52a..2e1c6ef8a34 100644 --- a/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaBalanceDaoImpl.java +++ b/framework/quota/src/org/apache/cloudstack/quota/dao/QuotaBalanceDaoImpl.java @@ -20,13 +20,17 @@ import com.cloud.exception.InvalidParameterValueException; 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.TransactionCallback; import com.cloud.utils.db.TransactionLegacy; -import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.db.TransactionStatus; + import org.apache.cloudstack.quota.vo.QuotaBalanceVO; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; import javax.ejb.Local; + import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collections; @@ -41,158 +45,141 @@ public class QuotaBalanceDaoImpl extends GenericDaoBase im @SuppressWarnings("deprecation") @Override public QuotaBalanceVO findLastBalanceEntry(final Long accountId, final Long domainId, final Date beforeThis) { - List quotaBalanceEntries = new ArrayList<>(); - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) { - Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", false, 0L, 1L); - SearchCriteria sc = createSearchCriteria(); - sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); - sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); - sc.addAnd("creditsId", SearchCriteria.Op.EQ, 0); - sc.addAnd("updatedOn", SearchCriteria.Op.LT, beforeThis); - quotaBalanceEntries = this.search(sc, filter); - } catch (Exception e) { - s_logger.error("QuotaBalanceDaoImpl::findLastBalanceEntry() failed due to: " + e.getMessage()); - throw new CloudRuntimeException("Unable to find last quota balance entry for account"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return quotaBalanceEntries.size() > 0 ? quotaBalanceEntries.get(0) : null; + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback() { + @Override + public QuotaBalanceVO doInTransaction(final TransactionStatus status) { + List quotaBalanceEntries = new ArrayList<>(); + Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", false, 0L, 1L); + SearchCriteria sc = createSearchCriteria(); + sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); + sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); + sc.addAnd("creditsId", SearchCriteria.Op.EQ, 0); + sc.addAnd("updatedOn", SearchCriteria.Op.LT, beforeThis); + quotaBalanceEntries = search(sc, filter); + return quotaBalanceEntries.size() > 0 ? quotaBalanceEntries.get(0) : null; + } + }); } @SuppressWarnings("deprecation") @Override public QuotaBalanceVO findLaterBalanceEntry(final Long accountId, final Long domainId, final Date afterThis) { - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - List quotaBalanceEntries = new ArrayList<>(); - try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) { - Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", true, 0L, 1L); - SearchCriteria sc = createSearchCriteria(); - sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); - sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); - sc.addAnd("creditsId", SearchCriteria.Op.EQ, 0); - sc.addAnd("updatedOn", SearchCriteria.Op.GT, afterThis); - quotaBalanceEntries = this.search(sc, filter); - } catch (Exception e) { - s_logger.error("QuotaBalanceDaoImpl::findLaterBalanceEntry() failed due to: " + e.getMessage()); - throw new CloudRuntimeException("Unable to find later quota balance entry"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return quotaBalanceEntries.size() > 0 ? quotaBalanceEntries.get(0) : null; + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback() { + @Override + public QuotaBalanceVO doInTransaction(final TransactionStatus status) { + List quotaBalanceEntries = new ArrayList<>(); + Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", true, 0L, 1L); + SearchCriteria sc = createSearchCriteria(); + sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); + sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); + sc.addAnd("creditsId", SearchCriteria.Op.EQ, 0); + sc.addAnd("updatedOn", SearchCriteria.Op.GT, afterThis); + quotaBalanceEntries = search(sc, filter); + return quotaBalanceEntries.size() > 0 ? quotaBalanceEntries.get(0) : null; + } + }); } @Override public void saveQuotaBalance(final List credits) { - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) { - for (QuotaBalanceVO credit : credits) { - persist(credit); + Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback() { + @Override + public Void doInTransaction(final TransactionStatus status) { + for (QuotaBalanceVO credit : credits) { + persist(credit); + } + return null; } - } catch (Exception e) { - throw new CloudRuntimeException("Unable to save quota balance"); - } finally { - TransactionLegacy.open(opendb).close(); - } + }); } @SuppressWarnings("deprecation") @Override public List findCreditBalance(final Long accountId, final Long domainId, final Date lastbalancedate, final Date beforeThis) { - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - List quotaBalances = new ArrayList<>(); - try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) { - Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", true, 0L, Long.MAX_VALUE); - SearchCriteria sc = createSearchCriteria(); - sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); - sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); - sc.addAnd("creditsId", SearchCriteria.Op.GT, 0); - if ((lastbalancedate != null) && (beforeThis != null) && lastbalancedate.before(beforeThis)) { - sc.addAnd("updatedOn", SearchCriteria.Op.BETWEEN, lastbalancedate, beforeThis); - } else { - return new ArrayList(); + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback>() { + @Override + public List doInTransaction(final TransactionStatus status) { + Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", true, 0L, Long.MAX_VALUE); + SearchCriteria sc = createSearchCriteria(); + sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); + sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); + sc.addAnd("creditsId", SearchCriteria.Op.GT, 0); + if ((lastbalancedate != null) && (beforeThis != null) && lastbalancedate.before(beforeThis)) { + sc.addAnd("updatedOn", SearchCriteria.Op.BETWEEN, lastbalancedate, beforeThis); + } else { + return new ArrayList(); + } + return search(sc, filter); } - quotaBalances = search(sc, filter); - } catch (Exception e) { - s_logger.error("QuotaBalanceDaoImpl::findCreditBalance() failed due to: " + e.getMessage()); - throw new CloudRuntimeException("Unable to find quota credit balance"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return quotaBalances; + }); } @SuppressWarnings("deprecation") @Override public List findQuotaBalance(final Long accountId, final Long domainId, final Date startDate, final Date endDate) { - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback>() { + @Override + public List doInTransaction(final TransactionStatus status) { + List quotaUsageRecords = null; + SearchCriteria sc = createSearchCriteria(); + if (accountId != null) { + sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); + } + if (domainId != null) { + sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); + } + if ((startDate != null) && (endDate != null) && startDate.before(endDate)) { + sc.addAnd("updatedOn", SearchCriteria.Op.BETWEEN, startDate, endDate); + } else { + return Collections. emptyList(); + } + quotaUsageRecords = listBy(sc); + if (quotaUsageRecords.size() == 0) { + quotaUsageRecords.addAll(lastQuotaBalanceVO(accountId, domainId, startDate)); + } + return quotaUsageRecords; - List quotaUsageRecords = null; - try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) { - SearchCriteria sc = createSearchCriteria(); - if (accountId != null) { - sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); } - if (domainId != null) { - sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); - } - if ((startDate != null) && (endDate != null) && startDate.before(endDate)) { - sc.addAnd("updatedOn", SearchCriteria.Op.BETWEEN, startDate, endDate); - } else { - return Collections.emptyList(); - } - quotaUsageRecords = listBy(sc); - if (quotaUsageRecords.size() == 0) { - quotaUsageRecords.addAll(lastQuotaBalanceVO(accountId, domainId, startDate)); - } - } catch (Exception e) { - throw new CloudRuntimeException("Unable to find quota balance"); - } finally { - TransactionLegacy.open(opendb).close(); - } + }); - return quotaUsageRecords; } - @SuppressWarnings("deprecation") @Override public List lastQuotaBalanceVO(final Long accountId, final Long domainId, final Date pivotDate) { - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - List quotaUsageRecords = null; - List trimmedRecords = new ArrayList(); - try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) { - Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", false, 0L, 100L); - // ASSUMPTION there will be less than 100 continuous credit - // transactions - SearchCriteria sc = createSearchCriteria(); - if (accountId != null) { - sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); - } - if (domainId != null) { - sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); - } - if ((pivotDate != null)) { - sc.addAnd("updatedOn", SearchCriteria.Op.LTEQ, pivotDate); - } - quotaUsageRecords = search(sc, filter); - - // get records before startDate to find start balance - for (QuotaBalanceVO entry : quotaUsageRecords) { - s_logger.debug("FindQuotaBalance Date=" + entry.getUpdatedOn().toGMTString() + " balance=" + entry.getCreditBalance() + " credit=" + entry.getCreditsId()); - if (entry.getCreditsId() > 0) { - trimmedRecords.add(entry); - } else { - trimmedRecords.add(entry); - break; // add only consecutive credit entries and last balance entry + return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback>() { + @Override + public List doInTransaction(final TransactionStatus status) { + List quotaUsageRecords = null; + List trimmedRecords = new ArrayList(); + Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", false, 0L, 100L); + // ASSUMPTION there will be less than 100 continuous credit + // transactions + SearchCriteria sc = createSearchCriteria(); + if (accountId != null) { + sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId); } + if (domainId != null) { + sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId); + } + if ((pivotDate != null)) { + sc.addAnd("updatedOn", SearchCriteria.Op.LTEQ, pivotDate); + } + quotaUsageRecords = search(sc, filter); + + // get records before startDate to find start balance + for (QuotaBalanceVO entry : quotaUsageRecords) { + s_logger.debug("FindQuotaBalance Date=" + entry.getUpdatedOn().toGMTString() + " balance=" + entry.getCreditBalance() + " credit=" + entry.getCreditsId()); + if (entry.getCreditsId() > 0) { + trimmedRecords.add(entry); + } else { + trimmedRecords.add(entry); + break; // add only consecutive credit entries and last balance entry + } + } + return trimmedRecords; } - } catch (Exception e) { - throw new CloudRuntimeException("Unable to get last quota balance"); - } finally { - TransactionLegacy.open(opendb).close(); - } - return trimmedRecords; + }); } @Override diff --git a/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaServiceImpl.java b/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaServiceImpl.java index 13265e7caeb..ac46ee74679 100644 --- a/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaServiceImpl.java +++ b/plugins/database/quota/src/org/apache/cloudstack/quota/QuotaServiceImpl.java @@ -104,7 +104,7 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi s_logger.warn("Usage stats job aggregation range is to small, using the minimum value of " + UsageUtils.USAGE_AGGREGATION_RANGE_MIN); _aggregationDuration = UsageUtils.USAGE_AGGREGATION_RANGE_MIN; } - if (s_logger.isDebugEnabled()){ + if (s_logger.isDebugEnabled()) { s_logger.debug("Usage timezone = " + _usageTimezone + " AggregationDuration=" + _aggregationDuration); } return true; @@ -133,8 +133,8 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi @Override public ConfigKey[] getConfigKeys() { - return new ConfigKey[] { QuotaPluginEnabled, QuotaEnableEnforcement, QuotaCurrencySymbol, QuotaSmtpHost, QuotaSmtpPort, QuotaSmtpTimeout, QuotaSmtpUser, - QuotaSmtpPassword, QuotaSmtpAuthType, QuotaSmtpSender }; + return new ConfigKey[] { QuotaPluginEnabled, QuotaEnableEnforcement, QuotaCurrencySymbol, QuotaSmtpHost, QuotaSmtpPort, QuotaSmtpTimeout, QuotaSmtpUser, QuotaSmtpPassword, + QuotaSmtpAuthType, QuotaSmtpSender }; } public Boolean isQuotaServiceEnabled() { @@ -143,9 +143,6 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi @Override public List findQuotaBalanceVO(Long accountId, String accountName, Long domainId, Date startDate, Date endDate) { - final short opendb = TransactionLegacy.currentTxn().getDatabaseId(); - TransactionLegacy.open(TransactionLegacy.CLOUD_DB).close(); - if ((accountId == null) && (accountName != null) && (domainId != null)) { Account userAccount = null; Account caller = CallContext.current().getCallingAccount(); @@ -164,18 +161,17 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi throw new PermissionDeniedException("Invalid Domain Id or Account"); } } - TransactionLegacy.open(opendb).close(); startDate = startDate == null ? new Date() : startDate; if (endDate == null) { // adjust start date to end of day as there is no end date Date adjustedStartDate = computeAdjustedTime(_respBldr.startOfNextDay(startDate)); - if (s_logger.isDebugEnabled()){ + if (s_logger.isDebugEnabled()) { s_logger.debug("getQuotaBalance1: Getting quota balance records for account: " + accountId + ", domainId: " + domainId + ", on or before " + adjustedStartDate); } List qbrecords = _quotaBalanceDao.lastQuotaBalanceVO(accountId, domainId, adjustedStartDate); - if (s_logger.isDebugEnabled()){ + if (s_logger.isDebugEnabled()) { s_logger.debug("Found records size=" + qbrecords.size()); } if (qbrecords.isEmpty()) { @@ -189,11 +185,12 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi throw new InvalidParameterValueException("Incorrect Date Range. End date:" + endDate + " should not be in future. "); } else if (startDate.before(endDate)) { Date adjustedEndDate = computeAdjustedTime(endDate); - if (s_logger.isDebugEnabled()){ - s_logger.debug("getQuotaBalance2: Getting quota balance records for account: " + accountId + ", domainId: " + domainId + ", between " + adjustedStartDate + " and " + adjustedEndDate); + if (s_logger.isDebugEnabled()) { + s_logger.debug("getQuotaBalance2: Getting quota balance records for account: " + accountId + ", domainId: " + domainId + ", between " + adjustedStartDate + " and " + + adjustedEndDate); } List qbrecords = _quotaBalanceDao.findQuotaBalance(accountId, domainId, adjustedStartDate, adjustedEndDate); - if (s_logger.isDebugEnabled()){ + if (s_logger.isDebugEnabled()) { s_logger.debug("getQuotaBalance3: Found records size=" + qbrecords.size()); } if (qbrecords.isEmpty()) {