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 17, 2024
1 parent 9eeefec commit 08920fc
Show file tree
Hide file tree
Showing 57 changed files with 1,599 additions and 2,485 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 All @@ -59,9 +60,9 @@ public class ZooKeeperClient implements ConfigClient {
/**
* Create a ZooKeeperClient, initialize the ZK client
*
* @param connectString connect string, must be in the following format: {@code host:port[(,host:port)...]}
* @param connectString connect string, must be in the following format: {@code host:port[(,host:port)...]}

Check warning on line 63 in sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperClient.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperClient.java:63:0: warning: 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck)
* @param sessionTimeout session timeout
* @param watcher Watcher for event processing
* @param watcher Watcher for event processing

Check warning on line 65 in sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperClient.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperClient.java:65:0: warning: 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck)
* @throws ZooKeeperInitException zk initialization exception
*/
public ZooKeeperClient(String connectString, int sessionTimeout, Watcher watcher) {
Expand All @@ -76,7 +77,7 @@ public ZooKeeperClient(String connectString, int sessionTimeout, Watcher watcher
* Add the specified scheme:auth information to this connection.
*
* @param scheme Permission control scheme
* @param auth Authorization information
* @param auth Authorization information

Check warning on line 80 in sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperClient.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperClient.java:80:0: warning: 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck)
*/
public void addAuthInfo(String scheme, byte[] auth) {
zkClient.addAuthInfo(scheme, auth);
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 Expand Up @@ -323,7 +329,7 @@ public List<String> listAllNodes(String path) {
* Note that when other listeners on the same node are precisely removed, the watcher will choose to abandon the
* loop registration because it cannot identify whether to remove itself
*
* @param path node path
* @param path node path

Check warning on line 332 in sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperClient.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperClient.java:332:0: warning: 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck)
* @param watcher The actual executing watcher
* @param handler Exception handler after loop registration failure of watcher
* @return add result
Expand Down Expand Up @@ -355,7 +361,7 @@ public void process(WatchedEvent event) {
/**
* Adds persistent recursive listeners, effective for descendant nodes
*
* @param path node path
* @param path node path

Check warning on line 364 in sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperClient.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperClient.java:364:0: warning: 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck)
* @param watcher node watcher
* @return add result
*/
Expand Down Expand Up @@ -410,7 +416,7 @@ public void close() throws InterruptedException {
/**
* Get the ZK path and fills in the missing "/"
*
* @param key key
* @param key key

Check warning on line 419 in sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperClient.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/zookeeper/ZooKeeperClient.java:419:0: warning: 编程规范-建议3.8 禁止插入空格水平对齐(包括多余括号)。 (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck)
* @param group group
* @return ZK path
*/
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)
}
}
Loading

0 comments on commit 08920fc

Please sign in to comment.