Skip to content

Commit

Permalink
optimize config operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewAden committed Dec 30, 2024
1 parent 762303b commit 4ac2eba
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
package com.alibaba.nacos.config.server.aspect;

import com.alibaba.nacos.api.config.remote.request.ConfigBatchListenRequest;
import com.alibaba.nacos.api.config.remote.request.ConfigPublishRequest;
import com.alibaba.nacos.api.config.remote.request.ConfigQueryRequest;
import com.alibaba.nacos.api.config.remote.request.ConfigRemoveRequest;
import com.alibaba.nacos.api.remote.request.Request;
import com.alibaba.nacos.api.remote.request.RequestMeta;
import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.model.ConfigRequestInfo;
import com.alibaba.nacos.config.server.model.form.ConfigForm;
import com.alibaba.nacos.config.server.model.gray.BetaGrayRule;
import com.alibaba.nacos.config.server.monitor.MetricsMonitor;
import com.alibaba.nacos.config.server.service.ConfigCacheService;
import com.alibaba.nacos.config.server.service.query.model.ConfigQueryChainRequest;
import com.alibaba.nacos.config.server.utils.GroupKey2;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.config.server.utils.RequestUtil;
Expand All @@ -35,13 +35,11 @@
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
* * Created with IntelliJ IDEA. User: dingjoey Date: 13-12-12 Time: 21:12 client api && sdk api 请求日志打点逻辑
* Aspect for logging HTTP API and SDK API requests in Nacos.
*
* @author Nacos
*/
Expand All @@ -52,183 +50,135 @@ public class RequestLogAspect {
/**
* Publish config.
*/
private static final String CLIENT_INTERFACE_PUBLISH_SINGLE_CONFIG =
"execution(* com.alibaba.nacos.config.server.controller.ConfigController.publishConfig(..)) && args"
+ "(request,response,dataId,group,tenant,content,..)";

/**
* Publish config.
*/
private static final String CLIENT_INTERFACE_PUBLISH_SINGLE_CONFIG_RPC =
"execution(* com.alibaba.nacos.core.remote.RequestHandler.handleRequest(..)) "
+ "&& target(com.alibaba.nacos.config.server.remote.ConfigPublishRequestHandler) "
+ "&& args(request,meta)";
private static final String PUBLISH_CONFIG =
"execution(* com.alibaba.nacos.config.server.service.ConfigOperationService.publishConfig(..))";

/**
* Get config.
*/
private static final String CLIENT_INTERFACE_GET_CONFIG =
"execution(* com.alibaba.nacos.config.server.controller.ConfigController.getConfig(..)) && args(request,"
+ "response,dataId,group,tenant,..)";

/**
* Get config.
*/
@SuppressWarnings("checkstyle:linelength")
private static final String CLIENT_INTERFACE_GET_CONFIG_RPC =
"execution(* com.alibaba.nacos.core.remote.RequestHandler.handleRequest(..)) "
+ " && target(com.alibaba.nacos.config.server.remote.ConfigQueryRequestHandler) && args(request,meta)";

/**
* Remove config.
*/
private static final String CLIENT_INTERFACE_REMOVE_ALL_CONFIG =
"execution(* com.alibaba.nacos.config.server.controller.ConfigController.deleteConfig(..)) && args(request,"
+ "response,dataId,group,tenant,..)";
private static final String GET_CONFIG =
"execution(* com.alibaba.nacos.config.server.service.query.ConfigQueryChainService.handle(..))";

/**
* Remove config.
* Delete config.
*/
@SuppressWarnings("checkstyle:linelength")
private static final String CLIENT_INTERFACE_REMOVE_ALL_CONFIG_RPC =
"execution(* com.alibaba.nacos.core.remote.RequestHandler.handleRequest(..)) "
+ " && target(com.alibaba.nacos.config.server.remote.ConfigRemoveRequestHandler) && args(request,meta)";
private static final String DELETE_CONFIG =
"execution(* com.alibaba.nacos.config.server.service.ConfigOperationService.deleteConfig(..))";

/**
* Remove config.
* Listen for config changes.
*/
@SuppressWarnings("checkstyle:linelength")
private static final String CLIENT_INTERFACE_LISTEN_CONFIG_RPC =
private static final String CONFIG_CHANGE_LISTEN_RPC =
"execution(* com.alibaba.nacos.core.remote.RequestHandler.handleRequest(..)) "
+ " && target(com.alibaba.nacos.config.server.remote.ConfigChangeBatchListenRequestHandler) && args(request,meta)";


/**
* PublishSingle.
*/
@Around(CLIENT_INTERFACE_PUBLISH_SINGLE_CONFIG_RPC)
public Object interfacePublishSingleRpc(ProceedingJoinPoint pjp, ConfigPublishRequest request, RequestMeta meta)
throws Throwable {
final String md5 =
request.getContent() == null ? null : MD5Utils.md5Hex(request.getContent(), Constants.ENCODE);
* Intercepts configuration publishing operations, records metrics, and logs client requests.
*/
@Around(PUBLISH_CONFIG)
public Object interfacePublishConfig(ProceedingJoinPoint pjp) throws Throwable {
Object[] args = pjp.getArgs();
ConfigForm configForm = (ConfigForm) args[0];
ConfigRequestInfo configRequestInfo = (ConfigRequestInfo) args[1];

String dataId = configForm.getDataId();
String group = configForm.getGroup();
String namespaceId = configForm.getNamespaceId();
String content = configForm.getContent();
String requestIp = configRequestInfo.getSrcIp();
String md5 = content == null ? null : MD5Utils.md5Hex(content, Constants.ENCODE);

MetricsMonitor.getPublishMonitor().incrementAndGet();
AtomicLong rtHolder = new AtomicLong();
Object retVal = logClientRequestRpc("publish", pjp, request, meta, request.getDataId(), request.getGroup(),
request.getTenant(), md5, rtHolder);
MetricsMonitor.getWriteConfigRpcRtTimer().record(rtHolder.get(), TimeUnit.MILLISECONDS);
return retVal;
}

/**
* PublishSingle.
*/
@Around(CLIENT_INTERFACE_PUBLISH_SINGLE_CONFIG)
public Object interfacePublishSingle(ProceedingJoinPoint pjp, HttpServletRequest request,
HttpServletResponse response, String dataId, String group, String tenant, String content) throws Throwable {
final String md5 = content == null ? null : MD5Utils.md5Hex(content, Constants.ENCODE);
MetricsMonitor.getPublishMonitor().incrementAndGet();
AtomicLong rtHolder = new AtomicLong();
Object retVal = logClientRequest("publish", pjp, request, response, dataId, group, tenant, md5, rtHolder);
Object retVal = logClientRequest("publish", pjp, dataId, group, namespaceId, requestIp, md5, rtHolder);
MetricsMonitor.getWriteConfigRtTimer().record(rtHolder.get(), TimeUnit.MILLISECONDS);

return retVal;
}

/**
* RemoveAll.
*/
@Around(CLIENT_INTERFACE_REMOVE_ALL_CONFIG)
public Object interfaceRemoveAll(ProceedingJoinPoint pjp, HttpServletRequest request, HttpServletResponse response,
String dataId, String group, String tenant) throws Throwable {
return logClientRequest("remove", pjp, request, response, dataId, group, tenant, null, null);
}

/**
* RemoveAll.
* Intercepts configuration get operations, records metrics, and logs client requests.
*/
@Around(CLIENT_INTERFACE_REMOVE_ALL_CONFIG_RPC)
public Object interfaceRemoveAllRpc(ProceedingJoinPoint pjp, ConfigRemoveRequest request, RequestMeta meta)
throws Throwable {
return logClientRequestRpc("remove", pjp, request, meta, request.getDataId(), request.getGroup(),
request.getTenant(), null, null);
}

/**
* GetConfig.
*/
@Around(CLIENT_INTERFACE_GET_CONFIG)
public Object interfaceGetConfig(ProceedingJoinPoint pjp, HttpServletRequest request, HttpServletResponse response,
String dataId, String group, String tenant) throws Throwable {
final String groupKey = GroupKey2.getKey(dataId, group, tenant);
final String md5 = ConfigCacheService.getContentMd5(groupKey);
@Around(GET_CONFIG)
public Object interfaceGetConfig(ProceedingJoinPoint pjp) throws Throwable {
Object[] args = pjp.getArgs();
ConfigQueryChainRequest chainRequest = (ConfigQueryChainRequest) args[0];

String dataId = chainRequest.getDataId();
String group = chainRequest.getGroup();
String tenant = chainRequest.getTenant();
String requestIp = null;
if (chainRequest.getAppLabels() != null) {
requestIp = chainRequest.getAppLabels().getOrDefault(BetaGrayRule.CLIENT_IP_LABEL, null);
}
String groupKey = GroupKey2.getKey(dataId, group, tenant);
String md5 = ConfigCacheService.getContentMd5(groupKey);

MetricsMonitor.getConfigMonitor().incrementAndGet();
AtomicLong rtHolder = new AtomicLong();
Object retVal = logClientRequest("get", pjp, request, response, dataId, group, tenant, md5, rtHolder);
Object retVal = logClientRequest("get", pjp, dataId, group, tenant, requestIp, md5, rtHolder);
MetricsMonitor.getReadConfigRtTimer().record(rtHolder.get(), TimeUnit.MILLISECONDS);

return retVal;
}

/**
* GetConfig.
* Deletes a configuration entry and logs the operation.
*/
@Around(CLIENT_INTERFACE_GET_CONFIG_RPC)
public Object interfaceGetConfigRpc(ProceedingJoinPoint pjp, ConfigQueryRequest request, RequestMeta meta)
throws Throwable {
final String groupKey = GroupKey2.getKey(request.getDataId(), request.getGroup(), request.getTenant());
final String md5 = ConfigCacheService.getContentMd5(groupKey);
@Around(DELETE_CONFIG)
public Object interfaceRemoveConfig(ProceedingJoinPoint pjp) throws Throwable {
Object[] args = pjp.getArgs();
String dataId = (String) args[0];
String group = (String) args[1];
String tenant = (String) args[2];
String clientIp = (String) args[4];
String groupKey = GroupKey2.getKey(dataId, group, tenant);
String md5 = ConfigCacheService.getContentMd5(groupKey);

MetricsMonitor.getConfigMonitor().incrementAndGet();
AtomicLong rtHolder = new AtomicLong();
Object retVal = logClientRequestRpc("get", pjp, request, meta, request.getDataId(), request.getGroup(),
request.getTenant(), md5, rtHolder);
MetricsMonitor.getReadConfigRpcRtTimer().record(rtHolder.get(), TimeUnit.MILLISECONDS);
return retVal;
}

/**
* Client api request log rt | status | requestIp | opType | dataId | group | datumId | md5.
*/
private Object logClientRequest(String requestType, ProceedingJoinPoint pjp, HttpServletRequest request,
HttpServletResponse response, String dataId, String group, String tenant, String md5, AtomicLong rtHolder) throws Throwable {
final String requestIp = RequestUtil.getRemoteIp(request);
String appName = request.getHeader(RequestUtil.CLIENT_APPNAME_HEADER);
final long st = System.currentTimeMillis();
Object retVal = pjp.proceed();
final long rt = System.currentTimeMillis() - st;
if (rtHolder != null) {
rtHolder.set(rt);
}
// rt | status | requestIp | opType | dataId | group | datumId | md5 |
// appName
LogUtil.CLIENT_LOG
.info("{}|{}|{}|{}|{}|{}|{}|{}|{}", rt, retVal, requestIp, requestType, dataId, group, tenant, md5,
appName);
Object retVal = logClientRequest("delete", pjp, dataId, group, tenant, clientIp, md5, rtHolder);
MetricsMonitor.getReadConfigRtTimer().record(rtHolder.get(), TimeUnit.MILLISECONDS);

return retVal;
}

/**
* Client api request log rt | status | requestIp | opType | dataId | group | datumId | md5.
*/
private Object logClientRequestRpc(String requestType, ProceedingJoinPoint pjp, Request request, RequestMeta meta,
String dataId, String group, String tenant, String md5, AtomicLong rtHolder) throws Throwable {
final String requestIp = meta.getClientIp();
String appName = request.getHeader(RequestUtil.CLIENT_APPNAME_HEADER);
final long st = System.currentTimeMillis();
Response retVal = (Response) pjp.proceed();
final long rt = System.currentTimeMillis() - st;
if (rtHolder != null) {
rtHolder.set(rt);
private Object logClientRequest(String requestType, ProceedingJoinPoint pjp, String dataId, String group,
String tenant, String requestIp, String md5, AtomicLong rtHolder) throws Throwable {
long startTime = System.currentTimeMillis();
try {
Object retVal = pjp.proceed();

long rt = System.currentTimeMillis() - startTime;
if (rtHolder != null) {
rtHolder.set(rt);
}

LogUtil.CLIENT_LOG.info("opType: {} | rt: {}ms | status: success | requestIp: {} | dataId: {} | group: {} | tenant: {} | md5: {}",
requestType, rt, requestIp, dataId, group, tenant, md5);

return retVal;

} catch (Throwable e) {
long rt = System.currentTimeMillis() - startTime;
if (rtHolder != null) {
rtHolder.set(rt);
}

LogUtil.CLIENT_LOG.error("opType: {} | rt: {}ms | status: failure | requestIp: {} | dataId: {} | group: {} | tenant: {} | md5: {}",
requestType, rt, requestIp, dataId, group, tenant, md5);

throw e;
}
// rt | status | requestIp | opType | dataId | group | datumId | md5 |
// appName
LogUtil.CLIENT_LOG.info("{}|{}|{}|{}|{}|{}|{}|{}|{}", rt,
retVal.isSuccess() ? retVal.getResultCode() : retVal.getErrorCode(), requestIp, requestType, dataId,
group, tenant, md5, appName);
return retVal;
}

/**
* GetConfig.
* Handles configuration change listening requests.
*/
@Around(CLIENT_INTERFACE_LISTEN_CONFIG_RPC)
@Around(CONFIG_CHANGE_LISTEN_RPC)
public Object interfaceListenConfigRpc(ProceedingJoinPoint pjp, ConfigBatchListenRequest request,
RequestMeta meta) throws Throwable {
MetricsMonitor.getConfigMonitor().incrementAndGet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,34 +303,15 @@ public Boolean deleteConfig(HttpServletRequest request, HttpServletResponse resp
}

/**
* Execute delete config operation.
*
* @return java.lang.Boolean
* @author klw
* @Description: delete configuration based on multiple config ids
* @Date 2019/7/5 10:26
* @Param [request, response, dataId, group, tenant, tag]
* delete configs based on the IDs list.
*/
@DeleteMapping(params = "delType=ids")
@Secured(action = ActionTypes.WRITE, signType = SignType.CONFIG)
public RestResult<Boolean> deleteConfigs(HttpServletRequest request, @RequestParam(value = "ids") List<Long> ids) {
String clientIp = RequestUtil.getRemoteIp(request);
String srcUser = RequestUtil.getSrcUserName(request);
final Timestamp time = TimeUtils.getCurrentTime();
List<ConfigAllInfo> configInfoList = configInfoPersistService.removeConfigInfoByIds(ids, clientIp, srcUser);
if (CollectionUtils.isEmpty(configInfoList)) {
return RestResultUtils.success(true);
}
for (ConfigAllInfo configInfo : configInfoList) {
ConfigChangePublisher.notifyConfigChange(
new ConfigDataChangeEvent(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(),
time.getTime()));

ConfigTraceService.logPersistenceEvent(configInfo.getDataId(), configInfo.getGroup(),
configInfo.getTenant(), null, time.getTime(), clientIp, ConfigTraceService.PERSISTENCE_EVENT,
ConfigTraceService.PERSISTENCE_TYPE_REMOVE, null);
}
return RestResultUtils.success(true);

return RestResultUtils.success(configOperationService.deleteConfigs(ids, clientIp, srcUser));
}

@GetMapping("/catalog")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,11 @@ public static Timer getReadConfigRtTimer() {
.timer(METER_REGISTRY, "nacos_timer", "module", "config", "name", "readConfigRt");
}

public static Timer getReadConfigRpcRtTimer() {
return NacosMeterRegistryCenter
.timer(METER_REGISTRY, "nacos_timer", "module", "config", "name", "readConfigRpcRt");
}

public static Timer getWriteConfigRtTimer() {
return NacosMeterRegistryCenter
.timer(METER_REGISTRY, "nacos_timer", "module", "config", "name", "writeConfigRt");
}

public static Timer getWriteConfigRpcRtTimer() {
return NacosMeterRegistryCenter
.timer(METER_REGISTRY, "nacos_timer", "module", "config", "name", "writeConfigRpcRt");
}

public static Timer getNotifyRtTimer() {
return NacosMeterRegistryCenter.timer(METER_REGISTRY, "nacos_timer", "module", "config", "name", "notifyRt");
}
Expand Down
Loading

0 comments on commit 4ac2eba

Please sign in to comment.