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

Performance improvement cluster slots slotNodes cache switch hashmap to arrays #3373

Merged
merged 5 commits into from
Apr 27, 2023
Merged
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
25 changes: 13 additions & 12 deletions src/main/java/redis/clients/jedis/JedisClusterInfoCache.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis.clients.jedis;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -21,8 +22,8 @@
public class JedisClusterInfoCache {

private final Map<String, ConnectionPool> nodes = new HashMap<>();
private final Map<Integer, ConnectionPool> slots = new HashMap<>();
private final Map<Integer, HostAndPort> slotNodes = new HashMap<>();
private final ConnectionPool[] slots = new ConnectionPool[Protocol.CLUSTER_HASHSLOTS];
private final HostAndPort[] slotNodes = new HostAndPort[Protocol.CLUSTER_HASHSLOTS];

private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
private final Lock r = rwl.readLock();
Expand Down Expand Up @@ -148,8 +149,8 @@ private void discoverClusterSlots(Connection jedis) {
}
w.lock();
try {
this.slots.clear();
this.slotNodes.clear();
Arrays.fill(slots, null);
Arrays.fill(slotNodes, null);
Set<String> hostAndPortKeys = new HashSet<>();

for (Object slotInfoObj : slotsInfo) {
Expand Down Expand Up @@ -224,8 +225,8 @@ public void assignSlotToNode(int slot, HostAndPort targetNode) {
w.lock();
try {
ConnectionPool targetPool = setupNodeIfNotExist(targetNode);
slots.put(slot, targetPool);
slotNodes.put(slot, targetNode);
slots[slot] = targetPool;
slotNodes[slot] = targetNode;
} finally {
w.unlock();
}
Expand All @@ -236,8 +237,8 @@ public void assignSlotsToNode(List<Integer> targetSlots, HostAndPort targetNode)
try {
ConnectionPool targetPool = setupNodeIfNotExist(targetNode);
for (Integer slot : targetSlots) {
slots.put(slot, targetPool);
slotNodes.put(slot, targetNode);
slots[slot] = targetPool;
slotNodes[slot] = targetNode;
}
} finally {
w.unlock();
Expand All @@ -260,7 +261,7 @@ public ConnectionPool getNode(HostAndPort node) {
public ConnectionPool getSlotPool(int slot) {
r.lock();
chenshi5012 marked this conversation as resolved.
Show resolved Hide resolved
try {
return slots.get(slot);
return slots[slot];
} finally {
r.unlock();
}
Expand All @@ -269,7 +270,7 @@ public ConnectionPool getSlotPool(int slot) {
public HostAndPort getSlotNode(int slot) {
r.lock();
try {
return slotNodes.get(slot);
return slotNodes[slot];
} finally {
r.unlock();
}
Expand Down Expand Up @@ -311,8 +312,8 @@ public void reset() {
}
}
nodes.clear();
slots.clear();
slotNodes.clear();
Arrays.fill(slots, null);
Arrays.fill(slotNodes, null);
} finally {
w.unlock();
}
Expand Down