-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathredis_helper.py
492 lines (447 loc) · 15.7 KB
/
redis_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
from __future__ import annotations
import asyncio
import inspect
import logging
import socket
from typing import (
Any,
AsyncIterator,
Awaitable,
Callable,
Dict,
Mapping,
MutableMapping,
Optional,
Sequence,
Tuple,
Union,
cast,
)
import redis.exceptions
import yarl
from redis.asyncio import Redis
from redis.asyncio.client import Pipeline, PubSub
from redis.asyncio.sentinel import MasterNotFoundError, Sentinel, SlaveNotFoundError
from redis.backoff import ExponentialBackoff
from redis.retry import Retry
from .logging import BraceStyleAdapter
from .types import EtcdRedisConfig, RedisConnectionInfo, RedisHelperConfig
from .validators import DelimiterSeperatedList, HostPortPair
__all__ = (
"execute",
"subscribe",
"blpop",
"read_stream",
"read_stream_by_group",
"get_redis_object",
)
_keepalive_options: MutableMapping[int, int] = {}
# macOS does not support several TCP_ options
# so check if socket package includes TCP options before adding it
if (_TCP_KEEPIDLE := getattr(socket, "TCP_KEEPIDLE", None)) is not None:
_keepalive_options[_TCP_KEEPIDLE] = 20
if (_TCP_KEEPINTVL := getattr(socket, "TCP_KEEPINTVL", None)) is not None:
_keepalive_options[_TCP_KEEPINTVL] = 5
if (_TCP_KEEPCNT := getattr(socket, "TCP_KEEPCNT", None)) is not None:
_keepalive_options[_TCP_KEEPCNT] = 3
_default_conn_opts: Mapping[str, Any] = {
"socket_keepalive": True,
"socket_keepalive_options": _keepalive_options,
"retry": Retry(ExponentialBackoff(), 10),
"retry_on_error": [
redis.exceptions.ConnectionError,
redis.exceptions.TimeoutError,
],
}
_scripts: Dict[str, str] = {}
log = BraceStyleAdapter(logging.getLogger(__spec__.name)) # type: ignore[name-defined]
class ConnectionNotAvailable(Exception):
pass
def _parse_stream_msg_id(msg_id: bytes) -> Tuple[int, int]:
timestamp, _, sequence = msg_id.partition(b"-")
return int(timestamp), int(sequence)
async def subscribe(channel: PubSub, *, reconnect_poll_interval: float = 0.3) -> AsyncIterator[Any]:
"""
An async-generator wrapper for pub-sub channel subscription.
It automatically recovers from server shutdowns until explicitly cancelled.
"""
async def _reset_chan():
channel.connection = None
try:
await channel.ping()
except redis.exceptions.ConnectionError:
pass
else:
assert channel.connection is not None
await channel.on_connect(channel.connection)
while True:
try:
if not channel.connection:
raise ConnectionNotAvailable
message = await channel.get_message(ignore_subscribe_messages=True, timeout=10.0)
if message is not None:
yield message["data"]
except (
redis.exceptions.ConnectionError,
MasterNotFoundError,
SlaveNotFoundError,
redis.exceptions.ReadOnlyError,
ConnectionResetError,
ConnectionNotAvailable,
):
await asyncio.sleep(reconnect_poll_interval)
await _reset_chan()
continue
except redis.exceptions.ResponseError as e:
if len(e.args) > 0 and e.args[0].startswith("NOREPLICAS "):
await asyncio.sleep(reconnect_poll_interval)
await _reset_chan()
continue
raise
except (redis.exceptions.TimeoutError, asyncio.TimeoutError):
continue
except asyncio.CancelledError:
raise
finally:
await asyncio.sleep(0)
async def blpop(
redis_obj: RedisConnectionInfo,
key: str,
*,
service_name: str = None,
) -> AsyncIterator[Any]:
"""
An async-generator wrapper for blpop (blocking left pop).
It automatically recovers from server shutdowns until explicitly cancelled.
"""
redis_client = redis_obj.client
service_name = service_name or redis_obj.service_name
reconnect_poll_interval = float(
cast(str, redis_obj.redis_helper_config.get("reconnect_poll_timeout"))
)
while True:
try:
raw_msg = await redis_client.blpop(key, timeout=10.0)
if not raw_msg:
continue
yield raw_msg[1]
except (
redis.exceptions.ConnectionError,
MasterNotFoundError,
SlaveNotFoundError,
redis.exceptions.ReadOnlyError,
ConnectionResetError,
):
await asyncio.sleep(reconnect_poll_interval)
continue
except redis.exceptions.ResponseError as e:
if e.args[0].startswith("NOREPLICAS "):
await asyncio.sleep(reconnect_poll_interval)
continue
raise
except (redis.exceptions.TimeoutError, asyncio.TimeoutError):
continue
except asyncio.CancelledError:
raise
finally:
await asyncio.sleep(0)
async def execute(
redis_obj: RedisConnectionInfo,
func: Callable[[Redis], Awaitable[Any]],
*,
service_name: str = None,
encoding: Optional[str] = None,
) -> Any:
"""
Executes a function that issues Redis commands or returns a pipeline/transaction of commands,
with automatic retries upon temporary connection failures.
Note that when retried, the given function may be executed *multiple* times, so the caller
should take care of side-effects of it.
"""
redis_client = redis_obj.client
service_name = service_name or redis_obj.service_name
reconnect_poll_interval = float(
cast(str, redis_obj.redis_helper_config.get("reconnect_poll_timeout"))
)
while True:
try:
async with redis_client:
if callable(func):
aw_or_pipe = func(redis_client)
else:
raise TypeError(
"The func must be a function or a coroutinefunction with no arguments."
)
if isinstance(aw_or_pipe, Pipeline):
async with aw_or_pipe:
result = await aw_or_pipe.execute()
elif inspect.isawaitable(aw_or_pipe):
result = await aw_or_pipe
else:
raise TypeError(
"The return value must be an awaitable"
"or redis.asyncio.client.Pipeline object"
)
if isinstance(result, Pipeline):
# This happens when func is an async function that returns a pipeline.
async with result:
result = await result.execute()
if encoding:
if isinstance(result, bytes):
return result.decode(encoding)
elif isinstance(result, dict):
newdict = {}
for k, v in result.items():
newdict[k.decode(encoding)] = v.decode(encoding)
return newdict
else:
return result
except (
MasterNotFoundError,
SlaveNotFoundError,
redis.exceptions.ReadOnlyError,
ConnectionResetError,
):
await asyncio.sleep(reconnect_poll_interval)
continue
except redis.exceptions.ConnectionError as e:
log.error(f"execute(): Connecting to redis failed: {e}")
await asyncio.sleep(reconnect_poll_interval)
continue
except redis.exceptions.ResponseError as e:
if "NOREPLICAS" in e.args[0]:
await asyncio.sleep(reconnect_poll_interval)
continue
raise
except (redis.exceptions.TimeoutError, asyncio.TimeoutError):
continue
except asyncio.CancelledError:
raise
finally:
await asyncio.sleep(0)
async def execute_script(
redis_obj: RedisConnectionInfo,
script_id: str,
script: str,
keys: Sequence[str],
args: Sequence[
Union[bytes, memoryview, str, int, float]
], # redis.asyncio.connection.EncodableT
) -> Any:
"""
Auto-load and execute the given script.
It uses the hash keys for scripts so that it does not send the whole
script every time but only at the first time.
Args:
conn: A Redis connection or pool with the commands mixin.
script_id: A human-readable identifier for the script.
This can be arbitrary string but must be unique for each script.
script: The script content.
keys: The Redis keys that will be passed to the script.
args: The arguments that will be passed to the script.
"""
script_hash = _scripts.get(script_id, "x")
while True:
try:
ret = await execute(
redis_obj,
lambda r: r.evalsha(
script_hash,
len(keys),
*keys,
*args,
),
)
break
except redis.exceptions.NoScriptError:
# Redis may have been restarted.
script_hash = await execute(redis_obj, lambda r: r.script_load(script))
_scripts[script_id] = script_hash
except redis.exceptions.ResponseError as e:
if "NOSCRIPT" in e.args[0]:
# Redis may have been restarted.
script_hash = await execute(redis_obj, lambda r: r.script_load(script))
_scripts[script_id] = script_hash
else:
raise
continue
return ret
async def read_stream(
r: RedisConnectionInfo,
stream_key: str,
*,
block_timeout: int = 10_000, # in msec
) -> AsyncIterator[Tuple[bytes, bytes]]:
"""
A high-level wrapper for the XREAD command.
"""
last_id = b"$"
while True:
try:
reply = await execute(
r,
lambda r: r.xread(
{stream_key: last_id},
block=block_timeout,
),
)
if not reply:
continue
# Keep some latest messages so that other manager
# processes to have chances of fetching them.
await execute(
r,
lambda r: r.xtrim(
stream_key,
maxlen=128,
approximate=True,
),
)
for msg_id, msg_data in reply[0][1]:
try:
yield msg_id, msg_data
finally:
last_id = msg_id
except asyncio.CancelledError:
raise
async def read_stream_by_group(
r: RedisConnectionInfo,
stream_key: str,
group_name: str,
consumer_id: str,
*,
autoclaim_idle_timeout: int = 1_000, # in msec
block_timeout: int = 10_000, # in msec
) -> AsyncIterator[Tuple[bytes, bytes]]:
"""
A high-level wrapper for the XREADGROUP command
combined with XAUTOCLAIM and XGROUP_CREATE.
"""
while True:
try:
messages = []
autoclaim_start_id = b"0-0"
while True:
reply = await execute(
r,
lambda r: r.execute_command(
"XAUTOCLAIM",
stream_key,
group_name,
consumer_id,
str(autoclaim_idle_timeout),
autoclaim_start_id,
),
)
for msg_id, msg_data in reply[1]:
messages.append((msg_id, msg_data))
if reply[0] == b"0-0":
break
autoclaim_start_id = reply[0]
reply = await execute(
r,
lambda r: r.xreadgroup(
group_name,
consumer_id,
{stream_key: b">"}, # fetch messages not seen by other consumers
block=block_timeout,
),
)
if len(reply) == 0:
continue
assert reply[0][0].decode() == stream_key
for msg_id, msg_data in reply[0][1]:
messages.append((msg_id, msg_data))
await execute(
r,
lambda r: r.xack(
stream_key,
group_name,
*(msg_id for msg_id, msg_data in reply[0][1]),
),
)
for msg_id, msg_data in messages:
yield msg_id, msg_data
except asyncio.CancelledError:
raise
except redis.exceptions.ResponseError as e:
if e.args[0].startswith("NOGROUP "):
try:
await execute(
r,
lambda r: r.xgroup_create(
stream_key,
group_name,
"$",
mkstream=True,
),
)
except redis.exceptions.ResponseError as e:
if e.args[0].startswith("BUSYGROUP "):
pass
else:
raise
continue
raise
def get_redis_object(
redis_config: EtcdRedisConfig,
db: int = 0,
**kwargs,
) -> RedisConnectionInfo:
redis_helper_config: RedisHelperConfig = cast(
RedisHelperConfig, redis_config.get("redis_helper_config")
)
if _sentinel_addresses := redis_config.get("sentinel"):
sentinel_addresses: Any = None
if isinstance(_sentinel_addresses, str):
sentinel_addresses = DelimiterSeperatedList(HostPortPair).check_and_return(
_sentinel_addresses
)
else:
sentinel_addresses = _sentinel_addresses
service_name = redis_config.get("service_name")
password = redis_config.get("password")
assert (
service_name is not None
), "config/redis/service_name is required when using Redis Sentinel"
sentinel = Sentinel(
[(str(host), port) for host, port in sentinel_addresses],
password=password,
db=str(db),
sentinel_kwargs={
"password": password,
**kwargs,
},
)
conn_opts = {
**_default_conn_opts,
**kwargs,
"socket_timeout": float(cast(str, redis_helper_config.get("socket_timeout"))),
"socket_connect_timeout": float(
cast(str, redis_helper_config.get("socket_connect_timeout"))
),
}
return RedisConnectionInfo(
client=sentinel.master_for(service_name=service_name, password=password, **conn_opts),
sentinel=sentinel,
service_name=service_name,
redis_helper_config=redis_helper_config,
)
else:
redis_url = redis_config.get("addr")
assert redis_url is not None
url = yarl.URL("redis://host").with_host(str(redis_url[0])).with_port(
redis_url[1]
).with_password(redis_config.get("password")) / str(db)
return RedisConnectionInfo(
client=Redis.from_url(str(url), **kwargs),
sentinel=None,
service_name=None,
redis_helper_config=redis_helper_config,
)
async def ping_redis_connection(redis_client: Redis) -> bool:
try:
return await redis_client.ping()
except (redis.exceptions.ConnectionError, redis.exceptions.TimeoutError) as e:
log.exception(f"ping_redis_connection(): Connecting to redis failed: {e}")
raise e