ruvio-client 0.1.1__tar.gz → 0.1.3.dev33__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/PKG-INFO +176 -7
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/README.md +175 -6
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/pyproject.toml +1 -1
- ruvio_client-0.1.3.dev33/ruvio/__init__.py +154 -0
- ruvio_client-0.1.3.dev33/ruvio/_blocking.py +175 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/ruvio/_cluster.py +141 -6
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/ruvio/_command_keys.py +8 -0
- ruvio_client-0.1.3.dev33/ruvio/_commands.py +6058 -0
- ruvio_client-0.1.3.dev33/ruvio/_constants.py +408 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/ruvio/_converters.py +48 -3
- ruvio_client-0.1.3.dev33/ruvio/_follow.py +97 -0
- ruvio_client-0.1.3.dev33/ruvio/_models.py +162 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/ruvio/_pipeline.py +15 -2
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/ruvio/_resp.py +18 -1
- ruvio_client-0.1.3.dev33/ruvio/_tls.py +46 -0
- ruvio_client-0.1.3.dev33/ruvio/asyncio/__init__.py +24 -0
- ruvio_client-0.1.3.dev33/ruvio/asyncio/client.py +1722 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/ruvio/asyncio/cluster.py +497 -62
- ruvio_client-0.1.3.dev33/ruvio/client.py +1654 -0
- ruvio_client-0.1.3.dev33/ruvio/cluster.py +995 -0
- ruvio_client-0.1.3.dev33/ruvio/errors.py +159 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/ruvio_client.egg-info/PKG-INFO +176 -7
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/ruvio_client.egg-info/SOURCES.txt +18 -1
- ruvio_client-0.1.3.dev33/tests/test_asyncio_timeout.py +116 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_client_edge.py +129 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_cluster_edge.py +114 -10
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_cluster_live.py +8 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_cluster_pure.py +5 -1
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_cluster_unit.py +3 -3
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_command_encoding.py +75 -3
- ruvio_client-0.1.3.dev33/tests/test_constants.py +156 -0
- ruvio_client-0.1.3.dev33/tests/test_coordinator_failover_live.py +116 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_coverage.py +1 -1
- ruvio_client-0.1.3.dev33/tests/test_durability_live.py +90 -0
- ruvio_client-0.1.3.dev33/tests/test_failover_live.py +174 -0
- ruvio_client-0.1.3.dev33/tests/test_follow_failover.py +678 -0
- ruvio_client-0.1.3.dev33/tests/test_follow_memory.py +414 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_helpers_live.py +2 -2
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_live.py +141 -2
- ruvio_client-0.1.3.dev33/tests/test_multi_node_live.py +56 -0
- ruvio_client-0.1.3.dev33/tests/test_replica_reads.py +308 -0
- ruvio_client-0.1.3.dev33/tests/test_replica_reads_live.py +73 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_resp_edge.py +11 -0
- ruvio_client-0.1.3.dev33/tests/test_seeds.py +246 -0
- ruvio_client-0.1.3.dev33/tests/test_tls_live.py +180 -0
- ruvio_client-0.1.3.dev33/tests/test_ttl_on_create.py +147 -0
- ruvio_client-0.1.1/ruvio/__init__.py +0 -73
- ruvio_client-0.1.1/ruvio/_commands.py +0 -1749
- ruvio_client-0.1.1/ruvio/_models.py +0 -86
- ruvio_client-0.1.1/ruvio/asyncio/__init__.py +0 -7
- ruvio_client-0.1.1/ruvio/asyncio/client.py +0 -672
- ruvio_client-0.1.1/ruvio/client.py +0 -641
- ruvio_client-0.1.1/ruvio/cluster.py +0 -541
- ruvio_client-0.1.1/ruvio/errors.py +0 -113
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/LICENSE +0 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/ruvio/py.typed +0 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/ruvio_client.egg-info/dependency_links.txt +0 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/ruvio_client.egg-info/requires.txt +0 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/ruvio_client.egg-info/top_level.txt +0 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/setup.cfg +0 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_key_vectors.py +0 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_resp.py +0 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_resp_vectors.py +0 -0
- {ruvio_client-0.1.1 → ruvio_client-0.1.3.dev33}/tests/test_slot_vectors.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ruvio-client
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3.dev33
|
|
4
4
|
Summary: Fast sync and asyncio Python client for the Ruvio key-value server (RESP2/RESP3)
|
|
5
5
|
Author: Salih Cantekin
|
|
6
6
|
License: MIT
|
|
@@ -62,11 +62,11 @@ dependencies and needs Python 3.9 or newer. It ships type information (`py.typed
|
|
|
62
62
|
- `ruvio.Ruvio` is the synchronous client. It uses one socket and a reusable `recv_into` buffer.
|
|
63
63
|
- `ruvio.asyncio.Ruvio` is the asyncio client. It keeps one multiplexed, pipelined connection
|
|
64
64
|
that all coroutines share.
|
|
65
|
-
- There is a typed method for each of the **
|
|
65
|
+
- There is a typed method for each of the **299** commands in `docs/compatibility-matrix.json`.
|
|
66
66
|
The methods also cover the options that change Ruvio's behaviour: `SET … IFEQ`,
|
|
67
67
|
`INCRBY … MAX`, `FILLGET … WAIT`, `FILLSET … SWR`, `GETEX` in milliseconds,
|
|
68
68
|
`DELEX key IFEQ v`, `LPUSHCAP`/`RPUSHCAP`, `BF.*`, `JSON.*` (including `JSON.INDEX` and
|
|
69
|
-
`JSON.FIND`), `IDEM BEGIN/COMPLETE`, `LEASE`/`RELEASE`, `SEMAPHORE`, `ONCE`, `TAKE`, `SETV`,
|
|
69
|
+
`JSON.FIND`), `IDEM BEGIN/COMPLETE`, `LEASE`/`RELEASE`, `SEMAPHORE`, `ONCE`, `TAKE`, `TRANSFER`/`HTRANSFER`, `SETV`,
|
|
70
70
|
`CHANGES`, `LIMIT` and `XADD … DELAY`.
|
|
71
71
|
- Pipelines, MULTI/EXEC transactions, Pub/Sub on a dedicated connection, and RESP3 through
|
|
72
72
|
`HELLO 3`, including push messages.
|
|
@@ -92,6 +92,7 @@ r = ruvio.Ruvio("127.0.0.1", 6379, db=0) # or ruvio.Ruvio.from_url("ruvio
|
|
|
92
92
|
r.set("greeting", "hello", px=60_000) # True
|
|
93
93
|
r.get("greeting") # 'hello' (None when missing)
|
|
94
94
|
r.set("greeting", "hi", ifeq="hello") # Ruvio: write only if the value matches
|
|
95
|
+
r.set("watermark", 1700000000, ifgt=True) # Ruvio: write only if greater than the stored number (iflt for smaller)
|
|
95
96
|
r.incrby("stock", 5, max=100) # Ruvio: None (nothing written) past the limit
|
|
96
97
|
r.delex("greeting", "hi") # Ruvio: delete only if the value matches
|
|
97
98
|
r.raw.get("greeting") # bytes, on the same connection
|
|
@@ -123,11 +124,50 @@ you want `bytes`.
|
|
|
123
124
|
Server errors raise `ruvio.ResponseError` or a more specific class: `WrongTypeError`,
|
|
124
125
|
`NoScriptError`, `NoGroupError`, `BusyError`, `ExecAbortError` or `MovedError`. Each one has
|
|
125
126
|
a `.code` such as `"WRONGTYPE"`. Socket problems raise `ruvio.ConnectionError` or
|
|
126
|
-
`ruvio.TimeoutError`.
|
|
127
|
+
`ruvio.TimeoutError`. `socket_timeout` is 30 seconds by default on the sync and the asyncio client (`None` waits
|
|
128
|
+
forever; on expiry the waiting commands fail and the connection is closed, and the next command opens it again); a blocking command such as `blpop(key, 5)` gets its own wait on top, and `0` (wait forever)
|
|
129
|
+
has no limit. See [Timeouts](../README.md#timeouts).
|
|
127
130
|
|
|
128
131
|
For any other command, `execute_command("CMD", arg, …)` sends it as written and returns the
|
|
129
132
|
raw reply.
|
|
130
133
|
|
|
134
|
+
### Typed keyword constants
|
|
135
|
+
|
|
136
|
+
Keyword arguments have `str` enums: `GeoUnit` (`m`, `km`, `ft`, `mi`), `GeoOrder`, `InsertPosition`, `ListSide`, `ExpireCondition`, `BitOperation`, `BitUnit`, `Aggregate`, `KeyType` (for `SCAN TYPE`), and for `BITFIELD` `BitFieldOp`, `Overflow`, `signed(bits)` / `unsigned(bits)`. A member is a `str`, so plain strings keep working and send the same bytes; the encoder uses each member's pre-encoded bytes, with no per-call cost.
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
from ruvio import GeoUnit, GeoOrder, KeyType, ListSide
|
|
140
|
+
|
|
141
|
+
r.geosearch("cities", member="A", radius=5, unit=GeoUnit.KILOMETERS, sort=GeoOrder.ASC) # same as unit="km", sort="ASC"
|
|
142
|
+
r.scan(0, type=KeyType.HASH)
|
|
143
|
+
r.lmove("src", "dst", ListSide.LEFT, ListSide.RIGHT)
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
More typed helpers: `InfoSection` (`r.info(InfoSection.MEMORY_BREAKDOWN)`), `ClientType` (`r.client_list(ClientType.PUBSUB)`), `ChangeType` with `change_types(...)` for `CHANGES SUBSCRIBE ... TYPES`, `StreamId` (`NEW_ENTRY`, `LAST`, `NEW_MESSAGES`, `MIN`, `MAX`), `ScoreBound` / `LexBound` with `score_exclusive(x)`, `lex_inclusive(m)`, `lex_exclusive(m)`, and `StreamTrim` for `XADD` / `XTRIM`:
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
from ruvio import StreamTrim, TrimMode
|
|
150
|
+
|
|
151
|
+
r.xadd("events", {"type": "signup"}, trim=StreamTrim.maxlen(10_000, TrimMode.APPROXIMATE)) # MAXLEN ~ 10000
|
|
152
|
+
r.xtrim("events", trim=StreamTrim.minid("1700000000000-0")) # MINID = ...
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### TTL on create
|
|
156
|
+
|
|
157
|
+
Commands that can create a key take an optional keyword-only `ttl` (seconds as `int` or `float`, or a `timedelta`): `incr`, `decr`, `incrby`, `decrby`, `incrbyfloat`, `hincrby`, `zincrby`, `setbit`, `zadd`, `geoadd`, `xadd`, `json_set`, `json_set_obj` and `bf_insert`. The deadline is set only when the call creates the key; an existing key keeps the deadline it has. Whole seconds are sent as `EX`, a sub-second part as `PX`. Zero, negative or non-numeric values raise before anything is sent.
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
r.incr("rate:user:7", ttl=60) # the 60 s window starts on the first call
|
|
161
|
+
r.incrby("quota:day", 1, max=100, ttl=86400) # counter with a ceiling and a daily window
|
|
162
|
+
r.zadd("recent", {"a": 1}, ttl=timedelta(hours=1))
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
`expire`, `pexpire`, `expireat` and `pexpireat` take `condition=` (`ExpireCondition.NX`, `XX`, `GT` or `LT`, or the string). A key without a deadline counts as infinite; a failed condition returns `False`.
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
r.expire("session", 3600, condition=ExpireCondition.NX) # only when the key has no deadline yet
|
|
169
|
+
```
|
|
170
|
+
|
|
131
171
|
### Pipelines and transactions
|
|
132
172
|
|
|
133
173
|
```python
|
|
@@ -173,6 +213,8 @@ to `on_push=callback` and never reach a command's reply.
|
|
|
173
213
|
| `r.lease_scope(key, ttl_ms)` | `LEASE` / `RELEASE` | Yields the fencing token and releases it on exit |
|
|
174
214
|
| `r.semaphore_scope(key, max, ttl_ms)` | `SEMAPHORE` / `RELEASE` | Raises `LockError` when full |
|
|
175
215
|
| `r.rate_limit(key, max, window_ms)` | `LIMIT` | `(allowed, remaining)` |
|
|
216
|
+
| `r.limit_sliding(key, max, window_ms)`, `r.limit_tokens(key, capacity, refill_ms, cost=1)` | `LIMIT ... SLIDING`, `LIMIT ... TOKENS` | remaining, or `None` |
|
|
217
|
+
| `r.limit_sliding_with_retry(...)`, `r.limit_tokens_with_retry(...)` | `LIMIT ... WITHRETRY` | `(allowed, remaining, retry_after_ms)` |
|
|
176
218
|
| `r.get_or_create(key, factory, ttl_ms, fill_ms=…, swr_ms=…)` | `FILLGET` / `FILLSET` | Exactly one caller runs `factory` (sync or async). Waiters poll plain `FILLGET` with backoff from 5 ms up to 100 ms instead of `WAIT`, which would park the shared connection |
|
|
177
219
|
| `r.idempotent(key, fingerprint, ttl_ms, fn)` | `IDEM BEGIN` / `COMPLETE` | `("executed", result)`, `("completed", stored)`, `("pending", None)` or `("conflict", None)` |
|
|
178
220
|
| `r.scan_iter()` / `hscan_iter` / `sscan_iter` / `zscan_iter` | `SCAN` family | Lazy and driven by the cursor; async iterators on the asyncio client |
|
|
@@ -276,6 +318,13 @@ Behaviour worth knowing:
|
|
|
276
318
|
the table points that one slot at it; a node that is not in the table, or a second `MOVED` for
|
|
277
319
|
the same command, re-reads `CLUSTER SLOTS`. `MOVED` means the command was not executed, so
|
|
278
320
|
following it is safe.
|
|
321
|
+
- **`-ASK`**: `ASKING` and the command are sent in one write to the node the reply names (a node
|
|
322
|
+
that is not in the table gets a connection), and its reply is returned. The slot table is not
|
|
323
|
+
changed, so the next command for that slot still goes to the table's owner. `ASK` and `MOVED`
|
|
324
|
+
share the budget of 3 redirects per command; an error reply to `ASKING` is the result.
|
|
325
|
+
- **`-TRYAGAIN`**: the command is sent again, routed by the current table, at most **7 times**,
|
|
326
|
+
after 5, 10, 20, 40, 80, 160 and 320 ms (635 ms in all; `asyncio.sleep` in the asyncio client); an
|
|
327
|
+
eighth `TRYAGAIN` is raised. Redirects during the retries are followed with their own budget.
|
|
279
328
|
- **No replay after transport failures.** A dropped node socket fails the command that saw it
|
|
280
329
|
(`ConnectionError`), is closed, and is reopened by the next command for that node. Other nodes
|
|
281
330
|
are not affected. A pipeline/split call that touched several nodes still reads every healthy
|
|
@@ -308,6 +357,116 @@ Short benchmark (`RUVIO_TEST_ADDR` plain 1-shard server vs `RUVIO_CLUSTER_ADDR`
|
|
|
308
357
|
coroutine: the node connection's pending-reply future is returned directly, and only a `MOVED`
|
|
309
358
|
reply is intercepted.
|
|
310
359
|
|
|
360
|
+
## Reads from replicas
|
|
361
|
+
|
|
362
|
+
`read_policy=` on a cluster client (sync and asyncio) sends reads to a replica of the key's shard. It takes
|
|
363
|
+
`ReadPolicy.PRIMARY` (default: nothing changes), `ReadPolicy.PREFER_REPLICA` or `ReadPolicy.REPLICA`, or the
|
|
364
|
+
strings `"primary"`, `"prefer-replica"` and `"replica"`. A standalone server ignores it.
|
|
365
|
+
|
|
366
|
+
```python
|
|
367
|
+
from ruvio import ClusterRuvio, ReadPolicy
|
|
368
|
+
|
|
369
|
+
client = ClusterRuvio("127.0.0.1", 7000, read_policy=ReadPolicy.PREFER_REPLICA)
|
|
370
|
+
client.set("user:1", "Ada") # primary
|
|
371
|
+
client.get("user:1") # a replica; may not have the write yet
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
- Replication is asynchronous: a read from a replica may miss a recent write, including your own.
|
|
375
|
+
- A read is a command the server flags read-only and not blocking (`GET`, `MGET`, `EXISTS`, `HGETALL`,
|
|
376
|
+
`ZRANGE` ...). Writes, keyless commands, transactions (`MULTI`/`EXEC`/`WATCH`), blocking commands, scripts,
|
|
377
|
+
Pub/Sub, `SCAN` and the commands sent to every node stay on the primaries. A multi-key command split over
|
|
378
|
+
several slots decides per part: `MGET` and `EXISTS` parts read from replicas, `DEL` and `MSET` parts go to primaries.
|
|
379
|
+
- A pipeline goes to replicas only when every command in it is a read; a pipeline with a write is routed as before.
|
|
380
|
+
- When the slot table loads, the client picks one replica per shard at random and keeps it until the next load.
|
|
381
|
+
Each replica connection sends `READONLY` right after it opens. With the default policy no replica connection
|
|
382
|
+
is opened and routing does one extra comparison.
|
|
383
|
+
- A replica that cannot be connected, or answers `LOADING` (still copying its primary), is skipped until the
|
|
384
|
+
next slot table load. `PREFER_REPLICA` then reads from the primary (a `LOADING` read is sent again there);
|
|
385
|
+
`REPLICA` raises (`ConnectionError`, or the `LOADING` `ResponseError`), and a `RuvioError` saying "no replica"
|
|
386
|
+
when none is known. A socket that drops after a command was sent fails the command, as for every node.
|
|
387
|
+
|
|
388
|
+
## Durable replies
|
|
389
|
+
|
|
390
|
+
`durability_replicas=n` (and optionally `durability_timeout_ms=`) on `Ruvio`, `asyncio.Ruvio` and the cluster
|
|
391
|
+
clients sends `CLIENT DURABILITY REPLICAS n [TIMEOUT ms]` right after the handshake of every connection,
|
|
392
|
+
reconnects included. The server then holds each reply until `n` replicas have the writes on disk; on a timeout
|
|
393
|
+
(default 5000 ms) the replies of that batch are `-NOREPLICAS` errors and the writes stay applied. A server
|
|
394
|
+
without replication refuses it and the connect fails. Without the option nothing is sent.
|
|
395
|
+
`client.client_durability(n, timeout_ms=None)` sets it on the current connection only (`0` turns it off).
|
|
396
|
+
|
|
397
|
+
```python
|
|
398
|
+
r = Ruvio("127.0.0.1", 6379, durability_replicas=1, durability_timeout_ms=5000)
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
## Seed addresses
|
|
402
|
+
|
|
403
|
+
`seeds=` takes the place of `host`/`port` (both clients, plain and `cluster=True`): `"host:port"` strings
|
|
404
|
+
(IPv6 as `"[::1]:6379"`) or `(host, port)` tuples. Without it nothing changes.
|
|
405
|
+
|
|
406
|
+
```python
|
|
407
|
+
from ruvio import Ruvio
|
|
408
|
+
|
|
409
|
+
client = Ruvio(seeds=["10.0.0.1:6379", ("10.0.0.2", 6379), "[::1]:6379"])
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
- Connecting tries the seeds in order, one connect attempt each, and keeps the first that answers. A seed
|
|
413
|
+
that cannot be reached is skipped; a refused `AUTH` or `SELECT` raises at once. When none answers, the
|
|
414
|
+
last `ConnectionError` is raised.
|
|
415
|
+
- After a dropped connection the command that hit the drop still raises. The next reconnect tries every seed
|
|
416
|
+
once, starting with the seed after the one that dropped and ending with it. A `pubsub()` session opened
|
|
417
|
+
later starts at the address the command connection is on.
|
|
418
|
+
- With `cluster=True`, `CLUSTER SLOTS` comes from the first reachable seed. A table reload asks the nodes that
|
|
419
|
+
have an open socket first; when none answers it opens every other known node and then every seed, one
|
|
420
|
+
connect attempt each, until one returns a table. A node that cannot be opened triggers that reload and the
|
|
421
|
+
command goes to the owner the new table names (a keyless command to the first reachable node of the new
|
|
422
|
+
table). Pipelines, fan-out commands and transactions do not trigger the reload on their own; they fail as
|
|
423
|
+
before and the next single-key or keyless command reloads. Seeds are never asked while the known nodes answer.
|
|
424
|
+
|
|
425
|
+
## Following a failover
|
|
426
|
+
|
|
427
|
+
A standalone client with two or more `seeds`, or with `sentinel_master`, finds the primary again after a
|
|
428
|
+
failover instead of staying on a node that is now a replica. It is on by default (`follow_primary=True`), in
|
|
429
|
+
both clients, and does nothing with one seed and no `sentinel_master`. A cluster client keeps its
|
|
430
|
+
`MOVED`/`ASK` rules and does not use it. The contract is in [docs/CLIENT_BEHAVIOR.md](../../docs/CLIENT_BEHAVIOR.md).
|
|
431
|
+
|
|
432
|
+
```python
|
|
433
|
+
client = Ruvio(seeds=["10.0.0.1:6379", "10.0.0.2:6379"]) # asks ROLE
|
|
434
|
+
client = Ruvio(seeds=["10.0.0.9:26379"], sentinel_master="main") # asks the coordinator
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
- **Reconnect.** After a dropped connection the next connect asks the seeds (starting with the one after the one
|
|
438
|
+
that dropped) which node answers `ROLE` with `master`, following the primary address a replica reports, and
|
|
439
|
+
connects to it. When none is found, or it cannot be opened, the seed order above applies. The command that hit
|
|
440
|
+
the drop still raises. The first connect keeps the seed order; with `sentinel_master` it asks the coordinators
|
|
441
|
+
(`SENTINEL get-master-addr-by-name`, confirmed with `ROLE`) and raises `ProtocolError` when none names a primary.
|
|
442
|
+
- **`READONLY`.** A command answered `READONLY` (the text is `READONLY` alone or followed by a space) makes the
|
|
443
|
+
client search for the primary, up to 8 rounds with waits of 100 ms doubling to 2 s (7.1 s in all; the asyncio
|
|
444
|
+
client waits without blocking other tasks), move to it and send the refused command once more. The node that
|
|
445
|
+
answered is skipped only while it still says `master`; once it says it is a replica, a later round takes it when
|
|
446
|
+
it is promoted. The retry's reply
|
|
447
|
+
goes to the caller, a second `READONLY` included; there is never a third send. When no primary is found the
|
|
448
|
+
`READONLY` error is raised and the connection stays where it was.
|
|
449
|
+
- **Not resent.** A command inside a transaction opened with `multi()` (the connection still moves, so the next
|
|
450
|
+
command works), a command of a pipeline (its `READONLY` is that command's result), and any other error.
|
|
451
|
+
- **Cost.** A healthy connection sends nothing extra; the error text of a reply is looked at only where error
|
|
452
|
+
replies are already inspected. The only extra sockets are short probes (`AUTH` as configured, one command, one
|
|
453
|
+
reply, bounded by the smaller of `connect_timeout` and `socket_timeout`). While following is active the asyncio
|
|
454
|
+
client keeps each command's encoded frame on its pending entry (a reference, no copy); command methods still
|
|
455
|
+
return the connection's future, and a `READONLY` reply is answered from the reader by one shared task.
|
|
456
|
+
- `follow_primary=False` restores the plain seed behavior: `READONLY` is returned as an error and no `ROLE` is
|
|
457
|
+
sent. The Python clients have no connection-lost or connection-restored hooks, so nothing is reported.
|
|
458
|
+
|
|
459
|
+
## TLS
|
|
460
|
+
|
|
461
|
+
Plain TCP is the default. `tls=True` wraps every socket the client opens (the sync and the asyncio client, cluster nodes, replicas, Pub/Sub and failover probes) in TLS. The server certificate chain and host name are verified against the system roots.
|
|
462
|
+
|
|
463
|
+
```python
|
|
464
|
+
client = ruvio.Ruvio("cache.internal", 6379, tls=True, tls_ca_file="ca.pem")
|
|
465
|
+
client = ruvio.Ruvio.from_url("ruvios://cache.internal:6379") # rediss:// is the same
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
`tls_ca_file` (PEM) trusts your private CA instead of the system roots. `tls_cert_file` and `tls_key_file` (PEM) answer a server that demands mutual TLS (`tls_client_auth`). `tls_server_name` is the name the certificate carries when you connect by IP address. Pass a ready `ssl.SSLContext` as `tls=` to control everything yourself. `tls_insecure_skip_verify=True` accepts any certificate and skips the host name check, which removes the protection against someone in the middle: use it for a throwaway test only. The handshake is part of the connect (bounded by `connect_timeout`), so a refused certificate raises `ruvio.ConnectionError`. A TLS option without `tls=True`, or `tls` with `unix_socket_path`, raises `ValueError`. Measured: the live TLS tests (`tests/test_tls_live.py`, sync and asyncio); the latency TLS adds: not measured.
|
|
469
|
+
|
|
311
470
|
## Tests
|
|
312
471
|
|
|
313
472
|
```bash
|
|
@@ -327,7 +486,7 @@ RUVIO_TEST_FLUSHALL=1 ... # also r
|
|
|
327
486
|
has no typed method on the sync or the async client. It also fails when a typed method is
|
|
328
487
|
never called by a live test.
|
|
329
488
|
- `tests/test_cluster_unit.py` needs no Ruvio server: slot vectors and hashtags, key
|
|
330
|
-
extraction through the typed methods, a check that **all
|
|
489
|
+
extraction through the typed methods, a check that **all 299 matrix commands have a routing
|
|
331
490
|
rule**, topology / `MOVED` / scan-cursor parsing, and the router against scripted fake servers
|
|
332
491
|
(`tests/fakecluster.py`) on both clients: owner-only routing, fail-fast `CROSSSLOT` (no byte
|
|
333
492
|
sent), split reassembly order, `MOVED` patch / refresh / unknown node / at most 3 redirects, no
|
|
@@ -343,6 +502,15 @@ RUVIO_TEST_FLUSHALL=1 ... # also r
|
|
|
343
502
|
`BLPOP`, pub/sub and sharded pub/sub, losing one node's connection. The standalone fallback
|
|
344
503
|
test uses `RUVIO_TEST_ADDR` (plain server). Cluster mode has only database 0, so the older
|
|
345
504
|
live suites (database 11) are not run on the cluster.
|
|
505
|
+
- `tests/test_replica_reads_live.py` needs `RUVIO_REPLICATED_ADDR` (a 2-shard primary) and `RUVIO_REPLICA_ADDR` (its
|
|
506
|
+
2-shard replica, shard i on port + i); `scripts/run_with_servers.sh` starts both. Without them it is skipped.
|
|
507
|
+
- `tests/test_multi_node_live.py` needs `RUVIO_MULTINODE_A_ADDR`, `RUVIO_MULTINODE_B_ADDR` and
|
|
508
|
+
`RUVIO_MULTINODE_STALE_ADDR` (a two-node cluster and a node with an older map, started by
|
|
509
|
+
`scripts/run_with_servers.sh`) and checks that the client follows `MOVED`. Without them it is skipped.
|
|
510
|
+
- `tests/test_failover_live.py` needs `RUVIO_FAILOVER_ADDRS` and `RUVIO_FAILOVER_REPLICATION` (a primary and a replica, comma
|
|
511
|
+
separated, primary first, and their replication addresses); `scripts/run_with_servers.sh` starts them. It swaps the roles
|
|
512
|
+
with `REPLICAOF` and checks that the client follows, that `follow_primary=False` gets `READONLY`, and that a reconnect lands
|
|
513
|
+
on the primary. Without them it is skipped. `tests/test_follow_failover.py` covers the rules against scripted nodes.
|
|
346
514
|
- Without `RUVIO_TEST_ADDR` the live tests are skipped. Live tests use database 11 and run
|
|
347
515
|
`FLUSHDB`. `test_keys` also uses database 12. `JSON.INDEX` only works in database 0, so
|
|
348
516
|
that test uses unique keys there and cleans up after itself.
|
|
@@ -355,8 +523,9 @@ RUVIO_TEST_FLUSHALL=1 ... # also r
|
|
|
355
523
|
`MULTI`/`EXEC` sent one by one are refused (use `pipeline(transaction=True)`); a
|
|
356
524
|
cross-slot `RENAME`/`COPY`/`SMOVE`/`LMOVE`/... is refused rather than emulated by
|
|
357
525
|
copying; `CLUSTER NODES/SHARDS/INFO/MYID` and `CHANGES` go to the seed only; `SCRIPT EXISTS`
|
|
358
|
-
and `SCRIPT KILL` ask the seed only;
|
|
359
|
-
|
|
526
|
+
and `SCRIPT KILL` ask the seed only; `ASK` and `TRYAGAIN` are handled
|
|
527
|
+
as described above, but only against scripted servers, since the server does not
|
|
528
|
+
migrate slots across nodes; no replica reads (`READONLY` is sent to the seed only); a cluster whose
|
|
360
529
|
advertised host is unreachable from the client is not handled. Not verified: a server
|
|
361
530
|
topology that changes while the client runs (`MOVED` recovery was verified by corrupting the
|
|
362
531
|
client's table, not by moving slots on a server), clusters with more than 3 nodes, AUTH on
|
|
@@ -27,11 +27,11 @@ dependencies and needs Python 3.9 or newer. It ships type information (`py.typed
|
|
|
27
27
|
- `ruvio.Ruvio` is the synchronous client. It uses one socket and a reusable `recv_into` buffer.
|
|
28
28
|
- `ruvio.asyncio.Ruvio` is the asyncio client. It keeps one multiplexed, pipelined connection
|
|
29
29
|
that all coroutines share.
|
|
30
|
-
- There is a typed method for each of the **
|
|
30
|
+
- There is a typed method for each of the **299** commands in `docs/compatibility-matrix.json`.
|
|
31
31
|
The methods also cover the options that change Ruvio's behaviour: `SET … IFEQ`,
|
|
32
32
|
`INCRBY … MAX`, `FILLGET … WAIT`, `FILLSET … SWR`, `GETEX` in milliseconds,
|
|
33
33
|
`DELEX key IFEQ v`, `LPUSHCAP`/`RPUSHCAP`, `BF.*`, `JSON.*` (including `JSON.INDEX` and
|
|
34
|
-
`JSON.FIND`), `IDEM BEGIN/COMPLETE`, `LEASE`/`RELEASE`, `SEMAPHORE`, `ONCE`, `TAKE`, `SETV`,
|
|
34
|
+
`JSON.FIND`), `IDEM BEGIN/COMPLETE`, `LEASE`/`RELEASE`, `SEMAPHORE`, `ONCE`, `TAKE`, `TRANSFER`/`HTRANSFER`, `SETV`,
|
|
35
35
|
`CHANGES`, `LIMIT` and `XADD … DELAY`.
|
|
36
36
|
- Pipelines, MULTI/EXEC transactions, Pub/Sub on a dedicated connection, and RESP3 through
|
|
37
37
|
`HELLO 3`, including push messages.
|
|
@@ -57,6 +57,7 @@ r = ruvio.Ruvio("127.0.0.1", 6379, db=0) # or ruvio.Ruvio.from_url("ruvio
|
|
|
57
57
|
r.set("greeting", "hello", px=60_000) # True
|
|
58
58
|
r.get("greeting") # 'hello' (None when missing)
|
|
59
59
|
r.set("greeting", "hi", ifeq="hello") # Ruvio: write only if the value matches
|
|
60
|
+
r.set("watermark", 1700000000, ifgt=True) # Ruvio: write only if greater than the stored number (iflt for smaller)
|
|
60
61
|
r.incrby("stock", 5, max=100) # Ruvio: None (nothing written) past the limit
|
|
61
62
|
r.delex("greeting", "hi") # Ruvio: delete only if the value matches
|
|
62
63
|
r.raw.get("greeting") # bytes, on the same connection
|
|
@@ -88,11 +89,50 @@ you want `bytes`.
|
|
|
88
89
|
Server errors raise `ruvio.ResponseError` or a more specific class: `WrongTypeError`,
|
|
89
90
|
`NoScriptError`, `NoGroupError`, `BusyError`, `ExecAbortError` or `MovedError`. Each one has
|
|
90
91
|
a `.code` such as `"WRONGTYPE"`. Socket problems raise `ruvio.ConnectionError` or
|
|
91
|
-
`ruvio.TimeoutError`.
|
|
92
|
+
`ruvio.TimeoutError`. `socket_timeout` is 30 seconds by default on the sync and the asyncio client (`None` waits
|
|
93
|
+
forever; on expiry the waiting commands fail and the connection is closed, and the next command opens it again); a blocking command such as `blpop(key, 5)` gets its own wait on top, and `0` (wait forever)
|
|
94
|
+
has no limit. See [Timeouts](../README.md#timeouts).
|
|
92
95
|
|
|
93
96
|
For any other command, `execute_command("CMD", arg, …)` sends it as written and returns the
|
|
94
97
|
raw reply.
|
|
95
98
|
|
|
99
|
+
### Typed keyword constants
|
|
100
|
+
|
|
101
|
+
Keyword arguments have `str` enums: `GeoUnit` (`m`, `km`, `ft`, `mi`), `GeoOrder`, `InsertPosition`, `ListSide`, `ExpireCondition`, `BitOperation`, `BitUnit`, `Aggregate`, `KeyType` (for `SCAN TYPE`), and for `BITFIELD` `BitFieldOp`, `Overflow`, `signed(bits)` / `unsigned(bits)`. A member is a `str`, so plain strings keep working and send the same bytes; the encoder uses each member's pre-encoded bytes, with no per-call cost.
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from ruvio import GeoUnit, GeoOrder, KeyType, ListSide
|
|
105
|
+
|
|
106
|
+
r.geosearch("cities", member="A", radius=5, unit=GeoUnit.KILOMETERS, sort=GeoOrder.ASC) # same as unit="km", sort="ASC"
|
|
107
|
+
r.scan(0, type=KeyType.HASH)
|
|
108
|
+
r.lmove("src", "dst", ListSide.LEFT, ListSide.RIGHT)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
More typed helpers: `InfoSection` (`r.info(InfoSection.MEMORY_BREAKDOWN)`), `ClientType` (`r.client_list(ClientType.PUBSUB)`), `ChangeType` with `change_types(...)` for `CHANGES SUBSCRIBE ... TYPES`, `StreamId` (`NEW_ENTRY`, `LAST`, `NEW_MESSAGES`, `MIN`, `MAX`), `ScoreBound` / `LexBound` with `score_exclusive(x)`, `lex_inclusive(m)`, `lex_exclusive(m)`, and `StreamTrim` for `XADD` / `XTRIM`:
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from ruvio import StreamTrim, TrimMode
|
|
115
|
+
|
|
116
|
+
r.xadd("events", {"type": "signup"}, trim=StreamTrim.maxlen(10_000, TrimMode.APPROXIMATE)) # MAXLEN ~ 10000
|
|
117
|
+
r.xtrim("events", trim=StreamTrim.minid("1700000000000-0")) # MINID = ...
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### TTL on create
|
|
121
|
+
|
|
122
|
+
Commands that can create a key take an optional keyword-only `ttl` (seconds as `int` or `float`, or a `timedelta`): `incr`, `decr`, `incrby`, `decrby`, `incrbyfloat`, `hincrby`, `zincrby`, `setbit`, `zadd`, `geoadd`, `xadd`, `json_set`, `json_set_obj` and `bf_insert`. The deadline is set only when the call creates the key; an existing key keeps the deadline it has. Whole seconds are sent as `EX`, a sub-second part as `PX`. Zero, negative or non-numeric values raise before anything is sent.
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
r.incr("rate:user:7", ttl=60) # the 60 s window starts on the first call
|
|
126
|
+
r.incrby("quota:day", 1, max=100, ttl=86400) # counter with a ceiling and a daily window
|
|
127
|
+
r.zadd("recent", {"a": 1}, ttl=timedelta(hours=1))
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`expire`, `pexpire`, `expireat` and `pexpireat` take `condition=` (`ExpireCondition.NX`, `XX`, `GT` or `LT`, or the string). A key without a deadline counts as infinite; a failed condition returns `False`.
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
r.expire("session", 3600, condition=ExpireCondition.NX) # only when the key has no deadline yet
|
|
134
|
+
```
|
|
135
|
+
|
|
96
136
|
### Pipelines and transactions
|
|
97
137
|
|
|
98
138
|
```python
|
|
@@ -138,6 +178,8 @@ to `on_push=callback` and never reach a command's reply.
|
|
|
138
178
|
| `r.lease_scope(key, ttl_ms)` | `LEASE` / `RELEASE` | Yields the fencing token and releases it on exit |
|
|
139
179
|
| `r.semaphore_scope(key, max, ttl_ms)` | `SEMAPHORE` / `RELEASE` | Raises `LockError` when full |
|
|
140
180
|
| `r.rate_limit(key, max, window_ms)` | `LIMIT` | `(allowed, remaining)` |
|
|
181
|
+
| `r.limit_sliding(key, max, window_ms)`, `r.limit_tokens(key, capacity, refill_ms, cost=1)` | `LIMIT ... SLIDING`, `LIMIT ... TOKENS` | remaining, or `None` |
|
|
182
|
+
| `r.limit_sliding_with_retry(...)`, `r.limit_tokens_with_retry(...)` | `LIMIT ... WITHRETRY` | `(allowed, remaining, retry_after_ms)` |
|
|
141
183
|
| `r.get_or_create(key, factory, ttl_ms, fill_ms=…, swr_ms=…)` | `FILLGET` / `FILLSET` | Exactly one caller runs `factory` (sync or async). Waiters poll plain `FILLGET` with backoff from 5 ms up to 100 ms instead of `WAIT`, which would park the shared connection |
|
|
142
184
|
| `r.idempotent(key, fingerprint, ttl_ms, fn)` | `IDEM BEGIN` / `COMPLETE` | `("executed", result)`, `("completed", stored)`, `("pending", None)` or `("conflict", None)` |
|
|
143
185
|
| `r.scan_iter()` / `hscan_iter` / `sscan_iter` / `zscan_iter` | `SCAN` family | Lazy and driven by the cursor; async iterators on the asyncio client |
|
|
@@ -241,6 +283,13 @@ Behaviour worth knowing:
|
|
|
241
283
|
the table points that one slot at it; a node that is not in the table, or a second `MOVED` for
|
|
242
284
|
the same command, re-reads `CLUSTER SLOTS`. `MOVED` means the command was not executed, so
|
|
243
285
|
following it is safe.
|
|
286
|
+
- **`-ASK`**: `ASKING` and the command are sent in one write to the node the reply names (a node
|
|
287
|
+
that is not in the table gets a connection), and its reply is returned. The slot table is not
|
|
288
|
+
changed, so the next command for that slot still goes to the table's owner. `ASK` and `MOVED`
|
|
289
|
+
share the budget of 3 redirects per command; an error reply to `ASKING` is the result.
|
|
290
|
+
- **`-TRYAGAIN`**: the command is sent again, routed by the current table, at most **7 times**,
|
|
291
|
+
after 5, 10, 20, 40, 80, 160 and 320 ms (635 ms in all; `asyncio.sleep` in the asyncio client); an
|
|
292
|
+
eighth `TRYAGAIN` is raised. Redirects during the retries are followed with their own budget.
|
|
244
293
|
- **No replay after transport failures.** A dropped node socket fails the command that saw it
|
|
245
294
|
(`ConnectionError`), is closed, and is reopened by the next command for that node. Other nodes
|
|
246
295
|
are not affected. A pipeline/split call that touched several nodes still reads every healthy
|
|
@@ -273,6 +322,116 @@ Short benchmark (`RUVIO_TEST_ADDR` plain 1-shard server vs `RUVIO_CLUSTER_ADDR`
|
|
|
273
322
|
coroutine: the node connection's pending-reply future is returned directly, and only a `MOVED`
|
|
274
323
|
reply is intercepted.
|
|
275
324
|
|
|
325
|
+
## Reads from replicas
|
|
326
|
+
|
|
327
|
+
`read_policy=` on a cluster client (sync and asyncio) sends reads to a replica of the key's shard. It takes
|
|
328
|
+
`ReadPolicy.PRIMARY` (default: nothing changes), `ReadPolicy.PREFER_REPLICA` or `ReadPolicy.REPLICA`, or the
|
|
329
|
+
strings `"primary"`, `"prefer-replica"` and `"replica"`. A standalone server ignores it.
|
|
330
|
+
|
|
331
|
+
```python
|
|
332
|
+
from ruvio import ClusterRuvio, ReadPolicy
|
|
333
|
+
|
|
334
|
+
client = ClusterRuvio("127.0.0.1", 7000, read_policy=ReadPolicy.PREFER_REPLICA)
|
|
335
|
+
client.set("user:1", "Ada") # primary
|
|
336
|
+
client.get("user:1") # a replica; may not have the write yet
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
- Replication is asynchronous: a read from a replica may miss a recent write, including your own.
|
|
340
|
+
- A read is a command the server flags read-only and not blocking (`GET`, `MGET`, `EXISTS`, `HGETALL`,
|
|
341
|
+
`ZRANGE` ...). Writes, keyless commands, transactions (`MULTI`/`EXEC`/`WATCH`), blocking commands, scripts,
|
|
342
|
+
Pub/Sub, `SCAN` and the commands sent to every node stay on the primaries. A multi-key command split over
|
|
343
|
+
several slots decides per part: `MGET` and `EXISTS` parts read from replicas, `DEL` and `MSET` parts go to primaries.
|
|
344
|
+
- A pipeline goes to replicas only when every command in it is a read; a pipeline with a write is routed as before.
|
|
345
|
+
- When the slot table loads, the client picks one replica per shard at random and keeps it until the next load.
|
|
346
|
+
Each replica connection sends `READONLY` right after it opens. With the default policy no replica connection
|
|
347
|
+
is opened and routing does one extra comparison.
|
|
348
|
+
- A replica that cannot be connected, or answers `LOADING` (still copying its primary), is skipped until the
|
|
349
|
+
next slot table load. `PREFER_REPLICA` then reads from the primary (a `LOADING` read is sent again there);
|
|
350
|
+
`REPLICA` raises (`ConnectionError`, or the `LOADING` `ResponseError`), and a `RuvioError` saying "no replica"
|
|
351
|
+
when none is known. A socket that drops after a command was sent fails the command, as for every node.
|
|
352
|
+
|
|
353
|
+
## Durable replies
|
|
354
|
+
|
|
355
|
+
`durability_replicas=n` (and optionally `durability_timeout_ms=`) on `Ruvio`, `asyncio.Ruvio` and the cluster
|
|
356
|
+
clients sends `CLIENT DURABILITY REPLICAS n [TIMEOUT ms]` right after the handshake of every connection,
|
|
357
|
+
reconnects included. The server then holds each reply until `n` replicas have the writes on disk; on a timeout
|
|
358
|
+
(default 5000 ms) the replies of that batch are `-NOREPLICAS` errors and the writes stay applied. A server
|
|
359
|
+
without replication refuses it and the connect fails. Without the option nothing is sent.
|
|
360
|
+
`client.client_durability(n, timeout_ms=None)` sets it on the current connection only (`0` turns it off).
|
|
361
|
+
|
|
362
|
+
```python
|
|
363
|
+
r = Ruvio("127.0.0.1", 6379, durability_replicas=1, durability_timeout_ms=5000)
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
## Seed addresses
|
|
367
|
+
|
|
368
|
+
`seeds=` takes the place of `host`/`port` (both clients, plain and `cluster=True`): `"host:port"` strings
|
|
369
|
+
(IPv6 as `"[::1]:6379"`) or `(host, port)` tuples. Without it nothing changes.
|
|
370
|
+
|
|
371
|
+
```python
|
|
372
|
+
from ruvio import Ruvio
|
|
373
|
+
|
|
374
|
+
client = Ruvio(seeds=["10.0.0.1:6379", ("10.0.0.2", 6379), "[::1]:6379"])
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
- Connecting tries the seeds in order, one connect attempt each, and keeps the first that answers. A seed
|
|
378
|
+
that cannot be reached is skipped; a refused `AUTH` or `SELECT` raises at once. When none answers, the
|
|
379
|
+
last `ConnectionError` is raised.
|
|
380
|
+
- After a dropped connection the command that hit the drop still raises. The next reconnect tries every seed
|
|
381
|
+
once, starting with the seed after the one that dropped and ending with it. A `pubsub()` session opened
|
|
382
|
+
later starts at the address the command connection is on.
|
|
383
|
+
- With `cluster=True`, `CLUSTER SLOTS` comes from the first reachable seed. A table reload asks the nodes that
|
|
384
|
+
have an open socket first; when none answers it opens every other known node and then every seed, one
|
|
385
|
+
connect attempt each, until one returns a table. A node that cannot be opened triggers that reload and the
|
|
386
|
+
command goes to the owner the new table names (a keyless command to the first reachable node of the new
|
|
387
|
+
table). Pipelines, fan-out commands and transactions do not trigger the reload on their own; they fail as
|
|
388
|
+
before and the next single-key or keyless command reloads. Seeds are never asked while the known nodes answer.
|
|
389
|
+
|
|
390
|
+
## Following a failover
|
|
391
|
+
|
|
392
|
+
A standalone client with two or more `seeds`, or with `sentinel_master`, finds the primary again after a
|
|
393
|
+
failover instead of staying on a node that is now a replica. It is on by default (`follow_primary=True`), in
|
|
394
|
+
both clients, and does nothing with one seed and no `sentinel_master`. A cluster client keeps its
|
|
395
|
+
`MOVED`/`ASK` rules and does not use it. The contract is in [docs/CLIENT_BEHAVIOR.md](../../docs/CLIENT_BEHAVIOR.md).
|
|
396
|
+
|
|
397
|
+
```python
|
|
398
|
+
client = Ruvio(seeds=["10.0.0.1:6379", "10.0.0.2:6379"]) # asks ROLE
|
|
399
|
+
client = Ruvio(seeds=["10.0.0.9:26379"], sentinel_master="main") # asks the coordinator
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
- **Reconnect.** After a dropped connection the next connect asks the seeds (starting with the one after the one
|
|
403
|
+
that dropped) which node answers `ROLE` with `master`, following the primary address a replica reports, and
|
|
404
|
+
connects to it. When none is found, or it cannot be opened, the seed order above applies. The command that hit
|
|
405
|
+
the drop still raises. The first connect keeps the seed order; with `sentinel_master` it asks the coordinators
|
|
406
|
+
(`SENTINEL get-master-addr-by-name`, confirmed with `ROLE`) and raises `ProtocolError` when none names a primary.
|
|
407
|
+
- **`READONLY`.** A command answered `READONLY` (the text is `READONLY` alone or followed by a space) makes the
|
|
408
|
+
client search for the primary, up to 8 rounds with waits of 100 ms doubling to 2 s (7.1 s in all; the asyncio
|
|
409
|
+
client waits without blocking other tasks), move to it and send the refused command once more. The node that
|
|
410
|
+
answered is skipped only while it still says `master`; once it says it is a replica, a later round takes it when
|
|
411
|
+
it is promoted. The retry's reply
|
|
412
|
+
goes to the caller, a second `READONLY` included; there is never a third send. When no primary is found the
|
|
413
|
+
`READONLY` error is raised and the connection stays where it was.
|
|
414
|
+
- **Not resent.** A command inside a transaction opened with `multi()` (the connection still moves, so the next
|
|
415
|
+
command works), a command of a pipeline (its `READONLY` is that command's result), and any other error.
|
|
416
|
+
- **Cost.** A healthy connection sends nothing extra; the error text of a reply is looked at only where error
|
|
417
|
+
replies are already inspected. The only extra sockets are short probes (`AUTH` as configured, one command, one
|
|
418
|
+
reply, bounded by the smaller of `connect_timeout` and `socket_timeout`). While following is active the asyncio
|
|
419
|
+
client keeps each command's encoded frame on its pending entry (a reference, no copy); command methods still
|
|
420
|
+
return the connection's future, and a `READONLY` reply is answered from the reader by one shared task.
|
|
421
|
+
- `follow_primary=False` restores the plain seed behavior: `READONLY` is returned as an error and no `ROLE` is
|
|
422
|
+
sent. The Python clients have no connection-lost or connection-restored hooks, so nothing is reported.
|
|
423
|
+
|
|
424
|
+
## TLS
|
|
425
|
+
|
|
426
|
+
Plain TCP is the default. `tls=True` wraps every socket the client opens (the sync and the asyncio client, cluster nodes, replicas, Pub/Sub and failover probes) in TLS. The server certificate chain and host name are verified against the system roots.
|
|
427
|
+
|
|
428
|
+
```python
|
|
429
|
+
client = ruvio.Ruvio("cache.internal", 6379, tls=True, tls_ca_file="ca.pem")
|
|
430
|
+
client = ruvio.Ruvio.from_url("ruvios://cache.internal:6379") # rediss:// is the same
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
`tls_ca_file` (PEM) trusts your private CA instead of the system roots. `tls_cert_file` and `tls_key_file` (PEM) answer a server that demands mutual TLS (`tls_client_auth`). `tls_server_name` is the name the certificate carries when you connect by IP address. Pass a ready `ssl.SSLContext` as `tls=` to control everything yourself. `tls_insecure_skip_verify=True` accepts any certificate and skips the host name check, which removes the protection against someone in the middle: use it for a throwaway test only. The handshake is part of the connect (bounded by `connect_timeout`), so a refused certificate raises `ruvio.ConnectionError`. A TLS option without `tls=True`, or `tls` with `unix_socket_path`, raises `ValueError`. Measured: the live TLS tests (`tests/test_tls_live.py`, sync and asyncio); the latency TLS adds: not measured.
|
|
434
|
+
|
|
276
435
|
## Tests
|
|
277
436
|
|
|
278
437
|
```bash
|
|
@@ -292,7 +451,7 @@ RUVIO_TEST_FLUSHALL=1 ... # also r
|
|
|
292
451
|
has no typed method on the sync or the async client. It also fails when a typed method is
|
|
293
452
|
never called by a live test.
|
|
294
453
|
- `tests/test_cluster_unit.py` needs no Ruvio server: slot vectors and hashtags, key
|
|
295
|
-
extraction through the typed methods, a check that **all
|
|
454
|
+
extraction through the typed methods, a check that **all 299 matrix commands have a routing
|
|
296
455
|
rule**, topology / `MOVED` / scan-cursor parsing, and the router against scripted fake servers
|
|
297
456
|
(`tests/fakecluster.py`) on both clients: owner-only routing, fail-fast `CROSSSLOT` (no byte
|
|
298
457
|
sent), split reassembly order, `MOVED` patch / refresh / unknown node / at most 3 redirects, no
|
|
@@ -308,6 +467,15 @@ RUVIO_TEST_FLUSHALL=1 ... # also r
|
|
|
308
467
|
`BLPOP`, pub/sub and sharded pub/sub, losing one node's connection. The standalone fallback
|
|
309
468
|
test uses `RUVIO_TEST_ADDR` (plain server). Cluster mode has only database 0, so the older
|
|
310
469
|
live suites (database 11) are not run on the cluster.
|
|
470
|
+
- `tests/test_replica_reads_live.py` needs `RUVIO_REPLICATED_ADDR` (a 2-shard primary) and `RUVIO_REPLICA_ADDR` (its
|
|
471
|
+
2-shard replica, shard i on port + i); `scripts/run_with_servers.sh` starts both. Without them it is skipped.
|
|
472
|
+
- `tests/test_multi_node_live.py` needs `RUVIO_MULTINODE_A_ADDR`, `RUVIO_MULTINODE_B_ADDR` and
|
|
473
|
+
`RUVIO_MULTINODE_STALE_ADDR` (a two-node cluster and a node with an older map, started by
|
|
474
|
+
`scripts/run_with_servers.sh`) and checks that the client follows `MOVED`. Without them it is skipped.
|
|
475
|
+
- `tests/test_failover_live.py` needs `RUVIO_FAILOVER_ADDRS` and `RUVIO_FAILOVER_REPLICATION` (a primary and a replica, comma
|
|
476
|
+
separated, primary first, and their replication addresses); `scripts/run_with_servers.sh` starts them. It swaps the roles
|
|
477
|
+
with `REPLICAOF` and checks that the client follows, that `follow_primary=False` gets `READONLY`, and that a reconnect lands
|
|
478
|
+
on the primary. Without them it is skipped. `tests/test_follow_failover.py` covers the rules against scripted nodes.
|
|
311
479
|
- Without `RUVIO_TEST_ADDR` the live tests are skipped. Live tests use database 11 and run
|
|
312
480
|
`FLUSHDB`. `test_keys` also uses database 12. `JSON.INDEX` only works in database 0, so
|
|
313
481
|
that test uses unique keys there and cleans up after itself.
|
|
@@ -320,8 +488,9 @@ RUVIO_TEST_FLUSHALL=1 ... # also r
|
|
|
320
488
|
`MULTI`/`EXEC` sent one by one are refused (use `pipeline(transaction=True)`); a
|
|
321
489
|
cross-slot `RENAME`/`COPY`/`SMOVE`/`LMOVE`/... is refused rather than emulated by
|
|
322
490
|
copying; `CLUSTER NODES/SHARDS/INFO/MYID` and `CHANGES` go to the seed only; `SCRIPT EXISTS`
|
|
323
|
-
and `SCRIPT KILL` ask the seed only;
|
|
324
|
-
|
|
491
|
+
and `SCRIPT KILL` ask the seed only; `ASK` and `TRYAGAIN` are handled
|
|
492
|
+
as described above, but only against scripted servers, since the server does not
|
|
493
|
+
migrate slots across nodes; no replica reads (`READONLY` is sent to the seed only); a cluster whose
|
|
325
494
|
advertised host is unreachable from the client is not handled. Not verified: a server
|
|
326
495
|
topology that changes while the client runs (`MOVED` recovery was verified by corrupting the
|
|
327
496
|
client's table, not by moving slots on a server), clusters with more than 3 nodes, AUTH on
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "ruvio-client"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.3.dev33"
|
|
8
8
|
description = "Fast sync and asyncio Python client for the Ruvio key-value server (RESP2/RESP3)"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = { text = "MIT" }
|