Skip to content

Commit 4d85f61

Browse files
committed
DATAREDIS-552 - Polishing.
Apply client name to Sentinel and unpooled connections if set. Add ticket references to JavaDoc. Add tests. Fix existing JavaDoc letter casing and punctuation. Rearrange isClusterAware/isRedisSentinelAware methods.
1 parent b92ba3d commit 4d85f61

File tree

7 files changed

+148
-74
lines changed

7 files changed

+148
-74
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.springframework.util.CollectionUtils;
6969
import org.springframework.util.ObjectUtils;
7070
import org.springframework.util.ReflectionUtils;
71+
import org.springframework.util.StringUtils;
7172

7273
import redis.clients.jedis.BinaryJedis;
7374
import redis.clients.jedis.BinaryJedisPubSub;
@@ -149,6 +150,7 @@ public class JedisConnection extends AbstractRedisConnection {
149150
private volatile JedisSubscription subscription;
150151
private volatile Pipeline pipeline;
151152
private final int dbIndex;
153+
private final String clientName;
152154
private boolean convertPipelineAndTxResults = true;
153155
private List<FutureResult<Response<?>>> pipelinedResults = new ArrayList<FutureResult<Response<?>>>();
154156
private Queue<FutureResult<Response<?>>> txResults = new LinkedList<FutureResult<Response<?>>>();
@@ -192,15 +194,30 @@ public JedisConnection(Jedis jedis) {
192194
*
193195
* @param jedis
194196
* @param pool can be null, if no pool is used
197+
* @param dbIndex
195198
*/
196199
public JedisConnection(Jedis jedis, Pool<Jedis> pool, int dbIndex) {
197-
this.jedis = jedis;
200+
this(jedis, pool, dbIndex, null);
201+
}
202+
203+
/**
204+
* Constructs a new <code>JedisConnection</code> instance backed by a jedis pool.
205+
*
206+
* @param jedis
207+
* @param pool can be null, if no pool is used
208+
* @param dbIndex
209+
* @param clientName the client name, can be {@literal null}.
210+
* @since 1.8
211+
*/
212+
protected JedisConnection(Jedis jedis, Pool<Jedis> pool, int dbIndex, String clientName) {
213+
198214
// extract underlying connection for batch operations
199215
client = (Client) ReflectionUtils.getField(CLIENT_FIELD, jedis);
200216

217+
this.jedis = jedis;
201218
this.pool = pool;
202-
203219
this.dbIndex = dbIndex;
220+
this.clientName = clientName;
204221

205222
// select the db
206223
// if this fail, do manual clean-up before propagating the exception
@@ -3988,7 +4005,14 @@ protected JedisSentinelConnection getSentinelConnection(RedisNode sentinel) {
39884005
}
39894006

39904007
protected Jedis getJedis(RedisNode node) {
3991-
return new Jedis(node.getHost(), node.getPort());
4008+
4009+
Jedis jedis = new Jedis(node.getHost(), node.getPort());
4010+
4011+
if (StringUtils.hasText(clientName)) {
4012+
jedis.clientSetname(clientName);
4013+
}
4014+
4015+
return jedis;
39924016
}
39934017

39944018
@Override

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnectionFactory.java

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,12 @@ protected Jedis fetchJedisConnector() {
193193
if (usePool && pool != null) {
194194
return pool.getResource();
195195
}
196+
196197
Jedis jedis = new Jedis(getShardInfo());
197198
// force initialization (see Jedis issue #82)
198199
jedis.connect();
200+
201+
potentiallySetClientName(jedis);
199202
return jedis;
200203
} catch (Exception ex) {
201204
throw new RedisConnectionFailureException("Cannot get Jedis connection", ex);
@@ -343,8 +346,8 @@ public RedisConnection getConnection() {
343346
}
344347

345348
Jedis jedis = fetchJedisConnector();
346-
JedisConnection connection = (usePool ? new JedisConnection(jedis, pool, dbIndex)
347-
: new JedisConnection(jedis, null, dbIndex));
349+
JedisConnection connection = (usePool ? new JedisConnection(jedis, pool, dbIndex, clientName)
350+
: new JedisConnection(jedis, null, dbIndex, clientName));
348351
connection.setConvertPipelineAndTxResults(convertPipelineAndTxResults);
349352
return postProcessConnection(connection);
350353
}
@@ -373,7 +376,7 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
373376
/**
374377
* Returns the Redis hostName.
375378
*
376-
* @return Returns the hostName
379+
* @return the hostName.
377380
*/
378381
public String getHostName() {
379382
return hostName;
@@ -382,7 +385,7 @@ public String getHostName() {
382385
/**
383386
* Sets the Redis hostName.
384387
*
385-
* @param hostName The hostName to set.
388+
* @param hostName the hostName to set.
386389
*/
387390
public void setHostName(String hostName) {
388391
this.hostName = hostName;
@@ -401,7 +404,7 @@ public void setUseSsl(boolean useSsl) {
401404
/**
402405
* Returns whether to use SSL.
403406
*
404-
* @return use of SSL
407+
* @return use of SSL.
405408
* @since 1.8
406409
*/
407410
public boolean isUseSsl() {
@@ -411,7 +414,7 @@ public boolean isUseSsl() {
411414
/**
412415
* Returns the password used for authenticating with the Redis server.
413416
*
414-
* @return password for authentication
417+
* @return password for authentication.
415418
*/
416419
public String getPassword() {
417420
return password;
@@ -420,7 +423,7 @@ public String getPassword() {
420423
/**
421424
* Sets the password used for authenticating with the Redis server.
422425
*
423-
* @param password the password to set
426+
* @param password the password to set.
424427
*/
425428
public void setPassword(String password) {
426429
this.password = password;
@@ -429,7 +432,7 @@ public void setPassword(String password) {
429432
/**
430433
* Returns the port used to connect to the Redis instance.
431434
*
432-
* @return Redis port.
435+
* @return the Redis port.
433436
*/
434437
public int getPort() {
435438
return port;
@@ -438,7 +441,7 @@ public int getPort() {
438441
/**
439442
* Sets the port used to connect to the Redis instance.
440443
*
441-
* @param port Redis port
444+
* @param port the Redis port.
442445
*/
443446
public void setPort(int port) {
444447
this.port = port;
@@ -447,7 +450,7 @@ public void setPort(int port) {
447450
/**
448451
* Returns the shardInfo.
449452
*
450-
* @return Returns the shardInfo
453+
* @return the shardInfo.
451454
*/
452455
public JedisShardInfo getShardInfo() {
453456
return shardInfo;
@@ -456,7 +459,7 @@ public JedisShardInfo getShardInfo() {
456459
/**
457460
* Sets the shard info for this factory.
458461
*
459-
* @param shardInfo The shardInfo to set.
462+
* @param shardInfo the shardInfo to set.
460463
*/
461464
public void setShardInfo(JedisShardInfo shardInfo) {
462465
this.shardInfo = shardInfo;
@@ -465,14 +468,16 @@ public void setShardInfo(JedisShardInfo shardInfo) {
465468
/**
466469
* Returns the timeout.
467470
*
468-
* @return Returns the timeout
471+
* @return the timeout.
469472
*/
470473
public int getTimeout() {
471474
return timeout;
472475
}
473476

474477
/**
475-
* @param timeout The timeout to set.
478+
* Sets the timeout.
479+
*
480+
* @param timeout the timeout to set.
476481
*/
477482
public void setTimeout(int timeout) {
478483
this.timeout = timeout;
@@ -481,7 +486,7 @@ public void setTimeout(int timeout) {
481486
/**
482487
* Indicates the use of a connection pool.
483488
*
484-
* @return Returns the use of connection pooling.
489+
* @return the use of connection pooling.
485490
*/
486491
public boolean getUsePool() {
487492
return usePool;
@@ -490,7 +495,7 @@ public boolean getUsePool() {
490495
/**
491496
* Turns on or off the use of connection pooling.
492497
*
493-
* @param usePool The usePool to set.
498+
* @param usePool the usePool to set.
494499
*/
495500
public void setUsePool(boolean usePool) {
496501
this.usePool = usePool;
@@ -499,7 +504,7 @@ public void setUsePool(boolean usePool) {
499504
/**
500505
* Returns the poolConfig.
501506
*
502-
* @return Returns the poolConfig
507+
* @return the poolConfig
503508
*/
504509
public JedisPoolConfig getPoolConfig() {
505510
return poolConfig;
@@ -508,7 +513,7 @@ public JedisPoolConfig getPoolConfig() {
508513
/**
509514
* Sets the pool configuration for this factory.
510515
*
511-
* @param poolConfig The poolConfig to set.
516+
* @param poolConfig the poolConfig to set.
512517
*/
513518
public void setPoolConfig(JedisPoolConfig poolConfig) {
514519
this.poolConfig = poolConfig;
@@ -517,7 +522,7 @@ public void setPoolConfig(JedisPoolConfig poolConfig) {
517522
/**
518523
* Returns the index of the database.
519524
*
520-
* @return Returns the database index
525+
* @return the database index.
521526
*/
522527
public int getDatabase() {
523528
return dbIndex;
@@ -526,26 +531,28 @@ public int getDatabase() {
526531
/**
527532
* Sets the index of the database used by this connection factory. Default is 0.
528533
*
529-
* @param index database index
534+
* @param index database index.
530535
*/
531536
public void setDatabase(int index) {
532537
Assert.isTrue(index >= 0, "invalid DB index (a positive index required)");
533538
this.dbIndex = index;
534539
}
535-
540+
536541
/**
537542
* Returns the client name.
538-
*
539-
* @return Returns the client name
543+
*
544+
* @return the client name.
545+
* @since 1.8
540546
*/
541547
public String getClientName() {
542548
return clientName;
543549
}
544550

545551
/**
546-
* Sets the client name used by this connection factory. Default is empty.
547-
*
548-
* @param clientName client name
552+
* Sets the client name used by this connection factory. Defaults to none which does not set a client name.
553+
*
554+
* @param clientName the client name.
555+
* @since 1.8
549556
*/
550557
public void setClientName(String clientName) {
551558
this.clientName = clientName;
@@ -554,9 +561,9 @@ public void setClientName(String clientName) {
554561
/**
555562
* Specifies if pipelined results should be converted to the expected data type. If false, results of
556563
* {@link JedisConnection#closePipeline()} and {@link JedisConnection#exec()} will be of the type returned by the
557-
* Jedis driver
564+
* Jedis driver.
558565
*
559-
* @return Whether or not to convert pipeline and tx results
566+
* @return Whether or not to convert pipeline and tx results.
560567
*/
561568
public boolean getConvertPipelineAndTxResults() {
562569
return convertPipelineAndTxResults;
@@ -565,9 +572,9 @@ public boolean getConvertPipelineAndTxResults() {
565572
/**
566573
* Specifies if pipelined results should be converted to the expected data type. If false, results of
567574
* {@link JedisConnection#closePipeline()} and {@link JedisConnection#exec()} will be of the type returned by the
568-
* Jedis driver
575+
* Jedis driver.
569576
*
570-
* @param convertPipelineAndTxResults Whether or not to convert pipeline and tx results
577+
* @param convertPipelineAndTxResults Whether or not to convert pipeline and tx results.
571578
*/
572579
public void setConvertPipelineAndTxResults(boolean convertPipelineAndTxResults) {
573580
this.convertPipelineAndTxResults = convertPipelineAndTxResults;
@@ -597,14 +604,19 @@ public RedisSentinelConnection getSentinelConnection() {
597604
private Jedis getActiveSentinel() {
598605

599606
Assert.notNull(this.sentinelConfig);
607+
600608
for (RedisNode node : this.sentinelConfig.getSentinels()) {
609+
601610
Jedis jedis = new Jedis(node.getHost(), node.getPort());
611+
602612
if (jedis.ping().equalsIgnoreCase("pong")) {
613+
614+
potentiallySetClientName(jedis);
603615
return jedis;
604616
}
605617
}
606618

607-
throw new InvalidDataAccessResourceUsageException("no sentinel found");
619+
throw new InvalidDataAccessResourceUsageException("No Sentinel found");
608620
}
609621

610622
private Set<String> convertToJedisSentinelSet(Collection<RedisNode> nodes) {
@@ -622,6 +634,13 @@ private Set<String> convertToJedisSentinelSet(Collection<RedisNode> nodes) {
622634
return convertedNodes;
623635
}
624636

637+
private void potentiallySetClientName(Jedis jedis) {
638+
639+
if (StringUtils.hasText(clientName)) {
640+
jedis.clientSetname(clientName);
641+
}
642+
}
643+
625644
private void setTimeoutOn(JedisShardInfo shardInfo, int timeout) {
626645
ReflectionUtils.invokeMethod(SET_TIMEOUT_METHOD, shardInfo, timeout);
627646
}

0 commit comments

Comments
 (0)