mirror of https://github.com/apache/cloudstack.git
CLOUDSTACK-8592: fixed QuotaBalanceDaoImpl to use the new Transaction
This commit is contained in:
parent
86e2e279a7
commit
cb0fde68f8
|
|
@ -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<QuotaBalanceVO, Long> im
|
|||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public QuotaBalanceVO findLastBalanceEntry(final Long accountId, final Long domainId, final Date beforeThis) {
|
||||
List<QuotaBalanceVO> 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<QuotaBalanceVO> 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<QuotaBalanceVO>() {
|
||||
@Override
|
||||
public QuotaBalanceVO doInTransaction(final TransactionStatus status) {
|
||||
List<QuotaBalanceVO> quotaBalanceEntries = new ArrayList<>();
|
||||
Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", false, 0L, 1L);
|
||||
SearchCriteria<QuotaBalanceVO> 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<QuotaBalanceVO> quotaBalanceEntries = new ArrayList<>();
|
||||
try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
|
||||
Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", true, 0L, 1L);
|
||||
SearchCriteria<QuotaBalanceVO> 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<QuotaBalanceVO>() {
|
||||
@Override
|
||||
public QuotaBalanceVO doInTransaction(final TransactionStatus status) {
|
||||
List<QuotaBalanceVO> quotaBalanceEntries = new ArrayList<>();
|
||||
Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", true, 0L, 1L);
|
||||
SearchCriteria<QuotaBalanceVO> 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<QuotaBalanceVO> 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<Void>() {
|
||||
@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<QuotaBalanceVO> findCreditBalance(final Long accountId, final Long domainId, final Date lastbalancedate, final Date beforeThis) {
|
||||
final short opendb = TransactionLegacy.currentTxn().getDatabaseId();
|
||||
List<QuotaBalanceVO> quotaBalances = new ArrayList<>();
|
||||
try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
|
||||
Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", true, 0L, Long.MAX_VALUE);
|
||||
SearchCriteria<QuotaBalanceVO> 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<QuotaBalanceVO>();
|
||||
return Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallback<List<QuotaBalanceVO>>() {
|
||||
@Override
|
||||
public List<QuotaBalanceVO> doInTransaction(final TransactionStatus status) {
|
||||
Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", true, 0L, Long.MAX_VALUE);
|
||||
SearchCriteria<QuotaBalanceVO> 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<QuotaBalanceVO>();
|
||||
}
|
||||
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<QuotaBalanceVO> 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<List<QuotaBalanceVO>>() {
|
||||
@Override
|
||||
public List<QuotaBalanceVO> doInTransaction(final TransactionStatus status) {
|
||||
List<QuotaBalanceVO> quotaUsageRecords = null;
|
||||
SearchCriteria<QuotaBalanceVO> 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.<QuotaBalanceVO> emptyList();
|
||||
}
|
||||
quotaUsageRecords = listBy(sc);
|
||||
if (quotaUsageRecords.size() == 0) {
|
||||
quotaUsageRecords.addAll(lastQuotaBalanceVO(accountId, domainId, startDate));
|
||||
}
|
||||
return quotaUsageRecords;
|
||||
|
||||
List<QuotaBalanceVO> quotaUsageRecords = null;
|
||||
try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
|
||||
SearchCriteria<QuotaBalanceVO> 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.<QuotaBalanceVO>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<QuotaBalanceVO> lastQuotaBalanceVO(final Long accountId, final Long domainId, final Date pivotDate) {
|
||||
final short opendb = TransactionLegacy.currentTxn().getDatabaseId();
|
||||
List<QuotaBalanceVO> quotaUsageRecords = null;
|
||||
List<QuotaBalanceVO> trimmedRecords = new ArrayList<QuotaBalanceVO>();
|
||||
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<QuotaBalanceVO> 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<List<QuotaBalanceVO>>() {
|
||||
@Override
|
||||
public List<QuotaBalanceVO> doInTransaction(final TransactionStatus status) {
|
||||
List<QuotaBalanceVO> quotaUsageRecords = null;
|
||||
List<QuotaBalanceVO> trimmedRecords = new ArrayList<QuotaBalanceVO>();
|
||||
Filter filter = new Filter(QuotaBalanceVO.class, "updatedOn", false, 0L, 100L);
|
||||
// ASSUMPTION there will be less than 100 continuous credit
|
||||
// transactions
|
||||
SearchCriteria<QuotaBalanceVO> 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
|
||||
|
|
|
|||
|
|
@ -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<QuotaBalanceVO> 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<QuotaBalanceVO> 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<QuotaBalanceVO> 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()) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue