@@ -22,6 +22,7 @@ public class JedisClusterInfoCache {
2222 private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock ();
2323 private final Lock r = rwl .readLock ();
2424 private final Lock w = rwl .writeLock ();
25+ private volatile boolean rediscovering ;
2526 private final GenericObjectPoolConfig poolConfig ;
2627
2728 private int connectionTimeout ;
@@ -79,36 +80,41 @@ public void discoverClusterNodesAndSlots(Jedis jedis) {
7980 }
8081
8182 public void discoverClusterSlots (Jedis jedis ) {
82- w .lock ();
83+ //If rediscovering is already in process - no need to start one more same rediscovering, just return
84+ if (!rediscovering ) {
85+ w .lock ();
86+ rediscovering = true ;
8387
84- try {
85- this .slots .clear ();
88+ try {
89+ this .slots .clear ();
8690
87- List <Object > slots = jedis .clusterSlots ();
91+ List <Object > slots = jedis .clusterSlots ();
8892
89- for (Object slotInfoObj : slots ) {
90- List <Object > slotInfo = (List <Object >) slotInfoObj ;
93+ for (Object slotInfoObj : slots ) {
94+ List <Object > slotInfo = (List <Object >) slotInfoObj ;
9195
92- if (slotInfo .size () <= 2 ) {
93- continue ;
94- }
96+ if (slotInfo .size () <= 2 ) {
97+ continue ;
98+ }
9599
96- List <Integer > slotNums = getAssignedSlotArray (slotInfo );
100+ List <Integer > slotNums = getAssignedSlotArray (slotInfo );
97101
98- // hostInfos
99- List <Object > hostInfos = (List <Object >) slotInfo .get (2 );
100- if (hostInfos .isEmpty ()) {
101- continue ;
102- }
102+ // hostInfos
103+ List <Object > hostInfos = (List <Object >) slotInfo .get (2 );
104+ if (hostInfos .isEmpty ()) {
105+ continue ;
106+ }
103107
104- // at this time, we just use master, discard slave information
105- HostAndPort targetNode = generateHostAndPort (hostInfos );
108+ // at this time, we just use master, discard slave information
109+ HostAndPort targetNode = generateHostAndPort (hostInfos );
106110
107- setNodeIfNotExist (targetNode );
108- assignSlotsToNode (slotNums , targetNode );
111+ setNodeIfNotExist (targetNode );
112+ assignSlotsToNode (slotNums , targetNode );
113+ }
114+ } finally {
115+ rediscovering = false ;
116+ w .unlock ();
109117 }
110- } finally {
111- w .unlock ();
112118 }
113119 }
114120
0 commit comments