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

[ISSUES #10029] optimize SearchableProperties building code #10030

Merged
merged 1 commit into from
Mar 2, 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 @@ -22,20 +22,17 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;

/**
* Searchable NacosClientProperties.
* the SearchableProperties that it can be specified search order by
* nacos.env.first
* Searchable NacosClientProperties. the SearchableProperties that it can be specified search order by nacos.env.first
*
* @author onewe
*/
class SearchableProperties implements NacosClientProperties {
Expand All @@ -48,11 +45,42 @@ class SearchableProperties implements NacosClientProperties {

private static final DefaultSettingPropertySource DEFAULT_SETTING_PROPERTY_SOURCE = new DefaultSettingPropertySource();

private static final List<SourceType> DEFAULT_ORDER = Arrays.asList(SourceType.PROPERTIES, SourceType.JVM,
SourceType.ENV, SourceType.DEFAULT_SETTING);
private static final List<SourceType> SEARCH_ORDER;

private static final CompositeConverter CONVERTER = new CompositeConverter();

static {
List<SourceType> initOrder = Arrays.asList(SourceType.PROPERTIES, SourceType.JVM, SourceType.ENV,
SourceType.DEFAULT_SETTING);

String firstEnv = JVM_ARGS_PROPERTY_SOURCE.getProperty(Constants.SysEnv.NACOS_ENV_FIRST);
if (StringUtils.isBlank(firstEnv)) {
firstEnv = SYSTEM_ENV_PROPERTY_SOURCE.getProperty(Constants.SysEnv.NACOS_ENV_FIRST);
}

if (StringUtils.isNotBlank(firstEnv)) {
try {
final SourceType sourceType = SourceType.valueOf(firstEnv.toUpperCase());
if (!sourceType.equals(SourceType.PROPERTIES) && !sourceType.equals(SourceType.DEFAULT_SETTING)) {
final int index = initOrder.indexOf(sourceType);
final SourceType replacedSourceType = initOrder.set(0, sourceType);
initOrder.set(index, replacedSourceType);
}
} catch (Exception e) {
LOGGER.warn("first source type parse error, it will be used default order!", e);
}
}
SEARCH_ORDER = initOrder;
StringBuilder orderInfo = new StringBuilder("properties search order:");
for (int i = 0; i < SEARCH_ORDER.size(); i++) {
orderInfo.append(SEARCH_ORDER.get(i).toString());
if (i < SEARCH_ORDER.size() - 1) {
orderInfo.append("->");
}
}
LOGGER.debug(orderInfo.toString());
}

static final SearchableProperties INSTANCE = new SearchableProperties();

private final List<AbstractPropertySource> propertySources;
Expand All @@ -65,8 +93,8 @@ private SearchableProperties() {

private SearchableProperties(PropertiesPropertySource propertiesPropertySource) {
this.propertiesPropertySource = propertiesPropertySource;
this.propertySources = build(propertiesPropertySource,
JVM_ARGS_PROPERTY_SOURCE, SYSTEM_ENV_PROPERTY_SOURCE, DEFAULT_SETTING_PROPERTY_SOURCE);
this.propertySources = build(propertiesPropertySource, JVM_ARGS_PROPERTY_SOURCE, SYSTEM_ENV_PROPERTY_SOURCE,
DEFAULT_SETTING_PROPERTY_SOURCE);
}

@Override
Expand Down Expand Up @@ -122,8 +150,7 @@ public void addProperties(Properties properties) {
@Override
public Properties asProperties() {
Properties properties = new Properties();
final ListIterator<AbstractPropertySource> iterator = propertySources.listIterator(
propertySources.size());
final ListIterator<AbstractPropertySource> iterator = propertySources.listIterator(propertySources.size());
while (iterator.hasPrevious()) {
final AbstractPropertySource previous = iterator.previous();
properties.putAll(previous.asProperties());
Expand Down Expand Up @@ -166,60 +193,9 @@ private <T> Optional<T> search(String key, Class<T> targetType) {
}

private List<AbstractPropertySource> build(AbstractPropertySource... propertySources) {

String firstEnv = JVM_ARGS_PROPERTY_SOURCE.getProperty(Constants.SysEnv.NACOS_ENV_FIRST);
if (StringUtils.isBlank(firstEnv)) {
firstEnv = SYSTEM_ENV_PROPERTY_SOURCE.getProperty(Constants.SysEnv.NACOS_ENV_FIRST);
}

if (StringUtils.isBlank(firstEnv)) {
return sortPropertySourceDefaultOrder(propertySources);
}

try {
final SourceType sourceType = SourceType.valueOf(firstEnv.toUpperCase());
if (SourceType.DEFAULT_SETTING.equals(sourceType) || SourceType.PROPERTIES.equals(sourceType)) {
return sortPropertySourceDefaultOrder(propertySources);
}
return sortPropertySource(sourceType, propertySources);
} catch (Exception e) {
LOGGER.error("first source type parse error, it will be used default order!", e);
return sortPropertySourceDefaultOrder(propertySources);
}
}

private List<AbstractPropertySource> sortPropertySourceDefaultOrder(
AbstractPropertySource... propertySources) {
final Map<SourceType, AbstractPropertySource> sourceMap = Arrays.stream(propertySources)
.collect(Collectors.toMap(AbstractPropertySource::getType, propertySource -> propertySource));
final List<AbstractPropertySource> collect = DEFAULT_ORDER.stream().map(sourceMap::get)
.collect(Collectors.toList());
LOGGER.info("properties search order:PROPERTIES->JVM->ENV->DEFAULT_SETTING");
return collect;
}

private List<AbstractPropertySource> sortPropertySource(SourceType firstType,
AbstractPropertySource... propertySources) {
List<SourceType> tempList = new ArrayList<>(4);
tempList.add(firstType);

final Map<SourceType, AbstractPropertySource> sourceMap = Arrays.stream(propertySources)
.collect(Collectors.toMap(AbstractPropertySource::getType, propertySource -> propertySource));
final List<AbstractPropertySource> collect = DEFAULT_ORDER.stream()
.filter(sourceType -> !sourceType.equals(firstType)).collect(() -> tempList, List::add, List::addAll)
.stream().map(sourceMap::get).filter(Objects::nonNull).collect(Collectors.toList());

StringBuilder orderInfo = new StringBuilder("properties search order:");
for (int i = 0; i < collect.size(); i++) {
final AbstractPropertySource abstractPropertySource = collect.get(i);
orderInfo.append(abstractPropertySource.getType().toString());
if (i < collect.size() - 1) {
orderInfo.append("->");
}
}
LOGGER.info(orderInfo.toString());

return collect;
return SEARCH_ORDER.stream().map(sourceMap::get).collect(Collectors.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@

package com.alibaba.nacos.client.logging;

import com.alibaba.nacos.client.constant.Constants;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.common.utils.ConvertUtils;
import com.alibaba.nacos.common.utils.StringUtils;

import java.io.File;

/**
* Abstract nacos logging.
*
Expand All @@ -35,16 +32,6 @@ public abstract class AbstractNacosLogging {

private static final String NACOS_LOGGING_DEFAULT_CONFIG_ENABLED_PROPERTY = "nacos.logging.default.config.enabled";

private static final String NACOS_LOGGING_PATH_DIR = "logs";

static {
String loggingPath = NacosClientProperties.PROTOTYPE.getProperty(Constants.SysEnv.JM_LOG_PATH);
if (StringUtils.isBlank(loggingPath)) {
String userHome = NacosClientProperties.PROTOTYPE.getProperty(Constants.SysEnv.USER_HOME);
NacosClientProperties.PROTOTYPE.setProperty(Constants.SysEnv.JM_LOG_PATH, userHome + File.separator + NACOS_LOGGING_PATH_DIR);
}
}

protected String getLocation(String defaultLocation) {
String location = NacosClientProperties.PROTOTYPE.getProperty(NACOS_LOGGING_CONFIG_PROPERTY);
if (StringUtils.isBlank(location)) {
Expand Down
22 changes: 13 additions & 9 deletions client/src/main/resources/nacos-log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
-->

<Configuration status="WARN">
<Properties>
<Property name="userHome" value="${nacosClientProperty:user.home}"/>
<Property name="logPath" value="${nacosClientProperty:JM.LOG.PATH:-${userHome}/logs}"/>
</Properties>
<Appenders>
<RollingFile name="CONFIG_LOG_FILE" fileName="${nacosClientProperty:JM.LOG.PATH}/nacos/config.log"
filePattern="${nacosClientProperty:JM.LOG.PATH}/nacos/config.log.%d{yyyy-MM-dd}.%i">
<RollingFile name="CONFIG_LOG_FILE" fileName="${logPath}/nacos/config.log"
filePattern="${logPath}/nacos/config.log.%d{yyyy-MM-dd}.%i">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n</Pattern>
</PatternLayout>
Expand All @@ -29,15 +33,15 @@
</Policies>

<DefaultRolloverStrategy max="${nacosClientProperty:JM.LOG.RETAIN.COUNT:-7}">
<Delete basePath="${nacosClientProperty:JM.LOG.PATH}/nacos" maxDepth="1" testMode="${nacosClientProperty:JM.LOG.RETAIN.DURATION.OFF:-true}">
<Delete basePath="${logPath}/nacos" maxDepth="1" testMode="${nacosClientProperty:JM.LOG.RETAIN.DURATION.OFF:-true}">
<IfFileName glob="config.log.*.*" />
<IfLastModified age="${nacosClientProperty:JM.LOG.RETAIN.DURATION:-P180D}" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>

<RollingFile name="REMOTE_LOG_FILE" fileName="${nacosClientProperty:JM.LOG.PATH}/nacos/remote.log"
filePattern="${nacosClientProperty:JM.LOG.PATH}/nacos/remote.log.%d{yyyy-MM-dd}.%i">
<RollingFile name="REMOTE_LOG_FILE" fileName="${logPath}/nacos/remote.log"
filePattern="${logPath}/nacos/remote.log.%d{yyyy-MM-dd}.%i">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n</Pattern>
</PatternLayout>
Expand All @@ -48,15 +52,15 @@
</Policies>

<DefaultRolloverStrategy max="${nacosClientProperty:JM.LOG.RETAIN.COUNT:-7}">
<Delete basePath="${nacosClientProperty:JM.LOG.PATH}/nacos" maxDepth="1" testMode="${nacosClientProperty:JM.LOG.RETAIN.DURATION.OFF:-true}">
<Delete basePath="${logPath}/nacos" maxDepth="1" testMode="${nacosClientProperty:JM.LOG.RETAIN.DURATION.OFF:-true}">
<IfFileName glob="remote.log.*.*" />
<IfLastModified age="${nacosClientProperty:JM.LOG.RETAIN.DURATION:-P180D}" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>

<RollingFile name="NAMING_LOG_FILE" fileName="${nacosClientProperty:JM.LOG.PATH}/nacos/naming.log"
filePattern="${nacosClientProperty:JM.LOG.PATH}/nacos/naming.log.%d{yyyy-MM-dd}.%i">
<RollingFile name="NAMING_LOG_FILE" fileName="${logPath}/nacos/naming.log"
filePattern="${logPath}/nacos/naming.log.%d{yyyy-MM-dd}.%i">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n</Pattern>
</PatternLayout>
Expand All @@ -67,7 +71,7 @@
</Policies>

<DefaultRolloverStrategy max="${nacosClientProperty:JM.LOG.RETAIN.COUNT:-7}">
<Delete basePath="${nacosClientProperty:JM.LOG.PATH}/nacos" maxDepth="1" testMode="${nacosClientProperty:JM.LOG.RETAIN.DURATION.OFF:-true}">
<Delete basePath="${logPath}/nacos" maxDepth="1" testMode="${nacosClientProperty:JM.LOG.RETAIN.DURATION.OFF:-true}">
<IfFileName glob="naming.log.*.*" />
<IfLastModified age="${nacosClientProperty:JM.LOG.RETAIN.DURATION:-P180D}" />
</Delete>
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/resources/nacos-logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
-->

<configuration debug="false" scan="false" packagingData="true">
<nacosClientProperty scope="context" name="logPath" source="JM.LOG.PATH"/>
<nacosClientProperty scope="context" name="logPath" source="JM.LOG.PATH" defaultValue="${user.home}/logs"/>
<nacosClientProperty scope="context" name="logRetainCount" source="JM.LOG.RETAIN.COUNT" defaultValue="7"/>
<nacosClientProperty scope="context" name="logFileSize" source="JM.LOG.FILE.SIZE" defaultValue="10MB"/>
<nacosClientProperty scope="context" name="nacosConfigLogLevel" source="com.alibaba.nacos.config.log.level" defaultValue="info"/>
Expand Down