|
4 | 4 | import static org.junit.Assert.assertFalse;
|
5 | 5 | import static org.junit.Assert.assertNotNull;
|
6 | 6 | import static org.junit.Assert.assertNull;
|
| 7 | +import static org.junit.Assert.assertTrue; |
7 | 8 | import static org.junit.Assert.fail;
|
8 | 9 | import static redis.clients.jedis.tests.utils.AssertUtil.assertByteArraySetEquals;
|
9 | 10 |
|
@@ -805,6 +806,63 @@ public void nullKeys() {
|
805 | 806 | }
|
806 | 807 | }
|
807 | 808 |
|
| 809 | + |
| 810 | + @Test |
| 811 | + public void clusterRefreshNodes() throws Exception { |
| 812 | + Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); |
| 813 | + jedisClusterNode.add(nodeInfo1); |
| 814 | + jedisClusterNode.add(nodeInfo2); |
| 815 | + jedisClusterNode.add(nodeInfo3); |
| 816 | + |
| 817 | + try (JedisCluster cluster = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, |
| 818 | + DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG)) { |
| 819 | + assertEquals(3, cluster.getClusterNodes().size()); |
| 820 | + cleanUp(); // cleanup and add node4 |
| 821 | + |
| 822 | + // at first, join node4 to cluster |
| 823 | + node1.clusterMeet(LOCAL_IP, nodeInfo2.getPort()); |
| 824 | + node1.clusterMeet(LOCAL_IP, nodeInfo3.getPort()); |
| 825 | + node1.clusterMeet(LOCAL_IP, nodeInfo4.getPort()); |
| 826 | + // split available slots across the three nodes |
| 827 | + int slotsPerNode = JedisCluster.HASHSLOTS / 4; |
| 828 | + int[] node1Slots = new int[slotsPerNode]; |
| 829 | + int[] node2Slots = new int[slotsPerNode]; |
| 830 | + int[] node3Slots = new int[slotsPerNode]; |
| 831 | + int[] node4Slots = new int[slotsPerNode]; |
| 832 | + for (int i = 0, slot1 = 0, slot2 = 0, slot3 = 0, slot4 = 0; i < JedisCluster.HASHSLOTS; i++) { |
| 833 | + if (i < slotsPerNode) { |
| 834 | + node1Slots[slot1++] = i; |
| 835 | + } else if (i >= slotsPerNode && i < slotsPerNode*2) { |
| 836 | + node2Slots[slot2++] = i; |
| 837 | + } else if (i >= slotsPerNode*2 && i < slotsPerNode*3) { |
| 838 | + node3Slots[slot3++] = i; |
| 839 | + } else { |
| 840 | + node4Slots[slot4++] = i; |
| 841 | + } |
| 842 | + } |
| 843 | + |
| 844 | + node1.clusterAddSlots(node1Slots); |
| 845 | + node2.clusterAddSlots(node2Slots); |
| 846 | + node3.clusterAddSlots(node3Slots); |
| 847 | + node4.clusterAddSlots(node4Slots); |
| 848 | + JedisClusterTestUtil.waitForClusterReady(node1, node2, node3, node4); |
| 849 | + |
| 850 | + // cluster.set("key", "value"); will get JedisMovedDataException and renewSlotCache |
| 851 | + cluster.set("key", "value"); |
| 852 | + |
| 853 | + assertEquals(4, cluster.getClusterNodes().size()); |
| 854 | + String nodeKey4 = LOCAL_IP + ":" + nodeInfo4.getPort(); |
| 855 | + assertTrue(cluster.getClusterNodes().keySet().contains(nodeKey4)); |
| 856 | + |
| 857 | + // make 4 nodes to 3 nodes |
| 858 | + cleanUp(); |
| 859 | + setUp(); |
| 860 | + // cluster.set("bar", "foo") will get JedisMovedDataException and renewSlotCache |
| 861 | + cluster.set("bar", "foo"); |
| 862 | + assertEquals(3, cluster.getClusterNodes().size()); |
| 863 | + } |
| 864 | + } |
| 865 | + |
808 | 866 | @Test
|
809 | 867 | public void georadiusStore() {
|
810 | 868 | Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
|
|
0 commit comments