Skip to content

Commit

Permalink
Merge pull request #11650 from alibaba/v1.x-develop
Browse files Browse the repository at this point in the history
Merged from v1.x-develop
  • Loading branch information
KomachiSion authored Jan 15, 2024
2 parents 08e3507 + cb72428 commit bd22667
Show file tree
Hide file tree
Showing 19 changed files with 409 additions and 46 deletions.
5 changes: 5 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/PropertyKeyConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public class PropertyKeyConst {

public static final String PUSH_RECEIVER_UDP_PORT = "push.receiver.udp.port";

/**
* Since 1.4.7, For some situation like java agent using nacos-client which can't use env ram info.
*/
public static final String IS_USE_RAM_INFO_PARSING = "isUseRamInfoParsing";

/**
* Get the key value of some variable value from the system property.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public interface SystemPropertyKeyConst {
* It is also supported by the -D parameter.
*/
String IS_USE_ENDPOINT_PARSING_RULE = "nacos.use.endpoint.parsing.rule";

/**
* Since 1.4.7, For some situation like java agent using nacos-client which can't use env ram info.
*/
String IS_USE_RAM_INFO_PARSING = "nacos.use.ram.info.parsing";
}
5 changes: 5 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 @@ -196,4 +196,9 @@ public class Constants {

public static final String ALL_PATTERN = "*";

/**
* Since 1.4.7, For some situation like java agent using nacos-client which can't use env ram info.
*/
public static final String DEFAULT_USE_RAM_INFO_PARSING = "true";

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.alibaba.nacos.client.config.utils.ParamUtils;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.PreInitUtils;
import com.alibaba.nacos.client.utils.ValidatorUtils;
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.utils.StringUtils;
Expand Down Expand Up @@ -75,6 +76,7 @@ public class NacosConfigService implements ConfigService {
private final ConfigFilterChainManager configFilterChainManager;

public NacosConfigService(Properties properties) throws NacosException {
PreInitUtils.asyncPreLoadCostComponent();
ValidatorUtils.checkInitParam(properties);
String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE);
if (StringUtils.isBlank(encodeTmp)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.alibaba.nacos.client.utils.ContextPathUtil;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.RamUtil;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.http.HttpClientConfig;
Expand Down Expand Up @@ -322,19 +323,8 @@ private void initAkSk(Properties properties) {
StsConfig.getInstance().setRamRoleName(ramRoleName);
}

String ak = properties.getProperty(PropertyKeyConst.ACCESS_KEY);
if (StringUtils.isBlank(ak)) {
accessKey = SpasAdapter.getAk();
} else {
accessKey = ak;
}

String sk = properties.getProperty(PropertyKeyConst.SECRET_KEY);
if (StringUtils.isBlank(sk)) {
secretKey = SpasAdapter.getSk();
} else {
secretKey = sk;
}
accessKey = RamUtil.getAccessKey(properties);
secretKey = RamUtil.getSecretKey(properties);
}

private void initMaxRetry(Properties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,21 @@ void checkListenerMd5() {
}
}
}

private static String getTrace(StackTraceElement[] stackTrace, int traceDeep) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("\n");
int deep = 0;
for (StackTraceElement element : stackTrace) {
stringBuilder.append("\tat ").append(element).append("\n");
deep++;
if (traceDeep > 0 && deep > traceDeep) {
stringBuilder.append("\tat ... \n");
break;
}
}
return stringBuilder.toString();
}

