55import java .util .List ;
66import java .util .Set ;
77import java .util .concurrent .atomic .AtomicBoolean ;
8+ import java .util .stream .Collectors ;
89
910import org .apache .commons .pool2 .impl .GenericObjectPoolConfig ;
1011import org .slf4j .Logger ;
@@ -21,7 +22,7 @@ public class JedisSentinelPool extends JedisPoolAbstract {
2122 @ Deprecated
2223 protected static Logger log = LoggerFactory .getLogger (JedisSentinelPool .class );
2324
24- protected final GenericObjectPoolConfig <Jedis > poolConfig ;
25+ @ Deprecated protected final GenericObjectPoolConfig <Jedis > poolConfig ;
2526 private final JedisFactory factory ;
2627
2728 @ Deprecated protected int connectionTimeout ;
@@ -39,6 +40,8 @@ public class JedisSentinelPool extends JedisPoolAbstract {
3940 @ Deprecated protected String sentinelPassword ;
4041 @ Deprecated protected String sentinelClientName ;
4142
43+ private final JedisClientConfig sentinelClientConfig ;
44+
4245 protected final Set <MasterListener > masterListeners = new HashSet <>();
4346
4447 private volatile HostAndPort currentHostMaster ;
@@ -177,14 +180,33 @@ public JedisSentinelPool(String masterName, Set<String> sentinels,
177180
178181 public JedisSentinelPool (String masterName , Set <String > sentinels ,
179182 final GenericObjectPoolConfig <Jedis > poolConfig , final JedisFactory factory ) {
183+ this (masterName , parseHostAndPorts (sentinels ), poolConfig , factory ,
184+ DefaultJedisClientConfig .builder ().build ());
185+ }
186+
187+ public JedisSentinelPool (String masterName , Set <HostAndPort > sentinels ,
188+ final GenericObjectPoolConfig <Jedis > poolConfig , final JedisClientConfig masteClientConfig ,
189+ final JedisClientConfig sentinelClientConfig ) {
190+ this (masterName , sentinels , poolConfig , new JedisFactory (masteClientConfig ), sentinelClientConfig );
191+ }
192+
193+ public JedisSentinelPool (String masterName , Set <HostAndPort > sentinels ,
194+ final GenericObjectPoolConfig <Jedis > poolConfig , final JedisFactory factory ,
195+ final JedisClientConfig sentinelClientConfig ) {
180196 super (poolConfig , factory );
197+
181198 this .poolConfig = poolConfig ;
182199 this .factory = factory ;
200+ this .sentinelClientConfig = sentinelClientConfig ;
183201
184202 HostAndPort master = initSentinels (sentinels , masterName );
185203 initMaster (master );
186204 }
187205
206+ private static Set <HostAndPort > parseHostAndPorts (Set <String > strings ) {
207+ return strings .parallelStream ().map (str -> HostAndPort .parseString (str )).collect (Collectors .toSet ());
208+ }
209+
188210 @ Override
189211 public void destroy () {
190212 for (MasterListener m : masterListeners ) {
@@ -212,51 +234,44 @@ private void initMaster(HostAndPort master) {
212234 }
213235 }
214236
215- private HostAndPort initSentinels (Set <String > sentinels , final String masterName ) {
237+ private HostAndPort initSentinels (Set <HostAndPort > sentinels , final String masterName ) {
216238
217239 HostAndPort master = null ;
218240 boolean sentinelAvailable = false ;
219241
220242 log .info ("Trying to find master from available Sentinels..." );
221243
222- for (String sentinel : sentinels ) {
223- final HostAndPort hap = HostAndPort .parseString (sentinel );
244+ for (HostAndPort sentinel : sentinels ) {
224245
225- log .debug ("Connecting to Sentinel {}" , hap );
246+ log .debug ("Connecting to Sentinel {}" , sentinel );
226247
227- try (Jedis jedis = new Jedis (hap .getHost (), hap .getPort (), sentinelConnectionTimeout , sentinelSoTimeout )) {
228- if (sentinelUser != null ) {
229- jedis .auth (sentinelUser , sentinelPassword );
230- } else if (sentinelPassword != null ) {
231- jedis .auth (sentinelPassword );
232- }
233- if (sentinelClientName != null ) {
234- jedis .clientSetname (sentinelClientName );
235- }
248+ try (Jedis jedis = new Jedis (sentinel , sentinelClientConfig )) {
236249
237250 List <String > masterAddr = jedis .sentinelGetMasterAddrByName (masterName );
238251
239252 // connected to sentinel...
240253 sentinelAvailable = true ;
241254
242255 if (masterAddr == null || masterAddr .size () != 2 ) {
243- log .warn ("Can not get master addr, master name: {}. Sentinel: {}" , masterName , hap );
256+ log .warn ("Can not get master addr, master name: {}. Sentinel: {}" , masterName , sentinel );
244257 continue ;
245258 }
246259
247260 master = toHostAndPort (masterAddr );
248261 log .debug ("Found Redis master at {}" , master );
249262 break ;
250263 } catch (JedisException e ) {
251- // resolves #1036, it should handle JedisException there's another chance of raising JedisDataException
252- log .warn ("Cannot get master address from sentinel running @ {}. Reason: {}. Trying next one." , hap , e );
264+ // resolves #1036, it should handle JedisException there's another chance
265+ // of raising JedisDataException
266+ log .warn (
267+ "Cannot get master address from sentinel running @ {}. Reason: {}. Trying next one." ,
268+ sentinel , e );
253269 }
254270 }
255271
256272 if (master == null ) {
257273 if (sentinelAvailable ) {
258- // can connect to sentinel, but master name seems to not
259- // monitored
274+ // can connect to sentinel, but master name seems to not monitored
260275 throw new JedisException ("Can connect to sentinel, but " + masterName
261276 + " seems to be not monitored..." );
262277 } else {
@@ -267,9 +282,9 @@ private HostAndPort initSentinels(Set<String> sentinels, final String masterName
267282
268283 log .info ("Redis master running at {}, starting Sentinel listeners..." , master );
269284
270- for (String sentinel : sentinels ) {
271- final HostAndPort hap = HostAndPort . parseString ( sentinel );
272- MasterListener masterListener = new MasterListener (masterName , hap .getHost (), hap .getPort ());
285+ for (HostAndPort sentinel : sentinels ) {
286+
287+ MasterListener masterListener = new MasterListener (masterName , sentinel .getHost (), sentinel .getPort ());
273288 // whether MasterListener threads are alive or not, process can be stopped
274289 masterListener .setDaemon (true );
275290 masterListeners .add (masterListener );
@@ -357,28 +372,22 @@ public void run() {
357372 break ;
358373 }
359374
360- j = new Jedis (host , port , sentinelConnectionTimeout , sentinelSoTimeout );
361- if (sentinelUser != null ) {
362- j .auth (sentinelUser , sentinelPassword );
363- } else if (sentinelPassword != null ) {
364- j .auth (sentinelPassword );
365- }
366- if (sentinelClientName != null ) {
367- j .clientSetname (sentinelClientName );
368- }
375+ final HostAndPort hostPort = new HostAndPort (host , port );
376+ j = new Jedis (hostPort , sentinelClientConfig );
369377
370378 // code for active refresh
371379 List <String > masterAddr = j .sentinelGetMasterAddrByName (masterName );
372380 if (masterAddr == null || masterAddr .size () != 2 ) {
373- log .warn ("Can not get master addr, master name: {}. Sentinel: {}:{}." , masterName , host , port );
381+ log .warn ("Can not get master addr, master name: {}. Sentinel: {}." , masterName ,
382+ hostPort );
374383 } else {
375384 initMaster (toHostAndPort (masterAddr ));
376385 }
377386
378387 j .subscribe (new JedisPubSub () {
379388 @ Override
380389 public void onMessage (String channel , String message ) {
381- log .debug ("Sentinel {}:{} published: {}." , host , port , message );
390+ log .debug ("Sentinel {} published: {}." , hostPort , message );
382391
383392 String [] switchMasterMsg = message .split (" " );
384393
@@ -393,9 +402,8 @@ public void onMessage(String channel, String message) {
393402 }
394403
395404 } else {
396- log .error (
397- "Invalid message received on Sentinel {}:{} on channel +switch-master: {}" , host ,
398- port , message );
405+ log .error ("Invalid message received on Sentinel {} on channel +switch-master: {}" ,
406+ hostPort , message );
399407 }
400408 }
401409 }, "+switch-master" );
@@ -427,7 +435,7 @@ public void shutdown() {
427435 running .set (false );
428436 // This isn't good, the Jedis object is not thread safe
429437 if (j != null ) {
430- j .disconnect ();
438+ j .close ();
431439 }
432440 } catch (Exception e ) {
433441 log .error ("Caught exception while shutting down: " , e );
0 commit comments