Skip to content

Commit

Permalink
Change the JSON format of gray rule.(alibaba#12217
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewAden committed Sep 11, 2024
1 parent b1bc3a1 commit 41e3ce4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,17 @@ public class Constants {

public static final String CONFIG_SEARCH_ACCURATE = "accurate";

/**
* Gray rule.
*/
public static final String GRAY_RULE_TYPE = "type";

public static final String GRAY_RULE_EXPR = "expr";

public static final String GRAY_RULE_VERSION = "version";

public static final String GRAY_RULE_PRIORITY = "priority";

/**
* default nacos encode.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,35 @@

package com.alibaba.nacos.config.server.model.gray;

import com.google.gson.annotations.SerializedName;

/**
* description.
*
* @author rong
* @date 2024-03-14 10:57
*/
public class ConfigGrayPersistInfo {
@SerializedName("gray_type")
private String grayType;

private String type;

private String version;

private String expr;

private int priority;

public ConfigGrayPersistInfo(String grayType, String version, String expr, int priority) {
this.grayType = grayType;
public ConfigGrayPersistInfo(String type, String version, String expr, int priority) {
this.type = type;
this.version = version;
this.expr = expr;
this.priority = priority;
}

public String getGrayType() {
return grayType;
public String getType() {
return type;
}

public void setGrayType(String grayType) {
this.grayType = grayType;
public void setType(String type) {
this.type = type;
}

public String getVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static Class<?> getClassByTypeAndVersion(String type, String version) {
* @date 2024/3/14
*/
public static GrayRule constructGrayRule(ConfigGrayPersistInfo configGrayPersistInfo) {
Class<?> classByTypeAndVersion = getClassByTypeAndVersion(configGrayPersistInfo.getGrayType(),
Class<?> classByTypeAndVersion = getClassByTypeAndVersion(configGrayPersistInfo.getType(),
configGrayPersistInfo.getVersion());
if (classByTypeAndVersion == null) {
return null;
Expand All @@ -74,7 +74,7 @@ public static GrayRule constructGrayRule(ConfigGrayPersistInfo configGrayPersist
configGrayPersistInfo.getPriority());
} catch (Exception e) {
throw new RuntimeException(String.format("construct gray rule failed with type[%s], version[%s].",
configGrayPersistInfo.getGrayType(), configGrayPersistInfo.getVersion()), e);
configGrayPersistInfo.getType(), configGrayPersistInfo.getVersion()), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
ConfigAllInfo4Gray oldConfigAllInfo4Gray = findConfigAllInfo4Gray(dataId, group,
tenantTmp, grayNameTmp);
if (oldConfigAllInfo4Gray == null) {
if (LogUtil.FATAL_LOG.isErrorEnabled()) {
LogUtil.FATAL_LOG.error("expected config info[dataid:{}, group:{}, tenent:{}] but not found.",
if (LogUtil.FATAL_LOG.isWarnEnabled()) {
LogUtil.FATAL_LOG.warn("expected config info[dataid:{}, group:{}, tenent:{}] but not found.",
dataId, group, tenant);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.config.server.utils;

import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.model.ConfigAllInfo;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -116,6 +117,7 @@ public static String getExtraInfoFromAllInfo(ConfigAllInfo configAllInfo) {

public static String getExtraInfoFromGrayInfo(String grayNameTmp, String grayRuleTmp, String oldSrcUser) {
ObjectNode node = OBJECT_MAPPER.createObjectNode();
ObjectNode grayRuleNode = OBJECT_MAPPER.createObjectNode();

if (StringUtils.isNotBlank(grayNameTmp)) {
node.put("gray_name", grayNameTmp);
Expand All @@ -127,14 +129,28 @@ public static String getExtraInfoFromGrayInfo(String grayNameTmp, String grayRul

if (StringUtils.isNotBlank(grayRuleTmp)) {
try {
JsonNode grayRuleNode = OBJECT_MAPPER.readTree(grayRuleTmp);
node.setAll((ObjectNode) grayRuleNode);
JsonNode parsedGrayRuleNode = OBJECT_MAPPER.readTree(grayRuleTmp);
if (parsedGrayRuleNode.has(Constants.GRAY_RULE_TYPE)) {
grayRuleNode.put(Constants.GRAY_RULE_TYPE, parsedGrayRuleNode.get(Constants.GRAY_RULE_TYPE).asText());
}
if (parsedGrayRuleNode.has(Constants.GRAY_RULE_EXPR)) {
grayRuleNode.put(Constants.GRAY_RULE_EXPR, parsedGrayRuleNode.get(Constants.GRAY_RULE_EXPR).asText());
}
if (parsedGrayRuleNode.has(Constants.GRAY_RULE_VERSION)) {
grayRuleNode.put(Constants.GRAY_RULE_VERSION, parsedGrayRuleNode.get(Constants.GRAY_RULE_VERSION).asText());
}
if (parsedGrayRuleNode.has(Constants.GRAY_RULE_PRIORITY)) {
grayRuleNode.put(Constants.GRAY_RULE_PRIORITY, parsedGrayRuleNode.get(Constants.GRAY_RULE_PRIORITY).asText());
}
node.set("gray_rule", grayRuleNode);
} catch (Exception ex) {
LOGGER.error("Failed to parse grayRuleTmp as JSON: " + grayRuleTmp, ex);
LOGGER.error("Failed to parse gray rule as json", ex);
return null;
}
}

node.put("type", "json");

try {
return OBJECT_MAPPER.writeValueAsString(node);
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void testDumpGray() throws Exception {
String group = "group11";
String tenant = "tenant112";
String grayName = "grayName";
String grayRule = "{\"gray_type\":\"tag\",\"version\":\"1.0.0\",\"expr\":\"dgray123\",\"priority\":1}";
String grayRule = "{\"type\":\"tag\",\"version\":\"1.0.0\",\"expr\":\"dgray123\",\"priority\":1}";
String content = "mockContent11";

String md5 = MD5Utils.md5Hex(content, "UTF-8");
Expand Down Expand Up @@ -201,7 +201,7 @@ public void testDumpGray() throws Exception {

//ts new ,md5 not update,grayRule changes
long tsNew2 = tsNew + 1;
String grayRuleNew = "{\"gray_type\":\"tag\",\"version\":\"1.0.0\",\"expr\":\"gray1234\",\"priority\":1}";
String grayRuleNew = "{\"type\":\"tag\",\"version\":\"1.0.0\",\"expr\":\"gray1234\",\"priority\":1}";

String contentWithPrev = contentNew;
boolean resultNew2 = ConfigCacheService.dumpGray(dataId, group, tenant, grayName, grayRuleNew, contentWithPrev,
Expand Down

0 comments on commit 41e3ce4

Please sign in to comment.