Skip to content
14 changes: 12 additions & 2 deletions source/extensions/clusters/redis/redis_cluster_lb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@ namespace Clusters {
namespace Redis {

bool ClusterSlot::operator==(const Envoy::Extensions::Clusters::Redis::ClusterSlot& rhs) const {
return start_ == rhs.start_ && end_ == rhs.end_ && primary_ == rhs.primary_ &&
replicas_ == rhs.replicas_;
if (start_ != rhs.start_ || end_ != rhs.end_ || *primary_ != *rhs.primary_ ||
replicas_.size() != rhs.replicas_.size()) {
return false;
} else {
for (auto it1 = replicas_.begin(), it2 = rhs.replicas_.begin(); it1 != replicas_.end();
it1++, it2++) {
if (**it1 != **it2) {
return false;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: this loop shouldn't be nested in else to lessen cognitive load.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your suggestion, I have add a new commit.

return true;
}
}

// RedisClusterLoadBalancerFactory
Expand Down
9 changes: 6 additions & 3 deletions test/extensions/clusters/redis/redis_cluster_lb_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ TEST_F(RedisClusterLoadBalancerTest, ClusterSlotUpdate) {

TEST_F(RedisClusterLoadBalancerTest, ClusterSlotNoUpdate) {
Upstream::HostVector hosts{Upstream::makeTestHost(info_, "tcp://127.0.0.1:90", simTime()),
Upstream::makeTestHost(info_, "tcp://127.0.0.1:91", simTime()),
Upstream::makeTestHost(info_, "tcp://127.0.0.1:92", simTime()),
Upstream::makeTestHost(info_, "tcp://127.0.0.1:90", simTime()),
Upstream::makeTestHost(info_, "tcp://127.0.0.1:91", simTime()),
Upstream::makeTestHost(info_, "tcp://127.0.0.1:92", simTime())};

Expand All @@ -373,9 +376,9 @@ TEST_F(RedisClusterLoadBalancerTest, ClusterSlotNoUpdate) {

// Calling cluster slot update without change should not change assignment.
std::vector<ClusterSlot> updated_slot{
ClusterSlot(0, 1000, hosts[0]->address()),
ClusterSlot(1001, 2000, hosts[1]->address()),
ClusterSlot(2001, 16383, hosts[2]->address()),
ClusterSlot(0, 1000, hosts[3]->address()),
ClusterSlot(1001, 2000, hosts[4]->address()),
ClusterSlot(2001, 16383, hosts[5]->address()),
};
EXPECT_EQ(false, factory_->onClusterSlotUpdate(
std::make_unique<std::vector<ClusterSlot>>(updated_slot), all_hosts));
Expand Down