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

simplify nacos app labels collector and log fix. #11867

Merged
merged 5 commits into from
Mar 22, 2024
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
28 changes: 8 additions & 20 deletions api/src/main/java/com/alibaba/nacos/api/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ public class Constants {

public static final int DEFAULT_REDO_THREAD_COUNT = 1;

public static final String APP_CONN_LABELS_PREFIX = "nacos.app.conn.labels";

public static final String GRAY = "gray";
public static final String APP_CONN_LABELS_KEY = "nacos.app.conn.labels";

public static final String DOT = ".";

Expand All @@ -239,21 +237,11 @@ public class Constants {

public static final String ENV_KEY = "env";

public static final String APP_CONN_LABELS_PROPERTIES_WEIGHT_KEY = APP_CONN_LABELS_PREFIX + DOT + WEIGHT + DOT + PROPERTIES_KEY;

public static final int APP_CONN_LABELS_PROPERTIES_DEFAULT_WEIGHT = 3;

public static final String APP_CONN_LABELS_JVM_WEIGHT_KEY = APP_CONN_LABELS_PREFIX + DOT + WEIGHT + DOT + JVM_KEY;

public static final int APP_CONN_LABELS_JVM_DEFAULT_WEIGHT = 2;

public static final String APP_CONN_LABELS_ENV_WEIGHT_KEY = APP_CONN_LABELS_PREFIX + DOT + WEIGHT + DOT + ENV_KEY;

public static final int APP_CONN_LABELS_ENV_DEFAULT_WEIGHT = 1;
public static final String APP_CONN_LABELS_PREFERRED = "nacos_app_conn_labels_preferred";

public static final String APP_CONN_PREFIX = "app_";

public static final String CONFIG_GRAY = "nacos.config" + DOT + GRAY;
public static final String CONFIG_GRAY_LABEL = "nacos.config.gray.label";

/**
* The constants in config directory.
Expand All @@ -269,17 +257,17 @@ public static class Config {
* The constants in naming directory.
*/
public static class Naming {

public static final String NAMING_MODULE = "naming";

public static final String CMDB_CONTEXT_TYPE = "CMDB";
}

/**
* The constants in remote directory.
*/
public static class Remote {

public static final String INTERNAL_MODULE = "internal";
}

Expand All @@ -291,9 +279,9 @@ public static class Exception {
public static final int SERIALIZE_ERROR_CODE = 100;

public static final int DESERIALIZE_ERROR_CODE = 101;

public static final int FIND_DATASOURCE_ERROR_CODE = 102;

public static final int FIND_TABLE_ERROR_CODE = 103;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.alibaba.nacos.client.config.filter.impl.ConfigResponse;
import com.alibaba.nacos.client.config.utils.ContentUtils;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.env.SourceType;
import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.utils.AppNameUtils;
Expand All @@ -49,6 +50,7 @@
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.TenantUtil;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.labels.impl.DefaultLabelsCollectorManager;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter;
Expand All @@ -60,6 +62,7 @@
import com.alibaba.nacos.common.remote.client.RpcClientFactory;
import com.alibaba.nacos.common.remote.client.RpcClientTlsConfig;
import com.alibaba.nacos.common.remote.client.ServerListFactory;
import com.alibaba.nacos.common.utils.ConnLabelsUtils;
import com.alibaba.nacos.common.utils.ConvertUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.MD5Utils;
Expand All @@ -80,6 +83,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
Expand All @@ -95,6 +99,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import static com.alibaba.nacos.api.common.Constants.APP_CONN_PREFIX;
import static com.alibaba.nacos.api.common.Constants.ENCODE;

/**
Expand Down Expand Up @@ -123,6 +128,8 @@ public class ClientWorker implements Closeable {
*/
private final AtomicReference<Map<String, CacheData>> cacheMap = new AtomicReference<>(new HashMap<>());

private Map<String, String> appLables = new HashMap<>();

private final ConfigFilterChainManager configFilterChainManager;

private final String uuid = UUID.randomUUID().toString();
Expand Down Expand Up @@ -477,14 +484,18 @@ public ClientWorker(final ConfigFilterChainManager configFilterChainManager, Ser
init(properties);

agent = new ConfigRpcTransportClient(properties, serverListManager);
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(
initWorkerThreadCount(properties),
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(initWorkerThreadCount(properties),
new NameThreadFactory("com.alibaba.nacos.client.Worker"));
agent.setExecutor(executorService);
agent.start();

}

void initAppLabels(Properties properties) {
this.appLables = ConnLabelsUtils.addPrefixForEachKey(defaultLabelsCollectorManager.getLabels(properties),
APP_CONN_PREFIX);
}

private int initWorkerThreadCount(NacosClientProperties properties) {
int count = ThreadUtils.getSuitableThreadCount(THREAD_MULTIPLE);
if (properties == null) {
Expand All @@ -505,6 +516,7 @@ private void init(NacosClientProperties properties) {

this.enableRemoteSyncConfig = Boolean.parseBoolean(
properties.getProperty(PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG));
initAppLabels(properties.getProperties(SourceType.PROPERTIES));
}

Map<String, Object> getMetrics(List<ClientConfigMetricRequest.MetricsKey> metricsKeys) {
Expand Down Expand Up @@ -567,6 +579,8 @@ public boolean isHealthServer() {
return agent.isHealthServer();
}

private static DefaultLabelsCollectorManager defaultLabelsCollectorManager = new DefaultLabelsCollectorManager();

public class ConfigRpcTransportClient extends ConfigTransportClient {

Map<String, ExecutorService> multiTaskExecutor = new HashMap<>();
Expand Down Expand Up @@ -633,10 +647,17 @@ private Map<String, String> getLabels() {
labels.put(RemoteConstants.LABEL_SOURCE, RemoteConstants.LABEL_SOURCE_SDK);
labels.put(RemoteConstants.LABEL_MODULE, RemoteConstants.LABEL_MODULE_CONFIG);
labels.put(Constants.APPNAME, AppNameUtils.getAppName());
labels.put(Constants.VIPSERVER_TAG, EnvUtil.getSelfVipserverTag());
labels.put(Constants.AMORY_TAG, EnvUtil.getSelfAmoryTag());
labels.put(Constants.LOCATION_TAG, EnvUtil.getSelfLocationTag());
if (EnvUtil.getSelfVipserverTag() != null) {
labels.put(Constants.VIPSERVER_TAG, EnvUtil.getSelfVipserverTag());
}
if (EnvUtil.getSelfAmoryTag() != null) {
labels.put(Constants.AMORY_TAG, EnvUtil.getSelfAmoryTag());
}
if (EnvUtil.getSelfLocationTag() != null) {
labels.put(Constants.LOCATION_TAG, EnvUtil.getSelfLocationTag());
}

labels.putAll(appLables);
return labels;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,19 @@
import java.util.Properties;

/**
* NacosClientProperties interface.
* include all the properties from jvm args, system environment, default setting.
* more details you can see https://github.com/alibaba/nacos/issues/8622
* NacosClientProperties interface. include all the properties from jvm args, system environment, default setting. more
* details you can see https://github.com/alibaba/nacos/issues/8622
*
* @author onewe
*/
public interface NacosClientProperties {

/**
* all the NacosClientProperties object must be created by PROTOTYPE,
* so child NacosClientProperties can read properties from the PROTOTYPE.
* it looks like this:
* |-PROTOTYPE----------------> ip=127.0.0.1
* |---|-child1---------------> port=6379
* if you search key called "port" from child1, certainly you will get 6379
* if you search key called "ip" from child1, you will get 127.0.0.1.
* because the child can read properties from parent NacosClientProperties
* all the NacosClientProperties object must be created by PROTOTYPE, so child NacosClientProperties can read
* properties from the PROTOTYPE. it looks like this: |-PROTOTYPE----------------> ip=127.0.0.1
* |---|-child1---------------> port=6379 if you search key called "port" from child1, certainly you will get 6379
* if you search key called "ip" from child1, you will get 127.0.0.1. because the child can read properties from
* parent NacosClientProperties
*/
NacosClientProperties PROTOTYPE = SearchableProperties.INSTANCE;

Expand All @@ -48,21 +45,32 @@ public interface NacosClientProperties {

/**
* get property, if the value can not be got by the special key, the default value will be returned.
* @param key special key
*
* @param key special key
* @param defaultValue default value
* @return string value or default value.
*/
String getProperty(String key, String defaultValue);

/**
* get property from special property source.
*
* @param source source type
* @see SourceType
* @param key special key
* @param key special key
* @return string value or null.
* @see SourceType
*/
String getPropertyFrom(SourceType source, String key);

/**
* get property from special property source.
*
* @param source source type
* @return string value or null.
* @see SourceType
*/
Properties getProperties(SourceType source);

/**
* get boolean, if the value can not be got by the special key, the null will be returned.
*
Expand All @@ -73,7 +81,8 @@ public interface NacosClientProperties {

/**
* get boolean, if the value can not be got by the special key, the default value will be returned.
* @param key special key
*
* @param key special key
* @param defaultValue default value
* @return boolean value or defaultValue.
*/
Expand All @@ -89,7 +98,8 @@ public interface NacosClientProperties {

/**
* get integer, if the value can not be got by the special key, the default value will be returned.
* @param key special key
*
* @param key special key
* @param defaultValue default value
* @return integer value or default value
*/
Expand All @@ -105,46 +115,53 @@ public interface NacosClientProperties {

/**
* get long, if the value can not be got by the special key, the default value will be returned.
* @param key special key
*
* @param key special key
* @param defaultValue default value
* @return long value or default value
*/
Long getLong(String key, Long defaultValue);

/**
* set property.
* @param key key
*
* @param key key
* @param value value
*/
void setProperty(String key, String value);

/**
* add properties.
*
* @param properties properties
*/
void addProperties(Properties properties);

/**
* Tests if the specified object is a key in this NacosClientProperties.
*
* @param key key – possible key
* @return true if and only if the specified object is a key in this NacosClientProperties, false otherwise.
*/
boolean containsKey(String key);

/**
* get properties from NacosClientProperties.
*
* @return properties
*/
Properties asProperties();

/**
* create a new NacosClientProperties which scope is itself.
*
* @return NacosClientProperties
*/
NacosClientProperties derive();

/**
* create a new NacosClientProperties from NacosClientProperties#PROTOTYPE and init.
*
* @param properties properties
* @return NacosClientProperties
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ public String getPropertyFrom(SourceType source, String key) {
}
}

@Override
public Properties getProperties(SourceType source) {
if (source == null) {
return null;
}
switch (source) {
case JVM:
return JVM_ARGS_PROPERTY_SOURCE.asProperties();
case ENV:
return SYSTEM_ENV_PROPERTY_SOURCE.asProperties();
case PROPERTIES:
return this.propertiesPropertySource.asProperties();
default:
return null;
}
}

@Override
public Boolean getBoolean(String key) {
return getBoolean(key, null);
Expand Down
5 changes: 5 additions & 0 deletions client/src/main/resources/nacos-log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
additivity="false">
<AppenderRef ref="ASYNC_REMOTE"/>
</Logger>

<Logger name="com.alibaba.nacos.common.labels" level="${nacosClientProperty:com.alibaba.nacos.config.log.level:-info}"
additivity="false">
<AppenderRef ref="ASYNC_REMOTE"/>
</Logger>

<Logger name="com.alibaba.nacos.client.config" level="${nacosClientProperty:com.alibaba.nacos.config.log.level:-info}"
additivity="false">
Expand Down
4 changes: 4 additions & 0 deletions client/src/main/resources/nacos-logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
<appender-ref ref="ASYNC-CONFIG"/>
</logger>

<Logger name="com.alibaba.nacos.common.labels" level="${nacosConfigLogLevel}"
additivity="false">
<appender-ref ref="ASYNC-REMOTE"/>
</Logger>

<Logger name="com.alibaba.nacos.common.remote.client" level="${nacosConfigLogLevel}"
additivity="false">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void testLoadConfiguration() {
loggerContext = (LoggerContext) LogManager.getContext(false);
contextConfiguration = loggerContext.getConfiguration();
Map<String, LoggerConfig> nacosClientLoggers = contextConfiguration.getLoggers();
assertEquals(4, nacosClientLoggers.size());
assertEquals(5, nacosClientLoggers.size());
for (Map.Entry<String, LoggerConfig> loggerEntry : nacosClientLoggers.entrySet()) {
String loggerName = loggerEntry.getKey();
Assert.assertTrue(loggerName.startsWith(NACOS_LOGGER_PREFIX));
Expand Down
Loading
Loading