1. public void start(final List<WholeRerunTaskSlopsPo> slopList, final WholeRerunTaskUpdate cancel, final WholeRerunTaskPo insert) {
    2. log.info("whole return task begin create");
    3. wholeRerunTaskDao.cancel(cancel);//一张任务表 每天insert一个。现在是每天晚上9点全亮跑数据。如果有执行中设置成取消状态。 不是重点
    4. deleteWholeRerunTaskSlops(); //删除全亮的字任务。按分钟存储的
    5. wholeRerunTaskDao.insert(insert);//insert一条任务
    6. Collections.shuffle(slopList);//洗牌,因为这个list有audio 和video
    7. wholeRerunTaskDao.lockTable();//锁住表
    8. for (final List<WholeRerunTaskSlopsPo> wholeRerunTaskSlopsPos : Lists.partition(slopList, Constants.INSERT_BATCH_SIZE)) {
    9. wholeRerunTaskSlopsPos.forEach(n -> n.setTaskId(insert.getId()));
    10. wholeRerunTaskSlopsDao.batchInsert(wholeRerunTaskSlopsPos);//插入子任务,有定时扫描字任务表跑数据
    11. }
    12. WholeRerunTaskPo update=new WholeRerunTaskPo();
    13. update.setId(insert.getId());
    14. update.setPlanExecutionTime(new Date(System.currentTimeMillis()+Constants.THREE_MINUTES));//执行完子任务,设置一个执行时间,作为后来等待创建索引的依据
    15. wholeRerunTaskDao.update(update);
    16. patternRerunTaskManager.updateToWholeProcessing(insert);
    17. }

    定时5秒扫一次跑数据代码

    1. private List<WholeRerunTaskSlopsPo> getSlopsFromDb(final PatternScopeEnum patternScope, final int getNum) {
    2. final Transaction transaction = Cat.newTransaction(CatType.TIMER, patternScope.name() + ".getSlopsFromDb");
    3. try {
    4. final WholeRerunTaskPo current = wholeRerunTaskManager.current();
    5. if (current == null || current.getPlanExecutionTime() == null) {
    6. log.info("there is no whole rerun task doing or not plan execution time,task {} ", current);
    7. return null;
    8. }
    9. if (System.currentTimeMillis() < current.getPlanExecutionTime().getTime()) {
    10. log.info("current time {} is less than plan execution time {} ", System.currentTimeMillis(), current.getPlanExecutionTime().getTime());
    11. return null;
    12. }
    13. WholeRerunTaskSlopsQuery query = new WholeRerunTaskSlopsQuery();
    14. query.setLimit(getNum);
    15. query.setTimeout(new Date(System.currentTimeMillis() + getTime()));
    16. query.setPatternScope(patternScope.getCode());
    17. query.setLockTag(UUID.randomUUID().toString().replace("-", ""));
    18. return wholeRerunTaskSlopsManager.lockSlops(query); //之前是这里会爆慢sql,上边有current.getPlanExecutionTime() 校验了一下。
    19. } catch (Exception e) {
    20. transaction.setStatus(e);
    21. throw e;
    22. } finally {
    23. transaction.complete();
    24. }
    25. }

    总结:
    原始逻辑:因为数据插入子任务表 定时就扫。然后子任务一边创建 定时一边查询,这个时候索引还没有创建好。所以查询爆慢sql

    现在逻辑:加入一个时间字段,等待子任务全部创建好,然后索引创建好, 定时才可以通过if判断。