diff --git a/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/config/impl/CommonConfigBean.java b/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/config/impl/CommonConfigBean.java index 892e4cf2b..abc14edff 100644 --- a/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/config/impl/CommonConfigBean.java +++ b/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/config/impl/CommonConfigBean.java @@ -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() { @@ -59,6 +61,11 @@ public Set getConsoleUserAccessWhiteList() { return new HashSet<>(Arrays.asList(whiteList.split(","))); } + public Set getExtraSyncDC() { + String dcs = getProperty(KEY_META_SYNC_EXTERNAL_DC, ""); + return getSplitStringSet(dcs); + } + public Set getAlertWhileList() { String whitelist = getProperty(KEY_ALERT_WHITE_LIST, ""); return getSplitStringSet(whitelist); diff --git a/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/config/impl/DataCenterConfigBean.java b/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/config/impl/DataCenterConfigBean.java index bb590988d..94a60e850 100644 --- a/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/config/impl/DataCenterConfigBean.java +++ b/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/config/impl/DataCenterConfigBean.java @@ -33,6 +33,8 @@ 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 zkConnection = new AtomicReference<>(); private AtomicReference zkNameSpace = new AtomicReference<>(); @@ -40,6 +42,10 @@ 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()); } diff --git a/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/impl/TestMetaCache.java b/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/impl/TestMetaCache.java index 1267b8948..e1574d7b5 100644 --- a/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/impl/TestMetaCache.java +++ b/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/impl/TestMetaCache.java @@ -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; @@ -205,4 +202,9 @@ public List getAllInstancesOfShard(String cluster, String shard) { public boolean isAsymmetricCluster(String clusterName) { return false; } + + @Override + public List getAllShardNamesByClusterName(String clusterName) { + return Collections.emptyList(); + } } diff --git a/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/spring/ConsoleDisableDbCondition.java b/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/spring/ConsoleDisableDbCondition.java new file mode 100644 index 000000000..4becaa77e --- /dev/null +++ b/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/spring/ConsoleDisableDbCondition.java @@ -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 attributes = annotatedTypeMetadata.getAnnotationAttributes(DisableDbMode.class.getName()); + boolean disable = (boolean) attributes.getOrDefault("value", false); + return consoleDataCenterConfigBean.disableDb() == disable; + } + + +} diff --git a/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/spring/DisableDbMode.java b/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/spring/DisableDbMode.java new file mode 100644 index 000000000..194a1996c --- /dev/null +++ b/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/spring/DisableDbMode.java @@ -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(); +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/cache/impl/AzGroupCacheImpl.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/cache/impl/AzGroupCacheImpl.java index 02dc82b20..572543be0 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/cache/impl/AzGroupCacheImpl.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/cache/impl/AzGroupCacheImpl.java @@ -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; @@ -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; @@ -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()); diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/cache/impl/AzGroupCacheWithoutDBImpl.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/cache/impl/AzGroupCacheWithoutDBImpl.java new file mode 100644 index 000000000..d9ab2b357 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/cache/impl/AzGroupCacheWithoutDBImpl.java @@ -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 azGroupModels = null; + + private Map idAzGroupMap = null; + + @Override + public List 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 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())); + } +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/ConsoleConfig.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/ConsoleConfig.java index 458c5d9b2..c680f57cd 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/ConsoleConfig.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/ConsoleConfig.java @@ -149,4 +149,9 @@ public interface ConsoleConfig extends CoreConfig, CheckerConfig, AlertConfig { long getMetaServerSlotClusterMapCacheTimeOutMilli(); boolean autoSetKeeperSyncLimit(); + + boolean disableDb(); + + Set getExtraSyncDC(); + } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/impl/CombConsoleConfig.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/impl/CombConsoleConfig.java index 6f9641d89..521462e10 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/impl/CombConsoleConfig.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/impl/CombConsoleConfig.java @@ -59,6 +59,8 @@ public CombConsoleConfig(CheckConfigBean checkConfigBean, private HickwallMetricInfo info; + + @Override public String getServerMode() { return checkConfigBean.getServerMode(); @@ -555,6 +557,16 @@ public boolean autoSetKeeperSyncLimit() { return consoleConfigBean.autoSetKeeperSyncLimit(); } + @Override + public boolean disableDb() { + return dataCenterConfigBean.disableDb(); + } + + @Override + public Set getExtraSyncDC() { + return commonConfigBean.getExtraSyncDC(); + } + @Override public void addListener(ConfigKeyListener listener) { this.listenersSet.add(listener); diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/impl/DefaultConsoleConfig.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/impl/DefaultConsoleConfig.java index d5c8e151a..a03fa76ba 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/impl/DefaultConsoleConfig.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/impl/DefaultConsoleConfig.java @@ -636,4 +636,14 @@ public boolean autoSetKeeperSyncLimit() { return getBooleanProperty(KEY_KEEPERCONTAINER_SYNC_LIMIT_ON, false); } + @Override + public boolean disableDb() { + return false; + } + + @Override + public Set getExtraSyncDC() { + return Collections.emptySet(); + } + } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/DcApiController.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/DcApiController.java index 2b0de9c5a..67828c1f9 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/DcApiController.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/DcApiController.java @@ -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; @@ -62,6 +63,11 @@ public RetMessage getAllDcs(HttpServletRequest request) { } } + @RequestMapping(value = "/dc_tbls", method = RequestMethod.GET) + public List getAllDcTbls(HttpServletRequest request) { + return dcService.findAllDcs(); + } + @RequestMapping(value = "/dc", method = RequestMethod.PUT) public RetMessage updateDc(HttpServletRequest request, @RequestBody DcModel dcModel){ @@ -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()); + } + } + } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/AzUpdateController.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/AzUpdateController.java index 66304d620..1d0213e07 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/AzUpdateController.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/AzUpdateController.java @@ -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.*; @@ -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 { @@ -79,4 +84,10 @@ public List getAllAvailableZoneInfos() { } } + @RequestMapping(value = "/azGroup/all", method = RequestMethod.GET) + public List allAvailableZones() { + return azGroupCache.getAllAzGroup(); + } + + } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/ClusterUpdateController.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/ClusterUpdateController.java index 6c3b2c8d2..3dbcd5ed2 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/ClusterUpdateController.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/ClusterUpdateController.java @@ -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; @@ -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; @@ -50,6 +44,9 @@ public class ClusterUpdateController extends AbstractController { @Autowired private ClusterService clusterService; + @Autowired + private DcService dcService; + @Autowired private RedisService redisService; @@ -420,4 +417,14 @@ void setConfig(ConsoleConfig config) { this.config = config; } + @RequestMapping(value = "/clusters/all", method = RequestMethod.GET) + public List findAllClusters() { + return clusterService.findAllClustersWithOrgInfo(); + } + + @RequestMapping(value = "/clusters/{clusterId}/dcs", method = RequestMethod.GET) + public List findClusterRelatedDc(@PathVariable String clusterId) { + return dcService.findClusterRelatedDc(clusterId); + } + } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/ConfigUpdateController.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/ConfigUpdateController.java new file mode 100644 index 000000000..2e1484a57 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/ConfigUpdateController.java @@ -0,0 +1,63 @@ +package com.ctrip.xpipe.redis.console.controller.api.data; + +import com.ctrip.xpipe.redis.checker.controller.result.RetMessage; +import com.ctrip.xpipe.redis.console.controller.AbstractConsoleController; +import com.ctrip.xpipe.redis.console.dao.ConfigDao; +import com.ctrip.xpipe.redis.console.model.ConfigModel; +import com.ctrip.xpipe.redis.console.model.ConfigTbl; +import com.ctrip.xpipe.utils.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import org.unidal.dal.jdbc.DalException; + +import java.util.Date; +import java.util.List; + +@RestController +@RequestMapping(AbstractConsoleController.API_PREFIX) +public class ConfigUpdateController extends AbstractConsoleController { + + @Autowired + private ConfigDao configDao; + + @RequestMapping(value = "/config/set", method = RequestMethod.POST) + public RetMessage setConfig(@RequestParam ConfigModel config, + @RequestParam Date util) { + try { + if(util == null) { + configDao.setConfig(config); + } else { + configDao.setConfigAndUntil(config, util); + } + return RetMessage.createSuccessMessage(); + } catch (DalException e) { + logger.error("[setConfig] DalException: {}", e); + return RetMessage.createFailMessage(e.getMessage()); + } + + } + + @GetMapping(value = "/config") + public ConfigTbl getConfigSubId(@RequestParam String key, + @RequestParam String subId) throws DalException { + + if(StringUtil.isEmpty(subId)) { + return configDao.getByKey(key); + } else { + return configDao.getByKeyAndSubId(key, subId); + } + } + + @GetMapping(value = "/config/getAll/{key}") + public List getAllByKey(@PathVariable String key) throws DalException { + return configDao.getAllByKey(key); + } + + @RequestMapping(value = "/config/findAll", method = RequestMethod.POST) + public List findAllByKeyAndValueAndUntilAfter(@RequestParam String key, + @RequestParam String value, + @RequestParam Date until) throws DalException { + return configDao.findAllByKeyAndValueAndUntilAfter(key, value, until); + } + +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/KeeperUpdateController.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/KeeperUpdateController.java index 91c839214..fc2b59672 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/KeeperUpdateController.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/KeeperUpdateController.java @@ -8,6 +8,7 @@ import com.ctrip.xpipe.redis.console.controller.annotation.ClusterTypeLimit; import com.ctrip.xpipe.redis.console.controller.api.data.meta.KeeperContainerCreateInfo; import com.ctrip.xpipe.redis.console.model.ClusterTbl; +import com.ctrip.xpipe.redis.console.model.KeepercontainerTbl; import com.ctrip.xpipe.redis.console.model.RedisTbl; import com.ctrip.xpipe.redis.console.service.*; import com.ctrip.xpipe.redis.console.service.exception.ResourceNotFoundException; @@ -188,4 +189,9 @@ public RetMessage deleteKeepercontainer(@PathVariable String keepercontaierIp, @ } } + @RequestMapping(value = "/keeper_container/all", method = RequestMethod.GET) + public List getAllKeeperContainers() { + return keeperContainerService.findAll(); + } + } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/OrganizationApiController.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/OrganizationApiController.java new file mode 100644 index 000000000..8dced66da --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/OrganizationApiController.java @@ -0,0 +1,24 @@ +package com.ctrip.xpipe.redis.console.controller.api.data; + +import com.ctrip.xpipe.redis.console.controller.AbstractConsoleController; +import com.ctrip.xpipe.redis.console.model.OrganizationTbl; +import com.ctrip.xpipe.redis.console.service.OrganizationService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping(AbstractConsoleController.API_PREFIX) +public class OrganizationApiController { + + @Autowired + private OrganizationService organizationService; + + @RequestMapping(value = "/organizations/all", method = RequestMethod.GET) + public List getOrganizationById() { + return organizationService.getAllOrganizations(); + } +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/RedisApiController.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/RedisApiController.java new file mode 100644 index 000000000..9f8790675 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/RedisApiController.java @@ -0,0 +1,48 @@ +package com.ctrip.xpipe.redis.console.controller.api.data; + +import com.ctrip.xpipe.redis.checker.controller.result.RetMessage; +import com.ctrip.xpipe.redis.console.controller.AbstractConsoleController; +import com.ctrip.xpipe.redis.console.controller.api.data.meta.RedisCreateInfo; +import com.ctrip.xpipe.redis.console.model.RedisTbl; +import com.ctrip.xpipe.redis.console.service.RedisService; +import com.ctrip.xpipe.tuple.Pair; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping(AbstractConsoleController.API_PREFIX) +public class RedisApiController extends AbstractConsoleController { + + @Autowired + private RedisService redisService; + + @RequestMapping(value = "/redis/{dcClusterShardId}", method = RequestMethod.GET) + List findAllByDcClusterShard(@PathVariable long dcClusterShardId) { + return redisService.findAllByDcClusterShard(dcClusterShardId); + } + + @RequestMapping(value = "/redis/updateBatchKeeperActive", method = RequestMethod.POST) + RetMessage updateBatchKeeperActive(@RequestBody List redises) { + try { + redisService.updateBatchKeeperActive(redises); + return RetMessage.createSuccessMessage(); + } catch (Exception e) { + return RetMessage.createFailMessage(e.getMessage()); + } + } + + @RequestMapping(value = "/redis/insert", method = RequestMethod.POST) + RetMessage insert(@RequestBody RedisCreateInfo redisCreateInfo) { + try { + redisService.insertRedises(redisCreateInfo.getDcId(), + redisCreateInfo.getClusterId(), redisCreateInfo.getShardName(), + redisCreateInfo.getRedisAddresses()); + return RetMessage.createSuccessMessage(); + } catch (Exception e) { + return RetMessage.createFailMessage(e.getMessage()); + } + } + +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/meta/RedisCreateInfo.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/meta/RedisCreateInfo.java index 5d28df9ce..b5ecb5802 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/meta/RedisCreateInfo.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/meta/RedisCreateInfo.java @@ -16,6 +16,7 @@ */ public class RedisCreateInfo extends AbstractCreateInfo { private String dcId; + private String clusterId; private String shardName; private String redises; @@ -64,4 +65,12 @@ public List> getRedisAddresses() { } return Lists.newArrayList(addresses); } + + public String getClusterId() { + return clusterId; + } + + public void setClusterId(String clusterId) { + this.clusterId = clusterId; + } } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/consoleportal/ConfigController.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/consoleportal/ConfigController.java index 4a046c74e..9b3e53471 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/consoleportal/ConfigController.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/consoleportal/ConfigController.java @@ -10,13 +10,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - +import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; +import java.util.Date; +import java.util.List; + import static com.ctrip.xpipe.redis.console.service.ConfigService.*; /** diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/consoleportal/KeeperContainerInfoController.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/consoleportal/KeeperContainerInfoController.java index f77051c3a..8e0ae853e 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/consoleportal/KeeperContainerInfoController.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/consoleportal/KeeperContainerInfoController.java @@ -8,6 +8,7 @@ import com.ctrip.xpipe.redis.console.keeper.entity.KeeperContainerDiskType; import com.ctrip.xpipe.redis.console.model.ConfigModel; import com.ctrip.xpipe.redis.console.model.KeeperContainerInfoModel; +import com.ctrip.xpipe.redis.console.model.KeepercontainerTbl; import com.ctrip.xpipe.redis.console.model.MigrationKeeperContainerDetailModel; import com.ctrip.xpipe.redis.console.service.ConfigService; import com.ctrip.xpipe.redis.console.service.KeeperContainerMigrationService; @@ -40,7 +41,11 @@ public List getAllKeeperContainerInfos() { return keeperContainerService.findAllInfos(); } - + @RequestMapping(value = "/keepercontainer/all", method = RequestMethod.GET) + public List getAllKeeperContainers() { + return keeperContainerService.findAll(); + } + @RequestMapping(value = "/keepercontainer/{id}", method = RequestMethod.GET) public KeeperContainerInfoModel getKeeperContainerById(@PathVariable long id) { return keeperContainerService.findKeeperContainerInfoModelById(id); diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/election/CrossDcLeaderElectionAction.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/election/CrossDcLeaderElectionAction.java index 24fc2df57..5e4782ecd 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/election/CrossDcLeaderElectionAction.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/election/CrossDcLeaderElectionAction.java @@ -40,11 +40,14 @@ public class CrossDcLeaderElectionAction extends AbstractPeriodicElectionAction private ConfigTbl lease; + private ConsoleConfig config; + @Autowired public CrossDcLeaderElectionAction(ConfigDao configDao, MetaCache metaCache, ConsoleConfig consoleConfig) { this.configDao = configDao; this.metaCache = metaCache; this.leaseName = consoleConfig.getCrossDcLeaderLeaseName(); + this.config = consoleConfig; } @Override @@ -80,6 +83,9 @@ protected void doElect() { @Override protected boolean shouldElect() { + if(config.disableDb()) { + return false; + } try { refreshConfig(); } catch (Exception e) { diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitor.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitor.java index f27d28854..54660eeda 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitor.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitor.java @@ -4,6 +4,7 @@ import com.ctrip.xpipe.redis.console.healthcheck.nonredis.cluster.ClusterHealthMonitor; import com.ctrip.xpipe.redis.console.healthcheck.nonredis.cluster.ClusterHealthState; import com.ctrip.xpipe.redis.console.service.ShardService; +import com.ctrip.xpipe.redis.core.meta.MetaCache; import com.google.common.collect.Lists; import com.google.common.collect.Sets; @@ -16,7 +17,7 @@ public class DefaultClusterHealthMonitor implements ClusterHealthMonitor { private String clusterId; - private ShardService shardService; + private MetaCache metaCache; private AtomicReference state = new AtomicReference<>(ClusterHealthState.NORMAL); @@ -28,9 +29,9 @@ public class DefaultClusterHealthMonitor implements ClusterHealthMonitor { private List listeners = Lists.newCopyOnWriteArrayList(); - public DefaultClusterHealthMonitor(String clusterId, ShardService shardService) { + public DefaultClusterHealthMonitor(String clusterId, MetaCache metaCache) { this.clusterId = clusterId; - this.shardService = shardService; + this.metaCache = metaCache; } @Override @@ -96,7 +97,7 @@ public ClusterHealthState getState() { private void checkIfStateChange() { int warningShardNums = warningShards.size(); - int totalShards = shardService.findAllShardNamesByClusterName(getClusterId()).size(); + int totalShards = metaCache.getAllShardNamesByClusterName(getClusterId()).size(); ClusterHealthState current = ClusterHealthState.getState(totalShards, warningShardNums); ClusterHealthState prev = state.get(); if(!prev.equals(current) && state.compareAndSet(prev, current)) { diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitorManager.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitorManager.java index 417c3edaf..9bcd1a6c8 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitorManager.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitorManager.java @@ -8,6 +8,7 @@ import com.ctrip.xpipe.redis.console.healthcheck.nonredis.cluster.ClusterHealthState; import com.ctrip.xpipe.redis.console.healthcheck.nonredis.cluster.LeveledEmbededSet; import com.ctrip.xpipe.redis.console.service.ShardService; +import com.ctrip.xpipe.redis.core.meta.MetaCache; import com.ctrip.xpipe.utils.MapUtils; import com.ctrip.xpipe.utils.OsUtils; import com.ctrip.xpipe.utils.VisibleForTesting; @@ -28,7 +29,7 @@ public class DefaultClusterHealthMonitorManager extends CheckerClusterHealthMana private static final Logger logger = LoggerFactory.getLogger(DefaultClusterHealthMonitorManager.class); @Autowired - private ShardService shardService; + private MetaCache metaCache; private ConcurrentMap monitors = Maps.newConcurrentMap(); @@ -98,7 +99,7 @@ private DefaultClusterHealthMonitor getOrCreate(String clusterId) { return MapUtils.getOrCreate(monitors, clusterId, new ObjectFactory() { @Override public DefaultClusterHealthMonitor create() { - DefaultClusterHealthMonitor monitor = new DefaultClusterHealthMonitor(clusterId, shardService); + DefaultClusterHealthMonitor monitor = new DefaultClusterHealthMonitor(clusterId, metaCache); monitor.addListener(clusterHealthMonitorListener); return monitor; } @@ -133,8 +134,8 @@ private void remove(String clusterId) { } @VisibleForTesting - protected DefaultClusterHealthMonitorManager setShardService(ShardService shardService) { - this.shardService = shardService; + protected DefaultClusterHealthMonitorManager setMetaCache(MetaCache metaCache) { + this.metaCache = metaCache; return this; } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/redisconfig/RedisConfigCheckMonitor.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/redisconfig/RedisConfigCheckMonitor.java index f804ac781..1439de783 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/redisconfig/RedisConfigCheckMonitor.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/redisconfig/RedisConfigCheckMonitor.java @@ -5,6 +5,7 @@ import com.ctrip.xpipe.redis.checker.alert.ALERT_TYPE; import com.ctrip.xpipe.redis.console.AbstractCrossDcIntervalAction; import com.ctrip.xpipe.redis.console.controller.api.data.meta.DcClusterCreateInfo; +import com.ctrip.xpipe.redis.console.service.DcService; import com.ctrip.xpipe.redis.console.service.impl.DcClusterServiceImpl; import com.ctrip.xpipe.redis.console.service.impl.DcServiceImpl; import com.ctrip.xpipe.redis.core.entity.ClusterMeta; @@ -26,7 +27,7 @@ public class RedisConfigCheckMonitor extends AbstractCrossDcIntervalAction { private MetaCache metaCache; @Autowired - private DcServiceImpl dcService; + private DcService dcService; @Autowired private DcClusterServiceImpl dcClusterService; diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/reporter/DefaultMigrationResultReporter.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/reporter/DefaultMigrationResultReporter.java index 1459f55c6..8bff8b3f5 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/reporter/DefaultMigrationResultReporter.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/reporter/DefaultMigrationResultReporter.java @@ -8,9 +8,11 @@ import com.ctrip.xpipe.redis.console.migration.status.MigrationStatus; import com.ctrip.xpipe.redis.console.model.MigrationClusterTbl; import com.ctrip.xpipe.redis.console.model.OrganizationTbl; -import com.ctrip.xpipe.redis.console.service.DcService; import com.ctrip.xpipe.redis.console.service.OrganizationService; import com.ctrip.xpipe.redis.console.service.migration.MigrationService; +import com.ctrip.xpipe.redis.core.entity.DcMeta; +import com.ctrip.xpipe.redis.core.entity.XpipeMeta; +import com.ctrip.xpipe.redis.core.meta.MetaCache; import com.ctrip.xpipe.spring.AbstractProfile; import com.ctrip.xpipe.utils.DateTimeUtils; import com.google.common.collect.Lists; @@ -21,6 +23,7 @@ import javax.annotation.PostConstruct; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -32,9 +35,6 @@ public class DefaultMigrationResultReporter extends AbstractSiteLeaderIntervalAc @Autowired private MigrationService migrationService; - @Autowired - private DcService dcService; - @Autowired private OrganizationService organizationService; @@ -63,14 +63,38 @@ public class DefaultMigrationResultReporter extends AbstractSiteLeaderIntervalAc @Autowired private MigrationReporterConfig migrationReporterConfig; + @Autowired + private MetaCache metaCache; + + private boolean isInited = false; + @PostConstruct public void init() { - dcIdNameMap = dcService.dcNameMap(); + dcIdNameMap = buildDcNameMap(); + if(dcIdNameMap == null) { + isInited = false; + return; + } orgIdNameMap = organizationService.getAllOrganizations().stream().collect(Collectors.toMap(OrganizationTbl::getId, OrganizationTbl::getOrgName)); + isInited = true; + } + + private Map buildDcNameMap() { + XpipeMeta xpipeMeta = metaCache.getXpipeMeta(); + if(xpipeMeta == null) { + return null; + } + Map allDcs = xpipeMeta.getDcs(); + Map result = new HashMap<>(); + allDcs.values().forEach(dc -> result.put(dc.getNo(), dc.getId())); + return result; } @Override protected void doAction() { + if(!isInited) { + init(); + } EventMonitor.DEFAULT.logEvent(REPORT_EVENT, "begin"); List latestMigrationClusters = migrationService.getLatestMigrationClusters(DEFAULT_STATISTICAL_INTERVAL); if (latestMigrationClusters == null || latestMigrationClusters.size() == 0) return; diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/AbstractMetaCache.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/AbstractMetaCache.java index 18350400d..965e7a82c 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/AbstractMetaCache.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/AbstractMetaCache.java @@ -668,6 +668,20 @@ private void checkDcClustersCnt(DcMeta currentDcMeta, DcMeta futureDcMeta, int m throw new TooManyClustersRemovedException(String.format("dc:%s, current cluster count:%d,future cluster count:%d", currentDcMeta.getId(), currentClusterCount, futureClusterCount)); } + @Override + public List getAllShardNamesByClusterName(String clusterName) { + List shards = new ArrayList<>(); + XpipeMeta xpipeMeta = meta.getKey(); + for (DcMeta dcMeta : xpipeMeta.getDcs().values()) { + ClusterMeta clusterMeta = dcMeta.findCluster(clusterName); + if (clusterMeta != null) { + Map shardMetaMap = clusterMeta.getShards(); + shards.addAll(shardMetaMap.values()); + } + } + return shards; + } + @VisibleForTesting public AbstractMetaCache setMeta(Pair meta) { this.meta = meta; diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/CheckerPersistenceCache.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/CheckerPersistenceCache.java index 0c3cb1f3a..4fd2bf8f5 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/CheckerPersistenceCache.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/CheckerPersistenceCache.java @@ -20,18 +20,18 @@ public CheckerPersistenceCache(CheckerConfig config, CheckerConsoleService servi @Override public void updateRedisRole(RedisHealthCheckInstance instance, Server.SERVER_ROLE role) { - service.updateRedisRole(config.getConsoleAddress(), instance, role); + service.updateRedisRole(getConsoleAddress(), instance, role); } @Override public void recordAlert(String eventOperator, AlertMessageEntity message, EmailResponse response) { - service.recordAlert(config.getConsoleAddress(), eventOperator, message, response); + service.recordAlert(getConsoleAddress(), eventOperator, message, response); } @Override public Set doSentinelCheckWhiteList() { try { - Set originWhitelist = service.sentinelCheckWhiteList(config.getConsoleAddress()); + Set originWhitelist = service.sentinelCheckWhiteList(getConsoleAddress()); return originWhitelist.stream().map(String::toLowerCase).collect(Collectors.toSet()); } catch (RestClientException e) { logger.warn("[doSentinelCheckWhiteList] rest fail, {}", e.getMessage()); @@ -45,7 +45,7 @@ public Set doSentinelCheckWhiteList() { @Override public Set doClusterAlertWhiteList() { try { - Set originWhitelist = service.clusterAlertWhiteList(config.getConsoleAddress()); + Set originWhitelist = service.clusterAlertWhiteList(getConsoleAddress()); return originWhitelist.stream().map(String::toLowerCase).collect(Collectors.toSet()); } catch (RestClientException e) { logger.warn("[doClusterAlertWhiteList] rest fail, {}", e.getMessage()); @@ -59,7 +59,7 @@ public Set doClusterAlertWhiteList() { @Override Set doGetMigratingClusterList() { try { - return service.migratingClusterList(config.getConsoleAddress()); + return service.migratingClusterList(getConsoleAddress()); } catch (RestClientException e) { logger.warn("[doGetMigratingClusterList] rest fail, {}", e.getMessage()); } catch (Throwable th) { @@ -72,7 +72,7 @@ Set doGetMigratingClusterList() { @Override public boolean doIsSentinelAutoProcess() { try { - return service.isSentinelAutoProcess(config.getConsoleAddress()); + return service.isSentinelAutoProcess(getConsoleAddress()); } catch (RestClientException e) { logger.warn("[doIsSentinelAutoProcess] rest fail, {}", e.getMessage()); } catch (Throwable th) { @@ -85,7 +85,7 @@ public boolean doIsSentinelAutoProcess() { @Override public boolean doIsAlertSystemOn() { try { - return service.isAlertSystemOn(config.getConsoleAddress()); + return service.isAlertSystemOn(getConsoleAddress()); } catch (RestClientException e) { logger.warn("[doIsAlertSystemOn] rest fail, {}", e.getMessage()); } catch (Throwable th) { @@ -98,7 +98,7 @@ public boolean doIsAlertSystemOn() { @Override boolean doIsKeeperBalanceInfoCollectOn() { try { - return service.isKeeperBalanceInfoCollectOn(config.getConsoleAddress()); + return service.isKeeperBalanceInfoCollectOn(getConsoleAddress()); } catch (RestClientException e) { logger.warn("[doIsKeeperBalanceInfoCollectOn] rest fail, {}", e.getMessage()); } catch (Throwable th) { @@ -111,7 +111,7 @@ boolean doIsKeeperBalanceInfoCollectOn() { @Override public Map doLoadAllClusterCreateTime() { try { - return service.loadAllClusterCreateTime(config.getConsoleAddress()); + return service.loadAllClusterCreateTime(getConsoleAddress()); } catch (RestClientException e) { logger.warn("[doLoadAllClusterCreateTime] rest fail, {}", e.getMessage()); } catch (Throwable th) { @@ -120,4 +120,8 @@ public Map doLoadAllClusterCreateTime() { return Collections.emptyMap(); } + + protected String getConsoleAddress() { + return config.getConsoleAddress(); + } } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ClusterMetaSynchronizer.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ClusterMetaSynchronizer.java index ea290d7bc..a165dc4ac 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ClusterMetaSynchronizer.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ClusterMetaSynchronizer.java @@ -38,12 +38,15 @@ public class ClusterMetaSynchronizer { private SentinelBalanceService sentinelBalanceService; private ConsoleConfig consoleConfig; private ClusterTypeUpdateEventFactory clusterTypeUpdateEventFactory; + private String dcId; public ClusterMetaSynchronizer(Set added, Set removed, Set modified, DcService dcService, ClusterService clusterService, ShardService shardService, RedisService redisService, OrganizationService organizationService, SentinelBalanceService sentinelBalanceService, ConsoleConfig consoleConfig, - ClusterTypeUpdateEventFactory clusterTypeUpdateEventFactory) { + ClusterTypeUpdateEventFactory clusterTypeUpdateEventFactory, + String dcId + ) { this.added = added; this.removed = removed; this.modified = modified; @@ -55,6 +58,7 @@ public ClusterMetaSynchronizer(Set added, Set removed, this.sentinelBalanceService = sentinelBalanceService; this.consoleConfig = consoleConfig; this.clusterTypeUpdateEventFactory = clusterTypeUpdateEventFactory; + this.dcId = dcId; } public void sync() { @@ -68,17 +72,17 @@ void remove() { removed.forEach(clusterMeta -> { try { List relatedDcs = clusterService.getClusterRelatedDcs(clusterMeta.getId()); - if (relatedDcs.size() == 0 || relatedDcs.size() == 1 && relatedDcs.get(0).getDcName().equalsIgnoreCase(DcMetaSynchronizer.currentDcId)) { - logger.info("[ClusterMetaSynchronizer][remove]{}, {}", clusterMeta, DcMetaSynchronizer.currentDcId); + if (relatedDcs.size() == 0 || relatedDcs.size() == 1 && relatedDcs.get(0).getDcName().equalsIgnoreCase(dcId)) { + logger.info("[ClusterMetaSynchronizer][remove]{}, {}", clusterMeta, dcId); clusterService.deleteCluster(clusterMeta.getId()); - CatEventMonitor.DEFAULT.logEvent(META_SYNC, String.format("[delCluster]%s-%s", DcMetaSynchronizer.currentDcId, clusterMeta.getId())); + CatEventMonitor.DEFAULT.logEvent(META_SYNC, String.format("[delCluster]%s-%s", dcId, clusterMeta.getId())); } else { - logger.info("[ClusterMetaSynchronizer][unbindDc]{}, {}", clusterMeta, DcMetaSynchronizer.currentDcId); - clusterService.unbindDc(clusterMeta.getId(), DcMetaSynchronizer.currentDcId); - CatEventMonitor.DEFAULT.logEvent(META_SYNC, String.format("[unbindDc]%s-%s", DcMetaSynchronizer.currentDcId, clusterMeta.getId())); + logger.info("[ClusterMetaSynchronizer][unbindDc]{}, {}", clusterMeta, dcId); + clusterService.unbindDc(clusterMeta.getId(), dcId); + CatEventMonitor.DEFAULT.logEvent(META_SYNC, String.format("[unbindDc]%s-%s", dcId, clusterMeta.getId())); } } catch (Exception e) { - logger.error("[ClusterMetaSynchronizer][unbindDc]{}, {}", clusterMeta, DcMetaSynchronizer.currentDcId, e); + logger.error("[ClusterMetaSynchronizer][unbindDc]{}, {}", clusterMeta, dcId, e); } }); } catch (Exception e) { @@ -103,7 +107,7 @@ void add() { return; } - new ShardMetaSynchronizer(Sets.newHashSet(toAdd.getShards().values()), null, null, redisService, shardService, sentinelBalanceService, consoleConfig).sync(); + new ShardMetaSynchronizer(Sets.newHashSet(toAdd.getShards().values()), null, null, redisService, shardService, sentinelBalanceService, consoleConfig, dcId).sync(); } catch (Exception e) { logger.error("[ClusterMetaSynchronizer][add]{}", toAdd, e); } @@ -115,9 +119,9 @@ void add() { } void bindDc(ClusterMeta toAdd){ - logger.info("[ClusterMetaSynchronizer][bindDc]{}, {}", toAdd, DcMetaSynchronizer.currentDcId); - clusterService.bindDc(new DcClusterTbl().setClusterName(toAdd.getId()).setDcName(DcMetaSynchronizer.currentDcId)); - CatEventMonitor.DEFAULT.logEvent(META_SYNC, String.format("[bindDc]%s-%s", DcMetaSynchronizer.currentDcId, toAdd.getId())); + logger.info("[ClusterMetaSynchronizer][bindDc]{}, {}", toAdd, dcId); + clusterService.bindDc(new DcClusterTbl().setClusterName(toAdd.getId()).setDcName(dcId)); + CatEventMonitor.DEFAULT.logEvent(META_SYNC, String.format("[bindDc]%s-%s", dcId, toAdd.getId())); } void createCluster(ClusterMeta toAdd){ @@ -136,18 +140,18 @@ void createCluster(ClusterMeta toAdd){ ClusterCreateDTO clusterCreateDTO = ClusterCreateDTO.builder() .clusterName(toAdd.getId()) .clusterType(toAdd.getType()) - .activeAz(DcMetaSynchronizer.currentDcId) + .activeAz(dcId) .description(toAdd.getId()) .orgName(orgName) .adminEmails(toAdd.getAdminEmails()) .build(); SingleGroupClusterCreateDTO createDTO = new SingleGroupClusterCreateDTO(clusterCreateDTO, - Collections.singletonList(DcMetaSynchronizer.currentDcId)); + Collections.singletonList(dcId)); logger.info("[ClusterMetaSynchronizer][createCluster]{}", toAdd); clusterService.createSingleGroupCluster(createDTO); - CatEventMonitor.DEFAULT.logEvent(META_SYNC, String.format("[createCluster]%s-%s", DcMetaSynchronizer.currentDcId, toAdd.getId())); + CatEventMonitor.DEFAULT.logEvent(META_SYNC, String.format("[createCluster]%s-%s", dcId, toAdd.getId())); } boolean shouldAddDc(ClusterMeta toAdd, ClusterTbl exist) { @@ -160,7 +164,7 @@ boolean existDiffTypeCluster(ClusterMeta toAdd, ClusterTbl exist) { void update() { try { - long currentDcId = dcService.find(DcMetaSynchronizer.currentDcId).getId(); + long currentDcId = dcService.find(dcId).getId(); modified.forEach(metaComparator -> { try { ClusterSyncMetaComparator clusterMetaComparator = (ClusterSyncMetaComparator) metaComparator; @@ -182,10 +186,11 @@ void update() { } logger.info("[ClusterMetaSynchronizer][update]{} -> {}, toUpdateTbl: {}", clusterMetaComparator.getCurrent(), future, currentClusterTbl); clusterService.update(currentClusterTbl); - CatEventMonitor.DEFAULT.logEvent(META_SYNC, String.format("[updateCluster]%s-%s", DcMetaSynchronizer.currentDcId, future.getId())); + CatEventMonitor.DEFAULT.logEvent(META_SYNC, String.format("[updateCluster]%s-%s", dcId, future.getId())); notifyIfClusterTypeUpdated(currentClusterTye, currentClusterTbl); } - new ShardMetaSynchronizer(clusterMetaComparator.getAdded(), clusterMetaComparator.getRemoved(), clusterMetaComparator.getMofified(), redisService, shardService, sentinelBalanceService, consoleConfig).sync(); + new ShardMetaSynchronizer(clusterMetaComparator.getAdded(), clusterMetaComparator.getRemoved(), clusterMetaComparator.getMofified(), + redisService, shardService, sentinelBalanceService, consoleConfig, dcId).sync(); } catch (Exception e) { logger.error("[ClusterMetaSynchronizer][update]{} -> {}", ((ClusterMetaComparator) metaComparator).getCurrent(), ((ClusterMetaComparator) metaComparator).getFuture(), e); } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ConsoleMetaCacheWithoutDB.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ConsoleMetaCacheWithoutDB.java new file mode 100644 index 000000000..3885a88ab --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ConsoleMetaCacheWithoutDB.java @@ -0,0 +1,36 @@ +package com.ctrip.xpipe.redis.console.resources; + +import com.ctrip.xpipe.api.monitor.Task; +import com.ctrip.xpipe.api.monitor.TransactionMonitor; +import com.ctrip.xpipe.redis.core.entity.XpipeMeta; + + +import java.util.*; + +public class ConsoleMetaCacheWithoutDB extends DefaultMetaCache { + + private ConsolePortalService consolePortalService; + + public ConsoleMetaCacheWithoutDB(ConsolePortalService consolePortalService) { + this.consolePortalService = consolePortalService; + } + + @Override + void loadCache() throws Exception { + + TransactionMonitor.DEFAULT.logTransaction("MetaCache", "load", new Task() { + + @Override + public void go() throws Exception { + XpipeMeta xpipeMeta = consolePortalService.getXpipeAllMeta(); + refreshMetaParts(); + refreshMeta(xpipeMeta); + } + + @Override + public Map getData() { + return null; + } + }); + } +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ConsolePortalService.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ConsolePortalService.java new file mode 100644 index 000000000..79f671dbd --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ConsolePortalService.java @@ -0,0 +1,198 @@ +package com.ctrip.xpipe.redis.console.resources; + +import com.ctrip.xpipe.redis.checker.controller.result.RetMessage; +import com.ctrip.xpipe.redis.checker.model.ProxyTunnelInfo; +import com.ctrip.xpipe.redis.console.config.ConsoleConfig; +import com.ctrip.xpipe.redis.console.controller.AbstractConsoleController; +import com.ctrip.xpipe.redis.console.controller.api.data.meta.RedisCreateInfo; +import com.ctrip.xpipe.redis.console.model.*; +import com.ctrip.xpipe.redis.core.console.ConsoleCheckerPath; +import com.ctrip.xpipe.redis.core.entity.XpipeMeta; +import com.ctrip.xpipe.redis.core.service.AbstractService; +import com.ctrip.xpipe.redis.core.transform.DefaultSaxParser; +import com.ctrip.xpipe.tuple.Pair; +import com.ctrip.xpipe.utils.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; +import org.springframework.stereotype.Service; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.util.UriComponents; +import org.springframework.web.util.UriComponentsBuilder; +import org.xml.sax.SAXException; + +import java.io.IOException; +import java.util.*; +import java.util.stream.Collectors; + +@Service +public class ConsolePortalService extends AbstractService { + + @Autowired + private ConsoleConfig config; + + public List getAllAzGroups() { + ResponseEntity> resp = restTemplate.exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/azGroup/all", + HttpMethod.GET, null, new ParameterizedTypeReference>(){}); + return resp.getBody(); + } + + public List getAllProxy() { + ResponseEntity> resp = restTemplate.exchange(config.getConsoleDomain() + "/api/proxies/all", + HttpMethod.GET, null, new ParameterizedTypeReference>(){}); + return resp.getBody(); + } + + public XpipeMeta getXpipeAllMeta() throws SAXException, IOException { + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + ConsoleCheckerPath.PATH_GET_ALL_META) + .queryParam("format", "xml").build(); + + String raw = restTemplate.getForObject(comp.toString(), String.class); + if (StringUtil.isEmpty(raw)) return null; + return DefaultSaxParser.parse(raw); + } + + public List getAllKeeperContainers() { + ResponseEntity> resp = restTemplate.exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/keeper_container/all", HttpMethod.GET, null, + new ParameterizedTypeReference>(){}); + return resp.getBody(); + } + + public boolean setConfig(ConfigModel model, Date util) { + MultiValueMap params = new LinkedMultiValueMap<>(); + params.add("config", model); + if (util != null) { + params.add("util", util); + } + HttpEntity> requestEntity = new HttpEntity<>(params); + ResponseEntity resp = restTemplate.exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/config/set", HttpMethod.POST, requestEntity, RetMessage.class); + RetMessage retMessage = resp.getBody(); + return retMessage.getState() == 0; + } + + public ConfigTbl getConfig(String key, String subId) { + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/config") + .queryParam("key", key) + .queryParam("subId", subId). + build(); + ResponseEntity resp = restTemplate.exchange(comp.toUri(), + HttpMethod.GET, null, ConfigTbl.class); + return resp.getBody(); + } + + public List getAllConfigs(String key) { + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/config/getAll/{key}").buildAndExpand(key); + ResponseEntity> resp = restTemplate.exchange(comp.toUri(), + HttpMethod.GET, null, new ParameterizedTypeReference>(){}); + return resp.getBody(); + } + + public List findAllByKeyAndValueAndUntilAfter(String key, String value, Date until) { + MultiValueMap params = new LinkedMultiValueMap<>(); + params.add("key", key); + params.add("value", value); + params.add("until", until); + HttpEntity> requestEntity = new HttpEntity<>(params); + ResponseEntity> resp = restTemplate.exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/config/findAll", HttpMethod.POST, requestEntity, + new ParameterizedTypeReference>(){}); + return resp.getBody(); + } + + public List getAllOrganizations() { + ResponseEntity> resp = restTemplate.exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/organizations/all", HttpMethod.GET, null, + new ParameterizedTypeReference>(){}); + return resp.getBody(); + } + + public List getAllDcs() { + ResponseEntity> resp = restTemplate.exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/dc_tbls", HttpMethod.GET, null, + new ParameterizedTypeReference>(){}); + return resp.getBody(); + } + + public List findClusterRelatedDc(String clusterId) { + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/clusters/" + clusterId + "/dcs").build(); + ResponseEntity> resp = restTemplate.exchange(comp.toUri(), HttpMethod.GET, null, + new ParameterizedTypeReference>(){}); + return resp.getBody(); + } + + public List findAllClusters() { + ResponseEntity> resp = restTemplate.exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/clusters/all", + HttpMethod.GET, null, + new ParameterizedTypeReference>(){}); + return resp.getBody(); + } + + public List findAllTunnelInfo() { + ResponseEntity> resp = restTemplate.exchange(config.getConsoleDomain() + + ConsoleCheckerPath.PATH_GET_PROXY_CHAINS, + HttpMethod.GET, null, + new ParameterizedTypeReference>(){}); + return resp.getBody(); + } + + public List findAllByDcClusterShard(long dcClusterShardId) { + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/redis/{dcClusterShardId}").buildAndExpand(dcClusterShardId); + ResponseEntity> resp = restTemplate.exchange(comp.toUri(), HttpMethod.GET, null, + new ParameterizedTypeReference>(){}); + return resp.getBody(); + } + public void updateBatchKeeperActive(List redises) { + HttpEntity> requestEntity = new HttpEntity<>(redises); + ResponseEntity resp = restTemplate.exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/redis/updateBatchKeeperActive", + HttpMethod.POST, requestEntity, RetMessage.class + ); + RetMessage ret = resp.getBody(); + if(ret.getState() != RetMessage.SUCCESS_STATE) { + throw new RuntimeException("update batch keeper active failed, state:" + ret.getState()); + } + } + + public void insertRedises(String dcId, String clusterId, String shardId, List> addrs) { + RedisCreateInfo redisCreateInfo = new RedisCreateInfo(); + redisCreateInfo.setDcId(dcId); + redisCreateInfo.setClusterId(clusterId); + redisCreateInfo.setShardName(shardId); + List redisAddrs = new ArrayList<>(); + for (Pair addr : addrs) { + redisAddrs.add(addr.getKey() + ":" + addr.getValue()); + } + redisCreateInfo.setRedises(redisAddrs.stream().collect(Collectors.joining(","))); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity requestEntity = new HttpEntity<>(redisCreateInfo, headers); + ResponseEntity resp = restTemplate.postForEntity(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/redis/insert", requestEntity, RetMessage.class); + RetMessage retMessage = resp.getBody(); + if(retMessage.getState() != RetMessage.SUCCESS_STATE) { + throw new RuntimeException(retMessage.getMessage()); + } + } + + public void bindDc(DcClusterTbl dcClusterTbl) { + HttpEntity requestEntity = new HttpEntity<>(dcClusterTbl); + ResponseEntity resp = restTemplate.exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/dc/bind", + HttpMethod.POST, requestEntity, RetMessage.class + ); + RetMessage ret = resp.getBody(); + if(ret.getState() != RetMessage.SUCCESS_STATE) { + throw new RuntimeException("bind dc fail:" + ret.getState()); + } + } +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/DcMetaSynchronizer.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/DcMetaSynchronizer.java index 16fb1bbe5..63e79dcdc 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/DcMetaSynchronizer.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/DcMetaSynchronizer.java @@ -19,70 +19,78 @@ import com.ctrip.xpipe.redis.core.util.SentinelUtil; import com.ctrip.xpipe.tuple.Pair; import com.ctrip.xpipe.utils.XpipeThreadFactory; -import com.google.common.base.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import javax.annotation.PostConstruct; import java.util.*; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import java.util.regex.Pattern; -@Component public class DcMetaSynchronizer implements MetaSynchronizer { + private static Logger logger = LoggerFactory.getLogger(DcMetaSynchronizer.class); - static final String currentDcId = FoundationService.DEFAULT.getDataCenter(); - private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1, XpipeThreadFactory.create("XPipe-Meta-Sync")); + + private final String currentDcId; + + private ScheduledExecutorService scheduledExecutorService; + private OuterClientService outerClientService = OuterClientService.DEFAULT; - @Autowired + public DcMetaSynchronizer(ConsoleConfig consoleConfig, + MetaCache metaCache, RedisService redisService, ShardService shardService, + ClusterService clusterService, DcService dcService, + OrganizationService organizationService, SentinelBalanceService sentinelBalanceService, + ClusterTypeUpdateEventFactory clusterTypeUpdateEventFactory, + String dcId + + ) { + this.consoleConfig = consoleConfig; + this.metaCache = metaCache; + this.redisService = redisService; + this.shardService = shardService; + this.clusterService = clusterService; + this.dcService = dcService; + this.organizationService = organizationService; + this.sentinelBalanceService = sentinelBalanceService; + this.clusterTypeUpdateEventFactory = clusterTypeUpdateEventFactory; + this.currentDcId = dcId; + this.scheduledExecutorService = Executors.newScheduledThreadPool(1, XpipeThreadFactory.create("XPipe-Meta-Sync-" + dcId)); + } + private MetaCache metaCache; - @Autowired private RedisService redisService; - @Autowired private ShardService shardService; - @Autowired private ClusterService clusterService; - @Autowired private DcService dcService; - @Autowired private OrganizationService organizationService; - @Autowired private ConsoleConfig consoleConfig; - @Autowired(required = false) - private ConsoleLeaderElector consoleLeaderElector; - - @Autowired private SentinelBalanceService sentinelBalanceService; - @Autowired private ClusterTypeUpdateEventFactory clusterTypeUpdateEventFactory; private Map organizations = new HashMap<>(); - @PostConstruct public void start() { scheduledExecutorService.scheduleWithFixedDelay(new Runnable() { @Override public void run() { - if (consoleLeaderElector != null && consoleLeaderElector.amILeader()) { - sync(); - } + sync(); } }, consoleConfig.getOuterClientSyncInterval(), consoleConfig.getOuterClientSyncInterval(), TimeUnit.MILLISECONDS); } + public void stop() { + scheduledExecutorService.shutdown(); + } + public void sync() { TransactionMonitor transaction = TransactionMonitor.DEFAULT; transaction.logTransactionSwallowException("meta.sync", currentDcId, new Task() { @@ -103,7 +111,7 @@ public void go() throws Exception { new ClusterMetaSynchronizer(dcMetaComparator.getAdded(), dcMetaComparator.getRemoved(), dcMetaComparator.getMofified(), dcService, clusterService, shardService, redisService, organizationService, sentinelBalanceService, consoleConfig, - clusterTypeUpdateEventFactory).sync(); + clusterTypeUpdateEventFactory, currentDcId).sync(); } catch (Throwable e) { logger.error("[DcMetaSynchronizer][sync]", e); } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/DefaultMetaCache.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/DefaultMetaCache.java index 70f9d59c4..8949d24b8 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/DefaultMetaCache.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/DefaultMetaCache.java @@ -161,7 +161,7 @@ public Map getData() { }); } - private void refreshMetaParts() { + protected void refreshMetaParts() { try { int parts = Math.max(1, consoleConfig.getClusterDividedParts()); logger.debug("[refreshClusterParts] start parts {}", parts); diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/MetaSynchronizer.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/MetaSynchronizer.java new file mode 100644 index 000000000..7115ba5cb --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/MetaSynchronizer.java @@ -0,0 +1,131 @@ +package com.ctrip.xpipe.redis.console.resources; + +import com.ctrip.xpipe.api.foundation.FoundationService; +import com.ctrip.xpipe.redis.console.cluster.ConsoleCrossDcServer; +import com.ctrip.xpipe.redis.console.cluster.ConsoleLeaderElector; +import com.ctrip.xpipe.redis.console.config.ConsoleConfig; +import com.ctrip.xpipe.redis.console.notifier.cluster.ClusterTypeUpdateEventFactory; +import com.ctrip.xpipe.redis.console.sentinel.SentinelBalanceService; +import com.ctrip.xpipe.redis.console.service.*; +import com.ctrip.xpipe.redis.core.meta.MetaCache; +import com.ctrip.xpipe.utils.XpipeThreadFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.util.*; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +@Service +public class MetaSynchronizer { + + private ConsoleCrossDcServer consoleCrossDcServer; + + private FoundationService foundationService; + + Map dcMetaSynchronizers; + + private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1, XpipeThreadFactory.create("XPipe-Meta-Sync"));; + + private static Logger logger = LoggerFactory.getLogger(MetaSynchronizer.class); + + @Autowired + public MetaSynchronizer(ConsoleConfig consoleConfig, ConsoleLeaderElector leaderElector, + MetaCache metaCache, RedisService redisService, ShardService shardService, + ClusterService clusterService, DcService dcService, OrganizationService organizationService, + SentinelBalanceService sentinelBalanceService, ClusterTypeUpdateEventFactory clusterTypeUpdateEventFactory, + FoundationService foundationService, ConsoleCrossDcServer consoleCrossDcServer + + ) { + this.consoleConfig = consoleConfig; + this.consoleLeaderElector = leaderElector; + this.metaCache = metaCache; + this.redisService = redisService; + this.shardService = shardService; + this.clusterService = clusterService; + this.dcService = dcService; + this.organizationService = organizationService; + this.sentinelBalanceService = sentinelBalanceService; + this.clusterTypeUpdateEventFactory = clusterTypeUpdateEventFactory; + this.foundationService = foundationService; + this.consoleCrossDcServer = consoleCrossDcServer; + dcMetaSynchronizers = new HashMap<>(); + } + + private MetaCache metaCache; + + private RedisService redisService; + + private ShardService shardService; + + private ClusterService clusterService; + + private DcService dcService; + + private OrganizationService organizationService; + + private ConsoleConfig consoleConfig; + + private ConsoleLeaderElector consoleLeaderElector; + + private SentinelBalanceService sentinelBalanceService; + + private ClusterTypeUpdateEventFactory clusterTypeUpdateEventFactory; + + @PostConstruct + public void postConstruct() { + scheduledExecutorService.scheduleWithFixedDelay(new Runnable() { + @Override + public void run() { + sync(); + } + }, consoleConfig.getOuterClientSyncInterval(), consoleConfig.getOuterClientSyncInterval(), TimeUnit.MILLISECONDS); + } + + public void sync() { + Set dcs = consoleConfig.getExtraSyncDC(); + for(String dc : dcs) { + if(consoleCrossDcServer.amILeader()) { + if(!dcMetaSynchronizers.containsKey(dc)) { + // meta.sync.external.dc 下dc 同步任务交给 cross dc leader + dcMetaSynchronizers.put(dc, new DcMetaSynchronizer(consoleConfig, metaCache, redisService, shardService, + clusterService, dcService, organizationService, sentinelBalanceService, clusterTypeUpdateEventFactory, dc)); + dcMetaSynchronizers.get(dc).start(); + + } + } else { + if(dcMetaSynchronizers.containsKey(dc)) { + dcMetaSynchronizers.get(dc).stop(); + dcMetaSynchronizers.remove(dc); + } + } + + } + String currentDc = foundationService.getDataCenter(); + if(dcs.contains(currentDc)) { + if(dcMetaSynchronizers.containsKey(currentDc)) { + dcMetaSynchronizers.get(currentDc).stop(); + dcMetaSynchronizers.remove(currentDc); + } + return; + } + if(consoleLeaderElector.amILeader()) { + if(!dcMetaSynchronizers.containsKey(currentDc)) { + // 没有被cross dc leader 托管需要自己完成 + dcMetaSynchronizers.put(currentDc, new DcMetaSynchronizer(consoleConfig, metaCache, redisService, shardService, + clusterService, dcService, organizationService, sentinelBalanceService, clusterTypeUpdateEventFactory, currentDc)); + dcMetaSynchronizers.get(currentDc).start(); + } + } else { + if(dcMetaSynchronizers.containsKey(currentDc)) { + dcMetaSynchronizers.get(currentDc).stop(); + dcMetaSynchronizers.remove(currentDc); + } + } + } + +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/PersistenceCacheWithoutDB.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/PersistenceCacheWithoutDB.java new file mode 100644 index 000000000..3f6b08597 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/PersistenceCacheWithoutDB.java @@ -0,0 +1,20 @@ +package com.ctrip.xpipe.redis.console.resources; + +import com.ctrip.xpipe.redis.checker.CheckerConsoleService; +import com.ctrip.xpipe.redis.console.config.ConsoleConfig; + +public class PersistenceCacheWithoutDB extends CheckerPersistenceCache { + + private ConsoleConfig consoleConfig; + + public PersistenceCacheWithoutDB(ConsoleConfig config, CheckerConsoleService service) { + super(config, service); + this.service = service; + this.consoleConfig = config; + } + + protected String getConsoleAddress() { + return consoleConfig.getConsoleDomain(); + } + +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/RedisMetaSynchronizer.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/RedisMetaSynchronizer.java index e127bd02b..8e2183a68 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/RedisMetaSynchronizer.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/RedisMetaSynchronizer.java @@ -21,13 +21,18 @@ public class RedisMetaSynchronizer implements MetaSynchronizer { private Set added; private Set removed; private Set modified; + private String dcId; - public RedisMetaSynchronizer(Set added, Set removed, Set modified, RedisService redisService) { + public RedisMetaSynchronizer(Set added, Set removed, + Set modified, RedisService redisService, + String dcId + ) { this.added = added; this.removed = removed; this.modified = modified; this.redisService = redisService; + this.dcId = dcId; } public void sync() { @@ -48,7 +53,7 @@ void remove() { shardId = ((RedisMeta) instanceNode).parent().getId(); } logger.info("[RedisMetaSynchronizer][deleteRedises]{}", removed); - redisService.deleteRedises(DcMetaSynchronizer.currentDcId, clusterId, shardId, toDeleted); + redisService.deleteRedises(dcId, clusterId, shardId, toDeleted); CatEventMonitor.DEFAULT.logEvent(META_SYNC, String.format("[deleteRedises]%s", toDeleted)); } catch (Exception e) { logger.error("[RedisMetaSynchronizer][deleteRedises]", e); @@ -70,7 +75,7 @@ void add() { shardId = ((RedisMeta) instanceNode).parent().getId(); } logger.info("[RedisMetaSynchronizer][insertRedises]{}", added); - redisService.insertRedises(DcMetaSynchronizer.currentDcId, clusterId, shardId, toAdded); + redisService.insertRedises(dcId, clusterId, shardId, toAdded); CatEventMonitor.DEFAULT.logEvent(META_SYNC, String.format("[addRedises]%s", toAdded)); } catch (Exception e) { logger.error("[RedisMetaSynchronizer][insertRedises]", e); diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ShardMetaSynchronizer.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ShardMetaSynchronizer.java index a01b29e87..00f80c492 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ShardMetaSynchronizer.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ShardMetaSynchronizer.java @@ -29,8 +29,13 @@ public class ShardMetaSynchronizer implements MetaSynchronizer { private ShardService shardService; private SentinelBalanceService sentinelBalanceService; private ConsoleConfig consoleConfig; + private String dcId; - public ShardMetaSynchronizer(Set added, Set removed, Set modified, RedisService redisService, ShardService shardService, SentinelBalanceService sentinelBalanceService, ConsoleConfig consoleConfig) { + public ShardMetaSynchronizer(Set added, Set removed, Set modified, + RedisService redisService, ShardService shardService, + SentinelBalanceService sentinelBalanceService, ConsoleConfig consoleConfig, + String dcId + ) { this.added = added; this.removed = removed; this.modified = modified; @@ -38,6 +43,7 @@ public ShardMetaSynchronizer(Set added, Set removed, Set {}", shardMetaComparator.getCurrent(), shardMetaComparator.getFuture()); - new RedisMetaSynchronizer(shardMetaComparator.getAdded(), shardMetaComparator.getRemoved(), shardMetaComparator.getMofified(), redisService).sync(); + new RedisMetaSynchronizer(shardMetaComparator.getAdded(), shardMetaComparator.getRemoved(), + shardMetaComparator.getMofified(), redisService, dcId).sync(); } catch (Exception e) { logger.error("[ShardMetaSynchronizer][update]{} -> {}", ((ShardMetaComparator) metaComparator).getCurrent(), ((ShardMetaComparator) metaComparator).getFuture(), e); } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/AzServiceImpl.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/AzServiceImpl.java index 0351ae45f..01fe2c2df 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/AzServiceImpl.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/AzServiceImpl.java @@ -7,6 +7,7 @@ import com.ctrip.xpipe.redis.console.service.AbstractConsoleService; import com.ctrip.xpipe.redis.console.service.AzService; import com.ctrip.xpipe.redis.console.service.DcService; +import com.ctrip.xpipe.redis.console.service.KeeperContainerService; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.collect.Lists; @@ -34,7 +35,7 @@ public class AzServiceImpl extends AbstractConsoleService private AzDao azDao; @Autowired - private KeeperContainerServiceImpl keeperContainerService; + private KeeperContainerService keeperContainerService; @Override public void addAvailableZone(AzCreateInfo createInfo) { diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ClusterServiceImpl.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ClusterServiceImpl.java index 201074373..844ecfef9 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ClusterServiceImpl.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ClusterServiceImpl.java @@ -4,6 +4,8 @@ import com.ctrip.xpipe.cluster.ClusterType; import com.ctrip.xpipe.cluster.DcGroupType; import com.ctrip.xpipe.redis.checker.controller.result.RetMessage; +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; import com.ctrip.xpipe.redis.console.annotation.DalTransaction; import com.ctrip.xpipe.redis.console.cache.AzGroupCache; import com.ctrip.xpipe.redis.console.config.ConsoleConfig; @@ -51,10 +53,10 @@ import com.google.common.collect.Maps; import com.google.common.collect.Sets; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; -import org.springframework.util.StringUtils; import org.unidal.dal.jdbc.DalException; import java.util.*; @@ -66,6 +68,8 @@ import static com.ctrip.xpipe.redis.core.protocal.RedisProtocol.KEEPER_PORT_DEFAULT; @Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(false) public class ClusterServiceImpl extends AbstractConsoleService implements ClusterService { @Autowired diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ClusterServiceWithoutDB.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ClusterServiceWithoutDB.java new file mode 100644 index 000000000..8c71f1bd3 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ClusterServiceWithoutDB.java @@ -0,0 +1,423 @@ +package com.ctrip.xpipe.redis.console.service.impl; + +import com.ctrip.xpipe.cache.TimeBoundCache; +import com.ctrip.xpipe.cluster.ClusterType; +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; +import com.ctrip.xpipe.redis.console.config.ConsoleConfig; +import com.ctrip.xpipe.redis.console.dto.ClusterDTO; +import com.ctrip.xpipe.redis.console.dto.ClusterUpdateDTO; +import com.ctrip.xpipe.redis.console.dto.MultiGroupClusterCreateDTO; +import com.ctrip.xpipe.redis.console.dto.SingleGroupClusterCreateDTO; +import com.ctrip.xpipe.redis.console.migration.status.ClusterStatus; +import com.ctrip.xpipe.redis.console.model.*; +import com.ctrip.xpipe.redis.console.model.consoleportal.ClusterListUnhealthyClusterModel; +import com.ctrip.xpipe.redis.console.model.consoleportal.RouteInfoModel; +import com.ctrip.xpipe.redis.console.resources.ConsolePortalService; +import com.ctrip.xpipe.redis.console.service.ClusterService; +import com.ctrip.xpipe.redis.console.service.DcService; +import com.ctrip.xpipe.utils.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.util.*; +import java.util.stream.IntStream; + +@Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(true) +public class ClusterServiceWithoutDB implements ClusterService { + + @Autowired + private DcService dcService; + + @Autowired + private ConsolePortalService consolePortalService; + + private TimeBoundCache> allClusters; + + @Autowired + private ConsoleConfig config; + + @PostConstruct + public void init() { + allClusters = new TimeBoundCache<>(config::getCacheRefreshInterval, consolePortalService::findAllClusters); + } + + @Override + public ClusterTbl find(String clusterName) { + List all = allClusters.getData(); + for(ClusterTbl clusterTbl : all) { + if(StringUtil.trimEquals(clusterTbl.getClusterName(), clusterName)) { + return clusterTbl; + } + } + return null; + } + + @Override + public List findClustersByGroupType(String groupType) { + List all = allClusters.getData(); + List result = new ArrayList<>(); + for(ClusterTbl clusterTbl : all) { + if(StringUtil.trimEquals(clusterTbl.getGroupType(), groupType)) { + result.add(clusterTbl); + } + } + + return result; + } + + @Override + public List findAllByNames(List clusterNames) { + List all = allClusters.getData(); + Set names = new HashSet<>(clusterNames); + List result = new ArrayList<>(); + for(ClusterTbl clusterTbl : all) { + if(names.contains(clusterTbl.getClusterName())) { + result.add(clusterTbl); + } + } + return result; + } + + @Override + public ClusterTbl findClusterAndOrg(String clusterName) { + return find(clusterName); + } + + @Override + public ClusterStatus clusterStatus(String clusterName) { + ClusterTbl clusterTbl = find(clusterName); + if(clusterTbl != null) { + return Enum.valueOf(ClusterStatus.class, clusterTbl.getStatus()); + } + return null; + } + + @Override + public List getClusterRelatedDcs(String clusterName) { + ClusterTbl clusterTbl = find(clusterName); + if(clusterTbl != null) { + return clusterTbl.getDcInfo(); + } + return Collections.emptyList(); + } + + @Override + public boolean containsRedisInstance(String clusterName) { + throw new UnsupportedOperationException(); + } + + @Override + public ClusterTbl find(long clusterId) { + List all = allClusters.getData(); + for(ClusterTbl clusterTbl : all) { + if(clusterTbl.getId() == clusterId) { + return clusterTbl; + } + } + return null; + } + + @Override + public List findAllClustersWithOrgInfo() { + return allClusters.getData(); + } + + @Override + public List findClustersWithOrgInfoByClusterType(String clusterType) { + List all = allClusters.getData(); + List result = new ArrayList<>(); + for(ClusterTbl clusterTbl : all) { + if(StringUtil.trimEquals(clusterTbl.getClusterType(), clusterType)) { + result.add(clusterTbl); + } + } + + return result; + } + + @Override + public List findClustersWithOrgInfoByActiveDcId(long activeDc) { + List all = allClusters.getData(); + List result = new ArrayList<>(); + for(ClusterTbl clusterTbl : all) { + if(clusterTbl.getDcId() == activeDc) { + result.add(clusterTbl); + } + } + + return result; + } + + @Override + public List findAllClusterNames() { + List all = allClusters.getData(); + List result = new ArrayList<>(); + for(ClusterTbl clusterTbl : all) { + result.add(clusterTbl.getClusterName()); + } + return result; + } + + @Override + public String findClusterTag(String clusterName) { + ClusterTbl clusterTbl = find(clusterName); + if(clusterTbl != null) { + return clusterTbl.getTag(); + } + return null; + } + + @Override + public void updateClusterTag(String clusterName, String clusterTag) { + throw new UnsupportedOperationException(); + } + + @Override + public Long getCountByActiveDc(long activeDcId) { + return Long.valueOf(findClustersWithOrgInfoByActiveDcId(activeDcId).size()); + } + + @Override + public Map getAllCountByActiveDc() { + throw new UnsupportedOperationException(); + } + + @Override + public Map getMigratableClustersCountByActiveDc() { + List dcs = dcService.findAllDcs(); + Map counts = new HashMap<>(); + + dcs.forEach(dcTbl -> { + counts.put(dcTbl.getDcName(), getMigratableClustersCountByActiveDcId(dcTbl.getId())); + }); + + return counts; + } + + @Override + public Long getCountByActiveDcAndClusterType(long activeDc, String clusterType) { + long result = 0; + for(ClusterTbl clusterTbl : allClusters.getData()) { + if(StringUtil.trimEquals(clusterTbl.getClusterType(), clusterType) && activeDc == clusterTbl.getId()) { + result++; + } + } + return result; + } + + @Override + public Long getAllCount() { + throw new UnsupportedOperationException(); + } + + @Override + public ClusterDTO getCluster(String clusterName) { + throw new UnsupportedOperationException(); + } + + @Override + public List getClusters(String clusterType) { + throw new UnsupportedOperationException(); + } + + @Override + public List getClusterWithShards(String clusterType) { + throw new UnsupportedOperationException(); + } + + @Override + public ClusterTbl createCluster(ClusterModel clusterModel) { + throw new UnsupportedOperationException(); + } + + @Override + public void createSingleGroupCluster(SingleGroupClusterCreateDTO clusterCreateDTO) { + throw new UnsupportedOperationException(); + } + + @Override + public void createMultiGroupCluster(MultiGroupClusterCreateDTO clusterCreateDTO) { + throw new UnsupportedOperationException(); + } + + @Override + public String updateCluster(ClusterUpdateDTO clusterUpdateDTO) { + throw new UnsupportedOperationException(); + } + + @Override + public void updateCluster(String clusterName, ClusterModel cluster) { + throw new UnsupportedOperationException(); + } + + @Override + public void updateActivedcId(long id, long activeDcId) { + throw new UnsupportedOperationException(); + } + + @Override + public void updateStatusById(long id, ClusterStatus clusterStatus, long migrationEventId) { + throw new UnsupportedOperationException(); + } + + @Override + public void deleteCluster(String clusterName) { + throw new UnsupportedOperationException(); + } + + @Override + public void bindDc(DcClusterTbl dcClusterTbl) { + consolePortalService.bindDc(dcClusterTbl); + } + + @Override + public void bindDc(String clusterName, String dcName) { + throw new UnsupportedOperationException(); + } + + @Override + public void unbindAz(String clusterName, String azName) { + throw new UnsupportedOperationException(); + } + + @Override + public void unbindDc(String clusterName, String dcName) { + throw new UnsupportedOperationException(); + } + + @Override + public void update(ClusterTbl cluster) { + throw new UnsupportedOperationException(); + } + + @Override + public void exchangeName(Long formerClusterId, String formerClusterName, Long latterClusterId, String latterClusterName) { + throw new UnsupportedOperationException(); + } + + @Override + public void exchangeRegion(Long formerClusterId, String formerClusterName, Long latterClusterId, String latterClusterName, String regionName) { + throw new UnsupportedOperationException(); + } + + @Override + public void upgradeAzGroup(String clusterName) { + throw new UnsupportedOperationException(); + } + + @Override + public void downgradeAzGroup(String clusterName) { + throw new UnsupportedOperationException(); + } + + @Override + public void bindRegionAz(String clusterName, String regionName, String azName) { + throw new UnsupportedOperationException(); + } + + @Override + public Set findMigratingClusterNames() { + return Collections.emptySet(); + } + + @Override + public List findErrorMigratingClusters() { + return Collections.emptyList(); + } + + @Override + public List findMigratingClusters() { + return Collections.emptyList(); + } + + @Override + public List findUnhealthyClusters() { + return Collections.emptyList(); + } + + @Override + public List findAllClusterByDcNameBind(String dcName) { + return Collections.emptyList(); + } + + @Override + public List findAllClusterByDcNameBindAndType(String dcName, String clusterType) { + return Collections.emptyList(); + } + + @Override + public List findActiveClustersByDcName(String dcName) { + return Collections.emptyList(); + } + + @Override + public List findActiveClustersByDcNameAndType(String dcName, String clusterType) { + return Collections.emptyList(); + } + + @Override + public List findAllClustersByDcName(String dcName) { + return Collections.emptyList(); + } + + @Override + public List findAllClusterByKeeperContainer(long keeperContainerId) { + throw new UnsupportedOperationException(); + } + + @Override + public List> divideClusters(int partsCnt) { + List allClusters = findAllClustersWithOrgInfo(); + if (null == allClusters) return Collections.emptyList(); + List> parts = new ArrayList<>(partsCnt); + IntStream.range(0, partsCnt).forEach(i -> parts.add(new HashSet<>())); + + allClusters.forEach(clusterTbl -> parts.get((int) (clusterTbl.getId() % partsCnt)).add(clusterTbl.getClusterName())); + return parts; + } + + @Override + public List findClusterDefaultRoutesBySrcDcNameAndClusterName(String backupDcId, String clusterName) { + throw new UnsupportedOperationException(); + } + + @Override + public List findClusterUsedRoutesBySrcDcNameAndClusterName(String backupDcId, String clusterName) { + throw new UnsupportedOperationException(); + } + + @Override + public List findClusterDesignateRoutesBySrcDcNameAndClusterName(String dcName, String clusterName) { + throw new UnsupportedOperationException(); + } + + @Override + public void updateClusterDesignateRoutes(String clusterName, String srcDcName, List newDesignatedRoutes) { + throw new UnsupportedOperationException(); + } + + @Override + public UnexpectedRouteUsageInfoModel findUnexpectedRouteUsageInfoModel() { + throw new UnsupportedOperationException(); + } + + @Override + public void completeReplicationByClusterAndReplDirection(ClusterTbl cluster, ReplDirectionInfoModel replDirection) { + throw new UnsupportedOperationException(); + } + + private long getMigratableClustersCountByActiveDcId(long activeDc) { + List dcClusters = findClustersWithOrgInfoByActiveDcId(activeDc); + int count = 0; + for (ClusterTbl clusterTbl : dcClusters) { + if (ClusterType.lookup(clusterTbl.getClusterType()).supportMigration()) + count++; + } + return count; + } +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ConfigServiceImpl.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ConfigServiceImpl.java index bdc4fabca..ab4328a64 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ConfigServiceImpl.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ConfigServiceImpl.java @@ -2,6 +2,8 @@ import com.ctrip.xpipe.api.foundation.FoundationService; import com.ctrip.xpipe.api.monitor.EventMonitor; +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; import com.ctrip.xpipe.redis.console.config.ConsoleConfig; import com.ctrip.xpipe.redis.console.dao.ConfigDao; import com.ctrip.xpipe.redis.console.election.CrossDcLeaderElectionAction; @@ -20,6 +22,7 @@ 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 org.unidal.dal.jdbc.DalException; @@ -32,6 +35,8 @@ * Nov 27, 2017 */ @Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(false) public class ConfigServiceImpl implements ConfigService { @Autowired @@ -73,7 +78,7 @@ public void setKeyKeeperContainerStandard(ConfigModel config) throws Exception { } catch (NumberFormatException e) { throw new RuntimeException(String.format("value %s should be number ", config.getVal())); } - configDao.setConfig(config); + setConfig(config); } @Override @@ -94,7 +99,7 @@ public void setKeyKeeperContainerIoRate(ConfigModel config) throws Exception { } catch (NumberFormatException e) { throw new RuntimeException(String.format("value %s should be number ", config.getVal())); } - configDao.setConfig(config); + setConfig(config); } @Override @@ -102,7 +107,7 @@ public void startAlertSystem(ConfigModel config) throws DalException { logger.info("[startAlertSystem] start alert system, config: {}", config); config.setKey(KEY_ALERT_SYSTEM_ON).setVal(String.valueOf(true)); - configDao.setConfig(config); + setConfig(config); } @Override @@ -115,7 +120,7 @@ public void stopAlertSystem(ConfigModel config, int hours) throws DalException { config.setKey(KEY_ALERT_SYSTEM_ON).setVal(String.valueOf(false)); logger.info("[stopAlertSystem] stop alert system, config: {}", config); - configDao.setConfigAndUntil(config, date); + setConfigAndUntil(config, date); if(previousStateOn) { logger.info("[stopAlertSystem] Alert System was On, alert this operation"); alertSystemOffChecker.startAlert(); @@ -127,7 +132,7 @@ public void startSentinelAutoProcess(ConfigModel config) throws DalException { logger.info("[startSentinelAutoProcess] start sentinel auto process, config: {}", config); config.setKey(KEY_SENTINEL_AUTO_PROCESS).setVal(String.valueOf(true)); - configDao.setConfig(config); + setConfig(config); } @Override @@ -138,7 +143,7 @@ public void stopSentinelAutoProcess(ConfigModel config, int hours) throws DalExc boolean previousStateOn = isSentinelAutoProcess(); config.setKey(KEY_SENTINEL_AUTO_PROCESS).setVal(String.valueOf(false)); - configDao.setConfigAndUntil(config, date); + setConfigAndUntil(config, date); if(previousStateOn) { sentinelAutoProcessChecker.startAlert(); } @@ -152,7 +157,7 @@ public void startKeeperBalanceInfoCollect(ConfigModel config, int hours) throws boolean previousStateOff = !isKeeperBalanceInfoCollectOn(); config.setKey(KEY_KEEPER_BALANCE_INFO_COLLECT).setVal(String.valueOf(true)); - configDao.setConfigAndUntil(config, date); + setConfigAndUntil(config, date); if(previousStateOff) { keeperBalanceInfoCollectOnChecker.startAlert(); } @@ -162,7 +167,7 @@ public void startKeeperBalanceInfoCollect(ConfigModel config, int hours) throws public void stopKeeperBalanceInfoCollect(ConfigModel config) throws DalException { logger.info("[stopKeeperBalanceInfoCollect] stop keeper balance info collect, config: {}", config); config.setKey(KEY_KEEPER_BALANCE_INFO_COLLECT).setVal(String.valueOf(false)); - configDao.setConfig(config); + setConfig(config); } @Override @@ -171,7 +176,7 @@ public void startClusterAlert(ConfigModel config) throws DalException { config.setKey(KEY_CLUSTER_ALERT_EXCLUDE) .setVal(String.valueOf(false)); logChangeEvent(config, null); - configDao.setConfig(config); + setConfig(config); } @Override @@ -182,7 +187,7 @@ public void stopClusterAlert(ConfigModel config, int minutes) throws DalExceptio config.setKey(KEY_CLUSTER_ALERT_EXCLUDE) .setVal(String.valueOf(true)); logChangeEvent(config, date); - configDao.setConfigAndUntil(config, date); + setConfigAndUntil(config, date); } @Override @@ -192,7 +197,7 @@ public void startSentinelCheck(ConfigModel config) throws DalException { config.setKey(KEY_SENTINEL_CHECK_EXCLUDE) .setVal(String.valueOf(false)); logChangeEvent(config, null); - configDao.setConfig(config); + setConfig(config); } @Override @@ -204,7 +209,7 @@ public void stopSentinelCheck(ConfigModel config, int minutes) throws DalExcepti config.setKey(KEY_SENTINEL_CHECK_EXCLUDE) .setVal(String.valueOf(true)); logChangeEvent(config, date); - configDao.setConfigAndUntil(config, date); + setConfigAndUntil(config, date); } @Override @@ -225,11 +230,11 @@ public void updateCrossDcLeader(ConfigModel config, Date until) throws DalExcept config.setKey(CrossDcLeaderElectionAction.KEY_LEASE_CONFIG); config.setSubKey(crossDcLeaderLeaseName); - configDao.setConfig(config, until); + setConfigAndUntil(config, until); } public String getCrossDcLeader() throws DalException { - ConfigTbl leaseConfig = configDao.getByKeyAndSubId(CrossDcLeaderElectionAction.KEY_LEASE_CONFIG, crossDcLeaderLeaseName); + ConfigTbl leaseConfig = getByKeyAndSubId(CrossDcLeaderElectionAction.KEY_LEASE_CONFIG, crossDcLeaderLeaseName); // only return when lease is active if (new Date().compareTo(leaseConfig.getUntil()) < 0) return leaseConfig.getValue(); @@ -247,7 +252,7 @@ public boolean shouldAlert(String cluster) { private boolean getConfigBooleanByKeyAndSubKey(String key, String subKey, boolean defaultVal) { try { - ConfigTbl config = configDao.getByKeyAndSubId(key, subKey); + ConfigTbl config = getByKeyAndSubId(key, subKey); if (null == config || (new Date()).after(config.getUntil())) { return defaultVal; } @@ -268,7 +273,7 @@ public List getActiveClusterAlertExcludeConfig() { } private List getActiveConfig(String key, String val) { - List configTbls = configDao.findAllByKeyAndValueAndUntilAfter(key, val, new Date()); + List configTbls = findAllByKeyAndValueAndUntilAfter(key, val, new Date()); if (configTbls.isEmpty()) return Collections.emptyList(); List models = new ArrayList<>(); @@ -295,7 +300,7 @@ public boolean isKeeperBalanceInfoCollectOn() { @Override public Date getAlertSystemRecoverTime() { try { - return configDao.getByKey(KEY_ALERT_SYSTEM_ON).getUntil(); + return getConfigByKey(KEY_ALERT_SYSTEM_ON).getUntil(); } catch (DalException e) { logger.error("[getAlertSystemRecovertIME]", e); return null; @@ -305,7 +310,7 @@ public Date getAlertSystemRecoverTime() { @Override public Date getSentinelAutoProcessRecoverTime() { try { - return configDao.getByKey(KEY_SENTINEL_AUTO_PROCESS).getUntil(); + return getConfigByKey(KEY_SENTINEL_AUTO_PROCESS).getUntil(); } catch (DalException e) { logger.error("[getSentinelAutoProcessRecoverTime]", e); return null; @@ -315,7 +320,7 @@ public Date getSentinelAutoProcessRecoverTime() { @Override public Date getKeeperBalanceInfoCollectRecoverTime() { try { - return configDao.getByKey(KEY_KEEPER_BALANCE_INFO_COLLECT).getUntil(); + return getConfigByKey(KEY_KEEPER_BALANCE_INFO_COLLECT).getUntil(); } catch (DalException e) { logger.error("[getKeeperBalanceInfoCollectRecoverTime]", e); return null; @@ -338,7 +343,7 @@ public void doIgnoreMigrationSystemAvailability(boolean ignore) { try { ConfigModel configModel = getOrCreate(KEY_IGNORE_MIGRATION_SYSTEM_AVAILABILITY, String.valueOf(ignore)); configModel.setVal(String.valueOf(ignore)); - configDao.setConfig(configModel); + setConfig(configModel); } catch (Exception e) { logger.error("[ignoreMigrationSystemAvailability]", e); throw new DalUpdateException(e.getMessage()); @@ -362,7 +367,7 @@ public void setAllowAutoMigration(boolean allow) throws DalException { ConfigModel configModel = getOrCreate(KEY_ALLOW_AUTO_MIGRATION, String.valueOf(allow)); boolean origin = Boolean.parseBoolean(configModel.getVal()); configModel.setVal(String.valueOf(allow)); - configDao.setConfig(configModel); + setConfig(configModel); if (origin && !allow) { logger.info("[setAllowAutoMigration] Auto Migration stop"); @@ -382,7 +387,7 @@ public ConfigModel getConfig(String key) { @Override public ConfigModel getConfig(String key, String subId) { try { - ConfigTbl configTbl = configDao.getByKeyAndSubId(key, subId); + ConfigTbl configTbl = getByKeyAndSubId(key, subId); return new ConfigModel(configTbl); } catch (DalException e) { logger.error("[getConfig]", e); @@ -393,7 +398,7 @@ public ConfigModel getConfig(String key, String subId) { @Override public List getConfigs(String key) { try { - List configTbl = configDao.getAllByKey(key); + List configTbl = getAllByKey(key); List configModels = new ArrayList<>(); configTbl.forEach(config -> configModels.add(new ConfigModel(config))); return configModels; @@ -406,7 +411,7 @@ public List getConfigs(String key) { private ConfigModel getOrCreate(String key, String defaultValue) { ConfigTbl configTbl = null; try { - configTbl = configDao.getByKey(key); + configTbl = getConfigByKey(key); } catch (DalException e) { logger.error("[getOrCreate]", e); } @@ -427,7 +432,7 @@ private ConfigModel createConfig(String key, String defaultValue) { config.setKey(key); config.setVal(defaultValue); try { - configDao.setConfig(config); + setConfig(config); } catch (DalException e) { logger.error("[createConfig]", e); } @@ -444,7 +449,7 @@ private boolean getAndResetFalseIfExpired(String key) { private boolean getAndResetExpectIfExpired(String key, boolean defaultVal) { try { - ConfigTbl config = configDao.getByKey(key); + ConfigTbl config = getConfigByKey(key); boolean result = Boolean.parseBoolean(config.getValue()); if(result != defaultVal) { Date expireDate = config.getUntil(); @@ -453,7 +458,7 @@ private boolean getAndResetExpectIfExpired(String key, boolean defaultVal) { .setVal(String.valueOf(defaultVal)).setUpdateUser("System"); if(currentDate.after(expireDate)) { logger.info("[getAndResetExpectIfExpired] time expired, reset to be {}", defaultVal); - configDao.setConfig(configModel); + setConfig(configModel); result = defaultVal; } } @@ -480,4 +485,29 @@ public void setAutoMigrationOffChecker(AutoMigrationOffChecker checker) { this.autoMigrationOffChecker = checker; } + protected void setConfig(ConfigModel config) throws DalException { + configDao.setConfig(config); + } + + protected ConfigTbl getConfigByKey(String key) throws DalException { + return configDao.getByKey(key); + } + + protected void setConfigAndUntil(ConfigModel config, Date date) throws DalException { + configDao.setConfigAndUntil(config, date); + } + + protected ConfigTbl getByKeyAndSubId(String key, String subId) throws DalException { + return configDao.getByKeyAndSubId(key, subId); + } + + protected List findAllByKeyAndValueAndUntilAfter(String key, String value, Date until) { + return configDao.findAllByKeyAndValueAndUntilAfter(key, value, until); + } + + protected List getAllByKey(String key) throws DalException { + return configDao.getAllByKey(key); + } + + } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ConfigServiceWithoutDB.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ConfigServiceWithoutDB.java new file mode 100644 index 000000000..56b561644 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ConfigServiceWithoutDB.java @@ -0,0 +1,59 @@ +package com.ctrip.xpipe.redis.console.service.impl; + +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; +import com.ctrip.xpipe.redis.console.config.ConsoleConfig; +import com.ctrip.xpipe.redis.console.model.ConfigModel; +import com.ctrip.xpipe.redis.console.model.ConfigTbl; +import com.ctrip.xpipe.redis.console.resources.ConsolePortalService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; +import org.springframework.stereotype.Service; +import org.unidal.dal.jdbc.DalException; + +import java.util.Date; +import java.util.List; + +@Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(true) +public class ConfigServiceWithoutDB extends ConfigServiceImpl{ + + private ConsolePortalService consolePortalService; + + @Autowired + public ConfigServiceWithoutDB(ConsoleConfig consoleConfig, ConsolePortalService consolePortalService) { + super(consoleConfig); + this.consolePortalService = consolePortalService; + } + + @Override + protected void setConfig(ConfigModel config) throws DalException { + consolePortalService.setConfig(config, null); + } + + @Override + protected ConfigTbl getConfigByKey(String key) throws DalException { + return consolePortalService.getConfig(key, null); + } + + @Override + protected void setConfigAndUntil(ConfigModel config, Date date) throws DalException { + consolePortalService.setConfig(config, date); + } + + @Override + protected ConfigTbl getByKeyAndSubId(String key, String subId) throws DalException { + return consolePortalService.getConfig(key, subId); + } + + @Override + protected List findAllByKeyAndValueAndUntilAfter(String key, String value, Date until) { + return consolePortalService.findAllByKeyAndValueAndUntilAfter(key, value, until); + } + + @Override + protected List getAllByKey(String key) throws DalException { + return consolePortalService.getAllConfigs(key); + } +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/DcServiceImpl.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/DcServiceImpl.java index 1f90c0e5d..391a136a1 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/DcServiceImpl.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/DcServiceImpl.java @@ -1,6 +1,8 @@ package com.ctrip.xpipe.redis.console.service.impl; import com.ctrip.xpipe.cluster.ClusterType; +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; import com.ctrip.xpipe.redis.console.model.DcModel; import com.ctrip.xpipe.redis.console.model.DcTbl; import com.ctrip.xpipe.redis.console.model.DcTblDao; @@ -14,6 +16,7 @@ import com.ctrip.xpipe.redis.core.entity.ClusterMeta; import com.ctrip.xpipe.redis.core.entity.DcMeta; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.unidal.dal.jdbc.DalException; @@ -22,6 +25,8 @@ import java.util.stream.Collectors; @Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(false) public class DcServiceImpl extends AbstractConsoleService implements DcService { @Autowired @@ -184,7 +189,6 @@ public Integer doQuery() throws DalException { @Override public DcModel findDcModelByDcName(String dcName) { - DcTbl dcTbl = find(dcName); return convertDcTblToDcModel(find(dcName)); } @@ -210,7 +214,7 @@ public Integer doQuery() throws DalException { }); } - private DcModel convertDcTblToDcModel(DcTbl dcTbl) { + public static DcModel convertDcTblToDcModel(DcTbl dcTbl) { if (dcTbl == null) { return null; } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/DcServiceWithoutDB.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/DcServiceWithoutDB.java new file mode 100644 index 000000000..5e7f8f694 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/DcServiceWithoutDB.java @@ -0,0 +1,155 @@ +package com.ctrip.xpipe.redis.console.service.impl; + +import com.ctrip.xpipe.cache.TimeBoundCache; +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; +import com.ctrip.xpipe.redis.console.config.ConsoleConfig; +import com.ctrip.xpipe.redis.console.model.DcModel; +import com.ctrip.xpipe.redis.console.model.DcTbl; +import com.ctrip.xpipe.redis.console.model.consoleportal.DcListDcModel; +import com.ctrip.xpipe.redis.console.resources.ConsolePortalService; +import com.ctrip.xpipe.redis.console.service.DcService; +import com.ctrip.xpipe.utils.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.util.*; + +import static com.ctrip.xpipe.redis.console.service.impl.DcServiceImpl.convertDcTblToDcModel; + +@Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(true) +public class DcServiceWithoutDB implements DcService { + + @Autowired + private ConsolePortalService consolePortalService; + + @Autowired + private ConsoleConfig config; + + private TimeBoundCache> dcs; + + @PostConstruct + public void postConstruct() { + dcs = new TimeBoundCache<>(config::getCacheRefreshInterval, consolePortalService::getAllDcs); + } + + @Override + public DcTbl find(String dcName) { + List dcs = findAllDcs(); + for (DcTbl dc : dcs) { + if (StringUtil.trimEquals(dc.getDcName(), dcName)) { + return dc; + } + } + return null; + } + + @Override + public DcTbl find(long dcId) { + List dcs = findAllDcs(); + for (DcTbl dc : dcs) { + if (dc.getId() == dcId) { + return dc; + } + } + return null; + } + + @Override + public String getDcName(long dcId) { + DcTbl dc = find(dcId); + if(dc != null) { + return dc.getDcName(); + } + return ""; + } + + @Override + public List findAllDcs() { + return dcs.getData(); + } + + @Override + public List findAllDcNames() { + List all = findAllDcs(); + List result = new ArrayList<>(); + for (DcTbl dc : all) { + result.add(dc.getDcName()); + } + return result; + } + + @Override + public List findAllDcBasic() { + return findAllDcs(); + } + + @Override + public List findClusterRelatedDc(String clusterName) { + return consolePortalService.findClusterRelatedDc(clusterName); + } + + @Override + public DcTbl findByDcName(String activeDcName) { + List all = findAllDcs(); + for (DcTbl dc : all) { + if (StringUtil.trimEquals(dc.getDcName(), activeDcName)) { + return dc; + } + } + return null; + } + + @Override + public Map dcNameMap() { + List allDcs = findAllDcs(); + Map result = new HashMap<>(); + allDcs.forEach(dcTbl -> result.put(dcTbl.getId(), dcTbl.getDcName())); + return result; + } + + @Override + public Map dcNameIdMap() { + List allDcs = findAllDcs(); + Map result = new HashMap<>(); + allDcs.forEach(dcTbl -> result.put(dcTbl.getDcName(), dcTbl.getId())); + return result; + } + + @Override + public Map dcNameZoneMap() { + List allDcs = findAllDcs(); + Map result = new HashMap<>(); + allDcs.forEach(dcTbl -> result.put(dcTbl.getDcName(), dcTbl.getZoneId())); + return result; + } + + @Override + public List findAllDcsRichInfo() { + throw new UnsupportedOperationException(); + } + + @Override + public void insertWithPartField(long zoneId, String dcName, String description) { + throw new UnsupportedOperationException(); + } + + @Override + public DcModel findDcModelByDcName(String dcName) { + return convertDcTblToDcModel(find(dcName)); + } + + @Override + public DcModel findDcModelByDcId(long dcId) { + return convertDcTblToDcModel(find(dcId)); + } + + @Override + public void updateDcZone(DcModel dcModel) { + throw new UnsupportedOperationException(); + } +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/KeeperContainerServiceImpl.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/KeeperContainerServiceImpl.java index a14afbd67..3dd243d7f 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/KeeperContainerServiceImpl.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/KeeperContainerServiceImpl.java @@ -3,6 +3,8 @@ import com.ctrip.xpipe.endpoint.HostPort; import com.ctrip.xpipe.exception.XpipeRuntimeException; import com.ctrip.xpipe.redis.checker.model.KeeperContainerUsedInfoModel; +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; import com.ctrip.xpipe.redis.console.constant.XPipeConsoleConstant; import com.ctrip.xpipe.redis.console.controller.api.data.meta.KeeperContainerCreateInfo; import com.ctrip.xpipe.redis.console.exception.BadRequestException; @@ -19,6 +21,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -34,6 +37,8 @@ import java.util.stream.IntStream; @Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(false) public class KeeperContainerServiceImpl extends AbstractConsoleService implements KeeperContainerService { diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/KeeperContainerServiceWithoutDB.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/KeeperContainerServiceWithoutDB.java new file mode 100644 index 000000000..1c357ae07 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/KeeperContainerServiceWithoutDB.java @@ -0,0 +1,212 @@ +package com.ctrip.xpipe.redis.console.service.impl; + +import com.ctrip.xpipe.cache.TimeBoundCache; +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; +import com.ctrip.xpipe.redis.console.config.ConsoleConfig; +import com.ctrip.xpipe.redis.console.controller.api.data.meta.KeeperContainerCreateInfo; +import com.ctrip.xpipe.redis.console.model.KeeperContainerInfoModel; +import com.ctrip.xpipe.redis.console.model.KeepercontainerTbl; +import com.ctrip.xpipe.redis.console.model.KeepercontainerTblDao; +import com.ctrip.xpipe.redis.console.resources.ConsolePortalService; +import com.ctrip.xpipe.redis.console.service.KeeperContainerService; +import com.ctrip.xpipe.redis.core.entity.KeeperInstanceMeta; +import com.ctrip.xpipe.redis.core.entity.KeeperTransMeta; +import com.ctrip.xpipe.spring.RestTemplateFactory; +import com.ctrip.xpipe.utils.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestOperations; + +import javax.annotation.PostConstruct; +import java.util.*; +import java.util.stream.IntStream; + +@Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(true) +public class KeeperContainerServiceWithoutDB implements KeeperContainerService { + + private ConsolePortalService consolePortalService; + + private ConsoleConfig config; + + private TimeBoundCache> allKeepContainer; + + private static RestOperations restTemplate = RestTemplateFactory.createCommonsHttpRestTemplate(10, 20, 3000, 5000); + + @Autowired + public KeeperContainerServiceWithoutDB(ConsolePortalService consolePortalService, ConsoleConfig config) { + this.config = config; + this.consolePortalService = consolePortalService; + allKeepContainer = new TimeBoundCache<>(config::getCacheRefreshInterval, consolePortalService::getAllKeeperContainers); + } + + @Override + public KeepercontainerTbl find(long id) { + List allKeeper = allKeepContainer.getData(); + for (KeepercontainerTbl keeper : allKeeper) { + if(keeper.getKeepercontainerId() == id) { + return keeper; + } + } + return null; + } + + @Override + public KeepercontainerTbl find(String ip) { + List allKeeper = findAll(); + for(KeepercontainerTbl keeperContainer : allKeeper) { + if(StringUtil.trimEquals(keeperContainer.getKeepercontainerIp(), ip)) { + return keeperContainer; + } + } + return null; + } + + @Override + public List findAll() { + return allKeepContainer.getData(); + } + + @Override + public List findAllByDcName(String dcName) { + List allKeeper = findAll(); + List result = new ArrayList<>(); + for(KeepercontainerTbl keeperContainer : allKeeper) { + if(StringUtil.trimEquals(keeperContainer.getDcName(), dcName)) { + result.add(keeperContainer); + } + } + return result; + } + + @Override + public List findAllActiveByDcName(String dcName) { + List allKeeper = findAll(); + List result = new ArrayList<>(); + for(KeepercontainerTbl keeperContainer : allKeeper) { + if(StringUtil.trimEquals(keeperContainer.getDcName(), dcName) && keeperContainer.isKeepercontainerActive()) { + result.add(keeperContainer); + } + } + return result; + } + + @Override + public List findKeeperCount(String dcName) { + throw new UnsupportedOperationException(); + } + + @Override + public List findBestKeeperContainersByDcCluster(String dcName, String clusterName) { + throw new UnsupportedOperationException(); + } + + @Override + public List findBestKeeperContainersByDcCluster(String dcName, String clusterName, boolean skipAzFilter) { + throw new UnsupportedOperationException(); + } + + @Override + public List getKeeperContainerByAz(Long azId) { + List result = new ArrayList<>(); + for(KeepercontainerTbl keeperContainer : allKeepContainer.getData()) { + if(keeperContainer.getAzId() == azId) { + result.add(keeperContainer); + } + } + return result; + } + + @Override + public List> divideKeeperContainers(int partsCount) { + List all = findAll(); + if (all == null) return Collections.emptyList(); + + List> result = new ArrayList<>(partsCount); + IntStream.range(0, partsCount).forEach(i -> result.add(new HashSet<>())); + + all.forEach(keeperContainer -> result.get((int) keeperContainer.getKeepercontainerId() % partsCount) + .add(keeperContainer.getKeepercontainerId())); + + return result; + } + + @Override + public List findAllInfos() { + throw new UnsupportedOperationException(); + } + + @Override + public KeeperContainerInfoModel findKeeperContainerInfoModelById(long id) { + throw new UnsupportedOperationException(); + } + + @Override + public List findAvailableKeeperContainerInfoModelsByDcAzAndOrg(String dcName, String azName, String orgName) { + throw new UnsupportedOperationException(); + } + + @Override + public void addKeeperContainer(KeeperContainerCreateInfo createInfo) { + throw new UnsupportedOperationException(); + } + + @Override + public List getDcAllKeeperContainers(String dcName) { + throw new UnsupportedOperationException(); + } + + @Override + public void updateKeeperContainer(KeeperContainerCreateInfo createInfo) { + throw new UnsupportedOperationException(); + } + + @Override + public void deleteKeeperContainer(String keepercontainerIp, int keepercontainerPort) { + throw new UnsupportedOperationException(); + } + + @Override + public void addKeeperContainerByInfoModel(KeeperContainerInfoModel keeperContainerInfoModel) { + throw new UnsupportedOperationException(); + } + + @Override + public void updateKeeperContainerByInfoModel(KeeperContainerInfoModel keeperContainerInfoModel) { + throw new UnsupportedOperationException(); + } + + @Override + public List getAllKeepers(String keeperContainerIp) { + throw new UnsupportedOperationException(); + } + + @Override + public void resetKeeper(String activeKeeperIp, Long replId) { + KeeperTransMeta keeperInstanceMeta = new KeeperTransMeta(); + keeperInstanceMeta.setReplId(replId); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity requestEntity = new HttpEntity<>(keeperInstanceMeta, headers); + restTemplate.exchange(String.format("http://%s:8080/keepers/election/reset", activeKeeperIp), + HttpMethod.POST, requestEntity, Void.class); + } + + @Override + public Map keeperContainerIdDcMap() { + Map keeperContainerIdDcMap = new HashMap<>(); + List allKeeperContainers = findAll(); + allKeeperContainers.forEach((keeperContainer) -> { + keeperContainerIdDcMap.put(keeperContainer.getKeyKeepercontainerId(), keeperContainer.getKeepercontainerDc()); + }); + return keeperContainerIdDcMap; + } + +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/OrganizationServiceImpl.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/OrganizationServiceImpl.java index 0eeebb33e..836f47434 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/OrganizationServiceImpl.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/OrganizationServiceImpl.java @@ -3,6 +3,8 @@ import com.ctrip.xpipe.api.monitor.TransactionMonitor; import com.ctrip.xpipe.api.organization.Organization; import com.ctrip.xpipe.api.organization.OrganizationModel; +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; import com.ctrip.xpipe.redis.console.dao.OrganizationDao; import com.ctrip.xpipe.redis.console.model.OrganizationTbl; import com.ctrip.xpipe.redis.console.model.OrganizationTblDao; @@ -12,6 +14,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; import org.springframework.stereotype.Service; import java.util.HashSet; @@ -27,6 +30,8 @@ * Sep 04, 2017 */ @Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(false) public class OrganizationServiceImpl extends AbstractConsoleService implements OrganizationService { diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/OrganizationServiceWithoutDB.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/OrganizationServiceWithoutDB.java new file mode 100644 index 000000000..773dffcdf --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/OrganizationServiceWithoutDB.java @@ -0,0 +1,84 @@ +package com.ctrip.xpipe.redis.console.service.impl; + +import com.ctrip.xpipe.cache.TimeBoundCache; +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; +import com.ctrip.xpipe.redis.console.config.ConsoleConfig; +import com.ctrip.xpipe.redis.console.model.OrganizationTbl; +import com.ctrip.xpipe.redis.console.resources.ConsolePortalService; +import com.ctrip.xpipe.redis.console.service.OrganizationService; +import com.ctrip.xpipe.utils.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.util.List; + +@Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(true) +public class OrganizationServiceWithoutDB implements OrganizationService { + + @Autowired + private ConsolePortalService consolePortalService; + + @Autowired + private ConsoleConfig config; + + private TimeBoundCache> allOrganizations; + + @PostConstruct + public void postConstruct(){ + allOrganizations = new TimeBoundCache<>(config::getCacheRefreshInterval, consolePortalService::getAllOrganizations); + } + + @Override + public void updateOrganizations() { + // not cross dc leader for disable database + throw new UnsupportedOperationException(); + } + + @Override + public List getAllOrganizations() { + return allOrganizations.getData(); + } + + @Override + public OrganizationTbl getOrganizationTblByCMSOrganiztionId(long organizationId) { + List all = allOrganizations.getData(); + for(OrganizationTbl organizationTbl : all){ + if(organizationTbl.getOrgId() == organizationId){ + return organizationTbl; + } + } + return null; + } + + @Override + public OrganizationTbl getOrgByName(String name) { + List all = allOrganizations.getData(); + for(OrganizationTbl organizationTbl : all){ + if (StringUtil.trimEquals(organizationTbl.getOrgName(), name)) { + return organizationTbl; + } + } + return null; + } + + @Override + public List getInvolvedOrgs() { + throw new UnsupportedOperationException(); + } + + @Override + public OrganizationTbl getOrganization(long xpipeOrgId) { + List all = allOrganizations.getData(); + for(OrganizationTbl organizationTbl : all){ + if (organizationTbl.getId() == xpipeOrgId) { + return organizationTbl; + } + } + return null; + } +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ProxyServiceImpl.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ProxyServiceImpl.java index 455040133..b59089ced 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ProxyServiceImpl.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ProxyServiceImpl.java @@ -6,6 +6,8 @@ import com.ctrip.xpipe.endpoint.HostPort; import com.ctrip.xpipe.redis.checker.controller.result.RetMessage; import com.ctrip.xpipe.redis.checker.model.ProxyTunnelInfo; +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; import com.ctrip.xpipe.redis.console.config.ConsoleConfig; import com.ctrip.xpipe.redis.console.dao.ProxyDao; import com.ctrip.xpipe.redis.console.model.*; @@ -28,6 +30,7 @@ import com.google.common.collect.Maps; import com.google.common.util.concurrent.MoreExecutors; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; @@ -43,7 +46,10 @@ *

* Jun 19, 2018 */ + @Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(false) public class ProxyServiceImpl extends AbstractService implements ProxyService { @Autowired @@ -52,9 +58,6 @@ public class ProxyServiceImpl extends AbstractService implements ProxyService { @Autowired private DcService dcService; - @Autowired - private ProxyService proxyService; - @Autowired private ProxyChainCollector proxyChainCollector; diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ProxyServiceWithoutDB.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ProxyServiceWithoutDB.java new file mode 100644 index 000000000..eb22b1f77 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ProxyServiceWithoutDB.java @@ -0,0 +1,164 @@ +package com.ctrip.xpipe.redis.console.service.impl; + +import com.ctrip.xpipe.cache.TimeBoundCache; +import com.ctrip.xpipe.endpoint.HostPort; +import com.ctrip.xpipe.redis.checker.controller.result.RetMessage; +import com.ctrip.xpipe.redis.checker.model.ProxyTunnelInfo; +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; +import com.ctrip.xpipe.redis.console.config.ConsoleConfig; +import com.ctrip.xpipe.redis.console.model.ProxyModel; +import com.ctrip.xpipe.redis.console.model.ProxyPingStatsModel; +import com.ctrip.xpipe.redis.console.model.ProxyTbl; +import com.ctrip.xpipe.redis.console.model.consoleportal.ProxyInfoModel; +import com.ctrip.xpipe.redis.console.proxy.ProxyChain; +import com.ctrip.xpipe.redis.console.proxy.ProxyMonitorCollector; +import com.ctrip.xpipe.redis.console.proxy.impl.DefaultTunnelInfo; +import com.ctrip.xpipe.redis.console.resources.ConsolePortalService; +import com.ctrip.xpipe.redis.console.service.ProxyService; +import com.ctrip.xpipe.utils.StringUtil; +import org.springframework.context.annotation.Conditional; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(true) +public class ProxyServiceWithoutDB implements ProxyService { + + private ConsolePortalService consolePortalService; + + private ConsoleConfig config; + + public ProxyServiceWithoutDB(ConsolePortalService consolePortalService, ConsoleConfig config) { + this.consolePortalService = consolePortalService; + this.config = config; + } + + private TimeBoundCache> allProxyCache; + + private TimeBoundCache> allTunnelCache; + + @PostConstruct + public void postConstruct(){ + allProxyCache = new TimeBoundCache<>(config::getCacheRefreshInterval, consolePortalService::getAllProxy); + allTunnelCache = new TimeBoundCache<>(config::getCacheRefreshInterval, consolePortalService::findAllTunnelInfo); + } + + @Override + public List getActiveProxies() { + return allProxyCache.getData(). + stream(). + filter(model -> model.isActive()). + collect(Collectors.toList()); + } + + @Override + public List getAllProxies() { + return allProxyCache.getData(); + } + + @Override + public List getMonitorActiveProxiesByDc(String dcName) { + return allProxyCache.getData(). + stream(). + filter(model -> model.isMonitorActive() + && StringUtil.trimEquals(model.getDcName(), dcName)). + collect(Collectors.toList()); + } + + @Override + public void updateProxy(ProxyModel model) { + throw new UnsupportedOperationException(); + } + + @Override + public void deleteProxy(long id) { + throw new UnsupportedOperationException(); + } + + @Override + public void addProxy(ProxyModel model) { + throw new UnsupportedOperationException(); + } + + @Override + public List getActiveProxyTbls() { + return Collections.emptyList(); + } + + @Override + public List getActiveProxyUrisByDc(String dcName) { + throw new UnsupportedOperationException(); + } + + @Override + public ProxyChain getProxyChain(String backupDcId, String clusterId, String shardId, String peerDcId) { + throw new UnsupportedOperationException(); + } + + @Override + public ProxyChain getProxyChain(String tunnelId) { + throw new UnsupportedOperationException(); + } + + @Override + public List getProxyTunnels(String dcId, String ip) { + throw new UnsupportedOperationException(); + } + + @Override + public Map> getProxyChains(String backupDcId, String clusterId) { + throw new UnsupportedOperationException(); + } + + @Override + public List getProxyPingStatsModels(String dcName) { + throw new UnsupportedOperationException(); + } + + @Override + public List getAllProxyMonitorCollectors() { + throw new UnsupportedOperationException(); + } + + @Override + public List getAllProxyInfo() { + throw new UnsupportedOperationException(); + } + + @Override + public RetMessage deleteProxyChain(List proxies) { + throw new UnsupportedOperationException(); + } + + @Override + public Map proxyIdUriMap() { + throw new UnsupportedOperationException(); + } + + @Override + public Map proxyUriIdMap() { + throw new UnsupportedOperationException(); + } + + @Override + public List getAllProxyTunnels() { + return allTunnelCache.getData(); + } + + @Override + public ProxyTunnelInfo getProxyTunnelInfo(String backupDcId, String clusterId, String shardId, String peerDcId) { + throw new UnsupportedOperationException(); + } + + @Override + public void closeProxyTunnel(ProxyTunnelInfo proxyTunnelInfo) { + throw new UnsupportedOperationException(); + } +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RedisServiceImpl.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RedisServiceImpl.java index 6eb67bdf1..be027a6f9 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RedisServiceImpl.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RedisServiceImpl.java @@ -1,6 +1,8 @@ package com.ctrip.xpipe.redis.console.service.impl; import com.ctrip.xpipe.cluster.ClusterType; +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; import com.ctrip.xpipe.redis.console.config.ConsoleConfig; import com.ctrip.xpipe.redis.console.constant.XPipeConsoleConstant; import com.ctrip.xpipe.redis.console.dao.RedisDao; @@ -18,6 +20,7 @@ import com.ctrip.xpipe.utils.StringUtil; import com.google.common.collect.Lists; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; import org.springframework.stereotype.Service; import org.unidal.dal.jdbc.DalException; @@ -27,6 +30,8 @@ @Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(false) public class RedisServiceImpl extends AbstractConsoleService implements RedisService { @Autowired diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RedisServiceWithoutDB.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RedisServiceWithoutDB.java new file mode 100644 index 000000000..97e4ad9bd --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RedisServiceWithoutDB.java @@ -0,0 +1,126 @@ +package com.ctrip.xpipe.redis.console.service.impl; + +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; +import com.ctrip.xpipe.redis.console.model.RedisTbl; +import com.ctrip.xpipe.redis.console.model.ShardModel; +import com.ctrip.xpipe.redis.console.resources.ConsolePortalService; +import com.ctrip.xpipe.redis.console.service.KeeperBasicInfo; +import com.ctrip.xpipe.redis.console.service.RedisService; +import com.ctrip.xpipe.redis.console.service.exception.ResourceNotFoundException; +import com.ctrip.xpipe.tuple.Pair; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; +import org.springframework.stereotype.Service; +import org.unidal.dal.jdbc.DalException; + +import java.util.List; + +@Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(true) +public class RedisServiceWithoutDB implements RedisService { + + @Autowired + private ConsolePortalService consolePortalService; + + @Override + public RedisTbl find(long id) { + throw new UnsupportedOperationException(); + } + + @Override + public RedisTbl findByIpPort(String ip, int port) { + throw new UnsupportedOperationException(); + } + + @Override + public List findAllRedisWithSameIP(String ip) { + throw new UnsupportedOperationException(); + } + + @Override + public List findAllByDcClusterShard(long dcClusterShardId) { + return consolePortalService.findAllByDcClusterShard(dcClusterShardId); + } + + @Override + public List findAllRedisesByDcClusterName(String dcId, String clusterId) { + throw new UnsupportedOperationException(); + } + + @Override + public List findAllKeepersByDcClusterName(String dcId, String clusterId) { + throw new UnsupportedOperationException(); + } + + @Override + public List findAllByDcClusterShard(String dcId, String clusterId, String shardId) throws ResourceNotFoundException { + throw new UnsupportedOperationException(); + } + + @Override + public List findRedisesByDcClusterShard(String dcId, String clusterId, String shardId) throws ResourceNotFoundException { + throw new UnsupportedOperationException(); + } + + @Override + public List findKeepersByDcClusterShard(String dcId, String clusterId, String shardId) throws ResourceNotFoundException { + throw new UnsupportedOperationException(); + } + + @Override + public void insertRedises(String dcId, String clusterId, String shardId, List> addrs) throws DalException, ResourceNotFoundException { + consolePortalService.insertRedises(dcId, clusterId, shardId, addrs); + } + + @Override + public void deleteRedises(String dcId, String clusterId, String shardId, List> addrs) throws ResourceNotFoundException { + throw new UnsupportedOperationException(); + } + + @Override + public int insertKeepers(String dcId, String clusterId, String shardId, List keepers) throws DalException, ResourceNotFoundException { + throw new UnsupportedOperationException(); + } + + @Override + public List deleteKeepers(String dcId, String clusterId, String shardId) throws DalException, ResourceNotFoundException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateByPK(RedisTbl redis) { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBatchMaster(List redises) { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBatchKeeperActive(List redises) { + consolePortalService.updateBatchKeeperActive(redises); + } + + @Override + public void updateSourceKeepers(String srcDcName, String clusterName, String shardName, long dstDcId, ShardModel sourceShard) { + throw new UnsupportedOperationException(); + } + + @Override + public void updateRedises(String dcName, String clusterName, String shardName, ShardModel shardModel) { + throw new UnsupportedOperationException(); + } + + @Override + public List findAllKeeperContainerCountInfo() { + throw new UnsupportedOperationException(); + } + + @Override + public List findClusterIdsByKeeperContainer(long keeperContainerId) { + throw new UnsupportedOperationException(); + } +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/AdvancedDcMetaService.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/AdvancedDcMetaService.java index 1be7fc4d7..267acb8eb 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/AdvancedDcMetaService.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/AdvancedDcMetaService.java @@ -7,6 +7,8 @@ import com.ctrip.xpipe.command.ParallelCommandChain; import com.ctrip.xpipe.command.RetryCommandFactory; import com.ctrip.xpipe.concurrent.DefaultExecutorFactory; +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.model.*; @@ -26,6 +28,7 @@ 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; @@ -45,6 +48,8 @@ * Apr 02, 2018 */ @Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(false) public class AdvancedDcMetaService implements DcMetaService { private static final Logger logger = LoggerFactory.getLogger(AdvancedDcMetaService.class); @@ -174,7 +179,11 @@ public Map getAllDcMetas() throws Exception { for (DcTbl dcTbl : dcTblList) { ZoneTbl zoneTbl = zoneService.findById(dcTbl.getZoneId()); - DcMeta dcMeta = new DcMeta().setId(dcTbl.getDcName()).setLastModifiedTime(dcTbl.getDcLastModifiedTime()).setZone(zoneTbl.getZoneName()); + DcMeta dcMeta = new DcMeta() + .setId(dcTbl.getDcName()) + .setLastModifiedTime(dcTbl.getDcLastModifiedTime()) + .setZone(zoneTbl.getZoneName()) + .setNo(dcTbl.getId()); dcMetaMap.put(dcMeta.getId().toUpperCase(), dcMeta); chain.add(retry3TimesUntilSuccess(new GetAllSentinelCommand(dcMeta))); diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/DcMetaServiceWithoutDB.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/DcMetaServiceWithoutDB.java new file mode 100644 index 000000000..0a2862bb6 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/DcMetaServiceWithoutDB.java @@ -0,0 +1,51 @@ +package com.ctrip.xpipe.redis.console.service.meta.impl; + +import com.ctrip.xpipe.redis.checker.spring.ConsoleDisableDbCondition; +import com.ctrip.xpipe.redis.checker.spring.DisableDbMode; +import com.ctrip.xpipe.redis.console.service.meta.DcMetaService; +import com.ctrip.xpipe.redis.core.entity.ClusterMeta; +import com.ctrip.xpipe.redis.core.entity.DcMeta; +import com.ctrip.xpipe.redis.core.meta.MetaCache; +import com.ctrip.xpipe.utils.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +@Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(true) +public class DcMetaServiceWithoutDB implements DcMetaService { + + @Autowired + private MetaCache metaCache; + + @Override + public DcMeta getDcMeta(String dcName) throws Exception { + return metaCache.getXpipeMeta().getDcs().get(dcName); + } + + @Override + public DcMeta getDcMeta(String dcName, Set allowTypes) throws Exception { + DcMeta dcMeta = metaCache.getXpipeMeta().getDcs().get(dcName); + DcMeta result = new DcMeta(); + BeanUtils.copyProperties(dcMeta, result); + Map clusterMetas = dcMeta.getClusters(); + Map newClusterMetas = new HashMap<>(); + for(ClusterMeta clusterMeta : clusterMetas.values()) { + if(allowTypes.contains(clusterMeta.getType())) { + newClusterMetas.put(clusterMeta.getId(), clusterMeta); + } + } + BeanUtils.setProperty(result, "m_clusters", newClusterMetas); + return result; + } + + @Override + public Map getAllDcMetas() throws Exception { + return metaCache.getXpipeMeta().getDcs(); + } +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/migration/impl/DefaultCheckMigrationCommandBuilder.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/migration/impl/DefaultCheckMigrationCommandBuilder.java index 731a282b9..79587b4e6 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/migration/impl/DefaultCheckMigrationCommandBuilder.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/migration/impl/DefaultCheckMigrationCommandBuilder.java @@ -6,7 +6,6 @@ import com.ctrip.xpipe.command.CommandRetryWrapper; import com.ctrip.xpipe.redis.console.config.ConsoleConfig; import com.ctrip.xpipe.redis.checker.controller.result.RetMessage; -import com.ctrip.xpipe.redis.console.healthcheck.nonredis.migration.MigrationSystemAvailableChecker; import com.ctrip.xpipe.redis.console.job.retry.RetryCondition; import com.ctrip.xpipe.redis.console.model.ClusterTbl; import com.ctrip.xpipe.redis.console.model.DcTbl; @@ -15,6 +14,7 @@ import com.ctrip.xpipe.redis.console.service.migration.CheckMigrationCommandBuilder; import com.ctrip.xpipe.redis.core.entity.KeeperMeta; import com.ctrip.xpipe.redis.core.exception.NoResourceException; +import com.ctrip.xpipe.redis.core.meta.MetaCache; import com.ctrip.xpipe.redis.core.metaserver.META_SERVER_SERVICE; import com.ctrip.xpipe.redis.core.service.AbstractService; import com.ctrip.xpipe.tuple.Pair; @@ -39,6 +39,8 @@ public class DefaultCheckMigrationCommandBuilder extends AbstractService impleme private ConsoleConfig consoleConfig; + private MetaCache metaCache; + public DefaultCheckMigrationCommandBuilder(ScheduledExecutorService scheduled, DcService dcService, ClusterService clusterService, OuterClientService outerClientService, ConsoleConfig consoleConfig) { this.scheduled = scheduled; @@ -48,6 +50,8 @@ public DefaultCheckMigrationCommandBuilder(ScheduledExecutorService scheduled, D this.consoleConfig = consoleConfig; } + + @Override public Command checkCommand(CHECK_MIGRATION_SYSTEM_STEP step) { Pair clusterShard = consoleConfig.getClusterShardForMigrationSysCheck(); diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/spring/CheckerContextConfig.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/spring/CheckerContextConfig.java index 595743375..daac3caa7 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/spring/CheckerContextConfig.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/spring/CheckerContextConfig.java @@ -2,6 +2,7 @@ import com.ctrip.xpipe.api.foundation.FoundationService; import com.ctrip.xpipe.redis.checker.*; +import com.ctrip.xpipe.redis.checker.KeeperContainerService; import com.ctrip.xpipe.redis.checker.alert.AlertManager; import com.ctrip.xpipe.redis.checker.cluster.AllCheckerLeaderElector; import com.ctrip.xpipe.redis.checker.cluster.GroupCheckerLeaderElector; @@ -26,7 +27,7 @@ import com.ctrip.xpipe.redis.console.migration.auto.MonitorManager; import com.ctrip.xpipe.redis.console.redis.DefaultSentinelManager; import com.ctrip.xpipe.redis.console.resources.*; -import com.ctrip.xpipe.redis.console.service.DcClusterShardService; +import com.ctrip.xpipe.redis.console.service.*; import com.ctrip.xpipe.redis.console.service.impl.DcClusterShardServiceImpl; import com.ctrip.xpipe.redis.console.service.impl.DefaultDcRelationsService; import com.ctrip.xpipe.redis.console.service.meta.BeaconMetaService; diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/spring/ConsoleContextConfig.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/spring/ConsoleContextConfig.java index 7f2f6f63c..2668fe79c 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/spring/ConsoleContextConfig.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/spring/ConsoleContextConfig.java @@ -6,7 +6,6 @@ import com.ctrip.xpipe.redis.checker.DcRelationsService; import com.ctrip.xpipe.redis.checker.KeeperContainerService; import com.ctrip.xpipe.redis.checker.PersistenceCache; -import com.ctrip.xpipe.redis.checker.config.CheckerConfig; import com.ctrip.xpipe.redis.checker.config.impl.CheckConfigBean; import com.ctrip.xpipe.redis.checker.config.impl.CommonConfigBean; import com.ctrip.xpipe.redis.checker.config.impl.ConsoleConfigBean; @@ -14,6 +13,7 @@ import com.ctrip.xpipe.redis.checker.healthcheck.actions.ping.PingService; import com.ctrip.xpipe.redis.checker.impl.DefaultKeeperContainerService; import com.ctrip.xpipe.redis.checker.impl.TestMetaCache; +import com.ctrip.xpipe.redis.checker.resource.DefaultCheckerConsoleService; import com.ctrip.xpipe.redis.checker.spring.ConsoleServerMode; import com.ctrip.xpipe.redis.checker.spring.ConsoleServerModeCondition; import com.ctrip.xpipe.redis.console.cluster.ConsoleLeaderElector; @@ -29,8 +29,7 @@ import com.ctrip.xpipe.redis.console.keeper.KeeperContainerUsedInfoAnalyzer; import com.ctrip.xpipe.redis.console.keeper.impl.DefaultKeeperContainerUsedInfoAnalyzer; import com.ctrip.xpipe.redis.console.keeper.impl.KeeperContainerMigrationAnalyzer; -import com.ctrip.xpipe.redis.console.resources.DefaultMetaCache; -import com.ctrip.xpipe.redis.console.resources.DefaultPersistenceCache; +import com.ctrip.xpipe.redis.console.resources.*; import com.ctrip.xpipe.redis.console.service.DcClusterShardService; import com.ctrip.xpipe.redis.console.service.RedisInfoService; import com.ctrip.xpipe.redis.console.service.impl.*; @@ -87,7 +86,10 @@ public FilterRegistrationBean userAccessFilter(ConsoleConfig consoleConfig) { @Bean @Lazy @Profile(AbstractProfile.PROFILE_NAME_PRODUCTION) - public MetaCache metaCache() { + public MetaCache metaCache(ConsolePortalService consolePortalService, ConsoleConfig config) { + if(config.disableDb()) { + return new ConsoleMetaCacheWithoutDB(consolePortalService); + } return new DefaultMetaCache(); } @@ -123,6 +125,7 @@ public DcRelationsService dcRelationsService(){ } @Bean + @DependsOn("metaCache") @Profile(AbstractProfile.PROFILE_NAME_PRODUCTION) public ConsoleLeaderElector consoleLeaderElector() { return new ConsoleLeaderElector(); @@ -157,12 +160,16 @@ public DefaultCrossMasterDelayService defaultCrossMasterDelayService(FoundationS } @Bean - public PersistenceCache persistenceCache3(CheckerConfig config, + public PersistenceCache persistenceCache3(ConsoleConfig config, AlertEventService alertEventService, ConfigDao configDao, DcClusterShardService dcClusterShardService, RedisDao redisDao, - ClusterDao clusterDao) { + ClusterDao clusterDao + ) { + if(config.disableDb()) { + return new PersistenceCacheWithoutDB(config, new DefaultCheckerConsoleService()); + } return new DefaultPersistenceCache( config, alertEventService, diff --git a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitorManagerTest.java b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitorManagerTest.java index c2723679d..10b8fa190 100644 --- a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitorManagerTest.java +++ b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitorManagerTest.java @@ -6,6 +6,8 @@ import com.ctrip.xpipe.redis.console.healthcheck.nonredis.cluster.ClusterHealthState; import com.ctrip.xpipe.redis.console.model.ShardTbl; import com.ctrip.xpipe.redis.console.service.ShardService; +import com.ctrip.xpipe.redis.core.entity.ShardMeta; +import com.ctrip.xpipe.redis.core.meta.MetaCache; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import org.junit.Assert; @@ -21,13 +23,13 @@ public class DefaultClusterHealthMonitorManagerTest { private DefaultClusterHealthMonitorManager manager; - private ShardService shardService; + private MetaCache metaCache; @Before public void beforeDefaultClusterHealthMonitorManagerTest() { manager = new DefaultClusterHealthMonitorManager(); - shardService = mock(ShardService.class); - manager.setShardService(shardService); + metaCache = mock(MetaCache.class); + manager.setMetaCache(metaCache); } @Test @@ -149,10 +151,10 @@ public void testOutterClientMasterUp() { } private void fakeShardService(String clusterId, String... shardIds) { - List result = Lists.newArrayList(); + List result = Lists.newArrayList(); for(String shardId : shardIds) { - result.add(new ShardTbl().setShardName(shardId)); + result.add(new ShardMeta().setId(shardId)); } - when(shardService.findAllShardNamesByClusterName(clusterId)).thenReturn(result); + when(metaCache.getAllShardNamesByClusterName(clusterId)).thenReturn(result); } } \ No newline at end of file diff --git a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitorTest.java b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitorTest.java index 8823c315c..c929616e3 100644 --- a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitorTest.java +++ b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/cluster/impl/DefaultClusterHealthMonitorTest.java @@ -3,25 +3,22 @@ import com.ctrip.xpipe.concurrent.AbstractExceptionLogTask; import com.ctrip.xpipe.redis.console.healthcheck.nonredis.cluster.ClusterHealthMonitor; import com.ctrip.xpipe.redis.console.healthcheck.nonredis.cluster.ClusterHealthState; -import com.ctrip.xpipe.redis.console.model.ShardTbl; -import com.ctrip.xpipe.redis.console.service.ShardService; +import com.ctrip.xpipe.redis.core.entity.ShardMeta; +import com.ctrip.xpipe.redis.core.meta.MetaCache; import com.ctrip.xpipe.utils.OsUtils; import com.google.common.collect.Lists; import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -import org.mockito.InjectMocks; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.List; -import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import static org.junit.Assert.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -33,12 +30,12 @@ public class DefaultClusterHealthMonitorTest { private String clusterId = "cluster"; - private ShardService shardService; + private MetaCache metaCache; @Before public void beforeDefaultClusterHealthMonitorTest() { - shardService = mock(ShardService.class); - monitor = new DefaultClusterHealthMonitor(clusterId, shardService); + metaCache = mock(MetaCache.class); + monitor = new DefaultClusterHealthMonitor(clusterId, metaCache); } @Test @@ -157,10 +154,10 @@ protected void doRun() throws Exception { } private void fakeShardService(String... shardIds) { - List result = Lists.newArrayList(); + List result = Lists.newArrayList(); for(String shardId : shardIds) { - result.add(new ShardTbl().setShardName(shardId)); + result.add(new ShardMeta().setId(shardId)); } - when(shardService.findAllShardNamesByClusterName(clusterId)).thenReturn(result); + when(metaCache.getAllShardNamesByClusterName(clusterId)).thenReturn(result); } } \ No newline at end of file diff --git a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/resources/DcMetaSynchronizerTest.java b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/resources/DcMetaSynchronizerTest.java index e79671206..9133a5ffd 100644 --- a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/resources/DcMetaSynchronizerTest.java +++ b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/resources/DcMetaSynchronizerTest.java @@ -1,6 +1,7 @@ package com.ctrip.xpipe.redis.console.resources; +import com.ctrip.xpipe.api.foundation.FoundationService; import com.ctrip.xpipe.api.migration.OuterClientService; import com.ctrip.xpipe.cluster.ClusterType; import com.ctrip.xpipe.redis.console.config.ConsoleConfig; @@ -66,6 +67,8 @@ public class DcMetaSynchronizerTest { @Mock private ClusterTypeUpdateEventFactory clusterTypeUpdateEventFactory; + + private String dcId = FoundationService.DEFAULT.getDataCenter(); private String singleDcCacheCluster = "SingleDcCacheCluster"; private String localDcCacheCluster = "LocalDcCacheCluster"; @@ -78,9 +81,9 @@ public void syncNoChangedTest() throws Exception { )); when(consoleConfig.getOuterClusterTypes()).thenReturn(Sets.newHashSet("SINGLE_DC", "LOCAL_DC")); - when(outerClientService.getOutClientDcMeta(DcMetaSynchronizer.currentDcId)).thenReturn(credisDcMeta().setDcName(DcMetaSynchronizer.currentDcId)); - when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(DcMetaSynchronizer.currentDcId))); - when(dcService.find(DcMetaSynchronizer.currentDcId)).thenReturn(new DcTbl().setId(1)); + when(outerClientService.getOutClientDcMeta(dcId)).thenReturn(credisDcMeta().setDcName(dcId)); + when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(dcId))); + when(dcService.find(dcId)).thenReturn(new DcTbl().setId(1)); when(clusterService.find(singleDcCacheCluster)).thenReturn(new ClusterTbl().setId(17730).setClusterName(singleDcCacheCluster).setActivedcId(1).setClusterType(OuterClientService.ClusterType.SINGEL_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(18)); when(clusterService.find(localDcCacheCluster)).thenReturn(new ClusterTbl().setId(17728).setClusterName(localDcCacheCluster).setActivedcId(1).setClusterType(OuterClientService.ClusterType.LOCAL_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(9)); dcMetaSynchronizer.sync(); @@ -107,13 +110,13 @@ public void syncClusterActiveDcChangedTest() throws Exception { )); when(consoleConfig.getOuterClusterTypes()).thenReturn(Sets.newHashSet("SINGLE_DC", "LOCAL_DC")); - when(outerClientService.getOutClientDcMeta(DcMetaSynchronizer.currentDcId)).thenReturn(credisDcMeta().setDcName(DcMetaSynchronizer.currentDcId)); + when(outerClientService.getOutClientDcMeta(dcId)).thenReturn(credisDcMeta().setDcName(dcId)); - DcMeta xpipeDcMeta = xpipeDcMeta().setId(DcMetaSynchronizer.currentDcId); + DcMeta xpipeDcMeta = xpipeDcMeta().setId(dcId); xpipeDcMeta.findCluster(singleDcCacheCluster).setActiveDc("oy"); when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta)); - when(dcService.find(DcMetaSynchronizer.currentDcId)).thenReturn(new DcTbl().setId(1)); + when(dcService.find(dcId)).thenReturn(new DcTbl().setId(1)); when(clusterService.find(singleDcCacheCluster)).thenReturn(new ClusterTbl().setId(17730).setClusterName(singleDcCacheCluster).setActivedcId(2).setClusterType(ClusterType.SINGLE_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(8)); when(clusterService.find(localDcCacheCluster)).thenReturn(new ClusterTbl().setId(17728).setClusterName(localDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.LOCAL_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(9)); dcMetaSynchronizer.sync(); @@ -142,12 +145,12 @@ public void syncClusterEmailsChangedTest() throws Exception { when(consoleConfig.getOuterClusterTypes()).thenReturn(Sets.newHashSet("SINGLE_DC", "LOCAL_DC")); // change admin emails - OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(DcMetaSynchronizer.currentDcId); + OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(dcId); credisDcMeta.getClusters().get(singleDcCacheCluster).setOwnerEmails("test2@ctrip.com"); - when(outerClientService.getOutClientDcMeta(DcMetaSynchronizer.currentDcId)).thenReturn(credisDcMeta); - when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(DcMetaSynchronizer.currentDcId))); - when(dcService.find(DcMetaSynchronizer.currentDcId)).thenReturn(new DcTbl().setId(1)); + when(outerClientService.getOutClientDcMeta(dcId)).thenReturn(credisDcMeta); + when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(dcId))); + when(dcService.find(dcId)).thenReturn(new DcTbl().setId(1)); when(clusterService.find(singleDcCacheCluster)).thenReturn(new ClusterTbl().setId(17730).setClusterName(singleDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.SINGLE_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(8)); when(clusterService.find(localDcCacheCluster)).thenReturn(new ClusterTbl().setId(17728).setClusterName(localDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.LOCAL_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(9)); dcMetaSynchronizer.sync(); @@ -176,15 +179,15 @@ public void syncClusterNameChangedTest() throws Exception { when(consoleConfig.getOuterClusterTypes()).thenReturn(Sets.newHashSet("SINGLE_DC", "LOCAL_DC")); // change cluster name - OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(DcMetaSynchronizer.currentDcId); + OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(dcId); OuterClientService.ClusterMeta oldSingle = credisDcMeta.getClusters().get(singleDcCacheCluster); credisDcMeta.getClusters().remove(singleDcCacheCluster); String newName = "newName"; credisDcMeta.getClusters().put(newName, oldSingle.setName(newName)); - when(outerClientService.getOutClientDcMeta(DcMetaSynchronizer.currentDcId)).thenReturn(credisDcMeta); - when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(DcMetaSynchronizer.currentDcId))); - when(dcService.find(DcMetaSynchronizer.currentDcId)).thenReturn(new DcTbl().setId(1)); + when(outerClientService.getOutClientDcMeta(dcId)).thenReturn(credisDcMeta); + when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(dcId))); + when(dcService.find(dcId)).thenReturn(new DcTbl().setId(1)); when(clusterService.find(singleDcCacheCluster)).thenReturn(new ClusterTbl().setId(17730).setClusterName(singleDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.SINGLE_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(8)); when(clusterService.find(localDcCacheCluster)).thenReturn(new ClusterTbl().setId(17728).setClusterName(localDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.LOCAL_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(9)); when(clusterService.getClusterRelatedDcs(singleDcCacheCluster)).thenReturn(Lists.newArrayList(new DcTbl().setDcName(credisDcMeta.getDcName()))); @@ -211,13 +214,13 @@ public void singleDcToOneWayTest() throws Exception { when(consoleConfig.getOuterClusterTypes()).thenReturn(Sets.newHashSet("SINGLE_DC", "LOCAL_DC")); - OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(DcMetaSynchronizer.currentDcId); - DcMeta xpipeDcMeta=xpipeDcMeta().setId(DcMetaSynchronizer.currentDcId); + OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(dcId); + DcMeta xpipeDcMeta=xpipeDcMeta().setId(dcId); xpipeDcMeta.removeCluster(singleDcCacheCluster); - when(outerClientService.getOutClientDcMeta(DcMetaSynchronizer.currentDcId)).thenReturn(credisDcMeta); + when(outerClientService.getOutClientDcMeta(dcId)).thenReturn(credisDcMeta); when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta)); - when(dcService.find(DcMetaSynchronizer.currentDcId)).thenReturn(new DcTbl().setId(1)); + when(dcService.find(dcId)).thenReturn(new DcTbl().setId(1)); when(clusterService.find(singleDcCacheCluster)).thenReturn(new ClusterTbl().setId(17730).setClusterName(singleDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.ONE_WAY.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(8)); when(clusterService.find(localDcCacheCluster)).thenReturn(new ClusterTbl().setId(17728).setClusterName(localDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.LOCAL_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(9)); dcMetaSynchronizer.sync(); @@ -243,13 +246,13 @@ public void clusterExistedButNoShardsTest() throws Exception { when(consoleConfig.getOuterClusterTypes()).thenReturn(Sets.newHashSet("SINGLE_DC", "LOCAL_DC")); - OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(DcMetaSynchronizer.currentDcId); - DcMeta xpipeDcMeta=xpipeDcMeta().setId(DcMetaSynchronizer.currentDcId); + OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(dcId); + DcMeta xpipeDcMeta=xpipeDcMeta().setId(dcId); xpipeDcMeta.removeCluster(singleDcCacheCluster); - when(outerClientService.getOutClientDcMeta(DcMetaSynchronizer.currentDcId)).thenReturn(credisDcMeta); + when(outerClientService.getOutClientDcMeta(dcId)).thenReturn(credisDcMeta); when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta)); - when(dcService.find(DcMetaSynchronizer.currentDcId)).thenReturn(new DcTbl().setId(1)); + when(dcService.find(dcId)).thenReturn(new DcTbl().setId(1)); when(clusterService.find(singleDcCacheCluster)).thenReturn(new ClusterTbl().setId(17730).setClusterName(singleDcCacheCluster).setActivedcId(2).setClusterType(ClusterType.SINGLE_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(8)); when(clusterService.find(localDcCacheCluster)).thenReturn(new ClusterTbl().setId(17728).setClusterName(localDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.LOCAL_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(9)); dcMetaSynchronizer.sync(); @@ -276,12 +279,12 @@ public void syncClusterClusterTypeChangedTest() throws Exception { when(consoleConfig.getOuterClusterTypes()).thenReturn(Sets.newHashSet("SINGLE_DC", "LOCAL_DC")); // change cluster type - OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(DcMetaSynchronizer.currentDcId); + OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(dcId); credisDcMeta.getClusters().get(singleDcCacheCluster).setClusterType(OuterClientService.ClusterType.LOCAL_DC); - when(outerClientService.getOutClientDcMeta(DcMetaSynchronizer.currentDcId)).thenReturn(credisDcMeta); - when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(DcMetaSynchronizer.currentDcId))); - when(dcService.find(DcMetaSynchronizer.currentDcId)).thenReturn(new DcTbl().setId(1)); + when(outerClientService.getOutClientDcMeta(dcId)).thenReturn(credisDcMeta); + when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(dcId))); + when(dcService.find(dcId)).thenReturn(new DcTbl().setId(1)); when(clusterService.find(singleDcCacheCluster)).thenReturn(new ClusterTbl().setId(17730).setClusterName(singleDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.SINGLE_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(8)); when(clusterService.find(localDcCacheCluster)).thenReturn(new ClusterTbl().setId(17728).setClusterName(localDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.LOCAL_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(9)); dcMetaSynchronizer.sync(); @@ -309,16 +312,16 @@ public void syncShardAddedTest() throws Exception { )); // add shard - OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(DcMetaSynchronizer.currentDcId); + OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(dcId); credisDcMeta.getClusters().get(singleDcCacheCluster).getGroups().put("newGroup", new OuterClientService.GroupMeta(). setClusterName(singleDcCacheCluster).setGroupName("newGroup"). setRedises(Lists.newArrayList(new OuterClientService.RedisMeta().setHost("127.0.0.1").setPort(6379).setMaster(true)))); when(consoleConfig.getOuterClusterTypes()).thenReturn(Sets.newHashSet("SINGLE_DC", "LOCAL_DC")); when(consoleConfig.supportSentinelHealthCheck(any(),any())).thenReturn(false); - when(outerClientService.getOutClientDcMeta(DcMetaSynchronizer.currentDcId)).thenReturn(credisDcMeta); - when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(DcMetaSynchronizer.currentDcId))); - when(dcService.find(DcMetaSynchronizer.currentDcId)).thenReturn(new DcTbl().setId(1)); + when(outerClientService.getOutClientDcMeta(dcId)).thenReturn(credisDcMeta); + when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(dcId))); + when(dcService.find(dcId)).thenReturn(new DcTbl().setId(1)); when(clusterService.find(singleDcCacheCluster)).thenReturn(new ClusterTbl().setId(17730).setClusterName(singleDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.SINGLE_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(8)); when(clusterService.find(localDcCacheCluster)).thenReturn(new ClusterTbl().setId(17728).setClusterName(localDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.LOCAL_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(9)); dcMetaSynchronizer.sync(); @@ -353,13 +356,13 @@ public void syncShardRemovedTest() throws Exception { )); // rm shard - OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(DcMetaSynchronizer.currentDcId); + OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(dcId); credisDcMeta.getClusters().get(singleDcCacheCluster).getGroups().clear(); when(consoleConfig.getOuterClusterTypes()).thenReturn(Sets.newHashSet("SINGLE_DC", "LOCAL_DC")); - when(outerClientService.getOutClientDcMeta(DcMetaSynchronizer.currentDcId)).thenReturn(credisDcMeta); - when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(DcMetaSynchronizer.currentDcId))); - when(dcService.find(DcMetaSynchronizer.currentDcId)).thenReturn(new DcTbl().setId(1)); + when(outerClientService.getOutClientDcMeta(dcId)).thenReturn(credisDcMeta); + when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(dcId))); + when(dcService.find(dcId)).thenReturn(new DcTbl().setId(1)); when(clusterService.find(singleDcCacheCluster)).thenReturn(new ClusterTbl().setId(17730).setClusterName(singleDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.SINGLE_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(8)); when(clusterService.find(localDcCacheCluster)).thenReturn(new ClusterTbl().setId(17728).setClusterName(localDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.LOCAL_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(9)); dcMetaSynchronizer.sync(); @@ -385,16 +388,16 @@ public void syncRedisAddedTest() throws Exception { )); // add redis - OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(DcMetaSynchronizer.currentDcId); + OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(dcId); credisDcMeta.getClusters().get(singleDcCacheCluster).getGroups().get("credis_test_cluster_1_1").getRedises().add(new OuterClientService.RedisMeta().setHost("127.0.0.1").setPort(6379).setMaster(true)); when(consoleConfig.getOuterClusterTypes()).thenReturn(Sets.newHashSet("SINGLE_DC", "LOCAL_DC")); - when(outerClientService.getOutClientDcMeta(DcMetaSynchronizer.currentDcId)).thenReturn(credisDcMeta); - when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(DcMetaSynchronizer.currentDcId))); - when(dcService.find(DcMetaSynchronizer.currentDcId)).thenReturn(new DcTbl().setId(1)); + when(outerClientService.getOutClientDcMeta(dcId)).thenReturn(credisDcMeta); + when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(dcId))); + when(dcService.find(dcId)).thenReturn(new DcTbl().setId(1)); when(clusterService.find(singleDcCacheCluster)).thenReturn(new ClusterTbl().setId(17730).setClusterName(singleDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.SINGLE_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(8)); when(clusterService.find(localDcCacheCluster)).thenReturn(new ClusterTbl().setId(17728).setClusterName(localDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.LOCAL_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(9)); - when(redisService.findRedisesByDcClusterShard(DcMetaSynchronizer.currentDcId,singleDcCacheCluster,"credis_test_cluster_1_1")).thenReturn(Lists.newArrayList(new RedisTbl().setRedisIp("127.0.0.1").setRedisPort(6379).setMaster(false))); + when(redisService.findRedisesByDcClusterShard(dcId,singleDcCacheCluster,"credis_test_cluster_1_1")).thenReturn(Lists.newArrayList(new RedisTbl().setRedisIp("127.0.0.1").setRedisPort(6379).setMaster(false))); dcMetaSynchronizer.sync(); verify(clusterService, never()).bindDc(any()); @@ -419,13 +422,13 @@ public void syncRedisRemovedTest() throws Exception { )); // add redis - OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(DcMetaSynchronizer.currentDcId); + OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(dcId); credisDcMeta.getClusters().get(singleDcCacheCluster).getGroups().get("credis_test_cluster_1_1").getRedises().clear(); when(consoleConfig.getOuterClusterTypes()).thenReturn(Sets.newHashSet("SINGLE_DC", "LOCAL_DC")); - when(outerClientService.getOutClientDcMeta(DcMetaSynchronizer.currentDcId)).thenReturn(credisDcMeta); - when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(DcMetaSynchronizer.currentDcId))); - when(dcService.find(DcMetaSynchronizer.currentDcId)).thenReturn(new DcTbl().setId(1)); + when(outerClientService.getOutClientDcMeta(dcId)).thenReturn(credisDcMeta); + when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(dcId))); + when(dcService.find(dcId)).thenReturn(new DcTbl().setId(1)); when(clusterService.find(singleDcCacheCluster)).thenReturn(new ClusterTbl().setId(17730).setClusterName(singleDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.SINGLE_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(8)); when(clusterService.find(localDcCacheCluster)).thenReturn(new ClusterTbl().setId(17728).setClusterName(localDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.LOCAL_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(9)); dcMetaSynchronizer.sync(); @@ -453,7 +456,7 @@ public void syncRedisChangedTest() throws Exception { )); // change redis role - OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(DcMetaSynchronizer.currentDcId); + OuterClientService.DcMeta credisDcMeta = credisDcMeta().setDcName(dcId); List redisTbls = new ArrayList<>(); List redisMetaList = credisDcMeta.getClusters().get(singleDcCacheCluster).getGroups().get("credis_test_cluster_1_1").getRedises(); @@ -464,12 +467,12 @@ public void syncRedisChangedTest() throws Exception { when(consoleConfig.getOuterClusterTypes()).thenReturn(Sets.newHashSet("SINGLE_DC", "LOCAL_DC")); - when(outerClientService.getOutClientDcMeta(DcMetaSynchronizer.currentDcId)).thenReturn(credisDcMeta); - when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(DcMetaSynchronizer.currentDcId))); - when(dcService.find(DcMetaSynchronizer.currentDcId)).thenReturn(new DcTbl().setId(1)); + when(outerClientService.getOutClientDcMeta(dcId)).thenReturn(credisDcMeta); + when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(dcId))); + when(dcService.find(dcId)).thenReturn(new DcTbl().setId(1)); when(clusterService.find(singleDcCacheCluster)).thenReturn(new ClusterTbl().setId(17730).setClusterName(singleDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.SINGLE_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(8)); when(clusterService.find(localDcCacheCluster)).thenReturn(new ClusterTbl().setId(17728).setClusterName(localDcCacheCluster).setActivedcId(1).setClusterType(ClusterType.LOCAL_DC.name()).setClusterAdminEmails("test@ctrip.com").setClusterOrgId(9)); - when(redisService.findRedisesByDcClusterShard(DcMetaSynchronizer.currentDcId, singleDcCacheCluster, "credis_test_cluster_1_1")).thenReturn(redisTbls); + when(redisService.findRedisesByDcClusterShard(dcId, singleDcCacheCluster, "credis_test_cluster_1_1")).thenReturn(redisTbls); dcMetaSynchronizer.sync(); diff --git a/redis/redis-core/src/main/java/com/ctrip/xpipe/redis/core/meta/MetaCache.java b/redis/redis-core/src/main/java/com/ctrip/xpipe/redis/core/meta/MetaCache.java index b3c48cfae..01889556b 100644 --- a/redis/redis-core/src/main/java/com/ctrip/xpipe/redis/core/meta/MetaCache.java +++ b/redis/redis-core/src/main/java/com/ctrip/xpipe/redis/core/meta/MetaCache.java @@ -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.tuple.Pair; import org.unidal.tuple.Triple; @@ -93,4 +90,6 @@ public interface MetaCache { Map dcShardIds(String clusterId, String dcId); boolean isAsymmetricCluster(String clusterName); + + List getAllShardNamesByClusterName(String clusterName); } diff --git a/redis/redis-core/src/main/resources/META-INF/dal/model/keeper-codegen.xml b/redis/redis-core/src/main/resources/META-INF/dal/model/keeper-codegen.xml index fc2e97748..a273db696 100644 --- a/redis/redis-core/src/main/resources/META-INF/dal/model/keeper-codegen.xml +++ b/redis/redis-core/src/main/resources/META-INF/dal/model/keeper-codegen.xml @@ -12,6 +12,7 @@ +