Skip to content

Commit

Permalink
[#2341] Initialize slots with empty BitSet in RedisClusterNode's cons…
Browse files Browse the repository at this point in the history
…tructors (#2852)

* fix(#2341): Initialize slots with empty BitSet in RedisClusterNode's constructors

* test(#2341): Add test cases for slot initialization in RedisClusterNode constructors

* fix(#2341): Initialize RedisClusterNode slots with SlotHash.SLOT_COUNT

* chore(#2341): Adjust the formatting

* test(#2341):Add test cases for hasSameSlotsAs()

* fix(#2341): Clone node2 from node1 using the RedisClusterNode constructor and compare the two clusters with hasSameSlotsAs()
  • Loading branch information
zeze1004 committed Jul 12, 2024
1 parent 83f459a commit dee8020
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,11 @@ public RedisClusterNode(RedisURI uri, String nodeId, boolean connected, String s
this.configEpoch = configEpoch;
this.replOffset = -1;

this.slots = new BitSet(slots.length());
this.slots.or(slots);
this.slots = new BitSet(SlotHash.SLOT_COUNT);

if (slots != null) {
this.slots.or(slots);
}

setFlags(flags);
}
Expand All @@ -123,8 +126,9 @@ public RedisClusterNode(RedisClusterNode redisClusterNode) {
this.replOffset = redisClusterNode.replOffset;
this.aliases.addAll(redisClusterNode.aliases);

if (redisClusterNode.slots != null && !redisClusterNode.slots.isEmpty()) {
this.slots = new BitSet(SlotHash.SLOT_COUNT);
this.slots = new BitSet(SlotHash.SLOT_COUNT);

if (redisClusterNode.slots != null) {
this.slots.or(redisClusterNode.slots);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import static org.assertj.core.api.Assertions.*;

import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.HashSet;

import org.junit.jupiter.api.Test;

Expand All @@ -16,6 +19,88 @@
*/
class RedisClusterNodeUnitTests {

@Test
void shouldCreateNodeWithEmptySlots() {

BitSet slots = new BitSet();
RedisClusterNode node = new RedisClusterNode(RedisURI.create("localhost", 6379), "1", true, null, 0, 0, 0, slots,
Collections.emptySet());

assertThat(node.getSlots()).isEmpty();
assertThat(node.getSlots()).isNotNull();
}

@Test
void shouldCreateNodeWithNonEmptySlots() {

BitSet slots = new BitSet();
slots.set(1);
slots.set(2);
RedisClusterNode node = new RedisClusterNode(RedisURI.create("localhost", 6379), "1", true, null, 0, 0, 0, slots,
Collections.emptySet());

assertThat(node.getSlots()).containsExactly(1, 2);
}

@Test
void shouldCopyNodeWithEmptySlots() {

BitSet slots = new BitSet();
RedisClusterNode originalNode = new RedisClusterNode(RedisURI.create("localhost", 6379), "1", true, null, 0, 0, 0,
slots, Collections.emptySet());

RedisClusterNode copiedNode = new RedisClusterNode(originalNode);

assertThat(copiedNode.getSlots()).isEmpty();
assertThat(copiedNode.getSlots()).isNotNull();
}

@Test
void shouldCopyNodeWithNonEmptySlots() {

BitSet slots = new BitSet();
slots.set(1);
slots.set(2);
RedisClusterNode originalNode = new RedisClusterNode(RedisURI.create("localhost", 6379), "1", true, null, 0, 0, 0,
slots, Collections.emptySet());

RedisClusterNode copiedNode = new RedisClusterNode(originalNode);

assertThat(copiedNode.getSlots()).containsExactly(1, 2);
}

@Test
public void testHasSameSlotsAs() {

BitSet emptySlots = new BitSet(SlotHash.SLOT_COUNT);
emptySlots.set(1);
emptySlots.set(2);

RedisClusterNode node1 = new RedisClusterNode(RedisURI.create("localhost", 6379), "nodeId1", true, "slaveOf", 0L, 0L,
0L, emptySlots, new HashSet<>());

RedisClusterNode node2 = new RedisClusterNode(node1);

assertThat(node1.hasSameSlotsAs(node2)).isTrue();
}

@Test
public void testHasDifferentSlotsAs() {

BitSet slots1 = new BitSet(SlotHash.SLOT_COUNT);
slots1.set(1);

BitSet slots2 = new BitSet(SlotHash.SLOT_COUNT);
slots2.set(2);

RedisClusterNode node1 = new RedisClusterNode(RedisURI.create("localhost", 6379), "nodeId1", true, "slaveOf", 0L, 0L,
0L, slots1, new HashSet<>());
RedisClusterNode node2 = new RedisClusterNode(RedisURI.create("localhost", 6379), "nodeId2", true, "slaveOf", 0L, 0L,
0L, slots2, new HashSet<>());

assertThat(node1.hasSameSlotsAs(node2)).isFalse();
}

@Test
void shouldCopyNode() {

Expand Down

0 comments on commit dee8020

Please sign in to comment.