Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shedfreez committed Nov 20, 2024
1 parent 24d7b1c commit 1ea0595
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ private void initDataSource(ConfigurationImpl configuration) {
throw new RuntimeException("Config server address is blank. Please check your config in bootstrap.yml"
+ " with spring.cloud.polaris.address or spring.cloud.polaris.config.address");
}

checkAddressAccessible(configAddresses);
if (polarisConfigProperties.isCheckAddress()) {
checkAddressAccessible(configAddresses);
}

configuration.getConfigFile().getServerConnector().setAddresses(configAddresses);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
// this class provides customized logic for some customers to configure special business group files
private final PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer = PolarisServiceLoaderUtil.getPolarisConfigCustomExtensionLayer();

private volatile CompositePropertySource compositePropertySourceCache = null;
private volatile static CompositePropertySource compositePropertySourceCache = null;

public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties,
PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, Environment environment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
Expand All @@ -30,6 +31,7 @@
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.logger.PolarisConfigLoggerContext;
import com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils;
import com.tencent.polaris.configuration.api.core.ConfigFileGroup;
import com.tencent.polaris.configuration.api.core.ConfigFileMetadata;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
Expand All @@ -41,6 +43,7 @@

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.PropertySource;
import org.springframework.lang.NonNull;
import org.springframework.util.CollectionUtils;

Expand Down Expand Up @@ -122,44 +125,49 @@ private void customInitRegisterPolarisConfig(PolarisConfigPropertyAutoRefresher
}

