Skip to content

Commit

Permalink
add disable db mode for console
Browse files Browse the repository at this point in the history
  • Loading branch information
qifanwang committed Sep 3, 2024
1 parent ec3d1bb commit 4e6dcfa
Show file tree
Hide file tree
Showing 61 changed files with 2,371 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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 +61,11 @@ 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 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_DISABLE_DB = "disable.db";

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

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

public boolean disableDb() {
return getBooleanProperty(KEY_DISABLE_DB, false);
}

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 @@ -205,4 +202,9 @@ public List<RedisMeta> getAllInstancesOfShard(String cluster, String shard) {
public boolean isAsymmetricCluster(String clusterName) {
return false;
}

@Override
public List<ShardMeta> getAllShardNamesByClusterName(String clusterName) {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ctrip.xpipe.redis.checker.spring;

import com.ctrip.xpipe.redis.checker.config.impl.DataCenterConfigBean;
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 DataCenterConfigBean consoleDataCenterConfigBean = new DataCenterConfigBean();

@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 consoleDataCenterConfigBean.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,9 @@ public interface ConsoleConfig extends CoreConfig, CheckerConfig, AlertConfig {
long getMetaServerSlotClusterMapCacheTimeOutMilli();

boolean autoSetKeeperSyncLimit();

boolean disableDb();

Set<String> getExtraSyncDC();

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public CombConsoleConfig(CheckConfigBean checkConfigBean,

private HickwallMetricInfo info;



@Override
public String getServerMode() {
return checkConfigBean.getServerMode();
Expand Down Expand Up @@ -555,6 +557,16 @@ public boolean autoSetKeeperSyncLimit() {
return consoleConfigBean.autoSetKeeperSyncLimit();
}

@Override
public boolean disableDb() {
return dataCenterConfigBean.disableDb();
}

@Override
public Set<String> getExtraSyncDC() {
return commonConfigBean.getExtraSyncDC();
}

@Override
public void addListener(ConfigKeyListener listener) {
this.listenersSet.add(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,4 +636,14 @@ public boolean autoSetKeeperSyncLimit() {
return getBooleanProperty(KEY_KEEPERCONTAINER_SYNC_LIMIT_ON, false);
}

@Override
public boolean disableDb() {
return false;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.ctrip.xpipe.redis.console.controller.AbstractConsoleController;
import com.ctrip.xpipe.redis.console.migration.status.ClusterStatus;
import com.ctrip.xpipe.redis.console.model.ClusterTbl;
import com.ctrip.xpipe.redis.console.model.DcClusterTbl;
import com.ctrip.xpipe.redis.console.model.DcModel;
import com.ctrip.xpipe.redis.console.model.DcTbl;
import com.ctrip.xpipe.redis.console.service.ClusterService;
Expand Down Expand Up @@ -62,6 +63,11 @@ public RetMessage getAllDcs(HttpServletRequest request) {
}
}

@RequestMapping(value = "/dc_tbls", method = RequestMethod.GET)
public List<DcTbl> getAllDcTbls(HttpServletRequest request) {
return dcService.findAllDcs();
}

@RequestMapping(value = "/dc", method = RequestMethod.PUT)
public RetMessage updateDc(HttpServletRequest request, @RequestBody DcModel dcModel){

Expand Down Expand Up @@ -98,4 +104,14 @@ public RetMessage resetDcClusters(HttpServletRequest request, @PathVariable Stri
return RetMessage.createSuccessMessage();
}

@RequestMapping(value = "/dc/bind", method = RequestMethod.POST)
public RetMessage bindDc(@RequestBody DcClusterTbl dcClusterTbl){
try {
clusterService.bindDc(dcClusterTbl);
return RetMessage.createSuccessMessage();
} catch (Exception e) {
return RetMessage.createFailMessage(e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.ctrip.xpipe.redis.console.controller.api.data;

import com.ctrip.xpipe.redis.checker.controller.result.RetMessage;
import com.ctrip.xpipe.redis.console.cache.AzGroupCache;
import com.ctrip.xpipe.redis.console.controller.AbstractConsoleController;
import com.ctrip.xpipe.redis.console.controller.api.data.meta.AzCreateInfo;
import com.ctrip.xpipe.redis.console.model.AzGroupModel;
import com.ctrip.xpipe.redis.console.service.AzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
Expand All @@ -22,6 +24,9 @@ public class AzUpdateController extends AbstractConsoleController {
@Autowired
private AzService azService;

@Autowired
private AzGroupCache azGroupCache;

@RequestMapping(value = "/az", method = RequestMethod.POST)
public RetMessage addAavilableZone(@RequestBody AzCreateInfo createInfo) {
try {
Expand Down Expand Up @@ -79,4 +84,10 @@ public List<AzCreateInfo> getAllAvailableZoneInfos() {
}
}

@RequestMapping(value = "/azGroup/all", method = RequestMethod.GET)
public List<AzGroupModel> allAvailableZones() {
return azGroupCache.getAllAzGroup();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import com.ctrip.xpipe.redis.console.dto.MultiGroupClusterCreateDTO;
import com.ctrip.xpipe.redis.console.dto.SingleGroupClusterCreateDTO;
import com.ctrip.xpipe.redis.console.model.ClusterTbl;
import com.ctrip.xpipe.redis.console.model.DcTbl;
import com.ctrip.xpipe.redis.console.model.OrganizationTbl;
import com.ctrip.xpipe.redis.console.model.RedisTbl;
import com.ctrip.xpipe.redis.console.service.ClusterService;
import com.ctrip.xpipe.redis.console.service.DcService;
import com.ctrip.xpipe.redis.console.service.OrganizationService;
import com.ctrip.xpipe.redis.console.service.RedisService;
import com.ctrip.xpipe.spring.AbstractController;
Expand All @@ -27,15 +29,7 @@
import com.dianping.cat.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -50,6 +44,9 @@ public class ClusterUpdateController extends AbstractController {
@Autowired
private ClusterService clusterService;

@Autowired
private DcService dcService;

@Autowired
private RedisService redisService;

Expand Down Expand Up @@ -420,4 +417,14 @@ void setConfig(ConsoleConfig config) {
this.config = config;
}

@RequestMapping(value = "/clusters/all", method = RequestMethod.GET)
public List<ClusterTbl> findAllClusters() {
return clusterService.findAllClustersWithOrgInfo();
}

@RequestMapping(value = "/clusters/{clusterId}/dcs", method = RequestMethod.GET)
public List<DcTbl> findClusterRelatedDc(@PathVariable String clusterId) {
return dcService.findClusterRelatedDc(clusterId);
}

}
Loading

0 comments on commit 4e6dcfa

Please sign in to comment.