Skip to content
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

Fix sentinel mode read-write separation #749

Merged
merged 5 commits into from
Oct 20, 2024

Conversation

JackiLin
Copy link
Contributor

@JackiLin JackiLin commented Sep 6, 2024

When I attempted to set up read-write separation in Sentinel mode by configuring redis://redis-master/db?is_master=0, I noticed that both read and write requests were still being directed to the master node, without any separation. After reviewing and debugging the source code, I found that the issue was due to redis-py being unable to correctly parse the is_master parameter, and there was also an error in how the project handled is_master. The pool.is_master was only set to False after retrieving the redis.sentinel.SentinelConnectionPool object from redis-py, which resulted in the system supposedly using the slave node, but actually using the master node.
image

Copy link

codecov bot commented Sep 6, 2024

Codecov Report

Attention: Patch coverage is 71.42857% with 4 lines in your changes missing coverage. Please review.

Project coverage is 63.2%. Comparing base (f30d34c) to head (f23b06d).
Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
django_redis/pool.py 66.7% 4 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master    #749     +/-   ##
========================================
- Coverage    63.2%   63.2%   -0.0%     
========================================
  Files          43      43             
  Lines        3223    3231      +8     
  Branches      244     244             
========================================
+ Hits         2034    2039      +5     
- Misses       1172    1175      +3     
  Partials       17      17             
Flag Coverage Δ
mypy 37.9% <21.5%> (-<0.1%) ⬇️
tests 90.3% <80.0%> (-<0.1%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@JackiLin JackiLin force-pushed the read-write-separation branch from f7d1ac0 to 9e56f92 Compare September 6, 2024 13:38
Copy link
Member

@WisdomPill WisdomPill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

# so it doesn't interfere with the SentinelConnectionPool constructor
if "is_master" in query_params:
del query_params["is_master"]
new_query = urlencode(query_params, doseq=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need this if you deleted the key from the original query_params variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this parameter is not deleted, it cannot be correctly parsed as bool type in redis-py, but as str type. This will cause it to use the master node for reading and writing even if is_master=0 is set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, sure, my bad

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please merge master into your branch and solve conflicts then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have merged and resolved the conflicts

Copy link
Member

@WisdomPill WisdomPill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@WisdomPill WisdomPill merged commit f98dcce into jazzband:master Oct 20, 2024
19 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants