Merge pull request #1 from Melodyl-xyz/hoxton

Hoxton
pull/1095/head
杨镌颖 2 years ago committed by GitHub
commit 61269754b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -117,7 +117,7 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL
try { try {
if (changedKey.startsWith("logging.level") && changedKey.length() >= 14) { if (changedKey.startsWith("logging.level") && changedKey.length() >= 14) {
String loggerName = changedKey.substring(14); String loggerName = changedKey.substring(14);
String newValue = (String) configPropertyChangeInfo.getNewValue(); String newValue = (String) configPropertyChangeInfo.getNewValue().toString();
LOGGER.info("[SCT Config] set logging.level loggerName:{}, newValue:{}", loggerName, newValue); LOGGER.info("[SCT Config] set logging.level loggerName:{}, newValue:{}", loggerName, newValue);
PolarisConfigLoggerContext.setLevel(loggerName, newValue); PolarisConfigLoggerContext.setLevel(loggerName, newValue);
} }

@ -27,6 +27,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.tencent.cloud.polaris.config.listener.ConfigChangeEvent; import com.tencent.cloud.polaris.config.listener.ConfigChangeEvent;
import com.tencent.cloud.polaris.config.listener.ConfigChangeListener; import com.tencent.cloud.polaris.config.listener.ConfigChangeListener;
import com.tencent.cloud.polaris.config.listener.SyncConfigChangeListener;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
@ -90,8 +91,19 @@ public class PolarisConfigAnnotationProcessor implements BeanPostProcessor, Prio
ReflectionUtils.makeAccessible(method); ReflectionUtils.makeAccessible(method);
String[] annotatedInterestedKeys = annotation.interestedKeys(); String[] annotatedInterestedKeys = annotation.interestedKeys();
String[] annotatedInterestedKeyPrefixes = annotation.interestedKeyPrefixes(); String[] annotatedInterestedKeyPrefixes = annotation.interestedKeyPrefixes();
boolean isAsync = annotation.async();
ConfigChangeListener configChangeListener = changeEvent -> ReflectionUtils.invokeMethod(method, bean, changeEvent); ConfigChangeListener configChangeListener = new SyncConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
ReflectionUtils.invokeMethod(method, bean, changeEvent);
}
@Override
public boolean isAsync() {
return isAsync;
}
};
Set<String> interestedKeys = Set<String> interestedKeys =
annotatedInterestedKeys.length > 0 ? Sets.newHashSet(annotatedInterestedKeys) : null; annotatedInterestedKeys.length > 0 ? Sets.newHashSet(annotatedInterestedKeys) : null;

@ -54,4 +54,6 @@ public @interface PolarisConfigKVFileChangeListener {
* @return interested key-prefixed in the listener * @return interested key-prefixed in the listener
*/ */
String[] interestedKeyPrefixes() default {}; String[] interestedKeyPrefixes() default {};
boolean async() default true;
} }

