Skip to content

Commit

Permalink
feat: add determine appid+cluster namespace num limit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
youngzil committed Sep 10, 2024
1 parent 31e6486 commit 4d29c26
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import com.ctrip.framework.apollo.common.config.RefreshablePropertySource;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;
Expand All @@ -36,6 +38,10 @@ public class BizConfig extends RefreshableConfig {

private static final int DEFAULT_ITEM_KEY_LENGTH = 128;
private static final int DEFAULT_ITEM_VALUE_LENGTH = 20000;

private static final int DEFAULT_MAX_NAMESPACE_NUM = 200;
private static final String[] DEFAULT_NAMESPACE_NUM_LIMIT_WHITE = new String[]{};

private static final int DEFAULT_APPNAMESPACE_CACHE_REBUILD_INTERVAL = 60; //60s
private static final int DEFAULT_GRAY_RELEASE_RULE_SCAN_INTERVAL = 60; //60s
private static final int DEFAULT_APPNAMESPACE_CACHE_SCAN_INTERVAL = 1; //1s
Expand Down Expand Up @@ -99,6 +105,15 @@ public int itemValueLengthLimit() {
return checkInt(limit, 5, Integer.MAX_VALUE, DEFAULT_ITEM_VALUE_LENGTH);
}

public int namespaceNumLimit() {
int limit = getIntProperty("namespace.num.limit", DEFAULT_MAX_NAMESPACE_NUM);
return checkInt(limit, 0, Integer.MAX_VALUE, DEFAULT_MAX_NAMESPACE_NUM);
}

public Set<String> namespaceNumLimitWhite() {
return Sets.newHashSet(getArrayProperty("namespace.num.limit.white", DEFAULT_NAMESPACE_NUM_LIMIT_WHITE));
}

public Map<Long, Integer> namespaceValueLengthLimitOverride() {
String namespaceValueLengthOverrideString = getValue("namespace.value.length.limit.override");
Map<Long, Integer> namespaceValueLengthOverride = Maps.newHashMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ public interface NamespaceRepository extends PagingAndSortingRepository<Namespac

int countByNamespaceNameAndAppIdNot(String namespaceName, String appId);

int countByAppIdAndClusterName(String appId, String clusterName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.ctrip.framework.apollo.biz.service;

import com.ctrip.framework.apollo.biz.config.BizConfig;
import com.ctrip.framework.apollo.biz.entity.Audit;
import com.ctrip.framework.apollo.biz.entity.Cluster;
import com.ctrip.framework.apollo.biz.entity.Item;
Expand Down Expand Up @@ -68,6 +69,7 @@ public class NamespaceService {
private final NamespaceLockService namespaceLockService;
private final InstanceService instanceService;
private final MessageSender messageSender;
private final BizConfig bizConfig;

public NamespaceService(
final ReleaseHistoryService releaseHistoryService,
Expand All @@ -81,7 +83,8 @@ public NamespaceService(
final @Lazy ClusterService clusterService,
final @Lazy NamespaceBranchService namespaceBranchService,
final NamespaceLockService namespaceLockService,
final InstanceService instanceService) {
final InstanceService instanceService,
final BizConfig bizConfig) {
this.releaseHistoryService = releaseHistoryService;
this.namespaceRepository = namespaceRepository;
this.auditService = auditService;
Expand All @@ -94,6 +97,7 @@ public NamespaceService(
this.namespaceBranchService = namespaceBranchService;
this.namespaceLockService = namespaceLockService;
this.instanceService = instanceService;
this.bizConfig = bizConfig;
}


Expand Down Expand Up @@ -349,6 +353,14 @@ public Namespace save(Namespace entity) {
if (!isNamespaceUnique(entity.getAppId(), entity.getClusterName(), entity.getNamespaceName())) {
throw new ServiceException("namespace not unique");
}

if (!bizConfig.namespaceNumLimitWhite().contains(entity.getAppId())){
int nowCount = namespaceRepository.countByAppIdAndClusterName(entity.getAppId(), entity.getClusterName()) ;
if(nowCount >= bizConfig.namespaceNumLimit()) {
throw new ServiceException("namespace[appId = " + entity.getAppId() + ", cluster= " + entity.getClusterName() + "] nowCount= " + nowCount + ", maxCount =" + bizConfig.namespaceNumLimit());
}
}

entity.setId(0);//protection
Namespace namespace = namespaceRepository.save(entity);

Expand Down

0 comments on commit 4d29c26

Please sign in to comment.