Re-format source to respect Jedis convention
This commit is contained in:
@@ -72,15 +72,15 @@ public abstract class JedisClusterCommand<T> {
|
|||||||
} catch (JedisRedirectionException jre) {
|
} catch (JedisRedirectionException jre) {
|
||||||
if (jre instanceof JedisAskDataException) {
|
if (jre instanceof JedisAskDataException) {
|
||||||
asking = true;
|
asking = true;
|
||||||
this.connectionHandler.assignSlotToNode(jre.getSlot(),
|
this.connectionHandler.assignSlotToNode(jre.getSlot(),
|
||||||
jre.getTargetNode());
|
jre.getTargetNode());
|
||||||
} else if (jre instanceof JedisMovedDataException) {
|
} else if (jre instanceof JedisMovedDataException) {
|
||||||
// it rebuilds cluster's slot cache
|
// it rebuilds cluster's slot cache
|
||||||
// recommended by Redis cluster specification
|
// recommended by Redis cluster specification
|
||||||
this.connectionHandler.renewSlotCache();
|
this.connectionHandler.renewSlotCache();
|
||||||
} else {
|
} else {
|
||||||
throw new JedisClusterException(jre);
|
throw new JedisClusterException(jre);
|
||||||
}
|
}
|
||||||
|
|
||||||
releaseConnection(connection, false);
|
releaseConnection(connection, false);
|
||||||
connection = null;
|
connection = null;
|
||||||
|
|||||||
@@ -14,13 +14,13 @@ public abstract class JedisClusterConnectionHandler {
|
|||||||
abstract Jedis getConnection();
|
abstract Jedis getConnection();
|
||||||
|
|
||||||
public void returnConnection(Jedis connection) {
|
public void returnConnection(Jedis connection) {
|
||||||
cache.getNode(getNodeKey(connection.getClient()))
|
cache.getNode(getNodeKey(connection.getClient())).returnResource(
|
||||||
.returnResource(connection);
|
connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void returnBrokenConnection(Jedis connection) {
|
public void returnBrokenConnection(Jedis connection) {
|
||||||
cache.getNode(getNodeKey(connection.getClient()))
|
cache.getNode(getNodeKey(connection.getClient())).returnBrokenResource(
|
||||||
.returnBrokenResource(connection);
|
connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract Jedis getConnectionFromSlot(int slot);
|
abstract Jedis getConnectionFromSlot(int slot);
|
||||||
@@ -34,7 +34,7 @@ public abstract class JedisClusterConnectionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void assignSlotToNode(int slot, HostAndPort targetNode) {
|
public void assignSlotToNode(int slot, HostAndPort targetNode) {
|
||||||
cache.assignSlotToNode(slot, targetNode);
|
cache.assignSlotToNode(slot, targetNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeSlotsCache(Set<HostAndPort> startNodes) {
|
private void initializeSlotsCache(Set<HostAndPort> startNodes) {
|
||||||
@@ -45,14 +45,14 @@ public abstract class JedisClusterConnectionHandler {
|
|||||||
Jedis jedis = null;
|
Jedis jedis = null;
|
||||||
try {
|
try {
|
||||||
jedis = jp.getResource();
|
jedis = jp.getResource();
|
||||||
cache.discoverClusterNodesAndSlots(jedis);
|
cache.discoverClusterNodesAndSlots(jedis);
|
||||||
break;
|
break;
|
||||||
} catch (JedisConnectionException e) {
|
} catch (JedisConnectionException e) {
|
||||||
// try next nodes
|
// try next nodes
|
||||||
} finally {
|
} finally {
|
||||||
if (jedis != null) {
|
if (jedis != null) {
|
||||||
jedis.close();
|
jedis.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,18 +62,18 @@ public abstract class JedisClusterConnectionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void renewSlotCache() {
|
public void renewSlotCache() {
|
||||||
for (JedisPool jp : cache.getNodes().values()) {
|
for (JedisPool jp : cache.getNodes().values()) {
|
||||||
Jedis jedis = null;
|
Jedis jedis = null;
|
||||||
try {
|
try {
|
||||||
jedis = jp.getResource();
|
jedis = jp.getResource();
|
||||||
cache.discoverClusterSlots(jedis);
|
cache.discoverClusterSlots(jedis);
|
||||||
break;
|
break;
|
||||||
} finally {
|
} finally {
|
||||||
if (jedis != null) {
|
if (jedis != null) {
|
||||||
jedis.close();
|
jedis.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected JedisPool getRandomConnection() {
|
protected JedisPool getRandomConnection() {
|
||||||
|
|||||||
@@ -22,162 +22,161 @@ public class JedisClusterInfoCache {
|
|||||||
private final Lock w = rwl.writeLock();
|
private final Lock w = rwl.writeLock();
|
||||||
|
|
||||||
public void discoverClusterNodesAndSlots(Jedis jedis) {
|
public void discoverClusterNodesAndSlots(Jedis jedis) {
|
||||||
w.lock();
|
w.lock();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.nodes.clear();
|
this.nodes.clear();
|
||||||
this.slots.clear();
|
this.slots.clear();
|
||||||
|
|
||||||
String localNodes = jedis.clusterNodes();
|
String localNodes = jedis.clusterNodes();
|
||||||
for (String nodeInfo : localNodes.split("\n")) {
|
for (String nodeInfo : localNodes.split("\n")) {
|
||||||
ClusterNodeInformation clusterNodeInfo = nodeInfoParser.parse(
|
ClusterNodeInformation clusterNodeInfo = nodeInfoParser.parse(
|
||||||
nodeInfo, new HostAndPort(jedis.getClient().getHost(),
|
nodeInfo, new HostAndPort(jedis.getClient().getHost(),
|
||||||
jedis.getClient().getPort()));
|
jedis.getClient().getPort()));
|
||||||
|
|
||||||
HostAndPort targetNode = clusterNodeInfo.getNode();
|
HostAndPort targetNode = clusterNodeInfo.getNode();
|
||||||
setNodeIfNotExist(targetNode);
|
setNodeIfNotExist(targetNode);
|
||||||
assignSlotsToNode(clusterNodeInfo.getAvailableSlots(), targetNode);
|
assignSlotsToNode(clusterNodeInfo.getAvailableSlots(),
|
||||||
}
|
targetNode);
|
||||||
} finally {
|
}
|
||||||
w.unlock();
|
} finally {
|
||||||
}
|
w.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void discoverClusterSlots(Jedis jedis) {
|
public void discoverClusterSlots(Jedis jedis) {
|
||||||
w.lock();
|
w.lock();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.slots.clear();
|
this.slots.clear();
|
||||||
|
|
||||||
List<Object> slots = jedis.clusterSlots();
|
List<Object> slots = jedis.clusterSlots();
|
||||||
|
|
||||||
for (Object slotInfoObj : slots) {
|
for (Object slotInfoObj : slots) {
|
||||||
List<Object> slotInfo = (List<Object>) slotInfoObj;
|
List<Object> slotInfo = (List<Object>) slotInfoObj;
|
||||||
|
|
||||||
if (slotInfo.size() <= 2) {
|
if (slotInfo.size() <= 2) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Integer> slotNums = getAssignedSlotArray(slotInfo);
|
List<Integer> slotNums = getAssignedSlotArray(slotInfo);
|
||||||
|
|
||||||
// hostInfos
|
// hostInfos
|
||||||
List<Object> hostInfos = (List<Object>) slotInfo.get(2);
|
List<Object> hostInfos = (List<Object>) slotInfo.get(2);
|
||||||
if (hostInfos.size() <= 0) {
|
if (hostInfos.size() <= 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// at this time, we just use master, discard slave information
|
// at this time, we just use master, discard slave information
|
||||||
HostAndPort targetNode = generateHostAndPort(hostInfos);
|
HostAndPort targetNode = generateHostAndPort(hostInfos);
|
||||||
|
|
||||||
setNodeIfNotExist(targetNode);
|
setNodeIfNotExist(targetNode);
|
||||||
assignSlotsToNode(slotNums, targetNode);
|
assignSlotsToNode(slotNums, targetNode);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
w.unlock();
|
w.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private HostAndPort generateHostAndPort(List<Object> hostInfos) {
|
private HostAndPort generateHostAndPort(List<Object> hostInfos) {
|
||||||
return new HostAndPort(
|
return new HostAndPort(SafeEncoder.encode((byte[]) hostInfos.get(0)),
|
||||||
SafeEncoder.encode((byte[]) hostInfos.get(0)),
|
((Long) hostInfos.get(1)).intValue());
|
||||||
((Long) hostInfos.get(1)).intValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNodeIfNotExist(HostAndPort node) {
|
public void setNodeIfNotExist(HostAndPort node) {
|
||||||
w.lock();
|
w.lock();
|
||||||
try {
|
try {
|
||||||
String nodeKey = getNodeKey(node);
|
String nodeKey = getNodeKey(node);
|
||||||
if (nodes.containsKey(nodeKey))
|
if (nodes.containsKey(nodeKey))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
JedisPool nodePool = new JedisPool(node.getHost(), node.getPort());
|
JedisPool nodePool = new JedisPool(node.getHost(), node.getPort());
|
||||||
nodes.put(nodeKey, nodePool);
|
nodes.put(nodeKey, nodePool);
|
||||||
} finally {
|
} finally {
|
||||||
w.unlock();
|
w.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void assignSlotToNode(int slot, HostAndPort targetNode) {
|
public void assignSlotToNode(int slot, HostAndPort targetNode) {
|
||||||
w.lock();
|
w.lock();
|
||||||
try {
|
try {
|
||||||
JedisPool targetPool = nodes.get(getNodeKey(targetNode));
|
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));
|
||||||
}
|
}
|
||||||
slots.put(slot, targetPool);
|
slots.put(slot, targetPool);
|
||||||
} finally {
|
} finally {
|
||||||
w.unlock();
|
w.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void assignSlotsToNode(List<Integer> targetSlots,
|
public synchronized void assignSlotsToNode(List<Integer> targetSlots,
|
||||||
HostAndPort targetNode) {
|
HostAndPort targetNode) {
|
||||||
w.lock();
|
w.lock();
|
||||||
try {
|
try {
|
||||||
JedisPool targetPool = nodes.get(getNodeKey(targetNode));
|
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 {
|
} finally {
|
||||||
w.unlock();
|
w.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized JedisPool getNode(String nodeKey) {
|
public synchronized JedisPool getNode(String nodeKey) {
|
||||||
r.lock();
|
r.lock();
|
||||||
try {
|
try {
|
||||||
return nodes.get(nodeKey);
|
return nodes.get(nodeKey);
|
||||||
} finally {
|
} finally {
|
||||||
r.unlock();
|
r.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized JedisPool getSlotPool(int slot) {
|
public synchronized JedisPool getSlotPool(int slot) {
|
||||||
r.lock();
|
r.lock();
|
||||||
try {
|
try {
|
||||||
return slots.get(slot);
|
return slots.get(slot);
|
||||||
} finally {
|
} finally {
|
||||||
r.unlock();
|
r.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized Map<String, JedisPool> getNodes() {
|
public synchronized Map<String, JedisPool> getNodes() {
|
||||||
r.lock();
|
r.lock();
|
||||||
try {
|
try {
|
||||||
return new HashMap<String, JedisPool>(nodes);
|
return new HashMap<String, JedisPool>(nodes);
|
||||||
} finally {
|
} finally {
|
||||||
r.unlock();
|
r.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getNodeKey(HostAndPort hnp) {
|
public static String getNodeKey(HostAndPort hnp) {
|
||||||
return hnp.getHost() + ":" + hnp.getPort();
|
return hnp.getHost() + ":" + hnp.getPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getNodeKey(Client client) {
|
public static String getNodeKey(Client client) {
|
||||||
return client.getHost() + ":" + client.getPort();
|
return client.getHost() + ":" + client.getPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getNodeKey(Jedis jedis) {
|
public static String getNodeKey(Jedis jedis) {
|
||||||
return getNodeKey(jedis.getClient());
|
return getNodeKey(jedis.getClient());
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Integer> getAssignedSlotArray(List<Object> slotInfo) {
|
private List<Integer> getAssignedSlotArray(List<Object> slotInfo) {
|
||||||
List<Integer> slotNums = new ArrayList<Integer>();
|
List<Integer> slotNums = new ArrayList<Integer>();
|
||||||
for (int slot = ((Long) slotInfo.get(0)).intValue();
|
for (int slot = ((Long) slotInfo.get(0)).intValue(); slot <= ((Long) slotInfo
|
||||||
slot <= ((Long) slotInfo.get(1)).intValue();
|
.get(1)).intValue(); slot++) {
|
||||||
slot++) {
|
slotNums.add(slot);
|
||||||
slotNums.add(slot);
|
}
|
||||||
}
|
return slotNums;
|
||||||
return slotNums;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user