Skip to content

Commit

Permalink
Merge pull request #872 from wangqifan/feature/disable_db
Browse files Browse the repository at this point in the history
add disable db mode for console
  • Loading branch information
LanternLee authored Sep 20, 2024
2 parents b42baac + 0359789 commit f0048d0
Show file tree
Hide file tree
Showing 77 changed files with 3,173 additions and 1,165 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.ctrip.xpipe.redis.checker.config.impl;

import com.ctrip.xpipe.api.config.ConfigProvider;
import com.ctrip.xpipe.api.foundation.FoundationService;
import com.ctrip.xpipe.cluster.ClusterType;
import com.ctrip.xpipe.codec.JsonCodec;
import com.ctrip.xpipe.config.AbstractConfigBean;
import com.ctrip.xpipe.redis.core.route.RouteChooseStrategyFactory;
import org.springframework.context.annotation.Configuration;

import java.util.*;
import java.util.stream.Collectors;

@Configuration
public class CommonConfigBean extends AbstractConfigBean {
Expand Down Expand Up @@ -48,6 +50,8 @@ public class CommonConfigBean extends AbstractConfigBean {

public static final String KEY_NO_ALARM_MUNITE_FOR_CLUSTER_UPDATE = "no.alarm.minute.for.cluster.update";

public static final String KEY_META_SYNC_EXTERNAL_DC = "meta.sync.external.dc";

private String defaultRouteChooseStrategyType = RouteChooseStrategyFactory.RouteStrategyType.CRC32_HASH.name();

public CommonConfigBean() {
Expand All @@ -59,6 +63,18 @@ public Set<String> getConsoleUserAccessWhiteList() {
return new HashSet<>(Arrays.asList(whiteList.split(",")));
}

public Set<String> getExtraSyncDC() {
String dcs = getProperty(KEY_META_SYNC_EXTERNAL_DC, "");
return getSplitStringSet(dcs);
}

public boolean disableDb() {
return getExtraSyncDC().stream()
.map(String::toUpperCase)
.collect(Collectors.toSet())
.contains(FoundationService.DEFAULT.getDataCenter());
}

public Set<String> getAlertWhileList() {
String whitelist = getProperty(KEY_ALERT_WHITE_LIST, "");
return getSplitStringSet(whitelist);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ public class DataCenterConfigBean extends AbstractConfigBean {

public static final String KEY_BEACON_ORG_ROUTE = "beacon.org.routes";

public static final String KEY_CONSOLE_NO_DB_DOMAIN = "console.no.db.domain";

private AtomicReference<String> zkConnection = new AtomicReference<>();
private AtomicReference<String> zkNameSpace = new AtomicReference<>();

public DataCenterConfigBean() {
super(ConfigProvider.DEFAULT.getOrCreateConfig(ConfigProvider.DATA_CENTER_CONFIG_NAME));
}

public String getConsoleNoDbDomain() {
return getProperty(KEY_CONSOLE_NO_DB_DOMAIN, "");
}

public String getZkConnectionString() {
return getProperty(KEY_ZK_ADDRESS, zkConnection.get() == null ? "127.0.0.1:2181" : zkConnection.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.ctrip.xpipe.cluster.ClusterType;
import com.ctrip.xpipe.endpoint.HostPort;
import com.ctrip.xpipe.redis.core.entity.KeeperMeta;
import com.ctrip.xpipe.redis.core.entity.RedisMeta;
import com.ctrip.xpipe.redis.core.entity.RouteMeta;
import com.ctrip.xpipe.redis.core.entity.XpipeMeta;
import com.ctrip.xpipe.redis.core.entity.*;
import com.ctrip.xpipe.redis.core.exception.MasterNotFoundException;
import com.ctrip.xpipe.redis.core.meta.MetaCache;
import com.ctrip.xpipe.redis.core.meta.XpipeMetaManager;
Expand Down Expand Up @@ -38,6 +35,11 @@ public XpipeMeta getXpipeMeta() {
return xpipeMeta;
}

@Override
public XpipeMeta getXpipeMetaLongPull(long updateTime) throws InterruptedException {
return null;
}

@Override
public XpipeMeta getDividedXpipeMeta(int partIndex) {
return xpipeMeta;
Expand Down Expand Up @@ -209,4 +211,10 @@ public List<RedisMeta> getAllInstancesOfShard(String cluster, String shard) {
public boolean isAsymmetricCluster(String clusterName) {
return false;
}

@Override
public Set<String> getAllShardNamesByClusterName(String clusterName) {
return Collections.emptySet();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ctrip.xpipe.redis.checker.spring;

import com.ctrip.xpipe.redis.checker.config.impl.CommonConfigBean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

public class ConsoleDisableDbCondition implements Condition {

private static CommonConfigBean commonConfigBean = new CommonConfigBean();


@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
Map<String, Object> attributes = annotatedTypeMetadata.getAnnotationAttributes(DisableDbMode.class.getName());
boolean disable = (boolean) attributes.getOrDefault("value", false);
return commonConfigBean.disableDb() == disable;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ctrip.xpipe.redis.checker.spring;

import org.springframework.context.annotation.Conditional;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Conditional(ConsoleDisableDbCondition.class)
public @interface DisableDbMode {
boolean value();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ctrip.xpipe.redis.console.cache.impl;

import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition;
import com.ctrip.xpipe.redis.checker.spring.DisableDbMode;
import com.ctrip.xpipe.redis.console.cache.AzGroupCache;
import com.ctrip.xpipe.redis.console.config.ConsoleConfig;
import com.ctrip.xpipe.redis.console.entity.AzGroupEntity;
Expand All @@ -11,7 +13,7 @@
import org.apache.commons.collections.SetUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
Expand All @@ -25,17 +27,28 @@
import java.util.stream.Collectors;

@Service
@Conditional(ConsoleDisableDbCondition.class)
@DisableDbMode(false)
public class AzGroupCacheImpl implements AzGroupCache {

@Autowired
private ConsoleConfig config;
@Autowired

private DcRepository dcRepository;
@Autowired

private AzGroupRepository azGroupRepository;
@Autowired

private AzGroupMappingRepository azGroupMappingRepository;

public AzGroupCacheImpl(ConsoleConfig config,
DcRepository dcRepository,
AzGroupRepository azGroupRepository,
AzGroupMappingRepository azGroupMappingRepository) {
this.config = config;
this.dcRepository = dcRepository;
this.azGroupRepository = azGroupRepository;
this.azGroupMappingRepository = azGroupMappingRepository;
}

private static final Logger logger = LoggerFactory.getLogger(AzGroupCacheImpl.class.getName());
// private static final AzGroupModel DEFAULT_AZ_GROUP = new AzGroupModel(0L, "", "", Collections.emptyList());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.ctrip.xpipe.redis.console.cache.impl;

import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition;
import com.ctrip.xpipe.redis.checker.spring.DisableDbMode;
import com.ctrip.xpipe.redis.console.cache.AzGroupCache;
import com.ctrip.xpipe.redis.console.model.AzGroupModel;
import com.ctrip.xpipe.redis.console.resources.ConsolePortalService;
import org.apache.commons.collections.SetUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Conditional(ConsoleDisableDbCondition.class)
@DisableDbMode(true)
public class AzGroupCacheWithoutDBImpl implements AzGroupCache {

private ConsolePortalService consolePortalService;

@Autowired
public AzGroupCacheWithoutDBImpl(ConsolePortalService consolePortalService) {
this.consolePortalService = consolePortalService;
}

private List<AzGroupModel> azGroupModels = null;

private Map<Long, AzGroupModel> idAzGroupMap = null;

@Override
public List<AzGroupModel> getAllAzGroup() {
if (this.azGroupModels == null) {
this.loadAzGroupCache();
}
return this.azGroupModels;
}

@Override
public AzGroupModel getAzGroupById(Long id) {
if (this.azGroupModels == null) {
this.loadAzGroupCache();
}
return this.idAzGroupMap.get(id);
}

@Override
public AzGroupModel getAzGroupByAzs(List<String> azs) {
if (this.azGroupModels == null) {
this.loadAzGroupCache();
}
for (AzGroupModel model : this.azGroupModels) {
if (SetUtils.isEqualSet(model.getAzs(), azs)) {
return model;
}
}
return null;
}

private void loadAzGroupCache() {
this.azGroupModels = consolePortalService.getAllAzGroups();
this.idAzGroupMap = this.azGroupModels.stream().collect(Collectors.toMap(AzGroupModel::getId, Function.identity()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,11 @@ public interface ConsoleConfig extends CoreConfig, CheckerConfig, AlertConfig {
long getMetaServerSlotClusterMapCacheTimeOutMilli();

boolean autoSetKeeperSyncLimit();

boolean disableDb();

Set<String> getExtraSyncDC();

String getConsoleNoDbDomain();

}
Loading

0 comments on commit f0048d0

Please sign in to comment.