From bcc91f6b43b50c909e4b431edc21d921068cfc2d Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 22 Nov 2019 12:59:39 +0100 Subject: [PATCH] DATAREDIS-1062 - Polishing. We now select the database on dedicated connection acquisition only if the current database is different from the default database. Database connections are expected to use the default database index when they are acquired so the database index may be changed for the duration when a connection is in use. We also reset dedicated connections to use the default database index on connection cleanup to bring the connection back into its initial state. Add author tag and tests. Original pull request: #496. --- .../connection/lettuce/LettuceConnection.java | 36 +++++++++++-------- .../LettuceConnectionIntegrationTests.java | 18 +++++++++- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java index dcde81da7..e65c4c5e7 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java @@ -84,6 +84,7 @@ import org.springframework.util.ObjectUtils; * @author David Liu * @author Mark Paluch * @author Ninad Divadkar + * @author Tamil Selvan */ public class LettuceConnection extends AbstractRedisConnection { @@ -451,6 +452,9 @@ public class LettuceConnection extends AbstractRedisConnection { if (asyncDedicatedConn != null) { try { + if (customizedDatabaseIndex()) { + potentiallySelectDatabase(defaultDbIndex); + } connectionProvider.release(asyncDedicatedConn); } catch (RuntimeException ex) { throw convertLettuceAccessException(ex); @@ -946,12 +950,7 @@ public class LettuceConnection extends AbstractRedisConnection { protected RedisClusterAsyncCommands getAsyncDedicatedConnection() { if (asyncDedicatedConn == null) { - asyncDedicatedConn = doGetAsyncDedicatedConnection(); - - if (asyncDedicatedConn instanceof StatefulRedisConnection) { - ((StatefulRedisConnection) asyncDedicatedConn).sync().select(dbIndex); - } } if (asyncDedicatedConn instanceof StatefulRedisConnection) { @@ -965,6 +964,16 @@ public class LettuceConnection extends AbstractRedisConnection { String.format("%s is not a supported connection type.", asyncDedicatedConn.getClass().getName())); } + private boolean customizedDatabaseIndex() { + return defaultDbIndex != dbIndex; + } + + private void potentiallySelectDatabase(int dbIndex) { + if (asyncDedicatedConn instanceof StatefulRedisConnection) { + ((StatefulRedisConnection) asyncDedicatedConn).sync().select(dbIndex); + } + } + @SuppressWarnings("unchecked") private RedisCommands getDedicatedRedisCommands() { return (RedisCommands) getDedicatedConnection(); @@ -973,15 +982,7 @@ public class LettuceConnection extends AbstractRedisConnection { RedisClusterCommands getDedicatedConnection() { if (asyncDedicatedConn == null) { - asyncDedicatedConn = doGetAsyncDedicatedConnection(); - - if (asyncDedicatedConn instanceof StatefulRedisConnection && dbIndex > 0) { - ((StatefulRedisConnection) asyncDedicatedConn).sync().select(dbIndex); - } - else if (asyncDedicatedConn instanceof StatefulRedisConnection) { - ((StatefulRedisConnection) asyncDedicatedConn).sync(); - } } if (asyncDedicatedConn instanceof StatefulRedisConnection) { @@ -997,7 +998,14 @@ public class LettuceConnection extends AbstractRedisConnection { @SuppressWarnings("unchecked") protected StatefulConnection doGetAsyncDedicatedConnection() { - return connectionProvider.getConnection(StatefulConnection.class); + + StatefulConnection connection = connectionProvider.getConnection(StatefulConnection.class); + + if (customizedDatabaseIndex()) { + potentiallySelectDatabase(dbIndex); + } + + return connection; } io.lettuce.core.ScanCursor getScanCursor(long cursorId) { diff --git a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionIntegrationTests.java index 2faabffb3..6b117c2f9 100644 --- a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionIntegrationTests.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -213,18 +214,33 @@ public class LettuceConnectionIntegrationTests extends AbstractConnectionIntegra pool.destroy(); } - @Test + @Test // DATAREDIS-1062 public void testSelectNotShared() { DefaultLettucePool pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort()); + GenericObjectPoolConfig config = new GenericObjectPoolConfig(); + config.setMaxTotal(1); + config.setMaxIdle(1); + pool.setPoolConfig(config); pool.setClientResources(LettuceTestClientResources.getSharedClientResources()); pool.afterPropertiesSet(); LettuceConnectionFactory factory2 = new LettuceConnectionFactory(pool); + factory2.setDatabase(0); factory2.setShutdownTimeout(0); factory2.setShareNativeConnection(false); factory2.afterPropertiesSet(); RedisConnection connection = factory2.getConnection(); + connection.select(2); + connection.rPush("key".getBytes(), "value1".getBytes(), "value2".getBytes()); + List bytes = connection.bLPop(1, "key".getBytes()); + assertThat(bytes).hasSize(2); connection.close(); + + connection = factory2.getConnection(); + assertThat(connection.lLen("key".getBytes())).isEqualTo(0); + connection.select(2); + assertThat(connection.lLen("key".getBytes())).isEqualTo(1); + factory2.destroy(); pool.destroy(); }