Search in sources :

Example 1 with QuorumConfig

use of com.ctrip.xpipe.redis.core.meta.QuorumConfig in project x-pipe by ctripcorp.

the class DefaultSentinelCollector method collect.

@Override
public void collect(SentinelSample sentinelSample) {
    Set<SentinelHello> hellos = sentinelSample.getHellos();
    String clusterId = sentinelSample.getSamplePlan().getClusterId();
    String shardId = sentinelSample.getSamplePlan().getShardId();
    String sentinelMonitorName = metaCache.getSentinelMonitorName(clusterId, shardId);
    Set<HostPort> masterDcSentinels = metaCache.getActiveDcSentinels(clusterId, shardId);
    QuorumConfig quorumConfig = consoleConfig.getDefaultSentinelQuorumConfig();
    HostPort masterAddr = null;
    try {
        masterAddr = metaCache.findMaster(clusterId, shardId);
    } catch (MasterNotFoundException e) {
        logger.error("[collect]" + e.getMessage(), e);
    }
    logger.debug("[collect]{},{},{}", clusterId, shardId, hellos);
    // check delete
    Set<SentinelHello> toDelete = checkAndDelete(sentinelMonitorName, masterDcSentinels, hellos, quorumConfig);
    // checkReset
    checkReset(clusterId, shardId, sentinelMonitorName, hellos);
    // check add
    Set<SentinelHello> toAdd = checkToAdd(clusterId, shardId, sentinelMonitorName, masterDcSentinels, hellos, masterAddr, quorumConfig);
    doAction(toDelete, toAdd, quorumConfig);
}
Also used : QuorumConfig(com.ctrip.xpipe.redis.core.meta.QuorumConfig) MasterNotFoundException(com.ctrip.xpipe.redis.console.resources.MasterNotFoundException) HostPort(com.ctrip.xpipe.endpoint.HostPort)

Example 2 with QuorumConfig

use of com.ctrip.xpipe.redis.core.meta.QuorumConfig in project x-pipe by ctripcorp.

the class DefaultSentinelCollector method doAction.

