Skip to content

Commit

Permalink
Optimize configuration management services
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbingleixue committed Sep 18, 2024
1 parent 9eeefec commit ba3db27
Show file tree
Hide file tree
Showing 57 changed files with 1,591 additions and 2,477 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

/**
* Kie Client
Expand Down Expand Up @@ -231,14 +232,15 @@ public Map<String, List<String>> getConfigList(String key, String group, boolean
return Collections.emptyMap();
}
Map<String, List<String>> result = new HashMap<>();
Pattern pattern = Pattern.compile(covertGroup);
for (KieConfigEntity entity : kieResponse.getData()) {
if (exactMatchFlag) {
List<String> configList = result.computeIfAbsent(LabelGroupUtils.createLabelGroup(entity.getLabels()),
configKey -> new ArrayList<>());
configList.add(entity.getKey());
} else {
String currentConfigGroup = LabelGroupUtils.createLabelGroup(entity.getLabels());
if (currentConfigGroup.contains(covertGroup)) {
if (currentConfigGroup.contains(covertGroup) || pattern.matcher(covertGroup).matches()) {
List<String> configList = result.computeIfAbsent(
LabelGroupUtils.createLabelGroup(entity.getLabels()), configKey -> new ArrayList<>());
configList.add(entity.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -175,9 +176,13 @@ private Map<String, List<String>> fuzzyGetConfigListByGroupAndKey(String key, St
private void fillChildrenInfo(String path, Map<String, List<String>> configMap, String nodeName,
String key) throws InterruptedException, KeeperException {
List<String> children = this.zkClient.getChildren(path, false);
if (children == null || children.isEmpty()) {
return;
}
Pattern groupPattern = Pattern.compile(nodeName);
children.parallelStream().forEach(child -> {
List<String> subChild;
if (!child.contains(nodeName)) {
if (!child.contains(nodeName) && !groupPattern.matcher(child).matches()) {
return;
}
String childPath = toPath(child, path);
Expand All @@ -193,7 +198,8 @@ private void fillChildrenInfo(String path, Map<String, List<String>> configMap,
configMap.put(childPath.substring(1), subChild);
return;
}
List<String> matchSubChild = subChild.stream().filter(value -> value.contains(key))
List<String> matchSubChild = subChild.stream()
.filter(value -> value.contains(key) || Pattern.compile(key).matcher(value).matches())
.collect(Collectors.toList());
if (matchSubChild.isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ public class CommonConst {
+ "--- " + System.lineSeparator();

/**
* Service name for global configuration
* Plugin type for non-Semant plugins
*/
public static final String GLOBAL_CONFIGURATION_SERVICE_NAME = "ALL_SERVICE";
public static final String OTHER_PLUGIN = "other";

/**
* Default regular expression for configuration item Group
* Wildcard
*/
public static final String CONFIGURATION_DEFAULT_PATTERN = "^(app=[^&]*+&environment=[^&]*+(&service=[^&]*)?)?$";
public static final String WILDCARD = "[^&]+";

