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

[#2341] Initialize slots with empty BitSet in RedisClusterNode's constructors #2852

Merged
merged 6 commits into from
Jul 12, 2024
Merged
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);
}

zeze1004 marked this conversation as resolved.
Show resolved Hide resolved
@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() {
tishun marked this conversation as resolved.
Show resolved Hide resolved

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
Loading