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

Add support to the use of JedisSocketFactory using a pool #2293

Merged
merged 7 commits into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,21 @@ private void initializeFromURI(URI uri) {
}
}

/**
* @deprecated This will constructor will be removed in future releases
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
*
* Use {@link BinaryJedis#BinaryJedis(redis.clients.jedis.JedisSocketFactory, redis.clients.jedis.JedisClientConfig)}
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
*/
@Deprecated
public BinaryJedis(final JedisSocketFactory jedisSocketFactory) {
client = new Client(jedisSocketFactory);
}

public BinaryJedis(final JedisSocketFactory jedisSocketFactory, final JedisClientConfig clientConfig) {
client = new Client(jedisSocketFactory);
initializeFromClientConfig(clientConfig);
}

public boolean isConnected() {
return client.isConnected();
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ public Socket createSocket() throws JedisConnectionException {
}
}

@Override
public JedisSocketFactory copyWith(HostAndPort hostAndPort) {
JedisClientConfig config =
DefaultJedisClientConfig.builder()
.connectionTimeoutMillis(this.connectionTimeout)
.socketTimeoutMillis(this.socketTimeout)
.ssl(this.ssl)
.sslSocketFactory(this.sslSocketFactory)
.sslParameters(this.sslParameters)
.hostnameVerifier(this.hostnameVerifier)
.hostAndPortMapper(this.hostAndPortMapper)
.build();
return new DefaultJedisSocketFactory(hostAndPort, config);
}

