Skip to content

Commit

Permalink
Optimize configuration management services
Browse files Browse the repository at this point in the history
Signed-off-by: hanbingleixue <hanbingleixue@hotmail.com>
  • Loading branch information
hanbingleixue committed Sep 23, 2024
1 parent acda541 commit bcaffdb
Show file tree
Hide file tree
Showing 59 changed files with 2,045 additions and 2,677 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

/**
* Kie Client
Expand All @@ -55,6 +57,11 @@ public class KieClient extends AbstractClient implements ConfigClient {

private final ResultHandler<KieResponse> defaultHandler = new ResultHandler.DefaultResultHandler();

/**
* Regular expression map, storing regular expressions for configuration items
*/
private final Map<String, Pattern> patternMap = new ConcurrentHashMap<>();

private String kieApi;

/**
Expand Down Expand Up @@ -221,24 +228,25 @@ public String getConfig(String key, String group) {
@Override
public Map<String, List<String>> getConfigList(String key, String group, boolean exactMatchFlag) {
final KieResponse kieResponse;
String covertGroup = group.replace(KieConstants.SEPARATOR, KieConstants.CONNECTOR);
String convertedGroup = group.replace(KieConstants.SEPARATOR, KieConstants.CONNECTOR);
if (exactMatchFlag) {
kieResponse = getKieResponse(key, covertGroup, exactMatchFlag);
kieResponse = getKieResponse(key, convertedGroup, exactMatchFlag);
} else {
kieResponse = getKieResponse(key, null, exactMatchFlag);
}
if (kieResponse == null || kieResponse.getData() == null) {
return Collections.emptyMap();
}
Map<String, List<String>> result = new HashMap<>();
Pattern pattern = patternMap.computeIfAbsent(convertedGroup, patternKey -> Pattern.compile(convertedGroup));
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(convertedGroup) || pattern.matcher(currentConfigGroup).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,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand All @@ -51,6 +53,11 @@ public class ZooKeeperClient implements ConfigClient {

private static final Logger LOGGER = LoggerFactory.getLogger(ZooKeeperClient.class.getName());

/**
* Regular expression map, storing regular expressions for configuration items
*/
private final Map<String, Pattern> patternMap = new ConcurrentHashMap<>();

/**
* ZK client
*/
Expand Down Expand Up @@ -175,9 +182,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 = patternMap.computeIfAbsent(nodeName, patternKey -> 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 +204,9 @@ 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))
Pattern keyPattern = patternMap.computeIfAbsent(key, patternKey -> Pattern.compile(key));
List<String> matchSubChild = subChild.stream()
.filter(value -> value.contains(key) || keyPattern.matcher(value).matches())
.collect(Collectors.toList());
if (matchSubChild.isEmpty()) {
return;
Expand Down
8 changes: 7 additions & 1 deletion sermant-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,13 @@
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>

<resource>
<directory>../sermant-plugins/config_ui_template</directory>
<includes>
<include>*.yml</include>
</includes>
<targetPath>template</targetPath>
</resource>
<resource>
<directory>src/main/webapp/</directory>
<excludes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,19 @@ public class CommonConst {
+ "--- " + System.lineSeparator();

/**
* Service name for global configuration
* Common Template Name
*/
public static final String GLOBAL_CONFIGURATION_SERVICE_NAME = "ALL_SERVICE";
public static final String COMMON_TEMPLATE = "common";

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

/**
* Wildcard characters in regular expressions
*/
public static final String QUERY_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,11 @@ 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.isEmpty(configInfo.getGroup())) {
return new Result<>(ResultCodeType.MISS_PARAM.getCode(), ResultCodeType.MISS_PARAM.getMessage());
}
exactMatchFlag = true;
if (StringUtils.equals(configInfo.getPluginType(), CommonConst.COMMON_TEMPLATE)
&& StringUtils.isEmpty(configInfo.getGroupRule())) {
return new Result<>(ResultCodeType.MISS_PARAM.getCode(), ResultCodeType.MISS_PARAM.getMessage());
}
return configService.getConfigList(configInfo, pluginType, exactMatchFlag);
return configService.getConfigList(configInfo);
}

/**
Expand Down Expand Up @@ -104,7 +95,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);
Result<List<ConfigInfo>> result = configService.getConfigList(new ConfigInfo(configInfo.getKey(),
configInfo.getGroup(), CommonConst.COMMON_TEMPLATE, true, configInfo.getNamespace()));
if (CollectionUtils.isEmpty(result.getData())) {
return configService.publishConfig(configInfo);
}
Expand All @@ -123,7 +115,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);
Result<List<ConfigInfo>> result = configService.getConfigList(new ConfigInfo(configInfo.getKey(),
configInfo.getGroup(), CommonConst.COMMON_TEMPLATE, true, configInfo.getNamespace()));
if (result.isSuccess() && CollectionUtils.isEmpty(result.getData())) {
return new Result<>(ResultCodeType.NOT_EXISTS.getCode(), ResultCodeType.NOT_EXISTS.getMessage());
}
Expand All @@ -141,7 +134,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);
Result<List<ConfigInfo>> result = configService.getConfigList(new ConfigInfo(configInfo.getKey(),
configInfo.getGroup(), CommonConst.COMMON_TEMPLATE, true, configInfo.getNamespace()));
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.PageTemplateInfo;
import io.sermant.backend.service.PageTemplateService;

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 for configuration management page
*
* @author zhp
* @since 2024-08-22
*/
@RestController
@RequestMapping("/sermant")
public class PageTemplateController {
@Resource
private PageTemplateService pageTemplateService;

/**
* Get the template information for configuration management page
*
* @return the template information
*/
@GetMapping("/templates")
public Result<List<PageTemplateInfo>> getTemplateInfo() {
return pageTemplateService.getTemplateList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import lombok.Getter;
import lombok.Setter;

import org.apache.commons.lang.StringUtils;

import java.util.Objects;

/**
* configuration information
*
Expand Down Expand Up @@ -74,4 +78,77 @@ 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;

/**
* Constructor
*
* @param key The key of the configuration item
* @param group Grouping name for configuration items
* @param keyRule Key generation rules
* @param groupRule Group generation rules
* @param namespace The namespace to which the configuration item belongs
*/
public ConfigInfo(String key, String group, String keyRule, String groupRule, String namespace) {
this.key = key;
this.group = group;
this.groupRule = groupRule;
this.keyRule = keyRule;
this.namespace = namespace;
}

/**
* Constructor
*
* @param keyRule Key generation rules
* @param groupRule Group generation rules
* @param pluginType plugin type, the plugin to which the configuration item belongs
* @param exactMatchFlag Indicator for exact matching
* @param namespace The namespace to which the configuration item belongs
*/
public ConfigInfo(String keyRule, String groupRule, String pluginType, boolean exactMatchFlag, String namespace) {
this.keyRule = keyRule;
this.groupRule = groupRule;
this.pluginType = pluginType;
this.exactMatchFlag = exactMatchFlag;
this.namespace = namespace;
}

/**
* Constructor
*/
public ConfigInfo() {
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
ConfigInfo configInfo = (ConfigInfo) obj;
return StringUtils.equals(this.getKey(), configInfo.getKey())
&& StringUtils.equals(this.getGroup(), configInfo.getGroup());
}

@Override
public int hashCode() {
return Objects.hash(this.getKey(), this.getGroup());
}
}
Loading

0 comments on commit bcaffdb

Please sign in to comment.