|
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; |
32 | | -import com.cloud.utils.db.TransactionCallbackNoReturn; |
33 | 33 | import com.cloud.utils.db.TransactionLegacy; |
34 | 34 | import com.cloud.utils.db.TransactionStatus; |
35 | 35 | import com.cloud.utils.exception.CloudRuntimeException; |
36 | 36 |
|
37 | 37 | import org.apache.cloudstack.acl.RoleType; |
| 38 | +import org.apache.commons.lang3.time.DateUtils; |
38 | 39 | import org.springframework.stereotype.Component; |
39 | 40 |
|
40 | 41 | import java.sql.PreparedStatement; |
|
51 | 52 | public class UsageDaoImpl extends GenericDaoBase<UsageVO, Long> implements UsageDao { |
52 | 53 | private static final String DELETE_ALL = "DELETE FROM cloud_usage"; |
53 | 54 | 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 | 55 | 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 | 56 | 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 | 57 | + " net_bytes_sent, current_bytes_received, current_bytes_sent, agg_bytes_received, agg_bytes_sent) VALUES (?,?,?,?,?,?,?,?,?,?, ?, ?, ?)"; |
@@ -88,8 +88,12 @@ public class UsageDaoImpl extends GenericDaoBase<UsageVO, Long> implements Usage |
88 | 88 |
|
89 | 89 | private static final String UPDATE_BUCKET_STATS = "UPDATE cloud_usage.bucket_statistics SET size=? WHERE id=?"; |
90 | 90 |
|
| 91 | + protected SearchBuilder<UsageVO> endDateLessThanSearch; |
91 | 92 |
|
92 | 93 | public UsageDaoImpl() { |
| 94 | + endDateLessThanSearch = createSearchBuilder(); |
| 95 | + endDateLessThanSearch.and("endDate", endDateLessThanSearch.entity().getEndDate(), SearchCriteria.Op.LT); |
| 96 | + endDateLessThanSearch.done(); |
93 | 97 | } |
94 | 98 |
|
95 | 99 | @Override |
@@ -539,21 +543,20 @@ public void saveUsageRecords(List<UsageVO> usageRecords) { |
539 | 543 | } |
540 | 544 |
|
541 | 545 | @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 | | - }); |
| 546 | + public void expungeAllOlderThan(int days, long limitPerQuery) { |
| 547 | + SearchCriteria<UsageVO> sc = endDateLessThanSearch.create(); |
| 548 | + |
| 549 | + Date limit = DateUtils.addDays(new Date(), -days); |
| 550 | + sc.setParameters("endDate", limit); |
| 551 | + |
| 552 | + TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); |
| 553 | + try { |
| 554 | + logger.debug("Removing all cloud_usage records older than [{}].", limit); |
| 555 | + int totalExpunged = batchExpunge(sc, limitPerQuery); |
| 556 | + logger.info("Removed a total of [{}] cloud_usage records older than [{}].", totalExpunged, limit); |
| 557 | + } finally { |
| 558 | + txn.close(); |
| 559 | + } |
557 | 560 | } |
558 | 561 |
|
559 | 562 | public UsageVO persistUsage(final UsageVO usage) { |
|
0 commit comments