From 95b592c6570affe2aa3c65945d02d93be0de7381 Mon Sep 17 00:00:00 2001 From: jianjun Date: Tue, 20 Oct 2020 13:45:03 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=8F=92=E5=85=A5=E6=8C=87=E6=A0=87?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E7=BB=93=E6=9E=9C=E6=95=B0=E6=8D=AE=E5=88=B0?= =?UTF-8?q?=E5=A4=A7=E5=B1=8F=E5=8E=BB=E6=8E=89=E4=BA=8B=E5=8A=A1=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=86=E5=B8=83=E5=BC=8F=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../epmet/commons/tools/redis/RedisKeys.java | 8 ++ .../impl/FactIndexCollectServiceImpl.java | 74 ++++++++++++------- 2 files changed, 57 insertions(+), 25 deletions(-) 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(); + } + } } /** From 9639e7f6ec20a89d36e50506e5d0df23490c73ae Mon Sep 17 00:00:00 2001 From: wxz Date: Tue, 20 Oct 2020 14:24:04 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9Dockerfile=20=20ENTRYPOIN?= =?UTF-8?q?T=EF=BC=8C=E5=A2=9E=E5=8A=A0exec?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data-statistical/data-statistical-server/Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/epmet-module/data-statistical/data-statistical-server/Dockerfile b/epmet-module/data-statistical/data-statistical-server/Dockerfile index 074bc1d2de..55d058c4b8 100644 --- a/epmet-module/data-statistical/data-statistical-server/Dockerfile +++ b/epmet-module/data-statistical/data-statistical-server/Dockerfile @@ -8,4 +8,5 @@ COPY ./target/*.jar ./data-stats.jar EXPOSE 8108 -ENTRYPOINT ["sh", "-c", "$RUN_INSTRUCT"] \ No newline at end of file +#ENTRYPOINT ["sh", "-c", "$RUN_INSTRUCT"] +ENTRYPOINT ["sh","-c","exec $RUN_INSTRUCT"] \ No newline at end of file From c6bc8e288551947ca1fc91a521de7db4d756fd2a Mon Sep 17 00:00:00 2001 From: wxz Date: Tue, 20 Oct 2020 14:27:03 +0800 Subject: [PATCH 3/3] =?UTF-8?q?preDestory=E5=A2=9E=E5=8A=A0=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/epmet/controller/IndexCalculateController.java | 1 + 1 file changed, 1 insertion(+) diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/IndexCalculateController.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/IndexCalculateController.java index 420838dec5..11e082057a 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/IndexCalculateController.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/IndexCalculateController.java @@ -60,6 +60,7 @@ public class IndexCalculateController { @PreDestroy public void saveCalStatus() { + log.info("data-statical-server服务被关闭,执行保存计算状态的动作"); // 实例销毁之前,将正在本实例中执行计算的客户列表的计算状态修改为pendding,等待其他实例重新计算 futureMap.forEach((customerId, future) -> { CalculateFlagModel flag = (CalculateFlagModel) redisUtils.get(RedisKeys.getCustomerStatsCalFlag(customerId));