Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.0] Ensure generated config id is unique, checking existed config #8322

Merged
merged 3 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -646,15 +646,15 @@ private <C extends AbstractConfig> C addIfAbsent(C config, Map<String, C> config
case IGNORE: {
// ignore later config
if (logger.isWarnEnabled() && duplicatedConfigs.add(config)) {
logger.warn(msgPrefix + "keep previous config and ignore later config: " + config);
logger.warn(msgPrefix + "keep previous config and ignore later config");
}
return oldOne;
}
case OVERRIDE: {
// clear previous config, add new config
configsMap.clear();
if (logger.isWarnEnabled() && duplicatedConfigs.add(config)) {
logger.warn(msgPrefix + "override previous config with later config: " + config);
logger.warn(msgPrefix + "override previous config with later config");
}
break;
}
Expand All @@ -663,16 +663,22 @@ private <C extends AbstractConfig> C addIfAbsent(C config, Map<String, C> config

String key = getId(config);
if (key == null) {
// generate key for non-default config compatible with API usages
key = generateConfigId(config);
do {
// generate key if id is not set
key = generateConfigId(config);
} while (configsMap.containsKey(key));
}

C existedConfig = configsMap.putIfAbsent(key, config);
if (isEquals(existedConfig, config)) {
C existedConfig = configsMap.get(key);
if (existedConfig != null && !isEquals(existedConfig, config)) {
String type = config.getClass().getSimpleName();
throw new IllegalStateException(String.format("Duplicate %s found, there already has one default %s or more than two %ss have the same id, " +
"you can try to give each %s a different id, key: %s, prev: %s, new: %s", type, type, type, type, key, existedConfig, config));
logger.warn(String.format("Duplicate %s found, there already has one default %s or more than two %ss have the same id, " +
"you can try to give each %s a different id, override previous config with later config. id: %s, prev: %s, later: %s",
type, type, type, type, key, existedConfig, config));
}

// override existed config if any
configsMap.put(key, config);
return config;
}

Expand Down Expand Up @@ -717,6 +723,9 @@ private AbstractInterfaceConfig checkDuplicatedInterfaceConfig(AbstractInterface

if (prevConfig.equals(config)) {
// TODO Is there any problem with ignoring duplicate and equivalent but different ReferenceConfig instances?
if (logger.isWarnEnabled() && duplicatedConfigs.add(config)) {
logger.warn("Ignore duplicated and equal config: "+config);
}
return prevConfig;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -767,6 +768,7 @@ private void startMetadataCenter() {
for (MetadataReportConfig metadataReportConfig : metadataReportConfigs) {
ConfigValidationUtils.validateMetadataConfig(metadataReportConfig);
if (!metadataReportConfig.isValid()) {
logger.info("Ignore invalid metadata-report config: " + metadataReportConfig);
continue;
}
MetadataReportInstance.init(metadataReportConfig);
Expand Down Expand Up @@ -797,11 +799,14 @@ private void useRegistryAsConfigCenterIfNecessary() {
.stream()
.filter(this::isUsedRegistryAsConfigCenter)
.map(this::registryAsConfigCenter)
.forEach(configManager::addConfigCenter);
.forEach(configCenter -> {
if (configManager.getConfigCenter(configCenter.getId()).isPresent()) {
return;
}
configManager.addConfigCenter(configCenter);
logger.info("use registry as config-center: " + configCenter);

if (configManager.getConfigCenters().size() > 0) {
logger.info("use registry as config-center: " + configManager.getConfigCenters());
}
});
}
}

Expand All @@ -813,7 +818,8 @@ private boolean isUsedRegistryAsConfigCenter(RegistryConfig registryConfig) {
private ConfigCenterConfig registryAsConfigCenter(RegistryConfig registryConfig) {
String protocol = registryConfig.getProtocol();
Integer port = registryConfig.getPort();
String id = "config-center-" + protocol + "-" + port;
URL url = URL.valueOf(registryConfig.getAddress());
String id = "config-center-" + protocol + "-" + url.getHost() + "-" + port;
ConfigCenterConfig cc = new ConfigCenterConfig();
cc.setId(id);
if (cc.getParameters() == null) {
Expand Down Expand Up @@ -853,11 +859,14 @@ private void useRegistryAsMetadataCenterIfNecessary() {
.stream()
.filter(this::isUsedRegistryAsMetadataCenter)
.map(this::registryAsMetadataCenter)
.forEach(configManager::addMetadataReport);

if (configManager.getMetadataConfigs().size() > 0) {
logger.info("use registry as metadata-center: " + configManager.getMetadataConfigs());
}
.forEach(metadataReportConfig -> {
Optional<MetadataReportConfig> configOptional = configManager.getConfig(MetadataReportConfig.class, metadataReportConfig.getId());
if (configOptional.isPresent()) {
return;
}
configManager.addMetadataReport(metadataReportConfig);
logger.info("use registry as metadata-center: " + metadataReportConfig);
});
}
}

Expand Down Expand Up @@ -918,8 +927,8 @@ private boolean supportsExtension(Class<?> extensionClass, String name) {

private MetadataReportConfig registryAsMetadataCenter(RegistryConfig registryConfig) {
String protocol = registryConfig.getProtocol();
Integer port = registryConfig.getPort();
String id = "metadata-center-" + protocol + "-" + port;
URL url = URL.valueOf(registryConfig.getAddress());
String id = "metadata-center-" + protocol + "-" + url.getHost() + "-" + url.getPort();
MetadataReportConfig metadataReportConfig = new MetadataReportConfig();
metadataReportConfig.setId(id);
if (metadataReportConfig.getParameters() == null) {
Expand Down