Skip to content

Commit

Permalink
Improve cluster slots slotNodes using arrays (#3373)
Browse files Browse the repository at this point in the history
* Performance improvement cluster slots slotNodes cache switch hashmap to arrays

Signed-off-by: c00603587 <[email protected]>
Co-authored-by: c00603587 <[email protected]>
  • Loading branch information
chenshi5012 and c00603587 authored Apr 27, 2023
1 parent b2c5acf commit e0f0357
Showing 1 changed file with 13 additions and 12 deletions.
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();
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

0 comments on commit e0f0357

Please sign in to comment.