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

[#4004] fixed firstPull address choose logic #4034

Merged
merged 1 commit into from
Nov 14, 2023
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 @@ -58,14 +58,13 @@ public ConfigCenterClient(ConfigCenterAddressManager addressManager, HttpTranspo
}

@Override
public QueryConfigurationsResponse queryConfigurations(QueryConfigurationsRequest request) {
public QueryConfigurationsResponse queryConfigurations(QueryConfigurationsRequest request, String address) {
String dimensionsInfo = buildDimensionsInfo(request, true);
QueryConfigurationsResponse queryConfigurationsResponse = new QueryConfigurationsResponse();

Map<String, Object> configurations = new HashMap<>();

String uri = null;
String address = addressManager.address();
try {
uri = address + "/configuration/items?dimensionsInfo="
+ HttpUtils.encodeURLParam(dimensionsInfo) + "&revision=" + request.getRevision();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ public class ConfigCenterManager extends AbstractTask {

private final ConfigCenterConfiguration configCenterConfiguration;

public ConfigCenterManager(ConfigCenterClient configCenterClient, EventBus eventBus,
ConfigConverter configConverter, ConfigCenterConfiguration configCenterConfiguration) {
private final ConfigCenterAddressManager configCenterAddressManager;

public ConfigCenterManager(ConfigCenterClient configCenterClient, EventBus eventBus, ConfigConverter configConverter,
ConfigCenterConfiguration configCenterConfiguration, ConfigCenterAddressManager configCenterAddressManager) {
super("config-center-configuration-task");
this.configCenterClient = configCenterClient;
this.eventBus = eventBus;
this.configConverter = configConverter;
this.configCenterConfiguration = configCenterConfiguration;
this.configCenterAddressManager = configCenterAddressManager;
}

public void setQueryConfigurationsRequest(QueryConfigurationsRequest queryConfigurationsRequest) {
Expand All @@ -72,7 +75,8 @@ public PollConfigurationTask(int failCount) {
@Override
public void execute() {
try {
QueryConfigurationsResponse response = configCenterClient.queryConfigurations(queryConfigurationsRequest);
QueryConfigurationsResponse response = configCenterClient.queryConfigurations(queryConfigurationsRequest,
configCenterAddressManager.address());
if (response.isChanged()) {
queryConfigurationsRequest.setRevision(response.getRevision());
Map<String, Object> lastData = configConverter.updateData(response.getConfigurations());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public interface ConfigCenterOperation {
/**
* 根据查询条件查询配置项。
* @param request 查询的维度(project, application, serviceName, version) 和 revision 信息。
* @param address 查询的配置中心地址。
* @return 如果存在配置变更,返回全量的配置项, changed = true。 如果没有变更, 返回 null, changed = false,
* @throws OperationException If some problems happened to contact service center or non http 200 returned.
*/
QueryConfigurationsResponse queryConfigurations(QueryConfigurationsRequest request);
QueryConfigurationsResponse queryConfigurations(QueryConfigurationsRequest request, String address);
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public KieClient(KieAddressManager addressManager, HttpTransport httpTransport,
}

@Override
public ConfigurationsResponse queryConfigurations(ConfigurationsRequest request) {
String address = addressManager.address();
public ConfigurationsResponse queryConfigurations(ConfigurationsRequest request, String address) {
String url = buildUrl(request, address);
try {
if (kieConfiguration.isEnableLongPolling()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.servicecomb.config.kie.client.model.ConfigurationsRequest;
import org.apache.servicecomb.config.kie.client.model.ConfigurationsRequestFactory;
import org.apache.servicecomb.config.kie.client.model.ConfigurationsResponse;
import org.apache.servicecomb.config.kie.client.model.KieAddressManager;
import org.apache.servicecomb.config.kie.client.model.KieConfiguration;
import org.apache.servicecomb.http.client.task.AbstractTask;
import org.apache.servicecomb.http.client.task.Task;
Expand All @@ -51,40 +52,58 @@ public class KieConfigManager extends AbstractTask {

private final KieConfiguration kieConfiguration;

private final KieAddressManager kieAddressManager;


public KieConfigManager(KieConfigOperation configKieClient, EventBus eventBus,
KieConfiguration kieConfiguration,
ConfigConverter configConverter) {
KieConfiguration kieConfiguration, ConfigConverter configConverter, KieAddressManager kieAddressManager) {
super("config-center-configuration-task");
this.configurationsRequests = ConfigurationsRequestFactory.buildConfigurationRequests(kieConfiguration);
this.configurationsRequests.sort(ConfigurationsRequest::compareTo);
this.configKieClient = configKieClient;
this.eventBus = eventBus;
this.configConverter = configConverter;
this.kieConfiguration = kieConfiguration;
this.kieAddressManager = kieAddressManager;
}

public void firstPull() {
Map<String, Object> data = new HashMap<>();
try {
Map<String, Object> data = new HashMap<>();
this.configurationsRequests.forEach(r -> {
r.setRevision(ConfigurationsRequest.INITIAL_REVISION);
ConfigurationsResponse response = configKieClient.queryConfigurations(r);
if (response.isChanged()) {
r.setRevision(response.getRevision());
r.setLastRawData(response.getConfigurations());
data.putAll(response.getConfigurations());
}
});
this.configConverter.updateData(data);
} catch (RuntimeException e) {
firstQueryConfigurations(data);
} catch (Exception e) {
if (this.kieConfiguration.isFirstPullRequired()) {
throw e;
} else {
LOGGER.warn("first pull failed, and ignore {}", e.getMessage());
LOGGER.warn("first pull failed!");
}
}
}

private void firstQueryConfigurations(Map<String, Object> data) {
for (int i = 0; i < 3;) {
String address = kieAddressManager.address();
try {
this.configurationsRequests.forEach(r -> {
r.setRevision(ConfigurationsRequest.INITIAL_REVISION);
ConfigurationsResponse response = configKieClient.queryConfigurations(r, address);
if (response.isChanged()) {
r.setRevision(response.getRevision());
r.setLastRawData(response.getConfigurations());
data.putAll(response.getConfigurations());
}
});
this.configConverter.updateData(data);
break;
} catch (Exception e) {
if (i == 2) {
throw e;
}
LOGGER.warn("firstQueryConfigurations failed, config address {} and ignore {}", address, e.getMessage());
}
i++;
}
}
private void onDataChanged() {
Map<String, Object> latestData = new HashMap<>();
this.configurationsRequests.forEach(r -> latestData.putAll(r.getLastRawData()));
Expand Down Expand Up @@ -121,7 +140,8 @@ public PollConfigurationTask(int failCount, ConfigurationsRequest configurations
@Override
public void execute() {
try {
ConfigurationsResponse response = configKieClient.queryConfigurations(configurationsRequest);
ConfigurationsResponse response = configKieClient.queryConfigurations(configurationsRequest,
kieAddressManager.address());
if (response.isChanged()) {
configurationsRequest.setRevision(response.getRevision());
configurationsRequest.setLastRawData(response.getConfigurations());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public interface KieConfigOperation {
/**
* 根据查询条件查询配置项。
* @param request 查询的维度(project, application, serviceName, version) 和 revision 信息。
* @param address 查询的配置中心地址。
* @return 如果存在配置变更,返回全量的配置项, changed = true。 如果没有变更, 返回 null, changed = false,
* @throws OperationException If some problems happened to contact service center or non http 200 returned.
*/
ConfigurationsResponse queryConfigurations(ConfigurationsRequest request);
ConfigurationsResponse queryConfigurations(ConfigurationsRequest request, String address);
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,30 @@ public boolean isValidSource(Configuration localConfiguration) {
public void init(Configuration localConfiguration) {
configConverter = new ConfigConverter(ConfigCenterConfig.INSTANCE.getFileSources());

ConfigCenterAddressManager kieAddressManager = configKieAddressManager();
ConfigCenterAddressManager configCenterAddressManager = configCenterAddressManager();

HttpTransport httpTransport = createHttpTransport(kieAddressManager,
HttpTransport httpTransport = createHttpTransport(configCenterAddressManager,
HttpTransportFactory.defaultRequestConfig().build(),
localConfiguration);
ConfigCenterClient configCenterClient = new ConfigCenterClient(kieAddressManager, httpTransport);
ConfigCenterClient configCenterClient = new ConfigCenterClient(configCenterAddressManager, httpTransport);
EventManager.register(this);

ConfigCenterConfiguration configCenterConfiguration = createConfigCenterConfiguration();

QueryConfigurationsRequest queryConfigurationsRequest = firstPull(configCenterClient);
QueryConfigurationsRequest queryConfigurationsRequest = firstPull(configCenterClient, configCenterAddressManager);

configCenterManager = new ConfigCenterManager(configCenterClient, EventManager.getEventBus(),
configConverter, configCenterConfiguration);
configConverter, configCenterConfiguration, configCenterAddressManager);
configCenterManager.setQueryConfigurationsRequest(queryConfigurationsRequest);
configCenterManager.startConfigCenterManager();
}

private QueryConfigurationsRequest firstPull(ConfigCenterClient configCenterClient) {
private QueryConfigurationsRequest firstPull(ConfigCenterClient configCenterClient,
ConfigCenterAddressManager configCenterAddressManager) {
QueryConfigurationsRequest queryConfigurationsRequest = createQueryConfigurationsRequest();
try {
QueryConfigurationsResponse response = configCenterClient
.queryConfigurations(queryConfigurationsRequest);
.queryConfigurations(queryConfigurationsRequest, configCenterAddressManager.address());
if (response.isChanged()) {
configConverter.updateData(response.getConfigurations());
updateConfiguration(WatchedUpdateResult.createIncremental(configConverter.getCurrentData(), null, null));
Expand Down Expand Up @@ -146,8 +147,8 @@ private ConfigCenterConfiguration createConfigCenterConfiguration(){
return new ConfigCenterConfiguration().setRefreshIntervalInMillis(ConfigCenterConfig.INSTANCE.getRefreshInterval());
}

private HttpTransport createHttpTransport(ConfigCenterAddressManager kieAddressManager, RequestConfig requestConfig,
Configuration localConfiguration) {
private HttpTransport createHttpTransport(ConfigCenterAddressManager configCenterAddressManager,
RequestConfig requestConfig, Configuration localConfiguration) {
List<AuthHeaderProvider> authHeaderProviders = SPIServiceUtils.getOrLoadSortedService(AuthHeaderProvider.class);

if (ConfigCenterConfig.INSTANCE.isProxyEnable()) {
Expand All @@ -165,14 +166,16 @@ private HttpTransport createHttpTransport(ConfigCenterAddressManager kieAddressM
return HttpTransportFactory
.createHttpTransport(
TransportUtils
.createSSLProperties(kieAddressManager.sslEnabled(), localConfiguration, ConfigCenterConfig.SSL_TAG),
.createSSLProperties(configCenterAddressManager.sslEnabled(), localConfiguration,
ConfigCenterConfig.SSL_TAG),
getRequestAuthHeaderProvider(authHeaderProviders), httpClientBuilder);
}

return HttpTransportFactory
.createHttpTransport(
TransportUtils
.createSSLProperties(kieAddressManager.sslEnabled(), localConfiguration, ConfigCenterConfig.SSL_TAG),
.createSSLProperties(configCenterAddressManager.sslEnabled(), localConfiguration,
ConfigCenterConfig.SSL_TAG),
getRequestAuthHeaderProvider(authHeaderProviders), requestConfig);
}

Expand All @@ -184,7 +187,7 @@ private static RequestAuthHeaderProvider getRequestAuthHeaderProvider(List<AuthH
};
}

private ConfigCenterAddressManager configKieAddressManager() {
private ConfigCenterAddressManager configCenterAddressManager() {
return new ConfigCenterAddressManager(ConfigCenterConfig.INSTANCE.getDomainName(),
Deployment
.getSystemBootStrapInfo(ConfigCenterDefaultDeploymentProvider.SYSTEM_KEY_CONFIG_CENTER).getAccessURL(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public void init(Configuration localConfiguration) {
KieConfiguration kieConfiguration = createKieConfiguration();
KieClient kieClient = new KieClient(kieAddressManager, httpTransport, kieConfiguration);
EventManager.register(this);
kieConfigManager = new KieConfigManager(kieClient, EventManager.getEventBus(), kieConfiguration, configConverter);
kieConfigManager = new KieConfigManager(kieClient, EventManager.getEventBus(), kieConfiguration, configConverter,
kieAddressManager);
kieConfigManager.firstPull();
kieConfigManager.startConfigKieManager();
updateConfiguration(WatchedUpdateResult.createIncremental(configConverter.getCurrentData(), null, null));
Expand Down
Loading