-
Notifications
You must be signed in to change notification settings - Fork 3
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
added get_replica_connections function #72
base: main
Are you sure you want to change the base?
Changes from 7 commits
e7beff6
6a717ab
0791c7e
c77580e
5ee68b7
c4dc33a
2c20a1e
99b6671
dd8a4ad
8eed87f
e646320
4a3d585
a1262e0
199a1e5
71622fe
9f261fc
9d21e95
6aad7bd
2a96be0
b693987
519b9dd
557f04f
222cfb4
a9f597e
ac8471e
cfd0864
65e41d0
d112322
ad7cedd
ac4cfa4
5a444db
4cc0b6a
51d4d90
cb8cd2e
1ad3162
4d4c1ac
263e8bf
af4f748
b1d6c41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -75,6 +75,7 @@ | |||||||||||||||||||||||
dynamic_startup_nodes=True, | ||||||||||||||||||||||||
url=None, | ||||||||||||||||||||||||
address_remap=None, | ||||||||||||||||||||||||
decode_responses=True | ||||||||||||||||||||||||
): | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
conn = redis.Redis( | ||||||||||||||||||||||||
|
@@ -92,7 +93,7 @@ | |||||||||||||||||||||||
encoding_errors=encoding_errors, | ||||||||||||||||||||||||
charset=charset, | ||||||||||||||||||||||||
errors=errors, | ||||||||||||||||||||||||
decode_responses=True, | ||||||||||||||||||||||||
decode_responses=decode_responses, | ||||||||||||||||||||||||
retry_on_timeout=retry_on_timeout, | ||||||||||||||||||||||||
retry_on_error=retry_on_error, | ||||||||||||||||||||||||
ssl=ssl, | ||||||||||||||||||||||||
|
@@ -125,6 +126,7 @@ | |||||||||||||||||||||||
self.sentinel, self.service_name = Sentinel_Conn(conn, ssl) | ||||||||||||||||||||||||
conn = self.sentinel.master_for(self.service_name, ssl=ssl) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
if Is_Cluster(conn): | ||||||||||||||||||||||||
conn = Cluster_Conn( | ||||||||||||||||||||||||
conn, | ||||||||||||||||||||||||
|
@@ -225,6 +227,36 @@ | |||||||||||||||||||||||
|
||||||||||||||||||||||||
return self.connection.execute_command(CONFIG_CMD, "GET", name)[1] | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def get_replica_connections(self): | ||||||||||||||||||||||||
""" | ||||||||||||||||||||||||
Retrieve a list of connections for FalkorDB replicas. | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
This function determines the FalkorDB setup (Sentinel or Cluster) and returns | ||||||||||||||||||||||||
the hostnames and ports of the replicas. | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
Returns: | ||||||||||||||||||||||||
list of tuple: A list of (hostname, port) tuples representing the | ||||||||||||||||||||||||
replica connections. | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
Raises: | ||||||||||||||||||||||||
ConnectionError: If unable to connect or retrieve information from | ||||||||||||||||||||||||
the FalkorDB setup. | ||||||||||||||||||||||||
ValueError: If the `mode` is neither Sentinel nor Cluster. | ||||||||||||||||||||||||
""" | ||||||||||||||||||||||||
# decide if it's Sentinel or cluster | ||||||||||||||||||||||||
mode = self.connection.execute_command("info")['redis_mode'] | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that 'redis_mode' exists in the dictionary returned by the 'info' command to avoid a KeyError.
Suggested change
Copilot is powered by AI, so mistakes are possible. Review output carefully before use. |
||||||||||||||||||||||||
if hasattr(self, 'sentinel') and self.sentinel is not None: | ||||||||||||||||||||||||
replica_hostnames = self.sentinel.discover_slaves(service_name=self.service_name) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Update deprecated Redis terminology The code uses deprecated 'slave' terminology. Redis has moved to more inclusive language. Update the terminology: - replica_hostnames = self.sentinel.discover_slaves(service_name=self.service_name)
+ replica_hostnames = self.sentinel.discover_replicas(service_name=self.service_name)
- return [(flag['hostname'], int(ip_port.split(':')[1])) for ip_port, flag in data.items() if 'slave' in flag["flags"] and flag["hostname"]]
+ return [(flag.get('hostname', ip_port.split(':')[0]), int(ip_port.split(':')[1]))
+ for ip_port, flag in data.items()
+ if 'replica' in flag.get('flags', [])] Also applies to: 255-255 |
||||||||||||||||||||||||
return [(host, port) for host, port in replica_hostnames] | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update implementation to use modern Redis terminology and handle edge cases The current implementation has several issues:
- replica_hostnames = self.sentinel.discover_slaves(service_name=self.service_name)
- return [(host, port) for host, port in replica_hostnames]
+ try:
+ replica_hostnames = self.sentinel.discover_replicas(service_name=self.service_name)
+ return [(host, int(port)) for host, port in replica_hostnames]
+ except Exception as e:
+ raise ConnectionError(f"Failed to discover replicas: {str(e)}")
# For cluster mode:
- return [(flag['hostname'], ip_port.split(':')[1]) for ip_port, flag in data.items() if 'slave' in flag["flags"]]
+ result = []
+ for ip_port, flag in data.items():
+ if 'replica' in flag.get('flags', []):
+ try:
+ host = ip_port.split(':')[0] # Use IP as hostname
+ port = int(ip_port.split(':')[1])
+ result.append((host, port))
+ except (IndexError, ValueError) as e:
+ continue # Skip malformed entries
+ return result Also applies to: 254-255 |
||||||||||||||||||||||||
elif mode == "cluster": | ||||||||||||||||||||||||
data = self.connection.cluster_nodes() | ||||||||||||||||||||||||
return [(flag['hostname'], ip_port.split(':')[1]) for ip_port, flag in data.items() if 'slave' in flag["flags"]] | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix potential KeyError in cluster mode implementation The current implementation assumes the presence of a 'hostname' key and doesn't handle potential errors. Apply this diff: - return [(flag['hostname'], ip_port.split(':')[1]) for ip_port, flag in data.items() if 'slave' in flag["flags"]]
+ result = []
+ for ip_port, flag in data.items():
+ if 'replica' in flag.get('flags', []): # Using 'replica' instead of 'slave'
+ try:
+ host = ip_port.split(':')[0] # Use IP as hostname
+ port = int(ip_port.split(':')[1])
+ result.append((host, port))
+ except (IndexError, ValueError) as e:
+ continue # Skip malformed entries
+ return result 📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Check: codecov/patch[warning] 248-254: falkordb/falkordb.py#L248-L254 |
||||||||||||||||||||||||
else: | ||||||||||||||||||||||||
raise ValueError(f"Unsupported Redis mode: {redis_mode}") | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix undefined variable reference The error message references Apply this diff: - raise ValueError(f"Unsupported Redis mode: {redis_mode}")
+ raise ValueError(f"Unsupported Redis mode: {mode}") 📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Check: codecov/patch[warning] 256-256: falkordb/falkordb.py#L256 🪛 Ruff256-256: (F405) |
||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def config_set(self, name: str, value=None) -> None: | ||||||||||||||||||||||||
""" | ||||||||||||||||||||||||
Update a DB level configuration. | ||||||||||||||||||||||||
|
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.
Add proper error handling
The code lacks proper error handling for Redis commands and dictionary access.
Add comprehensive error handling:
Also applies to: 254-255
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 249-255: falkordb/falkordb.py#L249-L255
Added lines #L249 - L255 were not covered by tests