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

Add hasSameSlotsOf method to RedisClusterNode for a quick slots comparison #1013

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ private void setSlotBits(List<Integer> slots) {
}
}

public boolean hasSameSlotsOf(RedisClusterNode other) {
if (other == null) return false;
else return this.slots.equals(other.slots);
}

public Set<NodeFlag> getFlags() {
return flags;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static boolean essentiallyEqualsTo(RedisClusterNode o1, RedisClusterNode o2) {
return false;
}

if (o1.getSlots().size() != o2.getSlots().size() || !o1.getSlots().containsAll(o2.getSlots())) {
if (!o1.hasSameSlotsOf(o2)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.util.Lists.newArrayList;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -308,6 +305,34 @@ void isChangedFlagsChangedReplicaToMaster() {
assertThat(isChanged(partitions1, partitions2)).isTrue();
}

@Test
void aNodeShouldNotHaveSameSlotsIfOtherNodeIsNull() {
RedisClusterNode nodeA = createNode(3);
assertThat(nodeA.hasSameSlotsOf(null)).isFalse();
}

@Test
void nodesShouldHaveSameSlots() {
RedisClusterNode nodeA = createNode(1, 4, 36, 98);
RedisClusterNode nodeB = createNode(4, 36, 1, 98);
assertThat(nodeA.getSlots().containsAll(nodeB.getSlots())).isTrue();
assertThat(nodeA.hasSameSlotsOf(nodeB)).isTrue();
}

@Test
void nodesShouldNotHaveSameSlots() {
RedisClusterNode nodeA = createNode(1, 4, 36, 99);
RedisClusterNode nodeB = createNode(4, 36, 1, 100);
assertThat(nodeA.getSlots().containsAll(nodeB.getSlots())).isFalse();
assertThat(nodeA.hasSameSlotsOf(nodeB)).isFalse();
}

private RedisClusterNode createNode(Integer ... slots) {
RedisClusterNode node = new RedisClusterNode();
node.setSlots(Arrays.asList(slots));
return node;
}

@Test
void isChangedFlagsChangedMasterToReplica() {
String nodes1 = "3d005a179da7d8dc1adae6409d47b39c369e992b 127.0.0.1:7380 master - 0 1401258245007 2 disconnected 8000-11999\n"
Expand Down