From f24a817987fe1990a9ff65b31dba7fa4b44969e6 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque Date: Sun, 3 May 2020 21:04:35 +0600 Subject: [PATCH] Fix and extend ACL implementation (#2180) * Fix/edit ACL implementation * Proper and further usage of ACL in JedisSentinelPool * ACL in many classes * minor edit and fix --- .../clients/jedis/BinaryJedisCluster.java | 41 +++++------ .../jedis/JedisClusterConnectionHandler.java | 39 ++++------- .../clients/jedis/JedisClusterInfoCache.java | 18 ++--- .../redis/clients/jedis/JedisFactory.java | 11 +-- .../java/redis/clients/jedis/JedisPool.java | 70 ++++++------------- .../clients/jedis/JedisSentinelPool.java | 61 ++++++++++------ .../JedisSlotBasedConnectionHandler.java | 28 +++++--- 7 files changed, 120 insertions(+), 148 deletions(-) diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCluster.java b/src/main/java/redis/clients/jedis/BinaryJedisCluster.java index dcfc0a330e..855928b956 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCluster.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCluster.java @@ -33,43 +33,35 @@ public class BinaryJedisCluster implements BinaryJedisClusterCommands, protected JedisClusterConnectionHandler connectionHandler; - public BinaryJedisCluster(Set nodes, int timeout) { - this(nodes, timeout, DEFAULT_MAX_ATTEMPTS, new GenericObjectPoolConfig()); - } - public BinaryJedisCluster(Set nodes) { this(nodes, DEFAULT_TIMEOUT); } + public BinaryJedisCluster(Set nodes, int timeout) { + this(nodes, timeout, DEFAULT_MAX_ATTEMPTS, new GenericObjectPoolConfig()); + } + public BinaryJedisCluster(Set jedisClusterNode, int timeout, int maxAttempts, final GenericObjectPoolConfig poolConfig) { - this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig, - timeout); - this.maxAttempts = maxAttempts; + this(jedisClusterNode, timeout, timeout, maxAttempts, poolConfig); } public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, final GenericObjectPoolConfig poolConfig) { - this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig, - connectionTimeout, soTimeout); - this.maxAttempts = maxAttempts; + this(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, null, poolConfig); } public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String password, GenericObjectPoolConfig poolConfig) { - this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig, - connectionTimeout, soTimeout, password); - this.maxAttempts = maxAttempts; + this(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, password, null, poolConfig); } public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String password, String clientName, GenericObjectPoolConfig poolConfig) { - this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig, - connectionTimeout, soTimeout, password, clientName); - this.maxAttempts = maxAttempts; + this(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, null, password, clientName, poolConfig); } public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String user, String password, String clientName, GenericObjectPoolConfig poolConfig) { this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig, - connectionTimeout, soTimeout, user, password, clientName); + connectionTimeout, soTimeout, user, password, clientName); this.maxAttempts = maxAttempts; } @@ -85,17 +77,16 @@ public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeo public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String password, String clientName, GenericObjectPoolConfig poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { - this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig, - connectionTimeout, soTimeout, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); - this.maxAttempts = maxAttempts; + this(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, null, password, clientName, + poolConfig, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } - public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, - String user, String password, String clientName, GenericObjectPoolConfig poolConfig, - boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, - HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { + public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, + int maxAttempts, String user, String password, String clientName, GenericObjectPoolConfig poolConfig, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig, - connectionTimeout, soTimeout, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); + connectionTimeout, soTimeout, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); this.maxAttempts = maxAttempts; } diff --git a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java index a9f748c8b8..ce891516d7 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java @@ -14,38 +14,35 @@ public abstract class JedisClusterConnectionHandler implements Closeable { protected final JedisClusterInfoCache cache; - public JedisClusterConnectionHandler(Set nodes, - final GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password) { + public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + int connectionTimeout, int soTimeout, String password) { this(nodes, poolConfig, connectionTimeout, soTimeout, password, null); } - public JedisClusterConnectionHandler(Set nodes, - final GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password, String clientName) { - this(nodes, poolConfig, connectionTimeout, soTimeout, password, clientName, false, null, null, null, null); + public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + int connectionTimeout, int soTimeout, String password, String clientName) { + this(nodes, poolConfig, connectionTimeout, soTimeout, null, password, clientName); } - public JedisClusterConnectionHandler(Set nodes, final GenericObjectPoolConfig poolConfig, + public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String user, String password, String clientName) { this(nodes, poolConfig, connectionTimeout, soTimeout, user, password, clientName, false, null, null, null, null); } - public JedisClusterConnectionHandler(Set nodes, - final GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password, String clientName, + public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + int connectionTimeout, int soTimeout, String password, String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap portMap) { - this.cache = new JedisClusterInfoCache(poolConfig, connectionTimeout, soTimeout, password, clientName, - ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); - initializeSlotsCache(nodes, connectionTimeout, soTimeout, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier); + this(nodes, poolConfig, connectionTimeout, soTimeout, null, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); } - public JedisClusterConnectionHandler(Set nodes, - final GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, - String user, String password, String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, - SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap portMap) { + public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + int connectionTimeout, int soTimeout, String user, String password, String clientName, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap portMap) { this.cache = new JedisClusterInfoCache(poolConfig, connectionTimeout, soTimeout, user, password, clientName, - ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); + ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); initializeSlotsCache(nodes, connectionTimeout, soTimeout, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier); - } abstract Jedis getConnection(); @@ -60,14 +57,6 @@ public Map getNodes() { return cache.getNodes(); } - private void initializeSlotsCache(Set startNodes, - int connectionTimeout, int soTimeout, String password, String clientName, - boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) { - - initializeSlotsCache(startNodes, connectionTimeout, soTimeout, null, password, clientName, - ssl, sslSocketFactory, sslParameters, hostnameVerifier ); - } - private void initializeSlotsCache(Set startNodes, int connectionTimeout, int soTimeout, String user, String password, String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) { diff --git a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java index 5abc628e61..e5aefa17d6 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java +++ b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java @@ -47,25 +47,25 @@ public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, int timeo public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final String password, final String clientName) { - this(poolConfig, connectionTimeout, soTimeout, password, clientName, false, null, null, null, null); + this(poolConfig, connectionTimeout, soTimeout, null, password, clientName); } public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, - final int connectionTimeout, final int soTimeout, final String user, final String password, final String clientName) { + final int connectionTimeout, final int soTimeout, final String user, final String password, final String clientName) { this(poolConfig, connectionTimeout, soTimeout, user, password, clientName, false, null, null, null, null); } - public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, - final int connectionTimeout, final int soTimeout, final String password, final String clientName, + public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, + final int soTimeout, final String password, final String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { this(poolConfig, connectionTimeout, soTimeout, null, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } - public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, - final int connectionTimeout, final int soTimeout, final String user, final String password, - final String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, - HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { + public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, + final int soTimeout, final String user, final String password, final String clientName, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { this.poolConfig = poolConfig; this.connectionTimeout = connectionTimeout; this.soTimeout = soTimeout; @@ -202,7 +202,7 @@ public JedisPool setupNodeIfNotExist(HostAndPort node) { if (existingPool != null) return existingPool; JedisPool nodePool = new JedisPool(poolConfig, node.getHost(), node.getPort(), - connectionTimeout, soTimeout, password, 0, clientName, + connectionTimeout, soTimeout, user, password, 0, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier); nodes.put(nodeKey, nodePool); return nodePool; diff --git a/src/main/java/redis/clients/jedis/JedisFactory.java b/src/main/java/redis/clients/jedis/JedisFactory.java index a9d143db87..cb1921d617 100644 --- a/src/main/java/redis/clients/jedis/JedisFactory.java +++ b/src/main/java/redis/clients/jedis/JedisFactory.java @@ -104,7 +104,6 @@ public void activateObject(PooledObject pooledJedis) throws Exception { if (jedis.getDB() != database) { jedis.select(database); } - } @Override @@ -118,18 +117,15 @@ public void destroyObject(PooledObject pooledJedis) throws Exception { } jedis.disconnect(); } catch (Exception e) { - } } - } @Override public PooledObject makeObject() throws Exception { - final HostAndPort hostAndPort = this.hostAndPort.get(); - final Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), connectionTimeout, - soTimeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier); - + final HostAndPort hp = this.hostAndPort.get(); + final Jedis jedis = new Jedis(hp.getHost(), hp.getPort(), connectionTimeout, soTimeout, + ssl, sslSocketFactory, sslParameters, hostnameVerifier); try { jedis.connect(); if (user != null) { @@ -149,7 +145,6 @@ public PooledObject makeObject() throws Exception { } return new DefaultPooledObject<>(jedis); - } @Override diff --git a/src/main/java/redis/clients/jedis/JedisPool.java b/src/main/java/redis/clients/jedis/JedisPool.java index 8e8b6d0ffa..c87dffe49a 100644 --- a/src/main/java/redis/clients/jedis/JedisPool.java +++ b/src/main/java/redis/clients/jedis/JedisPool.java @@ -29,10 +29,10 @@ public JedisPool(String host, int port) { public JedisPool(final String host) { URI uri = URI.create(host); if (JedisURIHelper.isValid(uri)) { - this.internalPool = new GenericObjectPool(new JedisFactory(uri, + this.internalPool = new GenericObjectPool<>(new JedisFactory(uri, Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null), new GenericObjectPoolConfig()); } else { - this.internalPool = new GenericObjectPool(new JedisFactory(host, + this.internalPool = new GenericObjectPool<>(new JedisFactory(host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null), new GenericObjectPoolConfig()); } @@ -42,11 +42,11 @@ public JedisPool(final String host, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { URI uri = URI.create(host); if (JedisURIHelper.isValid(uri)) { - this.internalPool = new GenericObjectPool(new JedisFactory(uri, + this.internalPool = new GenericObjectPool<>(new JedisFactory(uri, Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null, sslSocketFactory, sslParameters, hostnameVerifier), new GenericObjectPoolConfig()); } else { - this.internalPool = new GenericObjectPool(new JedisFactory(host, + this.internalPool = new GenericObjectPool<>(new JedisFactory(host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null, false, null, null, null), new GenericObjectPoolConfig()); } @@ -77,7 +77,7 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in } public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, - int timeout, final String user, final String password) { + int timeout, final String user, final String password) { this(poolConfig, host, port, timeout, user, password, Protocol.DEFAULT_DATABASE); } @@ -87,7 +87,7 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in } public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, - int timeout, final String user, final String password, final boolean ssl) { + int timeout, final String user, final String password, final boolean ssl) { this(poolConfig, host, port, timeout, user, password, Protocol.DEFAULT_DATABASE, ssl); } @@ -99,14 +99,6 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in sslSocketFactory, sslParameters, hostnameVerifier); } - public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, - int timeout, final String user, final String password, final boolean ssl, - final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, - final HostnameVerifier hostnameVerifier) { - this(poolConfig, host, port, timeout, user, password, Protocol.DEFAULT_DATABASE, ssl, - sslSocketFactory, sslParameters, hostnameVerifier); - } - public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, final int port) { this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT); } @@ -146,7 +138,7 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in } public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, - int timeout, final String user, final String password, final int database) { + int timeout, final String user, final String password, final int database) { this(poolConfig, host, port, timeout, user, password, database, null); } @@ -156,7 +148,7 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in } public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, - int timeout, final String user, final String password, final int database, final boolean ssl) { + int timeout, final String user, final String password, final int database, final boolean ssl) { this(poolConfig, host, port, timeout, user, password, database, null, ssl); } @@ -168,21 +160,13 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in sslParameters, hostnameVerifier); } - public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, - int timeout, final String user, final String password, final int database, final boolean ssl, - final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, - final HostnameVerifier hostnameVerifier) { - this(poolConfig, host, port, timeout, user, password, database, null, ssl, sslSocketFactory, - sslParameters, hostnameVerifier); - } - public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, int timeout, final String password, final int database, final String clientName) { this(poolConfig, host, port, timeout, timeout, password, database, clientName); } public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, - int timeout, final String user, final String password, final int database, final String clientName) { + int timeout, final String user, final String password, final int database, final String clientName) { this(poolConfig, host, port, timeout, timeout, user, password, database, clientName); } @@ -193,8 +177,8 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in } public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, - int timeout, final String user, final String password, final int database, - final String clientName, final boolean ssl) { + int timeout, final String user, final String password, final int database, final String clientName, + final boolean ssl) { this(poolConfig, host, port, timeout, timeout, user, password, database, clientName, ssl); } @@ -206,14 +190,6 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in sslSocketFactory, sslParameters, hostnameVerifier); } - public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, - int timeout, final String user, final String password, final int database, final String clientName, - final boolean ssl, final SSLSocketFactory sslSocketFactory, - final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - this(poolConfig, host, port, timeout, timeout, user, password, database, clientName, ssl, - sslSocketFactory, sslParameters, hostnameVerifier); - } - public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, final int connectionTimeout, final int soTimeout, final String password, final int database, final String clientName, final boolean ssl, final SSLSocketFactory sslSocketFactory, @@ -223,12 +199,12 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in } public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, - final int connectionTimeout, final int soTimeout, final String user, final String password, - final int database, final String clientName, final boolean ssl, - final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, - final HostnameVerifier hostnameVerifier) { + final int connectionTimeout, final int soTimeout, final String user, final String password, + final int database, final String clientName, final boolean ssl, + final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, + final HostnameVerifier hostnameVerifier) { super(poolConfig, new JedisFactory(host, port, connectionTimeout, soTimeout, user, password, - database, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier)); + database, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier)); } public JedisPool(final GenericObjectPoolConfig poolConfig) { @@ -247,10 +223,10 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in } public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, int port, - final int connectionTimeout, final int soTimeout, final String user, final String password, - final int database, final String clientName) { + final int connectionTimeout, final int soTimeout, final String user, final String password, + final int database, final String clientName) { super(poolConfig, new JedisFactory(host, port, connectionTimeout, soTimeout, user, password, - database, clientName)); + database, clientName)); } public JedisPool(final String host, final int port, final boolean ssl, @@ -268,10 +244,10 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, fi } public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, final int port, - final int connectionTimeout, final int soTimeout, final String user, - final String password, final int database, final String clientName, final boolean ssl) { + final int connectionTimeout, final int soTimeout, final String user, final String password, + final int database, final String clientName, final boolean ssl) { this(poolConfig, host, port, connectionTimeout, soTimeout, user, password, database, clientName, ssl, - null, null, null); + null, null, null); } public JedisPool(final GenericObjectPoolConfig poolConfig, final URI uri) { @@ -297,7 +273,7 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final URI uri, final public JedisPool(final GenericObjectPoolConfig poolConfig, final URI uri, final int connectionTimeout, final int soTimeout) { - super(poolConfig, new JedisFactory(uri, connectionTimeout, soTimeout, null)); + this(poolConfig, uri, connectionTimeout, soTimeout, null, null, null); } public JedisPool(final GenericObjectPoolConfig poolConfig, final URI uri, diff --git a/src/main/java/redis/clients/jedis/JedisSentinelPool.java b/src/main/java/redis/clients/jedis/JedisSentinelPool.java index aacae5253c..5e18124c13 100644 --- a/src/main/java/redis/clients/jedis/JedisSentinelPool.java +++ b/src/main/java/redis/clients/jedis/JedisSentinelPool.java @@ -26,6 +26,7 @@ public class JedisSentinelPool extends JedisPoolAbstract { protected int sentinelConnectionTimeout; protected int sentinelSoTimeout; + protected String sentinelUser; protected String sentinelPassword; protected String sentinelClientName; @@ -54,8 +55,8 @@ public JedisSentinelPool(String masterName, Set sentinels, String passwo } public JedisSentinelPool(String masterName, Set sentinels, String password, String sentinelPassword) { - this(masterName, sentinels, new GenericObjectPoolConfig(), Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, - null, password, Protocol.DEFAULT_DATABASE, null, Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, sentinelPassword, null); + this(masterName, sentinels, new GenericObjectPoolConfig(), Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, + password, Protocol.DEFAULT_DATABASE, null, Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, sentinelPassword, null); } public JedisSentinelPool(String masterName, Set sentinels, @@ -80,53 +81,62 @@ public JedisSentinelPool(String masterName, Set sentinels, } public JedisSentinelPool(String masterName, Set sentinels, - final GenericObjectPoolConfig poolConfig, int timeout, final String user, - final String password, final int database) { + final GenericObjectPoolConfig poolConfig, int timeout, final String user, + final String password, final int database) { this(masterName, sentinels, poolConfig, timeout, timeout, user, password, database); } - public JedisSentinelPool(String masterName, Set sentinels, final GenericObjectPoolConfig poolConfig, int timeout, final String password, final int database, final String clientName) { - this(masterName, sentinels, poolConfig, timeout, timeout, null, password, database, clientName, - Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null, null); + this(masterName, sentinels, poolConfig, timeout, timeout, password, database, clientName); } public JedisSentinelPool(String masterName, Set sentinels, - final GenericObjectPoolConfig poolConfig, int timeout, - final String user, final String password, - final int database, final String clientName) { - this(masterName, sentinels, poolConfig, timeout, timeout, user, password, database, clientName, - Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null, null); + final GenericObjectPoolConfig poolConfig, int timeout, final String user, + final String password, final int database, final String clientName) { + this(masterName, sentinels, poolConfig, timeout, timeout, user, password, database, clientName); } public JedisSentinelPool(String masterName, Set sentinels, final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final String password, final int database) { - this(masterName, sentinels, poolConfig, connectionTimeout, soTimeout, null, password, database, null, - Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null, null); + this(masterName, sentinels, poolConfig, connectionTimeout, soTimeout, null, password, database, null); } public JedisSentinelPool(String masterName, Set sentinels, - final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, - final String user, final String password, final int database) { - this(masterName, sentinels, poolConfig, connectionTimeout, soTimeout, user, password, database, null, - Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null, null); + final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, + final String user, final String password, final int database) { + this(masterName, sentinels, poolConfig, connectionTimeout, soTimeout, user, password, database, null); } public JedisSentinelPool(String masterName, Set sentinels, final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final String password, final int database, final String clientName) { + this(masterName, sentinels, poolConfig, connectionTimeout, soTimeout, null, password, database, clientName); + } + + public JedisSentinelPool(String masterName, Set sentinels, + final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, + final String user, final String password, final int database, final String clientName) { this(masterName, sentinels, poolConfig, connectionTimeout, soTimeout, null, password, database, clientName, - Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null, null); + Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null, null, null); } public JedisSentinelPool(String masterName, Set sentinels, final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, - final String user, final String password, final int database, final String clientName, + final String password, final int database, final String clientName, final int sentinelConnectionTimeout, final int sentinelSoTimeout, final String sentinelPassword, final String sentinelClientName) { + this(masterName, sentinels, poolConfig, connectionTimeout, soTimeout, null, password, database, clientName, + sentinelConnectionTimeout, sentinelSoTimeout, null, sentinelPassword, sentinelClientName); + } + + public JedisSentinelPool(String masterName, Set sentinels, + final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, + final String user, final String password, final int database, final String clientName, + final int sentinelConnectionTimeout, final int sentinelSoTimeout, final String sentinelUser, + final String sentinelPassword, final String sentinelClientName) { this.poolConfig = poolConfig; this.connectionTimeout = connectionTimeout; @@ -137,6 +147,7 @@ public JedisSentinelPool(String masterName, Set sentinels, this.clientName = clientName; this.sentinelConnectionTimeout = sentinelConnectionTimeout; this.sentinelSoTimeout = sentinelSoTimeout; + this.sentinelUser = sentinelUser; this.sentinelPassword = sentinelPassword; this.sentinelClientName = sentinelClientName; @@ -163,7 +174,7 @@ private void initPool(HostAndPort master) { currentHostMaster = master; if (factory == null) { factory = new JedisFactory(master.getHost(), master.getPort(), connectionTimeout, - soTimeout, password, database, clientName); + soTimeout, user, password, database, clientName); initPool(poolConfig, factory); } else { factory.setHostAndPort(currentHostMaster); @@ -194,7 +205,9 @@ private HostAndPort initSentinels(Set sentinels, final String masterName Jedis jedis = null; try { jedis = new Jedis(hap.getHost(), hap.getPort(), sentinelConnectionTimeout, sentinelSoTimeout); - if (sentinelPassword != null) { + if (sentinelUser != null) { + jedis.auth(sentinelUser, sentinelPassword); + } else if (sentinelPassword != null) { jedis.auth(sentinelPassword); } if (sentinelClientName != null) { @@ -338,7 +351,9 @@ public void run() { } j = new Jedis(host, port, sentinelConnectionTimeout, sentinelSoTimeout); - if (sentinelPassword != null) { + if (sentinelUser != null) { + j.auth(sentinelUser, sentinelPassword); + } else if (sentinelPassword != null) { j.auth(sentinelPassword); } if (sentinelClientName != null) { diff --git a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java index 21b2ab9a15..6deb39c75f 100644 --- a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java @@ -20,31 +20,37 @@ public JedisSlotBasedConnectionHandler(Set nodes, public JedisSlotBasedConnectionHandler(Set nodes, final GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout) { - super(nodes, poolConfig, connectionTimeout, soTimeout, null); + this(nodes, poolConfig, connectionTimeout, soTimeout, null); } - public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password) { + public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + int connectionTimeout, int soTimeout, String password) { super(nodes, poolConfig, connectionTimeout, soTimeout, password); } - public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password, String clientName) { + public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + int connectionTimeout, int soTimeout, String password, String clientName) { super(nodes, poolConfig, connectionTimeout, soTimeout, password, clientName); } - public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, - String user, String password, String clientName) { + public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + int connectionTimeout, int soTimeout, String user, String password, String clientName) { super(nodes, poolConfig, connectionTimeout, soTimeout, user, password, clientName); } - public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password, String clientName, - boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap portMap) { + public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + int connectionTimeout, int soTimeout, String password, String clientName, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap portMap) { super(nodes, poolConfig, connectionTimeout, soTimeout, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); } - public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, - String user, String password, String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, - SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap portMap) { - super(nodes, poolConfig, connectionTimeout, soTimeout, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); + public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + int connectionTimeout, int soTimeout, String user, String password, String clientName, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap portMap) { + super(nodes, poolConfig, connectionTimeout, soTimeout, user, password, clientName, + ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); } @Override