From 15ab93494562b712248190b2da78446fd5d3f758 Mon Sep 17 00:00:00 2001 From: Jungtaek Lim Date: Wed, 2 Jul 2014 23:52:42 +0900 Subject: [PATCH] Re-format source to respect Jedis convention --- .../clients/jedis/JedisClusterCommand.java | 26 +-- .../jedis/JedisClusterConnectionHandler.java | 42 ++-- .../clients/jedis/JedisClusterInfoCache.java | 209 +++++++++--------- 3 files changed, 138 insertions(+), 139 deletions(-) diff --git a/src/main/java/redis/clients/jedis/JedisClusterCommand.java b/src/main/java/redis/clients/jedis/JedisClusterCommand.java index c4d80fa..604afca 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterCommand.java +++ b/src/main/java/redis/clients/jedis/JedisClusterCommand.java @@ -63,35 +63,35 @@ public abstract class JedisClusterCommand { // maybe all connection is down throw jce; } - + releaseConnection(connection, true); connection = null; - + // retry with random connection return runWithRetries(key, redirections--, true, asking); } catch (JedisRedirectionException jre) { if (jre instanceof JedisAskDataException) { asking = true; - this.connectionHandler.assignSlotToNode(jre.getSlot(), - jre.getTargetNode()); - } else if (jre instanceof JedisMovedDataException) { - // it rebuilds cluster's slot cache - // recommended by Redis cluster specification - this.connectionHandler.renewSlotCache(); - } else { - throw new JedisClusterException(jre); - } + this.connectionHandler.assignSlotToNode(jre.getSlot(), + jre.getTargetNode()); + } else if (jre instanceof JedisMovedDataException) { + // it rebuilds cluster's slot cache + // recommended by Redis cluster specification + this.connectionHandler.renewSlotCache(); + } else { + throw new JedisClusterException(jre); + } releaseConnection(connection, false); connection = null; - + return runWithRetries(key, redirections - 1, false, asking); } finally { releaseConnection(connection, false); } } - + private void releaseConnection(Jedis connection, boolean broken) { if (connection != null) { if (broken) { diff --git a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java index 102921e..5288e7c 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java @@ -14,13 +14,13 @@ public abstract class JedisClusterConnectionHandler { abstract Jedis getConnection(); public void returnConnection(Jedis connection) { - cache.getNode(getNodeKey(connection.getClient())) - .returnResource(connection); + cache.getNode(getNodeKey(connection.getClient())).returnResource( + connection); } public void returnBrokenConnection(Jedis connection) { - cache.getNode(getNodeKey(connection.getClient())) - .returnBrokenResource(connection); + cache.getNode(getNodeKey(connection.getClient())).returnBrokenResource( + connection); } abstract Jedis getConnectionFromSlot(int slot); @@ -34,7 +34,7 @@ public abstract class JedisClusterConnectionHandler { } public void assignSlotToNode(int slot, HostAndPort targetNode) { - cache.assignSlotToNode(slot, targetNode); + cache.assignSlotToNode(slot, targetNode); } private void initializeSlotsCache(Set startNodes) { @@ -45,14 +45,14 @@ public abstract class JedisClusterConnectionHandler { Jedis jedis = null; try { jedis = jp.getResource(); - cache.discoverClusterNodesAndSlots(jedis); + cache.discoverClusterNodesAndSlots(jedis); break; } catch (JedisConnectionException e) { // try next nodes } finally { - if (jedis != null) { - jedis.close(); - } + if (jedis != null) { + jedis.close(); + } } } @@ -62,18 +62,18 @@ public abstract class JedisClusterConnectionHandler { } public void renewSlotCache() { - for (JedisPool jp : cache.getNodes().values()) { - Jedis jedis = null; - try { - jedis = jp.getResource(); - cache.discoverClusterSlots(jedis); - break; - } finally { - if (jedis != null) { - jedis.close(); - } - } - } + for (JedisPool jp : cache.getNodes().values()) { + Jedis jedis = null; + try { + jedis = jp.getResource(); + cache.discoverClusterSlots(jedis); + break; + } finally { + if (jedis != null) { + jedis.close(); + } + } + } } protected JedisPool getRandomConnection() { diff --git a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java index 0906b5e..c98cde2 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java +++ b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java @@ -22,162 +22,161 @@ public class JedisClusterInfoCache { private final Lock w = rwl.writeLock(); public void discoverClusterNodesAndSlots(Jedis jedis) { - w.lock(); + w.lock(); - try { - this.nodes.clear(); - this.slots.clear(); + try { + this.nodes.clear(); + 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())); + 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(); - } + HostAndPort targetNode = clusterNodeInfo.getNode(); + setNodeIfNotExist(targetNode); + assignSlotsToNode(clusterNodeInfo.getAvailableSlots(), + targetNode); + } + } finally { + w.unlock(); + } } public void discoverClusterSlots(Jedis jedis) { - w.lock(); + w.lock(); - try { - this.slots.clear(); + try { + this.slots.clear(); - List slots = jedis.clusterSlots(); + List slots = jedis.clusterSlots(); - for (Object slotInfoObj : slots) { - List slotInfo = (List) slotInfoObj; + for (Object slotInfoObj : slots) { + List slotInfo = (List) slotInfoObj; - if (slotInfo.size() <= 2) { - continue; - } + if (slotInfo.size() <= 2) { + continue; + } - List slotNums = getAssignedSlotArray(slotInfo); + List slotNums = getAssignedSlotArray(slotInfo); - // hostInfos - List hostInfos = (List) slotInfo.get(2); - if (hostInfos.size() <= 0) { - continue; - } + // hostInfos + List hostInfos = (List) slotInfo.get(2); + if (hostInfos.size() <= 0) { + continue; + } - // at this time, we just use master, discard slave information - HostAndPort targetNode = generateHostAndPort(hostInfos); + // at this time, we just use master, discard slave information + HostAndPort targetNode = generateHostAndPort(hostInfos); - setNodeIfNotExist(targetNode); - assignSlotsToNode(slotNums, targetNode); - } - } finally { - w.unlock(); - } + setNodeIfNotExist(targetNode); + assignSlotsToNode(slotNums, targetNode); + } + } finally { + w.unlock(); + } } private HostAndPort generateHostAndPort(List hostInfos) { - return new HostAndPort( - SafeEncoder.encode((byte[]) hostInfos.get(0)), - ((Long) hostInfos.get(1)).intValue()); + return new HostAndPort(SafeEncoder.encode((byte[]) hostInfos.get(0)), + ((Long) hostInfos.get(1)).intValue()); } public void setNodeIfNotExist(HostAndPort node) { - w.lock(); - try { - String nodeKey = getNodeKey(node); - if (nodes.containsKey(nodeKey)) - return; + w.lock(); + try { + String nodeKey = getNodeKey(node); + if (nodes.containsKey(nodeKey)) + return; - JedisPool nodePool = new JedisPool(node.getHost(), node.getPort()); - nodes.put(nodeKey, nodePool); - } finally { - w.unlock(); - } + JedisPool nodePool = new JedisPool(node.getHost(), node.getPort()); + nodes.put(nodeKey, nodePool); + } finally { + w.unlock(); + } } public void assignSlotToNode(int slot, HostAndPort targetNode) { - w.lock(); - try { - JedisPool targetPool = nodes.get(getNodeKey(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(); - } + if (targetPool == null) { + setNodeIfNotExist(targetNode); + targetPool = nodes.get(getNodeKey(targetNode)); + } + slots.put(slot, targetPool); + } finally { + w.unlock(); + } } public synchronized void assignSlotsToNode(List targetSlots, - HostAndPort targetNode) { - w.lock(); - try { - JedisPool targetPool = nodes.get(getNodeKey(targetNode)); + HostAndPort targetNode) { + w.lock(); + try { + JedisPool targetPool = nodes.get(getNodeKey(targetNode)); - if (targetPool == null) { - setNodeIfNotExist(targetNode); - targetPool = nodes.get(getNodeKey(targetNode)); - } + if (targetPool == null) { + setNodeIfNotExist(targetNode); + targetPool = nodes.get(getNodeKey(targetNode)); + } - for (Integer slot : targetSlots) { - slots.put(slot, targetPool); - } - } finally { - w.unlock(); - } + for (Integer slot : targetSlots) { + slots.put(slot, targetPool); + } + } finally { + w.unlock(); + } } public synchronized JedisPool getNode(String nodeKey) { - r.lock(); - try { - return nodes.get(nodeKey); - } finally { - r.unlock(); - } + r.lock(); + try { + return nodes.get(nodeKey); + } finally { + r.unlock(); + } } public synchronized JedisPool getSlotPool(int slot) { - r.lock(); - try { - return slots.get(slot); - } finally { - r.unlock(); - } + r.lock(); + try { + return slots.get(slot); + } finally { + r.unlock(); + } } public synchronized Map getNodes() { - r.lock(); - try { - return new HashMap(nodes); - } finally { - r.unlock(); - } + r.lock(); + try { + return new HashMap(nodes); + } finally { + r.unlock(); + } } public static String getNodeKey(HostAndPort hnp) { - return hnp.getHost() + ":" + hnp.getPort(); + return hnp.getHost() + ":" + hnp.getPort(); } public static String getNodeKey(Client client) { - return client.getHost() + ":" + client.getPort(); + return client.getHost() + ":" + client.getPort(); } public static String getNodeKey(Jedis jedis) { - return getNodeKey(jedis.getClient()); + return getNodeKey(jedis.getClient()); } private List getAssignedSlotArray(List slotInfo) { - List slotNums = new ArrayList(); - for (int slot = ((Long) slotInfo.get(0)).intValue(); - slot <= ((Long) slotInfo.get(1)).intValue(); - slot++) { - slotNums.add(slot); - } - return slotNums; + List slotNums = new ArrayList(); + for (int slot = ((Long) slotInfo.get(0)).intValue(); slot <= ((Long) slotInfo + .get(1)).intValue(); slot++) { + slotNums.add(slot); + } + return slotNums; } }