cluster.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. import asyncio
  2. from typing import (
  3. TYPE_CHECKING,
  4. Any,
  5. AsyncIterator,
  6. Dict,
  7. Iterable,
  8. Iterator,
  9. List,
  10. Literal,
  11. Mapping,
  12. NoReturn,
  13. Optional,
  14. Union,
  15. )
  16. from redis.crc import key_slot
  17. from redis.exceptions import RedisClusterException, RedisError
  18. from redis.typing import (
  19. AnyKeyT,
  20. ClusterCommandsProtocol,
  21. EncodableT,
  22. KeysT,
  23. KeyT,
  24. PatternT,
  25. ResponseT,
  26. )
  27. from .core import (
  28. ACLCommands,
  29. AsyncACLCommands,
  30. AsyncDataAccessCommands,
  31. AsyncFunctionCommands,
  32. AsyncManagementCommands,
  33. AsyncModuleCommands,
  34. AsyncScriptCommands,
  35. DataAccessCommands,
  36. FunctionCommands,
  37. ManagementCommands,
  38. ModuleCommands,
  39. PubSubCommands,
  40. ScriptCommands,
  41. )
  42. from .helpers import list_or_args
  43. from .redismodules import AsyncRedisModuleCommands, RedisModuleCommands
  44. if TYPE_CHECKING:
  45. from redis.asyncio.cluster import TargetNodesT
  46. # Not complete, but covers the major ones
  47. # https://redis.io/commands
  48. READ_COMMANDS = frozenset(
  49. [
  50. "BITCOUNT",
  51. "BITPOS",
  52. "EVAL_RO",
  53. "EVALSHA_RO",
  54. "EXISTS",
  55. "GEODIST",
  56. "GEOHASH",
  57. "GEOPOS",
  58. "GEORADIUS",
  59. "GEORADIUSBYMEMBER",
  60. "GET",
  61. "GETBIT",
  62. "GETRANGE",
  63. "HEXISTS",
  64. "HGET",
  65. "HGETALL",
  66. "HKEYS",
  67. "HLEN",
  68. "HMGET",
  69. "HSTRLEN",
  70. "HVALS",
  71. "KEYS",
  72. "LINDEX",
  73. "LLEN",
  74. "LRANGE",
  75. "MGET",
  76. "PTTL",
  77. "RANDOMKEY",
  78. "SCARD",
  79. "SDIFF",
  80. "SINTER",
  81. "SISMEMBER",
  82. "SMEMBERS",
  83. "SRANDMEMBER",
  84. "STRLEN",
  85. "SUNION",
  86. "TTL",
  87. "ZCARD",
  88. "ZCOUNT",
  89. "ZRANGE",
  90. "ZSCORE",
  91. ]
  92. )
  93. class ClusterMultiKeyCommands(ClusterCommandsProtocol):
  94. """
  95. A class containing commands that handle more than one key
  96. """
  97. def _partition_keys_by_slot(self, keys: Iterable[KeyT]) -> Dict[int, List[KeyT]]:
  98. """Split keys into a dictionary that maps a slot to a list of keys."""
  99. slots_to_keys = {}
  100. for key in keys:
  101. slot = key_slot(self.encoder.encode(key))
  102. slots_to_keys.setdefault(slot, []).append(key)
  103. return slots_to_keys
  104. def _partition_pairs_by_slot(
  105. self, mapping: Mapping[AnyKeyT, EncodableT]
  106. ) -> Dict[int, List[EncodableT]]:
  107. """Split pairs into a dictionary that maps a slot to a list of pairs."""
  108. slots_to_pairs = {}
  109. for pair in mapping.items():
  110. slot = key_slot(self.encoder.encode(pair[0]))
  111. slots_to_pairs.setdefault(slot, []).extend(pair)
  112. return slots_to_pairs
  113. def _execute_pipeline_by_slot(
  114. self, command: str, slots_to_args: Mapping[int, Iterable[EncodableT]]
  115. ) -> List[Any]:
  116. read_from_replicas = self.read_from_replicas and command in READ_COMMANDS
  117. pipe = self.pipeline()
  118. [
  119. pipe.execute_command(
  120. command,
  121. *slot_args,
  122. target_nodes=[
  123. self.nodes_manager.get_node_from_slot(slot, read_from_replicas)
  124. ],
  125. )
  126. for slot, slot_args in slots_to_args.items()
  127. ]
  128. return pipe.execute()
  129. def _reorder_keys_by_command(
  130. self,
  131. keys: Iterable[KeyT],
  132. slots_to_args: Mapping[int, Iterable[EncodableT]],
  133. responses: Iterable[Any],
  134. ) -> List[Any]:
  135. results = {
  136. k: v
  137. for slot_values, response in zip(slots_to_args.values(), responses)
  138. for k, v in zip(slot_values, response)
  139. }
  140. return [results[key] for key in keys]
  141. def mget_nonatomic(self, keys: KeysT, *args: KeyT) -> List[Optional[Any]]:
  142. """
  143. Splits the keys into different slots and then calls MGET
  144. for the keys of every slot. This operation will not be atomic
  145. if keys belong to more than one slot.
  146. Returns a list of values ordered identically to ``keys``
  147. For more information see https://redis.io/commands/mget
  148. """
  149. # Concatenate all keys into a list
  150. keys = list_or_args(keys, args)
  151. # Split keys into slots
  152. slots_to_keys = self._partition_keys_by_slot(keys)
  153. # Execute commands using a pipeline
  154. res = self._execute_pipeline_by_slot("MGET", slots_to_keys)
  155. # Reorder keys in the order the user provided & return
  156. return self._reorder_keys_by_command(keys, slots_to_keys, res)
  157. def mset_nonatomic(self, mapping: Mapping[AnyKeyT, EncodableT]) -> List[bool]:
  158. """
  159. Sets key/values based on a mapping. Mapping is a dictionary of
  160. key/value pairs. Both keys and values should be strings or types that
  161. can be cast to a string via str().
  162. Splits the keys into different slots and then calls MSET
  163. for the keys of every slot. This operation will not be atomic
  164. if keys belong to more than one slot.
  165. For more information see https://redis.io/commands/mset
  166. """
  167. # Partition the keys by slot
  168. slots_to_pairs = self._partition_pairs_by_slot(mapping)
  169. # Execute commands using a pipeline & return list of replies
  170. return self._execute_pipeline_by_slot("MSET", slots_to_pairs)
  171. def _split_command_across_slots(self, command: str, *keys: KeyT) -> int:
  172. """
  173. Runs the given command once for the keys
  174. of each slot. Returns the sum of the return values.
  175. """
  176. # Partition the keys by slot
  177. slots_to_keys = self._partition_keys_by_slot(keys)
  178. # Sum up the reply from each command
  179. return sum(self._execute_pipeline_by_slot(command, slots_to_keys))
  180. def exists(self, *keys: KeyT) -> ResponseT:
  181. """
  182. Returns the number of ``names`` that exist in the
  183. whole cluster. The keys are first split up into slots
  184. and then an EXISTS command is sent for every slot
  185. For more information see https://redis.io/commands/exists
  186. """
  187. return self._split_command_across_slots("EXISTS", *keys)
  188. def delete(self, *keys: KeyT) -> ResponseT:
  189. """
  190. Deletes the given keys in the cluster.
  191. The keys are first split up into slots
  192. and then an DEL command is sent for every slot
  193. Non-existent keys are ignored.
  194. Returns the number of keys that were deleted.
  195. For more information see https://redis.io/commands/del
  196. """
  197. return self._split_command_across_slots("DEL", *keys)
  198. def touch(self, *keys: KeyT) -> ResponseT:
  199. """
  200. Updates the last access time of given keys across the
  201. cluster.
  202. The keys are first split up into slots
  203. and then an TOUCH command is sent for every slot
  204. Non-existent keys are ignored.
  205. Returns the number of keys that were touched.
  206. For more information see https://redis.io/commands/touch
  207. """
  208. return self._split_command_across_slots("TOUCH", *keys)
  209. def unlink(self, *keys: KeyT) -> ResponseT:
  210. """
  211. Remove the specified keys in a different thread.
  212. The keys are first split up into slots
  213. and then an TOUCH command is sent for every slot
  214. Non-existent keys are ignored.
  215. Returns the number of keys that were unlinked.
  216. For more information see https://redis.io/commands/unlink
  217. """
  218. return self._split_command_across_slots("UNLINK", *keys)
  219. class AsyncClusterMultiKeyCommands(ClusterMultiKeyCommands):
  220. """
  221. A class containing commands that handle more than one key
  222. """
  223. async def mget_nonatomic(self, keys: KeysT, *args: KeyT) -> List[Optional[Any]]:
  224. """
  225. Splits the keys into different slots and then calls MGET
  226. for the keys of every slot. This operation will not be atomic
  227. if keys belong to more than one slot.
  228. Returns a list of values ordered identically to ``keys``
  229. For more information see https://redis.io/commands/mget
  230. """
  231. # Concatenate all keys into a list
  232. keys = list_or_args(keys, args)
  233. # Split keys into slots
  234. slots_to_keys = self._partition_keys_by_slot(keys)
  235. # Execute commands using a pipeline
  236. res = await self._execute_pipeline_by_slot("MGET", slots_to_keys)
  237. # Reorder keys in the order the user provided & return
  238. return self._reorder_keys_by_command(keys, slots_to_keys, res)
  239. async def mset_nonatomic(self, mapping: Mapping[AnyKeyT, EncodableT]) -> List[bool]:
  240. """
  241. Sets key/values based on a mapping. Mapping is a dictionary of
  242. key/value pairs. Both keys and values should be strings or types that
  243. can be cast to a string via str().
  244. Splits the keys into different slots and then calls MSET
  245. for the keys of every slot. This operation will not be atomic
  246. if keys belong to more than one slot.
  247. For more information see https://redis.io/commands/mset
  248. """
  249. # Partition the keys by slot
  250. slots_to_pairs = self._partition_pairs_by_slot(mapping)
  251. # Execute commands using a pipeline & return list of replies
  252. return await self._execute_pipeline_by_slot("MSET", slots_to_pairs)
  253. async def _split_command_across_slots(self, command: str, *keys: KeyT) -> int:
  254. """
  255. Runs the given command once for the keys
  256. of each slot. Returns the sum of the return values.
  257. """
  258. # Partition the keys by slot
  259. slots_to_keys = self._partition_keys_by_slot(keys)
  260. # Sum up the reply from each command
  261. return sum(await self._execute_pipeline_by_slot(command, slots_to_keys))
  262. async def _execute_pipeline_by_slot(
  263. self, command: str, slots_to_args: Mapping[int, Iterable[EncodableT]]
  264. ) -> List[Any]:
  265. if self._initialize:
  266. await self.initialize()
  267. read_from_replicas = self.read_from_replicas and command in READ_COMMANDS
  268. pipe = self.pipeline()
  269. [
  270. pipe.execute_command(
  271. command,
  272. *slot_args,
  273. target_nodes=[
  274. self.nodes_manager.get_node_from_slot(slot, read_from_replicas)
  275. ],
  276. )
  277. for slot, slot_args in slots_to_args.items()
  278. ]
  279. return await pipe.execute()
  280. class ClusterManagementCommands(ManagementCommands):
  281. """
  282. A class for Redis Cluster management commands
  283. The class inherits from Redis's core ManagementCommands class and do the
  284. required adjustments to work with cluster mode
  285. """
  286. def slaveof(self, *args, **kwargs) -> NoReturn:
  287. """
  288. Make the server a replica of another instance, or promote it as master.
  289. For more information see https://redis.io/commands/slaveof
  290. """
  291. raise RedisClusterException("SLAVEOF is not supported in cluster mode")
  292. def replicaof(self, *args, **kwargs) -> NoReturn:
  293. """
  294. Make the server a replica of another instance, or promote it as master.
  295. For more information see https://redis.io/commands/replicaof
  296. """
  297. raise RedisClusterException("REPLICAOF is not supported in cluster mode")
  298. def swapdb(self, *args, **kwargs) -> NoReturn:
  299. """
  300. Swaps two Redis databases.
  301. For more information see https://redis.io/commands/swapdb
  302. """
  303. raise RedisClusterException("SWAPDB is not supported in cluster mode")
  304. def cluster_myid(self, target_node: "TargetNodesT") -> ResponseT:
  305. """
  306. Returns the node's id.
  307. :target_node: 'ClusterNode'
  308. The node to execute the command on
  309. For more information check https://redis.io/commands/cluster-myid/
  310. """
  311. return self.execute_command("CLUSTER MYID", target_nodes=target_node)
  312. def cluster_addslots(
  313. self, target_node: "TargetNodesT", *slots: EncodableT
  314. ) -> ResponseT:
  315. """
  316. Assign new hash slots to receiving node. Sends to specified node.
  317. :target_node: 'ClusterNode'
  318. The node to execute the command on
  319. For more information see https://redis.io/commands/cluster-addslots
  320. """
  321. return self.execute_command(
  322. "CLUSTER ADDSLOTS", *slots, target_nodes=target_node
  323. )
  324. def cluster_addslotsrange(
  325. self, target_node: "TargetNodesT", *slots: EncodableT
  326. ) -> ResponseT:
  327. """
  328. Similar to the CLUSTER ADDSLOTS command.
  329. The difference between the two commands is that ADDSLOTS takes a list of slots
  330. to assign to the node, while ADDSLOTSRANGE takes a list of slot ranges
  331. (specified by start and end slots) to assign to the node.
  332. :target_node: 'ClusterNode'
  333. The node to execute the command on
  334. For more information see https://redis.io/commands/cluster-addslotsrange
  335. """
  336. return self.execute_command(
  337. "CLUSTER ADDSLOTSRANGE", *slots, target_nodes=target_node
  338. )
  339. def cluster_countkeysinslot(self, slot_id: int) -> ResponseT:
  340. """
  341. Return the number of local keys in the specified hash slot
  342. Send to node based on specified slot_id
  343. For more information see https://redis.io/commands/cluster-countkeysinslot
  344. """
  345. return self.execute_command("CLUSTER COUNTKEYSINSLOT", slot_id)
  346. def cluster_count_failure_report(self, node_id: str) -> ResponseT:
  347. """
  348. Return the number of failure reports active for a given node
  349. Sends to a random node
  350. For more information see https://redis.io/commands/cluster-count-failure-reports
  351. """
  352. return self.execute_command("CLUSTER COUNT-FAILURE-REPORTS", node_id)
  353. def cluster_delslots(self, *slots: EncodableT) -> List[bool]:
  354. """
  355. Set hash slots as unbound in the cluster.
  356. It determines by it self what node the slot is in and sends it there
  357. Returns a list of the results for each processed slot.
  358. For more information see https://redis.io/commands/cluster-delslots
  359. """
  360. return [self.execute_command("CLUSTER DELSLOTS", slot) for slot in slots]
  361. def cluster_delslotsrange(self, *slots: EncodableT) -> ResponseT:
  362. """
  363. Similar to the CLUSTER DELSLOTS command.
  364. The difference is that CLUSTER DELSLOTS takes a list of hash slots to remove
  365. from the node, while CLUSTER DELSLOTSRANGE takes a list of slot ranges to remove
  366. from the node.
  367. For more information see https://redis.io/commands/cluster-delslotsrange
  368. """
  369. return self.execute_command("CLUSTER DELSLOTSRANGE", *slots)
  370. def cluster_failover(
  371. self, target_node: "TargetNodesT", option: Optional[str] = None
  372. ) -> ResponseT:
  373. """
  374. Forces a slave to perform a manual failover of its master
  375. Sends to specified node
  376. :target_node: 'ClusterNode'
  377. The node to execute the command on
  378. For more information see https://redis.io/commands/cluster-failover
  379. """
  380. if option:
  381. if option.upper() not in ["FORCE", "TAKEOVER"]:
  382. raise RedisError(
  383. f"Invalid option for CLUSTER FAILOVER command: {option}"
  384. )
  385. else:
  386. return self.execute_command(
  387. "CLUSTER FAILOVER", option, target_nodes=target_node
  388. )
  389. else:
  390. return self.execute_command("CLUSTER FAILOVER", target_nodes=target_node)
  391. def cluster_info(self, target_nodes: Optional["TargetNodesT"] = None) -> ResponseT:
  392. """
  393. Provides info about Redis Cluster node state.
  394. The command will be sent to a random node in the cluster if no target
  395. node is specified.
  396. For more information see https://redis.io/commands/cluster-info
  397. """
  398. return self.execute_command("CLUSTER INFO", target_nodes=target_nodes)
  399. def cluster_keyslot(self, key: str) -> ResponseT:
  400. """
  401. Returns the hash slot of the specified key
  402. Sends to random node in the cluster
  403. For more information see https://redis.io/commands/cluster-keyslot
  404. """
  405. return self.execute_command("CLUSTER KEYSLOT", key)
  406. def cluster_meet(
  407. self, host: str, port: int, target_nodes: Optional["TargetNodesT"] = None
  408. ) -> ResponseT:
  409. """
  410. Force a node cluster to handshake with another node.
  411. Sends to specified node.
  412. For more information see https://redis.io/commands/cluster-meet
  413. """
  414. return self.execute_command(
  415. "CLUSTER MEET", host, port, target_nodes=target_nodes
  416. )
  417. def cluster_nodes(self) -> ResponseT:
  418. """
  419. Get Cluster config for the node.
  420. Sends to random node in the cluster
  421. For more information see https://redis.io/commands/cluster-nodes
  422. """
  423. return self.execute_command("CLUSTER NODES")
  424. def cluster_replicate(
  425. self, target_nodes: "TargetNodesT", node_id: str
  426. ) -> ResponseT:
  427. """
  428. Reconfigure a node as a slave of the specified master node
  429. For more information see https://redis.io/commands/cluster-replicate
  430. """
  431. return self.execute_command(
  432. "CLUSTER REPLICATE", node_id, target_nodes=target_nodes
  433. )
  434. def cluster_reset(
  435. self, soft: bool = True, target_nodes: Optional["TargetNodesT"] = None
  436. ) -> ResponseT:
  437. """
  438. Reset a Redis Cluster node
  439. If 'soft' is True then it will send 'SOFT' argument
  440. If 'soft' is False then it will send 'HARD' argument
  441. For more information see https://redis.io/commands/cluster-reset
  442. """
  443. return self.execute_command(
  444. "CLUSTER RESET", b"SOFT" if soft else b"HARD", target_nodes=target_nodes
  445. )
  446. def cluster_save_config(
  447. self, target_nodes: Optional["TargetNodesT"] = None
  448. ) -> ResponseT:
  449. """
  450. Forces the node to save cluster state on disk
  451. For more information see https://redis.io/commands/cluster-saveconfig
  452. """
  453. return self.execute_command("CLUSTER SAVECONFIG", target_nodes=target_nodes)
  454. def cluster_get_keys_in_slot(self, slot: int, num_keys: int) -> ResponseT:
  455. """
  456. Returns the number of keys in the specified cluster slot
  457. For more information see https://redis.io/commands/cluster-getkeysinslot
  458. """
  459. return self.execute_command("CLUSTER GETKEYSINSLOT", slot, num_keys)
  460. def cluster_set_config_epoch(
  461. self, epoch: int, target_nodes: Optional["TargetNodesT"] = None
  462. ) -> ResponseT:
  463. """
  464. Set the configuration epoch in a new node
  465. For more information see https://redis.io/commands/cluster-set-config-epoch
  466. """
  467. return self.execute_command(
  468. "CLUSTER SET-CONFIG-EPOCH", epoch, target_nodes=target_nodes
  469. )
  470. def cluster_setslot(
  471. self, target_node: "TargetNodesT", node_id: str, slot_id: int, state: str
  472. ) -> ResponseT:
  473. """
  474. Bind an hash slot to a specific node
  475. :target_node: 'ClusterNode'
  476. The node to execute the command on
  477. For more information see https://redis.io/commands/cluster-setslot
  478. """
  479. if state.upper() in ("IMPORTING", "NODE", "MIGRATING"):
  480. return self.execute_command(
  481. "CLUSTER SETSLOT", slot_id, state, node_id, target_nodes=target_node
  482. )
  483. elif state.upper() == "STABLE":
  484. raise RedisError('For "stable" state please use cluster_setslot_stable')
  485. else:
  486. raise RedisError(f"Invalid slot state: {state}")
  487. def cluster_setslot_stable(self, slot_id: int) -> ResponseT:
  488. """
  489. Clears migrating / importing state from the slot.
  490. It determines by it self what node the slot is in and sends it there.
  491. For more information see https://redis.io/commands/cluster-setslot
  492. """
  493. return self.execute_command("CLUSTER SETSLOT", slot_id, "STABLE")
  494. def cluster_replicas(
  495. self, node_id: str, target_nodes: Optional["TargetNodesT"] = None
  496. ) -> ResponseT:
  497. """
  498. Provides a list of replica nodes replicating from the specified primary
  499. target node.
  500. For more information see https://redis.io/commands/cluster-replicas
  501. """
  502. return self.execute_command(
  503. "CLUSTER REPLICAS", node_id, target_nodes=target_nodes
  504. )
  505. def cluster_slots(self, target_nodes: Optional["TargetNodesT"] = None) -> ResponseT:
  506. """
  507. Get array of Cluster slot to node mappings
  508. For more information see https://redis.io/commands/cluster-slots
  509. """
  510. return self.execute_command("CLUSTER SLOTS", target_nodes=target_nodes)
  511. def cluster_shards(self, target_nodes=None):
  512. """
  513. Returns details about the shards of the cluster.
  514. For more information see https://redis.io/commands/cluster-shards
  515. """
  516. return self.execute_command("CLUSTER SHARDS", target_nodes=target_nodes)
  517. def cluster_myshardid(self, target_nodes=None):
  518. """
  519. Returns the shard ID of the node.
  520. For more information see https://redis.io/commands/cluster-myshardid/
  521. """
  522. return self.execute_command("CLUSTER MYSHARDID", target_nodes=target_nodes)
  523. def cluster_links(self, target_node: "TargetNodesT") -> ResponseT:
  524. """
  525. Each node in a Redis Cluster maintains a pair of long-lived TCP link with each
  526. peer in the cluster: One for sending outbound messages towards the peer and one
  527. for receiving inbound messages from the peer.
  528. This command outputs information of all such peer links as an array.
  529. For more information see https://redis.io/commands/cluster-links
  530. """
  531. return self.execute_command("CLUSTER LINKS", target_nodes=target_node)
  532. def cluster_flushslots(self, target_nodes: Optional["TargetNodesT"] = None) -> None:
  533. raise NotImplementedError(
  534. "CLUSTER FLUSHSLOTS is intentionally not implemented in the client."
  535. )
  536. def cluster_bumpepoch(self, target_nodes: Optional["TargetNodesT"] = None) -> None:
  537. raise NotImplementedError(
  538. "CLUSTER BUMPEPOCH is intentionally not implemented in the client."
  539. )
  540. def readonly(self, target_nodes: Optional["TargetNodesT"] = None) -> ResponseT:
  541. """
  542. Enables read queries.
  543. The command will be sent to the default cluster node if target_nodes is
  544. not specified.
  545. For more information see https://redis.io/commands/readonly
  546. """
  547. if target_nodes == "replicas" or target_nodes == "all":
  548. # read_from_replicas will only be enabled if the READONLY command
  549. # is sent to all replicas
  550. self.read_from_replicas = True
  551. return self.execute_command("READONLY", target_nodes=target_nodes)
  552. def readwrite(self, target_nodes: Optional["TargetNodesT"] = None) -> ResponseT:
  553. """
  554. Disables read queries.
  555. The command will be sent to the default cluster node if target_nodes is
  556. not specified.
  557. For more information see https://redis.io/commands/readwrite
  558. """
  559. # Reset read from replicas flag
  560. self.read_from_replicas = False
  561. return self.execute_command("READWRITE", target_nodes=target_nodes)
  562. class AsyncClusterManagementCommands(
  563. ClusterManagementCommands, AsyncManagementCommands
  564. ):
  565. """
  566. A class for Redis Cluster management commands
  567. The class inherits from Redis's core ManagementCommands class and do the
  568. required adjustments to work with cluster mode
  569. """
  570. async def cluster_delslots(self, *slots: EncodableT) -> List[bool]:
  571. """
  572. Set hash slots as unbound in the cluster.
  573. It determines by it self what node the slot is in and sends it there
  574. Returns a list of the results for each processed slot.
  575. For more information see https://redis.io/commands/cluster-delslots
  576. """
  577. return await asyncio.gather(
  578. *(
  579. asyncio.create_task(self.execute_command("CLUSTER DELSLOTS", slot))
  580. for slot in slots
  581. )
  582. )
  583. class ClusterDataAccessCommands(DataAccessCommands):
  584. """
  585. A class for Redis Cluster Data Access Commands
  586. The class inherits from Redis's core DataAccessCommand class and do the
  587. required adjustments to work with cluster mode
  588. """
  589. def stralgo(
  590. self,
  591. algo: Literal["LCS"],
  592. value1: KeyT,
  593. value2: KeyT,
  594. specific_argument: Union[Literal["strings"], Literal["keys"]] = "strings",
  595. len: bool = False,
  596. idx: bool = False,
  597. minmatchlen: Optional[int] = None,
  598. withmatchlen: bool = False,
  599. **kwargs,
  600. ) -> ResponseT:
  601. """
  602. Implements complex algorithms that operate on strings.
  603. Right now the only algorithm implemented is the LCS algorithm
  604. (longest common substring). However new algorithms could be
  605. implemented in the future.
  606. ``algo`` Right now must be LCS
  607. ``value1`` and ``value2`` Can be two strings or two keys
  608. ``specific_argument`` Specifying if the arguments to the algorithm
  609. will be keys or strings. strings is the default.
  610. ``len`` Returns just the len of the match.
  611. ``idx`` Returns the match positions in each string.
  612. ``minmatchlen`` Restrict the list of matches to the ones of a given
  613. minimal length. Can be provided only when ``idx`` set to True.
  614. ``withmatchlen`` Returns the matches with the len of the match.
  615. Can be provided only when ``idx`` set to True.
  616. For more information see https://redis.io/commands/stralgo
  617. """
  618. target_nodes = kwargs.pop("target_nodes", None)
  619. if specific_argument == "strings" and target_nodes is None:
  620. target_nodes = "default-node"
  621. kwargs.update({"target_nodes": target_nodes})
  622. return super().stralgo(
  623. algo,
  624. value1,
  625. value2,
  626. specific_argument,
  627. len,
  628. idx,
  629. minmatchlen,
  630. withmatchlen,
  631. **kwargs,
  632. )
  633. def scan_iter(
  634. self,
  635. match: Optional[PatternT] = None,
  636. count: Optional[int] = None,
  637. _type: Optional[str] = None,
  638. **kwargs,
  639. ) -> Iterator:
  640. # Do the first query with cursor=0 for all nodes
  641. cursors, data = self.scan(match=match, count=count, _type=_type, **kwargs)
  642. yield from data
  643. cursors = {name: cursor for name, cursor in cursors.items() if cursor != 0}
  644. if cursors:
  645. # Get nodes by name
  646. nodes = {name: self.get_node(node_name=name) for name in cursors.keys()}
  647. # Iterate over each node till its cursor is 0
  648. kwargs.pop("target_nodes", None)
  649. while cursors:
  650. for name, cursor in cursors.items():
  651. cur, data = self.scan(
  652. cursor=cursor,
  653. match=match,
  654. count=count,
  655. _type=_type,
  656. target_nodes=nodes[name],
  657. **kwargs,
  658. )
  659. yield from data
  660. cursors[name] = cur[name]
  661. cursors = {
  662. name: cursor for name, cursor in cursors.items() if cursor != 0
  663. }
  664. class AsyncClusterDataAccessCommands(
  665. ClusterDataAccessCommands, AsyncDataAccessCommands
  666. ):
  667. """
  668. A class for Redis Cluster Data Access Commands
  669. The class inherits from Redis's core DataAccessCommand class and do the
  670. required adjustments to work with cluster mode
  671. """
  672. async def scan_iter(
  673. self,
  674. match: Optional[PatternT] = None,
  675. count: Optional[int] = None,
  676. _type: Optional[str] = None,
  677. **kwargs,
  678. ) -> AsyncIterator:
  679. # Do the first query with cursor=0 for all nodes
  680. cursors, data = await self.scan(match=match, count=count, _type=_type, **kwargs)
  681. for value in data:
  682. yield value
  683. cursors = {name: cursor for name, cursor in cursors.items() if cursor != 0}
  684. if cursors:
  685. # Get nodes by name
  686. nodes = {name: self.get_node(node_name=name) for name in cursors.keys()}
  687. # Iterate over each node till its cursor is 0
  688. kwargs.pop("target_nodes", None)
  689. while cursors:
  690. for name, cursor in cursors.items():
  691. cur, data = await self.scan(
  692. cursor=cursor,
  693. match=match,
  694. count=count,
  695. _type=_type,
  696. target_nodes=nodes[name],
  697. **kwargs,
  698. )
  699. for value in data:
  700. yield value
  701. cursors[name] = cur[name]
  702. cursors = {
  703. name: cursor for name, cursor in cursors.items() if cursor != 0
  704. }
  705. class RedisClusterCommands(
  706. ClusterMultiKeyCommands,
  707. ClusterManagementCommands,
  708. ACLCommands,
  709. PubSubCommands,
  710. ClusterDataAccessCommands,
  711. ScriptCommands,
  712. FunctionCommands,
  713. ModuleCommands,
  714. RedisModuleCommands,
  715. ):
  716. """
  717. A class for all Redis Cluster commands
  718. For key-based commands, the target node(s) will be internally determined
  719. by the keys' hash slot.
  720. Non-key-based commands can be executed with the 'target_nodes' argument to
  721. target specific nodes. By default, if target_nodes is not specified, the
  722. command will be executed on the default cluster node.
  723. :param :target_nodes: type can be one of the followings:
  724. - nodes flag: ALL_NODES, PRIMARIES, REPLICAS, RANDOM
  725. - 'ClusterNode'
  726. - 'list(ClusterNodes)'
  727. - 'dict(any:clusterNodes)'
  728. for example:
  729. r.cluster_info(target_nodes=RedisCluster.ALL_NODES)
  730. """
  731. class AsyncRedisClusterCommands(
  732. AsyncClusterMultiKeyCommands,
  733. AsyncClusterManagementCommands,
  734. AsyncACLCommands,
  735. AsyncClusterDataAccessCommands,
  736. AsyncScriptCommands,
  737. AsyncFunctionCommands,
  738. AsyncModuleCommands,
  739. AsyncRedisModuleCommands,
  740. ):
  741. """
  742. A class for all Redis Cluster commands
  743. For key-based commands, the target node(s) will be internally determined
  744. by the keys' hash slot.
  745. Non-key-based commands can be executed with the 'target_nodes' argument to
  746. target specific nodes. By default, if target_nodes is not specified, the
  747. command will be executed on the default cluster node.
  748. :param :target_nodes: type can be one of the followings:
  749. - nodes flag: ALL_NODES, PRIMARIES, REPLICAS, RANDOM
  750. - 'ClusterNode'
  751. - 'list(ClusterNodes)'
  752. - 'dict(any:clusterNodes)'
  753. for example:
  754. r.cluster_info(target_nodes=RedisCluster.ALL_NODES)
  755. """