Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure a Sentinel Connection when initialising the Sentinel pool #1943

Merged
merged 5 commits into from Nov 26, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 39 additions & 7 deletions src/main/java/redis/clients/jedis/JedisSentinelPool.java
Expand Up @@ -17,14 +17,18 @@ public class JedisSentinelPool extends JedisPoolAbstract {

protected GenericObjectPoolConfig poolConfig;

protected int connectionTimeout = Protocol.DEFAULT_TIMEOUT;
protected int soTimeout = Protocol.DEFAULT_TIMEOUT;

protected int connectionTimeout;
protected int soTimeout;
protected String password;

protected int database = Protocol.DEFAULT_DATABASE;
protected int sentinelConnectionTimeout;
karltinawi marked this conversation as resolved.
Show resolved Hide resolved
protected int sentinelSoTimeout;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should all these fields be final?

protected String sentinelPassword;

protected int database;

protected String clientName;
protected String sentinelClientName;

protected Set<MasterListener> masterListeners = new HashSet<MasterListener>();

Expand Down Expand Up @@ -84,14 +88,36 @@ public JedisSentinelPool(String masterName, Set<String> sentinels,
}

public JedisSentinelPool(String masterName, Set<String> sentinels,
final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout,
final String password, final int database, final String clientName) {
final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final String password,
final int database, final String clientName) {
this(masterName, sentinels, poolConfig, connectionTimeout, soTimeout, password, database, clientName,
Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null);
}

public JedisSentinelPool(String masterName, Set<String> sentinels,
karltinawi marked this conversation as resolved.
Show resolved Hide resolved
final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final String password,
final int database, final String clientName,
final int sentinelConnectionTimeout, final int sentinelSoTimeout, final String sentinelPassword) {
this(masterName, sentinels, poolConfig, connectionTimeout, soTimeout, password, database, clientName,
sentinelConnectionTimeout, sentinelSoTimeout, sentinelPassword, null);
}

public JedisSentinelPool(String masterName, Set<String> sentinels,
final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final String password,
final int database, final String clientName,
final int sentinelConnectionTimeout, final int sentinelSoTimeout, final String sentinelPassword,
final String sentinelClientName) {

this.poolConfig = poolConfig;
this.connectionTimeout = connectionTimeout;
this.soTimeout = soTimeout;
this.password = password;
this.database = database;
this.clientName = clientName;
this.sentinelConnectionTimeout = sentinelConnectionTimeout;
this.sentinelSoTimeout = sentinelSoTimeout;
this.sentinelPassword = sentinelPassword;
this.sentinelClientName = sentinelClientName;

HostAndPort master = initSentinels(sentinels, masterName);
initPool(master);
Expand Down Expand Up @@ -146,7 +172,13 @@ private HostAndPort initSentinels(Set<String> sentinels, final String masterName

Jedis jedis = null;
try {
jedis = new Jedis(hap);
jedis = new Jedis(hap.getHost(), hap.getPort(), sentinelConnectionTimeout, sentinelSoTimeout);
if (sentinelPassword != null) {
jedis.auth(sentinelPassword);
}
karltinawi marked this conversation as resolved.
Show resolved Hide resolved
if (sentinelClientName != null) {
jedis.clientSetname(sentinelClientName);
}

List<String> masterAddr = jedis.sentinelGetMasterAddrByName(masterName);

Expand Down