Skip to content

Commit b908f47

Browse files
committed
Add batch deletion support to removeRawUsageRecords
1 parent 7536516 commit b908f47

3 files changed

Lines changed: 18 additions & 18 deletions

File tree

engine/schema/src/main/java/com/cloud/usage/dao/UsageDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public interface UsageDao extends GenericDao<UsageVO, Long> {
6262

6363
void saveUsageRecords(List<UsageVO> usageRecords);
6464

65-
void removeOldUsageRecords(int days);
65+
void expungeAllOlderThan(int days, long limitPerQuery);
6666

6767
UsageVO persistUsage(final UsageVO usage);
6868

engine/schema/src/main/java/com/cloud/usage/dao/UsageDaoImpl.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.cloud.utils.db.Filter;
2727
import com.cloud.utils.db.GenericDaoBase;
2828
import com.cloud.utils.db.QueryBuilder;
29+
import com.cloud.utils.db.SearchBuilder;
2930
import com.cloud.utils.db.SearchCriteria;
3031
import com.cloud.utils.db.Transaction;
3132
import com.cloud.utils.db.TransactionCallback;
@@ -35,6 +36,7 @@
3536
import com.cloud.utils.exception.CloudRuntimeException;
3637

3738
import org.apache.cloudstack.acl.RoleType;
39+
import org.apache.commons.lang3.time.DateUtils;
3840
import org.springframework.stereotype.Component;
3941

4042
import java.sql.PreparedStatement;
@@ -51,7 +53,6 @@
5153
public class UsageDaoImpl extends GenericDaoBase<UsageVO, Long> implements UsageDao {
5254
private static final String DELETE_ALL = "DELETE FROM cloud_usage";
5355
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)";
5556
private static final String INSERT_ACCOUNT = "INSERT INTO cloud_usage.account (id, account_name, uuid, type, role_id, domain_id, removed, cleanup_needed) VALUES (?,?,?,?,?,?,?,?)";
5657
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,"
5758
+ " 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
8889

8990
private static final String UPDATE_BUCKET_STATS = "UPDATE cloud_usage.bucket_statistics SET size=? WHERE id=?";
9091

92+
protected SearchBuilder<UsageVO> endDateLessThanSearch;
9193

9294
public UsageDaoImpl() {
95+
endDateLessThanSearch = createSearchBuilder();
96+
endDateLessThanSearch.and("endDate", endDateLessThanSearch.entity().getEndDate(), SearchCriteria.Op.LT);
97+
endDateLessThanSearch.done();
9398
}
9499

95100
@Override
@@ -539,21 +544,15 @@ public void saveUsageRecords(List<UsageVO> usageRecords) {
539544
}
540545

541546
@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);
557556
}
558557

559558
public UsageVO persistUsage(final UsageVO usage) {

server/src/main/java/com/cloud/usage/UsageServiceImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.inject.Inject;
2727
import javax.naming.ConfigurationException;
2828

29+
import com.cloud.configuration.ConfigurationManagerImpl;
2930
import org.apache.cloudstack.api.command.admin.usage.GenerateUsageRecordsCmd;
3031
import org.apache.cloudstack.api.command.admin.usage.ListUsageRecordsCmd;
3132
import org.apache.cloudstack.api.command.admin.usage.RemoveRawUsageRecordsCmd;
@@ -489,7 +490,7 @@ public boolean removeRawUsageRecords(RemoveRawUsageRecordsCmd cmd) throws Invali
489490
}
490491
}
491492
}
492-
_usageDao.removeOldUsageRecords(interval);
493+
_usageDao.expungeAllOlderThan(interval, ConfigurationManagerImpl.DELETE_QUERY_BATCH_SIZE.value());
493494
} else {
494495
throw new InvalidParameterValueException("Invalid interval value. Interval to remove cloud_usage records should be greater than 0");
495496
}

0 commit comments

Comments
 (0)