Replace synchronized keyword lock to ReaderWriterLock
This commit is contained in:
@@ -8,6 +8,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||||
|
|
||||||
public class JedisClusterInfoCache {
|
public class JedisClusterInfoCache {
|
||||||
public static final ClusterNodeInformationParser nodeInfoParser = new ClusterNodeInformationParser();
|
public static final ClusterNodeInformationParser nodeInfoParser = new ClusterNodeInformationParser();
|
||||||
@@ -15,101 +17,145 @@ public class JedisClusterInfoCache {
|
|||||||
private Map<String, JedisPool> nodes = new HashMap<String, JedisPool>();
|
private Map<String, JedisPool> nodes = new HashMap<String, JedisPool>();
|
||||||
private Map<Integer, JedisPool> slots = new HashMap<Integer, JedisPool>();
|
private Map<Integer, JedisPool> slots = new HashMap<Integer, JedisPool>();
|
||||||
|
|
||||||
public synchronized void discoverClusterNodesAndSlots(Jedis jedis) {
|
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
|
||||||
this.nodes.clear();
|
private final Lock r = rwl.readLock();
|
||||||
this.slots.clear();
|
private final Lock w = rwl.writeLock();
|
||||||
|
|
||||||
String localNodes = jedis.clusterNodes();
|
public void discoverClusterNodesAndSlots(Jedis jedis) {
|
||||||
for (String nodeInfo : localNodes.split("\n")) {
|
w.lock();
|
||||||
ClusterNodeInformation clusterNodeInfo = nodeInfoParser.parse(
|
|
||||||
nodeInfo, new HostAndPort(jedis.getClient().getHost(),
|
|
||||||
jedis.getClient().getPort()));
|
|
||||||
|
|
||||||
HostAndPort targetNode = clusterNodeInfo.getNode();
|
try {
|
||||||
setNodeIfNotExist(targetNode);
|
this.nodes.clear();
|
||||||
assignSlotsToNode(clusterNodeInfo.getAvailableSlots(), targetNode);
|
this.slots.clear();
|
||||||
|
|
||||||
|
String localNodes = jedis.clusterNodes();
|
||||||
|
for (String nodeInfo : localNodes.split("\n")) {
|
||||||
|
ClusterNodeInformation clusterNodeInfo = nodeInfoParser.parse(
|
||||||
|
nodeInfo, new HostAndPort(jedis.getClient().getHost(),
|
||||||
|
jedis.getClient().getPort()));
|
||||||
|
|
||||||
|
HostAndPort targetNode = clusterNodeInfo.getNode();
|
||||||
|
setNodeIfNotExist(targetNode);
|
||||||
|
assignSlotsToNode(clusterNodeInfo.getAvailableSlots(), targetNode);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
w.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void discoverClusterSlots(Jedis jedis) {
|
public void discoverClusterSlots(Jedis jedis) {
|
||||||
this.slots.clear();
|
w.lock();
|
||||||
|
|
||||||
List<Object> slots = jedis.clusterSlots();
|
try {
|
||||||
|
this.slots.clear();
|
||||||
|
|
||||||
for (Object slotInfoObj : slots) {
|
List<Object> slots = jedis.clusterSlots();
|
||||||
List<Object> slotInfo = (List<Object>) slotInfoObj;
|
|
||||||
|
|
||||||
if (slotInfo.size() <= 2) {
|
for (Object slotInfoObj : slots) {
|
||||||
continue;
|
List<Object> slotInfo = (List<Object>) slotInfoObj;
|
||||||
|
|
||||||
|
if (slotInfo.size() <= 2) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Integer> slotNums = getAssignedSlotArray(slotInfo);
|
||||||
|
|
||||||
|
// hostInfos
|
||||||
|
List<Object> hostInfos = (List<Object>) slotInfo.get(2);
|
||||||
|
if (hostInfos.size() <= 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// at this time, we just use master, discard slave information
|
||||||
|
HostAndPort targetNode = generateHostAndPort(hostInfos);
|
||||||
|
|
||||||
|
setNodeIfNotExist(targetNode);
|
||||||
|
assignSlotsToNode(slotNums, targetNode);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
// assigned slots
|
w.unlock();
|
||||||
List<Integer> slotNums = new ArrayList<Integer>();
|
|
||||||
for (int slot = ((Long) slotInfo.get(0)).intValue() ;
|
|
||||||
slot <= ((Long) slotInfo.get(1)).intValue() ;
|
|
||||||
slot++) {
|
|
||||||
slotNums.add(slot);
|
|
||||||
}
|
|
||||||
|
|
||||||
// hostInfos
|
|
||||||
List<Object> hostInfos = (List<Object>) slotInfo.get(2);
|
|
||||||
if (hostInfos.size() <= 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// at this time, we just use master, discard slave information
|
|
||||||
HostAndPort targetNode = new HostAndPort(
|
|
||||||
SafeEncoder.encode((byte[]) hostInfos.get(0)),
|
|
||||||
((Long) hostInfos.get(1)).intValue());
|
|
||||||
|
|
||||||
setNodeIfNotExist(targetNode);
|
|
||||||
assignSlotsToNode(slotNums, targetNode);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void setNodeIfNotExist(HostAndPort node) {
|
private HostAndPort generateHostAndPort(List<Object> hostInfos) {
|
||||||
String nodeKey = getNodeKey(node);
|
return new HostAndPort(
|
||||||
if (nodes.containsKey(nodeKey))
|
SafeEncoder.encode((byte[]) hostInfos.get(0)),
|
||||||
return;
|
((Long) hostInfos.get(1)).intValue());
|
||||||
|
|
||||||
JedisPool nodePool = new JedisPool(node.getHost(), node.getPort());
|
|
||||||
nodes.put(nodeKey, nodePool);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void assignSlotToNode(int slot, HostAndPort targetNode) {
|
public void setNodeIfNotExist(HostAndPort node) {
|
||||||
JedisPool targetPool = nodes.get(getNodeKey(targetNode));
|
w.lock();
|
||||||
|
try {
|
||||||
|
String nodeKey = getNodeKey(node);
|
||||||
|
if (nodes.containsKey(nodeKey))
|
||||||
|
return;
|
||||||
|
|
||||||
if (targetPool == null) {
|
JedisPool nodePool = new JedisPool(node.getHost(), node.getPort());
|
||||||
setNodeIfNotExist(targetNode);
|
nodes.put(nodeKey, nodePool);
|
||||||
targetPool = nodes.get(getNodeKey(targetNode));
|
} finally {
|
||||||
|
w.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void assignSlotToNode(int slot, HostAndPort targetNode) {
|
||||||
|
w.lock();
|
||||||
|
try {
|
||||||
|
JedisPool targetPool = nodes.get(getNodeKey(targetNode));
|
||||||
|
|
||||||
|
if (targetPool == null) {
|
||||||
|
setNodeIfNotExist(targetNode);
|
||||||
|
targetPool = nodes.get(getNodeKey(targetNode));
|
||||||
|
}
|
||||||
|
slots.put(slot, targetPool);
|
||||||
|
} finally {
|
||||||
|
w.unlock();
|
||||||
}
|
}
|
||||||
slots.put(slot, targetPool);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void assignSlotsToNode(List<Integer> targetSlots,
|
public synchronized void assignSlotsToNode(List<Integer> targetSlots,
|
||||||
HostAndPort targetNode) {
|
HostAndPort targetNode) {
|
||||||
JedisPool targetPool = nodes.get(getNodeKey(targetNode));
|
w.lock();
|
||||||
|
try {
|
||||||
|
JedisPool targetPool = nodes.get(getNodeKey(targetNode));
|
||||||
|
|
||||||
if (targetPool == null) {
|
if (targetPool == null) {
|
||||||
setNodeIfNotExist(targetNode);
|
setNodeIfNotExist(targetNode);
|
||||||
targetPool = nodes.get(getNodeKey(targetNode));
|
targetPool = nodes.get(getNodeKey(targetNode));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Integer slot : targetSlots) {
|
for (Integer slot : targetSlots) {
|
||||||
slots.put(slot, targetPool);
|
slots.put(slot, targetPool);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
w.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized JedisPool getNode(String nodeKey) {
|
public synchronized JedisPool getNode(String nodeKey) {
|
||||||
return nodes.get(nodeKey);
|
r.lock();
|
||||||
|
try {
|
||||||
|
return nodes.get(nodeKey);
|
||||||
|
} finally {
|
||||||
|
r.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized JedisPool getSlotPool(int slot) {
|
public synchronized JedisPool getSlotPool(int slot) {
|
||||||
return slots.get(slot);
|
r.lock();
|
||||||
|
try {
|
||||||
|
return slots.get(slot);
|
||||||
|
} finally {
|
||||||
|
r.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized Map<String, JedisPool> getNodes() {
|
public synchronized Map<String, JedisPool> getNodes() {
|
||||||
return new HashMap<String, JedisPool>(nodes);
|
r.lock();
|
||||||
|
try {
|
||||||
|
return new HashMap<String, JedisPool>(nodes);
|
||||||
|
} finally {
|
||||||
|
r.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getNodeKey(HostAndPort hnp) {
|
public static String getNodeKey(HostAndPort hnp) {
|
||||||
@@ -124,4 +170,14 @@ public class JedisClusterInfoCache {
|
|||||||
return getNodeKey(jedis.getClient());
|
return getNodeKey(jedis.getClient());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<Integer> getAssignedSlotArray(List<Object> slotInfo) {
|
||||||
|
List<Integer> slotNums = new ArrayList<Integer>();
|
||||||
|
for (int slot = ((Long) slotInfo.get(0)).intValue();
|
||||||
|
slot <= ((Long) slotInfo.get(1)).intValue();
|
||||||
|
slot++) {
|
||||||
|
slotNums.add(slot);
|
||||||
|
}
|
||||||
|
return slotNums;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user