Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement-399 #430

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.datasophon.api.check;

import com.datasophon.api.enums.Status;
import com.datasophon.api.exceptions.ServiceException;
import com.datasophon.api.service.ClusterServiceRoleInstanceService;
import com.datasophon.common.model.ServiceRoleHostMapping;
import com.datasophon.common.utils.CollectionUtils;
import com.datasophon.dao.entity.ClusterServiceRoleInstanceEntity;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class CheckBasicServiceRule extends CheckRule{
private static final List<String> MUST_AT_SAME_NODE_BASIC_SERVICE =
Arrays.asList("Grafana", "AlertManager", "Prometheus");


private ClusterServiceRoleInstanceService roleInstanceService;

public CheckBasicServiceRule(ClusterServiceRoleInstanceService roleInstanceService) {
this.roleInstanceService = roleInstanceService;
}

@Override
public void checkeNodeRule(Integer clusterId, List<ServiceRoleHostMapping> list) {
Set<String> hostnameSet =
list.stream()
.filter(s -> MUST_AT_SAME_NODE_BASIC_SERVICE.contains(s.getServiceRole()))
.map(ServiceRoleHostMapping::getHosts)
.flatMap(Collection::stream)
.collect(Collectors.toSet());
if (CollectionUtils.isEmpty(hostnameSet)) {
return;
}

Set<String> installedHostnameSet =
roleInstanceService.lambdaQuery()
.eq(ClusterServiceRoleInstanceEntity::getClusterId, clusterId)
.in(
ClusterServiceRoleInstanceEntity::getServiceName,
MUST_AT_SAME_NODE_BASIC_SERVICE)
.list().stream()
.map(ClusterServiceRoleInstanceEntity::getHostname)
.collect(Collectors.toSet());
hostnameSet.addAll(installedHostnameSet);

if (hostnameSet.size() > 1) {
throw new ServiceException(Status.BASIC_SERVICE_SELECT_MOST_ONE_HOST.getMsg());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.datasophon.api.check;

import com.datasophon.api.enums.Status;
import com.datasophon.api.exceptions.ServiceException;
import com.datasophon.api.service.ClusterServiceRoleInstanceService;
import com.datasophon.common.model.ServiceRoleHostMapping;
import com.datasophon.common.utils.CollectionUtils;
import com.datasophon.dao.entity.ClusterServiceRoleInstanceEntity;
import org.springframework.beans.factory.annotation.Autowired;

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

public class CheckDorisRule extends CheckRule{
private static final List<String> MUST_AT_NOT_SAME_NODE_DORIS =
Arrays.asList("DorisFE","DorisFEObserver");

private ClusterServiceRoleInstanceService roleInstanceService;

public CheckDorisRule(ClusterServiceRoleInstanceService roleInstanceService) {
this.roleInstanceService = roleInstanceService;
}

@Override
public void checkeNodeRule(Integer clusterId, List<ServiceRoleHostMapping> list) {
Set<String> hostnameSet =
list.stream()
.filter(s -> MUST_AT_NOT_SAME_NODE_DORIS.contains(s.getServiceRole()))
.map(ServiceRoleHostMapping::getHosts)
.flatMap(Collection::stream)
.collect(Collectors.toSet());
if (CollectionUtils.isEmpty(hostnameSet)) {
return;
}


Map<String, String> roleHostMap = roleInstanceService.lambdaQuery()
.eq(ClusterServiceRoleInstanceEntity::getClusterId, clusterId)
.in(
ClusterServiceRoleInstanceEntity::getServiceName,
MUST_AT_NOT_SAME_NODE_DORIS)
.list().stream()
.collect(Collectors.toMap(ClusterServiceRoleInstanceEntity::getHostname,
ClusterServiceRoleInstanceEntity::getServiceRoleName));

if (roleHostMap.size() < 2) {
throw new ServiceException(Status.DORIS_FE_OBSERVER_NUST_IN_DIFFERENCE_NODE.getMsg());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.datasophon.api.check;

import com.datasophon.common.model.ServiceRoleHostMapping;

import java.util.List;

public abstract class CheckRule {
public abstract void checkeNodeRule(Integer clusterId, List<ServiceRoleHostMapping> list);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.alibaba.fastjson.JSONObject;

/**
* status enum
* status enum
*/
public enum Status {

Expand Down Expand Up @@ -86,6 +86,8 @@ public enum Status {
"All instances of the same service on the same machine need to be within the same role group",
"同一个服务在同一台机器上的所有实例需要在同一个角色组内"),
ODD_NUMBER_ARE_REQUIRED_FOR_DORISFE(10040, "The Number of DorisFE must be an odd number.", "DorisFE个数必须是奇数"),
DORIS_FE_OBSERVER_NUST_IN_DIFFERENCE_NODE(10041,"The DorisFE and DorisFEObserver must in difference node.",
"DorisFE和DorisFEObserver必须分布在不同节点"),
NO_SERVICE_ROLE_SELECTED(10041, "No service role selected", "未选择需要安装的服务实例");


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.datasophon.api.check.CheckBasicServiceRule;
import com.datasophon.api.check.CheckDorisRule;
import com.datasophon.api.check.CheckRule;
import com.datasophon.api.enums.Status;
import com.datasophon.api.exceptions.ServiceException;
import com.datasophon.api.load.GlobalVariables;
Expand Down Expand Up @@ -274,7 +277,7 @@ public Result saveServiceConfig(
@Override
public Result saveServiceRoleHostMapping(Integer clusterId, List<ServiceRoleHostMapping> list) {

checkOnSameNode(clusterId, list);
checkRules(clusterId, list);

ClusterInfoEntity clusterInfo = clusterInfoService.getById(clusterId);
String hostMapKey =
Expand Down Expand Up @@ -307,6 +310,19 @@ public Result saveServiceRoleHostMapping(Integer clusterId, List<ServiceRoleHost
return Result.success();
}

/*
* 规则校验入口
* */
private void checkRules(Integer clusterId, List<ServiceRoleHostMapping> list) {
List<CheckRule> checkRules = new ArrayList<>();
checkRules.add(new CheckBasicServiceRule(roleInstanceService));
checkRules.add(new CheckDorisRule(roleInstanceService));
checkRules.forEach(e->{
e.checkeNodeRule(clusterId,list);
});
}


@Override
public Result saveHostServiceRoleMapping(Integer clusterId, List<HostServiceRoleMapping> list) {
ClusterInfoEntity clusterInfo = clusterInfoService.getById(clusterId);
Expand Down Expand Up @@ -657,33 +673,6 @@ private void buildConfig(
roleGroupConfig.setConfigFileJsonMd5(SecureUtil.md5(configFileJson));
}

private void checkOnSameNode(Integer clusterId, List<ServiceRoleHostMapping> list) {
Set<String> hostnameSet =
list.stream()
.filter(s -> MUST_AT_SAME_NODE_BASIC_SERVICE.contains(s.getServiceRole()))
.map(ServiceRoleHostMapping::getHosts)
.flatMap(Collection::stream)
.collect(Collectors.toSet());
if (CollectionUtils.isEmpty(hostnameSet)) {
return;
}

Set<String> installedHostnameSet =
roleInstanceService.lambdaQuery()
.eq(ClusterServiceRoleInstanceEntity::getClusterId, clusterId)
.in(
ClusterServiceRoleInstanceEntity::getServiceName,
MUST_AT_SAME_NODE_BASIC_SERVICE)
.list().stream()
.map(ClusterServiceRoleInstanceEntity::getHostname)
.collect(Collectors.toSet());
hostnameSet.addAll(installedHostnameSet);

if (hostnameSet.size() > 1) {
throw new ServiceException(Status.BASIC_SERVICE_SELECT_MOST_ONE_HOST.getMsg());
}
}

private void serviceValidation(ServiceRoleHostMapping serviceRoleHostMapping) {
String serviceRole = serviceRoleHostMapping.getServiceRole();
List<String> hosts = serviceRoleHostMapping.getHosts();
Expand Down