Skip to content

DATAREDIS-462 - Support lettuce ClientResources. #169

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAREDIS-462-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import java.util.concurrent.TimeUnit;

import com.lambdaworks.redis.resource.ClientResources;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
Expand Down Expand Up @@ -50,6 +51,7 @@ public class DefaultLettucePool implements LettucePool, InitializingBean {
private String password;
private long timeout = TimeUnit.MILLISECONDS.convert(60, TimeUnit.SECONDS);
private RedisSentinelConfiguration sentinelConfiguration;
private ClientResources clientResources;

/**
* Constructs a new <code>DefaultLettucePool</code> instance with default settings.
Expand Down Expand Up @@ -101,7 +103,14 @@ public boolean isRedisSentinelAware() {

@SuppressWarnings({ "rawtypes" })
public void afterPropertiesSet() {
this.client = new RedisClient(getRedisURI());

if(clientResources != null) {
this.client = RedisClient.create(clientResources, getRedisURI());
}
else {
this.client = RedisClient.create(getRedisURI());
}

client.setDefaultTimeout(timeout, TimeUnit.MILLISECONDS);
this.internalPool = new GenericObjectPool<RedisAsyncConnection>(new LettuceFactory(client, dbIndex), poolConfig);
}
Expand Down Expand Up @@ -273,6 +282,24 @@ public void setTimeout(long timeout) {
this.timeout = timeout;
}

/**
* Returns the client resources to reuse the client infrastructure.
* @return client resources
* @since 1.7
*/
public ClientResources getClientResources() {
return clientResources;
}

/**
* Sets the client resources to reuse the client infrastructure.
* @param clientResources
* @since 1.7
*/
public void setClientResources(ClientResources clientResources) {
this.clientResources = clientResources;
}

@SuppressWarnings("rawtypes")
private static class LettuceFactory extends BasePooledObjectFactory<RedisAsyncConnection> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.lambdaworks.redis.resource.ClientResources;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.DisposableBean;
Expand Down Expand Up @@ -94,6 +95,7 @@ public class LettuceConnectionFactory implements InitializingBean, DisposableBea
private RedisSentinelConfiguration sentinelConfiguration;
private RedisClusterConfiguration clusterConfiguration;
private ClusterCommandExecutor clusterCommandExecutor;
private ClientResources clientResources;

/**
* Constructs a new <code>LettuceConnectionFactory</code> instance with default settings.
Expand Down Expand Up @@ -377,6 +379,24 @@ public void setPassword(String password) {
this.password = password;
}

/**
* Returns the client resources to reuse the client infrastructure.
* @return client resources
* @since 1.7
*/
public ClientResources getClientResources() {
return clientResources;
}

/**
* Sets the client resources to reuse the client infrastructure.
* @param clientResources
* @since 1.7
*/
public void setClientResources(ClientResources clientResources) {
this.clientResources = clientResources;
}

/**
* Returns the shutdown timeout for shutting down the RedisClient (in milliseconds).
*
Expand Down Expand Up @@ -458,7 +478,11 @@ private AbstractRedisClient createRedisClient() {

if (isRedisSentinelAware()) {
RedisURI redisURI = getSentinelRedisURI();
return new RedisClient(redisURI);
if(clientResources == null) {
return RedisClient.create(redisURI);
}

return RedisClient.create(clientResources, redisURI);
}

if (isClusterAware()) {
Expand All @@ -475,7 +499,13 @@ private AbstractRedisClient createRedisClient() {
initialUris.add(redisURI);
}

RedisClusterClient clusterClient = new RedisClusterClient(initialUris);
RedisClusterClient clusterClient;
if(clientResources == null) {
clusterClient = RedisClusterClient.create(initialUris);
}
else {
clusterClient = RedisClusterClient.create(clientResources, initialUris);
}

this.clusterCommandExecutor = new ClusterCommandExecutor(
new LettuceClusterConnection.LettuceClusterTopologyProvider(clusterClient),
Expand All @@ -493,8 +523,11 @@ private AbstractRedisClient createRedisClient() {
builder.withPassword(password);
}
builder.withTimeout(timeout, TimeUnit.MILLISECONDS);
RedisClient client = new RedisClient(builder.build());
return client;
if(clientResources != null) {
return RedisClient.create(clientResources, builder.build());
}

return RedisClient.create(builder.build());
}

private RedisURI getSentinelRedisURI() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,8 @@

import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisSentinelAsyncConnection;
import com.lambdaworks.redis.RedisURI;
import com.lambdaworks.redis.resource.ClientResources;
import org.springframework.data.redis.ExceptionTranslationStrategy;
import org.springframework.data.redis.FallbackExceptionTranslationStrategy;
import org.springframework.data.redis.connection.NamedNode;
Expand Down Expand Up @@ -51,12 +53,26 @@ public LettuceSentinelConnection(RedisNode sentinel) {

/**
* Creates a {@link LettuceSentinelConnection} with a client for the supplied {@code host} and {@code port}.
* @param host hostname
* @param host hostname must not be {@literal null}
* @param port sentinel port
*/
public LettuceSentinelConnection(String host, int port) {
Assert.notNull(host, "Cannot created LettuceSentinelConnection using 'null' as host.");
redisClient = new RedisClient(host, port);
Assert.notNull(host, "Cannot create LettuceSentinelConnection using 'null' as host.");
redisClient = RedisClient.create(new RedisURI.Builder().redis(host, port).build());
init();
}

/**
* Creates a {@link LettuceSentinelConnection} with a client for the supplied {@code host} and {@code port} and reuse
* existing {@code clientResources}.
* @param clientResources must not be {@literal null}
* @param host hostname must not be {@literal null}
* @param port sentinel port
*/
public LettuceSentinelConnection(ClientResources clientResources, String host, int port) {
Assert.notNull(clientResources, "Cannot create LettuceSentinelConnection using 'null' as ClientResources.");
Assert.notNull(host, "Cannot create LettuceSentinelConnection using 'null' as host.");
redisClient = RedisClient.create(clientResources, new RedisURI.Builder().redis(host, port).build());
init();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,6 +57,7 @@ public void tearDown() {
@Test
public void testGetResource() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
this.pool.setClientResources(TestClientResources.get());
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
Expand All @@ -70,6 +71,7 @@ public void testGetResourcePoolExhausted() {
poolConfig.setMaxTotal(1);
poolConfig.setMaxWaitMillis(1);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
this.pool.setClientResources(TestClientResources.get());
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
Expand All @@ -86,6 +88,7 @@ public void testGetResourceValidate() {
PoolConfig poolConfig = new PoolConfig();
poolConfig.setTestOnBorrow(true);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
this.pool.setClientResources(TestClientResources.get());
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
Expand All @@ -95,6 +98,7 @@ public void testGetResourceValidate() {
@Test(expected = PoolException.class)
public void testGetResourceCreationUnsuccessful() throws Exception {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), 3333);
this.pool.setClientResources(TestClientResources.get());
pool.afterPropertiesSet();
pool.getResource();
}
Expand All @@ -105,6 +109,7 @@ public void testReturnResource() {
poolConfig.setMaxTotal(1);
poolConfig.setMaxWaitMillis(1);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
this.pool.setClientResources(TestClientResources.get());
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
Expand All @@ -119,6 +124,7 @@ public void testReturnBrokenResource() {
poolConfig.setMaxTotal(1);
poolConfig.setMaxWaitMillis(1);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
this.pool.setClientResources(TestClientResources.get());
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
Expand All @@ -136,6 +142,18 @@ public void testReturnBrokenResource() {

@Test
public void testCreateWithDbIndex() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
this.pool.setClientResources(TestClientResources.get());
pool.setDatabase(1);
pool.afterPropertiesSet();
assertNotNull(pool.getResource());
}

/**
* @see DATAREDIS-462
*/
@Test
public void poolWorksWithoutClientResources() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.setDatabase(1);
pool.afterPropertiesSet();
Expand All @@ -145,6 +163,7 @@ public void testCreateWithDbIndex() {
@Test(expected = PoolException.class)
public void testCreateWithDbIndexInvalid() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
this.pool.setClientResources(TestClientResources.get());
pool.setDatabase(17);
pool.afterPropertiesSet();
pool.getResource();
Expand All @@ -153,6 +172,7 @@ public void testCreateWithDbIndexInvalid() {
@Test(expected = PoolException.class)
public void testCreateWithPasswordNoPassword() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
this.pool.setClientResources(TestClientResources.get());
pool.setPassword("notthepassword");
pool.afterPropertiesSet();
pool.getResource();
Expand All @@ -162,6 +182,7 @@ public void testCreateWithPasswordNoPassword() {
@Test
public void testCreatePassword() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
this.pool.setClientResources(TestClientResources.get());
pool.setPassword("foo");
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> conn = pool.getResource();
Expand All @@ -173,6 +194,7 @@ public void testCreatePassword() {
@Test(expected = PoolException.class)
public void testCreateInvalidPassword() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
this.pool.setClientResources(TestClientResources.get());
pool.setPassword("bad");
pool.afterPropertiesSet();
pool.getResource();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,9 +15,29 @@
*/
package org.springframework.data.redis.connection.lettuce;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.redis.connection.ClusterTestVariables.*;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.springframework.data.redis.connection.ClusterTestVariables.CLUSTER_HOST;
import static org.springframework.data.redis.connection.ClusterTestVariables.KEY_1;
import static org.springframework.data.redis.connection.ClusterTestVariables.KEY_2;
import static org.springframework.data.redis.connection.ClusterTestVariables.KEY_3;
import static org.springframework.data.redis.connection.ClusterTestVariables.MASTER_NODE_1_PORT;
import static org.springframework.data.redis.connection.ClusterTestVariables.MASTER_NODE_2_PORT;
import static org.springframework.data.redis.connection.ClusterTestVariables.MASTER_NODE_3_PORT;
import static org.springframework.data.redis.connection.ClusterTestVariables.SAME_SLOT_KEY_1;
import static org.springframework.data.redis.connection.ClusterTestVariables.SAME_SLOT_KEY_2;
import static org.springframework.data.redis.connection.ClusterTestVariables.SAME_SLOT_KEY_3;
import static org.springframework.data.redis.connection.ClusterTestVariables.SLAVEOF_NODE_1_PORT;
import static org.springframework.data.redis.connection.ClusterTestVariables.VALUE_1;
import static org.springframework.data.redis.connection.ClusterTestVariables.VALUE_2;
import static org.springframework.data.redis.connection.ClusterTestVariables.VALUE_3;

import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -82,8 +102,8 @@ public class LettuceClusterConnectionTests implements ClusterConnectionTests {
@Before
public void setUp() {

client = new RedisClusterClient(Builder.redis(CLUSTER_HOST, MASTER_NODE_1_PORT)
.withTimeout(100, TimeUnit.MILLISECONDS).build());
client = RedisClusterClient.create(TestClientResources.get(),
Builder.redis(CLUSTER_HOST, MASTER_NODE_1_PORT).withTimeout(100, TimeUnit.MILLISECONDS).build());
nativeConnection = client.connectCluster();
clusterConnection = new LettuceClusterConnection(client);
}
Expand Down
Loading