Skip to content

Commit

Permalink
Add log for server list manager and split constructor and start. (#12736
Browse files Browse the repository at this point in the history
)
  • Loading branch information
KomachiSion authored Oct 15, 2024
1 parent b971164 commit c6b088c
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public abstract class AbstractServerListManager implements ServerListFactory, Cl

protected ServerListProvider serverListProvider;

private NacosClientProperties properties;
protected NacosClientProperties properties;

public AbstractServerListManager(NacosClientProperties properties) throws NacosException {
this(properties, null);
Expand All @@ -54,24 +54,12 @@ public AbstractServerListManager(NacosClientProperties properties, String namesp
return;
}
if (StringUtils.isNotBlank(namespace)) {
// To avoid set operation affect the original properties.
properties = properties.derive();
properties.setProperty(PropertyKeyConst.NAMESPACE, namespace);
}
properties.setProperty(PropertyKeyConst.CLIENT_MODULE_TYPE, getModuleName());
this.properties = properties;
Collection<ServerListProvider> serverListProviders = NacosServiceLoader.load(ServerListProvider.class);
Collection<ServerListProvider> sorted = serverListProviders.stream()
.sorted((a, b) -> b.getOrder() - a.getOrder()).collect(Collectors.toList());
for (ServerListProvider each : sorted) {
if (each.match(properties)) {
this.serverListProvider = each;
break;
}
}
if (null == serverListProvider) {
LOGGER.error("no server list provider found");
return;
}
this.serverListProvider.init(properties, getNacosRestTemplate());
}

@Override
Expand All @@ -90,6 +78,32 @@ public void shutdown() throws NacosException {
LOGGER.info("{} do shutdown stop", className);
}

/**
* Start server list manager.
*
* @throws NacosException during start and initialize.
*/
public void start() throws NacosException {
Collection<ServerListProvider> serverListProviders = NacosServiceLoader.load(ServerListProvider.class);
Collection<ServerListProvider> sorted = serverListProviders.stream()
.sorted((a, b) -> b.getOrder() - a.getOrder()).collect(Collectors.toList());
for (ServerListProvider each : sorted) {
boolean matchResult = each.match(properties);
LOGGER.info("Load and match ServerListProvider {}, match result: {}", each.getClass().getCanonicalName(),
matchResult);
if (matchResult) {
this.serverListProvider = each;
LOGGER.info("Will use {} as ServerListProvider", this.serverListProvider.getClass().getCanonicalName());
break;
}
}
if (null == serverListProvider) {
LOGGER.error("no server list provider found");
return;
}
this.serverListProvider.init(properties, getNacosRestTemplate());
}

