-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add opt-in latency-aware cluster read balancing #4212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
lh0156
wants to merge
1
commit into
redis:master
Choose a base branch
from
lh0156:agent/latency-based-cluster-balancing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| """Compare round-robin and latency-aware reads on a Redis cluster. | ||
|
|
||
| The benchmark injects delay into one replica's client-side command path. This | ||
| keeps the setup small and repeatable while exercising the same async | ||
| ``RedisCluster`` selection and measurement paths used in production. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import asyncio | ||
| import math | ||
| import time | ||
| from collections import Counter | ||
| from dataclasses import dataclass, field | ||
|
|
||
| from redis.asyncio.cluster import ClusterNode, RedisCluster | ||
| from redis.cluster import LoadBalancingStrategy | ||
|
|
||
|
|
||
| @dataclass | ||
| class Measurements: | ||
| counts: Counter[str] = field(default_factory=Counter) | ||
| latencies: list[float] = field(default_factory=list) | ||
|
|
||
| @property | ||
| def p99_ms(self) -> float: | ||
| if not self.latencies: | ||
| return 0.0 | ||
| ordered = sorted(self.latencies) | ||
| index = min(len(ordered) - 1, math.ceil(len(ordered) * 0.99) - 1) | ||
| return ordered[index] * 1000 | ||
|
|
||
|
|
||
| async def run_mode( | ||
| host: str, | ||
| port: int, | ||
| key: str, | ||
| requests: int, | ||
| concurrency: int, | ||
| delay_ms: float, | ||
| strategy: LoadBalancingStrategy, | ||
| delayed_node_name: str | None, | ||
| ) -> tuple[str, Measurements, str]: | ||
| client = await RedisCluster( | ||
| host=host, | ||
| port=port, | ||
| load_balancing_strategy=strategy, | ||
| ) | ||
| slot_nodes = client.nodes_manager.slots_cache[client.keyslot(key)] | ||
| replicas = slot_nodes[1:] | ||
| if not replicas: | ||
| await client.aclose() | ||
| raise RuntimeError("the selected key slot must have at least one replica") | ||
|
|
||
| delayed_node = next( | ||
| (node for node in replicas if node.name == delayed_node_name), | ||
| replicas[-1] if delayed_node_name is None else None, | ||
| ) | ||
| if delayed_node is None: | ||
| await client.aclose() | ||
| raise ValueError( | ||
| f"node {delayed_node_name!r} is not a replica for key slot " | ||
| f"{client.keyslot(key)}" | ||
| ) | ||
|
|
||
| measurements = Measurements() | ||
| original_execute_command = ClusterNode.execute_command | ||
|
|
||
| async def delayed_execute_command(node, *args, **kwargs): | ||
| started = time.perf_counter() | ||
| if args and args[0] == "GET": | ||
| measurements.counts[node.name] += 1 | ||
| if node.name == delayed_node.name: | ||
| await asyncio.sleep(delay_ms / 1000) | ||
| try: | ||
| return await original_execute_command(node, *args, **kwargs) | ||
| finally: | ||
| if args and args[0] == "GET": | ||
| measurements.latencies.append(time.perf_counter() - started) | ||
|
|
||
| ClusterNode.execute_command = delayed_execute_command | ||
| try: | ||
| requests_per_worker, remainder = divmod(requests, concurrency) | ||
|
|
||
| async def worker(worker_index: int) -> None: | ||
| worker_requests = requests_per_worker + (worker_index < remainder) | ||
| for _ in range(worker_requests): | ||
| await client.get(key) | ||
|
|
||
| await asyncio.gather(*(worker(i) for i in range(concurrency))) | ||
| finally: | ||
| ClusterNode.execute_command = original_execute_command | ||
| await client.aclose() | ||
|
|
||
| return strategy.value, measurements, delayed_node.name | ||
|
|
||
|
|
||
| def parse_args() -> argparse.Namespace: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("--host", default="127.0.0.1") | ||
| parser.add_argument("--port", type=int, default=6379) | ||
| parser.add_argument("--key", default="{latency-benchmark}:key") | ||
| parser.add_argument("--requests", type=int, default=2000) | ||
| parser.add_argument("--concurrency", type=int, default=32) | ||
| parser.add_argument("--delay-ms", type=float, default=10.0) | ||
| parser.add_argument("--delayed-node") | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| async def main(args: argparse.Namespace) -> None: | ||
| if args.requests < args.concurrency: | ||
| raise ValueError("--requests must be at least --concurrency") | ||
|
|
||
| for strategy in ( | ||
| LoadBalancingStrategy.ROUND_ROBIN, | ||
| LoadBalancingStrategy.LATENCY_BASED, | ||
| ): | ||
| name, measurements, delayed_node = await run_mode( | ||
| host=args.host, | ||
| port=args.port, | ||
| key=args.key, | ||
| requests=args.requests, | ||
| concurrency=args.concurrency, | ||
| delay_ms=args.delay_ms, | ||
| strategy=strategy, | ||
| delayed_node_name=args.delayed_node, | ||
| ) | ||
| total = sum(measurements.counts.values()) | ||
| delayed_share = measurements.counts[delayed_node] / total * 100 | ||
| print( | ||
| f"strategy={name:<14} delayed_node={delayed_node:<24} " | ||
| f"delayed_share={delayed_share:6.2f}% p99={measurements.p99_ms:8.3f}ms" | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main(parse_args())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Situations that will produce fake good and prefered latency records:
MaxConnectionsError. Raised synchronously by acquire_connection, which is inside the tracked window because async starts the timer before target_node.execute_command(). So the most saturated node scores ≈0, attracts more reads, and produces more MaxConnectionsError. That's a strict inversion of the feature's purpose.
connect failure. A refused TCP connect fails in microseconds, so a dead node becomes top-ranked until the slot map refreshes.