private CommonConst() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,10 @@ public class DynamicConfig {
*/
@Value("${dynamic.config.enable}")
private boolean enable;

/**
* Path to the template file
*/
@Value("${dynamic.config.template.path}")
private String templatePath;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package io.sermant.backend.controller;

import io.sermant.backend.common.conf.CommonConst;
import io.sermant.backend.entity.config.ConfigInfo;
import io.sermant.backend.entity.config.ConfigServerInfo;
import io.sermant.backend.entity.config.PluginType;
import io.sermant.backend.entity.config.Result;
import io.sermant.backend.entity.config.ResultCodeType;
import io.sermant.backend.service.ConfigService;
Expand All @@ -35,7 +35,6 @@
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

import javax.annotation.Resource;

Expand Down Expand Up @@ -63,19 +62,13 @@ public Result<List<ConfigInfo>> getConfigList(ConfigInfo configInfo) {
if (StringUtils.isEmpty(configInfo.getPluginType())) {
return new Result<>(ResultCodeType.MISS_PARAM.getCode(), ResultCodeType.MISS_PARAM.getMessage());
}
Optional<PluginType> optionalPluginType = PluginType.getPluginType(configInfo.getPluginType());
if (!optionalPluginType.isPresent()) {
return new Result<>(ResultCodeType.FAIL.getCode(), "Invalid plugin name.");
}
PluginType pluginType = optionalPluginType.get();
boolean exactMatchFlag = false;
if (pluginType == PluginType.OTHER) {
if (StringUtils.equals(configInfo.getPluginType(), CommonConst.OTHER_PLUGIN)) {
if (StringUtils.isEmpty(configInfo.getGroup())) {
return new Result<>(ResultCodeType.MISS_PARAM.getCode(), ResultCodeType.MISS_PARAM.getMessage());
}
exactMatchFlag = true;
return configService.getConfigList(configInfo);
}
return configService.getConfigList(configInfo, pluginType, exactMatchFlag);
return configService.getConfigList(configInfo);
}

/**
Expand Down Expand Up @@ -104,7 +97,8 @@ public Result<Boolean> addConfig(@RequestBody ConfigInfo configInfo) {
|| StringUtils.isEmpty(configInfo.getContent())) {
return new Result<>(ResultCodeType.MISS_PARAM.getCode(), ResultCodeType.MISS_PARAM.getMessage());
}
Result<List<ConfigInfo>> result = configService.getConfigList(configInfo, PluginType.OTHER, true);
configInfo.setExactMatchFlag(true);
Result<List<ConfigInfo>> result = configService.getConfigList(configInfo);
if (CollectionUtils.isEmpty(result.getData())) {
return configService.publishConfig(configInfo);
}
Expand All @@ -123,7 +117,8 @@ public Result<Boolean> updateConfig(@RequestBody ConfigInfo configInfo) {
|| StringUtils.isEmpty(configInfo.getContent())) {
return new Result<>(ResultCodeType.MISS_PARAM.getCode(), ResultCodeType.MISS_PARAM.getMessage());
}
Result<List<ConfigInfo>> result = configService.getConfigList(configInfo, PluginType.OTHER, true);
configInfo.setExactMatchFlag(true);
Result<List<ConfigInfo>> result = configService.getConfigList(configInfo);
if (result.isSuccess() && CollectionUtils.isEmpty(result.getData())) {
return new Result<>(ResultCodeType.NOT_EXISTS.getCode(), ResultCodeType.NOT_EXISTS.getMessage());
}
Expand All @@ -141,7 +136,8 @@ public Result<Boolean> deleteConfig(ConfigInfo configInfo) {
if (StringUtils.isEmpty(configInfo.getGroup()) || StringUtils.isEmpty(configInfo.getKey())) {
return new Result<>(ResultCodeType.MISS_PARAM.getCode(), ResultCodeType.MISS_PARAM.getMessage());
}
Result<List<ConfigInfo>> result = configService.getConfigList(configInfo, PluginType.OTHER, true);
configInfo.setExactMatchFlag(true);
Result<List<ConfigInfo>> result = configService.getConfigList(configInfo);
if (result.isSuccess() && CollectionUtils.isEmpty(result.getData())) {
return new Result<>(ResultCodeType.NOT_EXISTS.getCode(), ResultCodeType.NOT_EXISTS.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.sermant.backend.controller;

import io.sermant.backend.entity.config.Result;
import io.sermant.backend.entity.template.PluginTemplateInfo;
import io.sermant.backend.service.TemplateService;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.annotation.Resource;

/**
* Template controller
*
* @author zhp
* @since 2024-08-22
*/
@RestController
@RequestMapping("/sermant")
public class TemplateController {
@Resource
private TemplateService templateService;

/**
* Gte the template information
*
* @return the template information
*/
@GetMapping("/templates")
public Result<List<PluginTemplateInfo>> getTemplateInfo() {
return templateService.getTemplateList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,31 @@ public class ConfigInfo {
* Configuration Content
*/
private String content;

/**
* Group generation rules
*/
private String groupRule;

/**
* Key generation rules
*/
private String keyRule;

/**
* Indicator for exact matching
*/
private boolean exactMatchFlag;

public ConfigInfo(String key, String group, String keyRule, String groupRule, String pluginType, String namespace) {

Check failure on line 93 in sermant-backend/src/main/java/io/sermant/backend/entity/config/ConfigInfo.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-backend/src/main/java/io/sermant/backend/entity/config/ConfigInfo.java:93:5: error: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)

Check failure on line 93 in sermant-backend/src/main/java/io/sermant/backend/entity/config/ConfigInfo.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 More than 5 parameters (found 6). Raw Output: /home/runner/work/Sermant/Sermant/./sermant-backend/src/main/java/io/sermant/backend/entity/config/ConfigInfo.java:93:12: error: More than 5 parameters (found 6). (com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck)
this.key = key;
this.group = group;
this.groupRule = groupRule;
this.keyRule = keyRule;
this.pluginType = pluginType;
this.namespace = namespace;
}

public ConfigInfo() {

Check failure on line 102 in sermant-backend/src/main/java/io/sermant/backend/entity/config/ConfigInfo.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-backend/src/main/java/io/sermant/backend/entity/config/ConfigInfo.java:102:5: error: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ public Result(ResultCodeType resultCodeType) {
this.message = resultCodeType.getMessage();
}

/**
* Constructor
*
* @param resultCodeType Result information
* @param data result data
*/
public Result(ResultCodeType resultCodeType, R data) {
this.code = resultCodeType.getCode();
this.message = resultCodeType.getMessage();
this.data = data;
}

/**
* Constructor
*
Expand Down
Loading

0 comments on commit ba3db27

Please sign in to comment.