Skip to content

Commit

Permalink
[ISSUE#12217] Add the reconciliation mechanism of gray configuration (#…
Browse files Browse the repository at this point in the history
…12507)

* Refine the formal configuration and add additional information(#12217)

* Change the "configType" field to "publishType"(#12217)

* Improve the gray configuration(#12217)

* add the reconciliation mechanism of gray configuration(#12217)

* Change the JSON format of gray rule.(#12217)

* Remove redundant queries(#12217)

* fix reconciliation mechanism of gray configuration.(#12217)

* Adjust the way of obtaining grayName.(#12217)

* Change the variable name.(#12217
  • Loading branch information
MatthewAden authored Oct 8, 2024
1 parent 6c3cbf8 commit 8af465a
Show file tree
Hide file tree
Showing 48 changed files with 2,254 additions and 459 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,31 @@ 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.
*/
public static final String DEFAULT_NACOS_ENCODE = "UTF-8";

public static final String NACOS_PERSIST_ENCODE_KEY = "nacosPersistEncodingKey";

/**
* config publish type.
*/
public static final String FORMAL = "formal";

public static final String GRAY = "gray";

static String getPersistEncode() {
String persistEncode = System.getenv(NACOS_PERSIST_ENCODE_KEY);
if (StringUtils.isBlank(persistEncode)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,11 @@ public RestResult<Boolean> deleteConfigs(HttpServletRequest request, @RequestPar
String clientIp = RequestUtil.getRemoteIp(request);
String srcUser = RequestUtil.getSrcUserName(request);
final Timestamp time = TimeUtils.getCurrentTime();
List<ConfigInfo> configInfoList = configInfoPersistService.removeConfigInfoByIds(ids, clientIp, srcUser);
List<ConfigAllInfo> configInfoList = configInfoPersistService.removeConfigInfoByIds(ids, clientIp, srcUser);
if (CollectionUtils.isEmpty(configInfoList)) {
return RestResultUtils.success(true);
}
for (ConfigInfo configInfo : configInfoList) {
for (ConfigAllInfo configInfo : configInfoList) {
ConfigChangePublisher.notifyConfigChange(
new ConfigDataChangeEvent(false, configInfo.getDataId(), configInfo.getGroup(),
configInfo.getTenant(), time.getTime()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 1999-$toady.year Alibaba Group Holding Ltd.
*
* 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 com.alibaba.nacos.config.server.model;

/**
* ConfigAllInfo4Gray.
*
* @author Nacos
*/
public class ConfigAllInfo4Gray extends ConfigInfo {

private static final long serialVersionUID = -7926709237557990936L;

private String srcUser;

private String srcIp;

private long gmtCreate;

private long gmtModified;

private String grayName;

private String grayRule;

public String getSrcUser() {
return srcUser;
}

public void setSrcUser(String srcUser) {
this.srcUser = srcUser;
}

public String getSrcIp() {
return srcIp;
}

public void setSrcIp(String srcIp) {
this.srcIp = srcIp;
}

public long getGmtCreate() {
return gmtCreate;
}

public void setGmtCreate(long gmtCreate) {
this.gmtCreate = gmtCreate;
}

public long getGmtModified() {
return gmtModified;
}

public void setGmtModified(long gmtModified) {
this.gmtModified = gmtModified;
}

public String getGrayName() {
return grayName;
}

public void setGrayName(String grayName) {
this.grayName = grayName;
}

public String getGrayRule() {
return grayRule;
}

public void setGrayRule(String grayRule) {
this.grayRule = grayRule;
}

@Override
public int hashCode() {
return super.hashCode();
}

@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public class ConfigHistoryInfo implements Serializable {
*/
private String opType;

private String publishType;

private String extInfo;

private Timestamp createdTime;

private Timestamp lastModifiedTime;
Expand Down Expand Up @@ -136,6 +140,22 @@ public void setOpType(String opType) {
this.opType = opType;
}

public String getPublishType() {
return publishType;
}

public void setPublishType(String publishType) {
this.publishType = publishType;
}

public String getExtInfo() {
return extInfo;
}

public void setExtInfo(String extInfo) {
this.extInfo = extInfo;
}

public Timestamp getCreatedTime() {
return new Timestamp(createdTime.getTime());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class ConfigInfoStateWrapper implements Serializable {

private String md5;

private String grayName;

public long getId() {
return id;
}
Expand Down Expand Up @@ -78,6 +80,14 @@ public void setTenant(String tenant) {
this.tenant = tenant;
}

public String getGrayName() {
return grayName;
}

public void setGrayName(String grayName) {
this.grayName = grayName;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @date 2024-03-14 10:57
*/
public class ConfigGrayPersistInfo {

private String type;

private String version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Boolean publishConfig(ConfigForm configForm, ConfigRequestInfo configRequ
configInfo.setEncryptedDataKey(encryptedDataKey);
ConfigOperateResult configOperateResult;

//beta public
//beta publish
if (StringUtils.isNotBlank(configRequestInfo.getBetaIps())) {
configForm.setGrayName(BetaGrayRule.TYPE_BETA);
configForm.setGrayRuleExp(configRequestInfo.getBetaIps());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public void run() {

while (true) {
List<ConfigInfoStateWrapper> configDeleted = historyConfigInfoPersistService.findDeletedConfig(startTime,
deleteCursorId, pageSize);
deleteCursorId, pageSize, Constants.FORMAL);
for (ConfigInfoStateWrapper configInfo : configDeleted) {
if (configInfoPersistService.findConfigInfoState(configInfo.getDataId(), configInfo.getGroup(),
configInfo.getTenant()) == null) {
ConfigCacheService.remove(configInfo.getDataId(), configInfo.getGroup(),
configInfo.getTenant());
LogUtil.DEFAULT_LOG.info("[dump-delete-ok] {}",
new Object[] {GroupKey2.getKey(configInfo.getDataId(), configInfo.getGroup())});
LogUtil.DEFAULT_LOG.info("[dump-delete-ok], groupKey: {}, tenant: {}",
new Object[] {GroupKey2.getKey(configInfo.getDataId(), configInfo.getGroup())}, configInfo.getTenant());
}
}
if (configDeleted.size() < pageSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
package com.alibaba.nacos.config.server.service.dump;

import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.model.ConfigInfoGrayWrapper;
import com.alibaba.nacos.config.server.model.ConfigInfoStateWrapper;
import com.alibaba.nacos.config.server.service.ConfigCacheService;
import com.alibaba.nacos.config.server.service.repository.ConfigInfoGrayPersistService;
import com.alibaba.nacos.config.server.service.repository.HistoryConfigInfoPersistService;
import com.alibaba.nacos.config.server.utils.ConfigExecutor;
import com.alibaba.nacos.config.server.utils.GroupKey2;
import com.alibaba.nacos.config.server.utils.LogUtil;
Expand All @@ -41,24 +44,56 @@ public class DumpChangeGrayConfigWorker implements Runnable {

ConfigInfoGrayPersistService configInfoGrayPersistService;

private final HistoryConfigInfoPersistService historyConfigInfoPersistService;

int pageSize = 100;

public DumpChangeGrayConfigWorker(ConfigInfoGrayPersistService configInfoGrayPersistService, Timestamp startTime) {
public DumpChangeGrayConfigWorker(ConfigInfoGrayPersistService configInfoGrayPersistService, Timestamp startTime,
HistoryConfigInfoPersistService historyConfigInfoPersistService) {
this.configInfoGrayPersistService = configInfoGrayPersistService;
this.startTime = startTime;
this.historyConfigInfoPersistService = historyConfigInfoPersistService;
}

@Override
public void run() {
try {

if (!PropertyUtil.isDumpChangeOn()) {
LogUtil.DEFAULT_LOG.info("DumpGrayChange task is not open");
return;
}
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
LogUtil.DEFAULT_LOG.info("DumpGrayChange start ,from time {},current time {}", startTime, currentTime);

LogUtil.DEFAULT_LOG.info("Start to check delete configs from time {}", startTime);
long startDeletedConfigTime = System.currentTimeMillis();
long deleteCursorId = 0L;
while (true) {
List<ConfigInfoStateWrapper> configDeleted = historyConfigInfoPersistService.findDeletedConfig(startTime,
deleteCursorId, pageSize, Constants.GRAY);
for (ConfigInfoStateWrapper configInfo : configDeleted) {
String grayName = configInfo.getGrayName();
if (StringUtils.isBlank(grayName)) {
continue;
}

ConfigInfoStateWrapper configInfoStateWrapper = configInfoGrayPersistService.findConfigInfo4GrayState(configInfo.getDataId(),
configInfo.getGroup(), configInfo.getTenant(), grayName);
if (configInfoStateWrapper == null) {
ConfigCacheService.removeGray(configInfo.getDataId(), configInfo.getGroup(),
configInfo.getTenant(), grayName);
LogUtil.DEFAULT_LOG.info("[dump-gray-delete-ok], groupKey: {}, tenant: {}, grayName: {}",
GroupKey2.getKey(configInfo.getDataId(), configInfo.getGroup()), configInfo.getTenant(), grayName);
}
}
if (configDeleted.size() < pageSize) {
break;
}
deleteCursorId = configDeleted.get(configDeleted.size() - 1).getId();
}
LogUtil.DEFAULT_LOG.info("Check delete configs finished,cost:{}",
System.currentTimeMillis() - startDeletedConfigTime);

LogUtil.DEFAULT_LOG.info("Check changeGrayConfig start");
long startChangeConfigTime = System.currentTimeMillis();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ protected void dumpOperate() throws NacosException {
currentTime), random.nextInt((int) PropertyUtil.getDumpChangeWorkerInterval()),
TimeUnit.MILLISECONDS);
ConfigExecutor.scheduleConfigChangeTask(
new DumpChangeGrayConfigWorker(this.configInfoGrayPersistService, currentTime),
new DumpChangeGrayConfigWorker(this.configInfoGrayPersistService, currentTime, this.historyConfigInfoPersistService),
random.nextInt((int) PropertyUtil.getDumpChangeWorkerInterval()), TimeUnit.MILLISECONDS);
}

Expand Down
Loading

0 comments on commit 8af465a

Please sign in to comment.