Skip to content

Commit

Permalink
Refactor ConfigCache and related pathways ,remove md5GBK to uniformly…
Browse files Browse the repository at this point in the history
… use UTF-8 encoding for md5 fields. Utilize the SPI mechanism in related pathways and methods to ensure extensibility for support of other types of encoding. Fix test.
  • Loading branch information
Sunrisea committed Nov 21, 2024
1 parent aeaaded commit 2a4da2a
Show file tree
Hide file tree
Showing 31 changed files with 1,133 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void testIgnoreExpiredEvent() throws InterruptedException {
defaultSharePublisher.addSubscriber(smartSubscriber2, MockSlowEvent2.class);
defaultSharePublisher.publish(mockSlowEvent1);
defaultSharePublisher.publish(mockSlowEvent2);
TimeUnit.MILLISECONDS.sleep(1100);
TimeUnit.MILLISECONDS.sleep(1500);
verify(smartSubscriber1).onEvent(mockSlowEvent1);
verify(smartSubscriber2).onEvent(mockSlowEvent2);
reset(smartSubscriber1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alibaba.nacos.config.server.filter.CircuitFilter;
import com.alibaba.nacos.config.server.filter.NacosWebFilter;
import com.alibaba.nacos.persistence.configuration.condition.ConditionDistributedEmbedStorage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
Expand All @@ -34,6 +35,7 @@
public class NacosConfigConfiguration {

@Bean
@ConditionalOnProperty(name = "nacos.web.charset.filter", havingValue = "nacos", matchIfMissing = true)
public FilterRegistrationBean<NacosWebFilter> nacosWebFilterRegistration() {
FilterRegistrationBean<NacosWebFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(nacosWebFilter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import java.util.List;
import java.util.Map;

import static com.alibaba.nacos.config.server.constant.Constants.ENCODE_UTF8;
import static com.alibaba.nacos.config.server.utils.LogUtil.PULL_LOG;

/**
Expand Down Expand Up @@ -135,8 +134,6 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon

boolean notify = StringUtils.isNotBlank(isNotify) && Boolean.parseBoolean(isNotify);

String acceptCharset = ENCODE_UTF8;

if (isV2) {
response.setHeader(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_JSON);
}
Expand Down Expand Up @@ -187,7 +184,7 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon
String encryptedDataKey;

if (matchedGray != null) {
md5 = matchedGray.getMd5(acceptCharset);
md5 = matchedGray.getMd5();
lastModified = matchedGray.getLastModifiedTs();
encryptedDataKey = matchedGray.getEncryptedDataKey();
content = ConfigDiskServiceFactory.getInstance()
Expand All @@ -211,7 +208,7 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon
response.setHeader(com.alibaba.nacos.api.common.Constants.VIPSERVER_TAG,
URLEncoder.encode(tag, StandardCharsets.UTF_8.displayName()));
} else {
md5 = cacheItem.getConfigCache().getMd5(acceptCharset);
md5 = cacheItem.getConfigCache().getMd5();
lastModified = cacheItem.getConfigCache().getLastModifiedTs();
encryptedDataKey = cacheItem.getConfigCache().getEncryptedDataKey();
content = ConfigDiskServiceFactory.getInstance().getContent(dataId, group, tenant);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class CacheItem {

public String type;

ConfigCache configCache = new ConfigCache();
ConfigCache configCache = ConfigCacheFactoryDelegate.getInstance().createConfigCache();

/**
* Use for gray.
Expand Down Expand Up @@ -92,7 +92,7 @@ public void initConfigGrayIfEmpty() {
public void initConfigGrayIfEmpty(String grayName) {
initConfigGrayIfEmpty();
if (!this.configCacheGray.containsKey(grayName)) {
this.configCacheGray.put(grayName, new ConfigCacheGray(grayName));
this.configCacheGray.put(grayName, ConfigCacheFactoryDelegate.getInstance().createConfigCacheGray(grayName));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@

import java.io.Serializable;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* config cache .
*
* @author shiyiyue1102
*/
public class ConfigCache implements Serializable {

volatile String md5Gbk = Constants.NULL;

volatile String md5Utf8 = Constants.NULL;
volatile String md5 = Constants.NULL;

volatile String encryptedDataKey;

Expand All @@ -42,21 +38,21 @@ public class ConfigCache implements Serializable {
* clear cache.
*/
public void clear() {
this.md5Gbk = Constants.NULL;
this.md5Utf8 = Constants.NULL;
this.md5 = Constants.NULL;
this.encryptedDataKey = null;
this.lastModifiedTs = -1L;
}

public ConfigCache() {
}

public String getMd5(String encode) {
if (UTF_8.name().equalsIgnoreCase(encode)) {
return md5Utf8;
} else {
return md5Gbk;
}
public ConfigCache(String md5, long lastModifiedTs) {
this.md5 = StringPool.get(md5);
this.lastModifiedTs = lastModifiedTs;
}

public String getMd5() {
return md5;
}

public String getEncryptedDataKey() {
Expand All @@ -67,26 +63,8 @@ public void setEncryptedDataKey(String encryptedDataKey) {
this.encryptedDataKey = encryptedDataKey;
}

public ConfigCache(String md5Gbk, String md5Utf8, long lastModifiedTs) {
this.md5Gbk = StringPool.get(md5Gbk);
this.md5Utf8 = StringPool.get(md5Utf8);
this.lastModifiedTs = lastModifiedTs;
}

public String getMd5Gbk() {
return md5Gbk;
}

public void setMd5Gbk(String md5Gbk) {
this.md5Gbk = StringPool.get(md5Gbk);
}

public String getMd5Utf8() {
return md5Utf8;
}

public void setMd5Utf8(String md5Utf8) {
this.md5Utf8 = StringPool.get(md5Utf8);
public void setMd5(String md5) {
this.md5 = StringPool.get(md5);
}

public long getLastModifiedTs() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 1999-2024 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;

/**
* The interface Config cache factory.
*
* @author Sunrisea
*/
public interface ConfigCacheFactory {

/**
* Create config cache config cache.
*
* @return the config cache
*/
public ConfigCache createConfigCache();

/**
* Create config cache config cache.
*
* @param md5 the md 5
* @param lastModifiedTs the last modified ts
* @return the config cache
*/
public ConfigCache createConfigCache(String md5, long lastModifiedTs);

/**
* Create config cache gray config cache gray.
*
* @param grayName the gray name
* @return the config cache gray
*/
public ConfigCacheGray createConfigCacheGray(String grayName);

/**
* Create config cache gray config cache gray.
*
* @param md5 the md 5
* @param lastModifiedTs the last modified ts
* @param grayRule the gray rule
* @return the config cache gray
*/
public ConfigCacheGray createConfigCacheGray(String md5, long lastModifiedTs, String grayRule);

/**
* Gets config cache factroy name.
*
* @return the config cache factory name
*/
public String getConfigCacheFactoryName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 1999-2024 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;

import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;

/**
* The type Config cache factory delegate.
*
* @author Sunrisea
*/
public class ConfigCacheFactoryDelegate {

private static final Logger LOGGER = LoggerFactory.getLogger(ConfigCacheFactoryDelegate.class);

private static final ConfigCacheFactoryDelegate INSTANCE = new ConfigCacheFactoryDelegate();

private String configCacheFactoryType = EnvUtil.getProperty("nacos.config.cache.type", "nacos");

private ConfigCacheFactory configCacheFactory = null;

private ConfigCacheFactoryDelegate() {
Collection<ConfigCacheFactory> configCacheFactories = NacosServiceLoader.load(ConfigCacheFactory.class);
for (ConfigCacheFactory each : configCacheFactories) {
if (StringUtils.isEmpty(each.getConfigCacheFactoryName())) {
LOGGER.warn("[ConfigCacheFactory] Load ConfigCacheFactory({}) ConfigFactroyName (null/empty) fail. "
+ "Please add ConfigFactoryName to resolve",
each.getClass());
continue;
}
LOGGER.info("[ConfigCacheFactory] Load ConfigCacheFactory({}) ConfigCacheFactoryName({}) successfully. ",
each.getClass(), each.getConfigCacheFactoryName());
if (StringUtils.equals(configCacheFactoryType, each.getConfigCacheFactoryName())) {
this.configCacheFactory = each;
}
}
if (this.configCacheFactory == null) {
this.configCacheFactory = new NacosConfigCacheFactory();
}
}

public static ConfigCacheFactoryDelegate getInstance() {
return INSTANCE;
}

public ConfigCache createConfigCache() {
return configCacheFactory.createConfigCache();
}

public ConfigCache createConfigCache(String md5, long lastModifiedTs) {
return configCacheFactory.createConfigCache(md5, lastModifiedTs);
}

public ConfigCacheGray createConfigCacheGray(String grayName) {
return configCacheFactory.createConfigCacheGray(grayName);
}

public ConfigCacheGray createConfigCacheGray(String md5, long lastModifiedTs, String grayRule) {
return configCacheFactory.createConfigCacheGray(md5, lastModifiedTs, grayRule);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public GrayRule getGrayRule() {
return grayRule;
}

public ConfigCacheGray(String md5Gbk, String md5Utf8, long lastModifiedTs, String grayRule)
public ConfigCacheGray(String md5, long lastModifiedTs, String grayRule)
throws RuntimeException {
super(md5Gbk, md5Utf8, lastModifiedTs);
super(md5, lastModifiedTs);
this.grayRule = GrayRuleManager.constructGrayRule(GrayRuleManager.deserializeConfigGrayPersistInfo(grayRule));
if (this.grayRule == null || !this.grayRule.isValid()) {
throw new RuntimeException("raw gray rule is invalid");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 1999-2024 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;

/**
* The interface Config cache md5 post processor.
*
* @author Sunrisea
*/
public interface ConfigCacheMd5PostProcessor {

/**
* Gets post processor name.
*
* @return the post processor name
*/
public String getPostProcessorName();

/**
* Post process.
*
* @param configCache the config cache
* @param content the content
*/
public void postProcess(ConfigCache configCache, String content);
}
Loading

0 comments on commit 2a4da2a

Please sign in to comment.