Skip to content

Commit

Permalink
perf: Remove constant Constants.ID/ALERT_GROUP_ID/ALERT_QUOTA_NAME/AL…
Browse files Browse the repository at this point in the history
…ERT_GROUP_NAME/ALERT_LEVEL, and use SFunction instead. (#200)
  • Loading branch information
WujieRen authored Mar 6, 2023
1 parent 4a48799 commit dabb84b
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.datasophon.api.enums.Status;
import com.datasophon.api.service.AlertGroupService;
import com.datasophon.api.service.ClusterAlertQuotaService;
import com.datasophon.common.Constants;
import com.datasophon.dao.entity.ClusterAlertQuota;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -96,7 +94,7 @@ public Result update(@RequestBody AlertGroupEntity alertGroup) {
public Result delete(@RequestBody Integer[] ids) {

//校验是否绑定告警指标
List<ClusterAlertQuota> list = alertQuotaService.list(new QueryWrapper<ClusterAlertQuota>().in(Constants.ALERT_GROUP_ID, ids));
List<ClusterAlertQuota> list = alertQuotaService.lambdaQuery().in(ClusterAlertQuota::getAlertGroupId, ids).list();
if(list.size() > 0){
return Result.error(Status.ALERT_GROUP_TIPS_ONE.getMsg());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Result list(String username,Integer page,Integer pageSize) {
*/
@RequestMapping("/all")
public Result all() {
List<UserInfoEntity> list = userInfoService.list(new QueryWrapper<UserInfoEntity>().ne(Constants.ID,1));
List<UserInfoEntity> list = userInfoService.lambdaQuery().eq(UserInfoEntity::getId, 1).list();
return Result.success(list);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

package com.datasophon.api.service.impl;

import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.datasophon.api.service.ClusterAlertGroupMapService;
import com.datasophon.api.service.ClusterAlertQuotaService;
import com.datasophon.common.Constants;
import com.datasophon.common.utils.CollectionUtils;
import com.datasophon.common.utils.Result;
import com.datasophon.dao.entity.ClusterAlertGroupMap;
import com.datasophon.dao.entity.ClusterAlertQuota;
Expand All @@ -28,6 +30,8 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
Expand All @@ -51,21 +55,36 @@ public class AlertGroupServiceImpl extends ServiceImpl<AlertGroupMapper, AlertGr
@Override
public Result getAlertGroupList(Integer clusterId, String alertGroupName, Integer page, Integer pageSize) {
Integer offset = (page - 1) * pageSize;
List<ClusterAlertGroupMap> list = alertGroupMapService.list(new QueryWrapper<ClusterAlertGroupMap>().eq(Constants.CLUSTER_ID, clusterId));
List<Integer> groupIds = list.stream().map(e -> e.getAlertGroupId()).collect(Collectors.toList());
List<AlertGroupEntity> alertGroupList = this.list(new QueryWrapper<AlertGroupEntity>()
.in(Constants.ID, groupIds)
.like(StringUtils.isNotBlank(alertGroupName),Constants.ALERT_GROUP_NAME,alertGroupName)
.last("limit " + offset + "," + pageSize));
int count = this.count(new QueryWrapper<AlertGroupEntity>()
.in(Constants.ID, groupIds)
.like(StringUtils.isNotBlank(alertGroupName),Constants.ALERT_GROUP_NAME,alertGroupName));

List<ClusterAlertGroupMap> alertGroupMapList = alertGroupMapService.list(new QueryWrapper<ClusterAlertGroupMap>().eq(Constants.CLUSTER_ID, clusterId));
if(CollectionUtils.isEmpty(alertGroupMapList)) {
return Result.successEmptyCount();
}

List<Integer> groupIds = alertGroupMapList.stream().map(ClusterAlertGroupMap::getAlertGroupId).collect(Collectors.toList());
LambdaQueryChainWrapper<AlertGroupEntity> wrapper = this.lambdaQuery()
.in(AlertGroupEntity::getId, groupIds)
.like(StringUtils.isNotBlank(alertGroupName), AlertGroupEntity::getAlertGroupName, alertGroupName);

List<AlertGroupEntity> alertGroupList = wrapper.last("limit " + offset + "," + pageSize).list();
if(CollectionUtils.isEmpty(alertGroupList)) {
return Result.successEmptyCount();
}

Set<Integer> alertGroupIdList = alertGroupList.stream().map(AlertGroupEntity::getId).collect(Collectors.toSet());
//查询告警组下告警指标个数
for (AlertGroupEntity alertGroupEntity : alertGroupList) {
List<ClusterAlertQuota> quotaList = quotaService.list(new QueryWrapper<ClusterAlertQuota>().eq(Constants.ALERT_GROUP_ID, alertGroupEntity.getId()));
alertGroupEntity.setAlertQuotaNum(quotaList.size());
List<ClusterAlertQuota> clusQuotaList = quotaService.lambdaQuery().in(ClusterAlertQuota::getAlertGroupId, alertGroupIdList).list();
if(CollectionUtils.isNotEmpty(clusQuotaList)) {
Map<Integer, List<ClusterAlertQuota>> alertGroupByGroupId = clusQuotaList.stream().collect(Collectors.groupingBy(ClusterAlertQuota::getAlertGroupId));
alertGroupList.forEach(a -> {
List<ClusterAlertQuota> tmpQuotaList = alertGroupByGroupId.get(a.getId());
int quotaCnt = CollectionUtils.isEmpty(tmpQuotaList) ? 0 : tmpQuotaList.size();
a.setAlertQuotaNum(quotaCnt);
});
}
return Result.success(alertGroupList).put(Constants.TOTAL,count);

int count = wrapper.count() == null ? 0 : wrapper.count();
return Result.success(alertGroupList).put(Constants.TOTAL, count);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,13 @@ public void saveAlertHistory(String alertMessage) {
if ("resolved".equals(status)) {
if (Objects.nonNull(clusterAlertHistory)) {
clusterAlertHistory.setIsEnabled(2);
List<ClusterAlertHistory> warnAlertList = this.list(new QueryWrapper<ClusterAlertHistory>()
.eq(Constants.HOSTNAME, hostname)
.eq(Constants.ALERT_GROUP_NAME, serviceRoleName.toLowerCase())
.eq(Constants.IS_ENABLED, 1)
.eq(Constants.ALERT_LEVEL, AlertLevel.WARN)
.ne(Constants.ID,clusterAlertHistory.getId()));
List<ClusterAlertHistory> warnAlertList = this.lambdaQuery()
.eq(ClusterAlertHistory::getHostname, hostname)
.eq(ClusterAlertHistory::getAlertGroupName, serviceRoleName.toLowerCase())
.eq(ClusterAlertHistory::getIsEnabled, 1)
.eq(ClusterAlertHistory::getAlertLevel, AlertLevel.WARN)
.ne(ClusterAlertHistory::getId,clusterAlertHistory.getId())
.list();
if("exception".equals(labels.getSeverity())){//异常告警处理
if("node".equals(serviceRoleName)){
//置为正常
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
package com.datasophon.api.service.impl;

import akka.actor.ActorRef;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.datasophon.api.master.ActorUtils;
import com.datasophon.api.master.PrometheusActor;
import com.datasophon.api.service.AlertGroupService;
import com.datasophon.common.Constants;
import com.datasophon.common.cache.CacheUtils;
import com.datasophon.common.command.GenerateAlertConfigCommand;
import com.datasophon.common.model.AlertItem;
import com.datasophon.common.model.Generators;
import com.datasophon.common.utils.CollectionUtils;
import com.datasophon.common.utils.Result;
import com.datasophon.dao.entity.AlertGroupEntity;
import com.datasophon.dao.enums.QuotaState;
Expand All @@ -36,6 +37,7 @@
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
Expand All @@ -56,67 +58,77 @@ public class ClusterAlertQuotaServiceImpl extends ServiceImpl<ClusterAlertQuotaM
@Override
public Result getAlertQuotaList(Integer clusterId, Integer alertGroupId, String quotaName, Integer page, Integer pageSize) {
Integer offset = (page - 1) * pageSize;
List<ClusterAlertQuota> list = this.list(new QueryWrapper<ClusterAlertQuota>()
.eq(alertGroupId != null, Constants.ALERT_GROUP_ID, alertGroupId)
.like(StringUtils.isNotBlank(quotaName), Constants.ALERT_QUOTA_NAME, quotaName)
.last("limit " + offset + "," + pageSize));
int count = this.count(new QueryWrapper<ClusterAlertQuota>()
.eq(alertGroupId != null, Constants.ALERT_GROUP_ID, alertGroupId)
.like(StringUtils.isNotBlank(quotaName), Constants.ALERT_QUOTA_NAME, quotaName));

LambdaQueryChainWrapper<ClusterAlertQuota> wrapper = this.lambdaQuery()
.eq(alertGroupId != null, ClusterAlertQuota::getAlertGroupId, alertGroupId)
.like(StringUtils.isNotBlank(quotaName), ClusterAlertQuota::getAlertQuotaName, quotaName);

List<ClusterAlertQuota> alertQuotaList = wrapper.last("limit " + offset + "," + pageSize).list();
if(CollectionUtils.isEmpty(alertQuotaList)) {
return Result.successEmptyCount();
}
//查询通知组
for (ClusterAlertQuota clusterAlertQuota : list) {
AlertGroupEntity alertGroupEntity = alertGroupService.getById(clusterAlertQuota.getAlertGroupId());
clusterAlertQuota.setAlertGroupName(alertGroupEntity.getAlertGroupName());
clusterAlertQuota.setQuotaStateCode(clusterAlertQuota.getQuotaState().getValue());
Set<Integer> alertQuotaIdList = alertQuotaList.stream().map(ClusterAlertQuota::getAlertGroupId).collect(Collectors.toSet());
Collection<AlertGroupEntity> alertGroupEntityList = alertGroupService.listByIds(alertQuotaIdList);
if(CollectionUtils.isNotEmpty(alertGroupEntityList)) {
Map<Integer, AlertGroupEntity> idMap = alertGroupEntityList.stream().collect(Collectors.toMap(AlertGroupEntity::getId, a -> a, (a1, a2) -> a1));
alertQuotaList.forEach(a -> {
AlertGroupEntity alertGroupEntity = idMap.get(a.getAlertGroupId());
if(Objects.nonNull(alertGroupEntity)) {
a.setAlertGroupName(alertGroupEntity.getAlertGroupName());
}
a.setQuotaStateCode(a.getQuotaState().getValue());
});
}
return Result.success(list).put(Constants.TOTAL,count);

int count = wrapper.count() == null ? 0 : wrapper.count();
return Result.success(alertQuotaList).put(Constants.TOTAL, count);
}

@Override
public Result start(Integer clusterId, String alertQuotaIds) {
HashMap<String, List<ClusterAlertQuota>> map = new HashMap<>();
List<String> ids = Arrays.asList(alertQuotaIds.split(","));
List<ClusterAlertQuota> alertQuotaList = this.list(new QueryWrapper<ClusterAlertQuota>()
.in(Constants.ID, ids));
List<ClusterAlertQuota> alertQuotaList = this.lambdaQuery().in(ClusterAlertQuota::getId, ids).list();
for (ClusterAlertQuota alertQuota : alertQuotaList) {
if(!map.containsKey(alertQuota.getServiceCategory())){
if (!map.containsKey(alertQuota.getServiceCategory())) {
ArrayList<ClusterAlertQuota> quotaList = new ArrayList<>();
quotaList.add(alertQuota);
map.put(alertQuota.getServiceCategory(),quotaList);
}else{
map.put(alertQuota.getServiceCategory(), quotaList);
} else {
List<ClusterAlertQuota> quotaList = map.get(alertQuota.getServiceCategory());

quotaList.add(alertQuota);
}
alertQuota.setQuotaState(QuotaState.RUNNING);
}
if(alertQuotaList.size() > 0){
logger.info("start alert size is {}",alertQuotaList.size());
if (alertQuotaList.size() > 0) {
logger.info("start alert size is {}", alertQuotaList.size());
this.updateBatchById(alertQuotaList);
}
HashMap<Generators, List<AlertItem>> configFileMap = new HashMap<>();
for (Map.Entry<String, List<ClusterAlertQuota>> entry : map.entrySet()) {
String category = entry.getKey();
List<ClusterAlertQuota> alerts = entry.getValue();
Generators generators = new Generators();
generators.setFilename(category.toLowerCase()+".yml");
generators.setFilename(category.toLowerCase() + ".yml");
generators.setConfigFormat("prometheus");
generators.setOutputDirectory("alert_rules");
ArrayList<AlertItem> alertItems = new ArrayList<>();
for (ClusterAlertQuota clusterAlertQuota : alerts) {
AlertItem alertItem = new AlertItem();
alertItem.setAlertName(clusterAlertQuota.getAlertQuotaName());
alertItem.setAlertExpr(clusterAlertQuota.getAlertExpr()+" "+ clusterAlertQuota.getCompareMethod()+" "+clusterAlertQuota.getAlertThreshold());
alertItem.setAlertExpr(clusterAlertQuota.getAlertExpr() + " " + clusterAlertQuota.getCompareMethod() + " " + clusterAlertQuota.getAlertThreshold());
alertItem.setClusterId(clusterId);
alertItem.setServiceRoleName(clusterAlertQuota.getServiceRoleName());
alertItem.setAlertLevel(clusterAlertQuota.getAlertLevel().getDesc());
alertItem.setAlertAdvice(clusterAlertQuota.getAlertAdvice());
alertItem.setTriggerDuration(clusterAlertQuota.getTriggerDuration());
alertItems.add(alertItem);
}
configFileMap.put(generators,alertItems);
configFileMap.put(generators, alertItems);
}
ActorRef prometheusActor = ActorUtils.getLocalActor(PrometheusActor.class,ActorUtils.getActorRefName(PrometheusActor.class));
ActorRef prometheusActor = ActorUtils.getLocalActor(PrometheusActor.class, ActorUtils.getActorRefName(PrometheusActor.class));
GenerateAlertConfigCommand alertConfigCommand = new GenerateAlertConfigCommand();
alertConfigCommand.setClusterId(clusterId);
alertConfigCommand.setConfigFileMap(configFileMap);
Expand All @@ -140,6 +152,6 @@ public void saveAlertQuota(ClusterAlertQuota clusterAlertQuota) {

@Override
public List<ClusterAlertQuota> listAlertQuotaByServiceName(String serviceName) {
return this.list(new QueryWrapper<ClusterAlertQuota>().eq(Constants.SERVICE_CATEGORY,serviceName));
return this.list(new QueryWrapper<ClusterAlertQuota>().eq(Constants.SERVICE_CATEGORY, serviceName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void deleteHostByClusterId(Integer clusterId) {

@Override
public void updateBatchNodeLabel(List<String> hostIds, String nodeLabel) {
List<ClusterHostEntity> list = this.list(new QueryWrapper<ClusterHostEntity>().in(Constants.ID, hostIds));
List<ClusterHostEntity> list = this.lambdaQuery().in(ClusterHostEntity::getId, hostIds).list();
for (ClusterHostEntity clusterHostEntity : list) {
clusterHostEntity.setNodeLabel(nodeLabel);
}
Expand All @@ -157,13 +157,13 @@ public void updateBatchNodeLabel(List<String> hostIds, String nodeLabel) {

@Override
public List<ClusterHostEntity> getHostListByIds(List<String> ids) {
return this.list(new QueryWrapper<ClusterHostEntity>().in(Constants.ID, ids));
return this.lambdaQuery().in(ClusterHostEntity::getId, ids).list();
}

@Override
public Result assignRack(Integer clusterId, String rack, String hostIds) {
List<String> ids = Arrays.asList(hostIds.split(","));
List<ClusterHostEntity> list = this.list(new QueryWrapper<ClusterHostEntity>().in(Constants.ID, ids));
List<ClusterHostEntity> list = this.lambdaQuery().in(ClusterHostEntity::getId, ids).list();
for (ClusterHostEntity clusterHostEntity : list) {
clusterHostEntity.setRack(rack);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,17 @@ public Result decommissionNode(String serviceRoleInstanceIds, String serviceName
}
}
//查询已退役节点
List<ClusterServiceRoleInstanceEntity> list = this.list(new QueryWrapper<ClusterServiceRoleInstanceEntity>()
.eq(Constants.SERVICE_ROLE_STATE, ServiceRoleState.DECOMMISSIONING)
.in(Constants.ID, serviceRoleInstanceIds));
List<ClusterServiceRoleInstanceEntity> list = this.lambdaQuery()
.eq(ClusterServiceRoleInstanceEntity::getServiceRoleState, ServiceRoleState.DECOMMISSIONING)
.in(ClusterServiceRoleInstanceEntity::getId, serviceRoleInstanceIds)
.list();
//添加已退役节点到黑名单
for (ClusterServiceRoleInstanceEntity roleInstanceEntity : list) {
hosts.add(roleInstanceEntity.getHostname());
}
String type = "blacklist";
String roleName = "NameNode";
if ("nodemanager".equals(serviceRoleName.toLowerCase())) {
if ("nodemanager".equalsIgnoreCase(serviceRoleName)) {
type = "nmexclude";
roleName = "ResourceManager";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public List<FrameServiceEntity> getAllFrameServiceByFrameCode(String clusterFram
@Override
public List<FrameServiceEntity> listServices(String serviceIds) {
List<String> ids = Arrays.stream(serviceIds.split(",")).collect(Collectors.toList());
return this.list(new QueryWrapper<FrameServiceEntity>().in(Constants.ID, ids));
return this.lambdaQuery().in(FrameServiceEntity::getId, ids).list();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public final class Constants {
public static final String LOCALE_LANGUAGE = "language";
public static final String CODE = "code";
public static final String CLUSTER_CODE = "cluster_code";
public static final String ID = "id";
public static final String START_DISTRIBUTE_AGENT = "start_distribute_agent";
public static final String CHECK_WORKER_MD5_CMD = "md5sum "+INSTALL_PATH+"/datasophon-worker.tar.gz | awk '{print $1}'";
public static final String CREATE_TIME = "create_time";
Expand All @@ -92,8 +91,6 @@ public final class Constants {
public static final String IS_ENABLED = "is_enabled";
public static final String SORT_NUM = "sort_num";
public static final String CN = "chinese";
public static final String ALERT_GROUP_ID = "alert_group_id";
public static final String ALERT_QUOTA_NAME = "alert_quota_name";
public static final String NAME = "name";
public static final String QUEUE_NAME = "queue_name";
public static final String ROLE_TYPE = "role_type";
Expand All @@ -107,8 +104,6 @@ public final class Constants {
public static final String ROLE_GROUP_TYPE = "role_group_type";
public static final String NEET_RESTART = "need_restart";

public static final String ALERT_GROUP_NAME = "alert_group_name";
public static final String ALERT_LEVEL = "alert_level";
public static final String CPU_ARCHITECTURE = "cpu_architecture";
public static final String HOST_STATE = "host_state";
public static final String FAILED = "failed";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.datasophon.common.Constants;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -80,6 +81,12 @@ public static Result success() {
return result;
}

public static Result successEmptyCount() {
Result result = success(new ArrayList<>(0));
result.put(Constants.TOTAL, 0);
return result;
}

@Override
public Result put(String key, Object value) {
super.put(key, value);
Expand Down

0 comments on commit dabb84b

Please sign in to comment.