示例#1
0
def test_slave_for(cluster, sentinel):
    cluster.slaves = [
        {'ip': '127.0.0.1', 'port': 6379,
         'is_odown': False, 'is_sdown': False},
    ]
    slave = sentinel.slave_for('mymaster', db=9)
    assert slave.ping()
示例#2
0
def test_slave_for(cluster, sentinel):
    cluster.slaves = [
        {'ip': '127.0.0.1', 'port': 6379,
         'is_odown': False, 'is_sdown': False},
    ]
    slave = sentinel.slave_for('mymaster', db=9)
    assert slave.ping()
示例#3
0
def test_slave_for(cluster, sentinel):
    cluster.slaves = [{
        "ip": "127.0.0.1",
        "port": 6379,
        "is_odown": False,
        "is_sdown": False
    }]
    slave = sentinel.slave_for("mymaster", db=9)
    assert slave.ping()
示例#4
0
    def __init__(
        self,
        url: Optional[str] = None,
        name: str = "tilecloud",
        stop_if_empty: bool = True,
        timeout: int = 5,
        pending_timeout: int = 5 * 60,
        max_retries: int = 5,
        max_errors_age: int = 24 * 3600,
        max_errors_nb: int = 100,
        sentinels: Optional[List[Tuple[str, int]]] = None,
        service_name: str = "mymaster",
        sentinel_kwargs: Any = None,
        connection_kwargs: Any = None,
        **kwargs: Any,
    ):
        super().__init__(**kwargs)

        connection_kwargs = connection_kwargs or {}

        if sentinels is not None:
            sentinel = redis.sentinel.Sentinel(sentinels,
                                               sentinel_kwargs=sentinel_kwargs,
                                               **connection_kwargs)
            self._master = sentinel.master_for(service_name)
            self._slave = sentinel.slave_for(service_name)
        else:
            assert url is not None
            self._master = redis.Redis.from_url(url, **connection_kwargs)
            self._slave = self._master

        self._stop_if_empty = stop_if_empty
        self._timeout_ms = int(timeout * 1000)
        self._pending_timeout_ms = int(pending_timeout * 1000)
        self._max_retries = max_retries
        self._max_errors_age = max_errors_age
        self._max_errors_nb = max_errors_nb
        if not name.startswith("queue_"):
            name = "queue_" + name
        self._name_str = name
        self._name = name.encode("utf-8")
        self._errors_name = self._name + b"_errors"
        try:
            self._master.xgroup_create(name=self._name,
                                       groupname=STREAM_GROUP,
                                       id="0-0",
                                       mkstream=True)
        except redis.ResponseError as e:
            if "BUSYGROUP" not in str(e):
                raise
示例#5
0
    def slave_for(self, service_name, **kwargs):
        try:
            return self.local.slave_connections[service_name]
        except AttributeError:
            self.local.slave_connections = {}
        except KeyError:
            pass

        sentinel = self.sentinel
        if sentinel is None:
            msg = 'Cannot get slave {} using non-sentinel configuration'
            raise RuntimeError(msg.format(service_name))

        conn = sentinel.slave_for(service_name, redis_class=self.client_class, **kwargs)
        self.local.slave_connections[service_name] = conn
        return conn
示例#6
0
    def __init__(
        self,
        sentinels=None,
        master=None,
        password=None,
        db=0,
        default_timeout=300,
        key_prefix=None,
        **kwargs
    ):
        super(RedisSentinelCache, self).__init__(default_timeout)

        try:
            import redis.sentinel
        except ImportError:
            raise RuntimeError("no redis module found")

        if kwargs.get("decode_responses", None):
            raise ValueError(
                "decode_responses is not supported by " "RedisCache."
            )

        sentinels = sentinels or [("127.0.0.1", 26379)]
        sentinel_kwargs = {
            key[9:]: value
            for key, value in kwargs.items()
            if key.startswith("sentinel_")
        }
        kwargs = {
            key[9:]: value
            for key, value in kwargs.items()
            if not key.startswith("sentinel_")
        }

        sentinel = redis.sentinel.Sentinel(
            sentinels=sentinels,
            password=password,
            db=db,
            sentinel_kwargs=sentinel_kwargs,
            **kwargs
        )

        self._write_client = sentinel.master_for(master)
        self._read_clients = sentinel.slave_for(master)

        self.key_prefix = key_prefix or ""
