提交 57aedb80 编写于 作者: K kezhenxu94 提交者: wu-sheng

Refactor ConfigWatcherRegister to only read interested configs. fixes #2834 (#2835)

上级 a763f8a8
...@@ -64,12 +64,12 @@ public abstract class ConfigWatcherRegister implements DynamicConfigurationServi ...@@ -64,12 +64,12 @@ public abstract class ConfigWatcherRegister implements DynamicConfigurationServi
logger.info("Current configurations after the bootstrap sync." + LINE_SEPARATOR + register.toString()); logger.info("Current configurations after the bootstrap sync." + LINE_SEPARATOR + register.toString());
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate( Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
new RunnableWithExceptionProtection(() -> configSync(), new RunnableWithExceptionProtection(this::configSync,
t -> logger.error("Sync config center error.", t)), syncPeriod, syncPeriod, TimeUnit.SECONDS); t -> logger.error("Sync config center error.", t)), syncPeriod, syncPeriod, TimeUnit.SECONDS);
} }
void configSync() { void configSync() {
ConfigTable configTable = readConfig(); ConfigTable configTable = readConfig(register.keys());
configTable.getItems().forEach(item -> { configTable.getItems().forEach(item -> {
String itemName = item.getName(); String itemName = item.getName();
...@@ -99,7 +99,7 @@ public abstract class ConfigWatcherRegister implements DynamicConfigurationServi ...@@ -99,7 +99,7 @@ public abstract class ConfigWatcherRegister implements DynamicConfigurationServi
logger.trace("Current configurations after the sync." + LINE_SEPARATOR + register.toString()); logger.trace("Current configurations after the sync." + LINE_SEPARATOR + register.toString());
} }
public abstract ConfigTable readConfig(); public abstract ConfigTable readConfig(Set<String> keys);
public class Register { public class Register {
private Map<String, WatcherHolder> register = new HashMap<>(); private Map<String, WatcherHolder> register = new HashMap<>();
...@@ -116,6 +116,10 @@ public abstract class ConfigWatcherRegister implements DynamicConfigurationServi ...@@ -116,6 +116,10 @@ public abstract class ConfigWatcherRegister implements DynamicConfigurationServi
return register.get(name); return register.get(name);
} }
public Set<String> keys() {
return register.keySet();
}
@Override public String toString() { @Override public String toString() {
StringBuilder registerTableDescription = new StringBuilder(); StringBuilder registerTableDescription = new StringBuilder();
registerTableDescription.append("Following dynamic config items are available.").append(LINE_SEPARATOR); registerTableDescription.append("Following dynamic config items are available.").append(LINE_SEPARATOR);
......
...@@ -22,6 +22,8 @@ import org.apache.skywalking.oap.server.library.module.*; ...@@ -22,6 +22,8 @@ import org.apache.skywalking.oap.server.library.module.*;
import org.junit.*; import org.junit.*;
import org.powermock.reflect.Whitebox; import org.powermock.reflect.Whitebox;
import java.util.Set;
/** /**
* @author wusheng * @author wusheng
*/ */
...@@ -80,7 +82,7 @@ public class ConfigWatcherRegisterTest { ...@@ -80,7 +82,7 @@ public class ConfigWatcherRegisterTest {
public static class MockConfigWatcherRegister extends ConfigWatcherRegister { public static class MockConfigWatcherRegister extends ConfigWatcherRegister {
@Override public ConfigTable readConfig() { @Override public ConfigTable readConfig(Set<String> keys) {
ConfigTable.ConfigItem item1 = new ConfigTable.ConfigItem("module.provider.prop1", "abc"); ConfigTable.ConfigItem item1 = new ConfigTable.ConfigItem("module.provider.prop1", "abc");
ConfigTable.ConfigItem item2 = new ConfigTable.ConfigItem("MockModule.provider.prop2", "abc2"); ConfigTable.ConfigItem item2 = new ConfigTable.ConfigItem("MockModule.provider.prop2", "abc2");
......
...@@ -23,6 +23,8 @@ import org.apache.skywalking.oap.server.configuration.api.*; ...@@ -23,6 +23,8 @@ import org.apache.skywalking.oap.server.configuration.api.*;
import org.apache.skywalking.oap.server.configuration.service.*; import org.apache.skywalking.oap.server.configuration.service.*;
import org.slf4j.*; import org.slf4j.*;
import java.util.Set;
/** /**
* @author wusheng * @author wusheng
*/ */
...@@ -38,12 +40,15 @@ public class GRPCConfigWatcherRegister extends ConfigWatcherRegister { ...@@ -38,12 +40,15 @@ public class GRPCConfigWatcherRegister extends ConfigWatcherRegister {
stub = ConfigurationServiceGrpc.newBlockingStub(NettyChannelBuilder.forAddress(settings.getHost(), settings.getPort()).usePlaintext().build()); stub = ConfigurationServiceGrpc.newBlockingStub(NettyChannelBuilder.forAddress(settings.getHost(), settings.getPort()).usePlaintext().build());
} }
@Override public ConfigTable readConfig() { @Override public ConfigTable readConfig(Set<String> keys) {
ConfigTable table = new ConfigTable(); ConfigTable table = new ConfigTable();
try { try {
ConfigurationResponse response = stub.call(ConfigurationRequest.newBuilder().setClusterName(settings.getClusterName()).build()); ConfigurationResponse response = stub.call(ConfigurationRequest.newBuilder().setClusterName(settings.getClusterName()).build());
response.getConfigTableList().forEach(config -> { response.getConfigTableList().forEach(config -> {
table.add(new ConfigTable.ConfigItem(config.getName(), config.getValue())); final String name = config.getName();
if (keys.contains(name)) {
table.add(new ConfigTable.ConfigItem(name, config.getValue()));
}
}); });
} catch (Exception e) { } catch (Exception e) {
logger.error("Remote config center [" + settings + "] is not available.", e); logger.error("Remote config center [" + settings + "] is not available.", e);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册