Skip to content

Commit

Permalink
V3.0 develop unified defualt namespace (#12887)
Browse files Browse the repository at this point in the history
* Unified default namespace id for APIs.

* Unified default namespace id for console ui.

* Fix nacos-client get config frequently when listen config use empty namespace id.

* Unified default namespace id for nacos-client.
  • Loading branch information
KomachiSion authored Nov 22, 2024
1 parent f947cc8 commit fdedaa2
Show file tree
Hide file tree
Showing 30 changed files with 115 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public static String parseNamespace(NacosClientProperties properties) {
if (StringUtils.isBlank(namespaceTmp)) {
namespaceTmp = properties.getProperty(PropertyKeyConst.NAMESPACE);
}
return StringUtils.isNotBlank(namespaceTmp) ? namespaceTmp.trim() : StringUtils.EMPTY;
return StringUtils.isNotBlank(namespaceTmp) ? namespaceTmp.trim() : Constants.DEFAULT_NAMESPACE_ID;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.nacos.common.utils;

import com.alibaba.nacos.api.common.Constants;

/**
* namespace(tenant) util. Because config and naming treat namespace(tenant) differently, this tool class can only be
* used by the config module.
Expand All @@ -24,28 +26,23 @@
* @date 2020/10/12 17:56
*/
public class NamespaceUtil {

private NamespaceUtil() {
}

private static final String NAMESPACE_PUBLIC_KEY = "public";

/**
* public id,默认值为 "".
* public id,默认值为 "public".
*/
public static String namespaceDefaultId = "";

private static final String NAMESPACE_NULL_KEY = "null";
public static String namespaceDefaultId = Constants.DEFAULT_NAMESPACE_ID;

/**
* Treat the namespace(tenant) parameters with values of "public" and "null" as an empty string.
* Treat the namespace(tenant) parameters with values of empty string as "public".
*
* @param tenant namespace(tenant) id
* @return java.lang.String A namespace(tenant) string processed
*/
public static String processNamespaceParameter(String tenant) {
if (StringUtils.isBlank(tenant) || NAMESPACE_PUBLIC_KEY.equalsIgnoreCase(tenant)
|| NAMESPACE_NULL_KEY.equalsIgnoreCase(tenant)) {
if (StringUtils.isBlank(tenant)) {
return getNamespaceDefaultId();
}
return tenant.trim();
Expand All @@ -68,4 +65,14 @@ public static void setNamespaceDefaultId(String namespaceDefaultId) {
public static String getNamespaceDefaultId() {
return NamespaceUtil.namespaceDefaultId;
}

/**
* Judge whether is default namespaceId.
*
* @param namespaceId to check namespaceId
* @return {@code true} if equals default namespaceId, otherwise {@code false}.
*/
public static boolean isDefaultNamespaceId(String namespaceId) {
return StringUtils.equals(namespaceId, getNamespaceDefaultId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package com.alibaba.nacos.common.utils;

import com.alibaba.nacos.api.common.Constants;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* test NamespaceUtil.
Expand All @@ -31,18 +34,18 @@ class NamespaceUtilTest {

@AfterEach
void tearDown() {
NamespaceUtil.setNamespaceDefaultId("");
NamespaceUtil.setNamespaceDefaultId(Constants.DEFAULT_NAMESPACE_ID);
}

@Test
void testProcessTenantParameter() {
String strPublic = "public";
assertEquals(strPublic, NamespaceUtil.processNamespaceParameter(strPublic));
String strEmpty = "";
assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strPublic));
String strNull = "null";
assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strNull));
assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strEmpty));
assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(null));
assertEquals(strNull, NamespaceUtil.processNamespaceParameter(strNull));
assertEquals(strPublic, NamespaceUtil.processNamespaceParameter(strEmpty));
assertEquals(strPublic, NamespaceUtil.processNamespaceParameter(null));
String strAbc = "abc";
assertEquals(strAbc, NamespaceUtil.processNamespaceParameter(strAbc));
String strdef123 = "def123";
Expand All @@ -53,7 +56,9 @@ void testProcessTenantParameter() {

@Test
void testSetNamespaceDefaultId() {
assertTrue(NamespaceUtil.isDefaultNamespaceId(Constants.DEFAULT_NAMESPACE_ID));
NamespaceUtil.setNamespaceDefaultId("Deprecated");
assertEquals("Deprecated", NamespaceUtil.getNamespaceDefaultId());
assertFalse(NamespaceUtil.isDefaultNamespaceId(Constants.DEFAULT_NAMESPACE_ID));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.utils.NamespaceUtil;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.paramcheck.ConfigDefaultHttpParamExtractor;
Expand Down Expand Up @@ -94,6 +95,7 @@ public ResponseEntity metric(@RequestParam("ip") String ip,
@RequestParam(value = "tenant", required = false) String tenant) throws NacosException {

ParamUtils.checkTenant(tenant);
tenant = NamespaceUtil.processNamespaceParameter(tenant);
ParamUtils.checkParam(dataId, group, "default", "default");

Loggers.CORE.info("Get cluster config metrics received, ip={},dataId={},group={},tenant={}", ip, dataId, group,
Expand Down Expand Up @@ -181,6 +183,7 @@ public Map<String, Object> getClientMetrics(@RequestParam("ip") String ip,
@RequestParam(value = "tenant", required = false) String tenant) throws NacosException {

ParamUtils.checkTenant(tenant);
tenant = NamespaceUtil.processNamespaceParameter(tenant);
ParamUtils.checkParam(dataId, group, "default", "default");

Map<String, Object> metrics = new HashMap<>(16);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.config.server.controller;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.NamespaceUtil;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.model.SampleResult;
Expand Down Expand Up @@ -74,6 +75,7 @@ public CommunicationController(LongPollingService longPollingService,
public SampleResult getSubClientConfig(@RequestParam("dataId") String dataId, @RequestParam("group") String group,
@RequestParam(value = "tenant", required = false) String tenant, ModelMap modelMap) {
group = StringUtils.isBlank(group) ? Constants.DEFAULT_GROUP : group;
tenant = NamespaceUtil.processNamespaceParameter(tenant);
// long polling listeners.
SampleResult result = longPollingService.getCollectSubscribleInfo(dataId, group, tenant);
// rpc listeners.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public Boolean publishConfig(HttpServletRequest request, HttpServletResponse res
}

// check tenant
tenant = NamespaceUtil.processNamespaceParameter(tenant);
ParamUtils.checkTenant(tenant);
ParamUtils.checkParam(dataId, group, "datumId", content);
ParamUtils.checkParam(tag);
Expand Down Expand Up @@ -263,6 +264,7 @@ public ConfigAllInfo detailConfigInfo(@RequestParam("dataId") String dataId, @Re
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant)
throws NacosException {
// check tenant
tenant = NamespaceUtil.processNamespaceParameter(tenant);
ParamUtils.checkTenant(tenant);
// check params
ParamUtils.checkParam(dataId, group, "datumId", "content");
Expand Down Expand Up @@ -297,6 +299,7 @@ public Boolean deleteConfig(HttpServletRequest request, HttpServletResponse resp
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant,
@RequestParam(value = "tag", required = false) String tag) throws NacosException {
// check tenant
tenant = NamespaceUtil.processNamespaceParameter(tenant);
ParamUtils.checkTenant(tenant);
ParamUtils.checkParam(dataId, group, "datumId", "rm");
ParamUtils.checkParam(tag);
Expand Down Expand Up @@ -345,6 +348,7 @@ public RestResult<Boolean> deleteConfigs(HttpServletRequest request, @RequestPar
public RestResult<ConfigAdvanceInfo> getConfigAdvanceInfo(@RequestParam("dataId") String dataId,
@RequestParam("group") String group,
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant) {
tenant = NamespaceUtil.processNamespaceParameter(tenant);
ConfigAdvanceInfo configInfo = configInfoPersistService.findConfigAdvanceInfo(dataId, group, tenant);
return RestResultUtils.success(configInfo);
}
Expand Down Expand Up @@ -389,6 +393,7 @@ public GroupkeyListenserStatus getListeners(@RequestParam("dataId") String dataI
@RequestParam("group") String group, @RequestParam(value = "tenant", required = false) String tenant,
@RequestParam(value = "sampleTime", required = false, defaultValue = "1") int sampleTime) throws Exception {
group = StringUtils.isBlank(group) ? Constants.DEFAULT_GROUP : group;
tenant = NamespaceUtil.processNamespaceParameter(tenant);
SampleResult collectSampleResult = configSubService.getCollectSampleResult(dataId, group, tenant, sampleTime);
GroupkeyListenserStatus gls = new GroupkeyListenserStatus();
gls.setCollectStatus(200);
Expand Down Expand Up @@ -417,6 +422,7 @@ public Page<ConfigInfo> searchConfig(@RequestParam("dataId") String dataId, @Req
if (StringUtils.isNotBlank(configTags)) {
configAdvanceInfo.put("config_tags", configTags);
}
tenant = NamespaceUtil.processNamespaceParameter(tenant);
try {
return configInfoPersistService.findConfigInfo4Page(pageNo, pageSize, dataId, group, tenant,
configAdvanceInfo);
Expand Down Expand Up @@ -452,6 +458,7 @@ public Page<ConfigInfo> fuzzySearchConfig(@RequestParam("dataId") String dataId,
if (StringUtils.isNotBlank(types)) {
configAdvanceInfo.put(ParametersField.TYPES, types);
}
tenant = NamespaceUtil.processNamespaceParameter(tenant);
try {
return configInfoPersistService.findConfigInfoLike4Page(pageNo, pageSize, dataId, group, tenant,
configAdvanceInfo);
Expand All @@ -478,6 +485,7 @@ public RestResult<Boolean> stopBeta(HttpServletRequest httpServletRequest,
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant) {
String remoteIp = getRemoteIp(httpServletRequest);
String requestIpApp = RequestUtil.getAppName(httpServletRequest);
tenant = NamespaceUtil.processNamespaceParameter(tenant);
try {
configInfoGrayPersistService.removeConfigInfoGray(dataId, group, tenant, BetaGrayRule.TYPE_BETA, remoteIp,
RequestUtil.getSrcUserName(httpServletRequest));
Expand Down Expand Up @@ -512,7 +520,7 @@ public RestResult<ConfigInfo4Beta> queryBeta(@RequestParam(value = "dataId") Str
@RequestParam(value = "group") String group,
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant) {
try {

tenant = NamespaceUtil.processNamespaceParameter(tenant);
ConfigInfo4Beta configInfo4Beta = null;
ConfigInfoGrayWrapper beta4Gray = configInfoGrayPersistService.findConfigInfo4Gray(dataId, group, tenant,
"beta");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public Page<ConfigHistoryInfo> listConfigHistory(@RequestParam("dataId") String
@RequestParam(value = "appName", required = false) String appName,
@RequestParam(value = "pageNo", required = false) Integer pageNo,
@RequestParam(value = "pageSize", required = false) Integer pageSize, ModelMap modelMap) {
tenant = NamespaceUtil.processNamespaceParameter(tenant);
pageNo = null == pageNo ? 1 : pageNo;
pageSize = null == pageSize ? 100 : pageSize;
pageSize = Math.min(500, pageSize);
Expand All @@ -102,6 +103,7 @@ public ConfigHistoryInfo getConfigHistoryInfo(@RequestParam("dataId") String dat
@RequestParam("group") String group,
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant,
@RequestParam("nid") Long nid) throws AccessException {
tenant = NamespaceUtil.processNamespaceParameter(tenant);
return historyService.getConfigHistoryInfo(dataId, group, tenant, nid);
}

Expand All @@ -123,6 +125,7 @@ public ConfigHistoryInfo getPreviousConfigHistoryInfo(@RequestParam("dataId") St
@RequestParam("group") String group,
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant,
@RequestParam("id") Long id) throws AccessException {
tenant = NamespaceUtil.processNamespaceParameter(tenant);
return historyService.getPreviousConfigHistoryInfo(dataId, group, tenant, id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.config.server.controller;

import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.NamespaceUtil;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.model.GroupkeyListenserStatus;
Expand Down Expand Up @@ -72,6 +73,7 @@ public GroupkeyListenserStatus getAllSubClientConfigByIp(@RequestParam("ip") Str
return gls;
}
Map<String, String> status = collectSampleResult.getLisentersGroupkeyStatus();
tenant = NamespaceUtil.processNamespaceParameter(tenant);
for (Map.Entry<String, String> config : status.entrySet()) {
if (!StringUtils.isBlank(tenant) && config.getKey().contains(tenant)) {
configMd5Status.put(config.getKey(), config.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public Page<ConfigInfo> searchConfigByDetails(@RequestParam("dataId") String dat
if (StringUtils.isNotBlank(configDetail)) {
configAdvanceInfo.put("content", configDetail);
}
tenant = NamespaceUtil.processNamespaceParameter(tenant);
try {
return configDetailService.findConfigInfoPage(search, pageNo, pageSize, dataId, group, tenant, configAdvanceInfo);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.remote.request.RequestMeta;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.NamespaceUtil;
import com.alibaba.nacos.config.server.service.ConfigCacheService;
import com.alibaba.nacos.config.server.utils.GroupKey2;
import com.alibaba.nacos.config.server.utils.ParamUtils;
import com.alibaba.nacos.core.control.TpsControl;
import com.alibaba.nacos.core.paramcheck.ExtractorManager;
import com.alibaba.nacos.core.paramcheck.impl.ConfigBatchListenRequestParamExtractor;
import com.alibaba.nacos.core.remote.RequestHandler;
import com.alibaba.nacos.core.control.TpsControl;
import com.alibaba.nacos.core.utils.StringPool;
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
import com.alibaba.nacos.plugin.auth.constant.SignType;
Expand Down Expand Up @@ -59,8 +60,8 @@ public ConfigChangeBatchListenResponse handle(ConfigBatchListenRequest configCha
ParamUtils.checkParam(tag);
ConfigChangeBatchListenResponse configChangeBatchListenResponse = new ConfigChangeBatchListenResponse();
for (ConfigBatchListenRequest.ConfigListenContext listenContext : configChangeListenRequest.getConfigListenContexts()) {
String groupKey = GroupKey2.getKey(listenContext.getDataId(), listenContext.getGroup(),
listenContext.getTenant());
String namespaceId = NamespaceUtil.processNamespaceParameter(listenContext.getTenant());
String groupKey = GroupKey2.getKey(listenContext.getDataId(), listenContext.getGroup(), namespaceId);
groupKey = StringPool.get(groupKey);

String md5 = StringPool.get(listenContext.getMd5());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.alibaba.nacos.api.remote.request.RequestMeta;
import com.alibaba.nacos.api.remote.response.ResponseCode;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.NamespaceUtil;
import com.alibaba.nacos.common.utils.Pair;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.config.server.model.ConfigRequestInfo;
Expand Down Expand Up @@ -66,7 +67,7 @@ public ConfigPublishResponse handle(ConfigPublishRequest request, RequestMeta me
String dataId = request.getDataId();
String group = request.getGroup();
String content = request.getContent();
final String tenant = request.getTenant();
final String tenant = NamespaceUtil.processNamespaceParameter(request.getTenant());

final String srcIp = meta.getClientIp();
final String tag = request.getAdditionParam("tag");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.alibaba.nacos.api.remote.request.RequestMeta;
import com.alibaba.nacos.api.remote.response.ResponseCode;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.NamespaceUtil;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.config.server.model.CacheItem;
import com.alibaba.nacos.config.server.model.ConfigCacheGray;
Expand Down Expand Up @@ -82,11 +83,11 @@ private ConfigQueryResponse getContext(ConfigQueryRequest configQueryRequest, Re
String dataId = configQueryRequest.getDataId();
String group = configQueryRequest.getGroup();
String tenant = configQueryRequest.getTenant();
tenant = NamespaceUtil.processNamespaceParameter(tenant);
String clientIp = meta.getClientIp();
String tag = configQueryRequest.getTag();

String groupKey = GroupKey2.getKey(configQueryRequest.getDataId(), configQueryRequest.getGroup(),
configQueryRequest.getTenant());
String groupKey = GroupKey2.getKey(configQueryRequest.getDataId(), configQueryRequest.getGroup(), tenant);
String requestIpApp = meta.getLabels().get(CLIENT_APPNAME_HEADER);
String acceptCharset = ENCODE_UTF8;
ParamUtils.checkParam(tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.remote.request.RequestMeta;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.NamespaceUtil;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent;
import com.alibaba.nacos.config.server.model.gray.TagGrayRule;
Expand Down Expand Up @@ -68,6 +69,7 @@ public ConfigRemoveResponse handle(ConfigRemoveRequest configRemoveRequest, Requ
throws NacosException {
// check tenant
String tenant = configRemoveRequest.getTenant();
tenant = NamespaceUtil.processNamespaceParameter(tenant);
String dataId = configRemoveRequest.getDataId();
String group = configRemoveRequest.getGroup();
String tag = configRemoveRequest.getTag();
Expand Down
4 changes: 2 additions & 2 deletions console-ui/src/components/NameSpaceList/NameSpaceList.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class NameSpaceList extends React.Component {
}

handleNameSpaces(data) {
const nownamespace = getParams('namespace') || '';
const nownamespace = getParams('namespace') || 'public';

// let namespaceShowName = this._namespaceShowName || data[0].namespaceShowName || '';
window.namespaceList = data;
Expand All @@ -154,7 +154,7 @@ class NameSpaceList extends React.Component {
}
window.namespaceShowName = namespaceShowName;
window.namespaceDesc = namespaceDesc;
setParams('namespace', nownamespace || '');
setParams('namespace', nownamespace || 'public');
localStorage.setItem('namespace', nownamespace);
// setParams('namespaceShowName', namespaceShowName);
this.props.setNowNameSpace &&
Expand Down
Loading

0 comments on commit fdedaa2

Please sign in to comment.