示例#7
0
    def __init__(
        self,
        sentinels=None,
        master=None,
        password=None,
        db=0,
        default_timeout=300,
        key_prefix=None,
        **kwargs
    ):
        super().__init__(default_timeout=default_timeout)

        try:
            import redis.sentinel
        except ImportError:
            raise RuntimeError("no redis module found")

        if kwargs.get("decode_responses", None):
            raise ValueError(
                "decode_responses is not supported by " "RedisCache."
            )

        sentinels = sentinels or [("127.0.0.1", 26379)]
        sentinel_kwargs = {
            key[9:]: value
            for key, value in kwargs.items()
            if key.startswith("sentinel_")
        }
        kwargs = {
            key[9:]: value
            for key, value in kwargs.items()
            if not key.startswith("sentinel_")
        }

        sentinel = redis.sentinel.Sentinel(
            sentinels=sentinels,
            password=password,
            db=db,
            sentinel_kwargs=sentinel_kwargs,
            **kwargs
        )

        self._write_client = sentinel.master_for(master)
        self._read_clients = sentinel.slave_for(master)

        self.key_prefix = key_prefix or ""
示例#8
0
    def _create_client(self):
        sentinel_kwargs = {}
        sentinel_kwargs.update(self.sentinel_kwargs)
        sentinel_kwargs.setdefault("password", self.password)

        connection_kwargs = {}
        connection_kwargs.update(self.connection_kwargs)
        connection_kwargs.setdefault("password", self.password)

        if self.db is not None:
            connection_kwargs.setdefault("db", self.db)
            sentinel_kwargs.setdefault("db", self.db)
        if self.socket_timeout is not None:
            connection_kwargs.setdefault("socket_timeout", self.socket_timeout)

        sentinel = redis.sentinel.Sentinel(self.sentinels,
                                           sentinel_kwargs=sentinel_kwargs,
                                           **connection_kwargs)
        self.writer_client = sentinel.master_for(self.service_name)
        self.reader_client = sentinel.slave_for(self.service_name)
    def __init__(self, config: tilecloud_chain.configuration.Redis,
                 **kwargs: Any):
        """Initialize."""
        super().__init__(**kwargs)

        connection_kwargs = {}
        if "socket_timeout" in config:
            connection_kwargs["socket_timeout"] = config["socket_timeout"]
        if "db" in config:
            connection_kwargs["db"] = config["db"]
        if "url" in config:
            self._master = redis.Redis.from_url(
                config["url"], **connection_kwargs)  # type: ignore
            self._slave = self._master
        else:
            sentinel = redis.sentinel.Sentinel(config["sentinels"],
                                               **connection_kwargs)
            self._master = sentinel.master_for(
                config.get("service_name", "mymaster"))
            self._slave = sentinel.slave_for(
                config.get("service_name", "mymaster"))
        self._prefix = config["prefix"]
        self._expiration = config["expiration"]
def connect(url,
            sentinel_class=redis.sentinel.Sentinel,
            sentinel_options=None,
            client_class=redis.StrictRedis,
            client_options=None):
    parsed_url = urlparse.urlparse(url)
    if parsed_url.scheme not in ['redis', 'rediss', 'unix', 'redis+sentinel']:
        raise ValueError('Unsupported redis URL scheme: {}'.format(
            parsed_url.scheme))

    if sentinel_options is None:
        sentinel_options = {}

    if client_options is None:
        client_options = {}

    if parsed_url.scheme != 'redis+sentinel':
        return None, client_class.from_url(url, **client_options)

    sentinel_url = parse_sentinel_url(url,
                                      sentinel_options=sentinel_options,
                                      client_options=client_options)

    sentinel = sentinel_class(sentinel_url.hosts,
                              sentinel_kwargs=sentinel_url.sentinel_options,
                              **sentinel_url.client_options)
    client = None

    if sentinel_url.default_client:
        if sentinel_url.default_client.type == 'master':
            client = sentinel.master_for(sentinel_url.default_client.service,
                                         redis_class=client_class)
        else:
            client = sentinel.slave_for(sentinel_url.default_client.service,
                                        redis_class=client_class)

    return sentinel, client
示例#11
0
def test_slave_for_slave_not_found_error(cluster, sentinel):
    cluster.master['is_odown'] = True
    slave = sentinel.slave_for('mymaster', db=9)
    with pytest.raises(SlaveNotFoundError):
        slave.ping()
示例#12
0
def test_slave_for_slave_not_found_error(cluster, sentinel):
    cluster.master['is_odown'] = True
    slave = sentinel.slave_for('mymaster', db=9)
    with pytest.raises(SlaveNotFoundError):
        slave.ping()