@ -72,11 +72,11 @@ public final class PolarisConfigListenerContext {
/** /**
* All custom interested keys defined in application . * All custom interested keys defined in application .
*/ */
private static final Map<ConfigChangeListener, Set<String>> interestedKeys = Maps.newHashMap(); private static final Map<ConfigChangeListener, Set<String>> interestedKeys = Maps.newConcurrentMap();
/** /**
* All custom interested key prefixes defined in application . * All custom interested key prefixes defined in application .
*/ */
private static final Map<ConfigChangeListener, Set<String>> interestedKeyPrefixes = Maps.newHashMap(); private static final Map<ConfigChangeListener, Set<String>> interestedKeyPrefixes = Maps.newConcurrentMap();
/** /**
* Cache all latest configuration information for users in the application environment . * Cache all latest configuration information for users in the application environment .
*/ */
@ -89,7 +89,7 @@ public final class PolarisConfigListenerContext {
* Get or Created new execute server . * Get or Created new execute server .
* @return execute service instance of {@link ExecutorService} * @return execute service instance of {@link ExecutorService}
*/ */
private static ExecutorService executor() { public static ExecutorService executor() {
if (EAR.get() == null) { if (EAR.get() == null) {
synchronized (PolarisConfigListenerContext.class) { synchronized (PolarisConfigListenerContext.class) {
int coreThreadSize = Runtime.getRuntime().availableProcessors(); int coreThreadSize = Runtime.getRuntime().availableProcessors();
@ -187,6 +187,15 @@ public final class PolarisConfigListenerContext {
Map<String, ConfigPropertyChangeInfo> modifiedChanges = new HashMap<>(interestedChangedKeys.size()); Map<String, ConfigPropertyChangeInfo> modifiedChanges = new HashMap<>(interestedChangedKeys.size());
interestedChangedKeys.parallelStream().forEach(key -> modifiedChanges.put(key, changes.get(key))); interestedChangedKeys.parallelStream().forEach(key -> modifiedChanges.put(key, changes.get(key)));
ConfigChangeEvent event = new ConfigChangeEvent(modifiedChanges, interestedChangedKeys); ConfigChangeEvent event = new ConfigChangeEvent(modifiedChanges, interestedChangedKeys);
if (listener instanceof SyncConfigChangeListener) {
SyncConfigChangeListener l = (SyncConfigChangeListener) listener;
if (!l.isAsync()) {
listener.onChange(event);
continue;
}
}
PolarisConfigListenerContext.executor().execute(() -> listener.onChange(event)); PolarisConfigListenerContext.executor().execute(() -> listener.onChange(event));
} }
} }

@ -0,0 +1,7 @@
package com.tencent.cloud.polaris.config.listener;
public interface SyncConfigChangeListener extends ConfigChangeListener {
default boolean isAsync() {
return true;
}
}

@ -18,14 +18,16 @@
package com.tencent.cloud.polaris.config.listener; package com.tencent.cloud.polaris.config.listener;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.google.common.collect.Sets;
import com.tencent.cloud.polaris.config.annotation.PolarisConfigKVFileChangeListener;
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -36,9 +38,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.google.common.collect.Sets; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
import com.tencent.cloud.polaris.config.annotation.PolarisConfigKVFileChangeListener;
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
/** /**
* Integration testing for change listener. * Integration testing for change listener.
@ -104,7 +104,7 @@ public class ConfigChangeListenerTest {
@PolarisConfigKVFileChangeListener(interestedKeys = {"timeout"}) @PolarisConfigKVFileChangeListener(interestedKeys = {"timeout"})
public void configChangedListener(ConfigChangeEvent event) { public void configChangedListener(ConfigChangeEvent event) {
ConfigPropertyChangeInfo changeInfo = event.getChange("timeout"); ConfigPropertyChangeInfo changeInfo = event.getChange("timeout");
timeout = Integer.parseInt((String)changeInfo.getNewValue()); timeout = Integer.parseInt(changeInfo.getNewValue().toString());
changeCnt++; changeCnt++;
hits.countDown(); hits.countDown();
} }
@ -112,7 +112,7 @@ public class ConfigChangeListenerTest {
@PolarisConfigKVFileChangeListener(interestedKeyPrefixes = {"timeout"}) @PolarisConfigKVFileChangeListener(interestedKeyPrefixes = {"timeout"})
public void configChangedListener2(ConfigChangeEvent event) { public void configChangedListener2(ConfigChangeEvent event) {
ConfigPropertyChangeInfo changeInfo = event.getChange("timeout"); ConfigPropertyChangeInfo changeInfo = event.getChange("timeout");
timeout = Integer.parseInt((String)changeInfo.getNewValue()); timeout = Integer.parseInt(changeInfo.getNewValue().toString());
changeCnt++; changeCnt++;
hits.countDown(); hits.countDown();
} }

@ -42,7 +42,25 @@ public final class PersonConfigChangeListener {
Set<String> changedKeys = event.changedKeys(); Set<String> changedKeys = event.changedKeys();
for (String changedKey : changedKeys) { for (String changedKey : changedKeys) {
System.out.printf("%s = %s \n", changedKey, event.getChange(changedKey)); System.out.printf("%s = %s , ThreadId: %s\n", changedKey, event.getChange(changedKey), Thread.currentThread().getId());
}
}
@PolarisConfigKVFileChangeListener(interestedKeyPrefixes = "teacher", async = false)
public void syncListen(ConfigChangeEvent event) {
Set<String> changedKeys = event.changedKeys();
for (String changedKey : changedKeys) {
System.out.printf("%s = %s , ThreadId: %s\n", changedKey, event.getChange(changedKey), Thread.currentThread().getId());
}
}
@PolarisConfigKVFileChangeListener(interestedKeyPrefixes = "teacher", async = false)
public void syncListen2(ConfigChangeEvent event) {
Set<String> changedKeys = event.changedKeys();
for (String changedKey : changedKeys) {
System.out.printf("%s = %s , ThreadId: %s\n", changedKey, event.getChange(changedKey), Thread.currentThread().getId());
} }
} }
} }

Loading…
Cancel
Save