private void doAction(Set<SentinelHello> toDelete, Set<SentinelHello> toAdd, QuorumConfig quorumConfig) {
    if ((toDelete == null || toDelete.size() == 0) && (toAdd == null || toAdd.size() == 0)) {
        return;
    }
    if (toAdd != null && toAdd.size() > 0) {
        logger.info("[doAction][add]{}", toAdd);
    }
    if (toDelete != null && toDelete.size() > 0) {
        logger.info("[doAction][del]{}", toDelete);
    }
    if (toDelete != null) {
        toDelete.forEach((hello -> {
            HostPort sentinelAddr = hello.getSentinelAddr();
            RedisClient redisConnection = null;
            try {
                CatEventMonitor.DEFAULT.logEvent(SENTINEL_TYPE, "[del]" + hello);
                redisConnection = sessionManager.findRedisConnection(sentinelAddr.getHost(), sentinelAddr.getPort());
                redisConnection.connectSentinel().sync().remove(hello.getMonitorName());
            } catch (Exception e) {
                logger.error("[doAction][delete]" + hello, e);
            } finally {
                if (redisConnection != null) {
                    redisConnection.shutdown();
                }
            }
        }));
    }
    if (toAdd != null) {
        toAdd.forEach((hello) -> {
            HostPort sentinelAddr = hello.getSentinelAddr();
            RedisClient redisConnection = null;
            try {
                // TODO: Re-use connection instead of creating them each time
                redisConnection = sessionManager.findRedisConnection(sentinelAddr.getHost(), sentinelAddr.getPort());
                boolean doAdd = true;
                try {
                    Map<String, String> map = redisConnection.connectSentinel().sync().master(hello.getMonitorName());
                    if (equals(hello.getMonitorName(), hello.getMasterAddr(), map)) {
                        doAdd = false;
                        logger.info("[doAction][already exist]{}, {}", map, hello.getSentinelAddr());
                    } else {
                        redisConnection.connectSentinel().sync().remove(hello.getMonitorName());
                    }
                } catch (Exception e) {
                // ingnore
                }
                if (doAdd) {
                    CatEventMonitor.DEFAULT.logEvent(SENTINEL_TYPE, "[add]" + hello);
                    redisConnection.connectSentinel().sync().monitor(hello.getMonitorName(), hello.getMasterAddr().getHost(), hello.getMasterAddr().getPort(), quorumConfig.getQuorum());
                }
            } catch (Exception e) {
                logger.error("[doAction][add]" + hello, e);
            } finally {
                if (redisConnection != null) {
                    redisConnection.shutdown();
                }
            }
        });
    }
}
Also used : AlertManager(com.ctrip.xpipe.redis.console.alert.AlertManager) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AtomicReference(java.util.concurrent.atomic.AtomicReference) Server(com.ctrip.xpipe.api.server.Server) HashSet(java.util.HashSet) QuorumConfig(com.ctrip.xpipe.redis.core.meta.QuorumConfig) DefaultRedisSessionManager(com.ctrip.xpipe.redis.console.health.DefaultRedisSessionManager) Map(java.util.Map) HostPort(com.ctrip.xpipe.endpoint.HostPort) RedisConnectionException(com.lambdaworks.redis.RedisConnectionException) EventMonitor(com.ctrip.xpipe.api.monitor.EventMonitor) ConsoleConfig(com.ctrip.xpipe.redis.console.config.ConsoleConfig) MasterNotFoundException(com.ctrip.xpipe.redis.console.resources.MasterNotFoundException) StatefulRedisSentinelConnection(com.lambdaworks.redis.sentinel.api.StatefulRedisSentinelConnection) Logger(org.slf4j.Logger) VisibleForTesting(com.ctrip.xpipe.utils.VisibleForTesting) Set(java.util.Set) RedisClient(com.lambdaworks.redis.RedisClient) Sets(com.google.common.collect.Sets) RedisSession(com.ctrip.xpipe.redis.console.health.RedisSession) Pair(com.ctrip.xpipe.tuple.Pair) ALERT_TYPE(com.ctrip.xpipe.redis.console.alert.ALERT_TYPE) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Component(org.springframework.stereotype.Component) List(java.util.List) ObjectUtils(com.ctrip.xpipe.utils.ObjectUtils) StringUtil(com.ctrip.xpipe.utils.StringUtil) CatEventMonitor(com.ctrip.xpipe.monitor.CatEventMonitor) Lazy(org.springframework.context.annotation.Lazy) MetaCache(com.ctrip.xpipe.redis.console.resources.MetaCache) RedisClient(com.lambdaworks.redis.RedisClient) HostPort(com.ctrip.xpipe.endpoint.HostPort) RedisConnectionException(com.lambdaworks.redis.RedisConnectionException) MasterNotFoundException(com.ctrip.xpipe.redis.console.resources.MasterNotFoundException)

Example 3 with QuorumConfig

use of com.ctrip.xpipe.redis.core.meta.QuorumConfig in project x-pipe by ctripcorp.

the class DefaultSentinelCollector method checkToAdd.