public NacosClientProperties getProperties() {
return properties;
}
Expand All @@ -116,12 +130,14 @@ public boolean isFixed() {

/**
* get module name.
*
* @return module name
*/
public abstract String getModuleName();

/**
* get nacos rest template.
*
* @return nacos rest template
*/
public abstract NacosRestTemplate getNacosRestTemplate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class ConfigServerListManager extends AbstractServerListManager {
/**
* The name of the different environment.
*/
private final String name;
private String name;

private String tenant = "";

Expand All @@ -53,7 +53,6 @@ public class ConfigServerListManager extends AbstractServerListManager {

public ConfigServerListManager(NacosClientProperties properties) throws NacosException {
super(properties);
this.name = initServerName(properties);
String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
if (StringUtils.isNotBlank(namespace)) {
this.tenant = namespace;
Expand All @@ -70,7 +69,10 @@ public NacosRestTemplate getNacosRestTemplate() {
return ConfigHttpClientManager.getInstance().getNacosRestTemplate();
}

public void start() {
@Override
public void start() throws NacosException {
super.start();
this.name = initServerName(properties);
iterator = iterator();
currentServerAddr = iterator.next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private void init(Properties properties) throws NacosException {
InitUtils.initSerialization();
InitUtils.initWebRootContext(nacosClientProperties);
serverListManager = new NamingServerListManager(nacosClientProperties, namespace);
serverListManager.start();
securityProxy = new SecurityProxy(serverListManager.getServerList(),
NamingHttpClientManager.getInstance().getNacosRestTemplate());
initSecurityProxy(properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientManager;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.common.JustForTest;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import org.slf4j.Logger;

Expand All @@ -46,16 +47,18 @@ public class NamingServerListManager extends AbstractServerListManager {

private boolean isDomain;

@JustForTest
public NamingServerListManager(Properties properties) throws NacosException {
this(NacosClientProperties.PROTOTYPE.derive(properties));
}

public NamingServerListManager(NacosClientProperties properties) throws NacosException {
this(properties, "");
this(NacosClientProperties.PROTOTYPE.derive(properties), "");
}

public NamingServerListManager(NacosClientProperties properties, String namespace) throws NacosException {
super(properties, namespace);
}

@Override
public void start() throws NacosException {
super.start();
List<String> serverList = getServerList();
if (serverList.isEmpty()) {
throw new NacosLoadException("serverList is empty,please check configuration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public NamingClientProxyDelegate(String namespace, ServiceInfoHolder serviceInfo
this.serviceInfoUpdateService = new ServiceInfoUpdateService(properties, serviceInfoHolder, this,
changeNotifier);
this.serverListManager = new NamingServerListManager(properties, namespace);
this.serverListManager.start();
this.serviceInfoHolder = serviceInfoHolder;
this.securityProxy = new SecurityProxy(this.serverListManager.getServerList(),
NamingHttpClientManager.getInstance().getNacosRestTemplate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ public void receiveConfigInfo(String configInfo) {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, "aaa");
final NacosClientProperties nacosProperties = NacosClientProperties.PROTOTYPE.derive(properties);
Mockito.when(mockWoker.getAgent()).thenReturn(new ConfigTransportClient(nacosProperties, new ConfigServerListManager(nacosProperties)) {
ConfigServerListManager mgr = new ConfigServerListManager(nacosProperties);
mgr.start();
ConfigTransportClient client = new ConfigTransportClient(nacosProperties, mgr) {
@Override
public void startInternal() throws NacosException {
// NOOP
Expand Down Expand Up @@ -235,7 +237,8 @@ public boolean publishConfig(String dataId, String group, String tenant, String
public boolean removeConfig(String dataId, String group, String tenant, String tag) throws NacosException {
return false;
}
});
};
Mockito.when(mockWoker.getAgent()).thenReturn(client);

final String config = nacosConfigService.getConfigAndSignListener(dataId, group, timeout, listener);
assertEquals(content, config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ void testGetterAndSetter() throws Exception {
when(mockedProperties.getProperty(PropertyKeyConst.ENDPOINT)).thenReturn("aaa");
when(mockedProperties.getProperty(PropertyKeyConst.NAMESPACE)).thenReturn("namespace1");
ConfigServerListManager server = new ConfigServerListManager(mockedProperties);
server.start();
final ServerHttpAgent serverHttpAgent = new ServerHttpAgent(server, new Properties());

final String appname = ServerHttpAgent.getAppname();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
import static com.alibaba.nacos.common.constant.RequestUrlConstants.HTTP_PREFIX;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.contains;
Expand Down Expand Up @@ -104,6 +104,7 @@ void testGetter() throws NacosException {
when(mockedProperties.getProperty(PropertyKeyConst.SERVER_ADDR)).thenReturn("1.1.1.1");
when(mockedProperties.getProperty(PropertyKeyConst.NAMESPACE)).thenReturn("namespace");
final ConfigServerListManager mgr = new ConfigServerListManager(mockedProperties);
mgr.start();
assertEquals("nacos", mgr.getContextPath());
assertEquals("Config-fixed-namespace-1.1.1.1_8848", mgr.getName());
assertEquals("namespace", mgr.getTenant());
Expand All @@ -119,6 +120,7 @@ void testGetter() throws NacosException {

final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
final ConfigServerListManager mgr2 = new ConfigServerListManager(nacosClientProperties);
mgr2.start();
assertEquals("aaa", mgr2.getContextPath());
}

Expand All @@ -129,6 +131,7 @@ void testGetter() throws NacosException {
properties.put(PropertyKeyConst.SERVER_ADDR, "https://1.1.1.1:8848");
final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
final ConfigServerListManager mgr2 = new ConfigServerListManager(nacosClientProperties);
mgr2.start();
assertEquals("aaa", mgr2.getContextPath());
assertEquals("[https://1.1.1.1:8848]", mgr2.getServerList().toString());
}
Expand All @@ -140,6 +143,7 @@ void testGetter() throws NacosException {

final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties2);
final ConfigServerListManager mgr3 = new ConfigServerListManager(nacosClientProperties);
mgr3.start();
assertEquals(1, mgr3.getServerList().size());
assertEquals("1.1.1.1:8848", mgr3.getServerList().get(0));
assertEquals("[1.1.1.1:8848]", mgr3.getUrlString());
Expand All @@ -154,6 +158,7 @@ void testGetter() throws NacosException {

final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties3);
final ConfigServerListManager mgr4 = new ConfigServerListManager(nacosClientProperties);
mgr4.start();
assertEquals(2, mgr4.getServerList().size());
assertEquals("1.1.1.1:8848", mgr4.getServerList().get(0));
assertEquals("2.2.2.2:8848", mgr4.getServerList().get(1));
Expand All @@ -169,6 +174,7 @@ void testGetter() throws NacosException {

final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties4);
final ConfigServerListManager mgr5 = new ConfigServerListManager(nacosClientProperties);
mgr5.start();
assertEquals(2, mgr5.getServerList().size());
assertEquals("1.1.1.1:8848", mgr5.getServerList().get(0));
assertEquals("2.2.2.2:8848", mgr5.getServerList().get(1));
Expand All @@ -185,15 +191,18 @@ void testIterator() throws NacosException {
when(mockedProperties.getProperty(PropertyKeyConst.SERVER_ADDR)).thenReturn("1.1.1.1:8848");
when(mockedProperties.getProperty(PropertyKeyConst.NAMESPACE)).thenReturn("aaa");
final ConfigServerListManager mgr = new ConfigServerListManager(mockedProperties);
mgr.start();

// new iterator
final Iterator<String> it = mgr.iterator();
assertTrue(it.hasNext());
assertEquals("1.1.1.1:8848", it.next());

assertNull(mgr.getIterator());
Iterator<String> initIterator = mgr.getIterator();
assertNotNull(initIterator);
mgr.refreshCurrentServerAddr();
assertNotNull(mgr.getIterator());
assertNotEquals(initIterator, mgr.getIterator());

final String currentServerAddr = mgr.getCurrentServer();
assertEquals("1.1.1.1:8848", currentServerAddr);
Expand All @@ -215,6 +224,7 @@ void testAddressServerBaseServerAddrsStr() throws NacosException {
properties.setProperty(PropertyKeyConst.CONTEXT_PATH, endpointContextPath);
final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
ConfigServerListManager serverListManager = new ConfigServerListManager(clientProperties);
serverListManager.start();
assertEquals(1, serverListManager.getServerList().size());
assertTrue(serverListManager.getServerList().contains(serverAddrStr));
}
Expand All @@ -230,6 +240,7 @@ void testAddressServerBaseEndpoint() throws NacosException {
properties.setProperty(PropertyKeyConst.ENDPOINT_CONTEXT_PATH, endpointContextPath);
final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
ConfigServerListManager serverListManager = new ConfigServerListManager(clientProperties);
serverListManager.start();
assertTrue(serverListManager.getAddressSource().startsWith(
HTTP_PREFIX + endpoint + ":" + endpointPort + endpointContextPath));
}
Expand All @@ -247,6 +258,7 @@ void testInitParam() throws NacosException, NoSuchFieldException, IllegalAccessE
properties.setProperty(PropertyKeyConst.CONTEXT_PATH, contextPath);
final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
ConfigServerListManager serverListManager = new ConfigServerListManager(clientProperties);
serverListManager.start();
Field providerField = AbstractServerListManager.class.getDeclaredField("serverListProvider");
providerField.setAccessible(true);
ServerListProvider serverListProvider = (ServerListProvider) providerField.get(serverListManager);
Expand Down Expand Up @@ -285,6 +297,7 @@ void testWithEndpointContextPath() throws NacosException {
properties.setProperty(PropertyKeyConst.CONTEXT_PATH, contextPath);
final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
ConfigServerListManager serverListManager = new ConfigServerListManager(clientProperties);
serverListManager.start();
assertTrue(serverListManager.getAddressSource().contains(endpointContextPath));
assertTrue(serverListManager.getName().contains("endpointContextPath"));
}
Expand All @@ -306,6 +319,7 @@ void testWithEndpointClusterName() throws NacosException {
properties.setProperty(PropertyKeyConst.CONTEXT_PATH, contextPath);
final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
ConfigServerListManager serverListManager = new ConfigServerListManager(clientProperties);
serverListManager.start();
String addressSource = serverListManager.getAddressSource();
assertTrue(addressSource.contains(endpointContextPath));
assertTrue(serverListManager.getName().contains("endpointContextPath"));
Expand All @@ -329,6 +343,7 @@ void testWithoutEndpointContextPath() throws NacosException {
properties.setProperty(PropertyKeyConst.CONTEXT_PATH, contextPath);
final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
ConfigServerListManager serverListManager = new ConfigServerListManager(clientProperties);
serverListManager.start();
String endpointContextPath = "/endpointContextPath";

assertFalse(serverListManager.getAddressSource().contains(endpointContextPath));
Expand All @@ -346,6 +361,7 @@ void testUseEndpointParsingRule() throws NacosException {
properties.setProperty(PropertyKeyConst.ENDPOINT_PORT, "9090");
final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
ConfigServerListManager serverListManager = new ConfigServerListManager(clientProperties);
serverListManager.start();
String addressServerUrl = serverListManager.getAddressSource();
assertTrue(addressServerUrl.startsWith("http://1.1.1.1"));
}
Expand Down
Loading

0 comments on commit c6b088c

Please sign in to comment.