private void registerPolarisConfigGroupChangeListener(PolarisPropertySource polarisPropertySource) {
configFileService.getConfigFileGroup(polarisPropertySource.getNamespace(), polarisPropertySource.getGroup()).
addChangeListener(event -> {
try {
LOGGER.debug("ConfigFileGroup receive onChange event:{}", event);
List<ConfigFileMetadata> oldConfigFileMetadataList = event.getOldConfigFileMetadataList();
List<ConfigFileMetadata> newConfigFileMetadataList = event.getNewConfigFileMetadataList();

Map<String, ConfigFileMetadata> added = calculateUnregister(oldConfigFileMetadataList, newConfigFileMetadataList);
if (added.isEmpty()) {
return;
}
Set<String> changedKeys = new HashSet<>();
ConfigFileGroup configFileGroup = configFileService.getConfigFileGroup(
polarisPropertySource.getNamespace(), polarisPropertySource.getGroup());

for (Map.Entry<String, ConfigFileMetadata> entry : added.entrySet()) {
if (registeredPolarisPropertySets.contains(entry.getKey())) {
continue;
}
registeredPolarisPropertySets.add(entry.getKey());
LOGGER.info("[SCT Config] add polaris config file:{}", entry.getKey());
ConfigFileMetadata configFileMetadata = entry.getValue();
PolarisPropertySource p = PolarisConfigFileLocator.loadPolarisPropertySource(
configFileService, configFileMetadata.getNamespace(),
configFileMetadata.getFileGroup(), configFileMetadata.getFileName());
LOGGER.info("[SCT Config] changed property = {}", p.getSource().keySet());
changedKeys.addAll(p.getSource().keySet());
this.registerPolarisConfigPublishChangeListener(p, polarisPropertySource);
PolarisPropertySourceManager.addPropertySource(p);
for (String changedKey : p.getSource().keySet()) {
polarisPropertySource.getSource().put(changedKey, p.getSource().get(changedKey));
refreshSpringValue(changedKey);
}
}
refreshConfigurationProperties(changedKeys);
if (configFileGroup == null) {
return;
}
configFileGroup.addChangeListener(event -> {
try {
LOGGER.debug("ConfigFileGroup receive onChange event:{}", event);
List<ConfigFileMetadata> oldConfigFileMetadataList = event.getOldConfigFileMetadataList();
List<ConfigFileMetadata> newConfigFileMetadataList = event.getNewConfigFileMetadataList();

Map<String, ConfigFileMetadata> added = calculateUnregister(oldConfigFileMetadataList, newConfigFileMetadataList);
if (added.isEmpty()) {
return;
}
Set<String> changedKeys = new HashSet<>();

for (Map.Entry<String, ConfigFileMetadata> entry : added.entrySet()) {
if (registeredPolarisPropertySets.contains(entry.getKey())) {
continue;
}
catch (Exception e) {
LOGGER.error("[SCT Config] receive onChange exception,", e);
registeredPolarisPropertySets.add(entry.getKey());
LOGGER.info("[SCT Config] add polaris config file:{}", entry.getKey());
ConfigFileMetadata configFileMetadata = entry.getValue();
PolarisPropertySource p = PolarisConfigFileLocator.loadPolarisPropertySource(
configFileService, configFileMetadata.getNamespace(),
configFileMetadata.getFileGroup(), configFileMetadata.getFileName());
LOGGER.info("[SCT Config] changed property = {}", p.getSource().keySet());
changedKeys.addAll(p.getSource().keySet());
this.registerPolarisConfigPublishChangeListener(p, polarisPropertySource);
PolarisPropertySourceManager.addPropertySource(p);
for (String changedKey : p.getSource().keySet()) {
polarisPropertySource.getSource().put(changedKey, p.getSource().get(changedKey));
refreshSpringValue(changedKey);
}
});
}
refreshConfigurationProperties(changedKeys);
}
catch (Exception e) {
LOGGER.error("[SCT Config] receive onChange exception,", e);
}
});
}

public void registerPolarisConfigPublishChangeListener(PolarisPropertySource polarisPropertySource) {
Expand Down Expand Up @@ -212,7 +220,9 @@ public void registerPolarisConfigPublishChangeListener(PolarisPropertySource lis
break;
case DELETED:
if (isGroupRefresh) {
Object newValue = newGroupSource.getSource().get(changedKey);
// when the key is deleted, the value should load from group source
Object newValue = Optional.ofNullable(newGroupSource).map(PropertySource::getSource).
map(source -> source.get(changedKey)).orElse(null);
if (newValue != null) {
effectSource.put(changedKey, newValue);
}
Expand Down Expand Up @@ -258,7 +268,6 @@ private Map<String, ConfigFileMetadata> calculateUnregister(List<ConfigFileMetad
configFileMetadata.getFileName()),
configFileMetadata -> configFileMetadata));

// 将新列表转换为一个 Map
Map<String, ConfigFileMetadata> newConfigFileMetadataMap = newConfigFileMetadataList.stream()
.collect(Collectors.toMap(
configFileMetadata -> PolarisPropertySourceUtils.generateName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public class PolarisConfigProperties {
*/
private boolean internalEnabled = true;

private boolean checkAddress = true;

public boolean isEnabled() {
return enabled;
}
Expand Down Expand Up @@ -192,6 +194,14 @@ public void setInternalEnabled(boolean internalEnabled) {
this.internalEnabled = internalEnabled;
}

public boolean isCheckAddress() {
return checkAddress;
}

public void setCheckAddress(boolean checkAddress) {
this.checkAddress = checkAddress;
}

@Override
public String toString() {
return "PolarisConfigProperties{" +
Expand All @@ -207,6 +217,7 @@ public String toString() {
", dataSource='" + dataSource + '\'' +
", localFileRootPath='" + localFileRootPath + '\'' +
", internalEnabled=" + internalEnabled +
", checkAddress=" + checkAddress +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.tencent.cloud.polaris.config.adapter;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -68,6 +69,7 @@ public void setUp() {

@Test
public void testLoadApplicationPropertiesFile() {
clearCompositePropertySourceCache();
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, environment);

Expand Down Expand Up @@ -105,6 +107,7 @@ public void testLoadApplicationPropertiesFile() {

@Test
public void testActiveProfileFilesPriorityBiggerThanDefault() {
clearCompositePropertySourceCache();
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, environment);

Expand Down Expand Up @@ -154,6 +157,7 @@ public void testActiveProfileFilesPriorityBiggerThanDefault() {

@Test
public void testGetCustomFiles() {
clearCompositePropertySourceCache();
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, environment);

Expand Down Expand Up @@ -208,6 +212,7 @@ public void testGetCustomFiles() {

@Test
public void testGetCustomGroupFiles() {
clearCompositePropertySourceCache();
PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties,
configFileService, environment);

Expand Down Expand Up @@ -260,4 +265,16 @@ public void testGetCustomGroupFiles() {
assertThat(propertySource.getProperty("k2")).isEqualTo("v2");
assertThat(propertySource.getProperty("k3")).isEqualTo("v3");
}

private void clearCompositePropertySourceCache() {
try {
Class<?> clazz = PolarisConfigFileLocator.class;
Field field = clazz.getDeclaredField("compositePropertySourceCache");
field.setAccessible(true);
field.set(null, null);
}
catch (Exception e) {
// ignore
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

package com.tencent.cloud.polaris.config.listener;

import java.lang.reflect.Field;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import com.google.common.collect.Sets;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator;
import com.tencent.cloud.polaris.config.annotation.PolarisConfigKVFileChangeListener;
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -35,6 +38,7 @@
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit.jupiter.SpringExtension;

Expand All @@ -47,7 +51,9 @@
*/
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = ConfigChangeListenerTest.TestApplication.class,
properties = {"server.port=48081", "spring.config.location = classpath:application-test.yml"})
properties = {"server.port=48081", "spring.config.location = classpath:application-test.yml",
"spring.cloud.polaris.config.connect-remote-server=true", "spring.cloud.polaris.config.check-address=false"
})
public class ConfigChangeListenerTest {

private static final CountDownLatch hits = new CountDownLatch(2);
Expand All @@ -58,6 +64,19 @@ public class ConfigChangeListenerTest {
@Autowired
private TestApplication.TestConfig testConfig;

@BeforeAll
public static void setUp() {
try {
Class<?> clazz = PolarisConfigFileLocator.class;
Field field = clazz.getDeclaredField("compositePropertySourceCache");
field.setAccessible(true);
field.set(null, new CompositePropertySource("mock"));
}
catch (Exception e) {
// ignore
}
}

@Test
public void test() throws InterruptedException {
//before change
Expand Down
Loading

0 comments on commit 1ea0595

Please sign in to comment.