Skip to content

Commit

Permalink
Fixed: server pulling old config in Nacos cluster mode (alibaba#3600)
Browse files Browse the repository at this point in the history
* Fix nacos config update in Nacos cluster

* add capacity limit for nacos config snapshots

* add capacity limit for nacos config snapshots

* optimized the nacos config snapshot
  • Loading branch information
ruansheng8 committed Mar 7, 2024
1 parent 1d04a64 commit bb64ed2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
import com.alibaba.cloud.nacos.parser.NacosDataParserHandler;
import com.alibaba.cloud.nacos.refresh.NacosSnapshotConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.slf4j.Logger;
Expand Down Expand Up @@ -82,7 +83,16 @@ private List<PropertySource<?>> loadNacosData(String dataId, String group,
String fileExtension) {
String data = null;
try {
data = configService.getConfig(dataId, group, timeout);
String configSnapshot = NacosSnapshotConfigManager.getAndRemoveConfigSnapshot(dataId, group);
if (StringUtils.isEmpty(configSnapshot)) {
log.debug("get config from nacos, dataId: {}, group: {}", dataId, group);
data = configService.getConfig(dataId, group, timeout);
}
else {
log.debug("get config from memory snapshot, dataId: {}, group: {}",
dataId, group);
data = configSnapshot;
}
if (StringUtils.isEmpty(data)) {
log.warn(
"Ignore the empty nacos configuration and get it based on dataId[{}] & group[{}]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public void innerReceive(String dataId, String group,
String configInfo) {
refreshCountIncrement();
nacosRefreshHistory.addRefreshRecord(dataId, group, configInfo);
NacosSnapshotConfigManager.putConfigSnapshot(dataId, group,
configInfo);
applicationContext.publishEvent(
new RefreshEvent(this, null, "Refresh Nacos config"));
if (log.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* 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
*
* https://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.cloud.nacos.refresh;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author: ruansheng
* @date: 2024-01-22
*/
public final class NacosSnapshotConfigManager {

private NacosSnapshotConfigManager() {
}

private static final Logger log = LoggerFactory
.getLogger(NacosSnapshotConfigManager.class);

private static final Map<String, String> CONFIG_INFO_SNAPSHOT_MAP = new ConcurrentHashMap<>(
8);

private static final int MAX_SNAPSHOT_COUNT = 100;

private static String formatConfigSnapshotKey(String dataId, String group) {
return dataId + "@" + group;
}

public static String getAndRemoveConfigSnapshot(String dataId, String group) {
String configInfo = CONFIG_INFO_SNAPSHOT_MAP
.get(formatConfigSnapshotKey(dataId, group));
removeConfigSnapshot(dataId, group);
return configInfo;
}

public static void putConfigSnapshot(String dataId, String group, String configInfo) {
try {
// Theoretically, the capacity limit restriction will never be triggered.
// This portion of the code serves as an additional fault tolerance layer.
if (CONFIG_INFO_SNAPSHOT_MAP.size() > MAX_SNAPSHOT_COUNT) {
Iterator<Map.Entry<String, String>> iterator = CONFIG_INFO_SNAPSHOT_MAP
.entrySet().iterator();
iterator.next();
iterator.remove();
}
CONFIG_INFO_SNAPSHOT_MAP.put(formatConfigSnapshotKey(dataId, group),
configInfo);
}
catch (Exception e) {
log.warn("remove nacos config snapshot error", e);
}
}

public static void removeConfigSnapshot(String dataId, String group) {
CONFIG_INFO_SNAPSHOT_MAP.remove(formatConfigSnapshotKey(dataId, group));
}

}

0 comments on commit bb64ed2

Please sign in to comment.