public HostAndPort getSocketHostAndPort() {
HostAndPortMapper mapper = getHostAndPortMapper();
HostAndPort hostAndPort = getHostAndPort();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,20 @@ public Jedis(final URI uri, JedisClientConfig config) {
super(uri, config);
}

/**
* @deprecated This will constructor will be removed in future releases
*
* Use {@link Jedis#Jedis(redis.clients.jedis.JedisSocketFactory, redis.clients.jedis.JedisClientConfig)}
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
*/
@Deprecated
public Jedis(final JedisSocketFactory jedisSocketFactory) {
super(jedisSocketFactory);
}

public Jedis(final JedisSocketFactory jedisSocketFactory, final JedisClientConfig clientConfig) {
super(jedisSocketFactory, clientConfig);
}

/**
* COPY source destination [DB destination-db] [REPLACE]
*
Expand Down
37 changes: 27 additions & 10 deletions src/main/java/redis/clients/jedis/JedisFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class JedisFactory implements PooledObjectFactory<Jedis> {

private static final Logger logger = LoggerFactory.getLogger(JedisFactory.class);

private final AtomicReference<HostAndPort> hostAndPort = new AtomicReference<>();
private final AtomicReference<JedisSocketFactory> jedisSocketFactory = new AtomicReference<>();
mina-asham marked this conversation as resolved.
Show resolved Hide resolved

private final JedisClientConfig clientConfig;

Expand Down Expand Up @@ -66,20 +66,25 @@ protected JedisFactory(final String host, final int port, final int connectionTi
}

protected JedisFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig) {
this.hostAndPort.set(hostAndPort);
this.clientConfig = DefaultJedisClientConfig.copyConfig(clientConfig);
this.jedisSocketFactory.set(new DefaultJedisSocketFactory(hostAndPort, this.clientConfig));
}

protected JedisFactory(final String host, final int port, final int connectionTimeout, final int soTimeout,
final int infiniteSoTimeout, 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.hostAndPort.set(new HostAndPort(host, port));
this.clientConfig = DefaultJedisClientConfig.builder().connectionTimeoutMillis(connectionTimeout)
.socketTimeoutMillis(soTimeout).blockingSocketTimeoutMillis(infiniteSoTimeout).user(user)
.password(password).database(database).clientName(clientName)
.ssl(ssl).sslSocketFactory(sslSocketFactory)
.sslParameters(sslParameters).hostnameVerifier(hostnameVerifier).build();
this.jedisSocketFactory.set(new DefaultJedisSocketFactory(new HostAndPort(host, port), this.clientConfig));
}

protected JedisFactory(final JedisSocketFactory jedisSocketFactory, final JedisClientConfig clientConfig) {
this.clientConfig = DefaultJedisClientConfig.copyConfig(clientConfig);
this.jedisSocketFactory.set(jedisSocketFactory);
}

/**
Expand Down Expand Up @@ -120,17 +125,28 @@ protected JedisFactory(final URI uri, final int connectionTimeout, final int soT
throw new InvalidURIException(String.format(
"Cannot open Redis connection due invalid URI. %s", uri.toString()));
}
this.hostAndPort.set(new HostAndPort(uri.getHost(), uri.getPort()));
this.clientConfig = DefaultJedisClientConfig.builder().connectionTimeoutMillis(connectionTimeout)
.socketTimeoutMillis(soTimeout).blockingSocketTimeoutMillis(infiniteSoTimeout)
.user(JedisURIHelper.getUser(uri)).password(JedisURIHelper.getPassword(uri))
.database(JedisURIHelper.getDBIndex(uri)).clientName(clientName)
.ssl(JedisURIHelper.isRedisSSLScheme(uri)).sslSocketFactory(sslSocketFactory)
.sslParameters(sslParameters).hostnameVerifier(hostnameVerifier).build();
this.jedisSocketFactory.set(new DefaultJedisSocketFactory(new HostAndPort(uri.getHost(), uri.getPort()), this.clientConfig));
}

public void setHostAndPort(final HostAndPort hostAndPort) {
mina-asham marked this conversation as resolved.
Show resolved Hide resolved
this.hostAndPort.set(hostAndPort);
JedisSocketFactory factory = jedisSocketFactory.get();
// This can only happen if we use a constructor that requires setHostAndPort to be called after,
// in that case we need to give it an initial value, so we use the default socket factory
if (factory == null) {
factory = new DefaultJedisSocketFactory(hostAndPort, this.clientConfig);
// If someone has some logic that can call this code twice in parallel (highly unlikely!)
// this should protect against setting it with the default twice
if (this.jedisSocketFactory.compareAndSet(null, factory)) {
return;
}
}
this.jedisSocketFactory.set(this.jedisSocketFactory.get().copyWith(hostAndPort));
}

public void setPassword(final String password) {
Expand Down Expand Up @@ -167,10 +183,9 @@ public void destroyObject(PooledObject<Jedis> pooledJedis) throws Exception {

@Override
public PooledObject<Jedis> makeObject() throws Exception {
final HostAndPort hostPort = this.hostAndPort.get();
Jedis jedis = null;
try {
jedis = new Jedis(hostPort, clientConfig);
jedis = new Jedis(jedisSocketFactory.get(), clientConfig);
jedis.connect();
return new DefaultPooledObject<>(jedis);
} catch (JedisException je) {
Expand Down Expand Up @@ -199,13 +214,15 @@ public void passivateObject(PooledObject<Jedis> pooledJedis) throws Exception {
public boolean validateObject(PooledObject<Jedis> pooledJedis) {
final BinaryJedis jedis = pooledJedis.getObject();
try {
HostAndPort hostAndPort = this.hostAndPort.get();
JedisSocketFactory factory = this.jedisSocketFactory.get();
String host = factory.getHost();
int port = factory.getPort();

String connectionHost = jedis.getClient().getHost();
int connectionPort = jedis.getClient().getPort();

return hostAndPort.getHost().equals(connectionHost)
&& hostAndPort.getPort() == connectionPort && jedis.isConnected()
return host.equals(connectionHost)
&& port == connectionPort && jedis.isConnected()
&& jedis.ping().equals("PONG");
} catch (final Exception e) {
return false;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/redis/clients/jedis/JedisPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig, final HostAndP
super(poolConfig, new JedisFactory(hostAndPort, clientConfig));
}

public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig, final JedisSocketFactory jedisSocketFactory,
final JedisClientConfig clientConfig) {
super(poolConfig, new JedisFactory(jedisSocketFactory, clientConfig));
}

public JedisPool(final GenericObjectPoolConfig<Jedis> poolConfig) {
this(poolConfig, Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/redis/clients/jedis/JedisSocketFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public interface JedisSocketFactory {
*/
Socket createSocket() throws IOException, JedisConnectionException;

JedisSocketFactory copyWith(HostAndPort hostAndPort);
mina-asham marked this conversation as resolved.
Show resolved Hide resolved

@Deprecated
String getDescription();

Expand Down
6 changes: 6 additions & 0 deletions src/test/java/redis/clients/jedis/tests/UdsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSocketFactory;
import redis.clients.jedis.Protocol;
Expand Down Expand Up @@ -38,6 +39,11 @@ public Socket createSocket() throws JedisConnectionException {
}
}

@Override
public JedisSocketFactory copyWith(HostAndPort hostAndPort) {
return this;
}

@Override
public String getDescription() {
return UDS_SOCKET.toString();
Expand Down