Exemple #1
0
def test_master_min_other_sentinels(cluster, master_ip):
    sentinel = Sentinel([("foo", 26379)], min_other_sentinels=1)
    # min_other_sentinels
    with pytest.raises(MasterNotFoundError):
        sentinel.discover_master("mymaster")
    cluster.master["num-other-sentinels"] = 2
    address = sentinel.discover_master("mymaster")
    assert address == (master_ip, 6379)
Exemple #2
0
def test_master_min_other_sentinels(cluster):
    sentinel = Sentinel([('foo', 26379)], min_other_sentinels=1)
    # min_other_sentinels
    with pytest.raises(MasterNotFoundError):
        sentinel.discover_master('mymaster')
    cluster.master['num-other-sentinels'] = 2
    address = sentinel.discover_master('mymaster')
    assert address == ('127.0.0.1', 6379)
Exemple #3
0
def test_master_min_other_sentinels(cluster):
    sentinel = Sentinel([('foo', 26379)], min_other_sentinels=1)
    # min_other_sentinels
    with pytest.raises(MasterNotFoundError):
        sentinel.discover_master('mymaster')
    cluster.master['num-other-sentinels'] = 2
    address = sentinel.discover_master('mymaster')
    assert address == ('127.0.0.1', 6379)
def test_master_min_other_sentinels(cluster):
    cluster.sentinels = [
        {'ip': 'foo', 'port': 26379, 'is_sentinel': True, 'is_sdown': False},
    ]
    sentinel = Sentinel([('foo', 26379)], min_other_sentinels=1)
    # min_other_sentinels
    with pytest.raises(MasterNotFoundError):
        sentinel.discover_master('mymaster')
    cluster.master['num-other-sentinels'] = 2
    address = sentinel.discover_master('mymaster')
    assert address == ('127.0.0.1', 6379)
Exemple #5
0
def test_discover_master_sentinel_timeout(cluster, sentinel, master_ip):
    # Put first sentinel 'foo' down
    cluster.nodes_timeout.add(("foo", 26379))
    address = sentinel.discover_master("mymaster")
    assert address == (master_ip, 6379)
    # 'bar' is now first sentinel
    assert sentinel.sentinels[0].id == ("bar", 26379)
Exemple #6
0
def test_discover_master_sentinel_timeout(cluster, sentinel):
    # Put first sentinel 'foo' down
    cluster.nodes_timeout.add(('foo', 26379))
    address = sentinel.discover_master('mymaster')
    assert address == ('127.0.0.1', 6379)
    # 'bar' is now first sentinel
    assert sentinel.sentinels[0].id == ('bar', 26379)
Exemple #7
0
def test_discover_master_sentinel_down(cluster, sentinel, master_ip):
    # Put first sentinel 'foo' down
    cluster.nodes_down.add(('foo', 26379))
    address = sentinel.discover_master('mymaster')
    assert address == (master_ip, 6379)
    # 'bar' is now first sentinel
    assert sentinel.sentinels[0].id == ('bar', 26379)
Exemple #8
0
def test_discover_master_sentinel_down(cluster, sentinel):
    # Put first sentinel 'foo' down
    cluster.nodes_down.add(('foo', 26379))
    address = sentinel.discover_master('mymaster')
    assert address == ('127.0.0.1', 6379)
    # 'bar' is now first sentinel
    assert sentinel.sentinels[0].id == ('bar', 26379)
