|
@@ -526,8 +526,8 @@ public class FsCourseTrafficLogServiceImpl implements IFsCourseTrafficLogService
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void batchDelTraffic() {
|
|
public void batchDelTraffic() {
|
|
|
- // 设置删除的时间条件:三个月之前
|
|
|
|
|
- LocalDate targetLocalDate = LocalDate.now().minusMonths(3).withDayOfMonth(1);
|
|
|
|
|
|
|
+ // 设置删除的时间条件:两个月之前(当月1日0点)
|
|
|
|
|
+ LocalDate targetLocalDate = LocalDate.now().minusMonths(2).withDayOfMonth(1);
|
|
|
Date targetDate = Date.from(targetLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
|
Date targetDate = Date.from(targetLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
|
|
|
|
|
|
|
int batchSize = 5000; // 每批次处理数量
|
|
int batchSize = 5000; // 每批次处理数量
|
|
@@ -540,7 +540,7 @@ public class FsCourseTrafficLogServiceImpl implements IFsCourseTrafficLogService
|
|
|
* 批量删除过期数据
|
|
* 批量删除过期数据
|
|
|
*/
|
|
*/
|
|
|
private void batchDeleteExpiredData(Date targetDate, int batchSize, int sleepMillis) {
|
|
private void batchDeleteExpiredData(Date targetDate, int batchSize, int sleepMillis) {
|
|
|
- int currentPage = 0;
|
|
|
|
|
|
|
+ int batchNumber = 0;
|
|
|
long totalDeleted = 0;
|
|
long totalDeleted = 0;
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
@@ -556,9 +556,8 @@ public class FsCourseTrafficLogServiceImpl implements IFsCourseTrafficLogService
|
|
|
long startTime = System.currentTimeMillis();
|
|
long startTime = System.currentTimeMillis();
|
|
|
|
|
|
|
|
while (true) {
|
|
while (true) {
|
|
|
- // 分页查询过期记录的log_id
|
|
|
|
|
- int offset = currentPage * batchSize;
|
|
|
|
|
- List<Long> logIds = fsCourseTrafficLogMapper.selectExpireLinkIds(targetDate, offset, batchSize);
|
|
|
|
|
|
|
+ // 始终查询最靠前的一批,删除后下一批自然顶上,避免 OFFSET 翻页漏删
|
|
|
|
|
+ List<Long> logIds = fsCourseTrafficLogMapper.selectExpireLinkIds(targetDate, batchSize);
|
|
|
|
|
|
|
|
if (logIds == null || logIds.isEmpty()) {
|
|
if (logIds == null || logIds.isEmpty()) {
|
|
|
log.info("批量删除完成,共删除 {} 条记录", totalDeleted);
|
|
log.info("批量删除完成,共删除 {} 条记录", totalDeleted);
|
|
@@ -568,20 +567,18 @@ public class FsCourseTrafficLogServiceImpl implements IFsCourseTrafficLogService
|
|
|
// 批量删除当前批次的log_id
|
|
// 批量删除当前批次的log_id
|
|
|
int deletedCount = fsCourseTrafficLogMapper.batchDeleteByIds(logIds);
|
|
int deletedCount = fsCourseTrafficLogMapper.batchDeleteByIds(logIds);
|
|
|
totalDeleted += deletedCount;
|
|
totalDeleted += deletedCount;
|
|
|
|
|
+ batchNumber++;
|
|
|
|
|
|
|
|
// 每5批次或最后一批输出日志
|
|
// 每5批次或最后一批输出日志
|
|
|
- if (currentPage % 5 == 0 || logIds.size() < batchSize) {
|
|
|
|
|
|
|
+ if (batchNumber % 5 == 0 || logIds.size() < batchSize) {
|
|
|
double progress = (double) totalDeleted / totalCount * 100;
|
|
double progress = (double) totalDeleted / totalCount * 100;
|
|
|
- long currentTime = System.currentTimeMillis();
|
|
|
|
|
- long elapsedSeconds = (currentTime - startTime) / 1000;
|
|
|
|
|
|
|
+ long elapsedSeconds = (System.currentTimeMillis() - startTime) / 1000;
|
|
|
|
|
|
|
|
- log.info("批次 {}: 删除 {} 条,进度: {}/{} ({:.2f}%),耗时: {}秒",
|
|
|
|
|
- currentPage + 1, deletedCount, totalDeleted, totalCount,
|
|
|
|
|
- progress, elapsedSeconds);
|
|
|
|
|
|
|
+ log.info("批次 {}: 删除 {} 条,进度: {}/{} ({}%),耗时: {}秒",
|
|
|
|
|
+ batchNumber, deletedCount, totalDeleted, totalCount,
|
|
|
|
|
+ String.format("%.2f", progress), elapsedSeconds);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- currentPage++;
|
|
|
|
|
-
|
|
|
|
|
// 批次间短暂休眠,避免数据库压力过大
|
|
// 批次间短暂休眠,避免数据库压力过大
|
|
|
if (sleepMillis > 0 && logIds.size() == batchSize) {
|
|
if (sleepMillis > 0 && logIds.size() == batchSize) {
|
|
|
try {
|
|
try {
|
|
@@ -594,8 +591,7 @@ public class FsCourseTrafficLogServiceImpl implements IFsCourseTrafficLogService
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- long endTime = System.currentTimeMillis();
|
|
|
|
|
- long totalSeconds = (endTime - startTime) / 1000;
|
|
|
|
|
|
|
+ long totalSeconds = (System.currentTimeMillis() - startTime) / 1000;
|
|
|
log.info("批量删除任务完成,总计删除: {} 条记录,总耗时: {} 秒", totalDeleted, totalSeconds);
|
|
log.info("批量删除任务完成,总计删除: {} 条记录,总耗时: {} 秒", totalDeleted, totalSeconds);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|