public void start(final List<WholeRerunTaskSlopsPo> slopList, final WholeRerunTaskUpdate cancel, final WholeRerunTaskPo insert) {log.info("whole return task begin create");wholeRerunTaskDao.cancel(cancel);//一张任务表 每天insert一个。现在是每天晚上9点全亮跑数据。如果有执行中设置成取消状态。 不是重点deleteWholeRerunTaskSlops(); //删除全亮的字任务。按分钟存储的wholeRerunTaskDao.insert(insert);//insert一条任务Collections.shuffle(slopList);//洗牌,因为这个list有audio 和videowholeRerunTaskDao.lockTable();//锁住表for (final List<WholeRerunTaskSlopsPo> wholeRerunTaskSlopsPos : Lists.partition(slopList, Constants.INSERT_BATCH_SIZE)) {wholeRerunTaskSlopsPos.forEach(n -> n.setTaskId(insert.getId()));wholeRerunTaskSlopsDao.batchInsert(wholeRerunTaskSlopsPos);//插入子任务,有定时扫描字任务表跑数据}WholeRerunTaskPo update=new WholeRerunTaskPo();update.setId(insert.getId());update.setPlanExecutionTime(new Date(System.currentTimeMillis()+Constants.THREE_MINUTES));//执行完子任务,设置一个执行时间,作为后来等待创建索引的依据wholeRerunTaskDao.update(update);patternRerunTaskManager.updateToWholeProcessing(insert);}
定时5秒扫一次跑数据代码
private List<WholeRerunTaskSlopsPo> getSlopsFromDb(final PatternScopeEnum patternScope, final int getNum) {final Transaction transaction = Cat.newTransaction(CatType.TIMER, patternScope.name() + ".getSlopsFromDb");try {final WholeRerunTaskPo current = wholeRerunTaskManager.current();if (current == null || current.getPlanExecutionTime() == null) {log.info("there is no whole rerun task doing or not plan execution time,task {} ", current);return null;}if (System.currentTimeMillis() < current.getPlanExecutionTime().getTime()) {log.info("current time {} is less than plan execution time {} ", System.currentTimeMillis(), current.getPlanExecutionTime().getTime());return null;}WholeRerunTaskSlopsQuery query = new WholeRerunTaskSlopsQuery();query.setLimit(getNum);query.setTimeout(new Date(System.currentTimeMillis() + getTime()));query.setPatternScope(patternScope.getCode());query.setLockTag(UUID.randomUUID().toString().replace("-", ""));return wholeRerunTaskSlopsManager.lockSlops(query); //之前是这里会爆慢sql,上边有current.getPlanExecutionTime() 校验了一下。} catch (Exception e) {transaction.setStatus(e);throw e;} finally {transaction.complete();}}
总结:
原始逻辑:因为数据插入子任务表 定时就扫。然后子任务一边创建 定时一边查询,这个时候索引还没有创建好。所以查询爆慢sql
现在逻辑:加入一个时间字段,等待子任务全部创建好,然后索引创建好, 定时才可以通过if判断。
