diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java index 12567a30d2..9de850cdfd 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java @@ -330,9 +330,17 @@ public class RedisKeys { /** * 获取计算标记的key前缀 + * * @return */ public static String getCustomerStatsCalKeyPrefix() { return rootPrefix.concat("stats:calflag"); } + + /** + * 插入大屏指标数据分布式锁 key + */ + public static String getScreenIndexDataLockKey() { + return rootPrefix.concat("stats:indexcal:lock"); + } } diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcoll/impl/FactIndexCollectServiceImpl.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcoll/impl/FactIndexCollectServiceImpl.java index bec0782a46..640698d058 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcoll/impl/FactIndexCollectServiceImpl.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcoll/impl/FactIndexCollectServiceImpl.java @@ -3,6 +3,7 @@ package com.epmet.service.evaluationindex.indexcoll.impl; import com.epmet.commons.dynamic.datasource.annotation.DataSource; import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.constant.StrConstant; +import com.epmet.commons.tools.redis.RedisKeys; import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.constant.DataSourceConstant; import com.epmet.constant.OrgTypeConstant; @@ -27,6 +28,8 @@ import com.epmet.service.evaluationindex.indexcoll.FactIndexCollectService; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import lombok.extern.slf4j.Slf4j; +import org.redisson.api.RLock; +import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -38,6 +41,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; /** @@ -84,6 +88,8 @@ public class FactIndexCollectServiceImpl implements FactIndexCollectService { private ScreenCustomerDeptDao screenCustomerDeptDao; @Autowired private ScreenCustomerAgencyDao screenCustomerAgencyDao; + @Autowired + private RedissonClient redissonClient; @Override @Transactional(rollbackFor = Exception.class) @@ -199,34 +205,52 @@ public class FactIndexCollectServiceImpl implements FactIndexCollectService { } @Override - @Transactional(rollbackFor = Exception.class) public void insertScreenIndexDataMonthlyAndYearly(String monthId, String customerId) { - if (NumConstant.SIX != monthId.length()){ + if (NumConstant.SIX != monthId.length()) { throw new RuntimeException("入参monthId格式不正确:monthId =" + monthId); } - allParentIds.cleanUp(); - // 根据网格类型,删除 指数-指数数据(每月数值)表中 匹配的数据 - this.batchDelIndexDataMonthly(customerId, monthId, OrgTypeConstant.GRID); - // 开始处理 网格相关分值表 - this.startHandleIndexGridScore(monthId, customerId); - - // 根据组织类型,删除 指数-指数数据(每月数值)表中 匹配的数据 - this.batchDelIndexDataMonthly(customerId, monthId, OrgTypeConstant.AGENCY); - // 开始处理 社区相关分值表 - this.startHandleIndexCommunityScore(monthId, customerId); - - // 根据部门类型,删除 指数-指数数据(每月数值)表中 匹配的数据 - this.batchDelIndexDataMonthly(customerId, monthId, OrgTypeConstant.DEPARTMENT); - // 开始处理 区直部门分值表 - this.startHandleIndexDeptScore(monthId, customerId); - - // 同样是 组织类型,所以不再重复删除了 - // 开始处理 区/街道相关分数表 - this.startHandleIndexAgencyScore(monthId, customerId); - - // 根据年,汇总今年各项得到,计算平均值后 插入年表 screen_index_data_yearly - this.insertIndexDataYear(monthId, customerId); - allParentIds.invalidateAll(); + RLock lock = redissonClient.getLock(RedisKeys.getScreenIndexDataLockKey()); + try { + AtomicInteger tryTimes = new AtomicInteger(0); + boolean lockFlag = false; + do { + lockFlag = lock.tryLock(1, 10, TimeUnit.MINUTES); + } while (!lockFlag && tryTimes.addAndGet(1) < 3); + if (!lockFlag) { + log.error("插入大屏指数方法获取锁失败"); + return; + } + allParentIds.cleanUp(); + // 根据网格类型,删除 指数-指数数据(每月数值)表中 匹配的数据 + this.batchDelIndexDataMonthly(customerId, monthId, OrgTypeConstant.GRID); + // 开始处理 网格相关分值表 + this.startHandleIndexGridScore(monthId, customerId); + + // 根据组织类型,删除 指数-指数数据(每月数值)表中 匹配的数据 + this.batchDelIndexDataMonthly(customerId, monthId, OrgTypeConstant.AGENCY); + // 开始处理 社区相关分值表 + this.startHandleIndexCommunityScore(monthId, customerId); + + // 根据部门类型,删除 指数-指数数据(每月数值)表中 匹配的数据 + this.batchDelIndexDataMonthly(customerId, monthId, OrgTypeConstant.DEPARTMENT); + // 开始处理 区直部门分值表 + this.startHandleIndexDeptScore(monthId, customerId); + + // 同样是 组织类型,所以不再重复删除了 + // 开始处理 区/街道相关分数表 + this.startHandleIndexAgencyScore(monthId, customerId); + + // 根据年,汇总今年各项得到,计算平均值后 插入年表 screen_index_data_yearly + this.insertIndexDataYear(monthId, customerId); + allParentIds.invalidateAll(); + } catch (InterruptedException inter) { + log.error("插入计算指标数据异常", inter); + } finally { + //判断是否是当前线程 持有锁 + if (lock.isHeldByCurrentThread()) { + lock.unlock(); + } + } } /**