private void safeNotifyListener(final String dataId, final String group, final String content, final String type,
final String md5, final String encryptedDataKey, final ManagerListenerWrap listenerWrap) {
Expand Down Expand Up @@ -226,7 +241,7 @@ public void run() {
name, dataId, group, md5, listener, ex.getErrCode(), ex.getErrMsg());
} catch (Throwable t) {
LOGGER.error("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} tx={}", name, dataId,
group, md5, listener, t.getCause());
group, md5, listener, getTrace(t.getStackTrace(), 3));
} finally {
Thread.currentThread().setContextClassLoader(myClassLoader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ public synchronized void start() throws NacosException {
GetServerListTask getServersTask = new GetServerListTask(addressServerUrl);
for (int i = 0; i < initServerlistRetryTimes && serverUrls.isEmpty(); ++i) {
getServersTask.run();
if (!serverUrls.isEmpty()) {
break;
}
try {
this.wait((i + 1) * 100L);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.InitUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.client.utils.PreInitUtils;
import com.alibaba.nacos.client.utils.ValidatorUtils;
import com.alibaba.nacos.common.utils.ConvertUtils;
import com.alibaba.nacos.common.utils.StringUtils;
Expand Down Expand Up @@ -82,6 +83,7 @@ public NacosNamingService(Properties properties) throws NacosException {
}

private void init(Properties properties) throws NacosException {
PreInitUtils.asyncPreLoadCostComponent();
ValidatorUtils.checkInitParam(properties);
this.namespace = InitUtils.initNamespaceForNaming(properties);
InitUtils.initSerialization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.client.security.SecurityProxy;
import com.alibaba.nacos.client.utils.AppNameUtils;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.client.utils.RamUtil;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
Expand Down Expand Up @@ -70,7 +70,6 @@
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
Expand Down Expand Up @@ -748,33 +747,16 @@ private void injectRoleName() {

public String getAccessKey() {
if (properties == null) {

return SpasAdapter.getAk();
}

return TemplateUtils
.stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.ACCESS_KEY), new Callable<String>() {

@Override
public String call() {
return SpasAdapter.getAk();
}
});
return RamUtil.getAccessKey(properties);
}

public String getSecretKey() {
if (properties == null) {

return SpasAdapter.getSk();
}

return TemplateUtils
.stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.SECRET_KEY), new Callable<String>() {
@Override
public String call() throws Exception {
return SpasAdapter.getSk();
}
});
return RamUtil.getSecretKey(properties);
}

public void setProperties(Properties properties) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.client.utils;

import com.alibaba.nacos.client.config.impl.SpasAdapter;
import com.alibaba.nacos.common.utils.JacksonUtils;

/**
* Async do pre init to load some cost component.
*
* <ul>
* <li>JacksonUtil</li>
* <li>SpasAdapter</li>
* </ul>
*
* @author xiweng.yy
*/
public class PreInitUtils {

/**
* Async pre load cost component.
*/
@SuppressWarnings("PMD.AvoidManuallyCreateThreadRule")
public static void asyncPreLoadCostComponent() {
Thread preLoadThread = new Thread(new Runnable() {
@Override
public void run() {
// Jackson util will init static {@code ObjectMapper}, which will cost hundreds milliseconds.
JacksonUtils.createEmptyJsonNode();
// Ram auth plugin will try to get credential from env and system when leak input identity by properties.
SpasAdapter.getAk();
}
});
preLoadThread.start();
}
}
59 changes: 59 additions & 0 deletions client/src/main/java/com/alibaba/nacos/client/utils/RamUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.client.utils;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.client.config.impl.SpasAdapter;
import com.alibaba.nacos.common.utils.StringUtils;

import java.util.Properties;

/**
* Util to get ram info, such as AK, SK and RAM role.
*
* @author xiweng.yy
*/
public class RamUtil {

public static String getAccessKey(Properties properties) {
boolean isUseRamInfoParsing = Boolean.parseBoolean(properties
.getProperty(PropertyKeyConst.IS_USE_RAM_INFO_PARSING,
System.getProperty(SystemPropertyKeyConst.IS_USE_RAM_INFO_PARSING,
Constants.DEFAULT_USE_RAM_INFO_PARSING)));

String result = properties.getProperty(PropertyKeyConst.ACCESS_KEY);
if (isUseRamInfoParsing && StringUtils.isBlank(result)) {
result = SpasAdapter.getAk();
}
return result;
}

public static String getSecretKey(Properties properties) {
boolean isUseRamInfoParsing = Boolean.parseBoolean(properties
.getProperty(PropertyKeyConst.IS_USE_RAM_INFO_PARSING,
System.getProperty(SystemPropertyKeyConst.IS_USE_RAM_INFO_PARSING,
Constants.DEFAULT_USE_RAM_INFO_PARSING)));

String result = properties.getProperty(PropertyKeyConst.SECRET_KEY);
if (isUseRamInfoParsing && StringUtils.isBlank(result)) {
result = SpasAdapter.getSk();
}
return result;
}
}
29 changes: 22 additions & 7 deletions client/src/main/resources/nacos-log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
-->

<Configuration status="WARN">
<Properties>
<Property name="userHome" value="${sys:user.home}"/>
<Property name="logPath" value="${sys:JM.LOG.PATH:-${userHome}}"/>
</Properties>
<Appenders>
<RollingFile name="CONFIG_LOG_FILE" fileName="${sys:JM.LOG.PATH}/nacos/config.log"
filePattern="${sys:JM.LOG.PATH}/nacos/config.log.%d{yyyy-MM-dd}.%i">
Expand All @@ -28,7 +32,12 @@
<SizeBasedTriggeringPolicy size="${sys:JM.LOG.FILE.SIZE:-10MB}"/>
</Policies>

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

<Async name="ASYNC_CONFIG" bufferSize="1024" blocking="false">
Expand All @@ -46,14 +55,20 @@
<SizeBasedTriggeringPolicy size="${sys:JM.LOG.FILE.SIZE:-10MB}"/>
</Policies>

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

<Async name="ASYNC_NAMING" bufferSize="1024" blocking="false">
<AppenderRef ref="NAMING_LOG_FILE"/>
</Async>
</Appenders>

<Async name="ASYNC_NAMING" bufferSize="1024" blocking="false">
<AppenderRef ref="NAMING_LOG_FILE"/>
</Async>



<Loggers>
<Logger name="com.alibaba.nacos.client" level="${sys:com.alibaba.nacos.config.log.level:-info}"
additivity="false">
Expand Down
Loading

0 comments on commit bd22667

Please sign in to comment.