|
26 | 26 | import com.cloud.utils.db.Filter; |
27 | 27 | import com.cloud.utils.db.GenericDaoBase; |
28 | 28 | import com.cloud.utils.db.QueryBuilder; |
| 29 | +import com.cloud.utils.db.SearchBuilder; |
29 | 30 | import com.cloud.utils.db.SearchCriteria; |
30 | 31 | import com.cloud.utils.db.Transaction; |
31 | 32 | import com.cloud.utils.db.TransactionCallback; |
|
35 | 36 | import com.cloud.utils.exception.CloudRuntimeException; |
36 | 37 |
|
37 | 38 | import org.apache.cloudstack.acl.RoleType; |
| 39 | +import org.apache.commons.lang3.time.DateUtils; |
38 | 40 | import org.springframework.stereotype.Component; |
39 | 41 |
|
40 | 42 | import java.sql.PreparedStatement; |
|
51 | 53 | public class UsageDaoImpl extends GenericDaoBase<UsageVO, Long> implements UsageDao { |
52 | 54 | private static final String DELETE_ALL = "DELETE FROM cloud_usage"; |
53 | 55 | private static final String DELETE_ALL_BY_ACCOUNTID = "DELETE FROM cloud_usage WHERE account_id = ?"; |
54 | | - private static final String DELETE_ALL_BY_INTERVAL = "DELETE FROM cloud_usage WHERE end_date < DATE_SUB(CURRENT_DATE(), INTERVAL ? DAY)"; |
55 | 56 | private static final String INSERT_ACCOUNT = "INSERT INTO cloud_usage.account (id, account_name, uuid, type, role_id, domain_id, removed, cleanup_needed) VALUES (?,?,?,?,?,?,?,?)"; |
56 | 57 | private static final String INSERT_USER_STATS = "INSERT INTO cloud_usage.user_statistics (id, data_center_id, account_id, public_ip_address, device_id, device_type, network_id, net_bytes_received," |
57 | 58 | + " net_bytes_sent, current_bytes_received, current_bytes_sent, agg_bytes_received, agg_bytes_sent) VALUES (?,?,?,?,?,?,?,?,?,?, ?, ?, ?)"; |
@@ -88,8 +89,12 @@ public class UsageDaoImpl extends GenericDaoBase<UsageVO, Long> implements Usage |
88 | 89 |
|
89 | 90 | private static final String UPDATE_BUCKET_STATS = "UPDATE cloud_usage.bucket_statistics SET size=? WHERE id=?"; |
90 | 91 |
|
| 92 | + protected SearchBuilder<UsageVO> endDateLessThanSearch; |
91 | 93 |
|
92 | 94 | public UsageDaoImpl() { |
| 95 | + endDateLessThanSearch = createSearchBuilder(); |
| 96 | + endDateLessThanSearch.and("endDate", endDateLessThanSearch.entity().getEndDate(), SearchCriteria.Op.LT); |
| 97 | + endDateLessThanSearch.done(); |
93 | 98 | } |
94 | 99 |
|
95 | 100 | @Override |
@@ -539,21 +544,15 @@ public void saveUsageRecords(List<UsageVO> usageRecords) { |
539 | 544 | } |
540 | 545 |
|
541 | 546 | @Override |
542 | | - public void removeOldUsageRecords(int days) { |
543 | | - Transaction.execute(TransactionLegacy.USAGE_DB, new TransactionCallbackNoReturn() { |
544 | | - @Override |
545 | | - public void doInTransactionWithoutResult(TransactionStatus status) { |
546 | | - TransactionLegacy txn = TransactionLegacy.currentTxn(); |
547 | | - PreparedStatement pstmt = null; |
548 | | - try { |
549 | | - pstmt = txn.prepareAutoCloseStatement(DELETE_ALL_BY_INTERVAL); |
550 | | - pstmt.setLong(1, days); |
551 | | - pstmt.executeUpdate(); |
552 | | - } catch (Exception ex) { |
553 | | - logger.error("error removing old cloud_usage records for interval: " + days); |
554 | | - } |
555 | | - } |
556 | | - }); |
| 547 | + public void expungeAllOlderThan(int days, long limitPerQuery) { |
| 548 | + SearchCriteria<UsageVO> sc = endDateLessThanSearch.create(); |
| 549 | + |
| 550 | + Date limit = DateUtils.addDays(new Date(), -days); |
| 551 | + sc.setParameters("endDate", limit); |
| 552 | + |
| 553 | + logger.debug("Removing all cloud_usage records older than [{}].", limit); |
| 554 | + int totalRemoved = Transaction.execute(TransactionLegacy.USAGE_DB, (TransactionCallback<Integer>) status -> batchExpunge(sc, limitPerQuery)); |
| 555 | + logger.info("Removed a total of [{}] cloud_usage records older than [{}].", totalRemoved, limit); |
557 | 556 | } |
558 | 557 |
|
559 | 558 | public UsageVO persistUsage(final UsageVO usage) { |
|
0 commit comments