-
Notifications
You must be signed in to change notification settings - Fork 5.5k
redis cluster: fix ClusterSlot operator == #16116
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
Changes from 4 commits
5c326e9
c6f431f
6dbef99
bb75fe2
4c19890
6594417
732d8e7
46e9ff6
ba8b162
61e7bc4
fa55163
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 |
|---|---|---|
|
|
@@ -37,7 +37,18 @@ class ClusterSlot { | |
| int64_t start() const { return start_; } | ||
| int64_t end() const { return end_; } | ||
| Network::Address::InstanceConstSharedPtr primary() const { return primary_; } | ||
| const absl::flat_hash_set<Network::Address::InstanceConstSharedPtr>& replicas() const { | ||
| struct Hash { | ||
| size_t operator()(const Network::Address::InstanceConstSharedPtr& address) const { | ||
| return absl::Hash<std::string>()(address->asString()); | ||
|
Contributor
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. As a matter of good practice, you'd want to use But in researching this I found a potential latent crash here. The doc for This suggests to me that bind() might mutate the name. If that happens after we have already stored an address in a hash-table, I think this can crash in subsequent hash-table operations. So my suggestion is to convert this to a
Member
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. +1 That's a good catch! There's no good in hashing keys which can mutate.
Contributor
Author
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. Thanks for this suggestion, I have use map instead of flat_hash_set and create a commit. |
||
| } | ||
| }; | ||
| struct Eq { | ||
| bool operator()(const Network::Address::InstanceConstSharedPtr& lhs, | ||
| const Network::Address::InstanceConstSharedPtr& rhs) const { | ||
| return *lhs == *rhs; | ||
| } | ||
| }; | ||
| const absl::flat_hash_set<Network::Address::InstanceConstSharedPtr, Hash, Eq>& replicas() const { | ||
| return replicas_; | ||
| } | ||
| void addReplica(Network::Address::InstanceConstSharedPtr replica_address) { | ||
|
|
@@ -50,7 +61,7 @@ class ClusterSlot { | |
| int64_t start_; | ||
| int64_t end_; | ||
| Network::Address::InstanceConstSharedPtr primary_; | ||
| absl::flat_hash_set<Network::Address::InstanceConstSharedPtr> replicas_; | ||
| absl::flat_hash_set<Network::Address::InstanceConstSharedPtr, Hash, Eq> replicas_; | ||
| }; | ||
|
|
||
| using ClusterSlotsPtr = std::unique_ptr<std::vector<ClusterSlot>>; | ||
|
|
||
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.
Isn't this loop equivalent to just
return replicas_ == rhs.replicas_;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.
In the test case, "replicas_ == rhs.replicas_" returns false, but In my code, it returns true. I'm trying to find the reason for this.
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.
This is really not equal. Because in absl, "operator ==" uses "==" to compare the value, and "find" uses "eq_ref" to compare the value.

