Skip to content

Commit

Permalink
[#12189] Do some enhance and refactor for new server list manager. (#…
Browse files Browse the repository at this point in the history
…12746)

* Refactor server list manager and server list provider.
And Add unit test for these classes.

* Little refactor and enhance for ConfigServerListManager and NamingServerListManager.
Add unit test for these.
  • Loading branch information
KomachiSion authored Oct 16, 2024
1 parent 85f27b2 commit 85a1e38
Show file tree
Hide file tree
Showing 17 changed files with 1,025 additions and 116 deletions.
16 changes: 14 additions & 2 deletions api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class PropertyKeyConst {

public static final String ENDPOINT_CLUSTER_NAME = "endpointClusterName";

public static final String ENDPOINT_REFRESH_INTERVAL_SECONDS = "endpointRefreshIntervalSeconds";

@Deprecated
public static final String SERVER_NAME = "serverName";

public static final String NAMESPACE = "namespace";
Expand All @@ -55,8 +58,19 @@ public class PropertyKeyConst {

public static final String CONTEXT_PATH = "contextPath";

/**
* Please use {@link #ENDPOINT_CLUSTER_NAME} replaced.
*/
@Deprecated
public static final String CLUSTER_NAME = "clusterName";

/**
* Default is {@code "false"}, if true, and without {@link #ENDPOINT_CLUSTER_NAME}, use {@link #CLUSTER_NAME} to set
* endpoint cluster name.
*/
@Deprecated
public static final String IS_ADAPT_CLUSTER_NAME_USAGE = "isAdaptClusterNameUsage";

public static final String ENCODE = "encode";

public static final String CONFIG_LONG_POLL_TIMEOUT = "configLongPollTimeout";
Expand Down Expand Up @@ -97,8 +111,6 @@ public class PropertyKeyConst {

public static final String LOG_ALL_PROPERTIES = "logAllProperties";

public static final String CLIENT_MODULE_TYPE = "clientModuleType";

/**
* Since 2.3.3, For some situation like java agent using nacos-client which can't use env ram info.
*/
Expand Down
2 changes: 2 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ public class Constants {
*/
public static final String DEFAULT_USE_RAM_INFO_PARSING = "true";

public static final String CLIENT_MODULE_TYPE = "clientModuleType";

/**
* The constants in config directory.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package com.alibaba.nacos.client.address;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.common.JustForTest;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.remote.client.ServerListFactory;
Expand All @@ -44,22 +46,18 @@ public abstract class AbstractServerListManager implements ServerListFactory, Cl

protected NacosClientProperties properties;

public AbstractServerListManager(NacosClientProperties properties) throws NacosException {
public AbstractServerListManager(NacosClientProperties properties) {
this(properties, null);
}

public AbstractServerListManager(NacosClientProperties properties, String namespace) throws NacosException {
if (null == properties) {
LOGGER.error("properties is null");
return;
}
public AbstractServerListManager(NacosClientProperties properties, String namespace) {
// To avoid set operation affect the original properties.
NacosClientProperties tmpProperties = properties.derive();
if (StringUtils.isNotBlank(namespace)) {
// To avoid set operation affect the original properties.
properties = properties.derive();
properties.setProperty(PropertyKeyConst.NAMESPACE, namespace);
tmpProperties.setProperty(PropertyKeyConst.NAMESPACE, namespace);
}
properties.setProperty(PropertyKeyConst.CLIENT_MODULE_TYPE, getModuleName());
this.properties = properties;
tmpProperties.setProperty(Constants.CLIENT_MODULE_TYPE, getModuleName());
this.properties = tmpProperties;
}

@Override
Expand Down Expand Up @@ -98,16 +96,12 @@ public void start() throws NacosException {
}
}
if (null == serverListProvider) {
LOGGER.error("no server list provider found");
return;
LOGGER.error("No server list provider found, SPI load size: {}", sorted.size());
throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "No server list provider found.");
}
this.serverListProvider.init(properties, getNacosRestTemplate());
}

public NacosClientProperties getProperties() {
return properties;
}

public String getServerName() {
return getModuleName() + "-" + serverListProvider.getServerName();
}
Expand All @@ -133,12 +127,17 @@ public boolean isFixed() {
*
* @return module name
*/
public abstract String getModuleName();
protected abstract String getModuleName();

/**
* get nacos rest template.
*
* @return nacos rest template
*/
public abstract NacosRestTemplate getNacosRestTemplate();
protected abstract NacosRestTemplate getNacosRestTemplate();

@JustForTest
NacosClientProperties getProperties() {
return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.constant.Constants.Address;
import com.alibaba.nacos.client.env.NacosClientProperties;
Expand Down Expand Up @@ -46,8 +47,8 @@

/**
* Endpoint server list provider.
*
* @author totalo
*
* @author totalo
*/
public class EndpointServerListProvider extends AbstractServerListProvider {

Expand Down Expand Up @@ -80,7 +81,8 @@ public class EndpointServerListProvider extends AbstractServerListProvider {
private String moduleName = "default";

@Override
public void init(final NacosClientProperties properties, final NacosRestTemplate nacosRestTemplate) throws NacosException {
public void init(final NacosClientProperties properties, final NacosRestTemplate nacosRestTemplate)
throws NacosException {
super.init(properties, nacosRestTemplate);
this.nacosRestTemplate = nacosRestTemplate;
initEndpoint(properties);
Expand All @@ -89,7 +91,7 @@ public void init(final NacosClientProperties properties, final NacosRestTemplate
initServerListName(properties);
initAddressServerUrl(properties);
initModuleName(properties);
startRefreshServerListTask();
startRefreshServerListTask(properties);
}

@Override
Expand All @@ -102,8 +104,7 @@ public String getServerName() {
String contextPathTmp =
StringUtils.isNotBlank(this.endpointContextPath) ? this.endpointContextPath : this.contextPath;
return CUSTOM_NAME + "-" + String.join("_", endpoint, String.valueOf(endpointPort), contextPathTmp,
serverListName) + (StringUtils.isNotBlank(namespace) ? ("_" + StringUtils.trim(
namespace)) : "");
serverListName) + (StringUtils.isNotBlank(namespace) ? ("_" + StringUtils.trim(namespace)) : "");
}

@Override
Expand All @@ -113,27 +114,32 @@ public int getOrder() {

@Override
public boolean match(final NacosClientProperties properties) {
String endpointTmp = getEndPointTmp(properties);
return StringUtils.isNotBlank(endpointTmp);
}

@Override
public String getAddressSource() {
return this.addressServerUrl;
}

private String getEndPointTmp(NacosClientProperties properties) {
String endpointTmp = properties.getProperty(PropertyKeyConst.ENDPOINT);
String isUseEndpointRuleParsing = properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE,
properties.getProperty(SystemPropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE,
String.valueOf(ParamUtil.USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE)));
if (Boolean.parseBoolean(isUseEndpointRuleParsing)) {
endpointTmp = ParamUtil.parsingEndpointRule(endpointTmp);
}
return StringUtils.isNotBlank(endpointTmp);
}

@Override
public String getAddressSource() {
return this.addressServerUrl;
return endpointTmp;
}

/**
* Start refresh server list task.
*
* @throws NacosException nacos exception
*/
public void startRefreshServerListTask() throws NacosException {
public void startRefreshServerListTask(NacosClientProperties properties) throws NacosException {
for (int i = 0; i < initServerListRetryTimes && getServerList().isEmpty(); ++i) {
refreshServerListIfNeed();
if (!serversFromEndpoint.isEmpty()) {
Expand All @@ -155,7 +161,10 @@ public void startRefreshServerListTask() throws NacosException {
refreshServerListExecutor = new ScheduledThreadPoolExecutor(1,
new NameThreadFactory("com.alibaba.nacos.client.address.EndpointServerListProvider.refreshServerList"));
// executor schedules the timer task
refreshServerListExecutor.scheduleWithFixedDelay(this::refreshServerListIfNeed, 0L, 30L, TimeUnit.SECONDS);
long refreshInterval = Long.parseLong(
properties.getProperty(PropertyKeyConst.ENDPOINT_REFRESH_INTERVAL_SECONDS, "30"));
refreshServerListExecutor.scheduleWithFixedDelay(this::refreshServerListIfNeed, 0L, refreshInterval,
TimeUnit.SECONDS);
}

private void refreshServerListIfNeed() {
Expand All @@ -168,7 +177,7 @@ private void refreshServerListIfNeed() {
throw new Exception("Can not acquire Nacos list");
}
list.sort(String::compareTo);
if (null == serversFromEndpoint || !CollectionUtils.isEqualCollection(list, serversFromEndpoint)) {
if (!CollectionUtils.isEqualCollection(list, serversFromEndpoint)) {
LOGGER.info("[SERVER-LIST] server list is updated: {}", list);
serversFromEndpoint = list;
lastServerListRefreshTime = System.currentTimeMillis();
Expand All @@ -181,8 +190,8 @@ private void refreshServerListIfNeed() {

private List<String> getServerListFromEndpoint() {
try {
HttpRestResult<String> httpResult = nacosRestTemplate.get(addressServerUrl, HttpUtils.builderHeader(moduleName),
Query.EMPTY, String.class);
HttpRestResult<String> httpResult = nacosRestTemplate.get(addressServerUrl,
HttpUtils.builderHeader(moduleName), Query.EMPTY, String.class);

if (!httpResult.ok()) {
LOGGER.error("[check-serverlist] error. addressServerUrl: {}, code: {}", addressServerUrl,
Expand All @@ -192,10 +201,7 @@ private List<String> getServerListFromEndpoint() {
List<String> lines = IoUtils.readLines(new StringReader(httpResult.getData()));
List<String> result = new ArrayList<>(lines.size());
for (String serverAddr : lines) {
if (StringUtils.isBlank(serverAddr)) {
continue;
}
String[] ipPort = InternetAddressUtil.splitIPPortStr(serverAddr.trim());
String[] ipPort = InternetAddressUtil.splitIPPortStr(serverAddr);
String ip = ipPort[0].trim();
if (ipPort.length == 1) {
result.add(ip + InternetAddressUtil.IP_PORT_SPLITER + ParamUtil.getDefaultServerPort());
Expand All @@ -211,15 +217,8 @@ private List<String> getServerListFromEndpoint() {
}

private void initEndpoint(NacosClientProperties properties) {
String endpointTmp = properties.getProperty(PropertyKeyConst.ENDPOINT);
// Whether to enable domain name resolution rules
String isUseEndpointRuleParsing = properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE,
properties.getProperty(SystemPropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE,
String.valueOf(ParamUtil.USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE)));
if (Boolean.parseBoolean(isUseEndpointRuleParsing)) {
endpointTmp = ParamUtil.parsingEndpointRule(endpointTmp);
}
this.endpoint = StringUtils.isNotBlank(endpointTmp) ? endpointTmp : "";
// Endpoint should not be null or empty, because the match has return `true`.
this.endpoint = getEndPointTmp(properties);
}

private void initEndpointPort(NacosClientProperties properties) {
Expand All @@ -241,8 +240,12 @@ private void initEndpointContextPath(NacosClientProperties properties) {
}

private void initServerListName(NacosClientProperties properties) {
String serverListNameTmp = properties.getProperty(PropertyKeyConst.ENDPOINT_CLUSTER_NAME,
properties.getProperty(PropertyKeyConst.CLUSTER_NAME));
String serverListNameTmp = properties.getProperty(PropertyKeyConst.ENDPOINT_CLUSTER_NAME);
boolean isUseClusterName = Boolean.parseBoolean(
properties.getProperty(PropertyKeyConst.IS_ADAPT_CLUSTER_NAME_USAGE));
if (StringUtils.isBlank(serverListNameTmp) && isUseClusterName) {
serverListNameTmp = properties.getProperty(PropertyKeyConst.CLUSTER_NAME);
}
if (!StringUtils.isBlank(serverListNameTmp)) {
this.serverListName = serverListNameTmp;
}
Expand All @@ -259,17 +262,16 @@ private void initAddressServerUrl(NacosClientProperties properties) {
addressServerUrlTem.append("?namespace=").append(namespace);
hasQueryString = true;
}
if (properties != null && properties.containsKey(PropertyKeyConst.ENDPOINT_QUERY_PARAMS)) {
addressServerUrlTem.append(
hasQueryString ? "&" : "?" + properties.getProperty(PropertyKeyConst.ENDPOINT_QUERY_PARAMS));

if (properties.containsKey(PropertyKeyConst.ENDPOINT_QUERY_PARAMS)) {
addressServerUrlTem.append(hasQueryString ? "&" : "?");
addressServerUrlTem.append(properties.getProperty(PropertyKeyConst.ENDPOINT_QUERY_PARAMS));
}
this.addressServerUrl = addressServerUrlTem.toString();
LOGGER.info("address server url = {}", this.addressServerUrl);
}

private void initModuleName(NacosClientProperties properties) {
String moduleNameTmp = properties.getProperty(PropertyKeyConst.CLIENT_MODULE_TYPE);
String moduleNameTmp = properties.getProperty(Constants.CLIENT_MODULE_TYPE);
if (StringUtils.isNotBlank(moduleNameTmp)) {
this.moduleName = moduleNameTmp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
import static com.alibaba.nacos.common.constant.RequestUrlConstants.HTTP_PREFIX;

/**
* Address server list provider.
* Properties server list provider.
*
* @author totalo
*/
public class AddressServerListProvider extends AbstractServerListProvider {
public class PropertiesListProvider extends AbstractServerListProvider {

private static final String FIXED_NAME = "fixed";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@
* @author zongtanghu
*/
public class ServerListChangeEvent extends SlowEvent {

private static final long serialVersionUID = -1655577508567092395L;
}
Loading

0 comments on commit 85a1e38

Please sign in to comment.