protected Set<SentinelHello> checkToAdd(String clusterId, String shardId, String sentinelMonitorName, Set<HostPort> masterDcSentinels, Set<SentinelHello> hellos, HostPort masterAddr, QuorumConfig quorumConfig) {
    if (masterAddr == null) {
        logger.warn("[checkToAdd][no monitor name]{}, {}", clusterId, shardId);
        return Sets.newHashSet();
    }
    if (StringUtil.isEmpty(sentinelMonitorName)) {
        logger.warn("[checkToAdd][no monitor name]{}, {}", clusterId, shardId);
        return Sets.newHashSet();
    }
    if (hellos.size() >= quorumConfig.getTotal()) {
        return Sets.newHashSet();
    }
    if (!checkMasterConsistent(masterAddr, hellos)) {
        logger.info("[collect][master not consistent]{}, {}, {}", sentinelMonitorName, masterAddr, hellos);
        return Sets.newHashSet();
    }
    Set<HostPort> currentSentinels = new HashSet<>();
    hellos.forEach((hello -> currentSentinels.add(hello.getSentinelAddr())));
    Set<SentinelHello> toAdd = new HashSet<>();
    int toAddSize = quorumConfig.getTotal() - hellos.size();
    int i = 0;
    for (HostPort hostPort : masterDcSentinels) {
        if (!currentSentinels.contains(hostPort)) {
            i++;
            if (i > toAddSize) {
                break;
            }
            toAdd.add(new SentinelHello(hostPort, masterAddr, sentinelMonitorName));
        }
    }
    return toAdd;
}
Also used : AlertManager(com.ctrip.xpipe.redis.console.alert.AlertManager) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AtomicReference(java.util.concurrent.atomic.AtomicReference) Server(com.ctrip.xpipe.api.server.Server) HashSet(java.util.HashSet) QuorumConfig(com.ctrip.xpipe.redis.core.meta.QuorumConfig) DefaultRedisSessionManager(com.ctrip.xpipe.redis.console.health.DefaultRedisSessionManager) Map(java.util.Map) HostPort(com.ctrip.xpipe.endpoint.HostPort) RedisConnectionException(com.lambdaworks.redis.RedisConnectionException) EventMonitor(com.ctrip.xpipe.api.monitor.EventMonitor) ConsoleConfig(com.ctrip.xpipe.redis.console.config.ConsoleConfig) MasterNotFoundException(com.ctrip.xpipe.redis.console.resources.MasterNotFoundException) StatefulRedisSentinelConnection(com.lambdaworks.redis.sentinel.api.StatefulRedisSentinelConnection) Logger(org.slf4j.Logger) VisibleForTesting(com.ctrip.xpipe.utils.VisibleForTesting) Set(java.util.Set) RedisClient(com.lambdaworks.redis.RedisClient) Sets(com.google.common.collect.Sets) RedisSession(com.ctrip.xpipe.redis.console.health.RedisSession) Pair(com.ctrip.xpipe.tuple.Pair) ALERT_TYPE(com.ctrip.xpipe.redis.console.alert.ALERT_TYPE) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Component(org.springframework.stereotype.Component) List(java.util.List) ObjectUtils(com.ctrip.xpipe.utils.ObjectUtils) StringUtil(com.ctrip.xpipe.utils.StringUtil) CatEventMonitor(com.ctrip.xpipe.monitor.CatEventMonitor) Lazy(org.springframework.context.annotation.Lazy) MetaCache(com.ctrip.xpipe.redis.console.resources.MetaCache) HostPort(com.ctrip.xpipe.endpoint.HostPort) HashSet(java.util.HashSet)

Aggregations

HostPort (com.ctrip.xpipe.endpoint.HostPort)3 MasterNotFoundException (com.ctrip.xpipe.redis.console.resources.MasterNotFoundException)3 QuorumConfig (com.ctrip.xpipe.redis.core.meta.QuorumConfig)3 EventMonitor (com.ctrip.xpipe.api.monitor.EventMonitor)2 Server (com.ctrip.xpipe.api.server.Server)2 CatEventMonitor (com.ctrip.xpipe.monitor.CatEventMonitor)2 ALERT_TYPE (com.ctrip.xpipe.redis.console.alert.ALERT_TYPE)2 AlertManager (com.ctrip.xpipe.redis.console.alert.AlertManager)2 ConsoleConfig (com.ctrip.xpipe.redis.console.config.ConsoleConfig)2 DefaultRedisSessionManager (com.ctrip.xpipe.redis.console.health.DefaultRedisSessionManager)2 RedisSession (com.ctrip.xpipe.redis.console.health.RedisSession)2 MetaCache (com.ctrip.xpipe.redis.console.resources.MetaCache)2 Pair (com.ctrip.xpipe.tuple.Pair)2 ObjectUtils (com.ctrip.xpipe.utils.ObjectUtils)2 StringUtil (com.ctrip.xpipe.utils.StringUtil)2 VisibleForTesting (com.ctrip.xpipe.utils.VisibleForTesting)2 Sets (com.google.common.collect.Sets)2 RedisClient (com.lambdaworks.redis.RedisClient)2 RedisConnectionException (com.lambdaworks.redis.RedisConnectionException)2 StatefulRedisSentinelConnection (com.lambdaworks.redis.sentinel.api.StatefulRedisSentinelConnection)2