Browse Source

插入指标计算结果数据到大屏去掉事务,添加分布式锁

dev_shibei_match
jianjun 5 years ago
parent
commit
95b592c657
  1. 8
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java
  2. 74
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcoll/impl/FactIndexCollectServiceImpl.java

8
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java

@ -330,9 +330,17 @@ public class RedisKeys {
/** /**
* 获取计算标记的key前缀 * 获取计算标记的key前缀
*
* @return * @return
*/ */
public static String getCustomerStatsCalKeyPrefix() { public static String getCustomerStatsCalKeyPrefix() {
return rootPrefix.concat("stats:calflag"); return rootPrefix.concat("stats:calflag");
} }
/**
* 插入大屏指标数据分布式锁 key
*/
public static String getScreenIndexDataLockKey() {
return rootPrefix.concat("stats:indexcal:lock");
}
} }

74
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.dynamic.datasource.annotation.DataSource;
import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.constant.StrConstant; import com.epmet.commons.tools.constant.StrConstant;
import com.epmet.commons.tools.redis.RedisKeys;
import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.commons.tools.utils.ConvertUtils;
import com.epmet.constant.DataSourceConstant; import com.epmet.constant.DataSourceConstant;
import com.epmet.constant.OrgTypeConstant; 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.Cache;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -38,6 +41,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -84,6 +88,8 @@ public class FactIndexCollectServiceImpl implements FactIndexCollectService {
private ScreenCustomerDeptDao screenCustomerDeptDao; private ScreenCustomerDeptDao screenCustomerDeptDao;
@Autowired @Autowired
private ScreenCustomerAgencyDao screenCustomerAgencyDao; private ScreenCustomerAgencyDao screenCustomerAgencyDao;
@Autowired
private RedissonClient redissonClient;
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@ -199,34 +205,52 @@ public class FactIndexCollectServiceImpl implements FactIndexCollectService {
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public void insertScreenIndexDataMonthlyAndYearly(String monthId, String customerId) { public void insertScreenIndexDataMonthlyAndYearly(String monthId, String customerId) {
if (NumConstant.SIX != monthId.length()){ if (NumConstant.SIX != monthId.length()) {
throw new RuntimeException("入参monthId格式不正确:monthId =" + monthId); throw new RuntimeException("入参monthId格式不正确:monthId =" + monthId);
} }
allParentIds.cleanUp(); RLock lock = redissonClient.getLock(RedisKeys.getScreenIndexDataLockKey());
// 根据网格类型,删除 指数-指数数据(每月数值)表中 匹配的数据 try {
this.batchDelIndexDataMonthly(customerId, monthId, OrgTypeConstant.GRID); AtomicInteger tryTimes = new AtomicInteger(0);
// 开始处理 网格相关分值表 boolean lockFlag = false;
this.startHandleIndexGridScore(monthId, customerId); do {
lockFlag = lock.tryLock(1, 10, TimeUnit.MINUTES);
// 根据组织类型,删除 指数-指数数据(每月数值)表中 匹配的数据 } while (!lockFlag && tryTimes.addAndGet(1) < 3);
this.batchDelIndexDataMonthly(customerId, monthId, OrgTypeConstant.AGENCY); if (!lockFlag) {
// 开始处理 社区相关分值表 log.error("插入大屏指数方法获取锁失败");
this.startHandleIndexCommunityScore(monthId, customerId); return;
}
// 根据部门类型,删除 指数-指数数据(每月数值)表中 匹配的数据 allParentIds.cleanUp();
this.batchDelIndexDataMonthly(customerId, monthId, OrgTypeConstant.DEPARTMENT); // 根据网格类型,删除 指数-指数数据(每月数值)表中 匹配的数据
// 开始处理 区直部门分值表 this.batchDelIndexDataMonthly(customerId, monthId, OrgTypeConstant.GRID);
this.startHandleIndexDeptScore(monthId, customerId); // 开始处理 网格相关分值表
this.startHandleIndexGridScore(monthId, customerId);
// 同样是 组织类型,所以不再重复删除了
// 开始处理 区/街道相关分数表 // 根据组织类型,删除 指数-指数数据(每月数值)表中 匹配的数据
this.startHandleIndexAgencyScore(monthId, customerId); this.batchDelIndexDataMonthly(customerId, monthId, OrgTypeConstant.AGENCY);
// 开始处理 社区相关分值表
// 根据年,汇总今年各项得到,计算平均值后 插入年表 screen_index_data_yearly this.startHandleIndexCommunityScore(monthId, customerId);
this.insertIndexDataYear(monthId, customerId);
allParentIds.invalidateAll(); // 根据部门类型,删除 指数-指数数据(每月数值)表中 匹配的数据
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();
}
}
} }
/** /**

Loading…
Cancel
Save