Skip to content

Commit 02d8f18

Browse files
committed
Remove ORDER BY from batch expunge
1 parent c303a18 commit 02d8f18

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,14 @@ public void expungeAllOlderThan(int days, long limitPerQuery) {
549549
Date limit = DateUtils.addDays(new Date(), -days);
550550
sc.setParameters("endDate", limit);
551551

552-
logger.debug("Removing all cloud_usage records older than [{}].", limit);
553-
int totalRemoved = Transaction.execute(TransactionLegacy.USAGE_DB, (TransactionCallback<Integer>) status -> batchExpunge(sc, limitPerQuery));
554-
logger.info("Removed a total of [{}] cloud_usage records older than [{}].", totalRemoved, limit);
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+
}
555560
}
556561

557562
public UsageVO persistUsage(final UsageVO usage) {

framework/db/src/main/java/com/cloud/utils/db/Filter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public Filter(Class<?> clazz, String field, boolean ascending) {
5757
}
5858

5959
public Filter(long limit) {
60-
_orderBy = " ORDER BY RAND() LIMIT " + limit;
60+
_orderBy = " ORDER BY RAND()";
61+
_limit = limit;
6162
}
6263

6364
public Filter(Long offset, Long limit) {

framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,8 @@ protected void addFilter(final StringBuilder sql, final Filter filter) {
11611161
if (filter.getLimit() != null) {
11621162
sql.append(", ").append(filter.getLimit());
11631163
}
1164+
} else if (filter.getLimit() != null) {
1165+
sql.append(" LIMIT ").append(filter.getLimit());
11641166
}
11651167
}
11661168
}
@@ -1322,7 +1324,7 @@ public int batchExpunge(final SearchCriteria<T> sc, final Long batchSize) {
13221324
Filter filter = null;
13231325
final long batchSizeFinal = ObjectUtils.defaultIfNull(batchSize, 0L);
13241326
if (batchSizeFinal > 0) {
1325-
filter = new Filter(batchSizeFinal);
1327+
filter = new Filter(null, batchSizeFinal);
13261328
}
13271329
int expunged = 0;
13281330
int currentExpunged = 0;

0 commit comments

Comments
 (0)