Exemple #9
0
    def _get_redis_connection(self, group, shard):
        """
        Create and return a Redis Connection for the given group

        Returns:
            redis.StrictRedis: The Redis Connection

        Raises:
            Exception: Passes through any exceptions that happen in trying to get the connection pool
        """
        redis_group = self.__config.redis_urls_by_group[group][shard]

        self.__logger.info(
            u'Attempting to connect to Redis for group "{}", shard "{}", url "{}"'
            .format(group, shard, redis_group))

        if isinstance(redis_group, PanoptesRedisConnectionConfiguration):

            redis_pool = redis.BlockingConnectionPool(
                host=redis_group.host,
                port=redis_group.port,
                db=redis_group.db,
                password=redis_group.password)
            redis_connection = redis.StrictRedis(connection_pool=redis_pool)
        elif isinstance(redis_group,
                        PanoptesRedisSentinelConnectionConfiguration):

            sentinels = [(sentinel.host, sentinel.port)
                         for sentinel in redis_group.sentinels]
            self.__logger.info(
                u'Querying Redis Sentinels "{}" for group "{}", shard "{}"'.
                format(repr(redis_group), group, shard))

            sentinel = redis.sentinel.Sentinel(sentinels)
            master = sentinel.discover_master(redis_group.master_name)
            password_present = u'yes' if redis_group.master_password else u'no'
            self.__logger.info(
                u'Going to connect to master "{}" ({}:{}, password: {}) for group "{}", shard "{}""'
                .format(redis_group.master_name, master[0], master[1],
                        password_present, group, shard))
            redis_connection = sentinel.master_for(
                redis_group.master_name, password=redis_group.master_password)
        else:

            self.__logger.info(
                u'Unknown Redis configuration object type: {}'.format(
                    type(redis_group)))
            return

        self.__logger.info(
            u'Successfully connected to Redis for group "{}", shard "{}", url "{}"'
            .format(group, shard, redis_group))

        return redis_connection
Exemple #10
0
def redis_get_master(_sentinel, sentinel_ip, sentinel_port):
    master_ip = None
    master_port = None
    if _sentinel:
        sentinel = redis.sentinel.Sentinel([(sentinel_ip, sentinel_port)],
                                           socket_timeout=0.1)
        try:
            [master_ip, master_port] = sentinel.discover_master('mymaster')
        except Exception as e:
            color.red(" (%s) " % str(e), end="")
    return master_ip, master_port
Exemple #11
0
    def check(self):
        log.info('检查 Redis Sentinel 连接: %s', self._master)

        try:
            sentinel = redis.sentinel.Sentinel(
                [s.split(':') for s in self._sentinels],
                password=self._password,
                socket_timeout=0.1)

            log.info('> master: %s', sentinel.discover_master(self._master))
            log.info('> slaves: %s', sentinel.discover_slaves(self._master))
            log.info('尝试获取 master 节点状态信息...')

            master = sentinel.master_for(self._master, socket_timeout=0.1)
            self._log_redis_info(master.info())
        except redis.sentinel.MasterNotFoundError as e:
            log.error('检查 Redis Sentinel 出错: %s - %s', e.__class__, e)
Exemple #12
0
def test_discover_master_error(sentinel):
    with pytest.raises(MasterNotFoundError):
        sentinel.discover_master('xxx')
Exemple #13
0
def test_discover_master(sentinel):
    address = sentinel.discover_master('mymaster')
    assert address == ('127.0.0.1', 6379)
Exemple #14
0
def test_master_sdown(cluster, sentinel):
    cluster.master['is_sdown'] = True
    with pytest.raises(MasterNotFoundError):
        sentinel.discover_master('mymaster')
Exemple #15
0
def test_discover_master(sentinel, master_ip):
    address = sentinel.discover_master('mymaster')
    assert address == (master_ip, 6379)
Exemple #16
0
def test_master_odown(cluster, sentinel):
    cluster.master['is_odown'] = True
    with pytest.raises(MasterNotFoundError):
        sentinel.discover_master('mymaster')
Exemple #17
0
def test_master_odown(cluster, sentinel):
    cluster.master["is_odown"] = True
    with pytest.raises(MasterNotFoundError):
        sentinel.discover_master("mymaster")
Exemple #18
0
def test_discover_master_error(sentinel):
    with pytest.raises(MasterNotFoundError):
        sentinel.discover_master('xxx')
Exemple #19
0
def test_discover_master(sentinel):
    address = sentinel.discover_master('mymaster')
    assert address == ('127.0.0.1', 6379)