From d151a93750af0a2c7d55f6432b941e9a4b1fb26a Mon Sep 17 00:00:00 2001 From: qifanwang Date: Fri, 16 Aug 2024 17:37:47 +0800 Subject: [PATCH 1/6] add disable db mode for console --- .../checker/config/impl/CommonConfigBean.java | 12 + .../redis/checker/impl/TestMetaCache.java | 15 +- .../spring/ConsoleDisableDbCondition.java | 23 + .../redis/checker/spring/DisableDbMode.java | 15 + .../console/cache/impl/AzGroupCacheImpl.java | 23 +- .../cache/impl/AzGroupCacheWithoutDBImpl.java | 67 +++ .../redis/console/config/ConsoleConfig.java | 5 + .../config/impl/CombConsoleConfig.java | 13 + .../config/impl/DefaultConsoleConfig.java | 10 + .../controller/api/DcApiController.java | 16 + .../api/checker/ConsoleCheckerController.java | 7 + .../api/data/AzUpdateController.java | 11 + .../api/data/ClusterUpdateController.java | 25 +- .../api/data/ConfigUpdateController.java | 64 +++ .../api/data/KeeperUpdateController.java | 6 + .../api/data/OrganizationApiController.java | 24 + .../controller/api/data/ProxyController.java | 12 +- .../api/data/RedisApiController.java | 48 ++ .../api/data/meta/RedisCreateInfo.java | 9 + .../config/DisableConsoleInterceptor.java | 20 + .../console/controller/config/MvcConfig.java | 8 + .../consoleportal/ConfigController.java | 9 +- .../KeeperContainerInfoController.java | 2 +- .../election/CrossDcLeaderElectionAction.java | 6 + .../impl/DefaultClusterHealthMonitor.java | 9 +- .../DefaultClusterHealthMonitorManager.java | 9 +- .../redisconfig/RedisConfigCheckMonitor.java | 3 +- .../DefaultMigrationResultReporter.java | 34 +- .../console/resources/AbstractMetaCache.java | 22 + .../resources/CheckerPersistenceCache.java | 22 +- .../resources/ClusterMetaSynchronizer.java | 41 +- .../resources/ConsoleMetaCacheWithoutDB.java | 54 +++ .../resources/ConsolePortalService.java | 288 ++++++++++++ .../console/resources/DcMetaSynchronizer.java | 63 +-- .../console/resources/DefaultMetaCache.java | 37 +- .../console/resources/MetaSynchronizer.java | 147 ++++++ .../resources/PersistenceCacheWithoutDB.java | 20 + .../resources/RedisMetaSynchronizer.java | 11 +- .../resources/ShardMetaSynchronizer.java | 13 +- .../console/service/impl/AzServiceImpl.java | 3 +- .../service/impl/ClusterServiceImpl.java | 6 +- .../service/impl/ClusterServiceWithoutDB.java | 423 ++++++++++++++++++ .../service/impl/ConfigServiceImpl.java | 84 ++-- .../service/impl/ConfigServiceWithoutDB.java | 59 +++ .../console/service/impl/DcServiceImpl.java | 8 +- .../service/impl/DcServiceWithoutDB.java | 155 +++++++ .../impl/KeeperContainerServiceImpl.java | 5 + .../impl/KeeperContainerServiceWithoutDB.java | 210 +++++++++ .../service/impl/OrganizationServiceImpl.java | 5 + .../impl/OrganizationServiceWithoutDB.java | 84 ++++ .../service/impl/ProxyServiceImpl.java | 9 +- .../service/impl/ProxyServiceWithoutDB.java | 177 ++++++++ .../service/impl/RedisServiceImpl.java | 5 + .../service/impl/RedisServiceWithoutDB.java | 126 ++++++ .../service/impl/RouteServiceImpl.java | 10 +- .../service/impl/RouteServiceWithoutDB.java | 130 ++++++ .../meta/impl/AdvancedDcMetaService.java | 11 +- .../meta/impl/DcMetaServiceWithoutDB.java | 55 +++ .../DefaultCheckMigrationCommandBuilder.java | 1 - .../console/spring/CheckerContextConfig.java | 3 +- .../console/spring/ConsoleContextConfig.java | 40 +- ...efaultClusterHealthMonitorManagerTest.java | 14 +- .../impl/DefaultClusterHealthMonitorTest.java | 19 +- .../resources/DcMetaSynchronizerTest.java | 130 +++--- .../core/console/ConsoleCheckerPath.java | 2 + .../xpipe/redis/core/meta/MetaCache.java | 9 +- .../META-INF/dal/model/keeper-codegen.xml | 1 + 67 files changed, 2775 insertions(+), 232 deletions(-) create mode 100644 redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/spring/ConsoleDisableDbCondition.java create mode 100644 redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/spring/DisableDbMode.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/cache/impl/AzGroupCacheWithoutDBImpl.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/ConfigUpdateController.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/OrganizationApiController.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/RedisApiController.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/config/DisableConsoleInterceptor.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ConsoleMetaCacheWithoutDB.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ConsolePortalService.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/MetaSynchronizer.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/PersistenceCacheWithoutDB.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ClusterServiceWithoutDB.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ConfigServiceWithoutDB.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/DcServiceWithoutDB.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/KeeperContainerServiceWithoutDB.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/OrganizationServiceWithoutDB.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ProxyServiceWithoutDB.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RedisServiceWithoutDB.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RouteServiceWithoutDB.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/DcMetaServiceWithoutDB.java 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..e077d3233 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 @@ -1,6 +1,7 @@ package com.ctrip.xpipe.redis.checker.config.impl; import com.ctrip.xpipe.api.config.ConfigProvider; +import com.ctrip.xpipe.api.foundation.FoundationService; import com.ctrip.xpipe.cluster.ClusterType; import com.ctrip.xpipe.codec.JsonCodec; import com.ctrip.xpipe.config.AbstractConfigBean; @@ -48,6 +49,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 +62,15 @@ 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 boolean disableDb() { + return getExtraSyncDC().contains(FoundationService.DEFAULT.getDataCenter()); + } + 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/impl/TestMetaCache.java b/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/impl/TestMetaCache.java index 1267b8948..5d2d10a14 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; @@ -34,6 +31,11 @@ public XpipeMeta getXpipeMeta() { return xpipeMeta; } + @Override + public XpipeMeta getXpipeMetaLongPull(long updateTime) throws InterruptedException { + return null; + } + @Override public XpipeMeta getDividedXpipeMeta(int partIndex) { return xpipeMeta; @@ -205,4 +207,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..51fe0c604 --- /dev/null +++ b/redis/redis-checker/src/main/java/com/ctrip/xpipe/redis/checker/spring/ConsoleDisableDbCondition.java @@ -0,0 +1,23 @@ +package com.ctrip.xpipe.redis.checker.spring; + +import com.ctrip.xpipe.redis.checker.config.impl.CommonConfigBean; +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; + +import java.util.Map; + +public class ConsoleDisableDbCondition implements Condition { + + private static CommonConfigBean commonConfigBean = new CommonConfigBean(); + + + @Override + public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { + Map attributes = annotatedTypeMetadata.getAnnotationAttributes(DisableDbMode.class.getName()); + boolean disable = (boolean) attributes.getOrDefault("value", false); + return commonConfigBean.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..4d66a2eda 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 @@ -2,6 +2,7 @@ import com.ctrip.xpipe.api.codec.GenericTypeReference; import com.ctrip.xpipe.api.config.ConfigChangeListener; +import com.ctrip.xpipe.api.foundation.FoundationService; import com.ctrip.xpipe.cluster.ClusterType; import com.ctrip.xpipe.codec.JsonCodec; import com.ctrip.xpipe.config.AbstractConfigBean; @@ -59,6 +60,8 @@ public CombConsoleConfig(CheckConfigBean checkConfigBean, private HickwallMetricInfo info; + + @Override public String getServerMode() { return checkConfigBean.getServerMode(); @@ -555,6 +558,16 @@ public boolean autoSetKeeperSyncLimit() { return consoleConfigBean.autoSetKeeperSyncLimit(); } + @Override + public boolean disableDb() { + return commonConfigBean.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/checker/ConsoleCheckerController.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/checker/ConsoleCheckerController.java index b469b4ff4..d40ba7808 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/checker/ConsoleCheckerController.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/checker/ConsoleCheckerController.java @@ -95,6 +95,13 @@ public String getDividedMeta(@RequestParam(value="format", required = false) Str return (format != null && format.equals("xml"))? xpipeMeta.toString() : coder.encode(xpipeMeta); } + @GetMapping(ConsoleCheckerPath.PATH_GET_ALL_META_LONG_PULL) + public String getDividedMetaLongPull(@RequestParam(value="format", required = false) String format, + @RequestParam(value="updateTime") long updateTime) throws InterruptedException { + XpipeMeta xpipeMeta = metaCache.getXpipeMetaLongPull(updateTime); + return (format != null && format.equals("xml"))? xpipeMeta.toString() : coder.encode(xpipeMeta); + } + @GetMapping(ConsoleCheckerPath.PATH_GET_DC_ALL_META) public String getDcAllMeta(@PathVariable String dcName, @RequestParam(value="format", required = false) String format) { DcMeta dcMeta = metaCache.getXpipeMeta().getDcs().get(dcName); 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..80e73dfc4 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/ConfigUpdateController.java @@ -0,0 +1,64 @@ +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 Long until) throws DalException { + + return configDao.findAllByKeyAndValueAndUntilAfter(key, value, new Date(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/ProxyController.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/ProxyController.java index 0f6033c0d..e102f77bb 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/ProxyController.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/data/ProxyController.java @@ -9,10 +9,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; -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 java.util.List; @@ -44,6 +41,13 @@ public String getAllProxies() { } } + @RequestMapping(value = "/proxies/monitor_active", method = RequestMethod.GET) + public String getMonitorActiveProxiesByDc(@RequestParam(value="dc") String dc) { + return pretty.encode(service.getMonitorActiveProxiesByDc(dc)); + } + + + @RequestMapping(value = "/proxies/active", method = RequestMethod.GET) public String getActiveProxies() { try { 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/config/DisableConsoleInterceptor.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/config/DisableConsoleInterceptor.java new file mode 100644 index 000000000..5f0f8e601 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/config/DisableConsoleInterceptor.java @@ -0,0 +1,20 @@ +package com.ctrip.xpipe.redis.console.controller.config; + +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class DisableConsoleInterceptor extends HandlerInterceptorAdapter { + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) + throws Exception { + if(request.getRequestURI().startsWith("/console/")) { + response.sendError(404, "cannot access db"); + return false; + } else { + return true; + } + } +} diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/config/MvcConfig.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/config/MvcConfig.java index c5c5c8533..8673af17a 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/config/MvcConfig.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/config/MvcConfig.java @@ -1,6 +1,8 @@ package com.ctrip.xpipe.redis.console.controller.config; +import com.ctrip.xpipe.redis.console.config.ConsoleConfig; import com.ctrip.xpipe.redis.console.service.ClusterService; +import com.ctrip.xpipe.redis.console.service.DcService; import com.ctrip.xpipe.redis.core.meta.MetaCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -21,10 +23,16 @@ public class MvcConfig extends WebMvcConfigurerAdapter{ @Autowired private ClusterService clusterService; + @Autowired + private ConsoleConfig config; + @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LogInterceptor()); registry.addInterceptor(new ClusterCheckInterceptor(metaCache, clusterService)); + if(config.disableDb()) { + registry.addInterceptor(new DisableConsoleInterceptor()); + } } } 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..87555b861 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,6 @@ public List getAllKeeperContainerInfos() { return keeperContainerService.findAllInfos(); } - @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..aca87dd46 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 @@ -24,6 +24,9 @@ import java.util.*; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; /** @@ -106,6 +109,11 @@ protected XpipeMeta createXpipeMeta(List dcMetas, List> crossDcClusters = typeDcClusterMap(xpipeMeta, ClusterType.CROSS_DC); @@ -668,6 +676,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..f7dfc42f1 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ConsoleMetaCacheWithoutDB.java @@ -0,0 +1,54 @@ +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(lastUpdateTime); + refreshMetaParts(); + refreshMeta(xpipeMeta); + } + + @Override + public Map getData() { + return null; + } + }); + } + + public void startLoadMeta() { + + logger.info("[loadMeta][start]{}", this); + while (true) { + try { + loadCache(); + } catch (Exception e) { + try { + // 如果抛异常,特别是刚启动时候,不能大量请求 + Thread.sleep(500); + } catch (InterruptedException ex) { + logger.error("[loadMeta][interrupted]", ex); + } + logger.error("[loadMeta]", e); + } + } + } +} 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..d9ae54ead --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/ConsolePortalService.java @@ -0,0 +1,288 @@ +package com.ctrip.xpipe.redis.console.resources; + +import com.ctrip.xpipe.api.foundation.FoundationService; +import com.ctrip.xpipe.codec.JsonCodec; +import com.ctrip.xpipe.monitor.CatTransactionMonitor; +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.net.URI; +import java.util.*; +import java.util.concurrent.Callable; +import java.util.stream.Collectors; + +@Service +public class ConsolePortalService extends AbstractService { + + private CatTransactionMonitor catTransactionMonitor = new CatTransactionMonitor(); + + @Autowired + private ConsoleConfig config; + + @Autowired + private FoundationService foundationService; + + public List getAllAzGroups() { + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/azGroup/all").build(); + ResponseEntity> resp = exchange(comp.toUri(), + HttpMethod.GET, null, new ParameterizedTypeReference>(){}, "getAllAzGroups"); + return resp.getBody(); + } + + public List getAllProxy() { + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + "/api/proxies/all").build(); + + ResponseEntity> resp = exchange(comp.toUri(), + HttpMethod.GET, null, new ParameterizedTypeReference>(){}, "getAllProxy"); + return resp.getBody(); + } + + public List getMonitorActiveProxiesByDc() { + String dc = foundationService.getDataCenter(); + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + "/api/proxies/monitor_active") + .queryParam("dc", dc) + .build(); + + ResponseEntity> resp = exchange(comp.toUri(), + HttpMethod.GET, null, new ParameterizedTypeReference>(){}, "getMonitorActiveProxiesByDc"); + + return resp.getBody(); + } + + public XpipeMeta getXpipeAllMeta(long updateTime) throws SAXException, IOException { + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + ConsoleCheckerPath.PATH_GET_ALL_META_LONG_PULL) + .queryParam("format", "xml") + .queryParam("updateTime", updateTime) + .build(); + + ResponseEntity raw = exchange(comp.toUri().toString(), HttpMethod.GET, null, String.class, "getXpipeAllMeta"); + if (StringUtil.isEmpty(raw.getBody())) return null; + return DefaultSaxParser.parse(raw.getBody()); + } + + public List getAllKeeperContainers() { + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/keeper_container/all").build(); + + ResponseEntity> resp = exchange(comp.toUri(), HttpMethod.GET, null, + new ParameterizedTypeReference>(){}, "getAllKeeperContainers"); + 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 = exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/config/set", HttpMethod.POST, requestEntity, RetMessage.class, "setConfig"); + 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 = exchange(comp.toUri().toString(), + HttpMethod.GET, null, ConfigTbl.class, "getConfig"); + return resp.getBody(); + } + + public List getAllConfigs(String key) { + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/config/getAll/{key}").buildAndExpand(key); + + ResponseEntity> resp = exchange(comp.toUri(), + HttpMethod.GET, null, new ParameterizedTypeReference>(){}, "getAllConfigs"); + return resp.getBody(); + } + + public List findAllByKeyAndValueAndUntilAfter(String key, String value, Date until) { + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/config/findAll").build(); + MultiValueMap params = new LinkedMultiValueMap<>(); + params.add("key", key); + params.add("value", value); + params.add("until", until.getTime()); + HttpEntity> requestEntity = new HttpEntity<>(params); + ResponseEntity> resp = exchange(comp.toUri(), HttpMethod.POST, requestEntity, + new ParameterizedTypeReference>(){}, "findAllByKeyAndValueAndUntilAfter"); + return resp.getBody(); + } + + public List getAllOrganizations() { + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/organizations/all").build(); + + ResponseEntity> resp = exchange(comp.toUri(), HttpMethod.GET, null, + new ParameterizedTypeReference>(){}, "getAllOrganizations"); + return resp.getBody(); + } + + public List getAllDcs() { + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/dc_tbls").build(); + + ResponseEntity> resp = restTemplate.exchange(comp.toUri(), 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 = exchange(comp.toUri(), HttpMethod.GET, null, + new ParameterizedTypeReference>(){}, "findClusterRelatedDc"); + return resp.getBody(); + } + + public List findAllClusters() { + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/clusters/all").build(); + + ResponseEntity> resp = exchange(comp.toUri(), + HttpMethod.GET, null, + new ParameterizedTypeReference>(){}, "findAllClusters"); + return resp.getBody(); + } + + public List findAllTunnelInfo() { + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + ConsoleCheckerPath.PATH_GET_PROXY_CHAINS).build(); + + ResponseEntity> resp = exchange(comp.toUri(), + HttpMethod.GET, null, + new ParameterizedTypeReference>(){}, "findAllTunnelInfo"); + return resp.getBody(); + } + + public List findAllByDcClusterShard(long dcClusterShardId) { + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/redis/{dcClusterShardId}").buildAndExpand(dcClusterShardId); + ResponseEntity> resp = exchange(comp.toUri(), HttpMethod.GET, null, + new ParameterizedTypeReference>(){}, "findAllByDcClusterShard"); + return resp.getBody(); + } + + public void updateBatchKeeperActive(List redises) { + HttpEntity> requestEntity = new HttpEntity<>(redises); + ResponseEntity resp = exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/redis/updateBatchKeeperActive", + HttpMethod.POST, requestEntity, RetMessage.class, "updateBatchKeeperActive" + ); + 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 = exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/redis/insert", HttpMethod.POST, + requestEntity, RetMessage.class, "insertRedises"); + 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 = exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/dc/bind", + HttpMethod.POST, requestEntity, RetMessage.class, "bindDc" + ); + RetMessage ret = resp.getBody(); + if(ret.getState() != RetMessage.SUCCESS_STATE) { + throw new RuntimeException("bind dc fail:" + ret.getState()); + } + } + + public List getActiveRoutes() { + ResponseEntity resp = exchange(config.getConsoleDomain() + + AbstractConsoleController.API_PREFIX + "/routes/active", + HttpMethod.GET, null, String.class, "getActiveRoutes" + ); + String content = resp.getBody(); + JsonCodec pretty = new JsonCodec(true, true); + return pretty.decode(content, List.class); + } + + ResponseEntity exchange(String url, HttpMethod var2, HttpEntity httpEntity, Class type, String name) { + try { + return catTransactionMonitor.logTransaction("API", name, new Callable>() { + @Override + public ResponseEntity call() { + return restTemplate.exchange(url, var2, httpEntity, type); + } + }); + } catch (Exception e) { + logger.error("[exchange]", e); + return null; + } + } + + ResponseEntity exchange(URI uri, HttpMethod var2, HttpEntity httpEntity, ParameterizedTypeReference type, String name) { + try { + return catTransactionMonitor.logTransaction("API", name, new Callable>() { + @Override + public ResponseEntity call() { + return restTemplate.exchange(uri, var2, httpEntity, type); + } + }); + } catch (Exception e) { + logger.error("[exchange]", e); + return null; + } + } + +} 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..3a09716c1 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 @@ -1,11 +1,9 @@ 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.api.monitor.Task; import com.ctrip.xpipe.api.monitor.TransactionMonitor; import com.ctrip.xpipe.cluster.ClusterType; -import com.ctrip.xpipe.redis.console.cluster.ConsoleLeaderElector; import com.ctrip.xpipe.redis.console.config.ConsoleConfig; import com.ctrip.xpipe.redis.console.constant.XPipeConsoleConstant; import com.ctrip.xpipe.redis.console.model.OrganizationTbl; @@ -19,70 +17,79 @@ 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 OuterClientService outerClientService = OuterClientService.DEFAULT; - @Autowired + private final String currentDcId; + + private ScheduledExecutorService scheduledExecutorService; + + private OuterClientService outerClientService; + + public DcMetaSynchronizer(ConsoleConfig consoleConfig, + MetaCache metaCache, RedisService redisService, ShardService shardService, + ClusterService clusterService, DcService dcService, + OrganizationService organizationService, SentinelBalanceService sentinelBalanceService, + ClusterTypeUpdateEventFactory clusterTypeUpdateEventFactory, OuterClientService outerClientService, + 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.outerClientService = outerClientService; + 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 +110,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..9acca6270 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 @@ -31,6 +31,9 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.stream.IntStream; /** @@ -74,6 +77,9 @@ public class DefaultMetaCache extends AbstractMetaCache implements MetaCache, Co private AtomicBoolean taskTrigger = new AtomicBoolean(false); + private final Lock lock = new ReentrantLock(); + protected final Condition condition = lock.newCondition(); + @Override public void isleader() { if (taskTrigger.compareAndSet(false, true)) { @@ -88,6 +94,26 @@ public void notLeader() { stopLoadMeta(); } + @Override + public XpipeMeta getXpipeMetaLongPull(long updateTime) throws InterruptedException { + XpipeMeta xpipeMeta = null; + if(lock.tryLock(consoleConfig.getCacheRefreshInterval(), TimeUnit.MILLISECONDS)) { + try { + if(lastUpdateTime <= updateTime) { + condition.await(consoleConfig.getCacheRefreshInterval(), TimeUnit.MILLISECONDS); + } + xpipeMeta = meta.getKey(); + } catch (Exception e) { + logger.debug("[getXpipeMeta]", e); + } finally { + lock.unlock(); + } + } else { + xpipeMeta = meta.getKey(); + } + return xpipeMeta; + } + private synchronized void stopLoadMeta(){ logger.info("[loadMeta][stop]{}", this); if (future != null) @@ -112,7 +138,14 @@ public void startLoadMeta() { protected void doRun() throws Exception { if(!taskTrigger.get()) return; - loadCache(); + if(lock.tryLock(consoleConfig.getCacheRefreshInterval(), TimeUnit.MILLISECONDS)) { + try { + loadCache(); + condition.signalAll(); + } finally { + lock.unlock(); + } + } } }, 1000, refreshIntervalMilli, TimeUnit.MILLISECONDS); @@ -161,7 +194,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..afdc76d67 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/resources/MetaSynchronizer.java @@ -0,0 +1,147 @@ +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.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.StringUtil; +import com.ctrip.xpipe.utils.XpipeThreadFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.annotation.PostConstruct; +import java.util.*; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +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); + + private OuterClientService outerClientService = OuterClientService.DEFAULT; + + @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, outerClientService, 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, outerClientService,currentDc)); + dcMetaSynchronizers.get(currentDc).start(); + } + } else { + if(dcMetaSynchronizers.containsKey(currentDc)) { + dcMetaSynchronizers.get(currentDc).stop(); + dcMetaSynchronizers.remove(currentDc); + } + } + // 检查 被移除托管的 dc + Set needRemoved = new HashSet<>(); + for(String dc : dcMetaSynchronizers.keySet()) { + if(!dcs.contains(dc) && !StringUtil.trimEquals(dc, currentDc)) { + needRemoved.add(dc); + } + } + + for(String dc : needRemoved) { + dcMetaSynchronizers.get(dc).stop(); + dcMetaSynchronizers.remove(dc); + } + } + +} 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..907349c0a --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/KeeperContainerServiceWithoutDB.java @@ -0,0 +1,210 @@ +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.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 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..4c91652a5 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/ProxyServiceWithoutDB.java @@ -0,0 +1,177 @@ +package com.ctrip.xpipe.redis.console.service.impl; + +import com.ctrip.xpipe.api.foundation.FoundationService; +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; + + private FoundationService foundationService; + + public ProxyServiceWithoutDB(ConsolePortalService consolePortalService, ConsoleConfig config, FoundationService foundationService) { + this.consolePortalService = consolePortalService; + this.config = config; + this.foundationService = foundationService; + } + + private TimeBoundCache> allProxyCache; + + private TimeBoundCache> allTunnelCache; + + private TimeBoundCache> currentDcActiveProxy; + + @PostConstruct + public void postConstruct(){ + allProxyCache = new TimeBoundCache<>(config::getCacheRefreshInterval, consolePortalService::getAllProxy); + + currentDcActiveProxy = new TimeBoundCache<>(config::getCacheRefreshInterval, consolePortalService::getMonitorActiveProxiesByDc); + + 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) { + if(StringUtil.trimEquals(dcName, foundationService.getDataCenter(), true)) { + return currentDcActiveProxy.getData(); + } else { + 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/impl/RouteServiceImpl.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RouteServiceImpl.java index d5b4e6879..da6077320 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RouteServiceImpl.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RouteServiceImpl.java @@ -1,9 +1,9 @@ package com.ctrip.xpipe.redis.console.service.impl; -import com.ctrip.xpipe.api.migration.OuterClientService; 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.annotation.DalTransaction; -import com.ctrip.xpipe.redis.console.dao.ClusterDao; import com.ctrip.xpipe.redis.console.dao.RouteDao; import com.ctrip.xpipe.redis.console.model.DcIdNameMapper; import com.ctrip.xpipe.redis.console.model.DcTbl; @@ -23,6 +23,7 @@ import com.google.common.collect.Lists; 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 java.util.*; @@ -33,14 +34,13 @@ * Jun 19, 2018 */ @Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(false) public class RouteServiceImpl implements RouteService { @Autowired private RouteDao routeDao; - @Autowired - private ClusterDao clusterDao; - @Autowired private DcService dcService; diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RouteServiceWithoutDB.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RouteServiceWithoutDB.java new file mode 100644 index 000000000..d284f74ee --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/impl/RouteServiceWithoutDB.java @@ -0,0 +1,130 @@ +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.DcIdNameMapper; +import com.ctrip.xpipe.redis.console.model.ProxyModel; +import com.ctrip.xpipe.redis.console.model.RouteModel; +import com.ctrip.xpipe.redis.console.model.RouteTbl; +import com.ctrip.xpipe.redis.console.model.consoleportal.RouteDirectionModel; +import com.ctrip.xpipe.redis.console.model.consoleportal.RouteInfoModel; +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 javax.annotation.PostConstruct; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +@Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(true) +public class RouteServiceWithoutDB extends RouteServiceImpl { + + private TimeBoundCache> allRouteCache; + + @Autowired + private ConsolePortalService consolePortalService; + + @Autowired + private ConsoleConfig consoleConfig; + + @PostConstruct + public void init() { + allRouteCache = new TimeBoundCache<>(consoleConfig::getCacheRefreshInterval, consolePortalService::getActiveRoutes); + } + + @Override + public List getActiveRouteTbls() { + throw new UnsupportedOperationException(); + } + + @Override + public List getAllRoutes() { + throw new UnsupportedOperationException(); + } + + @Override + public List getActiveRoutes() { + return allRouteCache.getData(); + } + + @Override + public void updateRoute(RouteModel model) { + throw new UnsupportedOperationException(); + } + + @Override + public void updateRoute(RouteInfoModel model) { + throw new UnsupportedOperationException(); + } + + @Override + public void deleteRoute(long id) { + throw new UnsupportedOperationException(); + } + + @Override + public void updateRoutes(List models) { + throw new UnsupportedOperationException(); + } + + @Override + public void addRoute(RouteModel model) { + throw new UnsupportedOperationException(); + } + + @Override + public void addRoute(RouteInfoModel model) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean existPeerRoutes(String currentDc, String clusterId) { + throw new UnsupportedOperationException(); + } + + @Override + public List getAllActiveRouteInfoModels() { + throw new UnsupportedOperationException(); + } + + @Override + public List getAllActiveRouteInfoModelsByTagAndSrcDcName(String tag, String srcDcName) { + throw new UnsupportedOperationException(); + } + + @Override + public List getAllActiveRouteInfoModelsByTag(String tag) { + throw new UnsupportedOperationException(); + } + + @Override + public List getAllActiveRouteInfoModelsByTagAndDirection(String tag, String srcDcName, String dstDcName) { + throw new UnsupportedOperationException(); + } + + @Override + public RouteInfoModel getRouteInfoModelById(long routeId) { + throw new UnsupportedOperationException(); + } + + @Override + public List getAllRouteDirectionModelsByTag(String tag) { + throw new UnsupportedOperationException(); + } + + @Override + public RouteInfoModel convertRouteTblToRouteInfoModel(RouteTbl routeTbl, DcIdNameMapper dcIdNameMapper, Map proxyIdUriMap) { + throw new UnsupportedOperationException(); + } + + @Override + public Map getRouteIdInfoModelMap() { + 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..f6c999607 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/DcMetaServiceWithoutDB.java @@ -0,0 +1,55 @@ +package com.ctrip.xpipe.redis.console.service.meta.impl; + +import org.apache.commons.lang3.SerializationUtils; +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.*; +import com.ctrip.xpipe.redis.core.meta.MetaCache; +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.Set; +import java.util.stream.Collectors; + +@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 { + // 大小写 + + Set upperCaseAllowTypes = allowTypes.stream() + .map(String::toUpperCase) + .collect(Collectors.toSet()); + + DcMeta dcMeta = metaCache.getXpipeMeta().getDcs().get(dcName); + DcMeta result = SerializationUtils.clone(dcMeta); + result.getClusters().clear(); + Map clusterMetas = dcMeta.getClusters(); + for(ClusterMeta clusterMeta : clusterMetas.values()) { + if(upperCaseAllowTypes.contains(clusterMeta.getType().toUpperCase())) { + result.addCluster(SerializationUtils.clone(clusterMeta)); + } + } + 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..2c3f45056 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; 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..0d3de1fca 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,8 +13,10 @@ 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.ConsoleCrossDcServer; import com.ctrip.xpipe.redis.console.cluster.ConsoleLeaderElector; import com.ctrip.xpipe.redis.console.config.ConsoleConfig; import com.ctrip.xpipe.redis.console.config.ConsoleDbConfig; @@ -29,10 +30,10 @@ 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.service.DcClusterShardService; -import com.ctrip.xpipe.redis.console.service.RedisInfoService; +import com.ctrip.xpipe.redis.console.notifier.cluster.ClusterTypeUpdateEventFactory; +import com.ctrip.xpipe.redis.console.resources.*; +import com.ctrip.xpipe.redis.console.sentinel.SentinelBalanceService; +import com.ctrip.xpipe.redis.console.service.*; import com.ctrip.xpipe.redis.console.service.impl.*; import com.ctrip.xpipe.redis.console.sso.UserAccessFilter; import com.ctrip.xpipe.redis.console.util.DefaultMetaServerConsoleServiceManagerWrapper; @@ -43,6 +44,7 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.*; +import org.springframework.stereotype.Service; /** @@ -87,7 +89,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(); } @@ -96,7 +101,7 @@ public RouteChooseStrategyFactory getRouteChooseStrategyFactory() { return new DefaultRouteChooseStrategyFactory(); } - @Bean + @Bean(name = "metaCache") @Lazy @Profile(AbstractProfile.PROFILE_NAME_TEST) public MetaCache testMetaCache() { @@ -123,6 +128,7 @@ public DcRelationsService dcRelationsService(){ } @Bean + @DependsOn("metaCache") @Profile(AbstractProfile.PROFILE_NAME_PRODUCTION) public ConsoleLeaderElector consoleLeaderElector() { return new ConsoleLeaderElector(); @@ -157,12 +163,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, @@ -182,4 +192,16 @@ public KeeperContainerService keeperContainerService() { return new DefaultKeeperContainerService(); } + @Bean + @Profile(AbstractProfile.PROFILE_NAME_PRODUCTION) + public MetaSynchronizer 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) { + return new MetaSynchronizer(consoleConfig, leaderElector, metaCache, redisService, shardService,clusterService, dcService, + organizationService, sentinelBalanceService, clusterTypeUpdateEventFactory, + foundationService, consoleCrossDcServer); + } + } 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..e71597918 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; @@ -19,12 +20,14 @@ import com.google.common.collect.Lists; import com.google.common.collect.Sets; import org.junit.Assert; +import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.test.util.ReflectionTestUtils; import java.util.*; @@ -34,7 +37,6 @@ @RunWith(MockitoJUnitRunner.Silent.class) public class DcMetaSynchronizerTest { - @InjectMocks private DcMetaSynchronizer dcMetaSynchronizer; @Mock @@ -70,17 +72,26 @@ public class DcMetaSynchronizerTest { private String singleDcCacheCluster = "SingleDcCacheCluster"; private String localDcCacheCluster = "LocalDcCacheCluster"; + private String dcId = FoundationService.DEFAULT.getDataCenter(); + + @Before + public void beforeDcMetaSynchronizerTest() { + dcMetaSynchronizer = new DcMetaSynchronizer(consoleConfig, metaCache, redisService, shardService, + clusterService, dcService, organizationService, sentinelBalanceService, + clusterTypeUpdateEventFactory, outerClientService, dcId); + } + @Test public void syncNoChangedTest() throws Exception { when(organizationService.getAllOrganizations()).thenReturn(Lists.newArrayList( new OrganizationTbl().setId(8L).setOrgId(44).setOrgName("框架"), new OrganizationTbl().setId(9L).setOrgId(45).setOrgName("酒店") )); - + OuterClientService. DcMeta meta = credisDcMeta().setDcName(dcId); 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(meta); + when(metaCache.getXpipeMeta()).thenReturn(new XpipeMeta().addDc(xpipeDcMeta().setId(dcId))); + when(dcService.find(anyString())).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,15 +118,19 @@ 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(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)); + 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(); verify(clusterService, never()).bindDc(any()); @@ -142,14 +157,18 @@ 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(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(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(); verify(clusterService, never()).bindDc(any()); @@ -176,15 +195,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 +230,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 +262,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 +295,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 +328,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 +372,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 +404,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 +438,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 +472,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 +483,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(); @@ -540,7 +559,8 @@ public void extractOuterDcMetaWithInterestedTypesTest() { } OuterClientService.DcMeta credisDcMeta() { - return JsonUtil.fromJson(JsonUtil.credisMetaString, OuterClientService.DcMeta.class); + OuterClientService.DcMeta res = JsonUtil.fromJson(JsonUtil.credisMetaString, OuterClientService.DcMeta.class); + return res; } DcMeta xpipeDcMeta() { diff --git a/redis/redis-core/src/main/java/com/ctrip/xpipe/redis/core/console/ConsoleCheckerPath.java b/redis/redis-core/src/main/java/com/ctrip/xpipe/redis/core/console/ConsoleCheckerPath.java index db6e71d58..16f01e071 100644 --- a/redis/redis-core/src/main/java/com/ctrip/xpipe/redis/core/console/ConsoleCheckerPath.java +++ b/redis/redis-core/src/main/java/com/ctrip/xpipe/redis/core/console/ConsoleCheckerPath.java @@ -12,6 +12,8 @@ private ConsoleCheckerPath() {} public static final String PATH_GET_ALL_META = "/api/meta/divide"; + public static final String PATH_GET_ALL_META_LONG_PULL = "/api/meta/divide/long"; + public static final String PATH_GET_DC_ALL_META = "/api/meta/{dcName}/all"; public static final String PATH_GET_META = "/api/meta/divide/{index}"; 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..ed0dcfb45 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; @@ -23,6 +20,8 @@ public interface MetaCache { XpipeMeta getXpipeMeta(); + XpipeMeta getXpipeMetaLongPull(long updateTime) throws InterruptedException; + XpipeMeta getDividedXpipeMeta(int partIndex); XpipeMetaManager.MetaDesc findMetaDesc(HostPort hostPort); @@ -93,4 +92,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 @@ + From 4eb4f97a37d97b00bb1f67db444e677bfa8fdd02 Mon Sep 17 00:00:00 2001 From: qifanwang Date: Wed, 11 Sep 2024 18:59:14 +0800 Subject: [PATCH 2/6] change no to dcId for dcmeta --- .../redis/console/reporter/DefaultMigrationResultReporter.java | 2 +- .../redis/console/service/meta/impl/AdvancedDcMetaService.java | 2 +- .../src/main/resources/META-INF/dal/model/keeper-codegen.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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 8bff8b3f5..e3d8d3bd5 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 @@ -86,7 +86,7 @@ private Map buildDcNameMap() { } Map allDcs = xpipeMeta.getDcs(); Map result = new HashMap<>(); - allDcs.values().forEach(dc -> result.put(dc.getNo(), dc.getId())); + allDcs.values().forEach(dc -> result.put(dc.getDcId(), dc.getId())); return result; } 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 267acb8eb..9746702fd 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 @@ -183,7 +183,7 @@ public Map getAllDcMetas() throws Exception { .setId(dcTbl.getDcName()) .setLastModifiedTime(dcTbl.getDcLastModifiedTime()) .setZone(zoneTbl.getZoneName()) - .setNo(dcTbl.getId()); + .setDcId(dcTbl.getId()); dcMetaMap.put(dcMeta.getId().toUpperCase(), dcMeta); chain.add(retry3TimesUntilSuccess(new GetAllSentinelCommand(dcMeta))); 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 a273db696..719411f1f 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,7 +12,7 @@ - + From 5c9b5e8a87c0e4f68dd17fd1b8a30efb0c601be3 Mon Sep 17 00:00:00 2001 From: qifanwang Date: Thu, 12 Sep 2024 14:27:31 +0800 Subject: [PATCH 3/6] improve for getAllShardNamesByClusterName --- .../com/ctrip/xpipe/redis/checker/impl/TestMetaCache.java | 5 +++-- .../xpipe/redis/console/resources/AbstractMetaCache.java | 6 +++--- .../impl/DefaultClusterHealthMonitorManagerTest.java | 6 ++++-- .../cluster/impl/DefaultClusterHealthMonitorTest.java | 6 ++++-- .../java/com/ctrip/xpipe/redis/core/meta/MetaCache.java | 2 +- 5 files changed, 15 insertions(+), 10 deletions(-) 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 8fc1590e1..69108195a 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 @@ -213,7 +213,8 @@ public boolean isAsymmetricCluster(String clusterName) { } @Override - public List getAllShardNamesByClusterName(String clusterName) { - return Collections.emptyList(); + public Set getAllShardNamesByClusterName(String clusterName) { + return Collections.emptySet(); } + } 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 aca87dd46..ac76eb390 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 @@ -677,14 +677,14 @@ private void checkDcClustersCnt(DcMeta currentDcMeta, DcMeta futureDcMeta, int m } @Override - public List getAllShardNamesByClusterName(String clusterName) { - List shards = new ArrayList<>(); + public Set getAllShardNamesByClusterName(String clusterName) { + Set shards = new HashSet<>(); 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()); + shards.addAll(shardMetaMap.keySet()); } } return shards; 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 10b8fa190..dd290f562 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 @@ -14,7 +14,9 @@ import org.junit.Before; import org.junit.Test; +import java.util.HashSet; import java.util.List; +import java.util.Set; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -151,9 +153,9 @@ public void testOutterClientMasterUp() { } private void fakeShardService(String clusterId, String... shardIds) { - List result = Lists.newArrayList(); + Set result = new HashSet<>(); for(String shardId : shardIds) { - result.add(new ShardMeta().setId(shardId)); + result.add(shardId); } when(metaCache.getAllShardNamesByClusterName(clusterId)).thenReturn(result); } 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 c929616e3..ee63d7e84 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 @@ -14,7 +14,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -154,9 +156,9 @@ protected void doRun() throws Exception { } private void fakeShardService(String... shardIds) { - List result = Lists.newArrayList(); + Set result = new HashSet<>(); for(String shardId : shardIds) { - result.add(new ShardMeta().setId(shardId)); + result.add(shardId); } when(metaCache.getAllShardNamesByClusterName(clusterId)).thenReturn(result); } 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 ed0dcfb45..1256eacd5 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 @@ -93,5 +93,5 @@ public interface MetaCache { boolean isAsymmetricCluster(String clusterName); - List getAllShardNamesByClusterName(String clusterName); + Set getAllShardNamesByClusterName(String clusterName); } From bd3c1f5b516c8ec7f022658b1884750b19287d92 Mon Sep 17 00:00:00 2001 From: qifanwang Date: Thu, 12 Sep 2024 15:07:21 +0800 Subject: [PATCH 4/6] not need access db if disabledb is true --- .../checker/config/impl/CommonConfigBean.java | 6 +- .../config/impl/DataCenterConfigBean.java | 6 + .../redis/console/config/ConsoleConfig.java | 2 + .../config/impl/CombConsoleConfig.java | 618 ------------------ .../config/impl/DefaultConsoleConfig.java | 583 ++++++++--------- .../api/checker/ConsoleCheckerController.java | 30 +- .../console/ds/MybatisDataSourceConfig.java | 11 +- .../console/resources/AbstractMetaCache.java | 9 +- .../resources/ConsoleMetaCacheWithoutDB.java | 45 +- .../resources/ConsolePortalService.java | 59 +- .../console/resources/DefaultMetaCache.java | 11 +- .../console/resources/MetaSynchronizer.java | 78 +-- .../meta/impl/AdvancedDcMetaService.java | 6 +- .../meta/impl/RedisMetaServiceImpl.java | 5 + .../meta/impl/RedisMetaServiceWithoutDB.java | 23 + .../console/spring/CheckerContextConfig.java | 4 +- .../console/spring/ConsoleContextConfig.java | 7 +- .../redis/console/AbstractConsoleTest.java | 4 +- .../config/impl/DefaultConsoleConfigTest.java | 4 +- .../ExclusiveThreadsForMigrationTest.java | 4 +- .../redis/console/health/ContextConfig.java | 4 +- .../RedisConfigCheckMonitorTest.java | 4 +- .../core/console/ConsoleCheckerPath.java | 2 +- .../META-INF/dal/model/keeper-codegen.xml | 1 + .../config/SpringEnvConsoleConfig.java | 4 +- 25 files changed, 491 insertions(+), 1039 deletions(-) delete mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/impl/CombConsoleConfig.java create mode 100644 redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/RedisMetaServiceWithoutDB.java 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 e077d3233..2f0e4efc7 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 @@ -9,6 +9,7 @@ import org.springframework.context.annotation.Configuration; import java.util.*; +import java.util.stream.Collectors; @Configuration public class CommonConfigBean extends AbstractConfigBean { @@ -68,7 +69,10 @@ public Set getExtraSyncDC() { } public boolean disableDb() { - return getExtraSyncDC().contains(FoundationService.DEFAULT.getDataCenter()); + return getExtraSyncDC().stream() + .map(String::toUpperCase) + .collect(Collectors.toSet()) + .contains(FoundationService.DEFAULT.getDataCenter()); } public Set getAlertWhileList() { 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..dc4177418 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_CONSOLE_NO_DB_DOMAIN = "console.no.db.domain"; + 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 String getConsoleNoDbDomain() { + return getProperty(KEY_CONSOLE_NO_DB_DOMAIN, ""); + } + public String getZkConnectionString() { return getProperty(KEY_ZK_ADDRESS, zkConnection.get() == null ? "127.0.0.1:2181" : zkConnection.get()); } 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 c680f57cd..ca8f7e3bb 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 @@ -154,4 +154,6 @@ public interface ConsoleConfig extends CoreConfig, CheckerConfig, AlertConfig { Set getExtraSyncDC(); + String getConsoleNoDbDomain(); + } 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 deleted file mode 100644 index 4d66a2eda..000000000 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/config/impl/CombConsoleConfig.java +++ /dev/null @@ -1,618 +0,0 @@ -package com.ctrip.xpipe.redis.console.config.impl; - -import com.ctrip.xpipe.api.codec.GenericTypeReference; -import com.ctrip.xpipe.api.config.ConfigChangeListener; -import com.ctrip.xpipe.api.foundation.FoundationService; -import com.ctrip.xpipe.cluster.ClusterType; -import com.ctrip.xpipe.codec.JsonCodec; -import com.ctrip.xpipe.config.AbstractConfigBean; -import com.ctrip.xpipe.config.ConfigKeyListener; -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; -import com.ctrip.xpipe.redis.checker.config.impl.DataCenterConfigBean; -import com.ctrip.xpipe.redis.console.config.ConsoleConfig; -import com.ctrip.xpipe.redis.console.config.model.BeaconOrgRoute; -import com.ctrip.xpipe.redis.console.util.HickwallMetricInfo; -import com.ctrip.xpipe.redis.core.meta.QuorumConfig; -import com.ctrip.xpipe.tuple.Pair; -import com.ctrip.xpipe.utils.StringUtil; -import com.google.common.collect.Maps; -import io.netty.util.internal.ConcurrentSet; - -import java.util.*; - -public class CombConsoleConfig implements ConsoleConfig, ConfigChangeListener { - - private CheckConfigBean checkConfigBean; - - private ConsoleConfigBean consoleConfigBean; - - private DataCenterConfigBean dataCenterConfigBean; - - private CommonConfigBean commonConfigBean; - - private List configBeans; - - public CombConsoleConfig(CheckConfigBean checkConfigBean, - ConsoleConfigBean consoleConfigBean, - DataCenterConfigBean dataCenterConfigBean, - CommonConfigBean commonConfigBean) { - this.checkConfigBean = checkConfigBean; - this.consoleConfigBean = consoleConfigBean; - this.dataCenterConfigBean = dataCenterConfigBean; - this.commonConfigBean = commonConfigBean; - configBeans = new ArrayList<>(); - configBeans.add(consoleConfigBean); - configBeans.add(dataCenterConfigBean); - configBeans.add(commonConfigBean); - configBeans.add(checkConfigBean); - for (AbstractConfigBean configBean : configBeans) { - configBean.addConfigChangeListener(this); - } - } - - private Map> listeners = Maps.newConcurrentMap(); - - private Set listenersSet = new ConcurrentSet<>(); - - private String hickwallInfo; - - private HickwallMetricInfo info; - - - - @Override - public String getServerMode() { - return checkConfigBean.getServerMode(); - } - - @Override - public String getDatasource() { - return commonConfigBean.getDatasource(); - } - - @Override - public int getConsoleNotifyRetryTimes() { - return consoleConfigBean.getConsoleNotifyRetryTimes(); - } - - @Override - public int getConsoleNotifyRetryInterval() { - return consoleConfigBean.getConsoleNotifyRetryInterval(); - } - - @Override - public Map getMetaservers() { - return dataCenterConfigBean.getMetaservers(); - } - - @Override - public int getConsoleNotifyThreads() { - return consoleConfigBean.getConsoleNotifyThreads(); - } - - @Override - public Set getConsoleUserAccessWhiteList() { - return commonConfigBean.getConsoleUserAccessWhiteList(); - } - - @Override - public int getRedisReplicationHealthCheckInterval() { - return checkConfigBean.getRedisReplicationHealthCheckInterval(); - } - - @Override - public int getCheckerCurrentDcAllMetaRefreshIntervalMilli() { - return checkConfigBean.getCheckerCurrentDcAllMetaRefreshIntervalMilli(); - } - - @Override - public int getClusterHealthCheckInterval() { - return checkConfigBean.getClusterHealthCheckInterval(); - } - - @Override - public Map getHickwallClusterMetricFormat() { - return commonConfigBean.getHickwallClusterMetricFormat(); - } - - @Override - public HickwallMetricInfo getHickwallMetricInfo() { - String localInfo = commonConfigBean.getHickwallMetricInfo(); - if(StringUtil.isEmpty(hickwallInfo) || !localInfo.equals(hickwallInfo)) { - hickwallInfo = localInfo; - info = JsonCodec.INSTANCE.decode(hickwallInfo, HickwallMetricInfo.class); - } - return info; - } - - @Override - public int getHealthyDelayMilli() { - return checkConfigBean.getHealthyDelayMilli(); - } - - @Override - public long getHealthMarkCompensateIntervalMill() { - return checkConfigBean.getHealthMarkCompensateIntervalMill(); - } - - @Override - public int getHealthMarkCompensateThreads() { - return checkConfigBean.getHealthMarkCompensateThreads(); - } - - @Override - public int getHealthyDelayMilliThroughProxy() { - return checkConfigBean.getHealthyDelayMilliThroughProxy(); - } - - @Override - public int getInstanceLongDelayMilli() { - return checkConfigBean.getInstanceLongDelayMilli(); - } - - @Override - public int getDownAfterCheckNums() { - return checkConfigBean.getDownAfterCheckNums(); - } - - @Override - public int getDownAfterCheckNumsThroughProxy() { - return checkConfigBean.getDownAfterCheckNumsThroughProxy(); - } - - @Override - public int getCacheRefreshInterval() { - return consoleConfigBean.getCacheRefreshInterval(); - } - - @Override - public Set getAlertWhileList() { - return commonConfigBean.getAlertWhileList(); - } - - @Override - public int getQuorum() { - return checkConfigBean.getQuorum(); - } - - @Override - public int getRedisConfCheckIntervalMilli() { - return checkConfigBean.getRedisConfCheckIntervalMilli(); - } - - @Override - public int getSentinelCheckIntervalMilli() { - return checkConfigBean.getSentinelCheckIntervalMilli(); - } - - @Override - public String getConsoleDomain() { - return commonConfigBean.getConsoleDomain(); - } - - @Override - public String getClusterExcludedRegex() { - return commonConfigBean.getClusterExcludedRegex(); - } - - @Override - public QuorumConfig getDefaultSentinelQuorumConfig() { - return checkConfigBean.getDefaultSentinelQuorumConfig(); - } - - @Override - public int getStableLossAfterRounds() { - return checkConfigBean.getStableLossAfterRounds(); - } - - @Override - public int getStableRecoverAfterRounds() { - return checkConfigBean.getStableRecoverAfterRounds(); - } - - @Override - public int getStableResetAfterRounds() { - return checkConfigBean.getStableResetAfterRounds(); - } - - @Override - public float getSiteStableThreshold() { - return checkConfigBean.getSiteStableThreshold(); - } - - @Override - public float getSiteUnstableThreshold() { - return checkConfigBean.getSiteUnstableThreshold(); - } - - @Override - public Boolean getSiteStable() { - return checkConfigBean.getSiteStable(); - } - - @Override - public String getReplDisklessMinRedisVersion() { - return checkConfigBean.getReplDisklessMinRedisVersion(); - } - - @Override - public String getXRedisMinimumRequestVersion() { - return checkConfigBean.getXRedisMinimumRequestVersion(); - } - - @Override - public String getXpipeRuntimeEnvironment() { - return commonConfigBean.getXpipeRuntimeEnvironment(); - } - - @Override - public String getDBAEmails() { - return commonConfigBean.getDBAEmails(); - } - - @Override - public String getRedisAlertSenderEmail() { - return commonConfigBean.getRedisAlertSenderEmail(); - } - - @Override - public String getXPipeAdminEmails() { - return commonConfigBean.getXPipeAdminEmails(); - } - - @Override - public int getAlertSystemSuspendMinute() { - return commonConfigBean.getAlertSystemSuspendMinute(); - } - - @Override - public int getAlertSystemRecoverMinute() { - return commonConfigBean.getAlertSystemRecoverMinute(); - } - - @Override - public int getConfigDefaultRestoreHours() { - return consoleConfigBean.getConfigDefaultRestoreHours(); - } - - @Override - public int getRebalanceSentinelInterval() { - return consoleConfigBean.getRebalanceSentinelInterval(); - } - - @Override - public int getRebalanceSentinelMaxNumOnce() { - return consoleConfigBean.getRebalanceSentinelMaxNumOnce(); - } - - @Override - public int getNoAlarmMinutesForClusterUpdate() { - return commonConfigBean.getNoAlarmMinutesForClusterUpdate(); - } - - @Override - public int getHealthCheckSuspendMinutes() { - return consoleConfigBean.getHealthCheckSuspendMinutes(); - } - - @Override - public Set getIgnoredHealthCheckDc() { - return checkConfigBean.getIgnoredHealthCheckDc(); - } - - @Override - public int getClustersPartIndex() { - return checkConfigBean.getClustersPartIndex(); - } - - @Override - public int getCheckerReportIntervalMilli() { - return checkConfigBean.getCheckerReportIntervalMilli(); - } - - @Override - public int getCheckerMetaRefreshIntervalMilli() { - return checkConfigBean.getCheckerMetaRefreshIntervalMilli(); - } - - @Override - public String getConsoleAddress() { - return checkConfigBean.getConsoleAddress(); - } - - @Override - public int getCheckerAckIntervalMilli() { - return checkConfigBean.getCheckerAckIntervalMilli(); - } - - @Override - public long getConfigCacheTimeoutMilli() { - return checkConfigBean.getConfigCacheTimeoutMilli(); - } - - @Override - public int getProxyCheckUpRetryTimes() { - return checkConfigBean.getProxyCheckUpRetryTimes(); - } - - @Override - public int getProxyCheckDownRetryTimes() { - return checkConfigBean.getProxyCheckDownRetryTimes(); - } - - private Set sentinelCheckOuterClientClusters() { - return checkConfigBean.sentinelCheckOuterClientClusters(); - } - - @Override - public boolean supportSentinelHealthCheck(ClusterType clusterType, String clusterName) { - return clusterType.supportHealthCheck() - || checkConfigBean.shouldSentinelCheckOuterClientClusters() - || sentinelCheckOuterClientClusters().contains(clusterName.toLowerCase()); - } - - @Override - public void register(List keys, ConfigChangeListener configListener) { - for(String key : keys) { - listeners.putIfAbsent(key, new LinkedList<>()); - listeners.get(key).add(configListener); - } - } - - @Override - public String sentinelCheckDowngradeStrategy() { - return checkConfigBean.sentinelCheckDowngradeStrategy(); - } - - @Override - public String crossDcSentinelMonitorNameSuffix() { - return checkConfigBean.crossDcSentinelMonitorNameSuffix(); - } - - @Override - public int getNonCoreCheckIntervalMilli() { - return checkConfigBean.getNonCoreCheckIntervalMilli(); - } - - @Override - public Set getOuterClusterTypes() { - return consoleConfigBean.getOuterClusterTypes(); - } - - @Override - public Map sentinelMasterConfig() { - return checkConfigBean.sentinelMasterConfig(); - } - - @Override - public long subscribeTimeoutMilli() { - return checkConfigBean.subscribeTimeoutMilli(); - } - - @Override - public String getDcsRelations() { - return commonConfigBean.getDcsRelations(); - } - - @Override - public int maxRemovedDcsCnt() { - return checkConfigBean.maxRemovedDcsCnt(); - } - - @Override - public int maxRemovedClustersPercent() { - return checkConfigBean.maxRemovedClustersPercent(); - } - - @Override - public int getKeeperCheckerIntervalMilli() { - return checkConfigBean.getKeeperCheckerIntervalMilli(); - } - - @Override - public int getPingDownAfterMilli() { - return checkConfigBean.getPingDownAfterMilli(); - } - - @Override - public int getPingDownAfterMilliThroughProxy() { - return checkConfigBean.getPingDownAfterMilliThroughProxy(); - } - - @Override - public Pair getClusterShardForMigrationSysCheck() { - return consoleConfigBean.getClusterShardForMigrationSysCheck(); - } - - @Override - public int getProxyInfoCollectInterval() { - return consoleConfigBean.getProxyInfoCollectInterval(); - } - - @Override - public int getOutterClientCheckInterval() { - return checkConfigBean.getOutterClientCheckInterval(); - } - - @Override - public int getOuterClientSyncInterval() { - return consoleConfigBean.getOuterClientSyncInterval(); - } - - @Override - public String getOuterClientToken() { - return commonConfigBean.getOuterClientToken(); - } - - @Override - public Map getConsoleDomains() { - return dataCenterConfigBean.getConsoleDomains(); - } - - @Override - public boolean isSentinelRateLimitOpen() { - return checkConfigBean.isSentinelRateLimitOpen(); - } - - @Override - public int getSentinelRateLimitSize() { - return checkConfigBean.getSentinelRateLimitSize(); - } - - @Override - public Set getVariablesCheckDataSources() { - return consoleConfigBean.getVariablesCheckDataSources(); - } - - @Override - public Set getOwnClusterType() { - return consoleConfigBean.getOwnClusterType(); - } - - @Override - public Set shouldNotifyClusterTypes() { - return commonConfigBean.shouldNotifyClusterTypes(); - } - - @Override - public String getCrossDcLeaderLeaseName() { - return dataCenterConfigBean.getCrossDcLeaderLeaseName(); - } - - @Override - public List getBeaconOrgRoutes() { - String property = dataCenterConfigBean.getBeaconOrgRoutes(); - return JsonCodec.INSTANCE.decode(property, new GenericTypeReference>() {}); - } - - @Override - public int getClusterDividedParts() { - List groupList = checkConfigBean.getClustersList(); - return groupList.size(); - } - - @Override - public int getCheckerAckTimeoutMilli() { - return dataCenterConfigBean.getCheckerAckTimeoutMilli(); - } - - @Override - public long getMigrationTimeoutMilli() { - return consoleConfigBean.getMigrationTimeoutMilli(); - } - - @Override - public long getServletMethodTimeoutMilli() { - return consoleConfigBean.getServletMethodTimeoutMilli(); - } - - @Override - public boolean isRedisConfigCheckMonitorOpen() { - return checkConfigBean.isRedisConfigCheckMonitorOpen(); - } - - @Override - public String getRedisConfigCheckRules() { - return checkConfigBean.getRedisConfigCheckRules(); - } - - @Override - public String getChooseRouteStrategyType() { - return commonConfigBean.getChooseRouteStrategyType(); - } - - @Override - public boolean isAutoMigrateOverloadKeeperContainerOpen() { - return consoleConfigBean.isAutoMigrateOverloadKeeperContainerOpen(); - } - - @Override - public long getAutoMigrateOverloadKeeperContainerIntervalMilli() { - return consoleConfigBean.getAutoMigrateOverloadKeeperContainerIntervalMilli(); - } - - @Override - public double getKeeperPairOverLoadFactor() { - return consoleConfigBean.getKeeperPairOverLoadFactor(); - } - - @Override - public double getKeeperContainerDiskOverLoadFactor() { - return consoleConfigBean.getKeeperContainerDiskOverLoadFactor(); - } - - @Override - public double getKeeperContainerIoRate() { - return consoleConfigBean.getKeeperContainerIoRate(); - } - - @Override - public long getMetaServerSlotClusterMapCacheTimeOutMilli() { - return consoleConfigBean.getMetaServerSlotClusterMapCacheTimeOutMilli(); - } - - @Override - public boolean autoSetKeeperSyncLimit() { - return consoleConfigBean.autoSetKeeperSyncLimit(); - } - - @Override - public boolean disableDb() { - return commonConfigBean.disableDb(); - } - - @Override - public Set getExtraSyncDC() { - return commonConfigBean.getExtraSyncDC(); - } - - @Override - public void addListener(ConfigKeyListener listener) { - this.listenersSet.add(listener); - } - - @Override - public String getZkConnectionString() { - return dataCenterConfigBean.getZkConnectionString(); - } - - @Override - public String getZkNameSpace() { - return dataCenterConfigBean.getZkNameSpace(); - } - - public void onChange(String key, String oldValue, String newValue) { - - for(ConfigKeyListener listener : listenersSet) { - listener.onChange(key, newValue); - } - - if(!listeners.containsKey(key)) { - return; - } - for(ConfigChangeListener listener : listeners.get(key)) { - listener.onChange(key, oldValue, newValue); - } - } - - protected String getProperty(String key) { - for(AbstractConfigBean configBean : configBeans) { - String val = configBean.getProperty(key); - if(val != null) { - return val; - } - } - return null; - } - - protected String getProperty(String key, String def) { - String value = this.getProperty(key); - if(value == null) { - return def; - } else { - return value; - } - } -} 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 a03fa76ba..7a7bb60d1 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 @@ -4,205 +4,121 @@ import com.ctrip.xpipe.api.config.ConfigChangeListener; import com.ctrip.xpipe.cluster.ClusterType; import com.ctrip.xpipe.codec.JsonCodec; +import com.ctrip.xpipe.config.AbstractConfigBean; +import com.ctrip.xpipe.config.ConfigKeyListener; +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; +import com.ctrip.xpipe.redis.checker.config.impl.DataCenterConfigBean; import com.ctrip.xpipe.redis.console.config.ConsoleConfig; import com.ctrip.xpipe.redis.console.config.model.BeaconOrgRoute; import com.ctrip.xpipe.redis.console.util.HickwallMetricInfo; -import com.ctrip.xpipe.redis.core.config.AbstractCoreConfig; import com.ctrip.xpipe.redis.core.meta.QuorumConfig; -import com.ctrip.xpipe.redis.core.route.RouteChooseStrategyFactory; import com.ctrip.xpipe.tuple.Pair; import com.ctrip.xpipe.utils.StringUtil; import com.google.common.collect.Maps; +import io.netty.util.internal.ConcurrentSet; import java.util.*; -import static com.ctrip.xpipe.redis.checker.config.impl.CheckConfigBean.*; +public class DefaultConsoleConfig implements ConsoleConfig, ConfigChangeListener { -/** - * @author shyin - *

- * Oct 19, 2016 - */ -public class DefaultConsoleConfig extends AbstractCoreConfig implements ConsoleConfig { + private CheckConfigBean checkConfigBean; - public static final String KEY_SERVER_MODE = "console.server.mode"; - public static final String KEY_DATASOURCE = "datasource"; - public static final String KEY_CONSOLE_NOTIFY_RETRY_TIMES = "console.notify.retry.times"; - public static final String KEY_CONSOLE_NOTIFY_THREADS = "console.notify.threads"; - public static final String KEY_CONSOLE_NOTIFY_RETRY_INTERVAL = "console.notify.retry.interval"; - public static final String KEY_METASERVERS = "metaservers"; - public static final String KEY_USER_ACCESS_WHITE_LIST = "user.access.white.list"; - public static final String KEY_HICKWALL_CLUSTER_METRIC_FORMAT = "console.hickwall.cluster.metric.format"; - public static final String KEY_HICKWALL_METRIC_INFO = "console.hickwall.metric.info"; - public static final String KEY_CACHE_REFERSH_INTERVAL = "console.cache.refresh.interval"; + private ConsoleConfigBean consoleConfigBean; - private static final String KEY_CONFIG_DEFAULT_RESTORE_HOUR = "console.config.default.restore.hour"; + private DataCenterConfigBean dataCenterConfigBean; - private static final String KEY_REBALANCE_SENTINEL_INTERVAL = "rebalance.sentinel.interval.second"; + private CommonConfigBean commonConfigBean; - private static final String KEY_REBALANCE_SENTINEL_MAX_NUM_ONCE = "rebalance.sentinel.max.num.once"; + private List configBeans; - public static final String KEY_CLUSTER_SHARD_FOR_MIGRATE_SYS_CHECK = "console.cluster.shard.for.migrate.sys.check"; - - private static final String KEY_PROXY_INFO_CHECK_INTERVAL = "console.proxy.info.collector.check.interval"; - - private static final String KEY_OUTTER_CLIENT_CHECK_INTERVAL = "console.outter.client.check.interval"; - - private static final String KEY_OUTER_CLIENT_SYNC_INTERVAL = "console.outer.client.sync.interval"; - - private static final String KEY_OUTER_CLIENT_TOKEN = "console.outer.client.token"; - - private static final String KEY_VARIABLES_CHECK_DATASOURCE = "console.health.variables.datasource"; - - private static final String KEY_OWN_CLUSTER_TYPES = "console.cluster.types"; - - private static final String KEY_NOTIFY_CLUSTER_TYPES = "console.notify.cluster.types"; - - private static final String KEY_OUTER_CLUSTER_TYPES = "console.outer.cluster.types"; - - private static final String KEY_NO_HEALTH_CHECK_MINUTES = "no.health.check.minutes"; - - private static final String KEY_CROSS_DC_LEADER_LEASE_NAME = "console.cross.dc.leader.lease.name"; - - public static final String KEY_BEACON_ORG_ROUTE = "beacon.org.routes"; - - private static final String KEY_CLUSTER_DIVIDED_PARTS = "console.cluster.divide.parts"; - - private static final String KEY_CHECKER_ACK_TIMEOUT_MILLI = "checker.ack.timeout.milli"; - - private static final String KEY_MIGRATION_TIMEOUT_MILLI = "migration.timeout.milli"; - - private static final String KEY_SERVLET_METHOD_TIMEOUT_MILLI = "servlet.method.timeout.milli"; - - private static final String KEY_REDIS_CONFIG_CHECK_MONITOR_OPEN = "cosnole.redis.config.check.open"; - - private static final String KEY_REDIS_CONFIG_CHECK_RULES = "console.redis.config.check.rules"; - private static final String KEY_CROSS_DC_SENTINEL_MONITOR_NAME_SUFFIX = "checker.cross.dc.sentinel.monitor.name.suffix"; - - private static final String KEY_SENTINEL_MASTER_CONFIG = "checker.sentinel.master.config"; - - private static final String KEY_ROUTE_CHOOSE_STRATEGY_TYPE = "route.choose.strategy.type"; - - private static final String KEY_DCS_RELATIONS = "dcs.relations"; - - private static final String KEY_MAX_REMOVED_DCS_CNT = "max.removed.dcs.count"; - private static final String KEY_MAX_REMOVED_CLUSTERS_PERCENT = "max.removed.clusters.percent"; - - private static final String KEY_CONSOLE_KEEPER_PAIR_OVERLOAD_FACTOR = "console.keeper.container.pair.overload.standard.factor"; - private static final String KEY_CONSOLE_KEEPER_CONTAINER_DISK_OVERLOAD_FACTOR = "console.keeper.container.disk.overload.factor"; - private static final String KEY_CONSOLE_KEEPER_CONTAINER_IO_RATE = "console.keeper.container.io.rate"; - private static final String KEY_CONSOLE_AUTO_MIGRATE_OVERLOAD_KEEPER_CONTAINER_OPEN = "console.auto.migrate.overload.keeper.container.open"; - private static final String KEY_CONSOLE_AUTO_MIGRATE_OVERLOAD_KEEPER_CONTAINER_INTERVAL_MILLI = "console.auto.migrate.overload.keeper.container.interval.milli"; - private static final String KEY_CONSOLE_META_SLOT_CACHE_MILLI = "console.meta.slot.cache.milli"; - - private static final String KEY_CLUSTERS_PART_INDEX = "checker.clusters.part.index" ; - - private static final String KEY_KEEPERCONTAINER_SYNC_LIMIT_ON = "keepercontainer.sync.limit.on"; - - private String defaultRouteChooseStrategyType = RouteChooseStrategyFactory.RouteStrategyType.CRC32_HASH.name(); - - private Map> listeners = Maps.newConcurrentMap(); - - @Override - public String getServerMode() { - return getProperty(KEY_SERVER_MODE, "CONSOLE_CHECKER"); - } - - @Override - public int getAlertSystemRecoverMinute() { - return getIntProperty(KEY_ALERT_MESSAGE_RECOVER_TIME, 5); + public DefaultConsoleConfig(CheckConfigBean checkConfigBean, + ConsoleConfigBean consoleConfigBean, + DataCenterConfigBean dataCenterConfigBean, + CommonConfigBean commonConfigBean) { + this.checkConfigBean = checkConfigBean; + this.consoleConfigBean = consoleConfigBean; + this.dataCenterConfigBean = dataCenterConfigBean; + this.commonConfigBean = commonConfigBean; + configBeans = new ArrayList<>(); + configBeans.add(consoleConfigBean); + configBeans.add(dataCenterConfigBean); + configBeans.add(commonConfigBean); + configBeans.add(checkConfigBean); + for (AbstractConfigBean configBean : configBeans) { + configBean.addConfigChangeListener(this); + } } - @Override - public int getAlertSystemSuspendMinute() { - return getIntProperty(KEY_ALERT_MESSAGE_SUSPEND_TIME, 30); - } + private Map> listeners = Maps.newConcurrentMap(); - @Override - public String getDBAEmails() { - return getProperty(KEY_DBA_EMAILS, "DBA@email.com"); - } + private Set listenersSet = new ConcurrentSet<>(); - @Override - public String getRedisAlertSenderEmail() { - return getProperty(KEY_REDIS_ALERT_SENDER_EMAIL, ""); - } + private String hickwallInfo; - @Override - public String getXpipeRuntimeEnvironment() { - return getProperty(KEY_XPIPE_RUNTIME_ENVIRONMENT, ""); - } + private HickwallMetricInfo info; - @Override - public String getXPipeAdminEmails() { - return getProperty(KEY_XPIPE_ADMIN_EMAILS, "XPipeAdmin@email.com"); - } - @Override - public String getReplDisklessMinRedisVersion() { - return getProperty(KEY_REDIS_REPL_DISKLESS_MINIMUM_VERSION, "2.8.22"); - } @Override - public String getXRedisMinimumRequestVersion() { - return getProperty(KEY_XREDIS_REQUEST_MINI_VERSION, "0.0.3"); + public String getServerMode() { + return checkConfigBean.getServerMode(); } @Override public String getDatasource() { - return getProperty(KEY_DATASOURCE, "fxxpipe"); + return commonConfigBean.getDatasource(); } @Override public int getConsoleNotifyRetryTimes() { - return getIntProperty(KEY_CONSOLE_NOTIFY_RETRY_TIMES, 3); + return consoleConfigBean.getConsoleNotifyRetryTimes(); } @Override public int getConsoleNotifyRetryInterval() { - return getIntProperty(KEY_CONSOLE_NOTIFY_RETRY_INTERVAL, 100); + return consoleConfigBean.getConsoleNotifyRetryInterval(); } @Override - public Map getMetaservers() { - String property = getProperty(KEY_METASERVERS, "{}"); - return JsonCodec.INSTANCE.decode(property, Map.class); + public Map getMetaservers() { + return dataCenterConfigBean.getMetaservers(); } @Override public int getConsoleNotifyThreads() { - return getIntProperty(KEY_CONSOLE_NOTIFY_THREADS, 20); + return consoleConfigBean.getConsoleNotifyThreads(); } @Override public Set getConsoleUserAccessWhiteList() { - String whiteList = getProperty(KEY_USER_ACCESS_WHITE_LIST, "*"); - return new HashSet<>(Arrays.asList(whiteList.split(","))); + return commonConfigBean.getConsoleUserAccessWhiteList(); } @Override public int getRedisReplicationHealthCheckInterval() { - return getIntProperty(KEY_REDIS_REPLICATION_HEALTH_CHECK_INTERVAL, 2000); + return checkConfigBean.getRedisReplicationHealthCheckInterval(); } @Override - public int getClusterHealthCheckInterval() { - return getIntProperty(KEY_CLUSTER_HEALTH_CHECK_INTERVAL, 300000); + public int getCheckerCurrentDcAllMetaRefreshIntervalMilli() { + return checkConfigBean.getCheckerCurrentDcAllMetaRefreshIntervalMilli(); } - private String hickwallInfo; - - private HickwallMetricInfo info; + @Override + public int getClusterHealthCheckInterval() { + return checkConfigBean.getClusterHealthCheckInterval(); + } @Override - public Map getHickwallClusterMetricFormat() { - String property = getProperty(KEY_HICKWALL_CLUSTER_METRIC_FORMAT, "{}"); - return JsonCodec.INSTANCE.decode(property, Map.class); + public Map getHickwallClusterMetricFormat() { + return commonConfigBean.getHickwallClusterMetricFormat(); } @Override public HickwallMetricInfo getHickwallMetricInfo() { - String localInfo = getProperty(KEY_HICKWALL_METRIC_INFO, "{\"domain\": \"http://hickwall.qa.nt.ctripcorp.com/grafanav2/d/UR32kfjWz/fx-xpipe?fullscreen&orgId=1&from=now-1h&to=now\", \"delayPanelId\": 2, \"crossDcDelayPanelId\": 14, \"proxyPingPanelId\": 4, \"proxyTrafficPanelId\": 6, \"proxyCollectionPanelId\": 8, \"outComingTrafficToPeerPanelId\": \"16\", \"inComingTrafficFromPeerPanelId\": \"18\",\"peerSyncFullPanelId\": \"20\",\"peerSyncPartialPanelId\": \"22\"}"); + String localInfo = commonConfigBean.getHickwallMetricInfo(); if(StringUtil.isEmpty(hickwallInfo) || !localInfo.equals(hickwallInfo)) { hickwallInfo = localInfo; info = JsonCodec.INSTANCE.decode(hickwallInfo, HickwallMetricInfo.class); @@ -212,438 +128,495 @@ public HickwallMetricInfo getHickwallMetricInfo() { @Override public int getHealthyDelayMilli() { - return getIntProperty(KEY_HEALTHY_DELAY, 2000); + return checkConfigBean.getHealthyDelayMilli(); } @Override public long getHealthMarkCompensateIntervalMill() { - return getLongProperty(KEY_HEALTHY_MARK_COMPENSATE_INTERVAL_MILLI, 2 * 60 * 1000L); + return checkConfigBean.getHealthMarkCompensateIntervalMill(); } @Override public int getHealthMarkCompensateThreads() { - return getIntProperty(KEY_HEALTHY_MARK_COMPENSATE_THREADS, 20); + return checkConfigBean.getHealthMarkCompensateThreads(); } @Override public int getHealthyDelayMilliThroughProxy() { - return getIntProperty(KEY_HEALTHY_DELAY_THROUGH_PROXY, 30 * 1000); + return checkConfigBean.getHealthyDelayMilliThroughProxy(); } @Override public int getInstanceLongDelayMilli() { - return getIntProperty(KEY_INSTANCE_LONG_DELAY_MILLI, 3 * 60 * 1000); + return checkConfigBean.getInstanceLongDelayMilli(); } @Override public int getDownAfterCheckNums() { - return getIntProperty(KEY_DOWN_AFTER_CHECK_NUMS, 5); + return checkConfigBean.getDownAfterCheckNums(); } @Override public int getDownAfterCheckNumsThroughProxy() { - return getIntProperty(KEY_DOWN_AFTER_CHECK_NUMS_THROUGH_PROXY, 10); + return checkConfigBean.getDownAfterCheckNumsThroughProxy(); } @Override public int getCacheRefreshInterval() { - return getIntProperty(KEY_CACHE_REFERSH_INTERVAL, 1000); + return consoleConfigBean.getCacheRefreshInterval(); } @Override public Set getAlertWhileList() { - String whitelist = getProperty(KEY_ALERT_WHITE_LIST, ""); - - return getSplitStringSet(whitelist); - + return commonConfigBean.getAlertWhileList(); } @Override public int getQuorum() { - return getIntProperty(KEY_QUORUM, 1); + return checkConfigBean.getQuorum(); } @Override public int getRedisConfCheckIntervalMilli() { - - return getIntProperty( - KEY_REDIS_CONF_CHECK_INTERVAL, - Integer.parseInt(System.getProperty(KEY_REDIS_CONF_CHECK_INTERVAL, "300000")) - ); + return checkConfigBean.getRedisConfCheckIntervalMilli(); } @Override public int getSentinelCheckIntervalMilli() { - return getIntProperty(KEY_SENTINEL_CHECK_INTERVAL, 300000); + return checkConfigBean.getSentinelCheckIntervalMilli(); } @Override public String getConsoleDomain() { - return getProperty(KEY_DOMAIN, "127.0.0.1"); + return commonConfigBean.getConsoleDomain(); + } + + @Override + public String getClusterExcludedRegex() { + return commonConfigBean.getClusterExcludedRegex(); } @Override public QuorumConfig getDefaultSentinelQuorumConfig() { + return checkConfigBean.getDefaultSentinelQuorumConfig(); + } - String config = getProperty(KEY_SENTINEL_QUORUM, "{}"); - return JsonCodec.INSTANCE.decode(config, QuorumConfig.class); + @Override + public int getStableLossAfterRounds() { + return checkConfigBean.getStableLossAfterRounds(); } @Override - public int getConfigDefaultRestoreHours() { - return getIntProperty(KEY_CONFIG_DEFAULT_RESTORE_HOUR, 10); + public int getStableRecoverAfterRounds() { + return checkConfigBean.getStableRecoverAfterRounds(); } @Override - public int getRebalanceSentinelInterval() { - return getIntProperty(KEY_REBALANCE_SENTINEL_INTERVAL, 120); + public int getStableResetAfterRounds() { + return checkConfigBean.getStableResetAfterRounds(); } @Override - public int getRebalanceSentinelMaxNumOnce() { - return getIntProperty(KEY_REBALANCE_SENTINEL_MAX_NUM_ONCE, 15); + public float getSiteStableThreshold() { + return checkConfigBean.getSiteStableThreshold(); } @Override - public int getNoAlarmMinutesForClusterUpdate() { - return getIntProperty(KEY_NO_ALARM_MUNITE_FOR_CLUSTER_UPDATE, 15); + public float getSiteUnstableThreshold() { + return checkConfigBean.getSiteUnstableThreshold(); } @Override - public int getHealthCheckSuspendMinutes() { - return getIntProperty(KEY_NO_HEALTH_CHECK_MINUTES, 40); + public Boolean getSiteStable() { + return checkConfigBean.getSiteStable(); } @Override - public Set getIgnoredHealthCheckDc() { - return getSplitStringSet(getProperty(KEY_IGNORED_DC_FOR_HEALTH_CHECK, "")); + public String getReplDisklessMinRedisVersion() { + return checkConfigBean.getReplDisklessMinRedisVersion(); } @Override - public int getPingDownAfterMilli() { - return getIntProperty(KEY_PING_DOWN_AFTER_MILLI, 12 * 1000); + public String getXRedisMinimumRequestVersion() { + return checkConfigBean.getXRedisMinimumRequestVersion(); } @Override - public int getPingDownAfterMilliThroughProxy() { - return getIntProperty(KEY_PING_DOWN_AFTER_MILLI_THROUGH_PROXY, 30 * 1000); + public String getXpipeRuntimeEnvironment() { + return commonConfigBean.getXpipeRuntimeEnvironment(); } @Override - public void onChange(String key, String oldValue, String newValue) { - super.onChange(key, oldValue, newValue); - if(!listeners.containsKey(key)) { - return; - } - for(ConfigChangeListener listener : listeners.get(key)) { - listener.onChange(key, oldValue, newValue); - } + public String getDBAEmails() { + return commonConfigBean.getDBAEmails(); } @Override - public void register(List keys, ConfigChangeListener configListener) { - for(String key : keys) { - listeners.putIfAbsent(key, new LinkedList<>()); - listeners.get(key).add(configListener); - } + public String getRedisAlertSenderEmail() { + return commonConfigBean.getRedisAlertSenderEmail(); } @Override - public Pair getClusterShardForMigrationSysCheck() { - String clusterShard = getProperty(KEY_CLUSTER_SHARD_FOR_MIGRATE_SYS_CHECK, "cluster1, shard1"); - String[] strs = StringUtil.splitRemoveEmpty("\\s*,\\s*", clusterShard); - return Pair.from(strs[0], strs[1]); + public String getXPipeAdminEmails() { + return commonConfigBean.getXPipeAdminEmails(); } @Override - public int getProxyInfoCollectInterval() { - return getIntProperty(KEY_PROXY_INFO_CHECK_INTERVAL, 30 * 1000); + public int getAlertSystemSuspendMinute() { + return commonConfigBean.getAlertSystemSuspendMinute(); } @Override - public int getOutterClientCheckInterval() { - return getIntProperty(KEY_OUTTER_CLIENT_CHECK_INTERVAL, 120 * 1000); + public int getAlertSystemRecoverMinute() { + return commonConfigBean.getAlertSystemRecoverMinute(); } @Override - public int getOuterClientSyncInterval() { - return getIntProperty(KEY_OUTER_CLIENT_SYNC_INTERVAL, 10 * 1000); + public int getConfigDefaultRestoreHours() { + return consoleConfigBean.getConfigDefaultRestoreHours(); } @Override - public String getOuterClientToken() { - return getProperty(KEY_OUTER_CLIENT_TOKEN, ""); + public int getRebalanceSentinelInterval() { + return consoleConfigBean.getRebalanceSentinelInterval(); } @Override - public Map getConsoleDomains() { - String property = getProperty(KEY_CONSOLE_DOMAINS, "{}"); - return JsonCodec.INSTANCE.decode(property, Map.class); + public int getRebalanceSentinelMaxNumOnce() { + return consoleConfigBean.getRebalanceSentinelMaxNumOnce(); } @Override - public boolean isSentinelRateLimitOpen() { - return getBooleanProperty(KEY_SENTINEL_RATE_LIMIT_OPEN, false); + public int getNoAlarmMinutesForClusterUpdate() { + return commonConfigBean.getNoAlarmMinutesForClusterUpdate(); } @Override - public int getSentinelRateLimitSize() { - return getIntProperty(KEY_SENTINEL_RATE_LIMIT_SIZE, 3); + public int getHealthCheckSuspendMinutes() { + return consoleConfigBean.getHealthCheckSuspendMinutes(); } @Override - public Set getVariablesCheckDataSources() { - String dataSources = getProperty(KEY_VARIABLES_CHECK_DATASOURCE, ""); + public Set getIgnoredHealthCheckDc() { + return checkConfigBean.getIgnoredHealthCheckDc(); + } - return getSplitStringSet(dataSources); + @Override + public int getClustersPartIndex() { + return checkConfigBean.getClustersPartIndex(); } @Override - public Set getOwnClusterType() { - String clusterTypes = getProperty(KEY_OWN_CLUSTER_TYPES, ClusterType.ONE_WAY.toString()); + public int getCheckerReportIntervalMilli() { + return checkConfigBean.getCheckerReportIntervalMilli(); + } - return getSplitStringSet(clusterTypes); + @Override + public int getCheckerMetaRefreshIntervalMilli() { + return checkConfigBean.getCheckerMetaRefreshIntervalMilli(); } @Override - public Set shouldNotifyClusterTypes() { - String clusterTypes = getProperty(KEY_NOTIFY_CLUSTER_TYPES, ClusterType.ONE_WAY.toString()+","+ClusterType.BI_DIRECTION.toString()); + public String getConsoleAddress() { + return checkConfigBean.getConsoleAddress(); + } - return getSplitStringSet(clusterTypes); + @Override + public int getCheckerAckIntervalMilli() { + return checkConfigBean.getCheckerAckIntervalMilli(); } @Override - public Set getOuterClusterTypes() { - String clusterTypes = getProperty(KEY_OUTER_CLUSTER_TYPES, ""); + public long getConfigCacheTimeoutMilli() { + return checkConfigBean.getConfigCacheTimeoutMilli(); + } - return getSplitStringSet(clusterTypes); + @Override + public int getProxyCheckUpRetryTimes() { + return checkConfigBean.getProxyCheckUpRetryTimes(); } - boolean shouldSentinelCheckOuterClientClusters() { - return getBooleanProperty(KEY_SHOULD_SENTINEL_CHECK_OUTER_TYPES, false); + @Override + public int getProxyCheckDownRetryTimes() { + return checkConfigBean.getProxyCheckDownRetryTimes(); } - Set sentinelCheckOuterClientClusters() { - return getSplitStringSet(getProperty(KEY_SENTINEL_CHECK_OUTER_CLUSTERS, "").toLowerCase()); + private Set sentinelCheckOuterClientClusters() { + return checkConfigBean.sentinelCheckOuterClientClusters(); } - // check if outer client clusters support sentinel health check or not @Override public boolean supportSentinelHealthCheck(ClusterType clusterType, String clusterName) { - return clusterType.supportHealthCheck() || shouldSentinelCheckOuterClientClusters() || sentinelCheckOuterClientClusters().contains(clusterName.toLowerCase()); + return clusterType.supportHealthCheck() + || checkConfigBean.shouldSentinelCheckOuterClientClusters() + || sentinelCheckOuterClientClusters().contains(clusterName.toLowerCase()); } @Override - public String sentinelCheckDowngradeStrategy() { - return getProperty(KEY_SENTINEL_CHECK_DOWNGRADE_STRATEGY, "lessThanHalf"); + public void register(List keys, ConfigChangeListener configListener) { + for(String key : keys) { + listeners.putIfAbsent(key, new LinkedList<>()); + listeners.get(key).add(configListener); + } } @Override - public String getCrossDcLeaderLeaseName() { - return getProperty(KEY_CROSS_DC_LEADER_LEASE_NAME, "CROSS_DC_LEADER"); + public String sentinelCheckDowngradeStrategy() { + return checkConfigBean.sentinelCheckDowngradeStrategy(); } @Override - public int getStableLossAfterRounds() { - return getIntProperty(KEY_CHECKER_STABLE_LOSS_AFTER_ROUNDS, 10); + public String crossDcSentinelMonitorNameSuffix() { + return checkConfigBean.crossDcSentinelMonitorNameSuffix(); } @Override - public int getStableRecoverAfterRounds() { - return getIntProperty(KEY_CHECKER_STABLE_RECOVER_AFTER_ROUNDS, 30 * 30); + public int getNonCoreCheckIntervalMilli() { + return checkConfigBean.getNonCoreCheckIntervalMilli(); } @Override - public int getStableResetAfterRounds() { - return getIntProperty(KEY_CHECKER_STABLE_RESET_AFTER_ROUNDS, 30); + public Set getOuterClusterTypes() { + return consoleConfigBean.getOuterClusterTypes(); } @Override - public float getSiteStableThreshold() { - return getFloatProperty(KEY_CHECKER_STABLE_THRESHOLD, 0.8f); + public Map sentinelMasterConfig() { + return checkConfigBean.sentinelMasterConfig(); } @Override - public Boolean getSiteStable() { - return getBooleanProperty(KEY_CHECKER_SITE_STABLE, null); + public long subscribeTimeoutMilli() { + return checkConfigBean.subscribeTimeoutMilli(); } @Override - public float getSiteUnstableThreshold() { - return getFloatProperty(KEY_CHECKER_UNSTABLE_THRESHOLD, 0.8f); + public String getDcsRelations() { + return commonConfigBean.getDcsRelations(); } @Override - public List getBeaconOrgRoutes() { - String property = getProperty(KEY_BEACON_ORG_ROUTE, "[]"); - return JsonCodec.INSTANCE.decode(property, new GenericTypeReference>() {}); + public int maxRemovedDcsCnt() { + return checkConfigBean.maxRemovedDcsCnt(); } @Override - public int getClusterDividedParts() { - return getIntProperty(KEY_CLUSTER_DIVIDED_PARTS, 1); + public int maxRemovedClustersPercent() { + return checkConfigBean.maxRemovedClustersPercent(); } @Override - public int getClustersPartIndex() { - return getIntProperty(KEY_CLUSTERS_PART_INDEX, 0); + public int getKeeperCheckerIntervalMilli() { + return checkConfigBean.getKeeperCheckerIntervalMilli(); } @Override - public int getCheckerAckIntervalMilli() { - return getIntProperty(KEY_CHECKER_ACK_INTERVAL, 10000); + public int getPingDownAfterMilli() { + return checkConfigBean.getPingDownAfterMilli(); } @Override - public int getCheckerAckTimeoutMilli() { - return getIntProperty(KEY_CHECKER_ACK_TIMEOUT_MILLI, 60000); + public int getPingDownAfterMilliThroughProxy() { + return checkConfigBean.getPingDownAfterMilliThroughProxy(); } @Override - public int getCheckerReportIntervalMilli() { - return getIntProperty(KEY_CHECKER_REPORT_INTERVAL, 10000); + public Pair getClusterShardForMigrationSysCheck() { + return consoleConfigBean.getClusterShardForMigrationSysCheck(); } @Override - public int getCheckerCurrentDcAllMetaRefreshIntervalMilli() { - return getIntProperty(KEY_CHECKER_CURRENT_DC_ALL_META_REFRESH_INTERVAL, 60 * 1000); + public int getProxyInfoCollectInterval() { + return consoleConfigBean.getProxyInfoCollectInterval(); } @Override - public int getCheckerMetaRefreshIntervalMilli() { - return getIntProperty(KEY_CHECKER_META_REFRESH_INTERVAL, 30000); + public int getOutterClientCheckInterval() { + return checkConfigBean.getOutterClientCheckInterval(); } @Override - public String getConsoleAddress() { - return getProperty(KEY_CONSOLE_ADDRESS, "http://localhost:8080"); + public int getOuterClientSyncInterval() { + return consoleConfigBean.getOuterClientSyncInterval(); } @Override - public long getConfigCacheTimeoutMilli() { - return getLongProperty(KEY_CONFIG_CACHE_TIMEOUT_MILLI, 1000L); + public String getOuterClientToken() { + return commonConfigBean.getOuterClientToken(); } @Override - public int getProxyCheckUpRetryTimes() { - return getIntProperty(KEY_PROXY_CHECK_UP_RETRY_TIMES, 10); + public Map getConsoleDomains() { + return dataCenterConfigBean.getConsoleDomains(); } @Override - public int getProxyCheckDownRetryTimes() { - return getIntProperty(KEY_PROXY_CHECK_DOWN_RETRY_TIMES, 1); - } - - @Override - public long getMigrationTimeoutMilli() { - return getLongProperty(KEY_MIGRATION_TIMEOUT_MILLI, 15000L); + public boolean isSentinelRateLimitOpen() { + return checkConfigBean.isSentinelRateLimitOpen(); } @Override - public long getServletMethodTimeoutMilli() { - return getLongProperty(KEY_SERVLET_METHOD_TIMEOUT_MILLI, 10000L); + public int getSentinelRateLimitSize() { + return checkConfigBean.getSentinelRateLimitSize(); } - public boolean isRedisConfigCheckMonitorOpen() { - return getBooleanProperty(KEY_REDIS_CONFIG_CHECK_MONITOR_OPEN, false); + @Override + public Set getVariablesCheckDataSources() { + return consoleConfigBean.getVariablesCheckDataSources(); } @Override - public String getRedisConfigCheckRules() { - return getProperty(KEY_REDIS_CONFIG_CHECK_RULES); + public Set getOwnClusterType() { + return consoleConfigBean.getOwnClusterType(); } - public String crossDcSentinelMonitorNameSuffix() { - return getProperty(KEY_CROSS_DC_SENTINEL_MONITOR_NAME_SUFFIX, "CROSS_DC"); + @Override + public Set shouldNotifyClusterTypes() { + return commonConfigBean.shouldNotifyClusterTypes(); } @Override - public int getNonCoreCheckIntervalMilli() { - return getIntProperty(KEY_NON_CORE_CHECK_INTERVAL, 3 * 60 * 60 * 1000); + public String getCrossDcLeaderLeaseName() { + return dataCenterConfigBean.getCrossDcLeaderLeaseName(); } @Override - public Map sentinelMasterConfig() { - String property = getProperty(KEY_SENTINEL_MASTER_CONFIG, "{}"); - return JsonCodec.INSTANCE.decode(property, Map.class); + public List getBeaconOrgRoutes() { + String property = dataCenterConfigBean.getBeaconOrgRoutes(); + return JsonCodec.INSTANCE.decode(property, new GenericTypeReference>() {}); } @Override - public long subscribeTimeoutMilli() { - return getLongProperty(KEY_SUBSCRIBE_TIMEOUT_MILLI, 5000L); + public int getClusterDividedParts() { + List groupList = checkConfigBean.getClustersList(); + return groupList.size(); } @Override - public String getDcsRelations() { - return getProperty(KEY_DCS_RELATIONS, "{}"); + public int getCheckerAckTimeoutMilli() { + return dataCenterConfigBean.getCheckerAckTimeoutMilli(); } @Override - public String getChooseRouteStrategyType() { return getProperty(KEY_ROUTE_CHOOSE_STRATEGY_TYPE, defaultRouteChooseStrategyType);} + public long getMigrationTimeoutMilli() { + return consoleConfigBean.getMigrationTimeoutMilli(); + } @Override - public String getClusterExcludedRegex() { - return getProperty(KEY_ALERT_CLUSTER_EXCLUDED_REGEX, ""); + public long getServletMethodTimeoutMilli() { + return consoleConfigBean.getServletMethodTimeoutMilli(); } @Override - public int maxRemovedDcsCnt() { - return getIntProperty(KEY_MAX_REMOVED_DCS_CNT, 1); + public boolean isRedisConfigCheckMonitorOpen() { + return checkConfigBean.isRedisConfigCheckMonitorOpen(); } @Override - public int maxRemovedClustersPercent() { - return getIntProperty(KEY_MAX_REMOVED_CLUSTERS_PERCENT, 50); + public String getRedisConfigCheckRules() { + return checkConfigBean.getRedisConfigCheckRules(); } @Override - public int getKeeperCheckerIntervalMilli() { - return getIntProperty(KEY_KEEPER_CHECKER_INTERVAL, 60 * 1000); + public String getChooseRouteStrategyType() { + return commonConfigBean.getChooseRouteStrategyType(); } @Override public boolean isAutoMigrateOverloadKeeperContainerOpen() { - return getBooleanProperty(KEY_CONSOLE_AUTO_MIGRATE_OVERLOAD_KEEPER_CONTAINER_OPEN, false); + return consoleConfigBean.isAutoMigrateOverloadKeeperContainerOpen(); } @Override public long getAutoMigrateOverloadKeeperContainerIntervalMilli() { - return getLongProperty(KEY_CONSOLE_AUTO_MIGRATE_OVERLOAD_KEEPER_CONTAINER_INTERVAL_MILLI, 60 * 60 * 1000L); + return consoleConfigBean.getAutoMigrateOverloadKeeperContainerIntervalMilli(); } @Override public double getKeeperPairOverLoadFactor() { - return getFloatProperty(KEY_CONSOLE_KEEPER_PAIR_OVERLOAD_FACTOR, 0.25F); + return consoleConfigBean.getKeeperPairOverLoadFactor(); } @Override public double getKeeperContainerDiskOverLoadFactor() { - return getFloatProperty(KEY_CONSOLE_KEEPER_CONTAINER_DISK_OVERLOAD_FACTOR, 0.8F); + return consoleConfigBean.getKeeperContainerDiskOverLoadFactor(); } @Override public double getKeeperContainerIoRate() { - return getFloatProperty(KEY_CONSOLE_KEEPER_CONTAINER_IO_RATE, 500F); + return consoleConfigBean.getKeeperContainerIoRate(); } @Override public long getMetaServerSlotClusterMapCacheTimeOutMilli() { - return getLongProperty(KEY_CONSOLE_META_SLOT_CACHE_MILLI, 30 * 1000L); + return consoleConfigBean.getMetaServerSlotClusterMapCacheTimeOutMilli(); } @Override public boolean autoSetKeeperSyncLimit() { - return getBooleanProperty(KEY_KEEPERCONTAINER_SYNC_LIMIT_ON, false); + return consoleConfigBean.autoSetKeeperSyncLimit(); } @Override public boolean disableDb() { - return false; + return commonConfigBean.disableDb(); } @Override public Set getExtraSyncDC() { - return Collections.emptySet(); + return commonConfigBean.getExtraSyncDC(); + } + + @Override + public String getConsoleNoDbDomain() { + return dataCenterConfigBean.getConsoleNoDbDomain(); + } + + @Override + public void addListener(ConfigKeyListener listener) { + this.listenersSet.add(listener); } + @Override + public String getZkConnectionString() { + return dataCenterConfigBean.getZkConnectionString(); + } + + @Override + public String getZkNameSpace() { + return dataCenterConfigBean.getZkNameSpace(); + } + + public void onChange(String key, String oldValue, String newValue) { + + for(ConfigKeyListener listener : listenersSet) { + listener.onChange(key, newValue); + } + + if(!listeners.containsKey(key)) { + return; + } + for(ConfigChangeListener listener : listeners.get(key)) { + listener.onChange(key, oldValue, newValue); + } + } + + protected String getProperty(String key) { + for(AbstractConfigBean configBean : configBeans) { + String val = configBean.getProperty(key); + if(val != null) { + return val; + } + } + return null; + } + + protected String getProperty(String key, String def) { + String value = this.getProperty(key); + if(value == null) { + return def; + } else { + return value; + } + } } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/checker/ConsoleCheckerController.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/checker/ConsoleCheckerController.java index d40ba7808..0d377b488 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/checker/ConsoleCheckerController.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/controller/api/checker/ConsoleCheckerController.java @@ -19,6 +19,7 @@ import com.ctrip.xpipe.redis.checker.spring.ConsoleServerMode; import com.ctrip.xpipe.redis.checker.spring.ConsoleServerModeCondition; import com.ctrip.xpipe.redis.console.checker.CheckerManager; +import com.ctrip.xpipe.redis.console.config.ConsoleConfig; import com.ctrip.xpipe.redis.console.controller.AbstractConsoleController; import com.ctrip.xpipe.redis.console.healthcheck.nonredis.cluster.ClusterHealthMonitorManager; import com.ctrip.xpipe.redis.console.keeper.KeeperContainerUsedInfoAnalyzer; @@ -29,15 +30,18 @@ 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.AbstractSpringConfigContext; import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import org.springframework.web.context.request.async.DeferredResult; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.*; +import java.util.concurrent.ExecutorService; /** * @author lishanglin @@ -47,6 +51,9 @@ @ConsoleServerMode(ConsoleServerModeCondition.SERVER_MODE.CONSOLE) public class ConsoleCheckerController extends AbstractConsoleController { + @Resource(name = AbstractSpringConfigContext.GLOBAL_EXECUTOR) + private ExecutorService executors; + @Autowired private MetaCache metaCache; @@ -81,6 +88,8 @@ public class ConsoleCheckerController extends AbstractConsoleController { private CheckerDbConfig checkerDbConfig; private Logger logger = LoggerFactory.getLogger(ConsoleCheckerController.class); + @Autowired + private ConsoleConfig consoleConfig; @GetMapping(ConsoleCheckerPath.PATH_GET_META) public String getDividedMeta(@PathVariable int index, @RequestParam(value="format", required = false) String format) { @@ -96,10 +105,23 @@ public String getDividedMeta(@RequestParam(value="format", required = false) Str } @GetMapping(ConsoleCheckerPath.PATH_GET_ALL_META_LONG_PULL) - public String getDividedMetaLongPull(@RequestParam(value="format", required = false) String format, - @RequestParam(value="updateTime") long updateTime) throws InterruptedException { - XpipeMeta xpipeMeta = metaCache.getXpipeMetaLongPull(updateTime); - return (format != null && format.equals("xml"))? xpipeMeta.toString() : coder.encode(xpipeMeta); + public DeferredResult getDividedMetaLongPull(@RequestParam(value="format", required = false) String format, + @RequestParam(value="updateTime") long updateTime) { + DeferredResult response = new DeferredResult<>(Long.valueOf(consoleConfig.getServletMethodTimeoutMilli())); + + executors.execute(new Runnable() { + @Override + public void run() { + try { + XpipeMeta xpipeMeta = metaCache.getXpipeMetaLongPull(updateTime); + response.setResult((format != null && format.equals("xml"))? xpipeMeta.toString() : coder.encode(xpipeMeta)); + } catch (InterruptedException e) { + response.setErrorResult(e.getMessage()); + } + } + }); + + return response; } @GetMapping(ConsoleCheckerPath.PATH_GET_DC_ALL_META) diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/ds/MybatisDataSourceConfig.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/ds/MybatisDataSourceConfig.java index c763319e8..4f30ef053 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/ds/MybatisDataSourceConfig.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/ds/MybatisDataSourceConfig.java @@ -13,6 +13,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.unidal.dal.jdbc.DalException; import org.unidal.dal.jdbc.datasource.DataSource; import org.unidal.dal.jdbc.datasource.DataSourceManager; import org.unidal.lookup.ContainerLoader; @@ -34,9 +35,13 @@ public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() throws Except @Bean public javax.sql.DataSource dataSource() throws Exception { - // 强制查询使Xpipe DataSource初始化 - ConfigTblDao configTblDao = ContainerLoader.getDefaultContainer().lookup(ConfigTblDao.class); - configTblDao.findByPK(1L, ConfigTblEntity.READSET_FULL); + try { + // 强制查询使Xpipe DataSource初始化 + ConfigTblDao configTblDao = ContainerLoader.getDefaultContainer().lookup(ConfigTblDao.class); + configTblDao.findByPK(1L, ConfigTblEntity.READSET_FULL); + } catch (ComponentLookupException | DalException e) { + logger.error("[MybatisDataSourceConfig]", e); + } XPipeDataSource dataSource = tryGetXpipeDataSource(); if (dataSource == null) { logger.info("[mybatisSqlSessionFactoryBean] no xpipe datasource found"); 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 ac76eb390..d5397b68d 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 @@ -89,7 +89,6 @@ protected void refreshMeta(XpipeMeta xpipeMeta) { monitor2ClusterShard = Maps.newHashMap(); allKeeperSize = allKeepers == null ? DEFAULT_KEEPER_NUMBERS : allKeepers.size(); allKeepers = null; - lastUpdateTime = System.currentTimeMillis(); } protected XpipeMeta createXpipeMeta(List dcMetas, List redisCheckRuleMetas){ @@ -105,6 +104,8 @@ protected XpipeMeta createXpipeMeta(List dcMetas, List getAllAzGroups() { - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/azGroup/all").build(); ResponseEntity> resp = exchange(comp.toUri(), HttpMethod.GET, null, new ParameterizedTypeReference>(){}, "getAllAzGroups"); @@ -52,7 +53,7 @@ public List getAllAzGroups() { public List getAllProxy() { - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + "/api/proxies/all").build(); + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + "/api/proxies/all").build(); ResponseEntity> resp = exchange(comp.toUri(), HttpMethod.GET, null, new ParameterizedTypeReference>(){}, "getAllProxy"); @@ -61,7 +62,7 @@ public List getAllProxy() { public List getMonitorActiveProxiesByDc() { String dc = foundationService.getDataCenter(); - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + "/api/proxies/monitor_active") + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + "/api/proxies/monitor_active") .queryParam("dc", dc) .build(); @@ -72,7 +73,7 @@ public List getMonitorActiveProxiesByDc() { } public XpipeMeta getXpipeAllMeta(long updateTime) throws SAXException, IOException { - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + ConsoleCheckerPath.PATH_GET_ALL_META_LONG_PULL) + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + ConsoleCheckerPath.PATH_GET_ALL_META_LONG_PULL) .queryParam("format", "xml") .queryParam("updateTime", updateTime) .build(); @@ -84,7 +85,7 @@ public XpipeMeta getXpipeAllMeta(long updateTime) throws SAXException, IOExcepti public List getAllKeeperContainers() { - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/keeper_container/all").build(); ResponseEntity> resp = exchange(comp.toUri(), HttpMethod.GET, null, @@ -99,7 +100,7 @@ public boolean setConfig(ConfigModel model, Date util) { params.add("util", util); } HttpEntity> requestEntity = new HttpEntity<>(params); - ResponseEntity resp = exchange(config.getConsoleDomain() + + ResponseEntity resp = exchange(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/config/set", HttpMethod.POST, requestEntity, RetMessage.class, "setConfig"); RetMessage retMessage = resp.getBody(); return retMessage.getState() == 0; @@ -107,7 +108,7 @@ public boolean setConfig(ConfigModel model, Date util) { public ConfigTbl getConfig(String key, String subId) { - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/config") .queryParam("key", key) .queryParam("subId", subId). @@ -120,7 +121,7 @@ public ConfigTbl getConfig(String key, String subId) { public List getAllConfigs(String key) { - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/config/getAll/{key}").buildAndExpand(key); ResponseEntity> resp = exchange(comp.toUri(), @@ -129,7 +130,7 @@ public List getAllConfigs(String key) { } public List findAllByKeyAndValueAndUntilAfter(String key, String value, Date until) { - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/config/findAll").build(); MultiValueMap params = new LinkedMultiValueMap<>(); params.add("key", key); @@ -143,7 +144,7 @@ public List findAllByKeyAndValueAndUntilAfter(String key, String valu public List getAllOrganizations() { - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/organizations/all").build(); ResponseEntity> resp = exchange(comp.toUri(), HttpMethod.GET, null, @@ -153,7 +154,7 @@ public List getAllOrganizations() { public List getAllDcs() { - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/dc_tbls").build(); ResponseEntity> resp = restTemplate.exchange(comp.toUri(), HttpMethod.GET, null, @@ -162,7 +163,7 @@ public List getAllDcs() { } public List findClusterRelatedDc(String clusterId) { - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/clusters/" + clusterId + "/dcs").build(); ResponseEntity> resp = exchange(comp.toUri(), HttpMethod.GET, null, new ParameterizedTypeReference>(){}, "findClusterRelatedDc"); @@ -171,7 +172,7 @@ public List findClusterRelatedDc(String clusterId) { public List findAllClusters() { - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/clusters/all").build(); ResponseEntity> resp = exchange(comp.toUri(), @@ -182,7 +183,7 @@ public List findAllClusters() { public List findAllTunnelInfo() { - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + ConsoleCheckerPath.PATH_GET_PROXY_CHAINS).build(); ResponseEntity> resp = exchange(comp.toUri(), @@ -192,7 +193,7 @@ public List findAllTunnelInfo() { } public List findAllByDcClusterShard(long dcClusterShardId) { - UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleDomain() + + UriComponents comp = UriComponentsBuilder.fromHttpUrl(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/redis/{dcClusterShardId}").buildAndExpand(dcClusterShardId); ResponseEntity> resp = exchange(comp.toUri(), HttpMethod.GET, null, new ParameterizedTypeReference>(){}, "findAllByDcClusterShard"); @@ -201,7 +202,7 @@ public List findAllByDcClusterShard(long dcClusterShardId) { public void updateBatchKeeperActive(List redises) { HttpEntity> requestEntity = new HttpEntity<>(redises); - ResponseEntity resp = exchange(config.getConsoleDomain() + + ResponseEntity resp = exchange(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/redis/updateBatchKeeperActive", HttpMethod.POST, requestEntity, RetMessage.class, "updateBatchKeeperActive" ); @@ -225,7 +226,7 @@ public void insertRedises(String dcId, String clusterId, String shardId, List requestEntity = new HttpEntity<>(redisCreateInfo, headers); - ResponseEntity resp = exchange(config.getConsoleDomain() + + ResponseEntity resp = exchange(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/redis/insert", HttpMethod.POST, requestEntity, RetMessage.class, "insertRedises"); RetMessage retMessage = resp.getBody(); @@ -237,7 +238,7 @@ public void insertRedises(String dcId, String clusterId, String shardId, List requestEntity = new HttpEntity<>(dcClusterTbl); - ResponseEntity resp = exchange(config.getConsoleDomain() + + ResponseEntity resp = exchange(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/dc/bind", HttpMethod.POST, requestEntity, RetMessage.class, "bindDc" ); @@ -248,7 +249,7 @@ public void bindDc(DcClusterTbl dcClusterTbl) { } public List getActiveRoutes() { - ResponseEntity resp = exchange(config.getConsoleDomain() + + ResponseEntity resp = exchange(config.getConsoleNoDbDomain() + AbstractConsoleController.API_PREFIX + "/routes/active", HttpMethod.GET, null, String.class, "getActiveRoutes" ); @@ -257,9 +258,25 @@ public List getActiveRoutes() { return pretty.decode(content, List.class); } + public void updateKeeperStatus(String dcId, String clusterId, String shardId, KeeperMeta newActiveKeeper) { + try { + catTransactionMonitor.logTransaction("ConsoleForwardAPI", "updateKeeperStatus", new Callable() { + @Override + public String call() { + restTemplate.put( config.getConsoleNoDbDomain() + "/api/dc/{dcId}/cluster/{clusterId}/shard/{shardId}/keepers/adjustment", + newActiveKeeper, dcId, clusterId, shardId); + return null; + } + }); + } catch (Exception e) { + logger.error("[updateKeeperStatus]", e); + } + + } + ResponseEntity exchange(String url, HttpMethod var2, HttpEntity httpEntity, Class type, String name) { try { - return catTransactionMonitor.logTransaction("API", name, new Callable>() { + return catTransactionMonitor.logTransaction("ConsoleForwardAPI", name, new Callable>() { @Override public ResponseEntity call() { return restTemplate.exchange(url, var2, httpEntity, type); @@ -273,7 +290,7 @@ public ResponseEntity call() { ResponseEntity exchange(URI uri, HttpMethod var2, HttpEntity httpEntity, ParameterizedTypeReference type, String name) { try { - return catTransactionMonitor.logTransaction("API", name, new Callable>() { + return catTransactionMonitor.logTransaction("ConsoleForwardAPI", name, new Callable>() { @Override public ResponseEntity call() { return restTemplate.exchange(uri, var2, httpEntity, type); 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 9acca6270..df1a3b8d0 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 @@ -58,7 +58,7 @@ public class DefaultMetaCache extends AbstractMetaCache implements MetaCache, Co private KeeperContainerService keeperContainerService; @Autowired - private ConsoleConfig consoleConfig; + protected ConsoleConfig consoleConfig; @Autowired private RouteChooseStrategyFactory routeChooseStrategyFactory; @@ -71,11 +71,11 @@ public class DefaultMetaCache extends AbstractMetaCache implements MetaCache, Co private List> xmlFormatXPipeMetaParts = null; - private ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1); + protected ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1); - private ScheduledFuture future; + protected ScheduledFuture future; - private AtomicBoolean taskTrigger = new AtomicBoolean(false); + protected AtomicBoolean taskTrigger = new AtomicBoolean(false); private final Lock lock = new ReentrantLock(); protected final Condition condition = lock.newCondition(); @@ -99,7 +99,7 @@ public XpipeMeta getXpipeMetaLongPull(long updateTime) throws InterruptedExcepti XpipeMeta xpipeMeta = null; if(lock.tryLock(consoleConfig.getCacheRefreshInterval(), TimeUnit.MILLISECONDS)) { try { - if(lastUpdateTime <= updateTime) { + if(getLastUpdateTime() <= updateTime) { condition.await(consoleConfig.getCacheRefreshInterval(), TimeUnit.MILLISECONDS); } xpipeMeta = meta.getKey(); @@ -125,7 +125,6 @@ private synchronized void stopLoadMeta(){ monitor2ClusterShard = null; allKeepers = null; allKeeperSize = DEFAULT_KEEPER_NUMBERS; - lastUpdateTime = 0; } public void startLoadMeta() { 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 index afdc76d67..873e5c6b1 100644 --- 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 @@ -9,7 +9,6 @@ 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.StringUtil; import com.ctrip.xpipe.utils.XpipeThreadFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -88,52 +87,24 @@ public void run() { }, 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, outerClientService, 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, + private void processNeedAdd(Set needProcess) { + for(String dc : needProcess) { + if(!dcMetaSynchronizers.containsKey(dc)) { + // 还未加入,需要添加 + dcMetaSynchronizers.put(dc, new DcMetaSynchronizer(consoleConfig, metaCache, redisService, shardService, clusterService, dcService, organizationService, sentinelBalanceService, - clusterTypeUpdateEventFactory, outerClientService,currentDc)); - dcMetaSynchronizers.get(currentDc).start(); - } - } else { - if(dcMetaSynchronizers.containsKey(currentDc)) { - dcMetaSynchronizers.get(currentDc).stop(); - dcMetaSynchronizers.remove(currentDc); + clusterTypeUpdateEventFactory, outerClientService, dc)); + dcMetaSynchronizers.get(dc).start(); } } - // 检查 被移除托管的 dc + } + + private void processNeedRemove(Set needProcess) { Set needRemoved = new HashSet<>(); + for(String dc : dcMetaSynchronizers.keySet()) { - if(!dcs.contains(dc) && !StringUtil.trimEquals(dc, currentDc)) { + if(!needProcess.contains(dc)) { + // 不在need process集合里面需要删除 needRemoved.add(dc); } } @@ -144,4 +115,27 @@ public void sync() { } } + private void updateTasks(Set needProcess) { + processNeedAdd(needProcess); + processNeedRemove(needProcess); + } + + + public void sync() { + + Set dcs = consoleConfig.getExtraSyncDC(); + String currentDc = foundationService.getDataCenter(); + Set needProcess = new HashSet<>(); + + if(consoleCrossDcServer.amILeader()) { + needProcess.addAll(dcs); + needProcess.add(currentDc); + } else { + if(consoleLeaderElector.amILeader() && !consoleConfig.disableDb()) { + needProcess.add(currentDc); + } + } + updateTasks(needProcess); + } + } 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 9746702fd..90d687f09 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 @@ -143,7 +143,11 @@ public DcMeta getDcMeta(String dcName, Set allowTypes) throws Exception } ZoneTbl zoneTbl = zoneService.findById(dcTbl.getZoneId()); - DcMeta dcMeta = new DcMeta().setId(dcName).setLastModifiedTime(dcTbl.getDcLastModifiedTime()).setZone(zoneTbl.getZoneName()); + DcMeta dcMeta = new DcMeta(). + setId(dcName). + setLastModifiedTime(dcTbl.getDcLastModifiedTime()). + setZone(zoneTbl.getZoneName()) + .setDcId(dcTbl.getId()); Map dcMetaMap = new HashMap<>(); dcMetaMap.put(dcMeta.getId().toUpperCase(), dcMeta); diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/RedisMetaServiceImpl.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/RedisMetaServiceImpl.java index bc44cfb16..b020391ff 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/RedisMetaServiceImpl.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/RedisMetaServiceImpl.java @@ -1,5 +1,7 @@ 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.constant.XPipeConsoleConstant; import com.ctrip.xpipe.redis.console.dao.RedisDao; import com.ctrip.xpipe.redis.console.model.RedisTbl; @@ -11,6 +13,7 @@ import com.ctrip.xpipe.redis.core.entity.RedisMeta; import com.ctrip.xpipe.redis.core.entity.ShardMeta; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -22,6 +25,8 @@ * Aug 17, 2016 */ @Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(false) public class RedisMetaServiceImpl extends AbstractMetaService implements RedisMetaService { public static long REDIS_MASTER_NULL = 0L; diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/RedisMetaServiceWithoutDB.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/RedisMetaServiceWithoutDB.java new file mode 100644 index 000000000..efcb4f4f3 --- /dev/null +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/service/meta/impl/RedisMetaServiceWithoutDB.java @@ -0,0 +1,23 @@ +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.resources.ConsolePortalService; +import com.ctrip.xpipe.redis.core.entity.KeeperMeta; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Conditional; +import org.springframework.stereotype.Service; + +@Service +@Conditional(ConsoleDisableDbCondition.class) +@DisableDbMode(true) +public class RedisMetaServiceWithoutDB extends RedisMetaServiceImpl { + + @Autowired + private ConsolePortalService consolePortalService; + + @Override + public void updateKeeperStatus(String dcId, String clusterId, String shardId, KeeperMeta newActiveKeeper) { + consolePortalService.updateKeeperStatus(dcId, clusterId, shardId, newActiveKeeper); + } +} 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 daac3caa7..7f655e8ea 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 @@ -19,7 +19,7 @@ import com.ctrip.xpipe.redis.checker.spring.ConsoleServerMode; import com.ctrip.xpipe.redis.checker.spring.ConsoleServerModeCondition; import com.ctrip.xpipe.redis.console.config.ConsoleConfig; -import com.ctrip.xpipe.redis.console.config.impl.CombConsoleConfig; +import com.ctrip.xpipe.redis.console.config.impl.DefaultConsoleConfig; import com.ctrip.xpipe.redis.console.config.impl.DefaultCommonConfig; import com.ctrip.xpipe.redis.console.healthcheck.meta.DcIgnoredConfigChangeListener; import com.ctrip.xpipe.redis.console.migration.auto.DefaultBeaconManager; @@ -105,7 +105,7 @@ public CheckerConfig checkerConfig(CheckConfigBean checkConfigBean, ConsoleConfigBean consoleConfigBean, DataCenterConfigBean dataCenterConfigBean, CommonConfigBean commonConfigBean) { - return new CombConsoleConfig(checkConfigBean, consoleConfigBean, dataCenterConfigBean, commonConfigBean); + return new DefaultConsoleConfig(checkConfigBean, consoleConfigBean, dataCenterConfigBean, commonConfigBean); } @Bean 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 0d3de1fca..e44af4af3 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 @@ -20,7 +20,7 @@ import com.ctrip.xpipe.redis.console.cluster.ConsoleLeaderElector; import com.ctrip.xpipe.redis.console.config.ConsoleConfig; import com.ctrip.xpipe.redis.console.config.ConsoleDbConfig; -import com.ctrip.xpipe.redis.console.config.impl.CombConsoleConfig; +import com.ctrip.xpipe.redis.console.config.impl.DefaultConsoleConfig; import com.ctrip.xpipe.redis.console.config.impl.DefaultConsoleDbConfig; import com.ctrip.xpipe.redis.console.dao.ClusterDao; import com.ctrip.xpipe.redis.console.dao.ConfigDao; @@ -44,7 +44,6 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.*; -import org.springframework.stereotype.Service; /** @@ -91,7 +90,7 @@ public FilterRegistrationBean userAccessFilter(ConsoleConfig consoleConfig) { @Profile(AbstractProfile.PROFILE_NAME_PRODUCTION) public MetaCache metaCache(ConsolePortalService consolePortalService, ConsoleConfig config) { if(config.disableDb()) { - return new ConsoleMetaCacheWithoutDB(consolePortalService); + return new ConsoleMetaCacheWithoutDB(consolePortalService, config); } return new DefaultMetaCache(); } @@ -110,7 +109,7 @@ public MetaCache testMetaCache() { @Bean public ConsoleConfig consoleConfig(FoundationService foundationService) { - return new CombConsoleConfig( + return new DefaultConsoleConfig( new CheckConfigBean(foundationService), new ConsoleConfigBean(foundationService), new DataCenterConfigBean(), diff --git a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/AbstractConsoleTest.java b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/AbstractConsoleTest.java index 611591489..718cf2014 100644 --- a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/AbstractConsoleTest.java +++ b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/AbstractConsoleTest.java @@ -7,7 +7,7 @@ import com.ctrip.xpipe.redis.checker.config.impl.CommonConfigBean; import com.ctrip.xpipe.redis.checker.config.impl.ConsoleConfigBean; import com.ctrip.xpipe.redis.checker.config.impl.DataCenterConfigBean; -import com.ctrip.xpipe.redis.console.config.impl.CombConsoleConfig; +import com.ctrip.xpipe.redis.console.config.impl.DefaultConsoleConfig; import org.junit.BeforeClass; /** @@ -25,7 +25,7 @@ public static void beforeAbstractConsoleTest() { @Override protected CheckerConfig buildCheckerConfig() { - return new CombConsoleConfig(new CheckConfigBean(FoundationService.DEFAULT), + return new DefaultConsoleConfig(new CheckConfigBean(FoundationService.DEFAULT), new ConsoleConfigBean(FoundationService.DEFAULT), new DataCenterConfigBean(), new CommonConfigBean()); diff --git a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/config/impl/DefaultConsoleConfigTest.java b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/config/impl/DefaultConsoleConfigTest.java index 83a5227a6..57fdb9516 100644 --- a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/config/impl/DefaultConsoleConfigTest.java +++ b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/config/impl/DefaultConsoleConfigTest.java @@ -24,11 +24,11 @@ */ public class DefaultConsoleConfigTest extends AbstractConsoleTest { - private CombConsoleConfig consoleConfig; + private DefaultConsoleConfig consoleConfig; @Before public void beforeDefaultConsoleConfigTest() { - consoleConfig = new CombConsoleConfig(new CheckConfigBean(FoundationService.DEFAULT), + consoleConfig = new DefaultConsoleConfig(new CheckConfigBean(FoundationService.DEFAULT), new ConsoleConfigBean(FoundationService.DEFAULT), new DataCenterConfigBean(), new CommonConfigBean()); diff --git a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/controller/consoleportal/migration/ExclusiveThreadsForMigrationTest.java b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/controller/consoleportal/migration/ExclusiveThreadsForMigrationTest.java index afd1f149d..39f75628e 100644 --- a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/controller/consoleportal/migration/ExclusiveThreadsForMigrationTest.java +++ b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/controller/consoleportal/migration/ExclusiveThreadsForMigrationTest.java @@ -10,7 +10,7 @@ import com.ctrip.xpipe.redis.checker.config.impl.DataCenterConfigBean; import com.ctrip.xpipe.redis.console.AbstractConsoleTest; import com.ctrip.xpipe.redis.console.config.ConsoleConfig; -import com.ctrip.xpipe.redis.console.config.impl.CombConsoleConfig; +import com.ctrip.xpipe.redis.console.config.impl.DefaultConsoleConfig; import com.ctrip.xpipe.redis.console.controller.api.migrate.MigrationApi4Beacon; import com.ctrip.xpipe.redis.console.controller.api.migrate.meta.BeaconMigrationRequest; import com.ctrip.xpipe.redis.console.controller.api.migrate.meta.BeaconMigrationResponse; @@ -88,7 +88,7 @@ public CommandFuture biMigrate(BeaconMigrationRequest migrationRequest) { @Bean public ConsoleConfig consoleConfig() { - return new CombConsoleConfig(new CheckConfigBean(FoundationService.DEFAULT), + return new DefaultConsoleConfig(new CheckConfigBean(FoundationService.DEFAULT), new ConsoleConfigBean(FoundationService.DEFAULT), new DataCenterConfigBean(), new CommonConfigBean()); diff --git a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/health/ContextConfig.java b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/health/ContextConfig.java index 0d8b124c0..a8396227e 100644 --- a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/health/ContextConfig.java +++ b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/health/ContextConfig.java @@ -6,7 +6,7 @@ import com.ctrip.xpipe.redis.checker.config.impl.ConsoleConfigBean; import com.ctrip.xpipe.redis.checker.config.impl.DataCenterConfigBean; import com.ctrip.xpipe.redis.console.config.ConsoleConfig; -import com.ctrip.xpipe.redis.console.config.impl.CombConsoleConfig; +import com.ctrip.xpipe.redis.console.config.impl.DefaultConsoleConfig; import com.ctrip.xpipe.redis.console.util.DefaultMetaServerConsoleServiceManagerWrapper; import com.ctrip.xpipe.redis.console.util.MetaServerConsoleServiceManagerWrapper; import com.ctrip.xpipe.redis.core.metaserver.MetaServerConsoleServiceManager; @@ -41,7 +41,7 @@ public MetaServerConsoleServiceManagerWrapper getMetaServerConsoleServiceManager @Bean public ConsoleConfig consoleConfig() { - return new CombConsoleConfig(new CheckConfigBean(FoundationService.DEFAULT), + return new DefaultConsoleConfig(new CheckConfigBean(FoundationService.DEFAULT), new ConsoleConfigBean(FoundationService.DEFAULT), new DataCenterConfigBean(), new CommonConfigBean()); diff --git a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/redisconfig/RedisConfigCheckMonitorTest.java b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/redisconfig/RedisConfigCheckMonitorTest.java index c2d905a63..dcdde5e51 100644 --- a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/redisconfig/RedisConfigCheckMonitorTest.java +++ b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/healthcheck/nonredis/redisconfig/RedisConfigCheckMonitorTest.java @@ -1,7 +1,7 @@ package com.ctrip.xpipe.redis.console.healthcheck.nonredis.redisconfig; import com.ctrip.xpipe.cluster.ClusterType; -import com.ctrip.xpipe.redis.console.config.impl.CombConsoleConfig; +import com.ctrip.xpipe.redis.console.config.impl.DefaultConsoleConfig; import com.ctrip.xpipe.redis.console.controller.api.data.meta.DcClusterCreateInfo; import com.ctrip.xpipe.redis.console.service.impl.DcClusterServiceImpl; import com.ctrip.xpipe.redis.console.service.impl.DcServiceImpl; @@ -39,7 +39,7 @@ public class RedisConfigCheckMonitorTest { DcServiceImpl dcService; @Mock - CombConsoleConfig consoleConfig; + DefaultConsoleConfig consoleConfig; private ClusterType mockClusterType = ClusterType.BI_DIRECTION; diff --git a/redis/redis-core/src/main/java/com/ctrip/xpipe/redis/core/console/ConsoleCheckerPath.java b/redis/redis-core/src/main/java/com/ctrip/xpipe/redis/core/console/ConsoleCheckerPath.java index 16f01e071..67e431b5d 100644 --- a/redis/redis-core/src/main/java/com/ctrip/xpipe/redis/core/console/ConsoleCheckerPath.java +++ b/redis/redis-core/src/main/java/com/ctrip/xpipe/redis/core/console/ConsoleCheckerPath.java @@ -12,7 +12,7 @@ private ConsoleCheckerPath() {} public static final String PATH_GET_ALL_META = "/api/meta/divide"; - public static final String PATH_GET_ALL_META_LONG_PULL = "/api/meta/divide/long"; + public static final String PATH_GET_ALL_META_LONG_PULL = "/api/meta/long_pull"; public static final String PATH_GET_DC_ALL_META = "/api/meta/{dcName}/all"; 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 719411f1f..b8b550a35 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 @@ -4,6 +4,7 @@ + diff --git a/redis/redis-integration-test/src/test/java/com/ctrip/xpipe/redis/integratedtest/console/config/SpringEnvConsoleConfig.java b/redis/redis-integration-test/src/test/java/com/ctrip/xpipe/redis/integratedtest/console/config/SpringEnvConsoleConfig.java index 5bf7d9470..c2015ece7 100644 --- a/redis/redis-integration-test/src/test/java/com/ctrip/xpipe/redis/integratedtest/console/config/SpringEnvConsoleConfig.java +++ b/redis/redis-integration-test/src/test/java/com/ctrip/xpipe/redis/integratedtest/console/config/SpringEnvConsoleConfig.java @@ -5,12 +5,12 @@ import com.ctrip.xpipe.redis.checker.config.impl.CommonConfigBean; import com.ctrip.xpipe.redis.checker.config.impl.ConsoleConfigBean; import com.ctrip.xpipe.redis.checker.config.impl.DataCenterConfigBean; -import com.ctrip.xpipe.redis.console.config.impl.CombConsoleConfig; +import com.ctrip.xpipe.redis.console.config.impl.DefaultConsoleConfig; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; -public class SpringEnvConsoleConfig extends CombConsoleConfig implements ApplicationContextAware { +public class SpringEnvConsoleConfig extends DefaultConsoleConfig implements ApplicationContextAware { public SpringEnvConsoleConfig() { super(new CheckConfigBean(FoundationService.DEFAULT), From 938b6d4e215710376101893596d6d518fd1b50cd Mon Sep 17 00:00:00 2001 From: qifanwang Date: Thu, 19 Sep 2024 17:19:04 +0800 Subject: [PATCH 5/6] add try catch --- .../xpipe/redis/console/resources/MetaSynchronizer.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 index 873e5c6b1..71482498a 100644 --- 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 @@ -82,7 +82,11 @@ public void postConstruct() { scheduledExecutorService.scheduleWithFixedDelay(new Runnable() { @Override public void run() { - sync(); + try { + sync(); + } catch (Throwable e) { + logger.error("[sync]", e); + } } }, consoleConfig.getOuterClientSyncInterval(), consoleConfig.getOuterClientSyncInterval(), TimeUnit.MILLISECONDS); } From 2b9a3d11fa22d5f4a36c70106f0fd1c5959b16e8 Mon Sep 17 00:00:00 2001 From: qifanwang Date: Thu, 19 Sep 2024 18:37:32 +0800 Subject: [PATCH 6/6] add try catch --- .../xpipe/redis/console/resources/DcMetaSynchronizer.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 3a09716c1..f70d87bf7 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 @@ -81,7 +81,11 @@ public void start() { scheduledExecutorService.scheduleWithFixedDelay(new Runnable() { @Override public void run() { - sync(); + try { + sync(); + } catch (Exception e) { + logger.error("[DcMetaSynchronizer]", e); + } } }, consoleConfig.getOuterClientSyncInterval(), consoleConfig.getOuterClientSyncInterval(), TimeUnit.MILLISECONDS); }