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 19, 2024
1 parent acda541 commit cf211cc
Show file tree
Hide file tree
Showing 59 changed files with 1,866 additions and 2,627 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 @@ -221,24 +222,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 = 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,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,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 = 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,14 @@ 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
*/
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.COMMON_TEMPLATE)) {
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.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 @@ -74,4 +74,42 @@ 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
*/
public ConfigInfo() {
}
}

This file was deleted.

Loading

0 comments on commit cf211cc

Please sign in to comment.